CustomScpExplorer.cpp 398 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959
  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. FEditorManager = new TEditorManager();
  210. FEditorManager->OnFileChange = ExecutedFileChanged;
  211. FEditorManager->OnFileReload = ExecutedFileReload;
  212. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  213. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  214. FLocalEditors = new TList();
  215. FQueueStatus = NULL;
  216. FQueueStatusSection = new TCriticalSection();
  217. FQueueStatusInvalidated = false;
  218. FQueueItemInvalidated = false;
  219. FQueueStatusUpdating = false;
  220. FQueueActedItem = NULL;
  221. FQueueController = new TQueueController(QueueView3);
  222. FQueueScrollOnDragOver = new TListViewScrollOnDragOver(QueueView3, true);
  223. FUserActionTimer = new TTimer(this);
  224. FUserActionTimer->Enabled = false;
  225. FUserActionTimer->Interval = 10;
  226. FUserActionTimer->OnTimer = UserActionTimer;
  227. FNotes = new TStringList();
  228. FNoteTimer = new TTimer(this);
  229. FNoteTimer->Enabled = false;
  230. FNoteTimer->OnTimer = NoteTimer;
  231. FOnNoteClick = NULL;
  232. FOle32Library = LoadLibrary(L"Ole32.dll");
  233. FDragMoveCursor = FOle32Library != NULL ?
  234. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  235. UseSystemSettings(this);
  236. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  237. static_cast<TObject*>(GetComponent(fcTransferList)));
  238. DebugAssert(TransferList != NULL);
  239. FTransferListHoverIndex = -1;
  240. TransferList->OnChange = TransferListChange;
  241. TransferList->OnDrawItem = TransferListDrawItem;
  242. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  243. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  244. UseDesktopFont(SessionsPageControl);
  245. UpdateSessionsPageControlHeight();
  246. UseDesktopFont(RemoteDirView);
  247. UseDesktopFont(RemoteDriveView);
  248. UseDesktopFont(QueueView3);
  249. UseDesktopFont(QueueFileList);
  250. UseDesktopFont(QueueLabel);
  251. UseDesktopFont(RemoteStatusBar);
  252. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  253. QueueSplitter->ShowHint = true;
  254. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  255. QueueFileListSplitter->ShowHint = true;
  256. RemotePanelSplitter->ShowHint = true;
  257. UpdateImages();
  258. FCustomCommandMenu = CreateTBXPopupMenu(this);
  259. FCustomCommandLocalFileList = NULL;
  260. FCustomCommandRemoteFileList = NULL;
  261. FSessionColors = new TPngImageList(this);
  262. FSessionColors->ColorDepth = cd32Bit;
  263. AddFixedSessionImages();
  264. SessionsPageControl->Images = FSessionColors;
  265. UpdateQueueLabel();
  266. CheckStoreTransition();
  267. CreateHiddenWindow();
  268. StartUpdates();
  269. // Start loading drive data, so that they are ready once we need them for StartCreationDirectoryMonitorsOnEachDrive
  270. // (necessary for "explorer" only, as "commander" loads it for its drive menu)
  271. DriveInfo->NeedData();
  272. }
  273. //---------------------------------------------------------------------------
  274. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  275. {
  276. FInvalid = true;
  277. if (FClipboardTerminal != NULL)
  278. {
  279. ClipboardClear(); // implies ClipboardStop
  280. }
  281. // this has to be one of the very first things to do
  282. StopUpdateThread();
  283. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  284. TTerminalManager::Instance()->ScpExplorer = NULL;
  285. delete FCustomCommandLocalFileList;
  286. delete FCustomCommandRemoteFileList;
  287. delete FCustomCommandMenu;
  288. delete FTrayIcon;
  289. FTrayIcon = NULL;
  290. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  291. delete FEditorManager;
  292. ForceCloseLocalEditors();
  293. delete FLocalEditors;
  294. HideFileFindDialog();
  295. if (FDelayedDeletionTimer)
  296. {
  297. DoDelayedDeletion(NULL);
  298. SAFE_DESTROY(FDelayedDeletionTimer);
  299. }
  300. SAFE_DESTROY(FDelayedDeletionList);
  301. // sometimes we do not get DDEnd so the list is not released
  302. SAFE_DESTROY(FDDFileList);
  303. DebugAssert(FSynchronizeController == NULL);
  304. CloseHandle(FDDExtMutex);
  305. FDDExtMutex = NULL;
  306. FreeLibrary(FOle32Library);
  307. FOle32Library = NULL;
  308. FDragMoveCursor = NULL;
  309. DebugAssert(!FErrorList);
  310. if (FEverShown)
  311. {
  312. // when window is never shown (like when running command-line operation),
  313. // particularly window site is not restored correctly (BoundsRect value set
  314. // in RestoreForm gets lost during handle allocation), so we do not want
  315. // it to be stored
  316. StoreParams();
  317. }
  318. ManagedSession = NULL;
  319. Queue = NULL;
  320. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  321. NonVisualDataModule->ScpExplorer = NULL;
  322. delete FQueueScrollOnDragOver;
  323. FQueueScrollOnDragOver = NULL;
  324. delete FQueueController;
  325. FQueueController = NULL;
  326. delete FQueueStatusSection;
  327. FQueueStatusSection = NULL;
  328. delete FQueueStatus;
  329. FQueueStatus = NULL;
  330. delete FUserActionTimer;
  331. FUserActionTimer = NULL;
  332. delete FNoteTimer;
  333. delete FNotes;
  334. delete FNoteData;
  335. SAFE_DESTROY(FDocks);
  336. SessionsPageControl->Images = NULL;
  337. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  338. SAFE_DESTROY(FSessionColors);
  339. SAFE_DESTROY(FSessionsDragDropFilesEx);
  340. SAFE_DESTROY(FHistoryMenu[0][0]);
  341. SAFE_DESTROY(FHistoryMenu[0][1]);
  342. SAFE_DESTROY(FHistoryMenu[1][0]);
  343. SAFE_DESTROY(FHistoryMenu[1][1]);
  344. if (FHiddenWindow != NULL)
  345. {
  346. DestroyWindow(FHiddenWindow);
  347. FHiddenWindow = NULL;
  348. }
  349. }
  350. //---------------------------------------------------------------------------
  351. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  352. {
  353. std::unique_ptr<TSessionData> Data;
  354. if (!Session.IsEmpty())
  355. {
  356. bool DefaultsOnly;
  357. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  358. }
  359. TTerminalManager * Manager = TTerminalManager::Instance();
  360. for (int Index = 0; Index < Manager->Count; Index++)
  361. {
  362. TTerminal * Terminal = Manager->Sessions[Index];
  363. if (Session.IsEmpty() ||
  364. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  365. {
  366. if (Path.IsEmpty())
  367. {
  368. Terminal->ClearCaches();
  369. }
  370. else
  371. {
  372. Terminal->DirectoryModified(Path, true);
  373. }
  374. }
  375. }
  376. // We should flag a pending refresh for the background terminals or busy foreground terminals
  377. if (HasActiveTerminal() &&
  378. CanCommandLineFromAnotherInstance() &&
  379. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  380. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  381. {
  382. Terminal->ReloadDirectory();
  383. }
  384. }
  385. //---------------------------------------------------------------------------
  386. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  387. {
  388. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  389. AppLogFmt(L"Received copy data message with size %d", (int(CopyData->cbData)));
  390. size_t MessageSize = sizeof(TCopyDataMessage);
  391. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  392. if (Result)
  393. {
  394. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  395. Result = (Message.Version == TCopyDataMessage::Version1);
  396. if (Result)
  397. {
  398. switch (Message.Command)
  399. {
  400. case TCopyDataMessage::CommandCanCommandLine:
  401. Result = CanCommandLineFromAnotherInstance();
  402. if (Result)
  403. {
  404. AppLog(L"Can process command-line from other instance");
  405. }
  406. else
  407. {
  408. AppLog(L"Cannot process command-line from other instance");
  409. }
  410. break;
  411. case TCopyDataMessage::CommandCommandLine:
  412. {
  413. UnicodeString CommandLine(Message.CommandLine);
  414. Result = CommandLineFromAnotherInstance(CommandLine);
  415. }
  416. break;
  417. case TCopyDataMessage::RefreshPanel:
  418. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  419. break;
  420. case TCopyDataMessage::MainWindowCheck:
  421. Result = true;
  422. break;
  423. default:
  424. Result = false;
  425. break;
  426. }
  427. }
  428. }
  429. Message.Result = Result ? 1 : 0;
  430. }
  431. //---------------------------------------------------------------------------
  432. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  433. {
  434. WNDCLASS WindowClass = {0};
  435. WindowClass.lpfnWndProc = DefWindowProc;
  436. WindowClass.hInstance = HInstance;
  437. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  438. FHiddenWindow = NULL;
  439. if (RegisterClass(&WindowClass))
  440. {
  441. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  442. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  443. }
  444. }
  445. //---------------------------------------------------------------------------
  446. bool __fastcall TCustomScpExplorerForm::CanConsole()
  447. {
  448. return
  449. HasActiveTerminal() &&
  450. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  451. }
  452. //---------------------------------------------------------------------------
  453. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  454. {
  455. bool Result = Visible && !NonVisualDataModule->Busy;
  456. return Result;
  457. }
  458. //---------------------------------------------------------------------------
  459. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  460. const UnicodeString & CommandLine)
  461. {
  462. TProgramParams Params(CommandLine);
  463. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  464. if (Result)
  465. {
  466. NonVisualDataModule->StartBusy();
  467. try
  468. {
  469. // this action is initiated from another process,
  470. // so it's likely that our window is not visible,
  471. // and user won't see what is going on
  472. Application->BringToFront();
  473. // reload sessions as we may be asked to open a session
  474. // just stored by another instance
  475. StoredSessions->Reload();
  476. UnicodeString SessionName = Params.Param[1];
  477. std::unique_ptr<TObjectList> DataList(new TObjectList());
  478. try
  479. {
  480. UnicodeString DownloadFile; // unused
  481. int Flags = GetCommandLineParseUrlFlags(&Params);
  482. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  483. }
  484. catch (EAbort &)
  485. {
  486. // Happens when opening session in PuTTY and in other situations,
  487. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  488. // to process the command-line on its own (or to pass it to yet another instance)
  489. }
  490. if (DataList->Count > 0)
  491. {
  492. TTerminalManager * Manager = TTerminalManager::Instance();
  493. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  494. }
  495. }
  496. __finally
  497. {
  498. NonVisualDataModule->EndBusy();
  499. }
  500. }
  501. return Result;
  502. }
  503. //---------------------------------------------------------------------------
  504. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  505. {
  506. TManagedTerminal * Result;
  507. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  508. {
  509. Result = FManagedSession;
  510. }
  511. else
  512. {
  513. Result = NULL;
  514. }
  515. return Result;
  516. }
  517. //---------------------------------------------------------------------------
  518. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  519. {
  520. if (FManagedSession != value)
  521. {
  522. SessionChanging();
  523. DoSetManagedSession(value, false);
  524. }
  525. }
  526. //---------------------------------------------------------------------------
  527. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  528. {
  529. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  530. FManagedSession = value;
  531. {
  532. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  533. FAllowTransferPresetAutoSelect = false;
  534. TAutoFlag SessionChangingFlag(FSessionChanging);
  535. SessionChanged(Replace);
  536. }
  537. if (Terminal != NULL)
  538. {
  539. TransferPresetAutoSelect();
  540. }
  541. // Update app and tab titles, prevented above by FSessionChanging.
  542. UpdateControls();
  543. }
  544. //---------------------------------------------------------------------------
  545. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  546. {
  547. DoSetManagedSession(value, true);
  548. }
  549. //---------------------------------------------------------------------------
  550. void __fastcall TCustomScpExplorerForm::SessionChanging()
  551. {
  552. if (ManagedSession != NULL)
  553. {
  554. UpdateSession(ManagedSession);
  555. }
  556. }
  557. //---------------------------------------------------------------------------
  558. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  559. {
  560. if (ManagedSession != NULL)
  561. {
  562. UpdateSessionColor(static_cast<TColor>(ManagedSession->StateData->Color));
  563. }
  564. if (!TTerminalManager::Instance()->Updating)
  565. {
  566. SessionListChanged();
  567. }
  568. if (Terminal != NULL)
  569. {
  570. TObject * State = NULL;
  571. if (WinConfiguration->PreservePanelState)
  572. {
  573. State = Terminal->RemoteExplorerState;
  574. DirView(osRemote)->AnnounceState(State);
  575. }
  576. try
  577. {
  578. if (Replaced)
  579. {
  580. RemoteDirView->ReplaceTerminal(Terminal);
  581. }
  582. else
  583. {
  584. RemoteDirView->Terminal = Terminal;
  585. }
  586. if (Terminal->Active)
  587. {
  588. Terminal->RefreshDirectory();
  589. }
  590. if (State != NULL)
  591. {
  592. DirView(osRemote)->RestoreState(State);
  593. }
  594. }
  595. __finally
  596. {
  597. DirView(osRemote)->AnnounceState(NULL);
  598. }
  599. }
  600. else
  601. {
  602. RemoteDirView->Terminal = NULL;
  603. }
  604. InitStatusBar();
  605. UpdateTransferList();
  606. // Update panels Enable state before refreshing the labels
  607. UpdateControls();
  608. }
  609. //---------------------------------------------------------------------------
  610. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  611. {
  612. if (Queue != value)
  613. {
  614. if (FQueue != NULL)
  615. {
  616. FQueue->OnListUpdate = NULL;
  617. FQueue->OnQueueItemUpdate = NULL;
  618. }
  619. FQueue = value;
  620. if (FQueue != NULL)
  621. {
  622. DebugAssert(FQueue->OnListUpdate == NULL);
  623. FQueue->OnListUpdate = QueueListUpdate;
  624. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  625. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  626. }
  627. QueueChanged();
  628. }
  629. }
  630. //---------------------------------------------------------------------------
  631. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  632. TListItem * Item)
  633. {
  634. if (FQueueActedItem == Item)
  635. {
  636. FQueueActedItem = NULL;
  637. if ((QueueView3->PopupMenu != NULL) &&
  638. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  639. {
  640. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  641. ReleaseCapture();
  642. }
  643. }
  644. if (Item->Data == FPendingQueueActionItem)
  645. {
  646. FPendingQueueActionItem = NULL;
  647. }
  648. }
  649. //---------------------------------------------------------------------------
  650. bool TCustomScpExplorerForm::IsAnythingQueued()
  651. {
  652. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  653. }
  654. //---------------------------------------------------------------------------
  655. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  656. {
  657. {
  658. TGuard Guard(FQueueStatusSection);
  659. FQueueStatusInvalidated = false;
  660. if (FQueue != NULL)
  661. {
  662. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  663. }
  664. }
  665. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  666. {
  667. FMaxQueueLength = FQueueStatus->Count;
  668. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  669. }
  670. {
  671. TAutoFlag Flag(FQueueStatusUpdating);
  672. FQueueController->UpdateQueueStatus(FQueueStatus);
  673. }
  674. UpdateQueueFileList();
  675. SetQueueProgress();
  676. UpdateQueueView();
  677. bool IsEmpty = !IsAnythingQueued();
  678. if (IsEmpty && (Terminal != NULL))
  679. {
  680. TOnceDoneOperation OnceDoneOperation =
  681. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  682. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  683. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  684. {
  685. FQueue->Enabled = false;
  686. }
  687. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  688. {
  689. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  690. }
  691. }
  692. }
  693. //---------------------------------------------------------------------------
  694. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  695. {
  696. UnicodeString Result;
  697. if (FQueueStatus != NULL)
  698. {
  699. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  700. {
  701. TFileOperationProgressType * ProgressData =
  702. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  703. if ((ProgressData != NULL) && ProgressData->InProgress)
  704. {
  705. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  706. }
  707. }
  708. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  709. {
  710. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  711. }
  712. }
  713. return Result;
  714. }
  715. //---------------------------------------------------------------------------
  716. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  717. {
  718. ComponentVisible[fcQueueView] =
  719. !IsLocalBrowserMode() &&
  720. ((WinConfiguration->QueueView.Show == qvShow) ||
  721. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  722. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  723. }
  724. //---------------------------------------------------------------------------
  725. void __fastcall TCustomScpExplorerForm::QueueChanged()
  726. {
  727. if (FQueueStatus != NULL)
  728. {
  729. // UpdateFileList implementation relies on the status being recreated when session changes
  730. delete FQueueStatus;
  731. FQueueStatus = NULL;
  732. }
  733. UpdateQueueStatus(true);
  734. }
  735. //---------------------------------------------------------------------------
  736. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  737. {
  738. if (FQueue == Queue)
  739. {
  740. FQueueStatusInvalidated = true;
  741. }
  742. }
  743. //---------------------------------------------------------------------------
  744. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  745. TQueueItem * Item)
  746. {
  747. if (FQueue == Queue)
  748. {
  749. TGuard Guard(FQueueStatusSection);
  750. // this may be running in parallel with QueueChanged
  751. if (FQueueStatus != NULL)
  752. {
  753. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  754. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  755. {
  756. FRefreshLocalDirectory = (QueueItem == NULL) ||
  757. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  758. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  759. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  760. }
  761. if (QueueItem != NULL)
  762. {
  763. QueueItem->UserData = (void*)true;
  764. FQueueItemInvalidated = true;
  765. }
  766. }
  767. }
  768. }
  769. //---------------------------------------------------------------------------
  770. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  771. {
  772. // during standalone editing, we have no way to see/control queue,
  773. // so we have to always popup prompts automatically
  774. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  775. }
  776. //---------------------------------------------------------------------------
  777. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  778. {
  779. if (FQueueStatus != NULL)
  780. {
  781. bool QueueAutoPopup = IsQueueAutoPopup();
  782. bool NeedRefresh = FQueueController->NeedRefresh();
  783. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  784. FQueueItemInvalidated = false;
  785. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  786. bool Updated = false;
  787. TQueueItemProxy * QueueItem;
  788. bool WasUserAction;
  789. for (int Index = 0; Index < Limit; Index++)
  790. {
  791. bool Update = false;
  792. QueueItem = FQueueStatus->Items[Index];
  793. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  794. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  795. (FPendingQueueActionItem == NULL))
  796. {
  797. FPendingQueueActionItem = QueueItem;
  798. FUserActionTimer->Enabled = true;
  799. }
  800. if ((bool)QueueItem->UserData)
  801. {
  802. QueueItem->UserData = (void*)false;
  803. QueueItem->Update();
  804. Updated = true;
  805. Update = true;
  806. }
  807. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  808. {
  809. Update = true;
  810. }
  811. if (Update)
  812. {
  813. FQueueController->RefreshQueueItem(QueueItem);
  814. }
  815. }
  816. if (Updated)
  817. {
  818. NonVisualDataModule->UpdateNonVisibleActions();
  819. SetQueueProgress();
  820. UpdateQueueFileList();
  821. }
  822. }
  823. }
  824. //---------------------------------------------------------------------------
  825. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  826. {
  827. if (FTaskbarList != NULL)
  828. {
  829. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  830. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  831. }
  832. }
  833. //---------------------------------------------------------------------------
  834. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  835. {
  836. if (Progress >= 0)
  837. {
  838. if (FTaskbarList != NULL)
  839. {
  840. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  841. }
  842. }
  843. else
  844. {
  845. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  846. }
  847. }
  848. //---------------------------------------------------------------------------
  849. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  850. {
  851. int OverallProgress;
  852. // FProgressForm is null when this is called from SetQueueProgress
  853. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  854. {
  855. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  856. }
  857. else if (!ProgressData->IsIndeterminate())
  858. {
  859. OverallProgress = ProgressData->OverallProgress();
  860. }
  861. else
  862. {
  863. OverallProgress = -1;
  864. }
  865. SetTaskbarListProgressValue(OverallProgress);
  866. }
  867. //---------------------------------------------------------------------------
  868. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  869. {
  870. TTerminalManager::Instance()->QueueStatusUpdated();
  871. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  872. {
  873. // Do not animate task bar button, when items are queued, but processing is disabled
  874. if ((FQueueStatus != NULL) && (FQueueStatus->ActivePrimaryCount > 0))
  875. {
  876. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  877. {
  878. TFileOperationProgressType * ProgressData = NULL;
  879. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  880. {
  881. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  882. }
  883. if ((ProgressData != NULL) &&
  884. ProgressData->InProgress)
  885. {
  886. SetTaskbarListProgressValue(ProgressData);
  887. }
  888. else
  889. {
  890. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  891. }
  892. }
  893. else
  894. {
  895. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  896. }
  897. }
  898. else
  899. {
  900. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  901. }
  902. }
  903. UpdateQueueLabel();
  904. }
  905. //---------------------------------------------------------------------------
  906. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  907. {
  908. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  909. if (FQueueStatus != NULL)
  910. {
  911. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  912. if (ActiveAndPendingPrimaryCount > 0)
  913. {
  914. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  915. }
  916. }
  917. QueueLabel->Caption = Caption;
  918. }
  919. //---------------------------------------------------------------------------
  920. void TCustomScpExplorerForm::CheckStoreTransition()
  921. {
  922. if (WinConfiguration->StoreTransition == stInit)
  923. {
  924. if (IsUWP())
  925. {
  926. WinConfiguration->StoreTransition = stStoreFresh;
  927. AppLog(L"Fresh UWP/store installation");
  928. }
  929. else
  930. {
  931. WinConfiguration->StoreTransition = stStandard;
  932. AppLog(L"Fresh standard installation");
  933. }
  934. }
  935. else if ((WinConfiguration->StoreTransition == stStandard) && IsUWP())
  936. {
  937. WinConfiguration->StoreTransition = stStoreMigrated;
  938. WinConfiguration->FirstRun = WinConfiguration->FirstRun + L"=>" + StandardDatestamp();
  939. AppLog(L"Standard installation migrated to store installation");
  940. }
  941. if (WinConfiguration->StoreTransition == stStoreMigrated)
  942. {
  943. TTBXToolbar * Toolbar = new TTBXToolbar(MessageDock);
  944. Toolbar->Parent = MessageDock;
  945. Toolbar->FullSize = true;
  946. Toolbar->Color = Application->HintColor;
  947. TTBXLabelItem * LabelItem;
  948. LabelItem = new TTBXLabelItem(Toolbar);
  949. // Convert to explicit RGB "black" color, to make sure it is contrasting even in Dark theme
  950. // (TTBXOfficeXPTheme.GetStandardColor would convert clWindow to "white" in dark theme)
  951. LabelItem->FontSettings->Color = static_cast<TColor>(GetSysColor(clWindow));
  952. LabelItem->Margin = ScaleByTextHeight(MessageDock, 2); // Is this automatically rescaled?
  953. LabelItem->Caption = LoadStr(STORE_MIGRATION_THANKS);
  954. Toolbar->Items->Add(LabelItem);
  955. TTBXCustomItem * LinkItem = new TTBXItem(Toolbar);
  956. LinkItem->FontSettings->Color = LinkColor;
  957. LinkItem->Caption = LoadStr(STORE_MIGRATION_LEARN);
  958. LinkItem->OnClick = StoreTransitionLinkClick;
  959. Toolbar->Items->Add(LinkItem);
  960. AddMenuSeparator(Toolbar->Items);
  961. TTBXCustomItem * CloseItem = new TTBXItem(Toolbar);
  962. CloseItem->FontSettings->Color = static_cast<TColor>(GetSysColor(clWindow));
  963. CloseItem->Caption = LoadStr(MESSAGE_DISMISS);
  964. CloseItem->OnClick = StoreTransitionCloseClick;
  965. Toolbar->Items->Add(CloseItem);
  966. GiveTBItemPriority(CloseItem);
  967. }
  968. }
  969. //---------------------------------------------------------------------------
  970. void __fastcall TCustomScpExplorerForm::StoreTransitionLinkClick(TObject *)
  971. {
  972. ShowHelp(HELP_STORE_TRANSITION);
  973. }
  974. //---------------------------------------------------------------------------
  975. void __fastcall TCustomScpExplorerForm::StoreTransitionCloseClick(TObject * Sender)
  976. {
  977. TTBCustomItem * Item = DebugNotNull(dynamic_cast<TTBCustomItem *>(Sender));
  978. TTBXToolbar * Toolbar = DebugNotNull(dynamic_cast<TTBXToolbar *>(DebugNotNull(DebugNotNull(Item->Parent)->ParentComponent)));
  979. delete Toolbar;
  980. WinConfiguration->StoreTransition = stStoreAcknowledged;
  981. UpdateControls();
  982. }
  983. //---------------------------------------------------------------------------
  984. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  985. {
  986. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  987. static_cast<TComponent*>(GetComponent(fcTransferList)));
  988. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  989. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  990. TransferList->Strings->BeginUpdate();
  991. try
  992. {
  993. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  994. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  995. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  996. if (FTransferDropDownHint.IsEmpty())
  997. {
  998. FTransferDropDownHint = TransferDropDown->Hint;
  999. }
  1000. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  1001. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  1002. TransferDropDown->Text = StripHotkey(Name);
  1003. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  1004. (FTransferDropDownHint, StripHotkey(Name),
  1005. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  1006. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  1007. // update the label, otherwise when it is updated only on the first draw
  1008. // of the list, it is drawn "bold" for some reason
  1009. FTransferListHoverIndex = TransferList->ItemIndex;
  1010. UpdateTransferLabel();
  1011. }
  1012. __finally
  1013. {
  1014. TransferList->Strings->EndUpdate();
  1015. }
  1016. }
  1017. //---------------------------------------------------------------------------
  1018. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  1019. {
  1020. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  1021. DebugAssert(Toolbar != NULL);
  1022. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  1023. }
  1024. //---------------------------------------------------------------------------
  1025. void __fastcall TCustomScpExplorerForm::UpdateActions()
  1026. {
  1027. TForm::UpdateActions();
  1028. if (ComponentVisible[fcCustomCommandsBand])
  1029. {
  1030. UpdateCustomCommandsToolbar();
  1031. }
  1032. }
  1033. //---------------------------------------------------------------------------
  1034. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  1035. {
  1036. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  1037. }
  1038. //---------------------------------------------------------------------------
  1039. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  1040. {
  1041. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  1042. {
  1043. DirView->RowSelect = WinConfiguration->FullRowSelect;
  1044. // Without this, the panel turns gray and unused part of header turns black
  1045. DirView->Perform(CM_COLORCHANGED, 0, 0);
  1046. }
  1047. }
  1048. //---------------------------------------------------------------------------
  1049. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  1050. {
  1051. Color = GetBtnFaceColor();
  1052. DebugAssert(Configuration && RemoteDirView);
  1053. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1054. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  1055. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  1056. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  1057. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1058. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1059. RemoteDirView->AlwaysSortDirectoriesByName = WinConfiguration->AlwaysSortDirectoriesByName;
  1060. UpdateRowSelect(RemoteDirView);
  1061. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1062. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  1063. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  1064. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1065. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1066. UpdateSessionsPageControlHeight();
  1067. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1068. UpdateToolbarDisplayMode();
  1069. UpdateTransferList();
  1070. if (Terminal != NULL)
  1071. {
  1072. TransferPresetAutoSelect();
  1073. }
  1074. UpdateQueueView();
  1075. UpdateControls();
  1076. // this can be called even before constuctor finishes.
  1077. if (FEditorManager != NULL)
  1078. {
  1079. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  1080. }
  1081. // this can be called even before constuctor finishes.
  1082. if (FLocalEditors != NULL)
  1083. {
  1084. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  1085. {
  1086. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  1087. }
  1088. }
  1089. if (ComponentVisible[fcCustomCommandsBand])
  1090. {
  1091. UpdateCustomCommandsToolbar();
  1092. }
  1093. // show only when keeping queue items forever,
  1094. // otherwise, it's enough to have it in the context menu
  1095. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1096. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1097. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1098. {
  1099. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1100. FFileColorsCurrent = WinConfiguration->FileColors;
  1101. FileColorsChanged();
  1102. }
  1103. }
  1104. //---------------------------------------------------------------------------
  1105. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1106. {
  1107. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1108. DirView->Invalidate();
  1109. }
  1110. //---------------------------------------------------------------------------
  1111. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1112. {
  1113. DoFileColorsChanged(RemoteDirView);
  1114. }
  1115. //---------------------------------------------------------------------------
  1116. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1117. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1118. void * /*Arg*/)
  1119. {
  1120. if (Token != NULL)
  1121. {
  1122. TForm * Editor = dynamic_cast<TForm*>(Token);
  1123. DebugAssert(Editor != NULL);
  1124. ReconfigureEditorForm(Editor);
  1125. }
  1126. }
  1127. //---------------------------------------------------------------------------
  1128. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1129. {
  1130. WinConfiguration->DDTransferConfirmation = asOn;
  1131. }
  1132. //---------------------------------------------------------------------------
  1133. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1134. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1135. {
  1136. // noop
  1137. }
  1138. //---------------------------------------------------------------------------
  1139. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1140. {
  1141. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1142. }
  1143. //---------------------------------------------------------------------------
  1144. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1145. {
  1146. if (DoNotShowAgain)
  1147. {
  1148. if (DragDrop)
  1149. {
  1150. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1151. {
  1152. PopupTrayBalloon(
  1153. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1154. }
  1155. WinConfiguration->DDTransferConfirmation = asOff;
  1156. }
  1157. else
  1158. {
  1159. WinConfiguration->ConfirmTransferring = false;
  1160. }
  1161. }
  1162. else
  1163. {
  1164. // User explicitly unchecked "do not show again",
  1165. // so show him the dialog the next time
  1166. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1167. {
  1168. WinConfiguration->DDTransferConfirmation = asOn;
  1169. }
  1170. }
  1171. }
  1172. //---------------------------------------------------------------------------
  1173. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1174. TTransferDirection Direction, TTransferType Type, bool Temp,
  1175. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1176. bool Confirm, bool DragDrop, int Options)
  1177. {
  1178. bool Result = true;
  1179. DebugAssert(HasActiveTerminal());
  1180. // these parameters are known in advance
  1181. int Params =
  1182. FLAGMASK(Type == ttMove, cpDelete);
  1183. bool ToTemp = (Temp && (Direction == tdToLocal));
  1184. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1185. {
  1186. bool DisableNewerOnly =
  1187. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1188. ToTemp;
  1189. Options |=
  1190. FLAGMASK(ToTemp, coTemp) |
  1191. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1192. coDoNotShowAgain;
  1193. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1194. int CopyParamAttrs = (Direction == tdToRemote ?
  1195. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1196. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1197. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1198. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1199. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1200. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1201. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1202. if (Result)
  1203. {
  1204. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1205. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1206. }
  1207. }
  1208. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1209. {
  1210. Configuration->Usage->Inc(L"TransfersOnBackground");
  1211. // these parameter are known only after transfer dialog
  1212. Params |=
  1213. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1214. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1215. Result = false;
  1216. ClearTransferSourceSelection(Direction);
  1217. }
  1218. return Result;
  1219. }
  1220. //---------------------------------------------------------------------------
  1221. void TCustomScpExplorerForm::ClearOperationSelection(TOperationSide Side)
  1222. {
  1223. if (FOnFileOperationFinished != NULL)
  1224. {
  1225. FOnFileOperationFinished(Side, UnicodeString(), true);
  1226. }
  1227. else
  1228. {
  1229. if (HasDirView[Side])
  1230. {
  1231. DirView(Side)->SelectAll(smNone);
  1232. }
  1233. }
  1234. }
  1235. //---------------------------------------------------------------------------
  1236. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1237. {
  1238. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1239. ClearOperationSelection(Side);
  1240. }
  1241. //---------------------------------------------------------------------------
  1242. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1243. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1244. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1245. int Params)
  1246. {
  1247. DebugAssert(Queue != NULL);
  1248. bool SingleFile = false;
  1249. if (FileList->Count == 1)
  1250. {
  1251. if (Direction == tdToRemote)
  1252. {
  1253. UnicodeString FileName = FileList->Strings[0];
  1254. SingleFile = FileExists(ApiPath(FileName));
  1255. }
  1256. else
  1257. {
  1258. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1259. SingleFile = !File->IsDirectory;
  1260. }
  1261. }
  1262. TQueueItem * QueueItem;
  1263. if (Direction == tdToRemote)
  1264. {
  1265. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1266. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1267. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1268. }
  1269. else
  1270. {
  1271. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1272. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1273. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1274. }
  1275. AddQueueItem(Queue, QueueItem, Terminal);
  1276. }
  1277. //---------------------------------------------------------------------------
  1278. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1279. {
  1280. if (Queue->IsEmpty)
  1281. {
  1282. Terminal->QueueOperationStart = Now();
  1283. }
  1284. Queue->AddItem(QueueItem);
  1285. }
  1286. //---------------------------------------------------------------------------
  1287. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1288. {
  1289. }
  1290. //---------------------------------------------------------------------------
  1291. void __fastcall TCustomScpExplorerForm::InitControls()
  1292. {
  1293. DebugAssert(FDocks != NULL);
  1294. for (int Index = 0; Index < ComponentCount; Index++)
  1295. {
  1296. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1297. if ((Dock != NULL) && (Dock->Tag == 0))
  1298. {
  1299. FDocks->Add(Dock);
  1300. }
  1301. }
  1302. CollectItemsWithTextDisplayMode(this);
  1303. }
  1304. //---------------------------------------------------------------------------
  1305. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1306. {
  1307. ConfigurationChanged();
  1308. QueuePanel->Height =
  1309. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1310. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1311. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1312. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1313. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1314. QueueFileList->Height =
  1315. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1316. if (QueueFileList->Visible)
  1317. {
  1318. AdjustQueueLayout();
  1319. }
  1320. UpdateCustomCommandsToolbar();
  1321. }
  1322. //---------------------------------------------------------------------------
  1323. void __fastcall TCustomScpExplorerForm::StoreParams()
  1324. {
  1325. int APixelsPerInch = GetControlPixelsPerInch(this);
  1326. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1327. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1328. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1329. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1330. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1331. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1332. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1333. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1334. }
  1335. //---------------------------------------------------------------------------
  1336. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1337. {
  1338. if (!FFormRestored)
  1339. {
  1340. FFormRestored = true;
  1341. RestoreFormParams();
  1342. }
  1343. TForm::CreateParams(Params);
  1344. }
  1345. //---------------------------------------------------------------------------
  1346. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1347. {
  1348. DebugAssert(FDocks != NULL);
  1349. for (int Index = 0; Index < FDocks->Count; Index++)
  1350. {
  1351. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1352. }
  1353. }
  1354. //---------------------------------------------------------------------------
  1355. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1356. {
  1357. SetDockAllowDrag(true);
  1358. while (!ButtonsStr.IsEmpty())
  1359. {
  1360. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1361. UnicodeString Name = CutToChar(S, L'=', true);
  1362. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1363. {
  1364. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1365. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1366. {
  1367. while (!S.IsEmpty())
  1368. {
  1369. UnicodeString S2 = CutToChar(S, L',', true);
  1370. UnicodeString Name = CutToChar(S2, L':', true);
  1371. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1372. {
  1373. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1374. if (SameText(GetToolbarItemName(Item), Name))
  1375. {
  1376. // forward compatibility
  1377. UnicodeString S3 = CutToChar(S2, L':', true);
  1378. bool Visible = StrToIntDef(S3, 0);
  1379. Item->Visible = Visible;
  1380. }
  1381. }
  1382. }
  1383. }
  1384. }
  1385. }
  1386. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1387. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1388. }
  1389. //---------------------------------------------------------------------------
  1390. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1391. {
  1392. UnicodeString Result;
  1393. SetDockAllowDrag(true);
  1394. Result = ::GetToolbarsLayoutStr(this);
  1395. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1396. return Result;
  1397. }
  1398. //---------------------------------------------------------------------------
  1399. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1400. {
  1401. UnicodeString Result;
  1402. if (Item->Action != NULL)
  1403. {
  1404. Result = Item->Action->Name;
  1405. Result = RemoveSuffix(Result, L"Action", true);
  1406. }
  1407. else
  1408. {
  1409. Result = Item->Name;
  1410. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1411. Result = RemoveSuffix(Result, L"Item", true);
  1412. }
  1413. return Result;
  1414. }
  1415. //---------------------------------------------------------------------------
  1416. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1417. {
  1418. UnicodeString Result;
  1419. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1420. {
  1421. UnicodeString ToolbarStr;
  1422. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1423. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1424. {
  1425. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1426. {
  1427. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1428. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1429. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1430. // and save the changes here only.
  1431. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1432. {
  1433. UnicodeString Name = GetToolbarItemName(Item);
  1434. DebugAssert(Name.Pos(L"TBX") == 0);
  1435. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1436. }
  1437. }
  1438. }
  1439. if (!ToolbarStr.IsEmpty())
  1440. {
  1441. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1442. }
  1443. }
  1444. return Result;
  1445. }
  1446. //---------------------------------------------------------------------------
  1447. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1448. {
  1449. DebugAssert(FProgressForm == NULL);
  1450. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1451. bool AllowMoveToQueue = (FTransferResumeList != NULL) || (FDeletedFiles != NULL);
  1452. FProgressForm = new TProgressForm(Application, AllowMoveToQueue, AllowSkip, SynchronizeProgress);
  1453. FProgressForm->DeleteLocalToRecycleBin =
  1454. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1455. FProgressForm->DeleteRemoteToRecycleBin =
  1456. HasActiveTerminal() &&
  1457. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1458. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1459. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1460. LockWindow();
  1461. // Actually, do not know what hides the progress once the operation finishes
  1462. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1463. SetTaskbarListProgressState(TBPF_NORMAL);
  1464. }
  1465. //---------------------------------------------------------------------------
  1466. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1467. {
  1468. SAFE_DESTROY(FProgressForm);
  1469. UnlockWindow();
  1470. SetQueueProgress();
  1471. }
  1472. //---------------------------------------------------------------------------
  1473. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1474. TFileOperationProgressType & ProgressData)
  1475. {
  1476. // Download to temporary local directory
  1477. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1478. {
  1479. bool Continue = true;
  1480. FPendingTempSpaceWarn = false;
  1481. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1482. if (!Continue)
  1483. {
  1484. Abort();
  1485. }
  1486. }
  1487. // operation is being executed and we still didn't show up progress form
  1488. if (ProgressData.InProgress && !FProgressForm)
  1489. {
  1490. CreateProgressForm(NULL);
  1491. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1492. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1493. }
  1494. // operation is finished (or terminated), so we hide progress form
  1495. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1496. {
  1497. DestroyProgressForm();
  1498. if (ProgressData.Operation == foCalculateSize)
  1499. {
  1500. // When calculating size before transfer, the abort caused by
  1501. // cancel flag set due to "MoveToQueue" below,
  1502. // is not propagated back to us in ExecuteFileOperation,
  1503. // as it is silently swallowed.
  1504. // So we explicitly (re)throw it here.
  1505. if (FMoveToQueue)
  1506. {
  1507. Abort();
  1508. }
  1509. }
  1510. else
  1511. {
  1512. if ((ProgressData.Cancel == csContinue) &&
  1513. !FAutoOperation)
  1514. {
  1515. OperationComplete(ProgressData.StartTime);
  1516. }
  1517. }
  1518. }
  1519. if (FProgressForm &&
  1520. // Can happen only during synchronization, when ending one of the operations
  1521. ProgressData.InProgress)
  1522. {
  1523. FProgressForm->SetProgressData(ProgressData);
  1524. DebugAssert(ProgressData.InProgress);
  1525. SetTaskbarListProgressValue(&ProgressData);
  1526. if (FProgressForm->Cancel > csContinue)
  1527. {
  1528. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1529. if (FProgressForm->Cancel == csCancelFile)
  1530. {
  1531. FProgressForm->ClearCancel();
  1532. }
  1533. // cancel cancels even the move
  1534. FMoveToQueue = false;
  1535. }
  1536. else if (FProgressForm->MoveToQueue)
  1537. {
  1538. FMoveToQueue = true;
  1539. ProgressData.SetCancelAtLeast(csCancel);
  1540. }
  1541. if ((FTransferResumeList != NULL) &&
  1542. ProgressData.InProgress &&
  1543. ProgressData.IsTransfer() &&
  1544. !ProgressData.FullFileName.IsEmpty())
  1545. {
  1546. if ((FTransferResumeList->Count == 0) ||
  1547. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1548. {
  1549. // note that we do not recognize directories from files here
  1550. FTransferResumeList->Add(ProgressData.FullFileName);
  1551. }
  1552. }
  1553. }
  1554. }
  1555. //---------------------------------------------------------------------------
  1556. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1557. const TDateTime & StartTime)
  1558. {
  1559. if (GUIConfiguration->BeepOnFinish &&
  1560. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1561. {
  1562. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1563. DWORD Sound;
  1564. if (!ExtractFileExt(BeepSound).IsEmpty())
  1565. {
  1566. Sound = SND_FILENAME;
  1567. }
  1568. else
  1569. {
  1570. Sound = SND_ALIAS;
  1571. }
  1572. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1573. }
  1574. }
  1575. //---------------------------------------------------------------------------
  1576. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1577. TFileOperationProgressType & ProgressData)
  1578. {
  1579. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1580. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1581. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1582. FileOperationProgress(ProgressData);
  1583. }
  1584. //---------------------------------------------------------------------------
  1585. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1586. {
  1587. UnicodeString Result;
  1588. if (FProgressForm != NULL)
  1589. {
  1590. Result = FProgressForm->ProgressStr();
  1591. }
  1592. return Result;
  1593. }
  1594. //---------------------------------------------------------------------------
  1595. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1596. bool DragDrop)
  1597. {
  1598. return (!DragDrop && (DropSourceControl == NULL)) ||
  1599. (DropSourceControl == DirView(osOther));
  1600. }
  1601. //---------------------------------------------------------------------------
  1602. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1603. {
  1604. TCustomDirView * ADirView = DirView(Side);
  1605. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1606. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1607. // this can happen when local drive is unplugged in the middle of the operation
  1608. if (Item != NULL)
  1609. {
  1610. if (Unselect)
  1611. {
  1612. Item->Selected = false;
  1613. }
  1614. ADirView->MakeProgressVisible(Item);
  1615. }
  1616. return Item;
  1617. }
  1618. //---------------------------------------------------------------------------
  1619. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1620. TFileOperation Operation, TOperationSide Side,
  1621. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1622. TOnceDoneOperation & OnceDoneOperation)
  1623. {
  1624. if (!FAutoOperation)
  1625. {
  1626. // no selection on "/upload", form serves only as event handler
  1627. // (it is not displayed)
  1628. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1629. ((Operation == FPrimaryOperation) ||
  1630. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1631. {
  1632. TOperationSide DViewSide;
  1633. if ((FProgressSide != osCurrent) &&
  1634. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1635. {
  1636. // assume the operation is over the focused panel
  1637. DViewSide = FProgressSide;
  1638. }
  1639. else
  1640. {
  1641. DViewSide = Side;
  1642. }
  1643. if (FOnFileOperationFinished != NULL)
  1644. {
  1645. FOnFileOperationFinished(DViewSide, FileName, Success);
  1646. }
  1647. else
  1648. {
  1649. VisualiseOperationFinished(DViewSide, FileName, Success);
  1650. }
  1651. }
  1652. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1653. {
  1654. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1655. {
  1656. if (Side == osLocal)
  1657. {
  1658. Configuration->Usage->Inc(L"UploadedFiles");
  1659. }
  1660. else
  1661. {
  1662. Configuration->Usage->Inc(L"DownloadedFiles");
  1663. }
  1664. }
  1665. }
  1666. }
  1667. if (Success && (FSynchronizeController != NULL))
  1668. {
  1669. DebugAssert(!IsSideLocalBrowser(osRemote));
  1670. if (Operation == foCopy)
  1671. {
  1672. DebugAssert(Side == osLocal);
  1673. FSynchronizeController->LogOperation(soUpload, FileName);
  1674. }
  1675. else if (Operation == foDelete)
  1676. {
  1677. DebugAssert(Side == osRemote);
  1678. FSynchronizeController->LogOperation(soDelete, FileName);
  1679. }
  1680. }
  1681. if (FProgressForm)
  1682. {
  1683. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1684. }
  1685. }
  1686. //---------------------------------------------------------------------------
  1687. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1688. TFileOperation Operation, TOperationSide Side,
  1689. bool Temp, const UnicodeString & FileName, Boolean Success,
  1690. TOnceDoneOperation & OnceDoneOperation)
  1691. {
  1692. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1693. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1694. OnceDoneOperation);
  1695. }
  1696. //---------------------------------------------------------------------------
  1697. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1698. {
  1699. return false;
  1700. }
  1701. //---------------------------------------------------------------------------
  1702. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1703. {
  1704. return false;
  1705. }
  1706. //---------------------------------------------------------------------------
  1707. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1708. {
  1709. return false;
  1710. }
  1711. //---------------------------------------------------------------------------
  1712. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1713. {
  1714. return NULL;
  1715. }
  1716. //---------------------------------------------------------------------------
  1717. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1718. {
  1719. DebugAssert(GetSide(Side) == osRemote);
  1720. DebugUsedParam(Side);
  1721. return RemoteDirView;
  1722. }
  1723. //---------------------------------------------------------------------------
  1724. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1725. {
  1726. DebugAssert(GetSide(Side) == osRemote);
  1727. DebugUsedParam(Side);
  1728. return RemoteDriveView;
  1729. }
  1730. //---------------------------------------------------------------------------
  1731. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1732. {
  1733. DebugAssert(GetSide(Side) == osRemote);
  1734. DebugUsedParam(Side);
  1735. return HasActiveTerminal();
  1736. }
  1737. //---------------------------------------------------------------------------
  1738. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1739. TOperationSide Side, int FilesOnly)
  1740. {
  1741. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1742. }
  1743. //---------------------------------------------------------------------------
  1744. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1745. TOperationSide Side, int FilesOnly)
  1746. {
  1747. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1748. }
  1749. //---------------------------------------------------------------------------
  1750. struct THistoryItemData
  1751. {
  1752. short int Side;
  1753. short int Index;
  1754. };
  1755. //---------------------------------------------------------------------------
  1756. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1757. {
  1758. DirView(Side)->HistoryGo(Index);
  1759. }
  1760. //---------------------------------------------------------------------------
  1761. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1762. {
  1763. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1764. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1765. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1766. }
  1767. //---------------------------------------------------------------------------
  1768. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1769. bool Back)
  1770. {
  1771. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1772. {
  1773. TCustomDirView * DView = DirView(Side);
  1774. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1775. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1776. if (ICount > 10)
  1777. {
  1778. ICount = 10;
  1779. }
  1780. Menu->Items->Clear();
  1781. THistoryItemData Data;
  1782. Data.Side = Side;
  1783. for (short int i = 1; i <= ICount; i++)
  1784. {
  1785. TTBCustomItem * Item = new TTBXItem(Menu);
  1786. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1787. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1788. Item->Hint = DView->HistoryPath[Data.Index];
  1789. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1790. Item->Tag = *reinterpret_cast<int*>(&Data);
  1791. Item->OnClick = HistoryItemClick;
  1792. Menu->Items->Add(Item);
  1793. }
  1794. }
  1795. }
  1796. //---------------------------------------------------------------------------
  1797. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1798. TOperationSide Side, bool Back)
  1799. {
  1800. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1801. {
  1802. // workaround
  1803. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1804. // operator new allocates memory in Pascal code, but calls inline
  1805. // contructor in C++, leading in problems, the function does
  1806. // both in Pascal code
  1807. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1808. UpdateHistoryMenu(Side, Back);
  1809. }
  1810. return FHistoryMenu[Side == osLocal][Back];
  1811. }
  1812. //---------------------------------------------------------------------------
  1813. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1814. TCustomDirView *Sender)
  1815. {
  1816. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1817. UpdateHistoryMenu(Side, true);
  1818. UpdateHistoryMenu(Side, false);
  1819. }
  1820. //---------------------------------------------------------------------------
  1821. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1822. {
  1823. // remote custom commands can be executed only if the server supports shell commands
  1824. // or have secondary shell
  1825. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1826. }
  1827. //---------------------------------------------------------------------------
  1828. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1829. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1830. {
  1831. // -1 = hidden, 0 = disabled, 1 = enabled
  1832. int Result;
  1833. TFileCustomCommand RemoteCustomCommand;
  1834. TLocalCustomCommand LocalCustomCommand;
  1835. TFileCustomCommand * NonInteractiveCustomCommand =
  1836. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1837. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1838. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1839. if (FLAGCLEAR(Command.Params, ccLocal))
  1840. {
  1841. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1842. // custom command that does not operate with files can be executed anytime ...
  1843. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1844. {
  1845. // ... but do not show such command in remote file menu
  1846. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1847. {
  1848. Result = AllowedState;
  1849. }
  1850. else
  1851. {
  1852. Result = -1;
  1853. }
  1854. }
  1855. else
  1856. {
  1857. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1858. {
  1859. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1860. }
  1861. else
  1862. {
  1863. Result = -1;
  1864. }
  1865. }
  1866. }
  1867. else
  1868. {
  1869. // custom command that does not operate with files can be executed anytime
  1870. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1871. {
  1872. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1873. }
  1874. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1875. {
  1876. if ((ListType == ccltAll) || (ListType == ccltFile))
  1877. {
  1878. // special case is "diff"-style command that can be executed over any side,
  1879. // if we have both sides
  1880. Result =
  1881. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1882. // directly (instead of EnableSelectedOperation) to pass
  1883. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1884. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1885. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1886. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1887. }
  1888. else if (ListType == ccltBoth)
  1889. {
  1890. DebugAssert(!IsLocalBrowserMode());
  1891. Result = 1;
  1892. }
  1893. else
  1894. {
  1895. Result = -1;
  1896. }
  1897. }
  1898. else
  1899. {
  1900. if ((ListType == ccltAll) ||
  1901. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1902. {
  1903. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1904. }
  1905. else
  1906. {
  1907. Result = -1;
  1908. }
  1909. }
  1910. if ((Result > 0) &&
  1911. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1912. !HasActiveTerminal())
  1913. {
  1914. Result = 0;
  1915. }
  1916. }
  1917. return Result;
  1918. }
  1919. //---------------------------------------------------------------------------
  1920. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1921. TStrings * FileList, const TCustomCommandType & ACommand,
  1922. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1923. {
  1924. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1925. {
  1926. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1927. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1928. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1929. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1930. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1931. bool Capture =
  1932. FLAGSET(ACommand.Params, ccShowResults) ||
  1933. FLAGSET(ACommand.Params, ccCopyResults) ||
  1934. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1935. TCaptureOutputEvent OutputEvent = NULL;
  1936. DebugAssert(FCapturedLog == NULL);
  1937. if (Capture)
  1938. {
  1939. FCapturedLog = new TStringList();
  1940. OutputEvent = TerminalCaptureLog;
  1941. }
  1942. try
  1943. {
  1944. if (!RemoteCustomCommand.IsFileCommand(Command))
  1945. {
  1946. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1947. OutputEvent);
  1948. }
  1949. else
  1950. {
  1951. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1952. }
  1953. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1954. {
  1955. if (FLAGSET(ACommand.Params, ccCopyResults))
  1956. {
  1957. CopyToClipboard(FCapturedLog);
  1958. }
  1959. if (FLAGSET(ACommand.Params, ccShowResults))
  1960. {
  1961. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1962. }
  1963. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1964. {
  1965. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1966. }
  1967. }
  1968. }
  1969. __finally
  1970. {
  1971. SAFE_DESTROY(FCapturedLog);
  1972. }
  1973. }
  1974. }
  1975. //---------------------------------------------------------------------------
  1976. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1977. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1978. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1979. {
  1980. DebugAssert(!IsLocalBrowserMode());
  1981. TStrings * LocalFileList = NULL;
  1982. TStrings * RemoteFileList = NULL;
  1983. TStrings * RemoteFileListFull = NULL;
  1984. try
  1985. {
  1986. if (LocalFileCommand)
  1987. {
  1988. if (ALocalFileList == NULL)
  1989. {
  1990. DebugAssert(HasDirView[osLocal]);
  1991. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1992. // directly (instead of EnableSelectedOperation) to pass
  1993. // false to FocusedFileOnlyWhenFocused
  1994. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1995. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1996. }
  1997. else
  1998. {
  1999. LocalFileList = ALocalFileList;
  2000. }
  2001. if (FileListCommand)
  2002. {
  2003. if (LocalFileList->Count != 1)
  2004. {
  2005. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  2006. }
  2007. }
  2008. else
  2009. {
  2010. if ((LocalFileList->Count != 1) &&
  2011. (FileList->Count != 1) &&
  2012. (LocalFileList->Count != FileList->Count))
  2013. {
  2014. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  2015. }
  2016. }
  2017. }
  2018. UnicodeString RootTempDir;
  2019. UnicodeString TempDir;
  2020. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  2021. if (!RemoteFiles)
  2022. {
  2023. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true, true);
  2024. }
  2025. try
  2026. {
  2027. TDateTimes RemoteFileTimes;
  2028. if (RemoteFiles)
  2029. {
  2030. RemoteFileList = FileList;
  2031. }
  2032. else
  2033. {
  2034. RemoteFileList = new TStringList();
  2035. TMakeLocalFileListParams MakeFileListParam;
  2036. MakeFileListParam.FileList = RemoteFileList;
  2037. MakeFileListParam.FileTimes = &RemoteFileTimes;
  2038. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2039. MakeFileListParam.Recursive =
  2040. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  2041. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2042. if (!MakeFileListParam.Recursive)
  2043. {
  2044. RemoteFileTimes.clear();
  2045. RemoteFileListFull = new TStringList();
  2046. MakeFileListParam.FileList = RemoteFileListFull;
  2047. MakeFileListParam.Recursive = true;
  2048. MakeFileListParam.IncludeDirs = false;
  2049. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2050. }
  2051. else
  2052. {
  2053. RemoteFileListFull = RemoteFileList;
  2054. }
  2055. }
  2056. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  2057. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2058. if (!NonBlocking)
  2059. {
  2060. int Count;
  2061. if (FileListCommand)
  2062. {
  2063. Count = 1;
  2064. }
  2065. else if (LocalFileCommand)
  2066. {
  2067. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  2068. }
  2069. else
  2070. {
  2071. Count = RemoteFileList->Count;
  2072. }
  2073. Progress.Start(foCustomCommand, osRemote, Count);
  2074. DebugAssert(FProgressForm != NULL);
  2075. FProgressForm->ReadOnly = true;
  2076. }
  2077. try
  2078. {
  2079. if (FileListCommand)
  2080. {
  2081. UnicodeString LocalFile;
  2082. // MakeFileList does not delimit filenames
  2083. UnicodeString FileList = MakeFileList(RemoteFileList);
  2084. if (LocalFileCommand)
  2085. {
  2086. DebugAssert(LocalFileList->Count == 1);
  2087. LocalFile = LocalFileList->Strings[0];
  2088. }
  2089. TLocalCustomCommand CustomCommand(Data,
  2090. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  2091. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  2092. if (NonBlocking)
  2093. {
  2094. DebugAssert(!POutput);
  2095. ExecuteShellChecked(ShellCommand);
  2096. }
  2097. else
  2098. {
  2099. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2100. }
  2101. }
  2102. else if (LocalFileCommand)
  2103. {
  2104. if (LocalFileList->Count == 1)
  2105. {
  2106. UnicodeString LocalFile = LocalFileList->Strings[0];
  2107. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2108. {
  2109. UnicodeString FileName = RemoteFileList->Strings[Index];
  2110. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2111. Progress.SetFile(FileNameForProgress);
  2112. TLocalCustomCommand CustomCommand(Data,
  2113. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2114. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2115. TOnceDoneOperation OnceDoneOperation;
  2116. // Do not unselect anything, as it won't work with two panels anyway
  2117. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2118. }
  2119. }
  2120. else if (RemoteFileList->Count == 1)
  2121. {
  2122. UnicodeString FileName = RemoteFileList->Strings[0];
  2123. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2124. {
  2125. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2126. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2127. Progress.SetFile(LocalFileNameOnly);
  2128. TLocalCustomCommand CustomCommand(
  2129. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2130. FileName, LocalFileName, 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
  2138. {
  2139. if (LocalFileList->Count != RemoteFileList->Count)
  2140. {
  2141. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2142. }
  2143. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2144. {
  2145. UnicodeString FileName = RemoteFileList->Strings[Index];
  2146. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2147. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2148. Progress.SetFile(LocalFileNameOnly); // sic
  2149. TLocalCustomCommand CustomCommand(
  2150. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2151. FileName, LocalFileName, L"");
  2152. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2153. TOnceDoneOperation OnceDoneOperation;
  2154. // Do not unselect anything, as it won't work with two panels anyway
  2155. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2156. }
  2157. }
  2158. }
  2159. else
  2160. {
  2161. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2162. {
  2163. UnicodeString FileName = RemoteFileList->Strings[Index];
  2164. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2165. Progress.SetFile(FileNameForProgress);
  2166. TLocalCustomCommand CustomCommand(Data,
  2167. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2168. FileName, L"", L"");
  2169. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2170. TOnceDoneOperation OnceDoneOperation;
  2171. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2172. }
  2173. }
  2174. }
  2175. __finally
  2176. {
  2177. if (!NonBlocking)
  2178. {
  2179. Progress.Stop();
  2180. }
  2181. }
  2182. DebugAssert(!FAutoOperation);
  2183. if (!RemoteFiles)
  2184. {
  2185. TempDir = IncludeTrailingBackslash(TempDir);
  2186. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2187. {
  2188. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2189. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2190. // Skip directories, process only nested files.
  2191. // The check is redundant as FileAge fails for directories anyway.
  2192. !DirectoryExists(FileName))
  2193. {
  2194. UnicodeString RemoteDir =
  2195. UnixExtractFileDir(
  2196. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2197. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2198. TDateTime NewTime;
  2199. if (FileAge(FileName, NewTime) &&
  2200. (NewTime != RemoteFileTimes[Index]))
  2201. {
  2202. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2203. TemporaryFileCopyParam(CopyParam);
  2204. CopyParam.FileMask = L"";
  2205. FAutoOperation = true;
  2206. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2207. TemporaryFilesList->Add(FileName);
  2208. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2209. }
  2210. }
  2211. }
  2212. }
  2213. }
  2214. __finally
  2215. {
  2216. FAutoOperation = false;
  2217. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2218. {
  2219. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2220. }
  2221. }
  2222. }
  2223. __finally
  2224. {
  2225. if (RemoteFileList != FileList)
  2226. {
  2227. delete RemoteFileList;
  2228. }
  2229. if (LocalFileList != ALocalFileList)
  2230. {
  2231. delete LocalFileList;
  2232. }
  2233. if (RemoteFileListFull != RemoteFileList)
  2234. {
  2235. delete RemoteFileListFull;
  2236. }
  2237. }
  2238. }
  2239. //---------------------------------------------------------------------------
  2240. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2241. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2242. bool FileListCommand, UnicodeString * POutput)
  2243. {
  2244. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2245. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2246. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2247. {
  2248. UnicodeString FileName = SelectedFileList->Strings[Index];
  2249. if (DirectoryExists(FileName))
  2250. {
  2251. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2252. {
  2253. LocalFileList->Add(FileName);
  2254. }
  2255. if (FLAGSET(ACommand.Params, ccRecursive))
  2256. {
  2257. TMakeLocalFileListParams MakeFileListParam;
  2258. MakeFileListParam.FileList = LocalFileList.get();
  2259. MakeFileListParam.FileTimes = NULL;
  2260. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2261. MakeFileListParam.Recursive = true;
  2262. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2263. }
  2264. }
  2265. else
  2266. {
  2267. LocalFileList->Add(FileName);
  2268. }
  2269. }
  2270. UnicodeString RemotePath;
  2271. if (Terminal != NULL)
  2272. {
  2273. RemotePath = Terminal->CurrentDirectory;
  2274. }
  2275. if (FileListCommand)
  2276. {
  2277. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2278. TLocalCustomCommand CustomCommand(
  2279. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2280. L"", L"", FileList);
  2281. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2282. }
  2283. else
  2284. {
  2285. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2286. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2287. DebugAssert(FProgressForm != NULL);
  2288. FProgressForm->ReadOnly = true;
  2289. try
  2290. {
  2291. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2292. {
  2293. UnicodeString FileName = LocalFileList->Strings[Index];
  2294. Progress.SetFile(FileName);
  2295. TLocalCustomCommand CustomCommand(
  2296. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2297. FileName, L"", L"");
  2298. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2299. TOnceDoneOperation OnceDoneOperation;
  2300. Progress.Finish(FileName, true, OnceDoneOperation);
  2301. }
  2302. }
  2303. __finally
  2304. {
  2305. Progress.Stop();
  2306. }
  2307. }
  2308. }
  2309. //---------------------------------------------------------------------------
  2310. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2311. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2312. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2313. {
  2314. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2315. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2316. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2317. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2318. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2319. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2320. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2321. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2322. std::unique_ptr<UnicodeString> POutput;
  2323. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2324. FLAGSET(ACommand.Params, ccCopyResults))
  2325. {
  2326. POutput.reset(new UnicodeString());
  2327. }
  2328. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2329. FProgressSide = FCurrentSide;
  2330. if (!LocalCustomCommand.IsFileCommand(Command))
  2331. {
  2332. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2333. }
  2334. // remote files?
  2335. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2336. {
  2337. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2338. }
  2339. // local files
  2340. else
  2341. {
  2342. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2343. }
  2344. if (POutput.get() != NULL)
  2345. {
  2346. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2347. int P = POutput->Pos(sLineBreak);
  2348. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2349. {
  2350. POutput->SetLength(P - 1);
  2351. }
  2352. // Copy even empty output to clipboard
  2353. if (FLAGSET(ACommand.Params, ccCopyResults))
  2354. {
  2355. CopyToClipboard(*POutput);
  2356. }
  2357. // But do not show an empty message box.
  2358. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2359. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2360. !POutput->IsEmpty())
  2361. {
  2362. TClipboardHandler ClipboardHandler;
  2363. ClipboardHandler.Text = *POutput;
  2364. TMessageParams Params;
  2365. TQueryButtonAlias Aliases[1];
  2366. Aliases[0].Button = qaRetry;
  2367. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2368. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2369. Params.Aliases = Aliases;
  2370. Params.AliasesCount = LENOF(Aliases);
  2371. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2372. }
  2373. }
  2374. }
  2375. //---------------------------------------------------------------------------
  2376. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2377. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2378. {
  2379. TCustomCommandData Data;
  2380. UnicodeString Site;
  2381. UnicodeString RemotePath;
  2382. if (HasActiveTerminal())
  2383. {
  2384. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2385. Data = TCustomCommandData(SessionData.get());
  2386. Site = Terminal->SessionData->SessionKey;
  2387. RemotePath = Terminal->CurrentDirectory;
  2388. }
  2389. UnicodeString HelpKeyword = ACommand.HomePage;
  2390. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2391. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2392. {
  2393. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2394. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2395. {
  2396. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2397. }
  2398. else
  2399. {
  2400. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2401. }
  2402. if (!DoCustomCommandOptionsDialog(
  2403. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2404. {
  2405. Abort();
  2406. }
  2407. }
  2408. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2409. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2410. {
  2411. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2412. }
  2413. else
  2414. {
  2415. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2416. }
  2417. }
  2418. //---------------------------------------------------------------------------
  2419. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2420. const TCustomCommandType & Command)
  2421. {
  2422. DebugAssert(FCustomCommandLocalFileList != NULL);
  2423. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2424. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2425. TStrings * LocalFileList = new TStringList();
  2426. TStrings * RemoteFileList = new TStringList();
  2427. try
  2428. {
  2429. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2430. {
  2431. LocalFileList->Clear();
  2432. LocalFileList->AddObject(
  2433. FCustomCommandLocalFileList->Strings[Index],
  2434. FCustomCommandLocalFileList->Objects[Index]);
  2435. RemoteFileList->Clear();
  2436. RemoteFileList->AddObject(
  2437. FCustomCommandRemoteFileList->Strings[Index],
  2438. FCustomCommandRemoteFileList->Objects[Index]);
  2439. CustomCommand(RemoteFileList, Command, LocalFileList);
  2440. }
  2441. }
  2442. __finally
  2443. {
  2444. delete LocalFileList;
  2445. delete RemoteFileList;
  2446. }
  2447. }
  2448. //---------------------------------------------------------------------------
  2449. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2450. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2451. {
  2452. delete FCustomCommandLocalFileList;
  2453. delete FCustomCommandRemoteFileList;
  2454. // takeover ownership,
  2455. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2456. FCustomCommandLocalFileList = LocalFileList;
  2457. FCustomCommandRemoteFileList = RemoteFileList;
  2458. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2459. if (Button != NULL)
  2460. {
  2461. FCustomCommandMenu->Items->Clear();
  2462. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2463. MenuPopup(FCustomCommandMenu, Button);
  2464. }
  2465. else
  2466. {
  2467. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2468. }
  2469. }
  2470. //---------------------------------------------------------------------------
  2471. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2472. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2473. {
  2474. DebugAssert(FCapturedLog != NULL);
  2475. if ((OutputType == cotOutput) || (OutputType == cotError))
  2476. {
  2477. FCapturedLog->Add(AddedLine);
  2478. }
  2479. }
  2480. //---------------------------------------------------------------------------
  2481. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2482. TOperationSide Side)
  2483. {
  2484. return
  2485. (Side == osOther) &&
  2486. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2487. }
  2488. //---------------------------------------------------------------------------
  2489. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2490. TOperationSide Side, TTBXCustomItem * Item,
  2491. TResolvedDoubleClickAction DoubleClickAction1, TResolvedDoubleClickAction DoubleClickAction2)
  2492. {
  2493. TCustomDirView * DView = DirView(Side);
  2494. bool IsDefault = false;
  2495. if (DView->ItemFocused != NULL)
  2496. {
  2497. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  2498. TResolvedDoubleClickAction DefaultAction =
  2499. WinConfiguration->ResolveDoubleClickAction(DView->ItemIsDirectory(DView->ItemFocused), ATerminal);
  2500. IsDefault = (DefaultAction == DoubleClickAction1) || (DefaultAction == DoubleClickAction2);
  2501. }
  2502. TTBItemOptions O = Item->Options;
  2503. if (IsDefault)
  2504. {
  2505. Item->Options = O << tboDefault;
  2506. }
  2507. else
  2508. {
  2509. Item->Options = O >> tboDefault;
  2510. }
  2511. }
  2512. //---------------------------------------------------------------------------
  2513. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2514. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2515. {
  2516. TCustomDirView * DView = DirView(Side);
  2517. TListItem * Item = DView->ItemFocused;
  2518. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2519. EnableFocusedOperation[Side])
  2520. {
  2521. TPoint ClientPoint;
  2522. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2523. TPoint(0, 0) : MousePos;
  2524. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2525. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2526. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2527. }
  2528. }
  2529. //---------------------------------------------------------------------------
  2530. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2531. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2532. {
  2533. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, rdcaChangeDir, rdcaOpen);
  2534. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, rdcaEdit, rdcaNone);
  2535. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, rdcaCopy, rdcaNone);
  2536. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2537. Handled = true;
  2538. }
  2539. //---------------------------------------------------------------------------
  2540. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2541. {
  2542. }
  2543. //---------------------------------------------------------------------------
  2544. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2545. {
  2546. DebugAssert(FErrorList == NULL);
  2547. if (WinConfiguration->ContinueOnError)
  2548. {
  2549. FErrorList = new TStringList();
  2550. Configuration->Usage->Inc(L"ContinuationsOnError");
  2551. }
  2552. NonVisualDataModule->StartBusy();
  2553. }
  2554. //---------------------------------------------------------------------------
  2555. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2556. {
  2557. NonVisualDataModule->EndBusy();
  2558. if (FErrorList)
  2559. {
  2560. HandleErrorList(FErrorList);
  2561. }
  2562. }
  2563. //---------------------------------------------------------------------------
  2564. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2565. const TCopyParamType & CopyParam)
  2566. {
  2567. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2568. {
  2569. Configuration->Usage->Inc(L"FileMaskUses");
  2570. }
  2571. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2572. {
  2573. Configuration->Usage->Inc(L"OperationMaskUses");
  2574. }
  2575. }
  2576. //---------------------------------------------------------------------------
  2577. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2578. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2579. {
  2580. DebugAssert(!IsLocalBrowserMode());
  2581. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2582. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2583. TTransferOperationParam DefaultParam;
  2584. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2585. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2586. switch (Param.Queue)
  2587. {
  2588. case asOn:
  2589. CopyParam.Queue = true;
  2590. break;
  2591. case asOff:
  2592. CopyParam.Queue = false;
  2593. break;
  2594. case asAuto:
  2595. default:
  2596. // keep default
  2597. break;
  2598. }
  2599. bool Result =
  2600. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2601. if (Result)
  2602. {
  2603. DebugAssert(Terminal);
  2604. bool SelectionRestored = false;
  2605. TCustomDirView * DView = NULL;
  2606. if (HasDirView[Side])
  2607. {
  2608. DView = DirView(Side);
  2609. DView->SaveSelection();
  2610. DView->SaveSelectedNames();
  2611. }
  2612. UpdateCopyParamCounters(CopyParam);
  2613. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2614. DebugAssert(FTransferResumeList == NULL);
  2615. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2616. FMoveToQueue = false;
  2617. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2618. try
  2619. {
  2620. TStrings * PermanentFileList;
  2621. std::unique_ptr<TStrings> PermanentFileListOwner;
  2622. try
  2623. {
  2624. if (Side == osLocal)
  2625. {
  2626. PermanentFileList = FileList;
  2627. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2628. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2629. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2630. if (Operation == foMove)
  2631. {
  2632. ReloadLocalDirectory();
  2633. if (DView != NULL)
  2634. {
  2635. DView->RestoreSelection();
  2636. }
  2637. SelectionRestored = true;
  2638. }
  2639. }
  2640. else
  2641. {
  2642. // Clone the file list as it may refer to current directory files,
  2643. // which get destroyed, when the source directory is reloaded after foMove operation.
  2644. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2645. // But for a hotfix, we are not going to do such a big change.
  2646. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2647. PermanentFileList = PermanentFileListOwner.get();
  2648. try
  2649. {
  2650. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2651. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2652. }
  2653. __finally
  2654. {
  2655. if (Operation == foMove)
  2656. {
  2657. if (DView != NULL)
  2658. {
  2659. DView->RestoreSelection();
  2660. }
  2661. SelectionRestored = true;
  2662. }
  2663. ReloadLocalDirectory(Param.TargetDirectory);
  2664. }
  2665. }
  2666. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2667. if (FMoveToQueue)
  2668. {
  2669. Abort();
  2670. }
  2671. }
  2672. catch (EAbort &)
  2673. {
  2674. if (FMoveToQueue)
  2675. {
  2676. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2677. DebugAssert(CopyParam.TransferSkipList == NULL);
  2678. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2679. if (TransferResumeList->Count > 0)
  2680. {
  2681. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2682. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2683. }
  2684. CopyParam.TransferSkipList = TransferResumeList.release();
  2685. // not really needed, just to keep it consistent with TransferResumeList
  2686. FTransferResumeList = NULL;
  2687. FMoveToQueue = false;
  2688. Configuration->Usage->Inc("MovesToBackground");
  2689. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2690. ClearTransferSourceSelection(Direction);
  2691. }
  2692. throw;
  2693. }
  2694. }
  2695. __finally
  2696. {
  2697. if (!SelectionRestored && (DView != NULL))
  2698. {
  2699. DView->DiscardSavedSelection();
  2700. }
  2701. FTransferResumeList = NULL;
  2702. }
  2703. }
  2704. return Result;
  2705. }
  2706. //---------------------------------------------------------------------------
  2707. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2708. TOperationSide Side, TStrings * FileList, void * Param)
  2709. {
  2710. DebugAssert(FileList->Count);
  2711. // We deliberately do not toggle alternative flag (Param), but use OR,
  2712. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2713. // shortcut of CurrentDeleteAlternativeAction
  2714. bool Alternative =
  2715. bool(Param) || UseAlternativeFunction();
  2716. bool Recycle;
  2717. if (IsSideLocalBrowser(Side))
  2718. {
  2719. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2720. }
  2721. else
  2722. {
  2723. Recycle =
  2724. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2725. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2726. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2727. }
  2728. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2729. if (!Result)
  2730. {
  2731. UnicodeString Query;
  2732. if (FileList->Count == 1)
  2733. {
  2734. if (IsSideLocalBrowser(Side))
  2735. {
  2736. Query = ExtractFileName(FileList->Strings[0]);
  2737. }
  2738. else
  2739. {
  2740. Query = UnixExtractFileName(FileList->Strings[0]);
  2741. }
  2742. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2743. }
  2744. else
  2745. {
  2746. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2747. }
  2748. TMessageParams Params(mpNeverAskAgainCheck);
  2749. Params.ImageName = L"Delete file";
  2750. unsigned int Answer =
  2751. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2752. if (Answer == qaNeverAskAgain)
  2753. {
  2754. Result = true;
  2755. if (Recycle)
  2756. {
  2757. WinConfiguration->ConfirmRecycling = false;
  2758. }
  2759. else
  2760. {
  2761. WinConfiguration->ConfirmDeleting = false;
  2762. }
  2763. }
  2764. else
  2765. {
  2766. Result = (Answer == qaOK);
  2767. }
  2768. }
  2769. if (Result)
  2770. {
  2771. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2772. }
  2773. return Result;
  2774. }
  2775. //---------------------------------------------------------------------------
  2776. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2777. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2778. {
  2779. TAutoBatch AutoBatch(this);
  2780. bool Result;
  2781. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2782. {
  2783. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2784. }
  2785. else if (Operation == foRename)
  2786. {
  2787. DebugAssert(DirView(Side)->ItemFocused);
  2788. DirView(Side)->ItemFocused->EditCaption();
  2789. Result = true;
  2790. }
  2791. else if (Operation == foDelete)
  2792. {
  2793. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2794. }
  2795. else if (Operation == foSetProperties)
  2796. {
  2797. DirView(osRemote)->SaveSelectedNames();
  2798. Result = SetProperties(Side, FileList);
  2799. }
  2800. else if (Operation == foCustomCommand)
  2801. {
  2802. DebugAssert(Param);
  2803. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2804. DirView(osRemote)->SaveSelectedNames();
  2805. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2806. CustomCommand(FileList, *Command, NULL);
  2807. Result = true;
  2808. }
  2809. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2810. {
  2811. DebugAssert(!IsSideLocalBrowser(Side));
  2812. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2813. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2814. }
  2815. else if (Operation == foLock)
  2816. {
  2817. DebugAssert(!IsSideLocalBrowser(Side));
  2818. LockFiles(FileList, true);
  2819. Result = true;
  2820. }
  2821. else if (Operation == foUnlock)
  2822. {
  2823. DebugAssert(!IsSideLocalBrowser(Side));
  2824. LockFiles(FileList, false);
  2825. Result = true;
  2826. }
  2827. else
  2828. {
  2829. DebugFail();
  2830. }
  2831. return Result;
  2832. }
  2833. //---------------------------------------------------------------------------
  2834. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2835. {
  2836. if (Side == osCurrent)
  2837. {
  2838. Side = FCurrentSide;
  2839. }
  2840. return Side;
  2841. }
  2842. //---------------------------------------------------------------------------
  2843. TOperationSide TCustomScpExplorerForm::GetOtherSide(TOperationSide Side)
  2844. {
  2845. TOperationSide Result;
  2846. switch (GetSide(Side))
  2847. {
  2848. case osLocal:
  2849. Result = osOther;
  2850. break;
  2851. case osOther:
  2852. Result = osLocal;
  2853. break;
  2854. default:
  2855. DebugFail();
  2856. Abort();
  2857. }
  2858. return Result;
  2859. }
  2860. //---------------------------------------------------------------------------
  2861. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2862. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2863. {
  2864. Side = GetSide(Side);
  2865. bool FullPath = IsSideLocalBrowser(Side);
  2866. TCustomDirView * ADirView = DirView(Side);
  2867. std::unique_ptr<TStringList> FileList(new TStringList());
  2868. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2869. bool ReloadProperties =
  2870. !IsSideLocalBrowser(Side) && (Operation == foSetProperties) &&
  2871. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2872. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2873. if (Result && ReloadProperties)
  2874. {
  2875. std::unique_ptr<TStringList> FileList2(new TStringList());
  2876. FileList->CaseSensitive = true;
  2877. FileList->Sort();
  2878. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2879. {
  2880. TListItem * Item = ADirView->Items->Item[Index];
  2881. UnicodeString FileName = ADirView->ItemFileName(Item);
  2882. int Index2;
  2883. if (FileList->Find(FileName, Index2))
  2884. {
  2885. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2886. }
  2887. }
  2888. LoadFilesProperties(FileList2.get());
  2889. }
  2890. return Result;
  2891. }
  2892. //---------------------------------------------------------------------------
  2893. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2894. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2895. bool NoConfirmation, void * Param)
  2896. {
  2897. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2898. {
  2899. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2900. {
  2901. DebugAssert(!IsLocalBrowserMode());
  2902. if (GetSide(Side) == osLocal)
  2903. {
  2904. Configuration->Usage->Inc(L"UploadsCommand");
  2905. }
  2906. else
  2907. {
  2908. Configuration->Usage->Inc(L"DownloadsCommand");
  2909. }
  2910. }
  2911. }
  2912. }
  2913. //---------------------------------------------------------------------------
  2914. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2915. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2916. {
  2917. TTransferOperationParam Param;
  2918. if ((WinConfiguration->Interface != ifCommander) ||
  2919. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2920. {
  2921. Flags &= ~cocShortCutHint;
  2922. }
  2923. TCustomDirView * DView = DirView(Side);
  2924. Param.Options =
  2925. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2926. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2927. if (FLAGSET(Flags, cocQueue))
  2928. {
  2929. Param.Queue = asOn;
  2930. }
  2931. else if (FLAGSET(Flags, cocNonQueue))
  2932. {
  2933. Param.Queue = asOff;
  2934. }
  2935. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2936. }
  2937. //---------------------------------------------------------------------------
  2938. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2939. {
  2940. int Count = ErrorList->Count;
  2941. try
  2942. {
  2943. if (ErrorList->Count)
  2944. {
  2945. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2946. if (MessageDialog(Message, qtError,
  2947. qaOK | qaCancel, HELP_NONE) == qaOK)
  2948. {
  2949. unsigned int Answer;
  2950. int Index = 0;
  2951. do
  2952. {
  2953. DebugAssert(Index >= 0 && Index < Count);
  2954. TQueryButtonAlias Aliases[2];
  2955. Aliases[0].Button = qaYes;
  2956. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2957. Aliases[1].Button = qaNo;
  2958. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2959. TMessageParams Params;
  2960. Params.Aliases = Aliases;
  2961. Params.AliasesCount = LENOF(Aliases);
  2962. int No = Index + 1;
  2963. UnicodeString Message = ErrorList->Strings[Index];
  2964. UnicodeString AMainInstructions;
  2965. UnicodeString Details;
  2966. if (ExtractMainInstructions(Message, AMainInstructions))
  2967. {
  2968. Details = Message;
  2969. Message = AMainInstructions;
  2970. }
  2971. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2972. Answer = MoreMessageDialog(
  2973. Message,
  2974. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2975. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2976. qaOK, HELP_NONE, &Params);
  2977. if (Answer == qaNo)
  2978. {
  2979. Index++;
  2980. }
  2981. if (Answer == qaYes)
  2982. {
  2983. Index--;
  2984. }
  2985. }
  2986. while (Answer != qaOK);
  2987. }
  2988. }
  2989. }
  2990. __finally
  2991. {
  2992. TStrings * List = ErrorList;
  2993. ErrorList = NULL;
  2994. for (int i = 0; i < Count; i++)
  2995. {
  2996. delete List->Objects[i];
  2997. }
  2998. delete List;
  2999. }
  3000. }
  3001. //---------------------------------------------------------------------------
  3002. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  3003. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  3004. {
  3005. // needed for checking filemasks, as there's no directory object
  3006. // associated with the file object
  3007. File->FullFileName = FullFileName;
  3008. TFileMasks::TParams MaskParams;
  3009. MaskParams.Size = File->Size;
  3010. MaskParams.Modification = File->Modification;
  3011. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  3012. }
  3013. //---------------------------------------------------------------------------
  3014. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  3015. {
  3016. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3017. Side = GetSide(Side);
  3018. TCustomDirView * DView = DirView(Side);
  3019. TListItem * FocusedItem = DView->ItemFocused;
  3020. UnicodeString Name;
  3021. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  3022. {
  3023. Name = DView->ItemFileName(FocusedItem);
  3024. }
  3025. else
  3026. {
  3027. Name = LoadStr(NEW_FILE);
  3028. }
  3029. UnicodeString Names = Name;
  3030. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  3031. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  3032. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  3033. {
  3034. while (!Names.IsEmpty())
  3035. {
  3036. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  3037. CustomWinConfiguration->History[L"EditFile"] = History.get();
  3038. UnicodeString TargetFileName;
  3039. UnicodeString LocalFileName;
  3040. UnicodeString RootTempDir;
  3041. UnicodeString TempDir;
  3042. UnicodeString RemoteDirectory;
  3043. bool ExistingFile = false;
  3044. if (!IsSideLocalBrowser(Side))
  3045. {
  3046. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  3047. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(Name));
  3048. if ((File.get() != NULL) &&
  3049. !File->IsDirectory)
  3050. {
  3051. ExecuteRemoteFile(Name, File.get(), efDefaultEditor);
  3052. ExistingFile = true;
  3053. }
  3054. File.reset(NULL);
  3055. if (!ExistingFile)
  3056. {
  3057. RemoteDirectory = UnixExtractFilePath(Name);
  3058. TemporaryDirectoryForRemoteFiles(
  3059. RemoteDirectory, GUIConfiguration->CurrentCopyParam, false, TempDir, RootTempDir);
  3060. TargetFileName = UnixExtractFileName(Name);
  3061. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3062. LocalFileName = TempDir +
  3063. // We probably do not want to trim the VMS version here
  3064. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  3065. }
  3066. }
  3067. else
  3068. {
  3069. if (ExtractFilePath(Name).IsEmpty())
  3070. {
  3071. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  3072. }
  3073. else
  3074. {
  3075. LocalFileName = ExpandFileName(Name);
  3076. }
  3077. TargetFileName = ExtractFileName(Name);
  3078. }
  3079. if (!ExistingFile)
  3080. {
  3081. bool NewFile = !FileExists(ApiPath(LocalFileName));
  3082. if (NewFile)
  3083. {
  3084. int File = FileCreate(ApiPath(LocalFileName));
  3085. if (File < 0)
  3086. {
  3087. if (!RootTempDir.IsEmpty())
  3088. {
  3089. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3090. }
  3091. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  3092. }
  3093. else
  3094. {
  3095. FileClose(File);
  3096. }
  3097. }
  3098. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  3099. const TEditorData * ExternalEditor = NULL;
  3100. TFileMasks::TParams MaskParams; // size not known
  3101. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  3102. false, MaskParams);
  3103. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  3104. ExternalEditor, RootTempDir, RemoteDirectory, NewFile, TDateTime());
  3105. }
  3106. }
  3107. }
  3108. }
  3109. //---------------------------------------------------------------------------
  3110. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  3111. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  3112. {
  3113. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3114. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3115. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3116. return
  3117. ((ExecuteFileBy == efInternalEditor)) ||
  3118. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  3119. }
  3120. //---------------------------------------------------------------------------
  3121. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  3122. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  3123. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  3124. UnicodeString RemoteDirectory, bool NewFile, const TDateTime & SourceTimestamp)
  3125. {
  3126. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3127. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3128. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3129. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3130. Side = GetSide(Side);
  3131. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3132. if (!IsSideLocalBrowser(Side))
  3133. {
  3134. Data->Terminal = Terminal;
  3135. Data->Queue = Queue;
  3136. Data->SessionData = CloneCurrentSessionData();
  3137. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3138. Data->RemoteDirectory = RemoteDirectory;
  3139. Data->SessionName = Terminal->SessionData->SessionName;
  3140. Data->LocalRootDirectory = LocalRootDirectory;
  3141. Data->OriginalFileName = OriginalFileName;
  3142. Data->Command = L""; // will be changed later for external editor
  3143. // Empty SourceTimestamp indicates that checking was disabled when the file was opened
  3144. if (WinConfiguration->EditorCheckNotModified)
  3145. {
  3146. Data->SourceTimestamp = SourceTimestamp;
  3147. }
  3148. }
  3149. if (ExecuteFileBy == efInternalEditor)
  3150. {
  3151. if (!IsSideLocalBrowser(Side))
  3152. {
  3153. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3154. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3155. TForm * Editor;
  3156. try
  3157. {
  3158. Editor =
  3159. ShowEditorForm(
  3160. FileName, this,
  3161. FEditorManager->FileChanged, FEditorManager->FileReload, FEditorManager->FileClosed,
  3162. SaveAllInternalEditors, AnyInternalEditorModified,
  3163. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3164. }
  3165. catch(...)
  3166. {
  3167. if (!LocalRootDirectory.IsEmpty())
  3168. {
  3169. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  3170. }
  3171. throw;
  3172. }
  3173. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3174. AppLogFmt(L"Opened internal editor with \"%s\"", (FileName));
  3175. }
  3176. else
  3177. {
  3178. DebugAssert(!StandaloneOperation);
  3179. TForm * Editor =
  3180. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3181. SaveAllInternalEditors, AnyInternalEditorModified,
  3182. L"", false, SessionColor, -1, NewFile);
  3183. FLocalEditors->Add(Editor);
  3184. }
  3185. }
  3186. else
  3187. {
  3188. HANDLE Process;
  3189. if (ExecuteFileBy == efExternalEditor)
  3190. {
  3191. UnicodeString Program, Params, Dir;
  3192. Data->Command = ExternalEditor->ExternalEditor;
  3193. ReformatFileNameCommand(Data->Command);
  3194. SplitCommand(Data->Command, Program, Params, Dir);
  3195. Params = ExpandFileNameCommand(Params, FileName);
  3196. Program = ExpandEnvironmentVariables(Program);
  3197. if (!ExecuteShell(Program, Params, Process))
  3198. {
  3199. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3200. }
  3201. }
  3202. else
  3203. {
  3204. DebugAssert(!IsSideLocalBrowser(Side));
  3205. if (!ExecuteShell(FileName, L"", Process))
  3206. {
  3207. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3208. }
  3209. }
  3210. if (IsSideLocalBrowser(Side) ||
  3211. ((ExecuteFileBy == efShell) &&
  3212. !WinConfiguration->Editor.SDIShellEditor) ||
  3213. ((ExecuteFileBy == efExternalEditor) &&
  3214. !ExternalEditor->SDIExternalEditor))
  3215. {
  3216. // no need for handle
  3217. if (Process != NULL)
  3218. {
  3219. DebugCheck(CloseHandle(Process));
  3220. }
  3221. Process = INVALID_HANDLE_VALUE;
  3222. }
  3223. else
  3224. {
  3225. if (Process == NULL)
  3226. {
  3227. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3228. }
  3229. }
  3230. if (!IsSideLocalBrowser(Side))
  3231. {
  3232. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3233. AppLogFmt(L"Opened external editor with remote file \"%s\".", (FileName));
  3234. }
  3235. }
  3236. }
  3237. //---------------------------------------------------------------------------
  3238. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3239. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3240. void * /*Arg*/)
  3241. {
  3242. if (Token != NULL)
  3243. {
  3244. EditorFormFileSave(static_cast<TForm *>(Token));
  3245. }
  3246. }
  3247. //---------------------------------------------------------------------------
  3248. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3249. {
  3250. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3251. {
  3252. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3253. }
  3254. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3255. }
  3256. //---------------------------------------------------------------------------
  3257. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3258. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3259. void * Arg)
  3260. {
  3261. if ((Token != NULL) &&
  3262. IsEditorFormModified(static_cast<TForm *>(Token)))
  3263. {
  3264. *static_cast<bool *>(Arg) = true;
  3265. }
  3266. }
  3267. //---------------------------------------------------------------------------
  3268. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3269. TObject * /*Sender*/, bool & Modified)
  3270. {
  3271. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3272. {
  3273. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3274. {
  3275. Modified = true;
  3276. }
  3277. }
  3278. if (!Modified)
  3279. {
  3280. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3281. }
  3282. }
  3283. //---------------------------------------------------------------------------
  3284. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3285. {
  3286. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3287. }
  3288. //---------------------------------------------------------------------------
  3289. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3290. const UnicodeString & RemoteDirectory, const TCopyParamType & CopyParam, bool Simple,
  3291. UnicodeString & Result, UnicodeString & RootDirectory)
  3292. {
  3293. if (!WinConfiguration->TemporaryDirectoryDeterministic || Simple)
  3294. {
  3295. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3296. Result = RootDirectory;
  3297. }
  3298. else
  3299. {
  3300. RootDirectory = L"";
  3301. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3302. Result = IncludeTrailingBackslash(Result);
  3303. }
  3304. if (WinConfiguration->TemporaryDirectoryAppendSession && !Simple)
  3305. {
  3306. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3307. }
  3308. if (WinConfiguration->TemporaryDirectoryAppendPath && !Simple)
  3309. {
  3310. UnicodeString ARemoteDirectory = RemoteDirectory;
  3311. if (!ARemoteDirectory.IsEmpty() && (ARemoteDirectory[1] == L'/'))
  3312. {
  3313. ARemoteDirectory.Delete(1, 1);
  3314. }
  3315. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(ARemoteDirectory)));
  3316. }
  3317. if (!ForceDirectories(ApiPath(Result)))
  3318. {
  3319. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3320. }
  3321. }
  3322. //---------------------------------------------------------------------------
  3323. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3324. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3325. bool GetTargetNames, bool AutoOperation, bool SimpleTempDir)
  3326. {
  3327. DebugAssert(!IsLocalBrowserMode());
  3328. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3329. if (ForceText)
  3330. {
  3331. CopyParam.TransferMode = tmAscii;
  3332. }
  3333. TemporaryFileCopyParam(CopyParam);
  3334. if (TempDir.IsEmpty())
  3335. {
  3336. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, SimpleTempDir, TempDir, RootTempDir);
  3337. }
  3338. DebugAssert(!FAutoOperation);
  3339. FAutoOperation = AutoOperation;
  3340. Terminal->ExceptionOnFail = true;
  3341. try
  3342. {
  3343. try
  3344. {
  3345. // turn off confirmations, as for MDI editors we may possibly download
  3346. // the same file over
  3347. Terminal->CopyToLocal(
  3348. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3349. if (GetTargetNames)
  3350. {
  3351. for (int i = 0; i < FileList->Count; i++)
  3352. {
  3353. FileList->Strings[i] =
  3354. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3355. }
  3356. }
  3357. }
  3358. catch(...)
  3359. {
  3360. if (!RootTempDir.IsEmpty())
  3361. {
  3362. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3363. }
  3364. throw;
  3365. }
  3366. }
  3367. __finally
  3368. {
  3369. FAutoOperation = false;
  3370. Terminal->ExceptionOnFail = false;
  3371. }
  3372. }
  3373. //---------------------------------------------------------------------------
  3374. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3375. {
  3376. // Do not waste time checking for default editor list the next time,
  3377. // or testing if default editor is not notepad.
  3378. bool TryNextTime = false;
  3379. UnicodeString UsageState;
  3380. if (!WinConfiguration->EditorList->IsDefaultList())
  3381. {
  3382. UsageState = "H";
  3383. }
  3384. else
  3385. {
  3386. UnicodeString Executable;
  3387. UnicodeString ExecutableDescription;
  3388. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3389. {
  3390. UnicodeString Message =
  3391. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3392. unsigned int Answer =
  3393. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3394. if (Answer != qaOK)
  3395. {
  3396. UsageState = "R";
  3397. }
  3398. else
  3399. {
  3400. UsageState = "A";
  3401. TEditorData EditorData;
  3402. EditorData.Editor = edExternal;
  3403. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3404. EditorData.ExternalEditorOptionsAutodetect();
  3405. TEditorList EditorList;
  3406. EditorList = *WinConfiguration->EditorList;
  3407. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3408. WinConfiguration->EditorList = &EditorList;
  3409. }
  3410. }
  3411. }
  3412. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3413. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3414. }
  3415. //---------------------------------------------------------------------------
  3416. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3417. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3418. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3419. {
  3420. if (ExecuteFileBy == efDefaultEditor)
  3421. {
  3422. if (!WinConfiguration->OfferedEditorAutoConfig)
  3423. {
  3424. EditorAutoConfig();
  3425. }
  3426. const TEditorPreferences * Editor =
  3427. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3428. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3429. {
  3430. ExecuteFileBy = efInternalEditor;
  3431. ExternalEditor = NULL;
  3432. }
  3433. else if (Editor->Data->Editor == edOpen)
  3434. {
  3435. ExecuteFileBy = efShell;
  3436. ExternalEditor = NULL;
  3437. }
  3438. else
  3439. {
  3440. ExecuteFileBy = efExternalEditor;
  3441. ExternalEditor = Editor->Data;
  3442. }
  3443. }
  3444. }
  3445. //---------------------------------------------------------------------------
  3446. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3447. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3448. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3449. {
  3450. UnicodeString OriginalFileName;
  3451. UnicodeString LocalRootDirectory;
  3452. UnicodeString RemoteDirectory;
  3453. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3454. IsSideLocalBrowser(Side), MaskParams);
  3455. UnicodeString Counter;
  3456. UnicodeString LocalFileName;
  3457. bool Handled = false;
  3458. if (!IsSideLocalBrowser(Side))
  3459. {
  3460. // We need to trim VMS version here, so that we use name without version
  3461. // when uploading back to create a new version of the file
  3462. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3463. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3464. TObject * Token = NULL;
  3465. UnicodeString LocalDirectory;
  3466. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3467. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3468. LocalDirectory))
  3469. {
  3470. if (Token != NULL)
  3471. {
  3472. TForm * Form = dynamic_cast<TForm *>(Token);
  3473. if (Form->WindowState == wsMinimized)
  3474. {
  3475. ShowWindow(Form->Handle, SW_RESTORE);
  3476. }
  3477. else
  3478. {
  3479. Form->SetFocus();
  3480. }
  3481. Handled = true;
  3482. }
  3483. else
  3484. {
  3485. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3486. }
  3487. }
  3488. if (!Handled)
  3489. {
  3490. TStringList * FileList1 = new TStringList();
  3491. try
  3492. {
  3493. FileList1->AddObject(FullFileName, Object);
  3494. TemporarilyDownloadFiles(FileList1,
  3495. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3496. LocalRootDirectory, LocalDirectory, true, true, false);
  3497. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3498. }
  3499. __finally
  3500. {
  3501. delete FileList1;
  3502. }
  3503. switch (ExecuteFileBy)
  3504. {
  3505. case efShell:
  3506. Counter = "RemoteFilesExecuted";
  3507. break;
  3508. case efInternalEditor:
  3509. Counter = "RemoteFilesOpenedInInternalEditor";
  3510. break;
  3511. case efExternalEditor:
  3512. Counter = "RemoteFilesOpenedInExternalEditor";
  3513. break;
  3514. default:
  3515. DebugFail();
  3516. }
  3517. }
  3518. }
  3519. else
  3520. {
  3521. LocalFileName = FullFileName;
  3522. OriginalFileName = ExtractFileName(FullFileName);
  3523. switch (ExecuteFileBy)
  3524. {
  3525. case efShell:
  3526. Counter = "LocalFilesExecuted";
  3527. break;
  3528. case efInternalEditor:
  3529. Counter = "LocalFilesOpenedInInternalEditor";
  3530. break;
  3531. case efExternalEditor:
  3532. Counter = "LocalFilesOpenedInExternalEditor";
  3533. break;
  3534. default:
  3535. DebugFail();
  3536. }
  3537. }
  3538. if (!Handled)
  3539. {
  3540. Configuration->Usage->Inc(Counter);
  3541. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3542. ExternalEditor, LocalRootDirectory, RemoteDirectory, false, MaskParams.Modification);
  3543. }
  3544. }
  3545. //---------------------------------------------------------------------------
  3546. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3547. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3548. bool AllSelected, bool OnFocused)
  3549. {
  3550. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3551. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3552. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3553. Side = GetSide(Side);
  3554. TCustomDirView * DView = DirView(Side);
  3555. TStrings * FileList =
  3556. AllSelected ?
  3557. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3558. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3559. try
  3560. {
  3561. DebugAssert(AllSelected || (FileList->Count == 1));
  3562. for (int i = 0; i < FileList->Count; i++)
  3563. {
  3564. UnicodeString ListFileName = FileList->Strings[i];
  3565. UnicodeString FileNameOnly =
  3566. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3567. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3568. if (!DView->ItemIsDirectory(Item))
  3569. {
  3570. UnicodeString FullFileName;
  3571. if (!IsSideLocalBrowser(Side))
  3572. {
  3573. FullFileName = DirView(Side)->Path + ListFileName;
  3574. }
  3575. else
  3576. {
  3577. FullFileName = ListFileName;
  3578. }
  3579. TObject * Object = FileList->Objects[i];
  3580. TFileMasks::TParams MaskParams;
  3581. MaskParams.Size = DView->ItemFileSize(Item);
  3582. TDateTimePrecision Precision;
  3583. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3584. std::unique_ptr<TRemoteFile> File;
  3585. if (!IsSideLocalBrowser(Side))
  3586. {
  3587. File.reset(Terminal->TryReadFile(FullFileName));
  3588. if (File.get() != NULL)
  3589. {
  3590. File->FullFileName = FullFileName;
  3591. Object = File.get();
  3592. MaskParams.Size = File->Size;
  3593. MaskParams.Modification = File->Modification;
  3594. }
  3595. }
  3596. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName, Object, MaskParams);
  3597. }
  3598. }
  3599. }
  3600. __finally
  3601. {
  3602. delete FileList;
  3603. }
  3604. }
  3605. //---------------------------------------------------------------------------
  3606. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3607. {
  3608. CopyParam.FileNameCase = ncNoChange;
  3609. CopyParam.PreserveRights = false;
  3610. CopyParam.PreserveReadOnly = false;
  3611. CopyParam.ReplaceInvalidChars = true;
  3612. CopyParam.IncludeFileMask = TFileMasks();
  3613. CopyParam.NewerOnly = false;
  3614. CopyParam.FileMask = L"";
  3615. }
  3616. //---------------------------------------------------------------------------
  3617. bool TCustomScpExplorerForm::EditorCheckNotModified(const TEditedFileData * Data)
  3618. {
  3619. return WinConfiguration->EditorCheckNotModified && (Data->SourceTimestamp != TDateTime());
  3620. }
  3621. //---------------------------------------------------------------------------
  3622. class TEditorUploadQueueItem : public TUploadQueueItem
  3623. {
  3624. public:
  3625. __fastcall TEditorUploadQueueItem(
  3626. TTerminal * Terminal, TStrings * FilesToCopy, const UnicodeString & TargetDir,
  3627. const TCopyParamType * CopyParam, int Params) :
  3628. TUploadQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, true, false)
  3629. {
  3630. }
  3631. protected:
  3632. virtual void __fastcall DoTransferExecute(TTerminal * Terminal, TParallelOperation * ParallelOperation)
  3633. {
  3634. TUploadQueueItem::DoTransferExecute(Terminal, ParallelOperation);
  3635. TTerminalManager::Instance()->ScpExplorer->EditedFileUploaded(Terminal, CompleteEvent);
  3636. }
  3637. };
  3638. //---------------------------------------------------------------------------
  3639. void TCustomScpExplorerForm::EditedFileUploaded(TTerminal * ATerminal, HANDLE UploadCompleteEvent)
  3640. {
  3641. if (WinConfiguration->EditorCheckNotModified) // optimization
  3642. {
  3643. UnicodeString RemoteFilePath;
  3644. {
  3645. TGuard Guard(FEditorManager->Section);
  3646. TEditedFileData * Data = FEditorManager->FindByUploadCompleteEvent(UploadCompleteEvent);
  3647. if (DebugAlwaysTrue(Data != NULL) &&
  3648. EditorCheckNotModified(Data))
  3649. {
  3650. RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3651. }
  3652. }
  3653. if (!RemoteFilePath.IsEmpty())
  3654. {
  3655. std::unique_ptr<TRemoteFile> File(ATerminal->TryReadFile(RemoteFilePath));
  3656. if (File.get() != NULL)
  3657. {
  3658. TGuard Guard(FEditorManager->Section);
  3659. TEditedFileData * Data = FEditorManager->FindByUploadCompleteEvent(UploadCompleteEvent);
  3660. if (DebugAlwaysTrue(Data != NULL))
  3661. {
  3662. Data->SourceTimestamp = File->Modification;
  3663. AppLogFmt(L"Uploaded edited remote file timestamp: %s: ", (StandardTimestamp(Data->SourceTimestamp)));
  3664. }
  3665. }
  3666. }
  3667. }
  3668. }
  3669. //---------------------------------------------------------------------------
  3670. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
  3671. const UnicodeString & FileName, TEditedFileData * Data, HANDLE UploadCompleteEvent, bool & Retry)
  3672. {
  3673. TTerminalManager * Manager = TTerminalManager::Instance();
  3674. if ((Manager->ActiveTerminal == Data->Terminal) &&
  3675. Manager->ScheduleTerminalReconnnect(Data->Terminal))
  3676. {
  3677. AppLog(L"Scheduled session reconnect, will retry the upload later");
  3678. Retry = true;
  3679. }
  3680. else if (!IsActiveTerminal(Data->Terminal))
  3681. {
  3682. if (!NonVisualDataModule->Busy)
  3683. {
  3684. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3685. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3686. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3687. if (Data->Terminal == NULL)
  3688. {
  3689. TManagedTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3690. if (SameSiteTerminal != NULL)
  3691. {
  3692. UnicodeString Message =
  3693. FMTLOAD(EDIT_SESSION_REATTACH,
  3694. (FileNameOnly, Data->SessionName, FileNameOnly));
  3695. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3696. {
  3697. Data->Terminal = SameSiteTerminal;
  3698. Data->Queue = Manager->FindQueueForTerminal(SameSiteTerminal);
  3699. Data->SessionName = SameSiteTerminal->SessionData->SessionName;
  3700. // We might also overwrite session data
  3701. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3702. }
  3703. else
  3704. {
  3705. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3706. Abort();
  3707. }
  3708. }
  3709. }
  3710. // Foreground session should reconnect itself (except for those disconnected explicitly by the user)
  3711. else if (DebugAlwaysTrue(Terminal->Disconnected || (Terminal != Data->Terminal)))
  3712. {
  3713. UnicodeString Message =
  3714. MainInstructions(
  3715. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3716. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3717. {
  3718. if (Terminal->Disconnected)
  3719. {
  3720. Manager->ActiveSession = Terminal;
  3721. ReconnectSession();
  3722. DebugAssert(Terminal == Data->Terminal);
  3723. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3724. }
  3725. else
  3726. {
  3727. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3728. }
  3729. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3730. }
  3731. else
  3732. {
  3733. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3734. Abort();
  3735. }
  3736. }
  3737. }
  3738. if (!IsActiveTerminal(Data->Terminal))
  3739. {
  3740. Configuration->Usage->Inc(L"EditInactiveSession");
  3741. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3742. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3743. (ExtractFileName(FileName), Data->SessionName)));
  3744. }
  3745. }
  3746. if (!Retry && EditorCheckNotModified(Data))
  3747. {
  3748. if (NonVisualDataModule->Busy)
  3749. {
  3750. Retry = true;
  3751. }
  3752. else
  3753. {
  3754. UnicodeString RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3755. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(RemoteFilePath));
  3756. if (File.get() != NULL)
  3757. {
  3758. AppLogFmt(L"Edited remote file timestamp: %s, Original timestamp: %s", (StandardTimestamp(File->Modification), StandardTimestamp(Data->SourceTimestamp)));
  3759. if (File->Modification != Data->SourceTimestamp)
  3760. {
  3761. UnicodeString Message = MainInstructions(LoadStr(EDIT_CHANGED_EXTERNALLY));
  3762. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) != qaOK)
  3763. {
  3764. Abort();
  3765. }
  3766. }
  3767. }
  3768. }
  3769. }
  3770. if (!Retry)
  3771. {
  3772. TStrings * FileList = new TStringList();
  3773. try
  3774. {
  3775. FileList->Add(FileName);
  3776. // Consider using the same settings (preset) as when the file was downloaded.
  3777. // More over this does not reflect the actual session that will do the upload.
  3778. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3779. TemporaryFileCopyParam(CopyParam);
  3780. if (Data->ForceText)
  3781. {
  3782. CopyParam.TransferMode = tmAscii;
  3783. }
  3784. // so i do not need to worry if masking algorithm works in all cases
  3785. // ("" means "copy file name", no masking is actually done)
  3786. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3787. {
  3788. CopyParam.FileMask = L"";
  3789. }
  3790. else
  3791. {
  3792. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3793. }
  3794. int Params = cpNoConfirmation | cpTemporary;
  3795. if (Data->Terminal->IsCapable[fcBackgroundTransfers])
  3796. {
  3797. DebugAssert(Data->Queue != NULL);
  3798. TQueueItem * QueueItem =
  3799. new TEditorUploadQueueItem(Data->Terminal, FileList, Data->RemoteDirectory, &CopyParam, Params);
  3800. QueueItem->CompleteEvent = UploadCompleteEvent;
  3801. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3802. }
  3803. else
  3804. {
  3805. if (NonVisualDataModule->Busy)
  3806. {
  3807. Retry = true;
  3808. }
  3809. else
  3810. {
  3811. Data->Terminal->CopyToRemote(FileList, Data->RemoteDirectory, &CopyParam, Params, NULL);
  3812. SetEvent(UploadCompleteEvent);
  3813. }
  3814. }
  3815. }
  3816. __finally
  3817. {
  3818. delete FileList;
  3819. }
  3820. }
  3821. }
  3822. //---------------------------------------------------------------------------
  3823. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3824. const UnicodeString & FileName, TEditedFileData * Data)
  3825. {
  3826. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3827. DebugAssert(!NonVisualDataModule->Busy);
  3828. if (!IsActiveTerminal(Data->Terminal))
  3829. {
  3830. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3831. (ExtractFileName(FileName), Data->SessionName)));
  3832. }
  3833. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3834. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3835. NonVisualDataModule->StartBusy();
  3836. try
  3837. {
  3838. std::unique_ptr<TRemoteFile> File;
  3839. UnicodeString RemoteFileName =
  3840. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3841. Terminal->ExceptionOnFail = true;
  3842. try
  3843. {
  3844. File.reset(Terminal->ReadFile(RemoteFileName));
  3845. if (!File->HaveFullFileName)
  3846. {
  3847. File->FullFileName = RemoteFileName;
  3848. }
  3849. }
  3850. __finally
  3851. {
  3852. Terminal->ExceptionOnFail = false;
  3853. }
  3854. std::unique_ptr<TStrings> FileList(new TStringList());
  3855. FileList->AddObject(RemoteFileName, File.get());
  3856. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3857. UnicodeString TempDir = ExtractFilePath(FileName);
  3858. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, true, false);
  3859. if (EditorCheckNotModified(Data))
  3860. {
  3861. Data->SourceTimestamp = File->Modification;
  3862. }
  3863. // sanity check, the target file name should be still the same
  3864. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3865. }
  3866. __finally
  3867. {
  3868. NonVisualDataModule->EndBusy();
  3869. // it actually may not exist anymore...
  3870. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3871. }
  3872. }
  3873. //---------------------------------------------------------------------------
  3874. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3875. const TEditedFileData * Data, bool & KeepOpen)
  3876. {
  3877. // Command is set for external editors only (not for "shell" open).
  3878. if (!Data->Command.IsEmpty())
  3879. {
  3880. bool AnyFound = false;
  3881. bool AnyMDI = false;
  3882. bool AnyNonMDI = false;
  3883. bool AnyDetect = false;
  3884. TEditorList * EditorList = new TEditorList();
  3885. try
  3886. {
  3887. *EditorList = *WinConfiguration->EditorList;
  3888. for (int i = 0; i < EditorList->Count; i++)
  3889. {
  3890. const TEditorPreferences * Editor = EditorList->Editors[i];
  3891. if ((Editor->Data->Editor == edExternal) &&
  3892. (Editor->Data->ExternalEditor == Data->Command))
  3893. {
  3894. AnyFound = true;
  3895. if (Editor->Data->SDIExternalEditor)
  3896. {
  3897. AnyNonMDI = true;
  3898. if (Editor->Data->DetectMDIExternalEditor)
  3899. {
  3900. AnyDetect = true;
  3901. }
  3902. }
  3903. else
  3904. {
  3905. AnyMDI = true;
  3906. }
  3907. }
  3908. }
  3909. bool EnableMDI = false;
  3910. bool DisableDetect = false;
  3911. if (AnyMDI)
  3912. {
  3913. KeepOpen = true;
  3914. if (AnyNonMDI)
  3915. {
  3916. // there is at least one instance of the editor with MDI support enabled,
  3917. // and one with disabled, enable it for all instances
  3918. EnableMDI = true;
  3919. }
  3920. }
  3921. else if (AnyFound && !AnyDetect)
  3922. {
  3923. // at least once instance found but all have MDI autodetection disabled
  3924. // => close the file (default action)
  3925. }
  3926. else
  3927. {
  3928. // no instance of the editor has MDI support enabled
  3929. TMessageParams Params;
  3930. if (AnyFound)
  3931. {
  3932. // there is at least one instance of the editor
  3933. Params.Params |= mpNeverAskAgainCheck;
  3934. }
  3935. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3936. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3937. switch (Answer)
  3938. {
  3939. case qaNeverAskAgain:
  3940. DisableDetect = true;
  3941. break;
  3942. case qaNo:
  3943. EnableMDI = true;
  3944. KeepOpen = true;
  3945. break;
  3946. }
  3947. }
  3948. if (AnyFound && (EnableMDI || DisableDetect))
  3949. {
  3950. bool Changed = false;
  3951. for (int i = 0; i < EditorList->Count; i++)
  3952. {
  3953. const TEditorPreferences * Editor = EditorList->Editors[i];
  3954. if ((Editor->Data->Editor == edExternal) &&
  3955. (Editor->Data->ExternalEditor == Data->Command) &&
  3956. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3957. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3958. {
  3959. Changed = true;
  3960. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3961. if (EnableMDI)
  3962. {
  3963. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3964. }
  3965. if (DisableDetect)
  3966. {
  3967. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3968. }
  3969. EditorList->Change(i, UpdatedEditor);
  3970. }
  3971. }
  3972. if (Changed)
  3973. {
  3974. WinConfiguration->EditorList = EditorList;
  3975. }
  3976. }
  3977. }
  3978. __finally
  3979. {
  3980. delete EditorList;
  3981. }
  3982. }
  3983. else
  3984. {
  3985. // "open" case
  3986. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3987. qaOK, HELP_APP_EARLY_CLOSED);
  3988. }
  3989. }
  3990. //---------------------------------------------------------------------------
  3991. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3992. {
  3993. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3994. }
  3995. //---------------------------------------------------------------------------
  3996. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3997. {
  3998. SideEnter(osRemote);
  3999. }
  4000. //---------------------------------------------------------------------------
  4001. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  4002. {
  4003. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  4004. SideEnter(osRemote);
  4005. }
  4006. //---------------------------------------------------------------------------
  4007. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  4008. {
  4009. FCurrentSide = Side;
  4010. if (Visible)
  4011. {
  4012. UpdateControls();
  4013. }
  4014. }
  4015. //---------------------------------------------------------------------------
  4016. void __fastcall TCustomScpExplorerForm::FileDeleted(TOperationSide Side, const UnicodeString & FileName, bool Success)
  4017. {
  4018. VisualiseOperationFinished(Side, FileName, Success);
  4019. if (DebugAlwaysTrue(FDeletedFiles != NULL) && Success)
  4020. {
  4021. FDeletedFiles->Add(FileName);
  4022. }
  4023. }
  4024. //---------------------------------------------------------------------------
  4025. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  4026. TStrings * FileList, bool Alternative)
  4027. {
  4028. TCustomDirView * DView = DirView(Side);
  4029. DView->SaveSelection();
  4030. DView->SaveSelectedNames();
  4031. DebugAssert(!FAlternativeDelete);
  4032. FAlternativeDelete = Alternative;
  4033. try
  4034. {
  4035. if (!IsSideLocalBrowser(Side))
  4036. {
  4037. DebugAssert(Terminal != NULL);
  4038. // Clone the file list as it may refer to current directory files, which get destroyed, when the directory is reloaded after the operation
  4039. std::unique_ptr<TStrings> PermanentFileList(TRemoteFileList::CloneStrings(FileList));
  4040. std::unique_ptr<TStringList> DeletedFiles(new TStringList());
  4041. DeletedFiles->CaseSensitive = DebugAlwaysTrue(!IsSideLocalBrowser(Side));
  4042. int Params = FLAGMASK(Alternative, dfAlternative);
  4043. try
  4044. {
  4045. TValueRestorer<TStrings *> DeletedFilesRestorer(FDeletedFiles);
  4046. DebugAssert(FDeletedFiles == NULL);
  4047. FDeletedFiles = DeletedFiles.get();
  4048. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  4049. FOnFileOperationFinished = FileDeleted;
  4050. FMoveToQueue = false;
  4051. Terminal->DeleteFiles(FileList, Params);
  4052. // Probably not needed for deleting, just for consistency with transfer code
  4053. if (FMoveToQueue)
  4054. {
  4055. Abort();
  4056. }
  4057. }
  4058. catch (EAbort &)
  4059. {
  4060. if (FMoveToQueue)
  4061. {
  4062. DeletedFiles->Sorted = true;
  4063. for (int Index = 0; Index < PermanentFileList->Count; Index++)
  4064. {
  4065. if (DeletedFiles->IndexOf(PermanentFileList->Strings[Index]) >= 0)
  4066. {
  4067. // We should always be deleting the first item => what can be used to optimize this code, if needed
  4068. DebugAssert(Index == 0);
  4069. PermanentFileList->Delete(Index);
  4070. Index--;
  4071. }
  4072. }
  4073. Configuration->Usage->Inc("MovesToBackgroundDelete");
  4074. TQueueItem * QueueItem = new TDeleteQueueItem(Terminal, PermanentFileList.get(), Params);
  4075. AddQueueItem(Queue, QueueItem, Terminal);
  4076. ClearOperationSelection(osRemote);
  4077. }
  4078. }
  4079. }
  4080. else
  4081. {
  4082. if (IsLocalBrowserMode())
  4083. {
  4084. Configuration->Usage->Inc(L"LocalLocalDeletes");
  4085. }
  4086. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  4087. FProgressSide = FCurrentSide;
  4088. try
  4089. {
  4090. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  4091. }
  4092. __finally
  4093. {
  4094. ReloadLocalDirectory();
  4095. }
  4096. }
  4097. FAlternativeDelete = false;
  4098. }
  4099. catch(...)
  4100. {
  4101. FAlternativeDelete = false;
  4102. DView->DiscardSavedSelection();
  4103. throw;
  4104. }
  4105. DView->RestoreSelection();
  4106. }
  4107. //---------------------------------------------------------------------------
  4108. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  4109. {
  4110. DebugAssert(Terminal);
  4111. DebugAssert(!IsLocalBrowserMode());
  4112. RemoteDirView->SaveSelection();
  4113. RemoteDirView->SaveSelectedNames();
  4114. try
  4115. {
  4116. if (Lock)
  4117. {
  4118. Terminal->LockFiles(FileList);
  4119. }
  4120. else
  4121. {
  4122. Terminal->UnlockFiles(FileList);
  4123. }
  4124. }
  4125. catch(...)
  4126. {
  4127. RemoteDirView->DiscardSavedSelection();
  4128. throw;
  4129. }
  4130. RemoteDirView->RestoreSelection();
  4131. }
  4132. //---------------------------------------------------------------------------
  4133. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  4134. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  4135. bool NoConfirmation, bool Move)
  4136. {
  4137. DebugAssert(Terminal != NULL);
  4138. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  4139. DebugAssert(!IsLocalBrowserMode());
  4140. if (Session == NULL)
  4141. {
  4142. Session = Terminal;
  4143. }
  4144. if (Session == Terminal)
  4145. {
  4146. if (RemoteDriveView->DropTarget != NULL)
  4147. {
  4148. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  4149. }
  4150. else if (RemoteDirView->DropTarget != NULL)
  4151. {
  4152. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  4153. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  4154. }
  4155. else
  4156. {
  4157. Target = RemoteDirView->Path;
  4158. }
  4159. }
  4160. else
  4161. {
  4162. Target = GetSessionPath(Session, osRemote);
  4163. }
  4164. Target = UnixIncludeTrailingBackslash(Target);
  4165. if (FileList->Count == 1)
  4166. {
  4167. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  4168. }
  4169. else
  4170. {
  4171. FileMask = AnyMask;
  4172. }
  4173. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  4174. bool Result = true;
  4175. if (!NoConfirmation)
  4176. {
  4177. bool Multi = (FileList->Count > 1);
  4178. if (Move)
  4179. {
  4180. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  4181. }
  4182. else
  4183. {
  4184. std::unique_ptr<TStrings> Sessions(new TStringList);
  4185. std::unique_ptr<TStrings> Directories(new TStringList);
  4186. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  4187. for (int Index = 0; Index < SessionList->Count; Index++)
  4188. {
  4189. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  4190. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  4191. {
  4192. Sessions->AddObject(SessionList->Strings[Index], ASession);
  4193. Directories->Add(GetSessionPath(ASession, osRemote));
  4194. }
  4195. }
  4196. TDirectRemoteCopy AllowDirectCopy;
  4197. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  4198. {
  4199. DebugAssert(DirectCopy);
  4200. AllowDirectCopy = drcAllow;
  4201. }
  4202. else if (Terminal->IsCapable[fcSecondaryShell])
  4203. {
  4204. DebugAssert(DirectCopy);
  4205. AllowDirectCopy = drcConfirmCommandSession;
  4206. }
  4207. else
  4208. {
  4209. DebugAssert(!DirectCopy);
  4210. AllowDirectCopy = drcDisallow;
  4211. }
  4212. void * ASession = Session;
  4213. Result = DoRemoteCopyDialog(
  4214. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal);
  4215. Session = static_cast<TManagedTerminal *>(ASession);
  4216. }
  4217. }
  4218. return Result;
  4219. }
  4220. //---------------------------------------------------------------------------
  4221. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  4222. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  4223. {
  4224. DebugAssert(!IsLocalBrowserMode());
  4225. bool DirectCopy;
  4226. UnicodeString Target, FileMask;
  4227. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  4228. if (Result)
  4229. {
  4230. if (!Move && !DirectCopy)
  4231. {
  4232. Configuration->Usage->Inc("RemoteCopyTemp");
  4233. UnicodeString RootTempDir;
  4234. UnicodeString TempDir;
  4235. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  4236. TStrings * TemporaryFilesList = new TStringList();
  4237. try
  4238. {
  4239. TMakeLocalFileListParams MakeFileListParam;
  4240. MakeFileListParam.FileList = TemporaryFilesList;
  4241. MakeFileListParam.FileTimes = NULL;
  4242. MakeFileListParam.IncludeDirs = true;
  4243. MakeFileListParam.Recursive = false;
  4244. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  4245. TTerminalManager::Instance()->ActiveSession = Session;
  4246. if (TemporaryFilesList->Count > 0)
  4247. {
  4248. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4249. CopyParam.FileMask = FileMask;
  4250. DebugAssert(!FAutoOperation);
  4251. FAutoOperation = true;
  4252. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  4253. }
  4254. }
  4255. __finally
  4256. {
  4257. delete TemporaryFilesList;
  4258. FAutoOperation = false;
  4259. if (!RootTempDir.IsEmpty())
  4260. {
  4261. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  4262. }
  4263. }
  4264. }
  4265. else
  4266. {
  4267. RemoteDirView->SaveSelection();
  4268. RemoteDirView->SaveSelectedNames();
  4269. try
  4270. {
  4271. if (Move)
  4272. {
  4273. Configuration->Usage->Inc("RemoteMove");
  4274. Terminal->MoveFiles(FileList, Target, FileMask, false);
  4275. }
  4276. else
  4277. {
  4278. Configuration->Usage->Inc("RemoteCopyDirect");
  4279. DebugAssert(DirectCopy);
  4280. DebugAssert(Session == Terminal);
  4281. if (Terminal->IsCapable[fcRemoteCopy] ||
  4282. Terminal->CommandSessionOpened ||
  4283. CommandSessionFallback())
  4284. {
  4285. Terminal->CopyFiles(FileList, Target, FileMask);
  4286. }
  4287. }
  4288. }
  4289. catch(...)
  4290. {
  4291. RemoteDirView->DiscardSavedSelection();
  4292. throw;
  4293. }
  4294. RemoteDirView->RestoreSelection();
  4295. }
  4296. }
  4297. return Result;
  4298. }
  4299. //---------------------------------------------------------------------------
  4300. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4301. const UnicodeString & Path, TRemoteProperties & Properties)
  4302. {
  4303. DebugAssert(!IsLocalBrowserMode());
  4304. Properties.Valid = Properties.Valid << vpEncrypt;
  4305. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4306. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4307. }
  4308. //---------------------------------------------------------------------------
  4309. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4310. {
  4311. Side = GetSide(Side);
  4312. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4313. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4314. UnicodeString Name = LoadStr(NEW_FOLDER);
  4315. int AllowedChanges =
  4316. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4317. bool SaveSettings = false;
  4318. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4319. {
  4320. TWindowLock Lock(this);
  4321. if (!IsSideLocalBrowser(Side))
  4322. {
  4323. if (SaveSettings)
  4324. {
  4325. GUIConfiguration->NewDirectoryProperties = Properties;
  4326. }
  4327. CreateRemoteDirectory(Name, Properties);
  4328. }
  4329. else
  4330. {
  4331. DirView(Side)->CreateDirectory(Name);
  4332. }
  4333. }
  4334. }
  4335. //---------------------------------------------------------------------------
  4336. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4337. {
  4338. TWindowLock Lock(this);
  4339. DirView(Side)->ExecuteHomeDirectory();
  4340. }
  4341. //---------------------------------------------------------------------------
  4342. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4343. {
  4344. DoOpenDirectoryDialog(odBrowse, Side);
  4345. }
  4346. //---------------------------------------------------------------------------
  4347. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4348. {
  4349. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4350. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4351. bool Result = !Path.IsEmpty();
  4352. if (Result)
  4353. {
  4354. TryOpenDirectory(Side, Path);
  4355. }
  4356. return Result;
  4357. }
  4358. //---------------------------------------------------------------------------
  4359. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4360. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4361. bool & Close)
  4362. {
  4363. // terminal can be already closed (e.g. dropped connection)
  4364. if (Terminal != NULL)
  4365. {
  4366. try
  4367. {
  4368. TCalculateSizeParams Params;
  4369. Params.Stats = &Stats;
  4370. Terminal->CalculateFilesSize(FileList, Size, Params);
  4371. }
  4372. catch(...)
  4373. {
  4374. if (!Terminal->Active)
  4375. {
  4376. Close = true;
  4377. }
  4378. throw;
  4379. }
  4380. }
  4381. }
  4382. //---------------------------------------------------------------------------
  4383. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4384. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4385. bool & Close)
  4386. {
  4387. if ((Terminal != NULL) && // terminal can be already closed (e.g. dropped connection)
  4388. EnsureCommandSessionFallback(fcCalculatingChecksum))
  4389. {
  4390. Configuration->Usage->Inc(L"ChecksumCalculated");
  4391. try
  4392. {
  4393. Terminal->CalculateFilesChecksum(Alg, FileList, OnCalculatedChecksum);
  4394. }
  4395. catch(...)
  4396. {
  4397. if (!Terminal->Active)
  4398. {
  4399. Close = true;
  4400. }
  4401. throw;
  4402. }
  4403. }
  4404. }
  4405. //---------------------------------------------------------------------------
  4406. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4407. {
  4408. if (Terminal->LoadFilesProperties(FileList))
  4409. {
  4410. RemoteDirView->Invalidate();
  4411. }
  4412. }
  4413. //---------------------------------------------------------------------------
  4414. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4415. {
  4416. bool Result;
  4417. if (!IsSideLocalBrowser(Side))
  4418. {
  4419. TRemoteTokenList * GroupList = NULL;
  4420. TRemoteTokenList * UserList = NULL;
  4421. try
  4422. {
  4423. TRemoteProperties CurrentProperties;
  4424. LoadFilesProperties(FileList);
  4425. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4426. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4427. if (CapableGroupChanging || CapableOwnerChanging)
  4428. {
  4429. if (CapableGroupChanging)
  4430. {
  4431. GroupList = Terminal->Groups->Duplicate();
  4432. }
  4433. if (CapableOwnerChanging)
  4434. {
  4435. UserList = Terminal->Users->Duplicate();
  4436. }
  4437. TRemoteDirectory * Files = Terminal->Files;
  4438. int Count = Files->Count;
  4439. if (Count > 100)
  4440. {
  4441. Count = 100;
  4442. }
  4443. for (int Index = 0; Index < Count; Index++)
  4444. {
  4445. TRemoteFile * File = Files->Files[Index];
  4446. if (CapableGroupChanging)
  4447. {
  4448. GroupList->AddUnique(File->Group);
  4449. }
  4450. if (CapableOwnerChanging)
  4451. {
  4452. UserList->AddUnique(File->Owner);
  4453. }
  4454. }
  4455. // if we haven't collected tokens for all files in current directory,
  4456. // make sure we collect them at least for all selected files.
  4457. // (note that so far the files in FileList has to be from current direcotry)
  4458. if (Count < Files->Count)
  4459. {
  4460. for (int Index = 0; Index < FileList->Count; Index++)
  4461. {
  4462. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4463. if (CapableGroupChanging)
  4464. {
  4465. GroupList->AddUnique(File->Group);
  4466. }
  4467. if (CapableOwnerChanging)
  4468. {
  4469. UserList->AddUnique(File->Owner);
  4470. }
  4471. }
  4472. }
  4473. }
  4474. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4475. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4476. for (int Index = 0; (Index < FileList->Count) && CapableAclChanging; Index++)
  4477. {
  4478. if (dynamic_cast<TRemoteFile *>(FileList->Objects[Index])->IsDirectory)
  4479. {
  4480. CapableAclChanging = false;
  4481. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4482. }
  4483. }
  4484. int Flags = 0;
  4485. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4486. if (CapableAclChanging) Flags |= cpAcl;
  4487. if (CapableOwnerChanging) Flags |= cpOwner;
  4488. if (CapableGroupChanging) Flags |= cpGroup;
  4489. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4490. if (Terminal->IsCapable[fcCalculatingChecksum] ||
  4491. (Terminal->IsCapable[fcSecondaryShell] && !Terminal->IsEncryptingFiles()))
  4492. {
  4493. CalculateChecksumEvent = CalculateChecksum;
  4494. }
  4495. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4496. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4497. TRemoteProperties NewProperties = CurrentProperties;
  4498. Result =
  4499. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4500. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4501. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4502. CalculateSize, CalculateChecksumEvent);
  4503. if (Result)
  4504. {
  4505. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4506. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4507. }
  4508. }
  4509. __finally
  4510. {
  4511. delete GroupList;
  4512. delete UserList;
  4513. }
  4514. }
  4515. else
  4516. {
  4517. DirView(Side)->DisplayPropertiesMenu();
  4518. Result = true;
  4519. }
  4520. return Result;
  4521. }
  4522. //---------------------------------------------------------------------------
  4523. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4524. {
  4525. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4526. {
  4527. FIgnoreNextDialogChar = Key;
  4528. }
  4529. Key = 0;
  4530. }
  4531. //---------------------------------------------------------------------------
  4532. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4533. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4534. {
  4535. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4536. if (Command != NULL)
  4537. {
  4538. KeyProcessed(Key, Shift);
  4539. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4540. {
  4541. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4542. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4543. false, false, const_cast<TCustomCommandType *>(Command));
  4544. }
  4545. }
  4546. }
  4547. //---------------------------------------------------------------------------
  4548. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4549. {
  4550. if (!NonVisualDataModule->Busy)
  4551. {
  4552. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4553. {
  4554. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4555. }
  4556. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4557. {
  4558. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4559. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4560. {
  4561. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4562. // But so far it's not a problem, as it does what we want.
  4563. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4564. if (((Action->ShortCut == KeyShortCut) ||
  4565. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4566. AllowedAction(Action, aaShortCut))
  4567. {
  4568. // Has to be called before the action as the dialog char is already in queue.
  4569. // So when the action consumes message queue, we already need to have the
  4570. // FIgnoreNextDialogChar set
  4571. KeyProcessed(Key, Shift);
  4572. // Reset reference to previous component (when menu/toolbar was clicked).
  4573. // Needed to detect that action was invoked by keyboard shortcut
  4574. // in TNonVisualDataModule::ExplorerActionsExecute
  4575. Action->ActionComponent = NULL;
  4576. Action->Execute();
  4577. return;
  4578. }
  4579. }
  4580. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4581. {
  4582. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4583. {
  4584. KeyProcessed(Key, Shift);
  4585. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4586. return;
  4587. }
  4588. }
  4589. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4590. {
  4591. KeyProcessed(Key, Shift);
  4592. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4593. }
  4594. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4595. if (IsCustomShortCut(CustomShortCut))
  4596. {
  4597. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4598. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4599. if (WinConfiguration->SharedBookmarks != NULL)
  4600. {
  4601. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4602. if ((Bookmark != NULL) &&
  4603. OpenBookmark(FCurrentSide, Bookmark))
  4604. {
  4605. KeyProcessed(Key, Shift);
  4606. }
  4607. }
  4608. }
  4609. }
  4610. }
  4611. TForm::KeyDown(Key, Shift);
  4612. }
  4613. //---------------------------------------------------------------------------
  4614. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4615. {
  4616. bool OpenedTerminal = false;
  4617. UnicodeString ProtocolName;
  4618. UnicodeString ProtocolNameHint;
  4619. UnicodeString SecurityProtocolName;
  4620. UnicodeString DurationHint;
  4621. // See the comment in TScpCommanderForm::UpdateControls()
  4622. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4623. if (ActiveTerminal != NULL)
  4624. {
  4625. // Active is not enough here
  4626. if (ActiveTerminal->Status == ssOpened)
  4627. {
  4628. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4629. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4630. if (FileSystemInfo.ProtocolName.IsEmpty())
  4631. {
  4632. ProtocolName = SessionInfo.ProtocolName;
  4633. }
  4634. else
  4635. {
  4636. ProtocolName = FileSystemInfo.ProtocolName;
  4637. }
  4638. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4639. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4640. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4641. OpenedTerminal = true;
  4642. }
  4643. else if (ActiveTerminal->Status > ssClosed)
  4644. {
  4645. ProtocolName = LoadStr(STATUS_CONNECTING);
  4646. }
  4647. else
  4648. {
  4649. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4650. }
  4651. }
  4652. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4653. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4654. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4655. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4656. if (ProtocolStatusPanel->Caption != ProtocolName)
  4657. {
  4658. ProtocolStatusPanel->Caption = ProtocolName;
  4659. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4660. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4661. }
  4662. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4663. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4664. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4665. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4666. SecurityStatusPanel->Enabled = SecurityEnabled;
  4667. UnicodeString SecurityProtocolHint;
  4668. if (SecurityEnabled)
  4669. {
  4670. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4671. }
  4672. else
  4673. {
  4674. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4675. }
  4676. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4677. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4678. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4679. DurationStatusPanel->Hint = DurationHint;
  4680. UpdateStatusBar();
  4681. }
  4682. //---------------------------------------------------------------------------
  4683. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4684. {
  4685. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4686. DebugAssert(SessionStatusBar != NULL);
  4687. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4688. if (!FNote.IsEmpty())
  4689. {
  4690. NoteStatusPanel->Caption = FNote;
  4691. }
  4692. else
  4693. {
  4694. UpdateStatusPanelText(NoteStatusPanel);
  4695. }
  4696. NoteStatusPanel->Hint = FNoteHints;
  4697. UnicodeString LoginTime;
  4698. // See the comment in TScpCommanderForm::UpdateControls()
  4699. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4700. // Active is not enough here
  4701. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4702. {
  4703. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4704. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4705. }
  4706. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4707. DurationStatusPanel->Caption = LoginTime;
  4708. }
  4709. //---------------------------------------------------------------------------
  4710. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4711. {
  4712. Panel->Caption = L"";
  4713. }
  4714. //---------------------------------------------------------------------------
  4715. void __fastcall TCustomScpExplorerForm::Idle()
  4716. {
  4717. if (FShowing ||
  4718. // Particularly for
  4719. // - Standalone editing
  4720. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4721. // while "keeping remote directory up to date".
  4722. StandaloneOperation)
  4723. {
  4724. FEditorManager->Check();
  4725. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4726. // that needs to know if queue view is visible (and it may be closed after queue update)
  4727. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4728. }
  4729. if (FShowing)
  4730. {
  4731. if (!NonVisualDataModule->Busy &&
  4732. // Menu is opened or drag&drop is going on
  4733. (Mouse->Capture == NULL))
  4734. {
  4735. if (FRefreshRemoteDirectory)
  4736. {
  4737. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4738. {
  4739. Terminal->RefreshDirectory();
  4740. }
  4741. FRefreshRemoteDirectory = false;
  4742. }
  4743. if (FRefreshLocalDirectory)
  4744. {
  4745. ReloadLocalDirectory();
  4746. FRefreshLocalDirectory = false;
  4747. }
  4748. if (WinConfiguration->RefreshRemotePanel)
  4749. {
  4750. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4751. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4752. {
  4753. RemoteDirView->ReloadDirectory();
  4754. }
  4755. }
  4756. }
  4757. // TB2 shows accels on Alt key down, but hides it only after menu modal loop finishes.
  4758. // But if Alt is used to switch apps or for keyboard shortcut, the menu modal loop does not happen and accelerators stay displayed.
  4759. // It's difficult to fix this in TB2, as there's no "up" equivalent to CM_DIALOGKEY, which shows the accels.
  4760. // 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.
  4761. TTBCustomToolbar * MenuToolbar = DebugNotNull(dynamic_cast<TTBCustomToolbar *>(GetComponent(fcMenu)));
  4762. MenuToolbar->Idle();
  4763. }
  4764. if (FShowing || StandaloneOperation)
  4765. {
  4766. if (FQueueStatusInvalidated)
  4767. {
  4768. UpdateQueueStatus(false);
  4769. }
  4770. RefreshQueueItems();
  4771. }
  4772. if (FShowing)
  4773. {
  4774. UpdateStatusBar();
  4775. }
  4776. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4777. {
  4778. FClipboardFakeMonitorsPendingReset = false;
  4779. FClipboardFakeMonitors.reset(NULL);
  4780. }
  4781. FIgnoreNextDialogChar = 0;
  4782. }
  4783. //---------------------------------------------------------------------------
  4784. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4785. {
  4786. try
  4787. {
  4788. FUserActionTimer->Enabled = false;
  4789. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4790. {
  4791. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4792. {
  4793. FPendingQueueActionItem->ProcessUserAction();
  4794. }
  4795. }
  4796. }
  4797. __finally
  4798. {
  4799. FPendingQueueActionItem = NULL;
  4800. }
  4801. }
  4802. //---------------------------------------------------------------------------
  4803. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4804. {
  4805. if (WinConfiguration->MinimizeToTray)
  4806. {
  4807. UpdateTrayIcon();
  4808. FTrayIcon->Visible = true;
  4809. if (Visible)
  4810. {
  4811. ShowWindow(Handle, SW_HIDE);
  4812. }
  4813. }
  4814. }
  4815. //---------------------------------------------------------------------------
  4816. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4817. {
  4818. // WORKAROUND
  4819. // When restoring maximized window from minimization,
  4820. // rarely some controls do not align properly.
  4821. // Two instances seen (both for Commander):
  4822. // - When restoring, window is temporarily narrower (not maximized),
  4823. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4824. // Once maximized already, top dock shinks back, but the session PageControl,
  4825. // do not align up, leaving space between TopDock and PageControl.
  4826. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4827. for (int Index = 0; Index < ControlCount; Index++)
  4828. {
  4829. RealignControl(Controls[Index]);
  4830. }
  4831. if (FTrayIcon->Visible)
  4832. {
  4833. FTrayIcon->Visible = false;
  4834. if (Visible)
  4835. {
  4836. ShowWindow(Handle, SW_SHOW);
  4837. }
  4838. }
  4839. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4840. {
  4841. FNeedSession = false;
  4842. NonVisualDataModule->StartBusy();
  4843. try
  4844. {
  4845. NeedSession(true);
  4846. }
  4847. __finally
  4848. {
  4849. NonVisualDataModule->EndBusy();
  4850. }
  4851. }
  4852. }
  4853. //---------------------------------------------------------------------------
  4854. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4855. {
  4856. FTrayIcon->Hint = GetMainForm()->Caption;
  4857. }
  4858. //---------------------------------------------------------------------------
  4859. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4860. {
  4861. UpdateTrayIcon();
  4862. }
  4863. //---------------------------------------------------------------------------
  4864. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4865. {
  4866. // workaround
  4867. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4868. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4869. // But WM_ACTIVATEAPP message can be received even when
  4870. // the main window is minimized to the tray and internal editor window is focused
  4871. // (after another application was previously active)
  4872. if (::IsIconic(Handle))
  4873. {
  4874. if (!IsAppIconic())
  4875. {
  4876. SetAppIconic(true);
  4877. }
  4878. }
  4879. ::ApplicationRestore();
  4880. Application->BringToFront();
  4881. }
  4882. //---------------------------------------------------------------------------
  4883. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4884. {
  4885. RestoreApp();
  4886. }
  4887. //---------------------------------------------------------------------------
  4888. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4889. {
  4890. if (OpenInNewWindow())
  4891. {
  4892. ExecuteNewInstance(SessionUrl);
  4893. }
  4894. else
  4895. {
  4896. TTerminalManager::Instance()->NewSession(SessionUrl);
  4897. }
  4898. }
  4899. //---------------------------------------------------------------------------
  4900. void TCustomScpExplorerForm::NewTab(TOperationSide, bool)
  4901. {
  4902. NewSession();
  4903. }
  4904. //---------------------------------------------------------------------------
  4905. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4906. {
  4907. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4908. StoredSessions->NewSession(SessionName, SessionData);
  4909. // modified only, explicit
  4910. StoredSessions->Save(false, true);
  4911. // encode session name because of slashes in hierarchical sessions
  4912. return EncodeUrlString(SessionName);
  4913. }
  4914. //---------------------------------------------------------------------------
  4915. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4916. {
  4917. // current working directories become defaults here, what is not right
  4918. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4919. return SaveHiddenDuplicateSession(SessionData.get());
  4920. }
  4921. //---------------------------------------------------------------------------
  4922. void __fastcall TCustomScpExplorerForm::DuplicateTab()
  4923. {
  4924. if (OpenInNewWindow())
  4925. {
  4926. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4927. }
  4928. else
  4929. {
  4930. // current working directories become defaults here, what is not right
  4931. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4932. TTerminalManager * Manager = TTerminalManager::Instance();
  4933. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4934. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4935. // We definitelly want these
  4936. ASession->Disconnected = ManagedSession->Disconnected;
  4937. // Not sure about these two
  4938. ASession->Permanent = ManagedSession->Permanent;
  4939. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4940. Manager->ActiveSession = ASession;
  4941. }
  4942. }
  4943. //---------------------------------------------------------------------------
  4944. void __fastcall TCustomScpExplorerForm::RenameTab()
  4945. {
  4946. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4947. if (InputDialog(LoadStr(RENAME_TAB_TITLE), LoadStr(RENAME_TAB_PROMPT), Name, HELP_TAB_RENAME) &&
  4948. // When submitting the default name, do not fix it
  4949. (Name != ManagedSession->SessionData->SessionName))
  4950. {
  4951. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4952. TSessionData::ValidateName(Name);
  4953. // This is inconsistent with how color (for example) is handled.
  4954. ManagedSession->SessionData->Name = Name;
  4955. UpdateControls();
  4956. // Add/Remove distinguishing paths from sessions of the same name.
  4957. SessionListChanged();
  4958. }
  4959. }
  4960. //---------------------------------------------------------------------------
  4961. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4962. {
  4963. DebugAssert(Queue != NULL);
  4964. bool Result = Queue->IsEmpty;
  4965. if (!Result)
  4966. {
  4967. SetFocus();
  4968. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4969. }
  4970. return Result;
  4971. }
  4972. //---------------------------------------------------------------------------
  4973. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4974. {
  4975. return CanCloseQueue(FQueue);
  4976. }
  4977. //---------------------------------------------------------------------------
  4978. void __fastcall TCustomScpExplorerForm::CloseTab()
  4979. {
  4980. if (CanCloseQueue())
  4981. {
  4982. TTerminalManager::Instance()->FreeActiveTerminal();
  4983. }
  4984. }
  4985. //---------------------------------------------------------------------------
  4986. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4987. {
  4988. if (CanCloseQueue())
  4989. {
  4990. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4991. }
  4992. }
  4993. //---------------------------------------------------------------------------
  4994. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4995. {
  4996. DetachTerminal(Terminal);
  4997. RemoteDirView->Terminal = Terminal;
  4998. UpdateRemotePathComboBox(false);
  4999. InitStatusBar();
  5000. UpdateControls();
  5001. }
  5002. //---------------------------------------------------------------------------
  5003. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  5004. {
  5005. InitStatusBar();
  5006. }
  5007. //---------------------------------------------------------------------------
  5008. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  5009. {
  5010. if (DebugAlwaysTrue(!Terminal->Active))
  5011. {
  5012. TTerminalManager::Instance()->ReconnectActiveTerminal();
  5013. }
  5014. }
  5015. //---------------------------------------------------------------------------
  5016. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  5017. {
  5018. if (OpenInNewWindow())
  5019. {
  5020. // encode session name because of slashes in hierarchical sessions
  5021. ExecuteNewInstance(EncodeUrlString(Data->Name));
  5022. }
  5023. else
  5024. {
  5025. TTerminalManager * Manager = TTerminalManager::Instance();
  5026. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  5027. Manager->ActiveSession = Terminal;
  5028. }
  5029. }
  5030. //---------------------------------------------------------------------------
  5031. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  5032. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  5033. {
  5034. TTerminalManager * Manager = TTerminalManager::Instance();
  5035. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5036. // This asks for a master password. Maybe we should postpone asking until a connection is actually opened.
  5037. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  5038. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  5039. {
  5040. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  5041. // FirstSession can be null, if none of the workspace sites exist anymore
  5042. // or if all workspace sessions are not suported by the interface
  5043. // (when trying to open all local-local workspace in "explorer" interface)
  5044. if (FirstSession != NULL)
  5045. {
  5046. if (!ConnectFirstTerminal)
  5047. {
  5048. FirstSession->Disconnected = true;
  5049. FirstSession->DisconnectedTemporarily = true;
  5050. }
  5051. Manager->ActiveSession = FirstSession;
  5052. }
  5053. }
  5054. }
  5055. //---------------------------------------------------------------------------
  5056. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  5057. {
  5058. if (OpenInNewWindow())
  5059. {
  5060. ExecuteNewInstance(Name);
  5061. }
  5062. else
  5063. {
  5064. DoOpenFolderOrWorkspace(Name, true, false);
  5065. }
  5066. }
  5067. //---------------------------------------------------------------------------
  5068. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  5069. bool &CanClose)
  5070. {
  5071. // See the comment in CloseApp()
  5072. if (NonVisualDataModule->Busy)
  5073. {
  5074. CanClose = false;
  5075. }
  5076. else
  5077. {
  5078. TTerminalManager * Manager = TTerminalManager::Instance();
  5079. int ActiveSessions = 0;
  5080. for (int Index = 0; Index < Manager->Count; Index++)
  5081. {
  5082. if (Manager->Sessions[Index]->Active)
  5083. {
  5084. ActiveSessions++;
  5085. }
  5086. }
  5087. // Confirm, when there's at least one active remote session or
  5088. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  5089. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  5090. WinConfiguration->ConfirmClosingSession)
  5091. {
  5092. unsigned int Result;
  5093. TMessageParams Params(mpNeverAskAgainCheck);
  5094. UnicodeString Message;
  5095. int Answers = qaOK | qaCancel;
  5096. // The current session is the only active session
  5097. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  5098. {
  5099. if (!WinConfiguration->AutoSaveWorkspace)
  5100. {
  5101. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  5102. }
  5103. else
  5104. {
  5105. Message = LoadStr(CLOSE_SESSION);
  5106. }
  5107. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  5108. }
  5109. // Multiple active sessions or one active session, but it's not the current one
  5110. else if (ActiveSessions > 0)
  5111. {
  5112. if (!WinConfiguration->AutoSaveWorkspace)
  5113. {
  5114. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  5115. }
  5116. else
  5117. {
  5118. Message = LoadStr(CLOSE_SESSIONS);
  5119. }
  5120. }
  5121. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  5122. else
  5123. {
  5124. DebugAlwaysTrue(Manager->Count > 1);
  5125. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  5126. Message = LoadStr(CLOSE_WORKSPACE);
  5127. }
  5128. UnicodeString Note;
  5129. if (WinConfiguration->AutoSaveWorkspace)
  5130. {
  5131. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  5132. }
  5133. else
  5134. {
  5135. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  5136. Answers = qaYes | qaNo | qaCancel;
  5137. }
  5138. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  5139. SetFocus();
  5140. Result = MessageDialog(Message, qtConfirmation,
  5141. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  5142. if (Result == qaNeverAskAgain)
  5143. {
  5144. WinConfiguration->ConfirmClosingSession = false;
  5145. }
  5146. if (Result == qaNo)
  5147. {
  5148. CanClose = SaveWorkspace(true);
  5149. // note that the workspace will be saved redundatly again from FormClose
  5150. }
  5151. else
  5152. {
  5153. CanClose =
  5154. (Result == qaOK) ||
  5155. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  5156. (Result == qaNeverAskAgain);
  5157. }
  5158. }
  5159. if (CanClose && HasActiveTerminal())
  5160. {
  5161. CanClose = CanCloseQueue();
  5162. }
  5163. }
  5164. if (CanClose)
  5165. {
  5166. CanClose =
  5167. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  5168. FEditorManager->CloseExternalFilesWithoutProcess();
  5169. if (CanClose)
  5170. {
  5171. while (CanClose && (FLocalEditors->Count > 0))
  5172. {
  5173. int PrevCount = FLocalEditors->Count;
  5174. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  5175. CanClose = (FLocalEditors->Count < PrevCount);
  5176. }
  5177. if (CanClose)
  5178. {
  5179. CanClose = FEditorManager->Empty(true);
  5180. if (!CanClose)
  5181. {
  5182. SetFocus();
  5183. CanClose =
  5184. (MessageDialog(
  5185. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  5186. }
  5187. }
  5188. }
  5189. }
  5190. }
  5191. //---------------------------------------------------------------------------
  5192. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  5193. {
  5194. TForm * Form = dynamic_cast<TForm *>(Sender);
  5195. DebugAssert(Form != NULL);
  5196. Form->Close();
  5197. }
  5198. //---------------------------------------------------------------------------
  5199. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  5200. {
  5201. TForm * Form = dynamic_cast<TForm *>(Sender);
  5202. delete Form;
  5203. }
  5204. //---------------------------------------------------------------------------
  5205. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  5206. {
  5207. while (FLocalEditors->Count > 0)
  5208. {
  5209. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  5210. }
  5211. }
  5212. //---------------------------------------------------------------------------
  5213. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  5214. TObject *Sender)
  5215. {
  5216. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  5217. try
  5218. {
  5219. SetProperties(osRemote, FileList);
  5220. }
  5221. __finally
  5222. {
  5223. delete FileList;
  5224. }
  5225. }
  5226. //---------------------------------------------------------------------------
  5227. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  5228. {
  5229. if (value)
  5230. {
  5231. if (Component == fcCustomCommandsBand)
  5232. {
  5233. UpdateCustomCommandsToolbar();
  5234. }
  5235. if (Component == fcQueueFileList)
  5236. {
  5237. DebugAssert(!QueueFileListSplitter->Visible);
  5238. AdjustQueueLayout();
  5239. UpdateQueueFileList();
  5240. }
  5241. }
  5242. }
  5243. //---------------------------------------------------------------------------
  5244. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  5245. {
  5246. int MinHeightLimit =
  5247. GetStaticQueuePanelComponentsHeight() +
  5248. QueueFileListSplitter->Height +
  5249. QueueFileList->Height +
  5250. GetMinQueueViewHeight();
  5251. if (QueuePanel->ClientHeight < MinHeightLimit)
  5252. {
  5253. QueuePanel->ClientHeight = MinHeightLimit;
  5254. }
  5255. }
  5256. //---------------------------------------------------------------------------
  5257. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  5258. {
  5259. TControl * Control = GetComponent(Component);
  5260. DebugAssert(Control);
  5261. bool Changed = (Control->Visible != value);
  5262. if (Changed)
  5263. {
  5264. ComponentShowing(Component, value);
  5265. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  5266. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  5267. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  5268. if (value)
  5269. {
  5270. int RemainingHeight = Control->Parent->ClientHeight;
  5271. int RemainingWidth = Control->Parent->ClientWidth;
  5272. for (int i = 0; i < Control->Parent->ControlCount; i++)
  5273. {
  5274. TControl * ChildControl = Control->Parent->Controls[i];
  5275. if (ChildControl->Visible)
  5276. {
  5277. switch (ChildControl->Align)
  5278. {
  5279. case alTop:
  5280. case alBottom:
  5281. RemainingHeight -= ChildControl->Height;
  5282. break;
  5283. case alLeft:
  5284. case alRight:
  5285. RemainingWidth -= ChildControl->Width;
  5286. break;
  5287. }
  5288. }
  5289. }
  5290. int Reserve = ScaleByTextHeight(this, 32);
  5291. // queue in explorer, trees in commander
  5292. if (Control->Height > RemainingHeight - Reserve)
  5293. {
  5294. Control->Height = RemainingHeight / 2;
  5295. }
  5296. if (Control->Width > RemainingWidth - Reserve)
  5297. {
  5298. Control->Width = RemainingWidth / 2;
  5299. }
  5300. }
  5301. Control->Visible = value;
  5302. if (WasFocused && Visible)
  5303. {
  5304. DirView(osCurrent)->SetFocus();
  5305. }
  5306. FixControlsPlacement();
  5307. }
  5308. }
  5309. //---------------------------------------------------------------------------
  5310. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  5311. {
  5312. TControl * Control = GetComponent(Component);
  5313. if (Control == NULL)
  5314. {
  5315. return false;
  5316. }
  5317. else
  5318. {
  5319. return Control->Visible;
  5320. }
  5321. }
  5322. //---------------------------------------------------------------------------
  5323. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  5324. {
  5325. bool Result;
  5326. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  5327. {
  5328. Result = !IsUWP();
  5329. }
  5330. else
  5331. {
  5332. Result = true;
  5333. }
  5334. return Result;
  5335. }
  5336. //---------------------------------------------------------------------------
  5337. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  5338. {
  5339. if (DirView->ItemFocused != NULL)
  5340. {
  5341. DirView->ItemFocused->MakeVisible(false);
  5342. }
  5343. }
  5344. //---------------------------------------------------------------------------
  5345. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5346. {
  5347. MakeFocusedItemVisible(RemoteDirView);
  5348. QueueSplitter->Visible = QueuePanel->Visible;
  5349. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5350. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5351. TControl * TopControlsOrder[] =
  5352. { TopDock, MessageDock, SessionsPageControl };
  5353. SetVerticalControlsOrder(TopControlsOrder, LENOF(TopControlsOrder));
  5354. TControl * QueueControlsOrder[] =
  5355. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5356. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5357. }
  5358. //---------------------------------------------------------------------------
  5359. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5360. {
  5361. switch (Component) {
  5362. case fcStatusBar: return RemoteStatusBar;
  5363. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5364. case fcQueueView: return QueuePanel;
  5365. case fcQueueToolbar: return QueueDock;
  5366. case fcRemoteTree: return RemoteDrivePanel;
  5367. case fcSessionsTabs: return SessionsPageControl;
  5368. case fcQueueFileList: return QueueFileList;
  5369. default: return NULL;
  5370. }
  5371. }
  5372. //---------------------------------------------------------------------------
  5373. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5374. TObject *Sender, TListColumn *Column, TPoint &Point)
  5375. {
  5376. DebugAssert(NonVisualDataModule && Column && Sender);
  5377. NonVisualDataModule->ListColumn = Column;
  5378. TPopupMenu * DirViewColumnMenu;
  5379. if (Sender == DirView(osOther))
  5380. {
  5381. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5382. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible = (Column->Index == uvName);
  5383. bool SizeColumn = (Column->Index == uvSize);
  5384. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible = SizeColumn;
  5385. NonVisualDataModule->RemoteCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5386. }
  5387. else
  5388. {
  5389. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5390. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible = (Column->Index == dvName);
  5391. bool SizeColumn = (Column->Index == dvSize);
  5392. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible = SizeColumn;
  5393. NonVisualDataModule->LocalCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5394. }
  5395. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5396. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5397. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5398. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5399. }
  5400. //---------------------------------------------------------------------------
  5401. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5402. TObject *Sender, TListItem *Item, bool &AllowExec)
  5403. {
  5404. DoDirViewExecFile(Sender, Item, AllowExec);
  5405. }
  5406. //---------------------------------------------------------------------------
  5407. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5408. TListItem * Item, bool & AllowExec)
  5409. {
  5410. DebugAssert(Sender && Item && Configuration);
  5411. DebugAssert(AllowExec);
  5412. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5413. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5414. bool Remote = !IsSideLocalBrowser(Side);
  5415. DebugAssert(Item == ADirView->ItemFocused);
  5416. bool IsDirectory = ADirView->ItemIsDirectory(Item);
  5417. TResolvedDoubleClickAction Action;
  5418. if (FForceExecution)
  5419. {
  5420. Action = IsDirectory ? rdcaChangeDir : rdcaOpen;
  5421. }
  5422. else
  5423. {
  5424. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  5425. Action = WinConfiguration->ResolveDoubleClickAction(IsDirectory, ATerminal);
  5426. }
  5427. if (Action == rdcaCopy)
  5428. {
  5429. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5430. UnlockWindow();
  5431. try
  5432. {
  5433. if (IsLocalBrowserMode())
  5434. {
  5435. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5436. }
  5437. else
  5438. {
  5439. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5440. }
  5441. }
  5442. __finally
  5443. {
  5444. LockWindow();
  5445. }
  5446. AllowExec = false;
  5447. }
  5448. else if (Action == rdcaEdit)
  5449. {
  5450. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5451. {
  5452. ExecuteFile(Side, efDefaultEditor);
  5453. AllowExec = false;
  5454. }
  5455. }
  5456. else if (Action == rdcaOpen)
  5457. {
  5458. if (DebugAlwaysTrue(!IsDirectory) && Remote && !WinConfiguration->DisableOpenEdit)
  5459. {
  5460. ExecuteFile(osRemote, efShell);
  5461. AllowExec = false;
  5462. }
  5463. }
  5464. }
  5465. //---------------------------------------------------------------------------
  5466. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5467. {
  5468. return ((Side == osRemote) || (Side == osCurrent));
  5469. }
  5470. //---------------------------------------------------------------------------
  5471. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5472. {
  5473. DebugFail();
  5474. }
  5475. //---------------------------------------------------------------------------
  5476. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5477. {
  5478. DebugFail();
  5479. }
  5480. //---------------------------------------------------------------------------
  5481. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5482. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5483. {
  5484. TSynchronizeParamType Params;
  5485. Params.LocalDirectory = LocalDirectory;
  5486. Params.RemoteDirectory = RemoteDirectory;
  5487. int UnusedParams =
  5488. (GUIConfiguration->SynchronizeParams &
  5489. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5490. TTerminal::spCaseSensitive));
  5491. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5492. Params.Options = GUIConfiguration->SynchronizeOptions;
  5493. bool SaveSettings = false;
  5494. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5495. &DoSynchronizeTooManyDirectories);
  5496. DebugAssert(FSynchronizeController == NULL);
  5497. FSynchronizeController = &Controller;
  5498. bool Result;
  5499. try
  5500. {
  5501. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5502. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5503. int Options =
  5504. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5505. DebugAssert(FOnFeedSynchronizeError == NULL);
  5506. DebugAssert(FOnSynchronizeAbort == NULL);
  5507. DebugAssert(FSynchronizeTerminal == NULL);
  5508. FSynchronizeTerminal = Terminal;
  5509. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5510. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5511. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5512. if (Result)
  5513. {
  5514. if (SaveSettings)
  5515. {
  5516. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5517. GUIConfiguration->SynchronizeOptions = Params.Options;
  5518. }
  5519. else
  5520. {
  5521. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5522. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5523. FLAGSET(Params.Options, soSynchronize))
  5524. {
  5525. GUIConfiguration->SynchronizeOptions =
  5526. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5527. soSynchronize;
  5528. }
  5529. }
  5530. LocalDirectory = Params.LocalDirectory;
  5531. RemoteDirectory = Params.RemoteDirectory;
  5532. }
  5533. }
  5534. __finally
  5535. {
  5536. FSynchronizeController = NULL;
  5537. DebugAssert(FOnFeedSynchronizeError == NULL);
  5538. FOnFeedSynchronizeError = NULL;
  5539. DebugAssert(FOnSynchronizeAbort == NULL);
  5540. FOnSynchronizeAbort = NULL;
  5541. FSynchronizeTerminal = NULL;
  5542. }
  5543. return Result;
  5544. }
  5545. //---------------------------------------------------------------------------
  5546. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5547. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5548. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5549. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5550. TSynchronizeOptions * Options, bool Full)
  5551. {
  5552. // Keep-up-to-date mode
  5553. if (Terminal->Status == ssOpened)
  5554. {
  5555. try
  5556. {
  5557. int PParams = Params.Params;
  5558. if (!Full)
  5559. {
  5560. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5561. if (FLAGSET(Params.Options, soRecurse))
  5562. {
  5563. PParams |= TTerminal::spSubDirs;
  5564. }
  5565. }
  5566. else
  5567. {
  5568. // if keepuptodate is non-recursive,
  5569. // full sync before has to be non-recursive as well
  5570. if (FLAGCLEAR(Params.Options, soRecurse))
  5571. {
  5572. PParams |= TTerminal::spNoRecurse;
  5573. }
  5574. }
  5575. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5576. PParams, Checklist, Options);
  5577. }
  5578. catch (Exception & E)
  5579. {
  5580. if (!Terminal->Active)
  5581. {
  5582. ShowExtendedExceptionEx(Terminal, &E);
  5583. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5584. if (!Terminal->Active)
  5585. {
  5586. throw;
  5587. }
  5588. }
  5589. else
  5590. {
  5591. if (FLAGSET(Params.Options, soContinueOnError) &&
  5592. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5593. {
  5594. // noop, already logged in MoreMessageDialog
  5595. }
  5596. else
  5597. {
  5598. // We get mostly EAbort here, so this is noop.
  5599. // Exception is an error when listing directories while looking for differences.
  5600. ShowExtendedExceptionEx(Terminal, &E);
  5601. throw;
  5602. }
  5603. }
  5604. }
  5605. }
  5606. }
  5607. //---------------------------------------------------------------------------
  5608. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5609. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5610. const UnicodeString ErrorStr)
  5611. {
  5612. if (!Directory.IsEmpty())
  5613. {
  5614. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5615. }
  5616. else
  5617. {
  5618. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5619. }
  5620. }
  5621. //---------------------------------------------------------------------------
  5622. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5623. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5624. {
  5625. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5626. {
  5627. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5628. }
  5629. else
  5630. {
  5631. TMessageParams Params(mpNeverAskAgainCheck);
  5632. unsigned int Result = MessageDialog(
  5633. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5634. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5635. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5636. {
  5637. MaxDirectories *= 2;
  5638. if (Result == qaNeverAskAgain)
  5639. {
  5640. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5641. }
  5642. }
  5643. else
  5644. {
  5645. Abort();
  5646. }
  5647. }
  5648. }
  5649. //---------------------------------------------------------------------------
  5650. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5651. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5652. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5653. TSynchronizeOptions * Options)
  5654. {
  5655. // Keep-up-to-date mode
  5656. DebugAssert(!FAutoOperation);
  5657. void * BatchStorage;
  5658. BatchStart(BatchStorage);
  5659. FAutoOperation = true;
  5660. bool AnyOperation = false;
  5661. TDateTime StartTime = Now();
  5662. TSynchronizeChecklist * AChecklist = NULL;
  5663. try
  5664. {
  5665. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5666. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5667. {
  5668. FSynchronizeProgressForm->Start();
  5669. }
  5670. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5671. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5672. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5673. Options);
  5674. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5675. SAFE_DESTROY(FSynchronizeProgressForm);
  5676. AnyOperation = (AChecklist->CheckedCount > 0);
  5677. if (AnyOperation)
  5678. {
  5679. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5680. CreateProgressForm(&SynchronizeProgress);
  5681. try
  5682. {
  5683. Terminal->SynchronizeApply(
  5684. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5685. SynchronizeProcessedItem, NULL, NULL, NULL);
  5686. }
  5687. __finally
  5688. {
  5689. DestroyProgressForm();
  5690. }
  5691. }
  5692. }
  5693. __finally
  5694. {
  5695. if (Checklist == NULL)
  5696. {
  5697. delete AChecklist;
  5698. }
  5699. else
  5700. {
  5701. *Checklist = AChecklist;
  5702. }
  5703. FAutoOperation = false;
  5704. SAFE_DESTROY(FSynchronizeProgressForm);
  5705. BatchEnd(BatchStorage);
  5706. ReloadLocalDirectory();
  5707. if (AnyOperation)
  5708. {
  5709. OperationComplete(StartTime);
  5710. }
  5711. }
  5712. }
  5713. //---------------------------------------------------------------------------
  5714. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5715. {
  5716. // can be called from command line
  5717. return Visible &&
  5718. ((DirView(osRemote)->SelCount > 0) ||
  5719. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5720. }
  5721. //---------------------------------------------------------------------------
  5722. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5723. {
  5724. LogSynchronizeEvent(Terminal, Message);
  5725. }
  5726. //---------------------------------------------------------------------------
  5727. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5728. int Params, TSynchronizeOptions & Options)
  5729. {
  5730. DebugAssert(!IsLocalBrowserMode());
  5731. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5732. {
  5733. Options.Filter = new TStringList();
  5734. Options.Filter->CaseSensitive = false;
  5735. Options.Filter->Duplicates = Types::dupAccept;
  5736. if (DirView(osRemote)->SelCount > 0)
  5737. {
  5738. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5739. }
  5740. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5741. {
  5742. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5743. }
  5744. Options.Filter->Sort();
  5745. }
  5746. }
  5747. //---------------------------------------------------------------------------
  5748. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5749. {
  5750. TCopyParamType Defaults;
  5751. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5752. ConfigStorage->AccessMode = smRead;
  5753. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5754. {
  5755. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5756. }
  5757. ConfigStorage.reset(NULL);
  5758. std::unique_ptr<TStringList> Options(new TStringList());
  5759. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5760. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5761. UnicodeString Result;
  5762. if (Options->Count > 0)
  5763. {
  5764. Result =
  5765. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5766. }
  5767. return Result;
  5768. }
  5769. //---------------------------------------------------------------------------
  5770. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5771. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5772. {
  5773. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5774. UnicodeString AdditionalParams =
  5775. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5776. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5777. SerializeCopyParamForCommandLine(CopyParams),
  5778. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5779. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5780. ExecuteNewInstance(SessionName, AdditionalParams);
  5781. }
  5782. //---------------------------------------------------------------------------
  5783. struct TSynchronizeParams
  5784. {
  5785. UnicodeString LocalDirectory;
  5786. UnicodeString RemoteDirectory;
  5787. TSynchronizeMode Mode;
  5788. TSynchronizeChecklist * Checklist;
  5789. int Params;
  5790. TCopyParamType * CopyParam;
  5791. TDateTime CollectElapsed;
  5792. TDateTime * StartTime;
  5793. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5794. };
  5795. //---------------------------------------------------------------------------
  5796. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5797. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5798. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5799. {
  5800. DebugAssert(!FAutoOperation);
  5801. void * BatchStorage;
  5802. BatchStart(BatchStorage);
  5803. FAutoOperation = true;
  5804. TFileOperationStatistics Statistics;
  5805. TDateTime Start = Now();
  5806. try
  5807. {
  5808. Params.OnProcessedItem = OnProcessedItem;
  5809. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5810. CreateProgressForm(&SynchronizeProgress);
  5811. Terminal->SynchronizeApply(
  5812. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5813. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5814. &Statistics);
  5815. }
  5816. __finally
  5817. {
  5818. Params.OnProcessedItem = NULL;
  5819. FAutoOperation = false;
  5820. DestroyProgressForm();
  5821. BatchEnd(BatchStorage);
  5822. ReloadLocalDirectory();
  5823. }
  5824. if (WinConfiguration->SynchronizeSummary)
  5825. {
  5826. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5827. // The statistics should be 0 anyway in this case
  5828. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5829. {
  5830. Message += L"\n";
  5831. if (Statistics.FilesUploaded > 0)
  5832. {
  5833. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5834. }
  5835. if (Statistics.FilesDownloaded > 0)
  5836. {
  5837. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5838. }
  5839. if (Statistics.FilesDeletedLocal > 0)
  5840. {
  5841. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5842. }
  5843. if (Statistics.FilesDeletedRemote > 0)
  5844. {
  5845. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5846. }
  5847. }
  5848. TDateTime Elapsed = (Now() - Start);
  5849. Message +=
  5850. L"\n" +
  5851. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5852. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5853. TMessageParams Params(mpNeverAskAgainCheck);
  5854. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5855. if (Result == qaNeverAskAgain)
  5856. {
  5857. WinConfiguration->SynchronizeSummary = false;
  5858. }
  5859. }
  5860. }
  5861. //---------------------------------------------------------------------------
  5862. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5863. {
  5864. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5865. {
  5866. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5867. }
  5868. // Not set in keep-up-to-date mode - Synchronize() method
  5869. if (Token != NULL)
  5870. {
  5871. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5872. if (Params.OnProcessedItem != NULL)
  5873. {
  5874. Params.OnProcessedItem(NULL, ChecklistItem);
  5875. }
  5876. }
  5877. }
  5878. //---------------------------------------------------------------------------
  5879. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5880. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5881. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5882. {
  5883. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5884. *Params.StartTime = Now();
  5885. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5886. }
  5887. //---------------------------------------------------------------------------
  5888. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5889. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5890. {
  5891. // terminal can be already closed (e.g. dropped connection)
  5892. if (Terminal != NULL)
  5893. {
  5894. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5895. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5896. }
  5897. }
  5898. //---------------------------------------------------------------------------
  5899. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5900. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5901. {
  5902. DebugAssert(!IsLocalBrowserMode());
  5903. TAutoBatch AutoBatch(this);
  5904. TAutoFlag AutoOperationFlag(FAutoOperation);
  5905. if (Side == osRemote)
  5906. {
  5907. std::unique_ptr<TStrings> FileList(new TStringList());
  5908. FileList->AddObject(FileName, RemoteFile);
  5909. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5910. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5911. RemoteDirView->SaveSelection();
  5912. RemoteDirView->SaveSelectedNames();
  5913. try
  5914. {
  5915. Terminal->MoveFiles(FileList.get(), Target, FileMask, false);
  5916. }
  5917. catch(...)
  5918. {
  5919. RemoteDirView->DiscardSavedSelection();
  5920. throw;
  5921. }
  5922. RemoteDirView->RestoreSelection();
  5923. }
  5924. else if (DebugAlwaysTrue(Side == osLocal))
  5925. {
  5926. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5927. {
  5928. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5929. }
  5930. UnicodeString Directory = ExtractFileDir(FileName);
  5931. ReloadLocalDirectory(Directory);
  5932. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5933. if (!SamePaths(Directory, NewDirectory))
  5934. {
  5935. ReloadLocalDirectory(NewDirectory);
  5936. }
  5937. }
  5938. }
  5939. //---------------------------------------------------------------------------
  5940. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5941. {
  5942. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5943. if (Side == osLocal)
  5944. {
  5945. if (Action == TSynchronizeChecklist::saDownloadNew)
  5946. {
  5947. OpenFolderInExplorer(LocalPath);
  5948. }
  5949. else
  5950. {
  5951. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5952. }
  5953. }
  5954. else if (DebugAlwaysTrue(Side == osRemote))
  5955. {
  5956. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5957. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5958. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5959. if (!LocalPath.IsEmpty())
  5960. {
  5961. SessionData->LocalDirectory = LocalPath;
  5962. }
  5963. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5964. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5965. }
  5966. }
  5967. //---------------------------------------------------------------------------
  5968. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5969. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5970. const TCopyParamType * CopyParams)
  5971. {
  5972. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5973. UnicodeString AdditionalParams =
  5974. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5975. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5976. SerializeCopyParamForCommandLine(CopyParams),
  5977. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5978. LocalDirectory, RemoteDirectory, Mode, Params));
  5979. ExecuteNewInstance(SessionName, AdditionalParams);
  5980. }
  5981. //---------------------------------------------------------------------------
  5982. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5983. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5984. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5985. {
  5986. int Result;
  5987. bool SaveSettings = false;
  5988. int Options =
  5989. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5990. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5991. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5992. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5993. bool Continue =
  5994. (UseDefaults == 0) ||
  5995. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5996. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5997. if (Continue)
  5998. {
  5999. Configuration->Usage->Inc(L"Synchronizations");
  6000. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  6001. UpdateCopyParamCounters(CopyParam);
  6002. TSynchronizeOptions SynchronizeOptions;
  6003. GetSynchronizeOptions(Params, SynchronizeOptions);
  6004. if (SaveSettings)
  6005. {
  6006. GUIConfiguration->SynchronizeParams = Params;
  6007. }
  6008. else
  6009. {
  6010. SaveMode = false;
  6011. }
  6012. TDateTime StartTime = Now();
  6013. TSynchronizeChecklist * Checklist = NULL;
  6014. try
  6015. {
  6016. DebugAssert(!FAutoOperation);
  6017. FAutoOperation = true;
  6018. try
  6019. {
  6020. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  6021. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  6022. int Files = -1;
  6023. if (DataList->Count >= 1)
  6024. {
  6025. Files = StrToIntDef(DataList->Strings[0], Files);
  6026. }
  6027. else
  6028. {
  6029. DataList->Add(UnicodeString());
  6030. }
  6031. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  6032. FSynchronizeProgressForm->Start();
  6033. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  6034. static_cast<TTerminal::TSynchronizeMode>(Mode),
  6035. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  6036. &SynchronizeOptions);
  6037. if (Terminal->SessionData->CacheDirectories)
  6038. {
  6039. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  6040. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  6041. }
  6042. }
  6043. __finally
  6044. {
  6045. FAutoOperation = false;
  6046. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  6047. SAFE_DESTROY(FSynchronizeProgressForm);
  6048. }
  6049. TSynchronizeParams SynchronizeParams;
  6050. SynchronizeParams.LocalDirectory = LocalDirectory;
  6051. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  6052. SynchronizeParams.Mode = Mode;
  6053. SynchronizeParams.CopyParam = &CopyParam;
  6054. SynchronizeParams.Params = Params;
  6055. SynchronizeParams.Checklist = Checklist;
  6056. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  6057. SynchronizeParams.StartTime = &StartTime;
  6058. SynchronizeParams.OnProcessedItem = NULL;
  6059. Result = Checklist->Count;
  6060. if (Checklist->Count > 0)
  6061. {
  6062. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  6063. {
  6064. if (!DoSynchronizeChecklistDialog(
  6065. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  6066. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  6067. {
  6068. Result = -1;
  6069. }
  6070. }
  6071. else
  6072. {
  6073. FullSynchronize(SynchronizeParams, NULL, NULL);
  6074. }
  6075. }
  6076. else
  6077. {
  6078. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  6079. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  6080. }
  6081. }
  6082. __finally
  6083. {
  6084. delete Checklist;
  6085. }
  6086. OperationComplete(StartTime);
  6087. }
  6088. else
  6089. {
  6090. Result = -1;
  6091. }
  6092. return Result;
  6093. }
  6094. //---------------------------------------------------------------------------
  6095. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  6096. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  6097. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  6098. {
  6099. if (Collect)
  6100. {
  6101. DebugAssert(FSynchronizeProgressForm != NULL);
  6102. if (!FSynchronizeProgressForm->Started)
  6103. {
  6104. FSynchronizeProgressForm->Start();
  6105. }
  6106. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  6107. SetTaskbarListProgressValue(CompareProgress);
  6108. }
  6109. else
  6110. {
  6111. DebugAssert(FSynchronizeProgressForm == NULL);
  6112. DebugAssert(FProgressForm != NULL);
  6113. }
  6114. }
  6115. //---------------------------------------------------------------------------
  6116. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  6117. {
  6118. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  6119. std::unique_ptr<TRemoteFile> File(Terminal->ReadFile(FullFileName));
  6120. if (File.get() != NULL)
  6121. {
  6122. ExecuteRemoteFile(FullFileName, File.get(), efInternalEditor);
  6123. Application->ShowMainForm = false;
  6124. Application->Run();
  6125. }
  6126. }
  6127. //---------------------------------------------------------------------------
  6128. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  6129. {
  6130. DebugFail();
  6131. }
  6132. //---------------------------------------------------------------------------
  6133. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  6134. {
  6135. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  6136. SessionData->Assign(ManagedSession->SessionData);
  6137. UpdateSessionData(SessionData.get());
  6138. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  6139. if (IsActiveTerminal(ManagedSession))
  6140. {
  6141. ManagedSession->UpdateSessionCredentials(SessionData.get());
  6142. }
  6143. if (!ManagedSession->SessionData->HasSessionName())
  6144. {
  6145. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  6146. // once IsWorkspace is cleared
  6147. SessionData->Name = UnicodeString();
  6148. }
  6149. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  6150. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  6151. SessionData->IsWorkspace = false;
  6152. return SessionData.release();
  6153. }
  6154. //---------------------------------------------------------------------------
  6155. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  6156. {
  6157. TSessionData * SessionData = CloneCurrentSessionData();
  6158. try
  6159. {
  6160. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  6161. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  6162. }
  6163. __finally
  6164. {
  6165. delete SessionData;
  6166. }
  6167. }
  6168. //---------------------------------------------------------------------------
  6169. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  6170. {
  6171. TTerminalManager * Manager = TTerminalManager::Instance();
  6172. std::unique_ptr<TObjectList> DataList(new TObjectList());
  6173. if (DebugAlwaysTrue(ManagedSession != NULL))
  6174. {
  6175. // Update ManagedSession->StateData
  6176. UpdateSession(ManagedSession);
  6177. }
  6178. Manager->SaveWorkspace(DataList.get());
  6179. return DataList.release();
  6180. }
  6181. //---------------------------------------------------------------------------
  6182. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  6183. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  6184. {
  6185. WinConfiguration->LastWorkspace = Name;
  6186. if (!SavePasswords)
  6187. {
  6188. for (int Index = 0; Index < DataList->Count; Index++)
  6189. {
  6190. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6191. if (SessionData->Link.IsEmpty())
  6192. {
  6193. SessionData->ClearSessionPasswords();
  6194. }
  6195. }
  6196. }
  6197. StoredSessions->NewWorkspace(Name, DataList);
  6198. // modified only
  6199. StoredSessions->Save(false, Explicit);
  6200. }
  6201. //---------------------------------------------------------------------------
  6202. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  6203. {
  6204. return
  6205. DefaultStr(WinConfiguration->LastWorkspace,
  6206. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  6207. }
  6208. //---------------------------------------------------------------------------
  6209. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  6210. {
  6211. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  6212. bool AnyNonStoredSessionWithPassword = false;
  6213. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  6214. bool AllNonStoredSessionsAnonymous = true;
  6215. TTerminalManager * Manager = TTerminalManager::Instance();
  6216. for (int Index = 0; Index < DataList->Count; Index++)
  6217. {
  6218. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6219. if (SessionData->Link.IsEmpty())
  6220. {
  6221. if (SessionData->HasAnySessionPassword())
  6222. {
  6223. AnyNonStoredSessionWithPassword = true;
  6224. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  6225. {
  6226. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  6227. }
  6228. }
  6229. if (!SameText(SessionData->UserName, AnonymousUserName))
  6230. {
  6231. AllNonStoredSessionsAnonymous = false;
  6232. }
  6233. }
  6234. }
  6235. bool SavePasswords;
  6236. bool * PSavePasswords;
  6237. bool NotRecommendedSavingPasswords =
  6238. !CustomWinConfiguration->UseMasterPassword &&
  6239. !AllNonStoredSessionsAnonymous;
  6240. if (Configuration->DisablePasswordStoring ||
  6241. !AnyNonStoredSessionWithPassword)
  6242. {
  6243. PSavePasswords = NULL;
  6244. SavePasswords = false;
  6245. }
  6246. else
  6247. {
  6248. PSavePasswords = &SavePasswords;
  6249. SavePasswords =
  6250. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  6251. !NotRecommendedSavingPasswords;
  6252. }
  6253. UnicodeString Name = WorkspaceName();
  6254. bool CreateShortcut = false;
  6255. bool Result =
  6256. DoSaveWorkspaceDialog(
  6257. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  6258. EnableAutoSave);
  6259. if (Result)
  6260. {
  6261. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  6262. if (CreateShortcut)
  6263. {
  6264. TOperationVisualizer Visualizer;
  6265. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  6266. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  6267. }
  6268. if (EnableAutoSave)
  6269. {
  6270. WinConfiguration->AutoSaveWorkspace = true;
  6271. WinConfiguration->AutoWorkspace = Name;
  6272. if (PSavePasswords != NULL)
  6273. {
  6274. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  6275. }
  6276. }
  6277. }
  6278. return Result;
  6279. }
  6280. //---------------------------------------------------------------------------
  6281. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  6282. {
  6283. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  6284. DebugAssert(Session == ManagedSession);
  6285. if (!IsLocalBrowserMode())
  6286. {
  6287. SAFE_DESTROY(Session->RemoteExplorerState);
  6288. if (WinConfiguration->PreservePanelState)
  6289. {
  6290. Session->RemoteExplorerState = RemoteDirView->SaveState();
  6291. }
  6292. }
  6293. UpdateSessionData(Session->StateData);
  6294. }
  6295. //---------------------------------------------------------------------------
  6296. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  6297. {
  6298. // Keep in sync with TSessionData::CopyStateData
  6299. DebugAssert(Data != NULL);
  6300. // This is inconsistent with how color (for example) is handled.
  6301. Data->Name = ManagedSession->SessionData->Name;
  6302. if (!IsLocalBrowserMode())
  6303. {
  6304. // cannot use RemoteDirView->Path, because it is empty if connection
  6305. // was already closed
  6306. // also only peek, we may not be connected at all atm
  6307. // so make sure we do not try retrieving current directory from the server
  6308. // (particularly with FTP)
  6309. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  6310. if (!ACurrentDirectory.IsEmpty())
  6311. {
  6312. Data->RemoteDirectory = ACurrentDirectory;
  6313. }
  6314. }
  6315. Data->Color = SessionColor;
  6316. }
  6317. //---------------------------------------------------------------------------
  6318. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  6319. {
  6320. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  6321. DebugAssert(Toolbar != NULL);
  6322. for (int i = 0; i < Toolbar->Items->Count; i++)
  6323. {
  6324. TTBXCustomDropDownItem * DropDownItem;
  6325. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6326. if (DropDownItem != NULL)
  6327. {
  6328. ToolbarItemResize(DropDownItem,
  6329. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  6330. Toolbar->NonClientWidth);
  6331. break;
  6332. }
  6333. }
  6334. }
  6335. //---------------------------------------------------------------------------
  6336. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6337. {
  6338. Item->EditWidth = Width;
  6339. }
  6340. //---------------------------------------------------------------------------
  6341. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6342. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6343. {
  6344. for (int i = 0; i < Toolbar->Items->Count; i++)
  6345. {
  6346. TTBXCustomDropDownItem * DropDownItem;
  6347. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6348. if (DropDownItem != NULL)
  6349. {
  6350. ASize.x -= DropDownItem->EditWidth;
  6351. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  6352. }
  6353. }
  6354. }
  6355. //---------------------------------------------------------------------------
  6356. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6357. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6358. {
  6359. if (WinConfiguration->DDWarnLackOfTempSpace)
  6360. {
  6361. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6362. if (!ADrive.IsEmpty())
  6363. {
  6364. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6365. DebugAssert(RequiredSpace >= 0);
  6366. __int64 RequiredWithReserve;
  6367. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6368. if (FreeSpace < RequiredWithReserve)
  6369. {
  6370. unsigned int Result;
  6371. TMessageParams Params(mpNeverAskAgainCheck);
  6372. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6373. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6374. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6375. if (Result == qaNeverAskAgain)
  6376. {
  6377. WinConfiguration->DDWarnLackOfTempSpace = false;
  6378. }
  6379. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6380. }
  6381. }
  6382. }
  6383. }
  6384. //---------------------------------------------------------------------------
  6385. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6386. {
  6387. DoOpenDirectoryDialog(odAddBookmark, Side);
  6388. }
  6389. //---------------------------------------------------------------------------
  6390. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6391. {
  6392. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6393. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6394. try
  6395. {
  6396. TCustomDirView * DView = DirView(Side);
  6397. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6398. {
  6399. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6400. }
  6401. }
  6402. catch (...)
  6403. {
  6404. delete VisitedDirectories;
  6405. throw;
  6406. }
  6407. return VisitedDirectories;
  6408. }
  6409. //---------------------------------------------------------------------------
  6410. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6411. TOpenDirectoryMode Mode, TOperationSide Side)
  6412. {
  6413. bool Continue = true;
  6414. if (Mode == odAddBookmark)
  6415. {
  6416. TMessageParams Params(mpNeverAskAgainCheck);
  6417. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6418. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6419. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6420. unsigned int Answer =
  6421. MessageDialog(Message,
  6422. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6423. if (Answer == qaNeverAskAgain)
  6424. {
  6425. Continue = true;
  6426. WinConfiguration->UseSharedBookmarks = true;
  6427. }
  6428. else if (Answer == qaOK)
  6429. {
  6430. Continue = true;
  6431. WinConfiguration->UseSharedBookmarks = false;
  6432. }
  6433. else
  6434. {
  6435. Continue = false;
  6436. }
  6437. }
  6438. if (Continue)
  6439. {
  6440. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6441. try
  6442. {
  6443. UnicodeString Name = DirView(Side)->PathName;
  6444. bool Repeat;
  6445. do
  6446. {
  6447. Repeat = false;
  6448. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6449. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6450. // do not allow switching to location profiles,
  6451. // if we are not connected
  6452. HasDirView[osLocal] && (Terminal != NULL)))
  6453. {
  6454. TWindowLock Lock(this);
  6455. if (!TryOpenDirectory(Side, Name))
  6456. {
  6457. Repeat = true;
  6458. Mode = odBrowse;
  6459. }
  6460. }
  6461. }
  6462. while (Repeat);
  6463. }
  6464. __finally
  6465. {
  6466. delete VisitedDirectories;
  6467. }
  6468. }
  6469. }
  6470. //---------------------------------------------------------------------------
  6471. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6472. {
  6473. DebugAssert(!Terminal->CommandSessionOpened);
  6474. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6475. }
  6476. //---------------------------------------------------------------------------
  6477. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6478. {
  6479. bool Result = Terminal->IsCapable[Capability] ||
  6480. Terminal->CommandSessionOpened;
  6481. if (!Result)
  6482. {
  6483. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6484. if (!GUIConfiguration->ConfirmCommandSession)
  6485. {
  6486. Result = true;
  6487. }
  6488. else
  6489. {
  6490. TMessageParams Params(mpNeverAskAgainCheck);
  6491. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6492. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6493. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6494. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6495. if (Answer == qaNeverAskAgain)
  6496. {
  6497. GUIConfiguration->ConfirmCommandSession = false;
  6498. Result = true;
  6499. }
  6500. else if (Answer == qaOK)
  6501. {
  6502. Result = true;
  6503. }
  6504. }
  6505. if (Result)
  6506. {
  6507. Result = CommandSessionFallback();
  6508. }
  6509. }
  6510. return Result;
  6511. }
  6512. //---------------------------------------------------------------------------
  6513. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6514. {
  6515. if (EnsureCommandSessionFallback(fcAnyCommand))
  6516. {
  6517. DoConsoleDialog(Terminal, Command);
  6518. }
  6519. }
  6520. //---------------------------------------------------------------------------
  6521. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6522. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6523. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6524. {
  6525. if (!IsSideLocalBrowser(osRemote) &&
  6526. IsFileControl(DropSourceControl, osRemote) &&
  6527. (FDDExtMapFile != NULL))
  6528. {
  6529. Accept = true;
  6530. }
  6531. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6532. DebugAssert(FDDTargetControl != NULL);
  6533. }
  6534. //---------------------------------------------------------------------------
  6535. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6536. _di_IDataObject DataObj, int KeyState,
  6537. const TPoint & Point, int & Effect, bool & Accept)
  6538. {
  6539. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6540. }
  6541. //---------------------------------------------------------------------------
  6542. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6543. _di_IDataObject DataObj, int KeyState,
  6544. const TPoint & Point, int & Effect, bool & Accept)
  6545. {
  6546. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6547. }
  6548. //---------------------------------------------------------------------------
  6549. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6550. TObject * Sender)
  6551. {
  6552. DebugUsedParam(Sender);
  6553. DebugAssert(FDDTargetControl == Sender);
  6554. FDDTargetControl = NULL;
  6555. }
  6556. //---------------------------------------------------------------------------
  6557. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6558. {
  6559. FileControlDDDragLeave(SessionsPageControl);
  6560. }
  6561. //---------------------------------------------------------------------------
  6562. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6563. {
  6564. FileControlDDDragLeave(QueueView3);
  6565. }
  6566. //---------------------------------------------------------------------------
  6567. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6568. {
  6569. DebugAssert(!IsSideLocalBrowser(Side));
  6570. DebugUsedParam(Side);
  6571. bool Edit = false;
  6572. TRemoteFile * File = NULL;
  6573. UnicodeString FileName;
  6574. UnicodeString PointTo;
  6575. bool SymbolicLink = true;
  6576. if (RemoteDirView->ItemFocused)
  6577. {
  6578. DebugAssert(RemoteDirView->ItemFocused->Data);
  6579. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6580. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6581. if (Edit)
  6582. {
  6583. FileName = File->FileName;
  6584. PointTo = File->LinkTo;
  6585. }
  6586. else if (!File->IsParentDirectory)
  6587. {
  6588. PointTo = File->FileName;
  6589. }
  6590. }
  6591. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6592. Terminal->IsCapable[fcHardLink]))
  6593. {
  6594. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6595. if (Edit)
  6596. {
  6597. DebugAssert(File->FileName == FileName);
  6598. int Params = dfNoRecursive;
  6599. Terminal->ExceptionOnFail = true;
  6600. try
  6601. {
  6602. Terminal->DeleteFile(L"", File, &Params);
  6603. }
  6604. __finally
  6605. {
  6606. Terminal->ExceptionOnFail = false;
  6607. }
  6608. }
  6609. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6610. }
  6611. }
  6612. //---------------------------------------------------------------------------
  6613. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6614. {
  6615. return
  6616. (IsSideLocalBrowser(Side) ||
  6617. (HasActiveTerminal() &&
  6618. Terminal->ResolvingSymlinks &&
  6619. Terminal->IsCapable[fcSymbolicLink]));
  6620. }
  6621. //---------------------------------------------------------------------------
  6622. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6623. {
  6624. return
  6625. !IsSideLocalBrowser(FCurrentSide) &&
  6626. (RemoteDirView->ItemFocused != NULL) &&
  6627. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6628. Terminal->SessionData->ResolveSymlinks;
  6629. }
  6630. //---------------------------------------------------------------------------
  6631. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6632. {
  6633. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6634. FForceExecution = true;
  6635. try
  6636. {
  6637. DirView(osCurrent)->ExecuteCurrentFile();
  6638. }
  6639. __finally
  6640. {
  6641. FForceExecution = false;
  6642. }
  6643. }
  6644. //---------------------------------------------------------------------------
  6645. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6646. {
  6647. TEditorData ExternalEditor;
  6648. ExternalEditor.Editor = edExternal;
  6649. bool Remember = false;
  6650. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6651. {
  6652. if (Remember)
  6653. {
  6654. TEditorList * EditorList = new TEditorList;
  6655. try
  6656. {
  6657. *EditorList = *WinConfiguration->EditorList;
  6658. bool Found = false;
  6659. int i = 0;
  6660. while (!Found && (i < EditorList->Count))
  6661. {
  6662. const TEditorPreferences * Editor = EditorList->Editors[i];
  6663. if ((Editor->Data->Editor == edExternal) &&
  6664. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6665. {
  6666. Found = true;
  6667. }
  6668. i++;
  6669. }
  6670. if (!Found)
  6671. {
  6672. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6673. WinConfiguration->EditorList = EditorList;
  6674. }
  6675. }
  6676. __finally
  6677. {
  6678. delete EditorList;
  6679. }
  6680. }
  6681. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6682. }
  6683. }
  6684. //---------------------------------------------------------------------------
  6685. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6686. {
  6687. if (FFileFindTerminal == ATerminal)
  6688. {
  6689. FFileFindTerminal = NULL;
  6690. HideFileFindDialog();
  6691. }
  6692. if (FClipboardTerminal == ATerminal)
  6693. {
  6694. ClipboardClear(); // implies ClipboardStop
  6695. }
  6696. if ((FOnSynchronizeAbort != NULL) &&
  6697. (ATerminal == FManagedSession))
  6698. {
  6699. FOnSynchronizeAbort(NULL);
  6700. }
  6701. }
  6702. //---------------------------------------------------------------------------
  6703. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6704. {
  6705. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  6706. DetachTerminal(Sender);
  6707. }
  6708. //---------------------------------------------------------------------------
  6709. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6710. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6711. {
  6712. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6713. DebugAssert(Terminal != NULL);
  6714. if (Data->Terminal == Terminal)
  6715. {
  6716. Data->Terminal = NULL;
  6717. }
  6718. }
  6719. //---------------------------------------------------------------------------
  6720. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6721. {
  6722. return NULL;
  6723. }
  6724. //---------------------------------------------------------------------------
  6725. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6726. {
  6727. UpdateControls();
  6728. SessionColor = TColor(0);
  6729. UpdateRemotePathComboBox(false);
  6730. NeedSession(false);
  6731. }
  6732. //---------------------------------------------------------------------------
  6733. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6734. {
  6735. try
  6736. {
  6737. // Cache, as the login dialog can change its value.
  6738. // See also DoShow. Also see TTerminalManager::NewSession.
  6739. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6740. try
  6741. {
  6742. if (ShowLogin)
  6743. {
  6744. bool ReloadSessions = !Startup;
  6745. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6746. }
  6747. }
  6748. __finally
  6749. {
  6750. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6751. // (so there was no chance for the user to open any session yet)
  6752. if ((ShowLogin || !Startup) &&
  6753. !WinConfiguration->KeepOpenWhenNoSession &&
  6754. // this rules out the implicit Commander's local-local browser
  6755. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6756. {
  6757. TerminateApplication();
  6758. }
  6759. }
  6760. }
  6761. catch (EAbort &)
  6762. {
  6763. // swallow
  6764. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6765. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6766. }
  6767. }
  6768. //---------------------------------------------------------------------------
  6769. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6770. {
  6771. TTerminalManager * Manager = TTerminalManager::Instance();
  6772. DebugAssert(!Manager->Updating);
  6773. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6774. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6775. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6776. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6777. try
  6778. {
  6779. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6780. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6781. {
  6782. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6783. }
  6784. for (int Index = 0; Index <= Manager->Count; Index++)
  6785. {
  6786. TThemeTabSheet * TabSheet;
  6787. if (Index >= SessionsPageControl->PageCount)
  6788. {
  6789. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6790. TabSheet->PageControl = SessionsPageControl;
  6791. }
  6792. else
  6793. {
  6794. TabSheet = SessionsPageControl->Pages[Index];
  6795. }
  6796. bool IsSessionTab = (Index < Manager->Count);
  6797. if (IsSessionTab)
  6798. {
  6799. TTerminal * Terminal = Manager->Sessions[Index];
  6800. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6801. TabSheet->CaptionTruncation = tttEllipsis;
  6802. UpdateSessionTab(TabSheet);
  6803. }
  6804. else
  6805. {
  6806. TabSheet->Tag = 0; // not really needed
  6807. TabSheet->Shadowed = false;
  6808. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6809. TabSheet->CaptionTruncation = tttNoText;
  6810. // 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)
  6811. UpdateNewTabTab();
  6812. }
  6813. }
  6814. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6815. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6816. {
  6817. SessionsPageControl->UpdateTabsCaptionTruncation();
  6818. }
  6819. }
  6820. __finally
  6821. {
  6822. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6823. }
  6824. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6825. }
  6826. //---------------------------------------------------------------------------
  6827. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6828. {
  6829. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6830. }
  6831. //---------------------------------------------------------------------------
  6832. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6833. {
  6834. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6835. UnicodeString TabCaption;
  6836. if (WinConfiguration->SelectiveToolbarText)
  6837. {
  6838. TabCaption = GetNewTabTabCaption();
  6839. }
  6840. TabSheet->BaseCaption = TabCaption;
  6841. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6842. }
  6843. //---------------------------------------------------------------------------
  6844. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6845. {
  6846. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6847. }
  6848. //---------------------------------------------------------------------------
  6849. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6850. {
  6851. DebugFail();
  6852. return UnicodeString();
  6853. }
  6854. //---------------------------------------------------------------------------
  6855. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6856. {
  6857. if (DebugAlwaysTrue(TabSheet != NULL))
  6858. {
  6859. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6860. if (DebugAlwaysTrue(ASession != NULL))
  6861. {
  6862. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6863. if (ASession->LocalBrowser)
  6864. {
  6865. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6866. }
  6867. else
  6868. {
  6869. TabSheet->ImageIndex = AddSessionColor(Color);
  6870. }
  6871. TTerminalManager * Manager = TTerminalManager::Instance();
  6872. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6873. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6874. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6875. }
  6876. }
  6877. }
  6878. //---------------------------------------------------------------------------
  6879. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6880. {
  6881. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6882. }
  6883. //---------------------------------------------------------------------------
  6884. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6885. {
  6886. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6887. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6888. bool Result = (Session != NULL);
  6889. if (Result)
  6890. {
  6891. TTerminalManager::Instance()->ActiveSession = Session;
  6892. }
  6893. else
  6894. {
  6895. try
  6896. {
  6897. NewTab();
  6898. }
  6899. __finally
  6900. {
  6901. SessionListChanged();
  6902. }
  6903. FSessionsPageControlNewTabTime = Now();
  6904. }
  6905. return Result;
  6906. }
  6907. //---------------------------------------------------------------------------
  6908. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6909. {
  6910. TWindowLock Lock(this);
  6911. SessionTabSwitched();
  6912. }
  6913. //---------------------------------------------------------------------------
  6914. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6915. {
  6916. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6917. DebugAssert(TransferList != NULL);
  6918. UnicodeString Name;
  6919. if (TransferList->ItemIndex <= 0)
  6920. {
  6921. Name = L"";
  6922. }
  6923. else
  6924. {
  6925. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6926. }
  6927. if (FCopyParamAutoSelected.IsEmpty())
  6928. {
  6929. // if previous preset was not autoselected, make new preset the "default"
  6930. FCopyParamDefault = Name;
  6931. }
  6932. GUIConfiguration->CopyParamCurrent = Name;
  6933. }
  6934. //---------------------------------------------------------------------------
  6935. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6936. {
  6937. // sanity check
  6938. bool ExistingPreset =
  6939. (FTransferListHoverIndex >= 0) &&
  6940. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6941. DebugAssert(ExistingPreset);
  6942. if (ExistingPreset)
  6943. {
  6944. HDC DC = GetDC(0);
  6945. TCanvas * Canvas = new TCanvas();
  6946. try
  6947. {
  6948. Canvas->Handle = DC;
  6949. Canvas->Font = GetToolbarFont(this);
  6950. UnicodeString Name;
  6951. if (FTransferListHoverIndex == 0)
  6952. {
  6953. Name = L"";
  6954. }
  6955. else
  6956. {
  6957. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6958. }
  6959. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6960. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6961. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6962. static_cast<TObject*>(GetComponent(fcTransferList)));
  6963. UnicodeString InfoStr =
  6964. GUIConfiguration->CopyParamPreset[Name].
  6965. GetInfoStr(L"; ",
  6966. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6967. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6968. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6969. {
  6970. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6971. {
  6972. InfoStr.SetLength(InfoStr.Length() - 1);
  6973. }
  6974. InfoStr += Ellipsis;
  6975. }
  6976. // UpdateCaption does not cause invalidation of whole submenu, while
  6977. // setting Caption property does.
  6978. // also it probably does not resize the label, even if necessary
  6979. // (we do not want that anyway)
  6980. TransferLabel->UpdateCaption(InfoStr);
  6981. }
  6982. __finally
  6983. {
  6984. Canvas->Handle = NULL;
  6985. ReleaseDC(0, DC);
  6986. delete Canvas;
  6987. }
  6988. }
  6989. }
  6990. //---------------------------------------------------------------------------
  6991. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6992. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6993. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6994. {
  6995. if (FTransferListHoverIndex != AHoverIndex)
  6996. {
  6997. FTransferListHoverIndex = AHoverIndex;
  6998. UpdateTransferLabel();
  6999. }
  7000. }
  7001. //---------------------------------------------------------------------------
  7002. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  7003. {
  7004. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  7005. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  7006. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  7007. {
  7008. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  7009. {
  7010. if (!NonVisualDataModule->Busy)
  7011. {
  7012. OpenDirectory(GetSide(osCurrent));
  7013. }
  7014. Message.Result = 1;
  7015. }
  7016. else
  7017. {
  7018. TForm::Dispatch(&Message);
  7019. }
  7020. }
  7021. else
  7022. {
  7023. TForm::Dispatch(&Message);
  7024. }
  7025. }
  7026. //---------------------------------------------------------------------------
  7027. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  7028. {
  7029. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  7030. if ((FIgnoreNextDialogChar != 0) &&
  7031. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  7032. {
  7033. Message.Result = 1;
  7034. }
  7035. else
  7036. {
  7037. TForm::Dispatch(&Message);
  7038. }
  7039. FIgnoreNextDialogChar = 0;
  7040. }
  7041. //---------------------------------------------------------------------------
  7042. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  7043. {
  7044. // The four low-order bits are used internally by Windows
  7045. unsigned int Cmd = (Message.WParam & 0xFFF0);
  7046. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  7047. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  7048. // 61730 - restore thru double click - undocumented
  7049. // 61490 - maximize thru double click - undocumented
  7050. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  7051. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  7052. {
  7053. SysResizing(Cmd);
  7054. }
  7055. TForm::Dispatch(&Message);
  7056. }
  7057. //---------------------------------------------------------------------------
  7058. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  7059. {
  7060. // We were actually never able to make ENDSESSION_CRITICAL happen.
  7061. // Also there no point returning TRUE as we are not able to
  7062. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  7063. // Hence the process termination might be safer :)
  7064. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  7065. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  7066. {
  7067. Message.Result = FALSE;
  7068. }
  7069. else
  7070. {
  7071. Message.Result = TRUE;
  7072. }
  7073. // Do not call default handling as that triggers OnCloseQuery,
  7074. // where our implementation will popup confirmation dialogs, what we do not want,
  7075. // as per Vista guidelines:
  7076. // https://learn.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  7077. }
  7078. //---------------------------------------------------------------------------
  7079. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  7080. {
  7081. if (Message.EndSession && IsApplicationMinimized())
  7082. {
  7083. // WORKAROUND
  7084. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  7085. // when it receives WM_ENDSESSION,
  7086. // but that sometimes (particularly when application is minimized) cause crashes.
  7087. // Crash popups message that blocks log off.
  7088. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  7089. // so we call ExitProcess() immediately, not even trying to cleanup.
  7090. // It still causes beep, so there's likely some popup, but it does not block
  7091. // log off.
  7092. ExitProcess(0);
  7093. }
  7094. TForm::Dispatch(&Message);
  7095. }
  7096. #ifdef _DEBUG
  7097. //---------------------------------------------------------------------------
  7098. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  7099. {
  7100. TForm::Dispatch(&Message);
  7101. }
  7102. #endif
  7103. //---------------------------------------------------------------------------
  7104. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  7105. {
  7106. }
  7107. //---------------------------------------------------------------------------
  7108. void __fastcall TCustomScpExplorerForm::DoShow()
  7109. {
  7110. // Called from TCustomScpExplorerForm::CMShowingChanged
  7111. // only now are the controls resized finally, so the size constraints
  7112. // will not conflict with possibly very small window size
  7113. RestoreFormParams();
  7114. // This indicates that the form was rescaled after parameters were loaded and
  7115. // as most probably the parameters were actually saved with the current form scaling
  7116. // (windows was the last time likely closed on the same monitor, where it is starting now),
  7117. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  7118. // and no rounding problems will occur.
  7119. // See also RestoreForm.
  7120. // (we should be safe to call RestoreParams unconditionally)
  7121. if (PixelsPerInch != Screen->PixelsPerInch)
  7122. {
  7123. RestoreParams();
  7124. }
  7125. FixControlsPlacement();
  7126. if (Position == poDefaultPosOnly)
  7127. {
  7128. CutFormToDesktop(this);
  7129. }
  7130. TForm::DoShow();
  7131. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  7132. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  7133. // This was previously in NeedSession, where other related code is.
  7134. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  7135. // unpleasantly shows briefly.
  7136. if ((ManagedSession == NULL) &&
  7137. !WinConfiguration->ShowLoginWhenNoSession &&
  7138. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  7139. // This detects if workspace was saved the last time the main widow was closed
  7140. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  7141. {
  7142. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  7143. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  7144. }
  7145. // Before main window displays
  7146. if (ManagedSession == NULL)
  7147. {
  7148. StartingWithoutSession();
  7149. }
  7150. UpdatePixelsPerInchMainWindowCounter();
  7151. FShowing = true;
  7152. }
  7153. //---------------------------------------------------------------------------
  7154. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  7155. {
  7156. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  7157. }
  7158. //---------------------------------------------------------------------------
  7159. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  7160. {
  7161. SessionListChanged();
  7162. InitStatusBar();
  7163. UpdateControls();
  7164. }
  7165. //---------------------------------------------------------------------------
  7166. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  7167. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  7168. TNotifyEvent OnBalloonClick, TObject * UserData)
  7169. {
  7170. bool Do;
  7171. UnicodeString Message;
  7172. if (E == NULL)
  7173. {
  7174. Message = Str;
  7175. Do = true;
  7176. }
  7177. else
  7178. {
  7179. Do = ExceptionMessage(E, Message);
  7180. }
  7181. if (Do && WinConfiguration->BalloonNotifications)
  7182. {
  7183. UnicodeString Title;
  7184. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  7185. {
  7186. Message = TrimLeft(Message);
  7187. }
  7188. else
  7189. {
  7190. Message = UnformatMessage(Message);
  7191. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  7192. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  7193. Title = LoadResourceString(Captions[Type]);
  7194. if (Terminal != NULL)
  7195. {
  7196. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7197. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  7198. }
  7199. }
  7200. if (Seconds == 0)
  7201. {
  7202. Seconds = WinConfiguration->NotificationsTimeout;
  7203. }
  7204. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  7205. }
  7206. }
  7207. //---------------------------------------------------------------------------
  7208. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  7209. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  7210. UnicodeString HelpKeyword, const TMessageParams * Params,
  7211. TTerminal * Terminal)
  7212. {
  7213. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  7214. (FOnFeedSynchronizeError != NULL)) &&
  7215. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  7216. {
  7217. if (FOnFeedSynchronizeError != NULL)
  7218. {
  7219. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  7220. }
  7221. else
  7222. {
  7223. DebugAssert(FErrorList != NULL);
  7224. TStringList * MoreMessagesCopy = NULL;
  7225. if (MoreMessages)
  7226. {
  7227. MoreMessagesCopy = new TStringList();
  7228. MoreMessagesCopy->Assign(MoreMessages);
  7229. }
  7230. FErrorList->AddObject(Message, MoreMessagesCopy);
  7231. }
  7232. PopupTrayBalloon(Terminal, Message, Type);
  7233. return ContinueAnswer(Answers);
  7234. }
  7235. else
  7236. {
  7237. bool UseBalloon = FTrayIcon->Visible;
  7238. if (UseBalloon)
  7239. {
  7240. PopupTrayBalloon(Terminal, Message, Type);
  7241. }
  7242. unsigned int Result;
  7243. try
  7244. {
  7245. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  7246. }
  7247. __finally
  7248. {
  7249. // cancel only balloon we popped up, otherwise we may cancel notification
  7250. // balloon that was there before the message dialog
  7251. // (such as "dd confirmation opt in balloon)
  7252. if (UseBalloon)
  7253. {
  7254. FTrayIcon->CancelBalloon();
  7255. }
  7256. }
  7257. return Result;
  7258. }
  7259. }
  7260. //---------------------------------------------------------------------------
  7261. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  7262. TTerminal * Terminal, Exception * E)
  7263. {
  7264. if (FTrayIcon->Visible)
  7265. {
  7266. PopupTrayBalloon(Terminal, L"", qtError, E);
  7267. }
  7268. // particularly prevent opening new session from jump list,
  7269. // while exception is shown
  7270. NonVisualDataModule->StartBusy();
  7271. try
  7272. {
  7273. ShowExtendedExceptionEx(Terminal, E);
  7274. }
  7275. __finally
  7276. {
  7277. NonVisualDataModule->EndBusy();
  7278. FTrayIcon->CancelBalloon();
  7279. }
  7280. }
  7281. //---------------------------------------------------------------------------
  7282. void __fastcall TCustomScpExplorerForm::SessionReady()
  7283. {
  7284. InitStatusBar();
  7285. // cannot rely on active page being page for active terminal,
  7286. // as it can happen that active page is the "new session" page
  7287. // (e.g. when reconnecting active terminal, while login dialog
  7288. // invoked from "new session" page is modal)
  7289. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  7290. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  7291. }
  7292. //---------------------------------------------------------------------------
  7293. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  7294. TTerminal * Terminal, Exception * E)
  7295. {
  7296. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  7297. if (!Terminal->Active)
  7298. {
  7299. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  7300. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  7301. {
  7302. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  7303. }
  7304. }
  7305. }
  7306. //---------------------------------------------------------------------------
  7307. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  7308. UnicodeString Message, TQueryType Type,
  7309. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  7310. {
  7311. if ((E == NULL) ||
  7312. ExceptionMessage(E, Message))
  7313. {
  7314. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  7315. if (Important)
  7316. {
  7317. Seconds *= 5;
  7318. }
  7319. UnicodeString NoteMessage(UnformatMessage(Message));
  7320. if (Terminal != NULL)
  7321. {
  7322. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7323. NoteMessage = FORMAT(L"%s: %s",
  7324. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  7325. }
  7326. if (WinConfiguration->BalloonNotifications)
  7327. {
  7328. AddNote(NoteMessage);
  7329. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  7330. }
  7331. else
  7332. {
  7333. FlashOnBackground();
  7334. PostNote(NoteMessage, Seconds, OnClick, UserData);
  7335. }
  7336. }
  7337. }
  7338. //---------------------------------------------------------------------------
  7339. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7340. {
  7341. RestoreApp();
  7342. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7343. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7344. !NonVisualDataModule->Busy)
  7345. {
  7346. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7347. TTerminalManager::Instance()->ActiveSession = Terminal;
  7348. if (!ComponentVisible[fcQueueView])
  7349. {
  7350. ToggleQueueVisibility();
  7351. GoToQueue();
  7352. }
  7353. }
  7354. }
  7355. //---------------------------------------------------------------------------
  7356. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7357. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7358. {
  7359. UnicodeString Message;
  7360. TNotifyEvent OnClick = NULL;
  7361. TObject * UserData = NULL;
  7362. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7363. switch (Event)
  7364. {
  7365. case qeEmptyButMonitored:
  7366. if ((ATerminal != Terminal) || QueueInvisible)
  7367. {
  7368. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7369. OnClick = QueueEmptyNoteClicked;
  7370. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7371. TerminalNoteData->Terminal = ATerminal;
  7372. UserData = TerminalNoteData;
  7373. }
  7374. break;
  7375. case qeEmpty:
  7376. OperationComplete(ATerminal->QueueOperationStart);
  7377. break;
  7378. case qePendingUserAction:
  7379. if ((ATerminal != Terminal) ||
  7380. (QueueInvisible && !IsQueueAutoPopup()))
  7381. {
  7382. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7383. }
  7384. break;
  7385. default:
  7386. DebugFail();
  7387. }
  7388. if (!Message.IsEmpty())
  7389. {
  7390. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7391. }
  7392. }
  7393. //---------------------------------------------------------------------------
  7394. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7395. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7396. {
  7397. if (FDDExtMapFile != NULL)
  7398. {
  7399. CloseHandle(FDDExtMapFile);
  7400. FDDExtMapFile = NULL;
  7401. }
  7402. if (WinConfiguration->DDFakeFile)
  7403. {
  7404. DDFakeFileInitDrag(FileList, Created);
  7405. }
  7406. }
  7407. //---------------------------------------------------------------------------
  7408. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7409. {
  7410. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7411. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7412. {
  7413. FFakeFileDropTarget = FileName;
  7414. }
  7415. }
  7416. //---------------------------------------------------------------------------
  7417. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7418. {
  7419. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7420. if (!ForceDirectories(ApiPath(Result)))
  7421. {
  7422. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7423. }
  7424. FileSetAttr(ApiPath(Result), faHidden);
  7425. return Result;
  7426. }
  7427. //---------------------------------------------------------------------------
  7428. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7429. bool & Created)
  7430. {
  7431. FFakeFileDropTarget = UnicodeString();
  7432. FDragFakeDirectory = CreateFakeTransferDirectory();
  7433. FileList->AddItem(NULL, FDragFakeDirectory);
  7434. Created = true;
  7435. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7436. {
  7437. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7438. }
  7439. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7440. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7441. {
  7442. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7443. TDragExtCommStruct* CommStruct;
  7444. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7445. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7446. DebugAssert(CommStruct != NULL);
  7447. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7448. CommStruct->Dragging = true;
  7449. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7450. LENOF(CommStruct->DropDest));
  7451. NULL_TERMINATE(CommStruct->DropDest);
  7452. UnmapViewOfFile(CommStruct);
  7453. }
  7454. }
  7455. //---------------------------------------------------------------------------
  7456. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7457. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7458. {
  7459. bool Result;
  7460. DebugAssert(!IsLocalBrowserMode());
  7461. if (Sender == RemoteDirView)
  7462. {
  7463. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7464. }
  7465. else
  7466. {
  7467. DebugAssert(Sender == RemoteDriveView);
  7468. TStrings * FileList = RemoteDriveView->DragFileList();
  7469. try
  7470. {
  7471. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7472. }
  7473. __finally
  7474. {
  7475. delete FileList;
  7476. }
  7477. }
  7478. if (FDDTargetControl == RemoteDriveView)
  7479. {
  7480. RemoteDriveView->UpdateDropTarget();
  7481. }
  7482. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7483. // or from tree view to dir view.
  7484. // foMove happens when file/dir is dragged from remote tree view outside of
  7485. // application via dragex
  7486. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7487. (DropSourceControl == RemoteDriveView))
  7488. {
  7489. RemoteDriveView->UpdateDropSource();
  7490. }
  7491. return Result;
  7492. }
  7493. //---------------------------------------------------------------------------
  7494. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7495. {
  7496. DebugAssert(!IsLocalBrowserMode());
  7497. // This also handles drops of remote files to queue.
  7498. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7499. SAFE_DESTROY(FDDFileList);
  7500. if (!FFakeFileDropTarget.IsEmpty())
  7501. {
  7502. RemoveDir(ApiPath(FFakeFileDropTarget));
  7503. }
  7504. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7505. {
  7506. try
  7507. {
  7508. TDragResult DDResult = (Sender == RemoteDirView) ?
  7509. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7510. // Focus is moved to the target application,
  7511. // but as we are going to present the UI, we need to steal the focus back.
  7512. // This most likely won't work though (windows does not allow application
  7513. // to steal focus most of the time)
  7514. Application->BringToFront();
  7515. // On older version of Windows we never got drMove here, see also comment below.
  7516. // On Windows 10, we get the "move".
  7517. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7518. {
  7519. UnicodeString TargetDirectory;
  7520. TFileOperation Operation;
  7521. // drInvalid may mean drMove, see comment below
  7522. switch (DDResult)
  7523. {
  7524. case drCopy:
  7525. Operation = foCopy;
  7526. break;
  7527. case drMove:
  7528. Operation = foMove;
  7529. break;
  7530. default:
  7531. DebugFail();
  7532. case drInvalid:
  7533. // prefer "copy" for safety
  7534. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7535. break;
  7536. }
  7537. TTransferOperationParam Param;
  7538. UnicodeString CounterName;
  7539. bool ForceQueue;
  7540. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7541. {
  7542. // we get drInvalid both if d&d was intercepted by ddext,
  7543. // and when users drops on no-drop location.
  7544. // we tell the difference by existence of response from ddext,
  7545. // so we ignore absence of response in this case
  7546. if (DDResult != drInvalid)
  7547. {
  7548. // here we know that the extension is installed,
  7549. // as it is checked as soon as drag&drop starts from
  7550. // RemoteFileControlDDCreateDragFileList
  7551. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7552. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7553. {
  7554. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7555. }
  7556. else
  7557. {
  7558. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7559. }
  7560. }
  7561. }
  7562. else
  7563. {
  7564. // download using fake file
  7565. Param.Temp = false;
  7566. Param.DragDrop = true;
  7567. if (ForceQueue)
  7568. {
  7569. Param.Queue = asOn;
  7570. }
  7571. if (Sender == RemoteDirView)
  7572. {
  7573. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7574. }
  7575. if (RemoteFileControlFileOperation(Sender, Operation,
  7576. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7577. {
  7578. Configuration->Usage->Inc(CounterName);
  7579. }
  7580. }
  7581. }
  7582. }
  7583. __finally
  7584. {
  7585. CloseHandle(FDDExtMapFile);
  7586. FDDExtMapFile = NULL;
  7587. delete FDragFakeMonitors;
  7588. FDragFakeMonitors = NULL;
  7589. RemoveDir(ApiPath(FDragFakeDirectory));
  7590. FDragFakeDirectory = L"";
  7591. FFakeFileDropTarget = UnicodeString();
  7592. }
  7593. }
  7594. if (FDragDropSshTerminate.get() != NULL)
  7595. {
  7596. FDragDropSshTerminate->Rethrow();
  7597. }
  7598. }
  7599. //---------------------------------------------------------------------------
  7600. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7601. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7602. {
  7603. // Remember drop effect so we know (when user drops files), if we copy or move
  7604. FLastDropEffect = dwEffect;
  7605. }
  7606. //---------------------------------------------------------------------------
  7607. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7608. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7609. {
  7610. bool Result;
  7611. if (FDDTargetControl == QueueView3)
  7612. {
  7613. Directory = DefaultDownloadTargetDirectory();
  7614. Result = true;
  7615. CounterName = L"DownloadsDragDropQueue";
  7616. ForceQueue = true;
  7617. }
  7618. else if (!FFakeFileDropTarget.IsEmpty())
  7619. {
  7620. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7621. Result = true;
  7622. ForceQueue = false;
  7623. CounterName = L"DownloadsDragDropFakeFile";
  7624. }
  7625. else
  7626. {
  7627. ForceQueue = false;
  7628. Enabled = false;
  7629. try
  7630. {
  7631. int Timer = 0;
  7632. Result = false;
  7633. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7634. {
  7635. {
  7636. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7637. TDragExtCommStruct* CommStruct;
  7638. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7639. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7640. DebugAssert(CommStruct != NULL);
  7641. Result = !CommStruct->Dragging;
  7642. if (Result)
  7643. {
  7644. Directory = ExtractFilePath(CommStruct->DropDest);
  7645. CounterName = L"DownloadsDragDropExternalExt";
  7646. }
  7647. UnmapViewOfFile(CommStruct);
  7648. }
  7649. if (!Result)
  7650. {
  7651. Sleep(50);
  7652. Timer += 50;
  7653. Application->ProcessMessages();
  7654. }
  7655. }
  7656. }
  7657. __finally
  7658. {
  7659. Enabled = true;
  7660. }
  7661. }
  7662. return Result;
  7663. }
  7664. //---------------------------------------------------------------------------
  7665. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7666. const UnicodeString TempDir, int SecDelay)
  7667. {
  7668. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7669. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7670. if (FDelayedDeletionTimer == NULL)
  7671. {
  7672. DebugAssert(HandleAllocated());
  7673. FDelayedDeletionTimer = new TTimer(this);
  7674. FDelayedDeletionTimer->Interval = 10000;
  7675. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7676. }
  7677. else
  7678. {
  7679. FDelayedDeletionTimer->Enabled = true;
  7680. }
  7681. }
  7682. //---------------------------------------------------------------------------
  7683. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7684. {
  7685. DebugAssert(FDelayedDeletionList != NULL);
  7686. TDateTime N = Now();
  7687. TDateTime Alert;
  7688. UnicodeString Directory;
  7689. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7690. {
  7691. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7692. if ((N >= Alert) || (Sender == NULL))
  7693. {
  7694. Directory = FDelayedDeletionList->Strings[Index];
  7695. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7696. {
  7697. FDelayedDeletionList->Delete(Index);
  7698. }
  7699. }
  7700. }
  7701. if (FDelayedDeletionList->Count == 0)
  7702. {
  7703. FDelayedDeletionTimer->Enabled = false;
  7704. }
  7705. }
  7706. //---------------------------------------------------------------------------
  7707. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7708. {
  7709. DebugAssert(!IsLocalBrowserMode());
  7710. if (IsFileControl(FDDTargetControl, osRemote) ||
  7711. (FDDTargetControl == SessionsPageControl))
  7712. {
  7713. TManagedTerminal * TargetTerminal = NULL;
  7714. TFileOperation Operation = foNone;
  7715. if (FDDTargetControl == SessionsPageControl)
  7716. {
  7717. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7718. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7719. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7720. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7721. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7722. {
  7723. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7724. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7725. {
  7726. Operation = foRemoteMove;
  7727. }
  7728. else
  7729. {
  7730. Operation = foRemoteCopy;
  7731. }
  7732. }
  7733. }
  7734. else
  7735. {
  7736. if (FLastDropEffect == DROPEFFECT_MOVE)
  7737. {
  7738. Operation = foRemoteMove;
  7739. }
  7740. else if (FLastDropEffect == DROPEFFECT_COPY)
  7741. {
  7742. Operation = foRemoteCopy;
  7743. }
  7744. }
  7745. if (Operation != foNone)
  7746. {
  7747. RemoteFileControlFileOperation(DropSourceControl,
  7748. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7749. }
  7750. // abort drag&drop
  7751. Abort();
  7752. }
  7753. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7754. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7755. (FDDTargetControl != QueueView3))
  7756. {
  7757. DebugAssert(!FDragTempDir.IsEmpty());
  7758. TTransferType Type;
  7759. UnicodeString TempDir = FDragTempDir;
  7760. // We clear FDragTempDir before calling
  7761. // just in case it fail (raises exception)
  7762. FDragTempDir = L"";
  7763. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7764. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7765. // empty directory parameter means temp directory -> don't display it!
  7766. UnicodeString TargetDir = L"";
  7767. int Options = 0;
  7768. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7769. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7770. {
  7771. Abort();
  7772. }
  7773. // TargetDir is set when dropped on local file control
  7774. // (this was workaround for legacy dirview event handling, now it should be
  7775. // made prettier)
  7776. if (TargetDir.IsEmpty())
  7777. {
  7778. TargetDir = TempDir;
  7779. if (ForceDirectories(ApiPath(TargetDir)))
  7780. {
  7781. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7782. FPendingTempSpaceWarn = true;
  7783. try
  7784. {
  7785. FDragDropOperation = true;
  7786. int Params = cpTemporary |
  7787. (Type == ttMove ? cpDelete : 0);
  7788. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7789. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7790. }
  7791. __finally
  7792. {
  7793. FDragDropOperation = false;
  7794. FPendingTempSpaceWarn = false;
  7795. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7796. }
  7797. }
  7798. else
  7799. {
  7800. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7801. }
  7802. }
  7803. }
  7804. }
  7805. //---------------------------------------------------------------------------
  7806. void __fastcall TCustomScpExplorerForm::DDDownload(
  7807. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  7808. {
  7809. DebugAssert(!IsLocalBrowserMode());
  7810. TAutoBatch AutoBatch(this);
  7811. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  7812. UpdateCopyParamCounters(*CopyParam);
  7813. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7814. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7815. {
  7816. RemoteDriveView->UpdateDropSource();
  7817. }
  7818. }
  7819. //---------------------------------------------------------------------------
  7820. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7821. {
  7822. public:
  7823. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7824. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7825. {
  7826. }
  7827. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7828. {
  7829. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7830. TDataObjectFilesEx::AllowData(FormatEtc);
  7831. }
  7832. };
  7833. //---------------------------------------------------------------------------
  7834. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7835. TObject * Sender, TDataObject *& DataObject)
  7836. {
  7837. if (FDDExtMapFile != NULL)
  7838. {
  7839. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7840. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7841. {
  7842. Abort();
  7843. }
  7844. if (FileList->Count > 0)
  7845. {
  7846. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7847. if (!FilesObject->IsValid(true, true))
  7848. {
  7849. FilesObject->_Release();
  7850. }
  7851. else
  7852. {
  7853. DataObject = FilesObject;
  7854. }
  7855. }
  7856. }
  7857. }
  7858. //---------------------------------------------------------------------------
  7859. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7860. {
  7861. DebugFail();
  7862. }
  7863. //---------------------------------------------------------------------------
  7864. void __fastcall TCustomScpExplorerForm::GoToTree()
  7865. {
  7866. ComponentVisible[fcRemoteTree] = true;
  7867. DriveView(osRemote)->SetFocus();
  7868. }
  7869. //---------------------------------------------------------------------------
  7870. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7871. TPanelExport Export)
  7872. {
  7873. TCustomDirView * DirView = this->DirView(Side);
  7874. std::unique_ptr<TStrings> ExportData(new TStringList());
  7875. switch (Export)
  7876. {
  7877. case pePath:
  7878. ExportData->Add(DirView->PathName);
  7879. break;
  7880. case peFileList:
  7881. case peFullFileList:
  7882. {
  7883. bool FullPath = (Export == peFullFileList);
  7884. DirView->CreateFileList(false, FullPath, ExportData.get());
  7885. for (int Index = 0; Index < ExportData->Count; Index++)
  7886. {
  7887. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7888. {
  7889. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7890. }
  7891. }
  7892. }
  7893. break;
  7894. default:
  7895. DebugFail();
  7896. }
  7897. return ExportData.release();
  7898. }
  7899. //---------------------------------------------------------------------------
  7900. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7901. TPanelExport Export, TPanelExportDestination Destination)
  7902. {
  7903. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7904. PanelExportStore(Side, Export, Destination, ExportData.get());
  7905. }
  7906. //---------------------------------------------------------------------------
  7907. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7908. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7909. TStrings * ExportData)
  7910. {
  7911. if (Destination == pedClipboard)
  7912. {
  7913. TInstantOperationVisualizer Visualizer;
  7914. CopyToClipboard(ExportData);
  7915. }
  7916. else
  7917. {
  7918. DebugFail();
  7919. }
  7920. }
  7921. //---------------------------------------------------------------------------
  7922. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7923. {
  7924. TCustomDirView * DirView = this->DirView(Side);
  7925. UnicodeString Mask = DirView->Mask;
  7926. if (DoFilterMaskDialog(DirView, Mask))
  7927. {
  7928. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7929. Configuration->Usage->Inc(L"Filters");
  7930. }
  7931. }
  7932. //---------------------------------------------------------------------------
  7933. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7934. {
  7935. return FQueueController->DefaultOperation();
  7936. }
  7937. //---------------------------------------------------------------------------
  7938. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7939. TQueueOperation Operation, void ** Param)
  7940. {
  7941. switch (Operation)
  7942. {
  7943. case qoPreferences:
  7944. return true;
  7945. case qoGoTo:
  7946. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7947. case qoOnceEmpty:
  7948. return IsAnythingQueued();
  7949. default:
  7950. return FQueueController->AllowOperation(Operation, Param);
  7951. }
  7952. }
  7953. //---------------------------------------------------------------------------
  7954. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7955. {
  7956. if (DebugAlwaysTrue(QueueView3->Visible))
  7957. {
  7958. QueueView3->SetFocus();
  7959. }
  7960. }
  7961. //---------------------------------------------------------------------------
  7962. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7963. TQueueOperation Operation, void * Param)
  7964. {
  7965. if (Operation == qoGoTo)
  7966. {
  7967. GoToQueue();
  7968. }
  7969. else if (Operation == qoPreferences)
  7970. {
  7971. PreferencesDialog(pmQueue);
  7972. }
  7973. else
  7974. {
  7975. FQueueController->ExecuteOperation(Operation, Param);
  7976. }
  7977. }
  7978. //---------------------------------------------------------------------------
  7979. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7980. {
  7981. return (Queue != NULL) && Queue->Enabled;
  7982. }
  7983. //---------------------------------------------------------------------------
  7984. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7985. {
  7986. DebugAssert(Queue != NULL);
  7987. if (Queue != NULL)
  7988. {
  7989. Queue->Enabled = !Queue->Enabled;
  7990. }
  7991. }
  7992. //---------------------------------------------------------------------------
  7993. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7994. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7995. {
  7996. FQueueActedItem = QueueView3->ItemFocused;
  7997. }
  7998. //---------------------------------------------------------------------------
  7999. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  8000. {
  8001. return TopDock->Height + (MessageDock->Visible ? MessageDock->Height : 0) + QueueSplitter->Height;
  8002. }
  8003. //---------------------------------------------------------------------------
  8004. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  8005. {
  8006. return
  8007. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  8008. (QueueDock->Visible ? QueueDock->Height : 0) +
  8009. (QueueLabel->Visible ? QueueLabel->Height : 0);
  8010. }
  8011. //---------------------------------------------------------------------------
  8012. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  8013. {
  8014. return ScaleByTextHeight(this, 48);
  8015. }
  8016. //---------------------------------------------------------------------------
  8017. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  8018. TObject * /*Sender*/, int & NewSize, bool & Accept)
  8019. {
  8020. // when queue is hidden by double-clicking splitter, stray attempt to
  8021. // resize the panel with strange value arrives, make sure it is ignored
  8022. if (ComponentVisible[fcQueueView])
  8023. {
  8024. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  8025. if (NewSize > MaxHeightLimit)
  8026. {
  8027. NewSize = MaxHeightLimit;
  8028. }
  8029. int MinHeightLimit =
  8030. GetStaticQueuePanelComponentsHeight() +
  8031. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  8032. GetMinQueueViewHeight();
  8033. if (NewSize < MinHeightLimit)
  8034. {
  8035. NewSize = MinHeightLimit;
  8036. }
  8037. }
  8038. else
  8039. {
  8040. Accept = false;
  8041. }
  8042. }
  8043. //---------------------------------------------------------------------------
  8044. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  8045. {
  8046. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  8047. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  8048. if (QueueViewHeight < GetMinQueueViewHeight())
  8049. {
  8050. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  8051. }
  8052. }
  8053. //---------------------------------------------------------------------------
  8054. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  8055. TDragObject *& /*DragObject*/)
  8056. {
  8057. FQueueActedItem = QueueView3->ItemFocused;
  8058. FQueueScrollOnDragOver->StartDrag();
  8059. }
  8060. //---------------------------------------------------------------------------
  8061. void __fastcall TCustomScpExplorerForm::QueueView3EndDrag(
  8062. TObject *, TObject * DebugUsedArg(Target), int DebugUsedArg(X), int DebugUsedArg(Y))
  8063. {
  8064. FQueueScrollOnDragOver->EndDrag();
  8065. }
  8066. //---------------------------------------------------------------------------
  8067. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  8068. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  8069. {
  8070. Accept = true;
  8071. if (Source == QueueView3)
  8072. {
  8073. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  8074. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  8075. if (Accept)
  8076. {
  8077. TQueueItemProxy * QueueItem;
  8078. TQueueItemProxy * DestQueueItem;
  8079. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  8080. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  8081. Accept = (QueueItem != DestQueueItem) &&
  8082. (QueueItem->Status == TQueueItem::qsPending) &&
  8083. (DestQueueItem->Status == TQueueItem::qsPending);
  8084. }
  8085. FQueueScrollOnDragOver->DragOver(TPoint(X, Y));
  8086. }
  8087. }
  8088. //---------------------------------------------------------------------------
  8089. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  8090. TObject * /*Source*/, int /*X*/, int /*Y*/)
  8091. {
  8092. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  8093. {
  8094. TQueueItemProxy * QueueItem;
  8095. TQueueItemProxy * DestQueueItem;
  8096. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  8097. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  8098. QueueItem->Move(DestQueueItem);
  8099. }
  8100. }
  8101. //---------------------------------------------------------------------------
  8102. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  8103. {
  8104. if ((QueueView3->ItemFocused == NULL) &&
  8105. (QueueView3->Items->Count > 0))
  8106. {
  8107. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  8108. }
  8109. QueueLabelUpdateStatus();
  8110. }
  8111. //---------------------------------------------------------------------------
  8112. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  8113. {
  8114. QueueLabelUpdateStatus();
  8115. }
  8116. //---------------------------------------------------------------------------
  8117. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  8118. {
  8119. QueueLabel->UpdateStatus();
  8120. }
  8121. //---------------------------------------------------------------------------
  8122. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  8123. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  8124. {
  8125. if (Selected)
  8126. {
  8127. NonVisualDataModule->UpdateNonVisibleActions();
  8128. }
  8129. }
  8130. //---------------------------------------------------------------------------
  8131. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  8132. {
  8133. TDragDropFilesEx * Result = NULL;
  8134. if (Sender == SessionsPageControl)
  8135. {
  8136. Result = FSessionsDragDropFilesEx;
  8137. }
  8138. else if (Sender == QueueView3)
  8139. {
  8140. Result = FQueueDragDropFilesEx;
  8141. }
  8142. else
  8143. {
  8144. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8145. if (DirView != NULL)
  8146. {
  8147. Result = DirView->DragDropFilesEx;
  8148. }
  8149. else
  8150. {
  8151. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  8152. if (DriveView != NULL)
  8153. {
  8154. Result = DriveView->DragDropFilesEx;
  8155. }
  8156. }
  8157. }
  8158. DebugAssert(Result != NULL);
  8159. return Result;
  8160. }
  8161. //---------------------------------------------------------------------------
  8162. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  8163. {
  8164. return (DView->SelCount == DView->FilesCount);
  8165. }
  8166. //---------------------------------------------------------------------------
  8167. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  8168. {
  8169. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  8170. }
  8171. //---------------------------------------------------------------------------
  8172. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  8173. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  8174. {
  8175. DebugAssert(!IsLocalBrowserMode());
  8176. TFileOperation Operation;
  8177. switch (Effect)
  8178. {
  8179. case DROPEFFECT_MOVE:
  8180. Operation = foMove;
  8181. break;
  8182. case DROPEFFECT_COPY:
  8183. // occures on WinXP (reported by user)
  8184. default:
  8185. Operation = foCopy;
  8186. break;
  8187. };
  8188. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  8189. // see a comment in TUnixDirView::PerformItemDragDropOperation
  8190. if (DragDropFilesEx->FileList->Count > 0)
  8191. {
  8192. TStrings * FileList = new TStringList();
  8193. try
  8194. {
  8195. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  8196. {
  8197. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  8198. }
  8199. FDragDropOperation = true;
  8200. TTransferOperationParam Param;
  8201. Param.TargetDirectory = TargetPath;
  8202. // upload, no temp dirs
  8203. Param.Temp = false;
  8204. Param.DragDrop = DragDrop;
  8205. Param.Options =
  8206. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  8207. if (ForceQueue)
  8208. {
  8209. Param.Queue = asOn;
  8210. }
  8211. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  8212. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  8213. {
  8214. if (IsFileControl(DropSourceControl, osLocal))
  8215. {
  8216. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  8217. }
  8218. else
  8219. {
  8220. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  8221. }
  8222. }
  8223. }
  8224. __finally
  8225. {
  8226. FDragDropOperation = false;
  8227. delete FileList;
  8228. }
  8229. }
  8230. }
  8231. //---------------------------------------------------------------------------
  8232. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  8233. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  8234. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  8235. {
  8236. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  8237. }
  8238. //---------------------------------------------------------------------------
  8239. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  8240. TObject * Sender, int grfKeyState, int & dwEffect)
  8241. {
  8242. DebugAssert(!IsLocalBrowserMode());
  8243. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  8244. if ((dwEffect != DROPEFFECT_NONE) &&
  8245. IsFileControl(DropSourceControl, osRemote))
  8246. {
  8247. // do not allow drop on remote panel (on free space, still allow drop on directories)
  8248. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  8249. (RemoteDirView->DropTarget == NULL))
  8250. {
  8251. dwEffect = DROPEFFECT_NONE;
  8252. }
  8253. else
  8254. {
  8255. if (dwEffect == DROPEFFECT_COPY)
  8256. {
  8257. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  8258. // currently we support copying always (at least via temporary directory);
  8259. // remove associated checks once this all proves stable and working
  8260. bool CopyCapable = true;
  8261. // if we do not support neither of operations, there's no discussion
  8262. if (!MoveCapable && !CopyCapable)
  8263. {
  8264. dwEffect = DROPEFFECT_NONE;
  8265. }
  8266. else
  8267. {
  8268. // The default effect inside remote panel is move,
  8269. // unless we do not support it, but support copy
  8270. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  8271. {
  8272. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  8273. }
  8274. else
  8275. {
  8276. // with ctrl-down, we want copy unless it is not supported
  8277. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  8278. }
  8279. }
  8280. }
  8281. }
  8282. }
  8283. }
  8284. //---------------------------------------------------------------------------
  8285. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  8286. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  8287. {
  8288. DebugAssert(!IsLocalBrowserMode());
  8289. if (FDDTotalSize >= 0)
  8290. {
  8291. if (File->IsDirectory)
  8292. {
  8293. FDDTotalSize = -1;
  8294. }
  8295. else
  8296. {
  8297. FDDTotalSize += File->Size;
  8298. }
  8299. }
  8300. DebugAssert(!FDragTempDir.IsEmpty());
  8301. // TODO: this is quite ineffective
  8302. // TODO: what if invalid character replacement is disabled?
  8303. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  8304. UnicodeString TransferFileName;
  8305. if (Sender == RemoteDriveView)
  8306. {
  8307. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  8308. }
  8309. else
  8310. {
  8311. TransferFileName = File->FileName;
  8312. }
  8313. FDDFileList->AddObject(TransferFileName, File);
  8314. }
  8315. //---------------------------------------------------------------------------
  8316. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  8317. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  8318. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  8319. {
  8320. // sometimes we do not get DDEnd so the list is not released
  8321. SAFE_DESTROY(FDDFileList);
  8322. FDDFileList = new TStringList();
  8323. FDragTempDir = WinConfiguration->TemporaryDir();
  8324. FDDTotalSize = 0;
  8325. FDragDropSshTerminate.reset(NULL);
  8326. }
  8327. //---------------------------------------------------------------------------
  8328. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  8329. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  8330. HRESULT & Result)
  8331. {
  8332. if (Result == DRAGDROP_S_DROP)
  8333. {
  8334. try
  8335. {
  8336. GlobalDragImageList->HideDragImage();
  8337. try
  8338. {
  8339. RemoteFileControlDDTargetDrop();
  8340. }
  8341. catch(ESshTerminate & E)
  8342. {
  8343. FDragDropSshTerminate.reset(E.Clone());
  8344. }
  8345. }
  8346. catch (Exception &E)
  8347. {
  8348. // If downloading fails we need to cancel drag&drop, otherwise
  8349. // Explorer shows error
  8350. // But by the way exception probably never reach this point as
  8351. // it's catched on way.
  8352. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8353. Result = DRAGDROP_S_CANCEL;
  8354. ShowExtendedException(Terminal, &E);
  8355. }
  8356. }
  8357. }
  8358. //---------------------------------------------------------------------------
  8359. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8360. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8361. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8362. {
  8363. TFileMasks::TParams MaskParams;
  8364. MaskParams.Size = Size;
  8365. MaskParams.Modification = Modification;
  8366. // this does not re-parse the mask if it is the same as the last time
  8367. FDirViewMatchMask = Masks;
  8368. bool ImplicitMatch;
  8369. Matches =
  8370. // Local and RecurseInclude parameters have no effect as there's no path.
  8371. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  8372. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  8373. (AllowImplicitMatches || !ImplicitMatch);
  8374. }
  8375. //---------------------------------------------------------------------------
  8376. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8377. TObject * Sender, TListItem * Item, WORD & Indexes)
  8378. {
  8379. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8380. if (GetPartialFileExtLen(DirView->ItemFileName(Item)) > 0)
  8381. {
  8382. Indexes |= oiPartial;
  8383. }
  8384. }
  8385. //---------------------------------------------------------------------------
  8386. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8387. {
  8388. return
  8389. DirViewEnabled(osCurrent) &&
  8390. DirView(osCurrent)->CanPasteFromClipBoard();
  8391. }
  8392. //---------------------------------------------------------------------------
  8393. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8394. {
  8395. bool Result = false;
  8396. if (CanPasteToDirViewFromClipBoard())
  8397. {
  8398. Result = true;
  8399. }
  8400. else
  8401. {
  8402. UnicodeString ClipboardText;
  8403. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8404. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8405. {
  8406. if (StoredSessions->IsUrl(ClipboardText))
  8407. {
  8408. Result = true;
  8409. }
  8410. else
  8411. {
  8412. Result = DirViewEnabled(osCurrent);
  8413. }
  8414. }
  8415. }
  8416. return Result;
  8417. }
  8418. //---------------------------------------------------------------------------
  8419. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8420. {
  8421. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8422. if (DoesClipboardContainOurFiles())
  8423. {
  8424. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8425. {
  8426. DebugAssert(!IsLocalBrowserMode());
  8427. TTerminalManager * Manager = TTerminalManager::Instance();
  8428. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8429. Manager->ActiveSession = FClipboardTerminal;
  8430. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8431. }
  8432. }
  8433. else if (CanPasteToDirViewFromClipBoard())
  8434. {
  8435. DirView(osCurrent)->PasteFromClipBoard();
  8436. }
  8437. else
  8438. {
  8439. UnicodeString ClipboardText;
  8440. if (NonEmptyTextFromClipboard(ClipboardText))
  8441. {
  8442. if (StoredSessions->IsUrl(ClipboardText))
  8443. {
  8444. NewSession(ClipboardText);
  8445. }
  8446. else
  8447. {
  8448. DirView(osCurrent)->Path = ClipboardText;
  8449. }
  8450. }
  8451. }
  8452. }
  8453. //---------------------------------------------------------------------------
  8454. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8455. {
  8456. // TBD
  8457. }
  8458. //---------------------------------------------------------------------------
  8459. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8460. {
  8461. TCustomDirView * ADirView = DirView(Side);
  8462. ADirView->SelectAll(Mode);
  8463. ADirView->SetFocus();
  8464. }
  8465. //---------------------------------------------------------------------------
  8466. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8467. {
  8468. TCustomDirView * ADirView = DirView(Side);
  8469. TFileFilter Filter;
  8470. DefaultFileFilter(Filter);
  8471. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8472. {
  8473. Configuration->Usage->Inc(L"MaskSelections");
  8474. ADirView->SelectFiles(Filter, Select);
  8475. ADirView->SetFocus();
  8476. }
  8477. }
  8478. //---------------------------------------------------------------------------
  8479. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8480. {
  8481. TCustomDirView * ADirView = DirView(Side);
  8482. ADirView->RestoreSelectedNames();
  8483. ADirView->SetFocus();
  8484. }
  8485. //---------------------------------------------------------------------------
  8486. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8487. {
  8488. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8489. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8490. {
  8491. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8492. UnicodeString Ext;
  8493. if (!IsSideLocalBrowser(osCurrent))
  8494. {
  8495. Ext = UnixExtractFileExt(FileName);
  8496. }
  8497. else
  8498. {
  8499. Ext = ExtractFileExt(FileName);
  8500. }
  8501. if (Ext.IsEmpty())
  8502. {
  8503. Ext = L".";
  8504. }
  8505. TFileFilter Filter;
  8506. Filter.Masks = FORMAT(L"*%s", (Ext));
  8507. Filter.Directories = false;
  8508. CurrentDirView->SelectFiles(Filter, Select);
  8509. }
  8510. }
  8511. //---------------------------------------------------------------------------
  8512. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8513. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8514. {
  8515. UnicodeString Result;
  8516. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8517. {
  8518. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8519. }
  8520. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8521. {
  8522. // noop
  8523. }
  8524. else
  8525. {
  8526. Result =
  8527. FMTLOAD(FILE_INFO_FORMAT,
  8528. (FormatBytes(FileInfo.SelectedSize),
  8529. FormatBytes(FileInfo.FilesSize),
  8530. FormatNumber(FileInfo.SelectedCount),
  8531. FormatNumber(FileInfo.FilesCount)));
  8532. }
  8533. return Result;
  8534. }
  8535. //---------------------------------------------------------------------------
  8536. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8537. TTBXStatusPanel * Panel, TOperationSide Side)
  8538. {
  8539. if (Panel->Index == 1)
  8540. {
  8541. ToggleShowHiddenFiles();
  8542. }
  8543. else if (Panel->Index == 2)
  8544. {
  8545. Filter(Side);
  8546. }
  8547. }
  8548. //---------------------------------------------------------------------------
  8549. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8550. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8551. {
  8552. DebugAssert(!StatusBar->SimplePanel);
  8553. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8554. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8555. }
  8556. //---------------------------------------------------------------------------
  8557. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8558. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8559. {
  8560. DebugAssert(StatusBar->Panels->Count >= 3);
  8561. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8562. if (FileInfo.HiddenCount > 0)
  8563. {
  8564. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8565. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8566. }
  8567. else
  8568. {
  8569. HiddenFilesPanel->ViewPriority = 0;
  8570. // not really necessary, just to cleanup no-longer-valid data
  8571. HiddenFilesPanel->Caption = L"";
  8572. }
  8573. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8574. if (FileInfo.FilteredCount > 0)
  8575. {
  8576. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8577. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8578. }
  8579. else
  8580. {
  8581. FilteredFilesPanel->ViewPriority = 0;
  8582. // not really necessary, just to cleanup no-longer-valid data
  8583. FilteredFilesPanel->Caption = L"";
  8584. }
  8585. }
  8586. //---------------------------------------------------------------------------
  8587. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8588. TObject * /*Sender*/)
  8589. {
  8590. if (DirView(osOther)->Enabled)
  8591. {
  8592. DirView(osOther)->SetFocus();
  8593. }
  8594. }
  8595. //---------------------------------------------------------------------------
  8596. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8597. {
  8598. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8599. switch (Config.Show)
  8600. {
  8601. case qvShow:
  8602. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8603. {
  8604. Config.Show = qvHide;
  8605. }
  8606. else
  8607. {
  8608. Config.Show = Config.LastHideShow;
  8609. }
  8610. break;
  8611. case qvHideWhenEmpty:
  8612. if (ComponentVisible[fcQueueView])
  8613. {
  8614. Config.Show = qvHide;
  8615. }
  8616. else
  8617. {
  8618. Config.LastHideShow = Config.Show;
  8619. Config.Show = qvShow;
  8620. }
  8621. break;
  8622. case qvHide:
  8623. Config.LastHideShow = Config.Show;
  8624. Config.Show = qvShow;
  8625. break;
  8626. }
  8627. WinConfiguration->QueueView = Config;
  8628. }
  8629. //---------------------------------------------------------------------------
  8630. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8631. {
  8632. UnicodeString Result;
  8633. if (Terminal != NULL)
  8634. {
  8635. switch (WinConfiguration->PathInCaption)
  8636. {
  8637. case picShort:
  8638. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8639. break;
  8640. case picFull:
  8641. Result = Terminal->CurrentDirectory;
  8642. break;
  8643. }
  8644. }
  8645. return Result;
  8646. }
  8647. //---------------------------------------------------------------------------
  8648. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8649. {
  8650. TColor Result = GetWindowColor(FSessionColor);
  8651. return Result;
  8652. }
  8653. //---------------------------------------------------------------------------
  8654. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8655. {
  8656. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8657. return Result;
  8658. }
  8659. //---------------------------------------------------------------------------
  8660. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8661. {
  8662. if (!FSessionChanging)
  8663. {
  8664. TTerminalManager::Instance()->UpdateAppTitle();
  8665. }
  8666. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8667. // background to gray on Windows 7 (works on Windows 10).
  8668. // See also EnableControl
  8669. if (Showing)
  8670. {
  8671. if (ManagedSession != NULL)
  8672. {
  8673. // Update path when it changes
  8674. if ((SessionsPageControl->ActivePage != NULL) &&
  8675. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8676. {
  8677. UpdateSessionTab(SessionsPageControl->ActivePage);
  8678. }
  8679. }
  8680. NonVisualDataModule->ReconnectSessionAction->Update();
  8681. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8682. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8683. CenterReconnectToolbar();
  8684. bool HasTerminal = HasActiveTerminal();
  8685. RemoteDirView->Enabled = HasTerminal;
  8686. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8687. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8688. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8689. RemoteDriveView->Color = RemoteDirView->Color;
  8690. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8691. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8692. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8693. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8694. QueueFileList->Enabled = QueueView3->Enabled;
  8695. QueueFileList->Color = QueueView3->Color;
  8696. QueueFileList->Font->Color = QueueView3->Font->Color;
  8697. QueueLabelUpdateStatus();
  8698. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8699. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8700. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8701. {
  8702. UpdateDarkMode();
  8703. }
  8704. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8705. MessageDock->Visible = (MessageDock->ToolbarCount > 0);
  8706. }
  8707. }
  8708. //---------------------------------------------------------------------------
  8709. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8710. {
  8711. UpdateControls();
  8712. }
  8713. //---------------------------------------------------------------------------
  8714. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8715. TObject * Sender)
  8716. {
  8717. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8718. DebugAssert(DirView != NULL);
  8719. DoDirViewLoaded(DirView);
  8720. TransferPresetAutoSelect();
  8721. }
  8722. //---------------------------------------------------------------------------
  8723. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8724. {
  8725. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8726. // first run after installation
  8727. if (double(Updates.LastCheck) == 0)
  8728. {
  8729. AppLog(L"First run, scheduling updates check for the next run");
  8730. // make sure next time there will be an update (if enabled)
  8731. Updates.LastCheck = TDateTime(1);
  8732. WinConfiguration->Updates = Updates;
  8733. }
  8734. else
  8735. {
  8736. TDateTime Period;
  8737. if (IsUWP() && !IsOfficialPackage())
  8738. {
  8739. Period = DefaultUpdatesPeriod;
  8740. AppLog(L"Thirdparty UWP package, using default updates check period");
  8741. }
  8742. else
  8743. {
  8744. Period = Updates.Period;
  8745. }
  8746. AppLogFmt(L"Updates check period: %.2f", (double(Period)));
  8747. if (double(Period) > 0)
  8748. {
  8749. TDateTime Interval = Now() - Updates.LastCheck;
  8750. AppLogFmt(L"Interval since the last updates check: %.2f", (double(Interval)));
  8751. if (Interval >= Period)
  8752. {
  8753. StartUpdateThread(UpdatesChecked);
  8754. }
  8755. }
  8756. }
  8757. }
  8758. //---------------------------------------------------------------------------
  8759. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8760. {
  8761. AppLog(L"Updates check done");
  8762. UnicodeString Message;
  8763. bool New;
  8764. TQueryType Type;
  8765. GetUpdatesMessage(Message, New, Type, false);
  8766. if (!Message.IsEmpty())
  8767. {
  8768. AppLogFmt(L"Updates check message: %s", (Message));
  8769. if (New)
  8770. {
  8771. AppLog(L"New version detected");
  8772. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8773. }
  8774. if (!New && (Type != qtWarning))
  8775. {
  8776. AppLog(L"Posting non-critical updates note");
  8777. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8778. }
  8779. else
  8780. {
  8781. AppLog(L"Posting new version or critical message notification");
  8782. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8783. }
  8784. if (New)
  8785. {
  8786. Configuration->Usage->Inc(L"UpdateNotifications");
  8787. }
  8788. }
  8789. }
  8790. //---------------------------------------------------------------------------
  8791. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8792. {
  8793. RestoreApp();
  8794. bool CanDisplay = !NonVisualDataModule->Busy;
  8795. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8796. {
  8797. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8798. }
  8799. if (CanDisplay)
  8800. {
  8801. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8802. // Probably not needed as CheckForUpdates is blocking since we started using neon for HTTP implementation.
  8803. NonVisualDataModule->StartBusy();
  8804. try
  8805. {
  8806. CheckForUpdates(true);
  8807. }
  8808. __finally
  8809. {
  8810. NonVisualDataModule->EndBusy();
  8811. }
  8812. }
  8813. else
  8814. {
  8815. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8816. }
  8817. }
  8818. //---------------------------------------------------------------------------
  8819. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8820. TCopyParamRuleData & Data)
  8821. {
  8822. DebugAssert(Terminal != NULL);
  8823. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8824. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8825. Data.RemoteDirectory = RemoteDirView->PathName;
  8826. }
  8827. //---------------------------------------------------------------------------
  8828. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8829. {
  8830. // Terminal can be null when we are changing local directory implicitly,
  8831. // because it has been deleted, while no session is connected
  8832. // (Login dialog is open)
  8833. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8834. {
  8835. DebugAssert(!IsLocalBrowserMode());
  8836. TCopyParamRuleData Data;
  8837. GetTransferPresetAutoSelectData(Data);
  8838. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8839. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8840. if (CopyParamIndex < 0)
  8841. {
  8842. // there is no preset that matches autoselection
  8843. // set preset that we consider "default"
  8844. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8845. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8846. }
  8847. else
  8848. {
  8849. // there is preset matching autoselection
  8850. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8851. if (CopyParamName == FCopyParamAutoSelected)
  8852. {
  8853. // autoselected the same preset as the last time
  8854. // make no change (i.e. preserve custom user preset, if any)
  8855. }
  8856. else
  8857. {
  8858. // autoselected the different preset then the last time (or there
  8859. // was default preset set)
  8860. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8861. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8862. }
  8863. }
  8864. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8865. {
  8866. if (CopyParamIndex >= 0)
  8867. {
  8868. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8869. }
  8870. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8871. try
  8872. {
  8873. int Fmt =
  8874. (CopyParamIndex < 0) ?
  8875. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8876. COPY_PARAM_AUTOSELECTED;
  8877. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8878. Data->Message = MainInstructions(Message);
  8879. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8880. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8881. CopyParamAttrs);
  8882. if (CopyParamIndex >= 0)
  8883. {
  8884. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8885. Info = FORMAT(L"%s\n \n%s", (Info,
  8886. FMTLOAD(COPY_PARAM_RULE,
  8887. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8888. }
  8889. Data->Message += L"\n\n" + Info;
  8890. if (WinConfiguration->CopyParamAutoSelectNotice)
  8891. {
  8892. TransferPresetNoteMessage(Data, true);
  8893. }
  8894. else
  8895. {
  8896. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8897. Data = NULL; // ownership passed
  8898. }
  8899. }
  8900. __finally
  8901. {
  8902. delete Data;
  8903. }
  8904. }
  8905. }
  8906. }
  8907. //---------------------------------------------------------------------------
  8908. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8909. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8910. {
  8911. DebugAssert(NoteData != NULL);
  8912. DebugAssert(!IsLocalBrowserMode());
  8913. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8914. TQueryButtonAlias Aliases[1];
  8915. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8916. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8917. Params.Aliases = Aliases;
  8918. Params.AliasesCount = LENOF(Aliases);
  8919. unsigned int Result =
  8920. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8921. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8922. switch (Result)
  8923. {
  8924. case qaNeverAskAgain:
  8925. DebugAssert(AllowNeverAskAgain);
  8926. WinConfiguration->CopyParamAutoSelectNotice = false;
  8927. break;
  8928. case qaIgnore:
  8929. PreferencesDialog(pmPresets);
  8930. break;
  8931. }
  8932. }
  8933. //---------------------------------------------------------------------------
  8934. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8935. {
  8936. // as of now useless, as this is used for notes only, never for balloons, ...
  8937. RestoreApp();
  8938. // .. and we should never be busy here
  8939. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8940. {
  8941. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8942. }
  8943. }
  8944. //---------------------------------------------------------------------------
  8945. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8946. TPreferencesMode APreferencesMode)
  8947. {
  8948. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8949. TCopyParamRuleData Data;
  8950. if (Terminal != NULL)
  8951. {
  8952. PreferencesData.reset(new TPreferencesDialogData());
  8953. GetTransferPresetAutoSelectData(Data);
  8954. PreferencesData->CopyParamRuleData = &Data;
  8955. }
  8956. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8957. }
  8958. //---------------------------------------------------------------------------
  8959. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8960. const TCustomCommandType & Command)
  8961. {
  8962. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8963. {
  8964. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8965. }
  8966. }
  8967. //---------------------------------------------------------------------------
  8968. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8969. {
  8970. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8971. TCustomCommandType Command;
  8972. // make sure we use local custom command when remote are not supported
  8973. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8974. {
  8975. Command = FLastCustomCommand;
  8976. }
  8977. else
  8978. {
  8979. Command.Params = Command.Params | ccLocal;
  8980. }
  8981. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8982. FEditingFocusedAdHocCommand = OnFocused;
  8983. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8984. int Options =
  8985. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8986. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8987. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8988. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8989. {
  8990. FLastCustomCommand = Command;
  8991. UpdateCustomCommandsToolbar();
  8992. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8993. }
  8994. }
  8995. //---------------------------------------------------------------------------
  8996. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8997. {
  8998. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8999. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  9000. DebugAssert(State > 0);
  9001. if (State <= 0)
  9002. {
  9003. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  9004. }
  9005. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  9006. }
  9007. //---------------------------------------------------------------------------
  9008. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  9009. TCustomCommandType & Command, int & State)
  9010. {
  9011. bool Result = !FLastCustomCommand.Command.IsEmpty();
  9012. if (Result)
  9013. {
  9014. Command = FLastCustomCommand;
  9015. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  9016. }
  9017. return Result;
  9018. }
  9019. //---------------------------------------------------------------------------
  9020. void __fastcall TCustomScpExplorerForm::BeforeAction()
  9021. {
  9022. ResetIncrementalSearch();
  9023. }
  9024. //---------------------------------------------------------------------------
  9025. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  9026. {
  9027. DebugAssert(ComponentVisible[Component]);
  9028. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  9029. }
  9030. //---------------------------------------------------------------------------
  9031. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  9032. {
  9033. // when queue panel is resized here directly, the status bar is stretched
  9034. // over whole space the panel occupied
  9035. PostComponentHide(fcQueueView);
  9036. }
  9037. //---------------------------------------------------------------------------
  9038. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  9039. {
  9040. ComponentVisible[fcQueueFileList] = false;
  9041. }
  9042. //---------------------------------------------------------------------------
  9043. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  9044. {
  9045. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  9046. RefreshColorMode();
  9047. ResetSysDarkTheme();
  9048. ConfigurationChanged();
  9049. ConfigureInterface();
  9050. // Should be called for all controls
  9051. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  9052. }
  9053. //---------------------------------------------------------------------------
  9054. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  9055. {
  9056. // Do not handle, when shutting down anyway (maybe also when not setup completely yet?)
  9057. if (!FInvalid &&
  9058. (Message.LParam != 0) &&
  9059. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  9060. {
  9061. ThemeChanged();
  9062. }
  9063. TForm::Dispatch(&Message);
  9064. }
  9065. //---------------------------------------------------------------------------
  9066. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  9067. {
  9068. TMessage * M = static_cast<TMessage*>(Message);
  9069. switch (M->Msg)
  9070. {
  9071. case CM_DIALOGCHAR:
  9072. CMDialogChar(*M);
  9073. break;
  9074. case WM_APPCOMMAND:
  9075. WMAppCommand(*M);
  9076. break;
  9077. case WM_SYSCOMMAND:
  9078. WMSysCommand(*M);
  9079. break;
  9080. case WM_QUERYENDSESSION:
  9081. WMQueryEndSession(*M);
  9082. break;
  9083. case WM_ENDSESSION:
  9084. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  9085. break;
  9086. #ifdef _DEBUG
  9087. case WM_WINDOWPOSCHANGED:
  9088. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  9089. break;
  9090. case CM_CUSTOMSTYLECHANGED:
  9091. TForm::Dispatch(Message);
  9092. break;
  9093. #endif
  9094. case WM_COMPONENT_HIDE:
  9095. {
  9096. Byte Component = static_cast<Byte>(M->WParam);
  9097. // sanity check
  9098. if (ComponentVisible[Component])
  9099. {
  9100. // special treatment
  9101. if (Component == fcQueueView)
  9102. {
  9103. ToggleQueueVisibility();
  9104. DebugAssert(!ComponentVisible[fcQueueView]);
  9105. }
  9106. else
  9107. {
  9108. ComponentVisible[Component] = false;
  9109. }
  9110. }
  9111. }
  9112. break;
  9113. case WM_COPYDATA:
  9114. WMCopyData(*M);
  9115. break;
  9116. case WM_MANAGES_CAPTION:
  9117. // caption managed in TTerminalManager::UpdateAppTitle()
  9118. M->Result = 1;
  9119. break;
  9120. case WM_WANTS_MOUSEWHEEL:
  9121. M->Result = 1;
  9122. break;
  9123. case CM_SHOWINGCHANGED:
  9124. CMShowingChanged(*M);
  9125. break;
  9126. case WM_CLOSE:
  9127. WMClose(*M);
  9128. break;
  9129. case WM_DPICHANGED:
  9130. WMDpiChanged(*M);
  9131. break;
  9132. case CM_DPICHANGED:
  9133. CMDpiChanged(*M);
  9134. break;
  9135. case WM_SETTINGCHANGE:
  9136. WMSettingChange(*M);
  9137. break;
  9138. case CM_DIALOGKEY:
  9139. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  9140. break;
  9141. case WM_PASTE_FILES:
  9142. PasteFiles();
  9143. break;
  9144. case WM_IS_HIDDEN:
  9145. // To treat master password prompt as a main window while showing it when workspace is loading while
  9146. // the exporer is showing up (Visible is true already)
  9147. M->Result = !FShowing;
  9148. break;
  9149. default:
  9150. TForm::Dispatch(Message);
  9151. break;
  9152. }
  9153. }
  9154. //---------------------------------------------------------------------------
  9155. void __fastcall TCustomScpExplorerForm::UpdateImages()
  9156. {
  9157. // noop
  9158. }
  9159. //---------------------------------------------------------------------------
  9160. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  9161. {
  9162. TForm::Dispatch(&Message);
  9163. FSessionColors->Clear();
  9164. AddFixedSessionImages();
  9165. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  9166. UpdateImages();
  9167. }
  9168. //---------------------------------------------------------------------------
  9169. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  9170. {
  9171. TForm::Dispatch(&Message);
  9172. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  9173. UpdatePixelsPerInchMainWindowCounter();
  9174. }
  9175. //---------------------------------------------------------------------------
  9176. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  9177. {
  9178. // Cannot close window while we are busy.
  9179. // We cannot test this in FormCloseQuery as that is called also from
  9180. // Close(), which is called by CloseApplicationAction. So we can be busy
  9181. // there already even, when it is legitimate to close the application.
  9182. // Possibly a better place to handle this would be WMSysCommand.
  9183. if (NonVisualDataModule->Busy)
  9184. {
  9185. Message.Result = 1;
  9186. }
  9187. else
  9188. {
  9189. TForm::Dispatch(&Message);
  9190. }
  9191. }
  9192. //---------------------------------------------------------------------------
  9193. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  9194. {
  9195. if (!FShowingChanged)
  9196. {
  9197. AddStartupSequence(L"D");
  9198. FShowingChanged = true;
  9199. }
  9200. // Ignoring implicit local-local browser of Commander, opened from DoShow
  9201. bool NoSession = (ManagedSession == NULL);
  9202. // This calls TCustomScpExplorerForm::DoShow
  9203. TForm::Dispatch(&Message);
  9204. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  9205. if (Showing && !FStarted)
  9206. {
  9207. FStarted = true;
  9208. InterfaceStarted();
  9209. }
  9210. if (Showing && NoSession)
  9211. {
  9212. // When we are starting minimized (i.e. from an installer),
  9213. // postpone showing Login dialog until we get restored.
  9214. // Otherwise the Login dialog (and Authentication window) show restored
  9215. // over invisible (minimized) main window.
  9216. if (WindowState == wsMinimized)
  9217. {
  9218. FNeedSession = true;
  9219. }
  9220. else
  9221. {
  9222. // This happens before application ever goes idle, so the toolbars would
  9223. // stay enabled (initial state) until the Login dialog is dismissed.
  9224. UpdateActions();
  9225. NonVisualDataModule->StartBusy();
  9226. try
  9227. {
  9228. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  9229. // otherwise the dialog does not receive focus.
  9230. // With Commander interface the ProcessMessages is called already
  9231. // by TDriveView, but with Explorer interface, we need to call it explicily
  9232. Application->ProcessMessages();
  9233. // do not reload sessions, they have been loaded just now (optimization)
  9234. NeedSession(true);
  9235. }
  9236. __finally
  9237. {
  9238. NonVisualDataModule->EndBusy();
  9239. }
  9240. }
  9241. }
  9242. }
  9243. //---------------------------------------------------------------------------
  9244. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  9245. {
  9246. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  9247. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  9248. }
  9249. //---------------------------------------------------------------------------
  9250. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  9251. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  9252. int & MaxHeight)
  9253. {
  9254. // workaround for bug in TWinControl.CalcConstraints
  9255. // Check for empty rect (restore from iconic state) is done there only after
  9256. // call to AdjustClientRect, which enlarges the rect (for forms).
  9257. TRect R = GetClientRect();
  9258. // when restoring from iconic state, place no restrictions
  9259. if (IsRectEmpty(R))
  9260. {
  9261. MinWidth = 0;
  9262. MinHeight = 0;
  9263. MaxWidth = 32000;
  9264. MaxHeight = 32000;
  9265. }
  9266. }
  9267. //---------------------------------------------------------------------------
  9268. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  9269. TSpaceAvailable & ASpaceAvailable, bool & Close)
  9270. {
  9271. // terminal can be already closed (e.g. dropped connection)
  9272. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  9273. {
  9274. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  9275. try
  9276. {
  9277. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  9278. }
  9279. catch(...)
  9280. {
  9281. if (!Terminal->Active)
  9282. {
  9283. Close = true;
  9284. }
  9285. throw;
  9286. }
  9287. }
  9288. }
  9289. //---------------------------------------------------------------------------
  9290. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  9291. {
  9292. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  9293. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  9294. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  9295. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  9296. {
  9297. OnGetSpaceAvailable = GetSpaceAvailable;
  9298. }
  9299. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  9300. OnGetSpaceAvailable);
  9301. }
  9302. //---------------------------------------------------------------------------
  9303. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  9304. {
  9305. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  9306. Terminal->FillSessionDataForCode(Data.get());
  9307. return Data.release();
  9308. }
  9309. //---------------------------------------------------------------------------
  9310. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  9311. {
  9312. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  9313. DoGenerateUrlDialog(Data.get(), Paths);
  9314. }
  9315. //---------------------------------------------------------------------------
  9316. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  9317. {
  9318. GenerateUrl(NULL);
  9319. }
  9320. //---------------------------------------------------------------------------
  9321. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  9322. {
  9323. std::unique_ptr<TStrings> Paths(new TStringList());
  9324. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  9325. GenerateUrl(Paths.get());
  9326. }
  9327. //---------------------------------------------------------------------------
  9328. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  9329. {
  9330. FSessionColor = value;
  9331. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  9332. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  9333. static_cast<TObject *>(GetComponent(fcColorMenu)));
  9334. DebugAssert(ColorItem != NULL);
  9335. ColorItem->Color = C;
  9336. NonVisualDataModule->ColorMenuItem->Color = C;
  9337. UpdateControls();
  9338. }
  9339. //---------------------------------------------------------------------------
  9340. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  9341. {
  9342. if (value != FSessionColor)
  9343. {
  9344. UpdateSessionColor(value);
  9345. // Is null when called from LastTerminalClosed
  9346. if (Terminal != NULL)
  9347. {
  9348. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  9349. }
  9350. }
  9351. }
  9352. //---------------------------------------------------------------------------
  9353. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  9354. {
  9355. bool Result = FNoteTimer->Enabled;
  9356. if (Result)
  9357. {
  9358. // cannot cancel note too early
  9359. bool NotEarly =
  9360. (Now() - FNoteShown >
  9361. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  9362. if (Force || NotEarly)
  9363. {
  9364. FNoteTimer->Enabled = false;
  9365. FNote = L"";
  9366. SAFE_DESTROY(FNoteData);
  9367. FOnNoteClick = NULL;
  9368. FNoteHints = FNotes->Text;
  9369. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  9370. UpdateStatusBar();
  9371. }
  9372. }
  9373. return Result;
  9374. }
  9375. //---------------------------------------------------------------------------
  9376. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  9377. {
  9378. DebugAssert(FNoteTimer->Enabled);
  9379. CancelNote(true);
  9380. }
  9381. //---------------------------------------------------------------------------
  9382. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9383. {
  9384. FNoteHints = FNotes->Text.TrimRight();
  9385. }
  9386. //---------------------------------------------------------------------------
  9387. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9388. {
  9389. int P = Note.Pos(L"\n");
  9390. if (P > 0)
  9391. {
  9392. Note.SetLength(P - 1);
  9393. }
  9394. FNotes->Add(FORMAT(L"[%s] %s",
  9395. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9396. while (FNotes->Count > 10)
  9397. {
  9398. FNotes->Delete(0);
  9399. }
  9400. if (UpdateNow)
  9401. {
  9402. UpdateNoteHints();
  9403. UpdateStatusBar();
  9404. }
  9405. }
  9406. //---------------------------------------------------------------------------
  9407. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  9408. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9409. {
  9410. int P = Note.Pos(L"\n");
  9411. if (P > 0)
  9412. {
  9413. Note.SetLength(P - 1);
  9414. }
  9415. UpdateNoteHints();
  9416. FNote = Note;
  9417. SAFE_DESTROY(FNoteData);
  9418. FNoteData = NoteData;
  9419. FOnNoteClick = OnNoteClick;
  9420. AddNote(Note, false);
  9421. UpdateStatusBar();
  9422. FNoteShown = Now();
  9423. FNoteTimer->Enabled = false;
  9424. if (Seconds == 0)
  9425. {
  9426. Seconds = WinConfiguration->NotificationsTimeout;
  9427. }
  9428. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9429. FNoteTimer->Enabled = true;
  9430. }
  9431. //---------------------------------------------------------------------------
  9432. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9433. {
  9434. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9435. }
  9436. //---------------------------------------------------------------------------
  9437. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9438. {
  9439. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9440. {
  9441. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9442. }
  9443. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9444. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9445. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9446. 0, NULL, NULL);
  9447. }
  9448. //---------------------------------------------------------------------------
  9449. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9450. {
  9451. bool NewShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9452. UnicodeString Note =
  9453. FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1), (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, (NewShowHiddenFiles ? 2 : 3))));
  9454. PostNote(Note, 0, NULL, NULL);
  9455. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9456. WinConfiguration->ShowHiddenFiles = NewShowHiddenFiles;
  9457. }
  9458. //---------------------------------------------------------------------------
  9459. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9460. {
  9461. WinConfiguration->FormatSizeBytes = Style;
  9462. }
  9463. //---------------------------------------------------------------------------
  9464. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9465. {
  9466. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9467. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9468. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9469. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9470. }
  9471. //---------------------------------------------------------------------------
  9472. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9473. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9474. {
  9475. if (Panel->Index == 0)
  9476. {
  9477. if (FOnNoteClick != NULL)
  9478. {
  9479. // prevent the user data from being freed by possible call
  9480. // to CancelNote or PostNote during call to OnNoteClick
  9481. std::unique_ptr<TObject> NoteData(FNoteData);
  9482. TNotifyEvent OnNoteClick = FOnNoteClick;
  9483. FNoteData = NULL;
  9484. // need to cancel the note as we are going to delete its user data
  9485. CancelNote(true);
  9486. OnNoteClick(NoteData.get());
  9487. }
  9488. }
  9489. if (HasActiveTerminal() &&
  9490. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9491. {
  9492. FileSystemInfo();
  9493. }
  9494. }
  9495. //---------------------------------------------------------------------------
  9496. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9497. {
  9498. // workaround:
  9499. // 1) for unknown reason, disabling window, while minimized,
  9500. // prevents it from restoring, even if it was enabled again meanwhile
  9501. // 2) when disabling the main window, while another has focus
  9502. // minimize is no longer possible
  9503. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9504. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9505. // Shouldn't we use IsApplicationMinimized() here?
  9506. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9507. {
  9508. Enabled = false;
  9509. }
  9510. FLockLevel++;
  9511. }
  9512. //---------------------------------------------------------------------------
  9513. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9514. {
  9515. DebugAssert(FLockLevel > 0);
  9516. FLockLevel--;
  9517. if (FLockLevel == 0)
  9518. {
  9519. DebugAssert(FLockSuspendLevel == 0);
  9520. Enabled = true;
  9521. }
  9522. }
  9523. //---------------------------------------------------------------------------
  9524. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9525. {
  9526. // We need to make sure that window is enabled when the last modal window closes
  9527. // otherwise focus is not restored correctly.
  9528. // So we re-enable the window when modal window opens and
  9529. // disable it back after is closes
  9530. if (FLockLevel > 0)
  9531. {
  9532. // while we have nesting counter, we know that we never be called
  9533. // recursivelly as Application->OnModalBegin is called only
  9534. // for the top-level modal window
  9535. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9536. {
  9537. // won't be disabled when conditions in LockWindow() were not satisfied
  9538. FDisabledOnLockSuspend = !Enabled;
  9539. // When minimized to tray (or actually when set to SW_HIDE),
  9540. // setting Enabled makes the window focusable even when there's
  9541. // modal window over it
  9542. if (!FTrayIcon->Visible)
  9543. {
  9544. Enabled = true;
  9545. }
  9546. }
  9547. FLockSuspendLevel++;
  9548. }
  9549. }
  9550. //---------------------------------------------------------------------------
  9551. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9552. {
  9553. if (FLockSuspendLevel > 0)
  9554. {
  9555. DebugAssert(FLockLevel > 0);
  9556. FLockSuspendLevel--;
  9557. // see comment in SuspendWindowLock
  9558. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9559. {
  9560. // Note that window can be enabled here, when we were minized to tray when
  9561. // was SuspendWindowLock() called.
  9562. // We should possibly do the same check as in LockWindow(),
  9563. // if it is ever possible that the consitions change between
  9564. // SuspendWindowLock() and ResumeWindowLock()
  9565. Enabled = !FDisabledOnLockSuspend;
  9566. }
  9567. }
  9568. }
  9569. //---------------------------------------------------------------------------
  9570. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9571. {
  9572. TTBXComboBoxItem * RemotePathComboBox =
  9573. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9574. if (!TextOnly)
  9575. {
  9576. TStrings * Items = RemotePathComboBox->Strings;
  9577. Items->BeginUpdate();
  9578. try
  9579. {
  9580. Items->Clear();
  9581. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9582. {
  9583. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9584. while (!IsUnixRootPath(APath))
  9585. {
  9586. int P = APath.LastDelimiter(L'/');
  9587. DebugAssert(P >= 0);
  9588. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9589. APath.SetLength(P - 1);
  9590. }
  9591. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9592. }
  9593. }
  9594. __finally
  9595. {
  9596. Items->EndUpdate();
  9597. }
  9598. }
  9599. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9600. // Setting ItemIndex to -1 does not reset its text
  9601. if (RemotePathComboBox->Strings->Count == 0)
  9602. {
  9603. RemotePathComboBox->Text = L"";
  9604. }
  9605. }
  9606. //---------------------------------------------------------------------------
  9607. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9608. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9609. {
  9610. if (AIndex < 0)
  9611. {
  9612. AIndex = Sender->ItemIndex;
  9613. }
  9614. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9615. }
  9616. //---------------------------------------------------------------------------
  9617. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9618. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9619. int & ImageIndex)
  9620. {
  9621. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9622. }
  9623. //---------------------------------------------------------------------------
  9624. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9625. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9626. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9627. {
  9628. if (!IsLocalBrowserMode())
  9629. {
  9630. ARect.Left += (10 * AIndex);
  9631. }
  9632. }
  9633. //---------------------------------------------------------------------------
  9634. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9635. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9636. {
  9637. AWidth += (10 * AIndex);
  9638. }
  9639. //---------------------------------------------------------------------------
  9640. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9641. {
  9642. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9643. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9644. int Index = RemotePathComboBox->ItemIndex;
  9645. while (Index < RemotePathComboBox->Strings->Count - 1)
  9646. {
  9647. APath = UnixExtractFileDir(APath);
  9648. Index++;
  9649. }
  9650. // VanDyke style paths
  9651. if (APath.IsEmpty())
  9652. {
  9653. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9654. APath = ROOTDIRECTORY;
  9655. }
  9656. if (RemoteDirView->Path != APath)
  9657. {
  9658. RemoteDirView->Path = APath;
  9659. }
  9660. }
  9661. //---------------------------------------------------------------------------
  9662. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9663. {
  9664. DoRemotePathComboBoxItemClick(Sender);
  9665. }
  9666. //---------------------------------------------------------------------------
  9667. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9668. {
  9669. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9670. DebugUsedParam(Sender);
  9671. UpdateRemotePathComboBox(true);
  9672. }
  9673. //---------------------------------------------------------------------------
  9674. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9675. {
  9676. DoRemotePathComboBoxCancel(Sender);
  9677. }
  9678. //---------------------------------------------------------------------------
  9679. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9680. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9681. {
  9682. ResetIncrementalSearch();
  9683. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9684. DebugAssert(DirView != NULL);
  9685. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9686. {
  9687. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9688. // OnEditing is called also from TCustomListView::CanEdit
  9689. if (Edit != NULL)
  9690. {
  9691. EditSelectBaseName(Edit);
  9692. }
  9693. }
  9694. }
  9695. //---------------------------------------------------------------------------
  9696. //---------------------------------------------------------------------------
  9697. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9698. {
  9699. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9700. Result->AutoDetectDnD = false;
  9701. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9702. Result->RenderDataOn = rdoEnterAndDropSync;
  9703. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9704. return Result;
  9705. }
  9706. //---------------------------------------------------------------------------
  9707. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9708. {
  9709. if (IsWin10Build(19041))
  9710. {
  9711. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9712. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9713. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9714. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9715. }
  9716. }
  9717. //---------------------------------------------------------------------------
  9718. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9719. {
  9720. TForm::CreateWnd();
  9721. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9722. UpdateDarkMode();
  9723. if (FSessionsDragDropFilesEx == NULL)
  9724. {
  9725. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9726. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9727. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9728. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9729. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9730. }
  9731. if (FQueueDragDropFilesEx == NULL)
  9732. {
  9733. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9734. // No need to set OnDragOver as we do not have any restrictions
  9735. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9736. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9737. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9738. }
  9739. }
  9740. //---------------------------------------------------------------------------
  9741. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9742. {
  9743. TForm::DestroyWnd();
  9744. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9745. FQueueDragDropFilesEx->DragDropControl = NULL;
  9746. }
  9747. //---------------------------------------------------------------------------
  9748. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9749. {
  9750. SideEnter(FCurrentSide);
  9751. FEverShown = true;
  9752. }
  9753. //---------------------------------------------------------------------------
  9754. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9755. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9756. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9757. {
  9758. if (!NonVisualDataModule->Busy)
  9759. {
  9760. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9761. Configuration->Usage->Inc(L"FileFinds");
  9762. LockWindow(true);
  9763. NonVisualDataModule->StartBusy();
  9764. WinConfiguration->LockedInterface = true;
  9765. try
  9766. {
  9767. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9768. }
  9769. __finally
  9770. {
  9771. WinConfiguration->LockedInterface = false;
  9772. NonVisualDataModule->EndBusy();
  9773. UnlockWindow();
  9774. }
  9775. }
  9776. }
  9777. //---------------------------------------------------------------------------
  9778. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9779. {
  9780. if (!NonVisualDataModule->Busy)
  9781. {
  9782. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9783. SetFocus();
  9784. RemoteDirView->Path = UnixExtractFilePath(Path);
  9785. UnicodeString FileName = UnixExtractFileName(Path);
  9786. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9787. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9788. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9789. if (Item == NULL)
  9790. {
  9791. ReloadDirectory(osRemote);
  9792. Item = RemoteDirView->FindFileItem(FileName);
  9793. }
  9794. if (Item != NULL)
  9795. {
  9796. RemoteDirView->ItemFocused = Item;
  9797. RemoteDirView->ItemFocused->MakeVisible(false);
  9798. RemoteDirView->SetFocus();
  9799. }
  9800. }
  9801. }
  9802. //---------------------------------------------------------------------------
  9803. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9804. {
  9805. bool Result = !NonVisualDataModule->Busy;
  9806. if (Result)
  9807. {
  9808. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9809. }
  9810. return Result;
  9811. }
  9812. //---------------------------------------------------------------------------
  9813. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9814. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9815. {
  9816. if (CanOperateOnFoundFiles(ATerminal))
  9817. {
  9818. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9819. FOnFileOperationFinished = OnFileOperationFinished;
  9820. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9821. }
  9822. }
  9823. //---------------------------------------------------------------------------
  9824. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9825. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9826. {
  9827. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9828. }
  9829. //---------------------------------------------------------------------------
  9830. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9831. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9832. {
  9833. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9834. }
  9835. //---------------------------------------------------------------------------
  9836. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9837. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9838. {
  9839. if (CanOperateOnFoundFiles(ATerminal))
  9840. {
  9841. for (int Index = 0; Index < FileList->Count; Index++)
  9842. {
  9843. UnicodeString FileName = FileList->Strings[Index];
  9844. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9845. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9846. OnFileOperationFinished(osRemote, FileName, true);
  9847. }
  9848. }
  9849. }
  9850. //---------------------------------------------------------------------------
  9851. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9852. {
  9853. FFileFindTerminal = Terminal;
  9854. ShowFileFindDialog(
  9855. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9856. DoDownloadFoundFiles, DoEditFoundFiles);
  9857. }
  9858. //---------------------------------------------------------------------------
  9859. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9860. {
  9861. FTaskbarList = TaskbarList;
  9862. }
  9863. //---------------------------------------------------------------------------
  9864. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9865. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9866. {
  9867. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9868. if (Index >= 0)
  9869. {
  9870. if (Button == mbLeft)
  9871. {
  9872. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9873. // If switching tab (switching session) takes long enough for user
  9874. // to actually release the button, "mouse down" is still raised,
  9875. // but we do not get "mouse up" event, so dragging is not cancelled,
  9876. // prevent that by not beginning dragging in the first place.
  9877. int Key = ((GetSystemMetrics(SM_SWAPBUTTON) == 0) ? VK_LBUTTON : VK_RBUTTON);
  9878. if (IsKeyPressed(Key))
  9879. {
  9880. // when user clicks the "+", we get mouse down only after the session
  9881. // is closed, when new session tab is already on X:Y, so dragging
  9882. // starts, prevent that
  9883. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9884. {
  9885. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9886. if (Terminal != NULL)
  9887. {
  9888. SessionsPageControl->BeginDrag(false);
  9889. }
  9890. }
  9891. }
  9892. }
  9893. else if (Button == mbMiddle)
  9894. {
  9895. // ignore middle-click for "New session tab" and the last local-local tab
  9896. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9897. if ((Session != NULL) && CanCloseSession(Session))
  9898. {
  9899. CloseSessionTab(Index);
  9900. }
  9901. }
  9902. }
  9903. }
  9904. //---------------------------------------------------------------------------
  9905. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9906. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9907. {
  9908. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9909. // do not allow dropping on the "+" tab
  9910. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9911. if ((TargetTerminal != NULL) &&
  9912. (SessionsPageControl->ActivePage->PageIndex != Index))
  9913. {
  9914. Configuration->Usage->Inc(L"SessionTabMoves");
  9915. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9916. // but we want to actually prevent that to avoid flicker
  9917. SessionsPageControl->ActivePage->PageIndex = Index;
  9918. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9919. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9920. }
  9921. }
  9922. //---------------------------------------------------------------------------
  9923. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9924. TObject * Sender, TObject * Source, int X, int Y,
  9925. TDragState /*State*/, bool & Accept)
  9926. {
  9927. Accept = (Sender == Source);
  9928. if (Accept)
  9929. {
  9930. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9931. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9932. // do not allow dragging to the "+" tab
  9933. Accept = (Terminal != NULL);
  9934. }
  9935. }
  9936. //---------------------------------------------------------------------------
  9937. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9938. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9939. {
  9940. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9941. if (Index < 0)
  9942. {
  9943. Effect = DROPEFFECT_NONE;
  9944. }
  9945. else
  9946. {
  9947. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9948. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9949. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9950. {
  9951. Effect = DROPEFFECT_NONE;
  9952. }
  9953. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9954. {
  9955. Effect = DROPEFFECT_COPY;
  9956. }
  9957. }
  9958. }
  9959. //---------------------------------------------------------------------------
  9960. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9961. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9962. {
  9963. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9964. // do not allow dropping on local-local, disconnected and the "+" tab
  9965. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9966. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9967. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9968. {
  9969. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9970. if (!IsFileControl(DropSourceControl, osRemote))
  9971. {
  9972. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9973. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9974. // Why don't we use Terminal->CurrentDirectory directly?
  9975. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9976. // do not force queue, drag drop
  9977. false, true);
  9978. }
  9979. }
  9980. }
  9981. //---------------------------------------------------------------------------
  9982. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9983. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9984. {
  9985. // Downloads are handled in RemoteFileControlDDEnd
  9986. if (!IsFileControl(DropSourceControl, osRemote))
  9987. {
  9988. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9989. Terminal->CurrentDirectory,
  9990. // force queue, drag drop
  9991. true, true);
  9992. }
  9993. }
  9994. //---------------------------------------------------------------------------
  9995. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9996. {
  9997. FShowing = false;
  9998. // Do not save empty workspace
  9999. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  10000. {
  10001. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  10002. UnicodeString Name = WorkspaceName();
  10003. DoSaveWorkspace(
  10004. Name, DataList.get(),
  10005. !Configuration->DisablePasswordStoring &&
  10006. WinConfiguration->AutoSaveWorkspacePasswords, false);
  10007. WinConfiguration->LastStoredSession = Name;
  10008. }
  10009. }
  10010. //---------------------------------------------------------------------------
  10011. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  10012. {
  10013. TManagedTerminal * ManagedTerminal =
  10014. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  10015. if (ManagedTerminal != NULL)
  10016. {
  10017. ManagedTerminal->DirectoryLoaded = Now();
  10018. }
  10019. }
  10020. //---------------------------------------------------------------------------
  10021. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  10022. TListItem * /*Item*/, bool /*Selected*/)
  10023. {
  10024. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  10025. switch (DirView->LastSelectMethod)
  10026. {
  10027. case smKeyboard:
  10028. Configuration->Usage->Inc(L"KeyboardSelections");
  10029. break;
  10030. case smMouse:
  10031. Configuration->Usage->Inc(L"MouseSelections");
  10032. break;
  10033. }
  10034. }
  10035. //---------------------------------------------------------------------------
  10036. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  10037. {
  10038. if (Color != 0)
  10039. {
  10040. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  10041. }
  10042. else
  10043. {
  10044. return FSessionTabImageIndex;
  10045. }
  10046. }
  10047. //---------------------------------------------------------------------------
  10048. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  10049. {
  10050. TPngImageCollectionItem * Item =
  10051. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  10052. FSessionColors->AddPng(Item->PngImage);
  10053. return FSessionColors->Count - 1;
  10054. }
  10055. //---------------------------------------------------------------------------
  10056. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  10057. {
  10058. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  10059. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  10060. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  10061. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  10062. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  10063. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  10064. }
  10065. //---------------------------------------------------------------------------
  10066. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  10067. {
  10068. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  10069. }
  10070. //---------------------------------------------------------------------------
  10071. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  10072. {
  10073. switch (Side)
  10074. {
  10075. case osLocal:
  10076. return FNewLocalTabTabImageIndex;
  10077. case osRemote:
  10078. case osCurrent:
  10079. return FNewRemoteTabTabImageIndex;
  10080. default:
  10081. DebugFail();
  10082. return -1;
  10083. }
  10084. }
  10085. //---------------------------------------------------------------------------
  10086. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  10087. {
  10088. for (int Index = 0; Index < Control->ControlCount; Index++)
  10089. {
  10090. TControl * AControl = Control->Controls[Index];
  10091. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  10092. if (WinControl != NULL)
  10093. {
  10094. CollectItemsWithTextDisplayMode(WinControl);
  10095. }
  10096. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  10097. if (Toolbar != NULL)
  10098. {
  10099. // we care for top-level items only
  10100. TTBCustomItem * Items = Toolbar->Items;
  10101. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  10102. {
  10103. TTBCustomItem * Item = Items->Items[ItemIndex];
  10104. if (((Item->DisplayMode == nbdmImageAndText) ||
  10105. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  10106. EligibleForImageDisplayMode(Item))
  10107. {
  10108. FItemsWithTextDisplayMode.insert(Item);
  10109. }
  10110. }
  10111. }
  10112. }
  10113. }
  10114. //---------------------------------------------------------------------------
  10115. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  10116. {
  10117. return true;
  10118. }
  10119. //---------------------------------------------------------------------------
  10120. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  10121. {
  10122. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  10123. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  10124. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  10125. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  10126. bool Result = true;
  10127. while (Result && (i != FItemsWithTextDisplayMode.end()))
  10128. {
  10129. TTBCustomItem * Item = *i;
  10130. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  10131. if (Label != NULL)
  10132. {
  10133. // optimization
  10134. if (Label->Visible == SelectiveToolbarText)
  10135. {
  10136. Result = false;
  10137. }
  10138. else
  10139. {
  10140. Label->Visible = SelectiveToolbarText;
  10141. }
  10142. }
  10143. else
  10144. {
  10145. // optimization
  10146. if (Item->DisplayMode == DisplayMode)
  10147. {
  10148. Result = false;
  10149. }
  10150. else
  10151. {
  10152. Item->DisplayMode = DisplayMode;
  10153. }
  10154. }
  10155. ++i;
  10156. }
  10157. UpdateNewTabTab();
  10158. return Result;
  10159. }
  10160. //---------------------------------------------------------------------------
  10161. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  10162. {
  10163. DebugFail();
  10164. }
  10165. //---------------------------------------------------------------------------
  10166. bool __fastcall TCustomScpExplorerForm::IsBusy()
  10167. {
  10168. // Among other, a lock level is non zero, while directory is being loaded,
  10169. // even when it happens as a result of dirview navigation,
  10170. // i.e. when TNonVisualDataModule is NOT FBusy.
  10171. // That's why the TNonVisualDataModule::GetBusy calls this method.
  10172. // Among other this prevents a panel auto update to occur while
  10173. // directory is changing.
  10174. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  10175. }
  10176. //---------------------------------------------------------------------------
  10177. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  10178. {
  10179. // While the window is disabled, we still seem to process menu shortcuts at least,
  10180. // so stop it at least here.
  10181. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  10182. bool Result;
  10183. if (Allowed == aaUpdate)
  10184. {
  10185. Result = true;
  10186. }
  10187. else
  10188. {
  10189. Result = !NonVisualDataModule->Busy;
  10190. if (Result && (Allowed == aaShortCut))
  10191. {
  10192. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  10193. Action->Update();
  10194. Result = Action->Enabled;
  10195. }
  10196. }
  10197. return Result;
  10198. }
  10199. //---------------------------------------------------------------------------
  10200. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  10201. {
  10202. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  10203. }
  10204. //---------------------------------------------------------------------------
  10205. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  10206. {
  10207. // This is somewhat redundant to LockWindow() call from
  10208. // TTerminalManager::TerminalReadDirectoryProgress.
  10209. // But disabling window is known not to block keyboard shorcuts
  10210. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  10211. if (Busy > 0)
  10212. {
  10213. if (NonVisualDataModule->Busy)
  10214. {
  10215. State = false;
  10216. }
  10217. else
  10218. {
  10219. NonVisualDataModule->StartBusy();
  10220. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  10221. LockWindow();
  10222. }
  10223. }
  10224. else if (Busy < 0)
  10225. {
  10226. UnlockWindow();
  10227. NonVisualDataModule->EndBusy();
  10228. }
  10229. else
  10230. {
  10231. State = NonVisualDataModule->Busy;
  10232. }
  10233. }
  10234. //---------------------------------------------------------------------------
  10235. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  10236. {
  10237. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  10238. if (Index >= 0)
  10239. {
  10240. TPopupMenu * PopupMenu = NULL;
  10241. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10242. if (Session != NULL)
  10243. {
  10244. SessionsPageControl->ActivePageIndex = Index;
  10245. if (DebugAlwaysTrue(SessionTabSwitched()))
  10246. {
  10247. // copied from TControl.WMContextMenu
  10248. SendCancelMode(SessionsPageControl);
  10249. // explicit popup instead of using PopupMenu property
  10250. // to avoid menu to popup somewhere within SessionTabSwitched above,
  10251. // while connecting yet not-connected session and hence
  10252. // allowing an access to commands over not-completely connected session
  10253. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  10254. }
  10255. }
  10256. else
  10257. {
  10258. if (SupportsLocalBrowser())
  10259. {
  10260. PopupMenu = NonVisualDataModule->NewTabPopup;
  10261. }
  10262. }
  10263. if (PopupMenu != NULL)
  10264. {
  10265. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  10266. PopupMenu->PopupComponent = SessionsPageControl;
  10267. PopupMenu->Popup(Point.x, Point.y);
  10268. }
  10269. }
  10270. Handled = true;
  10271. }
  10272. //---------------------------------------------------------------------------
  10273. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  10274. {
  10275. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  10276. }
  10277. //---------------------------------------------------------------------------
  10278. void __fastcall TCustomScpExplorerForm::ChangePassword()
  10279. {
  10280. Configuration->Usage->Inc(L"PasswordChanges");
  10281. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  10282. ChangePasswordData->DisableAuthentationsExceptPassword();
  10283. ChangePasswordData->ChangePassword = true;
  10284. std::unique_ptr<TTerminal> ChangePasswordSession(
  10285. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  10286. ChangePasswordSession->AutoReadDirectory = false;
  10287. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  10288. {
  10289. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  10290. }
  10291. else
  10292. {
  10293. Configuration->Usage->Inc(L"PasswordChangesFailed");
  10294. }
  10295. }
  10296. //---------------------------------------------------------------------------
  10297. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  10298. {
  10299. // No nice way to assert SSH2
  10300. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  10301. }
  10302. //---------------------------------------------------------------------------
  10303. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  10304. {
  10305. TOperationVisualizer OperationVisualizer;
  10306. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  10307. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  10308. }
  10309. //---------------------------------------------------------------------------
  10310. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  10311. {
  10312. TForm::ChangeScale(M, D);
  10313. int APixelsPerInch = GetControlPixelsPerInch(this);
  10314. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  10315. try
  10316. {
  10317. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  10318. }
  10319. __finally
  10320. {
  10321. GUIConfiguration->ChangeResourceModule(ResourceModule);
  10322. }
  10323. }
  10324. //---------------------------------------------------------------------------
  10325. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  10326. {
  10327. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  10328. }
  10329. //---------------------------------------------------------------------
  10330. class TPublicControl : public TControl
  10331. {
  10332. friend class TCustomScpExplorerForm;
  10333. };
  10334. //---------------------------------------------------------------------------
  10335. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  10336. {
  10337. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  10338. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  10339. }
  10340. //---------------------------------------------------------------------------
  10341. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  10342. {
  10343. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  10344. OpenBookmark(Side, Bookmark);
  10345. }
  10346. //---------------------------------------------------------------------------
  10347. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  10348. {
  10349. DoBookmarkClick(osLocal, Sender);
  10350. }
  10351. //---------------------------------------------------------------------------
  10352. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  10353. {
  10354. DoBookmarkClick(osRemote, Sender);
  10355. }
  10356. //---------------------------------------------------------------------------
  10357. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  10358. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  10359. {
  10360. if (BookmarkList != NULL)
  10361. {
  10362. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  10363. if (IsLocalBrowserMode())
  10364. {
  10365. Side = osLocal;
  10366. }
  10367. if (!WinConfiguration->UseLocationProfiles)
  10368. {
  10369. std::unique_ptr<TStringList> Directories(new TStringList());
  10370. Directories->CaseSensitive = (Side == osRemote);
  10371. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10372. {
  10373. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10374. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  10375. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  10376. {
  10377. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10378. Item->Caption = EscapeHotkey(Directory);
  10379. Item->ShortCut = Bookmark->ShortCut;
  10380. Item->OnClick = OnBookmarkClick;
  10381. Item->Tag = reinterpret_cast<int>(Bookmark);
  10382. Directories->Add(Directory);
  10383. Menu->Add(Item.release());
  10384. }
  10385. }
  10386. }
  10387. else
  10388. {
  10389. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10390. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10391. {
  10392. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10393. if (!Bookmark->Node.IsEmpty())
  10394. {
  10395. Folders->Add(Bookmark->Node);
  10396. }
  10397. }
  10398. for (int Index = 0; Index < Folders->Count; Index++)
  10399. {
  10400. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10401. Item->Caption = Folders->Strings[Index];
  10402. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10403. Folders->Objects[Index] = Item.get();
  10404. Menu->Add(Item.release());
  10405. }
  10406. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10407. {
  10408. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10409. TTBCustomItem * Parent;
  10410. if (!Bookmark->Node.IsEmpty())
  10411. {
  10412. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10413. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10414. }
  10415. else
  10416. {
  10417. Parent = Menu;
  10418. }
  10419. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10420. Item->Caption = Bookmark->Name;
  10421. Item->ShortCut = Bookmark->ShortCut;
  10422. Item->OnClick = OnBookmarkClick;
  10423. Item->Tag = reinterpret_cast<int>(Bookmark);
  10424. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10425. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10426. {
  10427. // No hint for location profiles that are actually mere "bookmarks"
  10428. }
  10429. else
  10430. {
  10431. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10432. UnicodeString LongHint;
  10433. if (!Bookmark->Local.IsEmpty())
  10434. {
  10435. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10436. }
  10437. if (!Bookmark->Remote.IsEmpty())
  10438. {
  10439. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10440. }
  10441. AddToList(Hint, LongHint, L"|");
  10442. Item->Hint = Hint;
  10443. }
  10444. Parent->Add(Item.release());
  10445. }
  10446. }
  10447. AddMenuSeparator(Menu);
  10448. }
  10449. }
  10450. //---------------------------------------------------------------------------
  10451. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10452. {
  10453. Menu->Clear();
  10454. std::unique_ptr<TTBCustomItem> Item;
  10455. Item.reset(new TTBXItem(Owner));
  10456. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10457. Item->Options = Item->Options << tboDefault;
  10458. Menu->Add(Item.release());
  10459. AddMenuSeparator(Menu);
  10460. if (Terminal != NULL)
  10461. {
  10462. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10463. }
  10464. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10465. Item.reset(new TTBXItem(Owner));
  10466. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10467. Menu->Add(Item.release());
  10468. }
  10469. //---------------------------------------------------------------------------
  10470. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10471. {
  10472. bool Result = true;
  10473. bool Remote = !IsSideLocalBrowser(Side);
  10474. try
  10475. {
  10476. UnicodeString APath = Path;
  10477. if (Remote)
  10478. {
  10479. Terminal->ExceptionOnFail = true;
  10480. }
  10481. else
  10482. {
  10483. APath = ExpandEnvironmentVariables(Path);
  10484. }
  10485. try
  10486. {
  10487. DirView(Side)->Path = APath;
  10488. }
  10489. __finally
  10490. {
  10491. if (Remote)
  10492. {
  10493. Terminal->ExceptionOnFail = false;
  10494. }
  10495. }
  10496. }
  10497. catch (Exception & E)
  10498. {
  10499. if (!Remote || Terminal->Active)
  10500. {
  10501. ShowExtendedExceptionEx(Terminal, &E);
  10502. Result = false;
  10503. }
  10504. else
  10505. {
  10506. throw;
  10507. }
  10508. }
  10509. return Result;
  10510. }
  10511. //---------------------------------------------------------------------------
  10512. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10513. {
  10514. // Make sure there some feedback even when loading tiny local folders
  10515. TInstantOperationVisualizer Visualizer;
  10516. DirView(Side)->ReloadDirectory();
  10517. }
  10518. //---------------------------------------------------------------------------
  10519. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10520. {
  10521. if (Index == SessionsPageControl->TabIndex)
  10522. {
  10523. CloseTab();
  10524. }
  10525. else
  10526. {
  10527. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10528. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10529. TTerminalManager * Manager = TTerminalManager::Instance();
  10530. if (!Terminal->Active)
  10531. {
  10532. Manager->FreeTerminal(Terminal);
  10533. }
  10534. else
  10535. {
  10536. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10537. if (CanCloseQueue(Queue))
  10538. {
  10539. Manager->FreeTerminal(Terminal);
  10540. }
  10541. }
  10542. }
  10543. }
  10544. //---------------------------------------------------------------------------
  10545. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10546. {
  10547. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10548. if (Session == NULL)
  10549. {
  10550. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10551. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10552. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10553. //P = SessionsPageControl->ClientToScreen(P);
  10554. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10555. }
  10556. else
  10557. {
  10558. CloseSessionTab(Index);
  10559. }
  10560. }
  10561. //---------------------------------------------------------------------------
  10562. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10563. {
  10564. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10565. {
  10566. TInstantOperationVisualizer Visualizer;
  10567. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10568. // before we create a new data object
  10569. ClipboardClear();
  10570. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10571. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10572. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10573. // Intermediate variable to WORKAROUND a crash in compiler
  10574. TObjectList * ClipboardFakeMonitors =
  10575. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10576. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10577. if (FClipboardDragDropFilesEx.get() == NULL)
  10578. {
  10579. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10580. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10581. }
  10582. FClipboardDragDropFilesEx->FileList->Clear();
  10583. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10584. FClipboardDragDropFilesEx->CopyToClipboard();
  10585. FClipboardTerminal = Terminal;
  10586. // Need full paths, as cwd can be different once files are pasted
  10587. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10588. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10589. {
  10590. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10591. }
  10592. }
  10593. }
  10594. //---------------------------------------------------------------------------
  10595. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10596. {
  10597. if (OpenClipboard(0))
  10598. {
  10599. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10600. CloseClipboard();
  10601. }
  10602. }
  10603. //---------------------------------------------------------------------------
  10604. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10605. {
  10606. return !FClipboardFakeDirectory.IsEmpty();
  10607. }
  10608. //---------------------------------------------------------------------------
  10609. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10610. {
  10611. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10612. FClipboardFakeDirectory = UnicodeString();
  10613. FClipboardTerminal = NULL;
  10614. if (FDownloadingFromClipboard)
  10615. {
  10616. // We are called by the monitor, so attempt to release it would deadlock
  10617. FClipboardFakeMonitorsPendingReset = true;
  10618. }
  10619. else
  10620. {
  10621. FClipboardFakeMonitors.reset(NULL);
  10622. }
  10623. }
  10624. //---------------------------------------------------------------------------
  10625. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10626. {
  10627. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10628. {
  10629. ClipboardStop();
  10630. }
  10631. }
  10632. //---------------------------------------------------------------------------
  10633. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10634. {
  10635. TTerminalManager * Manager = TTerminalManager::Instance();
  10636. Manager->ActiveSession = FClipboardTerminal;
  10637. TTransferOperationParam Params;
  10638. Params.TargetDirectory = TargetDirectory;
  10639. Params.DragDrop = DragDrop;
  10640. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10641. }
  10642. //---------------------------------------------------------------------------
  10643. void TCustomScpExplorerForm::PasteFiles()
  10644. {
  10645. // Guard against possible race conditions
  10646. if (!FClipboardPasteTarget.IsEmpty())
  10647. {
  10648. // ClipboardFakeCreated might be called while we are here, so make sure we finish operating on the same path we started with
  10649. UnicodeString Target = FClipboardPasteTarget;
  10650. FClipboardPasteTarget = EmptyStr;
  10651. // Can fail as it can be e.g. locked by AV, so we retry that later
  10652. bool Removed = RemoveDir(ApiPath(Target));
  10653. try
  10654. {
  10655. if (!NonVisualDataModule->Busy)
  10656. {
  10657. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10658. FClipboardFakeMonitorsPendingReset = false;
  10659. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10660. TAutoFlag Flag(FDownloadingFromClipboard);
  10661. ClipboardDownload(ExtractFilePath(Target), NoConfirmation, true);
  10662. }
  10663. }
  10664. __finally
  10665. {
  10666. if (!Removed)
  10667. {
  10668. RemoveDir(ApiPath(Target));
  10669. }
  10670. }
  10671. }
  10672. }
  10673. //---------------------------------------------------------------------------
  10674. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10675. {
  10676. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10677. // (as it pumps a Windows message queue while being shutted down)
  10678. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10679. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10680. {
  10681. AppLogFmt(L"Fake clipboard directory pasted to \"%s\"", (FileName));
  10682. FClipboardPasteTarget = FileName;
  10683. // Particularly when pasting to external HDD, the fake directory is strangely recreated if removed here in the monitor callback.
  10684. // Postponing removing (or actually whole pasting operation) seems to help.
  10685. PostMessage(Handle, WM_PASTE_FILES, 0, 0);
  10686. }
  10687. }
  10688. //---------------------------------------------------------------------------
  10689. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10690. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10691. {
  10692. TFileMasks::TParams MaskParams;
  10693. MaskParams.Size = Size;
  10694. MaskParams.Modification = Modification;
  10695. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10696. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10697. // TUnixDirView::ItemFullFileName returns trailing backslash for directories, what filemask cannot deal with
  10698. if (!Local && Directory)
  10699. {
  10700. FileName = UnixExcludeTrailingBackslash(FileName);
  10701. }
  10702. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10703. {
  10704. bool ImplicitMatch;
  10705. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10706. !ImplicitMatch)
  10707. {
  10708. Color = Iter->Color;
  10709. break;
  10710. }
  10711. }
  10712. }
  10713. //---------------------------------------------------------------------------
  10714. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10715. {
  10716. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10717. if (!DirView->IsEditing())
  10718. {
  10719. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10720. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10721. // while searching
  10722. if (Key == VK_BACK)
  10723. {
  10724. if (!FIncrementalSearch.IsEmpty())
  10725. {
  10726. if (FIncrementalSearch.Length() == 1)
  10727. {
  10728. ResetIncrementalSearch();
  10729. }
  10730. else
  10731. {
  10732. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10733. IncrementalSearch(NewText, false, false);
  10734. }
  10735. Key = 0;
  10736. }
  10737. }
  10738. }
  10739. }
  10740. //---------------------------------------------------------------------------
  10741. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10742. {
  10743. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10744. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10745. {
  10746. // Filter control sequences.
  10747. // When not searching yet, prefer use of the space for toggling file selection
  10748. // (so we cannot incrementally search for a string starting with a space).
  10749. if ((Key > VK_SPACE) ||
  10750. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10751. {
  10752. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10753. Key = 0;
  10754. }
  10755. }
  10756. }
  10757. //---------------------------------------------------------------------------
  10758. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10759. {
  10760. if (!FIncrementalSearch.IsEmpty())
  10761. {
  10762. FIncrementalSearch = L"";
  10763. DirView(osCurrent)->UpdateStatusBar();
  10764. }
  10765. }
  10766. //---------------------------------------------------------------------------
  10767. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10768. {
  10769. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10770. if (Item == NULL)
  10771. {
  10772. MessageBeep(MB_ICONHAND);
  10773. }
  10774. else
  10775. {
  10776. TCustomDirView * ADirView = DirView(osCurrent);
  10777. {
  10778. TAutoNestingCounter Guard(FIncrementalSearching);
  10779. ADirView->FocusItem(Item);
  10780. if (ADirView->NortonLike == nlOff)
  10781. {
  10782. if ((ADirView->Selected != Item) ||
  10783. (ADirView->SelCount > 1))
  10784. {
  10785. ADirView->ClearSelection();
  10786. ADirView->Selected = Item;
  10787. }
  10788. }
  10789. }
  10790. FIncrementalSearch = Text;
  10791. Item->MakeVisible(false);
  10792. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10793. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10794. ADirView->UpdateStatusBar();
  10795. }
  10796. }
  10797. //---------------------------------------------------------------------------
  10798. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10799. {
  10800. int Index = Item->Index;
  10801. if (!Reverse)
  10802. {
  10803. Index++;
  10804. if (Index >= Item->Owner->Count)
  10805. {
  10806. Index = 0;
  10807. }
  10808. }
  10809. else
  10810. {
  10811. Index--;
  10812. if (Index < 0)
  10813. {
  10814. Index = Item->Owner->Count - 1;
  10815. }
  10816. }
  10817. return Item->Owner->Item[Index];
  10818. }
  10819. //---------------------------------------------------------------------------
  10820. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10821. {
  10822. TCustomDirView * ADirView = DirView(osCurrent);
  10823. if (ADirView->Items->Count == 0)
  10824. {
  10825. return NULL;
  10826. }
  10827. else
  10828. {
  10829. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10830. TListItem * Item = CurrentItem;
  10831. if (SkipCurrent)
  10832. {
  10833. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10834. }
  10835. while (true)
  10836. {
  10837. bool Matches = false;
  10838. switch (WinConfiguration->PanelSearch)
  10839. {
  10840. case isNameStartOnly:
  10841. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item).SubString(1, Text.Length()), Text);
  10842. break;
  10843. case isName:
  10844. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item), Text);
  10845. break;
  10846. case isAll:
  10847. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10848. int Index = 0;
  10849. while ((Index < ColCount) && !Matches)
  10850. {
  10851. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10852. if (Visible)
  10853. {
  10854. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10855. }
  10856. Index++;
  10857. }
  10858. break;
  10859. }
  10860. if (Matches)
  10861. {
  10862. return Item;
  10863. }
  10864. Item = GetNextFile(Item, Reverse);
  10865. if (Item == CurrentItem)
  10866. {
  10867. return NULL;
  10868. }
  10869. }
  10870. }
  10871. }
  10872. //---------------------------------------------------------------------------
  10873. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10874. {
  10875. ResetIncrementalSearch();
  10876. }
  10877. //---------------------------------------------------------------------------
  10878. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10879. {
  10880. if (Message.CharCode == VK_TAB)
  10881. {
  10882. if (!FIncrementalSearch.IsEmpty())
  10883. {
  10884. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10885. bool Reverse = Shift.Contains(ssShift);
  10886. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10887. Message.Result = 1;
  10888. return;
  10889. }
  10890. }
  10891. else if (Message.CharCode == VK_ESCAPE)
  10892. {
  10893. if (!FIncrementalSearch.IsEmpty())
  10894. {
  10895. ResetIncrementalSearch();
  10896. Message.Result = 1;
  10897. return;
  10898. }
  10899. }
  10900. TForm::Dispatch(&Message);
  10901. }
  10902. //---------------------------------------------------------------------------
  10903. void __fastcall TCustomScpExplorerForm::Deactivate()
  10904. {
  10905. TForm::Deactivate();
  10906. // E.g. when switching to an internal editor window
  10907. ResetIncrementalSearch();
  10908. }
  10909. //---------------------------------------------------------------------------
  10910. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10911. {
  10912. // When switching to another application
  10913. ResetIncrementalSearch();
  10914. }
  10915. //---------------------------------------------------------------------------
  10916. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10917. {
  10918. ResetIncrementalSearch();
  10919. }
  10920. //---------------------------------------------------------------------------
  10921. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10922. {
  10923. if (FIncrementalSearching <= 0)
  10924. {
  10925. ResetIncrementalSearch();
  10926. }
  10927. }
  10928. //---------------------------------------------------------------------------
  10929. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10930. {
  10931. CountClicksForWindowPrint(this);
  10932. }
  10933. //---------------------------------------------------------------------------
  10934. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10935. {
  10936. CenterReconnectToolbar();
  10937. }
  10938. //---------------------------------------------------------------------------
  10939. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10940. {
  10941. if (RemoteDirView->ItemFocused != NULL)
  10942. {
  10943. RemoteDirView->ItemFocused->Selected = true;
  10944. }
  10945. }
  10946. //---------------------------------------------------------------------------
  10947. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10948. {
  10949. if (!FQueueStatusUpdating)
  10950. {
  10951. bool Refresh;
  10952. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10953. {
  10954. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10955. }
  10956. else
  10957. {
  10958. if (FQueueFileList->GetCount() > 0)
  10959. {
  10960. FQueueFileList->Clear();
  10961. Refresh = true;
  10962. }
  10963. }
  10964. int Count = FQueueFileList->GetCount();
  10965. if (QueueFileList->Items->Count != Count)
  10966. {
  10967. DebugAssert(Refresh);
  10968. QueueFileList->Items->Count = Count;
  10969. }
  10970. if (Refresh)
  10971. {
  10972. QueueFileList->Invalidate();
  10973. }
  10974. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10975. }
  10976. }
  10977. //---------------------------------------------------------------------------
  10978. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10979. {
  10980. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10981. }
  10982. //---------------------------------------------------------------------------
  10983. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10984. {
  10985. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10986. // See TSynchronizeChecklistDialog::UpdateTimer
  10987. if (QueueFileList->Items->Count > 0)
  10988. {
  10989. QueueFileList->Items->Item[0]->MakeVisible(false);
  10990. }
  10991. UpdateQueueFileList();
  10992. }
  10993. //---------------------------------------------------------------------------
  10994. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10995. {
  10996. Active = QueueView3->Focused() || QueueFileList->Focused();
  10997. }
  10998. //---------------------------------------------------------------------------
  10999. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  11000. {
  11001. QueueLabelUpdateStatus();
  11002. }
  11003. //---------------------------------------------------------------------------
  11004. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  11005. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  11006. {
  11007. int State = FQueueFileList->GetState(Item->Index);
  11008. if (State == qfsProcessed)
  11009. {
  11010. Sender->Canvas->Font->Color = clGrayText;
  11011. }
  11012. }
  11013. //---------------------------------------------------------------------------
  11014. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  11015. {
  11016. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  11017. QueueFileList->Column[0]->Width =
  11018. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  11019. }
  11020. //---------------------------------------------------------------------------
  11021. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  11022. {
  11023. QueueFileListColumnAutoSize();
  11024. }
  11025. //---------------------------------------------------------------------------
  11026. void __fastcall TCustomScpExplorerForm::CloseApp()
  11027. {
  11028. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  11029. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  11030. // So we clear the flag here.
  11031. DebugAssert(NonVisualDataModule->Busy);
  11032. NonVisualDataModule->EndBusy();
  11033. try
  11034. {
  11035. Close();
  11036. }
  11037. __finally
  11038. {
  11039. NonVisualDataModule->StartBusy();
  11040. }
  11041. }
  11042. //---------------------------------------------------------------------------
  11043. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  11044. {
  11045. return (Terminal != NULL) && Terminal->Active;
  11046. }
  11047. //---------------------------------------------------------------------------
  11048. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  11049. {
  11050. return (ManagedSession != NULL);
  11051. }
  11052. //---------------------------------------------------------------------------
  11053. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  11054. {
  11055. return IsActiveTerminal(Terminal);
  11056. }
  11057. //---------------------------------------------------------------------------
  11058. void TCustomScpExplorerForm::LocalLocalCopy(
  11059. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  11060. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  11061. {
  11062. DebugFail();
  11063. }
  11064. //---------------------------------------------------------------------------
  11065. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  11066. {
  11067. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  11068. }
  11069. //---------------------------------------------------------------------------
  11070. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  11071. {
  11072. // 1) Changing tab list triggers the OnResize.
  11073. // 2) Is called in TForm constructor, when we are not ready to call SessionListChanged (particularly when starting with colored session)
  11074. if (!FUpdatingSessionTabs && (FSessionColors != NULL))
  11075. {
  11076. // UpdateTabsCaptionTruncation should be enough
  11077. SessionListChanged(true);
  11078. }
  11079. }
  11080. //---------------------------------------------------------------------------
  11081. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  11082. {
  11083. if (ASession == ManagedSession)
  11084. {
  11085. UpdateSession(ASession);
  11086. }
  11087. TSessionData * Data = ASession->StateData;
  11088. UnicodeString Result;
  11089. if (Side == osLocal)
  11090. {
  11091. Result = Data->LocalDirectory;
  11092. }
  11093. else if (DebugAlwaysTrue(Side == osOther))
  11094. {
  11095. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  11096. }
  11097. return Result;
  11098. }
  11099. //---------------------------------------------------------------------------
  11100. UnicodeString TCustomScpExplorerForm::GetTabHintSessionDetails(TManagedTerminal * ASession)
  11101. {
  11102. UnicodeString Title = TTerminalManager::Instance()->GetSessionTitle(ASession, false);
  11103. UnicodeString Result = ASession->SessionData->DefaultSessionName;
  11104. if (Title == Result)
  11105. {
  11106. Result = EmptyStr;
  11107. }
  11108. else
  11109. {
  11110. Result += L"\n";
  11111. }
  11112. return Result;
  11113. }
  11114. //---------------------------------------------------------------------------
  11115. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  11116. {
  11117. UnicodeString Result = GetTabHintSessionDetails(ASession);
  11118. if (!ASession->Active)
  11119. {
  11120. Result += LoadStr(STATUS_NOT_CONNECTED2);
  11121. }
  11122. else
  11123. {
  11124. Result += GetSessionPath(ASession, osRemote);
  11125. }
  11126. return Result;
  11127. }
  11128. //---------------------------------------------------------------------------
  11129. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  11130. {
  11131. return LoadStr(NEW_REMOTE_TAB_HINT);
  11132. }
  11133. //---------------------------------------------------------------------------
  11134. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  11135. {
  11136. if (Index >= 0)
  11137. {
  11138. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  11139. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  11140. if (TabSession == NULL)
  11141. {
  11142. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  11143. if (NewTabTabCaption != TabSheet->Caption)
  11144. {
  11145. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  11146. }
  11147. }
  11148. else
  11149. {
  11150. TTerminalManager * Manager = TTerminalManager::Instance();
  11151. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  11152. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  11153. }
  11154. }
  11155. }
  11156. //---------------------------------------------------------------------------
  11157. typedef std::vector<TListItem *> TListItemsVector;
  11158. struct TCalculateSizeOperation
  11159. {
  11160. TCalculateSizeStats Stats;
  11161. TListItemsVector ListItems;
  11162. size_t Index;
  11163. };
  11164. //---------------------------------------------------------------------------
  11165. void TCustomScpExplorerForm::CalculateDirectorySizes(TOperationSide Side)
  11166. {
  11167. TCalculateSizeOperation CalculateSizeOperation;
  11168. Side = GetSide(Side);
  11169. TCustomDirView * ADirView = DirView(Side);
  11170. bool LocalSide = IsSideLocalBrowser(Side);
  11171. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, LocalSide, NULL, true));
  11172. std::unique_ptr<TStrings> FileList(new TStringList());
  11173. for (int Index = 0; Index < AllFileList->Count; Index++)
  11174. {
  11175. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  11176. if (ADirView->ItemIsDirectory(Item))
  11177. {
  11178. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  11179. CalculateSizeOperation.ListItems.push_back(Item);
  11180. }
  11181. }
  11182. CalculateSizeOperation.Index = 0;
  11183. __int64 Size = 0;
  11184. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation);
  11185. FCalculateSizeOperation = &CalculateSizeOperation;
  11186. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  11187. FOnFileOperationFinished = DirectorySizeCalculated;
  11188. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation);
  11189. FPrimaryOperation = foCalculateSize;
  11190. TCalculatedSizes CalculatedSizes;
  11191. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  11192. if (LocalSide)
  11193. {
  11194. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  11195. FProgressSide = Side;
  11196. ManagedSession->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  11197. }
  11198. else
  11199. {
  11200. TCalculateSizeParams Params;
  11201. Params.Stats = &CalculateSizeOperation.Stats;
  11202. Params.UseCache = true;
  11203. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  11204. }
  11205. }
  11206. //---------------------------------------------------------------------------
  11207. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(TOperationSide Side, const UnicodeString & FileName, bool Success)
  11208. {
  11209. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  11210. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  11211. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  11212. // optimization
  11213. bool GotExpectedItem =
  11214. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  11215. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  11216. int Index = -1;
  11217. if (DebugAlwaysTrue(GotExpectedItem))
  11218. {
  11219. Index = FCalculateSizeOperation->Index;
  11220. FCalculateSizeOperation->Index++;
  11221. }
  11222. else
  11223. {
  11224. TListItemsVector::iterator I =
  11225. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  11226. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  11227. {
  11228. Index = I - FCalculateSizeOperation->ListItems.begin();
  11229. }
  11230. }
  11231. if (DebugAlwaysTrue(Index >= 0) && Success)
  11232. {
  11233. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  11234. ADirView->SetItemCalculatedSize(Item, Size);
  11235. }
  11236. }
  11237. //---------------------------------------------------------------------------
  11238. void __fastcall TCustomScpExplorerForm::MessageDockRequestDock(TObject *, TTBCustomDockableWindow *, bool & Accept)
  11239. {
  11240. Accept = false;
  11241. }
  11242. //---------------------------------------------------------------------------
  11243. void TCustomScpExplorerForm::AutoSizeColumns(TOperationSide Side)
  11244. {
  11245. // Send Ctrl+Gray+ to the list view
  11246. TKeyboardState KeyState;
  11247. GetKeyboardState(KeyState);
  11248. TKeyboardState NewKeyState;
  11249. memset(NewKeyState, 0, sizeof(NewKeyState));
  11250. NewKeyState[VK_CONTROL] = 0x81;
  11251. SetKeyboardState(NewKeyState);
  11252. SendMessage(DirView(Side)->Handle, WM_KEYDOWN, VK_ADD, 0);
  11253. SetKeyboardState(KeyState);
  11254. }
  11255. //---------------------------------------------------------------------------