CustomScpExplorer.cpp 392 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803
  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. FQueueScrollOnDragOver = new TListViewScrollOnDragOver(QueueView3, true);
  222. FUserActionTimer = new TTimer(this);
  223. FUserActionTimer->Enabled = false;
  224. FUserActionTimer->Interval = 10;
  225. FUserActionTimer->OnTimer = UserActionTimer;
  226. FNotes = new TStringList();
  227. FNoteTimer = new TTimer(this);
  228. FNoteTimer->Enabled = false;
  229. FNoteTimer->OnTimer = NoteTimer;
  230. FOnNoteClick = NULL;
  231. FOle32Library = LoadLibrary(L"Ole32.dll");
  232. FDragMoveCursor = FOle32Library != NULL ?
  233. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  234. UseSystemSettings(this);
  235. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  236. static_cast<TObject*>(GetComponent(fcTransferList)));
  237. DebugAssert(TransferList != NULL);
  238. FTransferListHoverIndex = -1;
  239. TransferList->OnChange = TransferListChange;
  240. TransferList->OnDrawItem = TransferListDrawItem;
  241. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  242. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  243. UseDesktopFont(SessionsPageControl);
  244. UpdateSessionsPageControlHeight();
  245. UseDesktopFont(RemoteDirView);
  246. UseDesktopFont(RemoteDriveView);
  247. UseDesktopFont(QueueView3);
  248. UseDesktopFont(QueueFileList);
  249. UseDesktopFont(QueueLabel);
  250. UseDesktopFont(RemoteStatusBar);
  251. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  252. QueueSplitter->ShowHint = true;
  253. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  254. QueueFileListSplitter->ShowHint = true;
  255. RemotePanelSplitter->ShowHint = true;
  256. UpdateImages();
  257. FCustomCommandMenu = CreateTBXPopupMenu(this);
  258. FCustomCommandLocalFileList = NULL;
  259. FCustomCommandRemoteFileList = NULL;
  260. FSessionColors = new TPngImageList(this);
  261. FSessionColors->ColorDepth = cd32Bit;
  262. AddFixedSessionImages();
  263. SessionsPageControl->Images = FSessionColors;
  264. UpdateQueueLabel();
  265. CheckStoreTransition();
  266. CreateHiddenWindow();
  267. StartUpdates();
  268. }
  269. //---------------------------------------------------------------------------
  270. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  271. {
  272. FInvalid = true;
  273. // this has to be one of the very first things to do
  274. StopUpdateThread();
  275. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  276. TTerminalManager::Instance()->ScpExplorer = NULL;
  277. delete FCustomCommandLocalFileList;
  278. delete FCustomCommandRemoteFileList;
  279. delete FCustomCommandMenu;
  280. delete FTrayIcon;
  281. FTrayIcon = NULL;
  282. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  283. delete FEditorManager;
  284. ForceCloseLocalEditors();
  285. delete FLocalEditors;
  286. HideFileFindDialog();
  287. if (FDelayedDeletionTimer)
  288. {
  289. DoDelayedDeletion(NULL);
  290. SAFE_DESTROY(FDelayedDeletionTimer);
  291. }
  292. SAFE_DESTROY(FDelayedDeletionList);
  293. // sometimes we do not get DDEnd so the list is not released
  294. SAFE_DESTROY(FDDFileList);
  295. DebugAssert(FSynchronizeController == NULL);
  296. CloseHandle(FDDExtMutex);
  297. FDDExtMutex = NULL;
  298. FreeLibrary(FOle32Library);
  299. FOle32Library = NULL;
  300. FDragMoveCursor = NULL;
  301. DebugAssert(!FErrorList);
  302. if (FEverShown)
  303. {
  304. // when window is never shown (like when running command-line operation),
  305. // particularly window site is not restored correctly (BoundsRect value set
  306. // in RestoreForm gets lost during handle allocation), so we do not want
  307. // it to be stored
  308. StoreParams();
  309. }
  310. ManagedSession = NULL;
  311. Queue = NULL;
  312. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  313. NonVisualDataModule->ScpExplorer = NULL;
  314. delete FQueueScrollOnDragOver;
  315. FQueueScrollOnDragOver = NULL;
  316. delete FQueueController;
  317. FQueueController = NULL;
  318. delete FQueueStatusSection;
  319. FQueueStatusSection = NULL;
  320. delete FQueueStatus;
  321. FQueueStatus = NULL;
  322. delete FUserActionTimer;
  323. FUserActionTimer = NULL;
  324. delete FNoteTimer;
  325. delete FNotes;
  326. delete FNoteData;
  327. SAFE_DESTROY(FDocks);
  328. SessionsPageControl->Images = NULL;
  329. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  330. SAFE_DESTROY(FSessionColors);
  331. SAFE_DESTROY(FSessionsDragDropFilesEx);
  332. SAFE_DESTROY(FHistoryMenu[0][0]);
  333. SAFE_DESTROY(FHistoryMenu[0][1]);
  334. SAFE_DESTROY(FHistoryMenu[1][0]);
  335. SAFE_DESTROY(FHistoryMenu[1][1]);
  336. if (FHiddenWindow != NULL)
  337. {
  338. DestroyWindow(FHiddenWindow);
  339. FHiddenWindow = NULL;
  340. }
  341. }
  342. //---------------------------------------------------------------------------
  343. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  344. {
  345. std::unique_ptr<TSessionData> Data;
  346. if (!Session.IsEmpty())
  347. {
  348. bool DefaultsOnly;
  349. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  350. }
  351. TTerminalManager * Manager = TTerminalManager::Instance();
  352. for (int Index = 0; Index < Manager->Count; Index++)
  353. {
  354. TTerminal * Terminal = Manager->Sessions[Index];
  355. if (Session.IsEmpty() ||
  356. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  357. {
  358. if (Path.IsEmpty())
  359. {
  360. Terminal->ClearCaches();
  361. }
  362. else
  363. {
  364. Terminal->DirectoryModified(Path, true);
  365. }
  366. }
  367. }
  368. // We should flag a pending refresh for the background terminals or busy foreground terminals
  369. if (HasActiveTerminal() &&
  370. CanCommandLineFromAnotherInstance() &&
  371. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  372. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  373. {
  374. Terminal->ReloadDirectory();
  375. }
  376. }
  377. //---------------------------------------------------------------------------
  378. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  379. {
  380. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  381. AppLogFmt(L"Received copy data message with size %d", (int(CopyData->cbData)));
  382. size_t MessageSize = sizeof(TCopyDataMessage);
  383. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  384. if (Result)
  385. {
  386. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  387. Result = (Message.Version == TCopyDataMessage::Version1);
  388. if (Result)
  389. {
  390. switch (Message.Command)
  391. {
  392. case TCopyDataMessage::CommandCanCommandLine:
  393. Result = CanCommandLineFromAnotherInstance();
  394. if (Result)
  395. {
  396. AppLog(L"Can process command-line from other instance");
  397. }
  398. else
  399. {
  400. AppLog(L"Cannot process command-line from other instance");
  401. }
  402. break;
  403. case TCopyDataMessage::CommandCommandLine:
  404. {
  405. UnicodeString CommandLine(Message.CommandLine);
  406. Result = CommandLineFromAnotherInstance(CommandLine);
  407. }
  408. break;
  409. case TCopyDataMessage::RefreshPanel:
  410. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  411. break;
  412. case TCopyDataMessage::MainWindowCheck:
  413. Result = true;
  414. break;
  415. default:
  416. Result = false;
  417. break;
  418. }
  419. }
  420. }
  421. Message.Result = Result ? 1 : 0;
  422. }
  423. //---------------------------------------------------------------------------
  424. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  425. {
  426. WNDCLASS WindowClass = {0};
  427. WindowClass.lpfnWndProc = DefWindowProc;
  428. WindowClass.hInstance = HInstance;
  429. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  430. FHiddenWindow = NULL;
  431. if (RegisterClass(&WindowClass))
  432. {
  433. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  434. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  435. }
  436. }
  437. //---------------------------------------------------------------------------
  438. bool __fastcall TCustomScpExplorerForm::CanConsole()
  439. {
  440. return
  441. HasActiveTerminal() &&
  442. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  443. }
  444. //---------------------------------------------------------------------------
  445. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  446. {
  447. bool Result = Visible && !NonVisualDataModule->Busy;
  448. return Result;
  449. }
  450. //---------------------------------------------------------------------------
  451. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  452. const UnicodeString & CommandLine)
  453. {
  454. TProgramParams Params(CommandLine);
  455. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  456. if (Result)
  457. {
  458. NonVisualDataModule->StartBusy();
  459. try
  460. {
  461. // this action is initiated from another process,
  462. // so it's likely that our window is not visible,
  463. // and user won't see what is going on
  464. Application->BringToFront();
  465. // reload sessions as we may be asked to open a session
  466. // just stored by another instance
  467. StoredSessions->Reload();
  468. UnicodeString SessionName = Params.Param[1];
  469. std::unique_ptr<TObjectList> DataList(new TObjectList());
  470. try
  471. {
  472. UnicodeString DownloadFile; // unused
  473. int Flags = GetCommandLineParseUrlFlags(&Params);
  474. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  475. }
  476. catch (EAbort &)
  477. {
  478. // Happens when opening session in PuTTY and in other situations,
  479. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  480. // to process the command-line on its own (or to pass it to yet another instance)
  481. }
  482. if (DataList->Count > 0)
  483. {
  484. TTerminalManager * Manager = TTerminalManager::Instance();
  485. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  486. }
  487. }
  488. __finally
  489. {
  490. NonVisualDataModule->EndBusy();
  491. }
  492. }
  493. return Result;
  494. }
  495. //---------------------------------------------------------------------------
  496. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  497. {
  498. TManagedTerminal * Result;
  499. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  500. {
  501. Result = FManagedSession;
  502. }
  503. else
  504. {
  505. Result = NULL;
  506. }
  507. return Result;
  508. }
  509. //---------------------------------------------------------------------------
  510. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  511. {
  512. if (FManagedSession != value)
  513. {
  514. SessionChanging();
  515. DoSetManagedSession(value, false);
  516. }
  517. }
  518. //---------------------------------------------------------------------------
  519. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  520. {
  521. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  522. FManagedSession = value;
  523. {
  524. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  525. FAllowTransferPresetAutoSelect = false;
  526. TAutoFlag SessionChangingFlag(FSessionChanging);
  527. SessionChanged(Replace);
  528. }
  529. if (Terminal != NULL)
  530. {
  531. TransferPresetAutoSelect();
  532. }
  533. // Update app and tab titles, prevented above by FSessionChanging.
  534. UpdateControls();
  535. }
  536. //---------------------------------------------------------------------------
  537. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  538. {
  539. DoSetManagedSession(value, true);
  540. }
  541. //---------------------------------------------------------------------------
  542. void __fastcall TCustomScpExplorerForm::SessionChanging()
  543. {
  544. if (ManagedSession != NULL)
  545. {
  546. UpdateSession(ManagedSession);
  547. }
  548. }
  549. //---------------------------------------------------------------------------
  550. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  551. {
  552. if (ManagedSession != NULL)
  553. {
  554. UpdateSessionColor(static_cast<TColor>(ManagedSession->StateData->Color));
  555. }
  556. if (!TTerminalManager::Instance()->Updating)
  557. {
  558. SessionListChanged();
  559. }
  560. if (Terminal != NULL)
  561. {
  562. TObject * State = NULL;
  563. if (WinConfiguration->PreservePanelState)
  564. {
  565. State = Terminal->RemoteExplorerState;
  566. DirView(osRemote)->AnnounceState(State);
  567. }
  568. try
  569. {
  570. if (Replaced)
  571. {
  572. RemoteDirView->ReplaceTerminal(Terminal);
  573. }
  574. else
  575. {
  576. RemoteDirView->Terminal = Terminal;
  577. }
  578. if (Terminal->Active)
  579. {
  580. Terminal->RefreshDirectory();
  581. }
  582. if (State != NULL)
  583. {
  584. DirView(osRemote)->RestoreState(State);
  585. }
  586. }
  587. __finally
  588. {
  589. DirView(osRemote)->AnnounceState(NULL);
  590. }
  591. }
  592. else
  593. {
  594. RemoteDirView->Terminal = NULL;
  595. }
  596. InitStatusBar();
  597. UpdateTransferList();
  598. // Update panels Enable state before refreshing the labels
  599. UpdateControls();
  600. }
  601. //---------------------------------------------------------------------------
  602. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  603. {
  604. if (Queue != value)
  605. {
  606. if (FQueue != NULL)
  607. {
  608. FQueue->OnListUpdate = NULL;
  609. FQueue->OnQueueItemUpdate = NULL;
  610. }
  611. FQueue = value;
  612. if (FQueue != NULL)
  613. {
  614. DebugAssert(FQueue->OnListUpdate == NULL);
  615. FQueue->OnListUpdate = QueueListUpdate;
  616. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  617. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  618. }
  619. QueueChanged();
  620. }
  621. }
  622. //---------------------------------------------------------------------------
  623. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  624. TListItem * Item)
  625. {
  626. if (FQueueActedItem == Item)
  627. {
  628. FQueueActedItem = NULL;
  629. if ((QueueView3->PopupMenu != NULL) &&
  630. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  631. {
  632. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  633. ReleaseCapture();
  634. }
  635. }
  636. if (Item->Data == FPendingQueueActionItem)
  637. {
  638. FPendingQueueActionItem = NULL;
  639. }
  640. }
  641. //---------------------------------------------------------------------------
  642. bool TCustomScpExplorerForm::IsAnythingQueued()
  643. {
  644. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  645. }
  646. //---------------------------------------------------------------------------
  647. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  648. {
  649. {
  650. TGuard Guard(FQueueStatusSection);
  651. FQueueStatusInvalidated = false;
  652. if (FQueue != NULL)
  653. {
  654. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  655. }
  656. }
  657. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  658. {
  659. FMaxQueueLength = FQueueStatus->Count;
  660. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  661. }
  662. {
  663. TAutoFlag Flag(FQueueStatusUpdating);
  664. FQueueController->UpdateQueueStatus(FQueueStatus);
  665. }
  666. UpdateQueueFileList();
  667. SetQueueProgress();
  668. UpdateQueueView();
  669. bool IsEmpty = !IsAnythingQueued();
  670. if (IsEmpty && (Terminal != NULL))
  671. {
  672. TOnceDoneOperation OnceDoneOperation =
  673. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  674. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  675. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  676. {
  677. FQueue->Enabled = false;
  678. }
  679. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  680. {
  681. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  682. }
  683. }
  684. }
  685. //---------------------------------------------------------------------------
  686. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  687. {
  688. UnicodeString Result;
  689. if (FQueueStatus != NULL)
  690. {
  691. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  692. {
  693. TFileOperationProgressType * ProgressData =
  694. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  695. if ((ProgressData != NULL) && ProgressData->InProgress)
  696. {
  697. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  698. }
  699. }
  700. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  701. {
  702. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  703. }
  704. }
  705. return Result;
  706. }
  707. //---------------------------------------------------------------------------
  708. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  709. {
  710. ComponentVisible[fcQueueView] =
  711. !IsLocalBrowserMode() &&
  712. ((WinConfiguration->QueueView.Show == qvShow) ||
  713. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  714. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  715. }
  716. //---------------------------------------------------------------------------
  717. void __fastcall TCustomScpExplorerForm::QueueChanged()
  718. {
  719. if (FQueueStatus != NULL)
  720. {
  721. // UpdateFileList implementation relies on the status being recreated when session changes
  722. delete FQueueStatus;
  723. FQueueStatus = NULL;
  724. }
  725. UpdateQueueStatus(true);
  726. }
  727. //---------------------------------------------------------------------------
  728. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  729. {
  730. if (FQueue == Queue)
  731. {
  732. FQueueStatusInvalidated = true;
  733. }
  734. }
  735. //---------------------------------------------------------------------------
  736. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  737. TQueueItem * Item)
  738. {
  739. if (FQueue == Queue)
  740. {
  741. TGuard Guard(FQueueStatusSection);
  742. // this may be running in parallel with QueueChanged
  743. if (FQueueStatus != NULL)
  744. {
  745. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  746. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  747. {
  748. FRefreshLocalDirectory = (QueueItem == NULL) ||
  749. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  750. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  751. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  752. }
  753. if (QueueItem != NULL)
  754. {
  755. QueueItem->UserData = (void*)true;
  756. FQueueItemInvalidated = true;
  757. }
  758. }
  759. }
  760. }
  761. //---------------------------------------------------------------------------
  762. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  763. {
  764. // during standalone editing, we have no way to see/control queue,
  765. // so we have to always popup prompts automatically
  766. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  767. }
  768. //---------------------------------------------------------------------------
  769. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  770. {
  771. if (FQueueStatus != NULL)
  772. {
  773. bool QueueAutoPopup = IsQueueAutoPopup();
  774. bool NeedRefresh = FQueueController->NeedRefresh();
  775. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  776. FQueueItemInvalidated = false;
  777. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  778. bool Updated = false;
  779. TQueueItemProxy * QueueItem;
  780. bool WasUserAction;
  781. for (int Index = 0; Index < Limit; Index++)
  782. {
  783. bool Update = false;
  784. QueueItem = FQueueStatus->Items[Index];
  785. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  786. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  787. (FPendingQueueActionItem == NULL))
  788. {
  789. FPendingQueueActionItem = QueueItem;
  790. FUserActionTimer->Enabled = true;
  791. }
  792. if ((bool)QueueItem->UserData)
  793. {
  794. QueueItem->UserData = (void*)false;
  795. QueueItem->Update();
  796. Updated = true;
  797. Update = true;
  798. }
  799. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  800. {
  801. Update = true;
  802. }
  803. if (Update)
  804. {
  805. FQueueController->RefreshQueueItem(QueueItem);
  806. }
  807. }
  808. if (Updated)
  809. {
  810. NonVisualDataModule->UpdateNonVisibleActions();
  811. SetQueueProgress();
  812. UpdateQueueFileList();
  813. }
  814. }
  815. }
  816. //---------------------------------------------------------------------------
  817. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  818. {
  819. if (FTaskbarList != NULL)
  820. {
  821. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  822. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  823. }
  824. }
  825. //---------------------------------------------------------------------------
  826. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  827. {
  828. if (Progress >= 0)
  829. {
  830. if (FTaskbarList != NULL)
  831. {
  832. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  833. }
  834. }
  835. else
  836. {
  837. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  838. }
  839. }
  840. //---------------------------------------------------------------------------
  841. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  842. {
  843. int OverallProgress;
  844. // FProgressForm is null when this is called from SetQueueProgress
  845. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  846. {
  847. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  848. }
  849. else if (!ProgressData->IsIndeterminate())
  850. {
  851. OverallProgress = ProgressData->OverallProgress();
  852. }
  853. else
  854. {
  855. OverallProgress = -1;
  856. }
  857. SetTaskbarListProgressValue(OverallProgress);
  858. }
  859. //---------------------------------------------------------------------------
  860. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  861. {
  862. TTerminalManager::Instance()->QueueStatusUpdated();
  863. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  864. {
  865. // Do not animate task bar button, when items are queued, but processing is disabled
  866. if ((FQueueStatus != NULL) && (FQueueStatus->ActivePrimaryCount > 0))
  867. {
  868. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  869. {
  870. TFileOperationProgressType * ProgressData = NULL;
  871. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  872. {
  873. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  874. }
  875. if ((ProgressData != NULL) &&
  876. ProgressData->InProgress)
  877. {
  878. SetTaskbarListProgressValue(ProgressData);
  879. }
  880. else
  881. {
  882. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  883. }
  884. }
  885. else
  886. {
  887. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  888. }
  889. }
  890. else
  891. {
  892. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  893. }
  894. }
  895. UpdateQueueLabel();
  896. }
  897. //---------------------------------------------------------------------------
  898. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  899. {
  900. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  901. if (FQueueStatus != NULL)
  902. {
  903. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  904. if (ActiveAndPendingPrimaryCount > 0)
  905. {
  906. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  907. }
  908. }
  909. QueueLabel->Caption = Caption;
  910. }
  911. //---------------------------------------------------------------------------
  912. void TCustomScpExplorerForm::CheckStoreTransition()
  913. {
  914. if (WinConfiguration->StoreTransition == stInit)
  915. {
  916. if (IsUWP())
  917. {
  918. WinConfiguration->StoreTransition = stStoreFresh;
  919. AppLog(L"Fresh UWP/store installation");
  920. }
  921. else
  922. {
  923. WinConfiguration->StoreTransition = stStandard;
  924. AppLog(L"Fresh standard installation");
  925. }
  926. }
  927. else if ((WinConfiguration->StoreTransition == stStandard) && IsUWP())
  928. {
  929. WinConfiguration->StoreTransition = stStoreMigrated;
  930. WinConfiguration->FirstRun = WinConfiguration->FirstRun + L"=>" + StandardDatestamp();
  931. AppLog(L"Standard installation migrated to store installation");
  932. }
  933. if (WinConfiguration->StoreTransition == stStoreMigrated)
  934. {
  935. TTBXToolbar * Toolbar = new TTBXToolbar(MessageDock);
  936. Toolbar->Parent = MessageDock;
  937. Toolbar->FullSize = true;
  938. Toolbar->Color = Application->HintColor;
  939. TTBXLabelItem * LabelItem;
  940. LabelItem = new TTBXLabelItem(Toolbar);
  941. LabelItem->Margin = ScaleByTextHeight(MessageDock, 2); // Is this automatically rescaled?
  942. LabelItem->Caption = LoadStr(STORE_MIGRATION_THANKS);
  943. Toolbar->Items->Add(LabelItem);
  944. TTBXCustomItem * LinkItem = new TTBXItem(Toolbar);
  945. LinkItem->FontSettings->Color = LinkColor;
  946. LinkItem->Caption = LoadStr(STORE_MIGRATION_LEARN);
  947. LinkItem->OnClick = StoreTransitionLinkClick;
  948. Toolbar->Items->Add(LinkItem);
  949. AddMenuSeparator(Toolbar->Items);
  950. TTBCustomItem * CloseItem = new TTBXItem(Toolbar);
  951. CloseItem->Caption = LoadStr(MESSAGE_DISMISS);
  952. CloseItem->OnClick = StoreTransitionCloseClick;
  953. Toolbar->Items->Add(CloseItem);
  954. GiveTBItemPriority(CloseItem);
  955. }
  956. }
  957. //---------------------------------------------------------------------------
  958. void __fastcall TCustomScpExplorerForm::StoreTransitionLinkClick(TObject *)
  959. {
  960. ShowHelp(HELP_STORE_TRANSITION);
  961. }
  962. //---------------------------------------------------------------------------
  963. void __fastcall TCustomScpExplorerForm::StoreTransitionCloseClick(TObject * Sender)
  964. {
  965. TTBCustomItem * Item = DebugNotNull(dynamic_cast<TTBCustomItem *>(Sender));
  966. TTBXToolbar * Toolbar = DebugNotNull(dynamic_cast<TTBXToolbar *>(DebugNotNull(DebugNotNull(Item->Parent)->ParentComponent)));
  967. delete Toolbar;
  968. WinConfiguration->StoreTransition = stStoreAcknowledged;
  969. UpdateControls();
  970. }
  971. //---------------------------------------------------------------------------
  972. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  973. {
  974. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  975. static_cast<TComponent*>(GetComponent(fcTransferList)));
  976. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  977. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  978. TransferList->Strings->BeginUpdate();
  979. try
  980. {
  981. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  982. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  983. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  984. if (FTransferDropDownHint.IsEmpty())
  985. {
  986. FTransferDropDownHint = TransferDropDown->Hint;
  987. }
  988. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  989. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  990. TransferDropDown->Text = StripHotkey(Name);
  991. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  992. (FTransferDropDownHint, StripHotkey(Name),
  993. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  994. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  995. // update the label, otherwise when it is updated only on the first draw
  996. // of the list, it is drawn "bold" for some reason
  997. FTransferListHoverIndex = TransferList->ItemIndex;
  998. UpdateTransferLabel();
  999. }
  1000. __finally
  1001. {
  1002. TransferList->Strings->EndUpdate();
  1003. }
  1004. }
  1005. //---------------------------------------------------------------------------
  1006. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  1007. {
  1008. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  1009. DebugAssert(Toolbar != NULL);
  1010. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  1011. }
  1012. //---------------------------------------------------------------------------
  1013. void __fastcall TCustomScpExplorerForm::UpdateActions()
  1014. {
  1015. TForm::UpdateActions();
  1016. if (ComponentVisible[fcCustomCommandsBand])
  1017. {
  1018. UpdateCustomCommandsToolbar();
  1019. }
  1020. }
  1021. //---------------------------------------------------------------------------
  1022. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  1023. {
  1024. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  1025. }
  1026. //---------------------------------------------------------------------------
  1027. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  1028. {
  1029. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  1030. {
  1031. DirView->RowSelect = WinConfiguration->FullRowSelect;
  1032. // Without this, the panel turns gray and unused part of header turns black
  1033. DirView->Perform(CM_COLORCHANGED, 0, 0);
  1034. }
  1035. }
  1036. //---------------------------------------------------------------------------
  1037. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  1038. {
  1039. Color = GetBtnFaceColor();
  1040. DebugAssert(Configuration && RemoteDirView);
  1041. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1042. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  1043. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  1044. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  1045. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1046. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1047. RemoteDirView->AlwaysSortDirectoriesByName = WinConfiguration->AlwaysSortDirectoriesByName;
  1048. UpdateRowSelect(RemoteDirView);
  1049. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1050. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  1051. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  1052. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1053. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1054. UpdateSessionsPageControlHeight();
  1055. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1056. UpdateToolbarDisplayMode();
  1057. UpdateTransferList();
  1058. if (Terminal != NULL)
  1059. {
  1060. TransferPresetAutoSelect();
  1061. }
  1062. UpdateQueueView();
  1063. UpdateControls();
  1064. // this can be called even before constuctor finishes.
  1065. if (FEditorManager != NULL)
  1066. {
  1067. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  1068. }
  1069. // this can be called even before constuctor finishes.
  1070. if (FLocalEditors != NULL)
  1071. {
  1072. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  1073. {
  1074. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  1075. }
  1076. }
  1077. if (ComponentVisible[fcCustomCommandsBand])
  1078. {
  1079. UpdateCustomCommandsToolbar();
  1080. }
  1081. // show only when keeping queue items forever,
  1082. // otherwise, it's enough to have it in the context menu
  1083. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1084. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1085. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1086. {
  1087. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1088. FFileColorsCurrent = WinConfiguration->FileColors;
  1089. FileColorsChanged();
  1090. }
  1091. }
  1092. //---------------------------------------------------------------------------
  1093. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1094. {
  1095. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1096. DirView->Invalidate();
  1097. }
  1098. //---------------------------------------------------------------------------
  1099. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1100. {
  1101. DoFileColorsChanged(RemoteDirView);
  1102. }
  1103. //---------------------------------------------------------------------------
  1104. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1105. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1106. void * /*Arg*/)
  1107. {
  1108. if (Token != NULL)
  1109. {
  1110. TForm * Editor = dynamic_cast<TForm*>(Token);
  1111. DebugAssert(Editor != NULL);
  1112. ReconfigureEditorForm(Editor);
  1113. }
  1114. }
  1115. //---------------------------------------------------------------------------
  1116. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1117. {
  1118. WinConfiguration->DDTransferConfirmation = asOn;
  1119. }
  1120. //---------------------------------------------------------------------------
  1121. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1122. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1123. {
  1124. // noop
  1125. }
  1126. //---------------------------------------------------------------------------
  1127. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1128. {
  1129. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1130. }
  1131. //---------------------------------------------------------------------------
  1132. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1133. {
  1134. if (DoNotShowAgain)
  1135. {
  1136. if (DragDrop)
  1137. {
  1138. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1139. {
  1140. PopupTrayBalloon(
  1141. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1142. }
  1143. WinConfiguration->DDTransferConfirmation = asOff;
  1144. }
  1145. else
  1146. {
  1147. WinConfiguration->ConfirmTransferring = false;
  1148. }
  1149. }
  1150. else
  1151. {
  1152. // User explicitly unchecked "do not show again",
  1153. // so show him the dialog the next time
  1154. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1155. {
  1156. WinConfiguration->DDTransferConfirmation = asOn;
  1157. }
  1158. }
  1159. }
  1160. //---------------------------------------------------------------------------
  1161. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1162. TTransferDirection Direction, TTransferType Type, bool Temp,
  1163. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1164. bool Confirm, bool DragDrop, int Options)
  1165. {
  1166. bool Result = true;
  1167. DebugAssert(HasActiveTerminal());
  1168. // these parameters are known in advance
  1169. int Params =
  1170. FLAGMASK(Type == ttMove, cpDelete);
  1171. bool ToTemp = (Temp && (Direction == tdToLocal));
  1172. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1173. {
  1174. bool DisableNewerOnly =
  1175. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1176. ToTemp;
  1177. Options |=
  1178. FLAGMASK(ToTemp, coTemp) |
  1179. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1180. coDoNotShowAgain;
  1181. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1182. int CopyParamAttrs = (Direction == tdToRemote ?
  1183. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1184. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1185. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1186. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1187. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1188. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1189. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1190. if (Result)
  1191. {
  1192. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1193. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1194. }
  1195. }
  1196. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1197. {
  1198. Configuration->Usage->Inc(L"TransfersOnBackground");
  1199. // these parameter are known only after transfer dialog
  1200. Params |=
  1201. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1202. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1203. Result = false;
  1204. ClearTransferSourceSelection(Direction);
  1205. }
  1206. return Result;
  1207. }
  1208. //---------------------------------------------------------------------------
  1209. void TCustomScpExplorerForm::ClearOperationSelection(TOperationSide Side)
  1210. {
  1211. if (FOnFileOperationFinished != NULL)
  1212. {
  1213. FOnFileOperationFinished(Side, UnicodeString(), true);
  1214. }
  1215. else
  1216. {
  1217. if (HasDirView[Side])
  1218. {
  1219. DirView(Side)->SelectAll(smNone);
  1220. }
  1221. }
  1222. }
  1223. //---------------------------------------------------------------------------
  1224. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1225. {
  1226. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1227. ClearOperationSelection(Side);
  1228. }
  1229. //---------------------------------------------------------------------------
  1230. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1231. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1232. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1233. int Params)
  1234. {
  1235. DebugAssert(Queue != NULL);
  1236. bool SingleFile = false;
  1237. if (FileList->Count == 1)
  1238. {
  1239. if (Direction == tdToRemote)
  1240. {
  1241. UnicodeString FileName = FileList->Strings[0];
  1242. SingleFile = FileExists(ApiPath(FileName));
  1243. }
  1244. else
  1245. {
  1246. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1247. SingleFile = !File->IsDirectory;
  1248. }
  1249. }
  1250. TQueueItem * QueueItem;
  1251. if (Direction == tdToRemote)
  1252. {
  1253. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1254. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1255. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1256. }
  1257. else
  1258. {
  1259. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1260. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1261. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1262. }
  1263. AddQueueItem(Queue, QueueItem, Terminal);
  1264. }
  1265. //---------------------------------------------------------------------------
  1266. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1267. {
  1268. if (Queue->IsEmpty)
  1269. {
  1270. Terminal->QueueOperationStart = Now();
  1271. }
  1272. Queue->AddItem(QueueItem);
  1273. }
  1274. //---------------------------------------------------------------------------
  1275. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1276. {
  1277. }
  1278. //---------------------------------------------------------------------------
  1279. void __fastcall TCustomScpExplorerForm::InitControls()
  1280. {
  1281. DebugAssert(FDocks != NULL);
  1282. for (int Index = 0; Index < ComponentCount; Index++)
  1283. {
  1284. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1285. if ((Dock != NULL) && (Dock->Tag == 0))
  1286. {
  1287. FDocks->Add(Dock);
  1288. }
  1289. }
  1290. CollectItemsWithTextDisplayMode(this);
  1291. }
  1292. //---------------------------------------------------------------------------
  1293. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1294. {
  1295. ConfigurationChanged();
  1296. QueuePanel->Height =
  1297. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1298. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1299. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1300. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1301. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1302. QueueFileList->Height =
  1303. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1304. if (QueueFileList->Visible)
  1305. {
  1306. AdjustQueueLayout();
  1307. }
  1308. UpdateCustomCommandsToolbar();
  1309. }
  1310. //---------------------------------------------------------------------------
  1311. void __fastcall TCustomScpExplorerForm::StoreParams()
  1312. {
  1313. int APixelsPerInch = GetControlPixelsPerInch(this);
  1314. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1315. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1316. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1317. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1318. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1319. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1320. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1321. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1322. }
  1323. //---------------------------------------------------------------------------
  1324. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1325. {
  1326. if (!FFormRestored)
  1327. {
  1328. FFormRestored = true;
  1329. RestoreFormParams();
  1330. }
  1331. TForm::CreateParams(Params);
  1332. }
  1333. //---------------------------------------------------------------------------
  1334. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1335. {
  1336. DebugAssert(FDocks != NULL);
  1337. for (int Index = 0; Index < FDocks->Count; Index++)
  1338. {
  1339. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1340. }
  1341. }
  1342. //---------------------------------------------------------------------------
  1343. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1344. {
  1345. SetDockAllowDrag(true);
  1346. while (!ButtonsStr.IsEmpty())
  1347. {
  1348. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1349. UnicodeString Name = CutToChar(S, L'=', true);
  1350. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1351. {
  1352. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1353. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1354. {
  1355. while (!S.IsEmpty())
  1356. {
  1357. UnicodeString S2 = CutToChar(S, L',', true);
  1358. UnicodeString Name = CutToChar(S2, L':', true);
  1359. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1360. {
  1361. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1362. if (SameText(GetToolbarItemName(Item), Name))
  1363. {
  1364. // forward compatibility
  1365. UnicodeString S3 = CutToChar(S2, L':', true);
  1366. bool Visible = StrToIntDef(S3, 0);
  1367. Item->Visible = Visible;
  1368. }
  1369. }
  1370. }
  1371. }
  1372. }
  1373. }
  1374. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1375. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1376. }
  1377. //---------------------------------------------------------------------------
  1378. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1379. {
  1380. UnicodeString Result;
  1381. SetDockAllowDrag(true);
  1382. Result = ::GetToolbarsLayoutStr(this);
  1383. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1384. return Result;
  1385. }
  1386. //---------------------------------------------------------------------------
  1387. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1388. {
  1389. UnicodeString Result;
  1390. if (Item->Action != NULL)
  1391. {
  1392. Result = Item->Action->Name;
  1393. Result = RemoveSuffix(Result, L"Action", true);
  1394. }
  1395. else
  1396. {
  1397. Result = Item->Name;
  1398. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1399. Result = RemoveSuffix(Result, L"Item", true);
  1400. }
  1401. return Result;
  1402. }
  1403. //---------------------------------------------------------------------------
  1404. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1405. {
  1406. UnicodeString Result;
  1407. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1408. {
  1409. UnicodeString ToolbarStr;
  1410. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1411. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1412. {
  1413. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1414. {
  1415. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1416. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1417. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1418. // and save the changes here only.
  1419. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1420. {
  1421. UnicodeString Name = GetToolbarItemName(Item);
  1422. DebugAssert(Name.Pos(L"TBX") == 0);
  1423. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1424. }
  1425. }
  1426. }
  1427. if (!ToolbarStr.IsEmpty())
  1428. {
  1429. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1430. }
  1431. }
  1432. return Result;
  1433. }
  1434. //---------------------------------------------------------------------------
  1435. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1436. {
  1437. DebugAssert(FProgressForm == NULL);
  1438. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1439. bool AllowMoveToQueue = (FTransferResumeList != NULL) || (FDeletedFiles != NULL);
  1440. FProgressForm = new TProgressForm(Application, AllowMoveToQueue, AllowSkip, SynchronizeProgress);
  1441. FProgressForm->DeleteLocalToRecycleBin =
  1442. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1443. FProgressForm->DeleteRemoteToRecycleBin =
  1444. HasActiveTerminal() &&
  1445. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1446. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1447. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1448. LockWindow();
  1449. // Actually, do not know what hides the progress once the operation finishes
  1450. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1451. SetTaskbarListProgressState(TBPF_NORMAL);
  1452. }
  1453. //---------------------------------------------------------------------------
  1454. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1455. {
  1456. SAFE_DESTROY(FProgressForm);
  1457. UnlockWindow();
  1458. SetQueueProgress();
  1459. }
  1460. //---------------------------------------------------------------------------
  1461. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1462. TFileOperationProgressType & ProgressData)
  1463. {
  1464. // Download to temporary local directory
  1465. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1466. {
  1467. bool Continue = true;
  1468. FPendingTempSpaceWarn = false;
  1469. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1470. if (!Continue)
  1471. {
  1472. Abort();
  1473. }
  1474. }
  1475. // operation is being executed and we still didn't show up progress form
  1476. if (ProgressData.InProgress && !FProgressForm)
  1477. {
  1478. CreateProgressForm(NULL);
  1479. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1480. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1481. }
  1482. // operation is finished (or terminated), so we hide progress form
  1483. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1484. {
  1485. DestroyProgressForm();
  1486. if (ProgressData.Operation == foCalculateSize)
  1487. {
  1488. // When calculating size before transfer, the abort caused by
  1489. // cancel flag set due to "MoveToQueue" below,
  1490. // is not propagated back to us in ExecuteFileOperation,
  1491. // as it is silently swallowed.
  1492. // So we explicitly (re)throw it here.
  1493. if (FMoveToQueue)
  1494. {
  1495. Abort();
  1496. }
  1497. }
  1498. else
  1499. {
  1500. if ((ProgressData.Cancel == csContinue) &&
  1501. !FAutoOperation)
  1502. {
  1503. OperationComplete(ProgressData.StartTime);
  1504. }
  1505. }
  1506. }
  1507. if (FProgressForm &&
  1508. // Can happen only during synchronization, when ending one of the operations
  1509. ProgressData.InProgress)
  1510. {
  1511. FProgressForm->SetProgressData(ProgressData);
  1512. DebugAssert(ProgressData.InProgress);
  1513. SetTaskbarListProgressValue(&ProgressData);
  1514. if (FProgressForm->Cancel > csContinue)
  1515. {
  1516. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1517. if (FProgressForm->Cancel == csCancelFile)
  1518. {
  1519. FProgressForm->ClearCancel();
  1520. }
  1521. // cancel cancels even the move
  1522. FMoveToQueue = false;
  1523. }
  1524. else if (FProgressForm->MoveToQueue)
  1525. {
  1526. FMoveToQueue = true;
  1527. ProgressData.SetCancelAtLeast(csCancel);
  1528. }
  1529. if ((FTransferResumeList != NULL) &&
  1530. ProgressData.InProgress &&
  1531. ProgressData.IsTransfer() &&
  1532. !ProgressData.FullFileName.IsEmpty())
  1533. {
  1534. if ((FTransferResumeList->Count == 0) ||
  1535. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1536. {
  1537. // note that we do not recognize directories from files here
  1538. FTransferResumeList->Add(ProgressData.FullFileName);
  1539. }
  1540. }
  1541. }
  1542. }
  1543. //---------------------------------------------------------------------------
  1544. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1545. const TDateTime & StartTime)
  1546. {
  1547. if (GUIConfiguration->BeepOnFinish &&
  1548. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1549. {
  1550. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1551. DWORD Sound;
  1552. if (!ExtractFileExt(BeepSound).IsEmpty())
  1553. {
  1554. Sound = SND_FILENAME;
  1555. }
  1556. else
  1557. {
  1558. Sound = SND_ALIAS;
  1559. }
  1560. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1561. }
  1562. }
  1563. //---------------------------------------------------------------------------
  1564. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1565. TFileOperationProgressType & ProgressData)
  1566. {
  1567. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1568. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1569. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1570. FileOperationProgress(ProgressData);
  1571. }
  1572. //---------------------------------------------------------------------------
  1573. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1574. {
  1575. UnicodeString Result;
  1576. if (FProgressForm != NULL)
  1577. {
  1578. Result = FProgressForm->ProgressStr();
  1579. }
  1580. return Result;
  1581. }
  1582. //---------------------------------------------------------------------------
  1583. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1584. bool DragDrop)
  1585. {
  1586. return (!DragDrop && (DropSourceControl == NULL)) ||
  1587. (DropSourceControl == DirView(osOther));
  1588. }
  1589. //---------------------------------------------------------------------------
  1590. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1591. {
  1592. TCustomDirView * ADirView = DirView(Side);
  1593. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1594. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1595. // this can happen when local drive is unplugged in the middle of the operation
  1596. if (Item != NULL)
  1597. {
  1598. if (Unselect)
  1599. {
  1600. Item->Selected = false;
  1601. }
  1602. ADirView->MakeProgressVisible(Item);
  1603. }
  1604. return Item;
  1605. }
  1606. //---------------------------------------------------------------------------
  1607. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1608. TFileOperation Operation, TOperationSide Side,
  1609. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1610. TOnceDoneOperation & OnceDoneOperation)
  1611. {
  1612. if (!FAutoOperation)
  1613. {
  1614. // no selection on "/upload", form serves only as event handler
  1615. // (it is not displayed)
  1616. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1617. ((Operation == FPrimaryOperation) ||
  1618. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1619. {
  1620. TOperationSide DViewSide;
  1621. if ((FProgressSide != osCurrent) &&
  1622. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1623. {
  1624. // assume the operation is over the focused panel
  1625. DViewSide = FProgressSide;
  1626. }
  1627. else
  1628. {
  1629. DViewSide = Side;
  1630. }
  1631. if (FOnFileOperationFinished != NULL)
  1632. {
  1633. FOnFileOperationFinished(DViewSide, FileName, Success);
  1634. }
  1635. else
  1636. {
  1637. VisualiseOperationFinished(DViewSide, FileName, Success);
  1638. }
  1639. }
  1640. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1641. {
  1642. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1643. {
  1644. if (Side == osLocal)
  1645. {
  1646. Configuration->Usage->Inc(L"UploadedFiles");
  1647. }
  1648. else
  1649. {
  1650. Configuration->Usage->Inc(L"DownloadedFiles");
  1651. }
  1652. }
  1653. }
  1654. }
  1655. if (Success && (FSynchronizeController != NULL))
  1656. {
  1657. DebugAssert(!IsSideLocalBrowser(osRemote));
  1658. if (Operation == foCopy)
  1659. {
  1660. DebugAssert(Side == osLocal);
  1661. FSynchronizeController->LogOperation(soUpload, FileName);
  1662. }
  1663. else if (Operation == foDelete)
  1664. {
  1665. DebugAssert(Side == osRemote);
  1666. FSynchronizeController->LogOperation(soDelete, FileName);
  1667. }
  1668. }
  1669. if (FProgressForm)
  1670. {
  1671. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1672. }
  1673. }
  1674. //---------------------------------------------------------------------------
  1675. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1676. TFileOperation Operation, TOperationSide Side,
  1677. bool Temp, const UnicodeString & FileName, Boolean Success,
  1678. TOnceDoneOperation & OnceDoneOperation)
  1679. {
  1680. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1681. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1682. OnceDoneOperation);
  1683. }
  1684. //---------------------------------------------------------------------------
  1685. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1686. {
  1687. return false;
  1688. }
  1689. //---------------------------------------------------------------------------
  1690. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1691. {
  1692. return false;
  1693. }
  1694. //---------------------------------------------------------------------------
  1695. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1696. {
  1697. return false;
  1698. }
  1699. //---------------------------------------------------------------------------
  1700. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1701. {
  1702. return NULL;
  1703. }
  1704. //---------------------------------------------------------------------------
  1705. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1706. {
  1707. DebugAssert(GetSide(Side) == osRemote);
  1708. DebugUsedParam(Side);
  1709. return RemoteDirView;
  1710. }
  1711. //---------------------------------------------------------------------------
  1712. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1713. {
  1714. DebugAssert(GetSide(Side) == osRemote);
  1715. DebugUsedParam(Side);
  1716. return RemoteDriveView;
  1717. }
  1718. //---------------------------------------------------------------------------
  1719. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1720. {
  1721. DebugAssert(GetSide(Side) == osRemote);
  1722. DebugUsedParam(Side);
  1723. return HasActiveTerminal();
  1724. }
  1725. //---------------------------------------------------------------------------
  1726. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1727. TOperationSide Side, int FilesOnly)
  1728. {
  1729. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1730. }
  1731. //---------------------------------------------------------------------------
  1732. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1733. TOperationSide Side, int FilesOnly)
  1734. {
  1735. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1736. }
  1737. //---------------------------------------------------------------------------
  1738. struct THistoryItemData
  1739. {
  1740. short int Side;
  1741. short int Index;
  1742. };
  1743. //---------------------------------------------------------------------------
  1744. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1745. {
  1746. DirView(Side)->HistoryGo(Index);
  1747. }
  1748. //---------------------------------------------------------------------------
  1749. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1750. {
  1751. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1752. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1753. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1754. }
  1755. //---------------------------------------------------------------------------
  1756. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1757. bool Back)
  1758. {
  1759. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1760. {
  1761. TCustomDirView * DView = DirView(Side);
  1762. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1763. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1764. if (ICount > 10)
  1765. {
  1766. ICount = 10;
  1767. }
  1768. Menu->Items->Clear();
  1769. THistoryItemData Data;
  1770. Data.Side = Side;
  1771. for (short int i = 1; i <= ICount; i++)
  1772. {
  1773. TTBCustomItem * Item = new TTBXItem(Menu);
  1774. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1775. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1776. Item->Hint = DView->HistoryPath[Data.Index];
  1777. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1778. Item->Tag = *reinterpret_cast<int*>(&Data);
  1779. Item->OnClick = HistoryItemClick;
  1780. Menu->Items->Add(Item);
  1781. }
  1782. }
  1783. }
  1784. //---------------------------------------------------------------------------
  1785. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1786. TOperationSide Side, bool Back)
  1787. {
  1788. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1789. {
  1790. // workaround
  1791. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1792. // operator new allocates memory in Pascal code, but calls inline
  1793. // contructor in C++, leading in problems, the function does
  1794. // both in Pascal code
  1795. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1796. UpdateHistoryMenu(Side, Back);
  1797. }
  1798. return FHistoryMenu[Side == osLocal][Back];
  1799. }
  1800. //---------------------------------------------------------------------------
  1801. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1802. TCustomDirView *Sender)
  1803. {
  1804. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1805. UpdateHistoryMenu(Side, true);
  1806. UpdateHistoryMenu(Side, false);
  1807. }
  1808. //---------------------------------------------------------------------------
  1809. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1810. {
  1811. // remote custom commands can be executed only if the server supports shell commands
  1812. // or have secondary shell
  1813. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1814. }
  1815. //---------------------------------------------------------------------------
  1816. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1817. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1818. {
  1819. // -1 = hidden, 0 = disabled, 1 = enabled
  1820. int Result;
  1821. TFileCustomCommand RemoteCustomCommand;
  1822. TLocalCustomCommand LocalCustomCommand;
  1823. TFileCustomCommand * NonInteractiveCustomCommand =
  1824. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1825. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1826. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1827. if (FLAGCLEAR(Command.Params, ccLocal))
  1828. {
  1829. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1830. // custom command that does not operate with files can be executed anytime ...
  1831. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1832. {
  1833. // ... but do not show such command in remote file menu
  1834. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1835. {
  1836. Result = AllowedState;
  1837. }
  1838. else
  1839. {
  1840. Result = -1;
  1841. }
  1842. }
  1843. else
  1844. {
  1845. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1846. {
  1847. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1848. }
  1849. else
  1850. {
  1851. Result = -1;
  1852. }
  1853. }
  1854. }
  1855. else
  1856. {
  1857. // custom command that does not operate with files can be executed anytime
  1858. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1859. {
  1860. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1861. }
  1862. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1863. {
  1864. if ((ListType == ccltAll) || (ListType == ccltFile))
  1865. {
  1866. // special case is "diff"-style command that can be executed over any side,
  1867. // if we have both sides
  1868. Result =
  1869. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1870. // directly (instead of EnableSelectedOperation) to pass
  1871. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1872. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1873. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1874. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1875. }
  1876. else if (ListType == ccltBoth)
  1877. {
  1878. DebugAssert(!IsLocalBrowserMode());
  1879. Result = 1;
  1880. }
  1881. else
  1882. {
  1883. Result = -1;
  1884. }
  1885. }
  1886. else
  1887. {
  1888. if ((ListType == ccltAll) ||
  1889. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1890. {
  1891. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1892. }
  1893. else
  1894. {
  1895. Result = -1;
  1896. }
  1897. }
  1898. if ((Result > 0) &&
  1899. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1900. !HasActiveTerminal())
  1901. {
  1902. Result = 0;
  1903. }
  1904. }
  1905. return Result;
  1906. }
  1907. //---------------------------------------------------------------------------
  1908. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1909. TStrings * FileList, const TCustomCommandType & ACommand,
  1910. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1911. {
  1912. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1913. {
  1914. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1915. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1916. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1917. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1918. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1919. bool Capture =
  1920. FLAGSET(ACommand.Params, ccShowResults) ||
  1921. FLAGSET(ACommand.Params, ccCopyResults) ||
  1922. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1923. TCaptureOutputEvent OutputEvent = NULL;
  1924. DebugAssert(FCapturedLog == NULL);
  1925. if (Capture)
  1926. {
  1927. FCapturedLog = new TStringList();
  1928. OutputEvent = TerminalCaptureLog;
  1929. }
  1930. try
  1931. {
  1932. if (!RemoteCustomCommand.IsFileCommand(Command))
  1933. {
  1934. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1935. OutputEvent);
  1936. }
  1937. else
  1938. {
  1939. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1940. }
  1941. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1942. {
  1943. if (FLAGSET(ACommand.Params, ccCopyResults))
  1944. {
  1945. CopyToClipboard(FCapturedLog);
  1946. }
  1947. if (FLAGSET(ACommand.Params, ccShowResults))
  1948. {
  1949. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1950. }
  1951. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1952. {
  1953. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1954. }
  1955. }
  1956. }
  1957. __finally
  1958. {
  1959. SAFE_DESTROY(FCapturedLog);
  1960. }
  1961. }
  1962. }
  1963. //---------------------------------------------------------------------------
  1964. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1965. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1966. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1967. {
  1968. DebugAssert(!IsLocalBrowserMode());
  1969. TStrings * LocalFileList = NULL;
  1970. TStrings * RemoteFileList = NULL;
  1971. TStrings * RemoteFileListFull = NULL;
  1972. try
  1973. {
  1974. if (LocalFileCommand)
  1975. {
  1976. if (ALocalFileList == NULL)
  1977. {
  1978. DebugAssert(HasDirView[osLocal]);
  1979. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1980. // directly (instead of EnableSelectedOperation) to pass
  1981. // false to FocusedFileOnlyWhenFocused
  1982. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1983. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1984. }
  1985. else
  1986. {
  1987. LocalFileList = ALocalFileList;
  1988. }
  1989. if (FileListCommand)
  1990. {
  1991. if (LocalFileList->Count != 1)
  1992. {
  1993. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1994. }
  1995. }
  1996. else
  1997. {
  1998. if ((LocalFileList->Count != 1) &&
  1999. (FileList->Count != 1) &&
  2000. (LocalFileList->Count != FileList->Count))
  2001. {
  2002. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  2003. }
  2004. }
  2005. }
  2006. UnicodeString RootTempDir;
  2007. UnicodeString TempDir;
  2008. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  2009. if (!RemoteFiles)
  2010. {
  2011. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true, true);
  2012. }
  2013. try
  2014. {
  2015. TDateTimes RemoteFileTimes;
  2016. if (RemoteFiles)
  2017. {
  2018. RemoteFileList = FileList;
  2019. }
  2020. else
  2021. {
  2022. RemoteFileList = new TStringList();
  2023. TMakeLocalFileListParams MakeFileListParam;
  2024. MakeFileListParam.FileList = RemoteFileList;
  2025. MakeFileListParam.FileTimes = &RemoteFileTimes;
  2026. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2027. MakeFileListParam.Recursive =
  2028. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  2029. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2030. if (!MakeFileListParam.Recursive)
  2031. {
  2032. RemoteFileTimes.clear();
  2033. RemoteFileListFull = new TStringList();
  2034. MakeFileListParam.FileList = RemoteFileListFull;
  2035. MakeFileListParam.Recursive = true;
  2036. MakeFileListParam.IncludeDirs = false;
  2037. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2038. }
  2039. else
  2040. {
  2041. RemoteFileListFull = RemoteFileList;
  2042. }
  2043. }
  2044. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  2045. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2046. if (!NonBlocking)
  2047. {
  2048. int Count;
  2049. if (FileListCommand)
  2050. {
  2051. Count = 1;
  2052. }
  2053. else if (LocalFileCommand)
  2054. {
  2055. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  2056. }
  2057. else
  2058. {
  2059. Count = RemoteFileList->Count;
  2060. }
  2061. Progress.Start(foCustomCommand, osRemote, Count);
  2062. DebugAssert(FProgressForm != NULL);
  2063. FProgressForm->ReadOnly = true;
  2064. }
  2065. try
  2066. {
  2067. if (FileListCommand)
  2068. {
  2069. UnicodeString LocalFile;
  2070. // MakeFileList does not delimit filenames
  2071. UnicodeString FileList = MakeFileList(RemoteFileList);
  2072. if (LocalFileCommand)
  2073. {
  2074. DebugAssert(LocalFileList->Count == 1);
  2075. LocalFile = LocalFileList->Strings[0];
  2076. }
  2077. TLocalCustomCommand CustomCommand(Data,
  2078. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  2079. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  2080. if (NonBlocking)
  2081. {
  2082. DebugAssert(!POutput);
  2083. ExecuteShellChecked(ShellCommand);
  2084. }
  2085. else
  2086. {
  2087. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2088. }
  2089. }
  2090. else if (LocalFileCommand)
  2091. {
  2092. if (LocalFileList->Count == 1)
  2093. {
  2094. UnicodeString LocalFile = LocalFileList->Strings[0];
  2095. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2096. {
  2097. UnicodeString FileName = RemoteFileList->Strings[Index];
  2098. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2099. Progress.SetFile(FileNameForProgress);
  2100. TLocalCustomCommand CustomCommand(Data,
  2101. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2102. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2103. TOnceDoneOperation OnceDoneOperation;
  2104. // Do not unselect anything, as it won't work with two panels anyway
  2105. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2106. }
  2107. }
  2108. else if (RemoteFileList->Count == 1)
  2109. {
  2110. UnicodeString FileName = RemoteFileList->Strings[0];
  2111. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2112. {
  2113. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2114. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2115. Progress.SetFile(LocalFileNameOnly);
  2116. TLocalCustomCommand CustomCommand(
  2117. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2118. FileName, LocalFileName, L"");
  2119. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2120. TOnceDoneOperation OnceDoneOperation;
  2121. // Do not unselect anything, as it won't work with two panels anyway
  2122. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2123. }
  2124. }
  2125. else
  2126. {
  2127. if (LocalFileList->Count != RemoteFileList->Count)
  2128. {
  2129. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2130. }
  2131. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2132. {
  2133. UnicodeString FileName = RemoteFileList->Strings[Index];
  2134. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2135. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2136. Progress.SetFile(LocalFileNameOnly); // sic
  2137. TLocalCustomCommand CustomCommand(
  2138. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2139. FileName, LocalFileName, L"");
  2140. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2141. TOnceDoneOperation OnceDoneOperation;
  2142. // Do not unselect anything, as it won't work with two panels anyway
  2143. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2144. }
  2145. }
  2146. }
  2147. else
  2148. {
  2149. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2150. {
  2151. UnicodeString FileName = RemoteFileList->Strings[Index];
  2152. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2153. Progress.SetFile(FileNameForProgress);
  2154. TLocalCustomCommand CustomCommand(Data,
  2155. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2156. FileName, L"", L"");
  2157. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2158. TOnceDoneOperation OnceDoneOperation;
  2159. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2160. }
  2161. }
  2162. }
  2163. __finally
  2164. {
  2165. if (!NonBlocking)
  2166. {
  2167. Progress.Stop();
  2168. }
  2169. }
  2170. DebugAssert(!FAutoOperation);
  2171. if (!RemoteFiles)
  2172. {
  2173. TempDir = IncludeTrailingBackslash(TempDir);
  2174. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2175. {
  2176. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2177. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2178. // Skip directories, process only nested files.
  2179. // The check is redundant as FileAge fails for directories anyway.
  2180. !DirectoryExists(FileName))
  2181. {
  2182. UnicodeString RemoteDir =
  2183. UnixExtractFileDir(
  2184. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2185. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2186. TDateTime NewTime;
  2187. if (FileAge(FileName, NewTime) &&
  2188. (NewTime != RemoteFileTimes[Index]))
  2189. {
  2190. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2191. TemporaryFileCopyParam(CopyParam);
  2192. CopyParam.FileMask = L"";
  2193. FAutoOperation = true;
  2194. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2195. TemporaryFilesList->Add(FileName);
  2196. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2197. }
  2198. }
  2199. }
  2200. }
  2201. }
  2202. __finally
  2203. {
  2204. FAutoOperation = false;
  2205. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2206. {
  2207. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2208. }
  2209. }
  2210. }
  2211. __finally
  2212. {
  2213. if (RemoteFileList != FileList)
  2214. {
  2215. delete RemoteFileList;
  2216. }
  2217. if (LocalFileList != ALocalFileList)
  2218. {
  2219. delete LocalFileList;
  2220. }
  2221. if (RemoteFileListFull != RemoteFileList)
  2222. {
  2223. delete RemoteFileListFull;
  2224. }
  2225. }
  2226. }
  2227. //---------------------------------------------------------------------------
  2228. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2229. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2230. bool FileListCommand, UnicodeString * POutput)
  2231. {
  2232. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2233. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2234. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2235. {
  2236. UnicodeString FileName = SelectedFileList->Strings[Index];
  2237. if (DirectoryExists(FileName))
  2238. {
  2239. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2240. {
  2241. LocalFileList->Add(FileName);
  2242. }
  2243. if (FLAGSET(ACommand.Params, ccRecursive))
  2244. {
  2245. TMakeLocalFileListParams MakeFileListParam;
  2246. MakeFileListParam.FileList = LocalFileList.get();
  2247. MakeFileListParam.FileTimes = NULL;
  2248. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2249. MakeFileListParam.Recursive = true;
  2250. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2251. }
  2252. }
  2253. else
  2254. {
  2255. LocalFileList->Add(FileName);
  2256. }
  2257. }
  2258. UnicodeString RemotePath;
  2259. if (Terminal != NULL)
  2260. {
  2261. RemotePath = Terminal->CurrentDirectory;
  2262. }
  2263. if (FileListCommand)
  2264. {
  2265. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2266. TLocalCustomCommand CustomCommand(
  2267. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2268. L"", L"", FileList);
  2269. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2270. }
  2271. else
  2272. {
  2273. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2274. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2275. DebugAssert(FProgressForm != NULL);
  2276. FProgressForm->ReadOnly = true;
  2277. try
  2278. {
  2279. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2280. {
  2281. UnicodeString FileName = LocalFileList->Strings[Index];
  2282. Progress.SetFile(FileName);
  2283. TLocalCustomCommand CustomCommand(
  2284. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2285. FileName, L"", L"");
  2286. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2287. TOnceDoneOperation OnceDoneOperation;
  2288. Progress.Finish(FileName, true, OnceDoneOperation);
  2289. }
  2290. }
  2291. __finally
  2292. {
  2293. Progress.Stop();
  2294. }
  2295. }
  2296. }
  2297. //---------------------------------------------------------------------------
  2298. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2299. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2300. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2301. {
  2302. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2303. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2304. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2305. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2306. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2307. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2308. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2309. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2310. std::unique_ptr<UnicodeString> POutput;
  2311. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2312. FLAGSET(ACommand.Params, ccCopyResults))
  2313. {
  2314. POutput.reset(new UnicodeString());
  2315. }
  2316. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2317. FProgressSide = FCurrentSide;
  2318. if (!LocalCustomCommand.IsFileCommand(Command))
  2319. {
  2320. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2321. }
  2322. // remote files?
  2323. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2324. {
  2325. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2326. }
  2327. // local files
  2328. else
  2329. {
  2330. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2331. }
  2332. if (POutput.get() != NULL)
  2333. {
  2334. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2335. int P = POutput->Pos(sLineBreak);
  2336. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2337. {
  2338. POutput->SetLength(P - 1);
  2339. }
  2340. // Copy even empty output to clipboard
  2341. if (FLAGSET(ACommand.Params, ccCopyResults))
  2342. {
  2343. CopyToClipboard(*POutput);
  2344. }
  2345. // But do not show an empty message box.
  2346. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2347. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2348. !POutput->IsEmpty())
  2349. {
  2350. TClipboardHandler ClipboardHandler;
  2351. ClipboardHandler.Text = *POutput;
  2352. TMessageParams Params;
  2353. TQueryButtonAlias Aliases[1];
  2354. Aliases[0].Button = qaRetry;
  2355. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2356. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2357. Params.Aliases = Aliases;
  2358. Params.AliasesCount = LENOF(Aliases);
  2359. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2360. }
  2361. }
  2362. }
  2363. //---------------------------------------------------------------------------
  2364. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2365. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2366. {
  2367. TCustomCommandData Data;
  2368. UnicodeString Site;
  2369. UnicodeString RemotePath;
  2370. if (HasActiveTerminal())
  2371. {
  2372. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2373. Data = TCustomCommandData(SessionData.get());
  2374. Site = Terminal->SessionData->SessionKey;
  2375. RemotePath = Terminal->CurrentDirectory;
  2376. }
  2377. UnicodeString HelpKeyword = ACommand.HomePage;
  2378. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2379. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2380. {
  2381. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2382. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2383. {
  2384. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2385. }
  2386. else
  2387. {
  2388. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2389. }
  2390. if (!DoCustomCommandOptionsDialog(
  2391. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2392. {
  2393. Abort();
  2394. }
  2395. }
  2396. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2397. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2398. {
  2399. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2400. }
  2401. else
  2402. {
  2403. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2404. }
  2405. }
  2406. //---------------------------------------------------------------------------
  2407. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2408. const TCustomCommandType & Command)
  2409. {
  2410. DebugAssert(FCustomCommandLocalFileList != NULL);
  2411. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2412. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2413. TStrings * LocalFileList = new TStringList();
  2414. TStrings * RemoteFileList = new TStringList();
  2415. try
  2416. {
  2417. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2418. {
  2419. LocalFileList->Clear();
  2420. LocalFileList->AddObject(
  2421. FCustomCommandLocalFileList->Strings[Index],
  2422. FCustomCommandLocalFileList->Objects[Index]);
  2423. RemoteFileList->Clear();
  2424. RemoteFileList->AddObject(
  2425. FCustomCommandRemoteFileList->Strings[Index],
  2426. FCustomCommandRemoteFileList->Objects[Index]);
  2427. CustomCommand(RemoteFileList, Command, LocalFileList);
  2428. }
  2429. }
  2430. __finally
  2431. {
  2432. delete LocalFileList;
  2433. delete RemoteFileList;
  2434. }
  2435. }
  2436. //---------------------------------------------------------------------------
  2437. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2438. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2439. {
  2440. delete FCustomCommandLocalFileList;
  2441. delete FCustomCommandRemoteFileList;
  2442. // takeover ownership,
  2443. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2444. FCustomCommandLocalFileList = LocalFileList;
  2445. FCustomCommandRemoteFileList = RemoteFileList;
  2446. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2447. if (Button != NULL)
  2448. {
  2449. FCustomCommandMenu->Items->Clear();
  2450. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2451. MenuPopup(FCustomCommandMenu, Button);
  2452. }
  2453. else
  2454. {
  2455. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2456. }
  2457. }
  2458. //---------------------------------------------------------------------------
  2459. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2460. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2461. {
  2462. DebugAssert(FCapturedLog != NULL);
  2463. if ((OutputType == cotOutput) || (OutputType == cotError))
  2464. {
  2465. FCapturedLog->Add(AddedLine);
  2466. }
  2467. }
  2468. //---------------------------------------------------------------------------
  2469. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2470. TOperationSide Side)
  2471. {
  2472. return
  2473. (Side == osOther) &&
  2474. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2475. }
  2476. //---------------------------------------------------------------------------
  2477. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2478. TOperationSide Side, TTBXCustomItem * Item,
  2479. TResolvedDoubleClickAction DoubleClickAction1, TResolvedDoubleClickAction DoubleClickAction2)
  2480. {
  2481. TCustomDirView * DView = DirView(Side);
  2482. bool IsDefault = false;
  2483. if (DView->ItemFocused != NULL)
  2484. {
  2485. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  2486. TResolvedDoubleClickAction DefaultAction =
  2487. WinConfiguration->ResolveDoubleClickAction(DView->ItemIsDirectory(DView->ItemFocused), ATerminal);
  2488. IsDefault = (DefaultAction == DoubleClickAction1) || (DefaultAction == DoubleClickAction2);
  2489. }
  2490. TTBItemOptions O = Item->Options;
  2491. if (IsDefault)
  2492. {
  2493. Item->Options = O << tboDefault;
  2494. }
  2495. else
  2496. {
  2497. Item->Options = O >> tboDefault;
  2498. }
  2499. }
  2500. //---------------------------------------------------------------------------
  2501. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2502. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2503. {
  2504. TCustomDirView * DView = DirView(Side);
  2505. TListItem * Item = DView->ItemFocused;
  2506. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2507. EnableFocusedOperation[Side])
  2508. {
  2509. TPoint ClientPoint;
  2510. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2511. TPoint(0, 0) : MousePos;
  2512. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2513. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2514. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2515. }
  2516. }
  2517. //---------------------------------------------------------------------------
  2518. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2519. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2520. {
  2521. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, rdcaChangeDir, rdcaOpen);
  2522. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, rdcaEdit, rdcaNone);
  2523. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, rdcaCopy, rdcaNone);
  2524. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2525. Handled = true;
  2526. }
  2527. //---------------------------------------------------------------------------
  2528. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2529. {
  2530. }
  2531. //---------------------------------------------------------------------------
  2532. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2533. {
  2534. DebugAssert(FErrorList == NULL);
  2535. if (WinConfiguration->ContinueOnError)
  2536. {
  2537. FErrorList = new TStringList();
  2538. Configuration->Usage->Inc(L"ContinuationsOnError");
  2539. }
  2540. NonVisualDataModule->StartBusy();
  2541. }
  2542. //---------------------------------------------------------------------------
  2543. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2544. {
  2545. NonVisualDataModule->EndBusy();
  2546. if (FErrorList)
  2547. {
  2548. HandleErrorList(FErrorList);
  2549. }
  2550. }
  2551. //---------------------------------------------------------------------------
  2552. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2553. const TCopyParamType & CopyParam)
  2554. {
  2555. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2556. {
  2557. Configuration->Usage->Inc(L"FileMaskUses");
  2558. }
  2559. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2560. {
  2561. Configuration->Usage->Inc(L"OperationMaskUses");
  2562. }
  2563. }
  2564. //---------------------------------------------------------------------------
  2565. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2566. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2567. {
  2568. DebugAssert(!IsLocalBrowserMode());
  2569. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2570. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2571. TTransferOperationParam DefaultParam;
  2572. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2573. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2574. switch (Param.Queue)
  2575. {
  2576. case asOn:
  2577. CopyParam.Queue = true;
  2578. break;
  2579. case asOff:
  2580. CopyParam.Queue = false;
  2581. break;
  2582. case asAuto:
  2583. default:
  2584. // keep default
  2585. break;
  2586. }
  2587. bool Result =
  2588. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2589. if (Result)
  2590. {
  2591. DebugAssert(Terminal);
  2592. bool SelectionRestored = false;
  2593. TCustomDirView * DView = NULL;
  2594. if (HasDirView[Side])
  2595. {
  2596. DView = DirView(Side);
  2597. DView->SaveSelection();
  2598. DView->SaveSelectedNames();
  2599. }
  2600. UpdateCopyParamCounters(CopyParam);
  2601. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2602. DebugAssert(FTransferResumeList == NULL);
  2603. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2604. FMoveToQueue = false;
  2605. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2606. try
  2607. {
  2608. TStrings * PermanentFileList;
  2609. std::unique_ptr<TStrings> PermanentFileListOwner;
  2610. try
  2611. {
  2612. if (Side == osLocal)
  2613. {
  2614. PermanentFileList = FileList;
  2615. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2616. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2617. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2618. if (Operation == foMove)
  2619. {
  2620. ReloadLocalDirectory();
  2621. if (DView != NULL)
  2622. {
  2623. DView->RestoreSelection();
  2624. }
  2625. SelectionRestored = true;
  2626. }
  2627. }
  2628. else
  2629. {
  2630. // Clone the file list as it may refer to current directory files,
  2631. // which get destroyed, when the source directory is reloaded after foMove operation.
  2632. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2633. // But for a hotfix, we are not going to do such a big change.
  2634. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2635. PermanentFileList = PermanentFileListOwner.get();
  2636. try
  2637. {
  2638. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2639. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2640. }
  2641. __finally
  2642. {
  2643. if (Operation == foMove)
  2644. {
  2645. if (DView != NULL)
  2646. {
  2647. DView->RestoreSelection();
  2648. }
  2649. SelectionRestored = true;
  2650. }
  2651. ReloadLocalDirectory(Param.TargetDirectory);
  2652. }
  2653. }
  2654. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2655. if (FMoveToQueue)
  2656. {
  2657. Abort();
  2658. }
  2659. }
  2660. catch (EAbort &)
  2661. {
  2662. if (FMoveToQueue)
  2663. {
  2664. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2665. DebugAssert(CopyParam.TransferSkipList == NULL);
  2666. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2667. if (TransferResumeList->Count > 0)
  2668. {
  2669. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2670. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2671. }
  2672. CopyParam.TransferSkipList = TransferResumeList.release();
  2673. // not really needed, just to keep it consistent with TransferResumeList
  2674. FTransferResumeList = NULL;
  2675. FMoveToQueue = false;
  2676. Configuration->Usage->Inc("MovesToBackground");
  2677. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2678. ClearTransferSourceSelection(Direction);
  2679. }
  2680. throw;
  2681. }
  2682. }
  2683. __finally
  2684. {
  2685. if (!SelectionRestored && (DView != NULL))
  2686. {
  2687. DView->DiscardSavedSelection();
  2688. }
  2689. FTransferResumeList = NULL;
  2690. }
  2691. }
  2692. return Result;
  2693. }
  2694. //---------------------------------------------------------------------------
  2695. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2696. TOperationSide Side, TStrings * FileList, void * Param)
  2697. {
  2698. DebugAssert(FileList->Count);
  2699. // We deliberately do not toggle alternative flag (Param), but use OR,
  2700. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2701. // shortcut of CurrentDeleteAlternativeAction
  2702. bool Alternative =
  2703. bool(Param) || UseAlternativeFunction();
  2704. bool Recycle;
  2705. if (IsSideLocalBrowser(Side))
  2706. {
  2707. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2708. }
  2709. else
  2710. {
  2711. Recycle =
  2712. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2713. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2714. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2715. }
  2716. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2717. if (!Result)
  2718. {
  2719. UnicodeString Query;
  2720. if (FileList->Count == 1)
  2721. {
  2722. if (IsSideLocalBrowser(Side))
  2723. {
  2724. Query = ExtractFileName(FileList->Strings[0]);
  2725. }
  2726. else
  2727. {
  2728. Query = UnixExtractFileName(FileList->Strings[0]);
  2729. }
  2730. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2731. }
  2732. else
  2733. {
  2734. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2735. }
  2736. TMessageParams Params(mpNeverAskAgainCheck);
  2737. Params.ImageName = L"Delete file";
  2738. unsigned int Answer =
  2739. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2740. if (Answer == qaNeverAskAgain)
  2741. {
  2742. Result = true;
  2743. if (Recycle)
  2744. {
  2745. WinConfiguration->ConfirmRecycling = false;
  2746. }
  2747. else
  2748. {
  2749. WinConfiguration->ConfirmDeleting = false;
  2750. }
  2751. }
  2752. else
  2753. {
  2754. Result = (Answer == qaOK);
  2755. }
  2756. }
  2757. if (Result)
  2758. {
  2759. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2760. }
  2761. return Result;
  2762. }
  2763. //---------------------------------------------------------------------------
  2764. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2765. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2766. {
  2767. TAutoBatch AutoBatch(this);
  2768. bool Result;
  2769. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2770. {
  2771. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2772. }
  2773. else if (Operation == foRename)
  2774. {
  2775. DebugAssert(DirView(Side)->ItemFocused);
  2776. DirView(Side)->ItemFocused->EditCaption();
  2777. Result = true;
  2778. }
  2779. else if (Operation == foDelete)
  2780. {
  2781. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2782. }
  2783. else if (Operation == foSetProperties)
  2784. {
  2785. DirView(osRemote)->SaveSelectedNames();
  2786. Result = SetProperties(Side, FileList);
  2787. }
  2788. else if (Operation == foCustomCommand)
  2789. {
  2790. DebugAssert(Param);
  2791. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2792. DirView(osRemote)->SaveSelectedNames();
  2793. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2794. CustomCommand(FileList, *Command, NULL);
  2795. Result = true;
  2796. }
  2797. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2798. {
  2799. DebugAssert(!IsSideLocalBrowser(Side));
  2800. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2801. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2802. }
  2803. else if (Operation == foLock)
  2804. {
  2805. DebugAssert(!IsSideLocalBrowser(Side));
  2806. LockFiles(FileList, true);
  2807. Result = true;
  2808. }
  2809. else if (Operation == foUnlock)
  2810. {
  2811. DebugAssert(!IsSideLocalBrowser(Side));
  2812. LockFiles(FileList, false);
  2813. Result = true;
  2814. }
  2815. else
  2816. {
  2817. DebugFail();
  2818. }
  2819. return Result;
  2820. }
  2821. //---------------------------------------------------------------------------
  2822. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2823. {
  2824. if (Side == osCurrent)
  2825. {
  2826. Side = FCurrentSide;
  2827. }
  2828. return Side;
  2829. }
  2830. //---------------------------------------------------------------------------
  2831. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2832. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2833. {
  2834. Side = GetSide(Side);
  2835. bool FullPath = IsSideLocalBrowser(Side);
  2836. TCustomDirView * ADirView = DirView(Side);
  2837. std::unique_ptr<TStringList> FileList(new TStringList());
  2838. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2839. bool ReloadProperties =
  2840. !IsSideLocalBrowser(Side) && (Operation == foSetProperties) &&
  2841. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2842. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2843. if (Result && ReloadProperties)
  2844. {
  2845. std::unique_ptr<TStringList> FileList2(new TStringList());
  2846. FileList->CaseSensitive = true;
  2847. FileList->Sort();
  2848. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2849. {
  2850. TListItem * Item = ADirView->Items->Item[Index];
  2851. UnicodeString FileName = ADirView->ItemFileName(Item);
  2852. int Index2;
  2853. if (FileList->Find(FileName, Index2))
  2854. {
  2855. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2856. }
  2857. }
  2858. LoadFilesProperties(FileList2.get());
  2859. }
  2860. return Result;
  2861. }
  2862. //---------------------------------------------------------------------------
  2863. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2864. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2865. bool NoConfirmation, void * Param)
  2866. {
  2867. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2868. {
  2869. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2870. {
  2871. DebugAssert(!IsLocalBrowserMode());
  2872. if (GetSide(Side) == osLocal)
  2873. {
  2874. Configuration->Usage->Inc(L"UploadsCommand");
  2875. }
  2876. else
  2877. {
  2878. Configuration->Usage->Inc(L"DownloadsCommand");
  2879. }
  2880. }
  2881. }
  2882. }
  2883. //---------------------------------------------------------------------------
  2884. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2885. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2886. {
  2887. TTransferOperationParam Param;
  2888. if ((WinConfiguration->Interface != ifCommander) ||
  2889. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2890. {
  2891. Flags &= ~cocShortCutHint;
  2892. }
  2893. TCustomDirView * DView = DirView(Side);
  2894. Param.Options =
  2895. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2896. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2897. if (FLAGSET(Flags, cocQueue))
  2898. {
  2899. Param.Queue = asOn;
  2900. }
  2901. else if (FLAGSET(Flags, cocNonQueue))
  2902. {
  2903. Param.Queue = asOff;
  2904. }
  2905. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2906. }
  2907. //---------------------------------------------------------------------------
  2908. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2909. {
  2910. int Count = ErrorList->Count;
  2911. try
  2912. {
  2913. if (ErrorList->Count)
  2914. {
  2915. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2916. if (MessageDialog(Message, qtError,
  2917. qaOK | qaCancel, HELP_NONE) == qaOK)
  2918. {
  2919. unsigned int Answer;
  2920. int Index = 0;
  2921. do
  2922. {
  2923. DebugAssert(Index >= 0 && Index < Count);
  2924. TQueryButtonAlias Aliases[2];
  2925. Aliases[0].Button = qaYes;
  2926. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2927. Aliases[1].Button = qaNo;
  2928. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2929. TMessageParams Params;
  2930. Params.Aliases = Aliases;
  2931. Params.AliasesCount = LENOF(Aliases);
  2932. int No = Index + 1;
  2933. UnicodeString Message = ErrorList->Strings[Index];
  2934. UnicodeString AMainInstructions;
  2935. UnicodeString Details;
  2936. if (ExtractMainInstructions(Message, AMainInstructions))
  2937. {
  2938. Details = Message;
  2939. Message = AMainInstructions;
  2940. }
  2941. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2942. Answer = MoreMessageDialog(
  2943. Message,
  2944. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2945. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2946. qaOK, HELP_NONE, &Params);
  2947. if (Answer == qaNo)
  2948. {
  2949. Index++;
  2950. }
  2951. if (Answer == qaYes)
  2952. {
  2953. Index--;
  2954. }
  2955. }
  2956. while (Answer != qaOK);
  2957. }
  2958. }
  2959. }
  2960. __finally
  2961. {
  2962. TStrings * List = ErrorList;
  2963. ErrorList = NULL;
  2964. for (int i = 0; i < Count; i++)
  2965. {
  2966. delete List->Objects[i];
  2967. }
  2968. delete List;
  2969. }
  2970. }
  2971. //---------------------------------------------------------------------------
  2972. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2973. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2974. {
  2975. // needed for checking filemasks, as there's no directory object
  2976. // associated with the file object
  2977. File->FullFileName = FullFileName;
  2978. TFileMasks::TParams MaskParams;
  2979. MaskParams.Size = File->Size;
  2980. MaskParams.Modification = File->Modification;
  2981. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2982. }
  2983. //---------------------------------------------------------------------------
  2984. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2985. {
  2986. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2987. Side = GetSide(Side);
  2988. TCustomDirView * DView = DirView(Side);
  2989. TListItem * FocusedItem = DView->ItemFocused;
  2990. UnicodeString Name;
  2991. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2992. {
  2993. Name = DView->ItemFileName(FocusedItem);
  2994. }
  2995. else
  2996. {
  2997. Name = LoadStr(NEW_FILE);
  2998. }
  2999. UnicodeString Names = Name;
  3000. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  3001. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  3002. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  3003. {
  3004. while (!Names.IsEmpty())
  3005. {
  3006. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  3007. CustomWinConfiguration->History[L"EditFile"] = History.get();
  3008. UnicodeString TargetFileName;
  3009. UnicodeString LocalFileName;
  3010. UnicodeString RootTempDir;
  3011. UnicodeString TempDir;
  3012. UnicodeString RemoteDirectory;
  3013. bool ExistingFile = false;
  3014. if (!IsSideLocalBrowser(Side))
  3015. {
  3016. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  3017. TRemoteFile * File = NULL;
  3018. if (Terminal->FileExists(Name, &File) &&
  3019. !File->IsDirectory)
  3020. {
  3021. try
  3022. {
  3023. ExecuteRemoteFile(Name, File, efDefaultEditor);
  3024. ExistingFile = true;
  3025. }
  3026. __finally
  3027. {
  3028. delete File;
  3029. }
  3030. }
  3031. if (!ExistingFile)
  3032. {
  3033. RemoteDirectory = UnixExtractFilePath(Name);
  3034. TemporaryDirectoryForRemoteFiles(
  3035. RemoteDirectory, GUIConfiguration->CurrentCopyParam, false, TempDir, RootTempDir);
  3036. TargetFileName = UnixExtractFileName(Name);
  3037. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3038. LocalFileName = TempDir +
  3039. // We probably do not want to trim the VMS version here
  3040. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  3041. }
  3042. }
  3043. else
  3044. {
  3045. if (ExtractFilePath(Name).IsEmpty())
  3046. {
  3047. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  3048. }
  3049. else
  3050. {
  3051. LocalFileName = ExpandFileName(Name);
  3052. }
  3053. TargetFileName = ExtractFileName(Name);
  3054. }
  3055. if (!ExistingFile)
  3056. {
  3057. bool NewFile = !FileExists(ApiPath(LocalFileName));
  3058. if (NewFile)
  3059. {
  3060. int File = FileCreate(ApiPath(LocalFileName));
  3061. if (File < 0)
  3062. {
  3063. if (!RootTempDir.IsEmpty())
  3064. {
  3065. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3066. }
  3067. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  3068. }
  3069. else
  3070. {
  3071. FileClose(File);
  3072. }
  3073. }
  3074. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  3075. const TEditorData * ExternalEditor = NULL;
  3076. TFileMasks::TParams MaskParams; // size not known
  3077. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  3078. false, MaskParams);
  3079. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  3080. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  3081. }
  3082. }
  3083. }
  3084. }
  3085. //---------------------------------------------------------------------------
  3086. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  3087. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  3088. {
  3089. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3090. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3091. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3092. return
  3093. ((ExecuteFileBy == efInternalEditor)) ||
  3094. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  3095. }
  3096. //---------------------------------------------------------------------------
  3097. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  3098. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  3099. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  3100. UnicodeString RemoteDirectory, bool NewFile)
  3101. {
  3102. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3103. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3104. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3105. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3106. Side = GetSide(Side);
  3107. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3108. if (!IsSideLocalBrowser(Side))
  3109. {
  3110. Data->Terminal = Terminal;
  3111. Data->Queue = Queue;
  3112. Data->SessionData = CloneCurrentSessionData();
  3113. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3114. Data->RemoteDirectory = RemoteDirectory;
  3115. Data->SessionName = Terminal->SessionData->SessionName;
  3116. Data->LocalRootDirectory = LocalRootDirectory;
  3117. Data->OriginalFileName = OriginalFileName;
  3118. Data->Command = L""; // will be changed later for external editor
  3119. }
  3120. if (ExecuteFileBy == efInternalEditor)
  3121. {
  3122. if (!IsSideLocalBrowser(Side))
  3123. {
  3124. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3125. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3126. TForm * Editor;
  3127. try
  3128. {
  3129. Editor =
  3130. ShowEditorForm(
  3131. FileName, this,
  3132. FEditorManager->FileChanged, FEditorManager->FileReload, FEditorManager->FileClosed,
  3133. SaveAllInternalEditors, AnyInternalEditorModified,
  3134. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3135. }
  3136. catch(...)
  3137. {
  3138. if (!LocalRootDirectory.IsEmpty())
  3139. {
  3140. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  3141. }
  3142. throw;
  3143. }
  3144. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3145. AppLogFmt(L"Opened internal editor with \"%s\"", (FileName));
  3146. }
  3147. else
  3148. {
  3149. DebugAssert(!StandaloneOperation);
  3150. TForm * Editor =
  3151. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3152. SaveAllInternalEditors, AnyInternalEditorModified,
  3153. L"", false, SessionColor, -1, NewFile);
  3154. FLocalEditors->Add(Editor);
  3155. }
  3156. }
  3157. else
  3158. {
  3159. HANDLE Process;
  3160. if (ExecuteFileBy == efExternalEditor)
  3161. {
  3162. UnicodeString Program, Params, Dir;
  3163. Data->Command = ExternalEditor->ExternalEditor;
  3164. ReformatFileNameCommand(Data->Command);
  3165. SplitCommand(Data->Command, Program, Params, Dir);
  3166. Params = ExpandFileNameCommand(Params, FileName);
  3167. Program = ExpandEnvironmentVariables(Program);
  3168. if (!ExecuteShell(Program, Params, Process))
  3169. {
  3170. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3171. }
  3172. }
  3173. else
  3174. {
  3175. DebugAssert(!IsSideLocalBrowser(Side));
  3176. if (!ExecuteShell(FileName, L"", Process))
  3177. {
  3178. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3179. }
  3180. }
  3181. if (IsSideLocalBrowser(Side) ||
  3182. ((ExecuteFileBy == efShell) &&
  3183. !WinConfiguration->Editor.SDIShellEditor) ||
  3184. ((ExecuteFileBy == efExternalEditor) &&
  3185. !ExternalEditor->SDIExternalEditor))
  3186. {
  3187. // no need for handle
  3188. if (Process != NULL)
  3189. {
  3190. DebugCheck(CloseHandle(Process));
  3191. }
  3192. Process = INVALID_HANDLE_VALUE;
  3193. }
  3194. else
  3195. {
  3196. if (Process == NULL)
  3197. {
  3198. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3199. }
  3200. }
  3201. if (!IsSideLocalBrowser(Side))
  3202. {
  3203. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3204. AppLogFmt(L"Opened external editor with remote file \"%s\".", (FileName));
  3205. }
  3206. }
  3207. }
  3208. //---------------------------------------------------------------------------
  3209. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3210. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3211. void * /*Arg*/)
  3212. {
  3213. if (Token != NULL)
  3214. {
  3215. EditorFormFileSave(static_cast<TForm *>(Token));
  3216. }
  3217. }
  3218. //---------------------------------------------------------------------------
  3219. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3220. {
  3221. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3222. {
  3223. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3224. }
  3225. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3226. }
  3227. //---------------------------------------------------------------------------
  3228. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3229. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3230. void * Arg)
  3231. {
  3232. if ((Token != NULL) &&
  3233. IsEditorFormModified(static_cast<TForm *>(Token)))
  3234. {
  3235. *static_cast<bool *>(Arg) = true;
  3236. }
  3237. }
  3238. //---------------------------------------------------------------------------
  3239. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3240. TObject * /*Sender*/, bool & Modified)
  3241. {
  3242. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3243. {
  3244. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3245. {
  3246. Modified = true;
  3247. }
  3248. }
  3249. if (!Modified)
  3250. {
  3251. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3252. }
  3253. }
  3254. //---------------------------------------------------------------------------
  3255. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3256. {
  3257. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3258. }
  3259. //---------------------------------------------------------------------------
  3260. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3261. const UnicodeString & RemoteDirectory, const TCopyParamType & CopyParam, bool Simple,
  3262. UnicodeString & Result, UnicodeString & RootDirectory)
  3263. {
  3264. if (!WinConfiguration->TemporaryDirectoryDeterministic || Simple)
  3265. {
  3266. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3267. Result = RootDirectory;
  3268. }
  3269. else
  3270. {
  3271. RootDirectory = L"";
  3272. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3273. Result = IncludeTrailingBackslash(Result);
  3274. }
  3275. if (WinConfiguration->TemporaryDirectoryAppendSession && !Simple)
  3276. {
  3277. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3278. }
  3279. if (WinConfiguration->TemporaryDirectoryAppendPath && !Simple)
  3280. {
  3281. UnicodeString ARemoteDirectory = RemoteDirectory;
  3282. if (!ARemoteDirectory.IsEmpty() && (ARemoteDirectory[1] == L'/'))
  3283. {
  3284. ARemoteDirectory.Delete(1, 1);
  3285. }
  3286. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(ARemoteDirectory)));
  3287. }
  3288. if (!ForceDirectories(ApiPath(Result)))
  3289. {
  3290. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3291. }
  3292. }
  3293. //---------------------------------------------------------------------------
  3294. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3295. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3296. bool GetTargetNames, bool AutoOperation, bool SimpleTempDir)
  3297. {
  3298. DebugAssert(!IsLocalBrowserMode());
  3299. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3300. if (ForceText)
  3301. {
  3302. CopyParam.TransferMode = tmAscii;
  3303. }
  3304. TemporaryFileCopyParam(CopyParam);
  3305. if (TempDir.IsEmpty())
  3306. {
  3307. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, SimpleTempDir, TempDir, RootTempDir);
  3308. }
  3309. DebugAssert(!FAutoOperation);
  3310. FAutoOperation = AutoOperation;
  3311. Terminal->ExceptionOnFail = true;
  3312. try
  3313. {
  3314. try
  3315. {
  3316. // turn off confirmations, as for MDI editors we may possibly download
  3317. // the same file over
  3318. Terminal->CopyToLocal(
  3319. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3320. if (GetTargetNames)
  3321. {
  3322. for (int i = 0; i < FileList->Count; i++)
  3323. {
  3324. FileList->Strings[i] =
  3325. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3326. }
  3327. }
  3328. }
  3329. catch(...)
  3330. {
  3331. if (!RootTempDir.IsEmpty())
  3332. {
  3333. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3334. }
  3335. throw;
  3336. }
  3337. }
  3338. __finally
  3339. {
  3340. FAutoOperation = false;
  3341. Terminal->ExceptionOnFail = false;
  3342. }
  3343. }
  3344. //---------------------------------------------------------------------------
  3345. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3346. {
  3347. // Do not waste time checking for default editor list the next time,
  3348. // or testing if default editor is not notepad.
  3349. bool TryNextTime = false;
  3350. UnicodeString UsageState;
  3351. if (!WinConfiguration->EditorList->IsDefaultList())
  3352. {
  3353. UsageState = "H";
  3354. }
  3355. else
  3356. {
  3357. UnicodeString Executable;
  3358. UnicodeString ExecutableDescription;
  3359. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3360. {
  3361. UnicodeString Message =
  3362. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3363. unsigned int Answer =
  3364. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3365. if (Answer != qaOK)
  3366. {
  3367. UsageState = "R";
  3368. }
  3369. else
  3370. {
  3371. UsageState = "A";
  3372. TEditorData EditorData;
  3373. EditorData.Editor = edExternal;
  3374. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3375. EditorData.ExternalEditorOptionsAutodetect();
  3376. TEditorList EditorList;
  3377. EditorList = *WinConfiguration->EditorList;
  3378. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3379. WinConfiguration->EditorList = &EditorList;
  3380. }
  3381. }
  3382. }
  3383. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3384. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3385. }
  3386. //---------------------------------------------------------------------------
  3387. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3388. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3389. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3390. {
  3391. if (ExecuteFileBy == efDefaultEditor)
  3392. {
  3393. if (!WinConfiguration->OfferedEditorAutoConfig)
  3394. {
  3395. EditorAutoConfig();
  3396. }
  3397. const TEditorPreferences * Editor =
  3398. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3399. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3400. {
  3401. ExecuteFileBy = efInternalEditor;
  3402. ExternalEditor = NULL;
  3403. }
  3404. else if (Editor->Data->Editor == edOpen)
  3405. {
  3406. ExecuteFileBy = efShell;
  3407. ExternalEditor = NULL;
  3408. }
  3409. else
  3410. {
  3411. ExecuteFileBy = efExternalEditor;
  3412. ExternalEditor = Editor->Data;
  3413. }
  3414. }
  3415. }
  3416. //---------------------------------------------------------------------------
  3417. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3418. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3419. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3420. {
  3421. UnicodeString OriginalFileName;
  3422. UnicodeString LocalRootDirectory;
  3423. UnicodeString RemoteDirectory;
  3424. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3425. IsSideLocalBrowser(Side), MaskParams);
  3426. UnicodeString Counter;
  3427. UnicodeString LocalFileName;
  3428. bool Handled = false;
  3429. if (!IsSideLocalBrowser(Side))
  3430. {
  3431. // We need to trim VMS version here, so that we use name without version
  3432. // when uploading back to create a new version of the file
  3433. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3434. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3435. TObject * Token = NULL;
  3436. UnicodeString LocalDirectory;
  3437. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3438. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3439. LocalDirectory))
  3440. {
  3441. if (Token != NULL)
  3442. {
  3443. TForm * Form = dynamic_cast<TForm *>(Token);
  3444. if (Form->WindowState == wsMinimized)
  3445. {
  3446. ShowWindow(Form->Handle, SW_RESTORE);
  3447. }
  3448. else
  3449. {
  3450. Form->SetFocus();
  3451. }
  3452. Handled = true;
  3453. }
  3454. else
  3455. {
  3456. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3457. }
  3458. }
  3459. if (!Handled)
  3460. {
  3461. TStringList * FileList1 = new TStringList();
  3462. try
  3463. {
  3464. FileList1->AddObject(FullFileName, Object);
  3465. TemporarilyDownloadFiles(FileList1,
  3466. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3467. LocalRootDirectory, LocalDirectory, true, true, false);
  3468. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3469. }
  3470. __finally
  3471. {
  3472. delete FileList1;
  3473. }
  3474. switch (ExecuteFileBy)
  3475. {
  3476. case efShell:
  3477. Counter = "RemoteFilesExecuted";
  3478. break;
  3479. case efInternalEditor:
  3480. Counter = "RemoteFilesOpenedInInternalEditor";
  3481. break;
  3482. case efExternalEditor:
  3483. Counter = "RemoteFilesOpenedInExternalEditor";
  3484. break;
  3485. default:
  3486. DebugFail();
  3487. }
  3488. }
  3489. }
  3490. else
  3491. {
  3492. LocalFileName = FullFileName;
  3493. OriginalFileName = ExtractFileName(FullFileName);
  3494. switch (ExecuteFileBy)
  3495. {
  3496. case efShell:
  3497. Counter = "LocalFilesExecuted";
  3498. break;
  3499. case efInternalEditor:
  3500. Counter = "LocalFilesOpenedInInternalEditor";
  3501. break;
  3502. case efExternalEditor:
  3503. Counter = "LocalFilesOpenedInExternalEditor";
  3504. break;
  3505. default:
  3506. DebugFail();
  3507. }
  3508. }
  3509. if (!Handled)
  3510. {
  3511. Configuration->Usage->Inc(Counter);
  3512. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3513. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3514. }
  3515. }
  3516. //---------------------------------------------------------------------------
  3517. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3518. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3519. bool AllSelected, bool OnFocused)
  3520. {
  3521. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3522. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3523. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3524. Side = GetSide(Side);
  3525. TCustomDirView * DView = DirView(Side);
  3526. TStrings * FileList =
  3527. AllSelected ?
  3528. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3529. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3530. try
  3531. {
  3532. DebugAssert(AllSelected || (FileList->Count == 1));
  3533. for (int i = 0; i < FileList->Count; i++)
  3534. {
  3535. UnicodeString ListFileName = FileList->Strings[i];
  3536. UnicodeString FileNameOnly =
  3537. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3538. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3539. if (!DView->ItemIsDirectory(Item))
  3540. {
  3541. UnicodeString FullFileName;
  3542. if (!IsSideLocalBrowser(Side))
  3543. {
  3544. FullFileName = DirView(Side)->Path + ListFileName;
  3545. }
  3546. else
  3547. {
  3548. FullFileName = ListFileName;
  3549. }
  3550. TObject * Object = FileList->Objects[i];
  3551. TFileMasks::TParams MaskParams;
  3552. MaskParams.Size = DView->ItemFileSize(Item);
  3553. TDateTimePrecision Precision;
  3554. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3555. std::unique_ptr<TRemoteFile> FileOwner;
  3556. if (!IsSideLocalBrowser(Side))
  3557. {
  3558. TRemoteFile * File = NULL;
  3559. Terminal->FileExists(FullFileName, &File);
  3560. if (File != NULL)
  3561. {
  3562. File->FullFileName = FullFileName;
  3563. Object = File;
  3564. FileOwner.reset(File);
  3565. MaskParams.Size = File->Size;
  3566. MaskParams.Modification = File->Modification;
  3567. }
  3568. }
  3569. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName, Object, MaskParams);
  3570. }
  3571. }
  3572. }
  3573. __finally
  3574. {
  3575. delete FileList;
  3576. }
  3577. }
  3578. //---------------------------------------------------------------------------
  3579. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3580. {
  3581. CopyParam.FileNameCase = ncNoChange;
  3582. CopyParam.PreserveRights = false;
  3583. CopyParam.PreserveReadOnly = false;
  3584. CopyParam.ReplaceInvalidChars = true;
  3585. CopyParam.IncludeFileMask = TFileMasks();
  3586. CopyParam.NewerOnly = false;
  3587. CopyParam.FileMask = L"";
  3588. }
  3589. //---------------------------------------------------------------------------
  3590. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
  3591. const UnicodeString & FileName, TEditedFileData * Data, HANDLE UploadCompleteEvent, bool & Retry)
  3592. {
  3593. TTerminalManager * Manager = TTerminalManager::Instance();
  3594. if (!IsActiveTerminal(Data->Terminal))
  3595. {
  3596. if (!NonVisualDataModule->Busy)
  3597. {
  3598. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3599. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3600. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3601. if (Data->Terminal == NULL)
  3602. {
  3603. TManagedTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3604. if (SameSiteTerminal != NULL)
  3605. {
  3606. UnicodeString Message =
  3607. FMTLOAD(EDIT_SESSION_REATTACH,
  3608. (FileNameOnly, Data->SessionName, FileNameOnly));
  3609. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3610. {
  3611. Data->Terminal = SameSiteTerminal;
  3612. Data->Queue = Manager->FindQueueForTerminal(SameSiteTerminal);
  3613. Data->SessionName = SameSiteTerminal->SessionData->SessionName;
  3614. // We might also overwrite session data
  3615. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3616. }
  3617. else
  3618. {
  3619. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3620. Abort();
  3621. }
  3622. }
  3623. }
  3624. // foreground session should reconnect itself
  3625. else if (Terminal != Data->Terminal)
  3626. {
  3627. UnicodeString Message =
  3628. MainInstructions(
  3629. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3630. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3631. {
  3632. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3633. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3634. }
  3635. else
  3636. {
  3637. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3638. Abort();
  3639. }
  3640. }
  3641. }
  3642. if (!IsActiveTerminal(Data->Terminal))
  3643. {
  3644. Configuration->Usage->Inc(L"EditInactiveSession");
  3645. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3646. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3647. (ExtractFileName(FileName), Data->SessionName)));
  3648. }
  3649. }
  3650. TStrings * FileList = new TStringList();
  3651. try
  3652. {
  3653. FileList->Add(FileName);
  3654. // Consider using the same settings (preset) as when the file was downloaded.
  3655. // More over this does not reflect the actual session that will do the upload.
  3656. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3657. TemporaryFileCopyParam(CopyParam);
  3658. if (Data->ForceText)
  3659. {
  3660. CopyParam.TransferMode = tmAscii;
  3661. }
  3662. // so i do not need to worry if masking algorithm works in all cases
  3663. // ("" means "copy file name", no masking is actually done)
  3664. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3665. {
  3666. CopyParam.FileMask = L"";
  3667. }
  3668. else
  3669. {
  3670. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3671. }
  3672. int Params = cpNoConfirmation | cpTemporary;
  3673. if (Data->Terminal->IsCapable[fcBackgroundTransfers])
  3674. {
  3675. DebugAssert(Data->Queue != NULL);
  3676. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3677. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3678. QueueItem->CompleteEvent = UploadCompleteEvent;
  3679. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3680. }
  3681. else
  3682. {
  3683. if (NonVisualDataModule->Busy)
  3684. {
  3685. Retry = true;
  3686. }
  3687. else
  3688. {
  3689. Data->Terminal->CopyToRemote(FileList, Data->RemoteDirectory, &CopyParam, Params, NULL);
  3690. SetEvent(UploadCompleteEvent);
  3691. }
  3692. }
  3693. }
  3694. __finally
  3695. {
  3696. delete FileList;
  3697. }
  3698. }
  3699. //---------------------------------------------------------------------------
  3700. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3701. const UnicodeString FileName, const TEditedFileData * Data)
  3702. {
  3703. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3704. DebugAssert(!NonVisualDataModule->Busy);
  3705. if (!IsActiveTerminal(Data->Terminal))
  3706. {
  3707. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3708. (ExtractFileName(FileName), Data->SessionName)));
  3709. }
  3710. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3711. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3712. NonVisualDataModule->StartBusy();
  3713. try
  3714. {
  3715. std::unique_ptr<TRemoteFile> File;
  3716. UnicodeString RemoteFileName =
  3717. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3718. Terminal->ExceptionOnFail = true;
  3719. try
  3720. {
  3721. TRemoteFile * AFile = NULL;
  3722. Terminal->ReadFile(RemoteFileName, AFile);
  3723. File.reset(AFile);
  3724. if (!File->HaveFullFileName)
  3725. {
  3726. File->FullFileName = RemoteFileName;
  3727. }
  3728. }
  3729. __finally
  3730. {
  3731. Terminal->ExceptionOnFail = false;
  3732. }
  3733. std::unique_ptr<TStrings> FileList(new TStringList());
  3734. FileList->AddObject(RemoteFileName, File.get());
  3735. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3736. UnicodeString TempDir = ExtractFilePath(FileName);
  3737. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, true, false);
  3738. // sanity check, the target file name should be still the same
  3739. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3740. }
  3741. __finally
  3742. {
  3743. NonVisualDataModule->EndBusy();
  3744. // it actually may not exist anymore...
  3745. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3746. }
  3747. }
  3748. //---------------------------------------------------------------------------
  3749. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3750. const TEditedFileData * Data, bool & KeepOpen)
  3751. {
  3752. // Command is set for external editors only (not for "shell" open).
  3753. if (!Data->Command.IsEmpty())
  3754. {
  3755. bool AnyFound = false;
  3756. bool AnyMDI = false;
  3757. bool AnyNonMDI = false;
  3758. bool AnyDetect = false;
  3759. TEditorList * EditorList = new TEditorList();
  3760. try
  3761. {
  3762. *EditorList = *WinConfiguration->EditorList;
  3763. for (int i = 0; i < EditorList->Count; i++)
  3764. {
  3765. const TEditorPreferences * Editor = EditorList->Editors[i];
  3766. if ((Editor->Data->Editor == edExternal) &&
  3767. (Editor->Data->ExternalEditor == Data->Command))
  3768. {
  3769. AnyFound = true;
  3770. if (Editor->Data->SDIExternalEditor)
  3771. {
  3772. AnyNonMDI = true;
  3773. if (Editor->Data->DetectMDIExternalEditor)
  3774. {
  3775. AnyDetect = true;
  3776. }
  3777. }
  3778. else
  3779. {
  3780. AnyMDI = true;
  3781. }
  3782. }
  3783. }
  3784. bool EnableMDI = false;
  3785. bool DisableDetect = false;
  3786. if (AnyMDI)
  3787. {
  3788. KeepOpen = true;
  3789. if (AnyNonMDI)
  3790. {
  3791. // there is at least one instance of the editor with MDI support enabled,
  3792. // and one with disabled, enable it for all instances
  3793. EnableMDI = true;
  3794. }
  3795. }
  3796. else if (AnyFound && !AnyDetect)
  3797. {
  3798. // at least once instance found but all have MDI autodetection disabled
  3799. // => close the file (default action)
  3800. }
  3801. else
  3802. {
  3803. // no instance of the editor has MDI support enabled
  3804. TMessageParams Params;
  3805. if (AnyFound)
  3806. {
  3807. // there is at least one instance of the editor
  3808. Params.Params |= mpNeverAskAgainCheck;
  3809. }
  3810. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3811. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3812. switch (Answer)
  3813. {
  3814. case qaNeverAskAgain:
  3815. DisableDetect = true;
  3816. break;
  3817. case qaNo:
  3818. EnableMDI = true;
  3819. KeepOpen = true;
  3820. break;
  3821. }
  3822. }
  3823. if (AnyFound && (EnableMDI || DisableDetect))
  3824. {
  3825. bool Changed = false;
  3826. for (int i = 0; i < EditorList->Count; i++)
  3827. {
  3828. const TEditorPreferences * Editor = EditorList->Editors[i];
  3829. if ((Editor->Data->Editor == edExternal) &&
  3830. (Editor->Data->ExternalEditor == Data->Command) &&
  3831. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3832. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3833. {
  3834. Changed = true;
  3835. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3836. if (EnableMDI)
  3837. {
  3838. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3839. }
  3840. if (DisableDetect)
  3841. {
  3842. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3843. }
  3844. EditorList->Change(i, UpdatedEditor);
  3845. }
  3846. }
  3847. if (Changed)
  3848. {
  3849. WinConfiguration->EditorList = EditorList;
  3850. }
  3851. }
  3852. }
  3853. __finally
  3854. {
  3855. delete EditorList;
  3856. }
  3857. }
  3858. else
  3859. {
  3860. // "open" case
  3861. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3862. qaOK, HELP_APP_EARLY_CLOSED);
  3863. }
  3864. }
  3865. //---------------------------------------------------------------------------
  3866. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3867. {
  3868. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3869. }
  3870. //---------------------------------------------------------------------------
  3871. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3872. {
  3873. SideEnter(osRemote);
  3874. }
  3875. //---------------------------------------------------------------------------
  3876. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3877. {
  3878. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3879. SideEnter(osRemote);
  3880. }
  3881. //---------------------------------------------------------------------------
  3882. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3883. {
  3884. FCurrentSide = Side;
  3885. if (Visible)
  3886. {
  3887. UpdateControls();
  3888. }
  3889. }
  3890. //---------------------------------------------------------------------------
  3891. void __fastcall TCustomScpExplorerForm::FileDeleted(TOperationSide Side, const UnicodeString & FileName, bool Success)
  3892. {
  3893. VisualiseOperationFinished(Side, FileName, Success);
  3894. if (DebugAlwaysTrue(FDeletedFiles != NULL) && Success)
  3895. {
  3896. FDeletedFiles->Add(FileName);
  3897. }
  3898. }
  3899. //---------------------------------------------------------------------------
  3900. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3901. TStrings * FileList, bool Alternative)
  3902. {
  3903. TCustomDirView * DView = DirView(Side);
  3904. DView->SaveSelection();
  3905. DView->SaveSelectedNames();
  3906. DebugAssert(!FAlternativeDelete);
  3907. FAlternativeDelete = Alternative;
  3908. try
  3909. {
  3910. if (!IsSideLocalBrowser(Side))
  3911. {
  3912. DebugAssert(Terminal != NULL);
  3913. // Clone the file list as it may refer to current directory files, which get destroyed, when the directory is reloaded after the operation
  3914. std::unique_ptr<TStrings> PermanentFileList(TRemoteFileList::CloneStrings(FileList));
  3915. std::unique_ptr<TStringList> DeletedFiles(new TStringList());
  3916. DeletedFiles->CaseSensitive = DebugAlwaysTrue(!IsSideLocalBrowser(Side));
  3917. int Params = FLAGMASK(Alternative, dfAlternative);
  3918. try
  3919. {
  3920. TValueRestorer<TStrings *> DeletedFilesRestorer(FDeletedFiles);
  3921. DebugAssert(FDeletedFiles == NULL);
  3922. FDeletedFiles = DeletedFiles.get();
  3923. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  3924. FOnFileOperationFinished = FileDeleted;
  3925. FMoveToQueue = false;
  3926. Terminal->DeleteFiles(FileList, Params);
  3927. // Probably not needed for deleting, just for consistency with transfer code
  3928. if (FMoveToQueue)
  3929. {
  3930. Abort();
  3931. }
  3932. }
  3933. catch (EAbort &)
  3934. {
  3935. if (FMoveToQueue)
  3936. {
  3937. DeletedFiles->Sorted = true;
  3938. for (int Index = 0; Index < PermanentFileList->Count; Index++)
  3939. {
  3940. if (DeletedFiles->IndexOf(PermanentFileList->Strings[Index]) >= 0)
  3941. {
  3942. // We should always be deleting the first item => what can be used to optimize this code, if needed
  3943. DebugAssert(Index == 0);
  3944. PermanentFileList->Delete(Index);
  3945. Index--;
  3946. }
  3947. }
  3948. Configuration->Usage->Inc("MovesToBackgroundDelete");
  3949. TQueueItem * QueueItem = new TDeleteQueueItem(Terminal, PermanentFileList.get(), Params);
  3950. AddQueueItem(Queue, QueueItem, Terminal);
  3951. ClearOperationSelection(osRemote);
  3952. }
  3953. }
  3954. }
  3955. else
  3956. {
  3957. if (IsLocalBrowserMode())
  3958. {
  3959. Configuration->Usage->Inc(L"LocalLocalDeletes");
  3960. }
  3961. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  3962. FProgressSide = FCurrentSide;
  3963. try
  3964. {
  3965. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3966. }
  3967. __finally
  3968. {
  3969. ReloadLocalDirectory();
  3970. }
  3971. }
  3972. FAlternativeDelete = false;
  3973. }
  3974. catch(...)
  3975. {
  3976. FAlternativeDelete = false;
  3977. DView->DiscardSavedSelection();
  3978. throw;
  3979. }
  3980. DView->RestoreSelection();
  3981. }
  3982. //---------------------------------------------------------------------------
  3983. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3984. {
  3985. DebugAssert(Terminal);
  3986. DebugAssert(!IsLocalBrowserMode());
  3987. RemoteDirView->SaveSelection();
  3988. RemoteDirView->SaveSelectedNames();
  3989. try
  3990. {
  3991. if (Lock)
  3992. {
  3993. Terminal->LockFiles(FileList);
  3994. }
  3995. else
  3996. {
  3997. Terminal->UnlockFiles(FileList);
  3998. }
  3999. }
  4000. catch(...)
  4001. {
  4002. RemoteDirView->DiscardSavedSelection();
  4003. throw;
  4004. }
  4005. RemoteDirView->RestoreSelection();
  4006. }
  4007. //---------------------------------------------------------------------------
  4008. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  4009. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  4010. bool NoConfirmation, bool Move)
  4011. {
  4012. DebugAssert(Terminal != NULL);
  4013. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  4014. DebugAssert(!IsLocalBrowserMode());
  4015. if (Session == NULL)
  4016. {
  4017. Session = Terminal;
  4018. }
  4019. if (Session == Terminal)
  4020. {
  4021. if (RemoteDriveView->DropTarget != NULL)
  4022. {
  4023. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  4024. }
  4025. else if (RemoteDirView->DropTarget != NULL)
  4026. {
  4027. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  4028. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  4029. }
  4030. else
  4031. {
  4032. Target = RemoteDirView->Path;
  4033. }
  4034. }
  4035. else
  4036. {
  4037. Target = GetSessionPath(Session, osRemote);
  4038. }
  4039. Target = UnixIncludeTrailingBackslash(Target);
  4040. if (FileList->Count == 1)
  4041. {
  4042. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  4043. }
  4044. else
  4045. {
  4046. FileMask = AnyMask;
  4047. }
  4048. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  4049. bool Result = true;
  4050. if (!NoConfirmation)
  4051. {
  4052. bool Multi = (FileList->Count > 1);
  4053. if (Move)
  4054. {
  4055. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  4056. }
  4057. else
  4058. {
  4059. std::unique_ptr<TStrings> Sessions(new TStringList);
  4060. std::unique_ptr<TStrings> Directories(new TStringList);
  4061. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  4062. for (int Index = 0; Index < SessionList->Count; Index++)
  4063. {
  4064. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  4065. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  4066. {
  4067. Sessions->AddObject(SessionList->Strings[Index], ASession);
  4068. Directories->Add(GetSessionPath(ASession, osRemote));
  4069. }
  4070. }
  4071. TDirectRemoteCopy AllowDirectCopy;
  4072. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  4073. {
  4074. DebugAssert(DirectCopy);
  4075. AllowDirectCopy = drcAllow;
  4076. }
  4077. else if (Terminal->IsCapable[fcSecondaryShell])
  4078. {
  4079. DebugAssert(DirectCopy);
  4080. AllowDirectCopy = drcConfirmCommandSession;
  4081. }
  4082. else
  4083. {
  4084. DebugAssert(!DirectCopy);
  4085. AllowDirectCopy = drcDisallow;
  4086. }
  4087. void * ASession = Session;
  4088. Result = DoRemoteCopyDialog(
  4089. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal);
  4090. Session = static_cast<TManagedTerminal *>(ASession);
  4091. }
  4092. }
  4093. return Result;
  4094. }
  4095. //---------------------------------------------------------------------------
  4096. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  4097. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  4098. {
  4099. DebugAssert(!IsLocalBrowserMode());
  4100. bool DirectCopy;
  4101. UnicodeString Target, FileMask;
  4102. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  4103. if (Result)
  4104. {
  4105. if (!Move && !DirectCopy)
  4106. {
  4107. Configuration->Usage->Inc("RemoteCopyTemp");
  4108. UnicodeString RootTempDir;
  4109. UnicodeString TempDir;
  4110. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  4111. TStrings * TemporaryFilesList = new TStringList();
  4112. try
  4113. {
  4114. TMakeLocalFileListParams MakeFileListParam;
  4115. MakeFileListParam.FileList = TemporaryFilesList;
  4116. MakeFileListParam.FileTimes = NULL;
  4117. MakeFileListParam.IncludeDirs = true;
  4118. MakeFileListParam.Recursive = false;
  4119. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  4120. TTerminalManager::Instance()->ActiveSession = Session;
  4121. if (TemporaryFilesList->Count > 0)
  4122. {
  4123. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4124. CopyParam.FileMask = FileMask;
  4125. DebugAssert(!FAutoOperation);
  4126. FAutoOperation = true;
  4127. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  4128. }
  4129. }
  4130. __finally
  4131. {
  4132. delete TemporaryFilesList;
  4133. FAutoOperation = false;
  4134. if (!RootTempDir.IsEmpty())
  4135. {
  4136. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  4137. }
  4138. }
  4139. }
  4140. else
  4141. {
  4142. RemoteDirView->SaveSelection();
  4143. RemoteDirView->SaveSelectedNames();
  4144. try
  4145. {
  4146. if (Move)
  4147. {
  4148. Configuration->Usage->Inc("RemoteMove");
  4149. Terminal->MoveFiles(FileList, Target, FileMask, false);
  4150. }
  4151. else
  4152. {
  4153. Configuration->Usage->Inc("RemoteCopyDirect");
  4154. DebugAssert(DirectCopy);
  4155. DebugAssert(Session == Terminal);
  4156. if (Terminal->IsCapable[fcRemoteCopy] ||
  4157. Terminal->CommandSessionOpened ||
  4158. CommandSessionFallback())
  4159. {
  4160. Terminal->CopyFiles(FileList, Target, FileMask);
  4161. }
  4162. }
  4163. }
  4164. catch(...)
  4165. {
  4166. RemoteDirView->DiscardSavedSelection();
  4167. throw;
  4168. }
  4169. RemoteDirView->RestoreSelection();
  4170. }
  4171. }
  4172. return Result;
  4173. }
  4174. //---------------------------------------------------------------------------
  4175. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4176. const UnicodeString & Path, TRemoteProperties & Properties)
  4177. {
  4178. DebugAssert(!IsLocalBrowserMode());
  4179. Properties.Valid = Properties.Valid << vpEncrypt;
  4180. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4181. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4182. }
  4183. //---------------------------------------------------------------------------
  4184. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4185. {
  4186. Side = GetSide(Side);
  4187. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4188. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4189. UnicodeString Name = LoadStr(NEW_FOLDER);
  4190. int AllowedChanges =
  4191. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4192. bool SaveSettings = false;
  4193. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4194. {
  4195. TWindowLock Lock(this);
  4196. if (!IsSideLocalBrowser(Side))
  4197. {
  4198. if (SaveSettings)
  4199. {
  4200. GUIConfiguration->NewDirectoryProperties = Properties;
  4201. }
  4202. CreateRemoteDirectory(Name, Properties);
  4203. }
  4204. else
  4205. {
  4206. DirView(Side)->CreateDirectory(Name);
  4207. }
  4208. }
  4209. }
  4210. //---------------------------------------------------------------------------
  4211. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4212. {
  4213. TWindowLock Lock(this);
  4214. DirView(Side)->ExecuteHomeDirectory();
  4215. }
  4216. //---------------------------------------------------------------------------
  4217. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4218. {
  4219. DoOpenDirectoryDialog(odBrowse, Side);
  4220. }
  4221. //---------------------------------------------------------------------------
  4222. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4223. {
  4224. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4225. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4226. bool Result = !Path.IsEmpty();
  4227. if (Result)
  4228. {
  4229. TryOpenDirectory(Side, Path);
  4230. }
  4231. return Result;
  4232. }
  4233. //---------------------------------------------------------------------------
  4234. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4235. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4236. bool & Close)
  4237. {
  4238. // terminal can be already closed (e.g. dropped connection)
  4239. if (Terminal != NULL)
  4240. {
  4241. try
  4242. {
  4243. TCalculateSizeParams Params;
  4244. Params.Stats = &Stats;
  4245. Terminal->CalculateFilesSize(FileList, Size, Params);
  4246. }
  4247. catch(...)
  4248. {
  4249. if (!Terminal->Active)
  4250. {
  4251. Close = true;
  4252. }
  4253. throw;
  4254. }
  4255. }
  4256. }
  4257. //---------------------------------------------------------------------------
  4258. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4259. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4260. bool & Close)
  4261. {
  4262. if ((Terminal != NULL) && // terminal can be already closed (e.g. dropped connection)
  4263. EnsureCommandSessionFallback(fcCalculatingChecksum))
  4264. {
  4265. Configuration->Usage->Inc(L"ChecksumCalculated");
  4266. try
  4267. {
  4268. Terminal->CalculateFilesChecksum(Alg, FileList, OnCalculatedChecksum);
  4269. }
  4270. catch(...)
  4271. {
  4272. if (!Terminal->Active)
  4273. {
  4274. Close = true;
  4275. }
  4276. throw;
  4277. }
  4278. }
  4279. }
  4280. //---------------------------------------------------------------------------
  4281. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4282. {
  4283. if (Terminal->LoadFilesProperties(FileList))
  4284. {
  4285. RemoteDirView->Invalidate();
  4286. }
  4287. }
  4288. //---------------------------------------------------------------------------
  4289. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4290. {
  4291. bool Result;
  4292. if (!IsSideLocalBrowser(Side))
  4293. {
  4294. TRemoteTokenList * GroupList = NULL;
  4295. TRemoteTokenList * UserList = NULL;
  4296. try
  4297. {
  4298. TRemoteProperties CurrentProperties;
  4299. LoadFilesProperties(FileList);
  4300. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4301. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4302. if (CapableGroupChanging || CapableOwnerChanging)
  4303. {
  4304. if (CapableGroupChanging)
  4305. {
  4306. GroupList = Terminal->Groups->Duplicate();
  4307. }
  4308. if (CapableOwnerChanging)
  4309. {
  4310. UserList = Terminal->Users->Duplicate();
  4311. }
  4312. TRemoteDirectory * Files = Terminal->Files;
  4313. int Count = Files->Count;
  4314. if (Count > 100)
  4315. {
  4316. Count = 100;
  4317. }
  4318. for (int Index = 0; Index < Count; Index++)
  4319. {
  4320. TRemoteFile * File = Files->Files[Index];
  4321. if (CapableGroupChanging)
  4322. {
  4323. GroupList->AddUnique(File->Group);
  4324. }
  4325. if (CapableOwnerChanging)
  4326. {
  4327. UserList->AddUnique(File->Owner);
  4328. }
  4329. }
  4330. // if we haven't collected tokens for all files in current directory,
  4331. // make sure we collect them at least for all selected files.
  4332. // (note that so far the files in FileList has to be from current direcotry)
  4333. if (Count < Files->Count)
  4334. {
  4335. for (int Index = 0; Index < FileList->Count; Index++)
  4336. {
  4337. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4338. if (CapableGroupChanging)
  4339. {
  4340. GroupList->AddUnique(File->Group);
  4341. }
  4342. if (CapableOwnerChanging)
  4343. {
  4344. UserList->AddUnique(File->Owner);
  4345. }
  4346. }
  4347. }
  4348. }
  4349. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4350. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4351. for (int Index = 0; (Index < FileList->Count) && CapableAclChanging; Index++)
  4352. {
  4353. if (dynamic_cast<TRemoteFile *>(FileList->Objects[Index])->IsDirectory)
  4354. {
  4355. CapableAclChanging = false;
  4356. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4357. }
  4358. }
  4359. int Flags = 0;
  4360. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4361. if (CapableAclChanging) Flags |= cpAcl;
  4362. if (CapableOwnerChanging) Flags |= cpOwner;
  4363. if (CapableGroupChanging) Flags |= cpGroup;
  4364. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4365. if (Terminal->IsCapable[fcCalculatingChecksum] ||
  4366. (Terminal->IsCapable[fcSecondaryShell] && !Terminal->IsEncryptingFiles()))
  4367. {
  4368. CalculateChecksumEvent = CalculateChecksum;
  4369. }
  4370. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4371. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4372. TRemoteProperties NewProperties = CurrentProperties;
  4373. Result =
  4374. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4375. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4376. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4377. CalculateSize, CalculateChecksumEvent);
  4378. if (Result)
  4379. {
  4380. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4381. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4382. }
  4383. }
  4384. __finally
  4385. {
  4386. delete GroupList;
  4387. delete UserList;
  4388. }
  4389. }
  4390. else
  4391. {
  4392. DirView(Side)->DisplayPropertiesMenu();
  4393. Result = true;
  4394. }
  4395. return Result;
  4396. }
  4397. //---------------------------------------------------------------------------
  4398. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4399. {
  4400. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4401. {
  4402. FIgnoreNextDialogChar = Key;
  4403. }
  4404. Key = 0;
  4405. }
  4406. //---------------------------------------------------------------------------
  4407. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4408. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4409. {
  4410. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4411. if (Command != NULL)
  4412. {
  4413. KeyProcessed(Key, Shift);
  4414. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4415. {
  4416. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4417. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4418. false, false, const_cast<TCustomCommandType *>(Command));
  4419. }
  4420. }
  4421. }
  4422. //---------------------------------------------------------------------------
  4423. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4424. {
  4425. if (!NonVisualDataModule->Busy)
  4426. {
  4427. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4428. {
  4429. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4430. }
  4431. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4432. {
  4433. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4434. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4435. {
  4436. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4437. // But so far it's not a problem, as it does what we want.
  4438. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4439. if (((Action->ShortCut == KeyShortCut) ||
  4440. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4441. AllowedAction(Action, aaShortCut))
  4442. {
  4443. // Has to be called before the action as the dialog char is already in queue.
  4444. // So when the action consumes message queue, we already need to have the
  4445. // FIgnoreNextDialogChar set
  4446. KeyProcessed(Key, Shift);
  4447. // Reset reference to previous component (when menu/toolbar was clicked).
  4448. // Needed to detect that action was invoked by keyboard shortcut
  4449. // in TNonVisualDataModule::ExplorerActionsExecute
  4450. Action->ActionComponent = NULL;
  4451. Action->Execute();
  4452. return;
  4453. }
  4454. }
  4455. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4456. {
  4457. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4458. {
  4459. KeyProcessed(Key, Shift);
  4460. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4461. return;
  4462. }
  4463. }
  4464. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4465. {
  4466. KeyProcessed(Key, Shift);
  4467. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4468. }
  4469. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4470. if (IsCustomShortCut(CustomShortCut))
  4471. {
  4472. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4473. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4474. if (WinConfiguration->SharedBookmarks != NULL)
  4475. {
  4476. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4477. if ((Bookmark != NULL) &&
  4478. OpenBookmark(FCurrentSide, Bookmark))
  4479. {
  4480. KeyProcessed(Key, Shift);
  4481. }
  4482. }
  4483. }
  4484. }
  4485. }
  4486. TForm::KeyDown(Key, Shift);
  4487. }
  4488. //---------------------------------------------------------------------------
  4489. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4490. {
  4491. bool OpenedTerminal = false;
  4492. UnicodeString ProtocolName;
  4493. UnicodeString ProtocolNameHint;
  4494. UnicodeString SecurityProtocolName;
  4495. UnicodeString DurationHint;
  4496. // See the comment in TScpCommanderForm::UpdateControls()
  4497. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4498. if (ActiveTerminal != NULL)
  4499. {
  4500. // Active is not enough here
  4501. if (ActiveTerminal->Status == ssOpened)
  4502. {
  4503. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4504. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4505. if (FileSystemInfo.ProtocolName.IsEmpty())
  4506. {
  4507. ProtocolName = SessionInfo.ProtocolName;
  4508. }
  4509. else
  4510. {
  4511. ProtocolName = FileSystemInfo.ProtocolName;
  4512. }
  4513. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4514. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4515. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4516. OpenedTerminal = true;
  4517. }
  4518. else if (ActiveTerminal->Status > ssClosed)
  4519. {
  4520. ProtocolName = LoadStr(STATUS_CONNECTING);
  4521. }
  4522. else
  4523. {
  4524. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4525. }
  4526. }
  4527. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4528. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4529. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4530. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4531. if (ProtocolStatusPanel->Caption != ProtocolName)
  4532. {
  4533. ProtocolStatusPanel->Caption = ProtocolName;
  4534. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4535. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4536. }
  4537. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4538. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4539. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4540. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4541. SecurityStatusPanel->Enabled = SecurityEnabled;
  4542. UnicodeString SecurityProtocolHint;
  4543. if (SecurityEnabled)
  4544. {
  4545. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4546. }
  4547. else
  4548. {
  4549. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4550. }
  4551. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4552. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4553. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4554. DurationStatusPanel->Hint = DurationHint;
  4555. UpdateStatusBar();
  4556. }
  4557. //---------------------------------------------------------------------------
  4558. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4559. {
  4560. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4561. DebugAssert(SessionStatusBar != NULL);
  4562. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4563. if (!FNote.IsEmpty())
  4564. {
  4565. NoteStatusPanel->Caption = FNote;
  4566. }
  4567. else
  4568. {
  4569. UpdateStatusPanelText(NoteStatusPanel);
  4570. }
  4571. NoteStatusPanel->Hint = FNoteHints;
  4572. UnicodeString LoginTime;
  4573. // See the comment in TScpCommanderForm::UpdateControls()
  4574. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4575. // Active is not enough here
  4576. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4577. {
  4578. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4579. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4580. }
  4581. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4582. DurationStatusPanel->Caption = LoginTime;
  4583. }
  4584. //---------------------------------------------------------------------------
  4585. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4586. {
  4587. Panel->Caption = L"";
  4588. }
  4589. //---------------------------------------------------------------------------
  4590. void __fastcall TCustomScpExplorerForm::Idle()
  4591. {
  4592. if (FShowing ||
  4593. // Particularly for
  4594. // - Standalone editing
  4595. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4596. // while "keeping remote directory up to date".
  4597. StandaloneOperation)
  4598. {
  4599. FEditorManager->Check();
  4600. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4601. // that needs to know if queue view is visible (and it may be closed after queue update)
  4602. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4603. }
  4604. if (FShowing)
  4605. {
  4606. if (!NonVisualDataModule->Busy &&
  4607. // Menu is opened or drag&drop is going on
  4608. (Mouse->Capture == NULL))
  4609. {
  4610. if (FRefreshRemoteDirectory)
  4611. {
  4612. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4613. {
  4614. Terminal->RefreshDirectory();
  4615. }
  4616. FRefreshRemoteDirectory = false;
  4617. }
  4618. if (FRefreshLocalDirectory)
  4619. {
  4620. ReloadLocalDirectory();
  4621. FRefreshLocalDirectory = false;
  4622. }
  4623. if (WinConfiguration->RefreshRemotePanel)
  4624. {
  4625. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4626. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4627. {
  4628. RemoteDirView->ReloadDirectory();
  4629. }
  4630. }
  4631. }
  4632. // TB2 shows accels on Alt key down, but hides it only after menu modal loop finishes.
  4633. // But if Alt is used to switch apps or for keyboard shortcut, the menu modal loop does not happen and accelerators stay displayed.
  4634. // It's difficult to fix this in TB2, as there's no "up" equivalent to CM_DIALOGKEY, which shows the accels.
  4635. // And the Alt key can be released even when other window or application is active, so handling the key up in explorer window won't do either.
  4636. TTBCustomToolbar * MenuToolbar = DebugNotNull(dynamic_cast<TTBCustomToolbar *>(GetComponent(fcMenu)));
  4637. MenuToolbar->Idle();
  4638. }
  4639. if (FShowing || StandaloneOperation)
  4640. {
  4641. if (FQueueStatusInvalidated)
  4642. {
  4643. UpdateQueueStatus(false);
  4644. }
  4645. RefreshQueueItems();
  4646. }
  4647. if (FShowing)
  4648. {
  4649. UpdateStatusBar();
  4650. }
  4651. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4652. {
  4653. FClipboardFakeMonitorsPendingReset = false;
  4654. FClipboardFakeMonitors.reset(NULL);
  4655. }
  4656. FIgnoreNextDialogChar = 0;
  4657. }
  4658. //---------------------------------------------------------------------------
  4659. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4660. {
  4661. try
  4662. {
  4663. FUserActionTimer->Enabled = false;
  4664. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4665. {
  4666. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4667. {
  4668. FPendingQueueActionItem->ProcessUserAction();
  4669. }
  4670. }
  4671. }
  4672. __finally
  4673. {
  4674. FPendingQueueActionItem = NULL;
  4675. }
  4676. }
  4677. //---------------------------------------------------------------------------
  4678. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4679. {
  4680. if (WinConfiguration->MinimizeToTray)
  4681. {
  4682. UpdateTrayIcon();
  4683. FTrayIcon->Visible = true;
  4684. if (Visible)
  4685. {
  4686. ShowWindow(Handle, SW_HIDE);
  4687. }
  4688. }
  4689. }
  4690. //---------------------------------------------------------------------------
  4691. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4692. {
  4693. // WORKAROUND
  4694. // When restoring maximized window from minimization,
  4695. // rarely some controls do not align properly.
  4696. // Two instances seen (both for Commander):
  4697. // - When restoring, window is temporarily narrower (not maximized),
  4698. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4699. // Once maximized already, top dock shinks back, but the session PageControl,
  4700. // do not align up, leaving space between TopDock and PageControl.
  4701. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4702. for (int Index = 0; Index < ControlCount; Index++)
  4703. {
  4704. RealignControl(Controls[Index]);
  4705. }
  4706. if (FTrayIcon->Visible)
  4707. {
  4708. FTrayIcon->Visible = false;
  4709. if (Visible)
  4710. {
  4711. ShowWindow(Handle, SW_SHOW);
  4712. }
  4713. }
  4714. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4715. {
  4716. FNeedSession = false;
  4717. NonVisualDataModule->StartBusy();
  4718. try
  4719. {
  4720. NeedSession(true);
  4721. }
  4722. __finally
  4723. {
  4724. NonVisualDataModule->EndBusy();
  4725. }
  4726. }
  4727. }
  4728. //---------------------------------------------------------------------------
  4729. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4730. {
  4731. FTrayIcon->Hint = GetMainForm()->Caption;
  4732. }
  4733. //---------------------------------------------------------------------------
  4734. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4735. {
  4736. UpdateTrayIcon();
  4737. }
  4738. //---------------------------------------------------------------------------
  4739. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4740. {
  4741. // workaround
  4742. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4743. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4744. // But WM_ACTIVATEAPP message can be received even when
  4745. // the main window is minimized to the tray and internal editor window is focused
  4746. // (after another application was previously active)
  4747. if (::IsIconic(Handle))
  4748. {
  4749. if (!IsAppIconic())
  4750. {
  4751. SetAppIconic(true);
  4752. }
  4753. }
  4754. ::ApplicationRestore();
  4755. Application->BringToFront();
  4756. }
  4757. //---------------------------------------------------------------------------
  4758. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4759. {
  4760. RestoreApp();
  4761. }
  4762. //---------------------------------------------------------------------------
  4763. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4764. {
  4765. if (OpenInNewWindow())
  4766. {
  4767. ExecuteNewInstance(SessionUrl);
  4768. }
  4769. else
  4770. {
  4771. TTerminalManager::Instance()->NewSession(SessionUrl);
  4772. }
  4773. }
  4774. //---------------------------------------------------------------------------
  4775. void TCustomScpExplorerForm::NewTab(TOperationSide, bool)
  4776. {
  4777. NewSession();
  4778. }
  4779. //---------------------------------------------------------------------------
  4780. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4781. {
  4782. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4783. StoredSessions->NewSession(SessionName, SessionData);
  4784. // modified only, explicit
  4785. StoredSessions->Save(false, true);
  4786. // encode session name because of slashes in hierarchical sessions
  4787. return EncodeUrlString(SessionName);
  4788. }
  4789. //---------------------------------------------------------------------------
  4790. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4791. {
  4792. // current working directories become defaults here, what is not right
  4793. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4794. return SaveHiddenDuplicateSession(SessionData.get());
  4795. }
  4796. //---------------------------------------------------------------------------
  4797. void __fastcall TCustomScpExplorerForm::DuplicateTab()
  4798. {
  4799. if (OpenInNewWindow())
  4800. {
  4801. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4802. }
  4803. else
  4804. {
  4805. // current working directories become defaults here, what is not right
  4806. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4807. TTerminalManager * Manager = TTerminalManager::Instance();
  4808. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4809. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4810. // We definitelly want these
  4811. ASession->Disconnected = ManagedSession->Disconnected;
  4812. // Not sure about these two
  4813. ASession->Permanent = ManagedSession->Permanent;
  4814. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4815. Manager->ActiveSession = ASession;
  4816. }
  4817. }
  4818. //---------------------------------------------------------------------------
  4819. void __fastcall TCustomScpExplorerForm::RenameTab()
  4820. {
  4821. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4822. if (InputDialog(LoadStr(RENAME_TAB_TITLE), LoadStr(RENAME_TAB_PROMPT), Name, HELP_TAB_RENAME) &&
  4823. // When submitting the default name, do not fix it
  4824. (Name != ManagedSession->SessionData->SessionName))
  4825. {
  4826. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4827. TSessionData::ValidateName(Name);
  4828. // This is inconsistent with how color (for example) is handled.
  4829. ManagedSession->SessionData->Name = Name;
  4830. UpdateControls();
  4831. // Add/Remove distinguishing paths from sessions of the same name.
  4832. SessionListChanged();
  4833. }
  4834. }
  4835. //---------------------------------------------------------------------------
  4836. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4837. {
  4838. DebugAssert(Queue != NULL);
  4839. bool Result = Queue->IsEmpty;
  4840. if (!Result)
  4841. {
  4842. SetFocus();
  4843. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4844. }
  4845. return Result;
  4846. }
  4847. //---------------------------------------------------------------------------
  4848. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4849. {
  4850. return CanCloseQueue(FQueue);
  4851. }
  4852. //---------------------------------------------------------------------------
  4853. void __fastcall TCustomScpExplorerForm::CloseTab()
  4854. {
  4855. if (CanCloseQueue())
  4856. {
  4857. TTerminalManager::Instance()->FreeActiveTerminal();
  4858. }
  4859. }
  4860. //---------------------------------------------------------------------------
  4861. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4862. {
  4863. if (CanCloseQueue())
  4864. {
  4865. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4866. }
  4867. }
  4868. //---------------------------------------------------------------------------
  4869. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4870. {
  4871. DetachTerminal(Terminal);
  4872. RemoteDirView->Terminal = Terminal;
  4873. UpdateRemotePathComboBox(false);
  4874. InitStatusBar();
  4875. UpdateControls();
  4876. }
  4877. //---------------------------------------------------------------------------
  4878. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4879. {
  4880. InitStatusBar();
  4881. }
  4882. //---------------------------------------------------------------------------
  4883. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4884. {
  4885. if (DebugAlwaysTrue(!Terminal->Active))
  4886. {
  4887. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4888. }
  4889. }
  4890. //---------------------------------------------------------------------------
  4891. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4892. {
  4893. if (OpenInNewWindow())
  4894. {
  4895. // encode session name because of slashes in hierarchical sessions
  4896. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4897. }
  4898. else
  4899. {
  4900. TTerminalManager * Manager = TTerminalManager::Instance();
  4901. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4902. Manager->ActiveSession = Terminal;
  4903. }
  4904. }
  4905. //---------------------------------------------------------------------------
  4906. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  4907. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  4908. {
  4909. TTerminalManager * Manager = TTerminalManager::Instance();
  4910. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4911. // This asks for a master password. Maybe we should postpone asking until a connection is actually opened.
  4912. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4913. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  4914. {
  4915. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  4916. // FirstSession can be null, if none of the workspace sites exist anymore
  4917. // or if all workspace sessions are not suported by the interface
  4918. // (when trying to open all local-local workspace in "explorer" interface)
  4919. if (FirstSession != NULL)
  4920. {
  4921. if (!ConnectFirstTerminal)
  4922. {
  4923. FirstSession->Disconnected = true;
  4924. FirstSession->DisconnectedTemporarily = true;
  4925. }
  4926. Manager->ActiveSession = FirstSession;
  4927. }
  4928. }
  4929. }
  4930. //---------------------------------------------------------------------------
  4931. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4932. {
  4933. if (OpenInNewWindow())
  4934. {
  4935. ExecuteNewInstance(Name);
  4936. }
  4937. else
  4938. {
  4939. DoOpenFolderOrWorkspace(Name, true, false);
  4940. }
  4941. }
  4942. //---------------------------------------------------------------------------
  4943. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4944. bool &CanClose)
  4945. {
  4946. // See the comment in CloseApp()
  4947. if (NonVisualDataModule->Busy)
  4948. {
  4949. CanClose = false;
  4950. }
  4951. else
  4952. {
  4953. TTerminalManager * Manager = TTerminalManager::Instance();
  4954. int ActiveSessions = 0;
  4955. for (int Index = 0; Index < Manager->Count; Index++)
  4956. {
  4957. if (Manager->Sessions[Index]->Active)
  4958. {
  4959. ActiveSessions++;
  4960. }
  4961. }
  4962. // Confirm, when there's at least one active remote session or
  4963. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4964. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4965. WinConfiguration->ConfirmClosingSession)
  4966. {
  4967. unsigned int Result;
  4968. TMessageParams Params(mpNeverAskAgainCheck);
  4969. UnicodeString Message;
  4970. int Answers = qaOK | qaCancel;
  4971. // The current session is the only active session
  4972. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4973. {
  4974. if (!WinConfiguration->AutoSaveWorkspace)
  4975. {
  4976. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4977. }
  4978. else
  4979. {
  4980. Message = LoadStr(CLOSE_SESSION);
  4981. }
  4982. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4983. }
  4984. // Multiple active sessions or one active session, but it's not the current one
  4985. else if (ActiveSessions > 0)
  4986. {
  4987. if (!WinConfiguration->AutoSaveWorkspace)
  4988. {
  4989. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4990. }
  4991. else
  4992. {
  4993. Message = LoadStr(CLOSE_SESSIONS);
  4994. }
  4995. }
  4996. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  4997. else
  4998. {
  4999. DebugAlwaysTrue(Manager->Count > 1);
  5000. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  5001. Message = LoadStr(CLOSE_WORKSPACE);
  5002. }
  5003. UnicodeString Note;
  5004. if (WinConfiguration->AutoSaveWorkspace)
  5005. {
  5006. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  5007. }
  5008. else
  5009. {
  5010. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  5011. Answers = qaYes | qaNo | qaCancel;
  5012. }
  5013. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  5014. SetFocus();
  5015. Result = MessageDialog(Message, qtConfirmation,
  5016. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  5017. if (Result == qaNeverAskAgain)
  5018. {
  5019. WinConfiguration->ConfirmClosingSession = false;
  5020. }
  5021. if (Result == qaNo)
  5022. {
  5023. CanClose = SaveWorkspace(true);
  5024. // note that the workspace will be saved redundatly again from FormClose
  5025. }
  5026. else
  5027. {
  5028. CanClose =
  5029. (Result == qaOK) ||
  5030. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  5031. (Result == qaNeverAskAgain);
  5032. }
  5033. }
  5034. if (CanClose && HasActiveTerminal())
  5035. {
  5036. CanClose = CanCloseQueue();
  5037. }
  5038. }
  5039. if (CanClose)
  5040. {
  5041. CanClose =
  5042. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  5043. FEditorManager->CloseExternalFilesWithoutProcess();
  5044. if (CanClose)
  5045. {
  5046. while (CanClose && (FLocalEditors->Count > 0))
  5047. {
  5048. int PrevCount = FLocalEditors->Count;
  5049. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  5050. CanClose = (FLocalEditors->Count < PrevCount);
  5051. }
  5052. if (CanClose)
  5053. {
  5054. CanClose = FEditorManager->Empty(true);
  5055. if (!CanClose)
  5056. {
  5057. SetFocus();
  5058. CanClose =
  5059. (MessageDialog(
  5060. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  5061. }
  5062. }
  5063. }
  5064. }
  5065. }
  5066. //---------------------------------------------------------------------------
  5067. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  5068. {
  5069. TForm * Form = dynamic_cast<TForm *>(Sender);
  5070. DebugAssert(Form != NULL);
  5071. Form->Close();
  5072. }
  5073. //---------------------------------------------------------------------------
  5074. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  5075. {
  5076. TForm * Form = dynamic_cast<TForm *>(Sender);
  5077. delete Form;
  5078. }
  5079. //---------------------------------------------------------------------------
  5080. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  5081. {
  5082. while (FLocalEditors->Count > 0)
  5083. {
  5084. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  5085. }
  5086. }
  5087. //---------------------------------------------------------------------------
  5088. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  5089. TObject *Sender)
  5090. {
  5091. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  5092. try
  5093. {
  5094. SetProperties(osRemote, FileList);
  5095. }
  5096. __finally
  5097. {
  5098. delete FileList;
  5099. }
  5100. }
  5101. //---------------------------------------------------------------------------
  5102. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  5103. {
  5104. if (value)
  5105. {
  5106. if (Component == fcCustomCommandsBand)
  5107. {
  5108. UpdateCustomCommandsToolbar();
  5109. }
  5110. if (Component == fcQueueFileList)
  5111. {
  5112. DebugAssert(!QueueFileListSplitter->Visible);
  5113. AdjustQueueLayout();
  5114. UpdateQueueFileList();
  5115. }
  5116. }
  5117. }
  5118. //---------------------------------------------------------------------------
  5119. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  5120. {
  5121. int MinHeightLimit =
  5122. GetStaticQueuePanelComponentsHeight() +
  5123. QueueFileListSplitter->Height +
  5124. QueueFileList->Height +
  5125. GetMinQueueViewHeight();
  5126. if (QueuePanel->ClientHeight < MinHeightLimit)
  5127. {
  5128. QueuePanel->ClientHeight = MinHeightLimit;
  5129. }
  5130. }
  5131. //---------------------------------------------------------------------------
  5132. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  5133. {
  5134. TControl * Control = GetComponent(Component);
  5135. DebugAssert(Control);
  5136. bool Changed = (Control->Visible != value);
  5137. if (Changed)
  5138. {
  5139. ComponentShowing(Component, value);
  5140. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  5141. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  5142. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  5143. if (value)
  5144. {
  5145. int RemainingHeight = Control->Parent->ClientHeight;
  5146. int RemainingWidth = Control->Parent->ClientWidth;
  5147. for (int i = 0; i < Control->Parent->ControlCount; i++)
  5148. {
  5149. TControl * ChildControl = Control->Parent->Controls[i];
  5150. if (ChildControl->Visible)
  5151. {
  5152. switch (ChildControl->Align)
  5153. {
  5154. case alTop:
  5155. case alBottom:
  5156. RemainingHeight -= ChildControl->Height;
  5157. break;
  5158. case alLeft:
  5159. case alRight:
  5160. RemainingWidth -= ChildControl->Width;
  5161. break;
  5162. }
  5163. }
  5164. }
  5165. int Reserve = ScaleByTextHeight(this, 32);
  5166. // queue in explorer, trees in commander
  5167. if (Control->Height > RemainingHeight - Reserve)
  5168. {
  5169. Control->Height = RemainingHeight / 2;
  5170. }
  5171. if (Control->Width > RemainingWidth - Reserve)
  5172. {
  5173. Control->Width = RemainingWidth / 2;
  5174. }
  5175. }
  5176. Control->Visible = value;
  5177. if (WasFocused && Visible)
  5178. {
  5179. DirView(osCurrent)->SetFocus();
  5180. }
  5181. FixControlsPlacement();
  5182. }
  5183. }
  5184. //---------------------------------------------------------------------------
  5185. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  5186. {
  5187. TControl * Control = GetComponent(Component);
  5188. if (Control == NULL)
  5189. {
  5190. return false;
  5191. }
  5192. else
  5193. {
  5194. return Control->Visible;
  5195. }
  5196. }
  5197. //---------------------------------------------------------------------------
  5198. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  5199. {
  5200. bool Result;
  5201. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  5202. {
  5203. Result = !IsUWP();
  5204. }
  5205. else
  5206. {
  5207. Result = true;
  5208. }
  5209. return Result;
  5210. }
  5211. //---------------------------------------------------------------------------
  5212. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  5213. {
  5214. if (DirView->ItemFocused != NULL)
  5215. {
  5216. DirView->ItemFocused->MakeVisible(false);
  5217. }
  5218. }
  5219. //---------------------------------------------------------------------------
  5220. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5221. {
  5222. MakeFocusedItemVisible(RemoteDirView);
  5223. QueueSplitter->Visible = QueuePanel->Visible;
  5224. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5225. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5226. TControl * TopControlsOrder[] =
  5227. { TopDock, MessageDock, SessionsPageControl };
  5228. SetVerticalControlsOrder(TopControlsOrder, LENOF(TopControlsOrder));
  5229. TControl * QueueControlsOrder[] =
  5230. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5231. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5232. }
  5233. //---------------------------------------------------------------------------
  5234. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5235. {
  5236. switch (Component) {
  5237. case fcStatusBar: return RemoteStatusBar;
  5238. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5239. case fcQueueView: return QueuePanel;
  5240. case fcQueueToolbar: return QueueDock;
  5241. case fcRemoteTree: return RemoteDrivePanel;
  5242. case fcSessionsTabs: return SessionsPageControl;
  5243. case fcQueueFileList: return QueueFileList;
  5244. default: return NULL;
  5245. }
  5246. }
  5247. //---------------------------------------------------------------------------
  5248. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5249. TObject *Sender, TListColumn *Column, TPoint &Point)
  5250. {
  5251. DebugAssert(NonVisualDataModule && Column && Sender);
  5252. NonVisualDataModule->ListColumn = Column;
  5253. TPopupMenu * DirViewColumnMenu;
  5254. if (Sender == DirView(osOther))
  5255. {
  5256. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5257. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible = (Column->Index == uvName);
  5258. bool SizeColumn = (Column->Index == uvSize);
  5259. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible = SizeColumn;
  5260. NonVisualDataModule->RemoteCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5261. }
  5262. else
  5263. {
  5264. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5265. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible = (Column->Index == dvName);
  5266. bool SizeColumn = (Column->Index == dvSize);
  5267. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible = SizeColumn;
  5268. NonVisualDataModule->LocalCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5269. }
  5270. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5271. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5272. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5273. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5274. }
  5275. //---------------------------------------------------------------------------
  5276. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5277. TObject *Sender, TListItem *Item, bool &AllowExec)
  5278. {
  5279. DoDirViewExecFile(Sender, Item, AllowExec);
  5280. }
  5281. //---------------------------------------------------------------------------
  5282. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5283. TListItem * Item, bool & AllowExec)
  5284. {
  5285. DebugAssert(Sender && Item && Configuration);
  5286. DebugAssert(AllowExec);
  5287. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5288. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5289. bool Remote = !IsSideLocalBrowser(Side);
  5290. DebugAssert(Item == ADirView->ItemFocused);
  5291. bool IsDirectory = ADirView->ItemIsDirectory(Item);
  5292. TResolvedDoubleClickAction Action;
  5293. if (FForceExecution)
  5294. {
  5295. Action = IsDirectory ? rdcaChangeDir : rdcaOpen;
  5296. }
  5297. else
  5298. {
  5299. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  5300. Action = WinConfiguration->ResolveDoubleClickAction(IsDirectory, ATerminal);
  5301. }
  5302. if (Action == rdcaCopy)
  5303. {
  5304. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5305. UnlockWindow();
  5306. try
  5307. {
  5308. if (IsLocalBrowserMode())
  5309. {
  5310. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5311. }
  5312. else
  5313. {
  5314. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5315. }
  5316. }
  5317. __finally
  5318. {
  5319. LockWindow();
  5320. }
  5321. AllowExec = false;
  5322. }
  5323. else if (Action == rdcaEdit)
  5324. {
  5325. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5326. {
  5327. ExecuteFile(Side, efDefaultEditor);
  5328. AllowExec = false;
  5329. }
  5330. }
  5331. else if (Action == rdcaOpen)
  5332. {
  5333. if (DebugAlwaysTrue(!IsDirectory) && Remote && !WinConfiguration->DisableOpenEdit)
  5334. {
  5335. ExecuteFile(osRemote, efShell);
  5336. AllowExec = false;
  5337. }
  5338. }
  5339. }
  5340. //---------------------------------------------------------------------------
  5341. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5342. {
  5343. return ((Side == osRemote) || (Side == osCurrent));
  5344. }
  5345. //---------------------------------------------------------------------------
  5346. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5347. {
  5348. DebugFail();
  5349. }
  5350. //---------------------------------------------------------------------------
  5351. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5352. {
  5353. DebugFail();
  5354. }
  5355. //---------------------------------------------------------------------------
  5356. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5357. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5358. {
  5359. TSynchronizeParamType Params;
  5360. Params.LocalDirectory = LocalDirectory;
  5361. Params.RemoteDirectory = RemoteDirectory;
  5362. int UnusedParams =
  5363. (GUIConfiguration->SynchronizeParams &
  5364. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5365. TTerminal::spCaseSensitive));
  5366. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5367. Params.Options = GUIConfiguration->SynchronizeOptions;
  5368. bool SaveSettings = false;
  5369. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5370. &DoSynchronizeTooManyDirectories);
  5371. DebugAssert(FSynchronizeController == NULL);
  5372. FSynchronizeController = &Controller;
  5373. bool Result;
  5374. try
  5375. {
  5376. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5377. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5378. int Options =
  5379. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5380. DebugAssert(FOnFeedSynchronizeError == NULL);
  5381. DebugAssert(FOnSynchronizeAbort == NULL);
  5382. DebugAssert(FSynchronizeTerminal == NULL);
  5383. FSynchronizeTerminal = Terminal;
  5384. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5385. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5386. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5387. if (Result)
  5388. {
  5389. if (SaveSettings)
  5390. {
  5391. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5392. GUIConfiguration->SynchronizeOptions = Params.Options;
  5393. }
  5394. else
  5395. {
  5396. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5397. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5398. FLAGSET(Params.Options, soSynchronize))
  5399. {
  5400. GUIConfiguration->SynchronizeOptions =
  5401. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5402. soSynchronize;
  5403. }
  5404. }
  5405. LocalDirectory = Params.LocalDirectory;
  5406. RemoteDirectory = Params.RemoteDirectory;
  5407. }
  5408. }
  5409. __finally
  5410. {
  5411. FSynchronizeController = NULL;
  5412. DebugAssert(FOnFeedSynchronizeError == NULL);
  5413. FOnFeedSynchronizeError = NULL;
  5414. DebugAssert(FOnSynchronizeAbort == NULL);
  5415. FOnSynchronizeAbort = NULL;
  5416. FSynchronizeTerminal = NULL;
  5417. }
  5418. return Result;
  5419. }
  5420. //---------------------------------------------------------------------------
  5421. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5422. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5423. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5424. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5425. TSynchronizeOptions * Options, bool Full)
  5426. {
  5427. // Keep-up-to-date mode
  5428. if (Terminal->Status == ssOpened)
  5429. {
  5430. try
  5431. {
  5432. int PParams = Params.Params;
  5433. if (!Full)
  5434. {
  5435. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5436. if (FLAGSET(Params.Options, soRecurse))
  5437. {
  5438. PParams |= TTerminal::spSubDirs;
  5439. }
  5440. }
  5441. else
  5442. {
  5443. // if keepuptodate is non-recursive,
  5444. // full sync before has to be non-recursive as well
  5445. if (FLAGCLEAR(Params.Options, soRecurse))
  5446. {
  5447. PParams |= TTerminal::spNoRecurse;
  5448. }
  5449. }
  5450. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5451. PParams, Checklist, Options);
  5452. }
  5453. catch (Exception & E)
  5454. {
  5455. if (!Terminal->Active)
  5456. {
  5457. ShowExtendedExceptionEx(Terminal, &E);
  5458. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5459. if (!Terminal->Active)
  5460. {
  5461. throw;
  5462. }
  5463. }
  5464. else
  5465. {
  5466. if (FLAGSET(Params.Options, soContinueOnError) &&
  5467. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5468. {
  5469. // noop, already logged in MoreMessageDialog
  5470. }
  5471. else
  5472. {
  5473. // We get mostly EAbort here, so this is noop.
  5474. // Exception is an error when listing directories while looking for differences.
  5475. ShowExtendedExceptionEx(Terminal, &E);
  5476. throw;
  5477. }
  5478. }
  5479. }
  5480. }
  5481. }
  5482. //---------------------------------------------------------------------------
  5483. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5484. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5485. const UnicodeString ErrorStr)
  5486. {
  5487. if (!Directory.IsEmpty())
  5488. {
  5489. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5490. }
  5491. else
  5492. {
  5493. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5494. }
  5495. }
  5496. //---------------------------------------------------------------------------
  5497. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5498. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5499. {
  5500. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5501. {
  5502. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5503. }
  5504. else
  5505. {
  5506. TMessageParams Params(mpNeverAskAgainCheck);
  5507. unsigned int Result = MessageDialog(
  5508. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5509. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5510. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5511. {
  5512. MaxDirectories *= 2;
  5513. if (Result == qaNeverAskAgain)
  5514. {
  5515. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5516. }
  5517. }
  5518. else
  5519. {
  5520. Abort();
  5521. }
  5522. }
  5523. }
  5524. //---------------------------------------------------------------------------
  5525. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5526. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5527. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5528. TSynchronizeOptions * Options)
  5529. {
  5530. // Keep-up-to-date mode
  5531. DebugAssert(!FAutoOperation);
  5532. void * BatchStorage;
  5533. BatchStart(BatchStorage);
  5534. FAutoOperation = true;
  5535. bool AnyOperation = false;
  5536. TDateTime StartTime = Now();
  5537. TSynchronizeChecklist * AChecklist = NULL;
  5538. try
  5539. {
  5540. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5541. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5542. {
  5543. FSynchronizeProgressForm->Start();
  5544. }
  5545. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5546. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5547. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5548. Options);
  5549. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5550. SAFE_DESTROY(FSynchronizeProgressForm);
  5551. AnyOperation = (AChecklist->CheckedCount > 0);
  5552. if (AnyOperation)
  5553. {
  5554. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5555. CreateProgressForm(&SynchronizeProgress);
  5556. try
  5557. {
  5558. Terminal->SynchronizeApply(
  5559. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5560. SynchronizeProcessedItem, NULL, NULL, NULL);
  5561. }
  5562. __finally
  5563. {
  5564. DestroyProgressForm();
  5565. }
  5566. }
  5567. }
  5568. __finally
  5569. {
  5570. if (Checklist == NULL)
  5571. {
  5572. delete AChecklist;
  5573. }
  5574. else
  5575. {
  5576. *Checklist = AChecklist;
  5577. }
  5578. FAutoOperation = false;
  5579. SAFE_DESTROY(FSynchronizeProgressForm);
  5580. BatchEnd(BatchStorage);
  5581. ReloadLocalDirectory();
  5582. if (AnyOperation)
  5583. {
  5584. OperationComplete(StartTime);
  5585. }
  5586. }
  5587. }
  5588. //---------------------------------------------------------------------------
  5589. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5590. {
  5591. // can be called from command line
  5592. return Visible &&
  5593. ((DirView(osRemote)->SelCount > 0) ||
  5594. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5595. }
  5596. //---------------------------------------------------------------------------
  5597. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5598. {
  5599. LogSynchronizeEvent(Terminal, Message);
  5600. }
  5601. //---------------------------------------------------------------------------
  5602. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5603. int Params, TSynchronizeOptions & Options)
  5604. {
  5605. DebugAssert(!IsLocalBrowserMode());
  5606. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5607. {
  5608. Options.Filter = new TStringList();
  5609. Options.Filter->CaseSensitive = false;
  5610. Options.Filter->Duplicates = Types::dupAccept;
  5611. if (DirView(osRemote)->SelCount > 0)
  5612. {
  5613. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5614. }
  5615. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5616. {
  5617. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5618. }
  5619. Options.Filter->Sort();
  5620. }
  5621. }
  5622. //---------------------------------------------------------------------------
  5623. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5624. {
  5625. TCopyParamType Defaults;
  5626. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5627. ConfigStorage->AccessMode = smRead;
  5628. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5629. {
  5630. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5631. }
  5632. ConfigStorage.reset(NULL);
  5633. std::unique_ptr<TStringList> Options(new TStringList());
  5634. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5635. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5636. UnicodeString Result;
  5637. if (Options->Count > 0)
  5638. {
  5639. Result =
  5640. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5641. }
  5642. return Result;
  5643. }
  5644. //---------------------------------------------------------------------------
  5645. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5646. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5647. {
  5648. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5649. UnicodeString AdditionalParams =
  5650. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5651. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5652. SerializeCopyParamForCommandLine(CopyParams),
  5653. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5654. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5655. ExecuteNewInstance(SessionName, AdditionalParams);
  5656. }
  5657. //---------------------------------------------------------------------------
  5658. struct TSynchronizeParams
  5659. {
  5660. UnicodeString LocalDirectory;
  5661. UnicodeString RemoteDirectory;
  5662. TSynchronizeMode Mode;
  5663. TSynchronizeChecklist * Checklist;
  5664. int Params;
  5665. TCopyParamType * CopyParam;
  5666. TDateTime CollectElapsed;
  5667. TDateTime * StartTime;
  5668. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5669. };
  5670. //---------------------------------------------------------------------------
  5671. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5672. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5673. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5674. {
  5675. DebugAssert(!FAutoOperation);
  5676. void * BatchStorage;
  5677. BatchStart(BatchStorage);
  5678. FAutoOperation = true;
  5679. TFileOperationStatistics Statistics;
  5680. TDateTime Start = Now();
  5681. try
  5682. {
  5683. Params.OnProcessedItem = OnProcessedItem;
  5684. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5685. CreateProgressForm(&SynchronizeProgress);
  5686. Terminal->SynchronizeApply(
  5687. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5688. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5689. &Statistics);
  5690. }
  5691. __finally
  5692. {
  5693. Params.OnProcessedItem = NULL;
  5694. FAutoOperation = false;
  5695. DestroyProgressForm();
  5696. BatchEnd(BatchStorage);
  5697. ReloadLocalDirectory();
  5698. }
  5699. if (WinConfiguration->SynchronizeSummary)
  5700. {
  5701. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5702. // The statistics should be 0 anyway in this case
  5703. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5704. {
  5705. Message += L"\n";
  5706. if (Statistics.FilesUploaded > 0)
  5707. {
  5708. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5709. }
  5710. if (Statistics.FilesDownloaded > 0)
  5711. {
  5712. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5713. }
  5714. if (Statistics.FilesDeletedLocal > 0)
  5715. {
  5716. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5717. }
  5718. if (Statistics.FilesDeletedRemote > 0)
  5719. {
  5720. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5721. }
  5722. }
  5723. TDateTime Elapsed = (Now() - Start);
  5724. Message +=
  5725. L"\n" +
  5726. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5727. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5728. TMessageParams Params(mpNeverAskAgainCheck);
  5729. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5730. if (Result == qaNeverAskAgain)
  5731. {
  5732. WinConfiguration->SynchronizeSummary = false;
  5733. }
  5734. }
  5735. }
  5736. //---------------------------------------------------------------------------
  5737. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5738. {
  5739. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5740. {
  5741. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5742. }
  5743. // Not set in keep-up-to-date mode - Synchronize() method
  5744. if (Token != NULL)
  5745. {
  5746. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5747. if (Params.OnProcessedItem != NULL)
  5748. {
  5749. Params.OnProcessedItem(NULL, ChecklistItem);
  5750. }
  5751. }
  5752. }
  5753. //---------------------------------------------------------------------------
  5754. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5755. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5756. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5757. {
  5758. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5759. *Params.StartTime = Now();
  5760. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5761. }
  5762. //---------------------------------------------------------------------------
  5763. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5764. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5765. {
  5766. // terminal can be already closed (e.g. dropped connection)
  5767. if (Terminal != NULL)
  5768. {
  5769. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5770. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5771. }
  5772. }
  5773. //---------------------------------------------------------------------------
  5774. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5775. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5776. {
  5777. DebugAssert(!IsLocalBrowserMode());
  5778. TAutoBatch AutoBatch(this);
  5779. TAutoFlag AutoOperationFlag(FAutoOperation);
  5780. if (Side == osRemote)
  5781. {
  5782. std::unique_ptr<TStrings> FileList(new TStringList());
  5783. FileList->AddObject(FileName, RemoteFile);
  5784. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5785. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5786. RemoteDirView->SaveSelection();
  5787. RemoteDirView->SaveSelectedNames();
  5788. try
  5789. {
  5790. Terminal->MoveFiles(FileList.get(), Target, FileMask, false);
  5791. }
  5792. catch(...)
  5793. {
  5794. RemoteDirView->DiscardSavedSelection();
  5795. throw;
  5796. }
  5797. RemoteDirView->RestoreSelection();
  5798. }
  5799. else if (DebugAlwaysTrue(Side == osLocal))
  5800. {
  5801. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5802. {
  5803. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5804. }
  5805. UnicodeString Directory = ExtractFileDir(FileName);
  5806. ReloadLocalDirectory(Directory);
  5807. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5808. if (!SamePaths(Directory, NewDirectory))
  5809. {
  5810. ReloadLocalDirectory(NewDirectory);
  5811. }
  5812. }
  5813. }
  5814. //---------------------------------------------------------------------------
  5815. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5816. {
  5817. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5818. if (Side == osLocal)
  5819. {
  5820. if (Action == TSynchronizeChecklist::saDownloadNew)
  5821. {
  5822. OpenFolderInExplorer(LocalPath);
  5823. }
  5824. else
  5825. {
  5826. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5827. }
  5828. }
  5829. else if (DebugAlwaysTrue(Side == osRemote))
  5830. {
  5831. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5832. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5833. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5834. if (!LocalPath.IsEmpty())
  5835. {
  5836. SessionData->LocalDirectory = LocalPath;
  5837. }
  5838. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5839. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5840. }
  5841. }
  5842. //---------------------------------------------------------------------------
  5843. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5844. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5845. const TCopyParamType * CopyParams)
  5846. {
  5847. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5848. UnicodeString AdditionalParams =
  5849. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5850. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5851. SerializeCopyParamForCommandLine(CopyParams),
  5852. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5853. LocalDirectory, RemoteDirectory, Mode, Params));
  5854. ExecuteNewInstance(SessionName, AdditionalParams);
  5855. }
  5856. //---------------------------------------------------------------------------
  5857. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5858. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5859. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5860. {
  5861. int Result;
  5862. bool SaveSettings = false;
  5863. int Options =
  5864. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5865. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5866. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5867. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5868. bool Continue =
  5869. (UseDefaults == 0) ||
  5870. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5871. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5872. if (Continue)
  5873. {
  5874. Configuration->Usage->Inc(L"Synchronizations");
  5875. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  5876. UpdateCopyParamCounters(CopyParam);
  5877. TSynchronizeOptions SynchronizeOptions;
  5878. GetSynchronizeOptions(Params, SynchronizeOptions);
  5879. if (SaveSettings)
  5880. {
  5881. GUIConfiguration->SynchronizeParams = Params;
  5882. }
  5883. else
  5884. {
  5885. SaveMode = false;
  5886. }
  5887. TDateTime StartTime = Now();
  5888. TSynchronizeChecklist * Checklist = NULL;
  5889. try
  5890. {
  5891. DebugAssert(!FAutoOperation);
  5892. FAutoOperation = true;
  5893. try
  5894. {
  5895. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5896. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5897. int Files = -1;
  5898. if (DataList->Count >= 1)
  5899. {
  5900. Files = StrToIntDef(DataList->Strings[0], Files);
  5901. }
  5902. else
  5903. {
  5904. DataList->Add(UnicodeString());
  5905. }
  5906. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5907. FSynchronizeProgressForm->Start();
  5908. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5909. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5910. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5911. &SynchronizeOptions);
  5912. if (Terminal->SessionData->CacheDirectories)
  5913. {
  5914. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5915. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5916. }
  5917. }
  5918. __finally
  5919. {
  5920. FAutoOperation = false;
  5921. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5922. SAFE_DESTROY(FSynchronizeProgressForm);
  5923. }
  5924. TSynchronizeParams SynchronizeParams;
  5925. SynchronizeParams.LocalDirectory = LocalDirectory;
  5926. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5927. SynchronizeParams.Mode = Mode;
  5928. SynchronizeParams.CopyParam = &CopyParam;
  5929. SynchronizeParams.Params = Params;
  5930. SynchronizeParams.Checklist = Checklist;
  5931. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5932. SynchronizeParams.StartTime = &StartTime;
  5933. SynchronizeParams.OnProcessedItem = NULL;
  5934. Result = Checklist->Count;
  5935. if (Checklist->Count > 0)
  5936. {
  5937. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5938. {
  5939. if (!DoSynchronizeChecklistDialog(
  5940. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5941. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5942. {
  5943. Result = -1;
  5944. }
  5945. }
  5946. else
  5947. {
  5948. FullSynchronize(SynchronizeParams, NULL, NULL);
  5949. }
  5950. }
  5951. else
  5952. {
  5953. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5954. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5955. }
  5956. }
  5957. __finally
  5958. {
  5959. delete Checklist;
  5960. }
  5961. OperationComplete(StartTime);
  5962. }
  5963. else
  5964. {
  5965. Result = -1;
  5966. }
  5967. return Result;
  5968. }
  5969. //---------------------------------------------------------------------------
  5970. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5971. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5972. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5973. {
  5974. if (Collect)
  5975. {
  5976. DebugAssert(FSynchronizeProgressForm != NULL);
  5977. if (!FSynchronizeProgressForm->Started)
  5978. {
  5979. FSynchronizeProgressForm->Start();
  5980. }
  5981. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5982. SetTaskbarListProgressValue(CompareProgress);
  5983. }
  5984. else
  5985. {
  5986. DebugAssert(FSynchronizeProgressForm == NULL);
  5987. DebugAssert(FProgressForm != NULL);
  5988. }
  5989. }
  5990. //---------------------------------------------------------------------------
  5991. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5992. {
  5993. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  5994. TRemoteFile * File = NULL;
  5995. Terminal->ReadFile(FullFileName, File);
  5996. if (File != NULL)
  5997. {
  5998. std::unique_ptr<TRemoteFile> FileOwner(File);
  5999. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  6000. Application->ShowMainForm = false;
  6001. Application->Run();
  6002. }
  6003. }
  6004. //---------------------------------------------------------------------------
  6005. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  6006. {
  6007. DebugFail();
  6008. }
  6009. //---------------------------------------------------------------------------
  6010. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  6011. {
  6012. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  6013. SessionData->Assign(ManagedSession->SessionData);
  6014. UpdateSessionData(SessionData.get());
  6015. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  6016. if (IsActiveTerminal(ManagedSession))
  6017. {
  6018. ManagedSession->UpdateSessionCredentials(SessionData.get());
  6019. }
  6020. if (!ManagedSession->SessionData->HasSessionName())
  6021. {
  6022. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  6023. // once IsWorkspace is cleared
  6024. SessionData->Name = UnicodeString();
  6025. }
  6026. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  6027. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  6028. SessionData->IsWorkspace = false;
  6029. return SessionData.release();
  6030. }
  6031. //---------------------------------------------------------------------------
  6032. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  6033. {
  6034. TSessionData * SessionData = CloneCurrentSessionData();
  6035. try
  6036. {
  6037. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  6038. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  6039. }
  6040. __finally
  6041. {
  6042. delete SessionData;
  6043. }
  6044. }
  6045. //---------------------------------------------------------------------------
  6046. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  6047. {
  6048. TTerminalManager * Manager = TTerminalManager::Instance();
  6049. std::unique_ptr<TObjectList> DataList(new TObjectList());
  6050. if (DebugAlwaysTrue(ManagedSession != NULL))
  6051. {
  6052. // Update ManagedSession->StateData
  6053. UpdateSession(ManagedSession);
  6054. }
  6055. Manager->SaveWorkspace(DataList.get());
  6056. return DataList.release();
  6057. }
  6058. //---------------------------------------------------------------------------
  6059. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  6060. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  6061. {
  6062. WinConfiguration->LastWorkspace = Name;
  6063. if (!SavePasswords)
  6064. {
  6065. for (int Index = 0; Index < DataList->Count; Index++)
  6066. {
  6067. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6068. if (SessionData->Link.IsEmpty())
  6069. {
  6070. SessionData->ClearSessionPasswords();
  6071. }
  6072. }
  6073. }
  6074. StoredSessions->NewWorkspace(Name, DataList);
  6075. // modified only
  6076. StoredSessions->Save(false, Explicit);
  6077. }
  6078. //---------------------------------------------------------------------------
  6079. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  6080. {
  6081. return
  6082. DefaultStr(WinConfiguration->LastWorkspace,
  6083. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  6084. }
  6085. //---------------------------------------------------------------------------
  6086. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  6087. {
  6088. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  6089. bool AnyNonStoredSessionWithPassword = false;
  6090. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  6091. bool AllNonStoredSessionsAnonymous = true;
  6092. TTerminalManager * Manager = TTerminalManager::Instance();
  6093. for (int Index = 0; Index < DataList->Count; Index++)
  6094. {
  6095. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6096. if (SessionData->Link.IsEmpty())
  6097. {
  6098. if (SessionData->HasAnySessionPassword())
  6099. {
  6100. AnyNonStoredSessionWithPassword = true;
  6101. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  6102. {
  6103. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  6104. }
  6105. }
  6106. if (!SameText(SessionData->UserName, AnonymousUserName))
  6107. {
  6108. AllNonStoredSessionsAnonymous = false;
  6109. }
  6110. }
  6111. }
  6112. bool SavePasswords;
  6113. bool * PSavePasswords;
  6114. bool NotRecommendedSavingPasswords =
  6115. !CustomWinConfiguration->UseMasterPassword &&
  6116. !AllNonStoredSessionsAnonymous;
  6117. if (Configuration->DisablePasswordStoring ||
  6118. !AnyNonStoredSessionWithPassword)
  6119. {
  6120. PSavePasswords = NULL;
  6121. SavePasswords = false;
  6122. }
  6123. else
  6124. {
  6125. PSavePasswords = &SavePasswords;
  6126. SavePasswords =
  6127. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  6128. !NotRecommendedSavingPasswords;
  6129. }
  6130. UnicodeString Name = WorkspaceName();
  6131. bool CreateShortcut = false;
  6132. bool Result =
  6133. DoSaveWorkspaceDialog(
  6134. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  6135. EnableAutoSave);
  6136. if (Result)
  6137. {
  6138. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  6139. if (CreateShortcut)
  6140. {
  6141. TOperationVisualizer Visualizer;
  6142. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  6143. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  6144. }
  6145. if (EnableAutoSave)
  6146. {
  6147. WinConfiguration->AutoSaveWorkspace = true;
  6148. WinConfiguration->AutoWorkspace = Name;
  6149. if (PSavePasswords != NULL)
  6150. {
  6151. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  6152. }
  6153. }
  6154. }
  6155. return Result;
  6156. }
  6157. //---------------------------------------------------------------------------
  6158. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  6159. {
  6160. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  6161. DebugAssert(Session == ManagedSession);
  6162. if (!IsLocalBrowserMode())
  6163. {
  6164. SAFE_DESTROY(Session->RemoteExplorerState);
  6165. if (WinConfiguration->PreservePanelState)
  6166. {
  6167. Session->RemoteExplorerState = RemoteDirView->SaveState();
  6168. }
  6169. }
  6170. UpdateSessionData(Session->StateData);
  6171. }
  6172. //---------------------------------------------------------------------------
  6173. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  6174. {
  6175. // Keep in sync with TSessionData::CopyStateData
  6176. DebugAssert(Data != NULL);
  6177. // This is inconsistent with how color (for example) is handled.
  6178. Data->Name = ManagedSession->SessionData->Name;
  6179. if (!IsLocalBrowserMode())
  6180. {
  6181. // cannot use RemoteDirView->Path, because it is empty if connection
  6182. // was already closed
  6183. // also only peek, we may not be connected at all atm
  6184. // so make sure we do not try retrieving current directory from the server
  6185. // (particularly with FTP)
  6186. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  6187. if (!ACurrentDirectory.IsEmpty())
  6188. {
  6189. Data->RemoteDirectory = ACurrentDirectory;
  6190. }
  6191. }
  6192. Data->Color = SessionColor;
  6193. }
  6194. //---------------------------------------------------------------------------
  6195. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  6196. {
  6197. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  6198. DebugAssert(Toolbar != NULL);
  6199. for (int i = 0; i < Toolbar->Items->Count; i++)
  6200. {
  6201. TTBXCustomDropDownItem * DropDownItem;
  6202. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6203. if (DropDownItem != NULL)
  6204. {
  6205. ToolbarItemResize(DropDownItem,
  6206. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  6207. Toolbar->NonClientWidth);
  6208. break;
  6209. }
  6210. }
  6211. }
  6212. //---------------------------------------------------------------------------
  6213. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6214. {
  6215. Item->EditWidth = Width;
  6216. }
  6217. //---------------------------------------------------------------------------
  6218. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6219. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6220. {
  6221. for (int i = 0; i < Toolbar->Items->Count; i++)
  6222. {
  6223. TTBXCustomDropDownItem * DropDownItem;
  6224. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6225. if (DropDownItem != NULL)
  6226. {
  6227. ASize.x -= DropDownItem->EditWidth;
  6228. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  6229. }
  6230. }
  6231. }
  6232. //---------------------------------------------------------------------------
  6233. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6234. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6235. {
  6236. if (WinConfiguration->DDWarnLackOfTempSpace)
  6237. {
  6238. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6239. if (!ADrive.IsEmpty())
  6240. {
  6241. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6242. DebugAssert(RequiredSpace >= 0);
  6243. __int64 RequiredWithReserve;
  6244. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6245. if (FreeSpace < RequiredWithReserve)
  6246. {
  6247. unsigned int Result;
  6248. TMessageParams Params(mpNeverAskAgainCheck);
  6249. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6250. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6251. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6252. if (Result == qaNeverAskAgain)
  6253. {
  6254. WinConfiguration->DDWarnLackOfTempSpace = false;
  6255. }
  6256. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6257. }
  6258. }
  6259. }
  6260. }
  6261. //---------------------------------------------------------------------------
  6262. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6263. {
  6264. DoOpenDirectoryDialog(odAddBookmark, Side);
  6265. }
  6266. //---------------------------------------------------------------------------
  6267. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6268. {
  6269. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6270. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6271. try
  6272. {
  6273. TCustomDirView * DView = DirView(Side);
  6274. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6275. {
  6276. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6277. }
  6278. }
  6279. catch (...)
  6280. {
  6281. delete VisitedDirectories;
  6282. throw;
  6283. }
  6284. return VisitedDirectories;
  6285. }
  6286. //---------------------------------------------------------------------------
  6287. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6288. TOpenDirectoryMode Mode, TOperationSide Side)
  6289. {
  6290. bool Continue = true;
  6291. if (Mode == odAddBookmark)
  6292. {
  6293. TMessageParams Params(mpNeverAskAgainCheck);
  6294. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6295. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6296. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6297. unsigned int Answer =
  6298. MessageDialog(Message,
  6299. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6300. if (Answer == qaNeverAskAgain)
  6301. {
  6302. Continue = true;
  6303. WinConfiguration->UseSharedBookmarks = true;
  6304. }
  6305. else if (Answer == qaOK)
  6306. {
  6307. Continue = true;
  6308. WinConfiguration->UseSharedBookmarks = false;
  6309. }
  6310. else
  6311. {
  6312. Continue = false;
  6313. }
  6314. }
  6315. if (Continue)
  6316. {
  6317. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6318. try
  6319. {
  6320. UnicodeString Name = DirView(Side)->PathName;
  6321. bool Repeat;
  6322. do
  6323. {
  6324. Repeat = false;
  6325. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6326. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6327. // do not allow switching to location profiles,
  6328. // if we are not connected
  6329. HasDirView[osLocal] && (Terminal != NULL)))
  6330. {
  6331. TWindowLock Lock(this);
  6332. if (!TryOpenDirectory(Side, Name))
  6333. {
  6334. Repeat = true;
  6335. Mode = odBrowse;
  6336. }
  6337. }
  6338. }
  6339. while (Repeat);
  6340. }
  6341. __finally
  6342. {
  6343. delete VisitedDirectories;
  6344. }
  6345. }
  6346. }
  6347. //---------------------------------------------------------------------------
  6348. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6349. {
  6350. DebugAssert(!Terminal->CommandSessionOpened);
  6351. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6352. }
  6353. //---------------------------------------------------------------------------
  6354. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6355. {
  6356. bool Result = Terminal->IsCapable[Capability] ||
  6357. Terminal->CommandSessionOpened;
  6358. if (!Result)
  6359. {
  6360. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6361. if (!GUIConfiguration->ConfirmCommandSession)
  6362. {
  6363. Result = true;
  6364. }
  6365. else
  6366. {
  6367. TMessageParams Params(mpNeverAskAgainCheck);
  6368. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6369. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6370. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6371. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6372. if (Answer == qaNeverAskAgain)
  6373. {
  6374. GUIConfiguration->ConfirmCommandSession = false;
  6375. Result = true;
  6376. }
  6377. else if (Answer == qaOK)
  6378. {
  6379. Result = true;
  6380. }
  6381. }
  6382. if (Result)
  6383. {
  6384. Result = CommandSessionFallback();
  6385. }
  6386. }
  6387. return Result;
  6388. }
  6389. //---------------------------------------------------------------------------
  6390. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6391. {
  6392. if (EnsureCommandSessionFallback(fcAnyCommand))
  6393. {
  6394. DoConsoleDialog(Terminal, Command);
  6395. }
  6396. }
  6397. //---------------------------------------------------------------------------
  6398. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6399. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6400. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6401. {
  6402. if (!IsSideLocalBrowser(osRemote) &&
  6403. IsFileControl(DropSourceControl, osRemote) &&
  6404. (FDDExtMapFile != NULL))
  6405. {
  6406. Accept = true;
  6407. }
  6408. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6409. DebugAssert(FDDTargetControl != NULL);
  6410. }
  6411. //---------------------------------------------------------------------------
  6412. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6413. _di_IDataObject DataObj, int KeyState,
  6414. const TPoint & Point, int & Effect, bool & Accept)
  6415. {
  6416. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6417. }
  6418. //---------------------------------------------------------------------------
  6419. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6420. _di_IDataObject DataObj, int KeyState,
  6421. const TPoint & Point, int & Effect, bool & Accept)
  6422. {
  6423. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6424. }
  6425. //---------------------------------------------------------------------------
  6426. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6427. TObject * Sender)
  6428. {
  6429. DebugUsedParam(Sender);
  6430. DebugAssert(FDDTargetControl == Sender);
  6431. FDDTargetControl = NULL;
  6432. }
  6433. //---------------------------------------------------------------------------
  6434. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6435. {
  6436. FileControlDDDragLeave(SessionsPageControl);
  6437. }
  6438. //---------------------------------------------------------------------------
  6439. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6440. {
  6441. FileControlDDDragLeave(QueueView3);
  6442. }
  6443. //---------------------------------------------------------------------------
  6444. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6445. {
  6446. DebugAssert(!IsSideLocalBrowser(Side));
  6447. DebugUsedParam(Side);
  6448. bool Edit = false;
  6449. TRemoteFile * File = NULL;
  6450. UnicodeString FileName;
  6451. UnicodeString PointTo;
  6452. bool SymbolicLink = true;
  6453. if (RemoteDirView->ItemFocused)
  6454. {
  6455. DebugAssert(RemoteDirView->ItemFocused->Data);
  6456. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6457. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6458. if (Edit)
  6459. {
  6460. FileName = File->FileName;
  6461. PointTo = File->LinkTo;
  6462. }
  6463. else if (!File->IsParentDirectory)
  6464. {
  6465. PointTo = File->FileName;
  6466. }
  6467. }
  6468. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6469. Terminal->IsCapable[fcHardLink]))
  6470. {
  6471. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6472. if (Edit)
  6473. {
  6474. DebugAssert(File->FileName == FileName);
  6475. int Params = dfNoRecursive;
  6476. Terminal->ExceptionOnFail = true;
  6477. try
  6478. {
  6479. Terminal->DeleteFile(L"", File, &Params);
  6480. }
  6481. __finally
  6482. {
  6483. Terminal->ExceptionOnFail = false;
  6484. }
  6485. }
  6486. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6487. }
  6488. }
  6489. //---------------------------------------------------------------------------
  6490. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6491. {
  6492. return
  6493. (IsSideLocalBrowser(Side) ||
  6494. (HasActiveTerminal() &&
  6495. Terminal->ResolvingSymlinks &&
  6496. Terminal->IsCapable[fcSymbolicLink]));
  6497. }
  6498. //---------------------------------------------------------------------------
  6499. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6500. {
  6501. return
  6502. !IsSideLocalBrowser(FCurrentSide) &&
  6503. (RemoteDirView->ItemFocused != NULL) &&
  6504. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6505. Terminal->SessionData->ResolveSymlinks;
  6506. }
  6507. //---------------------------------------------------------------------------
  6508. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6509. {
  6510. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6511. FForceExecution = true;
  6512. try
  6513. {
  6514. DirView(osCurrent)->ExecuteCurrentFile();
  6515. }
  6516. __finally
  6517. {
  6518. FForceExecution = false;
  6519. }
  6520. }
  6521. //---------------------------------------------------------------------------
  6522. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6523. {
  6524. TEditorData ExternalEditor;
  6525. ExternalEditor.Editor = edExternal;
  6526. bool Remember = false;
  6527. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6528. {
  6529. if (Remember)
  6530. {
  6531. TEditorList * EditorList = new TEditorList;
  6532. try
  6533. {
  6534. *EditorList = *WinConfiguration->EditorList;
  6535. bool Found = false;
  6536. int i = 0;
  6537. while (!Found && (i < EditorList->Count))
  6538. {
  6539. const TEditorPreferences * Editor = EditorList->Editors[i];
  6540. if ((Editor->Data->Editor == edExternal) &&
  6541. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6542. {
  6543. Found = true;
  6544. }
  6545. i++;
  6546. }
  6547. if (!Found)
  6548. {
  6549. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6550. WinConfiguration->EditorList = EditorList;
  6551. }
  6552. }
  6553. __finally
  6554. {
  6555. delete EditorList;
  6556. }
  6557. }
  6558. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6559. }
  6560. }
  6561. //---------------------------------------------------------------------------
  6562. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6563. {
  6564. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6565. if (FFileFindTerminal == ATerminal)
  6566. {
  6567. FFileFindTerminal = NULL;
  6568. HideFileFindDialog();
  6569. }
  6570. if (FClipboardTerminal == ATerminal)
  6571. {
  6572. ClipboardClear(); // implies ClipboardStop
  6573. }
  6574. if ((FOnSynchronizeAbort != NULL) &&
  6575. (ATerminal == FManagedSession))
  6576. {
  6577. FOnSynchronizeAbort(NULL);
  6578. }
  6579. }
  6580. //---------------------------------------------------------------------------
  6581. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6582. {
  6583. DetachTerminal(Sender);
  6584. }
  6585. //---------------------------------------------------------------------------
  6586. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6587. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6588. {
  6589. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6590. DebugAssert(Terminal != NULL);
  6591. if (Data->Terminal == Terminal)
  6592. {
  6593. Data->Terminal = NULL;
  6594. }
  6595. }
  6596. //---------------------------------------------------------------------------
  6597. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6598. {
  6599. return NULL;
  6600. }
  6601. //---------------------------------------------------------------------------
  6602. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6603. {
  6604. UpdateControls();
  6605. SessionColor = TColor(0);
  6606. UpdateRemotePathComboBox(false);
  6607. NeedSession(false);
  6608. }
  6609. //---------------------------------------------------------------------------
  6610. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6611. {
  6612. try
  6613. {
  6614. // Cache, as the login dialog can change its value.
  6615. // See also DoShow. Also see TTerminalManager::NewSession.
  6616. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6617. try
  6618. {
  6619. if (ShowLogin)
  6620. {
  6621. bool ReloadSessions = !Startup;
  6622. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6623. }
  6624. }
  6625. __finally
  6626. {
  6627. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6628. // (so there was no chance for the user to open any session yet)
  6629. if ((ShowLogin || !Startup) &&
  6630. !WinConfiguration->KeepOpenWhenNoSession &&
  6631. // this rules out the implicit Commander's local-local browser
  6632. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6633. {
  6634. TerminateApplication();
  6635. }
  6636. }
  6637. }
  6638. catch (EAbort &)
  6639. {
  6640. // swallow
  6641. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6642. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6643. }
  6644. }
  6645. //---------------------------------------------------------------------------
  6646. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6647. {
  6648. TTerminalManager * Manager = TTerminalManager::Instance();
  6649. DebugAssert(!Manager->Updating);
  6650. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6651. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6652. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6653. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6654. try
  6655. {
  6656. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6657. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6658. {
  6659. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6660. }
  6661. for (int Index = 0; Index <= Manager->Count; Index++)
  6662. {
  6663. TThemeTabSheet * TabSheet;
  6664. if (Index >= SessionsPageControl->PageCount)
  6665. {
  6666. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6667. TabSheet->PageControl = SessionsPageControl;
  6668. }
  6669. else
  6670. {
  6671. TabSheet = SessionsPageControl->Pages[Index];
  6672. }
  6673. bool IsSessionTab = (Index < Manager->Count);
  6674. if (IsSessionTab)
  6675. {
  6676. TTerminal * Terminal = Manager->Sessions[Index];
  6677. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6678. TabSheet->CaptionTruncation = tttEllipsis;
  6679. UpdateSessionTab(TabSheet);
  6680. }
  6681. else
  6682. {
  6683. TabSheet->Tag = 0; // not really needed
  6684. TabSheet->Shadowed = false;
  6685. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6686. TabSheet->CaptionTruncation = tttNoText;
  6687. // 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)
  6688. UpdateNewTabTab();
  6689. }
  6690. }
  6691. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6692. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6693. {
  6694. SessionsPageControl->UpdateTabsCaptionTruncation();
  6695. }
  6696. }
  6697. __finally
  6698. {
  6699. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6700. }
  6701. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6702. }
  6703. //---------------------------------------------------------------------------
  6704. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6705. {
  6706. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6707. }
  6708. //---------------------------------------------------------------------------
  6709. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6710. {
  6711. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6712. UnicodeString TabCaption;
  6713. if (WinConfiguration->SelectiveToolbarText)
  6714. {
  6715. TabCaption = GetNewTabTabCaption();
  6716. }
  6717. TabSheet->BaseCaption = TabCaption;
  6718. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6719. }
  6720. //---------------------------------------------------------------------------
  6721. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6722. {
  6723. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6724. }
  6725. //---------------------------------------------------------------------------
  6726. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6727. {
  6728. DebugFail();
  6729. return UnicodeString();
  6730. }
  6731. //---------------------------------------------------------------------------
  6732. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6733. {
  6734. if (DebugAlwaysTrue(TabSheet != NULL))
  6735. {
  6736. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6737. if (DebugAlwaysTrue(ASession != NULL))
  6738. {
  6739. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6740. if (ASession->LocalBrowser)
  6741. {
  6742. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6743. }
  6744. else
  6745. {
  6746. TabSheet->ImageIndex = AddSessionColor(Color);
  6747. }
  6748. TTerminalManager * Manager = TTerminalManager::Instance();
  6749. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6750. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6751. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6752. }
  6753. }
  6754. }
  6755. //---------------------------------------------------------------------------
  6756. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6757. {
  6758. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6759. }
  6760. //---------------------------------------------------------------------------
  6761. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6762. {
  6763. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6764. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6765. bool Result = (Session != NULL);
  6766. if (Result)
  6767. {
  6768. TTerminalManager::Instance()->ActiveSession = Session;
  6769. }
  6770. else
  6771. {
  6772. try
  6773. {
  6774. NewTab();
  6775. }
  6776. __finally
  6777. {
  6778. SessionListChanged();
  6779. }
  6780. FSessionsPageControlNewTabTime = Now();
  6781. }
  6782. return Result;
  6783. }
  6784. //---------------------------------------------------------------------------
  6785. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6786. {
  6787. TWindowLock Lock(this);
  6788. SessionTabSwitched();
  6789. }
  6790. //---------------------------------------------------------------------------
  6791. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6792. {
  6793. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6794. DebugAssert(TransferList != NULL);
  6795. UnicodeString Name;
  6796. if (TransferList->ItemIndex <= 0)
  6797. {
  6798. Name = L"";
  6799. }
  6800. else
  6801. {
  6802. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6803. }
  6804. if (FCopyParamAutoSelected.IsEmpty())
  6805. {
  6806. // if previous preset was not autoselected, make new preset the "default"
  6807. FCopyParamDefault = Name;
  6808. }
  6809. GUIConfiguration->CopyParamCurrent = Name;
  6810. }
  6811. //---------------------------------------------------------------------------
  6812. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6813. {
  6814. // sanity check
  6815. bool ExistingPreset =
  6816. (FTransferListHoverIndex >= 0) &&
  6817. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6818. DebugAssert(ExistingPreset);
  6819. if (ExistingPreset)
  6820. {
  6821. HDC DC = GetDC(0);
  6822. TCanvas * Canvas = new TCanvas();
  6823. try
  6824. {
  6825. Canvas->Handle = DC;
  6826. Canvas->Font = GetToolbarFont(this);
  6827. UnicodeString Name;
  6828. if (FTransferListHoverIndex == 0)
  6829. {
  6830. Name = L"";
  6831. }
  6832. else
  6833. {
  6834. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6835. }
  6836. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6837. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6838. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6839. static_cast<TObject*>(GetComponent(fcTransferList)));
  6840. UnicodeString InfoStr =
  6841. GUIConfiguration->CopyParamPreset[Name].
  6842. GetInfoStr(L"; ",
  6843. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6844. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6845. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6846. {
  6847. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6848. {
  6849. InfoStr.SetLength(InfoStr.Length() - 1);
  6850. }
  6851. InfoStr += Ellipsis;
  6852. }
  6853. // UpdateCaption does not cause invalidation of whole submenu, while
  6854. // setting Caption property does.
  6855. // also it probably does not resize the label, even if necessary
  6856. // (we do not want that anyway)
  6857. TransferLabel->UpdateCaption(InfoStr);
  6858. }
  6859. __finally
  6860. {
  6861. Canvas->Handle = NULL;
  6862. ReleaseDC(0, DC);
  6863. delete Canvas;
  6864. }
  6865. }
  6866. }
  6867. //---------------------------------------------------------------------------
  6868. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6869. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6870. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6871. {
  6872. if (FTransferListHoverIndex != AHoverIndex)
  6873. {
  6874. FTransferListHoverIndex = AHoverIndex;
  6875. UpdateTransferLabel();
  6876. }
  6877. }
  6878. //---------------------------------------------------------------------------
  6879. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6880. {
  6881. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6882. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6883. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6884. {
  6885. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6886. {
  6887. if (!NonVisualDataModule->Busy)
  6888. {
  6889. OpenDirectory(GetSide(osCurrent));
  6890. }
  6891. Message.Result = 1;
  6892. }
  6893. else
  6894. {
  6895. TForm::Dispatch(&Message);
  6896. }
  6897. }
  6898. else
  6899. {
  6900. TForm::Dispatch(&Message);
  6901. }
  6902. }
  6903. //---------------------------------------------------------------------------
  6904. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6905. {
  6906. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6907. if ((FIgnoreNextDialogChar != 0) &&
  6908. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6909. {
  6910. Message.Result = 1;
  6911. }
  6912. else
  6913. {
  6914. TForm::Dispatch(&Message);
  6915. }
  6916. FIgnoreNextDialogChar = 0;
  6917. }
  6918. //---------------------------------------------------------------------------
  6919. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6920. {
  6921. // The four low-order bits are used internally by Windows
  6922. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6923. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6924. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6925. // 61730 - restore thru double click - undocumented
  6926. // 61490 - maximize thru double click - undocumented
  6927. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6928. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6929. {
  6930. SysResizing(Cmd);
  6931. }
  6932. TForm::Dispatch(&Message);
  6933. }
  6934. //---------------------------------------------------------------------------
  6935. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6936. {
  6937. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6938. // Also there no point returning TRUE as we are not able to
  6939. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6940. // Hence the process termination might be safer :)
  6941. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6942. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6943. {
  6944. Message.Result = FALSE;
  6945. }
  6946. else
  6947. {
  6948. Message.Result = TRUE;
  6949. }
  6950. // Do not call default handling as that triggers OnCloseQuery,
  6951. // where our implementation will popup confirmation dialogs, what we do not want,
  6952. // as per Vista guidelines:
  6953. // https://learn.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6954. }
  6955. //---------------------------------------------------------------------------
  6956. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6957. {
  6958. if (Message.EndSession && IsApplicationMinimized())
  6959. {
  6960. // WORKAROUND
  6961. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6962. // when it receives WM_ENDSESSION,
  6963. // but that sometimes (particularly when application is minimized) cause crashes.
  6964. // Crash popups message that blocks log off.
  6965. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6966. // so we call ExitProcess() immediately, not even trying to cleanup.
  6967. // It still causes beep, so there's likely some popup, but it does not block
  6968. // log off.
  6969. ExitProcess(0);
  6970. }
  6971. TForm::Dispatch(&Message);
  6972. }
  6973. #ifdef _DEBUG
  6974. //---------------------------------------------------------------------------
  6975. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6976. {
  6977. TForm::Dispatch(&Message);
  6978. }
  6979. #endif
  6980. //---------------------------------------------------------------------------
  6981. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6982. {
  6983. }
  6984. //---------------------------------------------------------------------------
  6985. void __fastcall TCustomScpExplorerForm::DoShow()
  6986. {
  6987. // Called from TCustomScpExplorerForm::CMShowingChanged
  6988. // only now are the controls resized finally, so the size constraints
  6989. // will not conflict with possibly very small window size
  6990. RestoreFormParams();
  6991. // This indicates that the form was rescaled after parameters were loaded and
  6992. // as most probably the parameters were actually saved with the current form scaling
  6993. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6994. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6995. // and no rounding problems will occur.
  6996. // See also RestoreForm.
  6997. // (we should be safe to call RestoreParams unconditionally)
  6998. if (PixelsPerInch != Screen->PixelsPerInch)
  6999. {
  7000. RestoreParams();
  7001. }
  7002. FixControlsPlacement();
  7003. if (Position == poDefaultPosOnly)
  7004. {
  7005. CutFormToDesktop(this);
  7006. }
  7007. TForm::DoShow();
  7008. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  7009. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  7010. // This was previously in NeedSession, where other related code is.
  7011. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  7012. // unpleasantly shows briefly.
  7013. if ((ManagedSession == NULL) &&
  7014. !WinConfiguration->ShowLoginWhenNoSession &&
  7015. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  7016. // This detects if workspace was saved the last time the main widow was closed
  7017. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  7018. {
  7019. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  7020. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  7021. }
  7022. // Before main window displays
  7023. if (ManagedSession == NULL)
  7024. {
  7025. StartingWithoutSession();
  7026. }
  7027. UpdatePixelsPerInchMainWindowCounter();
  7028. FShowing = true;
  7029. }
  7030. //---------------------------------------------------------------------------
  7031. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  7032. {
  7033. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  7034. }
  7035. //---------------------------------------------------------------------------
  7036. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  7037. {
  7038. SessionListChanged();
  7039. InitStatusBar();
  7040. UpdateControls();
  7041. }
  7042. //---------------------------------------------------------------------------
  7043. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  7044. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  7045. TNotifyEvent OnBalloonClick, TObject * UserData)
  7046. {
  7047. bool Do;
  7048. UnicodeString Message;
  7049. if (E == NULL)
  7050. {
  7051. Message = Str;
  7052. Do = true;
  7053. }
  7054. else
  7055. {
  7056. Do = ExceptionMessage(E, Message);
  7057. }
  7058. if (Do && WinConfiguration->BalloonNotifications)
  7059. {
  7060. UnicodeString Title;
  7061. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  7062. {
  7063. Message = TrimLeft(Message);
  7064. }
  7065. else
  7066. {
  7067. Message = UnformatMessage(Message);
  7068. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  7069. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  7070. Title = LoadResourceString(Captions[Type]);
  7071. if (Terminal != NULL)
  7072. {
  7073. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7074. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  7075. }
  7076. }
  7077. if (Seconds == 0)
  7078. {
  7079. Seconds = WinConfiguration->NotificationsTimeout;
  7080. }
  7081. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  7082. }
  7083. }
  7084. //---------------------------------------------------------------------------
  7085. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  7086. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  7087. UnicodeString HelpKeyword, const TMessageParams * Params,
  7088. TTerminal * Terminal)
  7089. {
  7090. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  7091. (FOnFeedSynchronizeError != NULL)) &&
  7092. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  7093. {
  7094. if (FOnFeedSynchronizeError != NULL)
  7095. {
  7096. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  7097. }
  7098. else
  7099. {
  7100. DebugAssert(FErrorList != NULL);
  7101. TStringList * MoreMessagesCopy = NULL;
  7102. if (MoreMessages)
  7103. {
  7104. MoreMessagesCopy = new TStringList();
  7105. MoreMessagesCopy->Assign(MoreMessages);
  7106. }
  7107. FErrorList->AddObject(Message, MoreMessagesCopy);
  7108. }
  7109. PopupTrayBalloon(Terminal, Message, Type);
  7110. return ContinueAnswer(Answers);
  7111. }
  7112. else
  7113. {
  7114. bool UseBalloon = FTrayIcon->Visible;
  7115. if (UseBalloon)
  7116. {
  7117. PopupTrayBalloon(Terminal, Message, Type);
  7118. }
  7119. unsigned int Result;
  7120. try
  7121. {
  7122. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  7123. }
  7124. __finally
  7125. {
  7126. // cancel only balloon we popped up, otherwise we may cancel notification
  7127. // balloon that was there before the message dialog
  7128. // (such as "dd confirmation opt in balloon)
  7129. if (UseBalloon)
  7130. {
  7131. FTrayIcon->CancelBalloon();
  7132. }
  7133. }
  7134. return Result;
  7135. }
  7136. }
  7137. //---------------------------------------------------------------------------
  7138. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  7139. TTerminal * Terminal, Exception * E)
  7140. {
  7141. if (FTrayIcon->Visible)
  7142. {
  7143. PopupTrayBalloon(Terminal, L"", qtError, E);
  7144. }
  7145. // particularly prevent opening new session from jump list,
  7146. // while exception is shown
  7147. NonVisualDataModule->StartBusy();
  7148. try
  7149. {
  7150. ShowExtendedExceptionEx(Terminal, E);
  7151. }
  7152. __finally
  7153. {
  7154. NonVisualDataModule->EndBusy();
  7155. FTrayIcon->CancelBalloon();
  7156. }
  7157. }
  7158. //---------------------------------------------------------------------------
  7159. void __fastcall TCustomScpExplorerForm::SessionReady()
  7160. {
  7161. InitStatusBar();
  7162. // cannot rely on active page being page for active terminal,
  7163. // as it can happen that active page is the "new session" page
  7164. // (e.g. when reconnecting active terminal, while login dialog
  7165. // invoked from "new session" page is modal)
  7166. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  7167. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  7168. }
  7169. //---------------------------------------------------------------------------
  7170. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  7171. TTerminal * Terminal, Exception * E)
  7172. {
  7173. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  7174. if (!Terminal->Active)
  7175. {
  7176. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  7177. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  7178. {
  7179. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  7180. }
  7181. }
  7182. }
  7183. //---------------------------------------------------------------------------
  7184. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  7185. UnicodeString Message, TQueryType Type,
  7186. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  7187. {
  7188. if ((E == NULL) ||
  7189. ExceptionMessage(E, Message))
  7190. {
  7191. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  7192. if (Important)
  7193. {
  7194. Seconds *= 5;
  7195. }
  7196. UnicodeString NoteMessage(UnformatMessage(Message));
  7197. if (Terminal != NULL)
  7198. {
  7199. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7200. NoteMessage = FORMAT(L"%s: %s",
  7201. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  7202. }
  7203. if (WinConfiguration->BalloonNotifications)
  7204. {
  7205. AddNote(NoteMessage);
  7206. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  7207. }
  7208. else
  7209. {
  7210. FlashOnBackground();
  7211. PostNote(NoteMessage, Seconds, OnClick, UserData);
  7212. }
  7213. }
  7214. }
  7215. //---------------------------------------------------------------------------
  7216. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7217. {
  7218. RestoreApp();
  7219. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7220. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7221. !NonVisualDataModule->Busy)
  7222. {
  7223. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7224. TTerminalManager::Instance()->ActiveSession = Terminal;
  7225. if (!ComponentVisible[fcQueueView])
  7226. {
  7227. ToggleQueueVisibility();
  7228. GoToQueue();
  7229. }
  7230. }
  7231. }
  7232. //---------------------------------------------------------------------------
  7233. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7234. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7235. {
  7236. UnicodeString Message;
  7237. TNotifyEvent OnClick = NULL;
  7238. TObject * UserData = NULL;
  7239. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7240. switch (Event)
  7241. {
  7242. case qeEmptyButMonitored:
  7243. if ((ATerminal != Terminal) || QueueInvisible)
  7244. {
  7245. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7246. OnClick = QueueEmptyNoteClicked;
  7247. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7248. TerminalNoteData->Terminal = ATerminal;
  7249. UserData = TerminalNoteData;
  7250. }
  7251. break;
  7252. case qeEmpty:
  7253. OperationComplete(ATerminal->QueueOperationStart);
  7254. break;
  7255. case qePendingUserAction:
  7256. if ((ATerminal != Terminal) ||
  7257. (QueueInvisible && !IsQueueAutoPopup()))
  7258. {
  7259. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7260. }
  7261. break;
  7262. default:
  7263. DebugFail();
  7264. }
  7265. if (!Message.IsEmpty())
  7266. {
  7267. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7268. }
  7269. }
  7270. //---------------------------------------------------------------------------
  7271. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7272. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7273. {
  7274. if (FDDExtMapFile != NULL)
  7275. {
  7276. CloseHandle(FDDExtMapFile);
  7277. FDDExtMapFile = NULL;
  7278. }
  7279. if (WinConfiguration->DDFakeFile)
  7280. {
  7281. DDFakeFileInitDrag(FileList, Created);
  7282. }
  7283. }
  7284. //---------------------------------------------------------------------------
  7285. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7286. {
  7287. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7288. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7289. {
  7290. FFakeFileDropTarget = FileName;
  7291. }
  7292. }
  7293. //---------------------------------------------------------------------------
  7294. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7295. {
  7296. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7297. if (!ForceDirectories(ApiPath(Result)))
  7298. {
  7299. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7300. }
  7301. FileSetAttr(ApiPath(Result), faHidden);
  7302. return Result;
  7303. }
  7304. //---------------------------------------------------------------------------
  7305. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7306. bool & Created)
  7307. {
  7308. FFakeFileDropTarget = UnicodeString();
  7309. FDragFakeDirectory = CreateFakeTransferDirectory();
  7310. FileList->AddItem(NULL, FDragFakeDirectory);
  7311. Created = true;
  7312. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7313. {
  7314. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7315. }
  7316. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7317. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7318. {
  7319. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7320. TDragExtCommStruct* CommStruct;
  7321. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7322. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7323. DebugAssert(CommStruct != NULL);
  7324. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7325. CommStruct->Dragging = true;
  7326. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7327. LENOF(CommStruct->DropDest));
  7328. NULL_TERMINATE(CommStruct->DropDest);
  7329. UnmapViewOfFile(CommStruct);
  7330. }
  7331. }
  7332. //---------------------------------------------------------------------------
  7333. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7334. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7335. {
  7336. bool Result;
  7337. DebugAssert(!IsLocalBrowserMode());
  7338. if (Sender == RemoteDirView)
  7339. {
  7340. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7341. }
  7342. else
  7343. {
  7344. DebugAssert(Sender == RemoteDriveView);
  7345. TStrings * FileList = RemoteDriveView->DragFileList();
  7346. try
  7347. {
  7348. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7349. }
  7350. __finally
  7351. {
  7352. delete FileList;
  7353. }
  7354. }
  7355. if (FDDTargetControl == RemoteDriveView)
  7356. {
  7357. RemoteDriveView->UpdateDropTarget();
  7358. }
  7359. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7360. // or from tree view to dir view.
  7361. // foMove happens when file/dir is dragged from remote tree view outside of
  7362. // application via dragex
  7363. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7364. (DropSourceControl == RemoteDriveView))
  7365. {
  7366. RemoteDriveView->UpdateDropSource();
  7367. }
  7368. return Result;
  7369. }
  7370. //---------------------------------------------------------------------------
  7371. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7372. {
  7373. DebugAssert(!IsLocalBrowserMode());
  7374. // This also handles drops of remote files to queue.
  7375. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7376. SAFE_DESTROY(FDDFileList);
  7377. if (!FFakeFileDropTarget.IsEmpty())
  7378. {
  7379. RemoveDir(ApiPath(FFakeFileDropTarget));
  7380. }
  7381. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7382. {
  7383. try
  7384. {
  7385. TDragResult DDResult = (Sender == RemoteDirView) ?
  7386. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7387. // Focus is moved to the target application,
  7388. // but as we are going to present the UI, we need to steal the focus back.
  7389. // This most likely won't work though (windows does not allow application
  7390. // to steal focus most of the time)
  7391. Application->BringToFront();
  7392. // On older version of Windows we never got drMove here, see also comment below.
  7393. // On Windows 10, we get the "move".
  7394. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7395. {
  7396. UnicodeString TargetDirectory;
  7397. TFileOperation Operation;
  7398. // drInvalid may mean drMove, see comment below
  7399. switch (DDResult)
  7400. {
  7401. case drCopy:
  7402. Operation = foCopy;
  7403. break;
  7404. case drMove:
  7405. Operation = foMove;
  7406. break;
  7407. default:
  7408. DebugFail();
  7409. case drInvalid:
  7410. // prefer "copy" for safety
  7411. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7412. break;
  7413. }
  7414. TTransferOperationParam Param;
  7415. UnicodeString CounterName;
  7416. bool ForceQueue;
  7417. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7418. {
  7419. // we get drInvalid both if d&d was intercepted by ddext,
  7420. // and when users drops on no-drop location.
  7421. // we tell the difference by existence of response from ddext,
  7422. // so we ignore absence of response in this case
  7423. if (DDResult != drInvalid)
  7424. {
  7425. // here we know that the extension is installed,
  7426. // as it is checked as soon as drag&drop starts from
  7427. // RemoteFileControlDDCreateDragFileList
  7428. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7429. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7430. {
  7431. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7432. }
  7433. else
  7434. {
  7435. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7436. }
  7437. }
  7438. }
  7439. else
  7440. {
  7441. // download using fake file
  7442. Param.Temp = false;
  7443. Param.DragDrop = true;
  7444. if (ForceQueue)
  7445. {
  7446. Param.Queue = asOn;
  7447. }
  7448. if (Sender == RemoteDirView)
  7449. {
  7450. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7451. }
  7452. if (RemoteFileControlFileOperation(Sender, Operation,
  7453. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7454. {
  7455. Configuration->Usage->Inc(CounterName);
  7456. }
  7457. }
  7458. }
  7459. }
  7460. __finally
  7461. {
  7462. CloseHandle(FDDExtMapFile);
  7463. FDDExtMapFile = NULL;
  7464. delete FDragFakeMonitors;
  7465. FDragFakeMonitors = NULL;
  7466. RemoveDir(ApiPath(FDragFakeDirectory));
  7467. FDragFakeDirectory = L"";
  7468. FFakeFileDropTarget = UnicodeString();
  7469. }
  7470. }
  7471. if (FDragDropSshTerminate.get() != NULL)
  7472. {
  7473. FDragDropSshTerminate->Rethrow();
  7474. }
  7475. }
  7476. //---------------------------------------------------------------------------
  7477. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7478. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7479. {
  7480. // Remember drop effect so we know (when user drops files), if we copy or move
  7481. FLastDropEffect = dwEffect;
  7482. }
  7483. //---------------------------------------------------------------------------
  7484. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7485. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7486. {
  7487. bool Result;
  7488. if (FDDTargetControl == QueueView3)
  7489. {
  7490. Directory = DefaultDownloadTargetDirectory();
  7491. Result = true;
  7492. CounterName = L"DownloadsDragDropQueue";
  7493. ForceQueue = true;
  7494. }
  7495. else if (!FFakeFileDropTarget.IsEmpty())
  7496. {
  7497. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7498. Result = true;
  7499. ForceQueue = false;
  7500. CounterName = L"DownloadsDragDropFakeFile";
  7501. }
  7502. else
  7503. {
  7504. ForceQueue = false;
  7505. Enabled = false;
  7506. try
  7507. {
  7508. int Timer = 0;
  7509. Result = false;
  7510. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7511. {
  7512. {
  7513. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7514. TDragExtCommStruct* CommStruct;
  7515. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7516. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7517. DebugAssert(CommStruct != NULL);
  7518. Result = !CommStruct->Dragging;
  7519. if (Result)
  7520. {
  7521. Directory = ExtractFilePath(CommStruct->DropDest);
  7522. CounterName = L"DownloadsDragDropExternalExt";
  7523. }
  7524. UnmapViewOfFile(CommStruct);
  7525. }
  7526. if (!Result)
  7527. {
  7528. Sleep(50);
  7529. Timer += 50;
  7530. Application->ProcessMessages();
  7531. }
  7532. }
  7533. }
  7534. __finally
  7535. {
  7536. Enabled = true;
  7537. }
  7538. }
  7539. return Result;
  7540. }
  7541. //---------------------------------------------------------------------------
  7542. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7543. const UnicodeString TempDir, int SecDelay)
  7544. {
  7545. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7546. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7547. if (FDelayedDeletionTimer == NULL)
  7548. {
  7549. DebugAssert(HandleAllocated());
  7550. FDelayedDeletionTimer = new TTimer(this);
  7551. FDelayedDeletionTimer->Interval = 10000;
  7552. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7553. }
  7554. else
  7555. {
  7556. FDelayedDeletionTimer->Enabled = true;
  7557. }
  7558. }
  7559. //---------------------------------------------------------------------------
  7560. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7561. {
  7562. DebugAssert(FDelayedDeletionList != NULL);
  7563. TDateTime N = Now();
  7564. TDateTime Alert;
  7565. UnicodeString Directory;
  7566. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7567. {
  7568. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7569. if ((N >= Alert) || (Sender == NULL))
  7570. {
  7571. Directory = FDelayedDeletionList->Strings[Index];
  7572. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7573. {
  7574. FDelayedDeletionList->Delete(Index);
  7575. }
  7576. }
  7577. }
  7578. if (FDelayedDeletionList->Count == 0)
  7579. {
  7580. FDelayedDeletionTimer->Enabled = false;
  7581. }
  7582. }
  7583. //---------------------------------------------------------------------------
  7584. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7585. {
  7586. DebugAssert(!IsLocalBrowserMode());
  7587. if (IsFileControl(FDDTargetControl, osRemote) ||
  7588. (FDDTargetControl == SessionsPageControl))
  7589. {
  7590. TManagedTerminal * TargetTerminal = NULL;
  7591. TFileOperation Operation = foNone;
  7592. if (FDDTargetControl == SessionsPageControl)
  7593. {
  7594. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7595. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7596. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7597. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7598. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7599. {
  7600. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7601. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7602. {
  7603. Operation = foRemoteMove;
  7604. }
  7605. else
  7606. {
  7607. Operation = foRemoteCopy;
  7608. }
  7609. }
  7610. }
  7611. else
  7612. {
  7613. if (FLastDropEffect == DROPEFFECT_MOVE)
  7614. {
  7615. Operation = foRemoteMove;
  7616. }
  7617. else if (FLastDropEffect == DROPEFFECT_COPY)
  7618. {
  7619. Operation = foRemoteCopy;
  7620. }
  7621. }
  7622. if (Operation != foNone)
  7623. {
  7624. RemoteFileControlFileOperation(DropSourceControl,
  7625. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7626. }
  7627. // abort drag&drop
  7628. Abort();
  7629. }
  7630. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7631. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7632. (FDDTargetControl != QueueView3))
  7633. {
  7634. DebugAssert(!FDragTempDir.IsEmpty());
  7635. TTransferType Type;
  7636. UnicodeString TempDir = FDragTempDir;
  7637. // We clear FDragTempDir before calling
  7638. // just in case it fail (raises exception)
  7639. FDragTempDir = L"";
  7640. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7641. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7642. // empty directory parameter means temp directory -> don't display it!
  7643. UnicodeString TargetDir = L"";
  7644. int Options = 0;
  7645. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7646. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7647. {
  7648. Abort();
  7649. }
  7650. // TargetDir is set when dropped on local file control
  7651. // (this was workaround for legacy dirview event handling, now it should be
  7652. // made prettier)
  7653. if (TargetDir.IsEmpty())
  7654. {
  7655. TargetDir = TempDir;
  7656. if (ForceDirectories(ApiPath(TargetDir)))
  7657. {
  7658. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7659. FPendingTempSpaceWarn = true;
  7660. try
  7661. {
  7662. FDragDropOperation = true;
  7663. int Params = cpTemporary |
  7664. (Type == ttMove ? cpDelete : 0);
  7665. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7666. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7667. }
  7668. __finally
  7669. {
  7670. FDragDropOperation = false;
  7671. FPendingTempSpaceWarn = false;
  7672. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7673. }
  7674. }
  7675. else
  7676. {
  7677. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7678. }
  7679. }
  7680. }
  7681. }
  7682. //---------------------------------------------------------------------------
  7683. void __fastcall TCustomScpExplorerForm::DDDownload(
  7684. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  7685. {
  7686. DebugAssert(!IsLocalBrowserMode());
  7687. TAutoBatch AutoBatch(this);
  7688. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  7689. UpdateCopyParamCounters(*CopyParam);
  7690. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7691. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7692. {
  7693. RemoteDriveView->UpdateDropSource();
  7694. }
  7695. }
  7696. //---------------------------------------------------------------------------
  7697. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7698. {
  7699. public:
  7700. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7701. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7702. {
  7703. }
  7704. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7705. {
  7706. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7707. TDataObjectFilesEx::AllowData(FormatEtc);
  7708. }
  7709. };
  7710. //---------------------------------------------------------------------------
  7711. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7712. TObject * Sender, TDataObject *& DataObject)
  7713. {
  7714. if (FDDExtMapFile != NULL)
  7715. {
  7716. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7717. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7718. {
  7719. Abort();
  7720. }
  7721. if (FileList->Count > 0)
  7722. {
  7723. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7724. if (!FilesObject->IsValid(true, true))
  7725. {
  7726. FilesObject->_Release();
  7727. }
  7728. else
  7729. {
  7730. DataObject = FilesObject;
  7731. }
  7732. }
  7733. }
  7734. }
  7735. //---------------------------------------------------------------------------
  7736. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7737. {
  7738. DebugFail();
  7739. }
  7740. //---------------------------------------------------------------------------
  7741. void __fastcall TCustomScpExplorerForm::GoToTree()
  7742. {
  7743. ComponentVisible[fcRemoteTree] = true;
  7744. DriveView(osRemote)->SetFocus();
  7745. }
  7746. //---------------------------------------------------------------------------
  7747. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7748. TPanelExport Export)
  7749. {
  7750. TCustomDirView * DirView = this->DirView(Side);
  7751. std::unique_ptr<TStrings> ExportData(new TStringList());
  7752. switch (Export)
  7753. {
  7754. case pePath:
  7755. ExportData->Add(DirView->PathName);
  7756. break;
  7757. case peFileList:
  7758. case peFullFileList:
  7759. {
  7760. bool FullPath = (Export == peFullFileList);
  7761. DirView->CreateFileList(false, FullPath, ExportData.get());
  7762. for (int Index = 0; Index < ExportData->Count; Index++)
  7763. {
  7764. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7765. {
  7766. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7767. }
  7768. }
  7769. }
  7770. break;
  7771. default:
  7772. DebugFail();
  7773. }
  7774. return ExportData.release();
  7775. }
  7776. //---------------------------------------------------------------------------
  7777. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7778. TPanelExport Export, TPanelExportDestination Destination)
  7779. {
  7780. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7781. PanelExportStore(Side, Export, Destination, ExportData.get());
  7782. }
  7783. //---------------------------------------------------------------------------
  7784. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7785. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7786. TStrings * ExportData)
  7787. {
  7788. if (Destination == pedClipboard)
  7789. {
  7790. TInstantOperationVisualizer Visualizer;
  7791. CopyToClipboard(ExportData);
  7792. }
  7793. else
  7794. {
  7795. DebugFail();
  7796. }
  7797. }
  7798. //---------------------------------------------------------------------------
  7799. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7800. {
  7801. TCustomDirView * DirView = this->DirView(Side);
  7802. UnicodeString Mask = DirView->Mask;
  7803. if (DoFilterMaskDialog(DirView, Mask))
  7804. {
  7805. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7806. Configuration->Usage->Inc(L"Filters");
  7807. }
  7808. }
  7809. //---------------------------------------------------------------------------
  7810. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7811. {
  7812. return FQueueController->DefaultOperation();
  7813. }
  7814. //---------------------------------------------------------------------------
  7815. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7816. TQueueOperation Operation, void ** Param)
  7817. {
  7818. switch (Operation)
  7819. {
  7820. case qoPreferences:
  7821. return true;
  7822. case qoGoTo:
  7823. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7824. case qoOnceEmpty:
  7825. return IsAnythingQueued();
  7826. default:
  7827. return FQueueController->AllowOperation(Operation, Param);
  7828. }
  7829. }
  7830. //---------------------------------------------------------------------------
  7831. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7832. {
  7833. if (DebugAlwaysTrue(QueueView3->Visible))
  7834. {
  7835. QueueView3->SetFocus();
  7836. }
  7837. }
  7838. //---------------------------------------------------------------------------
  7839. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7840. TQueueOperation Operation, void * Param)
  7841. {
  7842. if (Operation == qoGoTo)
  7843. {
  7844. GoToQueue();
  7845. }
  7846. else if (Operation == qoPreferences)
  7847. {
  7848. PreferencesDialog(pmQueue);
  7849. }
  7850. else
  7851. {
  7852. FQueueController->ExecuteOperation(Operation, Param);
  7853. }
  7854. }
  7855. //---------------------------------------------------------------------------
  7856. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7857. {
  7858. return (Queue != NULL) && Queue->Enabled;
  7859. }
  7860. //---------------------------------------------------------------------------
  7861. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7862. {
  7863. DebugAssert(Queue != NULL);
  7864. if (Queue != NULL)
  7865. {
  7866. Queue->Enabled = !Queue->Enabled;
  7867. }
  7868. }
  7869. //---------------------------------------------------------------------------
  7870. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7871. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7872. {
  7873. FQueueActedItem = QueueView3->ItemFocused;
  7874. }
  7875. //---------------------------------------------------------------------------
  7876. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7877. {
  7878. return TopDock->Height + (MessageDock->Visible ? MessageDock->Height : 0) + QueueSplitter->Height;
  7879. }
  7880. //---------------------------------------------------------------------------
  7881. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7882. {
  7883. return
  7884. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7885. (QueueDock->Visible ? QueueDock->Height : 0) +
  7886. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7887. }
  7888. //---------------------------------------------------------------------------
  7889. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7890. {
  7891. return ScaleByTextHeight(this, 48);
  7892. }
  7893. //---------------------------------------------------------------------------
  7894. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7895. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7896. {
  7897. // when queue is hidden by double-clicking splitter, stray attempt to
  7898. // resize the panel with strange value arrives, make sure it is ignored
  7899. if (ComponentVisible[fcQueueView])
  7900. {
  7901. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7902. if (NewSize > MaxHeightLimit)
  7903. {
  7904. NewSize = MaxHeightLimit;
  7905. }
  7906. int MinHeightLimit =
  7907. GetStaticQueuePanelComponentsHeight() +
  7908. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7909. GetMinQueueViewHeight();
  7910. if (NewSize < MinHeightLimit)
  7911. {
  7912. NewSize = MinHeightLimit;
  7913. }
  7914. }
  7915. else
  7916. {
  7917. Accept = false;
  7918. }
  7919. }
  7920. //---------------------------------------------------------------------------
  7921. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7922. {
  7923. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7924. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7925. if (QueueViewHeight < GetMinQueueViewHeight())
  7926. {
  7927. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7928. }
  7929. }
  7930. //---------------------------------------------------------------------------
  7931. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7932. TDragObject *& /*DragObject*/)
  7933. {
  7934. FQueueActedItem = QueueView3->ItemFocused;
  7935. FQueueScrollOnDragOver->StartDrag();
  7936. }
  7937. //---------------------------------------------------------------------------
  7938. void __fastcall TCustomScpExplorerForm::QueueView3EndDrag(
  7939. TObject *, TObject * DebugUsedArg(Target), int DebugUsedArg(X), int DebugUsedArg(Y))
  7940. {
  7941. FQueueScrollOnDragOver->EndDrag();
  7942. }
  7943. //---------------------------------------------------------------------------
  7944. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7945. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7946. {
  7947. Accept = true;
  7948. if (Source == QueueView3)
  7949. {
  7950. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7951. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7952. if (Accept)
  7953. {
  7954. TQueueItemProxy * QueueItem;
  7955. TQueueItemProxy * DestQueueItem;
  7956. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7957. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7958. Accept = (QueueItem != DestQueueItem) &&
  7959. (QueueItem->Status == TQueueItem::qsPending) &&
  7960. (DestQueueItem->Status == TQueueItem::qsPending);
  7961. }
  7962. FQueueScrollOnDragOver->DragOver(TPoint(X, Y));
  7963. }
  7964. }
  7965. //---------------------------------------------------------------------------
  7966. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7967. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7968. {
  7969. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7970. {
  7971. TQueueItemProxy * QueueItem;
  7972. TQueueItemProxy * DestQueueItem;
  7973. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7974. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7975. QueueItem->Move(DestQueueItem);
  7976. }
  7977. }
  7978. //---------------------------------------------------------------------------
  7979. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7980. {
  7981. if ((QueueView3->ItemFocused == NULL) &&
  7982. (QueueView3->Items->Count > 0))
  7983. {
  7984. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7985. }
  7986. QueueLabelUpdateStatus();
  7987. }
  7988. //---------------------------------------------------------------------------
  7989. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7990. {
  7991. QueueLabelUpdateStatus();
  7992. }
  7993. //---------------------------------------------------------------------------
  7994. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7995. {
  7996. QueueLabel->UpdateStatus();
  7997. }
  7998. //---------------------------------------------------------------------------
  7999. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  8000. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  8001. {
  8002. if (Selected)
  8003. {
  8004. NonVisualDataModule->UpdateNonVisibleActions();
  8005. }
  8006. }
  8007. //---------------------------------------------------------------------------
  8008. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  8009. {
  8010. TDragDropFilesEx * Result = NULL;
  8011. if (Sender == SessionsPageControl)
  8012. {
  8013. Result = FSessionsDragDropFilesEx;
  8014. }
  8015. else if (Sender == QueueView3)
  8016. {
  8017. Result = FQueueDragDropFilesEx;
  8018. }
  8019. else
  8020. {
  8021. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8022. if (DirView != NULL)
  8023. {
  8024. Result = DirView->DragDropFilesEx;
  8025. }
  8026. else
  8027. {
  8028. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  8029. if (DriveView != NULL)
  8030. {
  8031. Result = DriveView->DragDropFilesEx;
  8032. }
  8033. }
  8034. }
  8035. DebugAssert(Result != NULL);
  8036. return Result;
  8037. }
  8038. //---------------------------------------------------------------------------
  8039. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  8040. {
  8041. return (DView->SelCount == DView->FilesCount);
  8042. }
  8043. //---------------------------------------------------------------------------
  8044. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  8045. {
  8046. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  8047. }
  8048. //---------------------------------------------------------------------------
  8049. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  8050. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  8051. {
  8052. DebugAssert(!IsLocalBrowserMode());
  8053. TFileOperation Operation;
  8054. switch (Effect)
  8055. {
  8056. case DROPEFFECT_MOVE:
  8057. Operation = foMove;
  8058. break;
  8059. case DROPEFFECT_COPY:
  8060. // occures on WinXP (reported by user)
  8061. default:
  8062. Operation = foCopy;
  8063. break;
  8064. };
  8065. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  8066. // see a comment in TUnixDirView::PerformItemDragDropOperation
  8067. if (DragDropFilesEx->FileList->Count > 0)
  8068. {
  8069. TStrings * FileList = new TStringList();
  8070. try
  8071. {
  8072. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  8073. {
  8074. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  8075. }
  8076. FDragDropOperation = true;
  8077. TTransferOperationParam Param;
  8078. Param.TargetDirectory = TargetPath;
  8079. // upload, no temp dirs
  8080. Param.Temp = false;
  8081. Param.DragDrop = DragDrop;
  8082. Param.Options =
  8083. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  8084. if (ForceQueue)
  8085. {
  8086. Param.Queue = asOn;
  8087. }
  8088. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  8089. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  8090. {
  8091. if (IsFileControl(DropSourceControl, osLocal))
  8092. {
  8093. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  8094. }
  8095. else
  8096. {
  8097. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  8098. }
  8099. }
  8100. }
  8101. __finally
  8102. {
  8103. FDragDropOperation = false;
  8104. delete FileList;
  8105. }
  8106. }
  8107. }
  8108. //---------------------------------------------------------------------------
  8109. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  8110. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  8111. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  8112. {
  8113. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  8114. }
  8115. //---------------------------------------------------------------------------
  8116. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  8117. TObject * Sender, int grfKeyState, int & dwEffect)
  8118. {
  8119. DebugAssert(!IsLocalBrowserMode());
  8120. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  8121. if ((dwEffect != DROPEFFECT_NONE) &&
  8122. IsFileControl(DropSourceControl, osRemote))
  8123. {
  8124. // do not allow drop on remote panel (on free space, still allow drop on directories)
  8125. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  8126. (RemoteDirView->DropTarget == NULL))
  8127. {
  8128. dwEffect = DROPEFFECT_NONE;
  8129. }
  8130. else
  8131. {
  8132. if (dwEffect == DROPEFFECT_COPY)
  8133. {
  8134. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  8135. // currently we support copying always (at least via temporary directory);
  8136. // remove associated checks once this all proves stable and working
  8137. bool CopyCapable = true;
  8138. // if we do not support neither of operations, there's no discussion
  8139. if (!MoveCapable && !CopyCapable)
  8140. {
  8141. dwEffect = DROPEFFECT_NONE;
  8142. }
  8143. else
  8144. {
  8145. // The default effect inside remote panel is move,
  8146. // unless we do not support it, but support copy
  8147. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  8148. {
  8149. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  8150. }
  8151. else
  8152. {
  8153. // with ctrl-down, we want copy unless it is not supported
  8154. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  8155. }
  8156. }
  8157. }
  8158. }
  8159. }
  8160. }
  8161. //---------------------------------------------------------------------------
  8162. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  8163. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  8164. {
  8165. DebugAssert(!IsLocalBrowserMode());
  8166. if (FDDTotalSize >= 0)
  8167. {
  8168. if (File->IsDirectory)
  8169. {
  8170. FDDTotalSize = -1;
  8171. }
  8172. else
  8173. {
  8174. FDDTotalSize += File->Size;
  8175. }
  8176. }
  8177. DebugAssert(!FDragTempDir.IsEmpty());
  8178. // TODO: this is quite ineffective
  8179. // TODO: what if invalid character replacement is disabled?
  8180. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  8181. UnicodeString TransferFileName;
  8182. if (Sender == RemoteDriveView)
  8183. {
  8184. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  8185. }
  8186. else
  8187. {
  8188. TransferFileName = File->FileName;
  8189. }
  8190. FDDFileList->AddObject(TransferFileName, File);
  8191. }
  8192. //---------------------------------------------------------------------------
  8193. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  8194. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  8195. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  8196. {
  8197. // sometimes we do not get DDEnd so the list is not released
  8198. SAFE_DESTROY(FDDFileList);
  8199. FDDFileList = new TStringList();
  8200. FDragTempDir = WinConfiguration->TemporaryDir();
  8201. FDDTotalSize = 0;
  8202. FDragDropSshTerminate.reset(NULL);
  8203. }
  8204. //---------------------------------------------------------------------------
  8205. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  8206. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  8207. HRESULT & Result)
  8208. {
  8209. if (Result == DRAGDROP_S_DROP)
  8210. {
  8211. try
  8212. {
  8213. GlobalDragImageList->HideDragImage();
  8214. try
  8215. {
  8216. RemoteFileControlDDTargetDrop();
  8217. }
  8218. catch(ESshTerminate & E)
  8219. {
  8220. FDragDropSshTerminate.reset(E.Clone());
  8221. }
  8222. }
  8223. catch (Exception &E)
  8224. {
  8225. // If downloading fails we need to cancel drag&drop, otherwise
  8226. // Explorer shows error
  8227. // But by the way exception probably never reach this point as
  8228. // it's catched on way.
  8229. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8230. Result = DRAGDROP_S_CANCEL;
  8231. ShowExtendedException(Terminal, &E);
  8232. }
  8233. }
  8234. }
  8235. //---------------------------------------------------------------------------
  8236. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8237. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8238. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8239. {
  8240. TFileMasks::TParams MaskParams;
  8241. MaskParams.Size = Size;
  8242. MaskParams.Modification = Modification;
  8243. // this does not re-parse the mask if it is the same as the last time
  8244. FDirViewMatchMask = Masks;
  8245. bool ImplicitMatch;
  8246. Matches =
  8247. // Local and RecurseInclude parameters have no effect as there's no path.
  8248. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  8249. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  8250. (AllowImplicitMatches || !ImplicitMatch);
  8251. }
  8252. //---------------------------------------------------------------------------
  8253. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8254. TObject * Sender, TListItem * Item, WORD & Indexes)
  8255. {
  8256. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8257. UnicodeString Ext;
  8258. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  8259. {
  8260. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  8261. }
  8262. else
  8263. {
  8264. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  8265. }
  8266. if (SameText(Ext, Configuration->PartialExt))
  8267. {
  8268. Indexes |= oiPartial;
  8269. }
  8270. }
  8271. //---------------------------------------------------------------------------
  8272. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8273. {
  8274. return
  8275. DirViewEnabled(osCurrent) &&
  8276. DirView(osCurrent)->CanPasteFromClipBoard();
  8277. }
  8278. //---------------------------------------------------------------------------
  8279. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8280. {
  8281. bool Result = false;
  8282. if (CanPasteToDirViewFromClipBoard())
  8283. {
  8284. Result = true;
  8285. }
  8286. else
  8287. {
  8288. UnicodeString ClipboardText;
  8289. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8290. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8291. {
  8292. if (StoredSessions->IsUrl(ClipboardText))
  8293. {
  8294. Result = true;
  8295. }
  8296. else
  8297. {
  8298. Result = DirViewEnabled(osCurrent);
  8299. }
  8300. }
  8301. }
  8302. return Result;
  8303. }
  8304. //---------------------------------------------------------------------------
  8305. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8306. {
  8307. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8308. if (DoesClipboardContainOurFiles())
  8309. {
  8310. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8311. {
  8312. DebugAssert(!IsLocalBrowserMode());
  8313. TTerminalManager * Manager = TTerminalManager::Instance();
  8314. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8315. Manager->ActiveSession = FClipboardTerminal;
  8316. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8317. }
  8318. }
  8319. else if (CanPasteToDirViewFromClipBoard())
  8320. {
  8321. DirView(osCurrent)->PasteFromClipBoard();
  8322. }
  8323. else
  8324. {
  8325. UnicodeString ClipboardText;
  8326. if (NonEmptyTextFromClipboard(ClipboardText))
  8327. {
  8328. if (StoredSessions->IsUrl(ClipboardText))
  8329. {
  8330. NewSession(ClipboardText);
  8331. }
  8332. else
  8333. {
  8334. DirView(osCurrent)->Path = ClipboardText;
  8335. }
  8336. }
  8337. }
  8338. }
  8339. //---------------------------------------------------------------------------
  8340. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8341. {
  8342. // TBD
  8343. }
  8344. //---------------------------------------------------------------------------
  8345. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8346. {
  8347. TCustomDirView * ADirView = DirView(Side);
  8348. ADirView->SelectAll(Mode);
  8349. ADirView->SetFocus();
  8350. }
  8351. //---------------------------------------------------------------------------
  8352. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8353. {
  8354. TCustomDirView * ADirView = DirView(Side);
  8355. TFileFilter Filter;
  8356. DefaultFileFilter(Filter);
  8357. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8358. {
  8359. Configuration->Usage->Inc(L"MaskSelections");
  8360. ADirView->SelectFiles(Filter, Select);
  8361. ADirView->SetFocus();
  8362. }
  8363. }
  8364. //---------------------------------------------------------------------------
  8365. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8366. {
  8367. TCustomDirView * ADirView = DirView(Side);
  8368. ADirView->RestoreSelectedNames();
  8369. ADirView->SetFocus();
  8370. }
  8371. //---------------------------------------------------------------------------
  8372. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8373. {
  8374. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8375. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8376. {
  8377. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8378. UnicodeString Ext;
  8379. if (!IsSideLocalBrowser(osCurrent))
  8380. {
  8381. Ext = UnixExtractFileExt(FileName);
  8382. }
  8383. else
  8384. {
  8385. Ext = ExtractFileExt(FileName);
  8386. }
  8387. if (Ext.IsEmpty())
  8388. {
  8389. Ext = L".";
  8390. }
  8391. TFileFilter Filter;
  8392. Filter.Masks = FORMAT(L"*%s", (Ext));
  8393. Filter.Directories = false;
  8394. CurrentDirView->SelectFiles(Filter, Select);
  8395. }
  8396. }
  8397. //---------------------------------------------------------------------------
  8398. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8399. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8400. {
  8401. UnicodeString Result;
  8402. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8403. {
  8404. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8405. }
  8406. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8407. {
  8408. // noop
  8409. }
  8410. else
  8411. {
  8412. Result =
  8413. FMTLOAD(FILE_INFO_FORMAT,
  8414. (FormatBytes(FileInfo.SelectedSize),
  8415. FormatBytes(FileInfo.FilesSize),
  8416. FormatNumber(FileInfo.SelectedCount),
  8417. FormatNumber(FileInfo.FilesCount)));
  8418. }
  8419. return Result;
  8420. }
  8421. //---------------------------------------------------------------------------
  8422. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8423. TTBXStatusPanel * Panel, TOperationSide Side)
  8424. {
  8425. if (Panel->Index == 1)
  8426. {
  8427. ToggleShowHiddenFiles();
  8428. }
  8429. else if (Panel->Index == 2)
  8430. {
  8431. Filter(Side);
  8432. }
  8433. }
  8434. //---------------------------------------------------------------------------
  8435. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8436. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8437. {
  8438. DebugAssert(!StatusBar->SimplePanel);
  8439. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8440. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8441. }
  8442. //---------------------------------------------------------------------------
  8443. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8444. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8445. {
  8446. DebugAssert(StatusBar->Panels->Count >= 3);
  8447. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8448. if (FileInfo.HiddenCount > 0)
  8449. {
  8450. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8451. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8452. }
  8453. else
  8454. {
  8455. HiddenFilesPanel->ViewPriority = 0;
  8456. // not really necessary, just to cleanup no-longer-valid data
  8457. HiddenFilesPanel->Caption = L"";
  8458. }
  8459. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8460. if (FileInfo.FilteredCount > 0)
  8461. {
  8462. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8463. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8464. }
  8465. else
  8466. {
  8467. FilteredFilesPanel->ViewPriority = 0;
  8468. // not really necessary, just to cleanup no-longer-valid data
  8469. FilteredFilesPanel->Caption = L"";
  8470. }
  8471. }
  8472. //---------------------------------------------------------------------------
  8473. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8474. TObject * /*Sender*/)
  8475. {
  8476. if (DirView(osOther)->Enabled)
  8477. {
  8478. DirView(osOther)->SetFocus();
  8479. }
  8480. }
  8481. //---------------------------------------------------------------------------
  8482. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8483. {
  8484. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8485. switch (Config.Show)
  8486. {
  8487. case qvShow:
  8488. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8489. {
  8490. Config.Show = qvHide;
  8491. }
  8492. else
  8493. {
  8494. Config.Show = Config.LastHideShow;
  8495. }
  8496. break;
  8497. case qvHideWhenEmpty:
  8498. if (ComponentVisible[fcQueueView])
  8499. {
  8500. Config.Show = qvHide;
  8501. }
  8502. else
  8503. {
  8504. Config.LastHideShow = Config.Show;
  8505. Config.Show = qvShow;
  8506. }
  8507. break;
  8508. case qvHide:
  8509. Config.LastHideShow = Config.Show;
  8510. Config.Show = qvShow;
  8511. break;
  8512. }
  8513. WinConfiguration->QueueView = Config;
  8514. }
  8515. //---------------------------------------------------------------------------
  8516. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8517. {
  8518. UnicodeString Result;
  8519. if (Terminal != NULL)
  8520. {
  8521. switch (WinConfiguration->PathInCaption)
  8522. {
  8523. case picShort:
  8524. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8525. break;
  8526. case picFull:
  8527. Result = Terminal->CurrentDirectory;
  8528. break;
  8529. }
  8530. }
  8531. return Result;
  8532. }
  8533. //---------------------------------------------------------------------------
  8534. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8535. {
  8536. TColor Result = GetWindowColor(FSessionColor);
  8537. return Result;
  8538. }
  8539. //---------------------------------------------------------------------------
  8540. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8541. {
  8542. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8543. return Result;
  8544. }
  8545. //---------------------------------------------------------------------------
  8546. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8547. {
  8548. if (!FSessionChanging)
  8549. {
  8550. TTerminalManager::Instance()->UpdateAppTitle();
  8551. }
  8552. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8553. // background to gray on Windows 7 (works on Windows 10).
  8554. // See also EnableControl
  8555. if (Showing)
  8556. {
  8557. if (ManagedSession != NULL)
  8558. {
  8559. // Update path when it changes
  8560. if ((SessionsPageControl->ActivePage != NULL) &&
  8561. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8562. {
  8563. UpdateSessionTab(SessionsPageControl->ActivePage);
  8564. }
  8565. }
  8566. NonVisualDataModule->ReconnectSessionAction->Update();
  8567. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8568. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8569. CenterReconnectToolbar();
  8570. bool HasTerminal = HasActiveTerminal();
  8571. RemoteDirView->Enabled = HasTerminal;
  8572. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8573. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8574. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8575. RemoteDriveView->Color = RemoteDirView->Color;
  8576. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8577. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8578. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8579. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8580. QueueFileList->Enabled = QueueView3->Enabled;
  8581. QueueFileList->Color = QueueView3->Color;
  8582. QueueFileList->Font->Color = QueueView3->Font->Color;
  8583. QueueLabelUpdateStatus();
  8584. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8585. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8586. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8587. {
  8588. UpdateDarkMode();
  8589. }
  8590. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8591. MessageDock->Visible = (MessageDock->ToolbarCount > 0);
  8592. }
  8593. }
  8594. //---------------------------------------------------------------------------
  8595. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8596. {
  8597. UpdateControls();
  8598. }
  8599. //---------------------------------------------------------------------------
  8600. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8601. TObject * Sender)
  8602. {
  8603. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8604. DebugAssert(DirView != NULL);
  8605. DoDirViewLoaded(DirView);
  8606. TransferPresetAutoSelect();
  8607. }
  8608. //---------------------------------------------------------------------------
  8609. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8610. {
  8611. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8612. // first run after installation
  8613. if (double(Updates.LastCheck) == 0)
  8614. {
  8615. AppLog(L"First run, scheduling updates check for the next run");
  8616. // make sure next time there will be an update (if enabled)
  8617. Updates.LastCheck = TDateTime(1);
  8618. WinConfiguration->Updates = Updates;
  8619. }
  8620. else
  8621. {
  8622. TDateTime Period;
  8623. if (IsUWP() && !IsOfficialPackage())
  8624. {
  8625. Period = DefaultUpdatesPeriod;
  8626. AppLog(L"Thirdparty UWP package, using default updates check period");
  8627. }
  8628. else
  8629. {
  8630. Period = Updates.Period;
  8631. }
  8632. AppLogFmt(L"Updates check period: %.2f", (double(Period)));
  8633. if (double(Period) > 0)
  8634. {
  8635. TDateTime Interval = Now() - Updates.LastCheck;
  8636. AppLogFmt(L"Interval since the last updates check: %.2f", (double(Interval)));
  8637. if (Interval >= Period)
  8638. {
  8639. StartUpdateThread(UpdatesChecked);
  8640. }
  8641. }
  8642. }
  8643. }
  8644. //---------------------------------------------------------------------------
  8645. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8646. {
  8647. AppLog(L"Updates check done");
  8648. UnicodeString Message;
  8649. bool New;
  8650. TQueryType Type;
  8651. GetUpdatesMessage(Message, New, Type, false);
  8652. if (!Message.IsEmpty())
  8653. {
  8654. AppLogFmt(L"Updates check message: %s", (Message));
  8655. if (New)
  8656. {
  8657. AppLog(L"New version detected");
  8658. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8659. }
  8660. if (!New && (Type != qtWarning))
  8661. {
  8662. AppLog(L"Posting non-critical updates note");
  8663. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8664. }
  8665. else
  8666. {
  8667. AppLog(L"Posting new version or critical message notification");
  8668. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8669. }
  8670. if (New)
  8671. {
  8672. Configuration->Usage->Inc(L"UpdateNotifications");
  8673. }
  8674. }
  8675. }
  8676. //---------------------------------------------------------------------------
  8677. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8678. {
  8679. RestoreApp();
  8680. bool CanDisplay = !NonVisualDataModule->Busy;
  8681. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8682. {
  8683. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8684. }
  8685. if (CanDisplay)
  8686. {
  8687. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8688. // Probably not needed as CheckForUpdates is blocking since we started using neon for HTTP implementation.
  8689. NonVisualDataModule->StartBusy();
  8690. try
  8691. {
  8692. CheckForUpdates(true);
  8693. }
  8694. __finally
  8695. {
  8696. NonVisualDataModule->EndBusy();
  8697. }
  8698. }
  8699. else
  8700. {
  8701. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8702. }
  8703. }
  8704. //---------------------------------------------------------------------------
  8705. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8706. TCopyParamRuleData & Data)
  8707. {
  8708. DebugAssert(Terminal != NULL);
  8709. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8710. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8711. Data.RemoteDirectory = RemoteDirView->PathName;
  8712. }
  8713. //---------------------------------------------------------------------------
  8714. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8715. {
  8716. // Terminal can be null when we are changing local directory implicitly,
  8717. // because it has been deleted, while no session is connected
  8718. // (Login dialog is open)
  8719. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8720. {
  8721. DebugAssert(!IsLocalBrowserMode());
  8722. TCopyParamRuleData Data;
  8723. GetTransferPresetAutoSelectData(Data);
  8724. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8725. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8726. if (CopyParamIndex < 0)
  8727. {
  8728. // there is no preset that matches autoselection
  8729. // set preset that we consider "default"
  8730. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8731. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8732. }
  8733. else
  8734. {
  8735. // there is preset matching autoselection
  8736. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8737. if (CopyParamName == FCopyParamAutoSelected)
  8738. {
  8739. // autoselected the same preset as the last time
  8740. // make no change (i.e. preserve custom user preset, if any)
  8741. }
  8742. else
  8743. {
  8744. // autoselected the different preset then the last time (or there
  8745. // was default preset set)
  8746. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8747. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8748. }
  8749. }
  8750. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8751. {
  8752. if (CopyParamIndex >= 0)
  8753. {
  8754. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8755. }
  8756. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8757. try
  8758. {
  8759. int Fmt =
  8760. (CopyParamIndex < 0) ?
  8761. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8762. COPY_PARAM_AUTOSELECTED;
  8763. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8764. Data->Message = MainInstructions(Message);
  8765. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8766. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8767. CopyParamAttrs);
  8768. if (CopyParamIndex >= 0)
  8769. {
  8770. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8771. Info = FORMAT(L"%s\n \n%s", (Info,
  8772. FMTLOAD(COPY_PARAM_RULE,
  8773. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8774. }
  8775. Data->Message += L"\n\n" + Info;
  8776. if (WinConfiguration->CopyParamAutoSelectNotice)
  8777. {
  8778. TransferPresetNoteMessage(Data, true);
  8779. }
  8780. else
  8781. {
  8782. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8783. Data = NULL; // ownership passed
  8784. }
  8785. }
  8786. __finally
  8787. {
  8788. delete Data;
  8789. }
  8790. }
  8791. }
  8792. }
  8793. //---------------------------------------------------------------------------
  8794. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8795. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8796. {
  8797. DebugAssert(NoteData != NULL);
  8798. DebugAssert(!IsLocalBrowserMode());
  8799. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8800. TQueryButtonAlias Aliases[1];
  8801. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8802. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8803. Params.Aliases = Aliases;
  8804. Params.AliasesCount = LENOF(Aliases);
  8805. unsigned int Result =
  8806. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8807. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8808. switch (Result)
  8809. {
  8810. case qaNeverAskAgain:
  8811. DebugAssert(AllowNeverAskAgain);
  8812. WinConfiguration->CopyParamAutoSelectNotice = false;
  8813. break;
  8814. case qaIgnore:
  8815. PreferencesDialog(pmPresets);
  8816. break;
  8817. }
  8818. }
  8819. //---------------------------------------------------------------------------
  8820. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8821. {
  8822. // as of now useless, as this is used for notes only, never for balloons, ...
  8823. RestoreApp();
  8824. // .. and we should never be busy here
  8825. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8826. {
  8827. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8828. }
  8829. }
  8830. //---------------------------------------------------------------------------
  8831. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8832. TPreferencesMode APreferencesMode)
  8833. {
  8834. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8835. TCopyParamRuleData Data;
  8836. if (Terminal != NULL)
  8837. {
  8838. PreferencesData.reset(new TPreferencesDialogData());
  8839. GetTransferPresetAutoSelectData(Data);
  8840. PreferencesData->CopyParamRuleData = &Data;
  8841. }
  8842. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8843. }
  8844. //---------------------------------------------------------------------------
  8845. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8846. const TCustomCommandType & Command)
  8847. {
  8848. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8849. {
  8850. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8851. }
  8852. }
  8853. //---------------------------------------------------------------------------
  8854. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8855. {
  8856. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8857. TCustomCommandType Command;
  8858. // make sure we use local custom command when remote are not supported
  8859. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8860. {
  8861. Command = FLastCustomCommand;
  8862. }
  8863. else
  8864. {
  8865. Command.Params = Command.Params | ccLocal;
  8866. }
  8867. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8868. FEditingFocusedAdHocCommand = OnFocused;
  8869. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8870. int Options =
  8871. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8872. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8873. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8874. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8875. {
  8876. FLastCustomCommand = Command;
  8877. UpdateCustomCommandsToolbar();
  8878. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8879. }
  8880. }
  8881. //---------------------------------------------------------------------------
  8882. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8883. {
  8884. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8885. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8886. DebugAssert(State > 0);
  8887. if (State <= 0)
  8888. {
  8889. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8890. }
  8891. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8892. }
  8893. //---------------------------------------------------------------------------
  8894. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8895. TCustomCommandType & Command, int & State)
  8896. {
  8897. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8898. if (Result)
  8899. {
  8900. Command = FLastCustomCommand;
  8901. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8902. }
  8903. return Result;
  8904. }
  8905. //---------------------------------------------------------------------------
  8906. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8907. {
  8908. ResetIncrementalSearch();
  8909. }
  8910. //---------------------------------------------------------------------------
  8911. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8912. {
  8913. DebugAssert(ComponentVisible[Component]);
  8914. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8915. }
  8916. //---------------------------------------------------------------------------
  8917. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8918. {
  8919. // when queue panel is resized here directly, the status bar is stretched
  8920. // over whole space the panel occupied
  8921. PostComponentHide(fcQueueView);
  8922. }
  8923. //---------------------------------------------------------------------------
  8924. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8925. {
  8926. ComponentVisible[fcQueueFileList] = false;
  8927. }
  8928. //---------------------------------------------------------------------------
  8929. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8930. {
  8931. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8932. RefreshColorMode();
  8933. ResetSysDarkTheme();
  8934. ConfigurationChanged();
  8935. ConfigureInterface();
  8936. // Should be called for all controls
  8937. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8938. }
  8939. //---------------------------------------------------------------------------
  8940. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8941. {
  8942. // Do not handle, when shutting down anyway (maybe also when not setup completely yet?)
  8943. if (!FInvalid &&
  8944. (Message.LParam != 0) &&
  8945. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8946. {
  8947. ThemeChanged();
  8948. }
  8949. TForm::Dispatch(&Message);
  8950. }
  8951. //---------------------------------------------------------------------------
  8952. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8953. {
  8954. TMessage * M = static_cast<TMessage*>(Message);
  8955. switch (M->Msg)
  8956. {
  8957. case CM_DIALOGCHAR:
  8958. CMDialogChar(*M);
  8959. break;
  8960. case WM_APPCOMMAND:
  8961. WMAppCommand(*M);
  8962. break;
  8963. case WM_SYSCOMMAND:
  8964. WMSysCommand(*M);
  8965. break;
  8966. case WM_QUERYENDSESSION:
  8967. WMQueryEndSession(*M);
  8968. break;
  8969. case WM_ENDSESSION:
  8970. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8971. break;
  8972. #ifdef _DEBUG
  8973. case WM_WINDOWPOSCHANGED:
  8974. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8975. break;
  8976. case CM_CUSTOMSTYLECHANGED:
  8977. TForm::Dispatch(Message);
  8978. break;
  8979. #endif
  8980. case WM_COMPONENT_HIDE:
  8981. {
  8982. Byte Component = static_cast<Byte>(M->WParam);
  8983. // sanity check
  8984. if (ComponentVisible[Component])
  8985. {
  8986. // special treatment
  8987. if (Component == fcQueueView)
  8988. {
  8989. ToggleQueueVisibility();
  8990. DebugAssert(!ComponentVisible[fcQueueView]);
  8991. }
  8992. else
  8993. {
  8994. ComponentVisible[Component] = false;
  8995. }
  8996. }
  8997. }
  8998. break;
  8999. case WM_COPYDATA:
  9000. WMCopyData(*M);
  9001. break;
  9002. case WM_MANAGES_CAPTION:
  9003. // caption managed in TTerminalManager::UpdateAppTitle()
  9004. M->Result = 1;
  9005. break;
  9006. case WM_WANTS_MOUSEWHEEL:
  9007. M->Result = 1;
  9008. break;
  9009. case CM_SHOWINGCHANGED:
  9010. CMShowingChanged(*M);
  9011. break;
  9012. case WM_CLOSE:
  9013. WMClose(*M);
  9014. break;
  9015. case WM_DPICHANGED:
  9016. WMDpiChanged(*M);
  9017. break;
  9018. case CM_DPICHANGED:
  9019. CMDpiChanged(*M);
  9020. break;
  9021. case WM_SETTINGCHANGE:
  9022. WMSettingChange(*M);
  9023. break;
  9024. case CM_DIALOGKEY:
  9025. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  9026. break;
  9027. default:
  9028. TForm::Dispatch(Message);
  9029. break;
  9030. }
  9031. }
  9032. //---------------------------------------------------------------------------
  9033. void __fastcall TCustomScpExplorerForm::UpdateImages()
  9034. {
  9035. // noop
  9036. }
  9037. //---------------------------------------------------------------------------
  9038. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  9039. {
  9040. TForm::Dispatch(&Message);
  9041. FSessionColors->Clear();
  9042. AddFixedSessionImages();
  9043. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  9044. UpdateImages();
  9045. }
  9046. //---------------------------------------------------------------------------
  9047. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  9048. {
  9049. TForm::Dispatch(&Message);
  9050. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  9051. UpdatePixelsPerInchMainWindowCounter();
  9052. }
  9053. //---------------------------------------------------------------------------
  9054. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  9055. {
  9056. // Cannot close window while we are busy.
  9057. // We cannot test this in FormCloseQuery as that is called also from
  9058. // Close(), which is called by CloseApplicationAction. So we can be busy
  9059. // there already even, when it is legitimate to close the application.
  9060. // Possibly a better place to handle this would be WMSysCommand.
  9061. if (NonVisualDataModule->Busy)
  9062. {
  9063. Message.Result = 1;
  9064. }
  9065. else
  9066. {
  9067. TForm::Dispatch(&Message);
  9068. }
  9069. }
  9070. //---------------------------------------------------------------------------
  9071. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  9072. {
  9073. if (!FShowingChanged)
  9074. {
  9075. AddStartupSequence(L"D");
  9076. FShowingChanged = true;
  9077. }
  9078. // Ignoring implicit local-local browser of Commander, opened from DoShow
  9079. bool NoSession = (ManagedSession == NULL);
  9080. // This calls TCustomScpExplorerForm::DoShow
  9081. TForm::Dispatch(&Message);
  9082. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  9083. if (Showing && !FStarted)
  9084. {
  9085. FStarted = true;
  9086. InterfaceStarted();
  9087. }
  9088. if (Showing && NoSession)
  9089. {
  9090. // When we are starting minimized (i.e. from an installer),
  9091. // postpone showing Login dialog until we get restored.
  9092. // Otherwise the Login dialog (and Authentication window) show restored
  9093. // over invisible (minimized) main window.
  9094. if (WindowState == wsMinimized)
  9095. {
  9096. FNeedSession = true;
  9097. }
  9098. else
  9099. {
  9100. // This happens before application ever goes idle, so the toolbars would
  9101. // stay enabled (initial state) until the Login dialog is dismissed.
  9102. UpdateActions();
  9103. NonVisualDataModule->StartBusy();
  9104. try
  9105. {
  9106. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  9107. // otherwise the dialog does not receive focus.
  9108. // With Commander interface the ProcessMessages is called already
  9109. // by TDriveView, but with Explorer interface, we need to call it explicily
  9110. Application->ProcessMessages();
  9111. // do not reload sessions, they have been loaded just now (optimization)
  9112. NeedSession(true);
  9113. }
  9114. __finally
  9115. {
  9116. NonVisualDataModule->EndBusy();
  9117. }
  9118. }
  9119. }
  9120. }
  9121. //---------------------------------------------------------------------------
  9122. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  9123. {
  9124. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  9125. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  9126. }
  9127. //---------------------------------------------------------------------------
  9128. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  9129. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  9130. int & MaxHeight)
  9131. {
  9132. // workaround for bug in TWinControl.CalcConstraints
  9133. // Check for empty rect (restore from iconic state) is done there only after
  9134. // call to AdjustClientRect, which enlarges the rect (for forms).
  9135. TRect R = GetClientRect();
  9136. // when restoring from iconic state, place no restrictions
  9137. if (IsRectEmpty(R))
  9138. {
  9139. MinWidth = 0;
  9140. MinHeight = 0;
  9141. MaxWidth = 32000;
  9142. MaxHeight = 32000;
  9143. }
  9144. }
  9145. //---------------------------------------------------------------------------
  9146. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  9147. TSpaceAvailable & ASpaceAvailable, bool & Close)
  9148. {
  9149. // terminal can be already closed (e.g. dropped connection)
  9150. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  9151. {
  9152. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  9153. try
  9154. {
  9155. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  9156. }
  9157. catch(...)
  9158. {
  9159. if (!Terminal->Active)
  9160. {
  9161. Close = true;
  9162. }
  9163. throw;
  9164. }
  9165. }
  9166. }
  9167. //---------------------------------------------------------------------------
  9168. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  9169. {
  9170. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  9171. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  9172. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  9173. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  9174. {
  9175. OnGetSpaceAvailable = GetSpaceAvailable;
  9176. }
  9177. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  9178. OnGetSpaceAvailable);
  9179. }
  9180. //---------------------------------------------------------------------------
  9181. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  9182. {
  9183. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  9184. Terminal->FillSessionDataForCode(Data.get());
  9185. return Data.release();
  9186. }
  9187. //---------------------------------------------------------------------------
  9188. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  9189. {
  9190. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  9191. DoGenerateUrlDialog(Data.get(), Paths);
  9192. }
  9193. //---------------------------------------------------------------------------
  9194. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  9195. {
  9196. GenerateUrl(NULL);
  9197. }
  9198. //---------------------------------------------------------------------------
  9199. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  9200. {
  9201. std::unique_ptr<TStrings> Paths(new TStringList());
  9202. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  9203. GenerateUrl(Paths.get());
  9204. }
  9205. //---------------------------------------------------------------------------
  9206. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  9207. {
  9208. FSessionColor = value;
  9209. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  9210. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  9211. static_cast<TObject *>(GetComponent(fcColorMenu)));
  9212. DebugAssert(ColorItem != NULL);
  9213. ColorItem->Color = C;
  9214. NonVisualDataModule->ColorMenuItem->Color = C;
  9215. UpdateControls();
  9216. }
  9217. //---------------------------------------------------------------------------
  9218. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  9219. {
  9220. if (value != FSessionColor)
  9221. {
  9222. UpdateSessionColor(value);
  9223. // Is null when called from LastTerminalClosed
  9224. if (Terminal != NULL)
  9225. {
  9226. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  9227. }
  9228. }
  9229. }
  9230. //---------------------------------------------------------------------------
  9231. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  9232. {
  9233. bool Result = FNoteTimer->Enabled;
  9234. if (Result)
  9235. {
  9236. // cannot cancel note too early
  9237. bool NotEarly =
  9238. (Now() - FNoteShown >
  9239. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  9240. if (Force || NotEarly)
  9241. {
  9242. FNoteTimer->Enabled = false;
  9243. FNote = L"";
  9244. SAFE_DESTROY(FNoteData);
  9245. FOnNoteClick = NULL;
  9246. FNoteHints = FNotes->Text;
  9247. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  9248. UpdateStatusBar();
  9249. }
  9250. }
  9251. return Result;
  9252. }
  9253. //---------------------------------------------------------------------------
  9254. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  9255. {
  9256. DebugAssert(FNoteTimer->Enabled);
  9257. CancelNote(true);
  9258. }
  9259. //---------------------------------------------------------------------------
  9260. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9261. {
  9262. FNoteHints = FNotes->Text.TrimRight();
  9263. }
  9264. //---------------------------------------------------------------------------
  9265. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9266. {
  9267. int P = Note.Pos(L"\n");
  9268. if (P > 0)
  9269. {
  9270. Note.SetLength(P - 1);
  9271. }
  9272. FNotes->Add(FORMAT(L"[%s] %s",
  9273. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9274. while (FNotes->Count > 10)
  9275. {
  9276. FNotes->Delete(0);
  9277. }
  9278. if (UpdateNow)
  9279. {
  9280. UpdateNoteHints();
  9281. UpdateStatusBar();
  9282. }
  9283. }
  9284. //---------------------------------------------------------------------------
  9285. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  9286. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9287. {
  9288. int P = Note.Pos(L"\n");
  9289. if (P > 0)
  9290. {
  9291. Note.SetLength(P - 1);
  9292. }
  9293. UpdateNoteHints();
  9294. FNote = Note;
  9295. SAFE_DESTROY(FNoteData);
  9296. FNoteData = NoteData;
  9297. FOnNoteClick = OnNoteClick;
  9298. AddNote(Note, false);
  9299. UpdateStatusBar();
  9300. FNoteShown = Now();
  9301. FNoteTimer->Enabled = false;
  9302. if (Seconds == 0)
  9303. {
  9304. Seconds = WinConfiguration->NotificationsTimeout;
  9305. }
  9306. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9307. FNoteTimer->Enabled = true;
  9308. }
  9309. //---------------------------------------------------------------------------
  9310. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9311. {
  9312. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9313. }
  9314. //---------------------------------------------------------------------------
  9315. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9316. {
  9317. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9318. {
  9319. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9320. }
  9321. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9322. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9323. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9324. 0, NULL, NULL);
  9325. }
  9326. //---------------------------------------------------------------------------
  9327. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9328. {
  9329. bool NewShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9330. UnicodeString Note =
  9331. FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1), (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, (NewShowHiddenFiles ? 2 : 3))));
  9332. PostNote(Note, 0, NULL, NULL);
  9333. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9334. WinConfiguration->ShowHiddenFiles = NewShowHiddenFiles;
  9335. }
  9336. //---------------------------------------------------------------------------
  9337. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9338. {
  9339. WinConfiguration->FormatSizeBytes = Style;
  9340. }
  9341. //---------------------------------------------------------------------------
  9342. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9343. {
  9344. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9345. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9346. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9347. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9348. }
  9349. //---------------------------------------------------------------------------
  9350. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9351. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9352. {
  9353. if (Panel->Index == 0)
  9354. {
  9355. if (FOnNoteClick != NULL)
  9356. {
  9357. // prevent the user data from being freed by possible call
  9358. // to CancelNote or PostNote during call to OnNoteClick
  9359. std::unique_ptr<TObject> NoteData(FNoteData);
  9360. TNotifyEvent OnNoteClick = FOnNoteClick;
  9361. FNoteData = NULL;
  9362. // need to cancel the note as we are going to delete its user data
  9363. CancelNote(true);
  9364. OnNoteClick(NoteData.get());
  9365. }
  9366. }
  9367. if (HasActiveTerminal() &&
  9368. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9369. {
  9370. FileSystemInfo();
  9371. }
  9372. }
  9373. //---------------------------------------------------------------------------
  9374. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9375. {
  9376. // workaround:
  9377. // 1) for unknown reason, disabling window, while minimized,
  9378. // prevents it from restoring, even if it was enabled again meanwhile
  9379. // 2) when disabling the main window, while another has focus
  9380. // minimize is no longer possible
  9381. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9382. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9383. // Shouldn't we use IsApplicationMinimized() here?
  9384. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9385. {
  9386. Enabled = false;
  9387. }
  9388. FLockLevel++;
  9389. }
  9390. //---------------------------------------------------------------------------
  9391. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9392. {
  9393. DebugAssert(FLockLevel > 0);
  9394. FLockLevel--;
  9395. if (FLockLevel == 0)
  9396. {
  9397. DebugAssert(FLockSuspendLevel == 0);
  9398. Enabled = true;
  9399. }
  9400. }
  9401. //---------------------------------------------------------------------------
  9402. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9403. {
  9404. // We need to make sure that window is enabled when the last modal window closes
  9405. // otherwise focus is not restored correctly.
  9406. // So we re-enable the window when modal window opens and
  9407. // disable it back after is closes
  9408. if (FLockLevel > 0)
  9409. {
  9410. // while we have nesting counter, we know that we never be called
  9411. // recursivelly as Application->OnModalBegin is called only
  9412. // for the top-level modal window
  9413. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9414. {
  9415. // won't be disabled when conditions in LockWindow() were not satisfied
  9416. FDisabledOnLockSuspend = !Enabled;
  9417. // When minimized to tray (or actually when set to SW_HIDE),
  9418. // setting Enabled makes the window focusable even when there's
  9419. // modal window over it
  9420. if (!FTrayIcon->Visible)
  9421. {
  9422. Enabled = true;
  9423. }
  9424. }
  9425. FLockSuspendLevel++;
  9426. }
  9427. }
  9428. //---------------------------------------------------------------------------
  9429. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9430. {
  9431. if (FLockSuspendLevel > 0)
  9432. {
  9433. DebugAssert(FLockLevel > 0);
  9434. FLockSuspendLevel--;
  9435. // see comment in SuspendWindowLock
  9436. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9437. {
  9438. // Note that window can be enabled here, when we were minized to tray when
  9439. // was SuspendWindowLock() called.
  9440. // We should possibly do the same check as in LockWindow(),
  9441. // if it is ever possible that the consitions change between
  9442. // SuspendWindowLock() and ResumeWindowLock()
  9443. Enabled = !FDisabledOnLockSuspend;
  9444. }
  9445. }
  9446. }
  9447. //---------------------------------------------------------------------------
  9448. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9449. {
  9450. TTBXComboBoxItem * RemotePathComboBox =
  9451. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9452. if (!TextOnly)
  9453. {
  9454. TStrings * Items = RemotePathComboBox->Strings;
  9455. Items->BeginUpdate();
  9456. try
  9457. {
  9458. Items->Clear();
  9459. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9460. {
  9461. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9462. while (!IsUnixRootPath(APath))
  9463. {
  9464. int P = APath.LastDelimiter(L'/');
  9465. DebugAssert(P >= 0);
  9466. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9467. APath.SetLength(P - 1);
  9468. }
  9469. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9470. }
  9471. }
  9472. __finally
  9473. {
  9474. Items->EndUpdate();
  9475. }
  9476. }
  9477. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9478. // Setting ItemIndex to -1 does not reset its text
  9479. if (RemotePathComboBox->Strings->Count == 0)
  9480. {
  9481. RemotePathComboBox->Text = L"";
  9482. }
  9483. }
  9484. //---------------------------------------------------------------------------
  9485. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9486. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9487. {
  9488. if (AIndex < 0)
  9489. {
  9490. AIndex = Sender->ItemIndex;
  9491. }
  9492. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9493. }
  9494. //---------------------------------------------------------------------------
  9495. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9496. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9497. int & ImageIndex)
  9498. {
  9499. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9500. }
  9501. //---------------------------------------------------------------------------
  9502. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9503. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9504. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9505. {
  9506. if (!IsLocalBrowserMode())
  9507. {
  9508. ARect.Left += (10 * AIndex);
  9509. }
  9510. }
  9511. //---------------------------------------------------------------------------
  9512. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9513. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9514. {
  9515. AWidth += (10 * AIndex);
  9516. }
  9517. //---------------------------------------------------------------------------
  9518. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9519. {
  9520. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9521. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9522. int Index = RemotePathComboBox->ItemIndex;
  9523. while (Index < RemotePathComboBox->Strings->Count - 1)
  9524. {
  9525. APath = UnixExtractFileDir(APath);
  9526. Index++;
  9527. }
  9528. // VanDyke style paths
  9529. if (APath.IsEmpty())
  9530. {
  9531. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9532. APath = ROOTDIRECTORY;
  9533. }
  9534. if (RemoteDirView->Path != APath)
  9535. {
  9536. RemoteDirView->Path = APath;
  9537. }
  9538. }
  9539. //---------------------------------------------------------------------------
  9540. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9541. {
  9542. DoRemotePathComboBoxItemClick(Sender);
  9543. }
  9544. //---------------------------------------------------------------------------
  9545. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9546. {
  9547. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9548. DebugUsedParam(Sender);
  9549. UpdateRemotePathComboBox(true);
  9550. }
  9551. //---------------------------------------------------------------------------
  9552. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9553. {
  9554. DoRemotePathComboBoxCancel(Sender);
  9555. }
  9556. //---------------------------------------------------------------------------
  9557. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9558. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9559. {
  9560. ResetIncrementalSearch();
  9561. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9562. DebugAssert(DirView != NULL);
  9563. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9564. {
  9565. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9566. // OnEditing is called also from TCustomListView::CanEdit
  9567. if (Edit != NULL)
  9568. {
  9569. EditSelectBaseName(Edit);
  9570. }
  9571. }
  9572. }
  9573. //---------------------------------------------------------------------------
  9574. //---------------------------------------------------------------------------
  9575. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9576. {
  9577. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9578. Result->AutoDetectDnD = false;
  9579. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9580. Result->RenderDataOn = rdoEnterAndDropSync;
  9581. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9582. return Result;
  9583. }
  9584. //---------------------------------------------------------------------------
  9585. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9586. {
  9587. if (IsWin10Build(2004))
  9588. {
  9589. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9590. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9591. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9592. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9593. }
  9594. }
  9595. //---------------------------------------------------------------------------
  9596. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9597. {
  9598. TForm::CreateWnd();
  9599. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9600. UpdateDarkMode();
  9601. if (FSessionsDragDropFilesEx == NULL)
  9602. {
  9603. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9604. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9605. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9606. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9607. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9608. }
  9609. if (FQueueDragDropFilesEx == NULL)
  9610. {
  9611. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9612. // No need to set OnDragOver as we do not have any restrictions
  9613. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9614. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9615. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9616. }
  9617. }
  9618. //---------------------------------------------------------------------------
  9619. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9620. {
  9621. TForm::DestroyWnd();
  9622. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9623. FQueueDragDropFilesEx->DragDropControl = NULL;
  9624. }
  9625. //---------------------------------------------------------------------------
  9626. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9627. {
  9628. SideEnter(FCurrentSide);
  9629. FEverShown = true;
  9630. }
  9631. //---------------------------------------------------------------------------
  9632. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9633. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9634. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9635. {
  9636. if (!NonVisualDataModule->Busy)
  9637. {
  9638. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9639. Configuration->Usage->Inc(L"FileFinds");
  9640. LockWindow(true);
  9641. NonVisualDataModule->StartBusy();
  9642. WinConfiguration->LockedInterface = true;
  9643. try
  9644. {
  9645. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9646. }
  9647. __finally
  9648. {
  9649. WinConfiguration->LockedInterface = false;
  9650. NonVisualDataModule->EndBusy();
  9651. UnlockWindow();
  9652. }
  9653. }
  9654. }
  9655. //---------------------------------------------------------------------------
  9656. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9657. {
  9658. if (!NonVisualDataModule->Busy)
  9659. {
  9660. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9661. SetFocus();
  9662. RemoteDirView->Path = UnixExtractFilePath(Path);
  9663. UnicodeString FileName = UnixExtractFileName(Path);
  9664. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9665. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9666. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9667. if (Item == NULL)
  9668. {
  9669. ReloadDirectory(osRemote);
  9670. Item = RemoteDirView->FindFileItem(FileName);
  9671. }
  9672. if (Item != NULL)
  9673. {
  9674. RemoteDirView->ItemFocused = Item;
  9675. RemoteDirView->ItemFocused->MakeVisible(false);
  9676. RemoteDirView->SetFocus();
  9677. }
  9678. }
  9679. }
  9680. //---------------------------------------------------------------------------
  9681. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9682. {
  9683. bool Result = !NonVisualDataModule->Busy;
  9684. if (Result)
  9685. {
  9686. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9687. }
  9688. return Result;
  9689. }
  9690. //---------------------------------------------------------------------------
  9691. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9692. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9693. {
  9694. if (CanOperateOnFoundFiles(ATerminal))
  9695. {
  9696. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9697. FOnFileOperationFinished = OnFileOperationFinished;
  9698. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9699. }
  9700. }
  9701. //---------------------------------------------------------------------------
  9702. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9703. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9704. {
  9705. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9706. }
  9707. //---------------------------------------------------------------------------
  9708. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9709. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9710. {
  9711. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9712. }
  9713. //---------------------------------------------------------------------------
  9714. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9715. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9716. {
  9717. if (CanOperateOnFoundFiles(ATerminal))
  9718. {
  9719. for (int Index = 0; Index < FileList->Count; Index++)
  9720. {
  9721. UnicodeString FileName = FileList->Strings[Index];
  9722. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9723. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9724. OnFileOperationFinished(osRemote, FileName, true);
  9725. }
  9726. }
  9727. }
  9728. //---------------------------------------------------------------------------
  9729. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9730. {
  9731. FFileFindTerminal = Terminal;
  9732. ShowFileFindDialog(
  9733. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9734. DoDownloadFoundFiles, DoEditFoundFiles);
  9735. }
  9736. //---------------------------------------------------------------------------
  9737. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9738. {
  9739. FTaskbarList = TaskbarList;
  9740. }
  9741. //---------------------------------------------------------------------------
  9742. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9743. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9744. {
  9745. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9746. if (Index >= 0)
  9747. {
  9748. if (Button == mbLeft)
  9749. {
  9750. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9751. // If switching tab (switching session) takes long enough for user
  9752. // to actually release the button, "mouse down" is still raised,
  9753. // but we do not get "mouse up" event, so dragging is not cancelled,
  9754. // prevent that by not beginning dragging in the first place.
  9755. int Key = ((GetSystemMetrics(SM_SWAPBUTTON) == 0) ? VK_LBUTTON : VK_RBUTTON);
  9756. if (IsKeyPressed(Key))
  9757. {
  9758. // when user clicks the "+", we get mouse down only after the session
  9759. // is closed, when new session tab is already on X:Y, so dragging
  9760. // starts, prevent that
  9761. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9762. {
  9763. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9764. if (Terminal != NULL)
  9765. {
  9766. SessionsPageControl->BeginDrag(false);
  9767. }
  9768. }
  9769. }
  9770. }
  9771. else if (Button == mbMiddle)
  9772. {
  9773. // ignore middle-click for "New session tab" and the last local-local tab
  9774. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9775. if ((Session != NULL) && CanCloseSession(Session))
  9776. {
  9777. CloseSessionTab(Index);
  9778. }
  9779. }
  9780. }
  9781. }
  9782. //---------------------------------------------------------------------------
  9783. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9784. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9785. {
  9786. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9787. // do not allow dropping on the "+" tab
  9788. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9789. if ((TargetTerminal != NULL) &&
  9790. (SessionsPageControl->ActivePage->PageIndex != Index))
  9791. {
  9792. Configuration->Usage->Inc(L"SessionTabMoves");
  9793. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9794. // but we want to actually prevent that to avoid flicker
  9795. SessionsPageControl->ActivePage->PageIndex = Index;
  9796. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9797. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9798. }
  9799. }
  9800. //---------------------------------------------------------------------------
  9801. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9802. TObject * Sender, TObject * Source, int X, int Y,
  9803. TDragState /*State*/, bool & Accept)
  9804. {
  9805. Accept = (Sender == Source);
  9806. if (Accept)
  9807. {
  9808. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9809. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9810. // do not allow dragging to the "+" tab
  9811. Accept = (Terminal != NULL);
  9812. }
  9813. }
  9814. //---------------------------------------------------------------------------
  9815. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9816. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9817. {
  9818. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9819. if (Index < 0)
  9820. {
  9821. Effect = DROPEFFECT_NONE;
  9822. }
  9823. else
  9824. {
  9825. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9826. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9827. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9828. {
  9829. Effect = DROPEFFECT_NONE;
  9830. }
  9831. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9832. {
  9833. Effect = DROPEFFECT_COPY;
  9834. }
  9835. }
  9836. }
  9837. //---------------------------------------------------------------------------
  9838. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9839. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9840. {
  9841. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9842. // do not allow dropping on local-local, disconnected and the "+" tab
  9843. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9844. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9845. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9846. {
  9847. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9848. if (!IsFileControl(DropSourceControl, osRemote))
  9849. {
  9850. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9851. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9852. // Why don't we use Terminal->CurrentDirectory directly?
  9853. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9854. // do not force queue, drag drop
  9855. false, true);
  9856. }
  9857. }
  9858. }
  9859. //---------------------------------------------------------------------------
  9860. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9861. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9862. {
  9863. // Downloads are handled in RemoteFileControlDDEnd
  9864. if (!IsFileControl(DropSourceControl, osRemote))
  9865. {
  9866. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9867. Terminal->CurrentDirectory,
  9868. // force queue, drag drop
  9869. true, true);
  9870. }
  9871. }
  9872. //---------------------------------------------------------------------------
  9873. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9874. {
  9875. FShowing = false;
  9876. // Do not save empty workspace
  9877. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9878. {
  9879. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9880. UnicodeString Name = WorkspaceName();
  9881. DoSaveWorkspace(
  9882. Name, DataList.get(),
  9883. !Configuration->DisablePasswordStoring &&
  9884. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9885. WinConfiguration->LastStoredSession = Name;
  9886. }
  9887. }
  9888. //---------------------------------------------------------------------------
  9889. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9890. {
  9891. TManagedTerminal * ManagedTerminal =
  9892. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9893. if (ManagedTerminal != NULL)
  9894. {
  9895. ManagedTerminal->DirectoryLoaded = Now();
  9896. }
  9897. }
  9898. //---------------------------------------------------------------------------
  9899. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9900. TListItem * /*Item*/, bool /*Selected*/)
  9901. {
  9902. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9903. switch (DirView->LastSelectMethod)
  9904. {
  9905. case smKeyboard:
  9906. Configuration->Usage->Inc(L"KeyboardSelections");
  9907. break;
  9908. case smMouse:
  9909. Configuration->Usage->Inc(L"MouseSelections");
  9910. break;
  9911. }
  9912. }
  9913. //---------------------------------------------------------------------------
  9914. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9915. {
  9916. if (Color != 0)
  9917. {
  9918. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9919. }
  9920. else
  9921. {
  9922. return FSessionTabImageIndex;
  9923. }
  9924. }
  9925. //---------------------------------------------------------------------------
  9926. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9927. {
  9928. TPngImageCollectionItem * Item =
  9929. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9930. FSessionColors->AddPng(Item->PngImage);
  9931. return FSessionColors->Count - 1;
  9932. }
  9933. //---------------------------------------------------------------------------
  9934. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9935. {
  9936. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9937. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  9938. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  9939. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9940. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9941. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  9942. }
  9943. //---------------------------------------------------------------------------
  9944. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  9945. {
  9946. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  9947. }
  9948. //---------------------------------------------------------------------------
  9949. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  9950. {
  9951. switch (Side)
  9952. {
  9953. case osLocal:
  9954. return FNewLocalTabTabImageIndex;
  9955. case osRemote:
  9956. case osCurrent:
  9957. return FNewRemoteTabTabImageIndex;
  9958. default:
  9959. DebugFail();
  9960. return -1;
  9961. }
  9962. }
  9963. //---------------------------------------------------------------------------
  9964. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9965. {
  9966. for (int Index = 0; Index < Control->ControlCount; Index++)
  9967. {
  9968. TControl * AControl = Control->Controls[Index];
  9969. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9970. if (WinControl != NULL)
  9971. {
  9972. CollectItemsWithTextDisplayMode(WinControl);
  9973. }
  9974. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9975. if (Toolbar != NULL)
  9976. {
  9977. // we care for top-level items only
  9978. TTBCustomItem * Items = Toolbar->Items;
  9979. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9980. {
  9981. TTBCustomItem * Item = Items->Items[ItemIndex];
  9982. if (((Item->DisplayMode == nbdmImageAndText) ||
  9983. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9984. EligibleForImageDisplayMode(Item))
  9985. {
  9986. FItemsWithTextDisplayMode.insert(Item);
  9987. }
  9988. }
  9989. }
  9990. }
  9991. }
  9992. //---------------------------------------------------------------------------
  9993. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9994. {
  9995. return true;
  9996. }
  9997. //---------------------------------------------------------------------------
  9998. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9999. {
  10000. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  10001. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  10002. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  10003. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  10004. bool Result = true;
  10005. while (Result && (i != FItemsWithTextDisplayMode.end()))
  10006. {
  10007. TTBCustomItem * Item = *i;
  10008. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  10009. if (Label != NULL)
  10010. {
  10011. // optimization
  10012. if (Label->Visible == SelectiveToolbarText)
  10013. {
  10014. Result = false;
  10015. }
  10016. else
  10017. {
  10018. Label->Visible = SelectiveToolbarText;
  10019. }
  10020. }
  10021. else
  10022. {
  10023. // optimization
  10024. if (Item->DisplayMode == DisplayMode)
  10025. {
  10026. Result = false;
  10027. }
  10028. else
  10029. {
  10030. Item->DisplayMode = DisplayMode;
  10031. }
  10032. }
  10033. ++i;
  10034. }
  10035. UpdateNewTabTab();
  10036. return Result;
  10037. }
  10038. //---------------------------------------------------------------------------
  10039. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  10040. {
  10041. DebugFail();
  10042. }
  10043. //---------------------------------------------------------------------------
  10044. bool __fastcall TCustomScpExplorerForm::IsBusy()
  10045. {
  10046. // Among other, a lock level is non zero, while directory is being loaded,
  10047. // even when it happens as a result of dirview navigation,
  10048. // i.e. when TNonVisualDataModule is NOT FBusy.
  10049. // That's why the TNonVisualDataModule::GetBusy calls this method.
  10050. // Among other this prevents a panel auto update to occur while
  10051. // directory is changing.
  10052. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  10053. }
  10054. //---------------------------------------------------------------------------
  10055. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  10056. {
  10057. // While the window is disabled, we still seem to process menu shortcuts at least,
  10058. // so stop it at least here.
  10059. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  10060. bool Result;
  10061. if (Allowed == aaUpdate)
  10062. {
  10063. Result = true;
  10064. }
  10065. else
  10066. {
  10067. Result = !NonVisualDataModule->Busy;
  10068. if (Result && (Allowed == aaShortCut))
  10069. {
  10070. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  10071. Action->Update();
  10072. Result = Action->Enabled;
  10073. }
  10074. }
  10075. return Result;
  10076. }
  10077. //---------------------------------------------------------------------------
  10078. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  10079. {
  10080. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  10081. }
  10082. //---------------------------------------------------------------------------
  10083. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  10084. {
  10085. // This is somewhat redundant to LockWindow() call from
  10086. // TTerminalManager::TerminalReadDirectoryProgress.
  10087. // But disabling window is known not to block keyboard shorcuts
  10088. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  10089. if (Busy > 0)
  10090. {
  10091. if (NonVisualDataModule->Busy)
  10092. {
  10093. State = false;
  10094. }
  10095. else
  10096. {
  10097. NonVisualDataModule->StartBusy();
  10098. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  10099. LockWindow();
  10100. }
  10101. }
  10102. else if (Busy < 0)
  10103. {
  10104. UnlockWindow();
  10105. NonVisualDataModule->EndBusy();
  10106. }
  10107. else
  10108. {
  10109. State = NonVisualDataModule->Busy;
  10110. }
  10111. }
  10112. //---------------------------------------------------------------------------
  10113. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  10114. {
  10115. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  10116. if (Index >= 0)
  10117. {
  10118. TPopupMenu * PopupMenu = NULL;
  10119. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10120. if (Session != NULL)
  10121. {
  10122. SessionsPageControl->ActivePageIndex = Index;
  10123. if (DebugAlwaysTrue(SessionTabSwitched()))
  10124. {
  10125. // copied from TControl.WMContextMenu
  10126. SendCancelMode(SessionsPageControl);
  10127. // explicit popup instead of using PopupMenu property
  10128. // to avoid menu to popup somewhere within SessionTabSwitched above,
  10129. // while connecting yet not-connected session and hence
  10130. // allowing an access to commands over not-completely connected session
  10131. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  10132. }
  10133. }
  10134. else
  10135. {
  10136. if (SupportsLocalBrowser())
  10137. {
  10138. PopupMenu = NonVisualDataModule->NewTabPopup;
  10139. }
  10140. }
  10141. if (PopupMenu != NULL)
  10142. {
  10143. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  10144. PopupMenu->PopupComponent = SessionsPageControl;
  10145. PopupMenu->Popup(Point.x, Point.y);
  10146. }
  10147. }
  10148. Handled = true;
  10149. }
  10150. //---------------------------------------------------------------------------
  10151. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  10152. {
  10153. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  10154. }
  10155. //---------------------------------------------------------------------------
  10156. void __fastcall TCustomScpExplorerForm::ChangePassword()
  10157. {
  10158. Configuration->Usage->Inc(L"PasswordChanges");
  10159. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  10160. ChangePasswordData->DisableAuthentationsExceptPassword();
  10161. ChangePasswordData->ChangePassword = true;
  10162. std::unique_ptr<TTerminal> ChangePasswordSession(
  10163. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  10164. ChangePasswordSession->AutoReadDirectory = false;
  10165. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  10166. {
  10167. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  10168. }
  10169. else
  10170. {
  10171. Configuration->Usage->Inc(L"PasswordChangesFailed");
  10172. }
  10173. }
  10174. //---------------------------------------------------------------------------
  10175. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  10176. {
  10177. // No nice way to assert SSH2
  10178. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  10179. }
  10180. //---------------------------------------------------------------------------
  10181. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  10182. {
  10183. TOperationVisualizer OperationVisualizer;
  10184. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  10185. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  10186. }
  10187. //---------------------------------------------------------------------------
  10188. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  10189. {
  10190. TForm::ChangeScale(M, D);
  10191. int APixelsPerInch = GetControlPixelsPerInch(this);
  10192. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  10193. try
  10194. {
  10195. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  10196. }
  10197. __finally
  10198. {
  10199. GUIConfiguration->ChangeResourceModule(ResourceModule);
  10200. }
  10201. }
  10202. //---------------------------------------------------------------------------
  10203. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  10204. {
  10205. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  10206. }
  10207. //---------------------------------------------------------------------
  10208. class TPublicControl : public TControl
  10209. {
  10210. friend class TCustomScpExplorerForm;
  10211. };
  10212. //---------------------------------------------------------------------------
  10213. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  10214. {
  10215. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  10216. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  10217. }
  10218. //---------------------------------------------------------------------------
  10219. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  10220. {
  10221. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  10222. OpenBookmark(Side, Bookmark);
  10223. }
  10224. //---------------------------------------------------------------------------
  10225. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  10226. {
  10227. DoBookmarkClick(osLocal, Sender);
  10228. }
  10229. //---------------------------------------------------------------------------
  10230. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  10231. {
  10232. DoBookmarkClick(osRemote, Sender);
  10233. }
  10234. //---------------------------------------------------------------------------
  10235. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  10236. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  10237. {
  10238. if (BookmarkList != NULL)
  10239. {
  10240. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  10241. if (IsLocalBrowserMode())
  10242. {
  10243. Side = osLocal;
  10244. }
  10245. if (!WinConfiguration->UseLocationProfiles)
  10246. {
  10247. std::unique_ptr<TStringList> Directories(new TStringList());
  10248. Directories->CaseSensitive = (Side == osRemote);
  10249. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10250. {
  10251. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10252. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  10253. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  10254. {
  10255. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10256. Item->Caption = EscapeHotkey(Directory);
  10257. Item->ShortCut = Bookmark->ShortCut;
  10258. Item->OnClick = OnBookmarkClick;
  10259. Item->Tag = reinterpret_cast<int>(Bookmark);
  10260. Directories->Add(Directory);
  10261. Menu->Add(Item.release());
  10262. }
  10263. }
  10264. }
  10265. else
  10266. {
  10267. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10268. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10269. {
  10270. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10271. if (!Bookmark->Node.IsEmpty())
  10272. {
  10273. Folders->Add(Bookmark->Node);
  10274. }
  10275. }
  10276. for (int Index = 0; Index < Folders->Count; Index++)
  10277. {
  10278. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10279. Item->Caption = Folders->Strings[Index];
  10280. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10281. Folders->Objects[Index] = Item.get();
  10282. Menu->Add(Item.release());
  10283. }
  10284. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10285. {
  10286. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10287. TTBCustomItem * Parent;
  10288. if (!Bookmark->Node.IsEmpty())
  10289. {
  10290. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10291. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10292. }
  10293. else
  10294. {
  10295. Parent = Menu;
  10296. }
  10297. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10298. Item->Caption = Bookmark->Name;
  10299. Item->ShortCut = Bookmark->ShortCut;
  10300. Item->OnClick = OnBookmarkClick;
  10301. Item->Tag = reinterpret_cast<int>(Bookmark);
  10302. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10303. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10304. {
  10305. // No hint for location profiles that are actually mere "bookmarks"
  10306. }
  10307. else
  10308. {
  10309. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10310. UnicodeString LongHint;
  10311. if (!Bookmark->Local.IsEmpty())
  10312. {
  10313. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10314. }
  10315. if (!Bookmark->Remote.IsEmpty())
  10316. {
  10317. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10318. }
  10319. AddToList(Hint, LongHint, L"|");
  10320. Item->Hint = Hint;
  10321. }
  10322. Parent->Add(Item.release());
  10323. }
  10324. }
  10325. AddMenuSeparator(Menu);
  10326. }
  10327. }
  10328. //---------------------------------------------------------------------------
  10329. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10330. {
  10331. Menu->Clear();
  10332. std::unique_ptr<TTBCustomItem> Item;
  10333. Item.reset(new TTBXItem(Owner));
  10334. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10335. Item->Options = Item->Options << tboDefault;
  10336. Menu->Add(Item.release());
  10337. AddMenuSeparator(Menu);
  10338. if (Terminal != NULL)
  10339. {
  10340. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10341. }
  10342. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10343. Item.reset(new TTBXItem(Owner));
  10344. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10345. Menu->Add(Item.release());
  10346. }
  10347. //---------------------------------------------------------------------------
  10348. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10349. {
  10350. bool Result = true;
  10351. bool Remote = !IsSideLocalBrowser(Side);
  10352. try
  10353. {
  10354. UnicodeString APath = Path;
  10355. if (Remote)
  10356. {
  10357. Terminal->ExceptionOnFail = true;
  10358. }
  10359. else
  10360. {
  10361. APath = ExpandEnvironmentVariables(Path);
  10362. }
  10363. try
  10364. {
  10365. DirView(Side)->Path = APath;
  10366. }
  10367. __finally
  10368. {
  10369. if (Remote)
  10370. {
  10371. Terminal->ExceptionOnFail = false;
  10372. }
  10373. }
  10374. }
  10375. catch (Exception & E)
  10376. {
  10377. if (!Remote || Terminal->Active)
  10378. {
  10379. ShowExtendedExceptionEx(Terminal, &E);
  10380. Result = false;
  10381. }
  10382. else
  10383. {
  10384. throw;
  10385. }
  10386. }
  10387. return Result;
  10388. }
  10389. //---------------------------------------------------------------------------
  10390. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10391. {
  10392. // Make sure there some feedback even when loading tiny local folders
  10393. TInstantOperationVisualizer Visualizer;
  10394. DirView(Side)->ReloadDirectory();
  10395. }
  10396. //---------------------------------------------------------------------------
  10397. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10398. {
  10399. if (Index == SessionsPageControl->TabIndex)
  10400. {
  10401. CloseTab();
  10402. }
  10403. else
  10404. {
  10405. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10406. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10407. TTerminalManager * Manager = TTerminalManager::Instance();
  10408. if (!Terminal->Active)
  10409. {
  10410. Manager->FreeTerminal(Terminal);
  10411. }
  10412. else
  10413. {
  10414. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10415. if (CanCloseQueue(Queue))
  10416. {
  10417. Manager->FreeTerminal(Terminal);
  10418. }
  10419. }
  10420. }
  10421. }
  10422. //---------------------------------------------------------------------------
  10423. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10424. {
  10425. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10426. if (Session == NULL)
  10427. {
  10428. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10429. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10430. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10431. //P = SessionsPageControl->ClientToScreen(P);
  10432. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10433. }
  10434. else
  10435. {
  10436. CloseSessionTab(Index);
  10437. }
  10438. }
  10439. //---------------------------------------------------------------------------
  10440. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10441. {
  10442. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10443. {
  10444. TInstantOperationVisualizer Visualizer;
  10445. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10446. // before we create a new data object
  10447. ClipboardClear();
  10448. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10449. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10450. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10451. // Intermediate variable to WORKAROUND a crash in compiler
  10452. TObjectList * ClipboardFakeMonitors =
  10453. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10454. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10455. if (FClipboardDragDropFilesEx.get() == NULL)
  10456. {
  10457. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10458. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10459. }
  10460. FClipboardDragDropFilesEx->FileList->Clear();
  10461. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10462. FClipboardDragDropFilesEx->CopyToClipboard();
  10463. FClipboardTerminal = Terminal;
  10464. // Need full paths, as cwd can be different once files are pasted
  10465. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10466. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10467. {
  10468. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10469. }
  10470. }
  10471. }
  10472. //---------------------------------------------------------------------------
  10473. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10474. {
  10475. if (OpenClipboard(0))
  10476. {
  10477. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10478. CloseClipboard();
  10479. }
  10480. }
  10481. //---------------------------------------------------------------------------
  10482. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10483. {
  10484. return !FClipboardFakeDirectory.IsEmpty();
  10485. }
  10486. //---------------------------------------------------------------------------
  10487. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10488. {
  10489. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10490. FClipboardFakeDirectory = UnicodeString();
  10491. FClipboardTerminal = NULL;
  10492. if (FDownloadingFromClipboard)
  10493. {
  10494. // We are called by the monitor, so attempt to release it would deadlock
  10495. FClipboardFakeMonitorsPendingReset = true;
  10496. }
  10497. else
  10498. {
  10499. FClipboardFakeMonitors.reset(NULL);
  10500. }
  10501. }
  10502. //---------------------------------------------------------------------------
  10503. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10504. {
  10505. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10506. {
  10507. ClipboardStop();
  10508. }
  10509. }
  10510. //---------------------------------------------------------------------------
  10511. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10512. {
  10513. TTerminalManager * Manager = TTerminalManager::Instance();
  10514. Manager->ActiveSession = FClipboardTerminal;
  10515. TTransferOperationParam Params;
  10516. Params.TargetDirectory = TargetDirectory;
  10517. Params.DragDrop = DragDrop;
  10518. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10519. }
  10520. //---------------------------------------------------------------------------
  10521. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10522. {
  10523. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10524. // (as it pumps a Windows message queue while being shutted down)
  10525. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10526. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10527. {
  10528. // Can fail as it can be e.g. locked by AV, so we retry that later
  10529. bool Removed = RemoveDir(ApiPath(FileName));
  10530. try
  10531. {
  10532. if (!NonVisualDataModule->Busy)
  10533. {
  10534. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10535. FClipboardFakeMonitorsPendingReset = false;
  10536. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10537. TAutoFlag Flag(FDownloadingFromClipboard);
  10538. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10539. }
  10540. }
  10541. __finally
  10542. {
  10543. if (!Removed)
  10544. {
  10545. RemoveDir(ApiPath(FileName));
  10546. }
  10547. }
  10548. }
  10549. }
  10550. //---------------------------------------------------------------------------
  10551. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10552. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10553. {
  10554. TFileMasks::TParams MaskParams;
  10555. MaskParams.Size = Size;
  10556. MaskParams.Modification = Modification;
  10557. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10558. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10559. // TUnixDirView::ItemFullFileName returns trailing backslash for directories, what filemask cannot deal with
  10560. if (!Local && Directory)
  10561. {
  10562. FileName = UnixExcludeTrailingBackslash(FileName);
  10563. }
  10564. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10565. {
  10566. bool ImplicitMatch;
  10567. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10568. !ImplicitMatch)
  10569. {
  10570. Color = Iter->Color;
  10571. break;
  10572. }
  10573. }
  10574. }
  10575. //---------------------------------------------------------------------------
  10576. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10577. {
  10578. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10579. if (!DirView->IsEditing())
  10580. {
  10581. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10582. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10583. // while searching
  10584. if (Key == VK_BACK)
  10585. {
  10586. if (!FIncrementalSearch.IsEmpty())
  10587. {
  10588. if (FIncrementalSearch.Length() == 1)
  10589. {
  10590. ResetIncrementalSearch();
  10591. }
  10592. else
  10593. {
  10594. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10595. IncrementalSearch(NewText, false, false);
  10596. }
  10597. Key = 0;
  10598. }
  10599. }
  10600. }
  10601. }
  10602. //---------------------------------------------------------------------------
  10603. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10604. {
  10605. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10606. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10607. {
  10608. // Filter control sequences.
  10609. // When not searching yet, prefer use of the space for toggling file selection
  10610. // (so we cannot incrementally search for a string starting with a space).
  10611. if ((Key > VK_SPACE) ||
  10612. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10613. {
  10614. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10615. Key = 0;
  10616. }
  10617. }
  10618. }
  10619. //---------------------------------------------------------------------------
  10620. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10621. {
  10622. if (!FIncrementalSearch.IsEmpty())
  10623. {
  10624. FIncrementalSearch = L"";
  10625. DirView(osCurrent)->UpdateStatusBar();
  10626. }
  10627. }
  10628. //---------------------------------------------------------------------------
  10629. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10630. {
  10631. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10632. if (Item == NULL)
  10633. {
  10634. MessageBeep(MB_ICONHAND);
  10635. }
  10636. else
  10637. {
  10638. TCustomDirView * ADirView = DirView(osCurrent);
  10639. {
  10640. TAutoNestingCounter Guard(FIncrementalSearching);
  10641. ADirView->FocusItem(Item);
  10642. if (ADirView->NortonLike == nlOff)
  10643. {
  10644. if ((ADirView->Selected != Item) ||
  10645. (ADirView->SelCount > 1))
  10646. {
  10647. ADirView->ClearSelection();
  10648. ADirView->Selected = Item;
  10649. }
  10650. }
  10651. }
  10652. FIncrementalSearch = Text;
  10653. Item->MakeVisible(false);
  10654. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10655. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10656. ADirView->UpdateStatusBar();
  10657. }
  10658. }
  10659. //---------------------------------------------------------------------------
  10660. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10661. {
  10662. int Index = Item->Index;
  10663. if (!Reverse)
  10664. {
  10665. Index++;
  10666. if (Index >= Item->Owner->Count)
  10667. {
  10668. Index = 0;
  10669. }
  10670. }
  10671. else
  10672. {
  10673. Index--;
  10674. if (Index < 0)
  10675. {
  10676. Index = Item->Owner->Count - 1;
  10677. }
  10678. }
  10679. return Item->Owner->Item[Index];
  10680. }
  10681. //---------------------------------------------------------------------------
  10682. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10683. {
  10684. TCustomDirView * ADirView = DirView(osCurrent);
  10685. if (ADirView->Items->Count == 0)
  10686. {
  10687. return NULL;
  10688. }
  10689. else
  10690. {
  10691. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10692. TListItem * Item = CurrentItem;
  10693. if (SkipCurrent)
  10694. {
  10695. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10696. }
  10697. while (true)
  10698. {
  10699. bool Matches = false;
  10700. switch (WinConfiguration->PanelSearch)
  10701. {
  10702. case isNameStartOnly:
  10703. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item).SubString(1, Text.Length()), Text);
  10704. break;
  10705. case isName:
  10706. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item), Text);
  10707. break;
  10708. case isAll:
  10709. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10710. int Index = 0;
  10711. while ((Index < ColCount) && !Matches)
  10712. {
  10713. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10714. if (Visible)
  10715. {
  10716. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10717. }
  10718. Index++;
  10719. }
  10720. break;
  10721. }
  10722. if (Matches)
  10723. {
  10724. return Item;
  10725. }
  10726. Item = GetNextFile(Item, Reverse);
  10727. if (Item == CurrentItem)
  10728. {
  10729. return NULL;
  10730. }
  10731. }
  10732. }
  10733. }
  10734. //---------------------------------------------------------------------------
  10735. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10736. {
  10737. ResetIncrementalSearch();
  10738. }
  10739. //---------------------------------------------------------------------------
  10740. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10741. {
  10742. if (Message.CharCode == VK_TAB)
  10743. {
  10744. if (!FIncrementalSearch.IsEmpty())
  10745. {
  10746. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10747. bool Reverse = Shift.Contains(ssShift);
  10748. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10749. Message.Result = 1;
  10750. return;
  10751. }
  10752. }
  10753. else if (Message.CharCode == VK_ESCAPE)
  10754. {
  10755. if (!FIncrementalSearch.IsEmpty())
  10756. {
  10757. ResetIncrementalSearch();
  10758. Message.Result = 1;
  10759. return;
  10760. }
  10761. }
  10762. TForm::Dispatch(&Message);
  10763. }
  10764. //---------------------------------------------------------------------------
  10765. void __fastcall TCustomScpExplorerForm::Deactivate()
  10766. {
  10767. TForm::Deactivate();
  10768. // E.g. when switching to an internal editor window
  10769. ResetIncrementalSearch();
  10770. }
  10771. //---------------------------------------------------------------------------
  10772. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10773. {
  10774. // When switching to another application
  10775. ResetIncrementalSearch();
  10776. }
  10777. //---------------------------------------------------------------------------
  10778. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10779. {
  10780. ResetIncrementalSearch();
  10781. }
  10782. //---------------------------------------------------------------------------
  10783. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10784. {
  10785. if (FIncrementalSearching <= 0)
  10786. {
  10787. ResetIncrementalSearch();
  10788. }
  10789. }
  10790. //---------------------------------------------------------------------------
  10791. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10792. {
  10793. CountClicksForWindowPrint(this);
  10794. }
  10795. //---------------------------------------------------------------------------
  10796. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10797. {
  10798. CenterReconnectToolbar();
  10799. }
  10800. //---------------------------------------------------------------------------
  10801. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10802. {
  10803. if (RemoteDirView->ItemFocused != NULL)
  10804. {
  10805. RemoteDirView->ItemFocused->Selected = true;
  10806. }
  10807. }
  10808. //---------------------------------------------------------------------------
  10809. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10810. {
  10811. if (!FQueueStatusUpdating)
  10812. {
  10813. bool Refresh;
  10814. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10815. {
  10816. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10817. }
  10818. else
  10819. {
  10820. if (FQueueFileList->GetCount() > 0)
  10821. {
  10822. FQueueFileList->Clear();
  10823. Refresh = true;
  10824. }
  10825. }
  10826. int Count = FQueueFileList->GetCount();
  10827. if (QueueFileList->Items->Count != Count)
  10828. {
  10829. DebugAssert(Refresh);
  10830. QueueFileList->Items->Count = Count;
  10831. }
  10832. if (Refresh)
  10833. {
  10834. QueueFileList->Invalidate();
  10835. }
  10836. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10837. }
  10838. }
  10839. //---------------------------------------------------------------------------
  10840. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10841. {
  10842. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10843. }
  10844. //---------------------------------------------------------------------------
  10845. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10846. {
  10847. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10848. // See TSynchronizeChecklistDialog::UpdateTimer
  10849. if (QueueFileList->Items->Count > 0)
  10850. {
  10851. QueueFileList->Items->Item[0]->MakeVisible(false);
  10852. }
  10853. UpdateQueueFileList();
  10854. }
  10855. //---------------------------------------------------------------------------
  10856. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10857. {
  10858. Active = QueueView3->Focused() || QueueFileList->Focused();
  10859. }
  10860. //---------------------------------------------------------------------------
  10861. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10862. {
  10863. QueueLabelUpdateStatus();
  10864. }
  10865. //---------------------------------------------------------------------------
  10866. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10867. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10868. {
  10869. int State = FQueueFileList->GetState(Item->Index);
  10870. if (State == qfsProcessed)
  10871. {
  10872. Sender->Canvas->Font->Color = clGrayText;
  10873. }
  10874. }
  10875. //---------------------------------------------------------------------------
  10876. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10877. {
  10878. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10879. QueueFileList->Column[0]->Width =
  10880. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10881. }
  10882. //---------------------------------------------------------------------------
  10883. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10884. {
  10885. QueueFileListColumnAutoSize();
  10886. }
  10887. //---------------------------------------------------------------------------
  10888. void __fastcall TCustomScpExplorerForm::CloseApp()
  10889. {
  10890. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10891. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10892. // So we clear the flag here.
  10893. DebugAssert(NonVisualDataModule->Busy);
  10894. NonVisualDataModule->EndBusy();
  10895. try
  10896. {
  10897. Close();
  10898. }
  10899. __finally
  10900. {
  10901. NonVisualDataModule->StartBusy();
  10902. }
  10903. }
  10904. //---------------------------------------------------------------------------
  10905. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10906. {
  10907. return (Terminal != NULL) && Terminal->Active;
  10908. }
  10909. //---------------------------------------------------------------------------
  10910. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  10911. {
  10912. return (ManagedSession != NULL);
  10913. }
  10914. //---------------------------------------------------------------------------
  10915. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10916. {
  10917. return IsActiveTerminal(Terminal);
  10918. }
  10919. //---------------------------------------------------------------------------
  10920. void TCustomScpExplorerForm::LocalLocalCopy(
  10921. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  10922. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  10923. {
  10924. DebugFail();
  10925. }
  10926. //---------------------------------------------------------------------------
  10927. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  10928. {
  10929. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  10930. }
  10931. //---------------------------------------------------------------------------
  10932. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  10933. {
  10934. // 1) Changing tab list triggers the OnResize.
  10935. // 2) Is called in TForm constructor, when we are not ready to call SessionListChanged (particularly when starting with colored session)
  10936. if (!FUpdatingSessionTabs && (FSessionColors != NULL))
  10937. {
  10938. // UpdateTabsCaptionTruncation should be enough
  10939. SessionListChanged(true);
  10940. }
  10941. }
  10942. //---------------------------------------------------------------------------
  10943. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  10944. {
  10945. if (ASession == ManagedSession)
  10946. {
  10947. UpdateSession(ASession);
  10948. }
  10949. TSessionData * Data = ASession->StateData;
  10950. UnicodeString Result;
  10951. if (Side == osLocal)
  10952. {
  10953. Result = Data->LocalDirectory;
  10954. }
  10955. else if (DebugAlwaysTrue(Side == osOther))
  10956. {
  10957. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  10958. }
  10959. return Result;
  10960. }
  10961. //---------------------------------------------------------------------------
  10962. UnicodeString TCustomScpExplorerForm::GetTabHintSessionDetails(TManagedTerminal * ASession)
  10963. {
  10964. UnicodeString Title = TTerminalManager::Instance()->GetSessionTitle(ASession, false);
  10965. UnicodeString Result = ASession->SessionData->DefaultSessionName;
  10966. if (Title == Result)
  10967. {
  10968. Result = EmptyStr;
  10969. }
  10970. else
  10971. {
  10972. Result += L"\n";
  10973. }
  10974. return Result;
  10975. }
  10976. //---------------------------------------------------------------------------
  10977. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  10978. {
  10979. UnicodeString Result = GetTabHintSessionDetails(ASession);
  10980. if (!ASession->Active)
  10981. {
  10982. Result += LoadStr(STATUS_NOT_CONNECTED2);
  10983. }
  10984. else
  10985. {
  10986. Result += GetSessionPath(ASession, osRemote);
  10987. }
  10988. return Result;
  10989. }
  10990. //---------------------------------------------------------------------------
  10991. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  10992. {
  10993. return LoadStr(NEW_REMOTE_TAB_HINT);
  10994. }
  10995. //---------------------------------------------------------------------------
  10996. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  10997. {
  10998. if (Index >= 0)
  10999. {
  11000. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  11001. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  11002. if (TabSession == NULL)
  11003. {
  11004. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  11005. if (NewTabTabCaption != TabSheet->Caption)
  11006. {
  11007. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  11008. }
  11009. }
  11010. else
  11011. {
  11012. TTerminalManager * Manager = TTerminalManager::Instance();
  11013. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  11014. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  11015. }
  11016. }
  11017. }
  11018. //---------------------------------------------------------------------------
  11019. typedef std::vector<TListItem *> TListItemsVector;
  11020. struct TCalculateSizeOperation
  11021. {
  11022. TCalculateSizeStats Stats;
  11023. TListItemsVector ListItems;
  11024. size_t Index;
  11025. };
  11026. //---------------------------------------------------------------------------
  11027. void TCustomScpExplorerForm::CalculateDirectorySizes(TOperationSide Side)
  11028. {
  11029. TCalculateSizeOperation CalculateSizeOperation;
  11030. Side = GetSide(Side);
  11031. TCustomDirView * ADirView = DirView(Side);
  11032. bool LocalSide = IsSideLocalBrowser(Side);
  11033. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, LocalSide, NULL, true));
  11034. std::unique_ptr<TStrings> FileList(new TStringList());
  11035. for (int Index = 0; Index < AllFileList->Count; Index++)
  11036. {
  11037. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  11038. if (ADirView->ItemIsDirectory(Item))
  11039. {
  11040. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  11041. CalculateSizeOperation.ListItems.push_back(Item);
  11042. }
  11043. }
  11044. CalculateSizeOperation.Index = 0;
  11045. __int64 Size = 0;
  11046. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation);
  11047. FCalculateSizeOperation = &CalculateSizeOperation;
  11048. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  11049. FOnFileOperationFinished = DirectorySizeCalculated;
  11050. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation);
  11051. FPrimaryOperation = foCalculateSize;
  11052. TCalculatedSizes CalculatedSizes;
  11053. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  11054. if (LocalSide)
  11055. {
  11056. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  11057. FProgressSide = Side;
  11058. ManagedSession->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  11059. }
  11060. else
  11061. {
  11062. TCalculateSizeParams Params;
  11063. Params.Stats = &CalculateSizeOperation.Stats;
  11064. Params.UseCache = true;
  11065. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  11066. }
  11067. }
  11068. //---------------------------------------------------------------------------
  11069. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(TOperationSide Side, const UnicodeString & FileName, bool Success)
  11070. {
  11071. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  11072. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  11073. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  11074. // optimization
  11075. bool GotExpectedItem =
  11076. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  11077. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  11078. int Index = -1;
  11079. if (DebugAlwaysTrue(GotExpectedItem))
  11080. {
  11081. Index = FCalculateSizeOperation->Index;
  11082. FCalculateSizeOperation->Index++;
  11083. }
  11084. else
  11085. {
  11086. TListItemsVector::iterator I =
  11087. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  11088. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  11089. {
  11090. Index = I - FCalculateSizeOperation->ListItems.begin();
  11091. }
  11092. }
  11093. if (DebugAlwaysTrue(Index >= 0) && Success)
  11094. {
  11095. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  11096. ADirView->SetItemCalculatedSize(Item, Size);
  11097. }
  11098. }
  11099. //---------------------------------------------------------------------------
  11100. void __fastcall TCustomScpExplorerForm::MessageDockRequestDock(TObject *, TTBCustomDockableWindow *, bool & Accept)
  11101. {
  11102. Accept = false;
  11103. }
  11104. //---------------------------------------------------------------------------