CustomScpExplorer.cpp 404 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097
  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. #define WM_PASTE_FILES (WM_WINSCP_USER + 14)
  55. static const int SessionPanelCount = 3;
  56. //---------------------------------------------------------------------------
  57. class TMutexGuard
  58. {
  59. public:
  60. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  61. int Timeout = 5000)
  62. {
  63. FMutex = NULL;
  64. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  65. if (WaitResult == WAIT_TIMEOUT)
  66. {
  67. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  68. }
  69. else
  70. {
  71. FMutex = AMutex;
  72. }
  73. }
  74. ~TMutexGuard()
  75. {
  76. if (FMutex != NULL)
  77. {
  78. ReleaseMutex(FMutex);
  79. }
  80. }
  81. private:
  82. HANDLE FMutex;
  83. };
  84. //---------------------------------------------------------------------------
  85. class TWindowLock
  86. {
  87. public:
  88. TWindowLock(TCustomScpExplorerForm * Form) :
  89. FForm(Form)
  90. {
  91. FForm->LockWindow();
  92. }
  93. ~TWindowLock()
  94. {
  95. FForm->UnlockWindow();
  96. }
  97. private:
  98. TCustomScpExplorerForm * FForm;
  99. };
  100. //---------------------------------------------------------------------------
  101. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  102. FForm(Form)
  103. {
  104. FForm->BatchStart(FBatchStorage);
  105. }
  106. //---------------------------------------------------------------------------
  107. TAutoBatch::~TAutoBatch()
  108. {
  109. FForm->BatchEnd(FBatchStorage);
  110. }
  111. //---------------------------------------------------------------------------
  112. struct TTransferOperationParam
  113. {
  114. TTransferOperationParam();
  115. UnicodeString TargetDirectory;
  116. bool Temp;
  117. bool DragDrop;
  118. int Options;
  119. TAutoSwitch Queue;
  120. };
  121. //---------------------------------------------------------------------------
  122. TTransferOperationParam::TTransferOperationParam()
  123. {
  124. Temp = false;
  125. DragDrop = false;
  126. Options = 0;
  127. Queue = asAuto;
  128. }
  129. //---------------------------------------------------------------------------
  130. class TTransferPresetNoteData : public TObject
  131. {
  132. public:
  133. UnicodeString Message;
  134. };
  135. //---------------------------------------------------------------------------
  136. class TTerminalNoteData : public TObject
  137. {
  138. public:
  139. TManagedTerminal * Terminal;
  140. };
  141. //---------------------------------------------------------------------------
  142. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  143. FFormRestored(false),
  144. TForm(Owner)
  145. {
  146. AddStartupSequence(L"F");
  147. FInvalid = false;
  148. FCurrentSide = osRemote;
  149. FEverShown = false;
  150. FDocks = new TList();
  151. InitControls();
  152. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  153. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  154. NonVisualDataModule->ScpExplorer = this;
  155. RestoreParams();
  156. ConfigurationChanged();
  157. RemoteDirView->Invalidate();
  158. FAutoOperation = false;
  159. FOnFileOperationFinished = NULL;
  160. FPrimaryOperation = foNone;
  161. FForceExecution = false;
  162. FIgnoreNextDialogChar = 0;
  163. FErrorList = NULL;
  164. FSynchronizeProgressForm = NULL;
  165. FProgressForm = NULL;
  166. FRefreshLocalDirectory = false;
  167. FRefreshRemoteDirectory = false;
  168. FDDExtMapFile = NULL;
  169. // CreateMutexW keeps failing with ERROR_NOACCESS
  170. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  171. DebugAssert(FDDExtMutex != NULL);
  172. FDDTargetControl = NULL;
  173. FDelayedDeletionTimer = NULL;
  174. FDelayedDeletionList = new TStringList();
  175. FDDFileList = NULL;
  176. FPendingTempSpaceWarn = false;
  177. FCapturedLog = NULL;
  178. FDragDropOperation = false;
  179. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  180. FAllowTransferPresetAutoSelect = true;
  181. FSessionChanging = false;
  182. FCopyParamDefault = L"";
  183. FSynchronizeController = NULL;
  184. FPendingQueueActionItem = NULL;
  185. FLockLevel = 0;
  186. FLockSuspendLevel = 0;
  187. FDisabledOnLockSuspend = false;
  188. FAlternativeDelete = false;
  189. FTrayIcon = new ::TTrayIcon(0);
  190. FTrayIcon->OnClick = TrayIconClick;
  191. FMaxQueueLength = 0;
  192. FLastContextPopupScreenPoint = TPoint(-1, -1);
  193. FTransferResumeList = NULL;
  194. FDeletedFiles = NULL;
  195. FMoveToQueue = false;
  196. StandaloneOperation = false;
  197. FOnFeedSynchronizeError = NULL;
  198. FOnSynchronizeAbort = NULL;
  199. FSynchronizeTerminal = NULL;
  200. FNeedSession = false;
  201. FDoNotIdleCurrentTerminal = 0;
  202. FIncrementalSearching = 0;
  203. FIncrementalSearchHaveNext = false;
  204. FQueueFileList.reset(new TQueueFileList());
  205. FProgressSide = osCurrent;
  206. FCalculateSizeOperation = NULL;
  207. FDownloadingFromClipboard = false;
  208. FClipboardFakeMonitorsPendingReset = false;
  209. FHiContrastTheme = NULL;
  210. FEditorManager = new TEditorManager();
  211. FEditorManager->OnFileChange = ExecutedFileChanged;
  212. FEditorManager->OnFileReload = ExecutedFileReload;
  213. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  214. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  215. FLocalEditors = new TList();
  216. FQueueStatus = NULL;
  217. FQueueStatusSection = new TCriticalSection();
  218. FQueueStatusInvalidated = false;
  219. FQueueItemInvalidated = false;
  220. FQueueStatusUpdating = false;
  221. FQueueActedItem = NULL;
  222. FQueueController = new TQueueController(QueueView3);
  223. FQueueScrollOnDragOver = new TListViewScrollOnDragOver(QueueView3, true);
  224. FUserActionTimer = new TTimer(this);
  225. FUserActionTimer->Enabled = false;
  226. FUserActionTimer->Interval = 10;
  227. FUserActionTimer->OnTimer = UserActionTimer;
  228. FNotes = new TStringList();
  229. FNoteTimer = new TTimer(this);
  230. FNoteTimer->Enabled = false;
  231. FNoteTimer->OnTimer = NoteTimer;
  232. FOnNoteClick = NULL;
  233. FOle32Library = LoadLibrary(L"Ole32.dll");
  234. FDragMoveCursor = FOle32Library != NULL ?
  235. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  236. UseSystemSettings(this);
  237. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  238. static_cast<TObject*>(GetComponent(fcTransferList)));
  239. DebugAssert(TransferList != NULL);
  240. FTransferListHoverIndex = -1;
  241. TransferList->OnChange = TransferListChange;
  242. TransferList->OnDrawItem = TransferListDrawItem;
  243. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  244. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  245. UseDesktopFont(SessionsPageControl);
  246. UpdateSessionsPageControlHeight();
  247. UseDesktopFont(RemoteDirView);
  248. UseDesktopFont(RemoteDriveView);
  249. UseDesktopFont(QueueView3);
  250. UseDesktopFont(QueueFileList);
  251. UseDesktopFont(QueueLabel);
  252. UseDesktopFont(RemoteStatusBar);
  253. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  254. QueueSplitter->ShowHint = true;
  255. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  256. QueueFileListSplitter->ShowHint = true;
  257. RemotePanelSplitter->ShowHint = true;
  258. UpdateImages();
  259. FCustomCommandMenu = CreateTBXPopupMenu(this);
  260. FCustomCommandLocalFileList = NULL;
  261. FCustomCommandRemoteFileList = NULL;
  262. FSessionColors = new TPngImageList(this);
  263. FSessionColors->ColorDepth = cd32Bit;
  264. AddFixedSessionImages();
  265. SessionsPageControl->Images = FSessionColors;
  266. UpdateQueueLabel();
  267. CheckStoreTransition();
  268. CreateHiddenWindow();
  269. StartUpdates();
  270. // Start loading drive data, so that they are ready once we need them for StartCreationDirectoryMonitorsOnEachDrive
  271. // (necessary for "explorer" only, as "commander" loads it for its drive menu)
  272. DriveInfo->NeedData();
  273. }
  274. //---------------------------------------------------------------------------
  275. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  276. {
  277. FInvalid = true;
  278. ReleaseHiContrastTheme();
  279. if (FClipboardTerminal != NULL)
  280. {
  281. ClipboardClear(); // implies ClipboardStop
  282. }
  283. // this has to be one of the very first things to do
  284. StopUpdateThread();
  285. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  286. TTerminalManager::Instance()->ScpExplorer = NULL;
  287. delete FCustomCommandLocalFileList;
  288. delete FCustomCommandRemoteFileList;
  289. delete FCustomCommandMenu;
  290. delete FTrayIcon;
  291. FTrayIcon = NULL;
  292. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  293. delete FEditorManager;
  294. ForceCloseLocalEditors();
  295. delete FLocalEditors;
  296. HideFileFindDialog();
  297. if (FDelayedDeletionTimer)
  298. {
  299. DoDelayedDeletion(NULL);
  300. SAFE_DESTROY(FDelayedDeletionTimer);
  301. }
  302. SAFE_DESTROY(FDelayedDeletionList);
  303. // sometimes we do not get DDEnd so the list is not released
  304. SAFE_DESTROY(FDDFileList);
  305. DebugAssert(FSynchronizeController == NULL);
  306. CloseHandle(FDDExtMutex);
  307. FDDExtMutex = NULL;
  308. FreeLibrary(FOle32Library);
  309. FOle32Library = NULL;
  310. FDragMoveCursor = NULL;
  311. DebugAssert(!FErrorList);
  312. if (FEverShown)
  313. {
  314. // when window is never shown (like when running command-line operation),
  315. // particularly window site is not restored correctly (BoundsRect value set
  316. // in RestoreForm gets lost during handle allocation), so we do not want
  317. // it to be stored
  318. StoreParams();
  319. }
  320. ManagedSession = NULL;
  321. Queue = NULL;
  322. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  323. NonVisualDataModule->ScpExplorer = NULL;
  324. delete FQueueScrollOnDragOver;
  325. FQueueScrollOnDragOver = NULL;
  326. delete FQueueController;
  327. FQueueController = NULL;
  328. delete FQueueStatusSection;
  329. FQueueStatusSection = NULL;
  330. delete FQueueStatus;
  331. FQueueStatus = NULL;
  332. delete FUserActionTimer;
  333. FUserActionTimer = NULL;
  334. delete FNoteTimer;
  335. delete FNotes;
  336. delete FNoteData;
  337. SAFE_DESTROY(FDocks);
  338. SessionsPageControl->Images = NULL;
  339. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  340. SAFE_DESTROY(FSessionColors);
  341. SAFE_DESTROY(FSessionsDragDropFilesEx);
  342. SAFE_DESTROY(FHistoryMenu[0][0]);
  343. SAFE_DESTROY(FHistoryMenu[0][1]);
  344. SAFE_DESTROY(FHistoryMenu[1][0]);
  345. SAFE_DESTROY(FHistoryMenu[1][1]);
  346. if (FHiddenWindow != NULL)
  347. {
  348. DestroyWindow(FHiddenWindow);
  349. FHiddenWindow = NULL;
  350. }
  351. }
  352. //---------------------------------------------------------------------------
  353. void TCustomScpExplorerForm::ReleaseHiContrastTheme()
  354. {
  355. if (FHiContrastTheme != NULL)
  356. {
  357. ReleaseTBXTheme(FHiContrastTheme);
  358. FHiContrastTheme = NULL;
  359. }
  360. }
  361. //---------------------------------------------------------------------------
  362. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  363. {
  364. std::unique_ptr<TSessionData> Data;
  365. if (!Session.IsEmpty())
  366. {
  367. bool DefaultsOnly;
  368. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  369. }
  370. TTerminalManager * Manager = TTerminalManager::Instance();
  371. for (int Index = 0; Index < Manager->Count; Index++)
  372. {
  373. TTerminal * Terminal = Manager->Sessions[Index];
  374. if (Session.IsEmpty() ||
  375. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  376. {
  377. if (Path.IsEmpty())
  378. {
  379. Terminal->ClearCaches();
  380. }
  381. else
  382. {
  383. Terminal->DirectoryModified(Path, true);
  384. }
  385. }
  386. }
  387. // We should flag a pending refresh for the background terminals or busy foreground terminals
  388. if (HasActiveTerminal() &&
  389. CanCommandLineFromAnotherInstance() &&
  390. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  391. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  392. {
  393. Terminal->ReloadDirectory();
  394. }
  395. }
  396. //---------------------------------------------------------------------------
  397. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  398. {
  399. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  400. AppLogFmt(L"Received copy data message with size %d", (int(CopyData->cbData)));
  401. size_t MessageSize = sizeof(TCopyDataMessage);
  402. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  403. if (Result)
  404. {
  405. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  406. Result = (Message.Version == TCopyDataMessage::Version1);
  407. if (Result)
  408. {
  409. switch (Message.Command)
  410. {
  411. case TCopyDataMessage::CommandCanCommandLine:
  412. Result = CanCommandLineFromAnotherInstance();
  413. if (Result)
  414. {
  415. AppLog(L"Can process command-line from other instance");
  416. }
  417. else
  418. {
  419. AppLog(L"Cannot process command-line from other instance");
  420. }
  421. break;
  422. case TCopyDataMessage::CommandCommandLine:
  423. {
  424. UnicodeString CommandLine(Message.CommandLine);
  425. Result = CommandLineFromAnotherInstance(CommandLine);
  426. }
  427. break;
  428. case TCopyDataMessage::RefreshPanel:
  429. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  430. break;
  431. case TCopyDataMessage::MainWindowCheck:
  432. Result = true;
  433. break;
  434. default:
  435. Result = false;
  436. break;
  437. }
  438. }
  439. }
  440. Message.Result = Result ? 1 : 0;
  441. }
  442. //---------------------------------------------------------------------------
  443. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  444. {
  445. WNDCLASS WindowClass = {0};
  446. WindowClass.lpfnWndProc = DefWindowProc;
  447. WindowClass.hInstance = HInstance;
  448. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  449. FHiddenWindow = NULL;
  450. if (RegisterClass(&WindowClass))
  451. {
  452. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  453. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  454. }
  455. }
  456. //---------------------------------------------------------------------------
  457. bool __fastcall TCustomScpExplorerForm::CanConsole()
  458. {
  459. return
  460. HasActiveTerminal() &&
  461. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  462. }
  463. //---------------------------------------------------------------------------
  464. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  465. {
  466. bool Result = Visible && !NonVisualDataModule->Busy;
  467. return Result;
  468. }
  469. //---------------------------------------------------------------------------
  470. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  471. const UnicodeString & CommandLine)
  472. {
  473. TProgramParams Params(CommandLine);
  474. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  475. if (Result)
  476. {
  477. NonVisualDataModule->StartBusy();
  478. try
  479. {
  480. // this action is initiated from another process,
  481. // so it's likely that our window is not visible,
  482. // and user won't see what is going on
  483. Application->BringToFront();
  484. // reload sessions as we may be asked to open a session
  485. // just stored by another instance
  486. StoredSessions->Reload();
  487. UnicodeString SessionName = Params.Param[1];
  488. std::unique_ptr<TObjectList> DataList(new TObjectList());
  489. try
  490. {
  491. UnicodeString DownloadFile; // unused
  492. int Flags = GetCommandLineParseUrlFlags(&Params);
  493. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  494. }
  495. catch (EAbort &)
  496. {
  497. // Happens when opening session in PuTTY and in other situations,
  498. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  499. // to process the command-line on its own (or to pass it to yet another instance)
  500. }
  501. if (DataList->Count > 0)
  502. {
  503. TTerminalManager * Manager = TTerminalManager::Instance();
  504. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  505. }
  506. }
  507. __finally
  508. {
  509. NonVisualDataModule->EndBusy();
  510. }
  511. }
  512. return Result;
  513. }
  514. //---------------------------------------------------------------------------
  515. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  516. {
  517. TManagedTerminal * Result;
  518. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  519. {
  520. Result = FManagedSession;
  521. }
  522. else
  523. {
  524. Result = NULL;
  525. }
  526. return Result;
  527. }
  528. //---------------------------------------------------------------------------
  529. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  530. {
  531. if (FManagedSession != value)
  532. {
  533. TWindowLock Lock(this);
  534. SessionChanging();
  535. DoSetManagedSession(value, false);
  536. }
  537. }
  538. //---------------------------------------------------------------------------
  539. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  540. {
  541. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  542. FManagedSession = value;
  543. {
  544. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  545. FAllowTransferPresetAutoSelect = false;
  546. TAutoFlag SessionChangingFlag(FSessionChanging);
  547. SessionChanged(Replace);
  548. }
  549. if (Terminal != NULL)
  550. {
  551. TransferPresetAutoSelect();
  552. }
  553. // Update app and tab titles, prevented above by FSessionChanging.
  554. UpdateControls();
  555. }
  556. //---------------------------------------------------------------------------
  557. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  558. {
  559. FEditorManager->ProcessFiles(FileTerminalReplaced, value);
  560. DoSetManagedSession(value, true);
  561. }
  562. //---------------------------------------------------------------------------
  563. void __fastcall TCustomScpExplorerForm::SessionChanging()
  564. {
  565. if (ManagedSession != NULL)
  566. {
  567. UpdateSession(ManagedSession);
  568. }
  569. }
  570. //---------------------------------------------------------------------------
  571. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  572. {
  573. if (ManagedSession != NULL)
  574. {
  575. UpdateSessionColor(static_cast<TColor>(ManagedSession->StateData->Color));
  576. }
  577. if (!TTerminalManager::Instance()->Updating)
  578. {
  579. SessionListChanged();
  580. }
  581. if (Terminal != NULL)
  582. {
  583. TObject * State = NULL;
  584. if (WinConfiguration->PreservePanelState)
  585. {
  586. State = Terminal->RemoteExplorerState;
  587. DirView(osRemote)->AnnounceState(State);
  588. }
  589. try
  590. {
  591. if (Replaced)
  592. {
  593. RemoteDirView->ReplaceTerminal(Terminal);
  594. }
  595. else
  596. {
  597. RemoteDirView->Terminal = Terminal;
  598. }
  599. if (Terminal->Active)
  600. {
  601. Terminal->RefreshDirectory();
  602. }
  603. if (State != NULL)
  604. {
  605. DirView(osRemote)->RestoreState(State);
  606. }
  607. }
  608. __finally
  609. {
  610. DirView(osRemote)->AnnounceState(NULL);
  611. }
  612. }
  613. else
  614. {
  615. RemoteDirView->Terminal = NULL;
  616. }
  617. InitStatusBar();
  618. UpdateTransferList();
  619. // Update panels Enable state before refreshing the labels
  620. UpdateControls();
  621. }
  622. //---------------------------------------------------------------------------
  623. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  624. {
  625. if (Queue != value)
  626. {
  627. if (FQueue != NULL)
  628. {
  629. FQueue->OnListUpdate = NULL;
  630. FQueue->OnQueueItemUpdate = NULL;
  631. }
  632. FQueue = value;
  633. if (FQueue != NULL)
  634. {
  635. DebugAssert(FQueue->OnListUpdate == NULL);
  636. FQueue->OnListUpdate = QueueListUpdate;
  637. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  638. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  639. }
  640. QueueChanged();
  641. }
  642. }
  643. //---------------------------------------------------------------------------
  644. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  645. TListItem * Item)
  646. {
  647. if (FQueueActedItem == Item)
  648. {
  649. FQueueActedItem = NULL;
  650. if ((QueueView3->PopupMenu != NULL) &&
  651. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  652. {
  653. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  654. ReleaseCapture();
  655. }
  656. }
  657. if (Item->Data == FPendingQueueActionItem)
  658. {
  659. FPendingQueueActionItem = NULL;
  660. }
  661. }
  662. //---------------------------------------------------------------------------
  663. bool TCustomScpExplorerForm::IsAnythingQueued()
  664. {
  665. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  666. }
  667. //---------------------------------------------------------------------------
  668. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  669. {
  670. {
  671. TGuard Guard(FQueueStatusSection);
  672. FQueueStatusInvalidated = false;
  673. if (FQueue != NULL)
  674. {
  675. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  676. }
  677. }
  678. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  679. {
  680. FMaxQueueLength = FQueueStatus->Count;
  681. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  682. }
  683. {
  684. TAutoFlag Flag(FQueueStatusUpdating);
  685. FQueueController->UpdateQueueStatus(FQueueStatus);
  686. }
  687. UpdateQueueFileList();
  688. SetQueueProgress();
  689. UpdateQueueView();
  690. bool IsEmpty = !IsAnythingQueued();
  691. if (IsEmpty && (Terminal != NULL))
  692. {
  693. TOnceDoneOperation OnceDoneOperation =
  694. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  695. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  696. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  697. {
  698. FQueue->Enabled = false;
  699. }
  700. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  701. {
  702. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  703. }
  704. }
  705. }
  706. //---------------------------------------------------------------------------
  707. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  708. {
  709. UnicodeString Result;
  710. if (FQueueStatus != NULL)
  711. {
  712. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  713. {
  714. TFileOperationProgressType * ProgressData =
  715. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  716. if ((ProgressData != NULL) && ProgressData->InProgress)
  717. {
  718. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  719. }
  720. }
  721. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  722. {
  723. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  724. }
  725. }
  726. return Result;
  727. }
  728. //---------------------------------------------------------------------------
  729. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  730. {
  731. ComponentVisible[fcQueueView] =
  732. !IsLocalBrowserMode() &&
  733. ((WinConfiguration->QueueView.Show == qvShow) ||
  734. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  735. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  736. }
  737. //---------------------------------------------------------------------------
  738. void __fastcall TCustomScpExplorerForm::QueueChanged()
  739. {
  740. if (FQueueStatus != NULL)
  741. {
  742. // UpdateFileList implementation relies on the status being recreated when session changes
  743. delete FQueueStatus;
  744. FQueueStatus = NULL;
  745. }
  746. UpdateQueueStatus(true);
  747. }
  748. //---------------------------------------------------------------------------
  749. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  750. {
  751. if (FQueue == Queue)
  752. {
  753. FQueueStatusInvalidated = true;
  754. }
  755. }
  756. //---------------------------------------------------------------------------
  757. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  758. TQueueItem * Item)
  759. {
  760. if (FQueue == Queue)
  761. {
  762. TGuard Guard(FQueueStatusSection);
  763. // this may be running in parallel with QueueChanged
  764. if (FQueueStatus != NULL)
  765. {
  766. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  767. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  768. {
  769. FRefreshLocalDirectory = (QueueItem == NULL) ||
  770. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  771. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  772. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  773. }
  774. if (QueueItem != NULL)
  775. {
  776. QueueItem->UserData = (void*)true;
  777. FQueueItemInvalidated = true;
  778. }
  779. }
  780. }
  781. }
  782. //---------------------------------------------------------------------------
  783. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  784. {
  785. // during standalone editing, we have no way to see/control queue,
  786. // so we have to always popup prompts automatically
  787. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  788. }
  789. //---------------------------------------------------------------------------
  790. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  791. {
  792. if (FQueueStatus != NULL)
  793. {
  794. bool QueueAutoPopup = IsQueueAutoPopup();
  795. bool NeedRefresh = FQueueController->NeedRefresh();
  796. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  797. FQueueItemInvalidated = false;
  798. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  799. bool Updated = false;
  800. TQueueItemProxy * QueueItem;
  801. bool WasUserAction;
  802. for (int Index = 0; Index < Limit; Index++)
  803. {
  804. bool Update = false;
  805. QueueItem = FQueueStatus->Items[Index];
  806. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  807. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  808. (FPendingQueueActionItem == NULL))
  809. {
  810. FPendingQueueActionItem = QueueItem;
  811. FUserActionTimer->Enabled = true;
  812. }
  813. if ((bool)QueueItem->UserData)
  814. {
  815. QueueItem->UserData = (void*)false;
  816. QueueItem->Update();
  817. Updated = true;
  818. Update = true;
  819. }
  820. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  821. {
  822. Update = true;
  823. }
  824. if (Update)
  825. {
  826. FQueueController->RefreshQueueItem(QueueItem);
  827. }
  828. }
  829. if (Updated)
  830. {
  831. NonVisualDataModule->UpdateNonVisibleActions();
  832. SetQueueProgress();
  833. UpdateQueueFileList();
  834. }
  835. }
  836. }
  837. //---------------------------------------------------------------------------
  838. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  839. {
  840. if (FTaskbarList != NULL)
  841. {
  842. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  843. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  844. }
  845. }
  846. //---------------------------------------------------------------------------
  847. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  848. {
  849. if (Progress >= 0)
  850. {
  851. if (FTaskbarList != NULL)
  852. {
  853. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  854. }
  855. }
  856. else
  857. {
  858. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  859. }
  860. }
  861. //---------------------------------------------------------------------------
  862. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  863. {
  864. int OverallProgress;
  865. // FProgressForm is null when this is called from SetQueueProgress
  866. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  867. {
  868. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  869. }
  870. else if (!ProgressData->IsIndeterminate())
  871. {
  872. OverallProgress = ProgressData->OverallProgress();
  873. }
  874. else
  875. {
  876. OverallProgress = -1;
  877. }
  878. SetTaskbarListProgressValue(OverallProgress);
  879. }
  880. //---------------------------------------------------------------------------
  881. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  882. {
  883. TTerminalManager::Instance()->QueueStatusUpdated();
  884. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  885. {
  886. // Do not animate task bar button, when items are queued, but processing is disabled
  887. if ((FQueueStatus != NULL) && (FQueueStatus->ActivePrimaryCount > 0))
  888. {
  889. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  890. {
  891. TFileOperationProgressType * ProgressData = NULL;
  892. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  893. {
  894. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  895. }
  896. if ((ProgressData != NULL) &&
  897. ProgressData->InProgress)
  898. {
  899. SetTaskbarListProgressValue(ProgressData);
  900. }
  901. else
  902. {
  903. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  904. }
  905. }
  906. else
  907. {
  908. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  909. }
  910. }
  911. else
  912. {
  913. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  914. }
  915. }
  916. UpdateQueueLabel();
  917. }
  918. //---------------------------------------------------------------------------
  919. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  920. {
  921. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  922. if (FQueueStatus != NULL)
  923. {
  924. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  925. if (ActiveAndPendingPrimaryCount > 0)
  926. {
  927. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  928. }
  929. }
  930. QueueLabel->Caption = Caption;
  931. }
  932. //---------------------------------------------------------------------------
  933. void TCustomScpExplorerForm::CheckStoreTransition()
  934. {
  935. if (WinConfiguration->StoreTransition == stInit)
  936. {
  937. if (IsUWP())
  938. {
  939. WinConfiguration->StoreTransition = stStoreFresh;
  940. AppLog(L"Fresh UWP/store installation");
  941. }
  942. else
  943. {
  944. WinConfiguration->StoreTransition = stStandard;
  945. AppLog(L"Fresh standard installation");
  946. }
  947. }
  948. else if ((WinConfiguration->StoreTransition == stStandard) && IsUWP())
  949. {
  950. WinConfiguration->StoreTransition = stStoreMigrated;
  951. WinConfiguration->FirstRun = WinConfiguration->FirstRun + L"=>" + StandardDatestamp();
  952. AppLog(L"Standard installation migrated to store installation");
  953. }
  954. if (WinConfiguration->StoreTransition == stStoreMigrated)
  955. {
  956. TTBXToolbar * Toolbar = new TTBXToolbar(MessageDock);
  957. Toolbar->Parent = MessageDock;
  958. Toolbar->FullSize = true;
  959. Toolbar->Color = Application->HintColor;
  960. TTBXLabelItem * LabelItem;
  961. LabelItem = new TTBXLabelItem(Toolbar);
  962. // Convert to explicit RGB "black" color, to make sure it is contrasting even in Dark theme
  963. // (TTBXOfficeXPTheme.GetStandardColor would convert clWindow to "white" in dark theme)
  964. LabelItem->FontSettings->Color = static_cast<TColor>(GetSysColor(clWindow));
  965. LabelItem->Margin = ScaleByTextHeight(MessageDock, 2); // Is this automatically rescaled?
  966. LabelItem->Caption = LoadStr(STORE_MIGRATION_THANKS);
  967. Toolbar->Items->Add(LabelItem);
  968. TTBXCustomItem * LinkItem = new TTBXItem(Toolbar);
  969. LinkItem->FontSettings->Color = LinkColor;
  970. LinkItem->Caption = LoadStr(STORE_MIGRATION_LEARN);
  971. LinkItem->OnClick = StoreTransitionLinkClick;
  972. Toolbar->Items->Add(LinkItem);
  973. AddMenuSeparator(Toolbar->Items);
  974. TTBXCustomItem * CloseItem = new TTBXItem(Toolbar);
  975. CloseItem->FontSettings->Color = static_cast<TColor>(GetSysColor(clWindow));
  976. CloseItem->Caption = LoadStr(MESSAGE_DISMISS);
  977. CloseItem->OnClick = StoreTransitionCloseClick;
  978. Toolbar->Items->Add(CloseItem);
  979. GiveTBItemPriority(CloseItem);
  980. }
  981. }
  982. //---------------------------------------------------------------------------
  983. void __fastcall TCustomScpExplorerForm::StoreTransitionLinkClick(TObject *)
  984. {
  985. ShowHelp(HELP_STORE_TRANSITION);
  986. }
  987. //---------------------------------------------------------------------------
  988. void __fastcall TCustomScpExplorerForm::StoreTransitionCloseClick(TObject * Sender)
  989. {
  990. TTBCustomItem * Item = DebugNotNull(dynamic_cast<TTBCustomItem *>(Sender));
  991. TTBXToolbar * Toolbar = DebugNotNull(dynamic_cast<TTBXToolbar *>(DebugNotNull(DebugNotNull(Item->Parent)->ParentComponent)));
  992. delete Toolbar;
  993. WinConfiguration->StoreTransition = stStoreAcknowledged;
  994. UpdateControls();
  995. }
  996. //---------------------------------------------------------------------------
  997. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  998. {
  999. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  1000. static_cast<TComponent*>(GetComponent(fcTransferList)));
  1001. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  1002. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  1003. TransferList->Strings->BeginUpdate();
  1004. try
  1005. {
  1006. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  1007. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  1008. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  1009. if (FTransferDropDownHint.IsEmpty())
  1010. {
  1011. FTransferDropDownHint = TransferDropDown->Hint;
  1012. }
  1013. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  1014. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  1015. TransferDropDown->Text = StripHotkey(Name);
  1016. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  1017. (FTransferDropDownHint, StripHotkey(Name),
  1018. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  1019. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  1020. // update the label, otherwise when it is updated only on the first draw
  1021. // of the list, it is drawn "bold" for some reason
  1022. FTransferListHoverIndex = TransferList->ItemIndex;
  1023. UpdateTransferLabel();
  1024. }
  1025. __finally
  1026. {
  1027. TransferList->Strings->EndUpdate();
  1028. }
  1029. }
  1030. //---------------------------------------------------------------------------
  1031. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  1032. {
  1033. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  1034. DebugAssert(Toolbar != NULL);
  1035. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  1036. }
  1037. //---------------------------------------------------------------------------
  1038. void __fastcall TCustomScpExplorerForm::UpdateActions()
  1039. {
  1040. TForm::UpdateActions();
  1041. if (ComponentVisible[fcCustomCommandsBand])
  1042. {
  1043. UpdateCustomCommandsToolbar();
  1044. }
  1045. }
  1046. //---------------------------------------------------------------------------
  1047. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  1048. {
  1049. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  1050. }
  1051. //---------------------------------------------------------------------------
  1052. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  1053. {
  1054. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  1055. {
  1056. DirView->RowSelect = WinConfiguration->FullRowSelect;
  1057. // Without this, the panel turns gray and unused part of header turns black
  1058. DirView->Perform(CM_COLORCHANGED, 0, 0);
  1059. }
  1060. }
  1061. //---------------------------------------------------------------------------
  1062. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  1063. {
  1064. Color = GetBtnFaceColor();
  1065. DebugAssert(Configuration && RemoteDirView);
  1066. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1067. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  1068. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  1069. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  1070. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1071. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1072. RemoteDirView->AlwaysSortDirectoriesByName = WinConfiguration->AlwaysSortDirectoriesByName;
  1073. UpdateRowSelect(RemoteDirView);
  1074. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1075. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  1076. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  1077. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1078. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1079. UpdateSessionsPageControlHeight();
  1080. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1081. UpdateToolbarDisplayMode();
  1082. UpdateTransferList();
  1083. if (Terminal != NULL)
  1084. {
  1085. TransferPresetAutoSelect();
  1086. }
  1087. UpdateQueueView();
  1088. UpdateControls();
  1089. // this can be called even before constuctor finishes.
  1090. if (FEditorManager != NULL)
  1091. {
  1092. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  1093. }
  1094. // this can be called even before constuctor finishes.
  1095. if (FLocalEditors != NULL)
  1096. {
  1097. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  1098. {
  1099. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  1100. }
  1101. }
  1102. if (ComponentVisible[fcCustomCommandsBand])
  1103. {
  1104. UpdateCustomCommandsToolbar();
  1105. }
  1106. // show only when keeping queue items forever,
  1107. // otherwise, it's enough to have it in the context menu
  1108. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1109. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1110. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1111. {
  1112. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1113. FFileColorsCurrent = WinConfiguration->FileColors;
  1114. FileColorsChanged();
  1115. }
  1116. if (GetNewTabTab()->CaptionTruncation != GetNewTabTabCaptionTruncation())
  1117. {
  1118. SessionListChanged();
  1119. }
  1120. }
  1121. //---------------------------------------------------------------------------
  1122. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1123. {
  1124. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1125. DirView->Invalidate();
  1126. }
  1127. //---------------------------------------------------------------------------
  1128. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1129. {
  1130. DoFileColorsChanged(RemoteDirView);
  1131. }
  1132. //---------------------------------------------------------------------------
  1133. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1134. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1135. void * /*Arg*/)
  1136. {
  1137. if (Token != NULL)
  1138. {
  1139. TForm * Editor = dynamic_cast<TForm*>(Token);
  1140. DebugAssert(Editor != NULL);
  1141. ReconfigureEditorForm(Editor);
  1142. }
  1143. }
  1144. //---------------------------------------------------------------------------
  1145. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1146. {
  1147. WinConfiguration->DDTransferConfirmation = asOn;
  1148. }
  1149. //---------------------------------------------------------------------------
  1150. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1151. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1152. {
  1153. // noop
  1154. }
  1155. //---------------------------------------------------------------------------
  1156. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1157. {
  1158. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1159. }
  1160. //---------------------------------------------------------------------------
  1161. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1162. {
  1163. if (DoNotShowAgain)
  1164. {
  1165. if (DragDrop)
  1166. {
  1167. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1168. {
  1169. PopupTrayBalloon(
  1170. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1171. }
  1172. WinConfiguration->DDTransferConfirmation = asOff;
  1173. }
  1174. else
  1175. {
  1176. WinConfiguration->ConfirmTransferring = false;
  1177. }
  1178. }
  1179. else
  1180. {
  1181. // User explicitly unchecked "do not show again",
  1182. // so show him the dialog the next time
  1183. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1184. {
  1185. WinConfiguration->DDTransferConfirmation = asOn;
  1186. }
  1187. }
  1188. }
  1189. //---------------------------------------------------------------------------
  1190. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1191. TTransferDirection Direction, TTransferType Type, bool Temp,
  1192. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1193. bool Confirm, bool DragDrop, int Options)
  1194. {
  1195. bool Result = true;
  1196. DebugAssert(HasActiveTerminal());
  1197. // these parameters are known in advance
  1198. int Params =
  1199. FLAGMASK(Type == ttMove, cpDelete);
  1200. bool ToTemp = (Temp && (Direction == tdToLocal));
  1201. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1202. {
  1203. bool DisableNewerOnly =
  1204. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1205. ToTemp;
  1206. Options |=
  1207. FLAGMASK(ToTemp, coTemp) |
  1208. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1209. coDoNotShowAgain;
  1210. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1211. int CopyParamAttrs = (Direction == tdToRemote ?
  1212. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1213. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1214. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1215. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1216. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1217. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1218. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1219. if (Result)
  1220. {
  1221. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1222. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1223. }
  1224. }
  1225. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1226. {
  1227. Configuration->Usage->Inc(L"TransfersOnBackground");
  1228. // these parameter are known only after transfer dialog
  1229. Params |=
  1230. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1231. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1232. Result = false;
  1233. ClearTransferSourceSelection(Direction);
  1234. }
  1235. return Result;
  1236. }
  1237. //---------------------------------------------------------------------------
  1238. void TCustomScpExplorerForm::ClearOperationSelection(TOperationSide Side)
  1239. {
  1240. if (FOnFileOperationFinished != NULL)
  1241. {
  1242. FOnFileOperationFinished(Side, UnicodeString(), true);
  1243. }
  1244. else
  1245. {
  1246. if (HasDirView[Side])
  1247. {
  1248. DirView(Side)->SelectAll(smNone);
  1249. }
  1250. }
  1251. }
  1252. //---------------------------------------------------------------------------
  1253. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1254. {
  1255. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1256. ClearOperationSelection(Side);
  1257. }
  1258. //---------------------------------------------------------------------------
  1259. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1260. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1261. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1262. int Params)
  1263. {
  1264. DebugAssert(Queue != NULL);
  1265. bool SingleFile = false;
  1266. if (FileList->Count == 1)
  1267. {
  1268. if (Direction == tdToRemote)
  1269. {
  1270. UnicodeString FileName = FileList->Strings[0];
  1271. SingleFile = FileExists(ApiPath(FileName));
  1272. }
  1273. else
  1274. {
  1275. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1276. SingleFile = !File->IsDirectory;
  1277. }
  1278. }
  1279. TQueueItem * QueueItem;
  1280. if (Direction == tdToRemote)
  1281. {
  1282. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1283. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1284. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1285. }
  1286. else
  1287. {
  1288. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1289. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1290. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1291. }
  1292. AddQueueItem(Queue, QueueItem, Terminal);
  1293. }
  1294. //---------------------------------------------------------------------------
  1295. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1296. {
  1297. if (Queue->IsEmpty)
  1298. {
  1299. Terminal->QueueOperationStart = Now();
  1300. }
  1301. Queue->AddItem(QueueItem);
  1302. }
  1303. //---------------------------------------------------------------------------
  1304. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1305. {
  1306. }
  1307. //---------------------------------------------------------------------------
  1308. void __fastcall TCustomScpExplorerForm::InitControls()
  1309. {
  1310. DebugAssert(FDocks != NULL);
  1311. for (int Index = 0; Index < ComponentCount; Index++)
  1312. {
  1313. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1314. if ((Dock != NULL) && (Dock->Tag == 0))
  1315. {
  1316. FDocks->Add(Dock);
  1317. }
  1318. }
  1319. CollectItemsWithTextDisplayMode(this);
  1320. }
  1321. //---------------------------------------------------------------------------
  1322. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1323. {
  1324. ConfigurationChanged();
  1325. QueuePanel->Height =
  1326. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1327. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1328. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1329. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1330. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1331. QueueFileList->Height =
  1332. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1333. if (QueueFileList->Visible)
  1334. {
  1335. AdjustQueueLayout();
  1336. }
  1337. UpdateCustomCommandsToolbar();
  1338. }
  1339. //---------------------------------------------------------------------------
  1340. void __fastcall TCustomScpExplorerForm::StoreParams()
  1341. {
  1342. int APixelsPerInch = GetControlPixelsPerInch(this);
  1343. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1344. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1345. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1346. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1347. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1348. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1349. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1350. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1351. }
  1352. //---------------------------------------------------------------------------
  1353. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1354. {
  1355. if (!FFormRestored)
  1356. {
  1357. FFormRestored = true;
  1358. RestoreFormParams();
  1359. }
  1360. TForm::CreateParams(Params);
  1361. }
  1362. //---------------------------------------------------------------------------
  1363. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1364. {
  1365. DebugAssert(FDocks != NULL);
  1366. for (int Index = 0; Index < FDocks->Count; Index++)
  1367. {
  1368. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1369. }
  1370. }
  1371. //---------------------------------------------------------------------------
  1372. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1373. {
  1374. SetDockAllowDrag(true);
  1375. while (!ButtonsStr.IsEmpty())
  1376. {
  1377. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1378. UnicodeString Name = CutToChar(S, L'=', true);
  1379. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1380. {
  1381. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1382. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1383. {
  1384. while (!S.IsEmpty())
  1385. {
  1386. UnicodeString S2 = CutToChar(S, L',', true);
  1387. UnicodeString Name = CutToChar(S2, L':', true);
  1388. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1389. {
  1390. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1391. if (SameText(GetToolbarItemName(Item), Name))
  1392. {
  1393. // forward compatibility
  1394. UnicodeString S3 = CutToChar(S2, L':', true);
  1395. bool Visible = StrToIntDef(S3, 0);
  1396. Item->Visible = Visible;
  1397. }
  1398. }
  1399. }
  1400. }
  1401. }
  1402. }
  1403. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1404. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1405. }
  1406. //---------------------------------------------------------------------------
  1407. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1408. {
  1409. UnicodeString Result;
  1410. SetDockAllowDrag(true);
  1411. Result = ::GetToolbarsLayoutStr(this);
  1412. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1413. return Result;
  1414. }
  1415. //---------------------------------------------------------------------------
  1416. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1417. {
  1418. UnicodeString Result;
  1419. if (Item->Action != NULL)
  1420. {
  1421. Result = Item->Action->Name;
  1422. Result = RemoveSuffix(Result, L"Action", true);
  1423. }
  1424. else
  1425. {
  1426. Result = Item->Name;
  1427. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1428. Result = RemoveSuffix(Result, L"Item", true);
  1429. }
  1430. return Result;
  1431. }
  1432. //---------------------------------------------------------------------------
  1433. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1434. {
  1435. UnicodeString Result;
  1436. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1437. {
  1438. UnicodeString ToolbarStr;
  1439. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1440. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1441. {
  1442. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1443. {
  1444. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1445. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1446. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1447. // and save the changes here only.
  1448. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1449. {
  1450. UnicodeString Name = GetToolbarItemName(Item);
  1451. DebugAssert(Name.Pos(L"TBX") == 0);
  1452. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1453. }
  1454. }
  1455. }
  1456. if (!ToolbarStr.IsEmpty())
  1457. {
  1458. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1459. }
  1460. }
  1461. return Result;
  1462. }
  1463. //---------------------------------------------------------------------------
  1464. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1465. {
  1466. DebugAssert(FProgressForm == NULL);
  1467. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1468. bool AllowMoveToQueue = (FTransferResumeList != NULL) || (FDeletedFiles != NULL);
  1469. FProgressForm = new TProgressForm(Application, AllowMoveToQueue, AllowSkip, SynchronizeProgress);
  1470. FProgressForm->DeleteLocalToRecycleBin =
  1471. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1472. FProgressForm->DeleteRemoteToRecycleBin =
  1473. HasActiveTerminal() &&
  1474. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1475. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1476. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1477. LockWindow();
  1478. // Actually, do not know what hides the progress once the operation finishes
  1479. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1480. SetTaskbarListProgressState(TBPF_NORMAL);
  1481. }
  1482. //---------------------------------------------------------------------------
  1483. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1484. {
  1485. UnlockWindow();
  1486. SAFE_DESTROY(FProgressForm);
  1487. SetQueueProgress();
  1488. }
  1489. //---------------------------------------------------------------------------
  1490. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1491. TFileOperationProgressType & ProgressData)
  1492. {
  1493. // Download to temporary local directory
  1494. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1495. {
  1496. bool Continue = true;
  1497. FPendingTempSpaceWarn = false;
  1498. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1499. if (!Continue)
  1500. {
  1501. Abort();
  1502. }
  1503. }
  1504. // operation is being executed and we still didn't show up progress form
  1505. if (ProgressData.InProgress && !FProgressForm)
  1506. {
  1507. CreateProgressForm(NULL);
  1508. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1509. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1510. }
  1511. // operation is finished (or terminated), so we hide progress form
  1512. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1513. {
  1514. DestroyProgressForm();
  1515. if (ProgressData.Operation == foCalculateSize)
  1516. {
  1517. // When calculating size before transfer, the abort caused by
  1518. // cancel flag set due to "MoveToQueue" below,
  1519. // is not propagated back to us in ExecuteFileOperation,
  1520. // as it is silently swallowed.
  1521. // So we explicitly (re)throw it here.
  1522. if (FMoveToQueue)
  1523. {
  1524. Abort();
  1525. }
  1526. }
  1527. else
  1528. {
  1529. if ((ProgressData.Cancel == csContinue) &&
  1530. !FAutoOperation)
  1531. {
  1532. OperationComplete(ProgressData.StartTime);
  1533. }
  1534. }
  1535. }
  1536. if (FProgressForm &&
  1537. // Can happen only during synchronization, when ending one of the operations
  1538. ProgressData.InProgress)
  1539. {
  1540. FProgressForm->SetProgressData(ProgressData);
  1541. DebugAssert(ProgressData.InProgress);
  1542. SetTaskbarListProgressValue(&ProgressData);
  1543. if (FProgressForm->Cancel > csContinue)
  1544. {
  1545. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1546. if (FProgressForm->Cancel == csCancelFile)
  1547. {
  1548. FProgressForm->ClearCancel();
  1549. }
  1550. // cancel cancels even the move
  1551. FMoveToQueue = false;
  1552. }
  1553. else if (FProgressForm->MoveToQueue)
  1554. {
  1555. FMoveToQueue = true;
  1556. ProgressData.SetCancelAtLeast(csCancel);
  1557. }
  1558. if ((FTransferResumeList != NULL) &&
  1559. ProgressData.InProgress &&
  1560. ProgressData.IsTransfer() &&
  1561. !ProgressData.FullFileName.IsEmpty())
  1562. {
  1563. if ((FTransferResumeList->Count == 0) ||
  1564. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1565. {
  1566. // note that we do not recognize directories from files here
  1567. FTransferResumeList->Add(ProgressData.FullFileName);
  1568. }
  1569. }
  1570. }
  1571. }
  1572. //---------------------------------------------------------------------------
  1573. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1574. const TDateTime & StartTime)
  1575. {
  1576. if (GUIConfiguration->BeepOnFinish &&
  1577. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1578. {
  1579. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1580. DWORD Sound;
  1581. if (!ExtractFileExt(BeepSound).IsEmpty())
  1582. {
  1583. Sound = SND_FILENAME;
  1584. }
  1585. else
  1586. {
  1587. Sound = SND_ALIAS;
  1588. }
  1589. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1590. }
  1591. }
  1592. //---------------------------------------------------------------------------
  1593. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1594. TFileOperationProgressType & ProgressData)
  1595. {
  1596. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1597. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1598. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1599. FileOperationProgress(ProgressData);
  1600. }
  1601. //---------------------------------------------------------------------------
  1602. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1603. {
  1604. UnicodeString Result;
  1605. if (FProgressForm != NULL)
  1606. {
  1607. Result = FProgressForm->ProgressStr();
  1608. }
  1609. return Result;
  1610. }
  1611. //---------------------------------------------------------------------------
  1612. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1613. bool DragDrop)
  1614. {
  1615. return (!DragDrop && (DropSourceControl == NULL)) ||
  1616. (DropSourceControl == DirView(osOther));
  1617. }
  1618. //---------------------------------------------------------------------------
  1619. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1620. {
  1621. TCustomDirView * ADirView = DirView(Side);
  1622. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1623. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1624. // this can happen when local drive is unplugged in the middle of the operation
  1625. if (Item != NULL)
  1626. {
  1627. if (Unselect)
  1628. {
  1629. Item->Selected = false;
  1630. }
  1631. ADirView->MakeProgressVisible(Item);
  1632. }
  1633. return Item;
  1634. }
  1635. //---------------------------------------------------------------------------
  1636. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1637. TFileOperation Operation, TOperationSide Side,
  1638. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1639. TOnceDoneOperation & OnceDoneOperation)
  1640. {
  1641. if (!FAutoOperation)
  1642. {
  1643. // no selection on "/upload", form serves only as event handler
  1644. // (it is not displayed)
  1645. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1646. ((Operation == FPrimaryOperation) ||
  1647. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1648. {
  1649. TOperationSide DViewSide;
  1650. if ((FProgressSide != osCurrent) &&
  1651. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1652. {
  1653. // assume the operation is over the focused panel
  1654. DViewSide = FProgressSide;
  1655. }
  1656. else
  1657. {
  1658. DViewSide = Side;
  1659. }
  1660. if (FOnFileOperationFinished != NULL)
  1661. {
  1662. FOnFileOperationFinished(DViewSide, FileName, Success);
  1663. }
  1664. else
  1665. {
  1666. VisualiseOperationFinished(DViewSide, FileName, Success);
  1667. }
  1668. }
  1669. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1670. {
  1671. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1672. {
  1673. if (Side == osLocal)
  1674. {
  1675. Configuration->Usage->Inc(L"UploadedFiles");
  1676. }
  1677. else
  1678. {
  1679. Configuration->Usage->Inc(L"DownloadedFiles");
  1680. }
  1681. }
  1682. }
  1683. }
  1684. if (Success && (FSynchronizeController != NULL))
  1685. {
  1686. DebugAssert(!IsSideLocalBrowser(osRemote));
  1687. if (Operation == foCopy)
  1688. {
  1689. DebugAssert(Side == osLocal);
  1690. FSynchronizeController->LogOperation(soUpload, FileName);
  1691. }
  1692. else if (Operation == foDelete)
  1693. {
  1694. DebugAssert(Side == osRemote);
  1695. FSynchronizeController->LogOperation(soDelete, FileName);
  1696. }
  1697. }
  1698. if (FProgressForm)
  1699. {
  1700. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1701. }
  1702. }
  1703. //---------------------------------------------------------------------------
  1704. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1705. TFileOperation Operation, TOperationSide Side,
  1706. bool Temp, const UnicodeString & FileName, Boolean Success,
  1707. TOnceDoneOperation & OnceDoneOperation)
  1708. {
  1709. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1710. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1711. OnceDoneOperation);
  1712. }
  1713. //---------------------------------------------------------------------------
  1714. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1715. {
  1716. return false;
  1717. }
  1718. //---------------------------------------------------------------------------
  1719. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1720. {
  1721. return false;
  1722. }
  1723. //---------------------------------------------------------------------------
  1724. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1725. {
  1726. return false;
  1727. }
  1728. //---------------------------------------------------------------------------
  1729. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1730. {
  1731. return NULL;
  1732. }
  1733. //---------------------------------------------------------------------------
  1734. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1735. {
  1736. DebugAssert(GetSide(Side) == osRemote);
  1737. DebugUsedParam(Side);
  1738. return RemoteDirView;
  1739. }
  1740. //---------------------------------------------------------------------------
  1741. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1742. {
  1743. DebugAssert(GetSide(Side) == osRemote);
  1744. DebugUsedParam(Side);
  1745. return RemoteDriveView;
  1746. }
  1747. //---------------------------------------------------------------------------
  1748. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1749. {
  1750. DebugAssert(GetSide(Side) == osRemote);
  1751. DebugUsedParam(Side);
  1752. return HasActiveTerminal();
  1753. }
  1754. //---------------------------------------------------------------------------
  1755. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1756. TOperationSide Side, int FilesOnly)
  1757. {
  1758. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1759. }
  1760. //---------------------------------------------------------------------------
  1761. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1762. TOperationSide Side, int FilesOnly)
  1763. {
  1764. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1765. }
  1766. //---------------------------------------------------------------------------
  1767. struct THistoryItemData
  1768. {
  1769. short int Side;
  1770. short int Index;
  1771. };
  1772. //---------------------------------------------------------------------------
  1773. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1774. {
  1775. DirView(Side)->HistoryGo(Index);
  1776. }
  1777. //---------------------------------------------------------------------------
  1778. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1779. {
  1780. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1781. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1782. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1783. }
  1784. //---------------------------------------------------------------------------
  1785. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1786. bool Back)
  1787. {
  1788. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1789. {
  1790. TCustomDirView * DView = DirView(Side);
  1791. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1792. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1793. if (ICount > 10)
  1794. {
  1795. ICount = 10;
  1796. }
  1797. Menu->Items->Clear();
  1798. THistoryItemData Data;
  1799. Data.Side = Side;
  1800. for (short int i = 1; i <= ICount; i++)
  1801. {
  1802. TTBCustomItem * Item = new TTBXItem(Menu);
  1803. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1804. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1805. Item->Hint = DView->HistoryPath[Data.Index];
  1806. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1807. Item->Tag = *reinterpret_cast<int*>(&Data);
  1808. Item->OnClick = HistoryItemClick;
  1809. Menu->Items->Add(Item);
  1810. }
  1811. }
  1812. }
  1813. //---------------------------------------------------------------------------
  1814. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1815. TOperationSide Side, bool Back)
  1816. {
  1817. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1818. {
  1819. // workaround
  1820. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1821. // operator new allocates memory in Pascal code, but calls inline
  1822. // constructor in C++, leading in problems, the function does
  1823. // both in Pascal code
  1824. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1825. UpdateHistoryMenu(Side, Back);
  1826. }
  1827. return FHistoryMenu[Side == osLocal][Back];
  1828. }
  1829. //---------------------------------------------------------------------------
  1830. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1831. TCustomDirView *Sender)
  1832. {
  1833. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1834. UpdateHistoryMenu(Side, true);
  1835. UpdateHistoryMenu(Side, false);
  1836. }
  1837. //---------------------------------------------------------------------------
  1838. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1839. {
  1840. // remote custom commands can be executed only if the server supports shell commands
  1841. // or have secondary shell
  1842. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1843. }
  1844. //---------------------------------------------------------------------------
  1845. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1846. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1847. {
  1848. // -1 = hidden, 0 = disabled, 1 = enabled
  1849. int Result;
  1850. TFileCustomCommand RemoteCustomCommand;
  1851. TLocalCustomCommand LocalCustomCommand;
  1852. TFileCustomCommand * NonInteractiveCustomCommand =
  1853. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1854. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1855. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1856. if (FLAGCLEAR(Command.Params, ccLocal))
  1857. {
  1858. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1859. // custom command that does not operate with files can be executed anytime ...
  1860. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1861. {
  1862. // ... but do not show such command in remote file menu
  1863. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1864. {
  1865. Result = AllowedState;
  1866. }
  1867. else
  1868. {
  1869. Result = -1;
  1870. }
  1871. }
  1872. else
  1873. {
  1874. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1875. {
  1876. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1877. }
  1878. else
  1879. {
  1880. Result = -1;
  1881. }
  1882. }
  1883. }
  1884. else
  1885. {
  1886. // custom command that does not operate with files can be executed anytime
  1887. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1888. {
  1889. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1890. }
  1891. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1892. {
  1893. if ((ListType == ccltAll) || (ListType == ccltFile))
  1894. {
  1895. // special case is "diff"-style command that can be executed over any side,
  1896. // if we have both sides
  1897. Result =
  1898. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1899. // directly (instead of EnableSelectedOperation) to pass
  1900. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1901. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1902. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1903. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1904. }
  1905. else if (ListType == ccltBoth)
  1906. {
  1907. DebugAssert(!IsLocalBrowserMode());
  1908. Result = 1;
  1909. }
  1910. else
  1911. {
  1912. Result = -1;
  1913. }
  1914. }
  1915. else
  1916. {
  1917. if ((ListType == ccltAll) ||
  1918. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1919. {
  1920. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1921. }
  1922. else
  1923. {
  1924. Result = -1;
  1925. }
  1926. }
  1927. if ((Result > 0) &&
  1928. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1929. !HasActiveTerminal())
  1930. {
  1931. Result = 0;
  1932. }
  1933. }
  1934. return Result;
  1935. }
  1936. //---------------------------------------------------------------------------
  1937. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1938. TStrings * FileList, const TCustomCommandType & ACommand,
  1939. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1940. {
  1941. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1942. {
  1943. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1944. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1945. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1946. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1947. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1948. bool Capture =
  1949. FLAGSET(ACommand.Params, ccShowResults) ||
  1950. FLAGSET(ACommand.Params, ccCopyResults) ||
  1951. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1952. TCaptureOutputEvent OutputEvent = NULL;
  1953. DebugAssert(FCapturedLog == NULL);
  1954. if (Capture)
  1955. {
  1956. FCapturedLog = new TStringList();
  1957. OutputEvent = TerminalCaptureLog;
  1958. }
  1959. try
  1960. {
  1961. if (!RemoteCustomCommand.IsFileCommand(Command))
  1962. {
  1963. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1964. OutputEvent);
  1965. }
  1966. else
  1967. {
  1968. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1969. }
  1970. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1971. {
  1972. if (FLAGSET(ACommand.Params, ccCopyResults))
  1973. {
  1974. CopyToClipboard(FCapturedLog);
  1975. }
  1976. if (FLAGSET(ACommand.Params, ccShowResults))
  1977. {
  1978. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1979. }
  1980. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1981. {
  1982. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1983. }
  1984. }
  1985. }
  1986. __finally
  1987. {
  1988. SAFE_DESTROY(FCapturedLog);
  1989. }
  1990. }
  1991. }
  1992. //---------------------------------------------------------------------------
  1993. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1994. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1995. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1996. {
  1997. DebugAssert(!IsLocalBrowserMode());
  1998. TStrings * LocalFileList = NULL;
  1999. TStrings * RemoteFileList = NULL;
  2000. TStrings * RemoteFileListFull = NULL;
  2001. try
  2002. {
  2003. if (LocalFileCommand)
  2004. {
  2005. if (ALocalFileList == NULL)
  2006. {
  2007. DebugAssert(HasDirView[osLocal]);
  2008. // Cannot have focus on both panels, so we have to call AnyFileSelected
  2009. // directly (instead of EnableSelectedOperation) to pass
  2010. // false to FocusedFileOnlyWhenFocused
  2011. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  2012. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  2013. }
  2014. else
  2015. {
  2016. LocalFileList = ALocalFileList;
  2017. }
  2018. if (FileListCommand)
  2019. {
  2020. if (LocalFileList->Count != 1)
  2021. {
  2022. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  2023. }
  2024. }
  2025. else
  2026. {
  2027. if ((LocalFileList->Count != 1) &&
  2028. (FileList->Count != 1) &&
  2029. (LocalFileList->Count != FileList->Count))
  2030. {
  2031. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  2032. }
  2033. }
  2034. }
  2035. UnicodeString RootTempDir;
  2036. UnicodeString TempDir;
  2037. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  2038. if (!RemoteFiles)
  2039. {
  2040. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true, true);
  2041. }
  2042. try
  2043. {
  2044. TDateTimes RemoteFileTimes;
  2045. if (RemoteFiles)
  2046. {
  2047. RemoteFileList = FileList;
  2048. }
  2049. else
  2050. {
  2051. RemoteFileList = new TStringList();
  2052. TMakeLocalFileListParams MakeFileListParam;
  2053. MakeFileListParam.FileList = RemoteFileList;
  2054. MakeFileListParam.FileTimes = &RemoteFileTimes;
  2055. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2056. MakeFileListParam.Recursive =
  2057. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  2058. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2059. if (!MakeFileListParam.Recursive)
  2060. {
  2061. RemoteFileTimes.clear();
  2062. RemoteFileListFull = new TStringList();
  2063. MakeFileListParam.FileList = RemoteFileListFull;
  2064. MakeFileListParam.Recursive = true;
  2065. MakeFileListParam.IncludeDirs = false;
  2066. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2067. }
  2068. else
  2069. {
  2070. RemoteFileListFull = RemoteFileList;
  2071. }
  2072. }
  2073. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  2074. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2075. if (!NonBlocking)
  2076. {
  2077. int Count;
  2078. if (FileListCommand)
  2079. {
  2080. Count = 1;
  2081. }
  2082. else if (LocalFileCommand)
  2083. {
  2084. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  2085. }
  2086. else
  2087. {
  2088. Count = RemoteFileList->Count;
  2089. }
  2090. Progress.Start(foCustomCommand, osRemote, Count);
  2091. DebugAssert(FProgressForm != NULL);
  2092. FProgressForm->ReadOnly = true;
  2093. }
  2094. try
  2095. {
  2096. if (FileListCommand)
  2097. {
  2098. UnicodeString LocalFile;
  2099. // MakeFileList does not delimit filenames
  2100. UnicodeString FileList = MakeFileList(RemoteFileList);
  2101. if (LocalFileCommand)
  2102. {
  2103. DebugAssert(LocalFileList->Count == 1);
  2104. LocalFile = LocalFileList->Strings[0];
  2105. }
  2106. TLocalCustomCommand CustomCommand(Data,
  2107. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  2108. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  2109. if (NonBlocking)
  2110. {
  2111. DebugAssert(!POutput);
  2112. ExecuteShellChecked(ShellCommand);
  2113. }
  2114. else
  2115. {
  2116. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2117. }
  2118. }
  2119. else if (LocalFileCommand)
  2120. {
  2121. if (LocalFileList->Count == 1)
  2122. {
  2123. UnicodeString LocalFile = LocalFileList->Strings[0];
  2124. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2125. {
  2126. UnicodeString FileName = RemoteFileList->Strings[Index];
  2127. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2128. Progress.SetFile(FileNameForProgress);
  2129. TLocalCustomCommand CustomCommand(Data,
  2130. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2131. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2132. TOnceDoneOperation OnceDoneOperation;
  2133. // Do not unselect anything, as it won't work with two panels anyway
  2134. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2135. }
  2136. }
  2137. else if (RemoteFileList->Count == 1)
  2138. {
  2139. UnicodeString FileName = RemoteFileList->Strings[0];
  2140. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2141. {
  2142. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2143. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2144. Progress.SetFile(LocalFileNameOnly);
  2145. TLocalCustomCommand CustomCommand(
  2146. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2147. FileName, LocalFileName, L"");
  2148. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2149. TOnceDoneOperation OnceDoneOperation;
  2150. // Do not unselect anything, as it won't work with two panels anyway
  2151. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2152. }
  2153. }
  2154. else
  2155. {
  2156. if (LocalFileList->Count != RemoteFileList->Count)
  2157. {
  2158. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2159. }
  2160. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2161. {
  2162. UnicodeString FileName = RemoteFileList->Strings[Index];
  2163. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2164. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2165. Progress.SetFile(LocalFileNameOnly); // sic
  2166. TLocalCustomCommand CustomCommand(
  2167. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2168. FileName, LocalFileName, L"");
  2169. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2170. TOnceDoneOperation OnceDoneOperation;
  2171. // Do not unselect anything, as it won't work with two panels anyway
  2172. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2173. }
  2174. }
  2175. }
  2176. else
  2177. {
  2178. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2179. {
  2180. UnicodeString FileName = RemoteFileList->Strings[Index];
  2181. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2182. Progress.SetFile(FileNameForProgress);
  2183. TLocalCustomCommand CustomCommand(Data,
  2184. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2185. FileName, L"", L"");
  2186. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2187. TOnceDoneOperation OnceDoneOperation;
  2188. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2189. }
  2190. }
  2191. }
  2192. __finally
  2193. {
  2194. if (!NonBlocking)
  2195. {
  2196. Progress.Stop();
  2197. }
  2198. }
  2199. DebugAssert(!FAutoOperation);
  2200. if (!RemoteFiles)
  2201. {
  2202. TempDir = IncludeTrailingBackslash(TempDir);
  2203. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2204. {
  2205. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2206. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2207. // Skip directories, process only nested files.
  2208. // The check is redundant as FileAge fails for directories anyway.
  2209. !DirectoryExists(FileName))
  2210. {
  2211. UnicodeString RemoteDir =
  2212. UnixExtractFileDir(
  2213. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2214. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2215. TDateTime NewTime;
  2216. if (FileAge(FileName, NewTime) &&
  2217. (NewTime != RemoteFileTimes[Index]))
  2218. {
  2219. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2220. TemporaryFileCopyParam(CopyParam);
  2221. CopyParam.FileMask = L"";
  2222. FAutoOperation = true;
  2223. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2224. TemporaryFilesList->Add(FileName);
  2225. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2226. }
  2227. }
  2228. }
  2229. }
  2230. }
  2231. __finally
  2232. {
  2233. FAutoOperation = false;
  2234. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2235. {
  2236. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  2237. }
  2238. }
  2239. }
  2240. __finally
  2241. {
  2242. if (RemoteFileList != FileList)
  2243. {
  2244. delete RemoteFileList;
  2245. }
  2246. if (LocalFileList != ALocalFileList)
  2247. {
  2248. delete LocalFileList;
  2249. }
  2250. if (RemoteFileListFull != RemoteFileList)
  2251. {
  2252. delete RemoteFileListFull;
  2253. }
  2254. }
  2255. }
  2256. //---------------------------------------------------------------------------
  2257. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2258. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2259. bool FileListCommand, UnicodeString * POutput)
  2260. {
  2261. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2262. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2263. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2264. {
  2265. UnicodeString FileName = SelectedFileList->Strings[Index];
  2266. if (DirectoryExists(FileName))
  2267. {
  2268. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2269. {
  2270. LocalFileList->Add(FileName);
  2271. }
  2272. if (FLAGSET(ACommand.Params, ccRecursive))
  2273. {
  2274. TMakeLocalFileListParams MakeFileListParam;
  2275. MakeFileListParam.FileList = LocalFileList.get();
  2276. MakeFileListParam.FileTimes = NULL;
  2277. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2278. MakeFileListParam.Recursive = true;
  2279. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2280. }
  2281. }
  2282. else
  2283. {
  2284. LocalFileList->Add(FileName);
  2285. }
  2286. }
  2287. UnicodeString RemotePath;
  2288. if (Terminal != NULL)
  2289. {
  2290. RemotePath = Terminal->CurrentDirectory;
  2291. }
  2292. if (FileListCommand)
  2293. {
  2294. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2295. TLocalCustomCommand CustomCommand(
  2296. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2297. L"", L"", FileList);
  2298. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2299. }
  2300. else
  2301. {
  2302. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2303. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2304. DebugAssert(FProgressForm != NULL);
  2305. FProgressForm->ReadOnly = true;
  2306. try
  2307. {
  2308. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2309. {
  2310. UnicodeString FileName = LocalFileList->Strings[Index];
  2311. Progress.SetFile(FileName);
  2312. TLocalCustomCommand CustomCommand(
  2313. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2314. FileName, L"", L"");
  2315. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2316. TOnceDoneOperation OnceDoneOperation;
  2317. Progress.Finish(FileName, true, OnceDoneOperation);
  2318. }
  2319. }
  2320. __finally
  2321. {
  2322. Progress.Stop();
  2323. }
  2324. }
  2325. }
  2326. //---------------------------------------------------------------------------
  2327. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2328. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2329. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2330. {
  2331. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2332. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2333. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2334. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2335. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2336. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2337. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2338. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2339. std::unique_ptr<UnicodeString> POutput;
  2340. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2341. FLAGSET(ACommand.Params, ccCopyResults))
  2342. {
  2343. POutput.reset(new UnicodeString());
  2344. }
  2345. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2346. FProgressSide = FCurrentSide;
  2347. if (!LocalCustomCommand.IsFileCommand(Command))
  2348. {
  2349. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2350. }
  2351. // remote files?
  2352. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2353. {
  2354. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2355. }
  2356. // local files
  2357. else
  2358. {
  2359. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2360. }
  2361. if (POutput.get() != NULL)
  2362. {
  2363. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2364. int P = POutput->Pos(sLineBreak);
  2365. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2366. {
  2367. POutput->SetLength(P - 1);
  2368. }
  2369. // Copy even empty output to clipboard
  2370. if (FLAGSET(ACommand.Params, ccCopyResults))
  2371. {
  2372. CopyToClipboard(*POutput);
  2373. }
  2374. // But do not show an empty message box.
  2375. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2376. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2377. !POutput->IsEmpty())
  2378. {
  2379. TClipboardHandler ClipboardHandler;
  2380. ClipboardHandler.Text = *POutput;
  2381. TMessageParams Params;
  2382. TQueryButtonAlias Aliases[1];
  2383. Aliases[0].Button = qaRetry;
  2384. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2385. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2386. Params.Aliases = Aliases;
  2387. Params.AliasesCount = LENOF(Aliases);
  2388. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2389. }
  2390. }
  2391. }
  2392. //---------------------------------------------------------------------------
  2393. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2394. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2395. {
  2396. TCustomCommandData Data;
  2397. UnicodeString Site;
  2398. UnicodeString RemotePath;
  2399. if (HasActiveTerminal())
  2400. {
  2401. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2402. Data = TCustomCommandData(SessionData.get());
  2403. Site = Terminal->SessionData->SessionKey;
  2404. RemotePath = Terminal->CurrentDirectory;
  2405. }
  2406. UnicodeString HelpKeyword = ACommand.HomePage;
  2407. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2408. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2409. {
  2410. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2411. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2412. {
  2413. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2414. }
  2415. else
  2416. {
  2417. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2418. }
  2419. if (!DoCustomCommandOptionsDialog(
  2420. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2421. {
  2422. Abort();
  2423. }
  2424. }
  2425. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2426. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2427. {
  2428. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2429. }
  2430. else
  2431. {
  2432. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2433. }
  2434. }
  2435. //---------------------------------------------------------------------------
  2436. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2437. const TCustomCommandType & Command)
  2438. {
  2439. DebugAssert(FCustomCommandLocalFileList != NULL);
  2440. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2441. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2442. TStrings * LocalFileList = new TStringList();
  2443. TStrings * RemoteFileList = new TStringList();
  2444. try
  2445. {
  2446. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2447. {
  2448. LocalFileList->Clear();
  2449. LocalFileList->AddObject(
  2450. FCustomCommandLocalFileList->Strings[Index],
  2451. FCustomCommandLocalFileList->Objects[Index]);
  2452. RemoteFileList->Clear();
  2453. RemoteFileList->AddObject(
  2454. FCustomCommandRemoteFileList->Strings[Index],
  2455. FCustomCommandRemoteFileList->Objects[Index]);
  2456. CustomCommand(RemoteFileList, Command, LocalFileList);
  2457. }
  2458. }
  2459. __finally
  2460. {
  2461. delete LocalFileList;
  2462. delete RemoteFileList;
  2463. }
  2464. }
  2465. //---------------------------------------------------------------------------
  2466. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2467. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2468. {
  2469. delete FCustomCommandLocalFileList;
  2470. delete FCustomCommandRemoteFileList;
  2471. // takeover ownership,
  2472. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2473. FCustomCommandLocalFileList = LocalFileList;
  2474. FCustomCommandRemoteFileList = RemoteFileList;
  2475. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2476. if (Button != NULL)
  2477. {
  2478. FCustomCommandMenu->Items->Clear();
  2479. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2480. MenuPopup(FCustomCommandMenu, Button);
  2481. }
  2482. else
  2483. {
  2484. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2485. }
  2486. }
  2487. //---------------------------------------------------------------------------
  2488. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2489. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2490. {
  2491. DebugAssert(FCapturedLog != NULL);
  2492. if ((OutputType == cotOutput) || (OutputType == cotError))
  2493. {
  2494. FCapturedLog->Add(AddedLine);
  2495. }
  2496. }
  2497. //---------------------------------------------------------------------------
  2498. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2499. TOperationSide Side)
  2500. {
  2501. return
  2502. (Side == osOther) &&
  2503. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2504. }
  2505. //---------------------------------------------------------------------------
  2506. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2507. TOperationSide Side, TTBXCustomItem * Item,
  2508. TResolvedDoubleClickAction DoubleClickAction1, TResolvedDoubleClickAction DoubleClickAction2)
  2509. {
  2510. TCustomDirView * DView = DirView(Side);
  2511. bool IsDefault = false;
  2512. if (DView->ItemFocused != NULL)
  2513. {
  2514. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  2515. TResolvedDoubleClickAction DefaultAction =
  2516. WinConfiguration->ResolveDoubleClickAction(DView->ItemIsDirectory(DView->ItemFocused), ATerminal);
  2517. IsDefault = (DefaultAction == DoubleClickAction1) || (DefaultAction == DoubleClickAction2);
  2518. }
  2519. TTBItemOptions O = Item->Options;
  2520. if (IsDefault)
  2521. {
  2522. Item->Options = O << tboDefault;
  2523. }
  2524. else
  2525. {
  2526. Item->Options = O >> tboDefault;
  2527. }
  2528. }
  2529. //---------------------------------------------------------------------------
  2530. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2531. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2532. {
  2533. TCustomDirView * DView = DirView(Side);
  2534. TListItem * Item = DView->ItemFocused;
  2535. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2536. EnableFocusedOperation[Side])
  2537. {
  2538. TPoint ClientPoint;
  2539. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2540. TPoint(0, 0) : MousePos;
  2541. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2542. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2543. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2544. }
  2545. }
  2546. //---------------------------------------------------------------------------
  2547. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2548. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2549. {
  2550. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, rdcaChangeDir, rdcaOpen);
  2551. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, rdcaEdit, rdcaNone);
  2552. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, rdcaCopy, rdcaNone);
  2553. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2554. Handled = true;
  2555. }
  2556. //---------------------------------------------------------------------------
  2557. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2558. {
  2559. }
  2560. //---------------------------------------------------------------------------
  2561. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2562. {
  2563. DebugAssert(FErrorList == NULL);
  2564. if (WinConfiguration->ContinueOnError)
  2565. {
  2566. FErrorList = new TStringList();
  2567. Configuration->Usage->Inc(L"ContinuationsOnError");
  2568. }
  2569. NonVisualDataModule->StartBusy();
  2570. }
  2571. //---------------------------------------------------------------------------
  2572. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2573. {
  2574. NonVisualDataModule->EndBusy();
  2575. if (FErrorList)
  2576. {
  2577. HandleErrorList(FErrorList);
  2578. }
  2579. }
  2580. //---------------------------------------------------------------------------
  2581. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2582. const TCopyParamType & CopyParam)
  2583. {
  2584. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2585. {
  2586. Configuration->Usage->Inc(L"FileMaskUses");
  2587. }
  2588. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2589. {
  2590. Configuration->Usage->Inc(L"OperationMaskUses");
  2591. }
  2592. }
  2593. //---------------------------------------------------------------------------
  2594. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2595. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2596. {
  2597. DebugAssert(!IsLocalBrowserMode());
  2598. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2599. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2600. TTransferOperationParam DefaultParam;
  2601. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2602. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2603. switch (Param.Queue)
  2604. {
  2605. case asOn:
  2606. CopyParam.Queue = true;
  2607. break;
  2608. case asOff:
  2609. CopyParam.Queue = false;
  2610. break;
  2611. case asAuto:
  2612. default:
  2613. // keep default
  2614. break;
  2615. }
  2616. bool Result =
  2617. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2618. if (Result)
  2619. {
  2620. DebugAssert(Terminal);
  2621. bool SelectionRestored = false;
  2622. TCustomDirView * DView = NULL;
  2623. if (HasDirView[Side])
  2624. {
  2625. DView = DirView(Side);
  2626. DView->SaveSelection();
  2627. DView->SaveSelectedNames();
  2628. }
  2629. UpdateCopyParamCounters(CopyParam);
  2630. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2631. DebugAssert(FTransferResumeList == NULL);
  2632. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2633. FMoveToQueue = false;
  2634. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2635. try
  2636. {
  2637. TStrings * PermanentFileList;
  2638. std::unique_ptr<TStrings> PermanentFileListOwner;
  2639. try
  2640. {
  2641. if (Side == osLocal)
  2642. {
  2643. PermanentFileList = FileList;
  2644. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2645. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2646. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2647. if (Operation == foMove)
  2648. {
  2649. ReloadLocalDirectory();
  2650. if (DView != NULL)
  2651. {
  2652. DView->RestoreSelection();
  2653. }
  2654. SelectionRestored = true;
  2655. }
  2656. }
  2657. else
  2658. {
  2659. // Clone the file list as it may refer to current directory files,
  2660. // which get destroyed, when the source directory is reloaded after foMove operation.
  2661. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2662. // But for a hotfix, we are not going to do such a big change.
  2663. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2664. PermanentFileList = PermanentFileListOwner.get();
  2665. try
  2666. {
  2667. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2668. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2669. }
  2670. __finally
  2671. {
  2672. if (Operation == foMove)
  2673. {
  2674. if (DView != NULL)
  2675. {
  2676. DView->RestoreSelection();
  2677. }
  2678. SelectionRestored = true;
  2679. }
  2680. ReloadLocalDirectory(Param.TargetDirectory);
  2681. }
  2682. }
  2683. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2684. if (FMoveToQueue)
  2685. {
  2686. Abort();
  2687. }
  2688. }
  2689. catch (EAbort &)
  2690. {
  2691. if (FMoveToQueue)
  2692. {
  2693. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2694. DebugAssert(CopyParam.TransferSkipList == NULL);
  2695. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2696. if (TransferResumeList->Count > 0)
  2697. {
  2698. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2699. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2700. }
  2701. CopyParam.TransferSkipList = TransferResumeList.release();
  2702. // not really needed, just to keep it consistent with TransferResumeList
  2703. FTransferResumeList = NULL;
  2704. FMoveToQueue = false;
  2705. Configuration->Usage->Inc("MovesToBackground");
  2706. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2707. ClearTransferSourceSelection(Direction);
  2708. }
  2709. throw;
  2710. }
  2711. }
  2712. __finally
  2713. {
  2714. if (!SelectionRestored && (DView != NULL))
  2715. {
  2716. DView->DiscardSavedSelection();
  2717. }
  2718. FTransferResumeList = NULL;
  2719. }
  2720. }
  2721. return Result;
  2722. }
  2723. //---------------------------------------------------------------------------
  2724. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2725. TOperationSide Side, TStrings * FileList, void * Param)
  2726. {
  2727. DebugAssert(FileList->Count);
  2728. // We deliberately do not toggle alternative flag (Param), but use OR,
  2729. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2730. // shortcut of CurrentDeleteAlternativeAction
  2731. bool Alternative =
  2732. bool(Param) || UseAlternativeFunction();
  2733. bool Recycle;
  2734. if (IsSideLocalBrowser(Side))
  2735. {
  2736. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2737. }
  2738. else
  2739. {
  2740. Recycle =
  2741. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2742. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2743. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2744. }
  2745. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2746. if (!Result)
  2747. {
  2748. UnicodeString Query;
  2749. if (FileList->Count == 1)
  2750. {
  2751. if (IsSideLocalBrowser(Side))
  2752. {
  2753. Query = ExtractFileName(FileList->Strings[0]);
  2754. }
  2755. else
  2756. {
  2757. Query = UnixExtractFileName(FileList->Strings[0]);
  2758. }
  2759. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2760. }
  2761. else
  2762. {
  2763. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2764. }
  2765. TMessageParams Params(mpNeverAskAgainCheck);
  2766. Params.ImageName = L"Delete file";
  2767. unsigned int Answer =
  2768. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2769. if (Answer == qaNeverAskAgain)
  2770. {
  2771. Result = true;
  2772. if (Recycle)
  2773. {
  2774. WinConfiguration->ConfirmRecycling = false;
  2775. }
  2776. else
  2777. {
  2778. WinConfiguration->ConfirmDeleting = false;
  2779. }
  2780. }
  2781. else
  2782. {
  2783. Result = (Answer == qaOK);
  2784. }
  2785. }
  2786. if (Result)
  2787. {
  2788. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2789. }
  2790. return Result;
  2791. }
  2792. //---------------------------------------------------------------------------
  2793. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2794. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2795. {
  2796. TAutoBatch AutoBatch(this);
  2797. bool Result;
  2798. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2799. {
  2800. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2801. }
  2802. else if (Operation == foRename)
  2803. {
  2804. DebugAssert(DirView(Side)->ItemFocused);
  2805. DirView(Side)->ItemFocused->EditCaption();
  2806. Result = true;
  2807. }
  2808. else if (Operation == foDelete)
  2809. {
  2810. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2811. }
  2812. else if (Operation == foSetProperties)
  2813. {
  2814. DirView(osRemote)->SaveSelectedNames();
  2815. Result = SetProperties(Side, FileList);
  2816. }
  2817. else if (Operation == foCustomCommand)
  2818. {
  2819. DebugAssert(Param);
  2820. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2821. DirView(osRemote)->SaveSelectedNames();
  2822. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2823. CustomCommand(FileList, *Command, NULL);
  2824. Result = true;
  2825. }
  2826. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2827. {
  2828. DebugAssert(!IsSideLocalBrowser(Side));
  2829. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2830. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2831. }
  2832. else if (Operation == foLock)
  2833. {
  2834. DebugAssert(!IsSideLocalBrowser(Side));
  2835. LockFiles(FileList, true);
  2836. Result = true;
  2837. }
  2838. else if (Operation == foUnlock)
  2839. {
  2840. DebugAssert(!IsSideLocalBrowser(Side));
  2841. LockFiles(FileList, false);
  2842. Result = true;
  2843. }
  2844. else
  2845. {
  2846. DebugFail();
  2847. }
  2848. return Result;
  2849. }
  2850. //---------------------------------------------------------------------------
  2851. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2852. {
  2853. if (Side == osCurrent)
  2854. {
  2855. Side = FCurrentSide;
  2856. }
  2857. return Side;
  2858. }
  2859. //---------------------------------------------------------------------------
  2860. TOperationSide TCustomScpExplorerForm::GetOtherSide(TOperationSide Side)
  2861. {
  2862. TOperationSide Result;
  2863. switch (GetSide(Side))
  2864. {
  2865. case osLocal:
  2866. Result = osOther;
  2867. break;
  2868. case osOther:
  2869. Result = osLocal;
  2870. break;
  2871. default:
  2872. DebugFail();
  2873. Abort();
  2874. }
  2875. return Result;
  2876. }
  2877. //---------------------------------------------------------------------------
  2878. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2879. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2880. {
  2881. Side = GetSide(Side);
  2882. bool FullPath = IsSideLocalBrowser(Side);
  2883. TCustomDirView * ADirView = DirView(Side);
  2884. std::unique_ptr<TStringList> FileList(new TStringList());
  2885. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2886. bool ReloadProperties =
  2887. !IsSideLocalBrowser(Side) && (Operation == foSetProperties) &&
  2888. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2889. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2890. if (Result && ReloadProperties)
  2891. {
  2892. std::unique_ptr<TStringList> FileList2(new TStringList());
  2893. FileList->CaseSensitive = true;
  2894. FileList->Sort();
  2895. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2896. {
  2897. TListItem * Item = ADirView->Items->Item[Index];
  2898. UnicodeString FileName = ADirView->ItemFileName(Item);
  2899. int Index2;
  2900. if (FileList->Find(FileName, Index2))
  2901. {
  2902. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2903. }
  2904. }
  2905. LoadFilesProperties(FileList2.get());
  2906. }
  2907. return Result;
  2908. }
  2909. //---------------------------------------------------------------------------
  2910. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2911. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2912. bool NoConfirmation, void * Param)
  2913. {
  2914. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2915. {
  2916. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2917. {
  2918. DebugAssert(!IsLocalBrowserMode());
  2919. if (GetSide(Side) == osLocal)
  2920. {
  2921. Configuration->Usage->Inc(L"UploadsCommand");
  2922. }
  2923. else
  2924. {
  2925. Configuration->Usage->Inc(L"DownloadsCommand");
  2926. }
  2927. }
  2928. }
  2929. }
  2930. //---------------------------------------------------------------------------
  2931. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2932. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2933. {
  2934. TTransferOperationParam Param;
  2935. if ((WinConfiguration->Interface != ifCommander) ||
  2936. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2937. {
  2938. Flags &= ~cocShortCutHint;
  2939. }
  2940. TCustomDirView * DView = DirView(Side);
  2941. Param.Options =
  2942. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2943. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2944. if (FLAGSET(Flags, cocQueue))
  2945. {
  2946. Param.Queue = asOn;
  2947. }
  2948. else if (FLAGSET(Flags, cocNonQueue))
  2949. {
  2950. Param.Queue = asOff;
  2951. }
  2952. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2953. }
  2954. //---------------------------------------------------------------------------
  2955. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2956. {
  2957. int Count = ErrorList->Count;
  2958. try
  2959. {
  2960. if (ErrorList->Count)
  2961. {
  2962. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2963. if (MessageDialog(Message, qtError,
  2964. qaOK | qaCancel, HELP_NONE) == qaOK)
  2965. {
  2966. unsigned int Answer;
  2967. int Index = 0;
  2968. do
  2969. {
  2970. DebugAssert(Index >= 0 && Index < Count);
  2971. TQueryButtonAlias Aliases[2];
  2972. Aliases[0].Button = qaYes;
  2973. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2974. Aliases[1].Button = qaNo;
  2975. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2976. TMessageParams Params;
  2977. Params.Aliases = Aliases;
  2978. Params.AliasesCount = LENOF(Aliases);
  2979. int No = Index + 1;
  2980. UnicodeString Message = ErrorList->Strings[Index];
  2981. UnicodeString AMainInstructions;
  2982. UnicodeString Details;
  2983. if (ExtractMainInstructions(Message, AMainInstructions))
  2984. {
  2985. Details = Message;
  2986. Message = AMainInstructions;
  2987. }
  2988. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2989. Answer = MoreMessageDialog(
  2990. Message,
  2991. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2992. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2993. qaOK, HELP_NONE, &Params);
  2994. if (Answer == qaNo)
  2995. {
  2996. Index++;
  2997. }
  2998. if (Answer == qaYes)
  2999. {
  3000. Index--;
  3001. }
  3002. }
  3003. while (Answer != qaOK);
  3004. }
  3005. }
  3006. }
  3007. __finally
  3008. {
  3009. TStrings * List = ErrorList;
  3010. ErrorList = NULL;
  3011. for (int i = 0; i < Count; i++)
  3012. {
  3013. delete List->Objects[i];
  3014. }
  3015. delete List;
  3016. }
  3017. }
  3018. //---------------------------------------------------------------------------
  3019. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  3020. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  3021. {
  3022. // needed for checking filemasks, as there's no directory object
  3023. // associated with the file object
  3024. File->FullFileName = FullFileName;
  3025. TFileMasks::TParams MaskParams;
  3026. MaskParams.Size = File->Size;
  3027. MaskParams.Modification = File->Modification;
  3028. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  3029. }
  3030. //---------------------------------------------------------------------------
  3031. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  3032. {
  3033. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3034. Side = GetSide(Side);
  3035. TCustomDirView * DView = DirView(Side);
  3036. TListItem * FocusedItem = DView->ItemFocused;
  3037. UnicodeString Name;
  3038. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  3039. {
  3040. Name = DView->ItemFileName(FocusedItem);
  3041. }
  3042. else
  3043. {
  3044. Name = LoadStr(NEW_FILE);
  3045. }
  3046. UnicodeString Names = Name;
  3047. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  3048. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  3049. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  3050. {
  3051. while (!Names.IsEmpty())
  3052. {
  3053. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  3054. CustomWinConfiguration->History[L"EditFile"] = History.get();
  3055. UnicodeString TargetFileName;
  3056. UnicodeString LocalFileName;
  3057. UnicodeString RootTempDir;
  3058. UnicodeString TempDir;
  3059. UnicodeString RemoteDirectory;
  3060. bool ExistingFile = false;
  3061. if (!IsSideLocalBrowser(Side))
  3062. {
  3063. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  3064. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(Name));
  3065. if ((File.get() != NULL) &&
  3066. !File->IsDirectory)
  3067. {
  3068. ExecuteRemoteFile(Name, File.get(), efDefaultEditor);
  3069. ExistingFile = true;
  3070. }
  3071. File.reset(NULL);
  3072. if (!ExistingFile)
  3073. {
  3074. RemoteDirectory = UnixExtractFilePath(Name);
  3075. TemporaryDirectoryForRemoteFiles(
  3076. RemoteDirectory, GUIConfiguration->CurrentCopyParam, false, TempDir, RootTempDir);
  3077. TargetFileName = UnixExtractFileName(Name);
  3078. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3079. LocalFileName = TempDir +
  3080. // We probably do not want to trim the VMS version here
  3081. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  3082. }
  3083. }
  3084. else
  3085. {
  3086. if (ExtractFilePath(Name).IsEmpty())
  3087. {
  3088. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  3089. }
  3090. else
  3091. {
  3092. LocalFileName = ExpandFileName(Name);
  3093. }
  3094. TargetFileName = ExtractFileName(Name);
  3095. }
  3096. if (!ExistingFile)
  3097. {
  3098. bool NewFile = !FileExists(ApiPath(LocalFileName));
  3099. if (NewFile)
  3100. {
  3101. int File = FileCreate(ApiPath(LocalFileName));
  3102. if (File < 0)
  3103. {
  3104. if (!RootTempDir.IsEmpty())
  3105. {
  3106. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  3107. }
  3108. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  3109. }
  3110. else
  3111. {
  3112. FileClose(File);
  3113. }
  3114. }
  3115. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  3116. const TEditorData * ExternalEditor = NULL;
  3117. TFileMasks::TParams MaskParams; // size not known
  3118. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  3119. false, MaskParams);
  3120. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  3121. ExternalEditor, RootTempDir, RemoteDirectory, NewFile, TDateTime());
  3122. }
  3123. }
  3124. }
  3125. }
  3126. //---------------------------------------------------------------------------
  3127. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  3128. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  3129. {
  3130. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3131. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3132. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3133. return
  3134. ((ExecuteFileBy == efInternalEditor)) ||
  3135. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  3136. }
  3137. //---------------------------------------------------------------------------
  3138. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  3139. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  3140. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  3141. UnicodeString RemoteDirectory, bool NewFile, const TDateTime & SourceTimestamp)
  3142. {
  3143. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3144. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3145. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3146. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3147. Side = GetSide(Side);
  3148. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3149. if (!IsSideLocalBrowser(Side))
  3150. {
  3151. Data->Terminal = Terminal;
  3152. Data->SessionData = CloneCurrentSessionData();
  3153. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3154. Data->RemoteDirectory = RemoteDirectory;
  3155. Data->SessionName = Terminal->SessionData->SessionName;
  3156. Data->LocalRootDirectory = LocalRootDirectory;
  3157. Data->OriginalFileName = OriginalFileName;
  3158. Data->Command = L""; // will be changed later for external editor
  3159. // Empty SourceTimestamp indicates that checking was disabled when the file was opened
  3160. if (WinConfiguration->EditorCheckNotModified)
  3161. {
  3162. Data->SourceTimestamp = SourceTimestamp;
  3163. }
  3164. }
  3165. if (ExecuteFileBy == efInternalEditor)
  3166. {
  3167. if (!IsSideLocalBrowser(Side))
  3168. {
  3169. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3170. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3171. TForm * Editor;
  3172. try
  3173. {
  3174. Editor =
  3175. ShowEditorForm(
  3176. FileName, this,
  3177. FEditorManager->FileChanged, FEditorManager->FileReload, FEditorManager->FileClosed,
  3178. SaveAllInternalEditors, AnyInternalEditorModified,
  3179. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3180. }
  3181. catch(...)
  3182. {
  3183. if (!LocalRootDirectory.IsEmpty())
  3184. {
  3185. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory));
  3186. }
  3187. throw;
  3188. }
  3189. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3190. AppLogFmt(L"Opened internal editor with \"%s\"", (FileName));
  3191. }
  3192. else
  3193. {
  3194. DebugAssert(!StandaloneOperation);
  3195. TForm * Editor =
  3196. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3197. SaveAllInternalEditors, AnyInternalEditorModified,
  3198. L"", false, SessionColor, -1, NewFile);
  3199. FLocalEditors->Add(Editor);
  3200. }
  3201. }
  3202. else
  3203. {
  3204. HANDLE Process;
  3205. if (ExecuteFileBy == efExternalEditor)
  3206. {
  3207. UnicodeString Program, Params, Dir;
  3208. Data->Command = ExternalEditor->ExternalEditor;
  3209. ReformatFileNameCommand(Data->Command);
  3210. SplitCommand(Data->Command, Program, Params, Dir);
  3211. Params = ExpandFileNameCommand(Params, FileName);
  3212. Program = ExpandEnvironmentVariables(Program);
  3213. if (!ExecuteShell(Program, Params, Process))
  3214. {
  3215. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3216. }
  3217. }
  3218. else
  3219. {
  3220. DebugAssert(!IsSideLocalBrowser(Side));
  3221. if (!ExecuteShell(FileName, L"", Process))
  3222. {
  3223. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3224. }
  3225. }
  3226. if (IsSideLocalBrowser(Side) ||
  3227. ((ExecuteFileBy == efShell) &&
  3228. !WinConfiguration->Editor.SDIShellEditor) ||
  3229. ((ExecuteFileBy == efExternalEditor) &&
  3230. !ExternalEditor->SDIExternalEditor))
  3231. {
  3232. // no need for handle
  3233. if (Process != NULL)
  3234. {
  3235. DebugCheck(CloseHandle(Process));
  3236. }
  3237. Process = INVALID_HANDLE_VALUE;
  3238. }
  3239. else
  3240. {
  3241. if (Process == NULL)
  3242. {
  3243. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3244. }
  3245. }
  3246. if (!IsSideLocalBrowser(Side))
  3247. {
  3248. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3249. AppLogFmt(L"Opened external editor with remote file \"%s\".", (FileName));
  3250. }
  3251. }
  3252. }
  3253. //---------------------------------------------------------------------------
  3254. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3255. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3256. void * /*Arg*/)
  3257. {
  3258. if (Token != NULL)
  3259. {
  3260. EditorFormFileSave(static_cast<TForm *>(Token));
  3261. }
  3262. }
  3263. //---------------------------------------------------------------------------
  3264. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3265. {
  3266. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3267. {
  3268. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3269. }
  3270. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3271. }
  3272. //---------------------------------------------------------------------------
  3273. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3274. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3275. void * Arg)
  3276. {
  3277. if ((Token != NULL) &&
  3278. IsEditorFormModified(static_cast<TForm *>(Token)))
  3279. {
  3280. *static_cast<bool *>(Arg) = true;
  3281. }
  3282. }
  3283. //---------------------------------------------------------------------------
  3284. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3285. TObject * /*Sender*/, bool & Modified)
  3286. {
  3287. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3288. {
  3289. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3290. {
  3291. Modified = true;
  3292. }
  3293. }
  3294. if (!Modified)
  3295. {
  3296. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3297. }
  3298. }
  3299. //---------------------------------------------------------------------------
  3300. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3301. {
  3302. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3303. }
  3304. //---------------------------------------------------------------------------
  3305. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3306. const UnicodeString & RemoteDirectory, const TCopyParamType & CopyParam, bool Simple,
  3307. UnicodeString & Result, UnicodeString & RootDirectory)
  3308. {
  3309. if (!WinConfiguration->TemporaryDirectoryDeterministic || Simple)
  3310. {
  3311. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3312. Result = RootDirectory;
  3313. }
  3314. else
  3315. {
  3316. RootDirectory = L"";
  3317. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3318. Result = IncludeTrailingBackslash(Result);
  3319. }
  3320. if (WinConfiguration->TemporaryDirectoryAppendSession && !Simple)
  3321. {
  3322. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3323. }
  3324. if (WinConfiguration->TemporaryDirectoryAppendPath && !Simple)
  3325. {
  3326. UnicodeString ARemoteDirectory = RemoteDirectory;
  3327. if (!ARemoteDirectory.IsEmpty() && (ARemoteDirectory[1] == L'/'))
  3328. {
  3329. ARemoteDirectory.Delete(1, 1);
  3330. }
  3331. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(ARemoteDirectory)));
  3332. }
  3333. if (!ForceDirectories(ApiPath(Result)))
  3334. {
  3335. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3336. }
  3337. }
  3338. //---------------------------------------------------------------------------
  3339. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3340. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3341. bool GetTargetNames, bool AutoOperation, bool SimpleTempDir)
  3342. {
  3343. DebugAssert(!IsLocalBrowserMode());
  3344. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3345. if (ForceText)
  3346. {
  3347. CopyParam.TransferMode = tmAscii;
  3348. }
  3349. TemporaryFileCopyParam(CopyParam);
  3350. if (TempDir.IsEmpty())
  3351. {
  3352. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, SimpleTempDir, TempDir, RootTempDir);
  3353. }
  3354. DebugAssert(!FAutoOperation);
  3355. FAutoOperation = AutoOperation;
  3356. Terminal->ExceptionOnFail = true;
  3357. try
  3358. {
  3359. try
  3360. {
  3361. // turn off confirmations, as for MDI editors we may possibly download
  3362. // the same file over
  3363. Terminal->CopyToLocal(
  3364. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3365. if (GetTargetNames)
  3366. {
  3367. for (int i = 0; i < FileList->Count; i++)
  3368. {
  3369. FileList->Strings[i] =
  3370. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3371. }
  3372. }
  3373. }
  3374. catch(...)
  3375. {
  3376. if (!RootTempDir.IsEmpty())
  3377. {
  3378. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  3379. }
  3380. throw;
  3381. }
  3382. }
  3383. __finally
  3384. {
  3385. FAutoOperation = false;
  3386. Terminal->ExceptionOnFail = false;
  3387. }
  3388. }
  3389. //---------------------------------------------------------------------------
  3390. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3391. {
  3392. // Do not waste time checking for default editor list the next time,
  3393. // or testing if default editor is not notepad.
  3394. bool TryNextTime = false;
  3395. UnicodeString UsageState;
  3396. if (!WinConfiguration->EditorList->IsDefaultList())
  3397. {
  3398. UsageState = "H";
  3399. }
  3400. else
  3401. {
  3402. UnicodeString Executable;
  3403. UnicodeString ExecutableDescription;
  3404. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3405. {
  3406. UnicodeString Message =
  3407. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3408. unsigned int Answer =
  3409. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3410. if (Answer != qaOK)
  3411. {
  3412. UsageState = "R";
  3413. }
  3414. else
  3415. {
  3416. UsageState = "A";
  3417. TEditorData EditorData;
  3418. EditorData.Editor = edExternal;
  3419. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3420. EditorData.ExternalEditorOptionsAutodetect();
  3421. TEditorList EditorList;
  3422. EditorList = *WinConfiguration->EditorList;
  3423. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3424. WinConfiguration->EditorList = &EditorList;
  3425. }
  3426. }
  3427. }
  3428. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3429. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3430. }
  3431. //---------------------------------------------------------------------------
  3432. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3433. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3434. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3435. {
  3436. if (ExecuteFileBy == efDefaultEditor)
  3437. {
  3438. if (!WinConfiguration->OfferedEditorAutoConfig)
  3439. {
  3440. EditorAutoConfig();
  3441. }
  3442. const TEditorPreferences * Editor =
  3443. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3444. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3445. {
  3446. ExecuteFileBy = efInternalEditor;
  3447. ExternalEditor = NULL;
  3448. }
  3449. else if (Editor->Data->Editor == edOpen)
  3450. {
  3451. ExecuteFileBy = efShell;
  3452. ExternalEditor = NULL;
  3453. }
  3454. else
  3455. {
  3456. ExecuteFileBy = efExternalEditor;
  3457. ExternalEditor = Editor->Data;
  3458. }
  3459. }
  3460. }
  3461. //---------------------------------------------------------------------------
  3462. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3463. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3464. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3465. {
  3466. UnicodeString OriginalFileName;
  3467. UnicodeString LocalRootDirectory;
  3468. UnicodeString RemoteDirectory;
  3469. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3470. IsSideLocalBrowser(Side), MaskParams);
  3471. UnicodeString Counter;
  3472. UnicodeString LocalFileName;
  3473. bool Handled = false;
  3474. if (!IsSideLocalBrowser(Side))
  3475. {
  3476. // We need to trim VMS version here, so that we use name without version
  3477. // when uploading back to create a new version of the file
  3478. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3479. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3480. TObject * Token = NULL;
  3481. UnicodeString LocalDirectory;
  3482. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3483. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3484. LocalDirectory))
  3485. {
  3486. if (Token != NULL)
  3487. {
  3488. TForm * Form = dynamic_cast<TForm *>(Token);
  3489. if (Form->WindowState == wsMinimized)
  3490. {
  3491. ShowWindow(Form->Handle, SW_RESTORE);
  3492. }
  3493. else
  3494. {
  3495. Form->SetFocus();
  3496. }
  3497. Handled = true;
  3498. }
  3499. else
  3500. {
  3501. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3502. }
  3503. }
  3504. if (!Handled)
  3505. {
  3506. TStringList * FileList1 = new TStringList();
  3507. try
  3508. {
  3509. FileList1->AddObject(FullFileName, Object);
  3510. TemporarilyDownloadFiles(FileList1,
  3511. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3512. LocalRootDirectory, LocalDirectory, true, true, false);
  3513. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3514. }
  3515. __finally
  3516. {
  3517. delete FileList1;
  3518. }
  3519. switch (ExecuteFileBy)
  3520. {
  3521. case efShell:
  3522. Counter = "RemoteFilesExecuted";
  3523. break;
  3524. case efInternalEditor:
  3525. Counter = "RemoteFilesOpenedInInternalEditor";
  3526. break;
  3527. case efExternalEditor:
  3528. Counter = "RemoteFilesOpenedInExternalEditor";
  3529. break;
  3530. default:
  3531. DebugFail();
  3532. }
  3533. }
  3534. }
  3535. else
  3536. {
  3537. LocalFileName = FullFileName;
  3538. OriginalFileName = ExtractFileName(FullFileName);
  3539. switch (ExecuteFileBy)
  3540. {
  3541. case efShell:
  3542. Counter = "LocalFilesExecuted";
  3543. break;
  3544. case efInternalEditor:
  3545. Counter = "LocalFilesOpenedInInternalEditor";
  3546. break;
  3547. case efExternalEditor:
  3548. Counter = "LocalFilesOpenedInExternalEditor";
  3549. break;
  3550. default:
  3551. DebugFail();
  3552. }
  3553. }
  3554. if (!Handled)
  3555. {
  3556. Configuration->Usage->Inc(Counter);
  3557. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3558. ExternalEditor, LocalRootDirectory, RemoteDirectory, false, MaskParams.Modification);
  3559. }
  3560. }
  3561. //---------------------------------------------------------------------------
  3562. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3563. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3564. bool AllSelected, bool OnFocused)
  3565. {
  3566. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3567. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3568. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3569. Side = GetSide(Side);
  3570. TCustomDirView * DView = DirView(Side);
  3571. TStrings * FileList =
  3572. AllSelected ?
  3573. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3574. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3575. try
  3576. {
  3577. DebugAssert(AllSelected || (FileList->Count == 1));
  3578. for (int i = 0; i < FileList->Count; i++)
  3579. {
  3580. UnicodeString ListFileName = FileList->Strings[i];
  3581. UnicodeString FileNameOnly =
  3582. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3583. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3584. if (!DView->ItemIsDirectory(Item))
  3585. {
  3586. UnicodeString FullFileName;
  3587. if (!IsSideLocalBrowser(Side))
  3588. {
  3589. FullFileName = DirView(Side)->Path + ListFileName;
  3590. }
  3591. else
  3592. {
  3593. FullFileName = ListFileName;
  3594. }
  3595. TObject * Object = FileList->Objects[i];
  3596. TFileMasks::TParams MaskParams;
  3597. MaskParams.Size = DView->ItemFileSize(Item);
  3598. TDateTimePrecision Precision;
  3599. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3600. std::unique_ptr<TRemoteFile> File;
  3601. if (!IsSideLocalBrowser(Side))
  3602. {
  3603. File.reset(Terminal->TryReadFile(FullFileName));
  3604. if (File.get() != NULL)
  3605. {
  3606. File->FullFileName = FullFileName;
  3607. Object = File.get();
  3608. MaskParams.Size = File->Size;
  3609. MaskParams.Modification = File->Modification;
  3610. }
  3611. }
  3612. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName, Object, MaskParams);
  3613. }
  3614. }
  3615. }
  3616. __finally
  3617. {
  3618. delete FileList;
  3619. }
  3620. }
  3621. //---------------------------------------------------------------------------
  3622. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3623. {
  3624. CopyParam.FileNameCase = ncNoChange;
  3625. CopyParam.PreserveRights = false;
  3626. CopyParam.PreserveReadOnly = false;
  3627. CopyParam.ReplaceInvalidChars = true;
  3628. CopyParam.IncludeFileMask = TFileMasks();
  3629. CopyParam.NewerOnly = false;
  3630. CopyParam.FileMask = L"";
  3631. }
  3632. //---------------------------------------------------------------------------
  3633. bool TCustomScpExplorerForm::EditorCheckNotModified(const TEditedFileData * Data)
  3634. {
  3635. return WinConfiguration->EditorCheckNotModified && (Data->SourceTimestamp != TDateTime());
  3636. }
  3637. //---------------------------------------------------------------------------
  3638. class TEditorUploadQueueItem : public TUploadQueueItem
  3639. {
  3640. public:
  3641. __fastcall TEditorUploadQueueItem(
  3642. TTerminal * Terminal, TStrings * FilesToCopy, const UnicodeString & TargetDir,
  3643. const TCopyParamType * CopyParam, int Params) :
  3644. TUploadQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, true, false)
  3645. {
  3646. }
  3647. protected:
  3648. virtual void __fastcall DoTransferExecute(TTerminal * Terminal, TParallelOperation * ParallelOperation)
  3649. {
  3650. TUploadQueueItem::DoTransferExecute(Terminal, ParallelOperation);
  3651. TTerminalManager::Instance()->ScpExplorer->EditedFileUploaded(Terminal, CompleteEvent);
  3652. }
  3653. };
  3654. //---------------------------------------------------------------------------
  3655. void TCustomScpExplorerForm::EditedFileUploaded(TTerminal * ATerminal, HANDLE UploadCompleteEvent)
  3656. {
  3657. if (WinConfiguration->EditorCheckNotModified) // optimization
  3658. {
  3659. UnicodeString RemoteFilePath;
  3660. {
  3661. TGuard Guard(FEditorManager->Section);
  3662. TEditedFileData * Data = FEditorManager->FindByUploadCompleteEvent(UploadCompleteEvent);
  3663. if (DebugAlwaysTrue(Data != NULL) &&
  3664. EditorCheckNotModified(Data))
  3665. {
  3666. RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3667. }
  3668. }
  3669. if (!RemoteFilePath.IsEmpty())
  3670. {
  3671. std::unique_ptr<TRemoteFile> File(ATerminal->TryReadFile(RemoteFilePath));
  3672. if (File.get() != NULL)
  3673. {
  3674. TGuard Guard(FEditorManager->Section);
  3675. TEditedFileData * Data = FEditorManager->FindByUploadCompleteEvent(UploadCompleteEvent);
  3676. if (DebugAlwaysTrue(Data != NULL))
  3677. {
  3678. Data->SourceTimestamp = File->Modification;
  3679. AppLogFmt(L"Uploaded edited remote file timestamp: %s", (StandardTimestamp(Data->SourceTimestamp)));
  3680. }
  3681. }
  3682. }
  3683. }
  3684. }
  3685. //---------------------------------------------------------------------------
  3686. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
  3687. const UnicodeString & FileName, TEditedFileData * Data, HANDLE UploadCompleteEvent, bool & Retry)
  3688. {
  3689. TTerminalManager * Manager = TTerminalManager::Instance();
  3690. if ((Manager->ActiveTerminal == Data->Terminal) &&
  3691. Manager->ScheduleTerminalReconnnect(Data->Terminal))
  3692. {
  3693. AppLog(L"Scheduled session reconnect, will retry the upload later");
  3694. Retry = true;
  3695. }
  3696. else if (!IsActiveTerminal(Data->Terminal))
  3697. {
  3698. if (!NonVisualDataModule->Busy)
  3699. {
  3700. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3701. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3702. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3703. if (Data->Terminal == NULL)
  3704. {
  3705. TManagedTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3706. if (SameSiteTerminal != NULL)
  3707. {
  3708. UnicodeString Message =
  3709. FMTLOAD(EDIT_SESSION_REATTACH,
  3710. (FileNameOnly, Data->SessionName, FileNameOnly));
  3711. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3712. {
  3713. Data->Terminal = SameSiteTerminal;
  3714. Data->SessionName = SameSiteTerminal->SessionData->SessionName;
  3715. // We might also overwrite session data
  3716. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3717. }
  3718. else
  3719. {
  3720. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3721. Abort();
  3722. }
  3723. }
  3724. }
  3725. // Foreground session should reconnect itself (except for those disconnected explicitly by the user)
  3726. else if (DebugAlwaysTrue(Terminal->Disconnected || (Terminal != Data->Terminal)))
  3727. {
  3728. UnicodeString Message =
  3729. MainInstructions(
  3730. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3731. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3732. {
  3733. if (Terminal->Disconnected)
  3734. {
  3735. Manager->ActiveSession = Terminal;
  3736. ReconnectSession();
  3737. // Was it reconnected? (if not, Data->Terminal should be nulled now)
  3738. if (!IsActiveTerminal(Data->Terminal) || (Terminal != Data->Terminal))
  3739. {
  3740. Abort();
  3741. }
  3742. }
  3743. else
  3744. {
  3745. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3746. }
  3747. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3748. }
  3749. else
  3750. {
  3751. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3752. Abort();
  3753. }
  3754. }
  3755. }
  3756. if (!IsActiveTerminal(Data->Terminal))
  3757. {
  3758. Configuration->Usage->Inc(L"EditInactiveSession");
  3759. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3760. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3761. (ExtractFileName(FileName), Data->SessionName)));
  3762. }
  3763. }
  3764. if (!Retry && EditorCheckNotModified(Data))
  3765. {
  3766. if (NonVisualDataModule->Busy)
  3767. {
  3768. Retry = true;
  3769. }
  3770. else
  3771. {
  3772. UnicodeString RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3773. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(RemoteFilePath));
  3774. if (File.get() != NULL)
  3775. {
  3776. AppLogFmt(L"Edited remote file timestamp: %s, Original timestamp: %s", (StandardTimestamp(File->Modification), StandardTimestamp(Data->SourceTimestamp)));
  3777. if (File->Modification != Data->SourceTimestamp)
  3778. {
  3779. UnicodeString Message = MainInstructions(LoadStr(EDIT_CHANGED_EXTERNALLY));
  3780. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) != qaOK)
  3781. {
  3782. Abort();
  3783. }
  3784. }
  3785. }
  3786. }
  3787. }
  3788. if (!Retry)
  3789. {
  3790. TStrings * FileList = new TStringList();
  3791. try
  3792. {
  3793. FileList->Add(FileName);
  3794. // Consider using the same settings (preset) as when the file was downloaded.
  3795. // More over this does not reflect the actual session that will do the upload.
  3796. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3797. TemporaryFileCopyParam(CopyParam);
  3798. if (Data->ForceText)
  3799. {
  3800. CopyParam.TransferMode = tmAscii;
  3801. }
  3802. // so i do not need to worry if masking algorithm works in all cases
  3803. // ("" means "copy file name", no masking is actually done)
  3804. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3805. {
  3806. CopyParam.FileMask = L"";
  3807. }
  3808. else
  3809. {
  3810. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3811. }
  3812. int Params = cpNoConfirmation | cpTemporary;
  3813. if (Data->Terminal->IsCapable[fcBackgroundTransfers])
  3814. {
  3815. TTerminalQueue * AQueue = Manager->FindQueueForTerminal(Data->Terminal);
  3816. TQueueItem * QueueItem =
  3817. new TEditorUploadQueueItem(Data->Terminal, FileList, Data->RemoteDirectory, &CopyParam, Params);
  3818. QueueItem->CompleteEvent = UploadCompleteEvent;
  3819. AddQueueItem(AQueue, QueueItem, Data->Terminal);
  3820. }
  3821. else
  3822. {
  3823. if (NonVisualDataModule->Busy)
  3824. {
  3825. Retry = true;
  3826. }
  3827. else
  3828. {
  3829. Data->Terminal->CopyToRemote(FileList, Data->RemoteDirectory, &CopyParam, Params, NULL);
  3830. SetEvent(UploadCompleteEvent);
  3831. }
  3832. }
  3833. }
  3834. __finally
  3835. {
  3836. delete FileList;
  3837. }
  3838. }
  3839. }
  3840. //---------------------------------------------------------------------------
  3841. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3842. const UnicodeString & FileName, TEditedFileData * Data)
  3843. {
  3844. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3845. DebugAssert(!NonVisualDataModule->Busy);
  3846. if (!IsActiveTerminal(Data->Terminal))
  3847. {
  3848. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3849. (ExtractFileName(FileName), Data->SessionName)));
  3850. }
  3851. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3852. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3853. NonVisualDataModule->StartBusy();
  3854. try
  3855. {
  3856. std::unique_ptr<TRemoteFile> File;
  3857. UnicodeString RemoteFileName =
  3858. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3859. Terminal->ExceptionOnFail = true;
  3860. try
  3861. {
  3862. File.reset(Terminal->ReadFile(RemoteFileName));
  3863. if (!File->HaveFullFileName)
  3864. {
  3865. File->FullFileName = RemoteFileName;
  3866. }
  3867. }
  3868. __finally
  3869. {
  3870. Terminal->ExceptionOnFail = false;
  3871. }
  3872. std::unique_ptr<TStrings> FileList(new TStringList());
  3873. FileList->AddObject(RemoteFileName, File.get());
  3874. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3875. UnicodeString TempDir = ExtractFilePath(FileName);
  3876. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, true, false);
  3877. if (EditorCheckNotModified(Data))
  3878. {
  3879. Data->SourceTimestamp = File->Modification;
  3880. }
  3881. // sanity check, the target file name should be still the same
  3882. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3883. }
  3884. __finally
  3885. {
  3886. NonVisualDataModule->EndBusy();
  3887. // it actually may not exist anymore...
  3888. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3889. }
  3890. }
  3891. //---------------------------------------------------------------------------
  3892. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3893. const TEditedFileData * Data, bool & KeepOpen)
  3894. {
  3895. // Command is set for external editors only (not for "shell" open).
  3896. if (!Data->Command.IsEmpty())
  3897. {
  3898. bool AnyFound = false;
  3899. bool AnyMDI = false;
  3900. bool AnyNonMDI = false;
  3901. bool AnyDetect = false;
  3902. TEditorList * EditorList = new TEditorList();
  3903. try
  3904. {
  3905. *EditorList = *WinConfiguration->EditorList;
  3906. for (int i = 0; i < EditorList->Count; i++)
  3907. {
  3908. const TEditorPreferences * Editor = EditorList->Editors[i];
  3909. if ((Editor->Data->Editor == edExternal) &&
  3910. (Editor->Data->ExternalEditor == Data->Command))
  3911. {
  3912. AnyFound = true;
  3913. if (Editor->Data->SDIExternalEditor)
  3914. {
  3915. AnyNonMDI = true;
  3916. if (Editor->Data->DetectMDIExternalEditor)
  3917. {
  3918. AnyDetect = true;
  3919. }
  3920. }
  3921. else
  3922. {
  3923. AnyMDI = true;
  3924. }
  3925. }
  3926. }
  3927. bool EnableMDI = false;
  3928. bool DisableDetect = false;
  3929. if (AnyMDI)
  3930. {
  3931. KeepOpen = true;
  3932. if (AnyNonMDI)
  3933. {
  3934. // there is at least one instance of the editor with MDI support enabled,
  3935. // and one with disabled, enable it for all instances
  3936. EnableMDI = true;
  3937. }
  3938. }
  3939. else if (AnyFound && !AnyDetect)
  3940. {
  3941. // at least once instance found but all have MDI autodetection disabled
  3942. // => close the file (default action)
  3943. }
  3944. else
  3945. {
  3946. // no instance of the editor has MDI support enabled
  3947. TMessageParams Params;
  3948. if (AnyFound)
  3949. {
  3950. // there is at least one instance of the editor
  3951. Params.Params |= mpNeverAskAgainCheck;
  3952. }
  3953. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3954. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3955. switch (Answer)
  3956. {
  3957. case qaNeverAskAgain:
  3958. DisableDetect = true;
  3959. break;
  3960. case qaNo:
  3961. EnableMDI = true;
  3962. KeepOpen = true;
  3963. break;
  3964. }
  3965. }
  3966. if (AnyFound && (EnableMDI || DisableDetect))
  3967. {
  3968. bool Changed = false;
  3969. for (int i = 0; i < EditorList->Count; i++)
  3970. {
  3971. const TEditorPreferences * Editor = EditorList->Editors[i];
  3972. if ((Editor->Data->Editor == edExternal) &&
  3973. (Editor->Data->ExternalEditor == Data->Command) &&
  3974. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3975. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3976. {
  3977. Changed = true;
  3978. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3979. if (EnableMDI)
  3980. {
  3981. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3982. }
  3983. if (DisableDetect)
  3984. {
  3985. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3986. }
  3987. EditorList->Change(i, UpdatedEditor);
  3988. }
  3989. }
  3990. if (Changed)
  3991. {
  3992. WinConfiguration->EditorList = EditorList;
  3993. }
  3994. }
  3995. }
  3996. __finally
  3997. {
  3998. delete EditorList;
  3999. }
  4000. }
  4001. else
  4002. {
  4003. // "open" case
  4004. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  4005. qaOK, HELP_APP_EARLY_CLOSED);
  4006. }
  4007. }
  4008. //---------------------------------------------------------------------------
  4009. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  4010. {
  4011. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  4012. }
  4013. //---------------------------------------------------------------------------
  4014. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  4015. {
  4016. SideEnter(osRemote);
  4017. }
  4018. //---------------------------------------------------------------------------
  4019. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  4020. {
  4021. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  4022. SideEnter(osRemote);
  4023. }
  4024. //---------------------------------------------------------------------------
  4025. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  4026. {
  4027. FCurrentSide = Side;
  4028. if (Visible)
  4029. {
  4030. UpdateControls();
  4031. }
  4032. }
  4033. //---------------------------------------------------------------------------
  4034. void __fastcall TCustomScpExplorerForm::FileDeleted(TOperationSide Side, const UnicodeString & FileName, bool Success)
  4035. {
  4036. VisualiseOperationFinished(Side, FileName, Success);
  4037. if (DebugAlwaysTrue(FDeletedFiles != NULL) && Success)
  4038. {
  4039. FDeletedFiles->Add(FileName);
  4040. }
  4041. }
  4042. //---------------------------------------------------------------------------
  4043. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  4044. TStrings * FileList, bool Alternative)
  4045. {
  4046. TCustomDirView * DView = DirView(Side);
  4047. DView->SaveSelection();
  4048. DView->SaveSelectedNames();
  4049. DebugAssert(!FAlternativeDelete);
  4050. FAlternativeDelete = Alternative;
  4051. try
  4052. {
  4053. if (!IsSideLocalBrowser(Side))
  4054. {
  4055. DebugAssert(Terminal != NULL);
  4056. // Clone the file list as it may refer to current directory files, which get destroyed, when the directory is reloaded after the operation
  4057. std::unique_ptr<TStrings> PermanentFileList(TRemoteFileList::CloneStrings(FileList));
  4058. std::unique_ptr<TStringList> DeletedFiles(new TStringList());
  4059. DeletedFiles->CaseSensitive = DebugAlwaysTrue(!IsSideLocalBrowser(Side));
  4060. int Params = FLAGMASK(Alternative, dfAlternative);
  4061. try
  4062. {
  4063. TValueRestorer<TStrings *> DeletedFilesRestorer(FDeletedFiles);
  4064. DebugAssert(FDeletedFiles == NULL);
  4065. FDeletedFiles = DeletedFiles.get();
  4066. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  4067. FOnFileOperationFinished = FileDeleted;
  4068. FMoveToQueue = false;
  4069. Terminal->DeleteFiles(FileList, Params);
  4070. // Probably not needed for deleting, just for consistency with transfer code
  4071. if (FMoveToQueue)
  4072. {
  4073. Abort();
  4074. }
  4075. }
  4076. catch (EAbort &)
  4077. {
  4078. if (FMoveToQueue)
  4079. {
  4080. DeletedFiles->Sorted = true;
  4081. for (int Index = 0; Index < PermanentFileList->Count; Index++)
  4082. {
  4083. if (DeletedFiles->IndexOf(PermanentFileList->Strings[Index]) >= 0)
  4084. {
  4085. // We should always be deleting the first item => what can be used to optimize this code, if needed
  4086. DebugAssert(Index == 0);
  4087. PermanentFileList->Delete(Index);
  4088. Index--;
  4089. }
  4090. }
  4091. Configuration->Usage->Inc("MovesToBackgroundDelete");
  4092. TQueueItem * QueueItem = new TDeleteQueueItem(Terminal, PermanentFileList.get(), Params);
  4093. AddQueueItem(Queue, QueueItem, Terminal);
  4094. ClearOperationSelection(osRemote);
  4095. }
  4096. }
  4097. }
  4098. else
  4099. {
  4100. if (IsLocalBrowserMode())
  4101. {
  4102. Configuration->Usage->Inc(L"LocalLocalDeletes");
  4103. }
  4104. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  4105. FProgressSide = FCurrentSide;
  4106. try
  4107. {
  4108. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  4109. }
  4110. __finally
  4111. {
  4112. ReloadLocalDirectory();
  4113. }
  4114. }
  4115. FAlternativeDelete = false;
  4116. }
  4117. catch(...)
  4118. {
  4119. FAlternativeDelete = false;
  4120. DView->DiscardSavedSelection();
  4121. throw;
  4122. }
  4123. DView->RestoreSelection();
  4124. }
  4125. //---------------------------------------------------------------------------
  4126. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  4127. {
  4128. DebugAssert(Terminal);
  4129. DebugAssert(!IsLocalBrowserMode());
  4130. RemoteDirView->SaveSelection();
  4131. RemoteDirView->SaveSelectedNames();
  4132. try
  4133. {
  4134. if (Lock)
  4135. {
  4136. Terminal->LockFiles(FileList);
  4137. }
  4138. else
  4139. {
  4140. Terminal->UnlockFiles(FileList);
  4141. }
  4142. }
  4143. catch(...)
  4144. {
  4145. RemoteDirView->DiscardSavedSelection();
  4146. throw;
  4147. }
  4148. RemoteDirView->RestoreSelection();
  4149. }
  4150. //---------------------------------------------------------------------------
  4151. bool TCustomScpExplorerForm::DoDirectoryExists(void * Session, const UnicodeString & Directory)
  4152. {
  4153. bool Result = false;
  4154. TTerminal * ATerminal = (Session == NULL) ? Terminal : reinterpret_cast<TTerminal *>(Session);
  4155. if (IsActiveTerminal(ATerminal))
  4156. {
  4157. std::unique_ptr<TRemoteFile> File(ATerminal->TryReadFile(Directory));
  4158. Result = File && File->IsDirectory;
  4159. }
  4160. return Result;
  4161. }
  4162. //---------------------------------------------------------------------------
  4163. bool TCustomScpExplorerForm::NeedSecondarySessionForRemoteCopy(TStrings * FileList)
  4164. {
  4165. bool CopyDirsOnSecondarySession = Terminal->IsCapable[fcSecondaryShell];
  4166. return
  4167. !Terminal->IsCapable[fcRemoteCopy] ||
  4168. (CopyDirsOnSecondarySession && TRemoteFileList::AnyDirectory(FileList));
  4169. }
  4170. //---------------------------------------------------------------------------
  4171. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  4172. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  4173. bool NoConfirmation, bool Move)
  4174. {
  4175. DebugAssert(Terminal != NULL);
  4176. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  4177. DebugAssert(!IsLocalBrowserMode());
  4178. if (Session == NULL)
  4179. {
  4180. Session = Terminal;
  4181. }
  4182. bool TargetConfirmed = false;
  4183. if (Session == Terminal)
  4184. {
  4185. if (RemoteDriveView->DropTarget != NULL)
  4186. {
  4187. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  4188. TargetConfirmed = true;
  4189. }
  4190. else if (RemoteDirView->DropTarget != NULL)
  4191. {
  4192. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  4193. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  4194. TargetConfirmed = true;
  4195. }
  4196. else
  4197. {
  4198. Target = RemoteDirView->Path;
  4199. }
  4200. }
  4201. else
  4202. {
  4203. Target = GetSessionPath(Session, osRemote);
  4204. }
  4205. Target = UnixIncludeTrailingBackslash(Target);
  4206. if (FileList->Count == 1)
  4207. {
  4208. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  4209. }
  4210. else
  4211. {
  4212. FileMask = AnyMask;
  4213. }
  4214. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  4215. bool Result = true;
  4216. if (!NoConfirmation)
  4217. {
  4218. bool Multi = (FileList->Count > 1);
  4219. if (Move)
  4220. {
  4221. Result = DoRemoteMoveDialog(Multi, Target, FileMask, DoDirectoryExists);
  4222. }
  4223. else
  4224. {
  4225. std::unique_ptr<TStrings> Sessions(new TStringList);
  4226. std::unique_ptr<TStrings> Directories(new TStringList);
  4227. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  4228. for (int Index = 0; Index < SessionList->Count; Index++)
  4229. {
  4230. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  4231. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  4232. {
  4233. Sessions->AddObject(SessionList->Strings[Index], ASession);
  4234. Directories->Add(GetSessionPath(ASession, osRemote));
  4235. }
  4236. }
  4237. bool AnyDirectory = false;
  4238. bool CopyDirsOnSecondarySession = Terminal->IsCapable[fcSecondaryShell];
  4239. if (CopyDirsOnSecondarySession && !Terminal->CommandSessionOpened) // optimization
  4240. {
  4241. for (int Index = 0; !AnyDirectory && (Index < FileList->Count); Index++)
  4242. {
  4243. AnyDirectory = DebugNotNull(dynamic_cast<TRemoteFile *>(FileList->Objects[Index]))->IsDirectory;
  4244. }
  4245. }
  4246. TDirectRemoteCopy AllowDirectCopy;
  4247. if (Terminal->CommandSessionOpened || !NeedSecondarySessionForRemoteCopy(FileList))
  4248. {
  4249. DebugAssert(DirectCopy);
  4250. AllowDirectCopy = drcAllow;
  4251. }
  4252. else if (Terminal->IsCapable[fcSecondaryShell])
  4253. {
  4254. DebugAssert(DirectCopy);
  4255. AllowDirectCopy = Terminal->IsCapable[fcRemoteCopy] ? drcConfirmCommandSessionDirs : drcConfirmCommandSession;
  4256. }
  4257. else
  4258. {
  4259. DebugAssert(!DirectCopy);
  4260. AllowDirectCopy = drcDisallow;
  4261. }
  4262. void * ASession = Session;
  4263. Result = DoRemoteCopyDialog(
  4264. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal,
  4265. DoDirectoryExists, TargetConfirmed);
  4266. Session = static_cast<TManagedTerminal *>(ASession);
  4267. }
  4268. }
  4269. return Result;
  4270. }
  4271. //---------------------------------------------------------------------------
  4272. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  4273. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  4274. {
  4275. DebugAssert(!IsLocalBrowserMode());
  4276. bool DirectCopy;
  4277. UnicodeString Target, FileMask;
  4278. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  4279. if (Result)
  4280. {
  4281. if (!Move && !DirectCopy)
  4282. {
  4283. Configuration->Usage->Inc("RemoteCopyTemp");
  4284. UnicodeString RootTempDir;
  4285. UnicodeString TempDir;
  4286. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  4287. TStrings * TemporaryFilesList = new TStringList();
  4288. try
  4289. {
  4290. TMakeLocalFileListParams MakeFileListParam;
  4291. MakeFileListParam.FileList = TemporaryFilesList;
  4292. MakeFileListParam.FileTimes = NULL;
  4293. MakeFileListParam.IncludeDirs = true;
  4294. MakeFileListParam.Recursive = false;
  4295. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  4296. TTerminalManager::Instance()->ActiveSession = Session;
  4297. if (TemporaryFilesList->Count > 0)
  4298. {
  4299. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4300. CopyParam.FileMask = FileMask;
  4301. DebugAssert(!FAutoOperation);
  4302. FAutoOperation = true;
  4303. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  4304. }
  4305. }
  4306. __finally
  4307. {
  4308. delete TemporaryFilesList;
  4309. FAutoOperation = false;
  4310. if (!RootTempDir.IsEmpty())
  4311. {
  4312. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  4313. }
  4314. }
  4315. }
  4316. else
  4317. {
  4318. RemoteDirView->SaveSelection();
  4319. RemoteDirView->SaveSelectedNames();
  4320. try
  4321. {
  4322. if (Move)
  4323. {
  4324. Configuration->Usage->Inc("RemoteMove");
  4325. Terminal->MoveFiles(FileList, Target, FileMask, false);
  4326. }
  4327. else
  4328. {
  4329. Configuration->Usage->Inc("RemoteCopyDirect");
  4330. DebugAssert(DirectCopy);
  4331. DebugAssert(Session == Terminal);
  4332. if (!NeedSecondarySessionForRemoteCopy(FileList) ||
  4333. Terminal->CommandSessionOpened ||
  4334. CommandSessionFallback())
  4335. {
  4336. Terminal->CopyFiles(FileList, Target, FileMask, false);
  4337. }
  4338. }
  4339. }
  4340. catch(...)
  4341. {
  4342. RemoteDirView->DiscardSavedSelection();
  4343. throw;
  4344. }
  4345. RemoteDirView->RestoreSelection();
  4346. }
  4347. }
  4348. return Result;
  4349. }
  4350. //---------------------------------------------------------------------------
  4351. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4352. const UnicodeString & Path, TRemoteProperties & Properties)
  4353. {
  4354. DebugAssert(!IsLocalBrowserMode());
  4355. Properties.Valid = Properties.Valid << vpEncrypt;
  4356. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4357. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4358. }
  4359. //---------------------------------------------------------------------------
  4360. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4361. {
  4362. Side = GetSide(Side);
  4363. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4364. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4365. UnicodeString Name = LoadStr(NEW_FOLDER);
  4366. int AllowedChanges =
  4367. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4368. bool SaveSettings = false;
  4369. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4370. {
  4371. TWindowLock Lock(this);
  4372. if (!IsSideLocalBrowser(Side))
  4373. {
  4374. if (SaveSettings)
  4375. {
  4376. GUIConfiguration->NewDirectoryProperties = Properties;
  4377. }
  4378. CreateRemoteDirectory(Name, Properties);
  4379. }
  4380. else
  4381. {
  4382. DirView(Side)->CreateDirectory(Name);
  4383. }
  4384. }
  4385. }
  4386. //---------------------------------------------------------------------------
  4387. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4388. {
  4389. TWindowLock Lock(this);
  4390. DirView(Side)->ExecuteHomeDirectory();
  4391. }
  4392. //---------------------------------------------------------------------------
  4393. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4394. {
  4395. DoOpenDirectoryDialog(odBrowse, Side);
  4396. }
  4397. //---------------------------------------------------------------------------
  4398. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4399. {
  4400. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4401. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4402. bool Result = !Path.IsEmpty();
  4403. if (Result)
  4404. {
  4405. TryOpenDirectory(Side, Path);
  4406. }
  4407. return Result;
  4408. }
  4409. //---------------------------------------------------------------------------
  4410. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4411. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4412. bool & Close)
  4413. {
  4414. // terminal can be already closed (e.g. dropped connection)
  4415. if (Terminal != NULL)
  4416. {
  4417. try
  4418. {
  4419. TCalculateSizeParams Params;
  4420. Params.Stats = &Stats;
  4421. Terminal->CalculateFilesSize(FileList, Size, Params);
  4422. }
  4423. catch(...)
  4424. {
  4425. if (!Terminal->Active)
  4426. {
  4427. Close = true;
  4428. }
  4429. throw;
  4430. }
  4431. }
  4432. }
  4433. //---------------------------------------------------------------------------
  4434. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4435. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4436. bool & Close)
  4437. {
  4438. if ((Terminal != NULL) && // terminal can be already closed (e.g. dropped connection)
  4439. EnsureCommandSessionFallback(fcCalculatingChecksum))
  4440. {
  4441. Configuration->Usage->Inc(L"ChecksumCalculated");
  4442. try
  4443. {
  4444. Terminal->CalculateFilesChecksum(Alg, FileList, OnCalculatedChecksum);
  4445. }
  4446. catch(...)
  4447. {
  4448. if (!Terminal->Active)
  4449. {
  4450. Close = true;
  4451. }
  4452. throw;
  4453. }
  4454. }
  4455. }
  4456. //---------------------------------------------------------------------------
  4457. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4458. {
  4459. if (Terminal->LoadFilesProperties(FileList))
  4460. {
  4461. RemoteDirView->Invalidate();
  4462. }
  4463. }
  4464. //---------------------------------------------------------------------------
  4465. bool TCustomScpExplorerForm::CanCalculateChecksum()
  4466. {
  4467. return
  4468. Terminal->IsCapable[fcCalculatingChecksum] ||
  4469. (Terminal->IsCapable[fcSecondaryShell] && !Terminal->IsEncryptingFiles());
  4470. }
  4471. //---------------------------------------------------------------------------
  4472. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4473. {
  4474. bool Result;
  4475. if (!IsSideLocalBrowser(Side))
  4476. {
  4477. TRemoteTokenList * GroupList = NULL;
  4478. TRemoteTokenList * UserList = NULL;
  4479. try
  4480. {
  4481. TRemoteProperties CurrentProperties;
  4482. LoadFilesProperties(FileList);
  4483. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4484. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4485. if (CapableGroupChanging || CapableOwnerChanging)
  4486. {
  4487. if (CapableGroupChanging)
  4488. {
  4489. GroupList = Terminal->Groups->Duplicate();
  4490. }
  4491. if (CapableOwnerChanging)
  4492. {
  4493. UserList = Terminal->Users->Duplicate();
  4494. }
  4495. TRemoteDirectory * Files = Terminal->Files;
  4496. int Count = Files->Count;
  4497. if (Count > 100)
  4498. {
  4499. Count = 100;
  4500. }
  4501. for (int Index = 0; Index < Count; Index++)
  4502. {
  4503. TRemoteFile * File = Files->Files[Index];
  4504. if (CapableGroupChanging)
  4505. {
  4506. GroupList->AddUnique(File->Group);
  4507. }
  4508. if (CapableOwnerChanging)
  4509. {
  4510. UserList->AddUnique(File->Owner);
  4511. }
  4512. }
  4513. // if we haven't collected tokens for all files in current directory,
  4514. // make sure we collect them at least for all selected files.
  4515. // (note that so far the files in FileList has to be from current direcotry)
  4516. if (Count < Files->Count)
  4517. {
  4518. for (int Index = 0; Index < FileList->Count; Index++)
  4519. {
  4520. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4521. if (CapableGroupChanging)
  4522. {
  4523. GroupList->AddUnique(File->Group);
  4524. }
  4525. if (CapableOwnerChanging)
  4526. {
  4527. UserList->AddUnique(File->Owner);
  4528. }
  4529. }
  4530. }
  4531. }
  4532. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4533. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4534. if (CapableAclChanging && TRemoteFileList::AnyDirectory(FileList))
  4535. {
  4536. CapableAclChanging = false;
  4537. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4538. }
  4539. int Flags = 0;
  4540. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4541. if (CapableAclChanging) Flags |= cpAcl;
  4542. if (CapableOwnerChanging) Flags |= cpOwner;
  4543. if (CapableGroupChanging) Flags |= cpGroup;
  4544. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4545. if (CanCalculateChecksum())
  4546. {
  4547. CalculateChecksumEvent = CalculateChecksum;
  4548. }
  4549. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4550. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4551. TRemoteProperties NewProperties = CurrentProperties;
  4552. Result =
  4553. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4554. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4555. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4556. CalculateSize, CalculateChecksumEvent);
  4557. if (Result)
  4558. {
  4559. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4560. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4561. }
  4562. }
  4563. __finally
  4564. {
  4565. delete GroupList;
  4566. delete UserList;
  4567. }
  4568. }
  4569. else
  4570. {
  4571. DirView(Side)->DisplayPropertiesMenu();
  4572. Result = true;
  4573. }
  4574. return Result;
  4575. }
  4576. //---------------------------------------------------------------------------
  4577. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4578. {
  4579. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4580. {
  4581. FIgnoreNextDialogChar = Key;
  4582. }
  4583. Key = 0;
  4584. }
  4585. //---------------------------------------------------------------------------
  4586. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4587. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4588. {
  4589. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4590. if (Command != NULL)
  4591. {
  4592. KeyProcessed(Key, Shift);
  4593. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4594. {
  4595. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4596. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4597. false, false, const_cast<TCustomCommandType *>(Command));
  4598. }
  4599. }
  4600. }
  4601. //---------------------------------------------------------------------------
  4602. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4603. {
  4604. if (!NonVisualDataModule->Busy)
  4605. {
  4606. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4607. {
  4608. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4609. }
  4610. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4611. {
  4612. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4613. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4614. {
  4615. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4616. // But so far it's not a problem, as it does what we want.
  4617. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4618. if (((Action->ShortCut == KeyShortCut) ||
  4619. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4620. AllowedAction(Action, aaShortCut))
  4621. {
  4622. // Has to be called before the action as the dialog char is already in queue.
  4623. // So when the action consumes message queue, we already need to have the
  4624. // FIgnoreNextDialogChar set
  4625. KeyProcessed(Key, Shift);
  4626. // Reset reference to previous component (when menu/toolbar was clicked).
  4627. // Needed to detect that action was invoked by keyboard shortcut
  4628. // in TNonVisualDataModule::ExplorerActionsExecute
  4629. Action->ActionComponent = NULL;
  4630. Action->Execute();
  4631. return;
  4632. }
  4633. }
  4634. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4635. {
  4636. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4637. {
  4638. KeyProcessed(Key, Shift);
  4639. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4640. return;
  4641. }
  4642. }
  4643. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4644. {
  4645. KeyProcessed(Key, Shift);
  4646. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4647. }
  4648. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4649. if (IsCustomShortCut(CustomShortCut))
  4650. {
  4651. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4652. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4653. if (WinConfiguration->SharedBookmarks != NULL)
  4654. {
  4655. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4656. if ((Bookmark != NULL) &&
  4657. OpenBookmark(FCurrentSide, Bookmark))
  4658. {
  4659. KeyProcessed(Key, Shift);
  4660. }
  4661. }
  4662. }
  4663. }
  4664. }
  4665. TForm::KeyDown(Key, Shift);
  4666. }
  4667. //---------------------------------------------------------------------------
  4668. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4669. {
  4670. bool OpenedTerminal = false;
  4671. UnicodeString ProtocolName;
  4672. UnicodeString ProtocolNameHint;
  4673. UnicodeString SecurityProtocolName;
  4674. UnicodeString DurationHint;
  4675. // See the comment in TScpCommanderForm::UpdateControls()
  4676. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4677. if (ActiveTerminal != NULL)
  4678. {
  4679. // Active is not enough here
  4680. if (ActiveTerminal->Status == ssOpened)
  4681. {
  4682. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4683. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4684. if (FileSystemInfo.ProtocolName.IsEmpty())
  4685. {
  4686. ProtocolName = SessionInfo.ProtocolName;
  4687. }
  4688. else
  4689. {
  4690. ProtocolName = FileSystemInfo.ProtocolName;
  4691. }
  4692. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4693. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4694. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4695. OpenedTerminal = true;
  4696. }
  4697. else if (ActiveTerminal->Status > ssClosed)
  4698. {
  4699. ProtocolName = LoadStr(STATUS_CONNECTING);
  4700. }
  4701. else
  4702. {
  4703. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4704. }
  4705. }
  4706. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4707. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4708. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4709. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4710. if (ProtocolStatusPanel->Caption != ProtocolName)
  4711. {
  4712. ProtocolStatusPanel->Caption = ProtocolName;
  4713. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4714. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4715. }
  4716. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4717. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4718. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4719. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4720. SecurityStatusPanel->Enabled = SecurityEnabled;
  4721. UnicodeString SecurityProtocolHint;
  4722. if (SecurityEnabled)
  4723. {
  4724. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4725. }
  4726. else
  4727. {
  4728. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4729. }
  4730. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4731. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4732. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4733. DurationStatusPanel->Hint = DurationHint;
  4734. UpdateStatusBar();
  4735. }
  4736. //---------------------------------------------------------------------------
  4737. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4738. {
  4739. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4740. DebugAssert(SessionStatusBar != NULL);
  4741. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4742. if (!FNote.IsEmpty())
  4743. {
  4744. NoteStatusPanel->Caption = FNote;
  4745. }
  4746. else
  4747. {
  4748. UpdateStatusPanelText(NoteStatusPanel);
  4749. }
  4750. NoteStatusPanel->Hint = FNoteHints;
  4751. UnicodeString LoginTime;
  4752. // See the comment in TScpCommanderForm::UpdateControls()
  4753. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4754. // Active is not enough here
  4755. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4756. {
  4757. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4758. LoginTime = FormatDateTimeSpan(Now() - SessionInfo.LoginTime);
  4759. }
  4760. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4761. DurationStatusPanel->Caption = LoginTime;
  4762. }
  4763. //---------------------------------------------------------------------------
  4764. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4765. {
  4766. Panel->Caption = L"";
  4767. }
  4768. //---------------------------------------------------------------------------
  4769. void __fastcall TCustomScpExplorerForm::Idle()
  4770. {
  4771. if (FShowing ||
  4772. // Particularly for
  4773. // - Standalone editing
  4774. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4775. // while "keeping remote directory up to date".
  4776. StandaloneOperation)
  4777. {
  4778. FEditorManager->Check();
  4779. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4780. // that needs to know if queue view is visible (and it may be closed after queue update)
  4781. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4782. }
  4783. if (FShowing)
  4784. {
  4785. if (!NonVisualDataModule->Busy &&
  4786. // Menu is opened or drag&drop is going on
  4787. (Mouse->Capture == NULL))
  4788. {
  4789. if (FRefreshRemoteDirectory)
  4790. {
  4791. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4792. {
  4793. Terminal->RefreshDirectory();
  4794. }
  4795. FRefreshRemoteDirectory = false;
  4796. }
  4797. if (FRefreshLocalDirectory)
  4798. {
  4799. ReloadLocalDirectory();
  4800. FRefreshLocalDirectory = false;
  4801. }
  4802. if (WinConfiguration->RefreshRemotePanel)
  4803. {
  4804. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4805. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4806. {
  4807. RemoteDirView->ReloadDirectory();
  4808. }
  4809. }
  4810. }
  4811. // TB2 shows accels on Alt key down, but hides it only after menu modal loop finishes.
  4812. // But if Alt is used to switch apps or for keyboard shortcut, the menu modal loop does not happen and accelerators stay displayed.
  4813. // It's difficult to fix this in TB2, as there's no "up" equivalent to CM_DIALOGKEY, which shows the accels.
  4814. // 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.
  4815. TTBCustomToolbar * MenuToolbar = DebugNotNull(dynamic_cast<TTBCustomToolbar *>(GetComponent(fcMenu)));
  4816. MenuToolbar->Idle();
  4817. }
  4818. if (FShowing || StandaloneOperation)
  4819. {
  4820. if (FQueueStatusInvalidated)
  4821. {
  4822. UpdateQueueStatus(false);
  4823. }
  4824. RefreshQueueItems();
  4825. }
  4826. if (FShowing)
  4827. {
  4828. UpdateStatusBar();
  4829. }
  4830. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4831. {
  4832. FClipboardFakeMonitorsPendingReset = false;
  4833. FClipboardFakeMonitors.reset(NULL);
  4834. }
  4835. FIgnoreNextDialogChar = 0;
  4836. }
  4837. //---------------------------------------------------------------------------
  4838. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4839. {
  4840. try
  4841. {
  4842. FUserActionTimer->Enabled = false;
  4843. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4844. {
  4845. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4846. {
  4847. FPendingQueueActionItem->ProcessUserAction();
  4848. }
  4849. }
  4850. }
  4851. __finally
  4852. {
  4853. FPendingQueueActionItem = NULL;
  4854. }
  4855. }
  4856. //---------------------------------------------------------------------------
  4857. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4858. {
  4859. if (WinConfiguration->MinimizeToTray)
  4860. {
  4861. UpdateTrayIcon();
  4862. FTrayIcon->Visible = true;
  4863. if (Visible)
  4864. {
  4865. ShowWindow(Handle, SW_HIDE);
  4866. }
  4867. }
  4868. }
  4869. //---------------------------------------------------------------------------
  4870. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4871. {
  4872. // WORKAROUND
  4873. // When restoring maximized window from minimization,
  4874. // rarely some controls do not align properly.
  4875. // Two instances seen (both for Commander):
  4876. // - When restoring, window is temporarily narrower (not maximized),
  4877. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4878. // Once maximized already, top dock shinks back, but the session PageControl,
  4879. // do not align up, leaving space between TopDock and PageControl.
  4880. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4881. for (int Index = 0; Index < ControlCount; Index++)
  4882. {
  4883. RealignControl(Controls[Index]);
  4884. }
  4885. if (FTrayIcon->Visible)
  4886. {
  4887. FTrayIcon->Visible = false;
  4888. if (Visible)
  4889. {
  4890. ShowWindow(Handle, SW_SHOW);
  4891. }
  4892. }
  4893. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4894. {
  4895. FNeedSession = false;
  4896. NonVisualDataModule->StartBusy();
  4897. try
  4898. {
  4899. NeedSession(true);
  4900. }
  4901. __finally
  4902. {
  4903. NonVisualDataModule->EndBusy();
  4904. }
  4905. }
  4906. }
  4907. //---------------------------------------------------------------------------
  4908. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4909. {
  4910. FTrayIcon->Hint = GetMainForm()->Caption;
  4911. }
  4912. //---------------------------------------------------------------------------
  4913. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4914. {
  4915. UpdateTrayIcon();
  4916. }
  4917. //---------------------------------------------------------------------------
  4918. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4919. {
  4920. // workaround
  4921. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4922. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4923. // But WM_ACTIVATEAPP message can be received even when
  4924. // the main window is minimized to the tray and internal editor window is focused
  4925. // (after another application was previously active)
  4926. if (::IsIconic(Handle))
  4927. {
  4928. if (!IsAppIconic())
  4929. {
  4930. SetAppIconic(true);
  4931. }
  4932. }
  4933. ::ApplicationRestore();
  4934. Application->BringToFront();
  4935. }
  4936. //---------------------------------------------------------------------------
  4937. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4938. {
  4939. RestoreApp();
  4940. }
  4941. //---------------------------------------------------------------------------
  4942. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4943. {
  4944. if (OpenInNewWindow())
  4945. {
  4946. ExecuteNewInstance(SessionUrl);
  4947. }
  4948. else
  4949. {
  4950. TTerminalManager::Instance()->NewSession(SessionUrl);
  4951. }
  4952. }
  4953. //---------------------------------------------------------------------------
  4954. void TCustomScpExplorerForm::NewTab(TOperationSide, bool)
  4955. {
  4956. NewSession();
  4957. }
  4958. //---------------------------------------------------------------------------
  4959. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4960. {
  4961. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4962. StoredSessions->NewSession(SessionName, SessionData);
  4963. // modified only, explicit
  4964. StoredSessions->Save(false, true);
  4965. // encode session name because of slashes in hierarchical sessions
  4966. return EncodeUrlString(SessionName);
  4967. }
  4968. //---------------------------------------------------------------------------
  4969. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4970. {
  4971. // current working directories become defaults here, what is not right
  4972. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4973. return SaveHiddenDuplicateSession(SessionData.get());
  4974. }
  4975. //---------------------------------------------------------------------------
  4976. void __fastcall TCustomScpExplorerForm::DuplicateTab()
  4977. {
  4978. if (OpenInNewWindow())
  4979. {
  4980. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4981. }
  4982. else
  4983. {
  4984. // current working directories become defaults here, what is not right
  4985. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4986. TTerminalManager * Manager = TTerminalManager::Instance();
  4987. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4988. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4989. // We definitely want these
  4990. ASession->Disconnected = ManagedSession->Disconnected;
  4991. // Not sure about these two
  4992. ASession->Permanent = ManagedSession->Permanent;
  4993. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4994. Manager->ActiveSession = ASession;
  4995. }
  4996. }
  4997. //---------------------------------------------------------------------------
  4998. void __fastcall TCustomScpExplorerForm::RenameTab()
  4999. {
  5000. UnicodeString Name = ManagedSession->SessionData->SessionName;
  5001. if (InputDialog(LoadStr(RENAME_TAB_TITLE), LoadStr(RENAME_TAB_PROMPT), Name, HELP_TAB_RENAME) &&
  5002. // When submitting the default name, do not fix it
  5003. (Name != ManagedSession->SessionData->SessionName))
  5004. {
  5005. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  5006. TSessionData::ValidateName(Name);
  5007. // This is inconsistent with how color (for example) is handled.
  5008. ManagedSession->SessionData->Name = Name;
  5009. UpdateControls();
  5010. // Add/Remove distinguishing paths from sessions of the same name.
  5011. SessionListChanged();
  5012. }
  5013. }
  5014. //---------------------------------------------------------------------------
  5015. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  5016. {
  5017. DebugAssert(Queue != NULL);
  5018. bool Result = Queue->IsEmpty;
  5019. if (!Result)
  5020. {
  5021. SetFocus();
  5022. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  5023. }
  5024. return Result;
  5025. }
  5026. //---------------------------------------------------------------------------
  5027. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  5028. {
  5029. return CanCloseQueue(FQueue);
  5030. }
  5031. //---------------------------------------------------------------------------
  5032. void __fastcall TCustomScpExplorerForm::CloseTab()
  5033. {
  5034. if (CanCloseQueue())
  5035. {
  5036. TTerminalManager::Instance()->FreeActiveTerminal();
  5037. }
  5038. }
  5039. //---------------------------------------------------------------------------
  5040. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  5041. {
  5042. if (CanCloseQueue())
  5043. {
  5044. TTerminalManager::Instance()->DisconnectActiveTerminal();
  5045. }
  5046. }
  5047. //---------------------------------------------------------------------------
  5048. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  5049. {
  5050. DetachTerminal(Terminal);
  5051. RemoteDirView->Terminal = Terminal;
  5052. UpdateRemotePathComboBox(false);
  5053. InitStatusBar();
  5054. UpdateControls();
  5055. }
  5056. //---------------------------------------------------------------------------
  5057. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  5058. {
  5059. InitStatusBar();
  5060. }
  5061. //---------------------------------------------------------------------------
  5062. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  5063. {
  5064. if (DebugAlwaysTrue(!Terminal->Active))
  5065. {
  5066. TTerminalManager::Instance()->ReconnectActiveTerminal();
  5067. }
  5068. }
  5069. //---------------------------------------------------------------------------
  5070. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  5071. {
  5072. if (OpenInNewWindow())
  5073. {
  5074. // encode session name because of slashes in hierarchical sessions
  5075. ExecuteNewInstance(EncodeUrlString(Data->Name));
  5076. }
  5077. else
  5078. {
  5079. TTerminalManager * Manager = TTerminalManager::Instance();
  5080. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  5081. Manager->ActiveSession = Terminal;
  5082. }
  5083. }
  5084. //---------------------------------------------------------------------------
  5085. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  5086. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  5087. {
  5088. TTerminalManager * Manager = TTerminalManager::Instance();
  5089. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5090. // This asks for a master password. Maybe we should postpone asking until a connection is actually opened.
  5091. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  5092. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  5093. {
  5094. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  5095. // FirstSession can be null, if none of the workspace sites exist anymore
  5096. // or if all workspace sessions are not suported by the interface
  5097. // (when trying to open all local-local workspace in "explorer" interface)
  5098. if (FirstSession != NULL)
  5099. {
  5100. if (!ConnectFirstTerminal)
  5101. {
  5102. FirstSession->Disconnected = true;
  5103. FirstSession->DisconnectedTemporarily = true;
  5104. }
  5105. Manager->ActiveSession = FirstSession;
  5106. }
  5107. }
  5108. }
  5109. //---------------------------------------------------------------------------
  5110. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  5111. {
  5112. if (OpenInNewWindow())
  5113. {
  5114. ExecuteNewInstance(Name);
  5115. }
  5116. else
  5117. {
  5118. DoOpenFolderOrWorkspace(Name, true, false);
  5119. }
  5120. }
  5121. //---------------------------------------------------------------------------
  5122. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  5123. bool &CanClose)
  5124. {
  5125. AppLog(L"Windows close query");
  5126. // See the comment in CloseApp()
  5127. if (NonVisualDataModule->Busy)
  5128. {
  5129. CanClose = false;
  5130. }
  5131. else
  5132. {
  5133. TTerminalManager * Manager = TTerminalManager::Instance();
  5134. int ActiveSessions = 0;
  5135. for (int Index = 0; Index < Manager->Count; Index++)
  5136. {
  5137. if (Manager->Sessions[Index]->Active)
  5138. {
  5139. ActiveSessions++;
  5140. }
  5141. }
  5142. // Confirm, when there's at least one active remote session or
  5143. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  5144. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  5145. WinConfiguration->ConfirmClosingSession)
  5146. {
  5147. unsigned int Result;
  5148. TMessageParams Params(mpNeverAskAgainCheck);
  5149. UnicodeString Message;
  5150. int Answers = qaOK | qaCancel;
  5151. // The current session is the only active session
  5152. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  5153. {
  5154. if (!WinConfiguration->AutoSaveWorkspace)
  5155. {
  5156. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  5157. }
  5158. else
  5159. {
  5160. Message = LoadStr(CLOSE_SESSION);
  5161. }
  5162. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  5163. }
  5164. // Multiple active sessions or one active session, but it's not the current one
  5165. else if (ActiveSessions > 0)
  5166. {
  5167. if (!WinConfiguration->AutoSaveWorkspace)
  5168. {
  5169. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  5170. }
  5171. else
  5172. {
  5173. Message = LoadStr(CLOSE_SESSIONS);
  5174. }
  5175. }
  5176. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  5177. else
  5178. {
  5179. DebugAlwaysTrue(Manager->Count > 1);
  5180. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  5181. Message = LoadStr(CLOSE_WORKSPACE);
  5182. }
  5183. UnicodeString Note;
  5184. if (WinConfiguration->AutoSaveWorkspace)
  5185. {
  5186. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  5187. }
  5188. else
  5189. {
  5190. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  5191. Answers = qaYes | qaNo | qaCancel;
  5192. }
  5193. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  5194. SetFocus();
  5195. Result = MessageDialog(Message, qtConfirmation,
  5196. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  5197. if (Result == qaNeverAskAgain)
  5198. {
  5199. WinConfiguration->ConfirmClosingSession = false;
  5200. }
  5201. if (Result == qaNo)
  5202. {
  5203. CanClose = SaveWorkspace(true);
  5204. // note that the workspace will be saved redundatly again from FormClose
  5205. }
  5206. else
  5207. {
  5208. CanClose =
  5209. (Result == qaOK) ||
  5210. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  5211. (Result == qaNeverAskAgain);
  5212. }
  5213. }
  5214. if (CanClose && HasActiveTerminal())
  5215. {
  5216. CanClose = CanCloseQueue();
  5217. }
  5218. }
  5219. if (CanClose)
  5220. {
  5221. CanClose =
  5222. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  5223. FEditorManager->CloseExternalFilesWithoutProcess();
  5224. if (CanClose)
  5225. {
  5226. while (CanClose && (FLocalEditors->Count > 0))
  5227. {
  5228. int PrevCount = FLocalEditors->Count;
  5229. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  5230. CanClose = (FLocalEditors->Count < PrevCount);
  5231. }
  5232. if (CanClose)
  5233. {
  5234. CanClose = FEditorManager->Empty(true);
  5235. if (!CanClose)
  5236. {
  5237. SetFocus();
  5238. CanClose =
  5239. (MessageDialog(
  5240. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  5241. }
  5242. }
  5243. }
  5244. }
  5245. }
  5246. //---------------------------------------------------------------------------
  5247. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  5248. {
  5249. TForm * Form = dynamic_cast<TForm *>(Sender);
  5250. DebugAssert(Form != NULL);
  5251. Form->Close();
  5252. }
  5253. //---------------------------------------------------------------------------
  5254. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  5255. {
  5256. TForm * Form = dynamic_cast<TForm *>(Sender);
  5257. delete Form;
  5258. }
  5259. //---------------------------------------------------------------------------
  5260. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  5261. {
  5262. while (FLocalEditors->Count > 0)
  5263. {
  5264. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  5265. }
  5266. }
  5267. //---------------------------------------------------------------------------
  5268. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  5269. TObject *Sender)
  5270. {
  5271. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  5272. try
  5273. {
  5274. SetProperties(osRemote, FileList);
  5275. }
  5276. __finally
  5277. {
  5278. delete FileList;
  5279. }
  5280. }
  5281. //---------------------------------------------------------------------------
  5282. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  5283. {
  5284. if (value)
  5285. {
  5286. if (Component == fcCustomCommandsBand)
  5287. {
  5288. UpdateCustomCommandsToolbar();
  5289. }
  5290. if (Component == fcQueueFileList)
  5291. {
  5292. DebugAssert(!QueueFileListSplitter->Visible);
  5293. AdjustQueueLayout();
  5294. UpdateQueueFileList();
  5295. }
  5296. }
  5297. }
  5298. //---------------------------------------------------------------------------
  5299. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  5300. {
  5301. int MinHeightLimit =
  5302. GetStaticQueuePanelComponentsHeight() +
  5303. QueueFileListSplitter->Height +
  5304. QueueFileList->Height +
  5305. GetMinQueueViewHeight();
  5306. if (QueuePanel->ClientHeight < MinHeightLimit)
  5307. {
  5308. QueuePanel->ClientHeight = MinHeightLimit;
  5309. }
  5310. }
  5311. //---------------------------------------------------------------------------
  5312. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  5313. {
  5314. TControl * Control = GetComponent(Component);
  5315. DebugAssert(Control);
  5316. bool Changed = (Control->Visible != value);
  5317. if (Changed)
  5318. {
  5319. ComponentShowing(Component, value);
  5320. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  5321. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  5322. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  5323. if (value)
  5324. {
  5325. int RemainingHeight = Control->Parent->ClientHeight;
  5326. int RemainingWidth = Control->Parent->ClientWidth;
  5327. for (int i = 0; i < Control->Parent->ControlCount; i++)
  5328. {
  5329. TControl * ChildControl = Control->Parent->Controls[i];
  5330. if (ChildControl->Visible)
  5331. {
  5332. switch (ChildControl->Align)
  5333. {
  5334. case alTop:
  5335. case alBottom:
  5336. RemainingHeight -= ChildControl->Height;
  5337. break;
  5338. case alLeft:
  5339. case alRight:
  5340. RemainingWidth -= ChildControl->Width;
  5341. break;
  5342. }
  5343. }
  5344. }
  5345. int Reserve = ScaleByTextHeight(this, 32);
  5346. // queue in explorer, trees in commander
  5347. if (Control->Height > RemainingHeight - Reserve)
  5348. {
  5349. Control->Height = RemainingHeight / 2;
  5350. }
  5351. if (Control->Width > RemainingWidth - Reserve)
  5352. {
  5353. Control->Width = RemainingWidth / 2;
  5354. }
  5355. }
  5356. Control->Visible = value;
  5357. if (WasFocused && Visible)
  5358. {
  5359. DirView(osCurrent)->SetFocus();
  5360. }
  5361. FixControlsPlacement();
  5362. }
  5363. }
  5364. //---------------------------------------------------------------------------
  5365. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  5366. {
  5367. TControl * Control = GetComponent(Component);
  5368. if (Control == NULL)
  5369. {
  5370. return false;
  5371. }
  5372. else
  5373. {
  5374. return Control->Visible;
  5375. }
  5376. }
  5377. //---------------------------------------------------------------------------
  5378. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  5379. {
  5380. bool Result;
  5381. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  5382. {
  5383. Result = !IsUWP();
  5384. }
  5385. else
  5386. {
  5387. Result = true;
  5388. }
  5389. return Result;
  5390. }
  5391. //---------------------------------------------------------------------------
  5392. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  5393. {
  5394. if (DirView->ItemFocused != NULL)
  5395. {
  5396. DirView->ItemFocused->MakeVisible(false);
  5397. }
  5398. }
  5399. //---------------------------------------------------------------------------
  5400. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5401. {
  5402. MakeFocusedItemVisible(RemoteDirView);
  5403. QueueSplitter->Visible = QueuePanel->Visible;
  5404. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5405. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5406. TControl * TopControlsOrder[] =
  5407. { TopDock, MessageDock, SessionsPageControl };
  5408. SetVerticalControlsOrder(TopControlsOrder, LENOF(TopControlsOrder));
  5409. TControl * QueueControlsOrder[] =
  5410. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5411. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5412. }
  5413. //---------------------------------------------------------------------------
  5414. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5415. {
  5416. switch (Component) {
  5417. case fcStatusBar: return RemoteStatusBar;
  5418. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5419. case fcQueueView: return QueuePanel;
  5420. case fcQueueToolbar: return QueueDock;
  5421. case fcRemoteTree: return RemoteDrivePanel;
  5422. case fcSessionsTabs: return SessionsPageControl;
  5423. case fcQueueFileList: return QueueFileList;
  5424. default: return NULL;
  5425. }
  5426. }
  5427. //---------------------------------------------------------------------------
  5428. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5429. TObject *Sender, TListColumn *Column, TPoint &Point)
  5430. {
  5431. DebugAssert(NonVisualDataModule && Column && Sender);
  5432. NonVisualDataModule->ListColumn = Column;
  5433. TPopupMenu * DirViewColumnMenu;
  5434. if (Sender == DirView(osOther))
  5435. {
  5436. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5437. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible = (Column->Index == uvName);
  5438. bool SizeColumn = (Column->Index == uvSize);
  5439. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible = SizeColumn;
  5440. NonVisualDataModule->RemoteCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5441. }
  5442. else
  5443. {
  5444. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5445. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible = (Column->Index == dvName);
  5446. bool SizeColumn = (Column->Index == dvSize);
  5447. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible = SizeColumn;
  5448. NonVisualDataModule->LocalCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5449. }
  5450. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5451. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5452. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5453. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5454. }
  5455. //---------------------------------------------------------------------------
  5456. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5457. TObject *Sender, TListItem *Item, bool &AllowExec)
  5458. {
  5459. DoDirViewExecFile(Sender, Item, AllowExec);
  5460. }
  5461. //---------------------------------------------------------------------------
  5462. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5463. TListItem * Item, bool & AllowExec)
  5464. {
  5465. DebugAssert(Sender && Item && Configuration);
  5466. DebugAssert(AllowExec);
  5467. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5468. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5469. bool Remote = !IsSideLocalBrowser(Side);
  5470. DebugAssert(Item == ADirView->ItemFocused);
  5471. bool IsDirectory = ADirView->ItemIsDirectory(Item);
  5472. TResolvedDoubleClickAction Action;
  5473. if (FForceExecution)
  5474. {
  5475. Action = IsDirectory ? rdcaChangeDir : rdcaOpen;
  5476. }
  5477. else
  5478. {
  5479. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  5480. Action = WinConfiguration->ResolveDoubleClickAction(IsDirectory, ATerminal);
  5481. }
  5482. if (Action == rdcaCopy)
  5483. {
  5484. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5485. UnlockWindow();
  5486. try
  5487. {
  5488. if (IsLocalBrowserMode())
  5489. {
  5490. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5491. }
  5492. else
  5493. {
  5494. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5495. }
  5496. }
  5497. __finally
  5498. {
  5499. LockWindow();
  5500. }
  5501. AllowExec = false;
  5502. }
  5503. else if (Action == rdcaEdit)
  5504. {
  5505. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5506. {
  5507. ExecuteFile(Side, efDefaultEditor);
  5508. AllowExec = false;
  5509. }
  5510. }
  5511. else if (Action == rdcaOpen)
  5512. {
  5513. if (DebugAlwaysTrue(!IsDirectory) && Remote && !WinConfiguration->DisableOpenEdit)
  5514. {
  5515. ExecuteFile(osRemote, efShell);
  5516. AllowExec = false;
  5517. }
  5518. }
  5519. }
  5520. //---------------------------------------------------------------------------
  5521. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5522. {
  5523. return ((Side == osRemote) || (Side == osCurrent));
  5524. }
  5525. //---------------------------------------------------------------------------
  5526. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5527. {
  5528. DebugFail();
  5529. }
  5530. //---------------------------------------------------------------------------
  5531. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5532. {
  5533. DebugFail();
  5534. }
  5535. //---------------------------------------------------------------------------
  5536. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5537. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5538. {
  5539. TSynchronizeParamType Params;
  5540. Params.LocalDirectory = LocalDirectory;
  5541. Params.RemoteDirectory = RemoteDirectory;
  5542. int UnusedParams =
  5543. (GUIConfiguration->SynchronizeParams &
  5544. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5545. TTerminal::spCaseSensitive | TTerminal::spMirror | TTerminal::spByChecksum));
  5546. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5547. Params.Options = GUIConfiguration->SynchronizeOptions;
  5548. bool SaveSettings = false;
  5549. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5550. &DoSynchronizeTooManyDirectories);
  5551. DebugAssert(FSynchronizeController == NULL);
  5552. FSynchronizeController = &Controller;
  5553. bool Result;
  5554. try
  5555. {
  5556. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5557. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5558. int Options =
  5559. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5560. DebugAssert(FOnFeedSynchronizeError == NULL);
  5561. DebugAssert(FOnSynchronizeAbort == NULL);
  5562. DebugAssert(FSynchronizeTerminal == NULL);
  5563. FSynchronizeTerminal = Terminal;
  5564. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5565. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5566. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5567. if (Result)
  5568. {
  5569. if (SaveSettings)
  5570. {
  5571. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5572. GUIConfiguration->SynchronizeOptions = Params.Options;
  5573. }
  5574. else
  5575. {
  5576. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5577. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5578. FLAGSET(Params.Options, soSynchronize))
  5579. {
  5580. GUIConfiguration->SynchronizeOptions =
  5581. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5582. soSynchronize;
  5583. }
  5584. }
  5585. LocalDirectory = Params.LocalDirectory;
  5586. RemoteDirectory = Params.RemoteDirectory;
  5587. }
  5588. }
  5589. __finally
  5590. {
  5591. FSynchronizeController = NULL;
  5592. DebugAssert(FOnFeedSynchronizeError == NULL);
  5593. FOnFeedSynchronizeError = NULL;
  5594. DebugAssert(FOnSynchronizeAbort == NULL);
  5595. FOnSynchronizeAbort = NULL;
  5596. FSynchronizeTerminal = NULL;
  5597. }
  5598. return Result;
  5599. }
  5600. //---------------------------------------------------------------------------
  5601. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5602. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5603. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5604. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5605. TSynchronizeOptions * Options, bool Full)
  5606. {
  5607. // Keep-up-to-date mode
  5608. if (Terminal->Status == ssOpened)
  5609. {
  5610. try
  5611. {
  5612. int PParams = Params.Params;
  5613. if (!Full)
  5614. {
  5615. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5616. if (FLAGSET(Params.Options, soRecurse))
  5617. {
  5618. PParams |= TTerminal::spSubDirs;
  5619. }
  5620. }
  5621. else
  5622. {
  5623. // if keepuptodate is non-recursive,
  5624. // full sync before has to be non-recursive as well
  5625. if (FLAGCLEAR(Params.Options, soRecurse))
  5626. {
  5627. PParams |= TTerminal::spNoRecurse;
  5628. }
  5629. }
  5630. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5631. PParams, Checklist, Options);
  5632. }
  5633. catch (Exception & E)
  5634. {
  5635. if (!Terminal->Active)
  5636. {
  5637. ShowExtendedExceptionEx(Terminal, &E);
  5638. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5639. if (!Terminal->Active)
  5640. {
  5641. throw;
  5642. }
  5643. }
  5644. else
  5645. {
  5646. if (FLAGSET(Params.Options, soContinueOnError) &&
  5647. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5648. {
  5649. // noop, already logged in MoreMessageDialog
  5650. }
  5651. else
  5652. {
  5653. // We get mostly EAbort here, so this is noop.
  5654. // Exception is an error when listing directories while looking for differences.
  5655. ShowExtendedExceptionEx(Terminal, &E);
  5656. throw;
  5657. }
  5658. }
  5659. }
  5660. }
  5661. }
  5662. //---------------------------------------------------------------------------
  5663. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5664. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5665. const UnicodeString ErrorStr)
  5666. {
  5667. if (!Directory.IsEmpty())
  5668. {
  5669. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5670. }
  5671. else
  5672. {
  5673. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5674. }
  5675. }
  5676. //---------------------------------------------------------------------------
  5677. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5678. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5679. {
  5680. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5681. {
  5682. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5683. }
  5684. else
  5685. {
  5686. TMessageParams Params(mpNeverAskAgainCheck);
  5687. unsigned int Result = MessageDialog(
  5688. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5689. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5690. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5691. {
  5692. MaxDirectories *= 2;
  5693. if (Result == qaNeverAskAgain)
  5694. {
  5695. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5696. }
  5697. }
  5698. else
  5699. {
  5700. Abort();
  5701. }
  5702. }
  5703. }
  5704. //---------------------------------------------------------------------------
  5705. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5706. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5707. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5708. TSynchronizeOptions * Options)
  5709. {
  5710. // Keep-up-to-date mode
  5711. DebugAssert(!FAutoOperation);
  5712. void * BatchStorage;
  5713. BatchStart(BatchStorage);
  5714. FAutoOperation = true;
  5715. bool AnyOperation = false;
  5716. TDateTime StartTime = Now();
  5717. TSynchronizeChecklist * AChecklist = NULL;
  5718. try
  5719. {
  5720. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5721. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5722. {
  5723. FSynchronizeProgressForm->Start();
  5724. }
  5725. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5726. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5727. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5728. Options);
  5729. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5730. SAFE_DESTROY(FSynchronizeProgressForm);
  5731. AnyOperation = (AChecklist->CheckedCount > 0);
  5732. if (AnyOperation)
  5733. {
  5734. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5735. CreateProgressForm(&SynchronizeProgress);
  5736. try
  5737. {
  5738. Terminal->SynchronizeApply(
  5739. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5740. SynchronizeProcessedItem, NULL, NULL, NULL);
  5741. }
  5742. __finally
  5743. {
  5744. DestroyProgressForm();
  5745. }
  5746. }
  5747. }
  5748. __finally
  5749. {
  5750. if (Checklist == NULL)
  5751. {
  5752. delete AChecklist;
  5753. }
  5754. else
  5755. {
  5756. *Checklist = AChecklist;
  5757. }
  5758. FAutoOperation = false;
  5759. SAFE_DESTROY(FSynchronizeProgressForm);
  5760. BatchEnd(BatchStorage);
  5761. ReloadLocalDirectory();
  5762. if (AnyOperation)
  5763. {
  5764. OperationComplete(StartTime);
  5765. }
  5766. }
  5767. }
  5768. //---------------------------------------------------------------------------
  5769. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5770. {
  5771. // can be called from command line
  5772. return Visible &&
  5773. ((DirView(osRemote)->SelCount > 0) ||
  5774. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5775. }
  5776. //---------------------------------------------------------------------------
  5777. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5778. {
  5779. LogSynchronizeEvent(Terminal, Message);
  5780. }
  5781. //---------------------------------------------------------------------------
  5782. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5783. int Params, TSynchronizeOptions & Options)
  5784. {
  5785. DebugAssert(!IsLocalBrowserMode());
  5786. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5787. {
  5788. Options.Filter = new TStringList();
  5789. Options.Filter->CaseSensitive = false;
  5790. Options.Filter->Duplicates = Types::dupAccept;
  5791. if (DirView(osRemote)->SelCount > 0)
  5792. {
  5793. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5794. }
  5795. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5796. {
  5797. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5798. }
  5799. Options.Filter->Sort();
  5800. }
  5801. }
  5802. //---------------------------------------------------------------------------
  5803. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5804. {
  5805. TCopyParamType Defaults;
  5806. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5807. ConfigStorage->AccessMode = smRead;
  5808. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5809. {
  5810. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5811. }
  5812. ConfigStorage.reset(NULL);
  5813. std::unique_ptr<TStringList> Options(new TStringList());
  5814. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5815. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5816. UnicodeString Result;
  5817. if (Options->Count > 0)
  5818. {
  5819. Result =
  5820. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5821. }
  5822. return Result;
  5823. }
  5824. //---------------------------------------------------------------------------
  5825. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5826. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5827. {
  5828. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5829. UnicodeString AdditionalParams =
  5830. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5831. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5832. SerializeCopyParamForCommandLine(CopyParams),
  5833. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5834. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5835. ExecuteNewInstance(SessionName, AdditionalParams);
  5836. }
  5837. //---------------------------------------------------------------------------
  5838. struct TSynchronizeParams
  5839. {
  5840. UnicodeString LocalDirectory;
  5841. UnicodeString RemoteDirectory;
  5842. TSynchronizeMode Mode;
  5843. TSynchronizeChecklist * Checklist;
  5844. int Params;
  5845. TCopyParamType * CopyParam;
  5846. TDateTime CollectElapsed;
  5847. TDateTime * StartTime;
  5848. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5849. };
  5850. //---------------------------------------------------------------------------
  5851. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5852. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5853. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5854. {
  5855. DebugAssert(!FAutoOperation);
  5856. void * BatchStorage;
  5857. BatchStart(BatchStorage);
  5858. FAutoOperation = true;
  5859. TFileOperationStatistics Statistics;
  5860. TDateTime Start = Now();
  5861. try
  5862. {
  5863. Params.OnProcessedItem = OnProcessedItem;
  5864. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5865. CreateProgressForm(&SynchronizeProgress);
  5866. Terminal->SynchronizeApply(
  5867. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5868. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5869. &Statistics);
  5870. }
  5871. __finally
  5872. {
  5873. Params.OnProcessedItem = NULL;
  5874. FAutoOperation = false;
  5875. DestroyProgressForm();
  5876. BatchEnd(BatchStorage);
  5877. ReloadLocalDirectory();
  5878. }
  5879. if (WinConfiguration->SynchronizeSummary)
  5880. {
  5881. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5882. // The statistics should be 0 anyway in this case
  5883. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5884. {
  5885. Message += L"\n";
  5886. if (Statistics.FilesUploaded > 0)
  5887. {
  5888. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5889. }
  5890. if (Statistics.FilesDownloaded > 0)
  5891. {
  5892. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5893. }
  5894. if (Statistics.FilesDeletedLocal > 0)
  5895. {
  5896. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5897. }
  5898. if (Statistics.FilesDeletedRemote > 0)
  5899. {
  5900. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5901. }
  5902. }
  5903. TDateTime Elapsed = (Now() - Start);
  5904. Message +=
  5905. L"\n" +
  5906. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Params.CollectElapsed))) + L"\n" +
  5907. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Elapsed)));
  5908. TMessageParams Params(mpNeverAskAgainCheck);
  5909. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5910. if (Result == qaNeverAskAgain)
  5911. {
  5912. WinConfiguration->SynchronizeSummary = false;
  5913. }
  5914. }
  5915. }
  5916. //---------------------------------------------------------------------------
  5917. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5918. {
  5919. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5920. {
  5921. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5922. }
  5923. // Not set in keep-up-to-date mode - Synchronize() method
  5924. if (Token != NULL)
  5925. {
  5926. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5927. if (Params.OnProcessedItem != NULL)
  5928. {
  5929. Params.OnProcessedItem(NULL, ChecklistItem);
  5930. }
  5931. }
  5932. }
  5933. //---------------------------------------------------------------------------
  5934. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5935. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5936. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5937. {
  5938. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5939. *Params.StartTime = Now();
  5940. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5941. }
  5942. //---------------------------------------------------------------------------
  5943. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5944. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5945. {
  5946. // terminal can be already closed (e.g. dropped connection)
  5947. if (Terminal != NULL)
  5948. {
  5949. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5950. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5951. }
  5952. }
  5953. //---------------------------------------------------------------------------
  5954. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5955. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5956. {
  5957. DebugAssert(!IsLocalBrowserMode());
  5958. TAutoBatch AutoBatch(this);
  5959. TAutoFlag AutoOperationFlag(FAutoOperation);
  5960. if (Side == osRemote)
  5961. {
  5962. std::unique_ptr<TStrings> FileList(new TStringList());
  5963. FileList->AddObject(FileName, RemoteFile);
  5964. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5965. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5966. RemoteDirView->SaveSelection();
  5967. RemoteDirView->SaveSelectedNames();
  5968. try
  5969. {
  5970. Terminal->MoveFiles(FileList.get(), Target, FileMask, false);
  5971. }
  5972. catch(...)
  5973. {
  5974. RemoteDirView->DiscardSavedSelection();
  5975. throw;
  5976. }
  5977. RemoteDirView->RestoreSelection();
  5978. }
  5979. else if (DebugAlwaysTrue(Side == osLocal))
  5980. {
  5981. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5982. {
  5983. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5984. }
  5985. UnicodeString Directory = ExtractFileDir(FileName);
  5986. ReloadLocalDirectory(Directory);
  5987. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5988. if (!SamePaths(Directory, NewDirectory))
  5989. {
  5990. ReloadLocalDirectory(NewDirectory);
  5991. }
  5992. }
  5993. }
  5994. //---------------------------------------------------------------------------
  5995. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5996. {
  5997. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5998. if (Side == osLocal)
  5999. {
  6000. if (Action == TSynchronizeChecklist::saDownloadNew)
  6001. {
  6002. OpenFolderInExplorer(LocalPath);
  6003. }
  6004. else
  6005. {
  6006. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  6007. }
  6008. }
  6009. else if (DebugAlwaysTrue(Side == osRemote))
  6010. {
  6011. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  6012. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  6013. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  6014. if (!LocalPath.IsEmpty())
  6015. {
  6016. SessionData->LocalDirectory = LocalPath;
  6017. }
  6018. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  6019. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  6020. }
  6021. }
  6022. //---------------------------------------------------------------------------
  6023. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  6024. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  6025. const TCopyParamType * CopyParams)
  6026. {
  6027. UnicodeString SessionName = CreateHiddenDuplicateSession();
  6028. UnicodeString AdditionalParams =
  6029. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  6030. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  6031. SerializeCopyParamForCommandLine(CopyParams),
  6032. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  6033. LocalDirectory, RemoteDirectory, Mode, Params));
  6034. ExecuteNewInstance(SessionName, AdditionalParams);
  6035. }
  6036. //---------------------------------------------------------------------------
  6037. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  6038. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  6039. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  6040. {
  6041. int Result;
  6042. bool SaveSettings = false;
  6043. int Options =
  6044. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  6045. FLAGMASK(!CanCalculateChecksum(), fsoDisableByChecksum) |
  6046. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  6047. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  6048. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  6049. bool Continue =
  6050. ((UseDefaults == 0) ||
  6051. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  6052. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults)) &&
  6053. (FLAGCLEAR(Params, TTerminal::spByChecksum) || EnsureCommandSessionFallback(fcCalculatingChecksum));
  6054. if (Continue)
  6055. {
  6056. Configuration->Usage->Inc(L"Synchronizations");
  6057. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  6058. UpdateCopyParamCounters(CopyParam);
  6059. TSynchronizeOptions SynchronizeOptions;
  6060. GetSynchronizeOptions(Params, SynchronizeOptions);
  6061. if (SaveSettings)
  6062. {
  6063. GUIConfiguration->SynchronizeParams = Params;
  6064. }
  6065. else
  6066. {
  6067. SaveMode = false;
  6068. }
  6069. TDateTime StartTime = Now();
  6070. TSynchronizeChecklist * Checklist = NULL;
  6071. try
  6072. {
  6073. DebugAssert(!FAutoOperation);
  6074. FAutoOperation = true;
  6075. try
  6076. {
  6077. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  6078. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  6079. int Files = -1;
  6080. if (DataList->Count >= 1)
  6081. {
  6082. Files = StrToIntDef(DataList->Strings[0], Files);
  6083. }
  6084. else
  6085. {
  6086. DataList->Add(UnicodeString());
  6087. }
  6088. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  6089. FSynchronizeProgressForm->Start();
  6090. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  6091. static_cast<TTerminal::TSynchronizeMode>(Mode),
  6092. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  6093. &SynchronizeOptions);
  6094. if (Terminal->SessionData->CacheDirectories)
  6095. {
  6096. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  6097. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  6098. }
  6099. }
  6100. __finally
  6101. {
  6102. FAutoOperation = false;
  6103. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  6104. SAFE_DESTROY(FSynchronizeProgressForm);
  6105. }
  6106. TSynchronizeParams SynchronizeParams;
  6107. SynchronizeParams.LocalDirectory = LocalDirectory;
  6108. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  6109. SynchronizeParams.Mode = Mode;
  6110. SynchronizeParams.CopyParam = &CopyParam;
  6111. SynchronizeParams.Params = Params;
  6112. SynchronizeParams.Checklist = Checklist;
  6113. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  6114. SynchronizeParams.StartTime = &StartTime;
  6115. SynchronizeParams.OnProcessedItem = NULL;
  6116. Result = Checklist->Count;
  6117. if (Checklist->Count > 0)
  6118. {
  6119. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  6120. {
  6121. if (!DoSynchronizeChecklistDialog(
  6122. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  6123. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  6124. {
  6125. Result = -1;
  6126. }
  6127. }
  6128. else
  6129. {
  6130. FullSynchronize(SynchronizeParams, NULL, NULL);
  6131. }
  6132. }
  6133. else
  6134. {
  6135. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  6136. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  6137. }
  6138. }
  6139. __finally
  6140. {
  6141. delete Checklist;
  6142. }
  6143. OperationComplete(StartTime);
  6144. }
  6145. else
  6146. {
  6147. Result = -1;
  6148. }
  6149. return Result;
  6150. }
  6151. //---------------------------------------------------------------------------
  6152. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  6153. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  6154. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  6155. {
  6156. if (Collect)
  6157. {
  6158. DebugAssert(FSynchronizeProgressForm != NULL);
  6159. if (!FSynchronizeProgressForm->Started)
  6160. {
  6161. FSynchronizeProgressForm->Start();
  6162. }
  6163. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  6164. SetTaskbarListProgressValue(CompareProgress);
  6165. }
  6166. else
  6167. {
  6168. DebugAssert(FSynchronizeProgressForm == NULL);
  6169. DebugAssert(FProgressForm != NULL);
  6170. }
  6171. }
  6172. //---------------------------------------------------------------------------
  6173. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  6174. {
  6175. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  6176. std::unique_ptr<TRemoteFile> File(Terminal->ReadFile(FullFileName));
  6177. if (File.get() != NULL)
  6178. {
  6179. ExecuteRemoteFile(FullFileName, File.get(), efInternalEditor);
  6180. Application->ShowMainForm = false;
  6181. Application->Run();
  6182. }
  6183. }
  6184. //---------------------------------------------------------------------------
  6185. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  6186. {
  6187. DebugFail();
  6188. }
  6189. //---------------------------------------------------------------------------
  6190. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  6191. {
  6192. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  6193. SessionData->Assign(ManagedSession->SessionData);
  6194. UpdateSessionData(SessionData.get());
  6195. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  6196. if (IsActiveTerminal(ManagedSession))
  6197. {
  6198. ManagedSession->UpdateSessionCredentials(SessionData.get());
  6199. }
  6200. if (!ManagedSession->SessionData->HasSessionName())
  6201. {
  6202. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  6203. // once IsWorkspace is cleared
  6204. SessionData->Name = UnicodeString();
  6205. }
  6206. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  6207. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  6208. SessionData->IsWorkspace = false;
  6209. return SessionData.release();
  6210. }
  6211. //---------------------------------------------------------------------------
  6212. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  6213. {
  6214. TSessionData * SessionData = CloneCurrentSessionData();
  6215. try
  6216. {
  6217. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  6218. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  6219. }
  6220. __finally
  6221. {
  6222. delete SessionData;
  6223. }
  6224. }
  6225. //---------------------------------------------------------------------------
  6226. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  6227. {
  6228. TTerminalManager * Manager = TTerminalManager::Instance();
  6229. std::unique_ptr<TObjectList> DataList(new TObjectList());
  6230. if (DebugAlwaysTrue(ManagedSession != NULL))
  6231. {
  6232. // Update ManagedSession->StateData
  6233. UpdateSession(ManagedSession);
  6234. }
  6235. Manager->SaveWorkspace(DataList.get());
  6236. return DataList.release();
  6237. }
  6238. //---------------------------------------------------------------------------
  6239. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  6240. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  6241. {
  6242. WinConfiguration->LastWorkspace = Name;
  6243. if (!SavePasswords)
  6244. {
  6245. for (int Index = 0; Index < DataList->Count; Index++)
  6246. {
  6247. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6248. if (SessionData->Link.IsEmpty())
  6249. {
  6250. SessionData->ClearSessionPasswords();
  6251. }
  6252. }
  6253. }
  6254. StoredSessions->NewWorkspace(Name, DataList);
  6255. // modified only
  6256. StoredSessions->Save(false, Explicit);
  6257. }
  6258. //---------------------------------------------------------------------------
  6259. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  6260. {
  6261. return
  6262. DefaultStr(WinConfiguration->LastWorkspace,
  6263. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  6264. }
  6265. //---------------------------------------------------------------------------
  6266. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  6267. {
  6268. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  6269. bool AnyNonStoredSessionWithPassword = false;
  6270. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  6271. bool AllNonStoredSessionsAnonymous = true;
  6272. TTerminalManager * Manager = TTerminalManager::Instance();
  6273. for (int Index = 0; Index < DataList->Count; Index++)
  6274. {
  6275. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6276. if (SessionData->Link.IsEmpty())
  6277. {
  6278. if (SessionData->HasAnySessionPassword())
  6279. {
  6280. AnyNonStoredSessionWithPassword = true;
  6281. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  6282. {
  6283. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  6284. }
  6285. }
  6286. if (!SameText(SessionData->UserName, AnonymousUserName))
  6287. {
  6288. AllNonStoredSessionsAnonymous = false;
  6289. }
  6290. }
  6291. }
  6292. bool SavePasswords;
  6293. bool * PSavePasswords;
  6294. bool NotRecommendedSavingPasswords =
  6295. !CustomWinConfiguration->UseMasterPassword &&
  6296. !AllNonStoredSessionsAnonymous;
  6297. if (Configuration->DisablePasswordStoring ||
  6298. !AnyNonStoredSessionWithPassword)
  6299. {
  6300. PSavePasswords = NULL;
  6301. SavePasswords = false;
  6302. }
  6303. else
  6304. {
  6305. PSavePasswords = &SavePasswords;
  6306. SavePasswords =
  6307. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  6308. !NotRecommendedSavingPasswords;
  6309. }
  6310. UnicodeString Name = WorkspaceName();
  6311. bool CreateShortcut = false;
  6312. bool Result =
  6313. DoSaveWorkspaceDialog(
  6314. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  6315. EnableAutoSave);
  6316. if (Result)
  6317. {
  6318. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  6319. if (CreateShortcut)
  6320. {
  6321. TOperationVisualizer Visualizer;
  6322. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  6323. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  6324. }
  6325. if (EnableAutoSave)
  6326. {
  6327. WinConfiguration->AutoSaveWorkspace = true;
  6328. WinConfiguration->AutoWorkspace = Name;
  6329. if (PSavePasswords != NULL)
  6330. {
  6331. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  6332. }
  6333. }
  6334. }
  6335. return Result;
  6336. }
  6337. //---------------------------------------------------------------------------
  6338. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  6339. {
  6340. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  6341. DebugAssert(Session == ManagedSession);
  6342. if (!IsLocalBrowserMode())
  6343. {
  6344. SAFE_DESTROY(Session->RemoteExplorerState);
  6345. if (WinConfiguration->PreservePanelState)
  6346. {
  6347. Session->RemoteExplorerState = RemoteDirView->SaveState();
  6348. }
  6349. }
  6350. UpdateSessionData(Session->StateData);
  6351. }
  6352. //---------------------------------------------------------------------------
  6353. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  6354. {
  6355. // Keep in sync with TSessionData::CopyStateData
  6356. DebugAssert(Data != NULL);
  6357. // This is inconsistent with how color (for example) is handled.
  6358. Data->Name = ManagedSession->SessionData->Name;
  6359. if (!IsLocalBrowserMode())
  6360. {
  6361. // cannot use RemoteDirView->Path, because it is empty if connection
  6362. // was already closed
  6363. // also only peek, we may not be connected at all atm
  6364. // so make sure we do not try retrieving current directory from the server
  6365. // (particularly with FTP)
  6366. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  6367. if (!ACurrentDirectory.IsEmpty())
  6368. {
  6369. Data->RemoteDirectory = ACurrentDirectory;
  6370. }
  6371. }
  6372. Data->Color = SessionColor;
  6373. }
  6374. //---------------------------------------------------------------------------
  6375. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  6376. {
  6377. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  6378. DebugAssert(Toolbar != NULL);
  6379. for (int i = 0; i < Toolbar->Items->Count; i++)
  6380. {
  6381. TTBXCustomDropDownItem * DropDownItem;
  6382. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6383. if (DropDownItem != NULL)
  6384. {
  6385. ToolbarItemResize(DropDownItem,
  6386. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  6387. Toolbar->NonClientWidth);
  6388. break;
  6389. }
  6390. }
  6391. }
  6392. //---------------------------------------------------------------------------
  6393. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6394. {
  6395. Item->EditWidth = Width;
  6396. }
  6397. //---------------------------------------------------------------------------
  6398. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6399. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6400. {
  6401. for (int i = 0; i < Toolbar->Items->Count; i++)
  6402. {
  6403. TTBXCustomDropDownItem * DropDownItem;
  6404. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6405. if (DropDownItem != NULL)
  6406. {
  6407. ASize.x -= DropDownItem->EditWidth;
  6408. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  6409. }
  6410. }
  6411. }
  6412. //---------------------------------------------------------------------------
  6413. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6414. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6415. {
  6416. if (WinConfiguration->DDWarnLackOfTempSpace)
  6417. {
  6418. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6419. if (!ADrive.IsEmpty())
  6420. {
  6421. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6422. DebugAssert(RequiredSpace >= 0);
  6423. __int64 RequiredWithReserve;
  6424. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6425. if (FreeSpace < RequiredWithReserve)
  6426. {
  6427. unsigned int Result;
  6428. TMessageParams Params(mpNeverAskAgainCheck);
  6429. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6430. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6431. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6432. if (Result == qaNeverAskAgain)
  6433. {
  6434. WinConfiguration->DDWarnLackOfTempSpace = false;
  6435. }
  6436. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6437. }
  6438. }
  6439. }
  6440. }
  6441. //---------------------------------------------------------------------------
  6442. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6443. {
  6444. DoOpenDirectoryDialog(odAddBookmark, Side);
  6445. }
  6446. //---------------------------------------------------------------------------
  6447. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6448. {
  6449. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6450. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6451. try
  6452. {
  6453. TCustomDirView * DView = DirView(Side);
  6454. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6455. {
  6456. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6457. }
  6458. }
  6459. catch (...)
  6460. {
  6461. delete VisitedDirectories;
  6462. throw;
  6463. }
  6464. return VisitedDirectories;
  6465. }
  6466. //---------------------------------------------------------------------------
  6467. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6468. TOpenDirectoryMode Mode, TOperationSide Side)
  6469. {
  6470. bool Continue = true;
  6471. if (Mode == odAddBookmark)
  6472. {
  6473. TMessageParams Params(mpNeverAskAgainCheck);
  6474. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6475. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6476. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6477. unsigned int Answer =
  6478. MessageDialog(Message,
  6479. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6480. if (Answer == qaNeverAskAgain)
  6481. {
  6482. Continue = true;
  6483. WinConfiguration->UseSharedBookmarks = true;
  6484. }
  6485. else if (Answer == qaOK)
  6486. {
  6487. Continue = true;
  6488. WinConfiguration->UseSharedBookmarks = false;
  6489. }
  6490. else
  6491. {
  6492. Continue = false;
  6493. }
  6494. }
  6495. if (Continue)
  6496. {
  6497. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6498. try
  6499. {
  6500. UnicodeString Name = DirView(Side)->PathName;
  6501. bool Repeat;
  6502. do
  6503. {
  6504. Repeat = false;
  6505. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6506. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6507. // do not allow switching to location profiles,
  6508. // if we are not connected
  6509. HasDirView[osLocal] && (Terminal != NULL)))
  6510. {
  6511. TWindowLock Lock(this);
  6512. if (!TryOpenDirectory(Side, Name))
  6513. {
  6514. Repeat = true;
  6515. Mode = odBrowse;
  6516. }
  6517. }
  6518. }
  6519. while (Repeat);
  6520. }
  6521. __finally
  6522. {
  6523. delete VisitedDirectories;
  6524. }
  6525. }
  6526. }
  6527. //---------------------------------------------------------------------------
  6528. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6529. {
  6530. DebugAssert(!Terminal->CommandSessionOpened);
  6531. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6532. }
  6533. //---------------------------------------------------------------------------
  6534. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6535. {
  6536. bool Result = Terminal->IsCapable[Capability] ||
  6537. Terminal->CommandSessionOpened;
  6538. if (!Result)
  6539. {
  6540. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6541. if (!GUIConfiguration->ConfirmCommandSession)
  6542. {
  6543. Result = true;
  6544. }
  6545. else
  6546. {
  6547. TMessageParams Params(mpNeverAskAgainCheck);
  6548. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6549. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6550. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6551. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6552. if (Answer == qaNeverAskAgain)
  6553. {
  6554. GUIConfiguration->ConfirmCommandSession = false;
  6555. Result = true;
  6556. }
  6557. else if (Answer == qaOK)
  6558. {
  6559. Result = true;
  6560. }
  6561. }
  6562. if (Result)
  6563. {
  6564. Result = CommandSessionFallback();
  6565. }
  6566. }
  6567. return Result;
  6568. }
  6569. //---------------------------------------------------------------------------
  6570. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6571. {
  6572. if (EnsureCommandSessionFallback(fcAnyCommand))
  6573. {
  6574. DoConsoleDialog(Terminal, Command);
  6575. }
  6576. }
  6577. //---------------------------------------------------------------------------
  6578. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6579. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6580. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6581. {
  6582. if (!IsSideLocalBrowser(osRemote) &&
  6583. IsFileControl(DropSourceControl, osRemote) &&
  6584. (FDDExtMapFile != NULL))
  6585. {
  6586. Accept = true;
  6587. }
  6588. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6589. DebugAssert(FDDTargetControl != NULL);
  6590. }
  6591. //---------------------------------------------------------------------------
  6592. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6593. _di_IDataObject DataObj, int KeyState,
  6594. const TPoint & Point, int & Effect, bool & Accept)
  6595. {
  6596. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6597. }
  6598. //---------------------------------------------------------------------------
  6599. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6600. _di_IDataObject DataObj, int KeyState,
  6601. const TPoint & Point, int & Effect, bool & Accept)
  6602. {
  6603. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6604. }
  6605. //---------------------------------------------------------------------------
  6606. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6607. TObject * Sender)
  6608. {
  6609. DebugUsedParam(Sender);
  6610. DebugAssert(FDDTargetControl == Sender);
  6611. FDDTargetControl = NULL;
  6612. }
  6613. //---------------------------------------------------------------------------
  6614. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6615. {
  6616. FileControlDDDragLeave(SessionsPageControl);
  6617. }
  6618. //---------------------------------------------------------------------------
  6619. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6620. {
  6621. FileControlDDDragLeave(QueueView3);
  6622. }
  6623. //---------------------------------------------------------------------------
  6624. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6625. {
  6626. DebugAssert(!IsSideLocalBrowser(Side));
  6627. DebugUsedParam(Side);
  6628. bool Edit = false;
  6629. TRemoteFile * File = NULL;
  6630. UnicodeString FileName;
  6631. UnicodeString PointTo;
  6632. bool SymbolicLink = true;
  6633. if (RemoteDirView->ItemFocused)
  6634. {
  6635. DebugAssert(RemoteDirView->ItemFocused->Data);
  6636. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6637. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6638. if (Edit)
  6639. {
  6640. FileName = File->FileName;
  6641. PointTo = File->LinkTo;
  6642. }
  6643. else if (!File->IsParentDirectory)
  6644. {
  6645. PointTo = File->FileName;
  6646. }
  6647. }
  6648. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6649. Terminal->IsCapable[fcHardLink]))
  6650. {
  6651. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6652. if (Edit)
  6653. {
  6654. DebugAssert(File->FileName == FileName);
  6655. int Params = dfNoRecursive;
  6656. Terminal->ExceptionOnFail = true;
  6657. try
  6658. {
  6659. Terminal->DeleteFile(L"", File, &Params);
  6660. }
  6661. __finally
  6662. {
  6663. Terminal->ExceptionOnFail = false;
  6664. }
  6665. }
  6666. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6667. }
  6668. }
  6669. //---------------------------------------------------------------------------
  6670. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6671. {
  6672. return
  6673. (IsSideLocalBrowser(Side) ||
  6674. (HasActiveTerminal() &&
  6675. Terminal->ResolvingSymlinks &&
  6676. Terminal->IsCapable[fcSymbolicLink]));
  6677. }
  6678. //---------------------------------------------------------------------------
  6679. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6680. {
  6681. return
  6682. !IsSideLocalBrowser(FCurrentSide) &&
  6683. (RemoteDirView->ItemFocused != NULL) &&
  6684. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6685. Terminal->SessionData->ResolveSymlinks;
  6686. }
  6687. //---------------------------------------------------------------------------
  6688. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6689. {
  6690. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6691. FForceExecution = true;
  6692. try
  6693. {
  6694. DirView(osCurrent)->ExecuteCurrentFile();
  6695. }
  6696. __finally
  6697. {
  6698. FForceExecution = false;
  6699. }
  6700. }
  6701. //---------------------------------------------------------------------------
  6702. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6703. {
  6704. TEditorData ExternalEditor;
  6705. ExternalEditor.Editor = edExternal;
  6706. bool Remember = false;
  6707. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6708. {
  6709. if (Remember)
  6710. {
  6711. TEditorList * EditorList = new TEditorList;
  6712. try
  6713. {
  6714. *EditorList = *WinConfiguration->EditorList;
  6715. bool Found = false;
  6716. int i = 0;
  6717. while (!Found && (i < EditorList->Count))
  6718. {
  6719. const TEditorPreferences * Editor = EditorList->Editors[i];
  6720. if ((Editor->Data->Editor == edExternal) &&
  6721. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6722. {
  6723. Found = true;
  6724. }
  6725. i++;
  6726. }
  6727. if (!Found)
  6728. {
  6729. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6730. WinConfiguration->EditorList = EditorList;
  6731. }
  6732. }
  6733. __finally
  6734. {
  6735. delete EditorList;
  6736. }
  6737. }
  6738. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6739. }
  6740. }
  6741. //---------------------------------------------------------------------------
  6742. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6743. {
  6744. if (FFileFindTerminal == ATerminal)
  6745. {
  6746. FFileFindTerminal = NULL;
  6747. HideFileFindDialog();
  6748. }
  6749. if (FClipboardTerminal == ATerminal)
  6750. {
  6751. ClipboardClear(); // implies ClipboardStop
  6752. }
  6753. if ((FOnSynchronizeAbort != NULL) &&
  6754. (ATerminal == FManagedSession))
  6755. {
  6756. FOnSynchronizeAbort(NULL);
  6757. }
  6758. }
  6759. //---------------------------------------------------------------------------
  6760. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6761. {
  6762. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  6763. DetachTerminal(Sender);
  6764. }
  6765. //---------------------------------------------------------------------------
  6766. void __fastcall TCustomScpExplorerForm::FileTerminalReplaced(
  6767. const UnicodeString FileName, TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6768. {
  6769. TManagedTerminal * ATerminal = static_cast<TManagedTerminal *>(Arg);
  6770. if (Data->Terminal == Terminal)
  6771. {
  6772. Data->Terminal = ATerminal;
  6773. }
  6774. }
  6775. //---------------------------------------------------------------------------
  6776. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6777. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6778. {
  6779. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6780. DebugAssert(Terminal != NULL);
  6781. if (Data->Terminal == Terminal)
  6782. {
  6783. Data->Terminal = NULL;
  6784. }
  6785. }
  6786. //---------------------------------------------------------------------------
  6787. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6788. {
  6789. return NULL;
  6790. }
  6791. //---------------------------------------------------------------------------
  6792. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6793. {
  6794. UpdateControls();
  6795. SessionColor = TColor(0);
  6796. UpdateRemotePathComboBox(false);
  6797. NeedSession(false);
  6798. }
  6799. //---------------------------------------------------------------------------
  6800. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6801. {
  6802. try
  6803. {
  6804. // Cache, as the login dialog can change its value.
  6805. // See also DoShow. Also see TTerminalManager::NewSession.
  6806. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6807. try
  6808. {
  6809. if (ShowLogin)
  6810. {
  6811. bool ReloadSessions = !Startup;
  6812. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6813. }
  6814. }
  6815. __finally
  6816. {
  6817. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6818. // (so there was no chance for the user to open any session yet)
  6819. if ((ShowLogin || !Startup) &&
  6820. !WinConfiguration->KeepOpenWhenNoSession &&
  6821. // this rules out the implicit Commander's local-local browser
  6822. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6823. {
  6824. TerminateApplication();
  6825. }
  6826. }
  6827. }
  6828. catch (EAbort &)
  6829. {
  6830. // swallow
  6831. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6832. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6833. }
  6834. }
  6835. //---------------------------------------------------------------------------
  6836. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6837. {
  6838. TTerminalManager * Manager = TTerminalManager::Instance();
  6839. DebugAssert(!Manager->Updating);
  6840. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6841. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6842. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6843. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6844. try
  6845. {
  6846. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6847. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6848. {
  6849. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6850. }
  6851. for (int Index = 0; Index <= Manager->Count; Index++)
  6852. {
  6853. TThemeTabSheet * TabSheet;
  6854. if (Index >= SessionsPageControl->PageCount)
  6855. {
  6856. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6857. TabSheet->PageControl = SessionsPageControl;
  6858. }
  6859. else
  6860. {
  6861. TabSheet = SessionsPageControl->Pages[Index];
  6862. }
  6863. bool IsSessionTab = (Index < Manager->Count);
  6864. if (IsSessionTab)
  6865. {
  6866. TTerminal * Terminal = Manager->Sessions[Index];
  6867. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6868. TabSheet->CaptionTruncation = WinConfiguration->SessionTabCaptionTruncation ? tttEllipsis : tttNone;
  6869. UpdateSessionTab(TabSheet);
  6870. }
  6871. else
  6872. {
  6873. TabSheet->Tag = 0; // not really needed
  6874. TabSheet->Shadowed = false;
  6875. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6876. TabSheet->CaptionTruncation = GetNewTabTabCaptionTruncation();
  6877. // 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)
  6878. UpdateNewTabTab();
  6879. }
  6880. }
  6881. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6882. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6883. {
  6884. SessionsPageControl->UpdateTabsCaptionTruncation();
  6885. }
  6886. }
  6887. __finally
  6888. {
  6889. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6890. }
  6891. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6892. }
  6893. //---------------------------------------------------------------------------
  6894. TThemeTabCaptionTruncation TCustomScpExplorerForm::GetNewTabTabCaptionTruncation()
  6895. {
  6896. return WinConfiguration->SessionTabCaptionTruncation ? tttNoText : tttNone;
  6897. }
  6898. //---------------------------------------------------------------------------
  6899. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6900. {
  6901. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6902. }
  6903. //---------------------------------------------------------------------------
  6904. TThemeTabSheet * TCustomScpExplorerForm::GetNewTabTab()
  6905. {
  6906. return SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6907. }
  6908. //---------------------------------------------------------------------------
  6909. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6910. {
  6911. TThemeTabSheet * TabSheet = GetNewTabTab();
  6912. UnicodeString TabCaption;
  6913. if (WinConfiguration->SelectiveToolbarText)
  6914. {
  6915. TabCaption = GetNewTabTabCaption();
  6916. }
  6917. TabSheet->BaseCaption = TabCaption;
  6918. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6919. }
  6920. //---------------------------------------------------------------------------
  6921. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6922. {
  6923. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6924. }
  6925. //---------------------------------------------------------------------------
  6926. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6927. {
  6928. DebugFail();
  6929. return UnicodeString();
  6930. }
  6931. //---------------------------------------------------------------------------
  6932. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6933. {
  6934. if (DebugAlwaysTrue(TabSheet != NULL))
  6935. {
  6936. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6937. if (DebugAlwaysTrue(ASession != NULL))
  6938. {
  6939. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6940. if (ASession->LocalBrowser)
  6941. {
  6942. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6943. }
  6944. else
  6945. {
  6946. TabSheet->ImageIndex = AddSessionColor(Color);
  6947. }
  6948. TTerminalManager * Manager = TTerminalManager::Instance();
  6949. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6950. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6951. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6952. }
  6953. }
  6954. }
  6955. //---------------------------------------------------------------------------
  6956. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6957. {
  6958. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6959. }
  6960. //---------------------------------------------------------------------------
  6961. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6962. {
  6963. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6964. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6965. bool Result = (Session != NULL);
  6966. if (Result)
  6967. {
  6968. TTerminalManager::Instance()->ActiveSession = Session;
  6969. }
  6970. else
  6971. {
  6972. try
  6973. {
  6974. NewTab();
  6975. }
  6976. __finally
  6977. {
  6978. SessionListChanged();
  6979. }
  6980. FSessionsPageControlNewTabTime = Now();
  6981. }
  6982. return Result;
  6983. }
  6984. //---------------------------------------------------------------------------
  6985. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6986. {
  6987. TWindowLock Lock(this);
  6988. SessionTabSwitched();
  6989. }
  6990. //---------------------------------------------------------------------------
  6991. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6992. {
  6993. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6994. DebugAssert(TransferList != NULL);
  6995. UnicodeString Name;
  6996. if (TransferList->ItemIndex <= 0)
  6997. {
  6998. Name = L"";
  6999. }
  7000. else
  7001. {
  7002. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  7003. }
  7004. if (FCopyParamAutoSelected.IsEmpty())
  7005. {
  7006. // if previous preset was not autoselected, make new preset the "default"
  7007. FCopyParamDefault = Name;
  7008. }
  7009. GUIConfiguration->CopyParamCurrent = Name;
  7010. }
  7011. //---------------------------------------------------------------------------
  7012. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  7013. {
  7014. // sanity check
  7015. bool ExistingPreset =
  7016. (FTransferListHoverIndex >= 0) &&
  7017. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  7018. DebugAssert(ExistingPreset);
  7019. if (ExistingPreset)
  7020. {
  7021. HDC DC = GetDC(0);
  7022. TCanvas * Canvas = new TCanvas();
  7023. try
  7024. {
  7025. Canvas->Handle = DC;
  7026. Canvas->Font = GetToolbarFont(this);
  7027. UnicodeString Name;
  7028. if (FTransferListHoverIndex == 0)
  7029. {
  7030. Name = L"";
  7031. }
  7032. else
  7033. {
  7034. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  7035. }
  7036. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  7037. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  7038. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  7039. static_cast<TObject*>(GetComponent(fcTransferList)));
  7040. UnicodeString InfoStr =
  7041. GUIConfiguration->CopyParamPreset[Name].
  7042. GetInfoStr(L"; ",
  7043. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  7044. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  7045. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  7046. {
  7047. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  7048. {
  7049. InfoStr.SetLength(InfoStr.Length() - 1);
  7050. }
  7051. InfoStr += Ellipsis;
  7052. }
  7053. // UpdateCaption does not cause invalidation of whole submenu, while
  7054. // setting Caption property does.
  7055. // also it probably does not resize the label, even if necessary
  7056. // (we do not want that anyway)
  7057. TransferLabel->UpdateCaption(InfoStr);
  7058. }
  7059. __finally
  7060. {
  7061. Canvas->Handle = NULL;
  7062. ReleaseDC(0, DC);
  7063. delete Canvas;
  7064. }
  7065. }
  7066. }
  7067. //---------------------------------------------------------------------------
  7068. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  7069. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  7070. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  7071. {
  7072. if (FTransferListHoverIndex != AHoverIndex)
  7073. {
  7074. FTransferListHoverIndex = AHoverIndex;
  7075. UpdateTransferLabel();
  7076. }
  7077. }
  7078. //---------------------------------------------------------------------------
  7079. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  7080. {
  7081. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  7082. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  7083. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  7084. {
  7085. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  7086. {
  7087. if (!NonVisualDataModule->Busy)
  7088. {
  7089. OpenDirectory(GetSide(osCurrent));
  7090. }
  7091. Message.Result = 1;
  7092. }
  7093. else
  7094. {
  7095. TForm::Dispatch(&Message);
  7096. }
  7097. }
  7098. else
  7099. {
  7100. TForm::Dispatch(&Message);
  7101. }
  7102. }
  7103. //---------------------------------------------------------------------------
  7104. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  7105. {
  7106. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  7107. if ((FIgnoreNextDialogChar != 0) &&
  7108. (towupper(Message.CharCode) == towupper(FIgnoreNextDialogChar)))
  7109. {
  7110. Message.Result = 1;
  7111. }
  7112. else
  7113. {
  7114. TForm::Dispatch(&Message);
  7115. }
  7116. FIgnoreNextDialogChar = 0;
  7117. }
  7118. //---------------------------------------------------------------------------
  7119. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  7120. {
  7121. // The four low-order bits are used internally by Windows
  7122. unsigned int Cmd = (Message.WParam & 0xFFF0);
  7123. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  7124. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  7125. // 61730 - restore thru double click - undocumented
  7126. // 61490 - maximize thru double click - undocumented
  7127. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  7128. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  7129. {
  7130. SysResizing(Cmd);
  7131. }
  7132. TForm::Dispatch(&Message);
  7133. }
  7134. //---------------------------------------------------------------------------
  7135. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  7136. {
  7137. AppLog(L"Windows session end query");
  7138. // We were actually never able to make ENDSESSION_CRITICAL happen.
  7139. // Also there no point returning TRUE as we are not able to
  7140. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  7141. // Hence the process termination might be safer :)
  7142. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  7143. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  7144. {
  7145. Message.Result = FALSE;
  7146. }
  7147. else
  7148. {
  7149. Message.Result = TRUE;
  7150. }
  7151. // Do not call default handling as that triggers OnCloseQuery,
  7152. // where our implementation will popup confirmation dialogs, what we do not want,
  7153. // as per Vista guidelines:
  7154. // https://learn.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  7155. }
  7156. //---------------------------------------------------------------------------
  7157. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  7158. {
  7159. AppLog(L"Windows session end");
  7160. if (Message.EndSession && IsApplicationMinimized())
  7161. {
  7162. // WORKAROUND
  7163. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  7164. // when it receives WM_ENDSESSION,
  7165. // but that sometimes (particularly when application is minimized) cause crashes.
  7166. // Crash popups message that blocks log off.
  7167. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  7168. // so we call ExitProcess() immediately, not even trying to cleanup.
  7169. // It still causes beep, so there's likely some popup, but it does not block
  7170. // log off.
  7171. ExitProcess(0);
  7172. }
  7173. TForm::Dispatch(&Message);
  7174. }
  7175. #ifdef _DEBUG
  7176. //---------------------------------------------------------------------------
  7177. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  7178. {
  7179. TForm::Dispatch(&Message);
  7180. }
  7181. #endif
  7182. //---------------------------------------------------------------------------
  7183. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  7184. {
  7185. }
  7186. //---------------------------------------------------------------------------
  7187. void __fastcall TCustomScpExplorerForm::DoShow()
  7188. {
  7189. // Called from TCustomScpExplorerForm::CMShowingChanged
  7190. // only now are the controls resized finally, so the size constraints
  7191. // will not conflict with possibly very small window size
  7192. RestoreFormParams();
  7193. // This indicates that the form was rescaled after parameters were loaded and
  7194. // as most probably the parameters were actually saved with the current form scaling
  7195. // (windows was the last time likely closed on the same monitor, where it is starting now),
  7196. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  7197. // and no rounding problems will occur.
  7198. // See also RestoreForm.
  7199. // (we should be safe to call RestoreParams unconditionally)
  7200. if (PixelsPerInch != Screen->PixelsPerInch)
  7201. {
  7202. RestoreParams();
  7203. }
  7204. FixControlsPlacement();
  7205. if (Position == poDefaultPosOnly)
  7206. {
  7207. CutFormToDesktop(this);
  7208. }
  7209. TForm::DoShow();
  7210. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  7211. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  7212. // This was previously in NeedSession, where other related code is.
  7213. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  7214. // unpleasantly shows briefly.
  7215. if ((ManagedSession == NULL) &&
  7216. !WinConfiguration->ShowLoginWhenNoSession &&
  7217. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  7218. // This detects if workspace was saved the last time the main widow was closed
  7219. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  7220. {
  7221. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  7222. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  7223. }
  7224. // Before main window displays
  7225. if (ManagedSession == NULL)
  7226. {
  7227. StartingWithoutSession();
  7228. }
  7229. UpdatePixelsPerInchMainWindowCounter();
  7230. FShowing = true;
  7231. }
  7232. //---------------------------------------------------------------------------
  7233. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  7234. {
  7235. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  7236. }
  7237. //---------------------------------------------------------------------------
  7238. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  7239. {
  7240. SessionListChanged();
  7241. InitStatusBar();
  7242. UpdateControls();
  7243. }
  7244. //---------------------------------------------------------------------------
  7245. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  7246. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  7247. TNotifyEvent OnBalloonClick, TObject * UserData)
  7248. {
  7249. bool Do;
  7250. UnicodeString Message;
  7251. if (E == NULL)
  7252. {
  7253. Message = Str;
  7254. Do = true;
  7255. }
  7256. else
  7257. {
  7258. Do = ExceptionMessage(E, Message);
  7259. }
  7260. if (Do && WinConfiguration->BalloonNotifications)
  7261. {
  7262. UnicodeString Title;
  7263. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  7264. {
  7265. Message = TrimLeft(Message);
  7266. }
  7267. else
  7268. {
  7269. Message = UnformatMessage(Message);
  7270. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  7271. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  7272. Title = LoadResourceString(Captions[Type]);
  7273. if (Terminal != NULL)
  7274. {
  7275. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal->PrimaryTerminal));
  7276. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  7277. }
  7278. }
  7279. if (Seconds == 0)
  7280. {
  7281. Seconds = WinConfiguration->NotificationsTimeout;
  7282. }
  7283. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  7284. }
  7285. }
  7286. //---------------------------------------------------------------------------
  7287. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  7288. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  7289. UnicodeString HelpKeyword, const TMessageParams * Params,
  7290. TTerminal * Terminal)
  7291. {
  7292. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  7293. (FOnFeedSynchronizeError != NULL)) &&
  7294. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  7295. {
  7296. if (FOnFeedSynchronizeError != NULL)
  7297. {
  7298. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  7299. }
  7300. else
  7301. {
  7302. DebugAssert(FErrorList != NULL);
  7303. TStringList * MoreMessagesCopy = NULL;
  7304. if (MoreMessages)
  7305. {
  7306. MoreMessagesCopy = new TStringList();
  7307. MoreMessagesCopy->Assign(MoreMessages);
  7308. }
  7309. FErrorList->AddObject(Message, MoreMessagesCopy);
  7310. }
  7311. PopupTrayBalloon(Terminal, Message, Type);
  7312. return ContinueAnswer(Answers);
  7313. }
  7314. else
  7315. {
  7316. bool UseBalloon = FTrayIcon->Visible;
  7317. if (UseBalloon)
  7318. {
  7319. PopupTrayBalloon(Terminal, Message, Type);
  7320. }
  7321. unsigned int Result;
  7322. try
  7323. {
  7324. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  7325. }
  7326. __finally
  7327. {
  7328. // cancel only balloon we popped up, otherwise we may cancel notification
  7329. // balloon that was there before the message dialog
  7330. // (such as "dd confirmation opt in balloon)
  7331. if (UseBalloon)
  7332. {
  7333. FTrayIcon->CancelBalloon();
  7334. }
  7335. }
  7336. return Result;
  7337. }
  7338. }
  7339. //---------------------------------------------------------------------------
  7340. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  7341. TTerminal * Terminal, Exception * E)
  7342. {
  7343. if (FTrayIcon->Visible)
  7344. {
  7345. PopupTrayBalloon(Terminal, L"", qtError, E);
  7346. }
  7347. // particularly prevent opening new session from jump list,
  7348. // while exception is shown
  7349. NonVisualDataModule->StartBusy();
  7350. try
  7351. {
  7352. ShowExtendedExceptionEx(Terminal, E);
  7353. }
  7354. __finally
  7355. {
  7356. NonVisualDataModule->EndBusy();
  7357. FTrayIcon->CancelBalloon();
  7358. }
  7359. }
  7360. //---------------------------------------------------------------------------
  7361. void __fastcall TCustomScpExplorerForm::SessionReady()
  7362. {
  7363. InitStatusBar();
  7364. // cannot rely on active page being page for active terminal,
  7365. // as it can happen that active page is the "new session" page
  7366. // (e.g. when reconnecting active terminal, while login dialog
  7367. // invoked from "new session" page is modal)
  7368. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  7369. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  7370. }
  7371. //---------------------------------------------------------------------------
  7372. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  7373. TTerminal * Terminal, Exception * E)
  7374. {
  7375. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  7376. if (!Terminal->Active)
  7377. {
  7378. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  7379. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  7380. {
  7381. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  7382. }
  7383. }
  7384. }
  7385. //---------------------------------------------------------------------------
  7386. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  7387. UnicodeString Message, TQueryType Type,
  7388. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  7389. {
  7390. if ((E == NULL) ||
  7391. ExceptionMessage(E, Message))
  7392. {
  7393. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  7394. if (Important)
  7395. {
  7396. Seconds *= 5;
  7397. }
  7398. UnicodeString NoteMessage(UnformatMessage(Message));
  7399. if (Terminal != NULL)
  7400. {
  7401. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7402. NoteMessage = FORMAT(L"%s: %s",
  7403. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  7404. }
  7405. if (WinConfiguration->BalloonNotifications)
  7406. {
  7407. AddNote(NoteMessage);
  7408. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  7409. }
  7410. else
  7411. {
  7412. FlashOnBackground();
  7413. PostNote(NoteMessage, Seconds, OnClick, UserData);
  7414. }
  7415. }
  7416. }
  7417. //---------------------------------------------------------------------------
  7418. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7419. {
  7420. RestoreApp();
  7421. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7422. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7423. !NonVisualDataModule->Busy)
  7424. {
  7425. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7426. TTerminalManager::Instance()->ActiveSession = Terminal;
  7427. if (!ComponentVisible[fcQueueView])
  7428. {
  7429. ToggleQueueVisibility();
  7430. GoToQueue();
  7431. }
  7432. }
  7433. }
  7434. //---------------------------------------------------------------------------
  7435. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7436. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7437. {
  7438. UnicodeString Message;
  7439. TNotifyEvent OnClick = NULL;
  7440. TObject * UserData = NULL;
  7441. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7442. switch (Event)
  7443. {
  7444. case qeEmptyButMonitored:
  7445. if ((ATerminal != Terminal) || QueueInvisible)
  7446. {
  7447. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7448. OnClick = QueueEmptyNoteClicked;
  7449. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7450. TerminalNoteData->Terminal = ATerminal;
  7451. UserData = TerminalNoteData;
  7452. }
  7453. break;
  7454. case qeEmpty:
  7455. OperationComplete(ATerminal->QueueOperationStart);
  7456. break;
  7457. case qePendingUserAction:
  7458. if ((ATerminal != Terminal) ||
  7459. (QueueInvisible && !IsQueueAutoPopup()))
  7460. {
  7461. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7462. }
  7463. break;
  7464. default:
  7465. DebugFail();
  7466. }
  7467. if (!Message.IsEmpty())
  7468. {
  7469. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7470. }
  7471. }
  7472. //---------------------------------------------------------------------------
  7473. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7474. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7475. {
  7476. if (FDDExtMapFile != NULL)
  7477. {
  7478. CloseHandle(FDDExtMapFile);
  7479. FDDExtMapFile = NULL;
  7480. }
  7481. if (WinConfiguration->DDFakeFile)
  7482. {
  7483. DDFakeFileInitDrag(FileList, Created);
  7484. }
  7485. }
  7486. //---------------------------------------------------------------------------
  7487. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7488. {
  7489. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7490. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7491. {
  7492. FFakeFileDropTarget = FileName;
  7493. }
  7494. }
  7495. //---------------------------------------------------------------------------
  7496. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7497. {
  7498. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7499. if (!ForceDirectories(ApiPath(Result)))
  7500. {
  7501. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7502. }
  7503. FileSetAttr(ApiPath(Result), faHidden);
  7504. return Result;
  7505. }
  7506. //---------------------------------------------------------------------------
  7507. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7508. bool & Created)
  7509. {
  7510. FFakeFileDropTarget = UnicodeString();
  7511. FDragFakeDirectory = CreateFakeTransferDirectory();
  7512. FileList->AddItem(NULL, FDragFakeDirectory);
  7513. Created = true;
  7514. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7515. {
  7516. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7517. }
  7518. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7519. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7520. {
  7521. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7522. TDragExtCommStruct* CommStruct;
  7523. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7524. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7525. DebugAssert(CommStruct != NULL);
  7526. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7527. CommStruct->Dragging = true;
  7528. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7529. LENOF(CommStruct->DropDest));
  7530. NULL_TERMINATE(CommStruct->DropDest);
  7531. UnmapViewOfFile(CommStruct);
  7532. }
  7533. }
  7534. //---------------------------------------------------------------------------
  7535. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7536. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7537. {
  7538. bool Result;
  7539. DebugAssert(!IsLocalBrowserMode());
  7540. if (Sender == RemoteDirView)
  7541. {
  7542. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7543. }
  7544. else
  7545. {
  7546. DebugAssert(Sender == RemoteDriveView);
  7547. TStrings * FileList = RemoteDriveView->DragFileList();
  7548. try
  7549. {
  7550. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7551. }
  7552. __finally
  7553. {
  7554. delete FileList;
  7555. }
  7556. }
  7557. if (FDDTargetControl == RemoteDriveView)
  7558. {
  7559. RemoteDriveView->UpdateDropTarget();
  7560. }
  7561. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7562. // or from tree view to dir view.
  7563. // foMove happens when file/dir is dragged from remote tree view outside of
  7564. // application via dragex
  7565. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7566. (DropSourceControl == RemoteDriveView))
  7567. {
  7568. RemoteDriveView->UpdateDropSource();
  7569. }
  7570. return Result;
  7571. }
  7572. //---------------------------------------------------------------------------
  7573. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7574. {
  7575. DebugAssert(!IsLocalBrowserMode());
  7576. // This also handles drops of remote files to queue.
  7577. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7578. SAFE_DESTROY(FDDFileList);
  7579. if (!FFakeFileDropTarget.IsEmpty())
  7580. {
  7581. RemoveDir(ApiPath(FFakeFileDropTarget));
  7582. }
  7583. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7584. {
  7585. try
  7586. {
  7587. TDragResult DDResult = (Sender == RemoteDirView) ?
  7588. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7589. // Focus is moved to the target application,
  7590. // but as we are going to present the UI, we need to steal the focus back.
  7591. // This most likely won't work though (windows does not allow application
  7592. // to steal focus most of the time)
  7593. Application->BringToFront();
  7594. // On older version of Windows we never got drMove here, see also comment below.
  7595. // On Windows 10, we get the "move".
  7596. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7597. {
  7598. UnicodeString TargetDirectory;
  7599. TFileOperation Operation;
  7600. // drInvalid may mean drMove, see comment below
  7601. switch (DDResult)
  7602. {
  7603. case drCopy:
  7604. Operation = foCopy;
  7605. break;
  7606. case drMove:
  7607. Operation = foMove;
  7608. break;
  7609. default:
  7610. DebugFail();
  7611. case drInvalid:
  7612. // prefer "copy" for safety
  7613. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7614. break;
  7615. }
  7616. TTransferOperationParam Param;
  7617. UnicodeString CounterName;
  7618. bool ForceQueue;
  7619. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7620. {
  7621. // we get drInvalid both if d&d was intercepted by ddext,
  7622. // and when users drops on no-drop location.
  7623. // we tell the difference by existence of response from ddext,
  7624. // so we ignore absence of response in this case
  7625. if (DDResult != drInvalid)
  7626. {
  7627. // here we know that the extension is installed,
  7628. // as it is checked as soon as drag&drop starts from
  7629. // RemoteFileControlDDCreateDragFileList
  7630. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7631. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7632. {
  7633. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7634. }
  7635. else
  7636. {
  7637. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7638. }
  7639. }
  7640. }
  7641. else
  7642. {
  7643. // download using fake file
  7644. Param.Temp = false;
  7645. Param.DragDrop = true;
  7646. if (ForceQueue)
  7647. {
  7648. Param.Queue = asOn;
  7649. }
  7650. if (Sender == RemoteDirView)
  7651. {
  7652. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7653. }
  7654. if (RemoteFileControlFileOperation(Sender, Operation,
  7655. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7656. {
  7657. Configuration->Usage->Inc(CounterName);
  7658. }
  7659. }
  7660. }
  7661. }
  7662. __finally
  7663. {
  7664. CloseHandle(FDDExtMapFile);
  7665. FDDExtMapFile = NULL;
  7666. delete FDragFakeMonitors;
  7667. FDragFakeMonitors = NULL;
  7668. RemoveDir(ApiPath(FDragFakeDirectory));
  7669. FDragFakeDirectory = L"";
  7670. FFakeFileDropTarget = UnicodeString();
  7671. }
  7672. }
  7673. if (FDragDropSshTerminate.get() != NULL)
  7674. {
  7675. FDragDropSshTerminate->Rethrow();
  7676. }
  7677. }
  7678. //---------------------------------------------------------------------------
  7679. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7680. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7681. {
  7682. // Remember drop effect so we know (when user drops files), if we copy or move
  7683. FLastDropEffect = dwEffect;
  7684. }
  7685. //---------------------------------------------------------------------------
  7686. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7687. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7688. {
  7689. bool Result;
  7690. if (FDDTargetControl == QueueView3)
  7691. {
  7692. Directory = DefaultDownloadTargetDirectory();
  7693. Result = true;
  7694. CounterName = L"DownloadsDragDropQueue";
  7695. ForceQueue = true;
  7696. }
  7697. else if (!FFakeFileDropTarget.IsEmpty())
  7698. {
  7699. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7700. Result = true;
  7701. ForceQueue = false;
  7702. CounterName = L"DownloadsDragDropFakeFile";
  7703. }
  7704. else
  7705. {
  7706. ForceQueue = false;
  7707. Enabled = false;
  7708. try
  7709. {
  7710. int Timer = 0;
  7711. Result = false;
  7712. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7713. {
  7714. {
  7715. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7716. TDragExtCommStruct* CommStruct;
  7717. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7718. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7719. DebugAssert(CommStruct != NULL);
  7720. Result = !CommStruct->Dragging;
  7721. if (Result)
  7722. {
  7723. Directory = ExtractFilePath(CommStruct->DropDest);
  7724. CounterName = L"DownloadsDragDropExternalExt";
  7725. }
  7726. UnmapViewOfFile(CommStruct);
  7727. }
  7728. if (!Result)
  7729. {
  7730. Sleep(50);
  7731. Timer += 50;
  7732. Application->ProcessMessages();
  7733. }
  7734. }
  7735. }
  7736. __finally
  7737. {
  7738. Enabled = true;
  7739. }
  7740. }
  7741. return Result;
  7742. }
  7743. //---------------------------------------------------------------------------
  7744. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7745. const UnicodeString TempDir, int SecDelay)
  7746. {
  7747. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7748. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7749. if (FDelayedDeletionTimer == NULL)
  7750. {
  7751. DebugAssert(HandleAllocated());
  7752. FDelayedDeletionTimer = new TTimer(this);
  7753. FDelayedDeletionTimer->Interval = 10000;
  7754. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7755. }
  7756. else
  7757. {
  7758. FDelayedDeletionTimer->Enabled = true;
  7759. }
  7760. }
  7761. //---------------------------------------------------------------------------
  7762. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7763. {
  7764. DebugAssert(FDelayedDeletionList != NULL);
  7765. TDateTime N = Now();
  7766. TDateTime Alert;
  7767. UnicodeString Directory;
  7768. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7769. {
  7770. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7771. if ((N >= Alert) || (Sender == NULL))
  7772. {
  7773. Directory = FDelayedDeletionList->Strings[Index];
  7774. if (RecursiveDeleteFile(ExcludeTrailingBackslash(Directory)))
  7775. {
  7776. FDelayedDeletionList->Delete(Index);
  7777. }
  7778. }
  7779. }
  7780. if (FDelayedDeletionList->Count == 0)
  7781. {
  7782. FDelayedDeletionTimer->Enabled = false;
  7783. }
  7784. }
  7785. //---------------------------------------------------------------------------
  7786. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7787. {
  7788. DebugAssert(!IsLocalBrowserMode());
  7789. if (IsFileControl(FDDTargetControl, osRemote) ||
  7790. (FDDTargetControl == SessionsPageControl))
  7791. {
  7792. TManagedTerminal * TargetTerminal = NULL;
  7793. TFileOperation Operation = foNone;
  7794. if (FDDTargetControl == SessionsPageControl)
  7795. {
  7796. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7797. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7798. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7799. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7800. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7801. {
  7802. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7803. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7804. {
  7805. Operation = foRemoteMove;
  7806. }
  7807. else
  7808. {
  7809. Operation = foRemoteCopy;
  7810. }
  7811. }
  7812. }
  7813. else
  7814. {
  7815. if (FLastDropEffect == DROPEFFECT_MOVE)
  7816. {
  7817. Operation = foRemoteMove;
  7818. }
  7819. else if (FLastDropEffect == DROPEFFECT_COPY)
  7820. {
  7821. Operation = foRemoteCopy;
  7822. }
  7823. }
  7824. if (Operation != foNone)
  7825. {
  7826. RemoteFileControlFileOperation(DropSourceControl,
  7827. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7828. }
  7829. // abort drag&drop
  7830. Abort();
  7831. }
  7832. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7833. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7834. (FDDTargetControl != QueueView3))
  7835. {
  7836. DebugAssert(!FDragTempDir.IsEmpty());
  7837. TTransferType Type;
  7838. UnicodeString TempDir = FDragTempDir;
  7839. // We clear FDragTempDir before calling
  7840. // just in case it fail (raises exception)
  7841. FDragTempDir = L"";
  7842. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7843. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7844. // empty directory parameter means temp directory -> don't display it!
  7845. UnicodeString TargetDir = L"";
  7846. int Options = 0;
  7847. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7848. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7849. {
  7850. Abort();
  7851. }
  7852. // TargetDir is set when dropped on local file control
  7853. // (this was workaround for legacy dirview event handling, now it should be
  7854. // made prettier)
  7855. if (TargetDir.IsEmpty())
  7856. {
  7857. TargetDir = TempDir;
  7858. if (ForceDirectories(ApiPath(TargetDir)))
  7859. {
  7860. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7861. FPendingTempSpaceWarn = true;
  7862. try
  7863. {
  7864. FDragDropOperation = true;
  7865. int Params = cpTemporary |
  7866. (Type == ttMove ? cpDelete : 0);
  7867. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7868. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7869. }
  7870. __finally
  7871. {
  7872. FDragDropOperation = false;
  7873. FPendingTempSpaceWarn = false;
  7874. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7875. }
  7876. }
  7877. else
  7878. {
  7879. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7880. }
  7881. }
  7882. }
  7883. }
  7884. //---------------------------------------------------------------------------
  7885. void __fastcall TCustomScpExplorerForm::DDDownload(
  7886. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  7887. {
  7888. DebugAssert(!IsLocalBrowserMode());
  7889. TAutoBatch AutoBatch(this);
  7890. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  7891. UpdateCopyParamCounters(*CopyParam);
  7892. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7893. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7894. {
  7895. RemoteDriveView->UpdateDropSource();
  7896. }
  7897. }
  7898. //---------------------------------------------------------------------------
  7899. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7900. {
  7901. public:
  7902. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7903. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7904. {
  7905. }
  7906. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7907. {
  7908. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7909. TDataObjectFilesEx::AllowData(FormatEtc);
  7910. }
  7911. };
  7912. //---------------------------------------------------------------------------
  7913. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7914. TObject * Sender, TDataObject *& DataObject)
  7915. {
  7916. if (FDDExtMapFile != NULL)
  7917. {
  7918. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7919. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7920. {
  7921. Abort();
  7922. }
  7923. if (FileList->Count > 0)
  7924. {
  7925. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7926. if (!FilesObject->IsValid(true, true))
  7927. {
  7928. FilesObject->_Release();
  7929. }
  7930. else
  7931. {
  7932. DataObject = FilesObject;
  7933. }
  7934. }
  7935. }
  7936. }
  7937. //---------------------------------------------------------------------------
  7938. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7939. {
  7940. DebugFail();
  7941. }
  7942. //---------------------------------------------------------------------------
  7943. void __fastcall TCustomScpExplorerForm::GoToTree()
  7944. {
  7945. ComponentVisible[fcRemoteTree] = true;
  7946. DriveView(osRemote)->SetFocus();
  7947. }
  7948. //---------------------------------------------------------------------------
  7949. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7950. TPanelExport Export)
  7951. {
  7952. TCustomDirView * DirView = this->DirView(Side);
  7953. std::unique_ptr<TStrings> ExportData(new TStringList());
  7954. switch (Export)
  7955. {
  7956. case pePath:
  7957. ExportData->Add(DirView->PathName);
  7958. break;
  7959. case peFileList:
  7960. case peFullFileList:
  7961. {
  7962. bool FullPath = (Export == peFullFileList);
  7963. DirView->CreateFileList(false, FullPath, ExportData.get());
  7964. for (int Index = 0; Index < ExportData->Count; Index++)
  7965. {
  7966. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7967. {
  7968. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7969. }
  7970. }
  7971. }
  7972. break;
  7973. default:
  7974. DebugFail();
  7975. }
  7976. return ExportData.release();
  7977. }
  7978. //---------------------------------------------------------------------------
  7979. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7980. TPanelExport Export, TPanelExportDestination Destination)
  7981. {
  7982. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7983. PanelExportStore(Side, Export, Destination, ExportData.get());
  7984. }
  7985. //---------------------------------------------------------------------------
  7986. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7987. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7988. TStrings * ExportData)
  7989. {
  7990. if (Destination == pedClipboard)
  7991. {
  7992. TInstantOperationVisualizer Visualizer;
  7993. CopyToClipboard(ExportData);
  7994. }
  7995. else
  7996. {
  7997. DebugFail();
  7998. }
  7999. }
  8000. //---------------------------------------------------------------------------
  8001. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  8002. {
  8003. TCustomDirView * DirView = this->DirView(Side);
  8004. UnicodeString Mask = DirView->Mask;
  8005. if (DoFilterMaskDialog(DirView, Mask))
  8006. {
  8007. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  8008. Configuration->Usage->Inc(L"Filters");
  8009. }
  8010. }
  8011. //---------------------------------------------------------------------------
  8012. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  8013. {
  8014. return FQueueController->DefaultOperation();
  8015. }
  8016. //---------------------------------------------------------------------------
  8017. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  8018. TQueueOperation Operation, void ** Param)
  8019. {
  8020. switch (Operation)
  8021. {
  8022. case qoPreferences:
  8023. return true;
  8024. case qoGoTo:
  8025. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  8026. case qoOnceEmpty:
  8027. return IsAnythingQueued();
  8028. default:
  8029. return FQueueController->AllowOperation(Operation, Param);
  8030. }
  8031. }
  8032. //---------------------------------------------------------------------------
  8033. void __fastcall TCustomScpExplorerForm::GoToQueue()
  8034. {
  8035. if (DebugAlwaysTrue(QueueView3->Visible))
  8036. {
  8037. QueueView3->SetFocus();
  8038. }
  8039. }
  8040. //---------------------------------------------------------------------------
  8041. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  8042. TQueueOperation Operation, void * Param)
  8043. {
  8044. if (Operation == qoGoTo)
  8045. {
  8046. GoToQueue();
  8047. }
  8048. else if (Operation == qoPreferences)
  8049. {
  8050. PreferencesDialog(pmQueue);
  8051. }
  8052. else
  8053. {
  8054. FQueueController->ExecuteOperation(Operation, Param);
  8055. }
  8056. }
  8057. //---------------------------------------------------------------------------
  8058. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  8059. {
  8060. return (Queue != NULL) && Queue->Enabled;
  8061. }
  8062. //---------------------------------------------------------------------------
  8063. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  8064. {
  8065. DebugAssert(Queue != NULL);
  8066. if (Queue != NULL)
  8067. {
  8068. Queue->Enabled = !Queue->Enabled;
  8069. }
  8070. }
  8071. //---------------------------------------------------------------------------
  8072. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  8073. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  8074. {
  8075. FQueueActedItem = QueueView3->ItemFocused;
  8076. }
  8077. //---------------------------------------------------------------------------
  8078. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  8079. {
  8080. return TopDock->Height + (MessageDock->Visible ? MessageDock->Height : 0) + QueueSplitter->Height;
  8081. }
  8082. //---------------------------------------------------------------------------
  8083. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  8084. {
  8085. return
  8086. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  8087. (QueueDock->Visible ? QueueDock->Height : 0) +
  8088. (QueueLabel->Visible ? QueueLabel->Height : 0);
  8089. }
  8090. //---------------------------------------------------------------------------
  8091. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  8092. {
  8093. return ScaleByTextHeight(this, 48);
  8094. }
  8095. //---------------------------------------------------------------------------
  8096. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  8097. TObject * /*Sender*/, int & NewSize, bool & Accept)
  8098. {
  8099. // when queue is hidden by double-clicking splitter, stray attempt to
  8100. // resize the panel with strange value arrives, make sure it is ignored
  8101. if (ComponentVisible[fcQueueView])
  8102. {
  8103. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  8104. if (NewSize > MaxHeightLimit)
  8105. {
  8106. NewSize = MaxHeightLimit;
  8107. }
  8108. int MinHeightLimit =
  8109. GetStaticQueuePanelComponentsHeight() +
  8110. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  8111. GetMinQueueViewHeight();
  8112. if (NewSize < MinHeightLimit)
  8113. {
  8114. NewSize = MinHeightLimit;
  8115. }
  8116. }
  8117. else
  8118. {
  8119. Accept = false;
  8120. }
  8121. }
  8122. //---------------------------------------------------------------------------
  8123. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  8124. {
  8125. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  8126. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  8127. if (QueueViewHeight < GetMinQueueViewHeight())
  8128. {
  8129. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  8130. }
  8131. }
  8132. //---------------------------------------------------------------------------
  8133. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  8134. TDragObject *& /*DragObject*/)
  8135. {
  8136. FQueueActedItem = QueueView3->ItemFocused;
  8137. FQueueScrollOnDragOver->StartDrag();
  8138. }
  8139. //---------------------------------------------------------------------------
  8140. void __fastcall TCustomScpExplorerForm::QueueView3EndDrag(
  8141. TObject *, TObject * DebugUsedArg(Target), int DebugUsedArg(X), int DebugUsedArg(Y))
  8142. {
  8143. FQueueScrollOnDragOver->EndDrag();
  8144. }
  8145. //---------------------------------------------------------------------------
  8146. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  8147. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  8148. {
  8149. Accept = true;
  8150. if (Source == QueueView3)
  8151. {
  8152. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  8153. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  8154. if (Accept)
  8155. {
  8156. TQueueItemProxy * QueueItem;
  8157. TQueueItemProxy * DestQueueItem;
  8158. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  8159. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  8160. Accept = (QueueItem != DestQueueItem) &&
  8161. (QueueItem->Status == TQueueItem::qsPending) &&
  8162. (DestQueueItem->Status == TQueueItem::qsPending);
  8163. }
  8164. FQueueScrollOnDragOver->DragOver(TPoint(X, Y));
  8165. }
  8166. }
  8167. //---------------------------------------------------------------------------
  8168. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  8169. TObject * /*Source*/, int /*X*/, int /*Y*/)
  8170. {
  8171. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  8172. {
  8173. TQueueItemProxy * QueueItem;
  8174. TQueueItemProxy * DestQueueItem;
  8175. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  8176. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  8177. QueueItem->Move(DestQueueItem);
  8178. }
  8179. }
  8180. //---------------------------------------------------------------------------
  8181. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  8182. {
  8183. if ((QueueView3->ItemFocused == NULL) &&
  8184. (QueueView3->Items->Count > 0))
  8185. {
  8186. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  8187. }
  8188. QueueLabelUpdateStatus();
  8189. }
  8190. //---------------------------------------------------------------------------
  8191. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  8192. {
  8193. QueueLabelUpdateStatus();
  8194. }
  8195. //---------------------------------------------------------------------------
  8196. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  8197. {
  8198. QueueLabel->UpdateStatus();
  8199. }
  8200. //---------------------------------------------------------------------------
  8201. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  8202. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  8203. {
  8204. if (Selected)
  8205. {
  8206. NonVisualDataModule->UpdateNonVisibleActions();
  8207. }
  8208. }
  8209. //---------------------------------------------------------------------------
  8210. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  8211. {
  8212. TDragDropFilesEx * Result = NULL;
  8213. if (Sender == SessionsPageControl)
  8214. {
  8215. Result = FSessionsDragDropFilesEx;
  8216. }
  8217. else if (Sender == QueueView3)
  8218. {
  8219. Result = FQueueDragDropFilesEx;
  8220. }
  8221. else
  8222. {
  8223. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8224. if (DirView != NULL)
  8225. {
  8226. Result = DirView->DragDropFilesEx;
  8227. }
  8228. else
  8229. {
  8230. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  8231. if (DriveView != NULL)
  8232. {
  8233. Result = DriveView->DragDropFilesEx;
  8234. }
  8235. }
  8236. }
  8237. DebugAssert(Result != NULL);
  8238. return Result;
  8239. }
  8240. //---------------------------------------------------------------------------
  8241. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  8242. {
  8243. return (DView->SelCount == DView->FilesCount);
  8244. }
  8245. //---------------------------------------------------------------------------
  8246. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  8247. {
  8248. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  8249. }
  8250. //---------------------------------------------------------------------------
  8251. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  8252. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  8253. {
  8254. DebugAssert(!IsLocalBrowserMode());
  8255. TFileOperation Operation;
  8256. switch (Effect)
  8257. {
  8258. case DROPEFFECT_MOVE:
  8259. Operation = foMove;
  8260. break;
  8261. case DROPEFFECT_COPY:
  8262. // occures on WinXP (reported by user)
  8263. default:
  8264. Operation = foCopy;
  8265. break;
  8266. };
  8267. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  8268. // see a comment in TUnixDirView::PerformItemDragDropOperation
  8269. if (DragDropFilesEx->FileList->Count > 0)
  8270. {
  8271. TStrings * FileList = new TStringList();
  8272. try
  8273. {
  8274. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  8275. {
  8276. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  8277. }
  8278. FDragDropOperation = true;
  8279. TTransferOperationParam Param;
  8280. Param.TargetDirectory = TargetPath;
  8281. // upload, no temp dirs
  8282. Param.Temp = false;
  8283. Param.DragDrop = DragDrop;
  8284. Param.Options =
  8285. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  8286. if (ForceQueue)
  8287. {
  8288. Param.Queue = asOn;
  8289. }
  8290. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  8291. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  8292. {
  8293. if (IsFileControl(DropSourceControl, osLocal))
  8294. {
  8295. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  8296. }
  8297. else
  8298. {
  8299. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  8300. }
  8301. }
  8302. }
  8303. __finally
  8304. {
  8305. FDragDropOperation = false;
  8306. delete FileList;
  8307. }
  8308. }
  8309. }
  8310. //---------------------------------------------------------------------------
  8311. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  8312. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  8313. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  8314. {
  8315. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  8316. }
  8317. //---------------------------------------------------------------------------
  8318. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  8319. TObject * Sender, int grfKeyState, int & dwEffect)
  8320. {
  8321. DebugAssert(!IsLocalBrowserMode());
  8322. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  8323. if ((dwEffect != DROPEFFECT_NONE) &&
  8324. IsFileControl(DropSourceControl, osRemote))
  8325. {
  8326. // do not allow drop on remote panel (on free space, still allow drop on directories)
  8327. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  8328. (RemoteDirView->DropTarget == NULL))
  8329. {
  8330. dwEffect = DROPEFFECT_NONE;
  8331. }
  8332. else
  8333. {
  8334. if (dwEffect == DROPEFFECT_COPY)
  8335. {
  8336. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  8337. // currently we support copying always (at least via temporary directory);
  8338. // remove associated checks once this all proves stable and working
  8339. bool CopyCapable = true;
  8340. // if we do not support neither of operations, there's no discussion
  8341. if (!MoveCapable && !CopyCapable)
  8342. {
  8343. dwEffect = DROPEFFECT_NONE;
  8344. }
  8345. else
  8346. {
  8347. // The default effect inside remote panel is move,
  8348. // unless we do not support it, but support copy
  8349. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  8350. {
  8351. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  8352. }
  8353. else
  8354. {
  8355. // with ctrl-down, we want copy unless it is not supported
  8356. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  8357. }
  8358. }
  8359. }
  8360. }
  8361. }
  8362. }
  8363. //---------------------------------------------------------------------------
  8364. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  8365. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  8366. {
  8367. DebugAssert(!IsLocalBrowserMode());
  8368. if (FDDTotalSize >= 0)
  8369. {
  8370. if (File->IsDirectory)
  8371. {
  8372. FDDTotalSize = -1;
  8373. }
  8374. else
  8375. {
  8376. FDDTotalSize += File->Size;
  8377. }
  8378. }
  8379. DebugAssert(!FDragTempDir.IsEmpty());
  8380. // TODO: this is quite ineffective
  8381. // TODO: what if invalid character replacement is disabled?
  8382. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  8383. UnicodeString TransferFileName;
  8384. if (Sender == RemoteDriveView)
  8385. {
  8386. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  8387. }
  8388. else
  8389. {
  8390. TransferFileName = File->FileName;
  8391. }
  8392. FDDFileList->AddObject(TransferFileName, File);
  8393. }
  8394. //---------------------------------------------------------------------------
  8395. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  8396. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  8397. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  8398. {
  8399. // sometimes we do not get DDEnd so the list is not released
  8400. SAFE_DESTROY(FDDFileList);
  8401. FDDFileList = new TStringList();
  8402. FDragTempDir = WinConfiguration->TemporaryDir();
  8403. FDDTotalSize = 0;
  8404. FDragDropSshTerminate.reset(NULL);
  8405. }
  8406. //---------------------------------------------------------------------------
  8407. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  8408. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  8409. HRESULT & Result)
  8410. {
  8411. if (Result == DRAGDROP_S_DROP)
  8412. {
  8413. try
  8414. {
  8415. GlobalDragImageList->HideDragImage();
  8416. try
  8417. {
  8418. RemoteFileControlDDTargetDrop();
  8419. }
  8420. catch(ESshTerminate & E)
  8421. {
  8422. FDragDropSshTerminate.reset(E.Clone());
  8423. }
  8424. }
  8425. catch (Exception &E)
  8426. {
  8427. // If downloading fails we need to cancel drag&drop, otherwise
  8428. // Explorer shows error
  8429. // But by the way exception probably never reach this point as
  8430. // it's catched on way.
  8431. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8432. Result = DRAGDROP_S_CANCEL;
  8433. ShowExtendedException(Terminal, &E);
  8434. }
  8435. }
  8436. }
  8437. //---------------------------------------------------------------------------
  8438. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8439. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8440. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8441. {
  8442. TFileMasks::TParams MaskParams;
  8443. MaskParams.Size = Size;
  8444. MaskParams.Modification = Modification;
  8445. // this does not re-parse the mask if it is the same as the last time
  8446. FDirViewMatchMask = Masks;
  8447. bool ImplicitMatch;
  8448. Matches =
  8449. // Local and RecurseInclude parameters have no effect as there's no path.
  8450. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  8451. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  8452. (AllowImplicitMatches || !ImplicitMatch);
  8453. }
  8454. //---------------------------------------------------------------------------
  8455. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8456. TObject * Sender, TListItem * Item, WORD & Indexes)
  8457. {
  8458. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8459. if (GetPartialFileExtLen(DirView->ItemFileName(Item)) > 0)
  8460. {
  8461. Indexes |= oiPartial;
  8462. }
  8463. }
  8464. //---------------------------------------------------------------------------
  8465. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8466. {
  8467. return
  8468. DirViewEnabled(osCurrent) &&
  8469. DirView(osCurrent)->CanPasteFromClipBoard();
  8470. }
  8471. //---------------------------------------------------------------------------
  8472. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8473. {
  8474. bool Result = false;
  8475. if (CanPasteToDirViewFromClipBoard())
  8476. {
  8477. Result = true;
  8478. }
  8479. else
  8480. {
  8481. UnicodeString ClipboardText;
  8482. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8483. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8484. {
  8485. if (StoredSessions->IsUrl(ClipboardText))
  8486. {
  8487. Result = true;
  8488. }
  8489. else
  8490. {
  8491. Result = DirViewEnabled(osCurrent);
  8492. }
  8493. }
  8494. }
  8495. return Result;
  8496. }
  8497. //---------------------------------------------------------------------------
  8498. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8499. {
  8500. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8501. if (DoesClipboardContainOurFiles())
  8502. {
  8503. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8504. {
  8505. DebugAssert(!IsLocalBrowserMode());
  8506. TTerminalManager * Manager = TTerminalManager::Instance();
  8507. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8508. Manager->ActiveSession = FClipboardTerminal;
  8509. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8510. }
  8511. }
  8512. else if (CanPasteToDirViewFromClipBoard())
  8513. {
  8514. DirView(osCurrent)->PasteFromClipBoard();
  8515. }
  8516. else
  8517. {
  8518. UnicodeString ClipboardText;
  8519. if (NonEmptyTextFromClipboard(ClipboardText))
  8520. {
  8521. if (StoredSessions->IsUrl(ClipboardText))
  8522. {
  8523. NewSession(ClipboardText);
  8524. }
  8525. else
  8526. {
  8527. DirView(osCurrent)->Path = ClipboardText;
  8528. }
  8529. }
  8530. }
  8531. }
  8532. //---------------------------------------------------------------------------
  8533. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8534. {
  8535. // TBD
  8536. }
  8537. //---------------------------------------------------------------------------
  8538. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8539. {
  8540. TCustomDirView * ADirView = DirView(Side);
  8541. ADirView->SelectAll(Mode);
  8542. ADirView->SetFocus();
  8543. }
  8544. //---------------------------------------------------------------------------
  8545. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8546. {
  8547. TCustomDirView * ADirView = DirView(Side);
  8548. TFileFilter Filter;
  8549. DefaultFileFilter(Filter);
  8550. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8551. {
  8552. Configuration->Usage->Inc(L"MaskSelections");
  8553. ADirView->SelectFiles(Filter, Select);
  8554. ADirView->SetFocus();
  8555. }
  8556. }
  8557. //---------------------------------------------------------------------------
  8558. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8559. {
  8560. TCustomDirView * ADirView = DirView(Side);
  8561. ADirView->RestoreSelectedNames();
  8562. ADirView->SetFocus();
  8563. }
  8564. //---------------------------------------------------------------------------
  8565. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8566. {
  8567. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8568. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8569. {
  8570. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8571. UnicodeString Ext;
  8572. if (!IsSideLocalBrowser(osCurrent))
  8573. {
  8574. Ext = UnixExtractFileExt(FileName);
  8575. }
  8576. else
  8577. {
  8578. Ext = ExtractFileExt(FileName);
  8579. }
  8580. if (Ext.IsEmpty())
  8581. {
  8582. Ext = L".";
  8583. }
  8584. TFileFilter Filter;
  8585. Filter.Masks = FORMAT(L"*%s", (Ext));
  8586. Filter.Directories = false;
  8587. CurrentDirView->SelectFiles(Filter, Select);
  8588. }
  8589. }
  8590. //---------------------------------------------------------------------------
  8591. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8592. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8593. {
  8594. UnicodeString Result;
  8595. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8596. {
  8597. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8598. }
  8599. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8600. {
  8601. // noop
  8602. }
  8603. else
  8604. {
  8605. Result =
  8606. FMTLOAD(FILE_INFO_FORMAT,
  8607. (FormatBytes(FileInfo.SelectedSize),
  8608. FormatBytes(FileInfo.FilesSize),
  8609. FormatNumber(FileInfo.SelectedCount),
  8610. FormatNumber(FileInfo.FilesCount)));
  8611. }
  8612. return Result;
  8613. }
  8614. //---------------------------------------------------------------------------
  8615. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8616. TTBXStatusPanel * Panel, TOperationSide Side)
  8617. {
  8618. if (Panel->Index == 1)
  8619. {
  8620. ToggleShowHiddenFiles();
  8621. }
  8622. else if (Panel->Index == 2)
  8623. {
  8624. Filter(Side);
  8625. }
  8626. }
  8627. //---------------------------------------------------------------------------
  8628. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8629. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8630. {
  8631. DebugAssert(!StatusBar->SimplePanel);
  8632. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8633. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8634. }
  8635. //---------------------------------------------------------------------------
  8636. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8637. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8638. {
  8639. DebugAssert(StatusBar->Panels->Count >= 3);
  8640. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8641. if (FileInfo.HiddenCount > 0)
  8642. {
  8643. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8644. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8645. }
  8646. else
  8647. {
  8648. HiddenFilesPanel->ViewPriority = 0;
  8649. // not really necessary, just to cleanup no-longer-valid data
  8650. HiddenFilesPanel->Caption = L"";
  8651. }
  8652. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8653. if (FileInfo.FilteredCount > 0)
  8654. {
  8655. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8656. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8657. }
  8658. else
  8659. {
  8660. FilteredFilesPanel->ViewPriority = 0;
  8661. // not really necessary, just to cleanup no-longer-valid data
  8662. FilteredFilesPanel->Caption = L"";
  8663. }
  8664. }
  8665. //---------------------------------------------------------------------------
  8666. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8667. TObject * /*Sender*/)
  8668. {
  8669. if (DirView(osOther)->Enabled)
  8670. {
  8671. DirView(osOther)->SetFocus();
  8672. }
  8673. }
  8674. //---------------------------------------------------------------------------
  8675. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8676. {
  8677. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8678. switch (Config.Show)
  8679. {
  8680. case qvShow:
  8681. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8682. {
  8683. Config.Show = qvHide;
  8684. }
  8685. else
  8686. {
  8687. Config.Show = Config.LastHideShow;
  8688. }
  8689. break;
  8690. case qvHideWhenEmpty:
  8691. if (ComponentVisible[fcQueueView])
  8692. {
  8693. Config.Show = qvHide;
  8694. }
  8695. else
  8696. {
  8697. Config.LastHideShow = Config.Show;
  8698. Config.Show = qvShow;
  8699. }
  8700. break;
  8701. case qvHide:
  8702. Config.LastHideShow = Config.Show;
  8703. Config.Show = qvShow;
  8704. break;
  8705. }
  8706. WinConfiguration->QueueView = Config;
  8707. }
  8708. //---------------------------------------------------------------------------
  8709. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8710. {
  8711. UnicodeString Result;
  8712. if (Terminal != NULL)
  8713. {
  8714. switch (WinConfiguration->PathInCaption)
  8715. {
  8716. case picShort:
  8717. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8718. break;
  8719. case picFull:
  8720. Result = Terminal->CurrentDirectory;
  8721. break;
  8722. }
  8723. }
  8724. return Result;
  8725. }
  8726. //---------------------------------------------------------------------------
  8727. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8728. {
  8729. TColor Result = GetWindowColor(FSessionColor);
  8730. return Result;
  8731. }
  8732. //---------------------------------------------------------------------------
  8733. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8734. {
  8735. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8736. return Result;
  8737. }
  8738. //---------------------------------------------------------------------------
  8739. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8740. {
  8741. if (!FSessionChanging)
  8742. {
  8743. TTerminalManager::Instance()->UpdateAppTitle();
  8744. }
  8745. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8746. // background to gray on Windows 7 (works on Windows 10).
  8747. // See also EnableControl
  8748. if (Showing)
  8749. {
  8750. if (ManagedSession != NULL)
  8751. {
  8752. // Update path when it changes
  8753. if ((SessionsPageControl->ActivePage != NULL) &&
  8754. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8755. {
  8756. UpdateSessionTab(SessionsPageControl->ActivePage);
  8757. }
  8758. }
  8759. NonVisualDataModule->ReconnectSessionAction->Update();
  8760. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8761. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8762. CenterReconnectToolbar();
  8763. bool HasTerminal = HasActiveTerminal();
  8764. RemoteDirView->Enabled = HasTerminal;
  8765. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8766. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8767. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8768. RemoteDriveView->Color = RemoteDirView->Color;
  8769. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8770. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8771. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8772. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8773. QueueFileList->Enabled = QueueView3->Enabled;
  8774. QueueFileList->Color = QueueView3->Color;
  8775. QueueFileList->Font->Color = QueueView3->Font->Color;
  8776. QueueLabelUpdateStatus();
  8777. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8778. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8779. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8780. {
  8781. UpdateDarkMode();
  8782. }
  8783. // As the hi-contrast theme is currently used for session tabs only, which currently do not support dark mode,
  8784. // we always use dark theme. But ultimatelly, we should use an opposite theme to the main one.
  8785. UnicodeString CurrentHiContrastThemeName = (FHiContrastTheme != NULL) ? FHiContrastTheme->Name : EmptyStr;
  8786. UnicodeString NewHiContrastThemeName = WinConfiguration->HiContrast ? GetThemeName(true) : EmptyStr;
  8787. if (CurrentHiContrastThemeName != NewHiContrastThemeName)
  8788. {
  8789. ReleaseHiContrastTheme();
  8790. if (!NewHiContrastThemeName.IsEmpty())
  8791. {
  8792. FHiContrastTheme = GetTBXTheme(NewHiContrastThemeName);
  8793. }
  8794. SessionsPageControl->ActiveTabTheme = FHiContrastTheme;
  8795. }
  8796. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8797. MessageDock->Visible = (MessageDock->ToolbarCount > 0);
  8798. }
  8799. }
  8800. //---------------------------------------------------------------------------
  8801. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8802. {
  8803. UpdateControls();
  8804. }
  8805. //---------------------------------------------------------------------------
  8806. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8807. TObject * Sender)
  8808. {
  8809. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8810. DebugAssert(DirView != NULL);
  8811. DoDirViewLoaded(DirView);
  8812. TransferPresetAutoSelect();
  8813. }
  8814. //---------------------------------------------------------------------------
  8815. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8816. {
  8817. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8818. // first run after installation
  8819. if (double(Updates.LastCheck) == 0)
  8820. {
  8821. AppLog(L"First run, scheduling updates check for the next run");
  8822. // make sure next time there will be an update (if enabled)
  8823. Updates.LastCheck = TDateTime(1);
  8824. WinConfiguration->Updates = Updates;
  8825. }
  8826. else
  8827. {
  8828. TDateTime Period;
  8829. if (IsUWP() && !IsOfficialPackage())
  8830. {
  8831. Period = DefaultUpdatesPeriod;
  8832. AppLog(L"Thirdparty UWP package, using default updates check period");
  8833. }
  8834. else
  8835. {
  8836. Period = Updates.Period;
  8837. }
  8838. AppLogFmt(L"Updates check period: %.2f", (double(Period)));
  8839. if (double(Period) > 0)
  8840. {
  8841. TDateTime Interval = Now() - Updates.LastCheck;
  8842. AppLogFmt(L"Interval since the last updates check: %.2f", (double(Interval)));
  8843. if (Interval >= Period)
  8844. {
  8845. StartUpdateThread(UpdatesChecked);
  8846. }
  8847. }
  8848. }
  8849. }
  8850. //---------------------------------------------------------------------------
  8851. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8852. {
  8853. AppLog(L"Updates check done");
  8854. UnicodeString Message;
  8855. bool New;
  8856. TQueryType Type;
  8857. GetUpdatesMessage(Message, New, Type, false);
  8858. if (!Message.IsEmpty())
  8859. {
  8860. AppLogFmt(L"Updates check message: %s", (Message));
  8861. if (New)
  8862. {
  8863. AppLog(L"New version detected");
  8864. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8865. }
  8866. if (!New && (Type != qtWarning))
  8867. {
  8868. AppLog(L"Posting non-critical updates note");
  8869. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8870. }
  8871. else
  8872. {
  8873. AppLog(L"Posting new version or critical message notification");
  8874. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8875. }
  8876. if (New)
  8877. {
  8878. Configuration->Usage->Inc(L"UpdateNotifications");
  8879. }
  8880. }
  8881. }
  8882. //---------------------------------------------------------------------------
  8883. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8884. {
  8885. RestoreApp();
  8886. bool CanDisplay = !NonVisualDataModule->Busy;
  8887. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8888. {
  8889. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8890. }
  8891. if (CanDisplay)
  8892. {
  8893. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8894. // Probably not needed as CheckForUpdates is blocking since we started using neon for HTTP implementation.
  8895. NonVisualDataModule->StartBusy();
  8896. try
  8897. {
  8898. CheckForUpdates(true);
  8899. }
  8900. __finally
  8901. {
  8902. NonVisualDataModule->EndBusy();
  8903. }
  8904. }
  8905. else
  8906. {
  8907. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8908. }
  8909. }
  8910. //---------------------------------------------------------------------------
  8911. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8912. TCopyParamRuleData & Data)
  8913. {
  8914. DebugAssert(Terminal != NULL);
  8915. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8916. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8917. Data.RemoteDirectory = RemoteDirView->PathName;
  8918. }
  8919. //---------------------------------------------------------------------------
  8920. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8921. {
  8922. // Terminal can be null when we are changing local directory implicitly,
  8923. // because it has been deleted, while no session is connected
  8924. // (Login dialog is open)
  8925. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8926. {
  8927. DebugAssert(!IsLocalBrowserMode());
  8928. TCopyParamRuleData Data;
  8929. GetTransferPresetAutoSelectData(Data);
  8930. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8931. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8932. if (CopyParamIndex < 0)
  8933. {
  8934. // there is no preset that matches autoselection
  8935. // set preset that we consider "default"
  8936. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8937. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8938. }
  8939. else
  8940. {
  8941. // there is preset matching autoselection
  8942. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8943. if (CopyParamName == FCopyParamAutoSelected)
  8944. {
  8945. // autoselected the same preset as the last time
  8946. // make no change (i.e. preserve custom user preset, if any)
  8947. }
  8948. else
  8949. {
  8950. // autoselected the different preset then the last time (or there
  8951. // was default preset set)
  8952. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8953. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8954. }
  8955. }
  8956. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8957. {
  8958. if (CopyParamIndex >= 0)
  8959. {
  8960. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8961. }
  8962. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8963. try
  8964. {
  8965. int Fmt =
  8966. (CopyParamIndex < 0) ?
  8967. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8968. COPY_PARAM_AUTOSELECTED;
  8969. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8970. Data->Message = MainInstructions(Message);
  8971. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8972. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8973. CopyParamAttrs);
  8974. if (CopyParamIndex >= 0)
  8975. {
  8976. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8977. Info = FORMAT(L"%s\n \n%s", (Info,
  8978. FMTLOAD(COPY_PARAM_RULE,
  8979. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8980. }
  8981. Data->Message += L"\n\n" + Info;
  8982. if (WinConfiguration->CopyParamAutoSelectNotice)
  8983. {
  8984. TransferPresetNoteMessage(Data, true);
  8985. }
  8986. else
  8987. {
  8988. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8989. Data = NULL; // ownership passed
  8990. }
  8991. }
  8992. __finally
  8993. {
  8994. delete Data;
  8995. }
  8996. }
  8997. }
  8998. }
  8999. //---------------------------------------------------------------------------
  9000. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  9001. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  9002. {
  9003. DebugAssert(NoteData != NULL);
  9004. DebugAssert(!IsLocalBrowserMode());
  9005. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  9006. TQueryButtonAlias Aliases[1];
  9007. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  9008. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  9009. Params.Aliases = Aliases;
  9010. Params.AliasesCount = LENOF(Aliases);
  9011. unsigned int Result =
  9012. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  9013. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  9014. switch (Result)
  9015. {
  9016. case qaNeverAskAgain:
  9017. DebugAssert(AllowNeverAskAgain);
  9018. WinConfiguration->CopyParamAutoSelectNotice = false;
  9019. break;
  9020. case qaIgnore:
  9021. PreferencesDialog(pmPresets);
  9022. break;
  9023. }
  9024. }
  9025. //---------------------------------------------------------------------------
  9026. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  9027. {
  9028. // as of now useless, as this is used for notes only, never for balloons, ...
  9029. RestoreApp();
  9030. // .. and we should never be busy here
  9031. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  9032. {
  9033. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  9034. }
  9035. }
  9036. //---------------------------------------------------------------------------
  9037. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  9038. TPreferencesMode APreferencesMode)
  9039. {
  9040. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  9041. TCopyParamRuleData Data;
  9042. if (Terminal != NULL)
  9043. {
  9044. PreferencesData.reset(new TPreferencesDialogData());
  9045. GetTransferPresetAutoSelectData(Data);
  9046. PreferencesData->CopyParamRuleData = &Data;
  9047. }
  9048. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  9049. }
  9050. //---------------------------------------------------------------------------
  9051. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  9052. const TCustomCommandType & Command)
  9053. {
  9054. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  9055. {
  9056. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  9057. }
  9058. }
  9059. //---------------------------------------------------------------------------
  9060. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  9061. {
  9062. bool RemoteAllowed = CustomCommandRemoteAllowed();
  9063. TCustomCommandType Command;
  9064. // make sure we use local custom command when remote are not supported
  9065. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  9066. {
  9067. Command = FLastCustomCommand;
  9068. }
  9069. else
  9070. {
  9071. Command.Params = Command.Params | ccLocal;
  9072. }
  9073. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  9074. FEditingFocusedAdHocCommand = OnFocused;
  9075. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  9076. int Options =
  9077. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  9078. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  9079. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  9080. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  9081. {
  9082. FLastCustomCommand = Command;
  9083. UpdateCustomCommandsToolbar();
  9084. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  9085. }
  9086. }
  9087. //---------------------------------------------------------------------------
  9088. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  9089. {
  9090. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  9091. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  9092. DebugAssert(State > 0);
  9093. if (State <= 0)
  9094. {
  9095. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  9096. }
  9097. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  9098. }
  9099. //---------------------------------------------------------------------------
  9100. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  9101. TCustomCommandType & Command, int & State)
  9102. {
  9103. bool Result = !FLastCustomCommand.Command.IsEmpty();
  9104. if (Result)
  9105. {
  9106. Command = FLastCustomCommand;
  9107. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  9108. }
  9109. return Result;
  9110. }
  9111. //---------------------------------------------------------------------------
  9112. void __fastcall TCustomScpExplorerForm::BeforeAction()
  9113. {
  9114. ResetIncrementalSearch();
  9115. }
  9116. //---------------------------------------------------------------------------
  9117. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  9118. {
  9119. DebugAssert(ComponentVisible[Component]);
  9120. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  9121. }
  9122. //---------------------------------------------------------------------------
  9123. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  9124. {
  9125. // when queue panel is resized here directly, the status bar is stretched
  9126. // over whole space the panel occupied
  9127. PostComponentHide(fcQueueView);
  9128. }
  9129. //---------------------------------------------------------------------------
  9130. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  9131. {
  9132. ComponentVisible[fcQueueFileList] = false;
  9133. }
  9134. //---------------------------------------------------------------------------
  9135. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  9136. {
  9137. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  9138. RefreshColorMode();
  9139. ResetSysDarkTheme();
  9140. ConfigurationChanged();
  9141. ConfigureInterface();
  9142. // Should be called for all controls
  9143. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  9144. }
  9145. //---------------------------------------------------------------------------
  9146. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  9147. {
  9148. // Do not handle, when shutting down anyway (maybe also when not setup completely yet?)
  9149. if (!FInvalid &&
  9150. (Message.LParam != 0) &&
  9151. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  9152. {
  9153. ThemeChanged();
  9154. }
  9155. TForm::Dispatch(&Message);
  9156. }
  9157. //---------------------------------------------------------------------------
  9158. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  9159. {
  9160. TMessage * M = static_cast<TMessage*>(Message);
  9161. switch (M->Msg)
  9162. {
  9163. case CM_DIALOGCHAR:
  9164. CMDialogChar(*M);
  9165. break;
  9166. case WM_APPCOMMAND:
  9167. WMAppCommand(*M);
  9168. break;
  9169. case WM_SYSCOMMAND:
  9170. WMSysCommand(*M);
  9171. break;
  9172. case WM_QUERYENDSESSION:
  9173. WMQueryEndSession(*M);
  9174. break;
  9175. case WM_ENDSESSION:
  9176. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  9177. break;
  9178. #ifdef _DEBUG
  9179. case WM_WINDOWPOSCHANGED:
  9180. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  9181. break;
  9182. case CM_CUSTOMSTYLECHANGED:
  9183. TForm::Dispatch(Message);
  9184. break;
  9185. #endif
  9186. case WM_COMPONENT_HIDE:
  9187. {
  9188. Byte Component = static_cast<Byte>(M->WParam);
  9189. // sanity check
  9190. if (ComponentVisible[Component])
  9191. {
  9192. // special treatment
  9193. if (Component == fcQueueView)
  9194. {
  9195. ToggleQueueVisibility();
  9196. DebugAssert(!ComponentVisible[fcQueueView]);
  9197. }
  9198. else
  9199. {
  9200. ComponentVisible[Component] = false;
  9201. }
  9202. }
  9203. }
  9204. break;
  9205. case WM_COPYDATA:
  9206. WMCopyData(*M);
  9207. break;
  9208. case WM_MANAGES_CAPTION:
  9209. // caption managed in TTerminalManager::UpdateAppTitle()
  9210. M->Result = 1;
  9211. break;
  9212. case WM_WANTS_MOUSEWHEEL:
  9213. M->Result = 1;
  9214. break;
  9215. case CM_SHOWINGCHANGED:
  9216. CMShowingChanged(*M);
  9217. break;
  9218. case WM_CLOSE:
  9219. WMClose(*M);
  9220. break;
  9221. case WM_DPICHANGED:
  9222. WMDpiChanged(*M);
  9223. break;
  9224. case CM_DPICHANGED:
  9225. CMDpiChanged(*M);
  9226. break;
  9227. case WM_SETTINGCHANGE:
  9228. WMSettingChange(*M);
  9229. break;
  9230. case CM_DIALOGKEY:
  9231. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  9232. break;
  9233. case WM_PASTE_FILES:
  9234. PasteFiles();
  9235. break;
  9236. case WM_IS_HIDDEN:
  9237. // To treat master password prompt as a main window while showing it when workspace is loading while
  9238. // the exporer is showing up (Visible is true already)
  9239. M->Result = !FShowing;
  9240. break;
  9241. case WM_SHOWWINDOW:
  9242. if (M->WParam)
  9243. {
  9244. AddStartupSequence(L"J");
  9245. }
  9246. TForm::Dispatch(Message);
  9247. break;
  9248. default:
  9249. TForm::Dispatch(Message);
  9250. break;
  9251. }
  9252. }
  9253. //---------------------------------------------------------------------------
  9254. void __fastcall TCustomScpExplorerForm::UpdateImages()
  9255. {
  9256. // noop
  9257. }
  9258. //---------------------------------------------------------------------------
  9259. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  9260. {
  9261. TForm::Dispatch(&Message);
  9262. FSessionColors->Clear();
  9263. AddFixedSessionImages();
  9264. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  9265. UpdateImages();
  9266. }
  9267. //---------------------------------------------------------------------------
  9268. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  9269. {
  9270. TForm::Dispatch(&Message);
  9271. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  9272. UpdatePixelsPerInchMainWindowCounter();
  9273. }
  9274. //---------------------------------------------------------------------------
  9275. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  9276. {
  9277. AppLog(L"Main window WM_CLOSE");
  9278. // Cannot close window while we are busy.
  9279. // We cannot test this in FormCloseQuery as that is called also from
  9280. // Close(), which is called by CloseApplicationAction. So we can be busy
  9281. // there already even, when it is legitimate to close the application.
  9282. // Possibly a better place to handle this would be WMSysCommand.
  9283. if (NonVisualDataModule->Busy)
  9284. {
  9285. Message.Result = 1;
  9286. }
  9287. else
  9288. {
  9289. TForm::Dispatch(&Message);
  9290. }
  9291. }
  9292. //---------------------------------------------------------------------------
  9293. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  9294. {
  9295. if (!FShowingChanged)
  9296. {
  9297. AddStartupSequence(L"D");
  9298. FShowingChanged = true;
  9299. }
  9300. // Ignoring implicit local-local browser of Commander, opened from DoShow
  9301. bool NoSession = (ManagedSession == NULL);
  9302. // This calls TCustomScpExplorerForm::DoShow
  9303. TForm::Dispatch(&Message);
  9304. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  9305. if (Showing && !FStarted)
  9306. {
  9307. FStarted = true;
  9308. InterfaceStarted();
  9309. }
  9310. if (Showing && NoSession)
  9311. {
  9312. // When we are starting minimized (i.e. from an installer),
  9313. // postpone showing Login dialog until we get restored.
  9314. // Otherwise the Login dialog (and Authentication window) show restored
  9315. // over invisible (minimized) main window.
  9316. if (WindowState == wsMinimized)
  9317. {
  9318. FNeedSession = true;
  9319. }
  9320. else
  9321. {
  9322. // This happens before application ever goes idle, so the toolbars would
  9323. // stay enabled (initial state) until the Login dialog is dismissed.
  9324. UpdateActions();
  9325. NonVisualDataModule->StartBusy();
  9326. try
  9327. {
  9328. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  9329. // otherwise the dialog does not receive focus.
  9330. // With Commander interface the ProcessMessages is called already
  9331. // by TDriveView, but with Explorer interface, we need to call it explicily
  9332. Application->ProcessMessages();
  9333. // do not reload sessions, they have been loaded just now (optimization)
  9334. NeedSession(true);
  9335. }
  9336. __finally
  9337. {
  9338. NonVisualDataModule->EndBusy();
  9339. }
  9340. }
  9341. }
  9342. }
  9343. //---------------------------------------------------------------------------
  9344. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  9345. {
  9346. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  9347. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  9348. }
  9349. //---------------------------------------------------------------------------
  9350. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  9351. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  9352. int & MaxHeight)
  9353. {
  9354. // workaround for bug in TWinControl.CalcConstraints
  9355. // Check for empty rect (restore from iconic state) is done there only after
  9356. // call to AdjustClientRect, which enlarges the rect (for forms).
  9357. TRect R = GetClientRect();
  9358. // when restoring from iconic state, place no restrictions
  9359. if (IsRectEmpty(R))
  9360. {
  9361. MinWidth = 0;
  9362. MinHeight = 0;
  9363. MaxWidth = 32000;
  9364. MaxHeight = 32000;
  9365. }
  9366. }
  9367. //---------------------------------------------------------------------------
  9368. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  9369. TSpaceAvailable & ASpaceAvailable, bool & Close)
  9370. {
  9371. // terminal can be already closed (e.g. dropped connection)
  9372. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  9373. {
  9374. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  9375. try
  9376. {
  9377. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  9378. }
  9379. catch(...)
  9380. {
  9381. if (!Terminal->Active)
  9382. {
  9383. Close = true;
  9384. }
  9385. throw;
  9386. }
  9387. }
  9388. }
  9389. //---------------------------------------------------------------------------
  9390. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  9391. {
  9392. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  9393. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  9394. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  9395. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  9396. {
  9397. OnGetSpaceAvailable = GetSpaceAvailable;
  9398. }
  9399. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  9400. OnGetSpaceAvailable);
  9401. }
  9402. //---------------------------------------------------------------------------
  9403. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  9404. {
  9405. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  9406. Terminal->FillSessionDataForCode(Data.get());
  9407. return Data.release();
  9408. }
  9409. //---------------------------------------------------------------------------
  9410. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  9411. {
  9412. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  9413. DoGenerateUrlDialog(Data.get(), Paths);
  9414. }
  9415. //---------------------------------------------------------------------------
  9416. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  9417. {
  9418. GenerateUrl(NULL);
  9419. }
  9420. //---------------------------------------------------------------------------
  9421. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  9422. {
  9423. std::unique_ptr<TStrings> Paths(new TStringList());
  9424. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  9425. GenerateUrl(Paths.get());
  9426. }
  9427. //---------------------------------------------------------------------------
  9428. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  9429. {
  9430. FSessionColor = value;
  9431. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  9432. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  9433. static_cast<TObject *>(GetComponent(fcColorMenu)));
  9434. DebugAssert(ColorItem != NULL);
  9435. ColorItem->Color = C;
  9436. NonVisualDataModule->ColorMenuItem->Color = C;
  9437. UpdateControls();
  9438. }
  9439. //---------------------------------------------------------------------------
  9440. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  9441. {
  9442. if (value != FSessionColor)
  9443. {
  9444. UpdateSessionColor(value);
  9445. // Is null when called from LastTerminalClosed
  9446. if (Terminal != NULL)
  9447. {
  9448. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  9449. }
  9450. }
  9451. }
  9452. //---------------------------------------------------------------------------
  9453. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  9454. {
  9455. bool Result = FNoteTimer->Enabled;
  9456. if (Result)
  9457. {
  9458. // cannot cancel note too early
  9459. bool NotEarly =
  9460. (Now() - FNoteShown >
  9461. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  9462. if (Force || NotEarly)
  9463. {
  9464. FNoteTimer->Enabled = false;
  9465. FNote = L"";
  9466. SAFE_DESTROY(FNoteData);
  9467. FOnNoteClick = NULL;
  9468. FNoteHints = FNotes->Text;
  9469. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  9470. UpdateStatusBar();
  9471. }
  9472. }
  9473. return Result;
  9474. }
  9475. //---------------------------------------------------------------------------
  9476. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  9477. {
  9478. DebugAssert(FNoteTimer->Enabled);
  9479. CancelNote(true);
  9480. }
  9481. //---------------------------------------------------------------------------
  9482. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9483. {
  9484. FNoteHints = FNotes->Text.TrimRight();
  9485. }
  9486. //---------------------------------------------------------------------------
  9487. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9488. {
  9489. int P = Note.Pos(L"\n");
  9490. if (P > 0)
  9491. {
  9492. Note.SetLength(P - 1);
  9493. }
  9494. FNotes->Add(FORMAT(L"[%s] %s",
  9495. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9496. while (FNotes->Count > 10)
  9497. {
  9498. FNotes->Delete(0);
  9499. }
  9500. if (UpdateNow)
  9501. {
  9502. UpdateNoteHints();
  9503. UpdateStatusBar();
  9504. }
  9505. }
  9506. //---------------------------------------------------------------------------
  9507. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  9508. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9509. {
  9510. int P = Note.Pos(L"\n");
  9511. if (P > 0)
  9512. {
  9513. Note.SetLength(P - 1);
  9514. }
  9515. UpdateNoteHints();
  9516. FNote = Note;
  9517. SAFE_DESTROY(FNoteData);
  9518. FNoteData = NoteData;
  9519. FOnNoteClick = OnNoteClick;
  9520. AddNote(Note, false);
  9521. UpdateStatusBar();
  9522. FNoteShown = Now();
  9523. FNoteTimer->Enabled = false;
  9524. if (Seconds == 0)
  9525. {
  9526. Seconds = WinConfiguration->NotificationsTimeout;
  9527. }
  9528. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9529. FNoteTimer->Enabled = true;
  9530. }
  9531. //---------------------------------------------------------------------------
  9532. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9533. {
  9534. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9535. }
  9536. //---------------------------------------------------------------------------
  9537. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9538. {
  9539. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9540. {
  9541. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9542. }
  9543. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9544. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9545. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9546. 0, NULL, NULL);
  9547. }
  9548. //---------------------------------------------------------------------------
  9549. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9550. {
  9551. bool NewShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9552. UnicodeString Note =
  9553. FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1), (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, (NewShowHiddenFiles ? 2 : 3))));
  9554. PostNote(Note, 0, NULL, NULL);
  9555. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9556. WinConfiguration->ShowHiddenFiles = NewShowHiddenFiles;
  9557. }
  9558. //---------------------------------------------------------------------------
  9559. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9560. {
  9561. WinConfiguration->FormatSizeBytes = Style;
  9562. }
  9563. //---------------------------------------------------------------------------
  9564. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9565. {
  9566. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9567. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9568. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9569. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9570. }
  9571. //---------------------------------------------------------------------------
  9572. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9573. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9574. {
  9575. if (Panel->Index == 0)
  9576. {
  9577. if (FOnNoteClick != NULL)
  9578. {
  9579. // prevent the user data from being freed by possible call
  9580. // to CancelNote or PostNote during call to OnNoteClick
  9581. std::unique_ptr<TObject> NoteData(FNoteData);
  9582. TNotifyEvent OnNoteClick = FOnNoteClick;
  9583. FNoteData = NULL;
  9584. // need to cancel the note as we are going to delete its user data
  9585. CancelNote(true);
  9586. OnNoteClick(NoteData.get());
  9587. }
  9588. }
  9589. if (HasActiveTerminal() &&
  9590. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9591. {
  9592. FileSystemInfo();
  9593. }
  9594. }
  9595. //---------------------------------------------------------------------------
  9596. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9597. {
  9598. // workaround:
  9599. // 1) for unknown reason, disabling window, while minimized,
  9600. // prevents it from restoring, even if it was enabled again meanwhile
  9601. // 2) when disabling the main window, while another has focus
  9602. // minimize is no longer possible
  9603. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9604. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9605. // Shouldn't we use IsApplicationMinimized() here?
  9606. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9607. {
  9608. Enabled = false;
  9609. }
  9610. FLockLevel++;
  9611. }
  9612. //---------------------------------------------------------------------------
  9613. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9614. {
  9615. DebugAssert(FLockLevel > 0);
  9616. FLockLevel--;
  9617. if (FLockLevel == 0)
  9618. {
  9619. DebugAssert(FLockSuspendLevel == 0);
  9620. Enabled = true;
  9621. // VCL_COPY (TCustomForm.SetWindowFocus)
  9622. if (Active && (ActiveControl != NULL))
  9623. {
  9624. ::SetFocus(ActiveControl->Handle);
  9625. if (GetFocus() == ActiveControl->Handle)
  9626. {
  9627. ActiveControl->Perform(CM_UIACTIVATE, 0, 0);
  9628. }
  9629. }
  9630. }
  9631. }
  9632. //---------------------------------------------------------------------------
  9633. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9634. {
  9635. // We need to make sure that window is enabled when the last modal window closes
  9636. // otherwise focus is not restored correctly.
  9637. // So we re-enable the window when modal window opens and
  9638. // disable it back after is closes
  9639. if (FLockLevel > 0)
  9640. {
  9641. // while we have nesting counter, we know that we never be called
  9642. // recursivelly as Application->OnModalBegin is called only
  9643. // for the top-level modal window
  9644. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9645. {
  9646. // won't be disabled when conditions in LockWindow() were not satisfied
  9647. FDisabledOnLockSuspend = !Enabled;
  9648. // When minimized to tray (or actually when set to SW_HIDE),
  9649. // setting Enabled makes the window focusable even when there's
  9650. // modal window over it
  9651. if (!FTrayIcon->Visible)
  9652. {
  9653. Enabled = true;
  9654. }
  9655. }
  9656. FLockSuspendLevel++;
  9657. }
  9658. }
  9659. //---------------------------------------------------------------------------
  9660. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9661. {
  9662. if (FLockSuspendLevel > 0)
  9663. {
  9664. DebugAssert(FLockLevel > 0);
  9665. FLockSuspendLevel--;
  9666. // see comment in SuspendWindowLock
  9667. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9668. {
  9669. // Note that window can be enabled here, when we were minized to tray when
  9670. // was SuspendWindowLock() called.
  9671. // We should possibly do the same check as in LockWindow(),
  9672. // if it is ever possible that the consitions change between
  9673. // SuspendWindowLock() and ResumeWindowLock()
  9674. Enabled = !FDisabledOnLockSuspend;
  9675. }
  9676. }
  9677. }
  9678. //---------------------------------------------------------------------------
  9679. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9680. {
  9681. TTBXComboBoxItem * RemotePathComboBox =
  9682. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9683. if (!TextOnly)
  9684. {
  9685. TStrings * Items = RemotePathComboBox->Strings;
  9686. Items->BeginUpdate();
  9687. try
  9688. {
  9689. Items->Clear();
  9690. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9691. {
  9692. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9693. while (!IsUnixRootPath(APath))
  9694. {
  9695. int P = APath.LastDelimiter(L'/');
  9696. DebugAssert(P >= 0);
  9697. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9698. APath.SetLength(P - 1);
  9699. }
  9700. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9701. }
  9702. }
  9703. __finally
  9704. {
  9705. Items->EndUpdate();
  9706. }
  9707. }
  9708. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9709. // Setting ItemIndex to -1 does not reset its text
  9710. if (RemotePathComboBox->Strings->Count == 0)
  9711. {
  9712. RemotePathComboBox->Text = L"";
  9713. }
  9714. }
  9715. //---------------------------------------------------------------------------
  9716. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9717. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9718. {
  9719. if (AIndex < 0)
  9720. {
  9721. AIndex = Sender->ItemIndex;
  9722. }
  9723. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9724. }
  9725. //---------------------------------------------------------------------------
  9726. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9727. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9728. int & ImageIndex)
  9729. {
  9730. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9731. }
  9732. //---------------------------------------------------------------------------
  9733. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9734. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9735. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9736. {
  9737. if (!IsLocalBrowserMode())
  9738. {
  9739. ARect.Left += (10 * AIndex);
  9740. }
  9741. }
  9742. //---------------------------------------------------------------------------
  9743. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9744. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9745. {
  9746. AWidth += (10 * AIndex);
  9747. }
  9748. //---------------------------------------------------------------------------
  9749. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9750. {
  9751. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9752. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9753. int Index = RemotePathComboBox->ItemIndex;
  9754. while (Index < RemotePathComboBox->Strings->Count - 1)
  9755. {
  9756. APath = UnixExtractFileDir(APath);
  9757. Index++;
  9758. }
  9759. // VanDyke style paths
  9760. if (APath.IsEmpty())
  9761. {
  9762. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9763. APath = ROOTDIRECTORY;
  9764. }
  9765. if (RemoteDirView->Path != APath)
  9766. {
  9767. RemoteDirView->Path = APath;
  9768. }
  9769. }
  9770. //---------------------------------------------------------------------------
  9771. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9772. {
  9773. DoRemotePathComboBoxItemClick(Sender);
  9774. }
  9775. //---------------------------------------------------------------------------
  9776. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9777. {
  9778. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9779. DebugUsedParam(Sender);
  9780. UpdateRemotePathComboBox(true);
  9781. }
  9782. //---------------------------------------------------------------------------
  9783. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9784. {
  9785. DoRemotePathComboBoxCancel(Sender);
  9786. }
  9787. //---------------------------------------------------------------------------
  9788. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9789. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9790. {
  9791. ResetIncrementalSearch();
  9792. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9793. DebugAssert(DirView != NULL);
  9794. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9795. {
  9796. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9797. // OnEditing is called also from TCustomListView::CanEdit
  9798. if (Edit != NULL)
  9799. {
  9800. EditSelectBaseName(Edit);
  9801. }
  9802. }
  9803. }
  9804. //---------------------------------------------------------------------------
  9805. //---------------------------------------------------------------------------
  9806. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9807. {
  9808. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9809. Result->AutoDetectDnD = false;
  9810. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9811. Result->RenderDataOn = rdoEnterAndDropSync;
  9812. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9813. return Result;
  9814. }
  9815. //---------------------------------------------------------------------------
  9816. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9817. {
  9818. if (IsWin10Build(19041))
  9819. {
  9820. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9821. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9822. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9823. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9824. }
  9825. }
  9826. //---------------------------------------------------------------------------
  9827. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9828. {
  9829. TForm::CreateWnd();
  9830. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9831. UpdateDarkMode();
  9832. if (FSessionsDragDropFilesEx == NULL)
  9833. {
  9834. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9835. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9836. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9837. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9838. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9839. }
  9840. if (FQueueDragDropFilesEx == NULL)
  9841. {
  9842. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9843. // No need to set OnDragOver as we do not have any restrictions
  9844. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9845. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9846. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9847. }
  9848. }
  9849. //---------------------------------------------------------------------------
  9850. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9851. {
  9852. TForm::DestroyWnd();
  9853. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9854. FQueueDragDropFilesEx->DragDropControl = NULL;
  9855. }
  9856. //---------------------------------------------------------------------------
  9857. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9858. {
  9859. SideEnter(FCurrentSide);
  9860. FEverShown = true;
  9861. }
  9862. //---------------------------------------------------------------------------
  9863. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9864. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9865. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9866. {
  9867. if (!NonVisualDataModule->Busy)
  9868. {
  9869. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9870. Configuration->Usage->Inc(L"FileFinds");
  9871. LockWindow(true);
  9872. NonVisualDataModule->StartBusy();
  9873. WinConfiguration->LockedInterface = true;
  9874. try
  9875. {
  9876. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9877. }
  9878. __finally
  9879. {
  9880. WinConfiguration->LockedInterface = false;
  9881. NonVisualDataModule->EndBusy();
  9882. UnlockWindow();
  9883. }
  9884. }
  9885. }
  9886. //---------------------------------------------------------------------------
  9887. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9888. {
  9889. if (!NonVisualDataModule->Busy)
  9890. {
  9891. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9892. SetFocus();
  9893. RemoteDirView->Path = UnixExtractFilePath(Path);
  9894. UnicodeString FileName = UnixExtractFileName(Path);
  9895. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9896. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9897. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9898. if (Item == NULL)
  9899. {
  9900. ReloadDirectory(osRemote);
  9901. Item = RemoteDirView->FindFileItem(FileName);
  9902. }
  9903. if (Item != NULL)
  9904. {
  9905. RemoteDirView->ItemFocused = Item;
  9906. RemoteDirView->ItemFocused->MakeVisible(false);
  9907. RemoteDirView->SetFocus();
  9908. }
  9909. }
  9910. }
  9911. //---------------------------------------------------------------------------
  9912. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9913. {
  9914. bool Result = !NonVisualDataModule->Busy;
  9915. if (Result)
  9916. {
  9917. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9918. }
  9919. return Result;
  9920. }
  9921. //---------------------------------------------------------------------------
  9922. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9923. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9924. {
  9925. if (CanOperateOnFoundFiles(ATerminal))
  9926. {
  9927. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9928. FOnFileOperationFinished = OnFileOperationFinished;
  9929. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9930. }
  9931. }
  9932. //---------------------------------------------------------------------------
  9933. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9934. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9935. {
  9936. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9937. }
  9938. //---------------------------------------------------------------------------
  9939. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9940. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9941. {
  9942. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9943. }
  9944. //---------------------------------------------------------------------------
  9945. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9946. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9947. {
  9948. if (CanOperateOnFoundFiles(ATerminal))
  9949. {
  9950. for (int Index = 0; Index < FileList->Count; Index++)
  9951. {
  9952. UnicodeString FileName = FileList->Strings[Index];
  9953. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9954. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9955. OnFileOperationFinished(osRemote, FileName, true);
  9956. }
  9957. }
  9958. }
  9959. //---------------------------------------------------------------------------
  9960. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9961. {
  9962. FFileFindTerminal = Terminal;
  9963. ShowFileFindDialog(
  9964. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9965. DoDownloadFoundFiles, DoEditFoundFiles);
  9966. }
  9967. //---------------------------------------------------------------------------
  9968. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9969. {
  9970. FTaskbarList = TaskbarList;
  9971. }
  9972. //---------------------------------------------------------------------------
  9973. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9974. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9975. {
  9976. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9977. if (Index >= 0)
  9978. {
  9979. if (Button == mbLeft)
  9980. {
  9981. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9982. // If switching tab (switching session) takes long enough for user
  9983. // to actually release the button, "mouse down" is still raised,
  9984. // but we do not get "mouse up" event, so dragging is not cancelled,
  9985. // prevent that by not beginning dragging in the first place.
  9986. int Key = ((GetSystemMetrics(SM_SWAPBUTTON) == 0) ? VK_LBUTTON : VK_RBUTTON);
  9987. if (IsKeyPressed(Key))
  9988. {
  9989. // when user clicks the "+", we get mouse down only after the session
  9990. // is closed, when new session tab is already on X:Y, so dragging
  9991. // starts, prevent that
  9992. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9993. {
  9994. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9995. if (Terminal != NULL)
  9996. {
  9997. SessionsPageControl->BeginDrag(false);
  9998. }
  9999. }
  10000. }
  10001. }
  10002. else if (Button == mbMiddle)
  10003. {
  10004. // ignore middle-click for "New session tab" and the last local-local tab
  10005. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10006. if ((Session != NULL) && CanCloseSession(Session))
  10007. {
  10008. CloseSessionTab(Index);
  10009. }
  10010. }
  10011. }
  10012. }
  10013. //---------------------------------------------------------------------------
  10014. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  10015. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  10016. {
  10017. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  10018. // do not allow dropping on the "+" tab
  10019. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10020. if ((TargetTerminal != NULL) &&
  10021. (SessionsPageControl->ActivePage->PageIndex != Index))
  10022. {
  10023. Configuration->Usage->Inc(L"SessionTabMoves");
  10024. // this is almost redundant as we would recreate tabs in SessionListChanged,
  10025. // but we want to actually prevent that to avoid flicker
  10026. SessionsPageControl->ActivePage->PageIndex = Index;
  10027. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  10028. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  10029. }
  10030. }
  10031. //---------------------------------------------------------------------------
  10032. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  10033. TObject * Sender, TObject * Source, int X, int Y,
  10034. TDragState /*State*/, bool & Accept)
  10035. {
  10036. Accept = (Sender == Source);
  10037. if (Accept)
  10038. {
  10039. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  10040. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10041. // do not allow dragging to the "+" tab
  10042. Accept = (Terminal != NULL);
  10043. }
  10044. }
  10045. //---------------------------------------------------------------------------
  10046. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  10047. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  10048. {
  10049. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  10050. if (Index < 0)
  10051. {
  10052. Effect = DROPEFFECT_NONE;
  10053. }
  10054. else
  10055. {
  10056. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10057. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  10058. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  10059. {
  10060. Effect = DROPEFFECT_NONE;
  10061. }
  10062. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  10063. {
  10064. Effect = DROPEFFECT_COPY;
  10065. }
  10066. }
  10067. }
  10068. //---------------------------------------------------------------------------
  10069. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  10070. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  10071. {
  10072. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  10073. // do not allow dropping on local-local, disconnected and the "+" tab
  10074. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  10075. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10076. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  10077. {
  10078. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  10079. if (!IsFileControl(DropSourceControl, osRemote))
  10080. {
  10081. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  10082. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  10083. // Why don't we use Terminal->CurrentDirectory directly?
  10084. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  10085. // do not force queue, drag drop
  10086. false, true);
  10087. }
  10088. }
  10089. }
  10090. //---------------------------------------------------------------------------
  10091. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  10092. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  10093. {
  10094. // Downloads are handled in RemoteFileControlDDEnd
  10095. if (!IsFileControl(DropSourceControl, osRemote))
  10096. {
  10097. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  10098. Terminal->CurrentDirectory,
  10099. // force queue, drag drop
  10100. true, true);
  10101. }
  10102. }
  10103. //---------------------------------------------------------------------------
  10104. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  10105. {
  10106. FShowing = false;
  10107. // Do not save empty workspace
  10108. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  10109. {
  10110. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  10111. UnicodeString Name = WorkspaceName();
  10112. DoSaveWorkspace(
  10113. Name, DataList.get(),
  10114. !Configuration->DisablePasswordStoring &&
  10115. WinConfiguration->AutoSaveWorkspacePasswords, false);
  10116. WinConfiguration->LastStoredSession = Name;
  10117. }
  10118. AppLog(L"Closing main window");
  10119. }
  10120. //---------------------------------------------------------------------------
  10121. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  10122. {
  10123. TManagedTerminal * ManagedTerminal =
  10124. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  10125. if (ManagedTerminal != NULL)
  10126. {
  10127. ManagedTerminal->DirectoryLoaded = Now();
  10128. }
  10129. }
  10130. //---------------------------------------------------------------------------
  10131. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  10132. TListItem * /*Item*/, bool /*Selected*/)
  10133. {
  10134. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  10135. switch (DirView->LastSelectMethod)
  10136. {
  10137. case smKeyboard:
  10138. Configuration->Usage->Inc(L"KeyboardSelections");
  10139. break;
  10140. case smMouse:
  10141. Configuration->Usage->Inc(L"MouseSelections");
  10142. break;
  10143. }
  10144. }
  10145. //---------------------------------------------------------------------------
  10146. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  10147. {
  10148. if (Color != 0)
  10149. {
  10150. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  10151. }
  10152. else
  10153. {
  10154. return FSessionTabImageIndex;
  10155. }
  10156. }
  10157. //---------------------------------------------------------------------------
  10158. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  10159. {
  10160. TPngImageCollectionItem * Item =
  10161. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  10162. FSessionColors->AddPng(Item->PngImage);
  10163. return FSessionColors->Count - 1;
  10164. }
  10165. //---------------------------------------------------------------------------
  10166. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  10167. {
  10168. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  10169. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  10170. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  10171. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  10172. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  10173. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  10174. }
  10175. //---------------------------------------------------------------------------
  10176. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  10177. {
  10178. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  10179. }
  10180. //---------------------------------------------------------------------------
  10181. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  10182. {
  10183. switch (Side)
  10184. {
  10185. case osLocal:
  10186. return FNewLocalTabTabImageIndex;
  10187. case osRemote:
  10188. case osCurrent:
  10189. return FNewRemoteTabTabImageIndex;
  10190. default:
  10191. DebugFail();
  10192. return -1;
  10193. }
  10194. }
  10195. //---------------------------------------------------------------------------
  10196. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  10197. {
  10198. for (int Index = 0; Index < Control->ControlCount; Index++)
  10199. {
  10200. TControl * AControl = Control->Controls[Index];
  10201. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  10202. if (WinControl != NULL)
  10203. {
  10204. CollectItemsWithTextDisplayMode(WinControl);
  10205. }
  10206. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  10207. if (Toolbar != NULL)
  10208. {
  10209. // we care for top-level items only
  10210. TTBCustomItem * Items = Toolbar->Items;
  10211. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  10212. {
  10213. TTBCustomItem * Item = Items->Items[ItemIndex];
  10214. if (((Item->DisplayMode == nbdmImageAndText) ||
  10215. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  10216. EligibleForImageDisplayMode(Item))
  10217. {
  10218. FItemsWithTextDisplayMode.insert(Item);
  10219. }
  10220. }
  10221. }
  10222. }
  10223. }
  10224. //---------------------------------------------------------------------------
  10225. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  10226. {
  10227. return true;
  10228. }
  10229. //---------------------------------------------------------------------------
  10230. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  10231. {
  10232. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  10233. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  10234. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  10235. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  10236. bool Result = true;
  10237. while (Result && (i != FItemsWithTextDisplayMode.end()))
  10238. {
  10239. TTBCustomItem * Item = *i;
  10240. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  10241. if (Label != NULL)
  10242. {
  10243. // optimization
  10244. if (Label->Visible == SelectiveToolbarText)
  10245. {
  10246. Result = false;
  10247. }
  10248. else
  10249. {
  10250. Label->Visible = SelectiveToolbarText;
  10251. }
  10252. }
  10253. else
  10254. {
  10255. // optimization
  10256. if (Item->DisplayMode == DisplayMode)
  10257. {
  10258. Result = false;
  10259. }
  10260. else
  10261. {
  10262. Item->DisplayMode = DisplayMode;
  10263. }
  10264. }
  10265. ++i;
  10266. }
  10267. UpdateNewTabTab();
  10268. return Result;
  10269. }
  10270. //---------------------------------------------------------------------------
  10271. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  10272. {
  10273. DebugFail();
  10274. }
  10275. //---------------------------------------------------------------------------
  10276. bool __fastcall TCustomScpExplorerForm::IsBusy()
  10277. {
  10278. // Among other, a lock level is non zero, while directory is being loaded,
  10279. // even when it happens as a result of dirview navigation,
  10280. // i.e. when TNonVisualDataModule is NOT FBusy.
  10281. // That's why the TNonVisualDataModule::GetBusy calls this method.
  10282. // Among other this prevents a panel auto update to occur while
  10283. // directory is changing.
  10284. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  10285. }
  10286. //---------------------------------------------------------------------------
  10287. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  10288. {
  10289. // While the window is disabled, we still seem to process menu shortcuts at least,
  10290. // so stop it at least here.
  10291. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  10292. bool Result;
  10293. if (Allowed == aaUpdate)
  10294. {
  10295. Result = true;
  10296. }
  10297. else
  10298. {
  10299. Result = !NonVisualDataModule->Busy;
  10300. if (Result && (Allowed == aaShortCut))
  10301. {
  10302. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  10303. Action->Update();
  10304. Result = Action->Enabled;
  10305. }
  10306. }
  10307. return Result;
  10308. }
  10309. //---------------------------------------------------------------------------
  10310. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  10311. {
  10312. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  10313. }
  10314. //---------------------------------------------------------------------------
  10315. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  10316. {
  10317. // This is somewhat redundant to LockWindow() call from
  10318. // TTerminalManager::TerminalReadDirectoryProgress.
  10319. // But disabling window is known not to block keyboard shorcuts
  10320. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  10321. if (Busy > 0)
  10322. {
  10323. if (NonVisualDataModule->Busy)
  10324. {
  10325. State = false;
  10326. }
  10327. else
  10328. {
  10329. NonVisualDataModule->StartBusy();
  10330. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  10331. LockWindow();
  10332. }
  10333. }
  10334. else if (Busy < 0)
  10335. {
  10336. UnlockWindow();
  10337. NonVisualDataModule->EndBusy();
  10338. }
  10339. else
  10340. {
  10341. State = NonVisualDataModule->Busy;
  10342. }
  10343. }
  10344. //---------------------------------------------------------------------------
  10345. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  10346. {
  10347. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  10348. if (Index >= 0)
  10349. {
  10350. TPopupMenu * PopupMenu = NULL;
  10351. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10352. if (Session != NULL)
  10353. {
  10354. SessionsPageControl->ActivePageIndex = Index;
  10355. if (DebugAlwaysTrue(SessionTabSwitched()))
  10356. {
  10357. // copied from TControl.WMContextMenu
  10358. SendCancelMode(SessionsPageControl);
  10359. // explicit popup instead of using PopupMenu property
  10360. // to avoid menu to popup somewhere within SessionTabSwitched above,
  10361. // while connecting yet not-connected session and hence
  10362. // allowing an access to commands over not-completely connected session
  10363. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  10364. }
  10365. }
  10366. else
  10367. {
  10368. if (SupportsLocalBrowser())
  10369. {
  10370. PopupMenu = NonVisualDataModule->NewTabPopup;
  10371. }
  10372. }
  10373. if (PopupMenu != NULL)
  10374. {
  10375. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  10376. PopupMenu->PopupComponent = SessionsPageControl;
  10377. PopupMenu->Popup(Point.x, Point.y);
  10378. }
  10379. }
  10380. Handled = true;
  10381. }
  10382. //---------------------------------------------------------------------------
  10383. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  10384. {
  10385. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  10386. }
  10387. //---------------------------------------------------------------------------
  10388. void __fastcall TCustomScpExplorerForm::ChangePassword()
  10389. {
  10390. Configuration->Usage->Inc(L"PasswordChanges");
  10391. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  10392. ChangePasswordData->DisableAuthentationsExceptPassword();
  10393. ChangePasswordData->ChangePassword = true;
  10394. std::unique_ptr<TTerminal> ChangePasswordSession(
  10395. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  10396. ChangePasswordSession->AutoReadDirectory = false;
  10397. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  10398. {
  10399. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  10400. }
  10401. else
  10402. {
  10403. Configuration->Usage->Inc(L"PasswordChangesFailed");
  10404. }
  10405. }
  10406. //---------------------------------------------------------------------------
  10407. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  10408. {
  10409. // No nice way to assert SSH2
  10410. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  10411. }
  10412. //---------------------------------------------------------------------------
  10413. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  10414. {
  10415. TOperationVisualizer OperationVisualizer;
  10416. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  10417. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  10418. }
  10419. //---------------------------------------------------------------------------
  10420. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  10421. {
  10422. TForm::ChangeScale(M, D);
  10423. int APixelsPerInch = GetControlPixelsPerInch(this);
  10424. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  10425. try
  10426. {
  10427. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  10428. }
  10429. __finally
  10430. {
  10431. GUIConfiguration->ChangeResourceModule(ResourceModule);
  10432. }
  10433. }
  10434. //---------------------------------------------------------------------------
  10435. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  10436. {
  10437. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  10438. }
  10439. //---------------------------------------------------------------------
  10440. class TPublicControl : public TControl
  10441. {
  10442. friend class TCustomScpExplorerForm;
  10443. };
  10444. //---------------------------------------------------------------------------
  10445. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  10446. {
  10447. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  10448. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  10449. }
  10450. //---------------------------------------------------------------------------
  10451. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  10452. {
  10453. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  10454. OpenBookmark(Side, Bookmark);
  10455. }
  10456. //---------------------------------------------------------------------------
  10457. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  10458. {
  10459. DoBookmarkClick(osLocal, Sender);
  10460. }
  10461. //---------------------------------------------------------------------------
  10462. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  10463. {
  10464. DoBookmarkClick(osRemote, Sender);
  10465. }
  10466. //---------------------------------------------------------------------------
  10467. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  10468. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  10469. {
  10470. if (BookmarkList != NULL)
  10471. {
  10472. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  10473. if (IsLocalBrowserMode())
  10474. {
  10475. Side = osLocal;
  10476. }
  10477. if (!WinConfiguration->UseLocationProfiles)
  10478. {
  10479. std::unique_ptr<TStringList> Directories(new TStringList());
  10480. Directories->CaseSensitive = (Side == osRemote);
  10481. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10482. {
  10483. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10484. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  10485. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  10486. {
  10487. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10488. Item->Caption = EscapeHotkey(Directory);
  10489. Item->ShortCut = Bookmark->ShortCut;
  10490. Item->OnClick = OnBookmarkClick;
  10491. Item->Tag = reinterpret_cast<int>(Bookmark);
  10492. Directories->Add(Directory);
  10493. Menu->Add(Item.release());
  10494. }
  10495. }
  10496. }
  10497. else
  10498. {
  10499. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10500. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10501. {
  10502. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10503. if (!Bookmark->Node.IsEmpty())
  10504. {
  10505. Folders->Add(Bookmark->Node);
  10506. }
  10507. }
  10508. for (int Index = 0; Index < Folders->Count; Index++)
  10509. {
  10510. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10511. Item->Caption = Folders->Strings[Index];
  10512. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10513. Folders->Objects[Index] = Item.get();
  10514. Menu->Add(Item.release());
  10515. }
  10516. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10517. {
  10518. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10519. TTBCustomItem * Parent;
  10520. if (!Bookmark->Node.IsEmpty())
  10521. {
  10522. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10523. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10524. }
  10525. else
  10526. {
  10527. Parent = Menu;
  10528. }
  10529. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10530. Item->Caption = Bookmark->Name;
  10531. Item->ShortCut = Bookmark->ShortCut;
  10532. Item->OnClick = OnBookmarkClick;
  10533. Item->Tag = reinterpret_cast<int>(Bookmark);
  10534. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10535. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10536. {
  10537. // No hint for location profiles that are actually mere "bookmarks"
  10538. }
  10539. else
  10540. {
  10541. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10542. UnicodeString LongHint;
  10543. if (!Bookmark->Local.IsEmpty())
  10544. {
  10545. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10546. }
  10547. if (!Bookmark->Remote.IsEmpty())
  10548. {
  10549. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10550. }
  10551. AddToList(Hint, LongHint, L"|");
  10552. Item->Hint = Hint;
  10553. }
  10554. Parent->Add(Item.release());
  10555. }
  10556. }
  10557. AddMenuSeparator(Menu);
  10558. }
  10559. }
  10560. //---------------------------------------------------------------------------
  10561. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10562. {
  10563. Menu->Clear();
  10564. std::unique_ptr<TTBCustomItem> Item;
  10565. Item.reset(new TTBXItem(Owner));
  10566. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10567. Item->Options = Item->Options << tboDefault;
  10568. Menu->Add(Item.release());
  10569. AddMenuSeparator(Menu);
  10570. if (Terminal != NULL)
  10571. {
  10572. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10573. }
  10574. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10575. Item.reset(new TTBXItem(Owner));
  10576. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10577. Menu->Add(Item.release());
  10578. }
  10579. //---------------------------------------------------------------------------
  10580. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10581. {
  10582. bool Result = true;
  10583. bool Remote = !IsSideLocalBrowser(Side);
  10584. try
  10585. {
  10586. UnicodeString APath = Path;
  10587. if (Remote)
  10588. {
  10589. Terminal->ExceptionOnFail = true;
  10590. }
  10591. else
  10592. {
  10593. APath = ExpandEnvironmentVariables(Path);
  10594. }
  10595. try
  10596. {
  10597. DirView(Side)->Path = APath;
  10598. }
  10599. __finally
  10600. {
  10601. if (Remote)
  10602. {
  10603. Terminal->ExceptionOnFail = false;
  10604. }
  10605. }
  10606. }
  10607. catch (Exception & E)
  10608. {
  10609. if (!Remote || Terminal->Active)
  10610. {
  10611. ShowExtendedExceptionEx(Terminal, &E);
  10612. Result = false;
  10613. }
  10614. else
  10615. {
  10616. throw;
  10617. }
  10618. }
  10619. return Result;
  10620. }
  10621. //---------------------------------------------------------------------------
  10622. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10623. {
  10624. // Make sure there some feedback even when loading tiny local folders
  10625. TInstantOperationVisualizer Visualizer;
  10626. DirView(Side)->ReloadDirectory();
  10627. }
  10628. //---------------------------------------------------------------------------
  10629. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10630. {
  10631. if (Index == SessionsPageControl->TabIndex)
  10632. {
  10633. CloseTab();
  10634. }
  10635. else
  10636. {
  10637. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10638. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10639. TTerminalManager * Manager = TTerminalManager::Instance();
  10640. if (!Terminal->Active)
  10641. {
  10642. Manager->FreeTerminal(Terminal);
  10643. }
  10644. else
  10645. {
  10646. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10647. if (CanCloseQueue(Queue))
  10648. {
  10649. Manager->FreeTerminal(Terminal);
  10650. }
  10651. }
  10652. }
  10653. }
  10654. //---------------------------------------------------------------------------
  10655. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10656. {
  10657. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10658. if (Session == NULL)
  10659. {
  10660. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10661. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10662. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10663. //P = SessionsPageControl->ClientToScreen(P);
  10664. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10665. }
  10666. else
  10667. {
  10668. CloseSessionTab(Index);
  10669. }
  10670. }
  10671. //---------------------------------------------------------------------------
  10672. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10673. {
  10674. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10675. {
  10676. TInstantOperationVisualizer Visualizer;
  10677. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10678. // before we create a new data object
  10679. ClipboardClear();
  10680. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10681. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10682. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10683. // Intermediate variable to WORKAROUND a crash in compiler
  10684. TObjectList * ClipboardFakeMonitors =
  10685. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10686. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10687. if (FClipboardDragDropFilesEx.get() == NULL)
  10688. {
  10689. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10690. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10691. }
  10692. FClipboardDragDropFilesEx->FileList->Clear();
  10693. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10694. FClipboardDragDropFilesEx->CopyToClipboard();
  10695. FClipboardTerminal = Terminal;
  10696. // Need full paths, as cwd can be different once files are pasted
  10697. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10698. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10699. {
  10700. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10701. }
  10702. }
  10703. }
  10704. //---------------------------------------------------------------------------
  10705. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10706. {
  10707. if (OpenClipboard(0))
  10708. {
  10709. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10710. CloseClipboard();
  10711. }
  10712. }
  10713. //---------------------------------------------------------------------------
  10714. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10715. {
  10716. return !FClipboardFakeDirectory.IsEmpty();
  10717. }
  10718. //---------------------------------------------------------------------------
  10719. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10720. {
  10721. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10722. FClipboardFakeDirectory = UnicodeString();
  10723. FClipboardTerminal = NULL;
  10724. if (FDownloadingFromClipboard)
  10725. {
  10726. // We are called by the monitor, so attempt to release it would deadlock
  10727. FClipboardFakeMonitorsPendingReset = true;
  10728. }
  10729. else
  10730. {
  10731. FClipboardFakeMonitors.reset(NULL);
  10732. }
  10733. }
  10734. //---------------------------------------------------------------------------
  10735. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10736. {
  10737. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10738. {
  10739. ClipboardStop();
  10740. }
  10741. }
  10742. //---------------------------------------------------------------------------
  10743. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10744. {
  10745. TTerminalManager * Manager = TTerminalManager::Instance();
  10746. Manager->ActiveSession = FClipboardTerminal;
  10747. TTransferOperationParam Params;
  10748. Params.TargetDirectory = TargetDirectory;
  10749. Params.DragDrop = DragDrop;
  10750. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10751. }
  10752. //---------------------------------------------------------------------------
  10753. void TCustomScpExplorerForm::PasteFiles()
  10754. {
  10755. // Guard against possible race conditions
  10756. if (!FClipboardPasteTarget.IsEmpty())
  10757. {
  10758. // ClipboardFakeCreated might be called while we are here, so make sure we finish operating on the same path we started with
  10759. UnicodeString Target = FClipboardPasteTarget;
  10760. FClipboardPasteTarget = EmptyStr;
  10761. // Can fail as it can be e.g. locked by AV, so we retry that later
  10762. bool Removed = RemoveDir(ApiPath(Target));
  10763. try
  10764. {
  10765. if (!NonVisualDataModule->Busy)
  10766. {
  10767. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10768. FClipboardFakeMonitorsPendingReset = false;
  10769. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10770. TAutoFlag Flag(FDownloadingFromClipboard);
  10771. ClipboardDownload(ExtractFilePath(Target), NoConfirmation, true);
  10772. }
  10773. }
  10774. __finally
  10775. {
  10776. if (!Removed)
  10777. {
  10778. RemoveDir(ApiPath(Target));
  10779. }
  10780. }
  10781. }
  10782. }
  10783. //---------------------------------------------------------------------------
  10784. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10785. {
  10786. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10787. // (as it pumps a Windows message queue while being shutted down)
  10788. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10789. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10790. {
  10791. AppLogFmt(L"Fake clipboard directory pasted to \"%s\"", (FileName));
  10792. FClipboardPasteTarget = FileName;
  10793. // Particularly when pasting to external HDD, the fake directory is strangely recreated if removed here in the monitor callback.
  10794. // Postponing removing (or actually whole pasting operation) seems to help.
  10795. PostMessage(Handle, WM_PASTE_FILES, 0, 0);
  10796. }
  10797. }
  10798. //---------------------------------------------------------------------------
  10799. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10800. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10801. {
  10802. TFileMasks::TParams MaskParams;
  10803. MaskParams.Size = Size;
  10804. MaskParams.Modification = Modification;
  10805. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10806. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10807. // TUnixDirView::ItemFullFileName returns trailing backslash for directories, what filemask cannot deal with
  10808. if (!Local && Directory)
  10809. {
  10810. FileName = UnixExcludeTrailingBackslash(FileName);
  10811. }
  10812. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10813. {
  10814. bool ImplicitMatch;
  10815. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10816. !ImplicitMatch)
  10817. {
  10818. Color = Iter->Color;
  10819. break;
  10820. }
  10821. }
  10822. }
  10823. //---------------------------------------------------------------------------
  10824. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10825. {
  10826. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10827. if (!DirView->IsEditing())
  10828. {
  10829. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10830. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10831. // while searching
  10832. if (Key == VK_BACK)
  10833. {
  10834. if (!FIncrementalSearch.IsEmpty())
  10835. {
  10836. if (FIncrementalSearch.Length() == 1)
  10837. {
  10838. ResetIncrementalSearch();
  10839. }
  10840. else
  10841. {
  10842. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10843. IncrementalSearch(NewText, false, false);
  10844. }
  10845. Key = 0;
  10846. }
  10847. }
  10848. }
  10849. }
  10850. //---------------------------------------------------------------------------
  10851. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10852. {
  10853. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10854. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10855. {
  10856. // Filter control sequences.
  10857. // When not searching yet, prefer use of the space for toggling file selection
  10858. // (so we cannot incrementally search for a string starting with a space).
  10859. if ((Key > VK_SPACE) ||
  10860. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10861. {
  10862. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10863. Key = 0;
  10864. }
  10865. }
  10866. }
  10867. //---------------------------------------------------------------------------
  10868. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10869. {
  10870. if (!FIncrementalSearch.IsEmpty())
  10871. {
  10872. FIncrementalSearch = L"";
  10873. DirView(osCurrent)->UpdateStatusBar();
  10874. }
  10875. }
  10876. //---------------------------------------------------------------------------
  10877. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10878. {
  10879. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10880. if (Item == NULL)
  10881. {
  10882. MessageBeep(MB_ICONHAND);
  10883. }
  10884. else
  10885. {
  10886. TCustomDirView * ADirView = DirView(osCurrent);
  10887. {
  10888. TAutoNestingCounter Guard(FIncrementalSearching);
  10889. ADirView->FocusItem(Item);
  10890. if (ADirView->NortonLike == nlOff)
  10891. {
  10892. if ((ADirView->Selected != Item) ||
  10893. (ADirView->SelCount > 1))
  10894. {
  10895. ADirView->ClearSelection();
  10896. ADirView->Selected = Item;
  10897. }
  10898. }
  10899. }
  10900. FIncrementalSearch = Text;
  10901. Item->MakeVisible(false);
  10902. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10903. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10904. ADirView->UpdateStatusBar();
  10905. }
  10906. }
  10907. //---------------------------------------------------------------------------
  10908. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10909. {
  10910. int Index = Item->Index;
  10911. if (!Reverse)
  10912. {
  10913. Index++;
  10914. if (Index >= Item->Owner->Count)
  10915. {
  10916. Index = 0;
  10917. }
  10918. }
  10919. else
  10920. {
  10921. Index--;
  10922. if (Index < 0)
  10923. {
  10924. Index = Item->Owner->Count - 1;
  10925. }
  10926. }
  10927. return Item->Owner->Item[Index];
  10928. }
  10929. //---------------------------------------------------------------------------
  10930. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10931. {
  10932. TCustomDirView * ADirView = DirView(osCurrent);
  10933. if (ADirView->Items->Count == 0)
  10934. {
  10935. return NULL;
  10936. }
  10937. else
  10938. {
  10939. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10940. TListItem * Item = CurrentItem;
  10941. if (SkipCurrent)
  10942. {
  10943. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10944. }
  10945. while (true)
  10946. {
  10947. bool Matches = false;
  10948. switch (WinConfiguration->PanelSearch)
  10949. {
  10950. case isNameStartOnly:
  10951. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item).SubString(1, Text.Length()), Text);
  10952. break;
  10953. case isName:
  10954. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item), Text);
  10955. break;
  10956. case isAll:
  10957. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10958. int Index = 0;
  10959. while ((Index < ColCount) && !Matches)
  10960. {
  10961. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10962. if (Visible)
  10963. {
  10964. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10965. }
  10966. Index++;
  10967. }
  10968. break;
  10969. }
  10970. if (Matches)
  10971. {
  10972. return Item;
  10973. }
  10974. Item = GetNextFile(Item, Reverse);
  10975. if (Item == CurrentItem)
  10976. {
  10977. return NULL;
  10978. }
  10979. }
  10980. }
  10981. }
  10982. //---------------------------------------------------------------------------
  10983. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10984. {
  10985. ResetIncrementalSearch();
  10986. }
  10987. //---------------------------------------------------------------------------
  10988. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10989. {
  10990. if (Message.CharCode == VK_TAB)
  10991. {
  10992. if (!FIncrementalSearch.IsEmpty())
  10993. {
  10994. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10995. bool Reverse = Shift.Contains(ssShift);
  10996. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10997. Message.Result = 1;
  10998. return;
  10999. }
  11000. }
  11001. else if (Message.CharCode == VK_ESCAPE)
  11002. {
  11003. if (!FIncrementalSearch.IsEmpty())
  11004. {
  11005. ResetIncrementalSearch();
  11006. Message.Result = 1;
  11007. return;
  11008. }
  11009. }
  11010. TForm::Dispatch(&Message);
  11011. }
  11012. //---------------------------------------------------------------------------
  11013. void __fastcall TCustomScpExplorerForm::Deactivate()
  11014. {
  11015. TForm::Deactivate();
  11016. // E.g. when switching to an internal editor window
  11017. ResetIncrementalSearch();
  11018. }
  11019. //---------------------------------------------------------------------------
  11020. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  11021. {
  11022. // When switching to another application
  11023. ResetIncrementalSearch();
  11024. }
  11025. //---------------------------------------------------------------------------
  11026. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  11027. {
  11028. ResetIncrementalSearch();
  11029. }
  11030. //---------------------------------------------------------------------------
  11031. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  11032. {
  11033. if (FIncrementalSearching <= 0)
  11034. {
  11035. ResetIncrementalSearch();
  11036. }
  11037. }
  11038. //---------------------------------------------------------------------------
  11039. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  11040. {
  11041. CountClicksForWindowPrint(this);
  11042. }
  11043. //---------------------------------------------------------------------------
  11044. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  11045. {
  11046. CenterReconnectToolbar();
  11047. }
  11048. //---------------------------------------------------------------------------
  11049. void TCustomScpExplorerForm::DoBrowseFile(TCustomDirView * DirView, const UnicodeString & FileName)
  11050. {
  11051. if (!FileName.IsEmpty())
  11052. {
  11053. DirView->RestoreFocus(FileName);
  11054. if (DirView->ItemFocused != NULL)
  11055. {
  11056. DirView->ItemFocused->Selected = true;
  11057. }
  11058. }
  11059. }
  11060. //---------------------------------------------------------------------------
  11061. void __fastcall TCustomScpExplorerForm::BrowseFile(const UnicodeString & FileName)
  11062. {
  11063. DoBrowseFile(RemoteDirView, FileName);
  11064. }
  11065. //---------------------------------------------------------------------------
  11066. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  11067. {
  11068. if (!FQueueStatusUpdating)
  11069. {
  11070. bool Refresh;
  11071. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  11072. {
  11073. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  11074. }
  11075. else
  11076. {
  11077. if (FQueueFileList->GetCount() > 0)
  11078. {
  11079. FQueueFileList->Clear();
  11080. Refresh = true;
  11081. }
  11082. }
  11083. int Count = FQueueFileList->GetCount();
  11084. if (QueueFileList->Items->Count != Count)
  11085. {
  11086. DebugAssert(Refresh);
  11087. QueueFileList->Items->Count = Count;
  11088. }
  11089. if (Refresh)
  11090. {
  11091. QueueFileList->Invalidate();
  11092. }
  11093. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  11094. }
  11095. }
  11096. //---------------------------------------------------------------------------
  11097. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  11098. {
  11099. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  11100. }
  11101. //---------------------------------------------------------------------------
  11102. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  11103. {
  11104. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  11105. // See TSynchronizeChecklistDialog::UpdateTimer
  11106. if (QueueFileList->Items->Count > 0)
  11107. {
  11108. QueueFileList->Items->Item[0]->MakeVisible(false);
  11109. }
  11110. UpdateQueueFileList();
  11111. }
  11112. //---------------------------------------------------------------------------
  11113. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  11114. {
  11115. Active = QueueView3->Focused() || QueueFileList->Focused();
  11116. }
  11117. //---------------------------------------------------------------------------
  11118. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  11119. {
  11120. QueueLabelUpdateStatus();
  11121. }
  11122. //---------------------------------------------------------------------------
  11123. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  11124. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  11125. {
  11126. int State = FQueueFileList->GetState(Item->Index);
  11127. if (State == qfsProcessed)
  11128. {
  11129. Sender->Canvas->Font->Color = clGrayText;
  11130. }
  11131. }
  11132. //---------------------------------------------------------------------------
  11133. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  11134. {
  11135. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  11136. QueueFileList->Column[0]->Width =
  11137. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  11138. }
  11139. //---------------------------------------------------------------------------
  11140. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  11141. {
  11142. QueueFileListColumnAutoSize();
  11143. }
  11144. //---------------------------------------------------------------------------
  11145. void __fastcall TCustomScpExplorerForm::CloseApp()
  11146. {
  11147. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  11148. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  11149. // So we clear the flag here.
  11150. DebugAssert(NonVisualDataModule->Busy);
  11151. NonVisualDataModule->EndBusy();
  11152. try
  11153. {
  11154. Close();
  11155. }
  11156. __finally
  11157. {
  11158. NonVisualDataModule->StartBusy();
  11159. }
  11160. }
  11161. //---------------------------------------------------------------------------
  11162. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  11163. {
  11164. return (Terminal != NULL) && Terminal->Active;
  11165. }
  11166. //---------------------------------------------------------------------------
  11167. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  11168. {
  11169. return (ManagedSession != NULL);
  11170. }
  11171. //---------------------------------------------------------------------------
  11172. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  11173. {
  11174. return IsActiveTerminal(Terminal);
  11175. }
  11176. //---------------------------------------------------------------------------
  11177. void TCustomScpExplorerForm::LocalLocalCopy(
  11178. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  11179. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  11180. {
  11181. DebugFail();
  11182. }
  11183. //---------------------------------------------------------------------------
  11184. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  11185. {
  11186. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  11187. }
  11188. //---------------------------------------------------------------------------
  11189. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  11190. {
  11191. // 1) Changing tab list triggers the OnResize.
  11192. // 2) Is called in TForm constructor, when we are not ready to call SessionListChanged (particularly when starting with colored session)
  11193. if (!FUpdatingSessionTabs && (FSessionColors != NULL))
  11194. {
  11195. // UpdateTabsCaptionTruncation should be enough
  11196. SessionListChanged(true);
  11197. }
  11198. }
  11199. //---------------------------------------------------------------------------
  11200. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  11201. {
  11202. if (ASession == ManagedSession)
  11203. {
  11204. UpdateSession(ASession);
  11205. }
  11206. TSessionData * Data = ASession->StateData;
  11207. UnicodeString Result;
  11208. if (Side == osLocal)
  11209. {
  11210. Result = Data->LocalDirectory;
  11211. }
  11212. else if (DebugAlwaysTrue(Side == osOther))
  11213. {
  11214. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  11215. }
  11216. return Result;
  11217. }
  11218. //---------------------------------------------------------------------------
  11219. UnicodeString TCustomScpExplorerForm::GetTabHintSessionDetails(TManagedTerminal * ASession)
  11220. {
  11221. UnicodeString Title = TTerminalManager::Instance()->GetSessionTitle(ASession, false);
  11222. UnicodeString Result = ASession->SessionData->DefaultSessionName;
  11223. if (Title == Result)
  11224. {
  11225. Result = EmptyStr;
  11226. }
  11227. else
  11228. {
  11229. Result += L"\n";
  11230. }
  11231. return Result;
  11232. }
  11233. //---------------------------------------------------------------------------
  11234. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  11235. {
  11236. UnicodeString Result = GetTabHintSessionDetails(ASession);
  11237. if (!ASession->Active)
  11238. {
  11239. Result += LoadStr(STATUS_NOT_CONNECTED2);
  11240. }
  11241. else
  11242. {
  11243. Result += GetSessionPath(ASession, osRemote);
  11244. }
  11245. return Result;
  11246. }
  11247. //---------------------------------------------------------------------------
  11248. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  11249. {
  11250. return LoadStr(NEW_REMOTE_TAB_HINT);
  11251. }
  11252. //---------------------------------------------------------------------------
  11253. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  11254. {
  11255. if (Index >= 0)
  11256. {
  11257. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  11258. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  11259. if (TabSession == NULL)
  11260. {
  11261. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  11262. if (NewTabTabCaption != TabSheet->Caption)
  11263. {
  11264. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  11265. }
  11266. }
  11267. else
  11268. {
  11269. TTerminalManager * Manager = TTerminalManager::Instance();
  11270. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  11271. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  11272. }
  11273. }
  11274. }
  11275. //---------------------------------------------------------------------------
  11276. typedef std::vector<TListItem *> TListItemsVector;
  11277. struct TCalculateSizeOperation
  11278. {
  11279. TCalculateSizeStats Stats;
  11280. TListItemsVector ListItems;
  11281. size_t Index;
  11282. };
  11283. //---------------------------------------------------------------------------
  11284. void TCustomScpExplorerForm::CalculateDirectorySizes(TOperationSide Side)
  11285. {
  11286. TCalculateSizeOperation CalculateSizeOperation;
  11287. Side = GetSide(Side);
  11288. TCustomDirView * ADirView = DirView(Side);
  11289. bool LocalSide = IsSideLocalBrowser(Side);
  11290. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, LocalSide, NULL, true));
  11291. std::unique_ptr<TStrings> FileList(new TStringList());
  11292. for (int Index = 0; Index < AllFileList->Count; Index++)
  11293. {
  11294. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  11295. if (ADirView->ItemIsDirectory(Item))
  11296. {
  11297. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  11298. CalculateSizeOperation.ListItems.push_back(Item);
  11299. }
  11300. }
  11301. CalculateSizeOperation.Index = 0;
  11302. __int64 Size = 0;
  11303. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation);
  11304. FCalculateSizeOperation = &CalculateSizeOperation;
  11305. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  11306. FOnFileOperationFinished = DirectorySizeCalculated;
  11307. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation);
  11308. FPrimaryOperation = foCalculateSize;
  11309. TCalculatedSizes CalculatedSizes;
  11310. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  11311. if (LocalSide)
  11312. {
  11313. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  11314. FProgressSide = Side;
  11315. ManagedSession->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  11316. }
  11317. else
  11318. {
  11319. TCalculateSizeParams Params;
  11320. Params.Stats = &CalculateSizeOperation.Stats;
  11321. Params.UseCache = true;
  11322. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  11323. }
  11324. }
  11325. //---------------------------------------------------------------------------
  11326. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(TOperationSide Side, const UnicodeString & FileName, bool Success)
  11327. {
  11328. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  11329. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  11330. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  11331. // optimization
  11332. bool GotExpectedItem =
  11333. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  11334. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  11335. int Index = -1;
  11336. if (DebugAlwaysTrue(GotExpectedItem))
  11337. {
  11338. Index = FCalculateSizeOperation->Index;
  11339. FCalculateSizeOperation->Index++;
  11340. }
  11341. else
  11342. {
  11343. TListItemsVector::iterator I =
  11344. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  11345. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  11346. {
  11347. Index = I - FCalculateSizeOperation->ListItems.begin();
  11348. }
  11349. }
  11350. if (DebugAlwaysTrue(Index >= 0) && Success)
  11351. {
  11352. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  11353. ADirView->SetItemCalculatedSize(Item, Size);
  11354. }
  11355. }
  11356. //---------------------------------------------------------------------------
  11357. void __fastcall TCustomScpExplorerForm::MessageDockRequestDock(TObject *, TTBCustomDockableWindow *, bool & Accept)
  11358. {
  11359. Accept = false;
  11360. }
  11361. //---------------------------------------------------------------------------
  11362. void TCustomScpExplorerForm::AutoSizeColumns(TOperationSide Side)
  11363. {
  11364. // Send Ctrl+Gray+ to the list view
  11365. TKeyboardState KeyState;
  11366. GetKeyboardState(KeyState);
  11367. TKeyboardState NewKeyState;
  11368. memset(NewKeyState, 0, sizeof(NewKeyState));
  11369. NewKeyState[VK_CONTROL] = 0x81;
  11370. SetKeyboardState(NewKeyState);
  11371. SendMessage(DirView(Side)->Handle, WM_KEYDOWN, VK_ADD, 0);
  11372. SetKeyboardState(KeyState);
  11373. }
  11374. //---------------------------------------------------------------------------
  11375. void TCustomScpExplorerForm::QueueResetLayoutColumns()
  11376. {
  11377. LoadListViewStr(QueueView3, QueueViewLayoutDefault);
  11378. }
  11379. //---------------------------------------------------------------------------
  11380. void * TCustomScpExplorerForm::SaveFocus()
  11381. {
  11382. return NULL;
  11383. }
  11384. //---------------------------------------------------------------------------
  11385. void TCustomScpExplorerForm::RestoreFocus(void * Focus)
  11386. {
  11387. DebugUsedParam(Focus);
  11388. DebugAssert(Focus == NULL);
  11389. }