CustomScpExplorer.cpp 390 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <algorithm>
  8. #include <Bookmarks.h>
  9. #include <Interface.h>
  10. #include <Exceptions.h>
  11. #include <CoreMain.h>
  12. #include <FileSystems.h>
  13. #include <TextsCore.h>
  14. #include <TextsWin.h>
  15. #include <HelpWin.h>
  16. #include <VCLCommon.h>
  17. #include <Progress.h>
  18. #include <SynchronizeProgress.h>
  19. #include <DragExt.h>
  20. #include <WinApi.h>
  21. #include "GUITools.h"
  22. #include "NonVisual.h"
  23. #include "Glyphs.h"
  24. #include "Tools.h"
  25. #include "WinConfiguration.h"
  26. #include "TerminalManager.h"
  27. #include "EditorManager.h"
  28. #include "ProgParams.h"
  29. #include "Setup.h"
  30. #include <Consts.hpp>
  31. #include <DateUtils.hpp>
  32. #include <TB2Common.hpp>
  33. #include <DirectoryMonitor.hpp>
  34. #include <System.IOUtils.hpp>
  35. //---------------------------------------------------------------------------
  36. #pragma package(smart_init)
  37. #pragma link "CustomDirView"
  38. #pragma link "CustomUnixDirView"
  39. #pragma link "IEListView"
  40. #pragma link "NortonLikeListView"
  41. #pragma link "UnixDirView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "UnixDriveView"
  44. #pragma link "CustomDriveView"
  45. #pragma link "TB2Dock"
  46. #pragma link "TBXStatusBars"
  47. #pragma link "TB2Item"
  48. #pragma link "TB2Toolbar"
  49. #pragma link "ThemePageControl"
  50. #pragma link "PathLabel"
  51. #pragma resource "*.dfm"
  52. //---------------------------------------------------------------------------
  53. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  54. static const int SessionPanelCount = 3;
  55. //---------------------------------------------------------------------------
  56. class TMutexGuard
  57. {
  58. public:
  59. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  60. int Timeout = 5000)
  61. {
  62. FMutex = NULL;
  63. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  64. if (WaitResult == WAIT_TIMEOUT)
  65. {
  66. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  67. }
  68. else
  69. {
  70. FMutex = AMutex;
  71. }
  72. }
  73. ~TMutexGuard()
  74. {
  75. if (FMutex != NULL)
  76. {
  77. ReleaseMutex(FMutex);
  78. }
  79. }
  80. private:
  81. HANDLE FMutex;
  82. };
  83. //---------------------------------------------------------------------------
  84. class TWindowLock
  85. {
  86. public:
  87. TWindowLock(TCustomScpExplorerForm * Form) :
  88. FForm(Form)
  89. {
  90. FForm->LockWindow();
  91. }
  92. ~TWindowLock()
  93. {
  94. FForm->UnlockWindow();
  95. }
  96. private:
  97. TCustomScpExplorerForm * FForm;
  98. };
  99. //---------------------------------------------------------------------------
  100. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  101. FForm(Form)
  102. {
  103. FForm->BatchStart(FBatchStorage);
  104. }
  105. //---------------------------------------------------------------------------
  106. TAutoBatch::~TAutoBatch()
  107. {
  108. FForm->BatchEnd(FBatchStorage);
  109. }
  110. //---------------------------------------------------------------------------
  111. struct TTransferOperationParam
  112. {
  113. TTransferOperationParam();
  114. UnicodeString TargetDirectory;
  115. bool Temp;
  116. bool DragDrop;
  117. int Options;
  118. TAutoSwitch Queue;
  119. };
  120. //---------------------------------------------------------------------------
  121. TTransferOperationParam::TTransferOperationParam()
  122. {
  123. Temp = false;
  124. DragDrop = false;
  125. Options = 0;
  126. Queue = asAuto;
  127. }
  128. //---------------------------------------------------------------------------
  129. class TTransferPresetNoteData : public TObject
  130. {
  131. public:
  132. UnicodeString Message;
  133. };
  134. //---------------------------------------------------------------------------
  135. class TTerminalNoteData : public TObject
  136. {
  137. public:
  138. TManagedTerminal * Terminal;
  139. };
  140. //---------------------------------------------------------------------------
  141. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  142. FFormRestored(false),
  143. TForm(Owner)
  144. {
  145. AddStartupSequence(L"F");
  146. FInvalid = false;
  147. FCurrentSide = osRemote;
  148. FEverShown = false;
  149. FDocks = new TList();
  150. InitControls();
  151. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  152. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  153. NonVisualDataModule->ScpExplorer = this;
  154. RestoreParams();
  155. ConfigurationChanged();
  156. RemoteDirView->Invalidate();
  157. FAutoOperation = false;
  158. FOnFileOperationFinished = NULL;
  159. FPrimaryOperation = foNone;
  160. FForceExecution = false;
  161. FIgnoreNextDialogChar = 0;
  162. FErrorList = NULL;
  163. FSynchronizeProgressForm = NULL;
  164. FProgressForm = NULL;
  165. FRefreshLocalDirectory = false;
  166. FRefreshRemoteDirectory = false;
  167. FDDExtMapFile = NULL;
  168. // CreateMutexW keeps failing with ERROR_NOACCESS
  169. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  170. DebugAssert(FDDExtMutex != NULL);
  171. FDDTargetControl = NULL;
  172. FDelayedDeletionTimer = NULL;
  173. FDelayedDeletionList = new TStringList();
  174. FDDFileList = NULL;
  175. FPendingTempSpaceWarn = false;
  176. FCapturedLog = NULL;
  177. FDragDropOperation = false;
  178. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  179. FAllowTransferPresetAutoSelect = true;
  180. FSessionChanging = false;
  181. FCopyParamDefault = L"";
  182. FSynchronizeController = NULL;
  183. FPendingQueueActionItem = NULL;
  184. FLockLevel = 0;
  185. FLockSuspendLevel = 0;
  186. FDisabledOnLockSuspend = false;
  187. FAlternativeDelete = false;
  188. FTrayIcon = new ::TTrayIcon(0);
  189. FTrayIcon->OnClick = TrayIconClick;
  190. FMaxQueueLength = 0;
  191. FLastContextPopupScreenPoint = TPoint(-1, -1);
  192. FTransferResumeList = NULL;
  193. FDeletedFiles = NULL;
  194. FMoveToQueue = false;
  195. StandaloneOperation = false;
  196. FOnFeedSynchronizeError = NULL;
  197. FOnSynchronizeAbort = NULL;
  198. FSynchronizeTerminal = NULL;
  199. FNeedSession = false;
  200. FDoNotIdleCurrentTerminal = 0;
  201. FIncrementalSearching = 0;
  202. FIncrementalSearchHaveNext = false;
  203. FQueueFileList.reset(new TQueueFileList());
  204. FProgressSide = osCurrent;
  205. FCalculateSizeOperation = NULL;
  206. FDownloadingFromClipboard = false;
  207. FClipboardFakeMonitorsPendingReset = false;
  208. FEditorManager = new TEditorManager();
  209. FEditorManager->OnFileChange = ExecutedFileChanged;
  210. FEditorManager->OnFileReload = ExecutedFileReload;
  211. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  212. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  213. FLocalEditors = new TList();
  214. FQueueStatus = NULL;
  215. FQueueStatusSection = new TCriticalSection();
  216. FQueueStatusInvalidated = false;
  217. FQueueItemInvalidated = false;
  218. FQueueStatusUpdating = false;
  219. FQueueActedItem = NULL;
  220. FQueueController = new TQueueController(QueueView3);
  221. FUserActionTimer = new TTimer(this);
  222. FUserActionTimer->Enabled = false;
  223. FUserActionTimer->Interval = 10;
  224. FUserActionTimer->OnTimer = UserActionTimer;
  225. FNotes = new TStringList();
  226. FNoteTimer = new TTimer(this);
  227. FNoteTimer->Enabled = false;
  228. FNoteTimer->OnTimer = NoteTimer;
  229. FOnNoteClick = NULL;
  230. FOle32Library = LoadLibrary(L"Ole32.dll");
  231. FDragMoveCursor = FOle32Library != NULL ?
  232. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  233. UseSystemSettings(this);
  234. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  235. static_cast<TObject*>(GetComponent(fcTransferList)));
  236. DebugAssert(TransferList != NULL);
  237. FTransferListHoverIndex = -1;
  238. TransferList->OnChange = TransferListChange;
  239. TransferList->OnDrawItem = TransferListDrawItem;
  240. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  241. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  242. UseDesktopFont(SessionsPageControl);
  243. UpdateSessionsPageControlHeight();
  244. UseDesktopFont(RemoteDirView);
  245. UseDesktopFont(RemoteDriveView);
  246. UseDesktopFont(QueueView3);
  247. UseDesktopFont(QueueFileList);
  248. UseDesktopFont(QueueLabel);
  249. UseDesktopFont(RemoteStatusBar);
  250. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  251. QueueSplitter->ShowHint = true;
  252. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  253. QueueFileListSplitter->ShowHint = true;
  254. RemotePanelSplitter->ShowHint = true;
  255. UpdateImages();
  256. FCustomCommandMenu = CreateTBXPopupMenu(this);
  257. FCustomCommandLocalFileList = NULL;
  258. FCustomCommandRemoteFileList = NULL;
  259. FSessionColors = new TPngImageList(this);
  260. FSessionColors->ColorDepth = cd32Bit;
  261. AddFixedSessionImages();
  262. SessionsPageControl->Images = FSessionColors;
  263. UpdateQueueLabel();
  264. CheckStoreTransition();
  265. CreateHiddenWindow();
  266. StartUpdates();
  267. }
  268. //---------------------------------------------------------------------------
  269. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  270. {
  271. FInvalid = true;
  272. // this has to be one of the very first things to do
  273. StopUpdateThread();
  274. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  275. TTerminalManager::Instance()->ScpExplorer = NULL;
  276. delete FCustomCommandLocalFileList;
  277. delete FCustomCommandRemoteFileList;
  278. delete FCustomCommandMenu;
  279. delete FTrayIcon;
  280. FTrayIcon = NULL;
  281. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  282. delete FEditorManager;
  283. ForceCloseLocalEditors();
  284. delete FLocalEditors;
  285. HideFileFindDialog();
  286. if (FDelayedDeletionTimer)
  287. {
  288. DoDelayedDeletion(NULL);
  289. SAFE_DESTROY(FDelayedDeletionTimer);
  290. }
  291. SAFE_DESTROY(FDelayedDeletionList);
  292. // sometimes we do not get DDEnd so the list is not released
  293. SAFE_DESTROY(FDDFileList);
  294. DebugAssert(FSynchronizeController == NULL);
  295. CloseHandle(FDDExtMutex);
  296. FDDExtMutex = NULL;
  297. FreeLibrary(FOle32Library);
  298. FOle32Library = NULL;
  299. FDragMoveCursor = NULL;
  300. DebugAssert(!FErrorList);
  301. if (FEverShown)
  302. {
  303. // when window is never shown (like when running command-line operation),
  304. // particularly window site is not restored correctly (BoundsRect value set
  305. // in RestoreForm gets lost during handle allocation), so we do not want
  306. // it to be stored
  307. StoreParams();
  308. }
  309. ManagedSession = NULL;
  310. Queue = NULL;
  311. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  312. NonVisualDataModule->ScpExplorer = NULL;
  313. delete FQueueController;
  314. FQueueController = NULL;
  315. delete FQueueStatusSection;
  316. FQueueStatusSection = NULL;
  317. delete FQueueStatus;
  318. FQueueStatus = NULL;
  319. delete FUserActionTimer;
  320. FUserActionTimer = NULL;
  321. delete FNoteTimer;
  322. delete FNotes;
  323. delete FNoteData;
  324. SAFE_DESTROY(FDocks);
  325. SessionsPageControl->Images = NULL;
  326. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  327. SAFE_DESTROY(FSessionColors);
  328. SAFE_DESTROY(FSessionsDragDropFilesEx);
  329. SAFE_DESTROY(FHistoryMenu[0][0]);
  330. SAFE_DESTROY(FHistoryMenu[0][1]);
  331. SAFE_DESTROY(FHistoryMenu[1][0]);
  332. SAFE_DESTROY(FHistoryMenu[1][1]);
  333. if (FHiddenWindow != NULL)
  334. {
  335. DestroyWindow(FHiddenWindow);
  336. FHiddenWindow = NULL;
  337. }
  338. }
  339. //---------------------------------------------------------------------------
  340. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  341. {
  342. std::unique_ptr<TSessionData> Data;
  343. if (!Session.IsEmpty())
  344. {
  345. bool DefaultsOnly;
  346. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  347. }
  348. TTerminalManager * Manager = TTerminalManager::Instance();
  349. for (int Index = 0; Index < Manager->Count; Index++)
  350. {
  351. TTerminal * Terminal = Manager->Sessions[Index];
  352. if (Session.IsEmpty() ||
  353. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  354. {
  355. if (Path.IsEmpty())
  356. {
  357. Terminal->ClearCaches();
  358. }
  359. else
  360. {
  361. Terminal->DirectoryModified(Path, true);
  362. }
  363. }
  364. }
  365. // We should flag a pending refresh for the background terminals or busy foreground terminals
  366. if (HasActiveTerminal() &&
  367. CanCommandLineFromAnotherInstance() &&
  368. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  369. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  370. {
  371. Terminal->ReloadDirectory();
  372. }
  373. }
  374. //---------------------------------------------------------------------------
  375. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  376. {
  377. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  378. size_t MessageSize = sizeof(TCopyDataMessage);
  379. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  380. if (Result)
  381. {
  382. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  383. Result = (Message.Version == TCopyDataMessage::Version1);
  384. if (Result)
  385. {
  386. switch (Message.Command)
  387. {
  388. case TCopyDataMessage::CommandCanCommandLine:
  389. Result = CanCommandLineFromAnotherInstance();
  390. break;
  391. case TCopyDataMessage::CommandCommandLine:
  392. {
  393. UnicodeString CommandLine(Message.CommandLine);
  394. Result = CommandLineFromAnotherInstance(CommandLine);
  395. }
  396. break;
  397. case TCopyDataMessage::RefreshPanel:
  398. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  399. break;
  400. case TCopyDataMessage::MainWindowCheck:
  401. Result = true;
  402. break;
  403. default:
  404. Result = false;
  405. break;
  406. }
  407. }
  408. }
  409. Message.Result = Result ? 1 : 0;
  410. }
  411. //---------------------------------------------------------------------------
  412. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  413. {
  414. WNDCLASS WindowClass = {0};
  415. WindowClass.lpfnWndProc = DefWindowProc;
  416. WindowClass.hInstance = HInstance;
  417. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  418. FHiddenWindow = NULL;
  419. if (RegisterClass(&WindowClass))
  420. {
  421. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  422. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  423. }
  424. }
  425. //---------------------------------------------------------------------------
  426. bool __fastcall TCustomScpExplorerForm::CanConsole()
  427. {
  428. return
  429. HasActiveTerminal() &&
  430. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  431. }
  432. //---------------------------------------------------------------------------
  433. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  434. {
  435. bool Result = Visible && !NonVisualDataModule->Busy;
  436. return Result;
  437. }
  438. //---------------------------------------------------------------------------
  439. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  440. const UnicodeString & CommandLine)
  441. {
  442. TProgramParams Params(CommandLine);
  443. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  444. if (Result)
  445. {
  446. NonVisualDataModule->StartBusy();
  447. try
  448. {
  449. // this action is initiated from another process,
  450. // so it's likely that our window is not visible,
  451. // and user won't see what is going on
  452. Application->BringToFront();
  453. // reload sessions as we may be asked to open a session
  454. // just stored by another instance
  455. StoredSessions->Reload();
  456. UnicodeString SessionName = Params.Param[1];
  457. std::unique_ptr<TObjectList> DataList(new TObjectList());
  458. try
  459. {
  460. UnicodeString DownloadFile; // unused
  461. int Flags = GetCommandLineParseUrlFlags(&Params);
  462. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  463. }
  464. catch (EAbort &)
  465. {
  466. // Happens when opening session in PuTTY and in other situations,
  467. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  468. // to process the command-line on its own (or to pass it to yet another instance)
  469. }
  470. if (DataList->Count > 0)
  471. {
  472. TTerminalManager * Manager = TTerminalManager::Instance();
  473. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  474. }
  475. }
  476. __finally
  477. {
  478. NonVisualDataModule->EndBusy();
  479. }
  480. }
  481. return Result;
  482. }
  483. //---------------------------------------------------------------------------
  484. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  485. {
  486. TManagedTerminal * Result;
  487. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  488. {
  489. Result = FManagedSession;
  490. }
  491. else
  492. {
  493. Result = NULL;
  494. }
  495. return Result;
  496. }
  497. //---------------------------------------------------------------------------
  498. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  499. {
  500. if (FManagedSession != value)
  501. {
  502. SessionChanging();
  503. DoSetManagedSession(value, false);
  504. }
  505. }
  506. //---------------------------------------------------------------------------
  507. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  508. {
  509. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  510. FManagedSession = value;
  511. {
  512. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  513. FAllowTransferPresetAutoSelect = false;
  514. TAutoFlag SessionChangingFlag(FSessionChanging);
  515. SessionChanged(Replace);
  516. }
  517. if (Terminal != NULL)
  518. {
  519. TransferPresetAutoSelect();
  520. }
  521. // Update app and tab titles, prevented above by FSessionChanging.
  522. UpdateControls();
  523. }
  524. //---------------------------------------------------------------------------
  525. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  526. {
  527. DoSetManagedSession(value, true);
  528. }
  529. //---------------------------------------------------------------------------
  530. void __fastcall TCustomScpExplorerForm::SessionChanging()
  531. {
  532. if (ManagedSession != NULL)
  533. {
  534. UpdateSession(ManagedSession);
  535. }
  536. }
  537. //---------------------------------------------------------------------------
  538. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  539. {
  540. if (ManagedSession != NULL)
  541. {
  542. UpdateSessionColor(static_cast<TColor>(ManagedSession->StateData->Color));
  543. }
  544. if (!TTerminalManager::Instance()->Updating)
  545. {
  546. SessionListChanged();
  547. }
  548. if (Terminal != NULL)
  549. {
  550. TObject * State = NULL;
  551. if (WinConfiguration->PreservePanelState)
  552. {
  553. State = Terminal->RemoteExplorerState;
  554. DirView(osRemote)->AnnounceState(State);
  555. }
  556. try
  557. {
  558. if (Replaced)
  559. {
  560. RemoteDirView->ReplaceTerminal(Terminal);
  561. }
  562. else
  563. {
  564. RemoteDirView->Terminal = Terminal;
  565. }
  566. if (Terminal->Active)
  567. {
  568. Terminal->RefreshDirectory();
  569. }
  570. if (State != NULL)
  571. {
  572. DirView(osRemote)->RestoreState(State);
  573. }
  574. }
  575. __finally
  576. {
  577. DirView(osRemote)->AnnounceState(NULL);
  578. }
  579. }
  580. else
  581. {
  582. RemoteDirView->Terminal = NULL;
  583. }
  584. InitStatusBar();
  585. UpdateTransferList();
  586. // Update panels Enable state before refreshing the labels
  587. UpdateControls();
  588. }
  589. //---------------------------------------------------------------------------
  590. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  591. {
  592. if (Queue != value)
  593. {
  594. if (FQueue != NULL)
  595. {
  596. FQueue->OnListUpdate = NULL;
  597. FQueue->OnQueueItemUpdate = NULL;
  598. }
  599. FQueue = value;
  600. if (FQueue != NULL)
  601. {
  602. DebugAssert(FQueue->OnListUpdate == NULL);
  603. FQueue->OnListUpdate = QueueListUpdate;
  604. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  605. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  606. }
  607. QueueChanged();
  608. }
  609. }
  610. //---------------------------------------------------------------------------
  611. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  612. TListItem * Item)
  613. {
  614. if (FQueueActedItem == Item)
  615. {
  616. FQueueActedItem = NULL;
  617. if ((QueueView3->PopupMenu != NULL) &&
  618. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  619. {
  620. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  621. ReleaseCapture();
  622. }
  623. }
  624. if (Item->Data == FPendingQueueActionItem)
  625. {
  626. FPendingQueueActionItem = NULL;
  627. }
  628. }
  629. //---------------------------------------------------------------------------
  630. bool TCustomScpExplorerForm::IsAnythingQueued()
  631. {
  632. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  633. }
  634. //---------------------------------------------------------------------------
  635. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  636. {
  637. {
  638. TGuard Guard(FQueueStatusSection);
  639. FQueueStatusInvalidated = false;
  640. if (FQueue != NULL)
  641. {
  642. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  643. }
  644. }
  645. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  646. {
  647. FMaxQueueLength = FQueueStatus->Count;
  648. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  649. }
  650. {
  651. TAutoFlag Flag(FQueueStatusUpdating);
  652. FQueueController->UpdateQueueStatus(FQueueStatus);
  653. }
  654. UpdateQueueFileList();
  655. SetQueueProgress();
  656. UpdateQueueView();
  657. bool IsEmpty = !IsAnythingQueued();
  658. if (IsEmpty && (Terminal != NULL))
  659. {
  660. TOnceDoneOperation OnceDoneOperation =
  661. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  662. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  663. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  664. {
  665. FQueue->Enabled = false;
  666. }
  667. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  668. {
  669. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  670. }
  671. }
  672. }
  673. //---------------------------------------------------------------------------
  674. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  675. {
  676. UnicodeString Result;
  677. if (FQueueStatus != NULL)
  678. {
  679. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  680. {
  681. TFileOperationProgressType * ProgressData =
  682. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  683. if ((ProgressData != NULL) && ProgressData->InProgress)
  684. {
  685. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  686. }
  687. }
  688. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  689. {
  690. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  691. }
  692. }
  693. return Result;
  694. }
  695. //---------------------------------------------------------------------------
  696. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  697. {
  698. ComponentVisible[fcQueueView] =
  699. !IsLocalBrowserMode() &&
  700. ((WinConfiguration->QueueView.Show == qvShow) ||
  701. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  702. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  703. }
  704. //---------------------------------------------------------------------------
  705. void __fastcall TCustomScpExplorerForm::QueueChanged()
  706. {
  707. if (FQueueStatus != NULL)
  708. {
  709. // UpdateFileList implementation relies on the status being recreated when session changes
  710. delete FQueueStatus;
  711. FQueueStatus = NULL;
  712. }
  713. UpdateQueueStatus(true);
  714. }
  715. //---------------------------------------------------------------------------
  716. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  717. {
  718. if (FQueue == Queue)
  719. {
  720. FQueueStatusInvalidated = true;
  721. }
  722. }
  723. //---------------------------------------------------------------------------
  724. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  725. TQueueItem * Item)
  726. {
  727. if (FQueue == Queue)
  728. {
  729. TGuard Guard(FQueueStatusSection);
  730. // this may be running in parallel with QueueChanged
  731. if (FQueueStatus != NULL)
  732. {
  733. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  734. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  735. {
  736. FRefreshLocalDirectory = (QueueItem == NULL) ||
  737. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  738. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  739. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  740. }
  741. if (QueueItem != NULL)
  742. {
  743. QueueItem->UserData = (void*)true;
  744. FQueueItemInvalidated = true;
  745. }
  746. }
  747. }
  748. }
  749. //---------------------------------------------------------------------------
  750. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  751. {
  752. // during standalone editing, we have no way to see/control queue,
  753. // so we have to always popup prompts automatically
  754. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  755. }
  756. //---------------------------------------------------------------------------
  757. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  758. {
  759. if (FQueueStatus != NULL)
  760. {
  761. bool QueueAutoPopup = IsQueueAutoPopup();
  762. bool NeedRefresh = FQueueController->NeedRefresh();
  763. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  764. FQueueItemInvalidated = false;
  765. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  766. bool Updated = false;
  767. TQueueItemProxy * QueueItem;
  768. bool WasUserAction;
  769. for (int Index = 0; Index < Limit; Index++)
  770. {
  771. bool Update = false;
  772. QueueItem = FQueueStatus->Items[Index];
  773. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  774. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  775. (FPendingQueueActionItem == NULL))
  776. {
  777. FPendingQueueActionItem = QueueItem;
  778. FUserActionTimer->Enabled = true;
  779. }
  780. if ((bool)QueueItem->UserData)
  781. {
  782. QueueItem->UserData = (void*)false;
  783. QueueItem->Update();
  784. Updated = true;
  785. Update = true;
  786. }
  787. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  788. {
  789. Update = true;
  790. }
  791. if (Update)
  792. {
  793. FQueueController->RefreshQueueItem(QueueItem);
  794. }
  795. }
  796. if (Updated)
  797. {
  798. NonVisualDataModule->UpdateNonVisibleActions();
  799. SetQueueProgress();
  800. UpdateQueueFileList();
  801. }
  802. }
  803. }
  804. //---------------------------------------------------------------------------
  805. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  806. {
  807. if (FTaskbarList != NULL)
  808. {
  809. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  810. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  811. }
  812. }
  813. //---------------------------------------------------------------------------
  814. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  815. {
  816. if (Progress >= 0)
  817. {
  818. if (FTaskbarList != NULL)
  819. {
  820. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  821. }
  822. }
  823. else
  824. {
  825. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  826. }
  827. }
  828. //---------------------------------------------------------------------------
  829. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  830. {
  831. int OverallProgress;
  832. // FProgressForm is null when this is called from SetQueueProgress
  833. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  834. {
  835. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  836. }
  837. else if (!ProgressData->IsIndeterminate())
  838. {
  839. OverallProgress = ProgressData->OverallProgress();
  840. }
  841. else
  842. {
  843. OverallProgress = -1;
  844. }
  845. SetTaskbarListProgressValue(OverallProgress);
  846. }
  847. //---------------------------------------------------------------------------
  848. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  849. {
  850. TTerminalManager::Instance()->QueueStatusUpdated();
  851. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  852. {
  853. // Do not animate task bar button, when items are queued, but processing is disabled
  854. if ((FQueueStatus != NULL) && (FQueueStatus->ActivePrimaryCount > 0))
  855. {
  856. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  857. {
  858. TFileOperationProgressType * ProgressData = NULL;
  859. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  860. {
  861. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  862. }
  863. if ((ProgressData != NULL) &&
  864. ProgressData->InProgress)
  865. {
  866. SetTaskbarListProgressValue(ProgressData);
  867. }
  868. else
  869. {
  870. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  871. }
  872. }
  873. else
  874. {
  875. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  876. }
  877. }
  878. else
  879. {
  880. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  881. }
  882. }
  883. UpdateQueueLabel();
  884. }
  885. //---------------------------------------------------------------------------
  886. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  887. {
  888. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  889. if (FQueueStatus != NULL)
  890. {
  891. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  892. if (ActiveAndPendingPrimaryCount > 0)
  893. {
  894. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  895. }
  896. }
  897. QueueLabel->Caption = Caption;
  898. }
  899. //---------------------------------------------------------------------------
  900. void TCustomScpExplorerForm::CheckStoreTransition()
  901. {
  902. if (WinConfiguration->StoreTransition == stInit)
  903. {
  904. if (IsUWP())
  905. {
  906. WinConfiguration->StoreTransition = stStoreFresh;
  907. AppLog(L"Fresh UWP/store installation");
  908. }
  909. else
  910. {
  911. WinConfiguration->StoreTransition = stStandard;
  912. AppLog(L"Fresh standard installation");
  913. }
  914. }
  915. else if ((WinConfiguration->StoreTransition == stStandard) && IsUWP())
  916. {
  917. WinConfiguration->StoreTransition = stStoreMigrated;
  918. WinConfiguration->FirstRun = WinConfiguration->FirstRun + L"=>" + StandardDatestamp();
  919. AppLog(L"Standard installation migrated to store installation");
  920. }
  921. if (WinConfiguration->StoreTransition == stStoreMigrated)
  922. {
  923. TTBXToolbar * Toolbar = new TTBXToolbar(MessageDock);
  924. Toolbar->Parent = MessageDock;
  925. Toolbar->FullSize = true;
  926. Toolbar->Color = Application->HintColor;
  927. TTBXLabelItem * LabelItem;
  928. LabelItem = new TTBXLabelItem(Toolbar);
  929. LabelItem->Margin = ScaleByTextHeight(MessageDock, 2); // Is this automatically rescaled?
  930. LabelItem->Caption = LoadStr(STORE_MIGRATION_THANKS);
  931. Toolbar->Items->Add(LabelItem);
  932. TTBXCustomItem * LinkItem = new TTBXItem(Toolbar);
  933. LinkItem->FontSettings->Color = LinkColor;
  934. LinkItem->Caption = LoadStr(STORE_MIGRATION_LEARN);
  935. LinkItem->OnClick = StoreTransitionLinkClick;
  936. Toolbar->Items->Add(LinkItem);
  937. AddMenuSeparator(Toolbar->Items);
  938. TTBCustomItem * CloseItem = new TTBXItem(Toolbar);
  939. CloseItem->Caption = LoadStr(MESSAGE_DISMISS);
  940. CloseItem->OnClick = StoreTransitionCloseClick;
  941. Toolbar->Items->Add(CloseItem);
  942. GiveTBItemPriority(CloseItem);
  943. }
  944. }
  945. //---------------------------------------------------------------------------
  946. void __fastcall TCustomScpExplorerForm::StoreTransitionLinkClick(TObject *)
  947. {
  948. ShowHelp(HELP_STORE_TRANSITION);
  949. }
  950. //---------------------------------------------------------------------------
  951. void __fastcall TCustomScpExplorerForm::StoreTransitionCloseClick(TObject * Sender)
  952. {
  953. TTBCustomItem * Item = DebugNotNull(dynamic_cast<TTBCustomItem *>(Sender));
  954. TTBXToolbar * Toolbar = DebugNotNull(dynamic_cast<TTBXToolbar *>(DebugNotNull(DebugNotNull(Item->Parent)->ParentComponent)));
  955. delete Toolbar;
  956. WinConfiguration->StoreTransition = stStoreAcknowledged;
  957. UpdateControls();
  958. }
  959. //---------------------------------------------------------------------------
  960. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  961. {
  962. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  963. static_cast<TComponent*>(GetComponent(fcTransferList)));
  964. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  965. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  966. TransferList->Strings->BeginUpdate();
  967. try
  968. {
  969. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  970. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  971. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  972. if (FTransferDropDownHint.IsEmpty())
  973. {
  974. FTransferDropDownHint = TransferDropDown->Hint;
  975. }
  976. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  977. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  978. TransferDropDown->Text = StripHotkey(Name);
  979. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  980. (FTransferDropDownHint, StripHotkey(Name),
  981. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  982. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  983. // update the label, otherwise when it is updated only on the first draw
  984. // of the list, it is drawn "bold" for some reason
  985. FTransferListHoverIndex = TransferList->ItemIndex;
  986. UpdateTransferLabel();
  987. }
  988. __finally
  989. {
  990. TransferList->Strings->EndUpdate();
  991. }
  992. }
  993. //---------------------------------------------------------------------------
  994. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  995. {
  996. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  997. DebugAssert(Toolbar != NULL);
  998. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  999. }
  1000. //---------------------------------------------------------------------------
  1001. void __fastcall TCustomScpExplorerForm::UpdateActions()
  1002. {
  1003. TForm::UpdateActions();
  1004. if (ComponentVisible[fcCustomCommandsBand])
  1005. {
  1006. UpdateCustomCommandsToolbar();
  1007. }
  1008. }
  1009. //---------------------------------------------------------------------------
  1010. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  1011. {
  1012. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  1013. }
  1014. //---------------------------------------------------------------------------
  1015. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  1016. {
  1017. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  1018. {
  1019. DirView->RowSelect = WinConfiguration->FullRowSelect;
  1020. // Without this, the panel turns gray and unused part of header turns black
  1021. DirView->Perform(CM_COLORCHANGED, 0, 0);
  1022. }
  1023. }
  1024. //---------------------------------------------------------------------------
  1025. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  1026. {
  1027. Color = GetBtnFaceColor();
  1028. DebugAssert(Configuration && RemoteDirView);
  1029. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1030. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  1031. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  1032. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  1033. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1034. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1035. RemoteDirView->AlwaysSortDirectoriesByName = WinConfiguration->AlwaysSortDirectoriesByName;
  1036. UpdateRowSelect(RemoteDirView);
  1037. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1038. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  1039. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  1040. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1041. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1042. UpdateSessionsPageControlHeight();
  1043. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1044. UpdateToolbarDisplayMode();
  1045. UpdateTransferList();
  1046. if (Terminal != NULL)
  1047. {
  1048. TransferPresetAutoSelect();
  1049. }
  1050. UpdateQueueView();
  1051. UpdateControls();
  1052. // this can be called even before constuctor finishes.
  1053. if (FEditorManager != NULL)
  1054. {
  1055. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  1056. }
  1057. // this can be called even before constuctor finishes.
  1058. if (FLocalEditors != NULL)
  1059. {
  1060. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  1061. {
  1062. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  1063. }
  1064. }
  1065. if (ComponentVisible[fcCustomCommandsBand])
  1066. {
  1067. UpdateCustomCommandsToolbar();
  1068. }
  1069. // show only when keeping queue items forever,
  1070. // otherwise, it's enough to have it in the context menu
  1071. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1072. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1073. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1074. {
  1075. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1076. FFileColorsCurrent = WinConfiguration->FileColors;
  1077. FileColorsChanged();
  1078. }
  1079. }
  1080. //---------------------------------------------------------------------------
  1081. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1082. {
  1083. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1084. DirView->Invalidate();
  1085. }
  1086. //---------------------------------------------------------------------------
  1087. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1088. {
  1089. DoFileColorsChanged(RemoteDirView);
  1090. }
  1091. //---------------------------------------------------------------------------
  1092. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1093. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1094. void * /*Arg*/)
  1095. {
  1096. if (Token != NULL)
  1097. {
  1098. TForm * Editor = dynamic_cast<TForm*>(Token);
  1099. DebugAssert(Editor != NULL);
  1100. ReconfigureEditorForm(Editor);
  1101. }
  1102. }
  1103. //---------------------------------------------------------------------------
  1104. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1105. {
  1106. WinConfiguration->DDTransferConfirmation = asOn;
  1107. }
  1108. //---------------------------------------------------------------------------
  1109. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1110. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1111. {
  1112. // noop
  1113. }
  1114. //---------------------------------------------------------------------------
  1115. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1116. {
  1117. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1118. }
  1119. //---------------------------------------------------------------------------
  1120. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1121. {
  1122. if (DoNotShowAgain)
  1123. {
  1124. if (DragDrop)
  1125. {
  1126. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1127. {
  1128. PopupTrayBalloon(
  1129. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1130. }
  1131. WinConfiguration->DDTransferConfirmation = asOff;
  1132. }
  1133. else
  1134. {
  1135. WinConfiguration->ConfirmTransferring = false;
  1136. }
  1137. }
  1138. else
  1139. {
  1140. // User explicitly unchecked "do not show again",
  1141. // so show him the dialog the next time
  1142. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1143. {
  1144. WinConfiguration->DDTransferConfirmation = asOn;
  1145. }
  1146. }
  1147. }
  1148. //---------------------------------------------------------------------------
  1149. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1150. TTransferDirection Direction, TTransferType Type, bool Temp,
  1151. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1152. bool Confirm, bool DragDrop, int Options)
  1153. {
  1154. bool Result = true;
  1155. DebugAssert(HasActiveTerminal());
  1156. // these parameters are known in advance
  1157. int Params =
  1158. FLAGMASK(Type == ttMove, cpDelete);
  1159. bool ToTemp = (Temp && (Direction == tdToLocal));
  1160. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1161. {
  1162. bool DisableNewerOnly =
  1163. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1164. ToTemp;
  1165. Options |=
  1166. FLAGMASK(ToTemp, coTemp) |
  1167. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1168. coDoNotShowAgain;
  1169. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1170. int CopyParamAttrs = (Direction == tdToRemote ?
  1171. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1172. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1173. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1174. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1175. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1176. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1177. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1178. if (Result)
  1179. {
  1180. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1181. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1182. }
  1183. }
  1184. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1185. {
  1186. Configuration->Usage->Inc(L"TransfersOnBackground");
  1187. // these parameter are known only after transfer dialog
  1188. Params |=
  1189. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1190. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1191. Result = false;
  1192. ClearTransferSourceSelection(Direction);
  1193. }
  1194. return Result;
  1195. }
  1196. //---------------------------------------------------------------------------
  1197. void TCustomScpExplorerForm::ClearOperationSelection(TOperationSide Side)
  1198. {
  1199. if (FOnFileOperationFinished != NULL)
  1200. {
  1201. FOnFileOperationFinished(Side, UnicodeString(), true);
  1202. }
  1203. else
  1204. {
  1205. if (HasDirView[Side])
  1206. {
  1207. DirView(Side)->SelectAll(smNone);
  1208. }
  1209. }
  1210. }
  1211. //---------------------------------------------------------------------------
  1212. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1213. {
  1214. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1215. ClearOperationSelection(Side);
  1216. }
  1217. //---------------------------------------------------------------------------
  1218. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1219. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1220. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1221. int Params)
  1222. {
  1223. DebugAssert(Queue != NULL);
  1224. bool SingleFile = false;
  1225. if (FileList->Count == 1)
  1226. {
  1227. if (Direction == tdToRemote)
  1228. {
  1229. UnicodeString FileName = FileList->Strings[0];
  1230. SingleFile = FileExists(ApiPath(FileName));
  1231. }
  1232. else
  1233. {
  1234. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1235. SingleFile = !File->IsDirectory;
  1236. }
  1237. }
  1238. TQueueItem * QueueItem;
  1239. if (Direction == tdToRemote)
  1240. {
  1241. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1242. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1243. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1244. }
  1245. else
  1246. {
  1247. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1248. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1249. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1250. }
  1251. AddQueueItem(Queue, QueueItem, Terminal);
  1252. }
  1253. //---------------------------------------------------------------------------
  1254. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1255. {
  1256. if (Queue->IsEmpty)
  1257. {
  1258. Terminal->QueueOperationStart = Now();
  1259. }
  1260. Queue->AddItem(QueueItem);
  1261. }
  1262. //---------------------------------------------------------------------------
  1263. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1264. {
  1265. }
  1266. //---------------------------------------------------------------------------
  1267. void __fastcall TCustomScpExplorerForm::InitControls()
  1268. {
  1269. DebugAssert(FDocks != NULL);
  1270. for (int Index = 0; Index < ComponentCount; Index++)
  1271. {
  1272. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1273. if ((Dock != NULL) && (Dock->Tag == 0))
  1274. {
  1275. FDocks->Add(Dock);
  1276. }
  1277. }
  1278. CollectItemsWithTextDisplayMode(this);
  1279. }
  1280. //---------------------------------------------------------------------------
  1281. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1282. {
  1283. ConfigurationChanged();
  1284. QueuePanel->Height =
  1285. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1286. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1287. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1288. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1289. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1290. QueueFileList->Height =
  1291. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1292. if (QueueFileList->Visible)
  1293. {
  1294. AdjustQueueLayout();
  1295. }
  1296. UpdateCustomCommandsToolbar();
  1297. }
  1298. //---------------------------------------------------------------------------
  1299. void __fastcall TCustomScpExplorerForm::StoreParams()
  1300. {
  1301. int APixelsPerInch = GetControlPixelsPerInch(this);
  1302. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1303. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1304. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1305. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1306. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1307. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1308. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1309. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1310. }
  1311. //---------------------------------------------------------------------------
  1312. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1313. {
  1314. if (!FFormRestored)
  1315. {
  1316. FFormRestored = true;
  1317. RestoreFormParams();
  1318. }
  1319. TForm::CreateParams(Params);
  1320. }
  1321. //---------------------------------------------------------------------------
  1322. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1323. {
  1324. DebugAssert(FDocks != NULL);
  1325. for (int Index = 0; Index < FDocks->Count; Index++)
  1326. {
  1327. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1328. }
  1329. }
  1330. //---------------------------------------------------------------------------
  1331. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1332. {
  1333. SetDockAllowDrag(true);
  1334. while (!ButtonsStr.IsEmpty())
  1335. {
  1336. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1337. UnicodeString Name = CutToChar(S, L'=', true);
  1338. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1339. {
  1340. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1341. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1342. {
  1343. while (!S.IsEmpty())
  1344. {
  1345. UnicodeString S2 = CutToChar(S, L',', true);
  1346. UnicodeString Name = CutToChar(S2, L':', true);
  1347. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1348. {
  1349. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1350. if (SameText(GetToolbarItemName(Item), Name))
  1351. {
  1352. // forward compatibility
  1353. UnicodeString S3 = CutToChar(S2, L':', true);
  1354. bool Visible = StrToIntDef(S3, 0);
  1355. Item->Visible = Visible;
  1356. }
  1357. }
  1358. }
  1359. }
  1360. }
  1361. }
  1362. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1363. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1364. }
  1365. //---------------------------------------------------------------------------
  1366. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1367. {
  1368. UnicodeString Result;
  1369. SetDockAllowDrag(true);
  1370. Result = ::GetToolbarsLayoutStr(this);
  1371. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1372. return Result;
  1373. }
  1374. //---------------------------------------------------------------------------
  1375. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1376. {
  1377. UnicodeString Result;
  1378. if (Item->Action != NULL)
  1379. {
  1380. Result = Item->Action->Name;
  1381. Result = RemoveSuffix(Result, L"Action", true);
  1382. }
  1383. else
  1384. {
  1385. Result = Item->Name;
  1386. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1387. Result = RemoveSuffix(Result, L"Item", true);
  1388. }
  1389. return Result;
  1390. }
  1391. //---------------------------------------------------------------------------
  1392. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1393. {
  1394. UnicodeString Result;
  1395. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1396. {
  1397. UnicodeString ToolbarStr;
  1398. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1399. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1400. {
  1401. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1402. {
  1403. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1404. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1405. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1406. // and save the changes here only.
  1407. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1408. {
  1409. UnicodeString Name = GetToolbarItemName(Item);
  1410. DebugAssert(Name.Pos(L"TBX") == 0);
  1411. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1412. }
  1413. }
  1414. }
  1415. if (!ToolbarStr.IsEmpty())
  1416. {
  1417. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1418. }
  1419. }
  1420. return Result;
  1421. }
  1422. //---------------------------------------------------------------------------
  1423. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1424. {
  1425. DebugAssert(FProgressForm == NULL);
  1426. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1427. bool AllowMoveToQueue = (FTransferResumeList != NULL) || (FDeletedFiles != NULL);
  1428. FProgressForm = new TProgressForm(Application, AllowMoveToQueue, AllowSkip, SynchronizeProgress);
  1429. FProgressForm->DeleteLocalToRecycleBin =
  1430. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1431. FProgressForm->DeleteRemoteToRecycleBin =
  1432. HasActiveTerminal() &&
  1433. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1434. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1435. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1436. LockWindow();
  1437. // Actually, do not know what hides the progress once the operation finishes
  1438. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1439. SetTaskbarListProgressState(TBPF_NORMAL);
  1440. }
  1441. //---------------------------------------------------------------------------
  1442. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1443. {
  1444. SAFE_DESTROY(FProgressForm);
  1445. UnlockWindow();
  1446. SetQueueProgress();
  1447. }
  1448. //---------------------------------------------------------------------------
  1449. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1450. TFileOperationProgressType & ProgressData)
  1451. {
  1452. // Download to temporary local directory
  1453. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1454. {
  1455. bool Continue = true;
  1456. FPendingTempSpaceWarn = false;
  1457. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1458. if (!Continue)
  1459. {
  1460. Abort();
  1461. }
  1462. }
  1463. // operation is being executed and we still didn't show up progress form
  1464. if (ProgressData.InProgress && !FProgressForm)
  1465. {
  1466. CreateProgressForm(NULL);
  1467. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1468. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1469. }
  1470. // operation is finished (or terminated), so we hide progress form
  1471. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1472. {
  1473. DestroyProgressForm();
  1474. if (ProgressData.Operation == foCalculateSize)
  1475. {
  1476. // When calculating size before transfer, the abort caused by
  1477. // cancel flag set due to "MoveToQueue" below,
  1478. // is not propagated back to us in ExecuteFileOperation,
  1479. // as it is silently swallowed.
  1480. // So we explicitly (re)throw it here.
  1481. if (FMoveToQueue)
  1482. {
  1483. Abort();
  1484. }
  1485. }
  1486. else
  1487. {
  1488. if ((ProgressData.Cancel == csContinue) &&
  1489. !FAutoOperation)
  1490. {
  1491. OperationComplete(ProgressData.StartTime);
  1492. }
  1493. }
  1494. }
  1495. if (FProgressForm &&
  1496. // Can happen only during synchronization, when ending one of the operations
  1497. ProgressData.InProgress)
  1498. {
  1499. FProgressForm->SetProgressData(ProgressData);
  1500. DebugAssert(ProgressData.InProgress);
  1501. SetTaskbarListProgressValue(&ProgressData);
  1502. if (FProgressForm->Cancel > csContinue)
  1503. {
  1504. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1505. if (FProgressForm->Cancel == csCancelFile)
  1506. {
  1507. FProgressForm->ClearCancel();
  1508. }
  1509. // cancel cancels even the move
  1510. FMoveToQueue = false;
  1511. }
  1512. else if (FProgressForm->MoveToQueue)
  1513. {
  1514. FMoveToQueue = true;
  1515. ProgressData.SetCancelAtLeast(csCancel);
  1516. }
  1517. if ((FTransferResumeList != NULL) &&
  1518. ProgressData.InProgress &&
  1519. ProgressData.IsTransfer() &&
  1520. !ProgressData.FullFileName.IsEmpty())
  1521. {
  1522. if ((FTransferResumeList->Count == 0) ||
  1523. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1524. {
  1525. // note that we do not recognize directories from files here
  1526. FTransferResumeList->Add(ProgressData.FullFileName);
  1527. }
  1528. }
  1529. }
  1530. }
  1531. //---------------------------------------------------------------------------
  1532. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1533. const TDateTime & StartTime)
  1534. {
  1535. if (GUIConfiguration->BeepOnFinish &&
  1536. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1537. {
  1538. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1539. DWORD Sound;
  1540. if (!ExtractFileExt(BeepSound).IsEmpty())
  1541. {
  1542. Sound = SND_FILENAME;
  1543. }
  1544. else
  1545. {
  1546. Sound = SND_ALIAS;
  1547. }
  1548. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1549. }
  1550. }
  1551. //---------------------------------------------------------------------------
  1552. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1553. TFileOperationProgressType & ProgressData)
  1554. {
  1555. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1556. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1557. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1558. FileOperationProgress(ProgressData);
  1559. }
  1560. //---------------------------------------------------------------------------
  1561. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1562. {
  1563. UnicodeString Result;
  1564. if (FProgressForm != NULL)
  1565. {
  1566. Result = FProgressForm->ProgressStr();
  1567. }
  1568. return Result;
  1569. }
  1570. //---------------------------------------------------------------------------
  1571. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1572. bool DragDrop)
  1573. {
  1574. return (!DragDrop && (DropSourceControl == NULL)) ||
  1575. (DropSourceControl == DirView(osOther));
  1576. }
  1577. //---------------------------------------------------------------------------
  1578. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1579. {
  1580. TCustomDirView * ADirView = DirView(Side);
  1581. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1582. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1583. // this can happen when local drive is unplugged in the middle of the operation
  1584. if (Item != NULL)
  1585. {
  1586. if (Unselect)
  1587. {
  1588. Item->Selected = false;
  1589. }
  1590. ADirView->MakeProgressVisible(Item);
  1591. }
  1592. return Item;
  1593. }
  1594. //---------------------------------------------------------------------------
  1595. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1596. TFileOperation Operation, TOperationSide Side,
  1597. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1598. TOnceDoneOperation & OnceDoneOperation)
  1599. {
  1600. if (!FAutoOperation)
  1601. {
  1602. // no selection on "/upload", form serves only as event handler
  1603. // (it is not displayed)
  1604. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1605. ((Operation == FPrimaryOperation) ||
  1606. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1607. {
  1608. TOperationSide DViewSide;
  1609. if ((FProgressSide != osCurrent) &&
  1610. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1611. {
  1612. // assume the operation is over the focused panel
  1613. DViewSide = FProgressSide;
  1614. }
  1615. else
  1616. {
  1617. DViewSide = Side;
  1618. }
  1619. if (FOnFileOperationFinished != NULL)
  1620. {
  1621. FOnFileOperationFinished(DViewSide, FileName, Success);
  1622. }
  1623. else
  1624. {
  1625. VisualiseOperationFinished(DViewSide, FileName, Success);
  1626. }
  1627. }
  1628. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1629. {
  1630. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1631. {
  1632. if (Side == osLocal)
  1633. {
  1634. Configuration->Usage->Inc(L"UploadedFiles");
  1635. }
  1636. else
  1637. {
  1638. Configuration->Usage->Inc(L"DownloadedFiles");
  1639. }
  1640. }
  1641. }
  1642. }
  1643. if (Success && (FSynchronizeController != NULL))
  1644. {
  1645. DebugAssert(!IsSideLocalBrowser(osRemote));
  1646. if (Operation == foCopy)
  1647. {
  1648. DebugAssert(Side == osLocal);
  1649. FSynchronizeController->LogOperation(soUpload, FileName);
  1650. }
  1651. else if (Operation == foDelete)
  1652. {
  1653. DebugAssert(Side == osRemote);
  1654. FSynchronizeController->LogOperation(soDelete, FileName);
  1655. }
  1656. }
  1657. if (FProgressForm)
  1658. {
  1659. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1660. }
  1661. }
  1662. //---------------------------------------------------------------------------
  1663. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1664. TFileOperation Operation, TOperationSide Side,
  1665. bool Temp, const UnicodeString & FileName, Boolean Success,
  1666. TOnceDoneOperation & OnceDoneOperation)
  1667. {
  1668. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1669. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1670. OnceDoneOperation);
  1671. }
  1672. //---------------------------------------------------------------------------
  1673. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1674. {
  1675. return false;
  1676. }
  1677. //---------------------------------------------------------------------------
  1678. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1679. {
  1680. return false;
  1681. }
  1682. //---------------------------------------------------------------------------
  1683. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1684. {
  1685. return false;
  1686. }
  1687. //---------------------------------------------------------------------------
  1688. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1689. {
  1690. return NULL;
  1691. }
  1692. //---------------------------------------------------------------------------
  1693. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1694. {
  1695. DebugAssert(GetSide(Side) == osRemote);
  1696. DebugUsedParam(Side);
  1697. return RemoteDirView;
  1698. }
  1699. //---------------------------------------------------------------------------
  1700. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1701. {
  1702. DebugAssert(GetSide(Side) == osRemote);
  1703. DebugUsedParam(Side);
  1704. return RemoteDriveView;
  1705. }
  1706. //---------------------------------------------------------------------------
  1707. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1708. {
  1709. DebugAssert(GetSide(Side) == osRemote);
  1710. DebugUsedParam(Side);
  1711. return HasActiveTerminal();
  1712. }
  1713. //---------------------------------------------------------------------------
  1714. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1715. TOperationSide Side, int FilesOnly)
  1716. {
  1717. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1718. }
  1719. //---------------------------------------------------------------------------
  1720. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1721. TOperationSide Side, int FilesOnly)
  1722. {
  1723. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1724. }
  1725. //---------------------------------------------------------------------------
  1726. struct THistoryItemData
  1727. {
  1728. short int Side;
  1729. short int Index;
  1730. };
  1731. //---------------------------------------------------------------------------
  1732. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1733. {
  1734. DirView(Side)->HistoryGo(Index);
  1735. }
  1736. //---------------------------------------------------------------------------
  1737. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1738. {
  1739. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1740. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1741. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1742. }
  1743. //---------------------------------------------------------------------------
  1744. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1745. bool Back)
  1746. {
  1747. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1748. {
  1749. TCustomDirView * DView = DirView(Side);
  1750. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1751. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1752. if (ICount > 10)
  1753. {
  1754. ICount = 10;
  1755. }
  1756. Menu->Items->Clear();
  1757. THistoryItemData Data;
  1758. Data.Side = Side;
  1759. for (short int i = 1; i <= ICount; i++)
  1760. {
  1761. TTBCustomItem * Item = new TTBXItem(Menu);
  1762. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1763. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1764. Item->Hint = DView->HistoryPath[Data.Index];
  1765. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1766. Item->Tag = *reinterpret_cast<int*>(&Data);
  1767. Item->OnClick = HistoryItemClick;
  1768. Menu->Items->Add(Item);
  1769. }
  1770. }
  1771. }
  1772. //---------------------------------------------------------------------------
  1773. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1774. TOperationSide Side, bool Back)
  1775. {
  1776. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1777. {
  1778. // workaround
  1779. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1780. // operator new allocates memory in Pascal code, but calls inline
  1781. // contructor in C++, leading in problems, the function does
  1782. // both in Pascal code
  1783. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1784. UpdateHistoryMenu(Side, Back);
  1785. }
  1786. return FHistoryMenu[Side == osLocal][Back];
  1787. }
  1788. //---------------------------------------------------------------------------
  1789. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1790. TCustomDirView *Sender)
  1791. {
  1792. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1793. UpdateHistoryMenu(Side, true);
  1794. UpdateHistoryMenu(Side, false);
  1795. }
  1796. //---------------------------------------------------------------------------
  1797. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1798. {
  1799. // remote custom commands can be executed only if the server supports shell commands
  1800. // or have secondary shell
  1801. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1802. }
  1803. //---------------------------------------------------------------------------
  1804. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1805. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1806. {
  1807. // -1 = hidden, 0 = disabled, 1 = enabled
  1808. int Result;
  1809. TFileCustomCommand RemoteCustomCommand;
  1810. TLocalCustomCommand LocalCustomCommand;
  1811. TFileCustomCommand * NonInteractiveCustomCommand =
  1812. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1813. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1814. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1815. if (FLAGCLEAR(Command.Params, ccLocal))
  1816. {
  1817. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1818. // custom command that does not operate with files can be executed anytime ...
  1819. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1820. {
  1821. // ... but do not show such command in remote file menu
  1822. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1823. {
  1824. Result = AllowedState;
  1825. }
  1826. else
  1827. {
  1828. Result = -1;
  1829. }
  1830. }
  1831. else
  1832. {
  1833. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1834. {
  1835. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1836. }
  1837. else
  1838. {
  1839. Result = -1;
  1840. }
  1841. }
  1842. }
  1843. else
  1844. {
  1845. // custom command that does not operate with files can be executed anytime
  1846. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1847. {
  1848. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1849. }
  1850. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1851. {
  1852. if ((ListType == ccltAll) || (ListType == ccltFile))
  1853. {
  1854. // special case is "diff"-style command that can be executed over any side,
  1855. // if we have both sides
  1856. Result =
  1857. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1858. // directly (instead of EnableSelectedOperation) to pass
  1859. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1860. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1861. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1862. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1863. }
  1864. else if (ListType == ccltBoth)
  1865. {
  1866. DebugAssert(!IsLocalBrowserMode());
  1867. Result = 1;
  1868. }
  1869. else
  1870. {
  1871. Result = -1;
  1872. }
  1873. }
  1874. else
  1875. {
  1876. if ((ListType == ccltAll) ||
  1877. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1878. {
  1879. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1880. }
  1881. else
  1882. {
  1883. Result = -1;
  1884. }
  1885. }
  1886. if ((Result > 0) &&
  1887. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1888. !HasActiveTerminal())
  1889. {
  1890. Result = 0;
  1891. }
  1892. }
  1893. return Result;
  1894. }
  1895. //---------------------------------------------------------------------------
  1896. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1897. TStrings * FileList, const TCustomCommandType & ACommand,
  1898. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1899. {
  1900. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1901. {
  1902. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1903. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1904. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1905. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1906. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1907. bool Capture =
  1908. FLAGSET(ACommand.Params, ccShowResults) ||
  1909. FLAGSET(ACommand.Params, ccCopyResults) ||
  1910. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1911. TCaptureOutputEvent OutputEvent = NULL;
  1912. DebugAssert(FCapturedLog == NULL);
  1913. if (Capture)
  1914. {
  1915. FCapturedLog = new TStringList();
  1916. OutputEvent = TerminalCaptureLog;
  1917. }
  1918. try
  1919. {
  1920. if (!RemoteCustomCommand.IsFileCommand(Command))
  1921. {
  1922. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1923. OutputEvent);
  1924. }
  1925. else
  1926. {
  1927. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1928. }
  1929. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1930. {
  1931. if (FLAGSET(ACommand.Params, ccCopyResults))
  1932. {
  1933. CopyToClipboard(FCapturedLog);
  1934. }
  1935. if (FLAGSET(ACommand.Params, ccShowResults))
  1936. {
  1937. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1938. }
  1939. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1940. {
  1941. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1942. }
  1943. }
  1944. }
  1945. __finally
  1946. {
  1947. SAFE_DESTROY(FCapturedLog);
  1948. }
  1949. }
  1950. }
  1951. //---------------------------------------------------------------------------
  1952. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1953. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1954. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1955. {
  1956. DebugAssert(!IsLocalBrowserMode());
  1957. TStrings * LocalFileList = NULL;
  1958. TStrings * RemoteFileList = NULL;
  1959. TStrings * RemoteFileListFull = NULL;
  1960. try
  1961. {
  1962. if (LocalFileCommand)
  1963. {
  1964. if (ALocalFileList == NULL)
  1965. {
  1966. DebugAssert(HasDirView[osLocal]);
  1967. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1968. // directly (instead of EnableSelectedOperation) to pass
  1969. // false to FocusedFileOnlyWhenFocused
  1970. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1971. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1972. }
  1973. else
  1974. {
  1975. LocalFileList = ALocalFileList;
  1976. }
  1977. if (FileListCommand)
  1978. {
  1979. if (LocalFileList->Count != 1)
  1980. {
  1981. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1982. }
  1983. }
  1984. else
  1985. {
  1986. if ((LocalFileList->Count != 1) &&
  1987. (FileList->Count != 1) &&
  1988. (LocalFileList->Count != FileList->Count))
  1989. {
  1990. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1991. }
  1992. }
  1993. }
  1994. UnicodeString RootTempDir;
  1995. UnicodeString TempDir;
  1996. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1997. if (!RemoteFiles)
  1998. {
  1999. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true);
  2000. }
  2001. try
  2002. {
  2003. TDateTimes RemoteFileTimes;
  2004. if (RemoteFiles)
  2005. {
  2006. RemoteFileList = FileList;
  2007. }
  2008. else
  2009. {
  2010. RemoteFileList = new TStringList();
  2011. TMakeLocalFileListParams MakeFileListParam;
  2012. MakeFileListParam.FileList = RemoteFileList;
  2013. MakeFileListParam.FileTimes = &RemoteFileTimes;
  2014. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2015. MakeFileListParam.Recursive =
  2016. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  2017. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2018. if (!MakeFileListParam.Recursive)
  2019. {
  2020. RemoteFileTimes.clear();
  2021. RemoteFileListFull = new TStringList();
  2022. MakeFileListParam.FileList = RemoteFileListFull;
  2023. MakeFileListParam.Recursive = true;
  2024. MakeFileListParam.IncludeDirs = false;
  2025. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2026. }
  2027. else
  2028. {
  2029. RemoteFileListFull = RemoteFileList;
  2030. }
  2031. }
  2032. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  2033. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2034. if (!NonBlocking)
  2035. {
  2036. int Count;
  2037. if (FileListCommand)
  2038. {
  2039. Count = 1;
  2040. }
  2041. else if (LocalFileCommand)
  2042. {
  2043. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  2044. }
  2045. else
  2046. {
  2047. Count = RemoteFileList->Count;
  2048. }
  2049. Progress.Start(foCustomCommand, osRemote, Count);
  2050. DebugAssert(FProgressForm != NULL);
  2051. FProgressForm->ReadOnly = true;
  2052. }
  2053. try
  2054. {
  2055. if (FileListCommand)
  2056. {
  2057. UnicodeString LocalFile;
  2058. // MakeFileList does not delimit filenames
  2059. UnicodeString FileList = MakeFileList(RemoteFileList);
  2060. if (LocalFileCommand)
  2061. {
  2062. DebugAssert(LocalFileList->Count == 1);
  2063. LocalFile = LocalFileList->Strings[0];
  2064. }
  2065. TLocalCustomCommand CustomCommand(Data,
  2066. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  2067. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  2068. if (NonBlocking)
  2069. {
  2070. DebugAssert(!POutput);
  2071. ExecuteShellChecked(ShellCommand);
  2072. }
  2073. else
  2074. {
  2075. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2076. }
  2077. }
  2078. else if (LocalFileCommand)
  2079. {
  2080. if (LocalFileList->Count == 1)
  2081. {
  2082. UnicodeString LocalFile = LocalFileList->Strings[0];
  2083. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2084. {
  2085. UnicodeString FileName = RemoteFileList->Strings[Index];
  2086. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2087. Progress.SetFile(FileNameForProgress);
  2088. TLocalCustomCommand CustomCommand(Data,
  2089. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2090. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2091. TOnceDoneOperation OnceDoneOperation;
  2092. // Do not unselect anything, as it won't work with two panels anyway
  2093. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2094. }
  2095. }
  2096. else if (RemoteFileList->Count == 1)
  2097. {
  2098. UnicodeString FileName = RemoteFileList->Strings[0];
  2099. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2100. {
  2101. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2102. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2103. Progress.SetFile(LocalFileNameOnly);
  2104. TLocalCustomCommand CustomCommand(
  2105. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2106. FileName, LocalFileName, L"");
  2107. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2108. TOnceDoneOperation OnceDoneOperation;
  2109. // Do not unselect anything, as it won't work with two panels anyway
  2110. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2111. }
  2112. }
  2113. else
  2114. {
  2115. if (LocalFileList->Count != RemoteFileList->Count)
  2116. {
  2117. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2118. }
  2119. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2120. {
  2121. UnicodeString FileName = RemoteFileList->Strings[Index];
  2122. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2123. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2124. Progress.SetFile(LocalFileNameOnly); // sic
  2125. TLocalCustomCommand CustomCommand(
  2126. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2127. FileName, LocalFileName, L"");
  2128. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2129. TOnceDoneOperation OnceDoneOperation;
  2130. // Do not unselect anything, as it won't work with two panels anyway
  2131. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2132. }
  2133. }
  2134. }
  2135. else
  2136. {
  2137. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2138. {
  2139. UnicodeString FileName = RemoteFileList->Strings[Index];
  2140. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2141. Progress.SetFile(FileNameForProgress);
  2142. TLocalCustomCommand CustomCommand(Data,
  2143. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2144. FileName, L"", L"");
  2145. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2146. TOnceDoneOperation OnceDoneOperation;
  2147. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2148. }
  2149. }
  2150. }
  2151. __finally
  2152. {
  2153. if (!NonBlocking)
  2154. {
  2155. Progress.Stop();
  2156. }
  2157. }
  2158. DebugAssert(!FAutoOperation);
  2159. if (!RemoteFiles)
  2160. {
  2161. TempDir = IncludeTrailingBackslash(TempDir);
  2162. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2163. {
  2164. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2165. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2166. // Skip directories, process only nested files.
  2167. // The check is redundant as FileAge fails for directories anyway.
  2168. !DirectoryExists(FileName))
  2169. {
  2170. UnicodeString RemoteDir =
  2171. UnixExtractFileDir(
  2172. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2173. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2174. TDateTime NewTime;
  2175. if (FileAge(FileName, NewTime) &&
  2176. (NewTime != RemoteFileTimes[Index]))
  2177. {
  2178. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2179. TemporaryFileCopyParam(CopyParam);
  2180. CopyParam.FileMask = L"";
  2181. FAutoOperation = true;
  2182. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2183. TemporaryFilesList->Add(FileName);
  2184. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2185. }
  2186. }
  2187. }
  2188. }
  2189. }
  2190. __finally
  2191. {
  2192. FAutoOperation = false;
  2193. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2194. {
  2195. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2196. }
  2197. }
  2198. }
  2199. __finally
  2200. {
  2201. if (RemoteFileList != FileList)
  2202. {
  2203. delete RemoteFileList;
  2204. }
  2205. if (LocalFileList != ALocalFileList)
  2206. {
  2207. delete LocalFileList;
  2208. }
  2209. if (RemoteFileListFull != RemoteFileList)
  2210. {
  2211. delete RemoteFileListFull;
  2212. }
  2213. }
  2214. }
  2215. //---------------------------------------------------------------------------
  2216. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2217. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2218. bool FileListCommand, UnicodeString * POutput)
  2219. {
  2220. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2221. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2222. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2223. {
  2224. UnicodeString FileName = SelectedFileList->Strings[Index];
  2225. if (DirectoryExists(FileName))
  2226. {
  2227. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2228. {
  2229. LocalFileList->Add(FileName);
  2230. }
  2231. if (FLAGSET(ACommand.Params, ccRecursive))
  2232. {
  2233. TMakeLocalFileListParams MakeFileListParam;
  2234. MakeFileListParam.FileList = LocalFileList.get();
  2235. MakeFileListParam.FileTimes = NULL;
  2236. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2237. MakeFileListParam.Recursive = true;
  2238. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2239. }
  2240. }
  2241. else
  2242. {
  2243. LocalFileList->Add(FileName);
  2244. }
  2245. }
  2246. UnicodeString RemotePath;
  2247. if (Terminal != NULL)
  2248. {
  2249. RemotePath = Terminal->CurrentDirectory;
  2250. }
  2251. if (FileListCommand)
  2252. {
  2253. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2254. TLocalCustomCommand CustomCommand(
  2255. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2256. L"", L"", FileList);
  2257. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2258. }
  2259. else
  2260. {
  2261. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2262. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2263. DebugAssert(FProgressForm != NULL);
  2264. FProgressForm->ReadOnly = true;
  2265. try
  2266. {
  2267. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2268. {
  2269. UnicodeString FileName = LocalFileList->Strings[Index];
  2270. Progress.SetFile(FileName);
  2271. TLocalCustomCommand CustomCommand(
  2272. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2273. FileName, L"", L"");
  2274. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2275. TOnceDoneOperation OnceDoneOperation;
  2276. Progress.Finish(FileName, true, OnceDoneOperation);
  2277. }
  2278. }
  2279. __finally
  2280. {
  2281. Progress.Stop();
  2282. }
  2283. }
  2284. }
  2285. //---------------------------------------------------------------------------
  2286. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2287. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2288. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2289. {
  2290. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2291. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2292. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2293. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2294. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2295. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2296. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2297. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2298. std::unique_ptr<UnicodeString> POutput;
  2299. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2300. FLAGSET(ACommand.Params, ccCopyResults))
  2301. {
  2302. POutput.reset(new UnicodeString());
  2303. }
  2304. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2305. FProgressSide = FCurrentSide;
  2306. if (!LocalCustomCommand.IsFileCommand(Command))
  2307. {
  2308. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2309. }
  2310. // remote files?
  2311. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2312. {
  2313. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2314. }
  2315. // local files
  2316. else
  2317. {
  2318. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2319. }
  2320. if (POutput.get() != NULL)
  2321. {
  2322. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2323. int P = POutput->Pos(sLineBreak);
  2324. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2325. {
  2326. POutput->SetLength(P - 1);
  2327. }
  2328. // Copy even empty output to clipboard
  2329. if (FLAGSET(ACommand.Params, ccCopyResults))
  2330. {
  2331. CopyToClipboard(*POutput);
  2332. }
  2333. // But do not show an empty message box.
  2334. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2335. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2336. !POutput->IsEmpty())
  2337. {
  2338. TClipboardHandler ClipboardHandler;
  2339. ClipboardHandler.Text = *POutput;
  2340. TMessageParams Params;
  2341. TQueryButtonAlias Aliases[1];
  2342. Aliases[0].Button = qaRetry;
  2343. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2344. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2345. Params.Aliases = Aliases;
  2346. Params.AliasesCount = LENOF(Aliases);
  2347. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2348. }
  2349. }
  2350. }
  2351. //---------------------------------------------------------------------------
  2352. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2353. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2354. {
  2355. TCustomCommandData Data;
  2356. UnicodeString Site;
  2357. UnicodeString RemotePath;
  2358. if (HasActiveTerminal())
  2359. {
  2360. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2361. Data = TCustomCommandData(SessionData.get());
  2362. Site = Terminal->SessionData->SessionKey;
  2363. RemotePath = Terminal->CurrentDirectory;
  2364. }
  2365. UnicodeString HelpKeyword = ACommand.HomePage;
  2366. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2367. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2368. {
  2369. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2370. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2371. {
  2372. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2373. }
  2374. else
  2375. {
  2376. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2377. }
  2378. if (!DoCustomCommandOptionsDialog(
  2379. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2380. {
  2381. Abort();
  2382. }
  2383. }
  2384. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2385. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2386. {
  2387. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2388. }
  2389. else
  2390. {
  2391. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2392. }
  2393. }
  2394. //---------------------------------------------------------------------------
  2395. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2396. const TCustomCommandType & Command)
  2397. {
  2398. DebugAssert(FCustomCommandLocalFileList != NULL);
  2399. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2400. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2401. TStrings * LocalFileList = new TStringList();
  2402. TStrings * RemoteFileList = new TStringList();
  2403. try
  2404. {
  2405. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2406. {
  2407. LocalFileList->Clear();
  2408. LocalFileList->AddObject(
  2409. FCustomCommandLocalFileList->Strings[Index],
  2410. FCustomCommandLocalFileList->Objects[Index]);
  2411. RemoteFileList->Clear();
  2412. RemoteFileList->AddObject(
  2413. FCustomCommandRemoteFileList->Strings[Index],
  2414. FCustomCommandRemoteFileList->Objects[Index]);
  2415. CustomCommand(RemoteFileList, Command, LocalFileList);
  2416. }
  2417. }
  2418. __finally
  2419. {
  2420. delete LocalFileList;
  2421. delete RemoteFileList;
  2422. }
  2423. }
  2424. //---------------------------------------------------------------------------
  2425. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2426. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2427. {
  2428. delete FCustomCommandLocalFileList;
  2429. delete FCustomCommandRemoteFileList;
  2430. // takeover ownership,
  2431. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2432. FCustomCommandLocalFileList = LocalFileList;
  2433. FCustomCommandRemoteFileList = RemoteFileList;
  2434. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2435. if (Button != NULL)
  2436. {
  2437. FCustomCommandMenu->Items->Clear();
  2438. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2439. MenuPopup(FCustomCommandMenu, Button);
  2440. }
  2441. else
  2442. {
  2443. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2444. }
  2445. }
  2446. //---------------------------------------------------------------------------
  2447. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2448. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2449. {
  2450. DebugAssert(FCapturedLog != NULL);
  2451. if ((OutputType == cotOutput) || (OutputType == cotError))
  2452. {
  2453. FCapturedLog->Add(AddedLine);
  2454. }
  2455. }
  2456. //---------------------------------------------------------------------------
  2457. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2458. TOperationSide Side)
  2459. {
  2460. return
  2461. (Side == osOther) &&
  2462. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2463. }
  2464. //---------------------------------------------------------------------------
  2465. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2466. TOperationSide Side, TTBXCustomItem * Item,
  2467. TResolvedDoubleClickAction DoubleClickAction1, TResolvedDoubleClickAction DoubleClickAction2)
  2468. {
  2469. TCustomDirView * DView = DirView(Side);
  2470. bool IsDefault = false;
  2471. if (DView->ItemFocused != NULL)
  2472. {
  2473. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  2474. TResolvedDoubleClickAction DefaultAction =
  2475. WinConfiguration->ResolveDoubleClickAction(DView->ItemIsDirectory(DView->ItemFocused), ATerminal);
  2476. IsDefault = (DefaultAction == DoubleClickAction1) || (DefaultAction == DoubleClickAction2);
  2477. }
  2478. TTBItemOptions O = Item->Options;
  2479. if (IsDefault)
  2480. {
  2481. Item->Options = O << tboDefault;
  2482. }
  2483. else
  2484. {
  2485. Item->Options = O >> tboDefault;
  2486. }
  2487. }
  2488. //---------------------------------------------------------------------------
  2489. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2490. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2491. {
  2492. TCustomDirView * DView = DirView(Side);
  2493. TListItem * Item = DView->ItemFocused;
  2494. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2495. EnableFocusedOperation[Side])
  2496. {
  2497. TPoint ClientPoint;
  2498. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2499. TPoint(0, 0) : MousePos;
  2500. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2501. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2502. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2503. }
  2504. }
  2505. //---------------------------------------------------------------------------
  2506. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2507. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2508. {
  2509. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, rdcaChangeDir, rdcaOpen);
  2510. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, rdcaEdit, rdcaNone);
  2511. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, rdcaCopy, rdcaNone);
  2512. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2513. Handled = true;
  2514. }
  2515. //---------------------------------------------------------------------------
  2516. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2517. {
  2518. }
  2519. //---------------------------------------------------------------------------
  2520. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2521. {
  2522. DebugAssert(FErrorList == NULL);
  2523. if (WinConfiguration->ContinueOnError)
  2524. {
  2525. FErrorList = new TStringList();
  2526. Configuration->Usage->Inc(L"ContinuationsOnError");
  2527. }
  2528. NonVisualDataModule->StartBusy();
  2529. }
  2530. //---------------------------------------------------------------------------
  2531. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2532. {
  2533. NonVisualDataModule->EndBusy();
  2534. if (FErrorList)
  2535. {
  2536. HandleErrorList(FErrorList);
  2537. }
  2538. }
  2539. //---------------------------------------------------------------------------
  2540. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2541. const TCopyParamType & CopyParam)
  2542. {
  2543. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2544. {
  2545. Configuration->Usage->Inc(L"FileMaskUses");
  2546. }
  2547. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2548. {
  2549. Configuration->Usage->Inc(L"OperationMaskUses");
  2550. }
  2551. }
  2552. //---------------------------------------------------------------------------
  2553. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2554. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2555. {
  2556. DebugAssert(!IsLocalBrowserMode());
  2557. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2558. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2559. TTransferOperationParam DefaultParam;
  2560. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2561. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2562. switch (Param.Queue)
  2563. {
  2564. case asOn:
  2565. CopyParam.Queue = true;
  2566. break;
  2567. case asOff:
  2568. CopyParam.Queue = false;
  2569. break;
  2570. case asAuto:
  2571. default:
  2572. // keep default
  2573. break;
  2574. }
  2575. bool Result =
  2576. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2577. if (Result)
  2578. {
  2579. DebugAssert(Terminal);
  2580. bool SelectionRestored = false;
  2581. TCustomDirView * DView = NULL;
  2582. if (HasDirView[Side])
  2583. {
  2584. DView = DirView(Side);
  2585. DView->SaveSelection();
  2586. DView->SaveSelectedNames();
  2587. }
  2588. UpdateCopyParamCounters(CopyParam);
  2589. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2590. DebugAssert(FTransferResumeList == NULL);
  2591. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2592. FMoveToQueue = false;
  2593. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2594. try
  2595. {
  2596. TStrings * PermanentFileList;
  2597. std::unique_ptr<TStrings> PermanentFileListOwner;
  2598. try
  2599. {
  2600. if (Side == osLocal)
  2601. {
  2602. PermanentFileList = FileList;
  2603. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2604. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2605. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2606. if (Operation == foMove)
  2607. {
  2608. ReloadLocalDirectory();
  2609. if (DView != NULL)
  2610. {
  2611. DView->RestoreSelection();
  2612. }
  2613. SelectionRestored = true;
  2614. }
  2615. }
  2616. else
  2617. {
  2618. // Clone the file list as it may refer to current directory files,
  2619. // which get destroyed, when the source directory is reloaded after foMove operation.
  2620. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2621. // But for a hotfix, we are not going to do such a big change.
  2622. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2623. PermanentFileList = PermanentFileListOwner.get();
  2624. try
  2625. {
  2626. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2627. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2628. }
  2629. __finally
  2630. {
  2631. if (Operation == foMove)
  2632. {
  2633. if (DView != NULL)
  2634. {
  2635. DView->RestoreSelection();
  2636. }
  2637. SelectionRestored = true;
  2638. }
  2639. ReloadLocalDirectory(Param.TargetDirectory);
  2640. }
  2641. }
  2642. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2643. if (FMoveToQueue)
  2644. {
  2645. Abort();
  2646. }
  2647. }
  2648. catch (EAbort &)
  2649. {
  2650. if (FMoveToQueue)
  2651. {
  2652. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2653. DebugAssert(CopyParam.TransferSkipList == NULL);
  2654. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2655. if (TransferResumeList->Count > 0)
  2656. {
  2657. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2658. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2659. }
  2660. CopyParam.TransferSkipList = TransferResumeList.release();
  2661. // not really needed, just to keep it consistent with TransferResumeList
  2662. FTransferResumeList = NULL;
  2663. FMoveToQueue = false;
  2664. Configuration->Usage->Inc("MovesToBackground");
  2665. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2666. ClearTransferSourceSelection(Direction);
  2667. }
  2668. throw;
  2669. }
  2670. }
  2671. __finally
  2672. {
  2673. if (!SelectionRestored && (DView != NULL))
  2674. {
  2675. DView->DiscardSavedSelection();
  2676. }
  2677. FTransferResumeList = NULL;
  2678. }
  2679. }
  2680. return Result;
  2681. }
  2682. //---------------------------------------------------------------------------
  2683. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2684. TOperationSide Side, TStrings * FileList, void * Param)
  2685. {
  2686. DebugAssert(FileList->Count);
  2687. // We deliberately do not toggle alternative flag (Param), but use OR,
  2688. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2689. // shortcut of CurrentDeleteAlternativeAction
  2690. bool Alternative =
  2691. bool(Param) || UseAlternativeFunction();
  2692. bool Recycle;
  2693. if (IsSideLocalBrowser(Side))
  2694. {
  2695. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2696. }
  2697. else
  2698. {
  2699. Recycle =
  2700. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2701. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2702. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2703. }
  2704. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2705. if (!Result)
  2706. {
  2707. UnicodeString Query;
  2708. if (FileList->Count == 1)
  2709. {
  2710. if (IsSideLocalBrowser(Side))
  2711. {
  2712. Query = ExtractFileName(FileList->Strings[0]);
  2713. }
  2714. else
  2715. {
  2716. Query = UnixExtractFileName(FileList->Strings[0]);
  2717. }
  2718. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2719. }
  2720. else
  2721. {
  2722. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2723. }
  2724. TMessageParams Params(mpNeverAskAgainCheck);
  2725. Params.ImageName = L"Delete file";
  2726. unsigned int Answer =
  2727. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2728. if (Answer == qaNeverAskAgain)
  2729. {
  2730. Result = true;
  2731. if (Recycle)
  2732. {
  2733. WinConfiguration->ConfirmRecycling = false;
  2734. }
  2735. else
  2736. {
  2737. WinConfiguration->ConfirmDeleting = false;
  2738. }
  2739. }
  2740. else
  2741. {
  2742. Result = (Answer == qaOK);
  2743. }
  2744. }
  2745. if (Result)
  2746. {
  2747. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2748. }
  2749. return Result;
  2750. }
  2751. //---------------------------------------------------------------------------
  2752. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2753. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2754. {
  2755. TAutoBatch AutoBatch(this);
  2756. bool Result;
  2757. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2758. {
  2759. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2760. }
  2761. else if (Operation == foRename)
  2762. {
  2763. DebugAssert(DirView(Side)->ItemFocused);
  2764. DirView(Side)->ItemFocused->EditCaption();
  2765. Result = true;
  2766. }
  2767. else if (Operation == foDelete)
  2768. {
  2769. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2770. }
  2771. else if (Operation == foSetProperties)
  2772. {
  2773. DirView(osRemote)->SaveSelectedNames();
  2774. Result = SetProperties(Side, FileList);
  2775. }
  2776. else if (Operation == foCustomCommand)
  2777. {
  2778. DebugAssert(Param);
  2779. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2780. DirView(osRemote)->SaveSelectedNames();
  2781. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2782. CustomCommand(FileList, *Command, NULL);
  2783. Result = true;
  2784. }
  2785. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2786. {
  2787. DebugAssert(!IsSideLocalBrowser(Side));
  2788. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2789. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2790. }
  2791. else if (Operation == foLock)
  2792. {
  2793. DebugAssert(!IsSideLocalBrowser(Side));
  2794. LockFiles(FileList, true);
  2795. Result = true;
  2796. }
  2797. else if (Operation == foUnlock)
  2798. {
  2799. DebugAssert(!IsSideLocalBrowser(Side));
  2800. LockFiles(FileList, false);
  2801. Result = true;
  2802. }
  2803. else
  2804. {
  2805. DebugFail();
  2806. }
  2807. return Result;
  2808. }
  2809. //---------------------------------------------------------------------------
  2810. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2811. {
  2812. if (Side == osCurrent)
  2813. {
  2814. Side = FCurrentSide;
  2815. }
  2816. return Side;
  2817. }
  2818. //---------------------------------------------------------------------------
  2819. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2820. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2821. {
  2822. Side = GetSide(Side);
  2823. bool FullPath = IsSideLocalBrowser(Side);
  2824. TCustomDirView * ADirView = DirView(Side);
  2825. std::unique_ptr<TStringList> FileList(new TStringList());
  2826. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2827. bool ReloadProperties =
  2828. !IsSideLocalBrowser(Side) && (Operation == foSetProperties) &&
  2829. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2830. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2831. if (Result && ReloadProperties)
  2832. {
  2833. std::unique_ptr<TStringList> FileList2(new TStringList());
  2834. FileList->CaseSensitive = true;
  2835. FileList->Sort();
  2836. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2837. {
  2838. TListItem * Item = ADirView->Items->Item[Index];
  2839. UnicodeString FileName = ADirView->ItemFileName(Item);
  2840. int Index2;
  2841. if (FileList->Find(FileName, Index2))
  2842. {
  2843. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2844. }
  2845. }
  2846. LoadFilesProperties(FileList2.get());
  2847. }
  2848. return Result;
  2849. }
  2850. //---------------------------------------------------------------------------
  2851. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2852. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2853. bool NoConfirmation, void * Param)
  2854. {
  2855. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2856. {
  2857. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2858. {
  2859. DebugAssert(!IsLocalBrowserMode());
  2860. if (GetSide(Side) == osLocal)
  2861. {
  2862. Configuration->Usage->Inc(L"UploadsCommand");
  2863. }
  2864. else
  2865. {
  2866. Configuration->Usage->Inc(L"DownloadsCommand");
  2867. }
  2868. }
  2869. }
  2870. }
  2871. //---------------------------------------------------------------------------
  2872. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2873. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2874. {
  2875. TTransferOperationParam Param;
  2876. if ((WinConfiguration->Interface != ifCommander) ||
  2877. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2878. {
  2879. Flags &= ~cocShortCutHint;
  2880. }
  2881. TCustomDirView * DView = DirView(Side);
  2882. Param.Options =
  2883. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2884. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2885. if (FLAGSET(Flags, cocQueue))
  2886. {
  2887. Param.Queue = asOn;
  2888. }
  2889. else if (FLAGSET(Flags, cocNonQueue))
  2890. {
  2891. Param.Queue = asOff;
  2892. }
  2893. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2894. }
  2895. //---------------------------------------------------------------------------
  2896. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2897. {
  2898. int Count = ErrorList->Count;
  2899. try
  2900. {
  2901. if (ErrorList->Count)
  2902. {
  2903. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2904. if (MessageDialog(Message, qtError,
  2905. qaOK | qaCancel, HELP_NONE) == qaOK)
  2906. {
  2907. unsigned int Answer;
  2908. int Index = 0;
  2909. do
  2910. {
  2911. DebugAssert(Index >= 0 && Index < Count);
  2912. TQueryButtonAlias Aliases[2];
  2913. Aliases[0].Button = qaYes;
  2914. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2915. Aliases[1].Button = qaNo;
  2916. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2917. TMessageParams Params;
  2918. Params.Aliases = Aliases;
  2919. Params.AliasesCount = LENOF(Aliases);
  2920. int No = Index + 1;
  2921. UnicodeString Message = ErrorList->Strings[Index];
  2922. UnicodeString AMainInstructions;
  2923. UnicodeString Details;
  2924. if (ExtractMainInstructions(Message, AMainInstructions))
  2925. {
  2926. Details = Message;
  2927. Message = AMainInstructions;
  2928. }
  2929. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2930. Answer = MoreMessageDialog(
  2931. Message,
  2932. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2933. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2934. qaOK, HELP_NONE, &Params);
  2935. if (Answer == qaNo)
  2936. {
  2937. Index++;
  2938. }
  2939. if (Answer == qaYes)
  2940. {
  2941. Index--;
  2942. }
  2943. }
  2944. while (Answer != qaOK);
  2945. }
  2946. }
  2947. }
  2948. __finally
  2949. {
  2950. TStrings * List = ErrorList;
  2951. ErrorList = NULL;
  2952. for (int i = 0; i < Count; i++)
  2953. {
  2954. delete List->Objects[i];
  2955. }
  2956. delete List;
  2957. }
  2958. }
  2959. //---------------------------------------------------------------------------
  2960. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2961. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2962. {
  2963. // needed for checking filemasks, as there's no directory object
  2964. // associated with the file object
  2965. File->FullFileName = FullFileName;
  2966. TFileMasks::TParams MaskParams;
  2967. MaskParams.Size = File->Size;
  2968. MaskParams.Modification = File->Modification;
  2969. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2970. }
  2971. //---------------------------------------------------------------------------
  2972. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2973. {
  2974. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2975. Side = GetSide(Side);
  2976. TCustomDirView * DView = DirView(Side);
  2977. TListItem * FocusedItem = DView->ItemFocused;
  2978. UnicodeString Name;
  2979. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2980. {
  2981. Name = DView->ItemFileName(FocusedItem);
  2982. }
  2983. else
  2984. {
  2985. Name = LoadStr(NEW_FILE);
  2986. }
  2987. UnicodeString Names = Name;
  2988. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2989. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2990. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  2991. {
  2992. while (!Names.IsEmpty())
  2993. {
  2994. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2995. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2996. UnicodeString TargetFileName;
  2997. UnicodeString LocalFileName;
  2998. UnicodeString RootTempDir;
  2999. UnicodeString TempDir;
  3000. UnicodeString RemoteDirectory;
  3001. bool ExistingFile = false;
  3002. if (!IsSideLocalBrowser(Side))
  3003. {
  3004. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  3005. TRemoteFile * File = NULL;
  3006. if (Terminal->FileExists(Name, &File) &&
  3007. !File->IsDirectory)
  3008. {
  3009. try
  3010. {
  3011. ExecuteRemoteFile(Name, File, efDefaultEditor);
  3012. ExistingFile = true;
  3013. }
  3014. __finally
  3015. {
  3016. delete File;
  3017. }
  3018. }
  3019. if (!ExistingFile)
  3020. {
  3021. RemoteDirectory = UnixExtractFilePath(Name);
  3022. TemporaryDirectoryForRemoteFiles(
  3023. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  3024. TargetFileName = UnixExtractFileName(Name);
  3025. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3026. LocalFileName = TempDir +
  3027. // We probably do not want to trim the VMS version here
  3028. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  3029. }
  3030. }
  3031. else
  3032. {
  3033. if (ExtractFilePath(Name).IsEmpty())
  3034. {
  3035. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  3036. }
  3037. else
  3038. {
  3039. LocalFileName = ExpandFileName(Name);
  3040. }
  3041. TargetFileName = ExtractFileName(Name);
  3042. }
  3043. if (!ExistingFile)
  3044. {
  3045. bool NewFile = !FileExists(ApiPath(LocalFileName));
  3046. if (NewFile)
  3047. {
  3048. int File = FileCreate(ApiPath(LocalFileName));
  3049. if (File < 0)
  3050. {
  3051. if (!RootTempDir.IsEmpty())
  3052. {
  3053. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3054. }
  3055. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  3056. }
  3057. else
  3058. {
  3059. FileClose(File);
  3060. }
  3061. }
  3062. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  3063. const TEditorData * ExternalEditor = NULL;
  3064. TFileMasks::TParams MaskParams; // size not known
  3065. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  3066. false, MaskParams);
  3067. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  3068. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  3069. }
  3070. }
  3071. }
  3072. }
  3073. //---------------------------------------------------------------------------
  3074. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  3075. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  3076. {
  3077. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3078. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3079. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3080. return
  3081. ((ExecuteFileBy == efInternalEditor)) ||
  3082. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  3083. }
  3084. //---------------------------------------------------------------------------
  3085. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  3086. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  3087. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  3088. UnicodeString RemoteDirectory, bool NewFile)
  3089. {
  3090. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3091. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3092. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3093. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3094. Side = GetSide(Side);
  3095. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3096. if (!IsSideLocalBrowser(Side))
  3097. {
  3098. Data->Terminal = Terminal;
  3099. Data->Queue = Queue;
  3100. Data->SessionData = CloneCurrentSessionData();
  3101. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3102. Data->RemoteDirectory = RemoteDirectory;
  3103. Data->SessionName = Terminal->SessionData->SessionName;
  3104. Data->LocalRootDirectory = LocalRootDirectory;
  3105. Data->OriginalFileName = OriginalFileName;
  3106. Data->Command = L""; // will be changed later for external editor
  3107. }
  3108. if (ExecuteFileBy == efInternalEditor)
  3109. {
  3110. if (!IsSideLocalBrowser(Side))
  3111. {
  3112. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3113. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3114. TForm * Editor;
  3115. try
  3116. {
  3117. Editor =
  3118. ShowEditorForm(
  3119. FileName, this,
  3120. FEditorManager->FileChanged, FEditorManager->FileReload, FEditorManager->FileClosed,
  3121. SaveAllInternalEditors, AnyInternalEditorModified,
  3122. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3123. }
  3124. catch(...)
  3125. {
  3126. if (!LocalRootDirectory.IsEmpty())
  3127. {
  3128. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  3129. }
  3130. throw;
  3131. }
  3132. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3133. AppLogFmt(L"Opened internal editor with \"%s\"", (FileName));
  3134. }
  3135. else
  3136. {
  3137. DebugAssert(!StandaloneOperation);
  3138. TForm * Editor =
  3139. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3140. SaveAllInternalEditors, AnyInternalEditorModified,
  3141. L"", false, SessionColor, -1, NewFile);
  3142. FLocalEditors->Add(Editor);
  3143. }
  3144. }
  3145. else
  3146. {
  3147. HANDLE Process;
  3148. if (ExecuteFileBy == efExternalEditor)
  3149. {
  3150. UnicodeString Program, Params, Dir;
  3151. Data->Command = ExternalEditor->ExternalEditor;
  3152. ReformatFileNameCommand(Data->Command);
  3153. SplitCommand(Data->Command, Program, Params, Dir);
  3154. Params = ExpandFileNameCommand(Params, FileName);
  3155. Program = ExpandEnvironmentVariables(Program);
  3156. if (!ExecuteShell(Program, Params, Process))
  3157. {
  3158. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3159. }
  3160. }
  3161. else
  3162. {
  3163. DebugAssert(!IsSideLocalBrowser(Side));
  3164. if (!ExecuteShell(FileName, L"", Process))
  3165. {
  3166. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3167. }
  3168. }
  3169. if (IsSideLocalBrowser(Side) ||
  3170. ((ExecuteFileBy == efShell) &&
  3171. !WinConfiguration->Editor.SDIShellEditor) ||
  3172. ((ExecuteFileBy == efExternalEditor) &&
  3173. !ExternalEditor->SDIExternalEditor))
  3174. {
  3175. // no need for handle
  3176. if (Process != NULL)
  3177. {
  3178. DebugCheck(CloseHandle(Process));
  3179. }
  3180. Process = INVALID_HANDLE_VALUE;
  3181. }
  3182. else
  3183. {
  3184. if (Process == NULL)
  3185. {
  3186. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3187. }
  3188. }
  3189. if (!IsSideLocalBrowser(Side))
  3190. {
  3191. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3192. AppLogFmt(L"Opened external editor with remote file \"%s\".", (FileName));
  3193. }
  3194. }
  3195. }
  3196. //---------------------------------------------------------------------------
  3197. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3198. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3199. void * /*Arg*/)
  3200. {
  3201. if (Token != NULL)
  3202. {
  3203. EditorFormFileSave(static_cast<TForm *>(Token));
  3204. }
  3205. }
  3206. //---------------------------------------------------------------------------
  3207. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3208. {
  3209. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3210. {
  3211. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3212. }
  3213. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3214. }
  3215. //---------------------------------------------------------------------------
  3216. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3217. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3218. void * Arg)
  3219. {
  3220. if ((Token != NULL) &&
  3221. IsEditorFormModified(static_cast<TForm *>(Token)))
  3222. {
  3223. *static_cast<bool *>(Arg) = true;
  3224. }
  3225. }
  3226. //---------------------------------------------------------------------------
  3227. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3228. TObject * /*Sender*/, bool & Modified)
  3229. {
  3230. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3231. {
  3232. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3233. {
  3234. Modified = true;
  3235. }
  3236. }
  3237. if (!Modified)
  3238. {
  3239. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3240. }
  3241. }
  3242. //---------------------------------------------------------------------------
  3243. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3244. {
  3245. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3246. }
  3247. //---------------------------------------------------------------------------
  3248. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3249. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3250. UnicodeString & Result, UnicodeString & RootDirectory)
  3251. {
  3252. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3253. {
  3254. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3255. Result = RootDirectory;
  3256. }
  3257. else
  3258. {
  3259. RootDirectory = L"";
  3260. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3261. Result = IncludeTrailingBackslash(Result);
  3262. }
  3263. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3264. {
  3265. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3266. }
  3267. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3268. {
  3269. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3270. {
  3271. RemoteDirectory.Delete(1, 1);
  3272. }
  3273. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3274. }
  3275. if (!ForceDirectories(ApiPath(Result)))
  3276. {
  3277. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3278. }
  3279. }
  3280. //---------------------------------------------------------------------------
  3281. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3282. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3283. bool GetTargetNames, bool AutoOperation)
  3284. {
  3285. DebugAssert(!IsLocalBrowserMode());
  3286. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3287. if (ForceText)
  3288. {
  3289. CopyParam.TransferMode = tmAscii;
  3290. }
  3291. TemporaryFileCopyParam(CopyParam);
  3292. if (TempDir.IsEmpty())
  3293. {
  3294. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3295. }
  3296. DebugAssert(!FAutoOperation);
  3297. FAutoOperation = AutoOperation;
  3298. Terminal->ExceptionOnFail = true;
  3299. try
  3300. {
  3301. try
  3302. {
  3303. // turn off confirmations, as for MDI editors we may possibly download
  3304. // the same file over
  3305. Terminal->CopyToLocal(
  3306. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3307. if (GetTargetNames)
  3308. {
  3309. for (int i = 0; i < FileList->Count; i++)
  3310. {
  3311. FileList->Strings[i] =
  3312. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3313. }
  3314. }
  3315. }
  3316. catch(...)
  3317. {
  3318. if (!RootTempDir.IsEmpty())
  3319. {
  3320. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3321. }
  3322. throw;
  3323. }
  3324. }
  3325. __finally
  3326. {
  3327. FAutoOperation = false;
  3328. Terminal->ExceptionOnFail = false;
  3329. }
  3330. }
  3331. //---------------------------------------------------------------------------
  3332. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3333. {
  3334. // Do not waste time checking for default editor list the next time,
  3335. // or testing if default editor is not notepad.
  3336. bool TryNextTime = false;
  3337. UnicodeString UsageState;
  3338. if (!WinConfiguration->EditorList->IsDefaultList())
  3339. {
  3340. UsageState = "H";
  3341. }
  3342. else
  3343. {
  3344. UnicodeString Executable;
  3345. UnicodeString ExecutableDescription;
  3346. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3347. {
  3348. UnicodeString Message =
  3349. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3350. unsigned int Answer =
  3351. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3352. if (Answer != qaOK)
  3353. {
  3354. UsageState = "R";
  3355. }
  3356. else
  3357. {
  3358. UsageState = "A";
  3359. TEditorData EditorData;
  3360. EditorData.Editor = edExternal;
  3361. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3362. EditorData.ExternalEditorOptionsAutodetect();
  3363. TEditorList EditorList;
  3364. EditorList = *WinConfiguration->EditorList;
  3365. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3366. WinConfiguration->EditorList = &EditorList;
  3367. }
  3368. }
  3369. }
  3370. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3371. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3372. }
  3373. //---------------------------------------------------------------------------
  3374. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3375. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3376. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3377. {
  3378. if (ExecuteFileBy == efDefaultEditor)
  3379. {
  3380. if (!WinConfiguration->OfferedEditorAutoConfig)
  3381. {
  3382. EditorAutoConfig();
  3383. }
  3384. const TEditorPreferences * Editor =
  3385. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3386. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3387. {
  3388. ExecuteFileBy = efInternalEditor;
  3389. ExternalEditor = NULL;
  3390. }
  3391. else if (Editor->Data->Editor == edOpen)
  3392. {
  3393. ExecuteFileBy = efShell;
  3394. ExternalEditor = NULL;
  3395. }
  3396. else
  3397. {
  3398. ExecuteFileBy = efExternalEditor;
  3399. ExternalEditor = Editor->Data;
  3400. }
  3401. }
  3402. }
  3403. //---------------------------------------------------------------------------
  3404. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3405. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3406. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3407. {
  3408. UnicodeString OriginalFileName;
  3409. UnicodeString LocalRootDirectory;
  3410. UnicodeString RemoteDirectory;
  3411. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3412. IsSideLocalBrowser(Side), MaskParams);
  3413. UnicodeString Counter;
  3414. UnicodeString LocalFileName;
  3415. bool Handled = false;
  3416. if (!IsSideLocalBrowser(Side))
  3417. {
  3418. // We need to trim VMS version here, so that we use name without version
  3419. // when uploading back to create a new version of the file
  3420. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3421. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3422. TObject * Token = NULL;
  3423. UnicodeString LocalDirectory;
  3424. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3425. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3426. LocalDirectory))
  3427. {
  3428. if (Token != NULL)
  3429. {
  3430. TForm * Form = dynamic_cast<TForm *>(Token);
  3431. if (Form->WindowState == wsMinimized)
  3432. {
  3433. ShowWindow(Form->Handle, SW_RESTORE);
  3434. }
  3435. else
  3436. {
  3437. Form->SetFocus();
  3438. }
  3439. Handled = true;
  3440. }
  3441. else
  3442. {
  3443. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3444. }
  3445. }
  3446. if (!Handled)
  3447. {
  3448. TStringList * FileList1 = new TStringList();
  3449. try
  3450. {
  3451. FileList1->AddObject(FullFileName, Object);
  3452. TemporarilyDownloadFiles(FileList1,
  3453. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3454. LocalRootDirectory, LocalDirectory, true, true);
  3455. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3456. }
  3457. __finally
  3458. {
  3459. delete FileList1;
  3460. }
  3461. switch (ExecuteFileBy)
  3462. {
  3463. case efShell:
  3464. Counter = "RemoteFilesExecuted";
  3465. break;
  3466. case efInternalEditor:
  3467. Counter = "RemoteFilesOpenedInInternalEditor";
  3468. break;
  3469. case efExternalEditor:
  3470. Counter = "RemoteFilesOpenedInExternalEditor";
  3471. break;
  3472. default:
  3473. DebugFail();
  3474. }
  3475. }
  3476. }
  3477. else
  3478. {
  3479. LocalFileName = FullFileName;
  3480. OriginalFileName = ExtractFileName(FullFileName);
  3481. switch (ExecuteFileBy)
  3482. {
  3483. case efShell:
  3484. Counter = "LocalFilesExecuted";
  3485. break;
  3486. case efInternalEditor:
  3487. Counter = "LocalFilesOpenedInInternalEditor";
  3488. break;
  3489. case efExternalEditor:
  3490. Counter = "LocalFilesOpenedInExternalEditor";
  3491. break;
  3492. default:
  3493. DebugFail();
  3494. }
  3495. }
  3496. if (!Handled)
  3497. {
  3498. Configuration->Usage->Inc(Counter);
  3499. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3500. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3501. }
  3502. }
  3503. //---------------------------------------------------------------------------
  3504. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3505. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3506. bool AllSelected, bool OnFocused)
  3507. {
  3508. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3509. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3510. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3511. Side = GetSide(Side);
  3512. TCustomDirView * DView = DirView(Side);
  3513. TStrings * FileList =
  3514. AllSelected ?
  3515. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3516. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3517. try
  3518. {
  3519. DebugAssert(AllSelected || (FileList->Count == 1));
  3520. for (int i = 0; i < FileList->Count; i++)
  3521. {
  3522. UnicodeString ListFileName = FileList->Strings[i];
  3523. UnicodeString FileNameOnly =
  3524. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3525. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3526. if (!DView->ItemIsDirectory(Item))
  3527. {
  3528. UnicodeString FullFileName;
  3529. if (!IsSideLocalBrowser(Side))
  3530. {
  3531. FullFileName = DirView(Side)->Path + ListFileName;
  3532. }
  3533. else
  3534. {
  3535. FullFileName = ListFileName;
  3536. }
  3537. TObject * Object = FileList->Objects[i];
  3538. TFileMasks::TParams MaskParams;
  3539. MaskParams.Size = DView->ItemFileSize(Item);
  3540. TDateTimePrecision Precision;
  3541. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3542. std::unique_ptr<TRemoteFile> FileOwner;
  3543. if (!IsSideLocalBrowser(Side))
  3544. {
  3545. TRemoteFile * File = NULL;
  3546. Terminal->FileExists(FullFileName, &File);
  3547. if (File != NULL)
  3548. {
  3549. File->FullFileName = FullFileName;
  3550. Object = File;
  3551. FileOwner.reset(File);
  3552. MaskParams.Size = File->Size;
  3553. MaskParams.Modification = File->Modification;
  3554. }
  3555. }
  3556. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName, Object, MaskParams);
  3557. }
  3558. }
  3559. }
  3560. __finally
  3561. {
  3562. delete FileList;
  3563. }
  3564. }
  3565. //---------------------------------------------------------------------------
  3566. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3567. {
  3568. CopyParam.FileNameCase = ncNoChange;
  3569. CopyParam.PreserveRights = false;
  3570. CopyParam.PreserveReadOnly = false;
  3571. CopyParam.ReplaceInvalidChars = true;
  3572. CopyParam.IncludeFileMask = TFileMasks();
  3573. CopyParam.NewerOnly = false;
  3574. CopyParam.FileMask = L"";
  3575. }
  3576. //---------------------------------------------------------------------------
  3577. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
  3578. const UnicodeString & FileName, TEditedFileData * Data, HANDLE UploadCompleteEvent, bool & Retry)
  3579. {
  3580. TTerminalManager * Manager = TTerminalManager::Instance();
  3581. if (!IsActiveTerminal(Data->Terminal))
  3582. {
  3583. if (!NonVisualDataModule->Busy)
  3584. {
  3585. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3586. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3587. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3588. if (Data->Terminal == NULL)
  3589. {
  3590. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3591. if (SameSiteTerminal != NULL)
  3592. {
  3593. UnicodeString Message =
  3594. FMTLOAD(EDIT_SESSION_REATTACH,
  3595. (FileNameOnly, Data->SessionName, FileNameOnly));
  3596. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3597. {
  3598. Data->Terminal = Terminal;
  3599. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3600. Data->SessionName = Terminal->SessionData->SessionName;
  3601. // We might also overwrite session data
  3602. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3603. }
  3604. else
  3605. {
  3606. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3607. Abort();
  3608. }
  3609. }
  3610. }
  3611. // foreground session should reconnect itself
  3612. else if (Terminal != Data->Terminal)
  3613. {
  3614. UnicodeString Message =
  3615. MainInstructions(
  3616. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3617. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3618. {
  3619. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3620. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3621. }
  3622. else
  3623. {
  3624. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3625. Abort();
  3626. }
  3627. }
  3628. }
  3629. if (!IsActiveTerminal(Data->Terminal))
  3630. {
  3631. Configuration->Usage->Inc(L"EditInactiveSession");
  3632. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3633. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3634. (ExtractFileName(FileName), Data->SessionName)));
  3635. }
  3636. }
  3637. TStrings * FileList = new TStringList();
  3638. try
  3639. {
  3640. FileList->Add(FileName);
  3641. // consider using the same settings (preset) as when the file was downloaded
  3642. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3643. TemporaryFileCopyParam(CopyParam);
  3644. if (Data->ForceText)
  3645. {
  3646. CopyParam.TransferMode = tmAscii;
  3647. }
  3648. // so i do not need to worry if masking algorithm works in all cases
  3649. // ("" means "copy file name", no masking is actually done)
  3650. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3651. {
  3652. CopyParam.FileMask = L"";
  3653. }
  3654. else
  3655. {
  3656. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3657. }
  3658. int Params = cpNoConfirmation | cpTemporary;
  3659. if (Terminal->IsCapable[fcBackgroundTransfers])
  3660. {
  3661. DebugAssert(Data->Queue != NULL);
  3662. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3663. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3664. QueueItem->CompleteEvent = UploadCompleteEvent;
  3665. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3666. }
  3667. else
  3668. {
  3669. if (NonVisualDataModule->Busy)
  3670. {
  3671. Retry = true;
  3672. }
  3673. else
  3674. {
  3675. Terminal->CopyToRemote(FileList, Data->RemoteDirectory, &CopyParam, Params, NULL);
  3676. SetEvent(UploadCompleteEvent);
  3677. }
  3678. }
  3679. }
  3680. __finally
  3681. {
  3682. delete FileList;
  3683. }
  3684. }
  3685. //---------------------------------------------------------------------------
  3686. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3687. const UnicodeString FileName, const TEditedFileData * Data)
  3688. {
  3689. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3690. DebugAssert(!NonVisualDataModule->Busy);
  3691. if (!IsActiveTerminal(Data->Terminal))
  3692. {
  3693. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3694. (ExtractFileName(FileName), Data->SessionName)));
  3695. }
  3696. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3697. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3698. NonVisualDataModule->StartBusy();
  3699. try
  3700. {
  3701. std::unique_ptr<TRemoteFile> File;
  3702. UnicodeString RemoteFileName =
  3703. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3704. Terminal->ExceptionOnFail = true;
  3705. try
  3706. {
  3707. TRemoteFile * AFile = NULL;
  3708. Terminal->ReadFile(RemoteFileName, AFile);
  3709. File.reset(AFile);
  3710. if (!File->HaveFullFileName)
  3711. {
  3712. File->FullFileName = RemoteFileName;
  3713. }
  3714. }
  3715. __finally
  3716. {
  3717. Terminal->ExceptionOnFail = false;
  3718. }
  3719. std::unique_ptr<TStrings> FileList(new TStringList());
  3720. FileList->AddObject(RemoteFileName, File.get());
  3721. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3722. UnicodeString TempDir = ExtractFilePath(FileName);
  3723. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, true);
  3724. // sanity check, the target file name should be still the same
  3725. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3726. }
  3727. __finally
  3728. {
  3729. NonVisualDataModule->EndBusy();
  3730. // it actually may not exist anymore...
  3731. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3732. }
  3733. }
  3734. //---------------------------------------------------------------------------
  3735. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3736. const TEditedFileData * Data, bool & KeepOpen)
  3737. {
  3738. // Command is set for external editors only (not for "shell" open).
  3739. if (!Data->Command.IsEmpty())
  3740. {
  3741. bool AnyFound = false;
  3742. bool AnyMDI = false;
  3743. bool AnyNonMDI = false;
  3744. bool AnyDetect = false;
  3745. TEditorList * EditorList = new TEditorList();
  3746. try
  3747. {
  3748. *EditorList = *WinConfiguration->EditorList;
  3749. for (int i = 0; i < EditorList->Count; i++)
  3750. {
  3751. const TEditorPreferences * Editor = EditorList->Editors[i];
  3752. if ((Editor->Data->Editor == edExternal) &&
  3753. (Editor->Data->ExternalEditor == Data->Command))
  3754. {
  3755. AnyFound = true;
  3756. if (Editor->Data->SDIExternalEditor)
  3757. {
  3758. AnyNonMDI = true;
  3759. if (Editor->Data->DetectMDIExternalEditor)
  3760. {
  3761. AnyDetect = true;
  3762. }
  3763. }
  3764. else
  3765. {
  3766. AnyMDI = true;
  3767. }
  3768. }
  3769. }
  3770. bool EnableMDI = false;
  3771. bool DisableDetect = false;
  3772. if (AnyMDI)
  3773. {
  3774. KeepOpen = true;
  3775. if (AnyNonMDI)
  3776. {
  3777. // there is at least one instance of the editor with MDI support enabled,
  3778. // and one with disabled, enable it for all instances
  3779. EnableMDI = true;
  3780. }
  3781. }
  3782. else if (AnyFound && !AnyDetect)
  3783. {
  3784. // at least once instance found but all have MDI autodetection disabled
  3785. // => close the file (default action)
  3786. }
  3787. else
  3788. {
  3789. // no instance of the editor has MDI support enabled
  3790. TMessageParams Params;
  3791. if (AnyFound)
  3792. {
  3793. // there is at least one instance of the editor
  3794. Params.Params |= mpNeverAskAgainCheck;
  3795. }
  3796. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3797. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3798. switch (Answer)
  3799. {
  3800. case qaNeverAskAgain:
  3801. DisableDetect = true;
  3802. break;
  3803. case qaNo:
  3804. EnableMDI = true;
  3805. KeepOpen = true;
  3806. break;
  3807. }
  3808. }
  3809. if (AnyFound && (EnableMDI || DisableDetect))
  3810. {
  3811. bool Changed = false;
  3812. for (int i = 0; i < EditorList->Count; i++)
  3813. {
  3814. const TEditorPreferences * Editor = EditorList->Editors[i];
  3815. if ((Editor->Data->Editor == edExternal) &&
  3816. (Editor->Data->ExternalEditor == Data->Command) &&
  3817. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3818. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3819. {
  3820. Changed = true;
  3821. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3822. if (EnableMDI)
  3823. {
  3824. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3825. }
  3826. if (DisableDetect)
  3827. {
  3828. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3829. }
  3830. EditorList->Change(i, UpdatedEditor);
  3831. }
  3832. }
  3833. if (Changed)
  3834. {
  3835. WinConfiguration->EditorList = EditorList;
  3836. }
  3837. }
  3838. }
  3839. __finally
  3840. {
  3841. delete EditorList;
  3842. }
  3843. }
  3844. else
  3845. {
  3846. // "open" case
  3847. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3848. qaOK, HELP_APP_EARLY_CLOSED);
  3849. }
  3850. }
  3851. //---------------------------------------------------------------------------
  3852. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3853. {
  3854. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3855. }
  3856. //---------------------------------------------------------------------------
  3857. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3858. {
  3859. SideEnter(osRemote);
  3860. }
  3861. //---------------------------------------------------------------------------
  3862. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3863. {
  3864. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3865. SideEnter(osRemote);
  3866. }
  3867. //---------------------------------------------------------------------------
  3868. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3869. {
  3870. FCurrentSide = Side;
  3871. if (Visible)
  3872. {
  3873. UpdateControls();
  3874. }
  3875. }
  3876. //---------------------------------------------------------------------------
  3877. void __fastcall TCustomScpExplorerForm::FileDeleted(TOperationSide Side, const UnicodeString & FileName, bool Success)
  3878. {
  3879. VisualiseOperationFinished(Side, FileName, Success);
  3880. if (DebugAlwaysTrue(FDeletedFiles != NULL) && Success)
  3881. {
  3882. FDeletedFiles->Add(FileName);
  3883. }
  3884. }
  3885. //---------------------------------------------------------------------------
  3886. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3887. TStrings * FileList, bool Alternative)
  3888. {
  3889. TCustomDirView * DView = DirView(Side);
  3890. DView->SaveSelection();
  3891. DView->SaveSelectedNames();
  3892. DebugAssert(!FAlternativeDelete);
  3893. FAlternativeDelete = Alternative;
  3894. try
  3895. {
  3896. if (!IsSideLocalBrowser(Side))
  3897. {
  3898. DebugAssert(Terminal != NULL);
  3899. // Clone the file list as it may refer to current directory files, which get destroyed, when the directory is reloaded after the operation
  3900. std::unique_ptr<TStrings> PermanentFileList(TRemoteFileList::CloneStrings(FileList));
  3901. std::unique_ptr<TStringList> DeletedFiles(new TStringList());
  3902. DeletedFiles->CaseSensitive = DebugAlwaysTrue(!IsSideLocalBrowser(Side));
  3903. int Params = FLAGMASK(Alternative, dfAlternative);
  3904. try
  3905. {
  3906. TValueRestorer<TStrings *> DeletedFilesRestorer(FDeletedFiles);
  3907. DebugAssert(FDeletedFiles == NULL);
  3908. FDeletedFiles = DeletedFiles.get();
  3909. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  3910. FOnFileOperationFinished = FileDeleted;
  3911. FMoveToQueue = false;
  3912. Terminal->DeleteFiles(FileList, Params);
  3913. // Probably not needed for deleting, just for consistency with transfer code
  3914. if (FMoveToQueue)
  3915. {
  3916. Abort();
  3917. }
  3918. }
  3919. catch (EAbort &)
  3920. {
  3921. if (FMoveToQueue)
  3922. {
  3923. DeletedFiles->Sorted = true;
  3924. for (int Index = 0; Index < PermanentFileList->Count; Index++)
  3925. {
  3926. if (DeletedFiles->IndexOf(PermanentFileList->Strings[Index]) >= 0)
  3927. {
  3928. // We should always be deleting the first item => what can be used to optimize this code, if needed
  3929. DebugAssert(Index == 0);
  3930. PermanentFileList->Delete(Index);
  3931. Index--;
  3932. }
  3933. }
  3934. Configuration->Usage->Inc("MovesToBackgroundDelete");
  3935. TQueueItem * QueueItem = new TDeleteQueueItem(Terminal, PermanentFileList.get(), Params);
  3936. AddQueueItem(Queue, QueueItem, Terminal);
  3937. ClearOperationSelection(osRemote);
  3938. }
  3939. }
  3940. }
  3941. else
  3942. {
  3943. if (IsLocalBrowserMode())
  3944. {
  3945. Configuration->Usage->Inc(L"LocalLocalDeletes");
  3946. }
  3947. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  3948. FProgressSide = FCurrentSide;
  3949. try
  3950. {
  3951. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3952. }
  3953. __finally
  3954. {
  3955. ReloadLocalDirectory();
  3956. }
  3957. }
  3958. FAlternativeDelete = false;
  3959. }
  3960. catch(...)
  3961. {
  3962. FAlternativeDelete = false;
  3963. DView->DiscardSavedSelection();
  3964. throw;
  3965. }
  3966. DView->RestoreSelection();
  3967. }
  3968. //---------------------------------------------------------------------------
  3969. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3970. {
  3971. DebugAssert(Terminal);
  3972. DebugAssert(!IsLocalBrowserMode());
  3973. RemoteDirView->SaveSelection();
  3974. RemoteDirView->SaveSelectedNames();
  3975. try
  3976. {
  3977. if (Lock)
  3978. {
  3979. Terminal->LockFiles(FileList);
  3980. }
  3981. else
  3982. {
  3983. Terminal->UnlockFiles(FileList);
  3984. }
  3985. }
  3986. catch(...)
  3987. {
  3988. RemoteDirView->DiscardSavedSelection();
  3989. throw;
  3990. }
  3991. RemoteDirView->RestoreSelection();
  3992. }
  3993. //---------------------------------------------------------------------------
  3994. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3995. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3996. bool NoConfirmation, bool Move)
  3997. {
  3998. DebugAssert(Terminal != NULL);
  3999. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  4000. DebugAssert(!IsLocalBrowserMode());
  4001. if (Session == NULL)
  4002. {
  4003. Session = Terminal;
  4004. }
  4005. if (Session == Terminal)
  4006. {
  4007. if (RemoteDriveView->DropTarget != NULL)
  4008. {
  4009. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  4010. }
  4011. else if (RemoteDirView->DropTarget != NULL)
  4012. {
  4013. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  4014. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  4015. }
  4016. else
  4017. {
  4018. Target = RemoteDirView->Path;
  4019. }
  4020. }
  4021. else
  4022. {
  4023. Target = GetSessionPath(Session, osRemote);
  4024. }
  4025. Target = UnixIncludeTrailingBackslash(Target);
  4026. if (FileList->Count == 1)
  4027. {
  4028. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  4029. }
  4030. else
  4031. {
  4032. FileMask = AnyMask;
  4033. }
  4034. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  4035. bool Result = true;
  4036. if (!NoConfirmation)
  4037. {
  4038. bool Multi = (FileList->Count > 1);
  4039. if (Move)
  4040. {
  4041. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  4042. }
  4043. else
  4044. {
  4045. std::unique_ptr<TStrings> Sessions(new TStringList);
  4046. std::unique_ptr<TStrings> Directories(new TStringList);
  4047. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  4048. for (int Index = 0; Index < SessionList->Count; Index++)
  4049. {
  4050. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  4051. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  4052. {
  4053. Sessions->AddObject(SessionList->Strings[Index], ASession);
  4054. Directories->Add(GetSessionPath(ASession, osRemote));
  4055. }
  4056. }
  4057. TDirectRemoteCopy AllowDirectCopy;
  4058. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  4059. {
  4060. DebugAssert(DirectCopy);
  4061. AllowDirectCopy = drcAllow;
  4062. }
  4063. else if (Terminal->IsCapable[fcSecondaryShell])
  4064. {
  4065. DebugAssert(DirectCopy);
  4066. AllowDirectCopy = drcConfirmCommandSession;
  4067. }
  4068. else
  4069. {
  4070. DebugAssert(!DirectCopy);
  4071. AllowDirectCopy = drcDisallow;
  4072. }
  4073. void * ASession = Session;
  4074. Result = DoRemoteCopyDialog(
  4075. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal);
  4076. Session = static_cast<TManagedTerminal *>(ASession);
  4077. }
  4078. }
  4079. return Result;
  4080. }
  4081. //---------------------------------------------------------------------------
  4082. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  4083. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  4084. {
  4085. DebugAssert(!IsLocalBrowserMode());
  4086. bool DirectCopy;
  4087. UnicodeString Target, FileMask;
  4088. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  4089. if (Result)
  4090. {
  4091. if (!Move && !DirectCopy)
  4092. {
  4093. Configuration->Usage->Inc("RemoteCopyTemp");
  4094. UnicodeString RootTempDir;
  4095. UnicodeString TempDir;
  4096. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false);
  4097. TStrings * TemporaryFilesList = new TStringList();
  4098. try
  4099. {
  4100. TMakeLocalFileListParams MakeFileListParam;
  4101. MakeFileListParam.FileList = TemporaryFilesList;
  4102. MakeFileListParam.FileTimes = NULL;
  4103. MakeFileListParam.IncludeDirs = true;
  4104. MakeFileListParam.Recursive = false;
  4105. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  4106. TTerminalManager::Instance()->ActiveSession = Session;
  4107. if (TemporaryFilesList->Count > 0)
  4108. {
  4109. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4110. CopyParam.FileMask = FileMask;
  4111. DebugAssert(!FAutoOperation);
  4112. FAutoOperation = true;
  4113. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  4114. }
  4115. }
  4116. __finally
  4117. {
  4118. delete TemporaryFilesList;
  4119. FAutoOperation = false;
  4120. if (!RootTempDir.IsEmpty())
  4121. {
  4122. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  4123. }
  4124. }
  4125. }
  4126. else
  4127. {
  4128. RemoteDirView->SaveSelection();
  4129. RemoteDirView->SaveSelectedNames();
  4130. try
  4131. {
  4132. if (Move)
  4133. {
  4134. Configuration->Usage->Inc("RemoteMove");
  4135. Terminal->MoveFiles(FileList, Target, FileMask);
  4136. }
  4137. else
  4138. {
  4139. Configuration->Usage->Inc("RemoteCopyDirect");
  4140. DebugAssert(DirectCopy);
  4141. DebugAssert(Session == Terminal);
  4142. if (Terminal->IsCapable[fcRemoteCopy] ||
  4143. Terminal->CommandSessionOpened ||
  4144. CommandSessionFallback())
  4145. {
  4146. Terminal->CopyFiles(FileList, Target, FileMask);
  4147. }
  4148. }
  4149. }
  4150. catch(...)
  4151. {
  4152. RemoteDirView->DiscardSavedSelection();
  4153. throw;
  4154. }
  4155. RemoteDirView->RestoreSelection();
  4156. }
  4157. }
  4158. return Result;
  4159. }
  4160. //---------------------------------------------------------------------------
  4161. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4162. const UnicodeString & Path, TRemoteProperties & Properties)
  4163. {
  4164. DebugAssert(!IsLocalBrowserMode());
  4165. Properties.Valid = Properties.Valid << vpEncrypt;
  4166. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4167. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4168. }
  4169. //---------------------------------------------------------------------------
  4170. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4171. {
  4172. Side = GetSide(Side);
  4173. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4174. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4175. UnicodeString Name = LoadStr(NEW_FOLDER);
  4176. int AllowedChanges =
  4177. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4178. bool SaveSettings = false;
  4179. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4180. {
  4181. TWindowLock Lock(this);
  4182. if (!IsSideLocalBrowser(Side))
  4183. {
  4184. if (SaveSettings)
  4185. {
  4186. GUIConfiguration->NewDirectoryProperties = Properties;
  4187. }
  4188. CreateRemoteDirectory(Name, Properties);
  4189. }
  4190. else
  4191. {
  4192. DirView(Side)->CreateDirectory(Name);
  4193. }
  4194. }
  4195. }
  4196. //---------------------------------------------------------------------------
  4197. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4198. {
  4199. TWindowLock Lock(this);
  4200. DirView(Side)->ExecuteHomeDirectory();
  4201. }
  4202. //---------------------------------------------------------------------------
  4203. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4204. {
  4205. DoOpenDirectoryDialog(odBrowse, Side);
  4206. }
  4207. //---------------------------------------------------------------------------
  4208. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4209. {
  4210. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4211. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4212. bool Result = !Path.IsEmpty();
  4213. if (Result)
  4214. {
  4215. TryOpenDirectory(Side, Path);
  4216. }
  4217. return Result;
  4218. }
  4219. //---------------------------------------------------------------------------
  4220. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4221. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4222. bool & Close)
  4223. {
  4224. // terminal can be already closed (e.g. dropped connection)
  4225. if (Terminal != NULL)
  4226. {
  4227. try
  4228. {
  4229. TCalculateSizeParams Params;
  4230. Params.Stats = &Stats;
  4231. Terminal->CalculateFilesSize(FileList, Size, Params);
  4232. }
  4233. catch(...)
  4234. {
  4235. if (!Terminal->Active)
  4236. {
  4237. Close = true;
  4238. }
  4239. throw;
  4240. }
  4241. }
  4242. }
  4243. //---------------------------------------------------------------------------
  4244. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4245. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4246. bool & Close)
  4247. {
  4248. if ((Terminal != NULL) && // terminal can be already closed (e.g. dropped connection)
  4249. EnsureCommandSessionFallback(fcCalculatingChecksum))
  4250. {
  4251. Configuration->Usage->Inc(L"ChecksumCalculated");
  4252. try
  4253. {
  4254. Terminal->CalculateFilesChecksum(Alg, FileList, OnCalculatedChecksum);
  4255. }
  4256. catch(...)
  4257. {
  4258. if (!Terminal->Active)
  4259. {
  4260. Close = true;
  4261. }
  4262. throw;
  4263. }
  4264. }
  4265. }
  4266. //---------------------------------------------------------------------------
  4267. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4268. {
  4269. if (Terminal->LoadFilesProperties(FileList))
  4270. {
  4271. RemoteDirView->Invalidate();
  4272. }
  4273. }
  4274. //---------------------------------------------------------------------------
  4275. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4276. {
  4277. bool Result;
  4278. if (!IsSideLocalBrowser(Side))
  4279. {
  4280. TRemoteTokenList * GroupList = NULL;
  4281. TRemoteTokenList * UserList = NULL;
  4282. try
  4283. {
  4284. TRemoteProperties CurrentProperties;
  4285. LoadFilesProperties(FileList);
  4286. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4287. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4288. if (CapableGroupChanging || CapableOwnerChanging)
  4289. {
  4290. if (CapableGroupChanging)
  4291. {
  4292. GroupList = Terminal->Groups->Duplicate();
  4293. }
  4294. if (CapableOwnerChanging)
  4295. {
  4296. UserList = Terminal->Users->Duplicate();
  4297. }
  4298. TRemoteDirectory * Files = Terminal->Files;
  4299. int Count = Files->Count;
  4300. if (Count > 100)
  4301. {
  4302. Count = 100;
  4303. }
  4304. for (int Index = 0; Index < Count; Index++)
  4305. {
  4306. TRemoteFile * File = Files->Files[Index];
  4307. if (CapableGroupChanging)
  4308. {
  4309. GroupList->AddUnique(File->Group);
  4310. }
  4311. if (CapableOwnerChanging)
  4312. {
  4313. UserList->AddUnique(File->Owner);
  4314. }
  4315. }
  4316. // if we haven't collected tokens for all files in current directory,
  4317. // make sure we collect them at least for all selected files.
  4318. // (note that so far the files in FileList has to be from current direcotry)
  4319. if (Count < Files->Count)
  4320. {
  4321. for (int Index = 0; Index < FileList->Count; Index++)
  4322. {
  4323. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4324. if (CapableGroupChanging)
  4325. {
  4326. GroupList->AddUnique(File->Group);
  4327. }
  4328. if (CapableOwnerChanging)
  4329. {
  4330. UserList->AddUnique(File->Owner);
  4331. }
  4332. }
  4333. }
  4334. }
  4335. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4336. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4337. for (int Index = 0; (Index < FileList->Count) && CapableAclChanging; Index++)
  4338. {
  4339. if (dynamic_cast<TRemoteFile *>(FileList->Objects[Index])->IsDirectory)
  4340. {
  4341. CapableAclChanging = false;
  4342. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4343. }
  4344. }
  4345. int Flags = 0;
  4346. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4347. if (CapableAclChanging) Flags |= cpAcl;
  4348. if (CapableOwnerChanging) Flags |= cpOwner;
  4349. if (CapableGroupChanging) Flags |= cpGroup;
  4350. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4351. if (Terminal->IsCapable[fcCalculatingChecksum] ||
  4352. (Terminal->IsCapable[fcSecondaryShell] && !Terminal->IsEncryptingFiles()))
  4353. {
  4354. CalculateChecksumEvent = CalculateChecksum;
  4355. }
  4356. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4357. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4358. TRemoteProperties NewProperties = CurrentProperties;
  4359. Result =
  4360. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4361. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4362. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4363. CalculateSize, CalculateChecksumEvent);
  4364. if (Result)
  4365. {
  4366. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4367. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4368. }
  4369. }
  4370. __finally
  4371. {
  4372. delete GroupList;
  4373. delete UserList;
  4374. }
  4375. }
  4376. else
  4377. {
  4378. DirView(Side)->DisplayPropertiesMenu();
  4379. Result = true;
  4380. }
  4381. return Result;
  4382. }
  4383. //---------------------------------------------------------------------------
  4384. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4385. {
  4386. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4387. {
  4388. FIgnoreNextDialogChar = Key;
  4389. }
  4390. Key = 0;
  4391. }
  4392. //---------------------------------------------------------------------------
  4393. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4394. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4395. {
  4396. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4397. if (Command != NULL)
  4398. {
  4399. KeyProcessed(Key, Shift);
  4400. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4401. {
  4402. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4403. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4404. false, false, const_cast<TCustomCommandType *>(Command));
  4405. }
  4406. }
  4407. }
  4408. //---------------------------------------------------------------------------
  4409. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4410. {
  4411. if (!NonVisualDataModule->Busy)
  4412. {
  4413. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4414. {
  4415. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4416. }
  4417. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4418. {
  4419. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4420. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4421. {
  4422. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4423. // But so far it's not a problem, as it does what we want.
  4424. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4425. if (((Action->ShortCut == KeyShortCut) ||
  4426. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4427. AllowedAction(Action, aaShortCut))
  4428. {
  4429. // Has to be called before the action as the dialog char is already in queue.
  4430. // So when the action consumes message queue, we already need to have the
  4431. // FIgnoreNextDialogChar set
  4432. KeyProcessed(Key, Shift);
  4433. // Reset reference to previous component (when menu/toolbar was clicked).
  4434. // Needed to detect that action was invoked by keyboard shortcut
  4435. // in TNonVisualDataModule::ExplorerActionsExecute
  4436. Action->ActionComponent = NULL;
  4437. Action->Execute();
  4438. return;
  4439. }
  4440. }
  4441. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4442. {
  4443. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4444. {
  4445. KeyProcessed(Key, Shift);
  4446. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4447. return;
  4448. }
  4449. }
  4450. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4451. {
  4452. KeyProcessed(Key, Shift);
  4453. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4454. }
  4455. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4456. if (IsCustomShortCut(CustomShortCut))
  4457. {
  4458. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4459. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4460. if (WinConfiguration->SharedBookmarks != NULL)
  4461. {
  4462. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4463. if ((Bookmark != NULL) &&
  4464. OpenBookmark(FCurrentSide, Bookmark))
  4465. {
  4466. KeyProcessed(Key, Shift);
  4467. }
  4468. }
  4469. }
  4470. }
  4471. }
  4472. TForm::KeyDown(Key, Shift);
  4473. }
  4474. //---------------------------------------------------------------------------
  4475. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4476. {
  4477. bool OpenedTerminal = false;
  4478. UnicodeString ProtocolName;
  4479. UnicodeString ProtocolNameHint;
  4480. UnicodeString SecurityProtocolName;
  4481. UnicodeString DurationHint;
  4482. // See the comment in TScpCommanderForm::UpdateControls()
  4483. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4484. if (ActiveTerminal != NULL)
  4485. {
  4486. // Active is not enough here
  4487. if (ActiveTerminal->Status == ssOpened)
  4488. {
  4489. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4490. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4491. if (FileSystemInfo.ProtocolName.IsEmpty())
  4492. {
  4493. ProtocolName = SessionInfo.ProtocolName;
  4494. }
  4495. else
  4496. {
  4497. ProtocolName = FileSystemInfo.ProtocolName;
  4498. }
  4499. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4500. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4501. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4502. OpenedTerminal = true;
  4503. }
  4504. else if (ActiveTerminal->Status > ssClosed)
  4505. {
  4506. ProtocolName = LoadStr(STATUS_CONNECTING);
  4507. }
  4508. else
  4509. {
  4510. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4511. }
  4512. }
  4513. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4514. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4515. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4516. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4517. if (ProtocolStatusPanel->Caption != ProtocolName)
  4518. {
  4519. ProtocolStatusPanel->Caption = ProtocolName;
  4520. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4521. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4522. }
  4523. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4524. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4525. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4526. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4527. SecurityStatusPanel->Enabled = SecurityEnabled;
  4528. UnicodeString SecurityProtocolHint;
  4529. if (SecurityEnabled)
  4530. {
  4531. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4532. }
  4533. else
  4534. {
  4535. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4536. }
  4537. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4538. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4539. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4540. DurationStatusPanel->Hint = DurationHint;
  4541. UpdateStatusBar();
  4542. }
  4543. //---------------------------------------------------------------------------
  4544. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4545. {
  4546. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4547. DebugAssert(SessionStatusBar != NULL);
  4548. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4549. if (!FNote.IsEmpty())
  4550. {
  4551. NoteStatusPanel->Caption = FNote;
  4552. }
  4553. else
  4554. {
  4555. UpdateStatusPanelText(NoteStatusPanel);
  4556. }
  4557. NoteStatusPanel->Hint = FNoteHints;
  4558. UnicodeString LoginTime;
  4559. // See the comment in TScpCommanderForm::UpdateControls()
  4560. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4561. // Active is not enough here
  4562. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4563. {
  4564. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4565. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4566. }
  4567. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4568. DurationStatusPanel->Caption = LoginTime;
  4569. }
  4570. //---------------------------------------------------------------------------
  4571. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4572. {
  4573. Panel->Caption = L"";
  4574. }
  4575. //---------------------------------------------------------------------------
  4576. void __fastcall TCustomScpExplorerForm::Idle()
  4577. {
  4578. if (FShowing ||
  4579. // Particularly for
  4580. // - Standalone editing
  4581. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4582. // while "keeping remote directory up to date".
  4583. StandaloneOperation)
  4584. {
  4585. FEditorManager->Check();
  4586. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4587. // that needs to know if queue view is visible (and it may be closed after queue update)
  4588. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4589. }
  4590. if (FShowing)
  4591. {
  4592. if (!NonVisualDataModule->Busy &&
  4593. // Menu is opened or drag&drop is going on
  4594. (Mouse->Capture == NULL))
  4595. {
  4596. if (FRefreshRemoteDirectory)
  4597. {
  4598. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4599. {
  4600. Terminal->RefreshDirectory();
  4601. }
  4602. FRefreshRemoteDirectory = false;
  4603. }
  4604. if (FRefreshLocalDirectory)
  4605. {
  4606. ReloadLocalDirectory();
  4607. FRefreshLocalDirectory = false;
  4608. }
  4609. if (WinConfiguration->RefreshRemotePanel)
  4610. {
  4611. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4612. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4613. {
  4614. RemoteDirView->ReloadDirectory();
  4615. }
  4616. }
  4617. }
  4618. // TB2 shows accels on Alt key down, but hides it only after menu modal loop finishes.
  4619. // But if Alt is used to switch apps or for keyboard shortcut, the menu modal loop does not happen and accelerators stay displayed.
  4620. // It's difficult to fix this in TB2, as there's no "up" equivalent to CM_DIALOGKEY, which shows the accels.
  4621. // And the Alt key can be released even when other window or application is active, so handling the key up in explorer window won't do either.
  4622. TTBCustomToolbar * MenuToolbar = DebugNotNull(dynamic_cast<TTBCustomToolbar *>(GetComponent(fcMenu)));
  4623. MenuToolbar->Idle();
  4624. }
  4625. if (FShowing || StandaloneOperation)
  4626. {
  4627. if (FQueueStatusInvalidated)
  4628. {
  4629. UpdateQueueStatus(false);
  4630. }
  4631. RefreshQueueItems();
  4632. }
  4633. if (FShowing)
  4634. {
  4635. UpdateStatusBar();
  4636. }
  4637. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4638. {
  4639. FClipboardFakeMonitorsPendingReset = false;
  4640. FClipboardFakeMonitors.reset(NULL);
  4641. }
  4642. FIgnoreNextDialogChar = 0;
  4643. }
  4644. //---------------------------------------------------------------------------
  4645. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4646. {
  4647. try
  4648. {
  4649. FUserActionTimer->Enabled = false;
  4650. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4651. {
  4652. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4653. {
  4654. FPendingQueueActionItem->ProcessUserAction();
  4655. }
  4656. }
  4657. }
  4658. __finally
  4659. {
  4660. FPendingQueueActionItem = NULL;
  4661. }
  4662. }
  4663. //---------------------------------------------------------------------------
  4664. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4665. {
  4666. if (WinConfiguration->MinimizeToTray)
  4667. {
  4668. UpdateTrayIcon();
  4669. FTrayIcon->Visible = true;
  4670. if (Visible)
  4671. {
  4672. ShowWindow(Handle, SW_HIDE);
  4673. }
  4674. }
  4675. }
  4676. //---------------------------------------------------------------------------
  4677. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4678. {
  4679. // WORKAROUND
  4680. // When restoring maximized window from minimization,
  4681. // rarely some controls do not align properly.
  4682. // Two instances seen (both for Commander):
  4683. // - When restoring, window is temporarily narrower (not maximized),
  4684. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4685. // Once maximized already, top dock shinks back, but the session PageControl,
  4686. // do not align up, leaving space between TopDock and PageControl.
  4687. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4688. for (int Index = 0; Index < ControlCount; Index++)
  4689. {
  4690. RealignControl(Controls[Index]);
  4691. }
  4692. if (FTrayIcon->Visible)
  4693. {
  4694. FTrayIcon->Visible = false;
  4695. if (Visible)
  4696. {
  4697. ShowWindow(Handle, SW_SHOW);
  4698. }
  4699. }
  4700. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4701. {
  4702. FNeedSession = false;
  4703. NonVisualDataModule->StartBusy();
  4704. try
  4705. {
  4706. NeedSession(true);
  4707. }
  4708. __finally
  4709. {
  4710. NonVisualDataModule->EndBusy();
  4711. }
  4712. }
  4713. }
  4714. //---------------------------------------------------------------------------
  4715. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4716. {
  4717. FTrayIcon->Hint = GetMainForm()->Caption;
  4718. }
  4719. //---------------------------------------------------------------------------
  4720. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4721. {
  4722. UpdateTrayIcon();
  4723. }
  4724. //---------------------------------------------------------------------------
  4725. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4726. {
  4727. // workaround
  4728. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4729. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4730. // But WM_ACTIVATEAPP message can be received even when
  4731. // the main window is minimized to the tray and internal editor window is focused
  4732. // (after another application was previously active)
  4733. if (::IsIconic(Handle))
  4734. {
  4735. if (!IsAppIconic())
  4736. {
  4737. SetAppIconic(true);
  4738. }
  4739. }
  4740. ::ApplicationRestore();
  4741. Application->BringToFront();
  4742. }
  4743. //---------------------------------------------------------------------------
  4744. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4745. {
  4746. RestoreApp();
  4747. }
  4748. //---------------------------------------------------------------------------
  4749. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4750. {
  4751. if (OpenInNewWindow())
  4752. {
  4753. ExecuteNewInstance(SessionUrl);
  4754. }
  4755. else
  4756. {
  4757. TTerminalManager::Instance()->NewSession(SessionUrl);
  4758. }
  4759. }
  4760. //---------------------------------------------------------------------------
  4761. void TCustomScpExplorerForm::NewTab(TOperationSide)
  4762. {
  4763. NewSession();
  4764. }
  4765. //---------------------------------------------------------------------------
  4766. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4767. {
  4768. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4769. StoredSessions->NewSession(SessionName, SessionData);
  4770. // modified only, explicit
  4771. StoredSessions->Save(false, true);
  4772. // encode session name because of slashes in hierarchical sessions
  4773. return EncodeUrlString(SessionName);
  4774. }
  4775. //---------------------------------------------------------------------------
  4776. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4777. {
  4778. // current working directories become defaults here, what is not right
  4779. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4780. return SaveHiddenDuplicateSession(SessionData.get());
  4781. }
  4782. //---------------------------------------------------------------------------
  4783. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4784. {
  4785. if (OpenInNewWindow())
  4786. {
  4787. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4788. }
  4789. else
  4790. {
  4791. // current working directories become defaults here, what is not right
  4792. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4793. TTerminalManager * Manager = TTerminalManager::Instance();
  4794. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4795. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4796. // We definitelly want these
  4797. ASession->Disconnected = ManagedSession->Disconnected;
  4798. // Not sure about these two
  4799. ASession->Permanent = ManagedSession->Permanent;
  4800. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4801. Manager->ActiveSession = ASession;
  4802. }
  4803. }
  4804. //---------------------------------------------------------------------------
  4805. void __fastcall TCustomScpExplorerForm::RenameSession()
  4806. {
  4807. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4808. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME) &&
  4809. // When submitting the default name, do not fix it
  4810. (Name != ManagedSession->SessionData->SessionName))
  4811. {
  4812. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4813. TSessionData::ValidateName(Name);
  4814. // This is inconsistent with how color (for example) is handled.
  4815. ManagedSession->SessionData->Name = Name;
  4816. UpdateControls();
  4817. // Add/Remove distinguishing paths from sessions of the same name.
  4818. SessionListChanged();
  4819. }
  4820. }
  4821. //---------------------------------------------------------------------------
  4822. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4823. {
  4824. DebugAssert(Queue != NULL);
  4825. bool Result = Queue->IsEmpty;
  4826. if (!Result)
  4827. {
  4828. SetFocus();
  4829. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4830. }
  4831. return Result;
  4832. }
  4833. //---------------------------------------------------------------------------
  4834. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4835. {
  4836. return CanCloseQueue(FQueue);
  4837. }
  4838. //---------------------------------------------------------------------------
  4839. void __fastcall TCustomScpExplorerForm::CloseSession()
  4840. {
  4841. if (CanCloseQueue())
  4842. {
  4843. TTerminalManager::Instance()->FreeActiveTerminal();
  4844. }
  4845. }
  4846. //---------------------------------------------------------------------------
  4847. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4848. {
  4849. if (CanCloseQueue())
  4850. {
  4851. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4852. }
  4853. }
  4854. //---------------------------------------------------------------------------
  4855. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4856. {
  4857. DetachTerminal(Terminal);
  4858. RemoteDirView->Terminal = Terminal;
  4859. UpdateRemotePathComboBox(false);
  4860. InitStatusBar();
  4861. UpdateControls();
  4862. }
  4863. //---------------------------------------------------------------------------
  4864. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4865. {
  4866. InitStatusBar();
  4867. }
  4868. //---------------------------------------------------------------------------
  4869. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4870. {
  4871. if (DebugAlwaysTrue(!Terminal->Active))
  4872. {
  4873. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4874. }
  4875. }
  4876. //---------------------------------------------------------------------------
  4877. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4878. {
  4879. if (OpenInNewWindow())
  4880. {
  4881. // encode session name because of slashes in hierarchical sessions
  4882. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4883. }
  4884. else
  4885. {
  4886. TTerminalManager * Manager = TTerminalManager::Instance();
  4887. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4888. Manager->ActiveSession = Terminal;
  4889. }
  4890. }
  4891. //---------------------------------------------------------------------------
  4892. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  4893. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  4894. {
  4895. TTerminalManager * Manager = TTerminalManager::Instance();
  4896. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4897. // This asks for a master password. Maybe we should postpone asking until a connection is actually opened.
  4898. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4899. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  4900. {
  4901. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  4902. // FirstSession can be null, if some of the
  4903. if (!ConnectFirstTerminal && (FirstSession != NULL))
  4904. {
  4905. FirstSession->Disconnected = true;
  4906. FirstSession->DisconnectedTemporarily = true;
  4907. }
  4908. Manager->ActiveSession = FirstSession;
  4909. }
  4910. }
  4911. //---------------------------------------------------------------------------
  4912. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4913. {
  4914. if (OpenInNewWindow())
  4915. {
  4916. ExecuteNewInstance(Name);
  4917. }
  4918. else
  4919. {
  4920. DoOpenFolderOrWorkspace(Name, true, false);
  4921. }
  4922. }
  4923. //---------------------------------------------------------------------------
  4924. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4925. bool &CanClose)
  4926. {
  4927. // See the comment in CloseApp()
  4928. if (NonVisualDataModule->Busy)
  4929. {
  4930. CanClose = false;
  4931. }
  4932. else
  4933. {
  4934. TTerminalManager * Manager = TTerminalManager::Instance();
  4935. int ActiveSessions = 0;
  4936. for (int Index = 0; Index < Manager->Count; Index++)
  4937. {
  4938. if (Manager->Sessions[Index]->Active)
  4939. {
  4940. ActiveSessions++;
  4941. }
  4942. }
  4943. // Confirm, when there's at least one active remote session or
  4944. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4945. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4946. WinConfiguration->ConfirmClosingSession)
  4947. {
  4948. unsigned int Result;
  4949. TMessageParams Params(mpNeverAskAgainCheck);
  4950. UnicodeString Message;
  4951. int Answers = qaOK | qaCancel;
  4952. // The current session is the only active session
  4953. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4954. {
  4955. if (!WinConfiguration->AutoSaveWorkspace)
  4956. {
  4957. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4958. }
  4959. else
  4960. {
  4961. Message = LoadStr(CLOSE_SESSION);
  4962. }
  4963. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4964. }
  4965. // Multiple active sessions or one active session, but it's not the current one
  4966. else if (ActiveSessions > 0)
  4967. {
  4968. if (!WinConfiguration->AutoSaveWorkspace)
  4969. {
  4970. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4971. }
  4972. else
  4973. {
  4974. Message = LoadStr(CLOSE_SESSIONS);
  4975. }
  4976. }
  4977. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  4978. else
  4979. {
  4980. DebugAlwaysTrue(Manager->Count > 1);
  4981. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  4982. Message = LoadStr(CLOSE_WORKSPACE);
  4983. }
  4984. UnicodeString Note;
  4985. if (WinConfiguration->AutoSaveWorkspace)
  4986. {
  4987. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4988. }
  4989. else
  4990. {
  4991. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4992. Answers = qaYes | qaNo | qaCancel;
  4993. }
  4994. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4995. SetFocus();
  4996. Result = MessageDialog(Message, qtConfirmation,
  4997. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4998. if (Result == qaNeverAskAgain)
  4999. {
  5000. WinConfiguration->ConfirmClosingSession = false;
  5001. }
  5002. if (Result == qaNo)
  5003. {
  5004. CanClose = SaveWorkspace(true);
  5005. // note that the workspace will be saved redundatly again from FormClose
  5006. }
  5007. else
  5008. {
  5009. CanClose =
  5010. (Result == qaOK) ||
  5011. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  5012. (Result == qaNeverAskAgain);
  5013. }
  5014. }
  5015. if (CanClose && HasActiveTerminal())
  5016. {
  5017. CanClose = CanCloseQueue();
  5018. }
  5019. }
  5020. if (CanClose)
  5021. {
  5022. CanClose =
  5023. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  5024. FEditorManager->CloseExternalFilesWithoutProcess();
  5025. if (CanClose)
  5026. {
  5027. while (CanClose && (FLocalEditors->Count > 0))
  5028. {
  5029. int PrevCount = FLocalEditors->Count;
  5030. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  5031. CanClose = (FLocalEditors->Count < PrevCount);
  5032. }
  5033. if (CanClose)
  5034. {
  5035. CanClose = FEditorManager->Empty(true);
  5036. if (!CanClose)
  5037. {
  5038. SetFocus();
  5039. CanClose =
  5040. (MessageDialog(
  5041. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  5042. }
  5043. }
  5044. }
  5045. }
  5046. }
  5047. //---------------------------------------------------------------------------
  5048. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  5049. {
  5050. TForm * Form = dynamic_cast<TForm *>(Sender);
  5051. DebugAssert(Form != NULL);
  5052. Form->Close();
  5053. }
  5054. //---------------------------------------------------------------------------
  5055. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  5056. {
  5057. TForm * Form = dynamic_cast<TForm *>(Sender);
  5058. delete Form;
  5059. }
  5060. //---------------------------------------------------------------------------
  5061. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  5062. {
  5063. while (FLocalEditors->Count > 0)
  5064. {
  5065. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  5066. }
  5067. }
  5068. //---------------------------------------------------------------------------
  5069. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  5070. TObject *Sender)
  5071. {
  5072. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  5073. try
  5074. {
  5075. SetProperties(osRemote, FileList);
  5076. }
  5077. __finally
  5078. {
  5079. delete FileList;
  5080. }
  5081. }
  5082. //---------------------------------------------------------------------------
  5083. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  5084. {
  5085. if (value)
  5086. {
  5087. if (Component == fcCustomCommandsBand)
  5088. {
  5089. UpdateCustomCommandsToolbar();
  5090. }
  5091. if (Component == fcQueueFileList)
  5092. {
  5093. DebugAssert(!QueueFileListSplitter->Visible);
  5094. AdjustQueueLayout();
  5095. UpdateQueueFileList();
  5096. }
  5097. }
  5098. }
  5099. //---------------------------------------------------------------------------
  5100. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  5101. {
  5102. int MinHeightLimit =
  5103. GetStaticQueuePanelComponentsHeight() +
  5104. QueueFileListSplitter->Height +
  5105. QueueFileList->Height +
  5106. GetMinQueueViewHeight();
  5107. if (QueuePanel->ClientHeight < MinHeightLimit)
  5108. {
  5109. QueuePanel->ClientHeight = MinHeightLimit;
  5110. }
  5111. }
  5112. //---------------------------------------------------------------------------
  5113. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  5114. {
  5115. TControl * Control = GetComponent(Component);
  5116. DebugAssert(Control);
  5117. bool Changed = (Control->Visible != value);
  5118. if (Changed)
  5119. {
  5120. ComponentShowing(Component, value);
  5121. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  5122. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  5123. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  5124. if (value)
  5125. {
  5126. int RemainingHeight = Control->Parent->ClientHeight;
  5127. int RemainingWidth = Control->Parent->ClientWidth;
  5128. for (int i = 0; i < Control->Parent->ControlCount; i++)
  5129. {
  5130. TControl * ChildControl = Control->Parent->Controls[i];
  5131. if (ChildControl->Visible)
  5132. {
  5133. switch (ChildControl->Align)
  5134. {
  5135. case alTop:
  5136. case alBottom:
  5137. RemainingHeight -= ChildControl->Height;
  5138. break;
  5139. case alLeft:
  5140. case alRight:
  5141. RemainingWidth -= ChildControl->Width;
  5142. break;
  5143. }
  5144. }
  5145. }
  5146. int Reserve = ScaleByTextHeight(this, 32);
  5147. // queue in explorer, trees in commander
  5148. if (Control->Height > RemainingHeight - Reserve)
  5149. {
  5150. Control->Height = RemainingHeight / 2;
  5151. }
  5152. if (Control->Width > RemainingWidth - Reserve)
  5153. {
  5154. Control->Width = RemainingWidth / 2;
  5155. }
  5156. }
  5157. Control->Visible = value;
  5158. if (WasFocused && Visible)
  5159. {
  5160. DirView(osCurrent)->SetFocus();
  5161. }
  5162. FixControlsPlacement();
  5163. }
  5164. }
  5165. //---------------------------------------------------------------------------
  5166. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  5167. {
  5168. TControl * Control = GetComponent(Component);
  5169. if (Control == NULL)
  5170. {
  5171. return false;
  5172. }
  5173. else
  5174. {
  5175. return Control->Visible;
  5176. }
  5177. }
  5178. //---------------------------------------------------------------------------
  5179. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  5180. {
  5181. bool Result;
  5182. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  5183. {
  5184. Result = !IsUWP();
  5185. }
  5186. else
  5187. {
  5188. Result = true;
  5189. }
  5190. return Result;
  5191. }
  5192. //---------------------------------------------------------------------------
  5193. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  5194. {
  5195. if (DirView->ItemFocused != NULL)
  5196. {
  5197. DirView->ItemFocused->MakeVisible(false);
  5198. }
  5199. }
  5200. //---------------------------------------------------------------------------
  5201. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5202. {
  5203. MakeFocusedItemVisible(RemoteDirView);
  5204. QueueSplitter->Visible = QueuePanel->Visible;
  5205. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5206. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5207. TControl * TopControlsOrder[] =
  5208. { TopDock, MessageDock, SessionsPageControl };
  5209. SetVerticalControlsOrder(TopControlsOrder, LENOF(TopControlsOrder));
  5210. TControl * QueueControlsOrder[] =
  5211. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5212. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5213. }
  5214. //---------------------------------------------------------------------------
  5215. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5216. {
  5217. switch (Component) {
  5218. case fcStatusBar: return RemoteStatusBar;
  5219. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5220. case fcQueueView: return QueuePanel;
  5221. case fcQueueToolbar: return QueueDock;
  5222. case fcRemoteTree: return RemoteDrivePanel;
  5223. case fcSessionsTabs: return SessionsPageControl;
  5224. case fcQueueFileList: return QueueFileList;
  5225. default: return NULL;
  5226. }
  5227. }
  5228. //---------------------------------------------------------------------------
  5229. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5230. TObject *Sender, TListColumn *Column, TPoint &Point)
  5231. {
  5232. DebugAssert(NonVisualDataModule && Column && Sender);
  5233. NonVisualDataModule->ListColumn = Column;
  5234. TPopupMenu * DirViewColumnMenu;
  5235. if (Sender == DirView(osOther))
  5236. {
  5237. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5238. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  5239. (Column->Index == uvName);
  5240. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  5241. (Column->Index == uvSize);
  5242. }
  5243. else
  5244. {
  5245. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5246. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  5247. (Column->Index == dvName);
  5248. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  5249. (Column->Index == dvSize);
  5250. }
  5251. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5252. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5253. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5254. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5255. }
  5256. //---------------------------------------------------------------------------
  5257. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5258. TObject *Sender, TListItem *Item, bool &AllowExec)
  5259. {
  5260. DoDirViewExecFile(Sender, Item, AllowExec);
  5261. }
  5262. //---------------------------------------------------------------------------
  5263. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5264. TListItem * Item, bool & AllowExec)
  5265. {
  5266. DebugAssert(Sender && Item && Configuration);
  5267. DebugAssert(AllowExec);
  5268. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5269. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5270. bool Remote = !IsSideLocalBrowser(Side);
  5271. DebugAssert(Item == ADirView->ItemFocused);
  5272. bool IsDirectory = ADirView->ItemIsDirectory(Item);
  5273. TResolvedDoubleClickAction Action;
  5274. if (FForceExecution)
  5275. {
  5276. Action = IsDirectory ? rdcaChangeDir : rdcaOpen;
  5277. }
  5278. else
  5279. {
  5280. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  5281. Action = WinConfiguration->ResolveDoubleClickAction(IsDirectory, ATerminal);
  5282. }
  5283. if (Action == rdcaCopy)
  5284. {
  5285. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5286. UnlockWindow();
  5287. try
  5288. {
  5289. if (IsLocalBrowserMode())
  5290. {
  5291. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5292. }
  5293. else
  5294. {
  5295. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5296. }
  5297. }
  5298. __finally
  5299. {
  5300. LockWindow();
  5301. }
  5302. AllowExec = false;
  5303. }
  5304. else if (Action == rdcaEdit)
  5305. {
  5306. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5307. {
  5308. ExecuteFile(Side, efDefaultEditor);
  5309. AllowExec = false;
  5310. }
  5311. }
  5312. else if (Action == rdcaOpen)
  5313. {
  5314. if (DebugAlwaysTrue(!IsDirectory) && Remote && !WinConfiguration->DisableOpenEdit)
  5315. {
  5316. ExecuteFile(osRemote, efShell);
  5317. AllowExec = false;
  5318. }
  5319. }
  5320. }
  5321. //---------------------------------------------------------------------------
  5322. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5323. {
  5324. return ((Side == osRemote) || (Side == osCurrent));
  5325. }
  5326. //---------------------------------------------------------------------------
  5327. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5328. {
  5329. DebugFail();
  5330. }
  5331. //---------------------------------------------------------------------------
  5332. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5333. {
  5334. DebugFail();
  5335. }
  5336. //---------------------------------------------------------------------------
  5337. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5338. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5339. {
  5340. TSynchronizeParamType Params;
  5341. Params.LocalDirectory = LocalDirectory;
  5342. Params.RemoteDirectory = RemoteDirectory;
  5343. int UnusedParams =
  5344. (GUIConfiguration->SynchronizeParams &
  5345. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5346. TTerminal::spCaseSensitive));
  5347. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5348. Params.Options = GUIConfiguration->SynchronizeOptions;
  5349. bool SaveSettings = false;
  5350. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5351. &DoSynchronizeTooManyDirectories);
  5352. DebugAssert(FSynchronizeController == NULL);
  5353. FSynchronizeController = &Controller;
  5354. bool Result;
  5355. try
  5356. {
  5357. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5358. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5359. int Options =
  5360. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5361. DebugAssert(FOnFeedSynchronizeError == NULL);
  5362. DebugAssert(FOnSynchronizeAbort == NULL);
  5363. DebugAssert(FSynchronizeTerminal == NULL);
  5364. FSynchronizeTerminal = Terminal;
  5365. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5366. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5367. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5368. if (Result)
  5369. {
  5370. if (SaveSettings)
  5371. {
  5372. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5373. GUIConfiguration->SynchronizeOptions = Params.Options;
  5374. }
  5375. else
  5376. {
  5377. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5378. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5379. FLAGSET(Params.Options, soSynchronize))
  5380. {
  5381. GUIConfiguration->SynchronizeOptions =
  5382. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5383. soSynchronize;
  5384. }
  5385. }
  5386. LocalDirectory = Params.LocalDirectory;
  5387. RemoteDirectory = Params.RemoteDirectory;
  5388. }
  5389. }
  5390. __finally
  5391. {
  5392. FSynchronizeController = NULL;
  5393. DebugAssert(FOnFeedSynchronizeError == NULL);
  5394. FOnFeedSynchronizeError = NULL;
  5395. DebugAssert(FOnSynchronizeAbort == NULL);
  5396. FOnSynchronizeAbort = NULL;
  5397. FSynchronizeTerminal = NULL;
  5398. }
  5399. return Result;
  5400. }
  5401. //---------------------------------------------------------------------------
  5402. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5403. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5404. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5405. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5406. TSynchronizeOptions * Options, bool Full)
  5407. {
  5408. // Keep-up-to-date mode
  5409. if (Terminal->Status == ssOpened)
  5410. {
  5411. try
  5412. {
  5413. int PParams = Params.Params;
  5414. if (!Full)
  5415. {
  5416. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5417. if (FLAGSET(Params.Options, soRecurse))
  5418. {
  5419. PParams |= TTerminal::spSubDirs;
  5420. }
  5421. }
  5422. else
  5423. {
  5424. // if keepuptodate is non-recursive,
  5425. // full sync before has to be non-recursive as well
  5426. if (FLAGCLEAR(Params.Options, soRecurse))
  5427. {
  5428. PParams |= TTerminal::spNoRecurse;
  5429. }
  5430. }
  5431. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5432. PParams, Checklist, Options);
  5433. }
  5434. catch (Exception & E)
  5435. {
  5436. if (!Terminal->Active)
  5437. {
  5438. ShowExtendedExceptionEx(Terminal, &E);
  5439. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5440. if (!Terminal->Active)
  5441. {
  5442. throw;
  5443. }
  5444. }
  5445. else
  5446. {
  5447. if (FLAGSET(Params.Options, soContinueOnError) &&
  5448. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5449. {
  5450. // noop, already logged in MoreMessageDialog
  5451. }
  5452. else
  5453. {
  5454. // We get mostly EAbort here, so this is noop.
  5455. // Exception is an error when listing directories while looking for differences.
  5456. ShowExtendedExceptionEx(Terminal, &E);
  5457. throw;
  5458. }
  5459. }
  5460. }
  5461. }
  5462. }
  5463. //---------------------------------------------------------------------------
  5464. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5465. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5466. const UnicodeString ErrorStr)
  5467. {
  5468. if (!Directory.IsEmpty())
  5469. {
  5470. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5471. }
  5472. else
  5473. {
  5474. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5475. }
  5476. }
  5477. //---------------------------------------------------------------------------
  5478. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5479. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5480. {
  5481. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5482. {
  5483. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5484. }
  5485. else
  5486. {
  5487. TMessageParams Params(mpNeverAskAgainCheck);
  5488. unsigned int Result = MessageDialog(
  5489. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5490. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5491. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5492. {
  5493. MaxDirectories *= 2;
  5494. if (Result == qaNeverAskAgain)
  5495. {
  5496. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5497. }
  5498. }
  5499. else
  5500. {
  5501. Abort();
  5502. }
  5503. }
  5504. }
  5505. //---------------------------------------------------------------------------
  5506. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5507. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5508. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5509. TSynchronizeOptions * Options)
  5510. {
  5511. // Keep-up-to-date mode
  5512. DebugAssert(!FAutoOperation);
  5513. void * BatchStorage;
  5514. BatchStart(BatchStorage);
  5515. FAutoOperation = true;
  5516. bool AnyOperation = false;
  5517. TDateTime StartTime = Now();
  5518. TSynchronizeChecklist * AChecklist = NULL;
  5519. try
  5520. {
  5521. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5522. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5523. {
  5524. FSynchronizeProgressForm->Start();
  5525. }
  5526. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5527. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5528. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5529. Options);
  5530. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5531. SAFE_DESTROY(FSynchronizeProgressForm);
  5532. AnyOperation = (AChecklist->CheckedCount > 0);
  5533. if (AnyOperation)
  5534. {
  5535. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5536. CreateProgressForm(&SynchronizeProgress);
  5537. try
  5538. {
  5539. Terminal->SynchronizeApply(
  5540. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5541. SynchronizeProcessedItem, NULL, NULL, NULL);
  5542. }
  5543. __finally
  5544. {
  5545. DestroyProgressForm();
  5546. }
  5547. }
  5548. }
  5549. __finally
  5550. {
  5551. if (Checklist == NULL)
  5552. {
  5553. delete AChecklist;
  5554. }
  5555. else
  5556. {
  5557. *Checklist = AChecklist;
  5558. }
  5559. FAutoOperation = false;
  5560. SAFE_DESTROY(FSynchronizeProgressForm);
  5561. BatchEnd(BatchStorage);
  5562. ReloadLocalDirectory();
  5563. if (AnyOperation)
  5564. {
  5565. OperationComplete(StartTime);
  5566. }
  5567. }
  5568. }
  5569. //---------------------------------------------------------------------------
  5570. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5571. {
  5572. // can be called from command line
  5573. return Visible &&
  5574. ((DirView(osRemote)->SelCount > 0) ||
  5575. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5576. }
  5577. //---------------------------------------------------------------------------
  5578. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5579. {
  5580. LogSynchronizeEvent(Terminal, Message);
  5581. }
  5582. //---------------------------------------------------------------------------
  5583. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5584. int Params, TSynchronizeOptions & Options)
  5585. {
  5586. DebugAssert(!IsLocalBrowserMode());
  5587. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5588. {
  5589. Options.Filter = new TStringList();
  5590. Options.Filter->CaseSensitive = false;
  5591. Options.Filter->Duplicates = Types::dupAccept;
  5592. if (DirView(osRemote)->SelCount > 0)
  5593. {
  5594. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5595. }
  5596. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5597. {
  5598. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5599. }
  5600. Options.Filter->Sort();
  5601. }
  5602. }
  5603. //---------------------------------------------------------------------------
  5604. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5605. {
  5606. TCopyParamType Defaults;
  5607. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5608. ConfigStorage->AccessMode = smRead;
  5609. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5610. {
  5611. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5612. }
  5613. ConfigStorage.reset(NULL);
  5614. std::unique_ptr<TStringList> Options(new TStringList());
  5615. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5616. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5617. UnicodeString Result;
  5618. if (Options->Count > 0)
  5619. {
  5620. Result =
  5621. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5622. }
  5623. return Result;
  5624. }
  5625. //---------------------------------------------------------------------------
  5626. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5627. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5628. {
  5629. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5630. UnicodeString AdditionalParams =
  5631. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5632. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5633. SerializeCopyParamForCommandLine(CopyParams),
  5634. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5635. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5636. ExecuteNewInstance(SessionName, AdditionalParams);
  5637. }
  5638. //---------------------------------------------------------------------------
  5639. struct TSynchronizeParams
  5640. {
  5641. UnicodeString LocalDirectory;
  5642. UnicodeString RemoteDirectory;
  5643. TSynchronizeMode Mode;
  5644. TSynchronizeChecklist * Checklist;
  5645. int Params;
  5646. TCopyParamType * CopyParam;
  5647. TDateTime CollectElapsed;
  5648. TDateTime * StartTime;
  5649. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5650. };
  5651. //---------------------------------------------------------------------------
  5652. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5653. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5654. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5655. {
  5656. DebugAssert(!FAutoOperation);
  5657. void * BatchStorage;
  5658. BatchStart(BatchStorage);
  5659. FAutoOperation = true;
  5660. TFileOperationStatistics Statistics;
  5661. TDateTime Start = Now();
  5662. try
  5663. {
  5664. Params.OnProcessedItem = OnProcessedItem;
  5665. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5666. CreateProgressForm(&SynchronizeProgress);
  5667. Terminal->SynchronizeApply(
  5668. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5669. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5670. &Statistics);
  5671. }
  5672. __finally
  5673. {
  5674. Params.OnProcessedItem = NULL;
  5675. FAutoOperation = false;
  5676. DestroyProgressForm();
  5677. BatchEnd(BatchStorage);
  5678. ReloadLocalDirectory();
  5679. }
  5680. if (WinConfiguration->SynchronizeSummary)
  5681. {
  5682. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5683. // The statistics should be 0 anyway in this case
  5684. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5685. {
  5686. Message += L"\n";
  5687. if (Statistics.FilesUploaded > 0)
  5688. {
  5689. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5690. }
  5691. if (Statistics.FilesDownloaded > 0)
  5692. {
  5693. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5694. }
  5695. if (Statistics.FilesDeletedLocal > 0)
  5696. {
  5697. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5698. }
  5699. if (Statistics.FilesDeletedRemote > 0)
  5700. {
  5701. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5702. }
  5703. }
  5704. TDateTime Elapsed = (Now() - Start);
  5705. Message +=
  5706. L"\n" +
  5707. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5708. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5709. TMessageParams Params(mpNeverAskAgainCheck);
  5710. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5711. if (Result == qaNeverAskAgain)
  5712. {
  5713. WinConfiguration->SynchronizeSummary = false;
  5714. }
  5715. }
  5716. }
  5717. //---------------------------------------------------------------------------
  5718. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5719. {
  5720. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5721. {
  5722. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5723. }
  5724. // Not set in keep-up-to-date mode - Synchronize() method
  5725. if (Token != NULL)
  5726. {
  5727. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5728. if (Params.OnProcessedItem != NULL)
  5729. {
  5730. Params.OnProcessedItem(NULL, ChecklistItem);
  5731. }
  5732. }
  5733. }
  5734. //---------------------------------------------------------------------------
  5735. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5736. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5737. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5738. {
  5739. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5740. *Params.StartTime = Now();
  5741. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5742. }
  5743. //---------------------------------------------------------------------------
  5744. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5745. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5746. {
  5747. // terminal can be already closed (e.g. dropped connection)
  5748. if (Terminal != NULL)
  5749. {
  5750. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5751. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5752. }
  5753. }
  5754. //---------------------------------------------------------------------------
  5755. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5756. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5757. {
  5758. DebugAssert(!IsLocalBrowserMode());
  5759. TAutoBatch AutoBatch(this);
  5760. TAutoFlag AutoOperationFlag(FAutoOperation);
  5761. if (Side == osRemote)
  5762. {
  5763. std::unique_ptr<TStrings> FileList(new TStringList());
  5764. FileList->AddObject(FileName, RemoteFile);
  5765. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5766. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5767. RemoteDirView->SaveSelection();
  5768. RemoteDirView->SaveSelectedNames();
  5769. try
  5770. {
  5771. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5772. }
  5773. catch(...)
  5774. {
  5775. RemoteDirView->DiscardSavedSelection();
  5776. throw;
  5777. }
  5778. RemoteDirView->RestoreSelection();
  5779. }
  5780. else if (DebugAlwaysTrue(Side == osLocal))
  5781. {
  5782. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5783. {
  5784. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5785. }
  5786. UnicodeString Directory = ExtractFileDir(FileName);
  5787. ReloadLocalDirectory(Directory);
  5788. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5789. if (!SamePaths(Directory, NewDirectory))
  5790. {
  5791. ReloadLocalDirectory(NewDirectory);
  5792. }
  5793. }
  5794. }
  5795. //---------------------------------------------------------------------------
  5796. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5797. {
  5798. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5799. if (Side == osLocal)
  5800. {
  5801. if (Action == TSynchronizeChecklist::saDownloadNew)
  5802. {
  5803. OpenFolderInExplorer(LocalPath);
  5804. }
  5805. else
  5806. {
  5807. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5808. }
  5809. }
  5810. else if (DebugAlwaysTrue(Side == osRemote))
  5811. {
  5812. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5813. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5814. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5815. if (!LocalPath.IsEmpty())
  5816. {
  5817. SessionData->LocalDirectory = LocalPath;
  5818. }
  5819. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5820. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5821. }
  5822. }
  5823. //---------------------------------------------------------------------------
  5824. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5825. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5826. const TCopyParamType * CopyParams)
  5827. {
  5828. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5829. UnicodeString AdditionalParams =
  5830. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5831. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5832. SerializeCopyParamForCommandLine(CopyParams),
  5833. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5834. LocalDirectory, RemoteDirectory, Mode, Params));
  5835. ExecuteNewInstance(SessionName, AdditionalParams);
  5836. }
  5837. //---------------------------------------------------------------------------
  5838. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5839. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5840. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5841. {
  5842. int Result;
  5843. bool SaveSettings = false;
  5844. int Options =
  5845. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5846. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5847. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5848. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5849. bool Continue =
  5850. (UseDefaults == 0) ||
  5851. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5852. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5853. if (Continue)
  5854. {
  5855. Configuration->Usage->Inc(L"Synchronizations");
  5856. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  5857. UpdateCopyParamCounters(CopyParam);
  5858. TSynchronizeOptions SynchronizeOptions;
  5859. GetSynchronizeOptions(Params, SynchronizeOptions);
  5860. if (SaveSettings)
  5861. {
  5862. GUIConfiguration->SynchronizeParams = Params;
  5863. }
  5864. else
  5865. {
  5866. SaveMode = false;
  5867. }
  5868. TDateTime StartTime = Now();
  5869. TSynchronizeChecklist * Checklist = NULL;
  5870. try
  5871. {
  5872. DebugAssert(!FAutoOperation);
  5873. FAutoOperation = true;
  5874. try
  5875. {
  5876. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5877. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5878. int Files = -1;
  5879. if (DataList->Count >= 1)
  5880. {
  5881. Files = StrToIntDef(DataList->Strings[0], Files);
  5882. }
  5883. else
  5884. {
  5885. DataList->Add(UnicodeString());
  5886. }
  5887. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5888. FSynchronizeProgressForm->Start();
  5889. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5890. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5891. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5892. &SynchronizeOptions);
  5893. if (Terminal->SessionData->CacheDirectories)
  5894. {
  5895. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5896. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5897. }
  5898. }
  5899. __finally
  5900. {
  5901. FAutoOperation = false;
  5902. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5903. SAFE_DESTROY(FSynchronizeProgressForm);
  5904. }
  5905. TSynchronizeParams SynchronizeParams;
  5906. SynchronizeParams.LocalDirectory = LocalDirectory;
  5907. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5908. SynchronizeParams.Mode = Mode;
  5909. SynchronizeParams.CopyParam = &CopyParam;
  5910. SynchronizeParams.Params = Params;
  5911. SynchronizeParams.Checklist = Checklist;
  5912. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5913. SynchronizeParams.StartTime = &StartTime;
  5914. SynchronizeParams.OnProcessedItem = NULL;
  5915. Result = Checklist->Count;
  5916. if (Checklist->Count > 0)
  5917. {
  5918. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5919. {
  5920. if (!DoSynchronizeChecklistDialog(
  5921. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5922. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5923. {
  5924. Result = -1;
  5925. }
  5926. }
  5927. else
  5928. {
  5929. FullSynchronize(SynchronizeParams, NULL, NULL);
  5930. }
  5931. }
  5932. else
  5933. {
  5934. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5935. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5936. }
  5937. }
  5938. __finally
  5939. {
  5940. delete Checklist;
  5941. }
  5942. OperationComplete(StartTime);
  5943. }
  5944. else
  5945. {
  5946. Result = -1;
  5947. }
  5948. return Result;
  5949. }
  5950. //---------------------------------------------------------------------------
  5951. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5952. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5953. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5954. {
  5955. if (Collect)
  5956. {
  5957. DebugAssert(FSynchronizeProgressForm != NULL);
  5958. if (!FSynchronizeProgressForm->Started)
  5959. {
  5960. FSynchronizeProgressForm->Start();
  5961. }
  5962. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5963. SetTaskbarListProgressValue(CompareProgress);
  5964. }
  5965. else
  5966. {
  5967. DebugAssert(FSynchronizeProgressForm == NULL);
  5968. DebugAssert(FProgressForm != NULL);
  5969. }
  5970. }
  5971. //---------------------------------------------------------------------------
  5972. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5973. {
  5974. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  5975. TRemoteFile * File = NULL;
  5976. Terminal->ReadFile(FullFileName, File);
  5977. if (File != NULL)
  5978. {
  5979. std::unique_ptr<TRemoteFile> FileOwner(File);
  5980. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5981. Application->ShowMainForm = false;
  5982. Application->Run();
  5983. }
  5984. }
  5985. //---------------------------------------------------------------------------
  5986. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  5987. {
  5988. DebugFail();
  5989. }
  5990. //---------------------------------------------------------------------------
  5991. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5992. {
  5993. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5994. SessionData->Assign(ManagedSession->SessionData);
  5995. UpdateSessionData(SessionData.get());
  5996. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  5997. if (IsActiveTerminal(ManagedSession))
  5998. {
  5999. ManagedSession->UpdateSessionCredentials(SessionData.get());
  6000. }
  6001. if (!ManagedSession->SessionData->HasSessionName())
  6002. {
  6003. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  6004. // once IsWorkspace is cleared
  6005. SessionData->Name = UnicodeString();
  6006. }
  6007. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  6008. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  6009. SessionData->IsWorkspace = false;
  6010. return SessionData.release();
  6011. }
  6012. //---------------------------------------------------------------------------
  6013. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  6014. {
  6015. TSessionData * SessionData = CloneCurrentSessionData();
  6016. try
  6017. {
  6018. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  6019. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  6020. }
  6021. __finally
  6022. {
  6023. delete SessionData;
  6024. }
  6025. }
  6026. //---------------------------------------------------------------------------
  6027. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  6028. {
  6029. TTerminalManager * Manager = TTerminalManager::Instance();
  6030. std::unique_ptr<TObjectList> DataList(new TObjectList());
  6031. if (DebugAlwaysTrue(ManagedSession != NULL))
  6032. {
  6033. // Update ManagedSession->StateData
  6034. UpdateSession(ManagedSession);
  6035. }
  6036. Manager->SaveWorkspace(DataList.get());
  6037. return DataList.release();
  6038. }
  6039. //---------------------------------------------------------------------------
  6040. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  6041. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  6042. {
  6043. WinConfiguration->LastWorkspace = Name;
  6044. if (!SavePasswords)
  6045. {
  6046. for (int Index = 0; Index < DataList->Count; Index++)
  6047. {
  6048. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6049. if (SessionData->Link.IsEmpty())
  6050. {
  6051. SessionData->ClearSessionPasswords();
  6052. }
  6053. }
  6054. }
  6055. StoredSessions->NewWorkspace(Name, DataList);
  6056. // modified only
  6057. StoredSessions->Save(false, Explicit);
  6058. }
  6059. //---------------------------------------------------------------------------
  6060. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  6061. {
  6062. return
  6063. DefaultStr(WinConfiguration->LastWorkspace,
  6064. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  6065. }
  6066. //---------------------------------------------------------------------------
  6067. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  6068. {
  6069. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  6070. bool AnyNonStoredSessionWithPassword = false;
  6071. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  6072. bool AllNonStoredSessionsAnonymous = true;
  6073. TTerminalManager * Manager = TTerminalManager::Instance();
  6074. for (int Index = 0; Index < DataList->Count; Index++)
  6075. {
  6076. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6077. if (SessionData->Link.IsEmpty())
  6078. {
  6079. if (SessionData->HasAnySessionPassword())
  6080. {
  6081. AnyNonStoredSessionWithPassword = true;
  6082. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  6083. {
  6084. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  6085. }
  6086. }
  6087. if (!SameText(SessionData->UserName, AnonymousUserName))
  6088. {
  6089. AllNonStoredSessionsAnonymous = false;
  6090. }
  6091. }
  6092. }
  6093. bool SavePasswords;
  6094. bool * PSavePasswords;
  6095. bool NotRecommendedSavingPasswords =
  6096. !CustomWinConfiguration->UseMasterPassword &&
  6097. !AllNonStoredSessionsAnonymous;
  6098. if (Configuration->DisablePasswordStoring ||
  6099. !AnyNonStoredSessionWithPassword)
  6100. {
  6101. PSavePasswords = NULL;
  6102. SavePasswords = false;
  6103. }
  6104. else
  6105. {
  6106. PSavePasswords = &SavePasswords;
  6107. SavePasswords =
  6108. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  6109. !NotRecommendedSavingPasswords;
  6110. }
  6111. UnicodeString Name = WorkspaceName();
  6112. bool CreateShortcut = false;
  6113. bool Result =
  6114. DoSaveWorkspaceDialog(
  6115. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  6116. EnableAutoSave);
  6117. if (Result)
  6118. {
  6119. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  6120. if (CreateShortcut)
  6121. {
  6122. TOperationVisualizer Visualizer;
  6123. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  6124. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  6125. }
  6126. if (EnableAutoSave)
  6127. {
  6128. WinConfiguration->AutoSaveWorkspace = true;
  6129. WinConfiguration->AutoWorkspace = Name;
  6130. if (PSavePasswords != NULL)
  6131. {
  6132. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  6133. }
  6134. }
  6135. }
  6136. return Result;
  6137. }
  6138. //---------------------------------------------------------------------------
  6139. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  6140. {
  6141. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  6142. DebugAssert(Session == ManagedSession);
  6143. if (!IsLocalBrowserMode())
  6144. {
  6145. SAFE_DESTROY(Session->RemoteExplorerState);
  6146. if (WinConfiguration->PreservePanelState)
  6147. {
  6148. Session->RemoteExplorerState = RemoteDirView->SaveState();
  6149. }
  6150. }
  6151. UpdateSessionData(Session->StateData);
  6152. }
  6153. //---------------------------------------------------------------------------
  6154. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  6155. {
  6156. // Keep in sync with TSessionData::CopyStateData
  6157. DebugAssert(Data != NULL);
  6158. // This is inconsistent with how color (for example) is handled.
  6159. Data->Name = ManagedSession->SessionData->Name;
  6160. if (!IsLocalBrowserMode())
  6161. {
  6162. // cannot use RemoteDirView->Path, because it is empty if connection
  6163. // was already closed
  6164. // also only peek, we may not be connected at all atm
  6165. // so make sure we do not try retrieving current directory from the server
  6166. // (particularly with FTP)
  6167. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  6168. if (!ACurrentDirectory.IsEmpty())
  6169. {
  6170. Data->RemoteDirectory = ACurrentDirectory;
  6171. }
  6172. }
  6173. Data->Color = SessionColor;
  6174. }
  6175. //---------------------------------------------------------------------------
  6176. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  6177. {
  6178. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  6179. DebugAssert(Toolbar != NULL);
  6180. for (int i = 0; i < Toolbar->Items->Count; i++)
  6181. {
  6182. TTBXCustomDropDownItem * DropDownItem;
  6183. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6184. if (DropDownItem != NULL)
  6185. {
  6186. ToolbarItemResize(DropDownItem,
  6187. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  6188. Toolbar->NonClientWidth);
  6189. break;
  6190. }
  6191. }
  6192. }
  6193. //---------------------------------------------------------------------------
  6194. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6195. {
  6196. Item->EditWidth = Width;
  6197. }
  6198. //---------------------------------------------------------------------------
  6199. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6200. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6201. {
  6202. for (int i = 0; i < Toolbar->Items->Count; i++)
  6203. {
  6204. TTBXCustomDropDownItem * DropDownItem;
  6205. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6206. if (DropDownItem != NULL)
  6207. {
  6208. ASize.x -= DropDownItem->EditWidth;
  6209. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  6210. }
  6211. }
  6212. }
  6213. //---------------------------------------------------------------------------
  6214. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6215. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6216. {
  6217. if (WinConfiguration->DDWarnLackOfTempSpace)
  6218. {
  6219. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6220. if (!ADrive.IsEmpty())
  6221. {
  6222. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6223. DebugAssert(RequiredSpace >= 0);
  6224. __int64 RequiredWithReserve;
  6225. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6226. if (FreeSpace < RequiredWithReserve)
  6227. {
  6228. unsigned int Result;
  6229. TMessageParams Params(mpNeverAskAgainCheck);
  6230. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6231. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6232. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6233. if (Result == qaNeverAskAgain)
  6234. {
  6235. WinConfiguration->DDWarnLackOfTempSpace = false;
  6236. }
  6237. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6238. }
  6239. }
  6240. }
  6241. }
  6242. //---------------------------------------------------------------------------
  6243. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6244. {
  6245. DoOpenDirectoryDialog(odAddBookmark, Side);
  6246. }
  6247. //---------------------------------------------------------------------------
  6248. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6249. {
  6250. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6251. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6252. try
  6253. {
  6254. TCustomDirView * DView = DirView(Side);
  6255. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6256. {
  6257. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6258. }
  6259. }
  6260. catch (...)
  6261. {
  6262. delete VisitedDirectories;
  6263. throw;
  6264. }
  6265. return VisitedDirectories;
  6266. }
  6267. //---------------------------------------------------------------------------
  6268. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6269. TOpenDirectoryMode Mode, TOperationSide Side)
  6270. {
  6271. bool Continue = true;
  6272. if (Mode == odAddBookmark)
  6273. {
  6274. TMessageParams Params(mpNeverAskAgainCheck);
  6275. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6276. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6277. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6278. unsigned int Answer =
  6279. MessageDialog(Message,
  6280. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6281. if (Answer == qaNeverAskAgain)
  6282. {
  6283. Continue = true;
  6284. WinConfiguration->UseSharedBookmarks = true;
  6285. }
  6286. else if (Answer == qaOK)
  6287. {
  6288. Continue = true;
  6289. WinConfiguration->UseSharedBookmarks = false;
  6290. }
  6291. else
  6292. {
  6293. Continue = false;
  6294. }
  6295. }
  6296. if (Continue)
  6297. {
  6298. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6299. try
  6300. {
  6301. UnicodeString Name = DirView(Side)->PathName;
  6302. bool Repeat;
  6303. do
  6304. {
  6305. Repeat = false;
  6306. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6307. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6308. // do not allow switching to location profiles,
  6309. // if we are not connected
  6310. HasDirView[osLocal] && (Terminal != NULL)))
  6311. {
  6312. TWindowLock Lock(this);
  6313. if (!TryOpenDirectory(Side, Name))
  6314. {
  6315. Repeat = true;
  6316. Mode = odBrowse;
  6317. }
  6318. }
  6319. }
  6320. while (Repeat);
  6321. }
  6322. __finally
  6323. {
  6324. delete VisitedDirectories;
  6325. }
  6326. }
  6327. }
  6328. //---------------------------------------------------------------------------
  6329. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6330. {
  6331. DebugAssert(!Terminal->CommandSessionOpened);
  6332. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6333. }
  6334. //---------------------------------------------------------------------------
  6335. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6336. {
  6337. bool Result = Terminal->IsCapable[Capability] ||
  6338. Terminal->CommandSessionOpened;
  6339. if (!Result)
  6340. {
  6341. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6342. if (!GUIConfiguration->ConfirmCommandSession)
  6343. {
  6344. Result = true;
  6345. }
  6346. else
  6347. {
  6348. TMessageParams Params(mpNeverAskAgainCheck);
  6349. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6350. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6351. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6352. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6353. if (Answer == qaNeverAskAgain)
  6354. {
  6355. GUIConfiguration->ConfirmCommandSession = false;
  6356. Result = true;
  6357. }
  6358. else if (Answer == qaOK)
  6359. {
  6360. Result = true;
  6361. }
  6362. }
  6363. if (Result)
  6364. {
  6365. Result = CommandSessionFallback();
  6366. }
  6367. }
  6368. return Result;
  6369. }
  6370. //---------------------------------------------------------------------------
  6371. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6372. {
  6373. if (EnsureCommandSessionFallback(fcAnyCommand))
  6374. {
  6375. DoConsoleDialog(Terminal, Command);
  6376. }
  6377. }
  6378. //---------------------------------------------------------------------------
  6379. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6380. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6381. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6382. {
  6383. if (!IsSideLocalBrowser(osRemote) &&
  6384. IsFileControl(DropSourceControl, osRemote) &&
  6385. (FDDExtMapFile != NULL))
  6386. {
  6387. Accept = true;
  6388. }
  6389. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6390. DebugAssert(FDDTargetControl != NULL);
  6391. }
  6392. //---------------------------------------------------------------------------
  6393. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6394. _di_IDataObject DataObj, int KeyState,
  6395. const TPoint & Point, int & Effect, bool & Accept)
  6396. {
  6397. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6398. }
  6399. //---------------------------------------------------------------------------
  6400. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6401. _di_IDataObject DataObj, int KeyState,
  6402. const TPoint & Point, int & Effect, bool & Accept)
  6403. {
  6404. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6405. }
  6406. //---------------------------------------------------------------------------
  6407. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6408. TObject * Sender)
  6409. {
  6410. DebugUsedParam(Sender);
  6411. DebugAssert(FDDTargetControl == Sender);
  6412. FDDTargetControl = NULL;
  6413. }
  6414. //---------------------------------------------------------------------------
  6415. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6416. {
  6417. FileControlDDDragLeave(SessionsPageControl);
  6418. }
  6419. //---------------------------------------------------------------------------
  6420. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6421. {
  6422. FileControlDDDragLeave(QueueView3);
  6423. }
  6424. //---------------------------------------------------------------------------
  6425. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6426. {
  6427. DebugAssert(!IsSideLocalBrowser(Side));
  6428. DebugUsedParam(Side);
  6429. bool Edit = false;
  6430. TRemoteFile * File = NULL;
  6431. UnicodeString FileName;
  6432. UnicodeString PointTo;
  6433. bool SymbolicLink = true;
  6434. if (RemoteDirView->ItemFocused)
  6435. {
  6436. DebugAssert(RemoteDirView->ItemFocused->Data);
  6437. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6438. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6439. if (Edit)
  6440. {
  6441. FileName = File->FileName;
  6442. PointTo = File->LinkTo;
  6443. }
  6444. else if (!File->IsParentDirectory)
  6445. {
  6446. PointTo = File->FileName;
  6447. }
  6448. }
  6449. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6450. Terminal->IsCapable[fcHardLink]))
  6451. {
  6452. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6453. if (Edit)
  6454. {
  6455. DebugAssert(File->FileName == FileName);
  6456. int Params = dfNoRecursive;
  6457. Terminal->ExceptionOnFail = true;
  6458. try
  6459. {
  6460. Terminal->DeleteFile(L"", File, &Params);
  6461. }
  6462. __finally
  6463. {
  6464. Terminal->ExceptionOnFail = false;
  6465. }
  6466. }
  6467. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6468. }
  6469. }
  6470. //---------------------------------------------------------------------------
  6471. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6472. {
  6473. return
  6474. (IsSideLocalBrowser(Side) ||
  6475. (HasActiveTerminal() &&
  6476. Terminal->ResolvingSymlinks &&
  6477. Terminal->IsCapable[fcSymbolicLink]));
  6478. }
  6479. //---------------------------------------------------------------------------
  6480. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6481. {
  6482. return
  6483. !IsSideLocalBrowser(FCurrentSide) &&
  6484. (RemoteDirView->ItemFocused != NULL) &&
  6485. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6486. Terminal->SessionData->ResolveSymlinks;
  6487. }
  6488. //---------------------------------------------------------------------------
  6489. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6490. {
  6491. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6492. FForceExecution = true;
  6493. try
  6494. {
  6495. DirView(osCurrent)->ExecuteCurrentFile();
  6496. }
  6497. __finally
  6498. {
  6499. FForceExecution = false;
  6500. }
  6501. }
  6502. //---------------------------------------------------------------------------
  6503. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6504. {
  6505. TEditorData ExternalEditor;
  6506. ExternalEditor.Editor = edExternal;
  6507. bool Remember = false;
  6508. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6509. {
  6510. if (Remember)
  6511. {
  6512. TEditorList * EditorList = new TEditorList;
  6513. try
  6514. {
  6515. *EditorList = *WinConfiguration->EditorList;
  6516. bool Found = false;
  6517. int i = 0;
  6518. while (!Found && (i < EditorList->Count))
  6519. {
  6520. const TEditorPreferences * Editor = EditorList->Editors[i];
  6521. if ((Editor->Data->Editor == edExternal) &&
  6522. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6523. {
  6524. Found = true;
  6525. }
  6526. i++;
  6527. }
  6528. if (!Found)
  6529. {
  6530. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6531. WinConfiguration->EditorList = EditorList;
  6532. }
  6533. }
  6534. __finally
  6535. {
  6536. delete EditorList;
  6537. }
  6538. }
  6539. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6540. }
  6541. }
  6542. //---------------------------------------------------------------------------
  6543. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6544. {
  6545. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6546. if (FFileFindTerminal == ATerminal)
  6547. {
  6548. FFileFindTerminal = NULL;
  6549. HideFileFindDialog();
  6550. }
  6551. if (FClipboardTerminal == ATerminal)
  6552. {
  6553. ClipboardClear(); // implies ClipboardStop
  6554. }
  6555. if ((FOnSynchronizeAbort != NULL) &&
  6556. (ATerminal == FManagedSession))
  6557. {
  6558. FOnSynchronizeAbort(NULL);
  6559. }
  6560. }
  6561. //---------------------------------------------------------------------------
  6562. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6563. {
  6564. DetachTerminal(Sender);
  6565. }
  6566. //---------------------------------------------------------------------------
  6567. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6568. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6569. {
  6570. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6571. DebugAssert(Terminal != NULL);
  6572. if (Data->Terminal == Terminal)
  6573. {
  6574. Data->Terminal = NULL;
  6575. }
  6576. }
  6577. //---------------------------------------------------------------------------
  6578. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6579. {
  6580. return NULL;
  6581. }
  6582. //---------------------------------------------------------------------------
  6583. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6584. {
  6585. UpdateControls();
  6586. SessionColor = TColor(0);
  6587. UpdateRemotePathComboBox(false);
  6588. NeedSession(false);
  6589. }
  6590. //---------------------------------------------------------------------------
  6591. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6592. {
  6593. try
  6594. {
  6595. // Cache, as the login dialog can change its value.
  6596. // See also DoShow. Also see TTerminalManager::NewSession.
  6597. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6598. try
  6599. {
  6600. if (ShowLogin)
  6601. {
  6602. bool ReloadSessions = !Startup;
  6603. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6604. }
  6605. }
  6606. __finally
  6607. {
  6608. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6609. // (so there was no chance for the user to open any session yet)
  6610. if ((ShowLogin || !Startup) &&
  6611. !WinConfiguration->KeepOpenWhenNoSession &&
  6612. // this rules out the implicit Commander's local-local browser
  6613. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6614. {
  6615. TerminateApplication();
  6616. }
  6617. }
  6618. }
  6619. catch (EAbort &)
  6620. {
  6621. // swallow
  6622. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6623. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6624. }
  6625. }
  6626. //---------------------------------------------------------------------------
  6627. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6628. {
  6629. TTerminalManager * Manager = TTerminalManager::Instance();
  6630. DebugAssert(!Manager->Updating);
  6631. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6632. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6633. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6634. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6635. try
  6636. {
  6637. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6638. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6639. {
  6640. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6641. }
  6642. for (int Index = 0; Index <= Manager->Count; Index++)
  6643. {
  6644. TThemeTabSheet * TabSheet;
  6645. if (Index >= SessionsPageControl->PageCount)
  6646. {
  6647. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6648. TabSheet->PageControl = SessionsPageControl;
  6649. }
  6650. else
  6651. {
  6652. TabSheet = SessionsPageControl->Pages[Index];
  6653. }
  6654. bool IsSessionTab = (Index < Manager->Count);
  6655. if (IsSessionTab)
  6656. {
  6657. TTerminal * Terminal = Manager->Sessions[Index];
  6658. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6659. TabSheet->CaptionTruncation = tttEllipsis;
  6660. UpdateSessionTab(TabSheet);
  6661. }
  6662. else
  6663. {
  6664. TabSheet->Tag = 0; // not really needed
  6665. TabSheet->Shadowed = false;
  6666. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6667. TabSheet->CaptionTruncation = tttNoText;
  6668. // 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)
  6669. UpdateNewTabTab();
  6670. }
  6671. }
  6672. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6673. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6674. {
  6675. SessionsPageControl->UpdateTabsCaptionTruncation();
  6676. }
  6677. }
  6678. __finally
  6679. {
  6680. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6681. }
  6682. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6683. }
  6684. //---------------------------------------------------------------------------
  6685. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6686. {
  6687. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6688. }
  6689. //---------------------------------------------------------------------------
  6690. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6691. {
  6692. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6693. UnicodeString TabCaption;
  6694. if (WinConfiguration->SelectiveToolbarText)
  6695. {
  6696. TabCaption = GetNewTabTabCaption();
  6697. }
  6698. TabSheet->BaseCaption = TabCaption;
  6699. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6700. }
  6701. //---------------------------------------------------------------------------
  6702. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6703. {
  6704. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6705. }
  6706. //---------------------------------------------------------------------------
  6707. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6708. {
  6709. DebugFail();
  6710. return UnicodeString();
  6711. }
  6712. //---------------------------------------------------------------------------
  6713. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6714. {
  6715. if (DebugAlwaysTrue(TabSheet != NULL))
  6716. {
  6717. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6718. if (DebugAlwaysTrue(ASession != NULL))
  6719. {
  6720. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6721. if (ASession->LocalBrowser)
  6722. {
  6723. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6724. }
  6725. else
  6726. {
  6727. TabSheet->ImageIndex = AddSessionColor(Color);
  6728. }
  6729. TTerminalManager * Manager = TTerminalManager::Instance();
  6730. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6731. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6732. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6733. }
  6734. }
  6735. }
  6736. //---------------------------------------------------------------------------
  6737. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6738. {
  6739. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6740. }
  6741. //---------------------------------------------------------------------------
  6742. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6743. {
  6744. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6745. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6746. bool Result = (Session != NULL);
  6747. if (Result)
  6748. {
  6749. TTerminalManager::Instance()->ActiveSession = Session;
  6750. }
  6751. else
  6752. {
  6753. try
  6754. {
  6755. NewTab();
  6756. }
  6757. __finally
  6758. {
  6759. SessionListChanged();
  6760. }
  6761. FSessionsPageControlNewTabTime = Now();
  6762. }
  6763. return Result;
  6764. }
  6765. //---------------------------------------------------------------------------
  6766. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6767. {
  6768. TWindowLock Lock(this);
  6769. SessionTabSwitched();
  6770. }
  6771. //---------------------------------------------------------------------------
  6772. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6773. {
  6774. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6775. DebugAssert(TransferList != NULL);
  6776. UnicodeString Name;
  6777. if (TransferList->ItemIndex <= 0)
  6778. {
  6779. Name = L"";
  6780. }
  6781. else
  6782. {
  6783. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6784. }
  6785. if (FCopyParamAutoSelected.IsEmpty())
  6786. {
  6787. // if previous preset was not autoselected, make new preset the "default"
  6788. FCopyParamDefault = Name;
  6789. }
  6790. GUIConfiguration->CopyParamCurrent = Name;
  6791. }
  6792. //---------------------------------------------------------------------------
  6793. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6794. {
  6795. // sanity check
  6796. bool ExistingPreset =
  6797. (FTransferListHoverIndex >= 0) &&
  6798. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6799. DebugAssert(ExistingPreset);
  6800. if (ExistingPreset)
  6801. {
  6802. HDC DC = GetDC(0);
  6803. TCanvas * Canvas = new TCanvas();
  6804. try
  6805. {
  6806. Canvas->Handle = DC;
  6807. Canvas->Font = GetToolbarFont(this);
  6808. UnicodeString Name;
  6809. if (FTransferListHoverIndex == 0)
  6810. {
  6811. Name = L"";
  6812. }
  6813. else
  6814. {
  6815. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6816. }
  6817. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6818. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6819. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6820. static_cast<TObject*>(GetComponent(fcTransferList)));
  6821. UnicodeString InfoStr =
  6822. GUIConfiguration->CopyParamPreset[Name].
  6823. GetInfoStr(L"; ",
  6824. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6825. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6826. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6827. {
  6828. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6829. {
  6830. InfoStr.SetLength(InfoStr.Length() - 1);
  6831. }
  6832. InfoStr += Ellipsis;
  6833. }
  6834. // UpdateCaption does not cause invalidation of whole submenu, while
  6835. // setting Caption property does.
  6836. // also it probably does not resize the label, even if necessary
  6837. // (we do not want that anyway)
  6838. TransferLabel->UpdateCaption(InfoStr);
  6839. }
  6840. __finally
  6841. {
  6842. Canvas->Handle = NULL;
  6843. ReleaseDC(0, DC);
  6844. delete Canvas;
  6845. }
  6846. }
  6847. }
  6848. //---------------------------------------------------------------------------
  6849. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6850. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6851. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6852. {
  6853. if (FTransferListHoverIndex != AHoverIndex)
  6854. {
  6855. FTransferListHoverIndex = AHoverIndex;
  6856. UpdateTransferLabel();
  6857. }
  6858. }
  6859. //---------------------------------------------------------------------------
  6860. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6861. {
  6862. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6863. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6864. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6865. {
  6866. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6867. {
  6868. if (!NonVisualDataModule->Busy)
  6869. {
  6870. OpenDirectory(GetSide(osCurrent));
  6871. }
  6872. Message.Result = 1;
  6873. }
  6874. else
  6875. {
  6876. TForm::Dispatch(&Message);
  6877. }
  6878. }
  6879. else
  6880. {
  6881. TForm::Dispatch(&Message);
  6882. }
  6883. }
  6884. //---------------------------------------------------------------------------
  6885. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6886. {
  6887. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6888. if ((FIgnoreNextDialogChar != 0) &&
  6889. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6890. {
  6891. Message.Result = 1;
  6892. }
  6893. else
  6894. {
  6895. TForm::Dispatch(&Message);
  6896. }
  6897. FIgnoreNextDialogChar = 0;
  6898. }
  6899. //---------------------------------------------------------------------------
  6900. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6901. {
  6902. // The four low-order bits are used internally by Windows
  6903. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6904. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6905. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6906. // 61730 - restore thru double click - undocumented
  6907. // 61490 - maximize thru double click - undocumented
  6908. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6909. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6910. {
  6911. SysResizing(Cmd);
  6912. }
  6913. TForm::Dispatch(&Message);
  6914. }
  6915. //---------------------------------------------------------------------------
  6916. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6917. {
  6918. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6919. // Also there no point returning TRUE as we are not able to
  6920. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6921. // Hence the process termination might be safer :)
  6922. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6923. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6924. {
  6925. Message.Result = FALSE;
  6926. }
  6927. else
  6928. {
  6929. Message.Result = TRUE;
  6930. }
  6931. // Do not call default handling as that triggers OnCloseQuery,
  6932. // where our implementation will popup confirmation dialogs, what we do not want,
  6933. // as per Vista guidelines:
  6934. // https://learn.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6935. }
  6936. //---------------------------------------------------------------------------
  6937. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6938. {
  6939. if (Message.EndSession && IsApplicationMinimized())
  6940. {
  6941. // WORKAROUND
  6942. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6943. // when it receives WM_ENDSESSION,
  6944. // but that sometimes (particularly when application is minimized) cause crashes.
  6945. // Crash popups message that blocks log off.
  6946. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6947. // so we call ExitProcess() immediately, not even trying to cleanup.
  6948. // It still causes beep, so there's likely some popup, but it does not block
  6949. // log off.
  6950. ExitProcess(0);
  6951. }
  6952. TForm::Dispatch(&Message);
  6953. }
  6954. #ifdef _DEBUG
  6955. //---------------------------------------------------------------------------
  6956. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6957. {
  6958. TForm::Dispatch(&Message);
  6959. }
  6960. #endif
  6961. //---------------------------------------------------------------------------
  6962. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6963. {
  6964. }
  6965. //---------------------------------------------------------------------------
  6966. void __fastcall TCustomScpExplorerForm::DoShow()
  6967. {
  6968. // Called from TCustomScpExplorerForm::CMShowingChanged
  6969. // only now are the controls resized finally, so the size constraints
  6970. // will not conflict with possibly very small window size
  6971. RestoreFormParams();
  6972. // This indicates that the form was rescaled after parameters were loaded and
  6973. // as most probably the parameters were actually saved with the current form scaling
  6974. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6975. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6976. // and no rounding problems will occur.
  6977. // See also RestoreForm.
  6978. // (we should be safe to call RestoreParams unconditionally)
  6979. if (PixelsPerInch != Screen->PixelsPerInch)
  6980. {
  6981. RestoreParams();
  6982. }
  6983. FixControlsPlacement();
  6984. if (Position == poDefaultPosOnly)
  6985. {
  6986. CutFormToDesktop(this);
  6987. }
  6988. TForm::DoShow();
  6989. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6990. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6991. // This was previously in NeedSession, where other related code is.
  6992. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  6993. // unpleasantly shows briefly.
  6994. if ((ManagedSession == NULL) &&
  6995. !WinConfiguration->ShowLoginWhenNoSession &&
  6996. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6997. // This detects if workspace was saved the last time the main widow was closed
  6998. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6999. {
  7000. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  7001. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  7002. }
  7003. // Before main window displays
  7004. if (ManagedSession == NULL)
  7005. {
  7006. StartingWithoutSession();
  7007. }
  7008. UpdatePixelsPerInchMainWindowCounter();
  7009. FShowing = true;
  7010. }
  7011. //---------------------------------------------------------------------------
  7012. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  7013. {
  7014. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  7015. }
  7016. //---------------------------------------------------------------------------
  7017. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  7018. {
  7019. SessionListChanged();
  7020. InitStatusBar();
  7021. UpdateControls();
  7022. }
  7023. //---------------------------------------------------------------------------
  7024. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  7025. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  7026. TNotifyEvent OnBalloonClick, TObject * UserData)
  7027. {
  7028. bool Do;
  7029. UnicodeString Message;
  7030. if (E == NULL)
  7031. {
  7032. Message = Str;
  7033. Do = true;
  7034. }
  7035. else
  7036. {
  7037. Do = ExceptionMessage(E, Message);
  7038. }
  7039. if (Do && WinConfiguration->BalloonNotifications)
  7040. {
  7041. UnicodeString Title;
  7042. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  7043. {
  7044. Message = TrimLeft(Message);
  7045. }
  7046. else
  7047. {
  7048. Message = UnformatMessage(Message);
  7049. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  7050. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  7051. Title = LoadResourceString(Captions[Type]);
  7052. if (Terminal != NULL)
  7053. {
  7054. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7055. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  7056. }
  7057. }
  7058. if (Seconds == 0)
  7059. {
  7060. Seconds = WinConfiguration->NotificationsTimeout;
  7061. }
  7062. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  7063. }
  7064. }
  7065. //---------------------------------------------------------------------------
  7066. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  7067. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  7068. UnicodeString HelpKeyword, const TMessageParams * Params,
  7069. TTerminal * Terminal)
  7070. {
  7071. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  7072. (FOnFeedSynchronizeError != NULL)) &&
  7073. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  7074. {
  7075. if (FOnFeedSynchronizeError != NULL)
  7076. {
  7077. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  7078. }
  7079. else
  7080. {
  7081. DebugAssert(FErrorList != NULL);
  7082. TStringList * MoreMessagesCopy = NULL;
  7083. if (MoreMessages)
  7084. {
  7085. MoreMessagesCopy = new TStringList();
  7086. MoreMessagesCopy->Assign(MoreMessages);
  7087. }
  7088. FErrorList->AddObject(Message, MoreMessagesCopy);
  7089. }
  7090. PopupTrayBalloon(Terminal, Message, Type);
  7091. return ContinueAnswer(Answers);
  7092. }
  7093. else
  7094. {
  7095. bool UseBalloon = FTrayIcon->Visible;
  7096. if (UseBalloon)
  7097. {
  7098. PopupTrayBalloon(Terminal, Message, Type);
  7099. }
  7100. unsigned int Result;
  7101. try
  7102. {
  7103. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  7104. }
  7105. __finally
  7106. {
  7107. // cancel only balloon we popped up, otherwise we may cancel notification
  7108. // balloon that was there before the message dialog
  7109. // (such as "dd confirmation opt in balloon)
  7110. if (UseBalloon)
  7111. {
  7112. FTrayIcon->CancelBalloon();
  7113. }
  7114. }
  7115. return Result;
  7116. }
  7117. }
  7118. //---------------------------------------------------------------------------
  7119. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  7120. TTerminal * Terminal, Exception * E)
  7121. {
  7122. if (FTrayIcon->Visible)
  7123. {
  7124. PopupTrayBalloon(Terminal, L"", qtError, E);
  7125. }
  7126. // particularly prevent opening new session from jump list,
  7127. // while exception is shown
  7128. NonVisualDataModule->StartBusy();
  7129. try
  7130. {
  7131. ShowExtendedExceptionEx(Terminal, E);
  7132. }
  7133. __finally
  7134. {
  7135. NonVisualDataModule->EndBusy();
  7136. FTrayIcon->CancelBalloon();
  7137. }
  7138. }
  7139. //---------------------------------------------------------------------------
  7140. void __fastcall TCustomScpExplorerForm::SessionReady()
  7141. {
  7142. InitStatusBar();
  7143. // cannot rely on active page being page for active terminal,
  7144. // as it can happen that active page is the "new session" page
  7145. // (e.g. when reconnecting active terminal, while login dialog
  7146. // invoked from "new session" page is modal)
  7147. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  7148. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  7149. }
  7150. //---------------------------------------------------------------------------
  7151. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  7152. TTerminal * Terminal, Exception * E)
  7153. {
  7154. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  7155. if (!Terminal->Active)
  7156. {
  7157. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  7158. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  7159. {
  7160. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  7161. }
  7162. }
  7163. }
  7164. //---------------------------------------------------------------------------
  7165. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  7166. UnicodeString Message, TQueryType Type,
  7167. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  7168. {
  7169. if ((E == NULL) ||
  7170. ExceptionMessage(E, Message))
  7171. {
  7172. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  7173. if (Important)
  7174. {
  7175. Seconds *= 5;
  7176. }
  7177. UnicodeString NoteMessage(UnformatMessage(Message));
  7178. if (Terminal != NULL)
  7179. {
  7180. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7181. NoteMessage = FORMAT(L"%s: %s",
  7182. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  7183. }
  7184. if (WinConfiguration->BalloonNotifications)
  7185. {
  7186. AddNote(NoteMessage);
  7187. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  7188. }
  7189. else
  7190. {
  7191. FlashOnBackground();
  7192. PostNote(NoteMessage, Seconds, OnClick, UserData);
  7193. }
  7194. }
  7195. }
  7196. //---------------------------------------------------------------------------
  7197. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7198. {
  7199. RestoreApp();
  7200. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7201. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7202. !NonVisualDataModule->Busy)
  7203. {
  7204. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7205. TTerminalManager::Instance()->ActiveSession = Terminal;
  7206. if (!ComponentVisible[fcQueueView])
  7207. {
  7208. ToggleQueueVisibility();
  7209. GoToQueue();
  7210. }
  7211. }
  7212. }
  7213. //---------------------------------------------------------------------------
  7214. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7215. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7216. {
  7217. UnicodeString Message;
  7218. TNotifyEvent OnClick = NULL;
  7219. TObject * UserData = NULL;
  7220. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7221. switch (Event)
  7222. {
  7223. case qeEmptyButMonitored:
  7224. if ((ATerminal != Terminal) || QueueInvisible)
  7225. {
  7226. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7227. OnClick = QueueEmptyNoteClicked;
  7228. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7229. TerminalNoteData->Terminal = ATerminal;
  7230. UserData = TerminalNoteData;
  7231. }
  7232. break;
  7233. case qeEmpty:
  7234. OperationComplete(ATerminal->QueueOperationStart);
  7235. break;
  7236. case qePendingUserAction:
  7237. if ((ATerminal != Terminal) ||
  7238. (QueueInvisible && !IsQueueAutoPopup()))
  7239. {
  7240. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7241. }
  7242. break;
  7243. default:
  7244. DebugFail();
  7245. }
  7246. if (!Message.IsEmpty())
  7247. {
  7248. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7249. }
  7250. }
  7251. //---------------------------------------------------------------------------
  7252. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7253. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7254. {
  7255. if (FDDExtMapFile != NULL)
  7256. {
  7257. CloseHandle(FDDExtMapFile);
  7258. FDDExtMapFile = NULL;
  7259. }
  7260. if (WinConfiguration->DDFakeFile)
  7261. {
  7262. DDFakeFileInitDrag(FileList, Created);
  7263. }
  7264. }
  7265. //---------------------------------------------------------------------------
  7266. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7267. {
  7268. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7269. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7270. {
  7271. FFakeFileDropTarget = FileName;
  7272. }
  7273. }
  7274. //---------------------------------------------------------------------------
  7275. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7276. {
  7277. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7278. if (!ForceDirectories(ApiPath(Result)))
  7279. {
  7280. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7281. }
  7282. FileSetAttr(ApiPath(Result), faHidden);
  7283. return Result;
  7284. }
  7285. //---------------------------------------------------------------------------
  7286. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7287. bool & Created)
  7288. {
  7289. FFakeFileDropTarget = UnicodeString();
  7290. FDragFakeDirectory = CreateFakeTransferDirectory();
  7291. FileList->AddItem(NULL, FDragFakeDirectory);
  7292. Created = true;
  7293. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7294. {
  7295. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7296. }
  7297. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7298. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7299. {
  7300. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7301. TDragExtCommStruct* CommStruct;
  7302. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7303. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7304. DebugAssert(CommStruct != NULL);
  7305. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7306. CommStruct->Dragging = true;
  7307. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7308. LENOF(CommStruct->DropDest));
  7309. NULL_TERMINATE(CommStruct->DropDest);
  7310. UnmapViewOfFile(CommStruct);
  7311. }
  7312. }
  7313. //---------------------------------------------------------------------------
  7314. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7315. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7316. {
  7317. bool Result;
  7318. DebugAssert(!IsLocalBrowserMode());
  7319. if (Sender == RemoteDirView)
  7320. {
  7321. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7322. }
  7323. else
  7324. {
  7325. DebugAssert(Sender == RemoteDriveView);
  7326. TStrings * FileList = RemoteDriveView->DragFileList();
  7327. try
  7328. {
  7329. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7330. }
  7331. __finally
  7332. {
  7333. delete FileList;
  7334. }
  7335. }
  7336. if (FDDTargetControl == RemoteDriveView)
  7337. {
  7338. RemoteDriveView->UpdateDropTarget();
  7339. }
  7340. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7341. // or from tree view to dir view.
  7342. // foMove happens when file/dir is dragged from remote tree view outside of
  7343. // application via dragex
  7344. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7345. (DropSourceControl == RemoteDriveView))
  7346. {
  7347. RemoteDriveView->UpdateDropSource();
  7348. }
  7349. return Result;
  7350. }
  7351. //---------------------------------------------------------------------------
  7352. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7353. {
  7354. DebugAssert(!IsLocalBrowserMode());
  7355. // This also handles drops of remote files to queue.
  7356. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7357. SAFE_DESTROY(FDDFileList);
  7358. if (!FFakeFileDropTarget.IsEmpty())
  7359. {
  7360. RemoveDir(ApiPath(FFakeFileDropTarget));
  7361. }
  7362. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7363. {
  7364. try
  7365. {
  7366. TDragResult DDResult = (Sender == RemoteDirView) ?
  7367. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7368. // Focus is moved to the target application,
  7369. // but as we are going to present the UI, we need to steal the focus back.
  7370. // This most likely won't work though (windows does not allow application
  7371. // to steal focus most of the time)
  7372. Application->BringToFront();
  7373. // On older version of Windows we never got drMove here, see also comment below.
  7374. // On Windows 10, we get the "move".
  7375. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7376. {
  7377. UnicodeString TargetDirectory;
  7378. TFileOperation Operation;
  7379. // drInvalid may mean drMove, see comment below
  7380. switch (DDResult)
  7381. {
  7382. case drCopy:
  7383. Operation = foCopy;
  7384. break;
  7385. case drMove:
  7386. Operation = foMove;
  7387. break;
  7388. default:
  7389. DebugFail();
  7390. case drInvalid:
  7391. // prefer "copy" for safety
  7392. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7393. break;
  7394. }
  7395. TTransferOperationParam Param;
  7396. UnicodeString CounterName;
  7397. bool ForceQueue;
  7398. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7399. {
  7400. // we get drInvalid both if d&d was intercepted by ddext,
  7401. // and when users drops on no-drop location.
  7402. // we tell the difference by existence of response from ddext,
  7403. // so we ignore absence of response in this case
  7404. if (DDResult != drInvalid)
  7405. {
  7406. // here we know that the extension is installed,
  7407. // as it is checked as soon as drag&drop starts from
  7408. // RemoteFileControlDDCreateDragFileList
  7409. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7410. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7411. {
  7412. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7413. }
  7414. else
  7415. {
  7416. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7417. }
  7418. }
  7419. }
  7420. else
  7421. {
  7422. // download using fake file
  7423. Param.Temp = false;
  7424. Param.DragDrop = true;
  7425. if (ForceQueue)
  7426. {
  7427. Param.Queue = asOn;
  7428. }
  7429. if (Sender == RemoteDirView)
  7430. {
  7431. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7432. }
  7433. if (RemoteFileControlFileOperation(Sender, Operation,
  7434. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7435. {
  7436. Configuration->Usage->Inc(CounterName);
  7437. }
  7438. }
  7439. }
  7440. }
  7441. __finally
  7442. {
  7443. CloseHandle(FDDExtMapFile);
  7444. FDDExtMapFile = NULL;
  7445. delete FDragFakeMonitors;
  7446. FDragFakeMonitors = NULL;
  7447. RemoveDir(ApiPath(FDragFakeDirectory));
  7448. FDragFakeDirectory = L"";
  7449. FFakeFileDropTarget = UnicodeString();
  7450. }
  7451. }
  7452. if (FDragDropSshTerminate.get() != NULL)
  7453. {
  7454. FDragDropSshTerminate->Rethrow();
  7455. }
  7456. }
  7457. //---------------------------------------------------------------------------
  7458. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7459. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7460. {
  7461. // Remember drop effect so we know (when user drops files), if we copy or move
  7462. FLastDropEffect = dwEffect;
  7463. }
  7464. //---------------------------------------------------------------------------
  7465. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7466. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7467. {
  7468. bool Result;
  7469. if (FDDTargetControl == QueueView3)
  7470. {
  7471. Directory = DefaultDownloadTargetDirectory();
  7472. Result = true;
  7473. CounterName = L"DownloadsDragDropQueue";
  7474. ForceQueue = true;
  7475. }
  7476. else if (!FFakeFileDropTarget.IsEmpty())
  7477. {
  7478. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7479. Result = true;
  7480. ForceQueue = false;
  7481. CounterName = L"DownloadsDragDropFakeFile";
  7482. }
  7483. else
  7484. {
  7485. ForceQueue = false;
  7486. Enabled = false;
  7487. try
  7488. {
  7489. int Timer = 0;
  7490. Result = false;
  7491. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7492. {
  7493. {
  7494. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7495. TDragExtCommStruct* CommStruct;
  7496. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7497. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7498. DebugAssert(CommStruct != NULL);
  7499. Result = !CommStruct->Dragging;
  7500. if (Result)
  7501. {
  7502. Directory = ExtractFilePath(CommStruct->DropDest);
  7503. CounterName = L"DownloadsDragDropExternalExt";
  7504. }
  7505. UnmapViewOfFile(CommStruct);
  7506. }
  7507. if (!Result)
  7508. {
  7509. Sleep(50);
  7510. Timer += 50;
  7511. Application->ProcessMessages();
  7512. }
  7513. }
  7514. }
  7515. __finally
  7516. {
  7517. Enabled = true;
  7518. }
  7519. }
  7520. return Result;
  7521. }
  7522. //---------------------------------------------------------------------------
  7523. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7524. const UnicodeString TempDir, int SecDelay)
  7525. {
  7526. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7527. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7528. if (FDelayedDeletionTimer == NULL)
  7529. {
  7530. DebugAssert(HandleAllocated());
  7531. FDelayedDeletionTimer = new TTimer(this);
  7532. FDelayedDeletionTimer->Interval = 10000;
  7533. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7534. }
  7535. else
  7536. {
  7537. FDelayedDeletionTimer->Enabled = true;
  7538. }
  7539. }
  7540. //---------------------------------------------------------------------------
  7541. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7542. {
  7543. DebugAssert(FDelayedDeletionList != NULL);
  7544. TDateTime N = Now();
  7545. TDateTime Alert;
  7546. UnicodeString Directory;
  7547. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7548. {
  7549. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7550. if ((N >= Alert) || (Sender == NULL))
  7551. {
  7552. Directory = FDelayedDeletionList->Strings[Index];
  7553. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7554. {
  7555. FDelayedDeletionList->Delete(Index);
  7556. }
  7557. }
  7558. }
  7559. if (FDelayedDeletionList->Count == 0)
  7560. {
  7561. FDelayedDeletionTimer->Enabled = false;
  7562. }
  7563. }
  7564. //---------------------------------------------------------------------------
  7565. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7566. {
  7567. DebugAssert(!IsLocalBrowserMode());
  7568. if (IsFileControl(FDDTargetControl, osRemote) ||
  7569. (FDDTargetControl == SessionsPageControl))
  7570. {
  7571. TManagedTerminal * TargetTerminal = NULL;
  7572. TFileOperation Operation = foNone;
  7573. if (FDDTargetControl == SessionsPageControl)
  7574. {
  7575. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7576. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7577. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7578. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7579. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7580. {
  7581. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7582. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7583. {
  7584. Operation = foRemoteMove;
  7585. }
  7586. else
  7587. {
  7588. Operation = foRemoteCopy;
  7589. }
  7590. }
  7591. }
  7592. else
  7593. {
  7594. if (FLastDropEffect == DROPEFFECT_MOVE)
  7595. {
  7596. Operation = foRemoteMove;
  7597. }
  7598. else if (FLastDropEffect == DROPEFFECT_COPY)
  7599. {
  7600. Operation = foRemoteCopy;
  7601. }
  7602. }
  7603. if (Operation != foNone)
  7604. {
  7605. RemoteFileControlFileOperation(DropSourceControl,
  7606. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7607. }
  7608. // abort drag&drop
  7609. Abort();
  7610. }
  7611. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7612. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7613. (FDDTargetControl != QueueView3))
  7614. {
  7615. DebugAssert(!FDragTempDir.IsEmpty());
  7616. TTransferType Type;
  7617. UnicodeString TempDir = FDragTempDir;
  7618. // We clear FDragTempDir before calling
  7619. // just in case it fail (raises exception)
  7620. FDragTempDir = L"";
  7621. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7622. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7623. // empty directory parameter means temp directory -> don't display it!
  7624. UnicodeString TargetDir = L"";
  7625. int Options = 0;
  7626. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7627. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7628. {
  7629. Abort();
  7630. }
  7631. // TargetDir is set when dropped on local file control
  7632. // (this was workaround for legacy dirview event handling, now it should be
  7633. // made prettier)
  7634. if (TargetDir.IsEmpty())
  7635. {
  7636. TargetDir = TempDir;
  7637. if (ForceDirectories(ApiPath(TargetDir)))
  7638. {
  7639. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7640. FPendingTempSpaceWarn = true;
  7641. try
  7642. {
  7643. FDragDropOperation = true;
  7644. int Params = cpTemporary |
  7645. (Type == ttMove ? cpDelete : 0);
  7646. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7647. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7648. }
  7649. __finally
  7650. {
  7651. FDragDropOperation = false;
  7652. FPendingTempSpaceWarn = false;
  7653. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7654. }
  7655. }
  7656. else
  7657. {
  7658. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7659. }
  7660. }
  7661. }
  7662. }
  7663. //---------------------------------------------------------------------------
  7664. void __fastcall TCustomScpExplorerForm::DDDownload(
  7665. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  7666. {
  7667. DebugAssert(!IsLocalBrowserMode());
  7668. TAutoBatch AutoBatch(this);
  7669. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  7670. UpdateCopyParamCounters(*CopyParam);
  7671. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7672. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7673. {
  7674. RemoteDriveView->UpdateDropSource();
  7675. }
  7676. }
  7677. //---------------------------------------------------------------------------
  7678. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7679. {
  7680. public:
  7681. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7682. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7683. {
  7684. }
  7685. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7686. {
  7687. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7688. TDataObjectFilesEx::AllowData(FormatEtc);
  7689. }
  7690. };
  7691. //---------------------------------------------------------------------------
  7692. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7693. TObject * Sender, TDataObject *& DataObject)
  7694. {
  7695. if (FDDExtMapFile != NULL)
  7696. {
  7697. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7698. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7699. {
  7700. Abort();
  7701. }
  7702. if (FileList->Count > 0)
  7703. {
  7704. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7705. if (!FilesObject->IsValid(true, true))
  7706. {
  7707. FilesObject->_Release();
  7708. }
  7709. else
  7710. {
  7711. DataObject = FilesObject;
  7712. }
  7713. }
  7714. }
  7715. }
  7716. //---------------------------------------------------------------------------
  7717. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7718. {
  7719. DebugFail();
  7720. }
  7721. //---------------------------------------------------------------------------
  7722. void __fastcall TCustomScpExplorerForm::GoToTree()
  7723. {
  7724. ComponentVisible[fcRemoteTree] = true;
  7725. DriveView(osRemote)->SetFocus();
  7726. }
  7727. //---------------------------------------------------------------------------
  7728. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7729. TPanelExport Export)
  7730. {
  7731. TCustomDirView * DirView = this->DirView(Side);
  7732. std::unique_ptr<TStrings> ExportData(new TStringList());
  7733. switch (Export)
  7734. {
  7735. case pePath:
  7736. ExportData->Add(DirView->PathName);
  7737. break;
  7738. case peFileList:
  7739. case peFullFileList:
  7740. {
  7741. bool FullPath = (Export == peFullFileList);
  7742. DirView->CreateFileList(false, FullPath, ExportData.get());
  7743. for (int Index = 0; Index < ExportData->Count; Index++)
  7744. {
  7745. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7746. {
  7747. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7748. }
  7749. }
  7750. }
  7751. break;
  7752. default:
  7753. DebugFail();
  7754. }
  7755. return ExportData.release();
  7756. }
  7757. //---------------------------------------------------------------------------
  7758. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7759. TPanelExport Export, TPanelExportDestination Destination)
  7760. {
  7761. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7762. PanelExportStore(Side, Export, Destination, ExportData.get());
  7763. }
  7764. //---------------------------------------------------------------------------
  7765. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7766. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7767. TStrings * ExportData)
  7768. {
  7769. if (Destination == pedClipboard)
  7770. {
  7771. TInstantOperationVisualizer Visualizer;
  7772. CopyToClipboard(ExportData);
  7773. }
  7774. else
  7775. {
  7776. DebugFail();
  7777. }
  7778. }
  7779. //---------------------------------------------------------------------------
  7780. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7781. {
  7782. TCustomDirView * DirView = this->DirView(Side);
  7783. UnicodeString Mask = DirView->Mask;
  7784. if (DoFilterMaskDialog(DirView, Mask))
  7785. {
  7786. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7787. Configuration->Usage->Inc(L"Filters");
  7788. }
  7789. }
  7790. //---------------------------------------------------------------------------
  7791. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7792. {
  7793. return FQueueController->DefaultOperation();
  7794. }
  7795. //---------------------------------------------------------------------------
  7796. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7797. TQueueOperation Operation, void ** Param)
  7798. {
  7799. switch (Operation)
  7800. {
  7801. case qoPreferences:
  7802. return true;
  7803. case qoGoTo:
  7804. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7805. case qoOnceEmpty:
  7806. return IsAnythingQueued();
  7807. default:
  7808. return FQueueController->AllowOperation(Operation, Param);
  7809. }
  7810. }
  7811. //---------------------------------------------------------------------------
  7812. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7813. {
  7814. if (DebugAlwaysTrue(QueueView3->Visible))
  7815. {
  7816. QueueView3->SetFocus();
  7817. }
  7818. }
  7819. //---------------------------------------------------------------------------
  7820. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7821. TQueueOperation Operation, void * Param)
  7822. {
  7823. if (Operation == qoGoTo)
  7824. {
  7825. GoToQueue();
  7826. }
  7827. else if (Operation == qoPreferences)
  7828. {
  7829. PreferencesDialog(pmQueue);
  7830. }
  7831. else
  7832. {
  7833. FQueueController->ExecuteOperation(Operation, Param);
  7834. }
  7835. }
  7836. //---------------------------------------------------------------------------
  7837. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7838. {
  7839. return (Queue != NULL) && Queue->Enabled;
  7840. }
  7841. //---------------------------------------------------------------------------
  7842. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7843. {
  7844. DebugAssert(Queue != NULL);
  7845. if (Queue != NULL)
  7846. {
  7847. Queue->Enabled = !Queue->Enabled;
  7848. }
  7849. }
  7850. //---------------------------------------------------------------------------
  7851. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7852. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7853. {
  7854. FQueueActedItem = QueueView3->ItemFocused;
  7855. }
  7856. //---------------------------------------------------------------------------
  7857. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7858. {
  7859. return TopDock->Height + (MessageDock->Visible ? MessageDock->Height : 0) + QueueSplitter->Height;
  7860. }
  7861. //---------------------------------------------------------------------------
  7862. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7863. {
  7864. return
  7865. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7866. (QueueDock->Visible ? QueueDock->Height : 0) +
  7867. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7868. }
  7869. //---------------------------------------------------------------------------
  7870. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7871. {
  7872. return ScaleByTextHeight(this, 48);
  7873. }
  7874. //---------------------------------------------------------------------------
  7875. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7876. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7877. {
  7878. // when queue is hidden by double-clicking splitter, stray attempt to
  7879. // resize the panel with strange value arrives, make sure it is ignored
  7880. if (ComponentVisible[fcQueueView])
  7881. {
  7882. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7883. if (NewSize > MaxHeightLimit)
  7884. {
  7885. NewSize = MaxHeightLimit;
  7886. }
  7887. int MinHeightLimit =
  7888. GetStaticQueuePanelComponentsHeight() +
  7889. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7890. GetMinQueueViewHeight();
  7891. if (NewSize < MinHeightLimit)
  7892. {
  7893. NewSize = MinHeightLimit;
  7894. }
  7895. }
  7896. else
  7897. {
  7898. Accept = false;
  7899. }
  7900. }
  7901. //---------------------------------------------------------------------------
  7902. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7903. {
  7904. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7905. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7906. if (QueueViewHeight < GetMinQueueViewHeight())
  7907. {
  7908. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7909. }
  7910. }
  7911. //---------------------------------------------------------------------------
  7912. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7913. TDragObject *& /*DragObject*/)
  7914. {
  7915. FQueueActedItem = QueueView3->ItemFocused;
  7916. }
  7917. //---------------------------------------------------------------------------
  7918. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7919. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7920. {
  7921. Accept = true;
  7922. if (Source == QueueView3)
  7923. {
  7924. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7925. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7926. if (Accept)
  7927. {
  7928. TQueueItemProxy * QueueItem;
  7929. TQueueItemProxy * DestQueueItem;
  7930. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7931. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7932. Accept = (QueueItem != DestQueueItem) &&
  7933. (QueueItem->Status == TQueueItem::qsPending) &&
  7934. (DestQueueItem->Status == TQueueItem::qsPending);
  7935. }
  7936. }
  7937. }
  7938. //---------------------------------------------------------------------------
  7939. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7940. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7941. {
  7942. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7943. {
  7944. TQueueItemProxy * QueueItem;
  7945. TQueueItemProxy * DestQueueItem;
  7946. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7947. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7948. QueueItem->Move(DestQueueItem);
  7949. }
  7950. }
  7951. //---------------------------------------------------------------------------
  7952. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7953. {
  7954. if ((QueueView3->ItemFocused == NULL) &&
  7955. (QueueView3->Items->Count > 0))
  7956. {
  7957. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7958. }
  7959. QueueLabelUpdateStatus();
  7960. }
  7961. //---------------------------------------------------------------------------
  7962. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7963. {
  7964. QueueLabelUpdateStatus();
  7965. }
  7966. //---------------------------------------------------------------------------
  7967. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7968. {
  7969. QueueLabel->UpdateStatus();
  7970. }
  7971. //---------------------------------------------------------------------------
  7972. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7973. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7974. {
  7975. if (Selected)
  7976. {
  7977. NonVisualDataModule->UpdateNonVisibleActions();
  7978. }
  7979. }
  7980. //---------------------------------------------------------------------------
  7981. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7982. {
  7983. TDragDropFilesEx * Result = NULL;
  7984. if (Sender == SessionsPageControl)
  7985. {
  7986. Result = FSessionsDragDropFilesEx;
  7987. }
  7988. else if (Sender == QueueView3)
  7989. {
  7990. Result = FQueueDragDropFilesEx;
  7991. }
  7992. else
  7993. {
  7994. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7995. if (DirView != NULL)
  7996. {
  7997. Result = DirView->DragDropFilesEx;
  7998. }
  7999. else
  8000. {
  8001. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  8002. if (DriveView != NULL)
  8003. {
  8004. Result = DriveView->DragDropFilesEx;
  8005. }
  8006. }
  8007. }
  8008. DebugAssert(Result != NULL);
  8009. return Result;
  8010. }
  8011. //---------------------------------------------------------------------------
  8012. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  8013. {
  8014. return (DView->SelCount == DView->FilesCount);
  8015. }
  8016. //---------------------------------------------------------------------------
  8017. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  8018. {
  8019. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  8020. }
  8021. //---------------------------------------------------------------------------
  8022. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  8023. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  8024. {
  8025. DebugAssert(!IsLocalBrowserMode());
  8026. TFileOperation Operation;
  8027. switch (Effect)
  8028. {
  8029. case DROPEFFECT_MOVE:
  8030. Operation = foMove;
  8031. break;
  8032. case DROPEFFECT_COPY:
  8033. // occures on WinXP (reported by user)
  8034. default:
  8035. Operation = foCopy;
  8036. break;
  8037. };
  8038. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  8039. // see a comment in TUnixDirView::PerformItemDragDropOperation
  8040. if (DragDropFilesEx->FileList->Count > 0)
  8041. {
  8042. TStrings * FileList = new TStringList();
  8043. try
  8044. {
  8045. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  8046. {
  8047. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  8048. }
  8049. FDragDropOperation = true;
  8050. TTransferOperationParam Param;
  8051. Param.TargetDirectory = TargetPath;
  8052. // upload, no temp dirs
  8053. Param.Temp = false;
  8054. Param.DragDrop = DragDrop;
  8055. Param.Options =
  8056. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  8057. if (ForceQueue)
  8058. {
  8059. Param.Queue = asOn;
  8060. }
  8061. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  8062. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  8063. {
  8064. if (IsFileControl(DropSourceControl, osLocal))
  8065. {
  8066. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  8067. }
  8068. else
  8069. {
  8070. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  8071. }
  8072. }
  8073. }
  8074. __finally
  8075. {
  8076. FDragDropOperation = false;
  8077. delete FileList;
  8078. }
  8079. }
  8080. }
  8081. //---------------------------------------------------------------------------
  8082. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  8083. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  8084. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  8085. {
  8086. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  8087. }
  8088. //---------------------------------------------------------------------------
  8089. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  8090. TObject * Sender, int grfKeyState, int & dwEffect)
  8091. {
  8092. DebugAssert(!IsLocalBrowserMode());
  8093. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  8094. if ((dwEffect != DROPEFFECT_NONE) &&
  8095. IsFileControl(DropSourceControl, osRemote))
  8096. {
  8097. // do not allow drop on remote panel (on free space, still allow drop on directories)
  8098. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  8099. (RemoteDirView->DropTarget == NULL))
  8100. {
  8101. dwEffect = DROPEFFECT_NONE;
  8102. }
  8103. else
  8104. {
  8105. if (dwEffect == DROPEFFECT_COPY)
  8106. {
  8107. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  8108. // currently we support copying always (at least via temporary directory);
  8109. // remove associated checks once this all proves stable and working
  8110. bool CopyCapable = true;
  8111. // if we do not support neither of operations, there's no discussion
  8112. if (!MoveCapable && !CopyCapable)
  8113. {
  8114. dwEffect = DROPEFFECT_NONE;
  8115. }
  8116. else
  8117. {
  8118. // The default effect inside remote panel is move,
  8119. // unless we do not support it, but support copy
  8120. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  8121. {
  8122. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  8123. }
  8124. else
  8125. {
  8126. // with ctrl-down, we want copy unless it is not supported
  8127. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  8128. }
  8129. }
  8130. }
  8131. }
  8132. }
  8133. }
  8134. //---------------------------------------------------------------------------
  8135. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  8136. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  8137. {
  8138. DebugAssert(!IsLocalBrowserMode());
  8139. if (FDDTotalSize >= 0)
  8140. {
  8141. if (File->IsDirectory)
  8142. {
  8143. FDDTotalSize = -1;
  8144. }
  8145. else
  8146. {
  8147. FDDTotalSize += File->Size;
  8148. }
  8149. }
  8150. DebugAssert(!FDragTempDir.IsEmpty());
  8151. // TODO: this is quite ineffective
  8152. // TODO: what if invalid character replacement is disabled?
  8153. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  8154. UnicodeString TransferFileName;
  8155. if (Sender == RemoteDriveView)
  8156. {
  8157. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  8158. }
  8159. else
  8160. {
  8161. TransferFileName = File->FileName;
  8162. }
  8163. FDDFileList->AddObject(TransferFileName, File);
  8164. }
  8165. //---------------------------------------------------------------------------
  8166. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  8167. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  8168. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  8169. {
  8170. // sometimes we do not get DDEnd so the list is not released
  8171. SAFE_DESTROY(FDDFileList);
  8172. FDDFileList = new TStringList();
  8173. FDragTempDir = WinConfiguration->TemporaryDir();
  8174. FDDTotalSize = 0;
  8175. FDragDropSshTerminate.reset(NULL);
  8176. }
  8177. //---------------------------------------------------------------------------
  8178. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  8179. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  8180. HRESULT & Result)
  8181. {
  8182. if (Result == DRAGDROP_S_DROP)
  8183. {
  8184. try
  8185. {
  8186. GlobalDragImageList->HideDragImage();
  8187. try
  8188. {
  8189. RemoteFileControlDDTargetDrop();
  8190. }
  8191. catch(ESshTerminate & E)
  8192. {
  8193. FDragDropSshTerminate.reset(E.Clone());
  8194. }
  8195. }
  8196. catch (Exception &E)
  8197. {
  8198. // If downloading fails we need to cancel drag&drop, otherwise
  8199. // Explorer shows error
  8200. // But by the way exception probably never reach this point as
  8201. // it's catched on way.
  8202. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8203. Result = DRAGDROP_S_CANCEL;
  8204. ShowExtendedException(Terminal, &E);
  8205. }
  8206. }
  8207. }
  8208. //---------------------------------------------------------------------------
  8209. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8210. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8211. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8212. {
  8213. TFileMasks::TParams MaskParams;
  8214. MaskParams.Size = Size;
  8215. MaskParams.Modification = Modification;
  8216. // this does not re-parse the mask if it is the same as the last time
  8217. FDirViewMatchMask = Masks;
  8218. bool ImplicitMatch;
  8219. Matches =
  8220. // Local and RecurseInclude parameters have no effect as there's no path.
  8221. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  8222. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  8223. (AllowImplicitMatches || !ImplicitMatch);
  8224. }
  8225. //---------------------------------------------------------------------------
  8226. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8227. TObject * Sender, TListItem * Item, WORD & Indexes)
  8228. {
  8229. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8230. UnicodeString Ext;
  8231. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  8232. {
  8233. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  8234. }
  8235. else
  8236. {
  8237. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  8238. }
  8239. if (SameText(Ext, Configuration->PartialExt))
  8240. {
  8241. Indexes |= oiPartial;
  8242. }
  8243. }
  8244. //---------------------------------------------------------------------------
  8245. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8246. {
  8247. return
  8248. DirViewEnabled(osCurrent) &&
  8249. DirView(osCurrent)->CanPasteFromClipBoard();
  8250. }
  8251. //---------------------------------------------------------------------------
  8252. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8253. {
  8254. bool Result = false;
  8255. if (CanPasteToDirViewFromClipBoard())
  8256. {
  8257. Result = true;
  8258. }
  8259. else
  8260. {
  8261. UnicodeString ClipboardText;
  8262. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8263. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8264. {
  8265. if (StoredSessions->IsUrl(ClipboardText))
  8266. {
  8267. Result = true;
  8268. }
  8269. else
  8270. {
  8271. Result = DirViewEnabled(osCurrent);
  8272. }
  8273. }
  8274. }
  8275. return Result;
  8276. }
  8277. //---------------------------------------------------------------------------
  8278. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8279. {
  8280. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8281. if (DoesClipboardContainOurFiles())
  8282. {
  8283. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8284. {
  8285. DebugAssert(!IsLocalBrowserMode());
  8286. TTerminalManager * Manager = TTerminalManager::Instance();
  8287. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8288. Manager->ActiveSession = FClipboardTerminal;
  8289. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8290. }
  8291. }
  8292. else if (CanPasteToDirViewFromClipBoard())
  8293. {
  8294. DirView(osCurrent)->PasteFromClipBoard();
  8295. }
  8296. else
  8297. {
  8298. UnicodeString ClipboardText;
  8299. if (NonEmptyTextFromClipboard(ClipboardText))
  8300. {
  8301. if (StoredSessions->IsUrl(ClipboardText))
  8302. {
  8303. NewSession(ClipboardText);
  8304. }
  8305. else
  8306. {
  8307. DirView(osCurrent)->Path = ClipboardText;
  8308. }
  8309. }
  8310. }
  8311. }
  8312. //---------------------------------------------------------------------------
  8313. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8314. {
  8315. // TBD
  8316. }
  8317. //---------------------------------------------------------------------------
  8318. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8319. {
  8320. TCustomDirView * ADirView = DirView(Side);
  8321. ADirView->SelectAll(Mode);
  8322. ADirView->SetFocus();
  8323. }
  8324. //---------------------------------------------------------------------------
  8325. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8326. {
  8327. TCustomDirView * ADirView = DirView(Side);
  8328. TFileFilter Filter;
  8329. DefaultFileFilter(Filter);
  8330. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8331. {
  8332. Configuration->Usage->Inc(L"MaskSelections");
  8333. ADirView->SelectFiles(Filter, Select);
  8334. ADirView->SetFocus();
  8335. }
  8336. }
  8337. //---------------------------------------------------------------------------
  8338. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8339. {
  8340. TCustomDirView * ADirView = DirView(Side);
  8341. ADirView->RestoreSelectedNames();
  8342. ADirView->SetFocus();
  8343. }
  8344. //---------------------------------------------------------------------------
  8345. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8346. {
  8347. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8348. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8349. {
  8350. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8351. UnicodeString Ext;
  8352. if (!IsSideLocalBrowser(osCurrent))
  8353. {
  8354. Ext = UnixExtractFileExt(FileName);
  8355. }
  8356. else
  8357. {
  8358. Ext = ExtractFileExt(FileName);
  8359. }
  8360. if (Ext.IsEmpty())
  8361. {
  8362. Ext = L".";
  8363. }
  8364. TFileFilter Filter;
  8365. Filter.Masks = FORMAT(L"*%s", (Ext));
  8366. Filter.Directories = false;
  8367. CurrentDirView->SelectFiles(Filter, Select);
  8368. }
  8369. }
  8370. //---------------------------------------------------------------------------
  8371. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8372. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8373. {
  8374. UnicodeString Result;
  8375. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8376. {
  8377. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8378. }
  8379. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8380. {
  8381. // noop
  8382. }
  8383. else
  8384. {
  8385. Result =
  8386. FMTLOAD(FILE_INFO_FORMAT,
  8387. (FormatBytes(FileInfo.SelectedSize),
  8388. FormatBytes(FileInfo.FilesSize),
  8389. FormatNumber(FileInfo.SelectedCount),
  8390. FormatNumber(FileInfo.FilesCount)));
  8391. }
  8392. return Result;
  8393. }
  8394. //---------------------------------------------------------------------------
  8395. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8396. TTBXStatusPanel * Panel, TOperationSide Side)
  8397. {
  8398. if (Panel->Index == 1)
  8399. {
  8400. ToggleShowHiddenFiles();
  8401. }
  8402. else if (Panel->Index == 2)
  8403. {
  8404. Filter(Side);
  8405. }
  8406. }
  8407. //---------------------------------------------------------------------------
  8408. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8409. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8410. {
  8411. DebugAssert(!StatusBar->SimplePanel);
  8412. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8413. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8414. }
  8415. //---------------------------------------------------------------------------
  8416. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8417. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8418. {
  8419. DebugAssert(StatusBar->Panels->Count >= 3);
  8420. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8421. if (FileInfo.HiddenCount > 0)
  8422. {
  8423. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8424. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8425. }
  8426. else
  8427. {
  8428. HiddenFilesPanel->ViewPriority = 0;
  8429. // not really necessary, just to cleanup no-longer-valid data
  8430. HiddenFilesPanel->Caption = L"";
  8431. }
  8432. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8433. if (FileInfo.FilteredCount > 0)
  8434. {
  8435. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8436. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8437. }
  8438. else
  8439. {
  8440. FilteredFilesPanel->ViewPriority = 0;
  8441. // not really necessary, just to cleanup no-longer-valid data
  8442. FilteredFilesPanel->Caption = L"";
  8443. }
  8444. }
  8445. //---------------------------------------------------------------------------
  8446. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8447. TObject * /*Sender*/)
  8448. {
  8449. if (DirView(osOther)->Enabled)
  8450. {
  8451. DirView(osOther)->SetFocus();
  8452. }
  8453. }
  8454. //---------------------------------------------------------------------------
  8455. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8456. {
  8457. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8458. switch (Config.Show)
  8459. {
  8460. case qvShow:
  8461. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8462. {
  8463. Config.Show = qvHide;
  8464. }
  8465. else
  8466. {
  8467. Config.Show = Config.LastHideShow;
  8468. }
  8469. break;
  8470. case qvHideWhenEmpty:
  8471. if (ComponentVisible[fcQueueView])
  8472. {
  8473. Config.Show = qvHide;
  8474. }
  8475. else
  8476. {
  8477. Config.LastHideShow = Config.Show;
  8478. Config.Show = qvShow;
  8479. }
  8480. break;
  8481. case qvHide:
  8482. Config.LastHideShow = Config.Show;
  8483. Config.Show = qvShow;
  8484. break;
  8485. }
  8486. WinConfiguration->QueueView = Config;
  8487. }
  8488. //---------------------------------------------------------------------------
  8489. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8490. {
  8491. UnicodeString Result;
  8492. if (Terminal != NULL)
  8493. {
  8494. switch (WinConfiguration->PathInCaption)
  8495. {
  8496. case picShort:
  8497. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8498. break;
  8499. case picFull:
  8500. Result = Terminal->CurrentDirectory;
  8501. break;
  8502. }
  8503. }
  8504. return Result;
  8505. }
  8506. //---------------------------------------------------------------------------
  8507. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8508. {
  8509. TColor Result = GetWindowColor(FSessionColor);
  8510. return Result;
  8511. }
  8512. //---------------------------------------------------------------------------
  8513. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8514. {
  8515. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8516. return Result;
  8517. }
  8518. //---------------------------------------------------------------------------
  8519. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8520. {
  8521. if (!FSessionChanging)
  8522. {
  8523. TTerminalManager::Instance()->UpdateAppTitle();
  8524. }
  8525. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8526. // background to gray on Windows 7 (works on Windows 10).
  8527. // See also EnableControl
  8528. if (Showing)
  8529. {
  8530. if (ManagedSession != NULL)
  8531. {
  8532. // Update path when it changes
  8533. if ((SessionsPageControl->ActivePage != NULL) &&
  8534. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8535. {
  8536. UpdateSessionTab(SessionsPageControl->ActivePage);
  8537. }
  8538. }
  8539. NonVisualDataModule->ReconnectSessionAction->Update();
  8540. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8541. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8542. CenterReconnectToolbar();
  8543. bool HasTerminal = HasActiveTerminal();
  8544. RemoteDirView->Enabled = HasTerminal;
  8545. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8546. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8547. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8548. RemoteDriveView->Color = RemoteDirView->Color;
  8549. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8550. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8551. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8552. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8553. QueueFileList->Enabled = QueueView3->Enabled;
  8554. QueueFileList->Color = QueueView3->Color;
  8555. QueueFileList->Font->Color = QueueView3->Font->Color;
  8556. QueueLabelUpdateStatus();
  8557. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8558. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8559. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8560. {
  8561. UpdateDarkMode();
  8562. }
  8563. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8564. MessageDock->Visible = (MessageDock->ToolbarCount > 0);
  8565. }
  8566. }
  8567. //---------------------------------------------------------------------------
  8568. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8569. {
  8570. UpdateControls();
  8571. }
  8572. //---------------------------------------------------------------------------
  8573. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8574. TObject * Sender)
  8575. {
  8576. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8577. DebugAssert(DirView != NULL);
  8578. DoDirViewLoaded(DirView);
  8579. TransferPresetAutoSelect();
  8580. }
  8581. //---------------------------------------------------------------------------
  8582. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8583. {
  8584. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8585. // first run after installation
  8586. if (double(Updates.LastCheck) == 0)
  8587. {
  8588. AppLog(L"First run, scheduling updates check for the next run");
  8589. // make sure next time there will be an update (if enabled)
  8590. Updates.LastCheck = TDateTime(1);
  8591. WinConfiguration->Updates = Updates;
  8592. }
  8593. else
  8594. {
  8595. TDateTime Period;
  8596. if (IsUWP() && !IsOfficialPackage())
  8597. {
  8598. Period = DefaultUpdatesPeriod;
  8599. AppLog(L"Thirdparty UWP package, using default updates check period");
  8600. }
  8601. else
  8602. {
  8603. Period = Updates.Period;
  8604. }
  8605. AppLogFmt(L"Updates check period: %.2f", (double(Period)));
  8606. if (double(Period) > 0)
  8607. {
  8608. TDateTime Interval = Now() - Updates.LastCheck;
  8609. AppLogFmt(L"Interval since the last updates check: %.2f", (double(Interval)));
  8610. if (Interval >= Period)
  8611. {
  8612. StartUpdateThread(UpdatesChecked);
  8613. }
  8614. }
  8615. }
  8616. }
  8617. //---------------------------------------------------------------------------
  8618. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8619. {
  8620. AppLog(L"Updates check done");
  8621. UnicodeString Message;
  8622. bool New;
  8623. TQueryType Type;
  8624. GetUpdatesMessage(Message, New, Type, false);
  8625. if (!Message.IsEmpty())
  8626. {
  8627. AppLogFmt(L"Updates check message: %s", (Message));
  8628. if (New)
  8629. {
  8630. AppLog(L"New version detected");
  8631. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8632. }
  8633. if (!New && (Type != qtWarning))
  8634. {
  8635. AppLog(L"Posting non-critical updates note");
  8636. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8637. }
  8638. else
  8639. {
  8640. AppLog(L"Posting new version or critical message notification");
  8641. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8642. }
  8643. if (New)
  8644. {
  8645. Configuration->Usage->Inc(L"UpdateNotifications");
  8646. }
  8647. }
  8648. }
  8649. //---------------------------------------------------------------------------
  8650. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8651. {
  8652. RestoreApp();
  8653. bool CanDisplay = !NonVisualDataModule->Busy;
  8654. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8655. {
  8656. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8657. }
  8658. if (CanDisplay)
  8659. {
  8660. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8661. CheckForUpdates(true);
  8662. }
  8663. else
  8664. {
  8665. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8666. }
  8667. }
  8668. //---------------------------------------------------------------------------
  8669. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8670. TCopyParamRuleData & Data)
  8671. {
  8672. DebugAssert(Terminal != NULL);
  8673. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8674. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8675. Data.RemoteDirectory = RemoteDirView->PathName;
  8676. }
  8677. //---------------------------------------------------------------------------
  8678. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8679. {
  8680. // Terminal can be null when we are changing local directory implicitly,
  8681. // because it has been deleted, while no session is connected
  8682. // (Login dialog is open)
  8683. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8684. {
  8685. DebugAssert(!IsLocalBrowserMode());
  8686. TCopyParamRuleData Data;
  8687. GetTransferPresetAutoSelectData(Data);
  8688. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8689. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8690. if (CopyParamIndex < 0)
  8691. {
  8692. // there is no preset that matches autoselection
  8693. // set preset that we consider "default"
  8694. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8695. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8696. }
  8697. else
  8698. {
  8699. // there is preset matching autoselection
  8700. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8701. if (CopyParamName == FCopyParamAutoSelected)
  8702. {
  8703. // autoselected the same preset as the last time
  8704. // make no change (i.e. preserve custom user preset, if any)
  8705. }
  8706. else
  8707. {
  8708. // autoselected the different preset then the last time (or there
  8709. // was default preset set)
  8710. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8711. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8712. }
  8713. }
  8714. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8715. {
  8716. if (CopyParamIndex >= 0)
  8717. {
  8718. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8719. }
  8720. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8721. try
  8722. {
  8723. int Fmt =
  8724. (CopyParamIndex < 0) ?
  8725. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8726. COPY_PARAM_AUTOSELECTED;
  8727. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8728. Data->Message = MainInstructions(Message);
  8729. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8730. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8731. CopyParamAttrs);
  8732. if (CopyParamIndex >= 0)
  8733. {
  8734. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8735. Info = FORMAT(L"%s\n \n%s", (Info,
  8736. FMTLOAD(COPY_PARAM_RULE,
  8737. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8738. }
  8739. Data->Message += L"\n\n" + Info;
  8740. if (WinConfiguration->CopyParamAutoSelectNotice)
  8741. {
  8742. TransferPresetNoteMessage(Data, true);
  8743. }
  8744. else
  8745. {
  8746. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8747. Data = NULL; // ownership passed
  8748. }
  8749. }
  8750. __finally
  8751. {
  8752. delete Data;
  8753. }
  8754. }
  8755. }
  8756. }
  8757. //---------------------------------------------------------------------------
  8758. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8759. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8760. {
  8761. DebugAssert(NoteData != NULL);
  8762. DebugAssert(!IsLocalBrowserMode());
  8763. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8764. TQueryButtonAlias Aliases[1];
  8765. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8766. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8767. Params.Aliases = Aliases;
  8768. Params.AliasesCount = LENOF(Aliases);
  8769. unsigned int Result =
  8770. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8771. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8772. switch (Result)
  8773. {
  8774. case qaNeverAskAgain:
  8775. DebugAssert(AllowNeverAskAgain);
  8776. WinConfiguration->CopyParamAutoSelectNotice = false;
  8777. break;
  8778. case qaIgnore:
  8779. PreferencesDialog(pmPresets);
  8780. break;
  8781. }
  8782. }
  8783. //---------------------------------------------------------------------------
  8784. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8785. {
  8786. // as of now useless, as this is used for notes only, never for balloons, ...
  8787. RestoreApp();
  8788. // .. and we should never be busy here
  8789. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8790. {
  8791. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8792. }
  8793. }
  8794. //---------------------------------------------------------------------------
  8795. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8796. TPreferencesMode APreferencesMode)
  8797. {
  8798. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8799. TCopyParamRuleData Data;
  8800. if (Terminal != NULL)
  8801. {
  8802. PreferencesData.reset(new TPreferencesDialogData());
  8803. GetTransferPresetAutoSelectData(Data);
  8804. PreferencesData->CopyParamRuleData = &Data;
  8805. }
  8806. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8807. }
  8808. //---------------------------------------------------------------------------
  8809. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8810. const TCustomCommandType & Command)
  8811. {
  8812. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8813. {
  8814. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8815. }
  8816. }
  8817. //---------------------------------------------------------------------------
  8818. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8819. {
  8820. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8821. TCustomCommandType Command;
  8822. // make sure we use local custom command when remote are not supported
  8823. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8824. {
  8825. Command = FLastCustomCommand;
  8826. }
  8827. else
  8828. {
  8829. Command.Params = Command.Params | ccLocal;
  8830. }
  8831. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8832. FEditingFocusedAdHocCommand = OnFocused;
  8833. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8834. int Options =
  8835. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8836. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8837. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8838. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8839. {
  8840. FLastCustomCommand = Command;
  8841. UpdateCustomCommandsToolbar();
  8842. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8843. }
  8844. }
  8845. //---------------------------------------------------------------------------
  8846. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8847. {
  8848. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8849. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8850. DebugAssert(State > 0);
  8851. if (State <= 0)
  8852. {
  8853. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8854. }
  8855. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8856. }
  8857. //---------------------------------------------------------------------------
  8858. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8859. TCustomCommandType & Command, int & State)
  8860. {
  8861. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8862. if (Result)
  8863. {
  8864. Command = FLastCustomCommand;
  8865. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8866. }
  8867. return Result;
  8868. }
  8869. //---------------------------------------------------------------------------
  8870. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8871. {
  8872. ResetIncrementalSearch();
  8873. }
  8874. //---------------------------------------------------------------------------
  8875. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8876. {
  8877. DebugAssert(ComponentVisible[Component]);
  8878. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8879. }
  8880. //---------------------------------------------------------------------------
  8881. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8882. {
  8883. // when queue panel is resized here directly, the status bar is stretched
  8884. // over whole space the panel occupied
  8885. PostComponentHide(fcQueueView);
  8886. }
  8887. //---------------------------------------------------------------------------
  8888. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8889. {
  8890. ComponentVisible[fcQueueFileList] = false;
  8891. }
  8892. //---------------------------------------------------------------------------
  8893. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8894. {
  8895. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8896. RefreshColorMode();
  8897. ResetSysDarkTheme();
  8898. ConfigurationChanged();
  8899. ConfigureInterface();
  8900. // Should be called for all controls
  8901. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8902. }
  8903. //---------------------------------------------------------------------------
  8904. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8905. {
  8906. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8907. if (!FInvalid &&
  8908. (Message.LParam != 0) &&
  8909. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8910. {
  8911. ThemeChanged();
  8912. }
  8913. TForm::Dispatch(&Message);
  8914. }
  8915. //---------------------------------------------------------------------------
  8916. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8917. {
  8918. TMessage * M = static_cast<TMessage*>(Message);
  8919. switch (M->Msg)
  8920. {
  8921. case CM_DIALOGCHAR:
  8922. CMDialogChar(*M);
  8923. break;
  8924. case WM_APPCOMMAND:
  8925. WMAppCommand(*M);
  8926. break;
  8927. case WM_SYSCOMMAND:
  8928. WMSysCommand(*M);
  8929. break;
  8930. case WM_QUERYENDSESSION:
  8931. WMQueryEndSession(*M);
  8932. break;
  8933. case WM_ENDSESSION:
  8934. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8935. break;
  8936. #ifdef _DEBUG
  8937. case WM_WINDOWPOSCHANGED:
  8938. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8939. break;
  8940. case CM_CUSTOMSTYLECHANGED:
  8941. TForm::Dispatch(Message);
  8942. break;
  8943. #endif
  8944. case WM_COMPONENT_HIDE:
  8945. {
  8946. Byte Component = static_cast<Byte>(M->WParam);
  8947. // sanity check
  8948. if (ComponentVisible[Component])
  8949. {
  8950. // special treatment
  8951. if (Component == fcQueueView)
  8952. {
  8953. ToggleQueueVisibility();
  8954. DebugAssert(!ComponentVisible[fcQueueView]);
  8955. }
  8956. else
  8957. {
  8958. ComponentVisible[Component] = false;
  8959. }
  8960. }
  8961. }
  8962. break;
  8963. case WM_COPYDATA:
  8964. WMCopyData(*M);
  8965. break;
  8966. case WM_MANAGES_CAPTION:
  8967. // caption managed in TTerminalManager::UpdateAppTitle()
  8968. M->Result = 1;
  8969. break;
  8970. case WM_WANTS_MOUSEWHEEL:
  8971. M->Result = 1;
  8972. break;
  8973. case CM_SHOWINGCHANGED:
  8974. CMShowingChanged(*M);
  8975. break;
  8976. case WM_CLOSE:
  8977. WMClose(*M);
  8978. break;
  8979. case WM_DPICHANGED:
  8980. WMDpiChanged(*M);
  8981. break;
  8982. case CM_DPICHANGED:
  8983. CMDpiChanged(*M);
  8984. break;
  8985. case WM_SETTINGCHANGE:
  8986. WMSettingChange(*M);
  8987. break;
  8988. case CM_DIALOGKEY:
  8989. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8990. break;
  8991. default:
  8992. TForm::Dispatch(Message);
  8993. break;
  8994. }
  8995. }
  8996. //---------------------------------------------------------------------------
  8997. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8998. {
  8999. // noop
  9000. }
  9001. //---------------------------------------------------------------------------
  9002. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  9003. {
  9004. TForm::Dispatch(&Message);
  9005. FSessionColors->Clear();
  9006. AddFixedSessionImages();
  9007. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  9008. UpdateImages();
  9009. }
  9010. //---------------------------------------------------------------------------
  9011. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  9012. {
  9013. TForm::Dispatch(&Message);
  9014. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  9015. UpdatePixelsPerInchMainWindowCounter();
  9016. }
  9017. //---------------------------------------------------------------------------
  9018. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  9019. {
  9020. // Cannot close window while we are busy.
  9021. // We cannot test this in FormCloseQuery as that is called also from
  9022. // Close(), which is called by CloseApplicationAction. So we can be busy
  9023. // there already even, when it is legitimate to close the application.
  9024. // Possibly a better place to handle this would be WMSysCommand.
  9025. if (NonVisualDataModule->Busy)
  9026. {
  9027. Message.Result = 1;
  9028. }
  9029. else
  9030. {
  9031. TForm::Dispatch(&Message);
  9032. }
  9033. }
  9034. //---------------------------------------------------------------------------
  9035. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  9036. {
  9037. if (!FShowingChanged)
  9038. {
  9039. AddStartupSequence(L"D");
  9040. FShowingChanged = true;
  9041. }
  9042. // Ignoring implicit local-local browser of Commander, opened from DoShow
  9043. bool NoSession = (ManagedSession == NULL);
  9044. // This calls TCustomScpExplorerForm::DoShow
  9045. TForm::Dispatch(&Message);
  9046. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  9047. if (Showing && !FStarted)
  9048. {
  9049. FStarted = true;
  9050. InterfaceStarted();
  9051. }
  9052. if (Showing && NoSession)
  9053. {
  9054. // When we are starting minimized (i.e. from an installer),
  9055. // postpone showing Login dialog until we get restored.
  9056. // Otherwise the Login dialog (and Authentication window) show restored
  9057. // over invisible (minimized) main window.
  9058. if (WindowState == wsMinimized)
  9059. {
  9060. FNeedSession = true;
  9061. }
  9062. else
  9063. {
  9064. // This happens before application ever goes idle, so the toolbars would
  9065. // stay enabled (initial state) until the Login dialog is dismissed.
  9066. UpdateActions();
  9067. NonVisualDataModule->StartBusy();
  9068. try
  9069. {
  9070. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  9071. // otherwise the dialog does not receive focus.
  9072. // With Commander interface the ProcessMessages is called already
  9073. // by TDriveView, but with Explorer interface, we need to call it explicily
  9074. Application->ProcessMessages();
  9075. // do not reload sessions, they have been loaded just now (optimization)
  9076. NeedSession(true);
  9077. }
  9078. __finally
  9079. {
  9080. NonVisualDataModule->EndBusy();
  9081. }
  9082. }
  9083. }
  9084. }
  9085. //---------------------------------------------------------------------------
  9086. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  9087. {
  9088. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  9089. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  9090. }
  9091. //---------------------------------------------------------------------------
  9092. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  9093. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  9094. int & MaxHeight)
  9095. {
  9096. // workaround for bug in TWinControl.CalcConstraints
  9097. // Check for empty rect (restore from iconic state) is done there only after
  9098. // call to AdjustClientRect, which enlarges the rect (for forms).
  9099. TRect R = GetClientRect();
  9100. // when restoring from iconic state, place no restrictions
  9101. if (IsRectEmpty(R))
  9102. {
  9103. MinWidth = 0;
  9104. MinHeight = 0;
  9105. MaxWidth = 32000;
  9106. MaxHeight = 32000;
  9107. }
  9108. }
  9109. //---------------------------------------------------------------------------
  9110. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  9111. TSpaceAvailable & ASpaceAvailable, bool & Close)
  9112. {
  9113. // terminal can be already closed (e.g. dropped connection)
  9114. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  9115. {
  9116. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  9117. try
  9118. {
  9119. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  9120. }
  9121. catch(...)
  9122. {
  9123. if (!Terminal->Active)
  9124. {
  9125. Close = true;
  9126. }
  9127. throw;
  9128. }
  9129. }
  9130. }
  9131. //---------------------------------------------------------------------------
  9132. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  9133. {
  9134. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  9135. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  9136. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  9137. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  9138. {
  9139. OnGetSpaceAvailable = GetSpaceAvailable;
  9140. }
  9141. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  9142. OnGetSpaceAvailable);
  9143. }
  9144. //---------------------------------------------------------------------------
  9145. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  9146. {
  9147. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  9148. Terminal->FillSessionDataForCode(Data.get());
  9149. return Data.release();
  9150. }
  9151. //---------------------------------------------------------------------------
  9152. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  9153. {
  9154. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  9155. DoGenerateUrlDialog(Data.get(), Paths);
  9156. }
  9157. //---------------------------------------------------------------------------
  9158. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  9159. {
  9160. GenerateUrl(NULL);
  9161. }
  9162. //---------------------------------------------------------------------------
  9163. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  9164. {
  9165. std::unique_ptr<TStrings> Paths(new TStringList());
  9166. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  9167. GenerateUrl(Paths.get());
  9168. }
  9169. //---------------------------------------------------------------------------
  9170. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  9171. {
  9172. FSessionColor = value;
  9173. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  9174. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  9175. static_cast<TObject *>(GetComponent(fcColorMenu)));
  9176. DebugAssert(ColorItem != NULL);
  9177. ColorItem->Color = C;
  9178. NonVisualDataModule->ColorMenuItem->Color = C;
  9179. UpdateControls();
  9180. }
  9181. //---------------------------------------------------------------------------
  9182. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  9183. {
  9184. if (value != FSessionColor)
  9185. {
  9186. UpdateSessionColor(value);
  9187. // Is null when called from LastTerminalClosed
  9188. if (Terminal != NULL)
  9189. {
  9190. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  9191. }
  9192. }
  9193. }
  9194. //---------------------------------------------------------------------------
  9195. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  9196. {
  9197. bool Result = FNoteTimer->Enabled;
  9198. if (Result)
  9199. {
  9200. // cannot cancel note too early
  9201. bool NotEarly =
  9202. (Now() - FNoteShown >
  9203. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  9204. if (Force || NotEarly)
  9205. {
  9206. FNoteTimer->Enabled = false;
  9207. FNote = L"";
  9208. SAFE_DESTROY(FNoteData);
  9209. FOnNoteClick = NULL;
  9210. FNoteHints = FNotes->Text;
  9211. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  9212. UpdateStatusBar();
  9213. }
  9214. }
  9215. return Result;
  9216. }
  9217. //---------------------------------------------------------------------------
  9218. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  9219. {
  9220. DebugAssert(FNoteTimer->Enabled);
  9221. CancelNote(true);
  9222. }
  9223. //---------------------------------------------------------------------------
  9224. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9225. {
  9226. FNoteHints = FNotes->Text.TrimRight();
  9227. }
  9228. //---------------------------------------------------------------------------
  9229. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9230. {
  9231. int P = Note.Pos(L"\n");
  9232. if (P > 0)
  9233. {
  9234. Note.SetLength(P - 1);
  9235. }
  9236. FNotes->Add(FORMAT(L"[%s] %s",
  9237. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9238. while (FNotes->Count > 10)
  9239. {
  9240. FNotes->Delete(0);
  9241. }
  9242. if (UpdateNow)
  9243. {
  9244. UpdateNoteHints();
  9245. UpdateStatusBar();
  9246. }
  9247. }
  9248. //---------------------------------------------------------------------------
  9249. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  9250. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9251. {
  9252. int P = Note.Pos(L"\n");
  9253. if (P > 0)
  9254. {
  9255. Note.SetLength(P - 1);
  9256. }
  9257. UpdateNoteHints();
  9258. FNote = Note;
  9259. SAFE_DESTROY(FNoteData);
  9260. FNoteData = NoteData;
  9261. FOnNoteClick = OnNoteClick;
  9262. AddNote(Note, false);
  9263. UpdateStatusBar();
  9264. FNoteShown = Now();
  9265. FNoteTimer->Enabled = false;
  9266. if (Seconds == 0)
  9267. {
  9268. Seconds = WinConfiguration->NotificationsTimeout;
  9269. }
  9270. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9271. FNoteTimer->Enabled = true;
  9272. }
  9273. //---------------------------------------------------------------------------
  9274. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9275. {
  9276. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9277. }
  9278. //---------------------------------------------------------------------------
  9279. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9280. {
  9281. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9282. {
  9283. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9284. }
  9285. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9286. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9287. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9288. 0, NULL, NULL);
  9289. }
  9290. //---------------------------------------------------------------------------
  9291. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9292. {
  9293. bool NewShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9294. UnicodeString Note =
  9295. FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1), (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, (NewShowHiddenFiles ? 2 : 3))));
  9296. PostNote(Note, 0, NULL, NULL);
  9297. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9298. WinConfiguration->ShowHiddenFiles = NewShowHiddenFiles;
  9299. }
  9300. //---------------------------------------------------------------------------
  9301. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9302. {
  9303. WinConfiguration->FormatSizeBytes = Style;
  9304. }
  9305. //---------------------------------------------------------------------------
  9306. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9307. {
  9308. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9309. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9310. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9311. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9312. }
  9313. //---------------------------------------------------------------------------
  9314. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9315. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9316. {
  9317. if (Panel->Index == 0)
  9318. {
  9319. if (FOnNoteClick != NULL)
  9320. {
  9321. // prevent the user data from being freed by possible call
  9322. // to CancelNote or PostNote during call to OnNoteClick
  9323. std::unique_ptr<TObject> NoteData(FNoteData);
  9324. TNotifyEvent OnNoteClick = FOnNoteClick;
  9325. FNoteData = NULL;
  9326. // need to cancel the note as we are going to delete its user data
  9327. CancelNote(true);
  9328. OnNoteClick(NoteData.get());
  9329. }
  9330. }
  9331. if (HasActiveTerminal() &&
  9332. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9333. {
  9334. FileSystemInfo();
  9335. }
  9336. }
  9337. //---------------------------------------------------------------------------
  9338. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9339. {
  9340. // workaround:
  9341. // 1) for unknown reason, disabling window, while minimized,
  9342. // prevents it from restoring, even if it was enabled again meanwhile
  9343. // 2) when disabling the main window, while another has focus
  9344. // minimize is no longer possible
  9345. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9346. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9347. // Shouldn't we use IsApplicationMinimized() here?
  9348. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9349. {
  9350. Enabled = false;
  9351. }
  9352. FLockLevel++;
  9353. }
  9354. //---------------------------------------------------------------------------
  9355. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9356. {
  9357. DebugAssert(FLockLevel > 0);
  9358. FLockLevel--;
  9359. if (FLockLevel == 0)
  9360. {
  9361. DebugAssert(FLockSuspendLevel == 0);
  9362. Enabled = true;
  9363. }
  9364. }
  9365. //---------------------------------------------------------------------------
  9366. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9367. {
  9368. // We need to make sure that window is enabled when the last modal window closes
  9369. // otherwise focus is not restored correctly.
  9370. // So we re-enable the window when modal window opens and
  9371. // disable it back after is closes
  9372. if (FLockLevel > 0)
  9373. {
  9374. // while we have nesting counter, we know that we never be called
  9375. // recursivelly as Application->OnModalBegin is called only
  9376. // for the top-level modal window
  9377. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9378. {
  9379. // won't be disabled when conditions in LockWindow() were not satisfied
  9380. FDisabledOnLockSuspend = !Enabled;
  9381. // When minimized to tray (or actually when set to SW_HIDE),
  9382. // setting Enabled makes the window focusable even when there's
  9383. // modal window over it
  9384. if (!FTrayIcon->Visible)
  9385. {
  9386. Enabled = true;
  9387. }
  9388. }
  9389. FLockSuspendLevel++;
  9390. }
  9391. }
  9392. //---------------------------------------------------------------------------
  9393. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9394. {
  9395. if (FLockSuspendLevel > 0)
  9396. {
  9397. DebugAssert(FLockLevel > 0);
  9398. FLockSuspendLevel--;
  9399. // see comment in SuspendWindowLock
  9400. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9401. {
  9402. // Note that window can be enabled here, when we were minized to tray when
  9403. // was SuspendWindowLock() called.
  9404. // We should possibly do the same check as in LockWindow(),
  9405. // if it is ever possible that the consitions change between
  9406. // SuspendWindowLock() and ResumeWindowLock()
  9407. Enabled = !FDisabledOnLockSuspend;
  9408. }
  9409. }
  9410. }
  9411. //---------------------------------------------------------------------------
  9412. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9413. {
  9414. TTBXComboBoxItem * RemotePathComboBox =
  9415. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9416. if (!TextOnly)
  9417. {
  9418. TStrings * Items = RemotePathComboBox->Strings;
  9419. Items->BeginUpdate();
  9420. try
  9421. {
  9422. Items->Clear();
  9423. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9424. {
  9425. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9426. while (!IsUnixRootPath(APath))
  9427. {
  9428. int P = APath.LastDelimiter(L'/');
  9429. DebugAssert(P >= 0);
  9430. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9431. APath.SetLength(P - 1);
  9432. }
  9433. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9434. }
  9435. }
  9436. __finally
  9437. {
  9438. Items->EndUpdate();
  9439. }
  9440. }
  9441. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9442. // Setting ItemIndex to -1 does not reset its text
  9443. if (RemotePathComboBox->Strings->Count == 0)
  9444. {
  9445. RemotePathComboBox->Text = L"";
  9446. }
  9447. }
  9448. //---------------------------------------------------------------------------
  9449. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9450. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9451. {
  9452. if (AIndex < 0)
  9453. {
  9454. AIndex = Sender->ItemIndex;
  9455. }
  9456. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9457. }
  9458. //---------------------------------------------------------------------------
  9459. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9460. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9461. int & ImageIndex)
  9462. {
  9463. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9464. }
  9465. //---------------------------------------------------------------------------
  9466. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9467. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9468. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9469. {
  9470. if (!IsLocalBrowserMode())
  9471. {
  9472. ARect.Left += (10 * AIndex);
  9473. }
  9474. }
  9475. //---------------------------------------------------------------------------
  9476. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9477. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9478. {
  9479. AWidth += (10 * AIndex);
  9480. }
  9481. //---------------------------------------------------------------------------
  9482. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9483. {
  9484. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9485. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9486. int Index = RemotePathComboBox->ItemIndex;
  9487. while (Index < RemotePathComboBox->Strings->Count - 1)
  9488. {
  9489. APath = UnixExtractFileDir(APath);
  9490. Index++;
  9491. }
  9492. // VanDyke style paths
  9493. if (APath.IsEmpty())
  9494. {
  9495. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9496. APath = ROOTDIRECTORY;
  9497. }
  9498. if (RemoteDirView->Path != APath)
  9499. {
  9500. RemoteDirView->Path = APath;
  9501. }
  9502. }
  9503. //---------------------------------------------------------------------------
  9504. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9505. {
  9506. DoRemotePathComboBoxItemClick(Sender);
  9507. }
  9508. //---------------------------------------------------------------------------
  9509. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9510. {
  9511. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9512. DebugUsedParam(Sender);
  9513. UpdateRemotePathComboBox(true);
  9514. }
  9515. //---------------------------------------------------------------------------
  9516. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9517. {
  9518. DoRemotePathComboBoxCancel(Sender);
  9519. }
  9520. //---------------------------------------------------------------------------
  9521. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9522. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9523. {
  9524. ResetIncrementalSearch();
  9525. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9526. DebugAssert(DirView != NULL);
  9527. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9528. {
  9529. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9530. // OnEditing is called also from TCustomListView::CanEdit
  9531. if (Edit != NULL)
  9532. {
  9533. EditSelectBaseName(Edit);
  9534. }
  9535. }
  9536. }
  9537. //---------------------------------------------------------------------------
  9538. //---------------------------------------------------------------------------
  9539. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9540. {
  9541. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9542. Result->AutoDetectDnD = false;
  9543. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9544. Result->RenderDataOn = rdoEnterAndDropSync;
  9545. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9546. return Result;
  9547. }
  9548. //---------------------------------------------------------------------------
  9549. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9550. {
  9551. if (IsWin10Build(2004))
  9552. {
  9553. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9554. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9555. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9556. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9557. }
  9558. }
  9559. //---------------------------------------------------------------------------
  9560. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9561. {
  9562. TForm::CreateWnd();
  9563. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9564. UpdateDarkMode();
  9565. if (FSessionsDragDropFilesEx == NULL)
  9566. {
  9567. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9568. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9569. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9570. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9571. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9572. }
  9573. if (FQueueDragDropFilesEx == NULL)
  9574. {
  9575. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9576. // No need to set OnDragOver as we do not have any restrictions
  9577. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9578. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9579. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9580. }
  9581. }
  9582. //---------------------------------------------------------------------------
  9583. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9584. {
  9585. TForm::DestroyWnd();
  9586. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9587. FQueueDragDropFilesEx->DragDropControl = NULL;
  9588. }
  9589. //---------------------------------------------------------------------------
  9590. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9591. {
  9592. SideEnter(FCurrentSide);
  9593. FEverShown = true;
  9594. }
  9595. //---------------------------------------------------------------------------
  9596. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9597. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9598. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9599. {
  9600. if (!NonVisualDataModule->Busy)
  9601. {
  9602. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9603. Configuration->Usage->Inc(L"FileFinds");
  9604. LockWindow(true);
  9605. NonVisualDataModule->StartBusy();
  9606. WinConfiguration->LockedInterface = true;
  9607. try
  9608. {
  9609. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9610. }
  9611. __finally
  9612. {
  9613. WinConfiguration->LockedInterface = false;
  9614. NonVisualDataModule->EndBusy();
  9615. UnlockWindow();
  9616. }
  9617. }
  9618. }
  9619. //---------------------------------------------------------------------------
  9620. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9621. {
  9622. if (!NonVisualDataModule->Busy)
  9623. {
  9624. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9625. SetFocus();
  9626. RemoteDirView->Path = UnixExtractFilePath(Path);
  9627. UnicodeString FileName = UnixExtractFileName(Path);
  9628. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9629. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9630. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9631. if (Item == NULL)
  9632. {
  9633. ReloadDirectory(osRemote);
  9634. Item = RemoteDirView->FindFileItem(FileName);
  9635. }
  9636. if (Item != NULL)
  9637. {
  9638. RemoteDirView->ItemFocused = Item;
  9639. RemoteDirView->ItemFocused->MakeVisible(false);
  9640. RemoteDirView->SetFocus();
  9641. }
  9642. }
  9643. }
  9644. //---------------------------------------------------------------------------
  9645. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9646. {
  9647. bool Result = !NonVisualDataModule->Busy;
  9648. if (Result)
  9649. {
  9650. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9651. }
  9652. return Result;
  9653. }
  9654. //---------------------------------------------------------------------------
  9655. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9656. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9657. {
  9658. if (CanOperateOnFoundFiles(ATerminal))
  9659. {
  9660. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9661. FOnFileOperationFinished = OnFileOperationFinished;
  9662. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9663. }
  9664. }
  9665. //---------------------------------------------------------------------------
  9666. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9667. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9668. {
  9669. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9670. }
  9671. //---------------------------------------------------------------------------
  9672. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9673. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9674. {
  9675. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9676. }
  9677. //---------------------------------------------------------------------------
  9678. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9679. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9680. {
  9681. if (CanOperateOnFoundFiles(ATerminal))
  9682. {
  9683. for (int Index = 0; Index < FileList->Count; Index++)
  9684. {
  9685. UnicodeString FileName = FileList->Strings[Index];
  9686. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9687. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9688. OnFileOperationFinished(osRemote, FileName, true);
  9689. }
  9690. }
  9691. }
  9692. //---------------------------------------------------------------------------
  9693. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9694. {
  9695. FFileFindTerminal = Terminal;
  9696. ShowFileFindDialog(
  9697. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9698. DoDownloadFoundFiles, DoEditFoundFiles);
  9699. }
  9700. //---------------------------------------------------------------------------
  9701. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9702. {
  9703. FTaskbarList = TaskbarList;
  9704. }
  9705. //---------------------------------------------------------------------------
  9706. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9707. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9708. {
  9709. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9710. if (Index >= 0)
  9711. {
  9712. if (Button == mbLeft)
  9713. {
  9714. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9715. // If switching tab (switching session) takes long enough for user
  9716. // to actually release the button, "mouse down" is still raised,
  9717. // but we do not get "mouse up" event, so dragging is not cancelled,
  9718. // prevent that by not beginning dragging in the first place.
  9719. int Key = ((GetSystemMetrics(SM_SWAPBUTTON) == 0) ? VK_LBUTTON : VK_RBUTTON);
  9720. if (IsKeyPressed(Key))
  9721. {
  9722. // when user clicks the "+", we get mouse down only after the session
  9723. // is closed, when new session tab is already on X:Y, so dragging
  9724. // starts, prevent that
  9725. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9726. {
  9727. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9728. if (Terminal != NULL)
  9729. {
  9730. SessionsPageControl->BeginDrag(false);
  9731. }
  9732. }
  9733. }
  9734. }
  9735. else if (Button == mbMiddle)
  9736. {
  9737. // ignore middle-click for "New session tab" and the last local-local tab
  9738. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9739. if ((Session != NULL) && CanCloseSession(Session))
  9740. {
  9741. CloseSessionTab(Index);
  9742. }
  9743. }
  9744. }
  9745. }
  9746. //---------------------------------------------------------------------------
  9747. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9748. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9749. {
  9750. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9751. // do not allow dropping on the "+" tab
  9752. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9753. if ((TargetTerminal != NULL) &&
  9754. (SessionsPageControl->ActivePage->PageIndex != Index))
  9755. {
  9756. Configuration->Usage->Inc(L"SessionTabMoves");
  9757. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9758. // but we want to actually prevent that to avoid flicker
  9759. SessionsPageControl->ActivePage->PageIndex = Index;
  9760. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9761. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9762. }
  9763. }
  9764. //---------------------------------------------------------------------------
  9765. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9766. TObject * Sender, TObject * Source, int X, int Y,
  9767. TDragState /*State*/, bool & Accept)
  9768. {
  9769. Accept = (Sender == Source);
  9770. if (Accept)
  9771. {
  9772. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9773. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9774. // do not allow dragging to the "+" tab
  9775. Accept = (Terminal != NULL);
  9776. }
  9777. }
  9778. //---------------------------------------------------------------------------
  9779. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9780. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9781. {
  9782. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9783. if (Index < 0)
  9784. {
  9785. Effect = DROPEFFECT_NONE;
  9786. }
  9787. else
  9788. {
  9789. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9790. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9791. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9792. {
  9793. Effect = DROPEFFECT_NONE;
  9794. }
  9795. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9796. {
  9797. Effect = DROPEFFECT_COPY;
  9798. }
  9799. }
  9800. }
  9801. //---------------------------------------------------------------------------
  9802. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9803. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9804. {
  9805. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9806. // do not allow dropping on local-local, disconnected and the "+" tab
  9807. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9808. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9809. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9810. {
  9811. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9812. if (!IsFileControl(DropSourceControl, osRemote))
  9813. {
  9814. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9815. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9816. // Why don't we use Terminal->CurrentDirectory directly?
  9817. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9818. // do not force queue, drag drop
  9819. false, true);
  9820. }
  9821. }
  9822. }
  9823. //---------------------------------------------------------------------------
  9824. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9825. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9826. {
  9827. // Downloads are handled in RemoteFileControlDDEnd
  9828. if (!IsFileControl(DropSourceControl, osRemote))
  9829. {
  9830. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9831. Terminal->CurrentDirectory,
  9832. // force queue, drag drop
  9833. true, true);
  9834. }
  9835. }
  9836. //---------------------------------------------------------------------------
  9837. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9838. {
  9839. FShowing = false;
  9840. // Do not save empty workspace
  9841. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9842. {
  9843. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9844. UnicodeString Name = WorkspaceName();
  9845. DoSaveWorkspace(
  9846. Name, DataList.get(),
  9847. !Configuration->DisablePasswordStoring &&
  9848. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9849. WinConfiguration->LastStoredSession = Name;
  9850. }
  9851. }
  9852. //---------------------------------------------------------------------------
  9853. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9854. {
  9855. TManagedTerminal * ManagedTerminal =
  9856. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9857. if (ManagedTerminal != NULL)
  9858. {
  9859. ManagedTerminal->DirectoryLoaded = Now();
  9860. }
  9861. }
  9862. //---------------------------------------------------------------------------
  9863. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9864. TListItem * /*Item*/, bool /*Selected*/)
  9865. {
  9866. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9867. switch (DirView->LastSelectMethod)
  9868. {
  9869. case smKeyboard:
  9870. Configuration->Usage->Inc(L"KeyboardSelections");
  9871. break;
  9872. case smMouse:
  9873. Configuration->Usage->Inc(L"MouseSelections");
  9874. break;
  9875. }
  9876. }
  9877. //---------------------------------------------------------------------------
  9878. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9879. {
  9880. if (Color != 0)
  9881. {
  9882. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9883. }
  9884. else
  9885. {
  9886. return FSessionTabImageIndex;
  9887. }
  9888. }
  9889. //---------------------------------------------------------------------------
  9890. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9891. {
  9892. TPngImageCollectionItem * Item =
  9893. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9894. FSessionColors->AddPng(Item->PngImage);
  9895. return FSessionColors->Count - 1;
  9896. }
  9897. //---------------------------------------------------------------------------
  9898. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9899. {
  9900. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9901. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  9902. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  9903. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9904. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9905. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  9906. }
  9907. //---------------------------------------------------------------------------
  9908. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  9909. {
  9910. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  9911. }
  9912. //---------------------------------------------------------------------------
  9913. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  9914. {
  9915. switch (Side)
  9916. {
  9917. case osLocal:
  9918. return FNewLocalTabTabImageIndex;
  9919. case osRemote:
  9920. case osCurrent:
  9921. return FNewRemoteTabTabImageIndex;
  9922. default:
  9923. DebugFail();
  9924. return -1;
  9925. }
  9926. }
  9927. //---------------------------------------------------------------------------
  9928. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9929. {
  9930. for (int Index = 0; Index < Control->ControlCount; Index++)
  9931. {
  9932. TControl * AControl = Control->Controls[Index];
  9933. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9934. if (WinControl != NULL)
  9935. {
  9936. CollectItemsWithTextDisplayMode(WinControl);
  9937. }
  9938. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9939. if (Toolbar != NULL)
  9940. {
  9941. // we care for top-level items only
  9942. TTBCustomItem * Items = Toolbar->Items;
  9943. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9944. {
  9945. TTBCustomItem * Item = Items->Items[ItemIndex];
  9946. if (((Item->DisplayMode == nbdmImageAndText) ||
  9947. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9948. EligibleForImageDisplayMode(Item))
  9949. {
  9950. FItemsWithTextDisplayMode.insert(Item);
  9951. }
  9952. }
  9953. }
  9954. }
  9955. }
  9956. //---------------------------------------------------------------------------
  9957. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9958. {
  9959. return true;
  9960. }
  9961. //---------------------------------------------------------------------------
  9962. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9963. {
  9964. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9965. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9966. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9967. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9968. bool Result = true;
  9969. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9970. {
  9971. TTBCustomItem * Item = *i;
  9972. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9973. if (Label != NULL)
  9974. {
  9975. // optimization
  9976. if (Label->Visible == SelectiveToolbarText)
  9977. {
  9978. Result = false;
  9979. }
  9980. else
  9981. {
  9982. Label->Visible = SelectiveToolbarText;
  9983. }
  9984. }
  9985. else
  9986. {
  9987. // optimization
  9988. if (Item->DisplayMode == DisplayMode)
  9989. {
  9990. Result = false;
  9991. }
  9992. else
  9993. {
  9994. Item->DisplayMode = DisplayMode;
  9995. }
  9996. }
  9997. ++i;
  9998. }
  9999. UpdateNewTabTab();
  10000. return Result;
  10001. }
  10002. //---------------------------------------------------------------------------
  10003. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  10004. {
  10005. DebugFail();
  10006. }
  10007. //---------------------------------------------------------------------------
  10008. bool __fastcall TCustomScpExplorerForm::IsBusy()
  10009. {
  10010. // Among other, a lock level is non zero, while directory is being loaded,
  10011. // even when it happens as a result of dirview navigation,
  10012. // i.e. when TNonVisualDataModule is NOT FBusy.
  10013. // That's why the TNonVisualDataModule::GetBusy calls this method.
  10014. // Among other this prevents a panel auto update to occur while
  10015. // directory is changing.
  10016. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  10017. }
  10018. //---------------------------------------------------------------------------
  10019. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  10020. {
  10021. // While the window is disabled, we still seem to process menu shortcuts at least,
  10022. // so stop it at least here.
  10023. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  10024. bool Result;
  10025. if (Allowed == aaUpdate)
  10026. {
  10027. Result = true;
  10028. }
  10029. else
  10030. {
  10031. Result = !NonVisualDataModule->Busy;
  10032. if (Result && (Allowed == aaShortCut))
  10033. {
  10034. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  10035. Action->Update();
  10036. Result = Action->Enabled;
  10037. }
  10038. }
  10039. return Result;
  10040. }
  10041. //---------------------------------------------------------------------------
  10042. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  10043. {
  10044. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  10045. }
  10046. //---------------------------------------------------------------------------
  10047. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  10048. {
  10049. // This is somewhat redundant to LockWindow() call from
  10050. // TTerminalManager::TerminalReadDirectoryProgress.
  10051. // But disabling window is known not to block keyboard shorcuts
  10052. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  10053. if (Busy > 0)
  10054. {
  10055. if (NonVisualDataModule->Busy)
  10056. {
  10057. State = false;
  10058. }
  10059. else
  10060. {
  10061. NonVisualDataModule->StartBusy();
  10062. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  10063. LockWindow();
  10064. }
  10065. }
  10066. else if (Busy < 0)
  10067. {
  10068. UnlockWindow();
  10069. NonVisualDataModule->EndBusy();
  10070. }
  10071. else
  10072. {
  10073. State = NonVisualDataModule->Busy;
  10074. }
  10075. }
  10076. //---------------------------------------------------------------------------
  10077. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  10078. {
  10079. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  10080. if (Index >= 0)
  10081. {
  10082. TPopupMenu * PopupMenu = NULL;
  10083. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10084. if (Session != NULL)
  10085. {
  10086. SessionsPageControl->ActivePageIndex = Index;
  10087. if (DebugAlwaysTrue(SessionTabSwitched()))
  10088. {
  10089. // copied from TControl.WMContextMenu
  10090. SendCancelMode(SessionsPageControl);
  10091. // explicit popup instead of using PopupMenu property
  10092. // to avoid menu to popup somewhere within SessionTabSwitched above,
  10093. // while connecting yet not-connected session and hence
  10094. // allowing an access to commands over not-completelly connected session
  10095. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  10096. }
  10097. }
  10098. else
  10099. {
  10100. if (SupportsLocalBrowser())
  10101. {
  10102. PopupMenu = NonVisualDataModule->NewTabPopup;
  10103. }
  10104. }
  10105. if (PopupMenu != NULL)
  10106. {
  10107. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  10108. PopupMenu->PopupComponent = SessionsPageControl;
  10109. PopupMenu->Popup(Point.x, Point.y);
  10110. }
  10111. }
  10112. Handled = true;
  10113. }
  10114. //---------------------------------------------------------------------------
  10115. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  10116. {
  10117. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  10118. }
  10119. //---------------------------------------------------------------------------
  10120. void __fastcall TCustomScpExplorerForm::ChangePassword()
  10121. {
  10122. Configuration->Usage->Inc(L"PasswordChanges");
  10123. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  10124. ChangePasswordData->DisableAuthentationsExceptPassword();
  10125. ChangePasswordData->ChangePassword = true;
  10126. std::unique_ptr<TTerminal> ChangePasswordSession(
  10127. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  10128. ChangePasswordSession->AutoReadDirectory = false;
  10129. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  10130. {
  10131. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  10132. }
  10133. else
  10134. {
  10135. Configuration->Usage->Inc(L"PasswordChangesFailed");
  10136. }
  10137. }
  10138. //---------------------------------------------------------------------------
  10139. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  10140. {
  10141. // No nice way to assert SSH2
  10142. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  10143. }
  10144. //---------------------------------------------------------------------------
  10145. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  10146. {
  10147. TOperationVisualizer OperationVisualizer;
  10148. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  10149. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  10150. }
  10151. //---------------------------------------------------------------------------
  10152. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  10153. {
  10154. TForm::ChangeScale(M, D);
  10155. int APixelsPerInch = GetControlPixelsPerInch(this);
  10156. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  10157. try
  10158. {
  10159. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  10160. }
  10161. __finally
  10162. {
  10163. GUIConfiguration->ChangeResourceModule(ResourceModule);
  10164. }
  10165. }
  10166. //---------------------------------------------------------------------------
  10167. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  10168. {
  10169. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  10170. }
  10171. //---------------------------------------------------------------------
  10172. class TPublicControl : public TControl
  10173. {
  10174. friend class TCustomScpExplorerForm;
  10175. };
  10176. //---------------------------------------------------------------------------
  10177. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  10178. {
  10179. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  10180. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  10181. }
  10182. //---------------------------------------------------------------------------
  10183. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  10184. {
  10185. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  10186. OpenBookmark(Side, Bookmark);
  10187. }
  10188. //---------------------------------------------------------------------------
  10189. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  10190. {
  10191. DoBookmarkClick(osLocal, Sender);
  10192. }
  10193. //---------------------------------------------------------------------------
  10194. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  10195. {
  10196. DoBookmarkClick(osRemote, Sender);
  10197. }
  10198. //---------------------------------------------------------------------------
  10199. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  10200. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  10201. {
  10202. if (BookmarkList != NULL)
  10203. {
  10204. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  10205. if (IsLocalBrowserMode())
  10206. {
  10207. Side = osLocal;
  10208. }
  10209. if (!WinConfiguration->UseLocationProfiles)
  10210. {
  10211. std::unique_ptr<TStringList> Directories(new TStringList());
  10212. Directories->CaseSensitive = (Side == osRemote);
  10213. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10214. {
  10215. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10216. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  10217. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  10218. {
  10219. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10220. Item->Caption = EscapeHotkey(Directory);
  10221. Item->ShortCut = Bookmark->ShortCut;
  10222. Item->OnClick = OnBookmarkClick;
  10223. Item->Tag = reinterpret_cast<int>(Bookmark);
  10224. Directories->Add(Directory);
  10225. Menu->Add(Item.release());
  10226. }
  10227. }
  10228. }
  10229. else
  10230. {
  10231. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10232. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10233. {
  10234. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10235. if (!Bookmark->Node.IsEmpty())
  10236. {
  10237. Folders->Add(Bookmark->Node);
  10238. }
  10239. }
  10240. for (int Index = 0; Index < Folders->Count; Index++)
  10241. {
  10242. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10243. Item->Caption = Folders->Strings[Index];
  10244. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10245. Folders->Objects[Index] = Item.get();
  10246. Menu->Add(Item.release());
  10247. }
  10248. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10249. {
  10250. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10251. TTBCustomItem * Parent;
  10252. if (!Bookmark->Node.IsEmpty())
  10253. {
  10254. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10255. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10256. }
  10257. else
  10258. {
  10259. Parent = Menu;
  10260. }
  10261. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10262. Item->Caption = Bookmark->Name;
  10263. Item->ShortCut = Bookmark->ShortCut;
  10264. Item->OnClick = OnBookmarkClick;
  10265. Item->Tag = reinterpret_cast<int>(Bookmark);
  10266. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10267. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10268. {
  10269. // No hint for location profiles that are actually mere "bookmarks"
  10270. }
  10271. else
  10272. {
  10273. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10274. UnicodeString LongHint;
  10275. if (!Bookmark->Local.IsEmpty())
  10276. {
  10277. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10278. }
  10279. if (!Bookmark->Remote.IsEmpty())
  10280. {
  10281. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10282. }
  10283. AddToList(Hint, LongHint, L"|");
  10284. Item->Hint = Hint;
  10285. }
  10286. Parent->Add(Item.release());
  10287. }
  10288. }
  10289. AddMenuSeparator(Menu);
  10290. }
  10291. }
  10292. //---------------------------------------------------------------------------
  10293. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10294. {
  10295. Menu->Clear();
  10296. std::unique_ptr<TTBCustomItem> Item;
  10297. Item.reset(new TTBXItem(Owner));
  10298. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10299. Item->Options = Item->Options << tboDefault;
  10300. Menu->Add(Item.release());
  10301. AddMenuSeparator(Menu);
  10302. if (Terminal != NULL)
  10303. {
  10304. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10305. }
  10306. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10307. Item.reset(new TTBXItem(Owner));
  10308. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10309. Menu->Add(Item.release());
  10310. }
  10311. //---------------------------------------------------------------------------
  10312. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10313. {
  10314. bool Result = true;
  10315. bool Remote = !IsSideLocalBrowser(Side);
  10316. try
  10317. {
  10318. UnicodeString APath = Path;
  10319. if (Remote)
  10320. {
  10321. Terminal->ExceptionOnFail = true;
  10322. }
  10323. else
  10324. {
  10325. APath = ExpandEnvironmentVariables(Path);
  10326. }
  10327. try
  10328. {
  10329. DirView(Side)->Path = APath;
  10330. }
  10331. __finally
  10332. {
  10333. if (Remote)
  10334. {
  10335. Terminal->ExceptionOnFail = false;
  10336. }
  10337. }
  10338. }
  10339. catch (Exception & E)
  10340. {
  10341. if (!Remote || Terminal->Active)
  10342. {
  10343. ShowExtendedExceptionEx(Terminal, &E);
  10344. Result = false;
  10345. }
  10346. else
  10347. {
  10348. throw;
  10349. }
  10350. }
  10351. return Result;
  10352. }
  10353. //---------------------------------------------------------------------------
  10354. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10355. {
  10356. // Make sure there some feedback even when loading tiny local folders
  10357. TInstantOperationVisualizer Visualizer;
  10358. DirView(Side)->ReloadDirectory();
  10359. }
  10360. //---------------------------------------------------------------------------
  10361. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10362. {
  10363. if (Index == SessionsPageControl->TabIndex)
  10364. {
  10365. CloseSession();
  10366. }
  10367. else
  10368. {
  10369. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10370. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10371. TTerminalManager * Manager = TTerminalManager::Instance();
  10372. if (!Terminal->Active)
  10373. {
  10374. Manager->FreeTerminal(Terminal);
  10375. }
  10376. else
  10377. {
  10378. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10379. if (CanCloseQueue(Queue))
  10380. {
  10381. Manager->FreeTerminal(Terminal);
  10382. }
  10383. }
  10384. }
  10385. }
  10386. //---------------------------------------------------------------------------
  10387. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10388. {
  10389. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10390. if (Session == NULL)
  10391. {
  10392. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10393. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10394. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10395. //P = SessionsPageControl->ClientToScreen(P);
  10396. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10397. }
  10398. else
  10399. {
  10400. CloseSessionTab(Index);
  10401. }
  10402. }
  10403. //---------------------------------------------------------------------------
  10404. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10405. {
  10406. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10407. {
  10408. TInstantOperationVisualizer Visualizer;
  10409. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10410. // before we create a new data object
  10411. ClipboardClear();
  10412. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10413. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10414. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10415. // Intermediate variable to WORKAROUND a crash in compiler
  10416. TObjectList * ClipboardFakeMonitors =
  10417. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10418. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10419. if (FClipboardDragDropFilesEx.get() == NULL)
  10420. {
  10421. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10422. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10423. }
  10424. FClipboardDragDropFilesEx->FileList->Clear();
  10425. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10426. FClipboardDragDropFilesEx->CopyToClipboard();
  10427. FClipboardTerminal = Terminal;
  10428. // Need full paths, as cwd can be different once files are pasted
  10429. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10430. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10431. {
  10432. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10433. }
  10434. }
  10435. }
  10436. //---------------------------------------------------------------------------
  10437. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10438. {
  10439. if (OpenClipboard(0))
  10440. {
  10441. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10442. CloseClipboard();
  10443. }
  10444. }
  10445. //---------------------------------------------------------------------------
  10446. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10447. {
  10448. return !FClipboardFakeDirectory.IsEmpty();
  10449. }
  10450. //---------------------------------------------------------------------------
  10451. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10452. {
  10453. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10454. FClipboardFakeDirectory = UnicodeString();
  10455. FClipboardTerminal = NULL;
  10456. if (FDownloadingFromClipboard)
  10457. {
  10458. // We are called by the monitor, so attempt to release it would deadlock
  10459. FClipboardFakeMonitorsPendingReset = true;
  10460. }
  10461. else
  10462. {
  10463. FClipboardFakeMonitors.reset(NULL);
  10464. }
  10465. }
  10466. //---------------------------------------------------------------------------
  10467. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10468. {
  10469. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10470. {
  10471. ClipboardStop();
  10472. }
  10473. }
  10474. //---------------------------------------------------------------------------
  10475. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10476. {
  10477. TTerminalManager * Manager = TTerminalManager::Instance();
  10478. Manager->ActiveSession = FClipboardTerminal;
  10479. TTransferOperationParam Params;
  10480. Params.TargetDirectory = TargetDirectory;
  10481. Params.DragDrop = DragDrop;
  10482. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10483. }
  10484. //---------------------------------------------------------------------------
  10485. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10486. {
  10487. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10488. // (as it pumps a Windows message queue while being shutted down)
  10489. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10490. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10491. {
  10492. // Can fail as it can be e.g. locked by AV, so we retry that later
  10493. bool Removed = RemoveDir(ApiPath(FileName));
  10494. try
  10495. {
  10496. if (!NonVisualDataModule->Busy)
  10497. {
  10498. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10499. FClipboardFakeMonitorsPendingReset = false;
  10500. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10501. TAutoFlag Flag(FDownloadingFromClipboard);
  10502. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10503. }
  10504. }
  10505. __finally
  10506. {
  10507. if (!Removed)
  10508. {
  10509. RemoveDir(ApiPath(FileName));
  10510. }
  10511. }
  10512. }
  10513. }
  10514. //---------------------------------------------------------------------------
  10515. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10516. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10517. {
  10518. TFileMasks::TParams MaskParams;
  10519. MaskParams.Size = Size;
  10520. MaskParams.Modification = Modification;
  10521. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10522. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10523. // TUnixDirView::ItemFullFileName returns trailing backslash for directories, what filemask cannot deal with
  10524. if (!Local && Directory)
  10525. {
  10526. FileName = UnixExcludeTrailingBackslash(FileName);
  10527. }
  10528. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10529. {
  10530. bool ImplicitMatch;
  10531. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10532. !ImplicitMatch)
  10533. {
  10534. Color = Iter->Color;
  10535. break;
  10536. }
  10537. }
  10538. }
  10539. //---------------------------------------------------------------------------
  10540. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10541. {
  10542. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10543. if (!DirView->IsEditing())
  10544. {
  10545. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10546. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10547. // while searching
  10548. if (Key == VK_BACK)
  10549. {
  10550. if (!FIncrementalSearch.IsEmpty())
  10551. {
  10552. if (FIncrementalSearch.Length() == 1)
  10553. {
  10554. ResetIncrementalSearch();
  10555. }
  10556. else
  10557. {
  10558. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10559. IncrementalSearch(NewText, false, false);
  10560. }
  10561. Key = 0;
  10562. }
  10563. }
  10564. }
  10565. }
  10566. //---------------------------------------------------------------------------
  10567. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10568. {
  10569. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10570. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10571. {
  10572. // Filter control sequences.
  10573. // When not searching yet, prefer use of the space for toggling file selection
  10574. // (so we cannot incrementally search for a string starting with a space).
  10575. if ((Key > VK_SPACE) ||
  10576. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10577. {
  10578. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10579. Key = 0;
  10580. }
  10581. }
  10582. }
  10583. //---------------------------------------------------------------------------
  10584. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10585. {
  10586. if (!FIncrementalSearch.IsEmpty())
  10587. {
  10588. FIncrementalSearch = L"";
  10589. DirView(osCurrent)->UpdateStatusBar();
  10590. }
  10591. }
  10592. //---------------------------------------------------------------------------
  10593. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10594. {
  10595. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10596. if (Item == NULL)
  10597. {
  10598. MessageBeep(MB_ICONHAND);
  10599. }
  10600. else
  10601. {
  10602. TCustomDirView * ADirView = DirView(osCurrent);
  10603. {
  10604. TAutoNestingCounter Guard(FIncrementalSearching);
  10605. ADirView->FocusItem(Item);
  10606. if (ADirView->NortonLike == nlOff)
  10607. {
  10608. if ((ADirView->Selected != Item) ||
  10609. (ADirView->SelCount > 1))
  10610. {
  10611. ADirView->ClearSelection();
  10612. ADirView->Selected = Item;
  10613. }
  10614. }
  10615. }
  10616. FIncrementalSearch = Text;
  10617. Item->MakeVisible(false);
  10618. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10619. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10620. ADirView->UpdateStatusBar();
  10621. }
  10622. }
  10623. //---------------------------------------------------------------------------
  10624. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10625. {
  10626. int Index = Item->Index;
  10627. if (!Reverse)
  10628. {
  10629. Index++;
  10630. if (Index >= Item->Owner->Count)
  10631. {
  10632. Index = 0;
  10633. }
  10634. }
  10635. else
  10636. {
  10637. Index--;
  10638. if (Index < 0)
  10639. {
  10640. Index = Item->Owner->Count - 1;
  10641. }
  10642. }
  10643. return Item->Owner->Item[Index];
  10644. }
  10645. //---------------------------------------------------------------------------
  10646. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10647. {
  10648. TCustomDirView * ADirView = DirView(osCurrent);
  10649. if (ADirView->Items->Count == 0)
  10650. {
  10651. return NULL;
  10652. }
  10653. else
  10654. {
  10655. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10656. TListItem * Item = CurrentItem;
  10657. if (SkipCurrent)
  10658. {
  10659. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10660. }
  10661. while (true)
  10662. {
  10663. bool Matches = false;
  10664. switch (WinConfiguration->PanelSearch)
  10665. {
  10666. case isNameStartOnly:
  10667. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item).SubString(1, Text.Length()), Text);
  10668. break;
  10669. case isName:
  10670. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item), Text);
  10671. break;
  10672. case isAll:
  10673. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10674. int Index = 0;
  10675. while ((Index < ColCount) && !Matches)
  10676. {
  10677. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10678. if (Visible)
  10679. {
  10680. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10681. }
  10682. Index++;
  10683. }
  10684. break;
  10685. }
  10686. if (Matches)
  10687. {
  10688. return Item;
  10689. }
  10690. Item = GetNextFile(Item, Reverse);
  10691. if (Item == CurrentItem)
  10692. {
  10693. return NULL;
  10694. }
  10695. }
  10696. }
  10697. }
  10698. //---------------------------------------------------------------------------
  10699. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10700. {
  10701. ResetIncrementalSearch();
  10702. }
  10703. //---------------------------------------------------------------------------
  10704. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10705. {
  10706. if (Message.CharCode == VK_TAB)
  10707. {
  10708. if (!FIncrementalSearch.IsEmpty())
  10709. {
  10710. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10711. bool Reverse = Shift.Contains(ssShift);
  10712. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10713. Message.Result = 1;
  10714. return;
  10715. }
  10716. }
  10717. else if (Message.CharCode == VK_ESCAPE)
  10718. {
  10719. if (!FIncrementalSearch.IsEmpty())
  10720. {
  10721. ResetIncrementalSearch();
  10722. Message.Result = 1;
  10723. return;
  10724. }
  10725. }
  10726. TForm::Dispatch(&Message);
  10727. }
  10728. //---------------------------------------------------------------------------
  10729. void __fastcall TCustomScpExplorerForm::Deactivate()
  10730. {
  10731. TForm::Deactivate();
  10732. // E.g. when switching to an internal editor window
  10733. ResetIncrementalSearch();
  10734. }
  10735. //---------------------------------------------------------------------------
  10736. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10737. {
  10738. // When switching to another application
  10739. ResetIncrementalSearch();
  10740. }
  10741. //---------------------------------------------------------------------------
  10742. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10743. {
  10744. ResetIncrementalSearch();
  10745. }
  10746. //---------------------------------------------------------------------------
  10747. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10748. {
  10749. if (FIncrementalSearching <= 0)
  10750. {
  10751. ResetIncrementalSearch();
  10752. }
  10753. }
  10754. //---------------------------------------------------------------------------
  10755. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10756. {
  10757. CountClicksForWindowPrint(this);
  10758. }
  10759. //---------------------------------------------------------------------------
  10760. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10761. {
  10762. CenterReconnectToolbar();
  10763. }
  10764. //---------------------------------------------------------------------------
  10765. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10766. {
  10767. if (RemoteDirView->ItemFocused != NULL)
  10768. {
  10769. RemoteDirView->ItemFocused->Selected = true;
  10770. }
  10771. }
  10772. //---------------------------------------------------------------------------
  10773. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10774. {
  10775. if (!FQueueStatusUpdating)
  10776. {
  10777. bool Refresh;
  10778. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10779. {
  10780. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10781. }
  10782. else
  10783. {
  10784. if (FQueueFileList->GetCount() > 0)
  10785. {
  10786. FQueueFileList->Clear();
  10787. Refresh = true;
  10788. }
  10789. }
  10790. int Count = FQueueFileList->GetCount();
  10791. if (QueueFileList->Items->Count != Count)
  10792. {
  10793. DebugAssert(Refresh);
  10794. QueueFileList->Items->Count = Count;
  10795. }
  10796. if (Refresh)
  10797. {
  10798. QueueFileList->Invalidate();
  10799. }
  10800. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10801. }
  10802. }
  10803. //---------------------------------------------------------------------------
  10804. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10805. {
  10806. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10807. }
  10808. //---------------------------------------------------------------------------
  10809. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10810. {
  10811. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10812. // See TSynchronizeChecklistDialog::UpdateTimer
  10813. if (QueueFileList->Items->Count > 0)
  10814. {
  10815. QueueFileList->Items->Item[0]->MakeVisible(false);
  10816. }
  10817. UpdateQueueFileList();
  10818. }
  10819. //---------------------------------------------------------------------------
  10820. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10821. {
  10822. Active = QueueView3->Focused() || QueueFileList->Focused();
  10823. }
  10824. //---------------------------------------------------------------------------
  10825. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10826. {
  10827. QueueLabelUpdateStatus();
  10828. }
  10829. //---------------------------------------------------------------------------
  10830. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10831. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10832. {
  10833. int State = FQueueFileList->GetState(Item->Index);
  10834. if (State == qfsProcessed)
  10835. {
  10836. Sender->Canvas->Font->Color = clGrayText;
  10837. }
  10838. }
  10839. //---------------------------------------------------------------------------
  10840. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10841. {
  10842. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10843. QueueFileList->Column[0]->Width =
  10844. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10845. }
  10846. //---------------------------------------------------------------------------
  10847. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10848. {
  10849. QueueFileListColumnAutoSize();
  10850. }
  10851. //---------------------------------------------------------------------------
  10852. void __fastcall TCustomScpExplorerForm::CloseApp()
  10853. {
  10854. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10855. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10856. // So we clear the flag here.
  10857. DebugAssert(NonVisualDataModule->Busy);
  10858. NonVisualDataModule->EndBusy();
  10859. try
  10860. {
  10861. Close();
  10862. }
  10863. __finally
  10864. {
  10865. NonVisualDataModule->StartBusy();
  10866. }
  10867. }
  10868. //---------------------------------------------------------------------------
  10869. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10870. {
  10871. return (Terminal != NULL) && Terminal->Active;
  10872. }
  10873. //---------------------------------------------------------------------------
  10874. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  10875. {
  10876. return (ManagedSession != NULL);
  10877. }
  10878. //---------------------------------------------------------------------------
  10879. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10880. {
  10881. return IsActiveTerminal(Terminal);
  10882. }
  10883. //---------------------------------------------------------------------------
  10884. void TCustomScpExplorerForm::LocalLocalCopy(
  10885. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  10886. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  10887. {
  10888. DebugFail();
  10889. }
  10890. //---------------------------------------------------------------------------
  10891. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  10892. {
  10893. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  10894. }
  10895. //---------------------------------------------------------------------------
  10896. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  10897. {
  10898. // Changing tab list triggers the OnResize.
  10899. if (!FUpdatingSessionTabs)
  10900. {
  10901. // UpdateTabsCaptionTruncation should be enough
  10902. SessionListChanged(true);
  10903. }
  10904. }
  10905. //---------------------------------------------------------------------------
  10906. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  10907. {
  10908. if (ASession == ManagedSession)
  10909. {
  10910. UpdateSession(ASession);
  10911. }
  10912. TSessionData * Data = ASession->StateData;
  10913. UnicodeString Result;
  10914. if (Side == osLocal)
  10915. {
  10916. Result = Data->LocalDirectory;
  10917. }
  10918. else if (DebugAlwaysTrue(Side == osOther))
  10919. {
  10920. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  10921. }
  10922. return Result;
  10923. }
  10924. //---------------------------------------------------------------------------
  10925. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  10926. {
  10927. UnicodeString Result;
  10928. if (!ASession->Active)
  10929. {
  10930. Result = LoadStr(STATUS_NOT_CONNECTED2);
  10931. }
  10932. else
  10933. {
  10934. Result = GetSessionPath(ASession, osRemote);
  10935. }
  10936. return Result;
  10937. }
  10938. //---------------------------------------------------------------------------
  10939. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  10940. {
  10941. return LoadStr(NEW_REMOTE_TAB_HINT);
  10942. }
  10943. //---------------------------------------------------------------------------
  10944. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  10945. {
  10946. if (Index >= 0)
  10947. {
  10948. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10949. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  10950. if (TabSession == NULL)
  10951. {
  10952. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  10953. if (NewTabTabCaption != TabSheet->Caption)
  10954. {
  10955. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  10956. }
  10957. }
  10958. else
  10959. {
  10960. TTerminalManager * Manager = TTerminalManager::Instance();
  10961. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  10962. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  10963. }
  10964. }
  10965. }
  10966. //---------------------------------------------------------------------------
  10967. typedef std::vector<TListItem *> TListItemsVector;
  10968. struct TCalculateSizeOperation
  10969. {
  10970. TCalculateSizeStats Stats;
  10971. TListItemsVector ListItems;
  10972. size_t Index;
  10973. };
  10974. //---------------------------------------------------------------------------
  10975. void TCustomScpExplorerForm::CalculateDirectorySizes()
  10976. {
  10977. TCalculateSizeOperation CalculateSizeOperation;
  10978. TOperationSide Side = GetSide(osCurrent);
  10979. TCustomDirView * ADirView = DirView(Side);
  10980. bool FullPath = IsSideLocalBrowser(Side);
  10981. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, FullPath, NULL, true));
  10982. std::unique_ptr<TStrings> FileList(new TStringList());
  10983. for (int Index = 0; Index < AllFileList->Count; Index++)
  10984. {
  10985. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  10986. if (ADirView->ItemIsDirectory(Item))
  10987. {
  10988. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  10989. CalculateSizeOperation.ListItems.push_back(Item);
  10990. }
  10991. }
  10992. CalculateSizeOperation.Index = 0;
  10993. __int64 Size = 0;
  10994. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation);
  10995. FCalculateSizeOperation = &CalculateSizeOperation;
  10996. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  10997. FOnFileOperationFinished = DirectorySizeCalculated;
  10998. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation);
  10999. FPrimaryOperation = foCalculateSize;
  11000. TCalculatedSizes CalculatedSizes;
  11001. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  11002. if (Side == osLocal)
  11003. {
  11004. Terminal->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  11005. }
  11006. else
  11007. {
  11008. TCalculateSizeParams Params;
  11009. Params.Stats = &CalculateSizeOperation.Stats;
  11010. Params.UseCache = true;
  11011. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  11012. }
  11013. }
  11014. //---------------------------------------------------------------------------
  11015. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(TOperationSide Side, const UnicodeString & FileName, bool Success)
  11016. {
  11017. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  11018. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  11019. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  11020. // optimization
  11021. bool GotExpectedItem =
  11022. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  11023. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  11024. int Index = -1;
  11025. if (DebugAlwaysTrue(GotExpectedItem))
  11026. {
  11027. Index = FCalculateSizeOperation->Index;
  11028. FCalculateSizeOperation->Index++;
  11029. }
  11030. else
  11031. {
  11032. TListItemsVector::iterator I =
  11033. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  11034. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  11035. {
  11036. Index = I - FCalculateSizeOperation->ListItems.begin();
  11037. }
  11038. }
  11039. if (DebugAlwaysTrue(Index >= 0) && Success)
  11040. {
  11041. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  11042. ADirView->SetItemCalculatedSize(Item, Size);
  11043. }
  11044. }
  11045. //---------------------------------------------------------------------------
  11046. void __fastcall TCustomScpExplorerForm::MessageDockRequestDock(TObject *, TTBCustomDockableWindow *, bool & Accept)
  11047. {
  11048. Accept = false;
  11049. }
  11050. //---------------------------------------------------------------------------