CustomScpExplorer.cpp 389 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737
  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. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  854. {
  855. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  856. {
  857. TFileOperationProgressType * ProgressData = NULL;
  858. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  859. {
  860. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  861. }
  862. if ((ProgressData != NULL) &&
  863. ProgressData->InProgress)
  864. {
  865. SetTaskbarListProgressValue(ProgressData);
  866. }
  867. else
  868. {
  869. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  870. }
  871. }
  872. else
  873. {
  874. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  875. }
  876. }
  877. else
  878. {
  879. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  880. }
  881. }
  882. UpdateQueueLabel();
  883. }
  884. //---------------------------------------------------------------------------
  885. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  886. {
  887. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  888. if (FQueueStatus != NULL)
  889. {
  890. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  891. if (ActiveAndPendingPrimaryCount > 0)
  892. {
  893. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  894. }
  895. }
  896. QueueLabel->Caption = Caption;
  897. }
  898. //---------------------------------------------------------------------------
  899. void TCustomScpExplorerForm::CheckStoreTransition()
  900. {
  901. if (WinConfiguration->StoreTransition == stInit)
  902. {
  903. if (IsUWP())
  904. {
  905. WinConfiguration->StoreTransition = stStoreFresh;
  906. AppLog(L"Fresh UWP/store installation");
  907. }
  908. else
  909. {
  910. WinConfiguration->StoreTransition = stStandard;
  911. AppLog(L"Fresh standard installation");
  912. }
  913. }
  914. else if ((WinConfiguration->StoreTransition == stStandard) && IsUWP())
  915. {
  916. WinConfiguration->StoreTransition = stStoreMigrated;
  917. WinConfiguration->FirstRun = WinConfiguration->FirstRun + L"=>" + StandardDatestamp();
  918. AppLog(L"Standard installation migrated to store installation");
  919. }
  920. if (WinConfiguration->StoreTransition == stStoreMigrated)
  921. {
  922. TTBXToolbar * Toolbar = new TTBXToolbar(MessageDock);
  923. Toolbar->Parent = MessageDock;
  924. Toolbar->FullSize = true;
  925. Toolbar->Color = Application->HintColor;
  926. TTBXLabelItem * LabelItem;
  927. LabelItem = new TTBXLabelItem(Toolbar);
  928. LabelItem->Margin = ScaleByTextHeight(MessageDock, 2); // Is this automatically rescaled?
  929. LabelItem->Caption = LoadStr(STORE_MIGRATION_THANKS);
  930. Toolbar->Items->Add(LabelItem);
  931. TTBXCustomItem * LinkItem = new TTBXItem(Toolbar);
  932. LinkItem->FontSettings->Color = LinkColor;
  933. LinkItem->Caption = LoadStr(STORE_MIGRATION_LEARN);
  934. LinkItem->OnClick = StoreTransitionLinkClick;
  935. Toolbar->Items->Add(LinkItem);
  936. AddMenuSeparator(Toolbar->Items);
  937. TTBCustomItem * CloseItem = new TTBXItem(Toolbar);
  938. CloseItem->Caption = LoadStr(MESSAGE_DISMISS);
  939. CloseItem->OnClick = StoreTransitionCloseClick;
  940. Toolbar->Items->Add(CloseItem);
  941. GiveTBItemPriority(CloseItem);
  942. }
  943. }
  944. //---------------------------------------------------------------------------
  945. void __fastcall TCustomScpExplorerForm::StoreTransitionLinkClick(TObject *)
  946. {
  947. ShowHelp(HELP_STORE_TRANSITION);
  948. }
  949. //---------------------------------------------------------------------------
  950. void __fastcall TCustomScpExplorerForm::StoreTransitionCloseClick(TObject * Sender)
  951. {
  952. TTBCustomItem * Item = DebugNotNull(dynamic_cast<TTBCustomItem *>(Sender));
  953. TTBXToolbar * Toolbar = DebugNotNull(dynamic_cast<TTBXToolbar *>(DebugNotNull(DebugNotNull(Item->Parent)->ParentComponent)));
  954. delete Toolbar;
  955. WinConfiguration->StoreTransition = stStoreAcknowledged;
  956. UpdateControls();
  957. }
  958. //---------------------------------------------------------------------------
  959. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  960. {
  961. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  962. static_cast<TComponent*>(GetComponent(fcTransferList)));
  963. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  964. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  965. TransferList->Strings->BeginUpdate();
  966. try
  967. {
  968. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  969. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  970. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  971. if (FTransferDropDownHint.IsEmpty())
  972. {
  973. FTransferDropDownHint = TransferDropDown->Hint;
  974. }
  975. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  976. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  977. TransferDropDown->Text = StripHotkey(Name);
  978. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  979. (FTransferDropDownHint, StripHotkey(Name),
  980. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  981. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  982. // update the label, otherwise when it is updated only on the first draw
  983. // of the list, it is drawn "bold" for some reason
  984. FTransferListHoverIndex = TransferList->ItemIndex;
  985. UpdateTransferLabel();
  986. }
  987. __finally
  988. {
  989. TransferList->Strings->EndUpdate();
  990. }
  991. }
  992. //---------------------------------------------------------------------------
  993. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  994. {
  995. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  996. DebugAssert(Toolbar != NULL);
  997. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  998. }
  999. //---------------------------------------------------------------------------
  1000. void __fastcall TCustomScpExplorerForm::UpdateActions()
  1001. {
  1002. TForm::UpdateActions();
  1003. if (ComponentVisible[fcCustomCommandsBand])
  1004. {
  1005. UpdateCustomCommandsToolbar();
  1006. }
  1007. }
  1008. //---------------------------------------------------------------------------
  1009. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  1010. {
  1011. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  1012. }
  1013. //---------------------------------------------------------------------------
  1014. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  1015. {
  1016. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  1017. {
  1018. DirView->RowSelect = WinConfiguration->FullRowSelect;
  1019. // Without this, the panel turns gray and unused part of header turns black
  1020. DirView->Perform(CM_COLORCHANGED, 0, 0);
  1021. }
  1022. }
  1023. //---------------------------------------------------------------------------
  1024. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  1025. {
  1026. Color = GetBtnFaceColor();
  1027. DebugAssert(Configuration && RemoteDirView);
  1028. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1029. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  1030. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  1031. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  1032. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1033. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1034. UpdateRowSelect(RemoteDirView);
  1035. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1036. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  1037. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  1038. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1039. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1040. UpdateSessionsPageControlHeight();
  1041. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1042. UpdateToolbarDisplayMode();
  1043. UpdateTransferList();
  1044. if (Terminal != NULL)
  1045. {
  1046. TransferPresetAutoSelect();
  1047. }
  1048. UpdateQueueView();
  1049. UpdateControls();
  1050. // this can be called even before constuctor finishes.
  1051. if (FEditorManager != NULL)
  1052. {
  1053. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  1054. }
  1055. // this can be called even before constuctor finishes.
  1056. if (FLocalEditors != NULL)
  1057. {
  1058. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  1059. {
  1060. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  1061. }
  1062. }
  1063. if (ComponentVisible[fcCustomCommandsBand])
  1064. {
  1065. UpdateCustomCommandsToolbar();
  1066. }
  1067. // show only when keeping queue items forever,
  1068. // otherwise, it's enough to have it in the context menu
  1069. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1070. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1071. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1072. {
  1073. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1074. FFileColorsCurrent = WinConfiguration->FileColors;
  1075. FileColorsChanged();
  1076. }
  1077. }
  1078. //---------------------------------------------------------------------------
  1079. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1080. {
  1081. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1082. DirView->Invalidate();
  1083. }
  1084. //---------------------------------------------------------------------------
  1085. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1086. {
  1087. DoFileColorsChanged(RemoteDirView);
  1088. }
  1089. //---------------------------------------------------------------------------
  1090. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1091. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1092. void * /*Arg*/)
  1093. {
  1094. if (Token != NULL)
  1095. {
  1096. TForm * Editor = dynamic_cast<TForm*>(Token);
  1097. DebugAssert(Editor != NULL);
  1098. ReconfigureEditorForm(Editor);
  1099. }
  1100. }
  1101. //---------------------------------------------------------------------------
  1102. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1103. {
  1104. WinConfiguration->DDTransferConfirmation = asOn;
  1105. }
  1106. //---------------------------------------------------------------------------
  1107. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1108. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1109. {
  1110. // noop
  1111. }
  1112. //---------------------------------------------------------------------------
  1113. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1114. {
  1115. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1116. }
  1117. //---------------------------------------------------------------------------
  1118. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1119. {
  1120. if (DoNotShowAgain)
  1121. {
  1122. if (DragDrop)
  1123. {
  1124. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1125. {
  1126. PopupTrayBalloon(
  1127. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1128. }
  1129. WinConfiguration->DDTransferConfirmation = asOff;
  1130. }
  1131. else
  1132. {
  1133. WinConfiguration->ConfirmTransferring = false;
  1134. }
  1135. }
  1136. else
  1137. {
  1138. // User explicitly unchecked "do not show again",
  1139. // so show him the dialog the next time
  1140. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1141. {
  1142. WinConfiguration->DDTransferConfirmation = asOn;
  1143. }
  1144. }
  1145. }
  1146. //---------------------------------------------------------------------------
  1147. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1148. TTransferDirection Direction, TTransferType Type, bool Temp,
  1149. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1150. bool Confirm, bool DragDrop, int Options)
  1151. {
  1152. bool Result = true;
  1153. DebugAssert(HasActiveTerminal());
  1154. // these parameters are known in advance
  1155. int Params =
  1156. FLAGMASK(Type == ttMove, cpDelete);
  1157. bool ToTemp = (Temp && (Direction == tdToLocal));
  1158. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1159. {
  1160. bool DisableNewerOnly =
  1161. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1162. ToTemp;
  1163. Options |=
  1164. FLAGMASK(ToTemp, coTemp) |
  1165. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1166. coDoNotShowAgain;
  1167. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1168. int CopyParamAttrs = (Direction == tdToRemote ?
  1169. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1170. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1171. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1172. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1173. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1174. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1175. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1176. if (Result)
  1177. {
  1178. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1179. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1180. }
  1181. }
  1182. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1183. {
  1184. Configuration->Usage->Inc(L"TransfersOnBackground");
  1185. // these parameter are known only after transfer dialog
  1186. Params |=
  1187. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1188. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1189. Result = false;
  1190. ClearTransferSourceSelection(Direction);
  1191. }
  1192. return Result;
  1193. }
  1194. //---------------------------------------------------------------------------
  1195. void TCustomScpExplorerForm::ClearOperationSelection(TOperationSide Side)
  1196. {
  1197. if (FOnFileOperationFinished != NULL)
  1198. {
  1199. FOnFileOperationFinished(Side, UnicodeString(), true);
  1200. }
  1201. else
  1202. {
  1203. if (HasDirView[Side])
  1204. {
  1205. DirView(Side)->SelectAll(smNone);
  1206. }
  1207. }
  1208. }
  1209. //---------------------------------------------------------------------------
  1210. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1211. {
  1212. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1213. ClearOperationSelection(Side);
  1214. }
  1215. //---------------------------------------------------------------------------
  1216. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1217. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1218. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1219. int Params)
  1220. {
  1221. DebugAssert(Queue != NULL);
  1222. bool SingleFile = false;
  1223. if (FileList->Count == 1)
  1224. {
  1225. if (Direction == tdToRemote)
  1226. {
  1227. UnicodeString FileName = FileList->Strings[0];
  1228. SingleFile = FileExists(ApiPath(FileName));
  1229. }
  1230. else
  1231. {
  1232. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1233. SingleFile = !File->IsDirectory;
  1234. }
  1235. }
  1236. TQueueItem * QueueItem;
  1237. if (Direction == tdToRemote)
  1238. {
  1239. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1240. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1241. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1242. }
  1243. else
  1244. {
  1245. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1246. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1247. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1248. }
  1249. AddQueueItem(Queue, QueueItem, Terminal);
  1250. }
  1251. //---------------------------------------------------------------------------
  1252. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1253. {
  1254. if (Queue->IsEmpty)
  1255. {
  1256. Terminal->QueueOperationStart = Now();
  1257. }
  1258. Queue->AddItem(QueueItem);
  1259. }
  1260. //---------------------------------------------------------------------------
  1261. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1262. {
  1263. }
  1264. //---------------------------------------------------------------------------
  1265. void __fastcall TCustomScpExplorerForm::InitControls()
  1266. {
  1267. DebugAssert(FDocks != NULL);
  1268. for (int Index = 0; Index < ComponentCount; Index++)
  1269. {
  1270. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1271. if ((Dock != NULL) && (Dock->Tag == 0))
  1272. {
  1273. FDocks->Add(Dock);
  1274. }
  1275. }
  1276. CollectItemsWithTextDisplayMode(this);
  1277. }
  1278. //---------------------------------------------------------------------------
  1279. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1280. {
  1281. ConfigurationChanged();
  1282. QueuePanel->Height =
  1283. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1284. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1285. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1286. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1287. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1288. QueueFileList->Height =
  1289. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1290. if (QueueFileList->Visible)
  1291. {
  1292. AdjustQueueLayout();
  1293. }
  1294. UpdateCustomCommandsToolbar();
  1295. }
  1296. //---------------------------------------------------------------------------
  1297. void __fastcall TCustomScpExplorerForm::StoreParams()
  1298. {
  1299. int APixelsPerInch = GetControlPixelsPerInch(this);
  1300. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1301. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1302. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1303. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1304. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1305. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1306. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1307. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1308. }
  1309. //---------------------------------------------------------------------------
  1310. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1311. {
  1312. if (!FFormRestored)
  1313. {
  1314. FFormRestored = true;
  1315. RestoreFormParams();
  1316. }
  1317. TForm::CreateParams(Params);
  1318. }
  1319. //---------------------------------------------------------------------------
  1320. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1321. {
  1322. DebugAssert(FDocks != NULL);
  1323. for (int Index = 0; Index < FDocks->Count; Index++)
  1324. {
  1325. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1326. }
  1327. }
  1328. //---------------------------------------------------------------------------
  1329. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1330. {
  1331. SetDockAllowDrag(true);
  1332. while (!ButtonsStr.IsEmpty())
  1333. {
  1334. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1335. UnicodeString Name = CutToChar(S, L'=', true);
  1336. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1337. {
  1338. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1339. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1340. {
  1341. while (!S.IsEmpty())
  1342. {
  1343. UnicodeString S2 = CutToChar(S, L',', true);
  1344. UnicodeString Name = CutToChar(S2, L':', true);
  1345. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1346. {
  1347. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1348. if (SameText(GetToolbarItemName(Item), Name))
  1349. {
  1350. // forward compatibility
  1351. UnicodeString S3 = CutToChar(S2, L':', true);
  1352. bool Visible = StrToIntDef(S3, 0);
  1353. Item->Visible = Visible;
  1354. }
  1355. }
  1356. }
  1357. }
  1358. }
  1359. }
  1360. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1361. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1362. }
  1363. //---------------------------------------------------------------------------
  1364. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1365. {
  1366. UnicodeString Result;
  1367. SetDockAllowDrag(true);
  1368. Result = ::GetToolbarsLayoutStr(this);
  1369. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1370. return Result;
  1371. }
  1372. //---------------------------------------------------------------------------
  1373. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1374. {
  1375. UnicodeString Result;
  1376. if (Item->Action != NULL)
  1377. {
  1378. Result = Item->Action->Name;
  1379. Result = RemoveSuffix(Result, L"Action", true);
  1380. }
  1381. else
  1382. {
  1383. Result = Item->Name;
  1384. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1385. Result = RemoveSuffix(Result, L"Item", true);
  1386. }
  1387. return Result;
  1388. }
  1389. //---------------------------------------------------------------------------
  1390. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1391. {
  1392. UnicodeString Result;
  1393. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1394. {
  1395. UnicodeString ToolbarStr;
  1396. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1397. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1398. {
  1399. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1400. {
  1401. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1402. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1403. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1404. // and save the changes here only.
  1405. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1406. {
  1407. UnicodeString Name = GetToolbarItemName(Item);
  1408. DebugAssert(Name.Pos(L"TBX") == 0);
  1409. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1410. }
  1411. }
  1412. }
  1413. if (!ToolbarStr.IsEmpty())
  1414. {
  1415. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1416. }
  1417. }
  1418. return Result;
  1419. }
  1420. //---------------------------------------------------------------------------
  1421. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1422. {
  1423. DebugAssert(FProgressForm == NULL);
  1424. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1425. bool AllowMoveToQueue = (FTransferResumeList != NULL) || (FDeletedFiles != NULL);
  1426. FProgressForm = new TProgressForm(Application, AllowMoveToQueue, AllowSkip, SynchronizeProgress);
  1427. FProgressForm->DeleteLocalToRecycleBin =
  1428. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1429. FProgressForm->DeleteRemoteToRecycleBin =
  1430. HasActiveTerminal() &&
  1431. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1432. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1433. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1434. LockWindow();
  1435. // Actually, do not know what hides the progress once the operation finishes
  1436. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1437. SetTaskbarListProgressState(TBPF_NORMAL);
  1438. }
  1439. //---------------------------------------------------------------------------
  1440. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1441. {
  1442. SAFE_DESTROY(FProgressForm);
  1443. UnlockWindow();
  1444. SetQueueProgress();
  1445. }
  1446. //---------------------------------------------------------------------------
  1447. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1448. TFileOperationProgressType & ProgressData)
  1449. {
  1450. // Download to temporary local directory
  1451. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1452. {
  1453. bool Continue = true;
  1454. FPendingTempSpaceWarn = false;
  1455. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1456. if (!Continue)
  1457. {
  1458. Abort();
  1459. }
  1460. }
  1461. // operation is being executed and we still didn't show up progress form
  1462. if (ProgressData.InProgress && !FProgressForm)
  1463. {
  1464. CreateProgressForm(NULL);
  1465. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1466. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1467. }
  1468. // operation is finished (or terminated), so we hide progress form
  1469. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1470. {
  1471. DestroyProgressForm();
  1472. if (ProgressData.Operation == foCalculateSize)
  1473. {
  1474. // When calculating size before transfer, the abort caused by
  1475. // cancel flag set due to "MoveToQueue" below,
  1476. // is not propagated back to us in ExecuteFileOperation,
  1477. // as it is silently swallowed.
  1478. // So we explicitly (re)throw it here.
  1479. if (FMoveToQueue)
  1480. {
  1481. Abort();
  1482. }
  1483. }
  1484. else
  1485. {
  1486. if ((ProgressData.Cancel == csContinue) &&
  1487. !FAutoOperation)
  1488. {
  1489. OperationComplete(ProgressData.StartTime);
  1490. }
  1491. }
  1492. }
  1493. if (FProgressForm &&
  1494. // Can happen only during synchronization, when ending one of the operations
  1495. ProgressData.InProgress)
  1496. {
  1497. FProgressForm->SetProgressData(ProgressData);
  1498. DebugAssert(ProgressData.InProgress);
  1499. SetTaskbarListProgressValue(&ProgressData);
  1500. if (FProgressForm->Cancel > csContinue)
  1501. {
  1502. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1503. if (FProgressForm->Cancel == csCancelFile)
  1504. {
  1505. FProgressForm->ClearCancel();
  1506. }
  1507. // cancel cancels even the move
  1508. FMoveToQueue = false;
  1509. }
  1510. else if (FProgressForm->MoveToQueue)
  1511. {
  1512. FMoveToQueue = true;
  1513. ProgressData.SetCancelAtLeast(csCancel);
  1514. }
  1515. if ((FTransferResumeList != NULL) &&
  1516. ProgressData.InProgress &&
  1517. ProgressData.IsTransfer() &&
  1518. !ProgressData.FullFileName.IsEmpty())
  1519. {
  1520. if ((FTransferResumeList->Count == 0) ||
  1521. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1522. {
  1523. // note that we do not recognize directories from files here
  1524. FTransferResumeList->Add(ProgressData.FullFileName);
  1525. }
  1526. }
  1527. }
  1528. }
  1529. //---------------------------------------------------------------------------
  1530. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1531. const TDateTime & StartTime)
  1532. {
  1533. if (GUIConfiguration->BeepOnFinish &&
  1534. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1535. {
  1536. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1537. DWORD Sound;
  1538. if (!ExtractFileExt(BeepSound).IsEmpty())
  1539. {
  1540. Sound = SND_FILENAME;
  1541. }
  1542. else
  1543. {
  1544. Sound = SND_ALIAS;
  1545. }
  1546. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1547. }
  1548. }
  1549. //---------------------------------------------------------------------------
  1550. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1551. TFileOperationProgressType & ProgressData)
  1552. {
  1553. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1554. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1555. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1556. FileOperationProgress(ProgressData);
  1557. }
  1558. //---------------------------------------------------------------------------
  1559. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1560. {
  1561. UnicodeString Result;
  1562. if (FProgressForm != NULL)
  1563. {
  1564. Result = FProgressForm->ProgressStr();
  1565. }
  1566. return Result;
  1567. }
  1568. //---------------------------------------------------------------------------
  1569. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1570. bool DragDrop)
  1571. {
  1572. return (!DragDrop && (DropSourceControl == NULL)) ||
  1573. (DropSourceControl == DirView(osOther));
  1574. }
  1575. //---------------------------------------------------------------------------
  1576. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1577. {
  1578. TCustomDirView * ADirView = DirView(Side);
  1579. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1580. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1581. // this can happen when local drive is unplugged in the middle of the operation
  1582. if (Item != NULL)
  1583. {
  1584. if (Unselect)
  1585. {
  1586. Item->Selected = false;
  1587. }
  1588. ADirView->MakeProgressVisible(Item);
  1589. }
  1590. return Item;
  1591. }
  1592. //---------------------------------------------------------------------------
  1593. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1594. TFileOperation Operation, TOperationSide Side,
  1595. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1596. TOnceDoneOperation & OnceDoneOperation)
  1597. {
  1598. if (!FAutoOperation)
  1599. {
  1600. // no selection on "/upload", form serves only as event handler
  1601. // (it is not displayed)
  1602. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1603. ((Operation == FPrimaryOperation) ||
  1604. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1605. {
  1606. TOperationSide DViewSide;
  1607. if ((FProgressSide != osCurrent) &&
  1608. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1609. {
  1610. // assume the operation is over the focused panel
  1611. DViewSide = FProgressSide;
  1612. }
  1613. else
  1614. {
  1615. DViewSide = Side;
  1616. }
  1617. if (FOnFileOperationFinished != NULL)
  1618. {
  1619. FOnFileOperationFinished(DViewSide, FileName, Success);
  1620. }
  1621. else
  1622. {
  1623. VisualiseOperationFinished(DViewSide, FileName, Success);
  1624. }
  1625. }
  1626. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1627. {
  1628. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1629. {
  1630. if (Side == osLocal)
  1631. {
  1632. Configuration->Usage->Inc(L"UploadedFiles");
  1633. }
  1634. else
  1635. {
  1636. Configuration->Usage->Inc(L"DownloadedFiles");
  1637. }
  1638. }
  1639. }
  1640. }
  1641. if (Success && (FSynchronizeController != NULL))
  1642. {
  1643. DebugAssert(!IsSideLocalBrowser(osRemote));
  1644. if (Operation == foCopy)
  1645. {
  1646. DebugAssert(Side == osLocal);
  1647. FSynchronizeController->LogOperation(soUpload, FileName);
  1648. }
  1649. else if (Operation == foDelete)
  1650. {
  1651. DebugAssert(Side == osRemote);
  1652. FSynchronizeController->LogOperation(soDelete, FileName);
  1653. }
  1654. }
  1655. if (FProgressForm)
  1656. {
  1657. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1658. }
  1659. }
  1660. //---------------------------------------------------------------------------
  1661. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1662. TFileOperation Operation, TOperationSide Side,
  1663. bool Temp, const UnicodeString & FileName, Boolean Success,
  1664. TOnceDoneOperation & OnceDoneOperation)
  1665. {
  1666. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1667. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1668. OnceDoneOperation);
  1669. }
  1670. //---------------------------------------------------------------------------
  1671. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1672. {
  1673. return false;
  1674. }
  1675. //---------------------------------------------------------------------------
  1676. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1677. {
  1678. return false;
  1679. }
  1680. //---------------------------------------------------------------------------
  1681. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1682. {
  1683. return false;
  1684. }
  1685. //---------------------------------------------------------------------------
  1686. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1687. {
  1688. return NULL;
  1689. }
  1690. //---------------------------------------------------------------------------
  1691. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1692. {
  1693. DebugAssert(GetSide(Side) == osRemote);
  1694. DebugUsedParam(Side);
  1695. return RemoteDirView;
  1696. }
  1697. //---------------------------------------------------------------------------
  1698. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1699. {
  1700. DebugAssert(GetSide(Side) == osRemote);
  1701. DebugUsedParam(Side);
  1702. return RemoteDriveView;
  1703. }
  1704. //---------------------------------------------------------------------------
  1705. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1706. {
  1707. DebugAssert(GetSide(Side) == osRemote);
  1708. DebugUsedParam(Side);
  1709. return HasActiveTerminal();
  1710. }
  1711. //---------------------------------------------------------------------------
  1712. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1713. TOperationSide Side, int FilesOnly)
  1714. {
  1715. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1716. }
  1717. //---------------------------------------------------------------------------
  1718. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1719. TOperationSide Side, int FilesOnly)
  1720. {
  1721. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1722. }
  1723. //---------------------------------------------------------------------------
  1724. struct THistoryItemData
  1725. {
  1726. short int Side;
  1727. short int Index;
  1728. };
  1729. //---------------------------------------------------------------------------
  1730. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1731. {
  1732. DirView(Side)->HistoryGo(Index);
  1733. }
  1734. //---------------------------------------------------------------------------
  1735. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1736. {
  1737. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1738. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1739. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1740. }
  1741. //---------------------------------------------------------------------------
  1742. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1743. bool Back)
  1744. {
  1745. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1746. {
  1747. TCustomDirView * DView = DirView(Side);
  1748. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1749. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1750. if (ICount > 10)
  1751. {
  1752. ICount = 10;
  1753. }
  1754. Menu->Items->Clear();
  1755. THistoryItemData Data;
  1756. Data.Side = Side;
  1757. for (short int i = 1; i <= ICount; i++)
  1758. {
  1759. TTBCustomItem * Item = new TTBXItem(Menu);
  1760. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1761. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1762. Item->Hint = DView->HistoryPath[Data.Index];
  1763. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1764. Item->Tag = *reinterpret_cast<int*>(&Data);
  1765. Item->OnClick = HistoryItemClick;
  1766. Menu->Items->Add(Item);
  1767. }
  1768. }
  1769. }
  1770. //---------------------------------------------------------------------------
  1771. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1772. TOperationSide Side, bool Back)
  1773. {
  1774. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1775. {
  1776. // workaround
  1777. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1778. // operator new allocates memory in Pascal code, but calls inline
  1779. // contructor in C++, leading in problems, the function does
  1780. // both in Pascal code
  1781. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1782. UpdateHistoryMenu(Side, Back);
  1783. }
  1784. return FHistoryMenu[Side == osLocal][Back];
  1785. }
  1786. //---------------------------------------------------------------------------
  1787. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1788. TCustomDirView *Sender)
  1789. {
  1790. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1791. UpdateHistoryMenu(Side, true);
  1792. UpdateHistoryMenu(Side, false);
  1793. }
  1794. //---------------------------------------------------------------------------
  1795. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1796. {
  1797. // remote custom commands can be executed only if the server supports shell commands
  1798. // or have secondary shell
  1799. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1800. }
  1801. //---------------------------------------------------------------------------
  1802. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1803. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1804. {
  1805. // -1 = hidden, 0 = disabled, 1 = enabled
  1806. int Result;
  1807. TFileCustomCommand RemoteCustomCommand;
  1808. TLocalCustomCommand LocalCustomCommand;
  1809. TFileCustomCommand * NonInteractiveCustomCommand =
  1810. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1811. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1812. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1813. if (FLAGCLEAR(Command.Params, ccLocal))
  1814. {
  1815. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1816. // custom command that does not operate with files can be executed anytime ...
  1817. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1818. {
  1819. // ... but do not show such command in remote file menu
  1820. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1821. {
  1822. Result = AllowedState;
  1823. }
  1824. else
  1825. {
  1826. Result = -1;
  1827. }
  1828. }
  1829. else
  1830. {
  1831. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1832. {
  1833. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1834. }
  1835. else
  1836. {
  1837. Result = -1;
  1838. }
  1839. }
  1840. }
  1841. else
  1842. {
  1843. // custom command that does not operate with files can be executed anytime
  1844. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1845. {
  1846. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1847. }
  1848. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1849. {
  1850. if ((ListType == ccltAll) || (ListType == ccltFile))
  1851. {
  1852. // special case is "diff"-style command that can be executed over any side,
  1853. // if we have both sides
  1854. Result =
  1855. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1856. // directly (instead of EnableSelectedOperation) to pass
  1857. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1858. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1859. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1860. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1861. }
  1862. else if (ListType == ccltBoth)
  1863. {
  1864. DebugAssert(!IsLocalBrowserMode());
  1865. Result = 1;
  1866. }
  1867. else
  1868. {
  1869. Result = -1;
  1870. }
  1871. }
  1872. else
  1873. {
  1874. if ((ListType == ccltAll) ||
  1875. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1876. {
  1877. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1878. }
  1879. else
  1880. {
  1881. Result = -1;
  1882. }
  1883. }
  1884. if ((Result > 0) &&
  1885. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1886. !HasActiveTerminal())
  1887. {
  1888. Result = 0;
  1889. }
  1890. }
  1891. return Result;
  1892. }
  1893. //---------------------------------------------------------------------------
  1894. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1895. TStrings * FileList, const TCustomCommandType & ACommand,
  1896. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1897. {
  1898. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1899. {
  1900. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1901. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1902. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1903. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1904. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1905. bool Capture =
  1906. FLAGSET(ACommand.Params, ccShowResults) ||
  1907. FLAGSET(ACommand.Params, ccCopyResults) ||
  1908. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1909. TCaptureOutputEvent OutputEvent = NULL;
  1910. DebugAssert(FCapturedLog == NULL);
  1911. if (Capture)
  1912. {
  1913. FCapturedLog = new TStringList();
  1914. OutputEvent = TerminalCaptureLog;
  1915. }
  1916. try
  1917. {
  1918. if (!RemoteCustomCommand.IsFileCommand(Command))
  1919. {
  1920. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1921. OutputEvent);
  1922. }
  1923. else
  1924. {
  1925. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1926. }
  1927. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1928. {
  1929. if (FLAGSET(ACommand.Params, ccCopyResults))
  1930. {
  1931. CopyToClipboard(FCapturedLog);
  1932. }
  1933. if (FLAGSET(ACommand.Params, ccShowResults))
  1934. {
  1935. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1936. }
  1937. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1938. {
  1939. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1940. }
  1941. }
  1942. }
  1943. __finally
  1944. {
  1945. SAFE_DESTROY(FCapturedLog);
  1946. }
  1947. }
  1948. }
  1949. //---------------------------------------------------------------------------
  1950. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1951. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1952. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1953. {
  1954. DebugAssert(!IsLocalBrowserMode());
  1955. TStrings * LocalFileList = NULL;
  1956. TStrings * RemoteFileList = NULL;
  1957. TStrings * RemoteFileListFull = NULL;
  1958. try
  1959. {
  1960. if (LocalFileCommand)
  1961. {
  1962. if (ALocalFileList == NULL)
  1963. {
  1964. DebugAssert(HasDirView[osLocal]);
  1965. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1966. // directly (instead of EnableSelectedOperation) to pass
  1967. // false to FocusedFileOnlyWhenFocused
  1968. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1969. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1970. }
  1971. else
  1972. {
  1973. LocalFileList = ALocalFileList;
  1974. }
  1975. if (FileListCommand)
  1976. {
  1977. if (LocalFileList->Count != 1)
  1978. {
  1979. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1980. }
  1981. }
  1982. else
  1983. {
  1984. if ((LocalFileList->Count != 1) &&
  1985. (FileList->Count != 1) &&
  1986. (LocalFileList->Count != FileList->Count))
  1987. {
  1988. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1989. }
  1990. }
  1991. }
  1992. UnicodeString RootTempDir;
  1993. UnicodeString TempDir;
  1994. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1995. if (!RemoteFiles)
  1996. {
  1997. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true);
  1998. }
  1999. try
  2000. {
  2001. TDateTimes RemoteFileTimes;
  2002. if (RemoteFiles)
  2003. {
  2004. RemoteFileList = FileList;
  2005. }
  2006. else
  2007. {
  2008. RemoteFileList = new TStringList();
  2009. TMakeLocalFileListParams MakeFileListParam;
  2010. MakeFileListParam.FileList = RemoteFileList;
  2011. MakeFileListParam.FileTimes = &RemoteFileTimes;
  2012. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2013. MakeFileListParam.Recursive =
  2014. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  2015. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2016. if (!MakeFileListParam.Recursive)
  2017. {
  2018. RemoteFileTimes.clear();
  2019. RemoteFileListFull = new TStringList();
  2020. MakeFileListParam.FileList = RemoteFileListFull;
  2021. MakeFileListParam.Recursive = true;
  2022. MakeFileListParam.IncludeDirs = false;
  2023. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2024. }
  2025. else
  2026. {
  2027. RemoteFileListFull = RemoteFileList;
  2028. }
  2029. }
  2030. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  2031. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2032. if (!NonBlocking)
  2033. {
  2034. int Count;
  2035. if (FileListCommand)
  2036. {
  2037. Count = 1;
  2038. }
  2039. else if (LocalFileCommand)
  2040. {
  2041. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  2042. }
  2043. else
  2044. {
  2045. Count = RemoteFileList->Count;
  2046. }
  2047. Progress.Start(foCustomCommand, osRemote, Count);
  2048. DebugAssert(FProgressForm != NULL);
  2049. FProgressForm->ReadOnly = true;
  2050. }
  2051. try
  2052. {
  2053. if (FileListCommand)
  2054. {
  2055. UnicodeString LocalFile;
  2056. // MakeFileList does not delimit filenames
  2057. UnicodeString FileList = MakeFileList(RemoteFileList);
  2058. if (LocalFileCommand)
  2059. {
  2060. DebugAssert(LocalFileList->Count == 1);
  2061. LocalFile = LocalFileList->Strings[0];
  2062. }
  2063. TLocalCustomCommand CustomCommand(Data,
  2064. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  2065. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  2066. if (NonBlocking)
  2067. {
  2068. DebugAssert(!POutput);
  2069. ExecuteShellChecked(ShellCommand);
  2070. }
  2071. else
  2072. {
  2073. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2074. }
  2075. }
  2076. else if (LocalFileCommand)
  2077. {
  2078. if (LocalFileList->Count == 1)
  2079. {
  2080. UnicodeString LocalFile = LocalFileList->Strings[0];
  2081. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2082. {
  2083. UnicodeString FileName = RemoteFileList->Strings[Index];
  2084. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2085. Progress.SetFile(FileNameForProgress);
  2086. TLocalCustomCommand CustomCommand(Data,
  2087. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2088. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2089. TOnceDoneOperation OnceDoneOperation;
  2090. // Do not unselect anything, as it won't work with two panels anyway
  2091. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2092. }
  2093. }
  2094. else if (RemoteFileList->Count == 1)
  2095. {
  2096. UnicodeString FileName = RemoteFileList->Strings[0];
  2097. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2098. {
  2099. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2100. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2101. Progress.SetFile(LocalFileNameOnly);
  2102. TLocalCustomCommand CustomCommand(
  2103. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2104. FileName, LocalFileName, L"");
  2105. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2106. TOnceDoneOperation OnceDoneOperation;
  2107. // Do not unselect anything, as it won't work with two panels anyway
  2108. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2109. }
  2110. }
  2111. else
  2112. {
  2113. if (LocalFileList->Count != RemoteFileList->Count)
  2114. {
  2115. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2116. }
  2117. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2118. {
  2119. UnicodeString FileName = RemoteFileList->Strings[Index];
  2120. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2121. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2122. Progress.SetFile(LocalFileNameOnly); // sic
  2123. TLocalCustomCommand CustomCommand(
  2124. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2125. FileName, LocalFileName, L"");
  2126. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2127. TOnceDoneOperation OnceDoneOperation;
  2128. // Do not unselect anything, as it won't work with two panels anyway
  2129. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2130. }
  2131. }
  2132. }
  2133. else
  2134. {
  2135. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2136. {
  2137. UnicodeString FileName = RemoteFileList->Strings[Index];
  2138. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2139. Progress.SetFile(FileNameForProgress);
  2140. TLocalCustomCommand CustomCommand(Data,
  2141. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2142. FileName, L"", L"");
  2143. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2144. TOnceDoneOperation OnceDoneOperation;
  2145. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2146. }
  2147. }
  2148. }
  2149. __finally
  2150. {
  2151. if (!NonBlocking)
  2152. {
  2153. Progress.Stop();
  2154. }
  2155. }
  2156. DebugAssert(!FAutoOperation);
  2157. if (!RemoteFiles)
  2158. {
  2159. TempDir = IncludeTrailingBackslash(TempDir);
  2160. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2161. {
  2162. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2163. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2164. // Skip directories, process only nested files.
  2165. // The check is redundant as FileAge fails for directories anyway.
  2166. !DirectoryExists(FileName))
  2167. {
  2168. UnicodeString RemoteDir =
  2169. UnixExtractFileDir(
  2170. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2171. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2172. TDateTime NewTime;
  2173. if (FileAge(FileName, NewTime) &&
  2174. (NewTime != RemoteFileTimes[Index]))
  2175. {
  2176. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2177. TemporaryFileCopyParam(CopyParam);
  2178. CopyParam.FileMask = L"";
  2179. FAutoOperation = true;
  2180. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2181. TemporaryFilesList->Add(FileName);
  2182. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2183. }
  2184. }
  2185. }
  2186. }
  2187. }
  2188. __finally
  2189. {
  2190. FAutoOperation = false;
  2191. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2192. {
  2193. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2194. }
  2195. }
  2196. }
  2197. __finally
  2198. {
  2199. if (RemoteFileList != FileList)
  2200. {
  2201. delete RemoteFileList;
  2202. }
  2203. if (LocalFileList != ALocalFileList)
  2204. {
  2205. delete LocalFileList;
  2206. }
  2207. if (RemoteFileListFull != RemoteFileList)
  2208. {
  2209. delete RemoteFileListFull;
  2210. }
  2211. }
  2212. }
  2213. //---------------------------------------------------------------------------
  2214. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2215. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2216. bool FileListCommand, UnicodeString * POutput)
  2217. {
  2218. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2219. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2220. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2221. {
  2222. UnicodeString FileName = SelectedFileList->Strings[Index];
  2223. if (DirectoryExists(FileName))
  2224. {
  2225. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2226. {
  2227. LocalFileList->Add(FileName);
  2228. }
  2229. if (FLAGSET(ACommand.Params, ccRecursive))
  2230. {
  2231. TMakeLocalFileListParams MakeFileListParam;
  2232. MakeFileListParam.FileList = LocalFileList.get();
  2233. MakeFileListParam.FileTimes = NULL;
  2234. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2235. MakeFileListParam.Recursive = true;
  2236. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2237. }
  2238. }
  2239. else
  2240. {
  2241. LocalFileList->Add(FileName);
  2242. }
  2243. }
  2244. UnicodeString RemotePath;
  2245. if (Terminal != NULL)
  2246. {
  2247. RemotePath = Terminal->CurrentDirectory;
  2248. }
  2249. if (FileListCommand)
  2250. {
  2251. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2252. TLocalCustomCommand CustomCommand(
  2253. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2254. L"", L"", FileList);
  2255. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2256. }
  2257. else
  2258. {
  2259. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2260. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2261. DebugAssert(FProgressForm != NULL);
  2262. FProgressForm->ReadOnly = true;
  2263. try
  2264. {
  2265. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2266. {
  2267. UnicodeString FileName = LocalFileList->Strings[Index];
  2268. Progress.SetFile(FileName);
  2269. TLocalCustomCommand CustomCommand(
  2270. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2271. FileName, L"", L"");
  2272. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2273. TOnceDoneOperation OnceDoneOperation;
  2274. Progress.Finish(FileName, true, OnceDoneOperation);
  2275. }
  2276. }
  2277. __finally
  2278. {
  2279. Progress.Stop();
  2280. }
  2281. }
  2282. }
  2283. //---------------------------------------------------------------------------
  2284. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2285. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2286. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2287. {
  2288. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2289. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2290. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2291. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2292. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2293. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2294. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2295. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2296. std::unique_ptr<UnicodeString> POutput;
  2297. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2298. FLAGSET(ACommand.Params, ccCopyResults))
  2299. {
  2300. POutput.reset(new UnicodeString());
  2301. }
  2302. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2303. FProgressSide = FCurrentSide;
  2304. if (!LocalCustomCommand.IsFileCommand(Command))
  2305. {
  2306. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2307. }
  2308. // remote files?
  2309. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2310. {
  2311. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2312. }
  2313. // local files
  2314. else
  2315. {
  2316. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2317. }
  2318. if (POutput.get() != NULL)
  2319. {
  2320. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2321. int P = POutput->Pos(sLineBreak);
  2322. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2323. {
  2324. POutput->SetLength(P - 1);
  2325. }
  2326. // Copy even empty output to clipboard
  2327. if (FLAGSET(ACommand.Params, ccCopyResults))
  2328. {
  2329. CopyToClipboard(*POutput);
  2330. }
  2331. // But do not show an empty message box.
  2332. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2333. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2334. !POutput->IsEmpty())
  2335. {
  2336. TClipboardHandler ClipboardHandler;
  2337. ClipboardHandler.Text = *POutput;
  2338. TMessageParams Params;
  2339. TQueryButtonAlias Aliases[1];
  2340. Aliases[0].Button = qaRetry;
  2341. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2342. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2343. Params.Aliases = Aliases;
  2344. Params.AliasesCount = LENOF(Aliases);
  2345. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2346. }
  2347. }
  2348. }
  2349. //---------------------------------------------------------------------------
  2350. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2351. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2352. {
  2353. TCustomCommandData Data;
  2354. UnicodeString Site;
  2355. UnicodeString RemotePath;
  2356. if (HasActiveTerminal())
  2357. {
  2358. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2359. Data = TCustomCommandData(SessionData.get());
  2360. Site = Terminal->SessionData->SessionKey;
  2361. RemotePath = Terminal->CurrentDirectory;
  2362. }
  2363. UnicodeString HelpKeyword = ACommand.HomePage;
  2364. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2365. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2366. {
  2367. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2368. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2369. {
  2370. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2371. }
  2372. else
  2373. {
  2374. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2375. }
  2376. if (!DoCustomCommandOptionsDialog(
  2377. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2378. {
  2379. Abort();
  2380. }
  2381. }
  2382. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2383. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2384. {
  2385. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2386. }
  2387. else
  2388. {
  2389. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2390. }
  2391. }
  2392. //---------------------------------------------------------------------------
  2393. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2394. const TCustomCommandType & Command)
  2395. {
  2396. DebugAssert(FCustomCommandLocalFileList != NULL);
  2397. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2398. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2399. TStrings * LocalFileList = new TStringList();
  2400. TStrings * RemoteFileList = new TStringList();
  2401. try
  2402. {
  2403. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2404. {
  2405. LocalFileList->Clear();
  2406. LocalFileList->AddObject(
  2407. FCustomCommandLocalFileList->Strings[Index],
  2408. FCustomCommandLocalFileList->Objects[Index]);
  2409. RemoteFileList->Clear();
  2410. RemoteFileList->AddObject(
  2411. FCustomCommandRemoteFileList->Strings[Index],
  2412. FCustomCommandRemoteFileList->Objects[Index]);
  2413. CustomCommand(RemoteFileList, Command, LocalFileList);
  2414. }
  2415. }
  2416. __finally
  2417. {
  2418. delete LocalFileList;
  2419. delete RemoteFileList;
  2420. }
  2421. }
  2422. //---------------------------------------------------------------------------
  2423. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2424. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2425. {
  2426. delete FCustomCommandLocalFileList;
  2427. delete FCustomCommandRemoteFileList;
  2428. // takeover ownership,
  2429. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2430. FCustomCommandLocalFileList = LocalFileList;
  2431. FCustomCommandRemoteFileList = RemoteFileList;
  2432. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2433. if (Button != NULL)
  2434. {
  2435. FCustomCommandMenu->Items->Clear();
  2436. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2437. MenuPopup(FCustomCommandMenu, Button);
  2438. }
  2439. else
  2440. {
  2441. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2442. }
  2443. }
  2444. //---------------------------------------------------------------------------
  2445. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2446. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2447. {
  2448. DebugAssert(FCapturedLog != NULL);
  2449. if ((OutputType == cotOutput) || (OutputType == cotError))
  2450. {
  2451. FCapturedLog->Add(AddedLine);
  2452. }
  2453. }
  2454. //---------------------------------------------------------------------------
  2455. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2456. TOperationSide Side)
  2457. {
  2458. return
  2459. (Side == osOther) &&
  2460. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2461. }
  2462. //---------------------------------------------------------------------------
  2463. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2464. TOperationSide Side, TTBXCustomItem * Item,
  2465. TResolvedDoubleClickAction DoubleClickAction1, TResolvedDoubleClickAction DoubleClickAction2)
  2466. {
  2467. TCustomDirView * DView = DirView(Side);
  2468. bool IsDefault = false;
  2469. if (DView->ItemFocused != NULL)
  2470. {
  2471. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  2472. TResolvedDoubleClickAction DefaultAction =
  2473. WinConfiguration->ResolveDoubleClickAction(DView->ItemIsDirectory(DView->ItemFocused), ATerminal);
  2474. IsDefault = (DefaultAction == DoubleClickAction1) || (DefaultAction == DoubleClickAction2);
  2475. }
  2476. TTBItemOptions O = Item->Options;
  2477. if (IsDefault)
  2478. {
  2479. Item->Options = O << tboDefault;
  2480. }
  2481. else
  2482. {
  2483. Item->Options = O >> tboDefault;
  2484. }
  2485. }
  2486. //---------------------------------------------------------------------------
  2487. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2488. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2489. {
  2490. TCustomDirView * DView = DirView(Side);
  2491. TListItem * Item = DView->ItemFocused;
  2492. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2493. EnableFocusedOperation[Side])
  2494. {
  2495. TPoint ClientPoint;
  2496. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2497. TPoint(0, 0) : MousePos;
  2498. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2499. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2500. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2501. }
  2502. }
  2503. //---------------------------------------------------------------------------
  2504. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2505. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2506. {
  2507. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, rdcaChangeDir, rdcaOpen);
  2508. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, rdcaEdit, rdcaNone);
  2509. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, rdcaCopy, rdcaNone);
  2510. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2511. Handled = true;
  2512. }
  2513. //---------------------------------------------------------------------------
  2514. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2515. {
  2516. }
  2517. //---------------------------------------------------------------------------
  2518. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2519. {
  2520. DebugAssert(FErrorList == NULL);
  2521. if (WinConfiguration->ContinueOnError)
  2522. {
  2523. FErrorList = new TStringList();
  2524. Configuration->Usage->Inc(L"ContinuationsOnError");
  2525. }
  2526. NonVisualDataModule->StartBusy();
  2527. }
  2528. //---------------------------------------------------------------------------
  2529. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2530. {
  2531. NonVisualDataModule->EndBusy();
  2532. if (FErrorList)
  2533. {
  2534. HandleErrorList(FErrorList);
  2535. }
  2536. }
  2537. //---------------------------------------------------------------------------
  2538. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2539. const TCopyParamType & CopyParam)
  2540. {
  2541. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2542. {
  2543. Configuration->Usage->Inc(L"FileMaskUses");
  2544. }
  2545. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2546. {
  2547. Configuration->Usage->Inc(L"OperationMaskUses");
  2548. }
  2549. }
  2550. //---------------------------------------------------------------------------
  2551. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2552. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2553. {
  2554. DebugAssert(!IsLocalBrowserMode());
  2555. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2556. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2557. TTransferOperationParam DefaultParam;
  2558. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2559. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2560. switch (Param.Queue)
  2561. {
  2562. case asOn:
  2563. CopyParam.Queue = true;
  2564. break;
  2565. case asOff:
  2566. CopyParam.Queue = false;
  2567. break;
  2568. case asAuto:
  2569. default:
  2570. // keep default
  2571. break;
  2572. }
  2573. bool Result =
  2574. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2575. if (Result)
  2576. {
  2577. DebugAssert(Terminal);
  2578. bool SelectionRestored = false;
  2579. TCustomDirView * DView = NULL;
  2580. if (HasDirView[Side])
  2581. {
  2582. DView = DirView(Side);
  2583. DView->SaveSelection();
  2584. DView->SaveSelectedNames();
  2585. }
  2586. UpdateCopyParamCounters(CopyParam);
  2587. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2588. DebugAssert(FTransferResumeList == NULL);
  2589. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2590. FMoveToQueue = false;
  2591. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2592. try
  2593. {
  2594. TStrings * PermanentFileList;
  2595. std::unique_ptr<TStrings> PermanentFileListOwner;
  2596. try
  2597. {
  2598. if (Side == osLocal)
  2599. {
  2600. PermanentFileList = FileList;
  2601. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2602. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2603. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2604. if (Operation == foMove)
  2605. {
  2606. ReloadLocalDirectory();
  2607. if (DView != NULL)
  2608. {
  2609. DView->RestoreSelection();
  2610. }
  2611. SelectionRestored = true;
  2612. }
  2613. }
  2614. else
  2615. {
  2616. // Clone the file list as it may refer to current directory files,
  2617. // which get destroyed, when the source directory is reloaded after foMove operation.
  2618. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2619. // But for a hotfix, we are not going to do such a big change.
  2620. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2621. PermanentFileList = PermanentFileListOwner.get();
  2622. try
  2623. {
  2624. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2625. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2626. }
  2627. __finally
  2628. {
  2629. if (Operation == foMove)
  2630. {
  2631. if (DView != NULL)
  2632. {
  2633. DView->RestoreSelection();
  2634. }
  2635. SelectionRestored = true;
  2636. }
  2637. ReloadLocalDirectory(Param.TargetDirectory);
  2638. }
  2639. }
  2640. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2641. if (FMoveToQueue)
  2642. {
  2643. Abort();
  2644. }
  2645. }
  2646. catch (EAbort &)
  2647. {
  2648. if (FMoveToQueue)
  2649. {
  2650. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2651. DebugAssert(CopyParam.TransferSkipList == NULL);
  2652. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2653. if (TransferResumeList->Count > 0)
  2654. {
  2655. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2656. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2657. }
  2658. CopyParam.TransferSkipList = TransferResumeList.release();
  2659. // not really needed, just to keep it consistent with TransferResumeList
  2660. FTransferResumeList = NULL;
  2661. FMoveToQueue = false;
  2662. Configuration->Usage->Inc("MovesToBackground");
  2663. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2664. ClearTransferSourceSelection(Direction);
  2665. }
  2666. throw;
  2667. }
  2668. }
  2669. __finally
  2670. {
  2671. if (!SelectionRestored && (DView != NULL))
  2672. {
  2673. DView->DiscardSavedSelection();
  2674. }
  2675. FTransferResumeList = NULL;
  2676. }
  2677. }
  2678. return Result;
  2679. }
  2680. //---------------------------------------------------------------------------
  2681. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2682. TOperationSide Side, TStrings * FileList, void * Param)
  2683. {
  2684. DebugAssert(FileList->Count);
  2685. // We deliberately do not toggle alternative flag (Param), but use OR,
  2686. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2687. // shortcut of CurrentDeleteAlternativeAction
  2688. bool Alternative =
  2689. bool(Param) || UseAlternativeFunction();
  2690. bool Recycle;
  2691. if (IsSideLocalBrowser(Side))
  2692. {
  2693. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2694. }
  2695. else
  2696. {
  2697. Recycle =
  2698. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2699. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2700. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2701. }
  2702. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2703. if (!Result)
  2704. {
  2705. UnicodeString Query;
  2706. if (FileList->Count == 1)
  2707. {
  2708. if (IsSideLocalBrowser(Side))
  2709. {
  2710. Query = ExtractFileName(FileList->Strings[0]);
  2711. }
  2712. else
  2713. {
  2714. Query = UnixExtractFileName(FileList->Strings[0]);
  2715. }
  2716. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2717. }
  2718. else
  2719. {
  2720. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2721. }
  2722. TMessageParams Params(mpNeverAskAgainCheck);
  2723. Params.ImageName = L"Delete file";
  2724. unsigned int Answer =
  2725. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2726. if (Answer == qaNeverAskAgain)
  2727. {
  2728. Result = true;
  2729. if (Recycle)
  2730. {
  2731. WinConfiguration->ConfirmRecycling = false;
  2732. }
  2733. else
  2734. {
  2735. WinConfiguration->ConfirmDeleting = false;
  2736. }
  2737. }
  2738. else
  2739. {
  2740. Result = (Answer == qaOK);
  2741. }
  2742. }
  2743. if (Result)
  2744. {
  2745. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2746. }
  2747. return Result;
  2748. }
  2749. //---------------------------------------------------------------------------
  2750. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2751. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2752. {
  2753. TAutoBatch AutoBatch(this);
  2754. bool Result;
  2755. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2756. {
  2757. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2758. }
  2759. else if (Operation == foRename)
  2760. {
  2761. DebugAssert(DirView(Side)->ItemFocused);
  2762. DirView(Side)->ItemFocused->EditCaption();
  2763. Result = true;
  2764. }
  2765. else if (Operation == foDelete)
  2766. {
  2767. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2768. }
  2769. else if (Operation == foSetProperties)
  2770. {
  2771. DirView(osRemote)->SaveSelectedNames();
  2772. Result = SetProperties(Side, FileList);
  2773. }
  2774. else if (Operation == foCustomCommand)
  2775. {
  2776. DebugAssert(Param);
  2777. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2778. DirView(osRemote)->SaveSelectedNames();
  2779. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2780. CustomCommand(FileList, *Command, NULL);
  2781. Result = true;
  2782. }
  2783. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2784. {
  2785. DebugAssert(!IsSideLocalBrowser(Side));
  2786. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2787. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2788. }
  2789. else if (Operation == foLock)
  2790. {
  2791. DebugAssert(!IsSideLocalBrowser(Side));
  2792. LockFiles(FileList, true);
  2793. Result = true;
  2794. }
  2795. else if (Operation == foUnlock)
  2796. {
  2797. DebugAssert(!IsSideLocalBrowser(Side));
  2798. LockFiles(FileList, false);
  2799. Result = true;
  2800. }
  2801. else
  2802. {
  2803. DebugFail();
  2804. }
  2805. return Result;
  2806. }
  2807. //---------------------------------------------------------------------------
  2808. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2809. {
  2810. if (Side == osCurrent)
  2811. {
  2812. Side = FCurrentSide;
  2813. }
  2814. return Side;
  2815. }
  2816. //---------------------------------------------------------------------------
  2817. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2818. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2819. {
  2820. Side = GetSide(Side);
  2821. bool FullPath = IsSideLocalBrowser(Side);
  2822. TCustomDirView * ADirView = DirView(Side);
  2823. std::unique_ptr<TStringList> FileList(new TStringList());
  2824. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2825. bool ReloadProperties =
  2826. !IsSideLocalBrowser(Side) && (Operation == foSetProperties) &&
  2827. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2828. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2829. if (Result && ReloadProperties)
  2830. {
  2831. std::unique_ptr<TStringList> FileList2(new TStringList());
  2832. FileList->CaseSensitive = true;
  2833. FileList->Sort();
  2834. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2835. {
  2836. TListItem * Item = ADirView->Items->Item[Index];
  2837. UnicodeString FileName = ADirView->ItemFileName(Item);
  2838. int Index2;
  2839. if (FileList->Find(FileName, Index2))
  2840. {
  2841. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2842. }
  2843. }
  2844. LoadFilesProperties(FileList2.get());
  2845. }
  2846. return Result;
  2847. }
  2848. //---------------------------------------------------------------------------
  2849. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2850. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2851. bool NoConfirmation, void * Param)
  2852. {
  2853. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2854. {
  2855. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2856. {
  2857. DebugAssert(!IsLocalBrowserMode());
  2858. if (GetSide(Side) == osLocal)
  2859. {
  2860. Configuration->Usage->Inc(L"UploadsCommand");
  2861. }
  2862. else
  2863. {
  2864. Configuration->Usage->Inc(L"DownloadsCommand");
  2865. }
  2866. }
  2867. }
  2868. }
  2869. //---------------------------------------------------------------------------
  2870. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2871. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2872. {
  2873. TTransferOperationParam Param;
  2874. if ((WinConfiguration->Interface != ifCommander) ||
  2875. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2876. {
  2877. Flags &= ~cocShortCutHint;
  2878. }
  2879. TCustomDirView * DView = DirView(Side);
  2880. Param.Options =
  2881. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2882. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2883. if (FLAGSET(Flags, cocQueue))
  2884. {
  2885. Param.Queue = asOn;
  2886. }
  2887. else if (FLAGSET(Flags, cocNonQueue))
  2888. {
  2889. Param.Queue = asOff;
  2890. }
  2891. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2892. }
  2893. //---------------------------------------------------------------------------
  2894. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2895. {
  2896. int Count = ErrorList->Count;
  2897. try
  2898. {
  2899. if (ErrorList->Count)
  2900. {
  2901. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2902. if (MessageDialog(Message, qtError,
  2903. qaOK | qaCancel, HELP_NONE) == qaOK)
  2904. {
  2905. unsigned int Answer;
  2906. int Index = 0;
  2907. do
  2908. {
  2909. DebugAssert(Index >= 0 && Index < Count);
  2910. TQueryButtonAlias Aliases[2];
  2911. Aliases[0].Button = qaYes;
  2912. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2913. Aliases[1].Button = qaNo;
  2914. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2915. TMessageParams Params;
  2916. Params.Aliases = Aliases;
  2917. Params.AliasesCount = LENOF(Aliases);
  2918. int No = Index + 1;
  2919. UnicodeString Message = ErrorList->Strings[Index];
  2920. UnicodeString AMainInstructions;
  2921. UnicodeString Details;
  2922. if (ExtractMainInstructions(Message, AMainInstructions))
  2923. {
  2924. Details = Message;
  2925. Message = AMainInstructions;
  2926. }
  2927. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2928. Answer = MoreMessageDialog(
  2929. Message,
  2930. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2931. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2932. qaOK, HELP_NONE, &Params);
  2933. if (Answer == qaNo)
  2934. {
  2935. Index++;
  2936. }
  2937. if (Answer == qaYes)
  2938. {
  2939. Index--;
  2940. }
  2941. }
  2942. while (Answer != qaOK);
  2943. }
  2944. }
  2945. }
  2946. __finally
  2947. {
  2948. TStrings * List = ErrorList;
  2949. ErrorList = NULL;
  2950. for (int i = 0; i < Count; i++)
  2951. {
  2952. delete List->Objects[i];
  2953. }
  2954. delete List;
  2955. }
  2956. }
  2957. //---------------------------------------------------------------------------
  2958. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2959. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2960. {
  2961. // needed for checking filemasks, as there's no directory object
  2962. // associated with the file object
  2963. File->FullFileName = FullFileName;
  2964. TFileMasks::TParams MaskParams;
  2965. MaskParams.Size = File->Size;
  2966. MaskParams.Modification = File->Modification;
  2967. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2968. }
  2969. //---------------------------------------------------------------------------
  2970. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2971. {
  2972. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2973. Side = GetSide(Side);
  2974. TCustomDirView * DView = DirView(Side);
  2975. TListItem * FocusedItem = DView->ItemFocused;
  2976. UnicodeString Name;
  2977. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2978. {
  2979. Name = DView->ItemFileName(FocusedItem);
  2980. }
  2981. else
  2982. {
  2983. Name = LoadStr(NEW_FILE);
  2984. }
  2985. UnicodeString Names = Name;
  2986. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2987. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2988. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  2989. {
  2990. while (!Names.IsEmpty())
  2991. {
  2992. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2993. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2994. UnicodeString TargetFileName;
  2995. UnicodeString LocalFileName;
  2996. UnicodeString RootTempDir;
  2997. UnicodeString TempDir;
  2998. UnicodeString RemoteDirectory;
  2999. bool ExistingFile = false;
  3000. if (!IsSideLocalBrowser(Side))
  3001. {
  3002. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  3003. TRemoteFile * File = NULL;
  3004. if (Terminal->FileExists(Name, &File) &&
  3005. !File->IsDirectory)
  3006. {
  3007. try
  3008. {
  3009. ExecuteRemoteFile(Name, File, efDefaultEditor);
  3010. ExistingFile = true;
  3011. }
  3012. __finally
  3013. {
  3014. delete File;
  3015. }
  3016. }
  3017. if (!ExistingFile)
  3018. {
  3019. RemoteDirectory = UnixExtractFilePath(Name);
  3020. TemporaryDirectoryForRemoteFiles(
  3021. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  3022. TargetFileName = UnixExtractFileName(Name);
  3023. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3024. LocalFileName = TempDir +
  3025. // We probably do not want to trim the VMS version here
  3026. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  3027. }
  3028. }
  3029. else
  3030. {
  3031. if (ExtractFilePath(Name).IsEmpty())
  3032. {
  3033. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  3034. }
  3035. else
  3036. {
  3037. LocalFileName = ExpandFileName(Name);
  3038. }
  3039. TargetFileName = ExtractFileName(Name);
  3040. }
  3041. if (!ExistingFile)
  3042. {
  3043. bool NewFile = !FileExists(ApiPath(LocalFileName));
  3044. if (NewFile)
  3045. {
  3046. int File = FileCreate(ApiPath(LocalFileName));
  3047. if (File < 0)
  3048. {
  3049. if (!RootTempDir.IsEmpty())
  3050. {
  3051. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3052. }
  3053. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  3054. }
  3055. else
  3056. {
  3057. FileClose(File);
  3058. }
  3059. }
  3060. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  3061. const TEditorData * ExternalEditor = NULL;
  3062. TFileMasks::TParams MaskParams; // size not known
  3063. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  3064. false, MaskParams);
  3065. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  3066. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  3067. }
  3068. }
  3069. }
  3070. }
  3071. //---------------------------------------------------------------------------
  3072. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  3073. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  3074. {
  3075. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3076. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3077. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3078. return
  3079. ((ExecuteFileBy == efInternalEditor)) ||
  3080. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  3081. }
  3082. //---------------------------------------------------------------------------
  3083. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  3084. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  3085. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  3086. UnicodeString RemoteDirectory, bool NewFile)
  3087. {
  3088. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3089. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3090. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3091. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3092. Side = GetSide(Side);
  3093. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3094. if (!IsSideLocalBrowser(Side))
  3095. {
  3096. Data->Terminal = Terminal;
  3097. Data->Queue = Queue;
  3098. Data->SessionData = CloneCurrentSessionData();
  3099. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3100. Data->RemoteDirectory = RemoteDirectory;
  3101. Data->SessionName = Terminal->SessionData->SessionName;
  3102. Data->LocalRootDirectory = LocalRootDirectory;
  3103. Data->OriginalFileName = OriginalFileName;
  3104. Data->Command = L""; // will be changed later for external editor
  3105. }
  3106. if (ExecuteFileBy == efInternalEditor)
  3107. {
  3108. if (!IsSideLocalBrowser(Side))
  3109. {
  3110. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3111. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3112. TForm * Editor;
  3113. try
  3114. {
  3115. Editor =
  3116. ShowEditorForm(
  3117. FileName, this,
  3118. FEditorManager->FileChanged, FEditorManager->FileReload, FEditorManager->FileClosed,
  3119. SaveAllInternalEditors, AnyInternalEditorModified,
  3120. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3121. }
  3122. catch(...)
  3123. {
  3124. if (!LocalRootDirectory.IsEmpty())
  3125. {
  3126. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  3127. }
  3128. throw;
  3129. }
  3130. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3131. }
  3132. else
  3133. {
  3134. DebugAssert(!StandaloneOperation);
  3135. TForm * Editor =
  3136. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3137. SaveAllInternalEditors, AnyInternalEditorModified,
  3138. L"", false, SessionColor, -1, NewFile);
  3139. FLocalEditors->Add(Editor);
  3140. }
  3141. }
  3142. else
  3143. {
  3144. HANDLE Process;
  3145. if (ExecuteFileBy == efExternalEditor)
  3146. {
  3147. UnicodeString Program, Params, Dir;
  3148. Data->Command = ExternalEditor->ExternalEditor;
  3149. ReformatFileNameCommand(Data->Command);
  3150. SplitCommand(Data->Command, Program, Params, Dir);
  3151. Params = ExpandFileNameCommand(Params, FileName);
  3152. Program = ExpandEnvironmentVariables(Program);
  3153. if (!ExecuteShell(Program, Params, Process))
  3154. {
  3155. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3156. }
  3157. }
  3158. else
  3159. {
  3160. DebugAssert(!IsSideLocalBrowser(Side));
  3161. if (!ExecuteShell(FileName, L"", Process))
  3162. {
  3163. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3164. }
  3165. }
  3166. if (IsSideLocalBrowser(Side) ||
  3167. ((ExecuteFileBy == efShell) &&
  3168. !WinConfiguration->Editor.SDIShellEditor) ||
  3169. ((ExecuteFileBy == efExternalEditor) &&
  3170. !ExternalEditor->SDIExternalEditor))
  3171. {
  3172. // no need for handle
  3173. if (Process != NULL)
  3174. {
  3175. DebugCheck(CloseHandle(Process));
  3176. }
  3177. Process = INVALID_HANDLE_VALUE;
  3178. }
  3179. else
  3180. {
  3181. if (Process == NULL)
  3182. {
  3183. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3184. }
  3185. }
  3186. if (!IsSideLocalBrowser(Side))
  3187. {
  3188. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3189. }
  3190. }
  3191. }
  3192. //---------------------------------------------------------------------------
  3193. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3194. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3195. void * /*Arg*/)
  3196. {
  3197. if (Token != NULL)
  3198. {
  3199. EditorFormFileSave(static_cast<TForm *>(Token));
  3200. }
  3201. }
  3202. //---------------------------------------------------------------------------
  3203. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3204. {
  3205. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3206. {
  3207. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3208. }
  3209. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3210. }
  3211. //---------------------------------------------------------------------------
  3212. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3213. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3214. void * Arg)
  3215. {
  3216. if ((Token != NULL) &&
  3217. IsEditorFormModified(static_cast<TForm *>(Token)))
  3218. {
  3219. *static_cast<bool *>(Arg) = true;
  3220. }
  3221. }
  3222. //---------------------------------------------------------------------------
  3223. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3224. TObject * /*Sender*/, bool & Modified)
  3225. {
  3226. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3227. {
  3228. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3229. {
  3230. Modified = true;
  3231. }
  3232. }
  3233. if (!Modified)
  3234. {
  3235. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3236. }
  3237. }
  3238. //---------------------------------------------------------------------------
  3239. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3240. {
  3241. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3242. }
  3243. //---------------------------------------------------------------------------
  3244. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3245. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3246. UnicodeString & Result, UnicodeString & RootDirectory)
  3247. {
  3248. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3249. {
  3250. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3251. Result = RootDirectory;
  3252. }
  3253. else
  3254. {
  3255. RootDirectory = L"";
  3256. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3257. Result = IncludeTrailingBackslash(Result);
  3258. }
  3259. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3260. {
  3261. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3262. }
  3263. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3264. {
  3265. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3266. {
  3267. RemoteDirectory.Delete(1, 1);
  3268. }
  3269. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3270. }
  3271. if (!ForceDirectories(ApiPath(Result)))
  3272. {
  3273. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3274. }
  3275. }
  3276. //---------------------------------------------------------------------------
  3277. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3278. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3279. bool GetTargetNames, bool AutoOperation)
  3280. {
  3281. DebugAssert(!IsLocalBrowserMode());
  3282. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3283. if (ForceText)
  3284. {
  3285. CopyParam.TransferMode = tmAscii;
  3286. }
  3287. TemporaryFileCopyParam(CopyParam);
  3288. if (TempDir.IsEmpty())
  3289. {
  3290. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3291. }
  3292. DebugAssert(!FAutoOperation);
  3293. FAutoOperation = AutoOperation;
  3294. Terminal->ExceptionOnFail = true;
  3295. try
  3296. {
  3297. try
  3298. {
  3299. // turn off confirmations, as for MDI editors we may possibly download
  3300. // the same file over
  3301. Terminal->CopyToLocal(
  3302. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3303. if (GetTargetNames)
  3304. {
  3305. for (int i = 0; i < FileList->Count; i++)
  3306. {
  3307. FileList->Strings[i] =
  3308. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3309. }
  3310. }
  3311. }
  3312. catch(...)
  3313. {
  3314. if (!RootTempDir.IsEmpty())
  3315. {
  3316. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3317. }
  3318. throw;
  3319. }
  3320. }
  3321. __finally
  3322. {
  3323. FAutoOperation = false;
  3324. Terminal->ExceptionOnFail = false;
  3325. }
  3326. }
  3327. //---------------------------------------------------------------------------
  3328. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3329. {
  3330. // Do not waste time checking for default editor list the next time,
  3331. // or testing if default editor is not notepad.
  3332. bool TryNextTime = false;
  3333. UnicodeString UsageState;
  3334. if (!WinConfiguration->EditorList->IsDefaultList())
  3335. {
  3336. UsageState = "H";
  3337. }
  3338. else
  3339. {
  3340. UnicodeString Executable;
  3341. UnicodeString ExecutableDescription;
  3342. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3343. {
  3344. UnicodeString Message =
  3345. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3346. unsigned int Answer =
  3347. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3348. if (Answer != qaOK)
  3349. {
  3350. UsageState = "R";
  3351. }
  3352. else
  3353. {
  3354. UsageState = "A";
  3355. TEditorData EditorData;
  3356. EditorData.Editor = edExternal;
  3357. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3358. EditorData.ExternalEditorOptionsAutodetect();
  3359. TEditorList EditorList;
  3360. EditorList = *WinConfiguration->EditorList;
  3361. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3362. WinConfiguration->EditorList = &EditorList;
  3363. }
  3364. }
  3365. }
  3366. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3367. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3368. }
  3369. //---------------------------------------------------------------------------
  3370. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3371. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3372. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3373. {
  3374. if (ExecuteFileBy == efDefaultEditor)
  3375. {
  3376. if (!WinConfiguration->OfferedEditorAutoConfig)
  3377. {
  3378. EditorAutoConfig();
  3379. }
  3380. const TEditorPreferences * Editor =
  3381. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3382. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3383. {
  3384. ExecuteFileBy = efInternalEditor;
  3385. ExternalEditor = NULL;
  3386. }
  3387. else if (Editor->Data->Editor == edOpen)
  3388. {
  3389. ExecuteFileBy = efShell;
  3390. ExternalEditor = NULL;
  3391. }
  3392. else
  3393. {
  3394. ExecuteFileBy = efExternalEditor;
  3395. ExternalEditor = Editor->Data;
  3396. }
  3397. }
  3398. }
  3399. //---------------------------------------------------------------------------
  3400. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3401. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3402. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3403. {
  3404. UnicodeString OriginalFileName;
  3405. UnicodeString LocalRootDirectory;
  3406. UnicodeString RemoteDirectory;
  3407. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3408. IsSideLocalBrowser(Side), MaskParams);
  3409. UnicodeString Counter;
  3410. UnicodeString LocalFileName;
  3411. bool Handled = false;
  3412. if (!IsSideLocalBrowser(Side))
  3413. {
  3414. // We need to trim VMS version here, so that we use name without version
  3415. // when uploading back to create a new version of the file
  3416. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3417. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3418. TObject * Token = NULL;
  3419. UnicodeString LocalDirectory;
  3420. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3421. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3422. LocalDirectory))
  3423. {
  3424. if (Token != NULL)
  3425. {
  3426. TForm * Form = dynamic_cast<TForm *>(Token);
  3427. if (Form->WindowState == wsMinimized)
  3428. {
  3429. ShowWindow(Form->Handle, SW_RESTORE);
  3430. }
  3431. else
  3432. {
  3433. Form->SetFocus();
  3434. }
  3435. Handled = true;
  3436. }
  3437. else
  3438. {
  3439. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3440. }
  3441. }
  3442. if (!Handled)
  3443. {
  3444. TStringList * FileList1 = new TStringList();
  3445. try
  3446. {
  3447. FileList1->AddObject(FullFileName, Object);
  3448. TemporarilyDownloadFiles(FileList1,
  3449. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3450. LocalRootDirectory, LocalDirectory, true, true);
  3451. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3452. }
  3453. __finally
  3454. {
  3455. delete FileList1;
  3456. }
  3457. switch (ExecuteFileBy)
  3458. {
  3459. case efShell:
  3460. Counter = "RemoteFilesExecuted";
  3461. break;
  3462. case efInternalEditor:
  3463. Counter = "RemoteFilesOpenedInInternalEditor";
  3464. break;
  3465. case efExternalEditor:
  3466. Counter = "RemoteFilesOpenedInExternalEditor";
  3467. break;
  3468. default:
  3469. DebugFail();
  3470. }
  3471. }
  3472. }
  3473. else
  3474. {
  3475. LocalFileName = FullFileName;
  3476. OriginalFileName = ExtractFileName(FullFileName);
  3477. switch (ExecuteFileBy)
  3478. {
  3479. case efShell:
  3480. Counter = "LocalFilesExecuted";
  3481. break;
  3482. case efInternalEditor:
  3483. Counter = "LocalFilesOpenedInInternalEditor";
  3484. break;
  3485. case efExternalEditor:
  3486. Counter = "LocalFilesOpenedInExternalEditor";
  3487. break;
  3488. default:
  3489. DebugFail();
  3490. }
  3491. }
  3492. if (!Handled)
  3493. {
  3494. Configuration->Usage->Inc(Counter);
  3495. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3496. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3497. }
  3498. }
  3499. //---------------------------------------------------------------------------
  3500. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3501. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3502. bool AllSelected, bool OnFocused)
  3503. {
  3504. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3505. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3506. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3507. Side = GetSide(Side);
  3508. TCustomDirView * DView = DirView(Side);
  3509. TStrings * FileList =
  3510. AllSelected ?
  3511. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3512. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3513. try
  3514. {
  3515. DebugAssert(AllSelected || (FileList->Count == 1));
  3516. for (int i = 0; i < FileList->Count; i++)
  3517. {
  3518. UnicodeString ListFileName = FileList->Strings[i];
  3519. UnicodeString FileNameOnly =
  3520. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3521. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3522. if (!DView->ItemIsDirectory(Item))
  3523. {
  3524. UnicodeString FullFileName;
  3525. if (!IsSideLocalBrowser(Side))
  3526. {
  3527. FullFileName = DirView(Side)->Path + ListFileName;
  3528. }
  3529. else
  3530. {
  3531. FullFileName = ListFileName;
  3532. }
  3533. TObject * Object = FileList->Objects[i];
  3534. TFileMasks::TParams MaskParams;
  3535. MaskParams.Size = DView->ItemFileSize(Item);
  3536. TDateTimePrecision Precision;
  3537. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3538. std::unique_ptr<TRemoteFile> FileOwner;
  3539. if (!IsSideLocalBrowser(Side))
  3540. {
  3541. TRemoteFile * File = NULL;
  3542. Terminal->FileExists(FullFileName, &File);
  3543. if (File != NULL)
  3544. {
  3545. File->FullFileName = FullFileName;
  3546. Object = File;
  3547. FileOwner.reset(File);
  3548. MaskParams.Size = File->Size;
  3549. MaskParams.Modification = File->Modification;
  3550. }
  3551. }
  3552. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName, Object, MaskParams);
  3553. }
  3554. }
  3555. }
  3556. __finally
  3557. {
  3558. delete FileList;
  3559. }
  3560. }
  3561. //---------------------------------------------------------------------------
  3562. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3563. {
  3564. CopyParam.FileNameCase = ncNoChange;
  3565. CopyParam.PreserveRights = false;
  3566. CopyParam.PreserveReadOnly = false;
  3567. CopyParam.ReplaceInvalidChars = true;
  3568. CopyParam.IncludeFileMask = TFileMasks();
  3569. CopyParam.NewerOnly = false;
  3570. CopyParam.FileMask = L"";
  3571. }
  3572. //---------------------------------------------------------------------------
  3573. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
  3574. const UnicodeString & FileName, TEditedFileData * Data, HANDLE UploadCompleteEvent, bool & Retry)
  3575. {
  3576. TTerminalManager * Manager = TTerminalManager::Instance();
  3577. if (!IsActiveTerminal(Data->Terminal))
  3578. {
  3579. if (!NonVisualDataModule->Busy)
  3580. {
  3581. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3582. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3583. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3584. if (Data->Terminal == NULL)
  3585. {
  3586. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3587. if (SameSiteTerminal != NULL)
  3588. {
  3589. UnicodeString Message =
  3590. FMTLOAD(EDIT_SESSION_REATTACH,
  3591. (FileNameOnly, Data->SessionName, FileNameOnly));
  3592. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3593. {
  3594. Data->Terminal = Terminal;
  3595. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3596. Data->SessionName = Terminal->SessionData->SessionName;
  3597. // We might also overwrite session data
  3598. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3599. }
  3600. else
  3601. {
  3602. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3603. Abort();
  3604. }
  3605. }
  3606. }
  3607. // foreground session should reconnect itself
  3608. else if (Terminal != Data->Terminal)
  3609. {
  3610. UnicodeString Message =
  3611. MainInstructions(
  3612. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3613. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3614. {
  3615. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3616. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3617. }
  3618. else
  3619. {
  3620. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3621. Abort();
  3622. }
  3623. }
  3624. }
  3625. if (!IsActiveTerminal(Data->Terminal))
  3626. {
  3627. Configuration->Usage->Inc(L"EditInactiveSession");
  3628. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3629. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3630. (ExtractFileName(FileName), Data->SessionName)));
  3631. }
  3632. }
  3633. TStrings * FileList = new TStringList();
  3634. try
  3635. {
  3636. FileList->Add(FileName);
  3637. // consider using the same settings (preset) as when the file was downloaded
  3638. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3639. TemporaryFileCopyParam(CopyParam);
  3640. if (Data->ForceText)
  3641. {
  3642. CopyParam.TransferMode = tmAscii;
  3643. }
  3644. // so i do not need to worry if masking algorithm works in all cases
  3645. // ("" means "copy file name", no masking is actually done)
  3646. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3647. {
  3648. CopyParam.FileMask = L"";
  3649. }
  3650. else
  3651. {
  3652. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3653. }
  3654. int Params = cpNoConfirmation | cpTemporary;
  3655. if (Terminal->IsCapable[fcBackgroundTransfers])
  3656. {
  3657. DebugAssert(Data->Queue != NULL);
  3658. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3659. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3660. QueueItem->CompleteEvent = UploadCompleteEvent;
  3661. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3662. }
  3663. else
  3664. {
  3665. if (NonVisualDataModule->Busy)
  3666. {
  3667. Retry = true;
  3668. }
  3669. else
  3670. {
  3671. Terminal->CopyToRemote(FileList, Data->RemoteDirectory, &CopyParam, Params, NULL);
  3672. SetEvent(UploadCompleteEvent);
  3673. }
  3674. }
  3675. }
  3676. __finally
  3677. {
  3678. delete FileList;
  3679. }
  3680. }
  3681. //---------------------------------------------------------------------------
  3682. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3683. const UnicodeString FileName, const TEditedFileData * Data)
  3684. {
  3685. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3686. DebugAssert(!NonVisualDataModule->Busy);
  3687. if (!IsActiveTerminal(Data->Terminal))
  3688. {
  3689. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3690. (ExtractFileName(FileName), Data->SessionName)));
  3691. }
  3692. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3693. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3694. NonVisualDataModule->StartBusy();
  3695. try
  3696. {
  3697. std::unique_ptr<TRemoteFile> File;
  3698. UnicodeString RemoteFileName =
  3699. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3700. Terminal->ExceptionOnFail = true;
  3701. try
  3702. {
  3703. TRemoteFile * AFile = NULL;
  3704. Terminal->ReadFile(RemoteFileName, AFile);
  3705. File.reset(AFile);
  3706. if (!File->HaveFullFileName)
  3707. {
  3708. File->FullFileName = RemoteFileName;
  3709. }
  3710. }
  3711. __finally
  3712. {
  3713. Terminal->ExceptionOnFail = false;
  3714. }
  3715. std::unique_ptr<TStrings> FileList(new TStringList());
  3716. FileList->AddObject(RemoteFileName, File.get());
  3717. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3718. UnicodeString TempDir = ExtractFilePath(FileName);
  3719. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, true);
  3720. // sanity check, the target file name should be still the same
  3721. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3722. }
  3723. __finally
  3724. {
  3725. NonVisualDataModule->EndBusy();
  3726. // it actually may not exist anymore...
  3727. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3728. }
  3729. }
  3730. //---------------------------------------------------------------------------
  3731. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3732. const TEditedFileData * Data, bool & KeepOpen)
  3733. {
  3734. // Command is set for external editors only (not for "shell" open).
  3735. if (!Data->Command.IsEmpty())
  3736. {
  3737. bool AnyFound = false;
  3738. bool AnyMDI = false;
  3739. bool AnyNonMDI = false;
  3740. bool AnyDetect = false;
  3741. TEditorList * EditorList = new TEditorList();
  3742. try
  3743. {
  3744. *EditorList = *WinConfiguration->EditorList;
  3745. for (int i = 0; i < EditorList->Count; i++)
  3746. {
  3747. const TEditorPreferences * Editor = EditorList->Editors[i];
  3748. if ((Editor->Data->Editor == edExternal) &&
  3749. (Editor->Data->ExternalEditor == Data->Command))
  3750. {
  3751. AnyFound = true;
  3752. if (Editor->Data->SDIExternalEditor)
  3753. {
  3754. AnyNonMDI = true;
  3755. if (Editor->Data->DetectMDIExternalEditor)
  3756. {
  3757. AnyDetect = true;
  3758. }
  3759. }
  3760. else
  3761. {
  3762. AnyMDI = true;
  3763. }
  3764. }
  3765. }
  3766. bool EnableMDI = false;
  3767. bool DisableDetect = false;
  3768. if (AnyMDI)
  3769. {
  3770. KeepOpen = true;
  3771. if (AnyNonMDI)
  3772. {
  3773. // there is at least one instance of the editor with MDI support enabled,
  3774. // and one with disabled, enable it for all instances
  3775. EnableMDI = true;
  3776. }
  3777. }
  3778. else if (AnyFound && !AnyDetect)
  3779. {
  3780. // at least once instance found but all have MDI autodetection disabled
  3781. // => close the file (default action)
  3782. }
  3783. else
  3784. {
  3785. // no instance of the editor has MDI support enabled
  3786. TMessageParams Params;
  3787. if (AnyFound)
  3788. {
  3789. // there is at least one instance of the editor
  3790. Params.Params |= mpNeverAskAgainCheck;
  3791. }
  3792. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3793. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3794. switch (Answer)
  3795. {
  3796. case qaNeverAskAgain:
  3797. DisableDetect = true;
  3798. break;
  3799. case qaNo:
  3800. EnableMDI = true;
  3801. KeepOpen = true;
  3802. break;
  3803. }
  3804. }
  3805. if (AnyFound && (EnableMDI || DisableDetect))
  3806. {
  3807. bool Changed = false;
  3808. for (int i = 0; i < EditorList->Count; i++)
  3809. {
  3810. const TEditorPreferences * Editor = EditorList->Editors[i];
  3811. if ((Editor->Data->Editor == edExternal) &&
  3812. (Editor->Data->ExternalEditor == Data->Command) &&
  3813. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3814. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3815. {
  3816. Changed = true;
  3817. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3818. if (EnableMDI)
  3819. {
  3820. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3821. }
  3822. if (DisableDetect)
  3823. {
  3824. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3825. }
  3826. EditorList->Change(i, UpdatedEditor);
  3827. }
  3828. }
  3829. if (Changed)
  3830. {
  3831. WinConfiguration->EditorList = EditorList;
  3832. }
  3833. }
  3834. }
  3835. __finally
  3836. {
  3837. delete EditorList;
  3838. }
  3839. }
  3840. else
  3841. {
  3842. // "open" case
  3843. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3844. qaOK, HELP_APP_EARLY_CLOSED);
  3845. }
  3846. }
  3847. //---------------------------------------------------------------------------
  3848. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3849. {
  3850. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3851. }
  3852. //---------------------------------------------------------------------------
  3853. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3854. {
  3855. SideEnter(osRemote);
  3856. }
  3857. //---------------------------------------------------------------------------
  3858. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3859. {
  3860. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3861. SideEnter(osRemote);
  3862. }
  3863. //---------------------------------------------------------------------------
  3864. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3865. {
  3866. FCurrentSide = Side;
  3867. if (Visible)
  3868. {
  3869. UpdateControls();
  3870. }
  3871. }
  3872. //---------------------------------------------------------------------------
  3873. void __fastcall TCustomScpExplorerForm::FileDeleted(TOperationSide Side, const UnicodeString & FileName, bool Success)
  3874. {
  3875. VisualiseOperationFinished(Side, FileName, Success);
  3876. if (DebugAlwaysTrue(FDeletedFiles != NULL) && Success)
  3877. {
  3878. FDeletedFiles->Add(FileName);
  3879. }
  3880. }
  3881. //---------------------------------------------------------------------------
  3882. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3883. TStrings * FileList, bool Alternative)
  3884. {
  3885. TCustomDirView * DView = DirView(Side);
  3886. DView->SaveSelection();
  3887. DView->SaveSelectedNames();
  3888. DebugAssert(!FAlternativeDelete);
  3889. FAlternativeDelete = Alternative;
  3890. try
  3891. {
  3892. if (!IsSideLocalBrowser(Side))
  3893. {
  3894. DebugAssert(Terminal != NULL);
  3895. // Clone the file list as it may refer to current directory files, which get destroyed, when the directory is reloaded after the operation
  3896. std::unique_ptr<TStrings> PermanentFileList(TRemoteFileList::CloneStrings(FileList));
  3897. std::unique_ptr<TStringList> DeletedFiles(new TStringList());
  3898. DeletedFiles->CaseSensitive = DebugAlwaysTrue(!IsSideLocalBrowser(Side));
  3899. int Params = FLAGMASK(Alternative, dfAlternative);
  3900. try
  3901. {
  3902. TValueRestorer<TStrings *> DeletedFilesRestorer(FDeletedFiles);
  3903. DebugAssert(FDeletedFiles == NULL);
  3904. FDeletedFiles = DeletedFiles.get();
  3905. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  3906. FOnFileOperationFinished = FileDeleted;
  3907. FMoveToQueue = false;
  3908. Terminal->DeleteFiles(FileList, Params);
  3909. // Probably not needed for deleting, just for consistency with transfer code
  3910. if (FMoveToQueue)
  3911. {
  3912. Abort();
  3913. }
  3914. }
  3915. catch (EAbort &)
  3916. {
  3917. if (FMoveToQueue)
  3918. {
  3919. DeletedFiles->Sorted = true;
  3920. for (int Index = 0; Index < PermanentFileList->Count; Index++)
  3921. {
  3922. if (DeletedFiles->IndexOf(PermanentFileList->Strings[Index]) >= 0)
  3923. {
  3924. // We should always be deleting the first item => what can be used to optimize this code, if needed
  3925. DebugAssert(Index == 0);
  3926. PermanentFileList->Delete(Index);
  3927. Index--;
  3928. }
  3929. }
  3930. Configuration->Usage->Inc("MovesToBackgroundDelete");
  3931. TQueueItem * QueueItem = new TDeleteQueueItem(Terminal, PermanentFileList.get(), Params);
  3932. AddQueueItem(Queue, QueueItem, Terminal);
  3933. ClearOperationSelection(osRemote);
  3934. }
  3935. }
  3936. }
  3937. else
  3938. {
  3939. if (IsLocalBrowserMode())
  3940. {
  3941. Configuration->Usage->Inc(L"LocalLocalDeletes");
  3942. }
  3943. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  3944. FProgressSide = FCurrentSide;
  3945. try
  3946. {
  3947. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3948. }
  3949. __finally
  3950. {
  3951. ReloadLocalDirectory();
  3952. }
  3953. }
  3954. FAlternativeDelete = false;
  3955. }
  3956. catch(...)
  3957. {
  3958. FAlternativeDelete = false;
  3959. DView->DiscardSavedSelection();
  3960. throw;
  3961. }
  3962. DView->RestoreSelection();
  3963. }
  3964. //---------------------------------------------------------------------------
  3965. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3966. {
  3967. DebugAssert(Terminal);
  3968. DebugAssert(!IsLocalBrowserMode());
  3969. RemoteDirView->SaveSelection();
  3970. RemoteDirView->SaveSelectedNames();
  3971. try
  3972. {
  3973. if (Lock)
  3974. {
  3975. Terminal->LockFiles(FileList);
  3976. }
  3977. else
  3978. {
  3979. Terminal->UnlockFiles(FileList);
  3980. }
  3981. }
  3982. catch(...)
  3983. {
  3984. RemoteDirView->DiscardSavedSelection();
  3985. throw;
  3986. }
  3987. RemoteDirView->RestoreSelection();
  3988. }
  3989. //---------------------------------------------------------------------------
  3990. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3991. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3992. bool NoConfirmation, bool Move)
  3993. {
  3994. DebugAssert(Terminal != NULL);
  3995. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  3996. DebugAssert(!IsLocalBrowserMode());
  3997. if (Session == NULL)
  3998. {
  3999. Session = Terminal;
  4000. }
  4001. if (Session == Terminal)
  4002. {
  4003. if (RemoteDriveView->DropTarget != NULL)
  4004. {
  4005. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  4006. }
  4007. else if (RemoteDirView->DropTarget != NULL)
  4008. {
  4009. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  4010. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  4011. }
  4012. else
  4013. {
  4014. Target = RemoteDirView->Path;
  4015. }
  4016. }
  4017. else
  4018. {
  4019. Target = GetSessionPath(Session, osRemote);
  4020. }
  4021. Target = UnixIncludeTrailingBackslash(Target);
  4022. if (FileList->Count == 1)
  4023. {
  4024. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  4025. }
  4026. else
  4027. {
  4028. FileMask = AnyMask;
  4029. }
  4030. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  4031. bool Result = true;
  4032. if (!NoConfirmation)
  4033. {
  4034. bool Multi = (FileList->Count > 1);
  4035. if (Move)
  4036. {
  4037. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  4038. }
  4039. else
  4040. {
  4041. std::unique_ptr<TStrings> Sessions(new TStringList);
  4042. std::unique_ptr<TStrings> Directories(new TStringList);
  4043. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  4044. for (int Index = 0; Index < SessionList->Count; Index++)
  4045. {
  4046. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  4047. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  4048. {
  4049. Sessions->AddObject(SessionList->Strings[Index], ASession);
  4050. Directories->Add(GetSessionPath(ASession, osRemote));
  4051. }
  4052. }
  4053. TDirectRemoteCopy AllowDirectCopy;
  4054. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  4055. {
  4056. DebugAssert(DirectCopy);
  4057. AllowDirectCopy = drcAllow;
  4058. }
  4059. else if (Terminal->IsCapable[fcSecondaryShell])
  4060. {
  4061. DebugAssert(DirectCopy);
  4062. AllowDirectCopy = drcConfirmCommandSession;
  4063. }
  4064. else
  4065. {
  4066. DebugAssert(!DirectCopy);
  4067. AllowDirectCopy = drcDisallow;
  4068. }
  4069. void * ASession = Session;
  4070. Result = DoRemoteCopyDialog(
  4071. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal);
  4072. Session = static_cast<TManagedTerminal *>(ASession);
  4073. }
  4074. }
  4075. return Result;
  4076. }
  4077. //---------------------------------------------------------------------------
  4078. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  4079. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  4080. {
  4081. DebugAssert(!IsLocalBrowserMode());
  4082. bool DirectCopy;
  4083. UnicodeString Target, FileMask;
  4084. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  4085. if (Result)
  4086. {
  4087. if (!Move && !DirectCopy)
  4088. {
  4089. Configuration->Usage->Inc("RemoteCopyTemp");
  4090. UnicodeString RootTempDir;
  4091. UnicodeString TempDir;
  4092. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false);
  4093. TStrings * TemporaryFilesList = new TStringList();
  4094. try
  4095. {
  4096. TMakeLocalFileListParams MakeFileListParam;
  4097. MakeFileListParam.FileList = TemporaryFilesList;
  4098. MakeFileListParam.FileTimes = NULL;
  4099. MakeFileListParam.IncludeDirs = true;
  4100. MakeFileListParam.Recursive = false;
  4101. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  4102. TTerminalManager::Instance()->ActiveSession = Session;
  4103. if (TemporaryFilesList->Count > 0)
  4104. {
  4105. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4106. CopyParam.FileMask = FileMask;
  4107. DebugAssert(!FAutoOperation);
  4108. FAutoOperation = true;
  4109. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  4110. }
  4111. }
  4112. __finally
  4113. {
  4114. delete TemporaryFilesList;
  4115. FAutoOperation = false;
  4116. if (!RootTempDir.IsEmpty())
  4117. {
  4118. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  4119. }
  4120. }
  4121. }
  4122. else
  4123. {
  4124. RemoteDirView->SaveSelection();
  4125. RemoteDirView->SaveSelectedNames();
  4126. try
  4127. {
  4128. if (Move)
  4129. {
  4130. Configuration->Usage->Inc("RemoteMove");
  4131. Terminal->MoveFiles(FileList, Target, FileMask);
  4132. }
  4133. else
  4134. {
  4135. Configuration->Usage->Inc("RemoteCopyDirect");
  4136. DebugAssert(DirectCopy);
  4137. DebugAssert(Session == Terminal);
  4138. if (Terminal->IsCapable[fcRemoteCopy] ||
  4139. Terminal->CommandSessionOpened ||
  4140. CommandSessionFallback())
  4141. {
  4142. Terminal->CopyFiles(FileList, Target, FileMask);
  4143. }
  4144. }
  4145. }
  4146. catch(...)
  4147. {
  4148. RemoteDirView->DiscardSavedSelection();
  4149. throw;
  4150. }
  4151. RemoteDirView->RestoreSelection();
  4152. }
  4153. }
  4154. return Result;
  4155. }
  4156. //---------------------------------------------------------------------------
  4157. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4158. const UnicodeString & Path, TRemoteProperties & Properties)
  4159. {
  4160. DebugAssert(!IsLocalBrowserMode());
  4161. Properties.Valid = Properties.Valid << vpEncrypt;
  4162. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4163. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4164. }
  4165. //---------------------------------------------------------------------------
  4166. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4167. {
  4168. Side = GetSide(Side);
  4169. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4170. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4171. UnicodeString Name = LoadStr(NEW_FOLDER);
  4172. int AllowedChanges =
  4173. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4174. bool SaveSettings = false;
  4175. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4176. {
  4177. TWindowLock Lock(this);
  4178. if (!IsSideLocalBrowser(Side))
  4179. {
  4180. if (SaveSettings)
  4181. {
  4182. GUIConfiguration->NewDirectoryProperties = Properties;
  4183. }
  4184. CreateRemoteDirectory(Name, Properties);
  4185. }
  4186. else
  4187. {
  4188. DirView(Side)->CreateDirectory(Name);
  4189. }
  4190. }
  4191. }
  4192. //---------------------------------------------------------------------------
  4193. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4194. {
  4195. TWindowLock Lock(this);
  4196. DirView(Side)->ExecuteHomeDirectory();
  4197. }
  4198. //---------------------------------------------------------------------------
  4199. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4200. {
  4201. DoOpenDirectoryDialog(odBrowse, Side);
  4202. }
  4203. //---------------------------------------------------------------------------
  4204. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4205. {
  4206. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4207. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4208. bool Result = !Path.IsEmpty();
  4209. if (Result)
  4210. {
  4211. TryOpenDirectory(Side, Path);
  4212. }
  4213. return Result;
  4214. }
  4215. //---------------------------------------------------------------------------
  4216. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4217. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4218. bool & Close)
  4219. {
  4220. // terminal can be already closed (e.g. dropped connection)
  4221. if (Terminal != NULL)
  4222. {
  4223. try
  4224. {
  4225. TCalculateSizeParams Params;
  4226. Params.Stats = &Stats;
  4227. Terminal->CalculateFilesSize(FileList, Size, Params);
  4228. }
  4229. catch(...)
  4230. {
  4231. if (!Terminal->Active)
  4232. {
  4233. Close = true;
  4234. }
  4235. throw;
  4236. }
  4237. }
  4238. }
  4239. //---------------------------------------------------------------------------
  4240. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4241. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4242. bool & Close)
  4243. {
  4244. if ((Terminal != NULL) && // terminal can be already closed (e.g. dropped connection)
  4245. EnsureCommandSessionFallback(fcCalculatingChecksum))
  4246. {
  4247. Configuration->Usage->Inc(L"ChecksumCalculated");
  4248. try
  4249. {
  4250. Terminal->CalculateFilesChecksum(Alg, FileList, OnCalculatedChecksum);
  4251. }
  4252. catch(...)
  4253. {
  4254. if (!Terminal->Active)
  4255. {
  4256. Close = true;
  4257. }
  4258. throw;
  4259. }
  4260. }
  4261. }
  4262. //---------------------------------------------------------------------------
  4263. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4264. {
  4265. if (Terminal->LoadFilesProperties(FileList))
  4266. {
  4267. RemoteDirView->Invalidate();
  4268. }
  4269. }
  4270. //---------------------------------------------------------------------------
  4271. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4272. {
  4273. bool Result;
  4274. if (!IsSideLocalBrowser(Side))
  4275. {
  4276. TRemoteTokenList * GroupList = NULL;
  4277. TRemoteTokenList * UserList = NULL;
  4278. try
  4279. {
  4280. TRemoteProperties CurrentProperties;
  4281. LoadFilesProperties(FileList);
  4282. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4283. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4284. if (CapableGroupChanging || CapableOwnerChanging)
  4285. {
  4286. if (CapableGroupChanging)
  4287. {
  4288. GroupList = Terminal->Groups->Duplicate();
  4289. }
  4290. if (CapableOwnerChanging)
  4291. {
  4292. UserList = Terminal->Users->Duplicate();
  4293. }
  4294. TRemoteDirectory * Files = Terminal->Files;
  4295. int Count = Files->Count;
  4296. if (Count > 100)
  4297. {
  4298. Count = 100;
  4299. }
  4300. for (int Index = 0; Index < Count; Index++)
  4301. {
  4302. TRemoteFile * File = Files->Files[Index];
  4303. if (CapableGroupChanging)
  4304. {
  4305. GroupList->AddUnique(File->Group);
  4306. }
  4307. if (CapableOwnerChanging)
  4308. {
  4309. UserList->AddUnique(File->Owner);
  4310. }
  4311. }
  4312. // if we haven't collected tokens for all files in current directory,
  4313. // make sure we collect them at least for all selected files.
  4314. // (note that so far the files in FileList has to be from current direcotry)
  4315. if (Count < Files->Count)
  4316. {
  4317. for (int Index = 0; Index < FileList->Count; Index++)
  4318. {
  4319. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4320. if (CapableGroupChanging)
  4321. {
  4322. GroupList->AddUnique(File->Group);
  4323. }
  4324. if (CapableOwnerChanging)
  4325. {
  4326. UserList->AddUnique(File->Owner);
  4327. }
  4328. }
  4329. }
  4330. }
  4331. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4332. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4333. for (int Index = 0; (Index < FileList->Count) && CapableAclChanging; Index++)
  4334. {
  4335. if (dynamic_cast<TRemoteFile *>(FileList->Objects[Index])->IsDirectory)
  4336. {
  4337. CapableAclChanging = false;
  4338. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4339. }
  4340. }
  4341. int Flags = 0;
  4342. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4343. if (CapableAclChanging) Flags |= cpAcl;
  4344. if (CapableOwnerChanging) Flags |= cpOwner;
  4345. if (CapableGroupChanging) Flags |= cpGroup;
  4346. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4347. if (Terminal->IsCapable[fcCalculatingChecksum] ||
  4348. (Terminal->IsCapable[fcSecondaryShell] && !Terminal->IsEncryptingFiles()))
  4349. {
  4350. CalculateChecksumEvent = CalculateChecksum;
  4351. }
  4352. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4353. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4354. TRemoteProperties NewProperties = CurrentProperties;
  4355. Result =
  4356. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4357. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4358. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4359. CalculateSize, CalculateChecksumEvent);
  4360. if (Result)
  4361. {
  4362. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4363. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4364. }
  4365. }
  4366. __finally
  4367. {
  4368. delete GroupList;
  4369. delete UserList;
  4370. }
  4371. }
  4372. else
  4373. {
  4374. DirView(Side)->DisplayPropertiesMenu();
  4375. Result = true;
  4376. }
  4377. return Result;
  4378. }
  4379. //---------------------------------------------------------------------------
  4380. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4381. {
  4382. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4383. {
  4384. FIgnoreNextDialogChar = Key;
  4385. }
  4386. Key = 0;
  4387. }
  4388. //---------------------------------------------------------------------------
  4389. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4390. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4391. {
  4392. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4393. if (Command != NULL)
  4394. {
  4395. KeyProcessed(Key, Shift);
  4396. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4397. {
  4398. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4399. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4400. false, false, const_cast<TCustomCommandType *>(Command));
  4401. }
  4402. }
  4403. }
  4404. //---------------------------------------------------------------------------
  4405. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4406. {
  4407. if (!NonVisualDataModule->Busy)
  4408. {
  4409. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4410. {
  4411. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4412. }
  4413. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4414. {
  4415. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4416. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4417. {
  4418. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4419. // But so far it's not a problem, as it does what we want.
  4420. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4421. if (((Action->ShortCut == KeyShortCut) ||
  4422. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4423. AllowedAction(Action, aaShortCut))
  4424. {
  4425. // Has to be called before the action as the dialog char is already in queue.
  4426. // So when the action consumes message queue, we already need to have the
  4427. // FIgnoreNextDialogChar set
  4428. KeyProcessed(Key, Shift);
  4429. // Reset reference to previous component (when menu/toolbar was clicked).
  4430. // Needed to detect that action was invoked by keyboard shortcut
  4431. // in TNonVisualDataModule::ExplorerActionsExecute
  4432. Action->ActionComponent = NULL;
  4433. Action->Execute();
  4434. return;
  4435. }
  4436. }
  4437. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4438. {
  4439. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4440. {
  4441. KeyProcessed(Key, Shift);
  4442. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4443. return;
  4444. }
  4445. }
  4446. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4447. {
  4448. KeyProcessed(Key, Shift);
  4449. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4450. }
  4451. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4452. if (IsCustomShortCut(CustomShortCut))
  4453. {
  4454. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4455. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4456. if (WinConfiguration->SharedBookmarks != NULL)
  4457. {
  4458. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4459. if ((Bookmark != NULL) &&
  4460. OpenBookmark(FCurrentSide, Bookmark))
  4461. {
  4462. KeyProcessed(Key, Shift);
  4463. }
  4464. }
  4465. }
  4466. }
  4467. }
  4468. TForm::KeyDown(Key, Shift);
  4469. }
  4470. //---------------------------------------------------------------------------
  4471. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4472. {
  4473. bool OpenedTerminal = false;
  4474. UnicodeString ProtocolName;
  4475. UnicodeString ProtocolNameHint;
  4476. UnicodeString SecurityProtocolName;
  4477. UnicodeString DurationHint;
  4478. // See the comment in TScpCommanderForm::UpdateControls()
  4479. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4480. if (ActiveTerminal != NULL)
  4481. {
  4482. // Active is not enough here
  4483. if (ActiveTerminal->Status == ssOpened)
  4484. {
  4485. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4486. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4487. if (FileSystemInfo.ProtocolName.IsEmpty())
  4488. {
  4489. ProtocolName = SessionInfo.ProtocolName;
  4490. }
  4491. else
  4492. {
  4493. ProtocolName = FileSystemInfo.ProtocolName;
  4494. }
  4495. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4496. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4497. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4498. OpenedTerminal = true;
  4499. }
  4500. else if (ActiveTerminal->Status > ssClosed)
  4501. {
  4502. ProtocolName = LoadStr(STATUS_CONNECTING);
  4503. }
  4504. else
  4505. {
  4506. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4507. }
  4508. }
  4509. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4510. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4511. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4512. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4513. if (ProtocolStatusPanel->Caption != ProtocolName)
  4514. {
  4515. ProtocolStatusPanel->Caption = ProtocolName;
  4516. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4517. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4518. }
  4519. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4520. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4521. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4522. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4523. SecurityStatusPanel->Enabled = SecurityEnabled;
  4524. UnicodeString SecurityProtocolHint;
  4525. if (SecurityEnabled)
  4526. {
  4527. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4528. }
  4529. else
  4530. {
  4531. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4532. }
  4533. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4534. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4535. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4536. DurationStatusPanel->Hint = DurationHint;
  4537. UpdateStatusBar();
  4538. }
  4539. //---------------------------------------------------------------------------
  4540. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4541. {
  4542. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4543. DebugAssert(SessionStatusBar != NULL);
  4544. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4545. if (!FNote.IsEmpty())
  4546. {
  4547. NoteStatusPanel->Caption = FNote;
  4548. }
  4549. else
  4550. {
  4551. UpdateStatusPanelText(NoteStatusPanel);
  4552. }
  4553. NoteStatusPanel->Hint = FNoteHints;
  4554. UnicodeString LoginTime;
  4555. // See the comment in TScpCommanderForm::UpdateControls()
  4556. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4557. // Active is not enough here
  4558. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4559. {
  4560. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4561. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4562. }
  4563. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4564. DurationStatusPanel->Caption = LoginTime;
  4565. }
  4566. //---------------------------------------------------------------------------
  4567. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4568. {
  4569. Panel->Caption = L"";
  4570. }
  4571. //---------------------------------------------------------------------------
  4572. void __fastcall TCustomScpExplorerForm::Idle()
  4573. {
  4574. if (FShowing ||
  4575. // Particularly for
  4576. // - Standalone editing
  4577. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4578. // while "keeping remote directory up to date".
  4579. StandaloneOperation)
  4580. {
  4581. FEditorManager->Check();
  4582. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4583. // that needs to know if queue view is visible (and it may be closed after queue update)
  4584. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4585. }
  4586. if (FShowing)
  4587. {
  4588. if (!NonVisualDataModule->Busy &&
  4589. // Menu is opened or drag&drop is going on
  4590. (Mouse->Capture == NULL))
  4591. {
  4592. if (FRefreshRemoteDirectory)
  4593. {
  4594. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4595. {
  4596. Terminal->RefreshDirectory();
  4597. }
  4598. FRefreshRemoteDirectory = false;
  4599. }
  4600. if (FRefreshLocalDirectory)
  4601. {
  4602. ReloadLocalDirectory();
  4603. FRefreshLocalDirectory = false;
  4604. }
  4605. if (WinConfiguration->RefreshRemotePanel)
  4606. {
  4607. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4608. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4609. {
  4610. RemoteDirView->ReloadDirectory();
  4611. }
  4612. }
  4613. }
  4614. }
  4615. if (FShowing || StandaloneOperation)
  4616. {
  4617. if (FQueueStatusInvalidated)
  4618. {
  4619. UpdateQueueStatus(false);
  4620. }
  4621. RefreshQueueItems();
  4622. }
  4623. if (FShowing)
  4624. {
  4625. UpdateStatusBar();
  4626. }
  4627. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4628. {
  4629. FClipboardFakeMonitorsPendingReset = false;
  4630. FClipboardFakeMonitors.reset(NULL);
  4631. }
  4632. FIgnoreNextDialogChar = 0;
  4633. }
  4634. //---------------------------------------------------------------------------
  4635. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4636. {
  4637. try
  4638. {
  4639. FUserActionTimer->Enabled = false;
  4640. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4641. {
  4642. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4643. {
  4644. FPendingQueueActionItem->ProcessUserAction();
  4645. }
  4646. }
  4647. }
  4648. __finally
  4649. {
  4650. FPendingQueueActionItem = NULL;
  4651. }
  4652. }
  4653. //---------------------------------------------------------------------------
  4654. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4655. {
  4656. if (WinConfiguration->MinimizeToTray)
  4657. {
  4658. UpdateTrayIcon();
  4659. FTrayIcon->Visible = true;
  4660. if (Visible)
  4661. {
  4662. ShowWindow(Handle, SW_HIDE);
  4663. }
  4664. }
  4665. }
  4666. //---------------------------------------------------------------------------
  4667. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4668. {
  4669. // WORKAROUND
  4670. // When restoring maximized window from minimization,
  4671. // rarely some controls do not align properly.
  4672. // Two instances seen (both for Commander):
  4673. // - When restoring, window is temporarily narrower (not maximized),
  4674. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4675. // Once maximized already, top dock shinks back, but the session PageControl,
  4676. // do not align up, leaving space between TopDock and PageControl.
  4677. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4678. for (int Index = 0; Index < ControlCount; Index++)
  4679. {
  4680. RealignControl(Controls[Index]);
  4681. }
  4682. if (FTrayIcon->Visible)
  4683. {
  4684. FTrayIcon->Visible = false;
  4685. if (Visible)
  4686. {
  4687. ShowWindow(Handle, SW_SHOW);
  4688. }
  4689. }
  4690. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4691. {
  4692. FNeedSession = false;
  4693. NonVisualDataModule->StartBusy();
  4694. try
  4695. {
  4696. NeedSession(true);
  4697. }
  4698. __finally
  4699. {
  4700. NonVisualDataModule->EndBusy();
  4701. }
  4702. }
  4703. }
  4704. //---------------------------------------------------------------------------
  4705. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4706. {
  4707. FTrayIcon->Hint = GetMainForm()->Caption;
  4708. }
  4709. //---------------------------------------------------------------------------
  4710. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4711. {
  4712. UpdateTrayIcon();
  4713. }
  4714. //---------------------------------------------------------------------------
  4715. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4716. {
  4717. // workaround
  4718. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4719. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4720. // But WM_ACTIVATEAPP message can be received even when
  4721. // the main window is minimized to the tray and internal editor window is focused
  4722. // (after another application was previously active)
  4723. if (::IsIconic(Handle))
  4724. {
  4725. if (!IsAppIconic())
  4726. {
  4727. SetAppIconic(true);
  4728. }
  4729. }
  4730. ::ApplicationRestore();
  4731. Application->BringToFront();
  4732. }
  4733. //---------------------------------------------------------------------------
  4734. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4735. {
  4736. RestoreApp();
  4737. }
  4738. //---------------------------------------------------------------------------
  4739. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4740. {
  4741. if (OpenInNewWindow())
  4742. {
  4743. ExecuteNewInstance(SessionUrl);
  4744. }
  4745. else
  4746. {
  4747. TTerminalManager::Instance()->NewSession(SessionUrl);
  4748. }
  4749. }
  4750. //---------------------------------------------------------------------------
  4751. void TCustomScpExplorerForm::NewTab(TOperationSide)
  4752. {
  4753. NewSession();
  4754. }
  4755. //---------------------------------------------------------------------------
  4756. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4757. {
  4758. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4759. StoredSessions->NewSession(SessionName, SessionData);
  4760. // modified only, explicit
  4761. StoredSessions->Save(false, true);
  4762. // encode session name because of slashes in hierarchical sessions
  4763. return EncodeUrlString(SessionName);
  4764. }
  4765. //---------------------------------------------------------------------------
  4766. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4767. {
  4768. // current working directories become defaults here, what is not right
  4769. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4770. return SaveHiddenDuplicateSession(SessionData.get());
  4771. }
  4772. //---------------------------------------------------------------------------
  4773. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4774. {
  4775. if (OpenInNewWindow())
  4776. {
  4777. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4778. }
  4779. else
  4780. {
  4781. // current working directories become defaults here, what is not right
  4782. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4783. TTerminalManager * Manager = TTerminalManager::Instance();
  4784. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4785. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4786. // We definitelly want these
  4787. ASession->Disconnected = ManagedSession->Disconnected;
  4788. // Not sure about these two
  4789. ASession->Permanent = ManagedSession->Permanent;
  4790. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4791. Manager->ActiveSession = ASession;
  4792. }
  4793. }
  4794. //---------------------------------------------------------------------------
  4795. void __fastcall TCustomScpExplorerForm::RenameSession()
  4796. {
  4797. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4798. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME) &&
  4799. // When submitting the default name, do not fix it
  4800. (Name != ManagedSession->SessionData->SessionName))
  4801. {
  4802. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4803. TSessionData::ValidateName(Name);
  4804. // This is inconsistent with how color (for example) is handled.
  4805. ManagedSession->SessionData->Name = Name;
  4806. UpdateControls();
  4807. // Add/Remove distinguishing paths from sessions of the same name.
  4808. SessionListChanged();
  4809. }
  4810. }
  4811. //---------------------------------------------------------------------------
  4812. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4813. {
  4814. DebugAssert(Queue != NULL);
  4815. bool Result = Queue->IsEmpty;
  4816. if (!Result)
  4817. {
  4818. SetFocus();
  4819. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4820. }
  4821. return Result;
  4822. }
  4823. //---------------------------------------------------------------------------
  4824. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4825. {
  4826. return CanCloseQueue(FQueue);
  4827. }
  4828. //---------------------------------------------------------------------------
  4829. void __fastcall TCustomScpExplorerForm::CloseSession()
  4830. {
  4831. if (CanCloseQueue())
  4832. {
  4833. TTerminalManager::Instance()->FreeActiveTerminal();
  4834. }
  4835. }
  4836. //---------------------------------------------------------------------------
  4837. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4838. {
  4839. if (CanCloseQueue())
  4840. {
  4841. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4842. }
  4843. }
  4844. //---------------------------------------------------------------------------
  4845. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4846. {
  4847. DetachTerminal(Terminal);
  4848. RemoteDirView->Terminal = Terminal;
  4849. UpdateRemotePathComboBox(false);
  4850. InitStatusBar();
  4851. UpdateControls();
  4852. }
  4853. //---------------------------------------------------------------------------
  4854. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4855. {
  4856. InitStatusBar();
  4857. }
  4858. //---------------------------------------------------------------------------
  4859. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4860. {
  4861. if (DebugAlwaysTrue(!Terminal->Active))
  4862. {
  4863. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4864. }
  4865. }
  4866. //---------------------------------------------------------------------------
  4867. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4868. {
  4869. if (OpenInNewWindow())
  4870. {
  4871. // encode session name because of slashes in hierarchical sessions
  4872. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4873. }
  4874. else
  4875. {
  4876. TTerminalManager * Manager = TTerminalManager::Instance();
  4877. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4878. Manager->ActiveSession = Terminal;
  4879. }
  4880. }
  4881. //---------------------------------------------------------------------------
  4882. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  4883. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  4884. {
  4885. TTerminalManager * Manager = TTerminalManager::Instance();
  4886. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4887. // This asks for a master password. Maybe we should postpone asking until a connection is actually opened.
  4888. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4889. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  4890. {
  4891. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  4892. // FirstSession can be null, if some of the
  4893. if (!ConnectFirstTerminal && (FirstSession != NULL))
  4894. {
  4895. FirstSession->Disconnected = true;
  4896. FirstSession->DisconnectedTemporarily = true;
  4897. }
  4898. Manager->ActiveSession = FirstSession;
  4899. }
  4900. }
  4901. //---------------------------------------------------------------------------
  4902. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4903. {
  4904. if (OpenInNewWindow())
  4905. {
  4906. ExecuteNewInstance(Name);
  4907. }
  4908. else
  4909. {
  4910. DoOpenFolderOrWorkspace(Name, true, false);
  4911. }
  4912. }
  4913. //---------------------------------------------------------------------------
  4914. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4915. bool &CanClose)
  4916. {
  4917. // See the comment in CloseApp()
  4918. if (NonVisualDataModule->Busy)
  4919. {
  4920. CanClose = false;
  4921. }
  4922. else
  4923. {
  4924. TTerminalManager * Manager = TTerminalManager::Instance();
  4925. int ActiveSessions = 0;
  4926. for (int Index = 0; Index < Manager->Count; Index++)
  4927. {
  4928. if (Manager->Sessions[Index]->Active)
  4929. {
  4930. ActiveSessions++;
  4931. }
  4932. }
  4933. // Confirm, when there's at least one active remote session or
  4934. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4935. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4936. WinConfiguration->ConfirmClosingSession)
  4937. {
  4938. unsigned int Result;
  4939. TMessageParams Params(mpNeverAskAgainCheck);
  4940. UnicodeString Message;
  4941. int Answers = qaOK | qaCancel;
  4942. // The current session is the only active session
  4943. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4944. {
  4945. if (!WinConfiguration->AutoSaveWorkspace)
  4946. {
  4947. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4948. }
  4949. else
  4950. {
  4951. Message = LoadStr(CLOSE_SESSION);
  4952. }
  4953. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4954. }
  4955. // Multiple active sessions or one active session, but it's not the current one
  4956. else if (ActiveSessions > 0)
  4957. {
  4958. if (!WinConfiguration->AutoSaveWorkspace)
  4959. {
  4960. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4961. }
  4962. else
  4963. {
  4964. Message = LoadStr(CLOSE_SESSIONS);
  4965. }
  4966. }
  4967. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  4968. else
  4969. {
  4970. DebugAlwaysTrue(Manager->Count > 1);
  4971. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  4972. Message = LoadStr(CLOSE_WORKSPACE);
  4973. }
  4974. UnicodeString Note;
  4975. if (WinConfiguration->AutoSaveWorkspace)
  4976. {
  4977. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4978. }
  4979. else
  4980. {
  4981. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4982. Answers = qaYes | qaNo | qaCancel;
  4983. }
  4984. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4985. SetFocus();
  4986. Result = MessageDialog(Message, qtConfirmation,
  4987. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4988. if (Result == qaNeverAskAgain)
  4989. {
  4990. WinConfiguration->ConfirmClosingSession = false;
  4991. }
  4992. if (Result == qaNo)
  4993. {
  4994. CanClose = SaveWorkspace(true);
  4995. // note that the workspace will be saved redundatly again from FormClose
  4996. }
  4997. else
  4998. {
  4999. CanClose =
  5000. (Result == qaOK) ||
  5001. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  5002. (Result == qaNeverAskAgain);
  5003. }
  5004. }
  5005. if (CanClose && HasActiveTerminal())
  5006. {
  5007. CanClose = CanCloseQueue();
  5008. }
  5009. }
  5010. if (CanClose)
  5011. {
  5012. CanClose =
  5013. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  5014. FEditorManager->CloseExternalFilesWithoutProcess();
  5015. if (CanClose)
  5016. {
  5017. while (CanClose && (FLocalEditors->Count > 0))
  5018. {
  5019. int PrevCount = FLocalEditors->Count;
  5020. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  5021. CanClose = (FLocalEditors->Count < PrevCount);
  5022. }
  5023. if (CanClose)
  5024. {
  5025. CanClose = FEditorManager->Empty(true);
  5026. if (!CanClose)
  5027. {
  5028. SetFocus();
  5029. CanClose =
  5030. (MessageDialog(
  5031. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  5032. }
  5033. }
  5034. }
  5035. }
  5036. }
  5037. //---------------------------------------------------------------------------
  5038. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  5039. {
  5040. TForm * Form = dynamic_cast<TForm *>(Sender);
  5041. DebugAssert(Form != NULL);
  5042. Form->Close();
  5043. }
  5044. //---------------------------------------------------------------------------
  5045. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  5046. {
  5047. TForm * Form = dynamic_cast<TForm *>(Sender);
  5048. delete Form;
  5049. }
  5050. //---------------------------------------------------------------------------
  5051. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  5052. {
  5053. while (FLocalEditors->Count > 0)
  5054. {
  5055. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  5056. }
  5057. }
  5058. //---------------------------------------------------------------------------
  5059. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  5060. TObject *Sender)
  5061. {
  5062. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  5063. try
  5064. {
  5065. SetProperties(osRemote, FileList);
  5066. }
  5067. __finally
  5068. {
  5069. delete FileList;
  5070. }
  5071. }
  5072. //---------------------------------------------------------------------------
  5073. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  5074. {
  5075. if (value)
  5076. {
  5077. if (Component == fcCustomCommandsBand)
  5078. {
  5079. UpdateCustomCommandsToolbar();
  5080. }
  5081. if (Component == fcQueueFileList)
  5082. {
  5083. DebugAssert(!QueueFileListSplitter->Visible);
  5084. AdjustQueueLayout();
  5085. UpdateQueueFileList();
  5086. }
  5087. }
  5088. }
  5089. //---------------------------------------------------------------------------
  5090. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  5091. {
  5092. int MinHeightLimit =
  5093. GetStaticQueuePanelComponentsHeight() +
  5094. QueueFileListSplitter->Height +
  5095. QueueFileList->Height +
  5096. GetMinQueueViewHeight();
  5097. if (QueuePanel->ClientHeight < MinHeightLimit)
  5098. {
  5099. QueuePanel->ClientHeight = MinHeightLimit;
  5100. }
  5101. }
  5102. //---------------------------------------------------------------------------
  5103. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  5104. {
  5105. TControl * Control = GetComponent(Component);
  5106. DebugAssert(Control);
  5107. bool Changed = (Control->Visible != value);
  5108. if (Changed)
  5109. {
  5110. ComponentShowing(Component, value);
  5111. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  5112. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  5113. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  5114. if (value)
  5115. {
  5116. int RemainingHeight = Control->Parent->ClientHeight;
  5117. int RemainingWidth = Control->Parent->ClientWidth;
  5118. for (int i = 0; i < Control->Parent->ControlCount; i++)
  5119. {
  5120. TControl * ChildControl = Control->Parent->Controls[i];
  5121. if (ChildControl->Visible)
  5122. {
  5123. switch (ChildControl->Align)
  5124. {
  5125. case alTop:
  5126. case alBottom:
  5127. RemainingHeight -= ChildControl->Height;
  5128. break;
  5129. case alLeft:
  5130. case alRight:
  5131. RemainingWidth -= ChildControl->Width;
  5132. break;
  5133. }
  5134. }
  5135. }
  5136. int Reserve = ScaleByTextHeight(this, 32);
  5137. // queue in explorer, trees in commander
  5138. if (Control->Height > RemainingHeight - Reserve)
  5139. {
  5140. Control->Height = RemainingHeight / 2;
  5141. }
  5142. if (Control->Width > RemainingWidth - Reserve)
  5143. {
  5144. Control->Width = RemainingWidth / 2;
  5145. }
  5146. }
  5147. Control->Visible = value;
  5148. if (WasFocused && Visible)
  5149. {
  5150. DirView(osCurrent)->SetFocus();
  5151. }
  5152. FixControlsPlacement();
  5153. }
  5154. }
  5155. //---------------------------------------------------------------------------
  5156. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  5157. {
  5158. TControl * Control = GetComponent(Component);
  5159. if (Control == NULL)
  5160. {
  5161. return false;
  5162. }
  5163. else
  5164. {
  5165. return Control->Visible;
  5166. }
  5167. }
  5168. //---------------------------------------------------------------------------
  5169. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  5170. {
  5171. bool Result;
  5172. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  5173. {
  5174. Result = !IsUWP();
  5175. }
  5176. else
  5177. {
  5178. Result = true;
  5179. }
  5180. return Result;
  5181. }
  5182. //---------------------------------------------------------------------------
  5183. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  5184. {
  5185. if (DirView->ItemFocused != NULL)
  5186. {
  5187. DirView->ItemFocused->MakeVisible(false);
  5188. }
  5189. }
  5190. //---------------------------------------------------------------------------
  5191. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5192. {
  5193. MakeFocusedItemVisible(RemoteDirView);
  5194. QueueSplitter->Visible = QueuePanel->Visible;
  5195. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5196. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5197. TControl * TopControlsOrder[] =
  5198. { TopDock, MessageDock, SessionsPageControl };
  5199. SetVerticalControlsOrder(TopControlsOrder, LENOF(TopControlsOrder));
  5200. TControl * QueueControlsOrder[] =
  5201. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5202. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5203. }
  5204. //---------------------------------------------------------------------------
  5205. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5206. {
  5207. switch (Component) {
  5208. case fcStatusBar: return RemoteStatusBar;
  5209. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5210. case fcQueueView: return QueuePanel;
  5211. case fcQueueToolbar: return QueueDock;
  5212. case fcRemoteTree: return RemoteDrivePanel;
  5213. case fcSessionsTabs: return SessionsPageControl;
  5214. case fcQueueFileList: return QueueFileList;
  5215. default: return NULL;
  5216. }
  5217. }
  5218. //---------------------------------------------------------------------------
  5219. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5220. TObject *Sender, TListColumn *Column, TPoint &Point)
  5221. {
  5222. DebugAssert(NonVisualDataModule && Column && Sender);
  5223. NonVisualDataModule->ListColumn = Column;
  5224. TPopupMenu * DirViewColumnMenu;
  5225. if (Sender == DirView(osOther))
  5226. {
  5227. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5228. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  5229. (Column->Index == uvName);
  5230. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  5231. (Column->Index == uvSize);
  5232. }
  5233. else
  5234. {
  5235. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5236. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  5237. (Column->Index == dvName);
  5238. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  5239. (Column->Index == dvSize);
  5240. }
  5241. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5242. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5243. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5244. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5245. }
  5246. //---------------------------------------------------------------------------
  5247. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5248. TObject *Sender, TListItem *Item, bool &AllowExec)
  5249. {
  5250. DoDirViewExecFile(Sender, Item, AllowExec);
  5251. }
  5252. //---------------------------------------------------------------------------
  5253. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5254. TListItem * Item, bool & AllowExec)
  5255. {
  5256. DebugAssert(Sender && Item && Configuration);
  5257. DebugAssert(AllowExec);
  5258. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5259. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5260. bool Remote = !IsSideLocalBrowser(Side);
  5261. DebugAssert(Item == ADirView->ItemFocused);
  5262. bool IsDirectory = ADirView->ItemIsDirectory(Item);
  5263. TResolvedDoubleClickAction Action;
  5264. if (FForceExecution)
  5265. {
  5266. Action = IsDirectory ? rdcaChangeDir : rdcaOpen;
  5267. }
  5268. else
  5269. {
  5270. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  5271. Action = WinConfiguration->ResolveDoubleClickAction(IsDirectory, ATerminal);
  5272. }
  5273. if (Action == rdcaCopy)
  5274. {
  5275. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5276. UnlockWindow();
  5277. try
  5278. {
  5279. if (IsLocalBrowserMode())
  5280. {
  5281. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5282. }
  5283. else
  5284. {
  5285. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5286. }
  5287. }
  5288. __finally
  5289. {
  5290. LockWindow();
  5291. }
  5292. AllowExec = false;
  5293. }
  5294. else if (Action == rdcaEdit)
  5295. {
  5296. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5297. {
  5298. ExecuteFile(Side, efDefaultEditor);
  5299. AllowExec = false;
  5300. }
  5301. }
  5302. else if (Action == rdcaOpen)
  5303. {
  5304. if (DebugAlwaysTrue(!IsDirectory) && Remote && !WinConfiguration->DisableOpenEdit)
  5305. {
  5306. ExecuteFile(osRemote, efShell);
  5307. AllowExec = false;
  5308. }
  5309. }
  5310. }
  5311. //---------------------------------------------------------------------------
  5312. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5313. {
  5314. return ((Side == osRemote) || (Side == osCurrent));
  5315. }
  5316. //---------------------------------------------------------------------------
  5317. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5318. {
  5319. DebugFail();
  5320. }
  5321. //---------------------------------------------------------------------------
  5322. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5323. {
  5324. DebugFail();
  5325. }
  5326. //---------------------------------------------------------------------------
  5327. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5328. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5329. {
  5330. TSynchronizeParamType Params;
  5331. Params.LocalDirectory = LocalDirectory;
  5332. Params.RemoteDirectory = RemoteDirectory;
  5333. int UnusedParams =
  5334. (GUIConfiguration->SynchronizeParams &
  5335. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5336. TTerminal::spCaseSensitive));
  5337. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5338. Params.Options = GUIConfiguration->SynchronizeOptions;
  5339. bool SaveSettings = false;
  5340. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5341. &DoSynchronizeTooManyDirectories);
  5342. DebugAssert(FSynchronizeController == NULL);
  5343. FSynchronizeController = &Controller;
  5344. bool Result;
  5345. try
  5346. {
  5347. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5348. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5349. int Options =
  5350. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5351. DebugAssert(FOnFeedSynchronizeError == NULL);
  5352. DebugAssert(FOnSynchronizeAbort == NULL);
  5353. DebugAssert(FSynchronizeTerminal == NULL);
  5354. FSynchronizeTerminal = Terminal;
  5355. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5356. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5357. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5358. if (Result)
  5359. {
  5360. if (SaveSettings)
  5361. {
  5362. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5363. GUIConfiguration->SynchronizeOptions = Params.Options;
  5364. }
  5365. else
  5366. {
  5367. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5368. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5369. FLAGSET(Params.Options, soSynchronize))
  5370. {
  5371. GUIConfiguration->SynchronizeOptions =
  5372. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5373. soSynchronize;
  5374. }
  5375. }
  5376. LocalDirectory = Params.LocalDirectory;
  5377. RemoteDirectory = Params.RemoteDirectory;
  5378. }
  5379. }
  5380. __finally
  5381. {
  5382. FSynchronizeController = NULL;
  5383. DebugAssert(FOnFeedSynchronizeError == NULL);
  5384. FOnFeedSynchronizeError = NULL;
  5385. DebugAssert(FOnSynchronizeAbort == NULL);
  5386. FOnSynchronizeAbort = NULL;
  5387. FSynchronizeTerminal = NULL;
  5388. }
  5389. return Result;
  5390. }
  5391. //---------------------------------------------------------------------------
  5392. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5393. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5394. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5395. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5396. TSynchronizeOptions * Options, bool Full)
  5397. {
  5398. // Keep-up-to-date mode
  5399. if (Terminal->Status == ssOpened)
  5400. {
  5401. try
  5402. {
  5403. int PParams = Params.Params;
  5404. if (!Full)
  5405. {
  5406. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5407. if (FLAGSET(Params.Options, soRecurse))
  5408. {
  5409. PParams |= TTerminal::spSubDirs;
  5410. }
  5411. }
  5412. else
  5413. {
  5414. // if keepuptodate is non-recursive,
  5415. // full sync before has to be non-recursive as well
  5416. if (FLAGCLEAR(Params.Options, soRecurse))
  5417. {
  5418. PParams |= TTerminal::spNoRecurse;
  5419. }
  5420. }
  5421. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5422. PParams, Checklist, Options);
  5423. }
  5424. catch (Exception & E)
  5425. {
  5426. if (!Terminal->Active)
  5427. {
  5428. ShowExtendedExceptionEx(Terminal, &E);
  5429. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5430. if (!Terminal->Active)
  5431. {
  5432. throw;
  5433. }
  5434. }
  5435. else
  5436. {
  5437. if (FLAGSET(Params.Options, soContinueOnError) &&
  5438. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5439. {
  5440. // noop, already logged in MoreMessageDialog
  5441. }
  5442. else
  5443. {
  5444. // We get mostly EAbort here, so this is noop.
  5445. // Exception is an error when listing directories while looking for differences.
  5446. ShowExtendedExceptionEx(Terminal, &E);
  5447. throw;
  5448. }
  5449. }
  5450. }
  5451. }
  5452. }
  5453. //---------------------------------------------------------------------------
  5454. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5455. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5456. const UnicodeString ErrorStr)
  5457. {
  5458. if (!Directory.IsEmpty())
  5459. {
  5460. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5461. }
  5462. else
  5463. {
  5464. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5465. }
  5466. }
  5467. //---------------------------------------------------------------------------
  5468. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5469. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5470. {
  5471. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5472. {
  5473. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5474. }
  5475. else
  5476. {
  5477. TMessageParams Params(mpNeverAskAgainCheck);
  5478. unsigned int Result = MessageDialog(
  5479. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5480. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5481. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5482. {
  5483. MaxDirectories *= 2;
  5484. if (Result == qaNeverAskAgain)
  5485. {
  5486. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5487. }
  5488. }
  5489. else
  5490. {
  5491. Abort();
  5492. }
  5493. }
  5494. }
  5495. //---------------------------------------------------------------------------
  5496. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5497. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5498. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5499. TSynchronizeOptions * Options)
  5500. {
  5501. // Keep-up-to-date mode
  5502. DebugAssert(!FAutoOperation);
  5503. void * BatchStorage;
  5504. BatchStart(BatchStorage);
  5505. FAutoOperation = true;
  5506. bool AnyOperation = false;
  5507. TDateTime StartTime = Now();
  5508. TSynchronizeChecklist * AChecklist = NULL;
  5509. try
  5510. {
  5511. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5512. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5513. {
  5514. FSynchronizeProgressForm->Start();
  5515. }
  5516. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5517. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5518. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5519. Options);
  5520. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5521. SAFE_DESTROY(FSynchronizeProgressForm);
  5522. AnyOperation = (AChecklist->CheckedCount > 0);
  5523. if (AnyOperation)
  5524. {
  5525. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5526. CreateProgressForm(&SynchronizeProgress);
  5527. try
  5528. {
  5529. Terminal->SynchronizeApply(
  5530. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5531. SynchronizeProcessedItem, NULL, NULL, NULL);
  5532. }
  5533. __finally
  5534. {
  5535. DestroyProgressForm();
  5536. }
  5537. }
  5538. }
  5539. __finally
  5540. {
  5541. if (Checklist == NULL)
  5542. {
  5543. delete AChecklist;
  5544. }
  5545. else
  5546. {
  5547. *Checklist = AChecklist;
  5548. }
  5549. FAutoOperation = false;
  5550. SAFE_DESTROY(FSynchronizeProgressForm);
  5551. BatchEnd(BatchStorage);
  5552. ReloadLocalDirectory();
  5553. if (AnyOperation)
  5554. {
  5555. OperationComplete(StartTime);
  5556. }
  5557. }
  5558. }
  5559. //---------------------------------------------------------------------------
  5560. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5561. {
  5562. // can be called from command line
  5563. return Visible &&
  5564. ((DirView(osRemote)->SelCount > 0) ||
  5565. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5566. }
  5567. //---------------------------------------------------------------------------
  5568. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5569. {
  5570. LogSynchronizeEvent(Terminal, Message);
  5571. }
  5572. //---------------------------------------------------------------------------
  5573. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5574. int Params, TSynchronizeOptions & Options)
  5575. {
  5576. DebugAssert(!IsLocalBrowserMode());
  5577. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5578. {
  5579. Options.Filter = new TStringList();
  5580. Options.Filter->CaseSensitive = false;
  5581. Options.Filter->Duplicates = Types::dupAccept;
  5582. if (DirView(osRemote)->SelCount > 0)
  5583. {
  5584. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5585. }
  5586. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5587. {
  5588. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5589. }
  5590. Options.Filter->Sort();
  5591. }
  5592. }
  5593. //---------------------------------------------------------------------------
  5594. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5595. {
  5596. TCopyParamType Defaults;
  5597. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5598. ConfigStorage->AccessMode = smRead;
  5599. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5600. {
  5601. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5602. }
  5603. ConfigStorage.reset(NULL);
  5604. std::unique_ptr<TStringList> Options(new TStringList());
  5605. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5606. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5607. UnicodeString Result;
  5608. if (Options->Count > 0)
  5609. {
  5610. Result =
  5611. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5612. }
  5613. return Result;
  5614. }
  5615. //---------------------------------------------------------------------------
  5616. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5617. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5618. {
  5619. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5620. UnicodeString AdditionalParams =
  5621. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5622. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5623. SerializeCopyParamForCommandLine(CopyParams),
  5624. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5625. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5626. ExecuteNewInstance(SessionName, AdditionalParams);
  5627. }
  5628. //---------------------------------------------------------------------------
  5629. struct TSynchronizeParams
  5630. {
  5631. UnicodeString LocalDirectory;
  5632. UnicodeString RemoteDirectory;
  5633. TSynchronizeMode Mode;
  5634. TSynchronizeChecklist * Checklist;
  5635. int Params;
  5636. TCopyParamType * CopyParam;
  5637. TDateTime CollectElapsed;
  5638. TDateTime * StartTime;
  5639. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5640. };
  5641. //---------------------------------------------------------------------------
  5642. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5643. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5644. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5645. {
  5646. DebugAssert(!FAutoOperation);
  5647. void * BatchStorage;
  5648. BatchStart(BatchStorage);
  5649. FAutoOperation = true;
  5650. TFileOperationStatistics Statistics;
  5651. TDateTime Start = Now();
  5652. try
  5653. {
  5654. Params.OnProcessedItem = OnProcessedItem;
  5655. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5656. CreateProgressForm(&SynchronizeProgress);
  5657. Terminal->SynchronizeApply(
  5658. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5659. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5660. &Statistics);
  5661. }
  5662. __finally
  5663. {
  5664. Params.OnProcessedItem = NULL;
  5665. FAutoOperation = false;
  5666. DestroyProgressForm();
  5667. BatchEnd(BatchStorage);
  5668. ReloadLocalDirectory();
  5669. }
  5670. if (WinConfiguration->SynchronizeSummary)
  5671. {
  5672. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5673. // The statistics should be 0 anyway in this case
  5674. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5675. {
  5676. Message += L"\n";
  5677. if (Statistics.FilesUploaded > 0)
  5678. {
  5679. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5680. }
  5681. if (Statistics.FilesDownloaded > 0)
  5682. {
  5683. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5684. }
  5685. if (Statistics.FilesDeletedLocal > 0)
  5686. {
  5687. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5688. }
  5689. if (Statistics.FilesDeletedRemote > 0)
  5690. {
  5691. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5692. }
  5693. }
  5694. TDateTime Elapsed = (Now() - Start);
  5695. Message +=
  5696. L"\n" +
  5697. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5698. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5699. TMessageParams Params(mpNeverAskAgainCheck);
  5700. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5701. if (Result == qaNeverAskAgain)
  5702. {
  5703. WinConfiguration->SynchronizeSummary = false;
  5704. }
  5705. }
  5706. }
  5707. //---------------------------------------------------------------------------
  5708. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5709. {
  5710. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5711. {
  5712. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5713. }
  5714. // Not set in keep-up-to-date mode - Synchronize() method
  5715. if (Token != NULL)
  5716. {
  5717. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5718. if (Params.OnProcessedItem != NULL)
  5719. {
  5720. Params.OnProcessedItem(NULL, ChecklistItem);
  5721. }
  5722. }
  5723. }
  5724. //---------------------------------------------------------------------------
  5725. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5726. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5727. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5728. {
  5729. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5730. *Params.StartTime = Now();
  5731. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5732. }
  5733. //---------------------------------------------------------------------------
  5734. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5735. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5736. {
  5737. // terminal can be already closed (e.g. dropped connection)
  5738. if (Terminal != NULL)
  5739. {
  5740. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5741. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5742. }
  5743. }
  5744. //---------------------------------------------------------------------------
  5745. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5746. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5747. {
  5748. DebugAssert(!IsLocalBrowserMode());
  5749. TAutoBatch AutoBatch(this);
  5750. TAutoFlag AutoOperationFlag(FAutoOperation);
  5751. if (Side == osRemote)
  5752. {
  5753. std::unique_ptr<TStrings> FileList(new TStringList());
  5754. FileList->AddObject(FileName, RemoteFile);
  5755. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5756. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5757. RemoteDirView->SaveSelection();
  5758. RemoteDirView->SaveSelectedNames();
  5759. try
  5760. {
  5761. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5762. }
  5763. catch(...)
  5764. {
  5765. RemoteDirView->DiscardSavedSelection();
  5766. throw;
  5767. }
  5768. RemoteDirView->RestoreSelection();
  5769. }
  5770. else if (DebugAlwaysTrue(Side == osLocal))
  5771. {
  5772. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5773. {
  5774. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5775. }
  5776. UnicodeString Directory = ExtractFileDir(FileName);
  5777. ReloadLocalDirectory(Directory);
  5778. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5779. if (!SamePaths(Directory, NewDirectory))
  5780. {
  5781. ReloadLocalDirectory(NewDirectory);
  5782. }
  5783. }
  5784. }
  5785. //---------------------------------------------------------------------------
  5786. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5787. {
  5788. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5789. if (Side == osLocal)
  5790. {
  5791. if (Action == TSynchronizeChecklist::saDownloadNew)
  5792. {
  5793. OpenFolderInExplorer(LocalPath);
  5794. }
  5795. else
  5796. {
  5797. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5798. }
  5799. }
  5800. else if (DebugAlwaysTrue(Side == osRemote))
  5801. {
  5802. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5803. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5804. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5805. if (!LocalPath.IsEmpty())
  5806. {
  5807. SessionData->LocalDirectory = LocalPath;
  5808. }
  5809. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5810. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5811. }
  5812. }
  5813. //---------------------------------------------------------------------------
  5814. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5815. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5816. const TCopyParamType * CopyParams)
  5817. {
  5818. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5819. UnicodeString AdditionalParams =
  5820. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5821. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5822. SerializeCopyParamForCommandLine(CopyParams),
  5823. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5824. LocalDirectory, RemoteDirectory, Mode, Params));
  5825. ExecuteNewInstance(SessionName, AdditionalParams);
  5826. }
  5827. //---------------------------------------------------------------------------
  5828. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5829. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5830. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5831. {
  5832. int Result;
  5833. bool SaveSettings = false;
  5834. int Options =
  5835. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5836. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5837. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5838. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5839. bool Continue =
  5840. (UseDefaults == 0) ||
  5841. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5842. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5843. if (Continue)
  5844. {
  5845. Configuration->Usage->Inc(L"Synchronizations");
  5846. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  5847. UpdateCopyParamCounters(CopyParam);
  5848. TSynchronizeOptions SynchronizeOptions;
  5849. GetSynchronizeOptions(Params, SynchronizeOptions);
  5850. if (SaveSettings)
  5851. {
  5852. GUIConfiguration->SynchronizeParams = Params;
  5853. }
  5854. else
  5855. {
  5856. SaveMode = false;
  5857. }
  5858. TDateTime StartTime = Now();
  5859. TSynchronizeChecklist * Checklist = NULL;
  5860. try
  5861. {
  5862. DebugAssert(!FAutoOperation);
  5863. FAutoOperation = true;
  5864. try
  5865. {
  5866. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5867. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5868. int Files = -1;
  5869. if (DataList->Count >= 1)
  5870. {
  5871. Files = StrToIntDef(DataList->Strings[0], Files);
  5872. }
  5873. else
  5874. {
  5875. DataList->Add(UnicodeString());
  5876. }
  5877. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5878. FSynchronizeProgressForm->Start();
  5879. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5880. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5881. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5882. &SynchronizeOptions);
  5883. if (Terminal->SessionData->CacheDirectories)
  5884. {
  5885. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5886. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5887. }
  5888. }
  5889. __finally
  5890. {
  5891. FAutoOperation = false;
  5892. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5893. SAFE_DESTROY(FSynchronizeProgressForm);
  5894. }
  5895. TSynchronizeParams SynchronizeParams;
  5896. SynchronizeParams.LocalDirectory = LocalDirectory;
  5897. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5898. SynchronizeParams.Mode = Mode;
  5899. SynchronizeParams.CopyParam = &CopyParam;
  5900. SynchronizeParams.Params = Params;
  5901. SynchronizeParams.Checklist = Checklist;
  5902. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5903. SynchronizeParams.StartTime = &StartTime;
  5904. SynchronizeParams.OnProcessedItem = NULL;
  5905. Result = Checklist->Count;
  5906. if (Checklist->Count > 0)
  5907. {
  5908. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5909. {
  5910. if (!DoSynchronizeChecklistDialog(
  5911. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5912. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5913. {
  5914. Result = -1;
  5915. }
  5916. }
  5917. else
  5918. {
  5919. FullSynchronize(SynchronizeParams, NULL, NULL);
  5920. }
  5921. }
  5922. else
  5923. {
  5924. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5925. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5926. }
  5927. }
  5928. __finally
  5929. {
  5930. delete Checklist;
  5931. }
  5932. OperationComplete(StartTime);
  5933. }
  5934. else
  5935. {
  5936. Result = -1;
  5937. }
  5938. return Result;
  5939. }
  5940. //---------------------------------------------------------------------------
  5941. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5942. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5943. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5944. {
  5945. if (Collect)
  5946. {
  5947. DebugAssert(FSynchronizeProgressForm != NULL);
  5948. if (!FSynchronizeProgressForm->Started)
  5949. {
  5950. FSynchronizeProgressForm->Start();
  5951. }
  5952. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5953. SetTaskbarListProgressValue(CompareProgress);
  5954. }
  5955. else
  5956. {
  5957. DebugAssert(FSynchronizeProgressForm == NULL);
  5958. DebugAssert(FProgressForm != NULL);
  5959. }
  5960. }
  5961. //---------------------------------------------------------------------------
  5962. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5963. {
  5964. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  5965. TRemoteFile * File = NULL;
  5966. Terminal->ReadFile(FullFileName, File);
  5967. if (File != NULL)
  5968. {
  5969. std::unique_ptr<TRemoteFile> FileOwner(File);
  5970. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5971. Application->ShowMainForm = false;
  5972. Application->Run();
  5973. }
  5974. }
  5975. //---------------------------------------------------------------------------
  5976. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  5977. {
  5978. DebugFail();
  5979. }
  5980. //---------------------------------------------------------------------------
  5981. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5982. {
  5983. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5984. SessionData->Assign(ManagedSession->SessionData);
  5985. UpdateSessionData(SessionData.get());
  5986. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  5987. if (IsActiveTerminal(ManagedSession))
  5988. {
  5989. ManagedSession->UpdateSessionCredentials(SessionData.get());
  5990. }
  5991. if (!ManagedSession->SessionData->HasSessionName())
  5992. {
  5993. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5994. // once IsWorkspace is cleared
  5995. SessionData->Name = UnicodeString();
  5996. }
  5997. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  5998. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  5999. SessionData->IsWorkspace = false;
  6000. return SessionData.release();
  6001. }
  6002. //---------------------------------------------------------------------------
  6003. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  6004. {
  6005. TSessionData * SessionData = CloneCurrentSessionData();
  6006. try
  6007. {
  6008. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  6009. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  6010. }
  6011. __finally
  6012. {
  6013. delete SessionData;
  6014. }
  6015. }
  6016. //---------------------------------------------------------------------------
  6017. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  6018. {
  6019. TTerminalManager * Manager = TTerminalManager::Instance();
  6020. std::unique_ptr<TObjectList> DataList(new TObjectList());
  6021. if (DebugAlwaysTrue(ManagedSession != NULL))
  6022. {
  6023. // Update ManagedSession->StateData
  6024. UpdateSession(ManagedSession);
  6025. }
  6026. Manager->SaveWorkspace(DataList.get());
  6027. return DataList.release();
  6028. }
  6029. //---------------------------------------------------------------------------
  6030. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  6031. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  6032. {
  6033. WinConfiguration->LastWorkspace = Name;
  6034. if (!SavePasswords)
  6035. {
  6036. for (int Index = 0; Index < DataList->Count; Index++)
  6037. {
  6038. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6039. if (SessionData->Link.IsEmpty())
  6040. {
  6041. SessionData->ClearSessionPasswords();
  6042. }
  6043. }
  6044. }
  6045. StoredSessions->NewWorkspace(Name, DataList);
  6046. // modified only
  6047. StoredSessions->Save(false, Explicit);
  6048. }
  6049. //---------------------------------------------------------------------------
  6050. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  6051. {
  6052. return
  6053. DefaultStr(WinConfiguration->LastWorkspace,
  6054. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  6055. }
  6056. //---------------------------------------------------------------------------
  6057. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  6058. {
  6059. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  6060. bool AnyNonStoredSessionWithPassword = false;
  6061. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  6062. bool AllNonStoredSessionsAnonymous = true;
  6063. TTerminalManager * Manager = TTerminalManager::Instance();
  6064. for (int Index = 0; Index < DataList->Count; Index++)
  6065. {
  6066. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6067. if (SessionData->Link.IsEmpty())
  6068. {
  6069. if (SessionData->HasAnySessionPassword())
  6070. {
  6071. AnyNonStoredSessionWithPassword = true;
  6072. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  6073. {
  6074. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  6075. }
  6076. }
  6077. if (!SameText(SessionData->UserName, AnonymousUserName))
  6078. {
  6079. AllNonStoredSessionsAnonymous = false;
  6080. }
  6081. }
  6082. }
  6083. bool SavePasswords;
  6084. bool * PSavePasswords;
  6085. bool NotRecommendedSavingPasswords =
  6086. !CustomWinConfiguration->UseMasterPassword &&
  6087. !AllNonStoredSessionsAnonymous;
  6088. if (Configuration->DisablePasswordStoring ||
  6089. !AnyNonStoredSessionWithPassword)
  6090. {
  6091. PSavePasswords = NULL;
  6092. SavePasswords = false;
  6093. }
  6094. else
  6095. {
  6096. PSavePasswords = &SavePasswords;
  6097. SavePasswords =
  6098. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  6099. !NotRecommendedSavingPasswords;
  6100. }
  6101. UnicodeString Name = WorkspaceName();
  6102. bool CreateShortcut = false;
  6103. bool Result =
  6104. DoSaveWorkspaceDialog(
  6105. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  6106. EnableAutoSave);
  6107. if (Result)
  6108. {
  6109. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  6110. if (CreateShortcut)
  6111. {
  6112. TOperationVisualizer Visualizer;
  6113. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  6114. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  6115. }
  6116. if (EnableAutoSave)
  6117. {
  6118. WinConfiguration->AutoSaveWorkspace = true;
  6119. WinConfiguration->AutoWorkspace = Name;
  6120. if (PSavePasswords != NULL)
  6121. {
  6122. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  6123. }
  6124. }
  6125. }
  6126. return Result;
  6127. }
  6128. //---------------------------------------------------------------------------
  6129. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  6130. {
  6131. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  6132. DebugAssert(Session == ManagedSession);
  6133. if (!IsLocalBrowserMode())
  6134. {
  6135. SAFE_DESTROY(Session->RemoteExplorerState);
  6136. if (WinConfiguration->PreservePanelState)
  6137. {
  6138. Session->RemoteExplorerState = RemoteDirView->SaveState();
  6139. }
  6140. }
  6141. UpdateSessionData(Session->StateData);
  6142. }
  6143. //---------------------------------------------------------------------------
  6144. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  6145. {
  6146. // Keep in sync with TSessionData::CopyStateData
  6147. DebugAssert(Data != NULL);
  6148. // This is inconsistent with how color (for example) is handled.
  6149. Data->Name = ManagedSession->SessionData->Name;
  6150. if (!IsLocalBrowserMode())
  6151. {
  6152. // cannot use RemoteDirView->Path, because it is empty if connection
  6153. // was already closed
  6154. // also only peek, we may not be connected at all atm
  6155. // so make sure we do not try retrieving current directory from the server
  6156. // (particularly with FTP)
  6157. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  6158. if (!ACurrentDirectory.IsEmpty())
  6159. {
  6160. Data->RemoteDirectory = ACurrentDirectory;
  6161. }
  6162. }
  6163. Data->Color = SessionColor;
  6164. }
  6165. //---------------------------------------------------------------------------
  6166. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  6167. {
  6168. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  6169. DebugAssert(Toolbar != NULL);
  6170. for (int i = 0; i < Toolbar->Items->Count; i++)
  6171. {
  6172. TTBXCustomDropDownItem * DropDownItem;
  6173. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6174. if (DropDownItem != NULL)
  6175. {
  6176. ToolbarItemResize(DropDownItem,
  6177. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  6178. Toolbar->NonClientWidth);
  6179. break;
  6180. }
  6181. }
  6182. }
  6183. //---------------------------------------------------------------------------
  6184. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6185. {
  6186. Item->EditWidth = Width;
  6187. }
  6188. //---------------------------------------------------------------------------
  6189. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6190. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6191. {
  6192. for (int i = 0; i < Toolbar->Items->Count; i++)
  6193. {
  6194. TTBXCustomDropDownItem * DropDownItem;
  6195. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6196. if (DropDownItem != NULL)
  6197. {
  6198. ASize.x -= DropDownItem->EditWidth;
  6199. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  6200. }
  6201. }
  6202. }
  6203. //---------------------------------------------------------------------------
  6204. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6205. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6206. {
  6207. if (WinConfiguration->DDWarnLackOfTempSpace)
  6208. {
  6209. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6210. if (!ADrive.IsEmpty())
  6211. {
  6212. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6213. DebugAssert(RequiredSpace >= 0);
  6214. __int64 RequiredWithReserve;
  6215. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6216. if (FreeSpace < RequiredWithReserve)
  6217. {
  6218. unsigned int Result;
  6219. TMessageParams Params(mpNeverAskAgainCheck);
  6220. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6221. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6222. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6223. if (Result == qaNeverAskAgain)
  6224. {
  6225. WinConfiguration->DDWarnLackOfTempSpace = false;
  6226. }
  6227. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6228. }
  6229. }
  6230. }
  6231. }
  6232. //---------------------------------------------------------------------------
  6233. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6234. {
  6235. DoOpenDirectoryDialog(odAddBookmark, Side);
  6236. }
  6237. //---------------------------------------------------------------------------
  6238. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6239. {
  6240. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6241. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6242. try
  6243. {
  6244. TCustomDirView * DView = DirView(Side);
  6245. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6246. {
  6247. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6248. }
  6249. }
  6250. catch (...)
  6251. {
  6252. delete VisitedDirectories;
  6253. throw;
  6254. }
  6255. return VisitedDirectories;
  6256. }
  6257. //---------------------------------------------------------------------------
  6258. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6259. TOpenDirectoryMode Mode, TOperationSide Side)
  6260. {
  6261. bool Continue = true;
  6262. if (Mode == odAddBookmark)
  6263. {
  6264. TMessageParams Params(mpNeverAskAgainCheck);
  6265. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6266. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6267. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6268. unsigned int Answer =
  6269. MessageDialog(Message,
  6270. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6271. if (Answer == qaNeverAskAgain)
  6272. {
  6273. Continue = true;
  6274. WinConfiguration->UseSharedBookmarks = true;
  6275. }
  6276. else if (Answer == qaOK)
  6277. {
  6278. Continue = true;
  6279. WinConfiguration->UseSharedBookmarks = false;
  6280. }
  6281. else
  6282. {
  6283. Continue = false;
  6284. }
  6285. }
  6286. if (Continue)
  6287. {
  6288. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6289. try
  6290. {
  6291. UnicodeString Name = DirView(Side)->PathName;
  6292. bool Repeat;
  6293. do
  6294. {
  6295. Repeat = false;
  6296. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6297. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6298. // do not allow switching to location profiles,
  6299. // if we are not connected
  6300. HasDirView[osLocal] && (Terminal != NULL)))
  6301. {
  6302. TWindowLock Lock(this);
  6303. if (!TryOpenDirectory(Side, Name))
  6304. {
  6305. Repeat = true;
  6306. Mode = odBrowse;
  6307. }
  6308. }
  6309. }
  6310. while (Repeat);
  6311. }
  6312. __finally
  6313. {
  6314. delete VisitedDirectories;
  6315. }
  6316. }
  6317. }
  6318. //---------------------------------------------------------------------------
  6319. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6320. {
  6321. DebugAssert(!Terminal->CommandSessionOpened);
  6322. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6323. }
  6324. //---------------------------------------------------------------------------
  6325. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6326. {
  6327. bool Result = Terminal->IsCapable[Capability] ||
  6328. Terminal->CommandSessionOpened;
  6329. if (!Result)
  6330. {
  6331. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6332. if (!GUIConfiguration->ConfirmCommandSession)
  6333. {
  6334. Result = true;
  6335. }
  6336. else
  6337. {
  6338. TMessageParams Params(mpNeverAskAgainCheck);
  6339. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6340. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6341. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6342. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6343. if (Answer == qaNeverAskAgain)
  6344. {
  6345. GUIConfiguration->ConfirmCommandSession = false;
  6346. Result = true;
  6347. }
  6348. else if (Answer == qaOK)
  6349. {
  6350. Result = true;
  6351. }
  6352. }
  6353. if (Result)
  6354. {
  6355. Result = CommandSessionFallback();
  6356. }
  6357. }
  6358. return Result;
  6359. }
  6360. //---------------------------------------------------------------------------
  6361. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6362. {
  6363. if (EnsureCommandSessionFallback(fcAnyCommand))
  6364. {
  6365. DoConsoleDialog(Terminal, Command);
  6366. }
  6367. }
  6368. //---------------------------------------------------------------------------
  6369. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6370. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6371. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6372. {
  6373. if (!IsSideLocalBrowser(osRemote) &&
  6374. IsFileControl(DropSourceControl, osRemote) &&
  6375. (FDDExtMapFile != NULL))
  6376. {
  6377. Accept = true;
  6378. }
  6379. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6380. DebugAssert(FDDTargetControl != NULL);
  6381. }
  6382. //---------------------------------------------------------------------------
  6383. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6384. _di_IDataObject DataObj, int KeyState,
  6385. const TPoint & Point, int & Effect, bool & Accept)
  6386. {
  6387. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6388. }
  6389. //---------------------------------------------------------------------------
  6390. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6391. _di_IDataObject DataObj, int KeyState,
  6392. const TPoint & Point, int & Effect, bool & Accept)
  6393. {
  6394. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6395. }
  6396. //---------------------------------------------------------------------------
  6397. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6398. TObject * Sender)
  6399. {
  6400. DebugUsedParam(Sender);
  6401. DebugAssert(FDDTargetControl == Sender);
  6402. FDDTargetControl = NULL;
  6403. }
  6404. //---------------------------------------------------------------------------
  6405. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6406. {
  6407. FileControlDDDragLeave(SessionsPageControl);
  6408. }
  6409. //---------------------------------------------------------------------------
  6410. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6411. {
  6412. FileControlDDDragLeave(QueueView3);
  6413. }
  6414. //---------------------------------------------------------------------------
  6415. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6416. {
  6417. DebugAssert(!IsSideLocalBrowser(Side));
  6418. DebugUsedParam(Side);
  6419. bool Edit = false;
  6420. TRemoteFile * File = NULL;
  6421. UnicodeString FileName;
  6422. UnicodeString PointTo;
  6423. bool SymbolicLink = true;
  6424. if (RemoteDirView->ItemFocused)
  6425. {
  6426. DebugAssert(RemoteDirView->ItemFocused->Data);
  6427. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6428. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6429. if (Edit)
  6430. {
  6431. FileName = File->FileName;
  6432. PointTo = File->LinkTo;
  6433. }
  6434. else if (!File->IsParentDirectory)
  6435. {
  6436. PointTo = File->FileName;
  6437. }
  6438. }
  6439. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6440. Terminal->IsCapable[fcHardLink]))
  6441. {
  6442. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6443. if (Edit)
  6444. {
  6445. DebugAssert(File->FileName == FileName);
  6446. int Params = dfNoRecursive;
  6447. Terminal->ExceptionOnFail = true;
  6448. try
  6449. {
  6450. Terminal->DeleteFile(L"", File, &Params);
  6451. }
  6452. __finally
  6453. {
  6454. Terminal->ExceptionOnFail = false;
  6455. }
  6456. }
  6457. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6458. }
  6459. }
  6460. //---------------------------------------------------------------------------
  6461. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6462. {
  6463. return
  6464. (IsSideLocalBrowser(Side) ||
  6465. (HasActiveTerminal() &&
  6466. Terminal->ResolvingSymlinks &&
  6467. Terminal->IsCapable[fcSymbolicLink]));
  6468. }
  6469. //---------------------------------------------------------------------------
  6470. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6471. {
  6472. return
  6473. !IsSideLocalBrowser(FCurrentSide) &&
  6474. (RemoteDirView->ItemFocused != NULL) &&
  6475. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6476. Terminal->SessionData->ResolveSymlinks;
  6477. }
  6478. //---------------------------------------------------------------------------
  6479. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6480. {
  6481. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6482. FForceExecution = true;
  6483. try
  6484. {
  6485. DirView(osCurrent)->ExecuteCurrentFile();
  6486. }
  6487. __finally
  6488. {
  6489. FForceExecution = false;
  6490. }
  6491. }
  6492. //---------------------------------------------------------------------------
  6493. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6494. {
  6495. TEditorData ExternalEditor;
  6496. ExternalEditor.Editor = edExternal;
  6497. bool Remember = false;
  6498. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6499. {
  6500. if (Remember)
  6501. {
  6502. TEditorList * EditorList = new TEditorList;
  6503. try
  6504. {
  6505. *EditorList = *WinConfiguration->EditorList;
  6506. bool Found = false;
  6507. int i = 0;
  6508. while (!Found && (i < EditorList->Count))
  6509. {
  6510. const TEditorPreferences * Editor = EditorList->Editors[i];
  6511. if ((Editor->Data->Editor == edExternal) &&
  6512. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6513. {
  6514. Found = true;
  6515. }
  6516. i++;
  6517. }
  6518. if (!Found)
  6519. {
  6520. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6521. WinConfiguration->EditorList = EditorList;
  6522. }
  6523. }
  6524. __finally
  6525. {
  6526. delete EditorList;
  6527. }
  6528. }
  6529. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6530. }
  6531. }
  6532. //---------------------------------------------------------------------------
  6533. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6534. {
  6535. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6536. if (FFileFindTerminal == ATerminal)
  6537. {
  6538. FFileFindTerminal = NULL;
  6539. HideFileFindDialog();
  6540. }
  6541. if (FClipboardTerminal == ATerminal)
  6542. {
  6543. ClipboardClear(); // implies ClipboardStop
  6544. }
  6545. if ((FOnSynchronizeAbort != NULL) &&
  6546. (ATerminal == FManagedSession))
  6547. {
  6548. FOnSynchronizeAbort(NULL);
  6549. }
  6550. }
  6551. //---------------------------------------------------------------------------
  6552. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6553. {
  6554. DetachTerminal(Sender);
  6555. }
  6556. //---------------------------------------------------------------------------
  6557. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6558. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6559. {
  6560. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6561. DebugAssert(Terminal != NULL);
  6562. if (Data->Terminal == Terminal)
  6563. {
  6564. Data->Terminal = NULL;
  6565. }
  6566. }
  6567. //---------------------------------------------------------------------------
  6568. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6569. {
  6570. return NULL;
  6571. }
  6572. //---------------------------------------------------------------------------
  6573. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6574. {
  6575. UpdateControls();
  6576. SessionColor = TColor(0);
  6577. UpdateRemotePathComboBox(false);
  6578. NeedSession(false);
  6579. }
  6580. //---------------------------------------------------------------------------
  6581. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6582. {
  6583. try
  6584. {
  6585. // Cache, as the login dialog can change its value.
  6586. // See also DoShow. Also see TTerminalManager::NewSession.
  6587. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6588. try
  6589. {
  6590. if (ShowLogin)
  6591. {
  6592. bool ReloadSessions = !Startup;
  6593. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6594. }
  6595. }
  6596. __finally
  6597. {
  6598. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6599. // (so there was no chance for the user to open any session yet)
  6600. if ((ShowLogin || !Startup) &&
  6601. !WinConfiguration->KeepOpenWhenNoSession &&
  6602. // this rules out the implicit Commander's local-local browser
  6603. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6604. {
  6605. TerminateApplication();
  6606. }
  6607. }
  6608. }
  6609. catch (EAbort &)
  6610. {
  6611. // swallow
  6612. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6613. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6614. }
  6615. }
  6616. //---------------------------------------------------------------------------
  6617. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6618. {
  6619. TTerminalManager * Manager = TTerminalManager::Instance();
  6620. DebugAssert(!Manager->Updating);
  6621. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6622. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6623. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6624. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6625. try
  6626. {
  6627. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6628. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6629. {
  6630. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6631. }
  6632. for (int Index = 0; Index <= Manager->Count; Index++)
  6633. {
  6634. TThemeTabSheet * TabSheet;
  6635. if (Index >= SessionsPageControl->PageCount)
  6636. {
  6637. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6638. TabSheet->PageControl = SessionsPageControl;
  6639. }
  6640. else
  6641. {
  6642. TabSheet = SessionsPageControl->Pages[Index];
  6643. }
  6644. bool IsSessionTab = (Index < Manager->Count);
  6645. if (IsSessionTab)
  6646. {
  6647. TTerminal * Terminal = Manager->Sessions[Index];
  6648. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6649. TabSheet->CaptionTruncation = tttEllipsis;
  6650. UpdateSessionTab(TabSheet);
  6651. }
  6652. else
  6653. {
  6654. TabSheet->Tag = 0; // not really needed
  6655. TabSheet->Shadowed = false;
  6656. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6657. TabSheet->CaptionTruncation = tttNoText;
  6658. // 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)
  6659. UpdateNewTabTab();
  6660. }
  6661. }
  6662. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6663. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6664. {
  6665. SessionsPageControl->UpdateTabsCaptionTruncation();
  6666. }
  6667. }
  6668. __finally
  6669. {
  6670. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6671. }
  6672. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6673. }
  6674. //---------------------------------------------------------------------------
  6675. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6676. {
  6677. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6678. }
  6679. //---------------------------------------------------------------------------
  6680. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6681. {
  6682. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6683. UnicodeString TabCaption;
  6684. if (WinConfiguration->SelectiveToolbarText)
  6685. {
  6686. TabCaption = GetNewTabTabCaption();
  6687. }
  6688. TabSheet->BaseCaption = TabCaption;
  6689. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6690. }
  6691. //---------------------------------------------------------------------------
  6692. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6693. {
  6694. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6695. }
  6696. //---------------------------------------------------------------------------
  6697. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6698. {
  6699. DebugFail();
  6700. return UnicodeString();
  6701. }
  6702. //---------------------------------------------------------------------------
  6703. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6704. {
  6705. if (DebugAlwaysTrue(TabSheet != NULL))
  6706. {
  6707. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6708. if (DebugAlwaysTrue(ASession != NULL))
  6709. {
  6710. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6711. if (ASession->LocalBrowser)
  6712. {
  6713. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6714. }
  6715. else
  6716. {
  6717. TabSheet->ImageIndex = AddSessionColor(Color);
  6718. }
  6719. TTerminalManager * Manager = TTerminalManager::Instance();
  6720. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6721. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6722. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6723. }
  6724. }
  6725. }
  6726. //---------------------------------------------------------------------------
  6727. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6728. {
  6729. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6730. }
  6731. //---------------------------------------------------------------------------
  6732. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6733. {
  6734. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6735. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6736. bool Result = (Session != NULL);
  6737. if (Result)
  6738. {
  6739. TTerminalManager::Instance()->ActiveSession = Session;
  6740. }
  6741. else
  6742. {
  6743. try
  6744. {
  6745. NewTab();
  6746. }
  6747. __finally
  6748. {
  6749. SessionListChanged();
  6750. }
  6751. FSessionsPageControlNewTabTime = Now();
  6752. }
  6753. return Result;
  6754. }
  6755. //---------------------------------------------------------------------------
  6756. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6757. {
  6758. TWindowLock Lock(this);
  6759. SessionTabSwitched();
  6760. }
  6761. //---------------------------------------------------------------------------
  6762. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6763. {
  6764. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6765. DebugAssert(TransferList != NULL);
  6766. UnicodeString Name;
  6767. if (TransferList->ItemIndex <= 0)
  6768. {
  6769. Name = L"";
  6770. }
  6771. else
  6772. {
  6773. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6774. }
  6775. if (FCopyParamAutoSelected.IsEmpty())
  6776. {
  6777. // if previous preset was not autoselected, make new preset the "default"
  6778. FCopyParamDefault = Name;
  6779. }
  6780. GUIConfiguration->CopyParamCurrent = Name;
  6781. }
  6782. //---------------------------------------------------------------------------
  6783. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6784. {
  6785. // sanity check
  6786. bool ExistingPreset =
  6787. (FTransferListHoverIndex >= 0) &&
  6788. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6789. DebugAssert(ExistingPreset);
  6790. if (ExistingPreset)
  6791. {
  6792. HDC DC = GetDC(0);
  6793. TCanvas * Canvas = new TCanvas();
  6794. try
  6795. {
  6796. Canvas->Handle = DC;
  6797. Canvas->Font = GetToolbarFont(this);
  6798. UnicodeString Name;
  6799. if (FTransferListHoverIndex == 0)
  6800. {
  6801. Name = L"";
  6802. }
  6803. else
  6804. {
  6805. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6806. }
  6807. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6808. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6809. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6810. static_cast<TObject*>(GetComponent(fcTransferList)));
  6811. UnicodeString InfoStr =
  6812. GUIConfiguration->CopyParamPreset[Name].
  6813. GetInfoStr(L"; ",
  6814. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6815. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6816. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6817. {
  6818. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6819. {
  6820. InfoStr.SetLength(InfoStr.Length() - 1);
  6821. }
  6822. InfoStr += Ellipsis;
  6823. }
  6824. // UpdateCaption does not cause invalidation of whole submenu, while
  6825. // setting Caption property does.
  6826. // also it probably does not resize the label, even if necessary
  6827. // (we do not want that anyway)
  6828. TransferLabel->UpdateCaption(InfoStr);
  6829. }
  6830. __finally
  6831. {
  6832. Canvas->Handle = NULL;
  6833. ReleaseDC(0, DC);
  6834. delete Canvas;
  6835. }
  6836. }
  6837. }
  6838. //---------------------------------------------------------------------------
  6839. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6840. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6841. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6842. {
  6843. if (FTransferListHoverIndex != AHoverIndex)
  6844. {
  6845. FTransferListHoverIndex = AHoverIndex;
  6846. UpdateTransferLabel();
  6847. }
  6848. }
  6849. //---------------------------------------------------------------------------
  6850. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6851. {
  6852. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6853. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6854. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6855. {
  6856. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6857. {
  6858. if (!NonVisualDataModule->Busy)
  6859. {
  6860. OpenDirectory(GetSide(osCurrent));
  6861. }
  6862. Message.Result = 1;
  6863. }
  6864. else
  6865. {
  6866. TForm::Dispatch(&Message);
  6867. }
  6868. }
  6869. else
  6870. {
  6871. TForm::Dispatch(&Message);
  6872. }
  6873. }
  6874. //---------------------------------------------------------------------------
  6875. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6876. {
  6877. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6878. if ((FIgnoreNextDialogChar != 0) &&
  6879. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6880. {
  6881. Message.Result = 1;
  6882. }
  6883. else
  6884. {
  6885. TForm::Dispatch(&Message);
  6886. }
  6887. FIgnoreNextDialogChar = 0;
  6888. }
  6889. //---------------------------------------------------------------------------
  6890. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6891. {
  6892. // The four low-order bits are used internally by Windows
  6893. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6894. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6895. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6896. // 61730 - restore thru double click - undocumented
  6897. // 61490 - maximize thru double click - undocumented
  6898. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6899. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6900. {
  6901. SysResizing(Cmd);
  6902. }
  6903. TForm::Dispatch(&Message);
  6904. }
  6905. //---------------------------------------------------------------------------
  6906. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6907. {
  6908. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6909. // Also there no point returning TRUE as we are not able to
  6910. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6911. // Hence the process termination might be safer :)
  6912. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6913. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6914. {
  6915. Message.Result = FALSE;
  6916. }
  6917. else
  6918. {
  6919. Message.Result = TRUE;
  6920. }
  6921. // Do not call default handling as that triggers OnCloseQuery,
  6922. // where our implementation will popup confirmation dialogs, what we do not want,
  6923. // as per Vista guidelines:
  6924. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6925. }
  6926. //---------------------------------------------------------------------------
  6927. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6928. {
  6929. if (Message.EndSession && IsApplicationMinimized())
  6930. {
  6931. // WORKAROUND
  6932. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6933. // when it receives WM_ENDSESSION,
  6934. // but that sometimes (particularly when application is minimized) cause crashes.
  6935. // Crash popups message that blocks log off.
  6936. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6937. // so we call ExitProcess() immediately, not even trying to cleanup.
  6938. // It still causes beep, so there's likely some popup, but it does not block
  6939. // log off.
  6940. ExitProcess(0);
  6941. }
  6942. TForm::Dispatch(&Message);
  6943. }
  6944. #ifdef _DEBUG
  6945. //---------------------------------------------------------------------------
  6946. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6947. {
  6948. TForm::Dispatch(&Message);
  6949. }
  6950. #endif
  6951. //---------------------------------------------------------------------------
  6952. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6953. {
  6954. }
  6955. //---------------------------------------------------------------------------
  6956. void __fastcall TCustomScpExplorerForm::DoShow()
  6957. {
  6958. // Called from TCustomScpExplorerForm::CMShowingChanged
  6959. // only now are the controls resized finally, so the size constraints
  6960. // will not conflict with possibly very small window size
  6961. RestoreFormParams();
  6962. // This indicates that the form was rescaled after parameters were loaded and
  6963. // as most probably the parameters were actually saved with the current form scaling
  6964. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6965. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6966. // and no rounding problems will occur.
  6967. // See also RestoreForm.
  6968. // (we should be safe to call RestoreParams unconditionally)
  6969. if (PixelsPerInch != Screen->PixelsPerInch)
  6970. {
  6971. RestoreParams();
  6972. }
  6973. FixControlsPlacement();
  6974. if (Position == poDefaultPosOnly)
  6975. {
  6976. CutFormToDesktop(this);
  6977. }
  6978. TForm::DoShow();
  6979. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6980. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6981. // This was previously in NeedSession, where other related code is.
  6982. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  6983. // unpleasantly shows briefly.
  6984. if ((ManagedSession == NULL) &&
  6985. !WinConfiguration->ShowLoginWhenNoSession &&
  6986. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6987. // This detects if workspace was saved the last time the main widow was closed
  6988. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6989. {
  6990. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  6991. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6992. }
  6993. // Before main window displays
  6994. if (ManagedSession == NULL)
  6995. {
  6996. StartingWithoutSession();
  6997. }
  6998. UpdatePixelsPerInchMainWindowCounter();
  6999. FShowing = true;
  7000. }
  7001. //---------------------------------------------------------------------------
  7002. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  7003. {
  7004. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  7005. }
  7006. //---------------------------------------------------------------------------
  7007. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  7008. {
  7009. SessionListChanged();
  7010. InitStatusBar();
  7011. UpdateControls();
  7012. }
  7013. //---------------------------------------------------------------------------
  7014. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  7015. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  7016. TNotifyEvent OnBalloonClick, TObject * UserData)
  7017. {
  7018. bool Do;
  7019. UnicodeString Message;
  7020. if (E == NULL)
  7021. {
  7022. Message = Str;
  7023. Do = true;
  7024. }
  7025. else
  7026. {
  7027. Do = ExceptionMessage(E, Message);
  7028. }
  7029. if (Do && WinConfiguration->BalloonNotifications)
  7030. {
  7031. UnicodeString Title;
  7032. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  7033. {
  7034. Message = TrimLeft(Message);
  7035. }
  7036. else
  7037. {
  7038. Message = UnformatMessage(Message);
  7039. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  7040. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  7041. Title = LoadResourceString(Captions[Type]);
  7042. if (Terminal != NULL)
  7043. {
  7044. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7045. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  7046. }
  7047. }
  7048. if (Seconds == 0)
  7049. {
  7050. Seconds = WinConfiguration->NotificationsTimeout;
  7051. }
  7052. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  7053. }
  7054. }
  7055. //---------------------------------------------------------------------------
  7056. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  7057. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  7058. UnicodeString HelpKeyword, const TMessageParams * Params,
  7059. TTerminal * Terminal)
  7060. {
  7061. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  7062. (FOnFeedSynchronizeError != NULL)) &&
  7063. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  7064. {
  7065. if (FOnFeedSynchronizeError != NULL)
  7066. {
  7067. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  7068. }
  7069. else
  7070. {
  7071. DebugAssert(FErrorList != NULL);
  7072. TStringList * MoreMessagesCopy = NULL;
  7073. if (MoreMessages)
  7074. {
  7075. MoreMessagesCopy = new TStringList();
  7076. MoreMessagesCopy->Assign(MoreMessages);
  7077. }
  7078. FErrorList->AddObject(Message, MoreMessagesCopy);
  7079. }
  7080. PopupTrayBalloon(Terminal, Message, Type);
  7081. return ContinueAnswer(Answers);
  7082. }
  7083. else
  7084. {
  7085. bool UseBalloon = FTrayIcon->Visible;
  7086. if (UseBalloon)
  7087. {
  7088. PopupTrayBalloon(Terminal, Message, Type);
  7089. }
  7090. unsigned int Result;
  7091. try
  7092. {
  7093. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  7094. }
  7095. __finally
  7096. {
  7097. // cancel only balloon we popped up, otherwise we may cancel notification
  7098. // balloon that was there before the message dialog
  7099. // (such as "dd confirmation opt in balloon)
  7100. if (UseBalloon)
  7101. {
  7102. FTrayIcon->CancelBalloon();
  7103. }
  7104. }
  7105. return Result;
  7106. }
  7107. }
  7108. //---------------------------------------------------------------------------
  7109. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  7110. TTerminal * Terminal, Exception * E)
  7111. {
  7112. if (FTrayIcon->Visible)
  7113. {
  7114. PopupTrayBalloon(Terminal, L"", qtError, E);
  7115. }
  7116. // particularly prevent opening new session from jump list,
  7117. // while exception is shown
  7118. NonVisualDataModule->StartBusy();
  7119. try
  7120. {
  7121. ShowExtendedExceptionEx(Terminal, E);
  7122. }
  7123. __finally
  7124. {
  7125. NonVisualDataModule->EndBusy();
  7126. FTrayIcon->CancelBalloon();
  7127. }
  7128. }
  7129. //---------------------------------------------------------------------------
  7130. void __fastcall TCustomScpExplorerForm::SessionReady()
  7131. {
  7132. InitStatusBar();
  7133. // cannot rely on active page being page for active terminal,
  7134. // as it can happen that active page is the "new session" page
  7135. // (e.g. when reconnecting active terminal, while login dialog
  7136. // invoked from "new session" page is modal)
  7137. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  7138. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  7139. }
  7140. //---------------------------------------------------------------------------
  7141. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  7142. TTerminal * Terminal, Exception * E)
  7143. {
  7144. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  7145. if (!Terminal->Active)
  7146. {
  7147. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  7148. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  7149. {
  7150. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  7151. }
  7152. }
  7153. }
  7154. //---------------------------------------------------------------------------
  7155. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  7156. UnicodeString Message, TQueryType Type,
  7157. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  7158. {
  7159. if ((E == NULL) ||
  7160. ExceptionMessage(E, Message))
  7161. {
  7162. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  7163. if (Important)
  7164. {
  7165. Seconds *= 5;
  7166. }
  7167. UnicodeString NoteMessage(UnformatMessage(Message));
  7168. if (Terminal != NULL)
  7169. {
  7170. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7171. NoteMessage = FORMAT(L"%s: %s",
  7172. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  7173. }
  7174. if (WinConfiguration->BalloonNotifications)
  7175. {
  7176. AddNote(NoteMessage);
  7177. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  7178. }
  7179. else
  7180. {
  7181. FlashOnBackground();
  7182. PostNote(NoteMessage, Seconds, OnClick, UserData);
  7183. }
  7184. }
  7185. }
  7186. //---------------------------------------------------------------------------
  7187. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7188. {
  7189. RestoreApp();
  7190. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7191. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7192. !NonVisualDataModule->Busy)
  7193. {
  7194. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7195. TTerminalManager::Instance()->ActiveSession = Terminal;
  7196. if (!ComponentVisible[fcQueueView])
  7197. {
  7198. ToggleQueueVisibility();
  7199. GoToQueue();
  7200. }
  7201. }
  7202. }
  7203. //---------------------------------------------------------------------------
  7204. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7205. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7206. {
  7207. UnicodeString Message;
  7208. TNotifyEvent OnClick = NULL;
  7209. TObject * UserData = NULL;
  7210. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7211. switch (Event)
  7212. {
  7213. case qeEmptyButMonitored:
  7214. if ((ATerminal != Terminal) || QueueInvisible)
  7215. {
  7216. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7217. OnClick = QueueEmptyNoteClicked;
  7218. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7219. TerminalNoteData->Terminal = ATerminal;
  7220. UserData = TerminalNoteData;
  7221. }
  7222. break;
  7223. case qeEmpty:
  7224. OperationComplete(ATerminal->QueueOperationStart);
  7225. break;
  7226. case qePendingUserAction:
  7227. if ((ATerminal != Terminal) ||
  7228. (QueueInvisible && !IsQueueAutoPopup()))
  7229. {
  7230. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7231. }
  7232. break;
  7233. default:
  7234. DebugFail();
  7235. }
  7236. if (!Message.IsEmpty())
  7237. {
  7238. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7239. }
  7240. }
  7241. //---------------------------------------------------------------------------
  7242. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7243. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7244. {
  7245. if (FDDExtMapFile != NULL)
  7246. {
  7247. CloseHandle(FDDExtMapFile);
  7248. FDDExtMapFile = NULL;
  7249. }
  7250. if (WinConfiguration->DDFakeFile)
  7251. {
  7252. DDFakeFileInitDrag(FileList, Created);
  7253. }
  7254. }
  7255. //---------------------------------------------------------------------------
  7256. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7257. {
  7258. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7259. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7260. {
  7261. FFakeFileDropTarget = FileName;
  7262. }
  7263. }
  7264. //---------------------------------------------------------------------------
  7265. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7266. {
  7267. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7268. if (!ForceDirectories(ApiPath(Result)))
  7269. {
  7270. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7271. }
  7272. FileSetAttr(ApiPath(Result), faHidden);
  7273. return Result;
  7274. }
  7275. //---------------------------------------------------------------------------
  7276. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7277. bool & Created)
  7278. {
  7279. FFakeFileDropTarget = UnicodeString();
  7280. FDragFakeDirectory = CreateFakeTransferDirectory();
  7281. FileList->AddItem(NULL, FDragFakeDirectory);
  7282. Created = true;
  7283. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7284. {
  7285. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7286. }
  7287. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7288. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7289. {
  7290. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7291. TDragExtCommStruct* CommStruct;
  7292. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7293. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7294. DebugAssert(CommStruct != NULL);
  7295. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7296. CommStruct->Dragging = true;
  7297. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7298. LENOF(CommStruct->DropDest));
  7299. NULL_TERMINATE(CommStruct->DropDest);
  7300. UnmapViewOfFile(CommStruct);
  7301. }
  7302. }
  7303. //---------------------------------------------------------------------------
  7304. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7305. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7306. {
  7307. bool Result;
  7308. DebugAssert(!IsLocalBrowserMode());
  7309. if (Sender == RemoteDirView)
  7310. {
  7311. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7312. }
  7313. else
  7314. {
  7315. DebugAssert(Sender == RemoteDriveView);
  7316. TStrings * FileList = RemoteDriveView->DragFileList();
  7317. try
  7318. {
  7319. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7320. }
  7321. __finally
  7322. {
  7323. delete FileList;
  7324. }
  7325. }
  7326. if (FDDTargetControl == RemoteDriveView)
  7327. {
  7328. RemoteDriveView->UpdateDropTarget();
  7329. }
  7330. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7331. // or from tree view to dir view.
  7332. // foMove happens when file/dir is dragged from remote tree view outside of
  7333. // application via dragex
  7334. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7335. (DropSourceControl == RemoteDriveView))
  7336. {
  7337. RemoteDriveView->UpdateDropSource();
  7338. }
  7339. return Result;
  7340. }
  7341. //---------------------------------------------------------------------------
  7342. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7343. {
  7344. DebugAssert(!IsLocalBrowserMode());
  7345. // This also handles drops of remote files to queue.
  7346. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7347. SAFE_DESTROY(FDDFileList);
  7348. if (!FFakeFileDropTarget.IsEmpty())
  7349. {
  7350. RemoveDir(ApiPath(FFakeFileDropTarget));
  7351. }
  7352. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7353. {
  7354. try
  7355. {
  7356. TDragResult DDResult = (Sender == RemoteDirView) ?
  7357. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7358. // Focus is moved to the target application,
  7359. // but as we are going to present the UI, we need to steal the focus back.
  7360. // This most likely won't work though (windows does not allow application
  7361. // to steal focus most of the time)
  7362. Application->BringToFront();
  7363. // On older version of Windows we never got drMove here, see also comment below.
  7364. // On Windows 10, we get the "move".
  7365. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7366. {
  7367. UnicodeString TargetDirectory;
  7368. TFileOperation Operation;
  7369. // drInvalid may mean drMove, see comment below
  7370. switch (DDResult)
  7371. {
  7372. case drCopy:
  7373. Operation = foCopy;
  7374. break;
  7375. case drMove:
  7376. Operation = foMove;
  7377. break;
  7378. default:
  7379. DebugFail();
  7380. case drInvalid:
  7381. // prefer "copy" for safety
  7382. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7383. break;
  7384. }
  7385. TTransferOperationParam Param;
  7386. UnicodeString CounterName;
  7387. bool ForceQueue;
  7388. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7389. {
  7390. // we get drInvalid both if d&d was intercepted by ddext,
  7391. // and when users drops on no-drop location.
  7392. // we tell the difference by existence of response from ddext,
  7393. // so we ignore absence of response in this case
  7394. if (DDResult != drInvalid)
  7395. {
  7396. // here we know that the extension is installed,
  7397. // as it is checked as soon as drag&drop starts from
  7398. // RemoteFileControlDDCreateDragFileList
  7399. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7400. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7401. {
  7402. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7403. }
  7404. else
  7405. {
  7406. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7407. }
  7408. }
  7409. }
  7410. else
  7411. {
  7412. // download using fake file
  7413. Param.Temp = false;
  7414. Param.DragDrop = true;
  7415. if (ForceQueue)
  7416. {
  7417. Param.Queue = asOn;
  7418. }
  7419. if (Sender == RemoteDirView)
  7420. {
  7421. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7422. }
  7423. if (RemoteFileControlFileOperation(Sender, Operation,
  7424. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7425. {
  7426. Configuration->Usage->Inc(CounterName);
  7427. }
  7428. }
  7429. }
  7430. }
  7431. __finally
  7432. {
  7433. CloseHandle(FDDExtMapFile);
  7434. FDDExtMapFile = NULL;
  7435. delete FDragFakeMonitors;
  7436. FDragFakeMonitors = NULL;
  7437. RemoveDir(ApiPath(FDragFakeDirectory));
  7438. FDragFakeDirectory = L"";
  7439. FFakeFileDropTarget = UnicodeString();
  7440. }
  7441. }
  7442. if (FDragDropSshTerminate.get() != NULL)
  7443. {
  7444. FDragDropSshTerminate->Rethrow();
  7445. }
  7446. }
  7447. //---------------------------------------------------------------------------
  7448. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7449. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7450. {
  7451. // Remember drop effect so we know (when user drops files), if we copy or move
  7452. FLastDropEffect = dwEffect;
  7453. }
  7454. //---------------------------------------------------------------------------
  7455. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7456. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7457. {
  7458. bool Result;
  7459. if (FDDTargetControl == QueueView3)
  7460. {
  7461. Directory = DefaultDownloadTargetDirectory();
  7462. Result = true;
  7463. CounterName = L"DownloadsDragDropQueue";
  7464. ForceQueue = true;
  7465. }
  7466. else if (!FFakeFileDropTarget.IsEmpty())
  7467. {
  7468. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7469. Result = true;
  7470. ForceQueue = false;
  7471. CounterName = L"DownloadsDragDropFakeFile";
  7472. }
  7473. else
  7474. {
  7475. ForceQueue = false;
  7476. Enabled = false;
  7477. try
  7478. {
  7479. int Timer = 0;
  7480. Result = false;
  7481. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7482. {
  7483. {
  7484. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7485. TDragExtCommStruct* CommStruct;
  7486. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7487. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7488. DebugAssert(CommStruct != NULL);
  7489. Result = !CommStruct->Dragging;
  7490. if (Result)
  7491. {
  7492. Directory = ExtractFilePath(CommStruct->DropDest);
  7493. CounterName = L"DownloadsDragDropExternalExt";
  7494. }
  7495. UnmapViewOfFile(CommStruct);
  7496. }
  7497. if (!Result)
  7498. {
  7499. Sleep(50);
  7500. Timer += 50;
  7501. Application->ProcessMessages();
  7502. }
  7503. }
  7504. }
  7505. __finally
  7506. {
  7507. Enabled = true;
  7508. }
  7509. }
  7510. return Result;
  7511. }
  7512. //---------------------------------------------------------------------------
  7513. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7514. const UnicodeString TempDir, int SecDelay)
  7515. {
  7516. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7517. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7518. if (FDelayedDeletionTimer == NULL)
  7519. {
  7520. DebugAssert(HandleAllocated());
  7521. FDelayedDeletionTimer = new TTimer(this);
  7522. FDelayedDeletionTimer->Interval = 10000;
  7523. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7524. }
  7525. else
  7526. {
  7527. FDelayedDeletionTimer->Enabled = true;
  7528. }
  7529. }
  7530. //---------------------------------------------------------------------------
  7531. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7532. {
  7533. DebugAssert(FDelayedDeletionList != NULL);
  7534. TDateTime N = Now();
  7535. TDateTime Alert;
  7536. UnicodeString Directory;
  7537. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7538. {
  7539. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7540. if ((N >= Alert) || (Sender == NULL))
  7541. {
  7542. Directory = FDelayedDeletionList->Strings[Index];
  7543. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7544. {
  7545. FDelayedDeletionList->Delete(Index);
  7546. }
  7547. }
  7548. }
  7549. if (FDelayedDeletionList->Count == 0)
  7550. {
  7551. FDelayedDeletionTimer->Enabled = false;
  7552. }
  7553. }
  7554. //---------------------------------------------------------------------------
  7555. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7556. {
  7557. DebugAssert(!IsLocalBrowserMode());
  7558. if (IsFileControl(FDDTargetControl, osRemote) ||
  7559. (FDDTargetControl == SessionsPageControl))
  7560. {
  7561. TManagedTerminal * TargetTerminal = NULL;
  7562. TFileOperation Operation = foNone;
  7563. if (FDDTargetControl == SessionsPageControl)
  7564. {
  7565. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7566. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7567. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7568. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7569. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7570. {
  7571. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7572. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7573. {
  7574. Operation = foRemoteMove;
  7575. }
  7576. else
  7577. {
  7578. Operation = foRemoteCopy;
  7579. }
  7580. }
  7581. }
  7582. else
  7583. {
  7584. if (FLastDropEffect == DROPEFFECT_MOVE)
  7585. {
  7586. Operation = foRemoteMove;
  7587. }
  7588. else if (FLastDropEffect == DROPEFFECT_COPY)
  7589. {
  7590. Operation = foRemoteCopy;
  7591. }
  7592. }
  7593. if (Operation != foNone)
  7594. {
  7595. RemoteFileControlFileOperation(DropSourceControl,
  7596. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7597. }
  7598. // abort drag&drop
  7599. Abort();
  7600. }
  7601. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7602. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7603. (FDDTargetControl != QueueView3))
  7604. {
  7605. DebugAssert(!FDragTempDir.IsEmpty());
  7606. TTransferType Type;
  7607. UnicodeString TempDir = FDragTempDir;
  7608. // We clear FDragTempDir before calling
  7609. // just in case it fail (raises exception)
  7610. FDragTempDir = L"";
  7611. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7612. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7613. // empty directory parameter means temp directory -> don't display it!
  7614. UnicodeString TargetDir = L"";
  7615. int Options = 0;
  7616. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7617. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7618. {
  7619. Abort();
  7620. }
  7621. // TargetDir is set when dropped on local file control
  7622. // (this was workaround for legacy dirview event handling, now it should be
  7623. // made prettier)
  7624. if (TargetDir.IsEmpty())
  7625. {
  7626. TargetDir = TempDir;
  7627. if (ForceDirectories(ApiPath(TargetDir)))
  7628. {
  7629. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7630. FPendingTempSpaceWarn = true;
  7631. try
  7632. {
  7633. FDragDropOperation = true;
  7634. int Params = cpTemporary |
  7635. (Type == ttMove ? cpDelete : 0);
  7636. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7637. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7638. }
  7639. __finally
  7640. {
  7641. FDragDropOperation = false;
  7642. FPendingTempSpaceWarn = false;
  7643. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7644. }
  7645. }
  7646. else
  7647. {
  7648. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7649. }
  7650. }
  7651. }
  7652. }
  7653. //---------------------------------------------------------------------------
  7654. void __fastcall TCustomScpExplorerForm::DDDownload(
  7655. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  7656. {
  7657. DebugAssert(!IsLocalBrowserMode());
  7658. TAutoBatch AutoBatch(this);
  7659. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  7660. UpdateCopyParamCounters(*CopyParam);
  7661. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7662. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7663. {
  7664. RemoteDriveView->UpdateDropSource();
  7665. }
  7666. }
  7667. //---------------------------------------------------------------------------
  7668. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7669. {
  7670. public:
  7671. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7672. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7673. {
  7674. }
  7675. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7676. {
  7677. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7678. TDataObjectFilesEx::AllowData(FormatEtc);
  7679. }
  7680. };
  7681. //---------------------------------------------------------------------------
  7682. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7683. TObject * Sender, TDataObject *& DataObject)
  7684. {
  7685. if (FDDExtMapFile != NULL)
  7686. {
  7687. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7688. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7689. {
  7690. Abort();
  7691. }
  7692. if (FileList->Count > 0)
  7693. {
  7694. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7695. if (!FilesObject->IsValid(true, true))
  7696. {
  7697. FilesObject->_Release();
  7698. }
  7699. else
  7700. {
  7701. DataObject = FilesObject;
  7702. }
  7703. }
  7704. }
  7705. }
  7706. //---------------------------------------------------------------------------
  7707. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7708. {
  7709. DebugFail();
  7710. }
  7711. //---------------------------------------------------------------------------
  7712. void __fastcall TCustomScpExplorerForm::GoToTree()
  7713. {
  7714. ComponentVisible[fcRemoteTree] = true;
  7715. DriveView(osRemote)->SetFocus();
  7716. }
  7717. //---------------------------------------------------------------------------
  7718. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7719. TPanelExport Export)
  7720. {
  7721. TCustomDirView * DirView = this->DirView(Side);
  7722. std::unique_ptr<TStrings> ExportData(new TStringList());
  7723. switch (Export)
  7724. {
  7725. case pePath:
  7726. ExportData->Add(DirView->PathName);
  7727. break;
  7728. case peFileList:
  7729. case peFullFileList:
  7730. {
  7731. bool FullPath = (Export == peFullFileList);
  7732. DirView->CreateFileList(false, FullPath, ExportData.get());
  7733. for (int Index = 0; Index < ExportData->Count; Index++)
  7734. {
  7735. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7736. {
  7737. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7738. }
  7739. }
  7740. }
  7741. break;
  7742. default:
  7743. DebugFail();
  7744. }
  7745. return ExportData.release();
  7746. }
  7747. //---------------------------------------------------------------------------
  7748. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7749. TPanelExport Export, TPanelExportDestination Destination)
  7750. {
  7751. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7752. PanelExportStore(Side, Export, Destination, ExportData.get());
  7753. }
  7754. //---------------------------------------------------------------------------
  7755. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7756. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7757. TStrings * ExportData)
  7758. {
  7759. if (Destination == pedClipboard)
  7760. {
  7761. TInstantOperationVisualizer Visualizer;
  7762. CopyToClipboard(ExportData);
  7763. }
  7764. else
  7765. {
  7766. DebugFail();
  7767. }
  7768. }
  7769. //---------------------------------------------------------------------------
  7770. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7771. {
  7772. TCustomDirView * DirView = this->DirView(Side);
  7773. UnicodeString Mask = DirView->Mask;
  7774. if (DoFilterMaskDialog(DirView, Mask))
  7775. {
  7776. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7777. Configuration->Usage->Inc(L"Filters");
  7778. }
  7779. }
  7780. //---------------------------------------------------------------------------
  7781. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7782. {
  7783. return FQueueController->DefaultOperation();
  7784. }
  7785. //---------------------------------------------------------------------------
  7786. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7787. TQueueOperation Operation, void ** Param)
  7788. {
  7789. switch (Operation)
  7790. {
  7791. case qoPreferences:
  7792. return true;
  7793. case qoGoTo:
  7794. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7795. case qoOnceEmpty:
  7796. return IsAnythingQueued();
  7797. default:
  7798. return FQueueController->AllowOperation(Operation, Param);
  7799. }
  7800. }
  7801. //---------------------------------------------------------------------------
  7802. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7803. {
  7804. if (DebugAlwaysTrue(QueueView3->Visible))
  7805. {
  7806. QueueView3->SetFocus();
  7807. }
  7808. }
  7809. //---------------------------------------------------------------------------
  7810. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7811. TQueueOperation Operation, void * Param)
  7812. {
  7813. if (Operation == qoGoTo)
  7814. {
  7815. GoToQueue();
  7816. }
  7817. else if (Operation == qoPreferences)
  7818. {
  7819. PreferencesDialog(pmQueue);
  7820. }
  7821. else
  7822. {
  7823. FQueueController->ExecuteOperation(Operation, Param);
  7824. }
  7825. }
  7826. //---------------------------------------------------------------------------
  7827. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7828. {
  7829. return (Queue != NULL) && Queue->Enabled;
  7830. }
  7831. //---------------------------------------------------------------------------
  7832. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7833. {
  7834. DebugAssert(Queue != NULL);
  7835. if (Queue != NULL)
  7836. {
  7837. Queue->Enabled = !Queue->Enabled;
  7838. }
  7839. }
  7840. //---------------------------------------------------------------------------
  7841. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7842. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7843. {
  7844. FQueueActedItem = QueueView3->ItemFocused;
  7845. }
  7846. //---------------------------------------------------------------------------
  7847. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7848. {
  7849. return TopDock->Height + (MessageDock->Visible ? MessageDock->Height : 0) + QueueSplitter->Height;
  7850. }
  7851. //---------------------------------------------------------------------------
  7852. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7853. {
  7854. return
  7855. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7856. (QueueDock->Visible ? QueueDock->Height : 0) +
  7857. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7858. }
  7859. //---------------------------------------------------------------------------
  7860. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7861. {
  7862. return ScaleByTextHeight(this, 48);
  7863. }
  7864. //---------------------------------------------------------------------------
  7865. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7866. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7867. {
  7868. // when queue is hidden by double-clicking splitter, stray attempt to
  7869. // resize the panel with strange value arrives, make sure it is ignored
  7870. if (ComponentVisible[fcQueueView])
  7871. {
  7872. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7873. if (NewSize > MaxHeightLimit)
  7874. {
  7875. NewSize = MaxHeightLimit;
  7876. }
  7877. int MinHeightLimit =
  7878. GetStaticQueuePanelComponentsHeight() +
  7879. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7880. GetMinQueueViewHeight();
  7881. if (NewSize < MinHeightLimit)
  7882. {
  7883. NewSize = MinHeightLimit;
  7884. }
  7885. }
  7886. else
  7887. {
  7888. Accept = false;
  7889. }
  7890. }
  7891. //---------------------------------------------------------------------------
  7892. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7893. {
  7894. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7895. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7896. if (QueueViewHeight < GetMinQueueViewHeight())
  7897. {
  7898. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7899. }
  7900. }
  7901. //---------------------------------------------------------------------------
  7902. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7903. TDragObject *& /*DragObject*/)
  7904. {
  7905. FQueueActedItem = QueueView3->ItemFocused;
  7906. }
  7907. //---------------------------------------------------------------------------
  7908. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7909. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7910. {
  7911. Accept = true;
  7912. if (Source == QueueView3)
  7913. {
  7914. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7915. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7916. if (Accept)
  7917. {
  7918. TQueueItemProxy * QueueItem;
  7919. TQueueItemProxy * DestQueueItem;
  7920. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7921. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7922. Accept = (QueueItem != DestQueueItem) &&
  7923. (QueueItem->Status == TQueueItem::qsPending) &&
  7924. (DestQueueItem->Status == TQueueItem::qsPending);
  7925. }
  7926. }
  7927. }
  7928. //---------------------------------------------------------------------------
  7929. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7930. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7931. {
  7932. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7933. {
  7934. TQueueItemProxy * QueueItem;
  7935. TQueueItemProxy * DestQueueItem;
  7936. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7937. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7938. QueueItem->Move(DestQueueItem);
  7939. }
  7940. }
  7941. //---------------------------------------------------------------------------
  7942. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7943. {
  7944. if ((QueueView3->ItemFocused == NULL) &&
  7945. (QueueView3->Items->Count > 0))
  7946. {
  7947. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7948. }
  7949. QueueLabelUpdateStatus();
  7950. }
  7951. //---------------------------------------------------------------------------
  7952. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7953. {
  7954. QueueLabelUpdateStatus();
  7955. }
  7956. //---------------------------------------------------------------------------
  7957. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7958. {
  7959. QueueLabel->UpdateStatus();
  7960. }
  7961. //---------------------------------------------------------------------------
  7962. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7963. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7964. {
  7965. if (Selected)
  7966. {
  7967. NonVisualDataModule->UpdateNonVisibleActions();
  7968. }
  7969. }
  7970. //---------------------------------------------------------------------------
  7971. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7972. {
  7973. TDragDropFilesEx * Result = NULL;
  7974. if (Sender == SessionsPageControl)
  7975. {
  7976. Result = FSessionsDragDropFilesEx;
  7977. }
  7978. else if (Sender == QueueView3)
  7979. {
  7980. Result = FQueueDragDropFilesEx;
  7981. }
  7982. else
  7983. {
  7984. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7985. if (DirView != NULL)
  7986. {
  7987. Result = DirView->DragDropFilesEx;
  7988. }
  7989. else
  7990. {
  7991. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7992. if (DriveView != NULL)
  7993. {
  7994. Result = DriveView->DragDropFilesEx;
  7995. }
  7996. }
  7997. }
  7998. DebugAssert(Result != NULL);
  7999. return Result;
  8000. }
  8001. //---------------------------------------------------------------------------
  8002. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  8003. {
  8004. return (DView->SelCount == DView->FilesCount);
  8005. }
  8006. //---------------------------------------------------------------------------
  8007. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  8008. {
  8009. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  8010. }
  8011. //---------------------------------------------------------------------------
  8012. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  8013. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  8014. {
  8015. DebugAssert(!IsLocalBrowserMode());
  8016. TFileOperation Operation;
  8017. switch (Effect)
  8018. {
  8019. case DROPEFFECT_MOVE:
  8020. Operation = foMove;
  8021. break;
  8022. case DROPEFFECT_COPY:
  8023. // occures on WinXP (reported by user)
  8024. default:
  8025. Operation = foCopy;
  8026. break;
  8027. };
  8028. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  8029. // see a comment in TUnixDirView::PerformItemDragDropOperation
  8030. if (DragDropFilesEx->FileList->Count > 0)
  8031. {
  8032. TStrings * FileList = new TStringList();
  8033. try
  8034. {
  8035. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  8036. {
  8037. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  8038. }
  8039. FDragDropOperation = true;
  8040. TTransferOperationParam Param;
  8041. Param.TargetDirectory = TargetPath;
  8042. // upload, no temp dirs
  8043. Param.Temp = false;
  8044. Param.DragDrop = DragDrop;
  8045. Param.Options =
  8046. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  8047. if (ForceQueue)
  8048. {
  8049. Param.Queue = asOn;
  8050. }
  8051. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  8052. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  8053. {
  8054. if (IsFileControl(DropSourceControl, osLocal))
  8055. {
  8056. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  8057. }
  8058. else
  8059. {
  8060. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  8061. }
  8062. }
  8063. }
  8064. __finally
  8065. {
  8066. FDragDropOperation = false;
  8067. delete FileList;
  8068. }
  8069. }
  8070. }
  8071. //---------------------------------------------------------------------------
  8072. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  8073. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  8074. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  8075. {
  8076. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  8077. }
  8078. //---------------------------------------------------------------------------
  8079. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  8080. TObject * Sender, int grfKeyState, int & dwEffect)
  8081. {
  8082. DebugAssert(!IsLocalBrowserMode());
  8083. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  8084. if ((dwEffect != DROPEFFECT_NONE) &&
  8085. IsFileControl(DropSourceControl, osRemote))
  8086. {
  8087. // do not allow drop on remote panel (on free space, still allow drop on directories)
  8088. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  8089. (RemoteDirView->DropTarget == NULL))
  8090. {
  8091. dwEffect = DROPEFFECT_NONE;
  8092. }
  8093. else
  8094. {
  8095. if (dwEffect == DROPEFFECT_COPY)
  8096. {
  8097. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  8098. // currently we support copying always (at least via temporary directory);
  8099. // remove associated checks once this all proves stable and working
  8100. bool CopyCapable = true;
  8101. // if we do not support neither of operations, there's no discussion
  8102. if (!MoveCapable && !CopyCapable)
  8103. {
  8104. dwEffect = DROPEFFECT_NONE;
  8105. }
  8106. else
  8107. {
  8108. // The default effect inside remote panel is move,
  8109. // unless we do not support it, but support copy
  8110. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  8111. {
  8112. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  8113. }
  8114. else
  8115. {
  8116. // with ctrl-down, we want copy unless it is not supported
  8117. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  8118. }
  8119. }
  8120. }
  8121. }
  8122. }
  8123. }
  8124. //---------------------------------------------------------------------------
  8125. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  8126. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  8127. {
  8128. DebugAssert(!IsLocalBrowserMode());
  8129. if (FDDTotalSize >= 0)
  8130. {
  8131. if (File->IsDirectory)
  8132. {
  8133. FDDTotalSize = -1;
  8134. }
  8135. else
  8136. {
  8137. FDDTotalSize += File->Size;
  8138. }
  8139. }
  8140. DebugAssert(!FDragTempDir.IsEmpty());
  8141. // TODO: this is quite ineffective
  8142. // TODO: what if invalid character replacement is disabled?
  8143. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  8144. UnicodeString TransferFileName;
  8145. if (Sender == RemoteDriveView)
  8146. {
  8147. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  8148. }
  8149. else
  8150. {
  8151. TransferFileName = File->FileName;
  8152. }
  8153. FDDFileList->AddObject(TransferFileName, File);
  8154. }
  8155. //---------------------------------------------------------------------------
  8156. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  8157. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  8158. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  8159. {
  8160. // sometimes we do not get DDEnd so the list is not released
  8161. SAFE_DESTROY(FDDFileList);
  8162. FDDFileList = new TStringList();
  8163. FDragTempDir = WinConfiguration->TemporaryDir();
  8164. FDDTotalSize = 0;
  8165. FDragDropSshTerminate.reset(NULL);
  8166. }
  8167. //---------------------------------------------------------------------------
  8168. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  8169. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  8170. HRESULT & Result)
  8171. {
  8172. if (Result == DRAGDROP_S_DROP)
  8173. {
  8174. try
  8175. {
  8176. GlobalDragImageList->HideDragImage();
  8177. try
  8178. {
  8179. RemoteFileControlDDTargetDrop();
  8180. }
  8181. catch(ESshTerminate & E)
  8182. {
  8183. FDragDropSshTerminate.reset(E.Clone());
  8184. }
  8185. }
  8186. catch (Exception &E)
  8187. {
  8188. // If downloading fails we need to cancel drag&drop, otherwise
  8189. // Explorer shows error
  8190. // But by the way exception probably never reach this point as
  8191. // it's catched on way.
  8192. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8193. Result = DRAGDROP_S_CANCEL;
  8194. ShowExtendedException(Terminal, &E);
  8195. }
  8196. }
  8197. }
  8198. //---------------------------------------------------------------------------
  8199. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8200. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8201. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8202. {
  8203. TFileMasks::TParams MaskParams;
  8204. MaskParams.Size = Size;
  8205. MaskParams.Modification = Modification;
  8206. // this does not re-parse the mask if it is the same as the last time
  8207. FDirViewMatchMask = Masks;
  8208. bool ImplicitMatch;
  8209. Matches =
  8210. // Local and RecurseInclude parameters have no effect as there's no path.
  8211. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  8212. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  8213. (AllowImplicitMatches || !ImplicitMatch);
  8214. }
  8215. //---------------------------------------------------------------------------
  8216. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8217. TObject * Sender, TListItem * Item, WORD & Indexes)
  8218. {
  8219. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8220. UnicodeString Ext;
  8221. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  8222. {
  8223. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  8224. }
  8225. else
  8226. {
  8227. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  8228. }
  8229. if (SameText(Ext, Configuration->PartialExt))
  8230. {
  8231. Indexes |= oiPartial;
  8232. }
  8233. }
  8234. //---------------------------------------------------------------------------
  8235. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8236. {
  8237. return
  8238. DirViewEnabled(osCurrent) &&
  8239. DirView(osCurrent)->CanPasteFromClipBoard();
  8240. }
  8241. //---------------------------------------------------------------------------
  8242. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8243. {
  8244. bool Result = false;
  8245. if (CanPasteToDirViewFromClipBoard())
  8246. {
  8247. Result = true;
  8248. }
  8249. else
  8250. {
  8251. UnicodeString ClipboardText;
  8252. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8253. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8254. {
  8255. if (StoredSessions->IsUrl(ClipboardText))
  8256. {
  8257. Result = true;
  8258. }
  8259. else
  8260. {
  8261. Result = DirViewEnabled(osCurrent);
  8262. }
  8263. }
  8264. }
  8265. return Result;
  8266. }
  8267. //---------------------------------------------------------------------------
  8268. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8269. {
  8270. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8271. if (DoesClipboardContainOurFiles())
  8272. {
  8273. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8274. {
  8275. DebugAssert(!IsLocalBrowserMode());
  8276. TTerminalManager * Manager = TTerminalManager::Instance();
  8277. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8278. Manager->ActiveSession = FClipboardTerminal;
  8279. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8280. }
  8281. }
  8282. else if (CanPasteToDirViewFromClipBoard())
  8283. {
  8284. DirView(osCurrent)->PasteFromClipBoard();
  8285. }
  8286. else
  8287. {
  8288. UnicodeString ClipboardText;
  8289. if (NonEmptyTextFromClipboard(ClipboardText))
  8290. {
  8291. if (StoredSessions->IsUrl(ClipboardText))
  8292. {
  8293. NewSession(ClipboardText);
  8294. }
  8295. else
  8296. {
  8297. DirView(osCurrent)->Path = ClipboardText;
  8298. }
  8299. }
  8300. }
  8301. }
  8302. //---------------------------------------------------------------------------
  8303. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8304. {
  8305. // TBD
  8306. }
  8307. //---------------------------------------------------------------------------
  8308. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8309. {
  8310. TCustomDirView * ADirView = DirView(Side);
  8311. ADirView->SelectAll(Mode);
  8312. ADirView->SetFocus();
  8313. }
  8314. //---------------------------------------------------------------------------
  8315. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8316. {
  8317. TCustomDirView * ADirView = DirView(Side);
  8318. TFileFilter Filter;
  8319. DefaultFileFilter(Filter);
  8320. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8321. {
  8322. Configuration->Usage->Inc(L"MaskSelections");
  8323. ADirView->SelectFiles(Filter, Select);
  8324. ADirView->SetFocus();
  8325. }
  8326. }
  8327. //---------------------------------------------------------------------------
  8328. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8329. {
  8330. TCustomDirView * ADirView = DirView(Side);
  8331. ADirView->RestoreSelectedNames();
  8332. ADirView->SetFocus();
  8333. }
  8334. //---------------------------------------------------------------------------
  8335. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8336. {
  8337. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8338. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8339. {
  8340. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8341. UnicodeString Ext;
  8342. if (!IsSideLocalBrowser(osCurrent))
  8343. {
  8344. Ext = UnixExtractFileExt(FileName);
  8345. }
  8346. else
  8347. {
  8348. Ext = ExtractFileExt(FileName);
  8349. }
  8350. if (Ext.IsEmpty())
  8351. {
  8352. Ext = L".";
  8353. }
  8354. TFileFilter Filter;
  8355. Filter.Masks = FORMAT(L"*%s", (Ext));
  8356. Filter.Directories = false;
  8357. CurrentDirView->SelectFiles(Filter, Select);
  8358. }
  8359. }
  8360. //---------------------------------------------------------------------------
  8361. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8362. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8363. {
  8364. UnicodeString Result;
  8365. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8366. {
  8367. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8368. }
  8369. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8370. {
  8371. // noop
  8372. }
  8373. else
  8374. {
  8375. Result =
  8376. FMTLOAD(FILE_INFO_FORMAT,
  8377. (FormatBytes(FileInfo.SelectedSize),
  8378. FormatBytes(FileInfo.FilesSize),
  8379. FormatNumber(FileInfo.SelectedCount),
  8380. FormatNumber(FileInfo.FilesCount)));
  8381. }
  8382. return Result;
  8383. }
  8384. //---------------------------------------------------------------------------
  8385. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8386. TTBXStatusPanel * Panel, TOperationSide Side)
  8387. {
  8388. if (Panel->Index == 1)
  8389. {
  8390. ToggleShowHiddenFiles();
  8391. }
  8392. else if (Panel->Index == 2)
  8393. {
  8394. Filter(Side);
  8395. }
  8396. }
  8397. //---------------------------------------------------------------------------
  8398. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8399. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8400. {
  8401. DebugAssert(!StatusBar->SimplePanel);
  8402. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8403. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8404. }
  8405. //---------------------------------------------------------------------------
  8406. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8407. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8408. {
  8409. DebugAssert(StatusBar->Panels->Count >= 3);
  8410. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8411. if (FileInfo.HiddenCount > 0)
  8412. {
  8413. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8414. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8415. }
  8416. else
  8417. {
  8418. HiddenFilesPanel->ViewPriority = 0;
  8419. // not really necessary, just to cleanup no-longer-valid data
  8420. HiddenFilesPanel->Caption = L"";
  8421. }
  8422. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8423. if (FileInfo.FilteredCount > 0)
  8424. {
  8425. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8426. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8427. }
  8428. else
  8429. {
  8430. FilteredFilesPanel->ViewPriority = 0;
  8431. // not really necessary, just to cleanup no-longer-valid data
  8432. FilteredFilesPanel->Caption = L"";
  8433. }
  8434. }
  8435. //---------------------------------------------------------------------------
  8436. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8437. TObject * /*Sender*/)
  8438. {
  8439. if (DirView(osOther)->Enabled)
  8440. {
  8441. DirView(osOther)->SetFocus();
  8442. }
  8443. }
  8444. //---------------------------------------------------------------------------
  8445. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8446. {
  8447. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8448. switch (Config.Show)
  8449. {
  8450. case qvShow:
  8451. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8452. {
  8453. Config.Show = qvHide;
  8454. }
  8455. else
  8456. {
  8457. Config.Show = Config.LastHideShow;
  8458. }
  8459. break;
  8460. case qvHideWhenEmpty:
  8461. if (ComponentVisible[fcQueueView])
  8462. {
  8463. Config.Show = qvHide;
  8464. }
  8465. else
  8466. {
  8467. Config.LastHideShow = Config.Show;
  8468. Config.Show = qvShow;
  8469. }
  8470. break;
  8471. case qvHide:
  8472. Config.LastHideShow = Config.Show;
  8473. Config.Show = qvShow;
  8474. break;
  8475. }
  8476. WinConfiguration->QueueView = Config;
  8477. }
  8478. //---------------------------------------------------------------------------
  8479. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8480. {
  8481. UnicodeString Result;
  8482. if (Terminal != NULL)
  8483. {
  8484. switch (WinConfiguration->PathInCaption)
  8485. {
  8486. case picShort:
  8487. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8488. break;
  8489. case picFull:
  8490. Result = Terminal->CurrentDirectory;
  8491. break;
  8492. }
  8493. }
  8494. return Result;
  8495. }
  8496. //---------------------------------------------------------------------------
  8497. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8498. {
  8499. TColor Result = GetWindowColor(FSessionColor);
  8500. return Result;
  8501. }
  8502. //---------------------------------------------------------------------------
  8503. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8504. {
  8505. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8506. return Result;
  8507. }
  8508. //---------------------------------------------------------------------------
  8509. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8510. {
  8511. if (!FSessionChanging)
  8512. {
  8513. TTerminalManager::Instance()->UpdateAppTitle();
  8514. }
  8515. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8516. // background to gray on Windows 7 (works on Windows 10).
  8517. // See also EnableControl
  8518. if (Showing)
  8519. {
  8520. if (ManagedSession != NULL)
  8521. {
  8522. // Update path when it changes
  8523. if ((SessionsPageControl->ActivePage != NULL) &&
  8524. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8525. {
  8526. UpdateSessionTab(SessionsPageControl->ActivePage);
  8527. }
  8528. }
  8529. NonVisualDataModule->ReconnectSessionAction->Update();
  8530. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8531. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8532. CenterReconnectToolbar();
  8533. bool HasTerminal = HasActiveTerminal();
  8534. RemoteDirView->Enabled = HasTerminal;
  8535. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8536. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8537. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8538. RemoteDriveView->Color = RemoteDirView->Color;
  8539. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8540. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8541. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8542. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8543. QueueFileList->Enabled = QueueView3->Enabled;
  8544. QueueFileList->Color = QueueView3->Color;
  8545. QueueFileList->Font->Color = QueueView3->Font->Color;
  8546. QueueLabelUpdateStatus();
  8547. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8548. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8549. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8550. {
  8551. UpdateDarkMode();
  8552. }
  8553. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8554. MessageDock->Visible = (MessageDock->ToolbarCount > 0);
  8555. }
  8556. }
  8557. //---------------------------------------------------------------------------
  8558. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8559. {
  8560. UpdateControls();
  8561. }
  8562. //---------------------------------------------------------------------------
  8563. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8564. TObject * Sender)
  8565. {
  8566. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8567. DebugAssert(DirView != NULL);
  8568. DoDirViewLoaded(DirView);
  8569. TransferPresetAutoSelect();
  8570. }
  8571. //---------------------------------------------------------------------------
  8572. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8573. {
  8574. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8575. // first run after installation
  8576. if (double(Updates.LastCheck) == 0)
  8577. {
  8578. AppLog(L"First run, scheduling updates check for the next run");
  8579. // make sure next time there will be an update (if enabled)
  8580. Updates.LastCheck = TDateTime(1);
  8581. WinConfiguration->Updates = Updates;
  8582. }
  8583. else
  8584. {
  8585. TDateTime Period;
  8586. if (IsUWP() && !IsOfficialPackage())
  8587. {
  8588. Period = DefaultUpdatesPeriod;
  8589. AppLog(L"Thirdparty UWP package, using default updates check period");
  8590. }
  8591. else
  8592. {
  8593. Period = Updates.Period;
  8594. }
  8595. AppLogFmt(L"Updates check period: %.2f", (double(Period)));
  8596. if (double(Period) > 0)
  8597. {
  8598. TDateTime Interval = Now() - Updates.LastCheck;
  8599. AppLogFmt(L"Interval since the last updates check: %.2f", (double(Interval)));
  8600. if (Interval >= Period)
  8601. {
  8602. StartUpdateThread(UpdatesChecked);
  8603. }
  8604. }
  8605. }
  8606. }
  8607. //---------------------------------------------------------------------------
  8608. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8609. {
  8610. AppLog(L"Updates check done");
  8611. UnicodeString Message;
  8612. bool New;
  8613. TQueryType Type;
  8614. GetUpdatesMessage(Message, New, Type, false);
  8615. if (!Message.IsEmpty())
  8616. {
  8617. AppLogFmt(L"Updates check message: %s", (Message));
  8618. if (New)
  8619. {
  8620. AppLog(L"New version detected");
  8621. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8622. }
  8623. if (!New && (Type != qtWarning))
  8624. {
  8625. AppLog(L"Posting non-critical updates note");
  8626. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8627. }
  8628. else
  8629. {
  8630. AppLog(L"Posting new version or critical message notification");
  8631. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8632. }
  8633. if (New)
  8634. {
  8635. Configuration->Usage->Inc(L"UpdateNotifications");
  8636. }
  8637. }
  8638. }
  8639. //---------------------------------------------------------------------------
  8640. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8641. {
  8642. RestoreApp();
  8643. bool CanDisplay = !NonVisualDataModule->Busy;
  8644. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8645. {
  8646. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8647. }
  8648. if (CanDisplay)
  8649. {
  8650. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8651. CheckForUpdates(true);
  8652. }
  8653. else
  8654. {
  8655. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8656. }
  8657. }
  8658. //---------------------------------------------------------------------------
  8659. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8660. TCopyParamRuleData & Data)
  8661. {
  8662. DebugAssert(Terminal != NULL);
  8663. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8664. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8665. Data.RemoteDirectory = RemoteDirView->PathName;
  8666. }
  8667. //---------------------------------------------------------------------------
  8668. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8669. {
  8670. // Terminal can be null when we are changing local directory implicitly,
  8671. // because it has been deleted, while no session is connected
  8672. // (Login dialog is open)
  8673. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8674. {
  8675. DebugAssert(!IsLocalBrowserMode());
  8676. TCopyParamRuleData Data;
  8677. GetTransferPresetAutoSelectData(Data);
  8678. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8679. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8680. if (CopyParamIndex < 0)
  8681. {
  8682. // there is no preset that matches autoselection
  8683. // set preset that we consider "default"
  8684. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8685. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8686. }
  8687. else
  8688. {
  8689. // there is preset matching autoselection
  8690. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8691. if (CopyParamName == FCopyParamAutoSelected)
  8692. {
  8693. // autoselected the same preset as the last time
  8694. // make no change (i.e. preserve custom user preset, if any)
  8695. }
  8696. else
  8697. {
  8698. // autoselected the different preset then the last time (or there
  8699. // was default preset set)
  8700. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8701. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8702. }
  8703. }
  8704. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8705. {
  8706. if (CopyParamIndex >= 0)
  8707. {
  8708. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8709. }
  8710. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8711. try
  8712. {
  8713. int Fmt =
  8714. (CopyParamIndex < 0) ?
  8715. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8716. COPY_PARAM_AUTOSELECTED;
  8717. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8718. Data->Message = MainInstructions(Message);
  8719. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8720. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8721. CopyParamAttrs);
  8722. if (CopyParamIndex >= 0)
  8723. {
  8724. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8725. Info = FORMAT(L"%s\n \n%s", (Info,
  8726. FMTLOAD(COPY_PARAM_RULE,
  8727. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8728. }
  8729. Data->Message += L"\n\n" + Info;
  8730. if (WinConfiguration->CopyParamAutoSelectNotice)
  8731. {
  8732. TransferPresetNoteMessage(Data, true);
  8733. }
  8734. else
  8735. {
  8736. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8737. Data = NULL; // ownership passed
  8738. }
  8739. }
  8740. __finally
  8741. {
  8742. delete Data;
  8743. }
  8744. }
  8745. }
  8746. }
  8747. //---------------------------------------------------------------------------
  8748. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8749. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8750. {
  8751. DebugAssert(NoteData != NULL);
  8752. DebugAssert(!IsLocalBrowserMode());
  8753. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8754. TQueryButtonAlias Aliases[1];
  8755. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8756. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8757. Params.Aliases = Aliases;
  8758. Params.AliasesCount = LENOF(Aliases);
  8759. unsigned int Result =
  8760. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8761. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8762. switch (Result)
  8763. {
  8764. case qaNeverAskAgain:
  8765. DebugAssert(AllowNeverAskAgain);
  8766. WinConfiguration->CopyParamAutoSelectNotice = false;
  8767. break;
  8768. case qaIgnore:
  8769. PreferencesDialog(pmPresets);
  8770. break;
  8771. }
  8772. }
  8773. //---------------------------------------------------------------------------
  8774. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8775. {
  8776. // as of now useless, as this is used for notes only, never for balloons, ...
  8777. RestoreApp();
  8778. // .. and we should never be busy here
  8779. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8780. {
  8781. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8782. }
  8783. }
  8784. //---------------------------------------------------------------------------
  8785. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8786. TPreferencesMode APreferencesMode)
  8787. {
  8788. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8789. TCopyParamRuleData Data;
  8790. if (Terminal != NULL)
  8791. {
  8792. PreferencesData.reset(new TPreferencesDialogData());
  8793. GetTransferPresetAutoSelectData(Data);
  8794. PreferencesData->CopyParamRuleData = &Data;
  8795. }
  8796. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8797. }
  8798. //---------------------------------------------------------------------------
  8799. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8800. const TCustomCommandType & Command)
  8801. {
  8802. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8803. {
  8804. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8805. }
  8806. }
  8807. //---------------------------------------------------------------------------
  8808. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8809. {
  8810. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8811. TCustomCommandType Command;
  8812. // make sure we use local custom command when remote are not supported
  8813. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8814. {
  8815. Command = FLastCustomCommand;
  8816. }
  8817. else
  8818. {
  8819. Command.Params = Command.Params | ccLocal;
  8820. }
  8821. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8822. FEditingFocusedAdHocCommand = OnFocused;
  8823. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8824. int Options =
  8825. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8826. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8827. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8828. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8829. {
  8830. FLastCustomCommand = Command;
  8831. UpdateCustomCommandsToolbar();
  8832. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8833. }
  8834. }
  8835. //---------------------------------------------------------------------------
  8836. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8837. {
  8838. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8839. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8840. DebugAssert(State > 0);
  8841. if (State <= 0)
  8842. {
  8843. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8844. }
  8845. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8846. }
  8847. //---------------------------------------------------------------------------
  8848. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8849. TCustomCommandType & Command, int & State)
  8850. {
  8851. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8852. if (Result)
  8853. {
  8854. Command = FLastCustomCommand;
  8855. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8856. }
  8857. return Result;
  8858. }
  8859. //---------------------------------------------------------------------------
  8860. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8861. {
  8862. ResetIncrementalSearch();
  8863. }
  8864. //---------------------------------------------------------------------------
  8865. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8866. {
  8867. DebugAssert(ComponentVisible[Component]);
  8868. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8869. }
  8870. //---------------------------------------------------------------------------
  8871. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8872. {
  8873. // when queue panel is resized here directly, the status bar is stretched
  8874. // over whole space the panel occupied
  8875. PostComponentHide(fcQueueView);
  8876. }
  8877. //---------------------------------------------------------------------------
  8878. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8879. {
  8880. ComponentVisible[fcQueueFileList] = false;
  8881. }
  8882. //---------------------------------------------------------------------------
  8883. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8884. {
  8885. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8886. RefreshColorMode();
  8887. ResetSysDarkTheme();
  8888. ConfigurationChanged();
  8889. ConfigureInterface();
  8890. // Should be called for all controls
  8891. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8892. }
  8893. //---------------------------------------------------------------------------
  8894. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8895. {
  8896. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8897. if (!FInvalid &&
  8898. (Message.LParam != 0) &&
  8899. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8900. {
  8901. ThemeChanged();
  8902. }
  8903. TForm::Dispatch(&Message);
  8904. }
  8905. //---------------------------------------------------------------------------
  8906. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8907. {
  8908. TMessage * M = static_cast<TMessage*>(Message);
  8909. switch (M->Msg)
  8910. {
  8911. case CM_DIALOGCHAR:
  8912. CMDialogChar(*M);
  8913. break;
  8914. case WM_APPCOMMAND:
  8915. WMAppCommand(*M);
  8916. break;
  8917. case WM_SYSCOMMAND:
  8918. WMSysCommand(*M);
  8919. break;
  8920. case WM_QUERYENDSESSION:
  8921. WMQueryEndSession(*M);
  8922. break;
  8923. case WM_ENDSESSION:
  8924. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8925. break;
  8926. #ifdef _DEBUG
  8927. case WM_WINDOWPOSCHANGED:
  8928. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8929. break;
  8930. case CM_CUSTOMSTYLECHANGED:
  8931. TForm::Dispatch(Message);
  8932. break;
  8933. #endif
  8934. case WM_COMPONENT_HIDE:
  8935. {
  8936. Byte Component = static_cast<Byte>(M->WParam);
  8937. // sanity check
  8938. if (ComponentVisible[Component])
  8939. {
  8940. // special treatment
  8941. if (Component == fcQueueView)
  8942. {
  8943. ToggleQueueVisibility();
  8944. DebugAssert(!ComponentVisible[fcQueueView]);
  8945. }
  8946. else
  8947. {
  8948. ComponentVisible[Component] = false;
  8949. }
  8950. }
  8951. }
  8952. break;
  8953. case WM_COPYDATA:
  8954. WMCopyData(*M);
  8955. break;
  8956. case WM_MANAGES_CAPTION:
  8957. // caption managed in TTerminalManager::UpdateAppTitle()
  8958. M->Result = 1;
  8959. break;
  8960. case WM_WANTS_MOUSEWHEEL:
  8961. M->Result = 1;
  8962. break;
  8963. case CM_SHOWINGCHANGED:
  8964. CMShowingChanged(*M);
  8965. break;
  8966. case WM_CLOSE:
  8967. WMClose(*M);
  8968. break;
  8969. case WM_DPICHANGED:
  8970. WMDpiChanged(*M);
  8971. break;
  8972. case CM_DPICHANGED:
  8973. CMDpiChanged(*M);
  8974. break;
  8975. case WM_SETTINGCHANGE:
  8976. WMSettingChange(*M);
  8977. break;
  8978. case CM_DIALOGKEY:
  8979. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8980. break;
  8981. default:
  8982. TForm::Dispatch(Message);
  8983. break;
  8984. }
  8985. }
  8986. //---------------------------------------------------------------------------
  8987. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8988. {
  8989. // noop
  8990. }
  8991. //---------------------------------------------------------------------------
  8992. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8993. {
  8994. TForm::Dispatch(&Message);
  8995. FSessionColors->Clear();
  8996. AddFixedSessionImages();
  8997. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8998. UpdateImages();
  8999. }
  9000. //---------------------------------------------------------------------------
  9001. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  9002. {
  9003. TForm::Dispatch(&Message);
  9004. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  9005. UpdatePixelsPerInchMainWindowCounter();
  9006. }
  9007. //---------------------------------------------------------------------------
  9008. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  9009. {
  9010. // Cannot close window while we are busy.
  9011. // We cannot test this in FormCloseQuery as that is called also from
  9012. // Close(), which is called by CloseApplicationAction. So we can be busy
  9013. // there already even, when it is legitimate to close the application.
  9014. // Possibly a better place to handle this would be WMSysCommand.
  9015. if (NonVisualDataModule->Busy)
  9016. {
  9017. Message.Result = 1;
  9018. }
  9019. else
  9020. {
  9021. TForm::Dispatch(&Message);
  9022. }
  9023. }
  9024. //---------------------------------------------------------------------------
  9025. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  9026. {
  9027. if (!FShowingChanged)
  9028. {
  9029. AddStartupSequence(L"D");
  9030. FShowingChanged = true;
  9031. }
  9032. // Ignoring implicit local-local browser of Commander, opened from DoShow
  9033. bool NoSession = (ManagedSession == NULL);
  9034. // This calls TCustomScpExplorerForm::DoShow
  9035. TForm::Dispatch(&Message);
  9036. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  9037. if (Showing && !FStarted)
  9038. {
  9039. FStarted = true;
  9040. InterfaceStarted();
  9041. }
  9042. if (Showing && NoSession)
  9043. {
  9044. // When we are starting minimized (i.e. from an installer),
  9045. // postpone showing Login dialog until we get restored.
  9046. // Otherwise the Login dialog (and Authentication window) show restored
  9047. // over invisible (minimized) main window.
  9048. if (WindowState == wsMinimized)
  9049. {
  9050. FNeedSession = true;
  9051. }
  9052. else
  9053. {
  9054. // This happens before application ever goes idle, so the toolbars would
  9055. // stay enabled (initial state) until the Login dialog is dismissed.
  9056. UpdateActions();
  9057. NonVisualDataModule->StartBusy();
  9058. try
  9059. {
  9060. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  9061. // otherwise the dialog does not receive focus.
  9062. // With Commander interface the ProcessMessages is called already
  9063. // by TDriveView, but with Explorer interface, we need to call it explicily
  9064. Application->ProcessMessages();
  9065. // do not reload sessions, they have been loaded just now (optimization)
  9066. NeedSession(true);
  9067. }
  9068. __finally
  9069. {
  9070. NonVisualDataModule->EndBusy();
  9071. }
  9072. }
  9073. }
  9074. }
  9075. //---------------------------------------------------------------------------
  9076. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  9077. {
  9078. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  9079. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  9080. }
  9081. //---------------------------------------------------------------------------
  9082. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  9083. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  9084. int & MaxHeight)
  9085. {
  9086. // workaround for bug in TWinControl.CalcConstraints
  9087. // Check for empty rect (restore from iconic state) is done there only after
  9088. // call to AdjustClientRect, which enlarges the rect (for forms).
  9089. TRect R = GetClientRect();
  9090. // when restoring from iconic state, place no restrictions
  9091. if (IsRectEmpty(R))
  9092. {
  9093. MinWidth = 0;
  9094. MinHeight = 0;
  9095. MaxWidth = 32000;
  9096. MaxHeight = 32000;
  9097. }
  9098. }
  9099. //---------------------------------------------------------------------------
  9100. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  9101. TSpaceAvailable & ASpaceAvailable, bool & Close)
  9102. {
  9103. // terminal can be already closed (e.g. dropped connection)
  9104. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  9105. {
  9106. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  9107. try
  9108. {
  9109. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  9110. }
  9111. catch(...)
  9112. {
  9113. if (!Terminal->Active)
  9114. {
  9115. Close = true;
  9116. }
  9117. throw;
  9118. }
  9119. }
  9120. }
  9121. //---------------------------------------------------------------------------
  9122. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  9123. {
  9124. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  9125. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  9126. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  9127. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  9128. {
  9129. OnGetSpaceAvailable = GetSpaceAvailable;
  9130. }
  9131. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  9132. OnGetSpaceAvailable);
  9133. }
  9134. //---------------------------------------------------------------------------
  9135. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  9136. {
  9137. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  9138. Terminal->FillSessionDataForCode(Data.get());
  9139. return Data.release();
  9140. }
  9141. //---------------------------------------------------------------------------
  9142. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  9143. {
  9144. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  9145. DoGenerateUrlDialog(Data.get(), Paths);
  9146. }
  9147. //---------------------------------------------------------------------------
  9148. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  9149. {
  9150. GenerateUrl(NULL);
  9151. }
  9152. //---------------------------------------------------------------------------
  9153. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  9154. {
  9155. std::unique_ptr<TStrings> Paths(new TStringList());
  9156. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  9157. GenerateUrl(Paths.get());
  9158. }
  9159. //---------------------------------------------------------------------------
  9160. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  9161. {
  9162. FSessionColor = value;
  9163. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  9164. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  9165. static_cast<TObject *>(GetComponent(fcColorMenu)));
  9166. DebugAssert(ColorItem != NULL);
  9167. ColorItem->Color = C;
  9168. NonVisualDataModule->ColorMenuItem->Color = C;
  9169. UpdateControls();
  9170. }
  9171. //---------------------------------------------------------------------------
  9172. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  9173. {
  9174. if (value != FSessionColor)
  9175. {
  9176. UpdateSessionColor(value);
  9177. // Is null when called from LastTerminalClosed
  9178. if (Terminal != NULL)
  9179. {
  9180. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  9181. }
  9182. }
  9183. }
  9184. //---------------------------------------------------------------------------
  9185. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  9186. {
  9187. bool Result = FNoteTimer->Enabled;
  9188. if (Result)
  9189. {
  9190. // cannot cancel note too early
  9191. bool NotEarly =
  9192. (Now() - FNoteShown >
  9193. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  9194. if (Force || NotEarly)
  9195. {
  9196. FNoteTimer->Enabled = false;
  9197. FNote = L"";
  9198. SAFE_DESTROY(FNoteData);
  9199. FOnNoteClick = NULL;
  9200. FNoteHints = FNotes->Text;
  9201. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  9202. UpdateStatusBar();
  9203. }
  9204. }
  9205. return Result;
  9206. }
  9207. //---------------------------------------------------------------------------
  9208. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  9209. {
  9210. DebugAssert(FNoteTimer->Enabled);
  9211. CancelNote(true);
  9212. }
  9213. //---------------------------------------------------------------------------
  9214. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9215. {
  9216. FNoteHints = FNotes->Text.TrimRight();
  9217. }
  9218. //---------------------------------------------------------------------------
  9219. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9220. {
  9221. int P = Note.Pos(L"\n");
  9222. if (P > 0)
  9223. {
  9224. Note.SetLength(P - 1);
  9225. }
  9226. FNotes->Add(FORMAT(L"[%s] %s",
  9227. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9228. while (FNotes->Count > 10)
  9229. {
  9230. FNotes->Delete(0);
  9231. }
  9232. if (UpdateNow)
  9233. {
  9234. UpdateNoteHints();
  9235. UpdateStatusBar();
  9236. }
  9237. }
  9238. //---------------------------------------------------------------------------
  9239. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  9240. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9241. {
  9242. int P = Note.Pos(L"\n");
  9243. if (P > 0)
  9244. {
  9245. Note.SetLength(P - 1);
  9246. }
  9247. UpdateNoteHints();
  9248. FNote = Note;
  9249. SAFE_DESTROY(FNoteData);
  9250. FNoteData = NoteData;
  9251. FOnNoteClick = OnNoteClick;
  9252. AddNote(Note, false);
  9253. UpdateStatusBar();
  9254. FNoteShown = Now();
  9255. FNoteTimer->Enabled = false;
  9256. if (Seconds == 0)
  9257. {
  9258. Seconds = WinConfiguration->NotificationsTimeout;
  9259. }
  9260. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9261. FNoteTimer->Enabled = true;
  9262. }
  9263. //---------------------------------------------------------------------------
  9264. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9265. {
  9266. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9267. }
  9268. //---------------------------------------------------------------------------
  9269. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9270. {
  9271. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9272. {
  9273. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9274. }
  9275. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9276. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9277. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9278. 0, NULL, NULL);
  9279. }
  9280. //---------------------------------------------------------------------------
  9281. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9282. {
  9283. bool NewShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9284. UnicodeString Note =
  9285. FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1), (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, (NewShowHiddenFiles ? 2 : 3))));
  9286. PostNote(Note, 0, NULL, NULL);
  9287. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9288. WinConfiguration->ShowHiddenFiles = NewShowHiddenFiles;
  9289. }
  9290. //---------------------------------------------------------------------------
  9291. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9292. {
  9293. WinConfiguration->FormatSizeBytes = Style;
  9294. }
  9295. //---------------------------------------------------------------------------
  9296. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9297. {
  9298. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9299. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9300. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9301. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9302. }
  9303. //---------------------------------------------------------------------------
  9304. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9305. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9306. {
  9307. if (Panel->Index == 0)
  9308. {
  9309. if (FOnNoteClick != NULL)
  9310. {
  9311. // prevent the user data from being freed by possible call
  9312. // to CancelNote or PostNote during call to OnNoteClick
  9313. std::unique_ptr<TObject> NoteData(FNoteData);
  9314. TNotifyEvent OnNoteClick = FOnNoteClick;
  9315. FNoteData = NULL;
  9316. // need to cancel the note as we are going to delete its user data
  9317. CancelNote(true);
  9318. OnNoteClick(NoteData.get());
  9319. }
  9320. }
  9321. if (HasActiveTerminal() &&
  9322. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9323. {
  9324. FileSystemInfo();
  9325. }
  9326. }
  9327. //---------------------------------------------------------------------------
  9328. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9329. {
  9330. // workaround:
  9331. // 1) for unknown reason, disabling window, while minimized,
  9332. // prevents it from restoring, even if it was enabled again meanwhile
  9333. // 2) when disabling the main window, while another has focus
  9334. // minimize is no longer possible
  9335. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9336. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9337. // Shouldn't we use IsApplicationMinimized() here?
  9338. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9339. {
  9340. Enabled = false;
  9341. }
  9342. FLockLevel++;
  9343. }
  9344. //---------------------------------------------------------------------------
  9345. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9346. {
  9347. DebugAssert(FLockLevel > 0);
  9348. FLockLevel--;
  9349. if (FLockLevel == 0)
  9350. {
  9351. DebugAssert(FLockSuspendLevel == 0);
  9352. Enabled = true;
  9353. }
  9354. }
  9355. //---------------------------------------------------------------------------
  9356. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9357. {
  9358. // We need to make sure that window is enabled when the last modal window closes
  9359. // otherwise focus is not restored correctly.
  9360. // So we re-enable the window when modal window opens and
  9361. // disable it back after is closes
  9362. if (FLockLevel > 0)
  9363. {
  9364. // while we have nesting counter, we know that we never be called
  9365. // recursivelly as Application->OnModalBegin is called only
  9366. // for the top-level modal window
  9367. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9368. {
  9369. // won't be disabled when conditions in LockWindow() were not satisfied
  9370. FDisabledOnLockSuspend = !Enabled;
  9371. // When minimized to tray (or actually when set to SW_HIDE),
  9372. // setting Enabled makes the window focusable even when there's
  9373. // modal window over it
  9374. if (!FTrayIcon->Visible)
  9375. {
  9376. Enabled = true;
  9377. }
  9378. }
  9379. FLockSuspendLevel++;
  9380. }
  9381. }
  9382. //---------------------------------------------------------------------------
  9383. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9384. {
  9385. if (FLockSuspendLevel > 0)
  9386. {
  9387. DebugAssert(FLockLevel > 0);
  9388. FLockSuspendLevel--;
  9389. // see comment in SuspendWindowLock
  9390. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9391. {
  9392. // Note that window can be enabled here, when we were minized to tray when
  9393. // was SuspendWindowLock() called.
  9394. // We should possibly do the same check as in LockWindow(),
  9395. // if it is ever possible that the consitions change between
  9396. // SuspendWindowLock() and ResumeWindowLock()
  9397. Enabled = !FDisabledOnLockSuspend;
  9398. }
  9399. }
  9400. }
  9401. //---------------------------------------------------------------------------
  9402. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9403. {
  9404. TTBXComboBoxItem * RemotePathComboBox =
  9405. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9406. if (!TextOnly)
  9407. {
  9408. TStrings * Items = RemotePathComboBox->Strings;
  9409. Items->BeginUpdate();
  9410. try
  9411. {
  9412. Items->Clear();
  9413. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9414. {
  9415. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9416. while (!IsUnixRootPath(APath))
  9417. {
  9418. int P = APath.LastDelimiter(L'/');
  9419. DebugAssert(P >= 0);
  9420. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9421. APath.SetLength(P - 1);
  9422. }
  9423. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9424. }
  9425. }
  9426. __finally
  9427. {
  9428. Items->EndUpdate();
  9429. }
  9430. }
  9431. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9432. // Setting ItemIndex to -1 does not reset its text
  9433. if (RemotePathComboBox->Strings->Count == 0)
  9434. {
  9435. RemotePathComboBox->Text = L"";
  9436. }
  9437. }
  9438. //---------------------------------------------------------------------------
  9439. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9440. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9441. {
  9442. if (AIndex < 0)
  9443. {
  9444. AIndex = Sender->ItemIndex;
  9445. }
  9446. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9447. }
  9448. //---------------------------------------------------------------------------
  9449. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9450. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9451. int & ImageIndex)
  9452. {
  9453. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9454. }
  9455. //---------------------------------------------------------------------------
  9456. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9457. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9458. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9459. {
  9460. if (!IsLocalBrowserMode())
  9461. {
  9462. ARect.Left += (10 * AIndex);
  9463. }
  9464. }
  9465. //---------------------------------------------------------------------------
  9466. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9467. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9468. {
  9469. AWidth += (10 * AIndex);
  9470. }
  9471. //---------------------------------------------------------------------------
  9472. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9473. {
  9474. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9475. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9476. int Index = RemotePathComboBox->ItemIndex;
  9477. while (Index < RemotePathComboBox->Strings->Count - 1)
  9478. {
  9479. APath = UnixExtractFileDir(APath);
  9480. Index++;
  9481. }
  9482. // VanDyke style paths
  9483. if (APath.IsEmpty())
  9484. {
  9485. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9486. APath = ROOTDIRECTORY;
  9487. }
  9488. if (RemoteDirView->Path != APath)
  9489. {
  9490. RemoteDirView->Path = APath;
  9491. }
  9492. }
  9493. //---------------------------------------------------------------------------
  9494. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9495. {
  9496. DoRemotePathComboBoxItemClick(Sender);
  9497. }
  9498. //---------------------------------------------------------------------------
  9499. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9500. {
  9501. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9502. DebugUsedParam(Sender);
  9503. UpdateRemotePathComboBox(true);
  9504. }
  9505. //---------------------------------------------------------------------------
  9506. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9507. {
  9508. DoRemotePathComboBoxCancel(Sender);
  9509. }
  9510. //---------------------------------------------------------------------------
  9511. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9512. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9513. {
  9514. ResetIncrementalSearch();
  9515. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9516. DebugAssert(DirView != NULL);
  9517. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9518. {
  9519. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9520. // OnEditing is called also from TCustomListView::CanEdit
  9521. if (Edit != NULL)
  9522. {
  9523. EditSelectBaseName(Edit);
  9524. }
  9525. }
  9526. }
  9527. //---------------------------------------------------------------------------
  9528. //---------------------------------------------------------------------------
  9529. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9530. {
  9531. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9532. Result->AutoDetectDnD = false;
  9533. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9534. Result->RenderDataOn = rdoEnterAndDropSync;
  9535. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9536. return Result;
  9537. }
  9538. //---------------------------------------------------------------------------
  9539. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9540. {
  9541. if (IsWin10Build(2004))
  9542. {
  9543. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9544. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9545. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9546. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9547. }
  9548. }
  9549. //---------------------------------------------------------------------------
  9550. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9551. {
  9552. TForm::CreateWnd();
  9553. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9554. UpdateDarkMode();
  9555. if (FSessionsDragDropFilesEx == NULL)
  9556. {
  9557. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9558. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9559. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9560. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9561. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9562. }
  9563. if (FQueueDragDropFilesEx == NULL)
  9564. {
  9565. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9566. // No need to set OnDragOver as we do not have any restrictions
  9567. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9568. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9569. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9570. }
  9571. }
  9572. //---------------------------------------------------------------------------
  9573. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9574. {
  9575. TForm::DestroyWnd();
  9576. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9577. FQueueDragDropFilesEx->DragDropControl = NULL;
  9578. }
  9579. //---------------------------------------------------------------------------
  9580. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9581. {
  9582. SideEnter(FCurrentSide);
  9583. FEverShown = true;
  9584. }
  9585. //---------------------------------------------------------------------------
  9586. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9587. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9588. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9589. {
  9590. if (!NonVisualDataModule->Busy)
  9591. {
  9592. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9593. Configuration->Usage->Inc(L"FileFinds");
  9594. LockWindow(true);
  9595. NonVisualDataModule->StartBusy();
  9596. WinConfiguration->LockedInterface = true;
  9597. try
  9598. {
  9599. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9600. }
  9601. __finally
  9602. {
  9603. WinConfiguration->LockedInterface = false;
  9604. NonVisualDataModule->EndBusy();
  9605. UnlockWindow();
  9606. }
  9607. }
  9608. }
  9609. //---------------------------------------------------------------------------
  9610. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9611. {
  9612. if (!NonVisualDataModule->Busy)
  9613. {
  9614. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9615. SetFocus();
  9616. RemoteDirView->Path = UnixExtractFilePath(Path);
  9617. UnicodeString FileName = UnixExtractFileName(Path);
  9618. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9619. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9620. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9621. if (Item == NULL)
  9622. {
  9623. ReloadDirectory(osRemote);
  9624. Item = RemoteDirView->FindFileItem(FileName);
  9625. }
  9626. if (Item != NULL)
  9627. {
  9628. RemoteDirView->ItemFocused = Item;
  9629. RemoteDirView->ItemFocused->MakeVisible(false);
  9630. RemoteDirView->SetFocus();
  9631. }
  9632. }
  9633. }
  9634. //---------------------------------------------------------------------------
  9635. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9636. {
  9637. bool Result = !NonVisualDataModule->Busy;
  9638. if (Result)
  9639. {
  9640. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9641. }
  9642. return Result;
  9643. }
  9644. //---------------------------------------------------------------------------
  9645. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9646. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9647. {
  9648. if (CanOperateOnFoundFiles(ATerminal))
  9649. {
  9650. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9651. FOnFileOperationFinished = OnFileOperationFinished;
  9652. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9653. }
  9654. }
  9655. //---------------------------------------------------------------------------
  9656. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9657. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9658. {
  9659. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9660. }
  9661. //---------------------------------------------------------------------------
  9662. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9663. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9664. {
  9665. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9666. }
  9667. //---------------------------------------------------------------------------
  9668. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9669. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9670. {
  9671. if (CanOperateOnFoundFiles(ATerminal))
  9672. {
  9673. for (int Index = 0; Index < FileList->Count; Index++)
  9674. {
  9675. UnicodeString FileName = FileList->Strings[Index];
  9676. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9677. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9678. OnFileOperationFinished(osRemote, FileName, true);
  9679. }
  9680. }
  9681. }
  9682. //---------------------------------------------------------------------------
  9683. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9684. {
  9685. FFileFindTerminal = Terminal;
  9686. ShowFileFindDialog(
  9687. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9688. DoDownloadFoundFiles, DoEditFoundFiles);
  9689. }
  9690. //---------------------------------------------------------------------------
  9691. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9692. {
  9693. FTaskbarList = TaskbarList;
  9694. }
  9695. //---------------------------------------------------------------------------
  9696. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9697. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9698. {
  9699. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9700. if (Index >= 0)
  9701. {
  9702. if (Button == mbLeft)
  9703. {
  9704. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9705. // If switching tab (switching session) takes long enough for user
  9706. // to actually release the button, "mouse down" is still raised,
  9707. // but we do not get "mouse up" event, so dragging is not cancelled,
  9708. // prevent that by not beginning dragging in the first place.
  9709. if (IsKeyPressed(VK_LBUTTON))
  9710. {
  9711. // when user clicks the "+", we get mouse down only after the session
  9712. // is closed, when new session tab is already on X:Y, so dragging
  9713. // starts, prevent that
  9714. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9715. {
  9716. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9717. if (Terminal != NULL)
  9718. {
  9719. SessionsPageControl->BeginDrag(false);
  9720. }
  9721. }
  9722. }
  9723. }
  9724. else if (Button == mbMiddle)
  9725. {
  9726. // ignore middle-click for "New session tab" and the last local-local tab
  9727. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9728. if ((Session != NULL) && CanCloseSession(Session))
  9729. {
  9730. CloseSessionTab(Index);
  9731. }
  9732. }
  9733. }
  9734. }
  9735. //---------------------------------------------------------------------------
  9736. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9737. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9738. {
  9739. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9740. // do not allow dropping on the "+" tab
  9741. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9742. if ((TargetTerminal != NULL) &&
  9743. (SessionsPageControl->ActivePage->PageIndex != Index))
  9744. {
  9745. Configuration->Usage->Inc(L"SessionTabMoves");
  9746. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9747. // but we want to actually prevent that to avoid flicker
  9748. SessionsPageControl->ActivePage->PageIndex = Index;
  9749. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9750. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9751. }
  9752. }
  9753. //---------------------------------------------------------------------------
  9754. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9755. TObject * Sender, TObject * Source, int X, int Y,
  9756. TDragState /*State*/, bool & Accept)
  9757. {
  9758. Accept = (Sender == Source);
  9759. if (Accept)
  9760. {
  9761. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9762. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9763. // do not allow dragging to the "+" tab
  9764. Accept = (Terminal != NULL);
  9765. }
  9766. }
  9767. //---------------------------------------------------------------------------
  9768. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9769. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9770. {
  9771. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9772. if (Index < 0)
  9773. {
  9774. Effect = DROPEFFECT_NONE;
  9775. }
  9776. else
  9777. {
  9778. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9779. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9780. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9781. {
  9782. Effect = DROPEFFECT_NONE;
  9783. }
  9784. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9785. {
  9786. Effect = DROPEFFECT_COPY;
  9787. }
  9788. }
  9789. }
  9790. //---------------------------------------------------------------------------
  9791. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9792. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9793. {
  9794. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9795. // do not allow dropping on local-local, disconnected and the "+" tab
  9796. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9797. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9798. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9799. {
  9800. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9801. if (!IsFileControl(DropSourceControl, osRemote))
  9802. {
  9803. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9804. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9805. // Why don't we use Terminal->CurrentDirectory directly?
  9806. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9807. // do not force queue, drag drop
  9808. false, true);
  9809. }
  9810. }
  9811. }
  9812. //---------------------------------------------------------------------------
  9813. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9814. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9815. {
  9816. // Downloads are handled in RemoteFileControlDDEnd
  9817. if (!IsFileControl(DropSourceControl, osRemote))
  9818. {
  9819. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9820. Terminal->CurrentDirectory,
  9821. // force queue, drag drop
  9822. true, true);
  9823. }
  9824. }
  9825. //---------------------------------------------------------------------------
  9826. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9827. {
  9828. FShowing = false;
  9829. // Do not save empty workspace
  9830. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9831. {
  9832. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9833. UnicodeString Name = WorkspaceName();
  9834. DoSaveWorkspace(
  9835. Name, DataList.get(),
  9836. !Configuration->DisablePasswordStoring &&
  9837. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9838. WinConfiguration->LastStoredSession = Name;
  9839. }
  9840. }
  9841. //---------------------------------------------------------------------------
  9842. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9843. {
  9844. TManagedTerminal * ManagedTerminal =
  9845. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9846. if (ManagedTerminal != NULL)
  9847. {
  9848. ManagedTerminal->DirectoryLoaded = Now();
  9849. }
  9850. }
  9851. //---------------------------------------------------------------------------
  9852. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9853. TListItem * /*Item*/, bool /*Selected*/)
  9854. {
  9855. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9856. switch (DirView->LastSelectMethod)
  9857. {
  9858. case smKeyboard:
  9859. Configuration->Usage->Inc(L"KeyboardSelections");
  9860. break;
  9861. case smMouse:
  9862. Configuration->Usage->Inc(L"MouseSelections");
  9863. break;
  9864. }
  9865. }
  9866. //---------------------------------------------------------------------------
  9867. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9868. {
  9869. if (Color != 0)
  9870. {
  9871. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9872. }
  9873. else
  9874. {
  9875. return FSessionTabImageIndex;
  9876. }
  9877. }
  9878. //---------------------------------------------------------------------------
  9879. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9880. {
  9881. TPngImageCollectionItem * Item =
  9882. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9883. FSessionColors->AddPng(Item->PngImage);
  9884. return FSessionColors->Count - 1;
  9885. }
  9886. //---------------------------------------------------------------------------
  9887. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9888. {
  9889. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9890. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  9891. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  9892. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9893. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9894. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  9895. }
  9896. //---------------------------------------------------------------------------
  9897. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  9898. {
  9899. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  9900. }
  9901. //---------------------------------------------------------------------------
  9902. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  9903. {
  9904. switch (Side)
  9905. {
  9906. case osLocal:
  9907. return FNewLocalTabTabImageIndex;
  9908. case osRemote:
  9909. case osCurrent:
  9910. return FNewRemoteTabTabImageIndex;
  9911. default:
  9912. DebugFail();
  9913. return -1;
  9914. }
  9915. }
  9916. //---------------------------------------------------------------------------
  9917. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9918. {
  9919. for (int Index = 0; Index < Control->ControlCount; Index++)
  9920. {
  9921. TControl * AControl = Control->Controls[Index];
  9922. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9923. if (WinControl != NULL)
  9924. {
  9925. CollectItemsWithTextDisplayMode(WinControl);
  9926. }
  9927. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9928. if (Toolbar != NULL)
  9929. {
  9930. // we care for top-level items only
  9931. TTBCustomItem * Items = Toolbar->Items;
  9932. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9933. {
  9934. TTBCustomItem * Item = Items->Items[ItemIndex];
  9935. if (((Item->DisplayMode == nbdmImageAndText) ||
  9936. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9937. EligibleForImageDisplayMode(Item))
  9938. {
  9939. FItemsWithTextDisplayMode.insert(Item);
  9940. }
  9941. }
  9942. }
  9943. }
  9944. }
  9945. //---------------------------------------------------------------------------
  9946. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9947. {
  9948. return true;
  9949. }
  9950. //---------------------------------------------------------------------------
  9951. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9952. {
  9953. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9954. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9955. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9956. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9957. bool Result = true;
  9958. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9959. {
  9960. TTBCustomItem * Item = *i;
  9961. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9962. if (Label != NULL)
  9963. {
  9964. // optimization
  9965. if (Label->Visible == SelectiveToolbarText)
  9966. {
  9967. Result = false;
  9968. }
  9969. else
  9970. {
  9971. Label->Visible = SelectiveToolbarText;
  9972. }
  9973. }
  9974. else
  9975. {
  9976. // optimization
  9977. if (Item->DisplayMode == DisplayMode)
  9978. {
  9979. Result = false;
  9980. }
  9981. else
  9982. {
  9983. Item->DisplayMode = DisplayMode;
  9984. }
  9985. }
  9986. ++i;
  9987. }
  9988. UpdateNewTabTab();
  9989. return Result;
  9990. }
  9991. //---------------------------------------------------------------------------
  9992. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9993. {
  9994. DebugFail();
  9995. }
  9996. //---------------------------------------------------------------------------
  9997. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9998. {
  9999. // Among other, a lock level is non zero, while directory is being loaded,
  10000. // even when it happens as a result of dirview navigation,
  10001. // i.e. when TNonVisualDataModule is NOT FBusy.
  10002. // That's why the TNonVisualDataModule::GetBusy calls this method.
  10003. // Among other this prevents a panel auto update to occur while
  10004. // directory is changing.
  10005. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  10006. }
  10007. //---------------------------------------------------------------------------
  10008. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  10009. {
  10010. // While the window is disabled, we still seem to process menu shortcuts at least,
  10011. // so stop it at least here.
  10012. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  10013. bool Result;
  10014. if (Allowed == aaUpdate)
  10015. {
  10016. Result = true;
  10017. }
  10018. else
  10019. {
  10020. Result = !NonVisualDataModule->Busy;
  10021. if (Result && (Allowed == aaShortCut))
  10022. {
  10023. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  10024. Action->Update();
  10025. Result = Action->Enabled;
  10026. }
  10027. }
  10028. return Result;
  10029. }
  10030. //---------------------------------------------------------------------------
  10031. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  10032. {
  10033. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  10034. }
  10035. //---------------------------------------------------------------------------
  10036. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  10037. {
  10038. // This is somewhat redundant to LockWindow() call from
  10039. // TTerminalManager::TerminalReadDirectoryProgress.
  10040. // But disabling window is known not to block keyboard shorcuts
  10041. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  10042. if (Busy > 0)
  10043. {
  10044. if (NonVisualDataModule->Busy)
  10045. {
  10046. State = false;
  10047. }
  10048. else
  10049. {
  10050. NonVisualDataModule->StartBusy();
  10051. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  10052. LockWindow();
  10053. }
  10054. }
  10055. else if (Busy < 0)
  10056. {
  10057. UnlockWindow();
  10058. NonVisualDataModule->EndBusy();
  10059. }
  10060. else
  10061. {
  10062. State = NonVisualDataModule->Busy;
  10063. }
  10064. }
  10065. //---------------------------------------------------------------------------
  10066. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  10067. {
  10068. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  10069. if (Index >= 0)
  10070. {
  10071. TPopupMenu * PopupMenu = NULL;
  10072. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10073. if (Session != NULL)
  10074. {
  10075. SessionsPageControl->ActivePageIndex = Index;
  10076. if (DebugAlwaysTrue(SessionTabSwitched()))
  10077. {
  10078. // copied from TControl.WMContextMenu
  10079. SendCancelMode(SessionsPageControl);
  10080. // explicit popup instead of using PopupMenu property
  10081. // to avoid menu to popup somewhere within SessionTabSwitched above,
  10082. // while connecting yet not-connected session and hence
  10083. // allowing an access to commands over not-completelly connected session
  10084. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  10085. }
  10086. }
  10087. else
  10088. {
  10089. if (SupportsLocalBrowser())
  10090. {
  10091. PopupMenu = NonVisualDataModule->NewTabPopup;
  10092. }
  10093. }
  10094. if (PopupMenu != NULL)
  10095. {
  10096. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  10097. PopupMenu->PopupComponent = SessionsPageControl;
  10098. PopupMenu->Popup(Point.x, Point.y);
  10099. }
  10100. }
  10101. Handled = true;
  10102. }
  10103. //---------------------------------------------------------------------------
  10104. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  10105. {
  10106. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  10107. }
  10108. //---------------------------------------------------------------------------
  10109. void __fastcall TCustomScpExplorerForm::ChangePassword()
  10110. {
  10111. Configuration->Usage->Inc(L"PasswordChanges");
  10112. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  10113. ChangePasswordData->DisableAuthentationsExceptPassword();
  10114. ChangePasswordData->ChangePassword = true;
  10115. std::unique_ptr<TTerminal> ChangePasswordSession(
  10116. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  10117. ChangePasswordSession->AutoReadDirectory = false;
  10118. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  10119. {
  10120. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  10121. }
  10122. else
  10123. {
  10124. Configuration->Usage->Inc(L"PasswordChangesFailed");
  10125. }
  10126. }
  10127. //---------------------------------------------------------------------------
  10128. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  10129. {
  10130. // No nice way to assert SSH2
  10131. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  10132. }
  10133. //---------------------------------------------------------------------------
  10134. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  10135. {
  10136. TOperationVisualizer OperationVisualizer;
  10137. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  10138. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  10139. }
  10140. //---------------------------------------------------------------------------
  10141. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  10142. {
  10143. TForm::ChangeScale(M, D);
  10144. int APixelsPerInch = GetControlPixelsPerInch(this);
  10145. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  10146. try
  10147. {
  10148. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  10149. }
  10150. __finally
  10151. {
  10152. GUIConfiguration->ChangeResourceModule(ResourceModule);
  10153. }
  10154. }
  10155. //---------------------------------------------------------------------------
  10156. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  10157. {
  10158. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  10159. }
  10160. //---------------------------------------------------------------------
  10161. class TPublicControl : public TControl
  10162. {
  10163. friend class TCustomScpExplorerForm;
  10164. };
  10165. //---------------------------------------------------------------------------
  10166. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  10167. {
  10168. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  10169. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  10170. }
  10171. //---------------------------------------------------------------------------
  10172. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  10173. {
  10174. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  10175. OpenBookmark(Side, Bookmark);
  10176. }
  10177. //---------------------------------------------------------------------------
  10178. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  10179. {
  10180. DoBookmarkClick(osLocal, Sender);
  10181. }
  10182. //---------------------------------------------------------------------------
  10183. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  10184. {
  10185. DoBookmarkClick(osRemote, Sender);
  10186. }
  10187. //---------------------------------------------------------------------------
  10188. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  10189. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  10190. {
  10191. if (BookmarkList != NULL)
  10192. {
  10193. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  10194. if (IsLocalBrowserMode())
  10195. {
  10196. Side = osLocal;
  10197. }
  10198. if (!WinConfiguration->UseLocationProfiles)
  10199. {
  10200. std::unique_ptr<TStringList> Directories(new TStringList());
  10201. Directories->CaseSensitive = (Side == osRemote);
  10202. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10203. {
  10204. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10205. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  10206. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  10207. {
  10208. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10209. Item->Caption = EscapeHotkey(Directory);
  10210. Item->ShortCut = Bookmark->ShortCut;
  10211. Item->OnClick = OnBookmarkClick;
  10212. Item->Tag = reinterpret_cast<int>(Bookmark);
  10213. Directories->Add(Directory);
  10214. Menu->Add(Item.release());
  10215. }
  10216. }
  10217. }
  10218. else
  10219. {
  10220. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10221. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10222. {
  10223. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10224. if (!Bookmark->Node.IsEmpty())
  10225. {
  10226. Folders->Add(Bookmark->Node);
  10227. }
  10228. }
  10229. for (int Index = 0; Index < Folders->Count; Index++)
  10230. {
  10231. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10232. Item->Caption = Folders->Strings[Index];
  10233. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10234. Folders->Objects[Index] = Item.get();
  10235. Menu->Add(Item.release());
  10236. }
  10237. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10238. {
  10239. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10240. TTBCustomItem * Parent;
  10241. if (!Bookmark->Node.IsEmpty())
  10242. {
  10243. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10244. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10245. }
  10246. else
  10247. {
  10248. Parent = Menu;
  10249. }
  10250. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10251. Item->Caption = Bookmark->Name;
  10252. Item->ShortCut = Bookmark->ShortCut;
  10253. Item->OnClick = OnBookmarkClick;
  10254. Item->Tag = reinterpret_cast<int>(Bookmark);
  10255. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10256. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10257. {
  10258. // No hint for location profiles that are actually mere "bookmarks"
  10259. }
  10260. else
  10261. {
  10262. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10263. UnicodeString LongHint;
  10264. if (!Bookmark->Local.IsEmpty())
  10265. {
  10266. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10267. }
  10268. if (!Bookmark->Remote.IsEmpty())
  10269. {
  10270. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10271. }
  10272. AddToList(Hint, LongHint, L"|");
  10273. Item->Hint = Hint;
  10274. }
  10275. Parent->Add(Item.release());
  10276. }
  10277. }
  10278. AddMenuSeparator(Menu);
  10279. }
  10280. }
  10281. //---------------------------------------------------------------------------
  10282. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10283. {
  10284. Menu->Clear();
  10285. std::unique_ptr<TTBCustomItem> Item;
  10286. Item.reset(new TTBXItem(Owner));
  10287. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10288. Item->Options = Item->Options << tboDefault;
  10289. Menu->Add(Item.release());
  10290. AddMenuSeparator(Menu);
  10291. if (Terminal != NULL)
  10292. {
  10293. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10294. }
  10295. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10296. Item.reset(new TTBXItem(Owner));
  10297. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10298. Menu->Add(Item.release());
  10299. }
  10300. //---------------------------------------------------------------------------
  10301. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10302. {
  10303. bool Result = true;
  10304. bool Remote = !IsSideLocalBrowser(Side);
  10305. try
  10306. {
  10307. UnicodeString APath = Path;
  10308. if (Remote)
  10309. {
  10310. Terminal->ExceptionOnFail = true;
  10311. }
  10312. else
  10313. {
  10314. APath = ExpandEnvironmentVariables(Path);
  10315. }
  10316. try
  10317. {
  10318. DirView(Side)->Path = APath;
  10319. }
  10320. __finally
  10321. {
  10322. if (Remote)
  10323. {
  10324. Terminal->ExceptionOnFail = false;
  10325. }
  10326. }
  10327. }
  10328. catch (Exception & E)
  10329. {
  10330. if (!Remote || Terminal->Active)
  10331. {
  10332. ShowExtendedExceptionEx(Terminal, &E);
  10333. Result = false;
  10334. }
  10335. else
  10336. {
  10337. throw;
  10338. }
  10339. }
  10340. return Result;
  10341. }
  10342. //---------------------------------------------------------------------------
  10343. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10344. {
  10345. // Make sure there some feedback even when loading tiny local folders
  10346. TInstantOperationVisualizer Visualizer;
  10347. DirView(Side)->ReloadDirectory();
  10348. }
  10349. //---------------------------------------------------------------------------
  10350. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10351. {
  10352. if (Index == SessionsPageControl->TabIndex)
  10353. {
  10354. CloseSession();
  10355. }
  10356. else
  10357. {
  10358. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10359. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10360. TTerminalManager * Manager = TTerminalManager::Instance();
  10361. if (!Terminal->Active)
  10362. {
  10363. Manager->FreeTerminal(Terminal);
  10364. }
  10365. else
  10366. {
  10367. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10368. if (CanCloseQueue(Queue))
  10369. {
  10370. Manager->FreeTerminal(Terminal);
  10371. }
  10372. }
  10373. }
  10374. }
  10375. //---------------------------------------------------------------------------
  10376. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10377. {
  10378. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10379. if (Session == NULL)
  10380. {
  10381. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10382. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10383. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10384. //P = SessionsPageControl->ClientToScreen(P);
  10385. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10386. }
  10387. else
  10388. {
  10389. CloseSessionTab(Index);
  10390. }
  10391. }
  10392. //---------------------------------------------------------------------------
  10393. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10394. {
  10395. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10396. {
  10397. TInstantOperationVisualizer Visualizer;
  10398. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10399. // before we create a new data object
  10400. ClipboardClear();
  10401. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10402. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10403. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10404. // Intermediate variable to WORKAROUND a crash in compiler
  10405. TObjectList * ClipboardFakeMonitors =
  10406. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10407. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10408. if (FClipboardDragDropFilesEx.get() == NULL)
  10409. {
  10410. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10411. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10412. }
  10413. FClipboardDragDropFilesEx->FileList->Clear();
  10414. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10415. FClipboardDragDropFilesEx->CopyToClipboard();
  10416. FClipboardTerminal = Terminal;
  10417. // Need full paths, as cwd can be different once files are pasted
  10418. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10419. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10420. {
  10421. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10422. }
  10423. }
  10424. }
  10425. //---------------------------------------------------------------------------
  10426. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10427. {
  10428. if (OpenClipboard(0))
  10429. {
  10430. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10431. CloseClipboard();
  10432. }
  10433. }
  10434. //---------------------------------------------------------------------------
  10435. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10436. {
  10437. return !FClipboardFakeDirectory.IsEmpty();
  10438. }
  10439. //---------------------------------------------------------------------------
  10440. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10441. {
  10442. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10443. FClipboardFakeDirectory = UnicodeString();
  10444. FClipboardTerminal = NULL;
  10445. if (FDownloadingFromClipboard)
  10446. {
  10447. // We are called by the monitor, so attempt to release it would deadlock
  10448. FClipboardFakeMonitorsPendingReset = true;
  10449. }
  10450. else
  10451. {
  10452. FClipboardFakeMonitors.reset(NULL);
  10453. }
  10454. }
  10455. //---------------------------------------------------------------------------
  10456. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10457. {
  10458. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10459. {
  10460. ClipboardStop();
  10461. }
  10462. }
  10463. //---------------------------------------------------------------------------
  10464. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10465. {
  10466. TTerminalManager * Manager = TTerminalManager::Instance();
  10467. Manager->ActiveSession = FClipboardTerminal;
  10468. TTransferOperationParam Params;
  10469. Params.TargetDirectory = TargetDirectory;
  10470. Params.DragDrop = DragDrop;
  10471. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10472. }
  10473. //---------------------------------------------------------------------------
  10474. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10475. {
  10476. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10477. // (as it pumps a Windows message queue while being shutted down)
  10478. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10479. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10480. {
  10481. // Can fail as it can be e.g. locked by AV, so we retry that later
  10482. bool Removed = RemoveDir(ApiPath(FileName));
  10483. try
  10484. {
  10485. if (!NonVisualDataModule->Busy)
  10486. {
  10487. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10488. FClipboardFakeMonitorsPendingReset = false;
  10489. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10490. TAutoFlag Flag(FDownloadingFromClipboard);
  10491. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10492. }
  10493. }
  10494. __finally
  10495. {
  10496. if (!Removed)
  10497. {
  10498. RemoveDir(ApiPath(FileName));
  10499. }
  10500. }
  10501. }
  10502. }
  10503. //---------------------------------------------------------------------------
  10504. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10505. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10506. {
  10507. TFileMasks::TParams MaskParams;
  10508. MaskParams.Size = Size;
  10509. MaskParams.Modification = Modification;
  10510. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10511. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10512. // TUnixDirView::ItemFullFileName returns trailing backslash for directories, what filemask cannot deal with
  10513. if (!Local && Directory)
  10514. {
  10515. FileName = UnixExcludeTrailingBackslash(FileName);
  10516. }
  10517. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10518. {
  10519. bool ImplicitMatch;
  10520. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10521. !ImplicitMatch)
  10522. {
  10523. Color = Iter->Color;
  10524. break;
  10525. }
  10526. }
  10527. }
  10528. //---------------------------------------------------------------------------
  10529. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10530. {
  10531. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10532. if (!DirView->IsEditing())
  10533. {
  10534. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10535. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10536. // while searching
  10537. if (Key == VK_BACK)
  10538. {
  10539. if (!FIncrementalSearch.IsEmpty())
  10540. {
  10541. if (FIncrementalSearch.Length() == 1)
  10542. {
  10543. ResetIncrementalSearch();
  10544. }
  10545. else
  10546. {
  10547. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10548. IncrementalSearch(NewText, false, false);
  10549. }
  10550. Key = 0;
  10551. }
  10552. }
  10553. }
  10554. }
  10555. //---------------------------------------------------------------------------
  10556. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10557. {
  10558. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10559. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10560. {
  10561. // Filter control sequences.
  10562. // When not searching yet, prefer use of the space for toggling file selection
  10563. // (so we cannot incrementally search for a string starting with a space).
  10564. if ((Key > VK_SPACE) ||
  10565. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10566. {
  10567. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10568. Key = 0;
  10569. }
  10570. }
  10571. }
  10572. //---------------------------------------------------------------------------
  10573. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10574. {
  10575. if (!FIncrementalSearch.IsEmpty())
  10576. {
  10577. FIncrementalSearch = L"";
  10578. DirView(osCurrent)->UpdateStatusBar();
  10579. }
  10580. }
  10581. //---------------------------------------------------------------------------
  10582. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10583. {
  10584. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10585. if (Item == NULL)
  10586. {
  10587. MessageBeep(MB_ICONHAND);
  10588. }
  10589. else
  10590. {
  10591. TCustomDirView * ADirView = DirView(osCurrent);
  10592. {
  10593. TAutoNestingCounter Guard(FIncrementalSearching);
  10594. ADirView->FocusItem(Item);
  10595. if (ADirView->NortonLike == nlOff)
  10596. {
  10597. if ((ADirView->Selected != Item) ||
  10598. (ADirView->SelCount > 1))
  10599. {
  10600. ADirView->ClearSelection();
  10601. ADirView->Selected = Item;
  10602. }
  10603. }
  10604. }
  10605. FIncrementalSearch = Text;
  10606. Item->MakeVisible(false);
  10607. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10608. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10609. ADirView->UpdateStatusBar();
  10610. }
  10611. }
  10612. //---------------------------------------------------------------------------
  10613. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10614. {
  10615. int Index = Item->Index;
  10616. if (!Reverse)
  10617. {
  10618. Index++;
  10619. if (Index >= Item->Owner->Count)
  10620. {
  10621. Index = 0;
  10622. }
  10623. }
  10624. else
  10625. {
  10626. Index--;
  10627. if (Index < 0)
  10628. {
  10629. Index = Item->Owner->Count - 1;
  10630. }
  10631. }
  10632. return Item->Owner->Item[Index];
  10633. }
  10634. //---------------------------------------------------------------------------
  10635. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10636. {
  10637. TCustomDirView * ADirView = DirView(osCurrent);
  10638. if (ADirView->Items->Count == 0)
  10639. {
  10640. return NULL;
  10641. }
  10642. else
  10643. {
  10644. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10645. TListItem * Item = CurrentItem;
  10646. if (SkipCurrent)
  10647. {
  10648. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10649. }
  10650. while (true)
  10651. {
  10652. bool Matches = false;
  10653. switch (WinConfiguration->PanelSearch)
  10654. {
  10655. case isNameStartOnly:
  10656. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item).SubString(1, Text.Length()), Text);
  10657. break;
  10658. case isName:
  10659. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item), Text);
  10660. break;
  10661. case isAll:
  10662. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10663. int Index = 0;
  10664. while ((Index < ColCount) && !Matches)
  10665. {
  10666. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10667. if (Visible)
  10668. {
  10669. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10670. }
  10671. Index++;
  10672. }
  10673. break;
  10674. }
  10675. if (Matches)
  10676. {
  10677. return Item;
  10678. }
  10679. Item = GetNextFile(Item, Reverse);
  10680. if (Item == CurrentItem)
  10681. {
  10682. return NULL;
  10683. }
  10684. }
  10685. }
  10686. }
  10687. //---------------------------------------------------------------------------
  10688. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10689. {
  10690. ResetIncrementalSearch();
  10691. }
  10692. //---------------------------------------------------------------------------
  10693. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10694. {
  10695. if (Message.CharCode == VK_TAB)
  10696. {
  10697. if (!FIncrementalSearch.IsEmpty())
  10698. {
  10699. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10700. bool Reverse = Shift.Contains(ssShift);
  10701. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10702. Message.Result = 1;
  10703. return;
  10704. }
  10705. }
  10706. else if (Message.CharCode == VK_ESCAPE)
  10707. {
  10708. if (!FIncrementalSearch.IsEmpty())
  10709. {
  10710. ResetIncrementalSearch();
  10711. Message.Result = 1;
  10712. return;
  10713. }
  10714. }
  10715. TForm::Dispatch(&Message);
  10716. }
  10717. //---------------------------------------------------------------------------
  10718. void __fastcall TCustomScpExplorerForm::Deactivate()
  10719. {
  10720. TForm::Deactivate();
  10721. // E.g. when switching to an internal editor window
  10722. ResetIncrementalSearch();
  10723. }
  10724. //---------------------------------------------------------------------------
  10725. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10726. {
  10727. // When switching to another application
  10728. ResetIncrementalSearch();
  10729. }
  10730. //---------------------------------------------------------------------------
  10731. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10732. {
  10733. ResetIncrementalSearch();
  10734. }
  10735. //---------------------------------------------------------------------------
  10736. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10737. {
  10738. if (FIncrementalSearching <= 0)
  10739. {
  10740. ResetIncrementalSearch();
  10741. }
  10742. }
  10743. //---------------------------------------------------------------------------
  10744. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10745. {
  10746. CountClicksForWindowPrint(this);
  10747. }
  10748. //---------------------------------------------------------------------------
  10749. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10750. {
  10751. CenterReconnectToolbar();
  10752. }
  10753. //---------------------------------------------------------------------------
  10754. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10755. {
  10756. if (RemoteDirView->ItemFocused != NULL)
  10757. {
  10758. RemoteDirView->ItemFocused->Selected = true;
  10759. }
  10760. }
  10761. //---------------------------------------------------------------------------
  10762. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10763. {
  10764. if (!FQueueStatusUpdating)
  10765. {
  10766. bool Refresh;
  10767. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10768. {
  10769. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10770. }
  10771. else
  10772. {
  10773. if (FQueueFileList->GetCount() > 0)
  10774. {
  10775. FQueueFileList->Clear();
  10776. Refresh = true;
  10777. }
  10778. }
  10779. int Count = FQueueFileList->GetCount();
  10780. if (QueueFileList->Items->Count != Count)
  10781. {
  10782. DebugAssert(Refresh);
  10783. QueueFileList->Items->Count = Count;
  10784. }
  10785. if (Refresh)
  10786. {
  10787. QueueFileList->Invalidate();
  10788. }
  10789. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10790. }
  10791. }
  10792. //---------------------------------------------------------------------------
  10793. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10794. {
  10795. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10796. }
  10797. //---------------------------------------------------------------------------
  10798. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10799. {
  10800. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10801. // See TSynchronizeChecklistDialog::UpdateTimer
  10802. if (QueueFileList->Items->Count > 0)
  10803. {
  10804. QueueFileList->Items->Item[0]->MakeVisible(false);
  10805. }
  10806. UpdateQueueFileList();
  10807. }
  10808. //---------------------------------------------------------------------------
  10809. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10810. {
  10811. Active = QueueView3->Focused() || QueueFileList->Focused();
  10812. }
  10813. //---------------------------------------------------------------------------
  10814. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10815. {
  10816. QueueLabelUpdateStatus();
  10817. }
  10818. //---------------------------------------------------------------------------
  10819. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10820. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10821. {
  10822. int State = FQueueFileList->GetState(Item->Index);
  10823. if (State == qfsProcessed)
  10824. {
  10825. Sender->Canvas->Font->Color = clGrayText;
  10826. }
  10827. }
  10828. //---------------------------------------------------------------------------
  10829. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10830. {
  10831. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10832. QueueFileList->Column[0]->Width =
  10833. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10834. }
  10835. //---------------------------------------------------------------------------
  10836. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10837. {
  10838. QueueFileListColumnAutoSize();
  10839. }
  10840. //---------------------------------------------------------------------------
  10841. void __fastcall TCustomScpExplorerForm::CloseApp()
  10842. {
  10843. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10844. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10845. // So we clear the flag here.
  10846. DebugAssert(NonVisualDataModule->Busy);
  10847. NonVisualDataModule->EndBusy();
  10848. try
  10849. {
  10850. Close();
  10851. }
  10852. __finally
  10853. {
  10854. NonVisualDataModule->StartBusy();
  10855. }
  10856. }
  10857. //---------------------------------------------------------------------------
  10858. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10859. {
  10860. return (Terminal != NULL) && Terminal->Active;
  10861. }
  10862. //---------------------------------------------------------------------------
  10863. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  10864. {
  10865. return (ManagedSession != NULL);
  10866. }
  10867. //---------------------------------------------------------------------------
  10868. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10869. {
  10870. return IsActiveTerminal(Terminal);
  10871. }
  10872. //---------------------------------------------------------------------------
  10873. void TCustomScpExplorerForm::LocalLocalCopy(
  10874. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  10875. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  10876. {
  10877. DebugFail();
  10878. }
  10879. //---------------------------------------------------------------------------
  10880. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  10881. {
  10882. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  10883. }
  10884. //---------------------------------------------------------------------------
  10885. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  10886. {
  10887. // Changing tab list triggers the OnResize.
  10888. if (!FUpdatingSessionTabs)
  10889. {
  10890. // UpdateTabsCaptionTruncation should be enough
  10891. SessionListChanged(true);
  10892. }
  10893. }
  10894. //---------------------------------------------------------------------------
  10895. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  10896. {
  10897. if (ASession == ManagedSession)
  10898. {
  10899. UpdateSession(ASession);
  10900. }
  10901. TSessionData * Data = ASession->StateData;
  10902. UnicodeString Result;
  10903. if (Side == osLocal)
  10904. {
  10905. Result = Data->LocalDirectory;
  10906. }
  10907. else if (DebugAlwaysTrue(Side == osOther))
  10908. {
  10909. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  10910. }
  10911. return Result;
  10912. }
  10913. //---------------------------------------------------------------------------
  10914. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  10915. {
  10916. UnicodeString Result;
  10917. if (!ASession->Active)
  10918. {
  10919. Result = LoadStr(STATUS_NOT_CONNECTED2);
  10920. }
  10921. else
  10922. {
  10923. Result = GetSessionPath(ASession, osRemote);
  10924. }
  10925. return Result;
  10926. }
  10927. //---------------------------------------------------------------------------
  10928. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  10929. {
  10930. return LoadStr(NEW_REMOTE_TAB_HINT);
  10931. }
  10932. //---------------------------------------------------------------------------
  10933. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  10934. {
  10935. if (Index >= 0)
  10936. {
  10937. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10938. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  10939. if (TabSession == NULL)
  10940. {
  10941. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  10942. if (NewTabTabCaption != TabSheet->Caption)
  10943. {
  10944. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  10945. }
  10946. }
  10947. else
  10948. {
  10949. TTerminalManager * Manager = TTerminalManager::Instance();
  10950. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  10951. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  10952. }
  10953. }
  10954. }
  10955. //---------------------------------------------------------------------------
  10956. typedef std::vector<TListItem *> TListItemsVector;
  10957. struct TCalculateSizeOperation
  10958. {
  10959. TCalculateSizeStats Stats;
  10960. TListItemsVector ListItems;
  10961. size_t Index;
  10962. };
  10963. //---------------------------------------------------------------------------
  10964. void TCustomScpExplorerForm::CalculateDirectorySizes()
  10965. {
  10966. TCalculateSizeOperation CalculateSizeOperation;
  10967. TOperationSide Side = GetSide(osCurrent);
  10968. TCustomDirView * ADirView = DirView(Side);
  10969. bool FullPath = IsSideLocalBrowser(Side);
  10970. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, FullPath, NULL, true));
  10971. std::unique_ptr<TStrings> FileList(new TStringList());
  10972. for (int Index = 0; Index < AllFileList->Count; Index++)
  10973. {
  10974. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  10975. if (ADirView->ItemIsDirectory(Item))
  10976. {
  10977. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  10978. CalculateSizeOperation.ListItems.push_back(Item);
  10979. }
  10980. }
  10981. CalculateSizeOperation.Index = 0;
  10982. __int64 Size = 0;
  10983. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation);
  10984. FCalculateSizeOperation = &CalculateSizeOperation;
  10985. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  10986. FOnFileOperationFinished = DirectorySizeCalculated;
  10987. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation);
  10988. FPrimaryOperation = foCalculateSize;
  10989. TCalculatedSizes CalculatedSizes;
  10990. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  10991. if (Side == osLocal)
  10992. {
  10993. Terminal->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  10994. }
  10995. else
  10996. {
  10997. TCalculateSizeParams Params;
  10998. Params.Stats = &CalculateSizeOperation.Stats;
  10999. Params.UseCache = true;
  11000. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  11001. }
  11002. }
  11003. //---------------------------------------------------------------------------
  11004. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(TOperationSide Side, const UnicodeString & FileName, bool)
  11005. {
  11006. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  11007. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  11008. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  11009. // optimization
  11010. bool GotExpectedItem =
  11011. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  11012. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  11013. int Index = -1;
  11014. if (DebugAlwaysTrue(GotExpectedItem))
  11015. {
  11016. Index = FCalculateSizeOperation->Index;
  11017. FCalculateSizeOperation->Index++;
  11018. }
  11019. else
  11020. {
  11021. TListItemsVector::iterator I =
  11022. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  11023. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  11024. {
  11025. Index = I - FCalculateSizeOperation->ListItems.begin();
  11026. }
  11027. }
  11028. if (DebugAlwaysTrue(Index >= 0))
  11029. {
  11030. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  11031. ADirView->SetItemCalculatedSize(Item, Size);
  11032. }
  11033. }
  11034. //---------------------------------------------------------------------------
  11035. void __fastcall TCustomScpExplorerForm::MessageDockRequestDock(TObject *, TTBCustomDockableWindow *, bool & Accept)
  11036. {
  11037. Accept = false;
  11038. }
  11039. //---------------------------------------------------------------------------