CustomScpExplorer.cpp 392 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <algorithm>
  8. #include <Bookmarks.h>
  9. #include <Interface.h>
  10. #include <Exceptions.h>
  11. #include <CoreMain.h>
  12. #include <FileSystems.h>
  13. #include <TextsCore.h>
  14. #include <TextsWin.h>
  15. #include <HelpWin.h>
  16. #include <VCLCommon.h>
  17. #include <Progress.h>
  18. #include <SynchronizeProgress.h>
  19. #include <DragExt.h>
  20. #include <WinApi.h>
  21. #include "GUITools.h"
  22. #include "NonVisual.h"
  23. #include "Glyphs.h"
  24. #include "Tools.h"
  25. #include "WinConfiguration.h"
  26. #include "TerminalManager.h"
  27. #include "EditorManager.h"
  28. #include "ProgParams.h"
  29. #include "Setup.h"
  30. #include <Consts.hpp>
  31. #include <DateUtils.hpp>
  32. #include <TB2Common.hpp>
  33. #include <DirectoryMonitor.hpp>
  34. #include <System.IOUtils.hpp>
  35. //---------------------------------------------------------------------------
  36. #pragma package(smart_init)
  37. #pragma link "CustomDirView"
  38. #pragma link "CustomUnixDirView"
  39. #pragma link "IEListView"
  40. #pragma link "NortonLikeListView"
  41. #pragma link "UnixDirView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "UnixDriveView"
  44. #pragma link "CustomDriveView"
  45. #pragma link "TB2Dock"
  46. #pragma link "TBXStatusBars"
  47. #pragma link "TB2Item"
  48. #pragma link "TB2Toolbar"
  49. #pragma link "ThemePageControl"
  50. #pragma link "PathLabel"
  51. #pragma resource "*.dfm"
  52. //---------------------------------------------------------------------------
  53. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  54. static const int SessionPanelCount = 3;
  55. //---------------------------------------------------------------------------
  56. class TMutexGuard
  57. {
  58. public:
  59. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  60. int Timeout = 5000)
  61. {
  62. FMutex = NULL;
  63. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  64. if (WaitResult == WAIT_TIMEOUT)
  65. {
  66. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  67. }
  68. else
  69. {
  70. FMutex = AMutex;
  71. }
  72. }
  73. ~TMutexGuard()
  74. {
  75. if (FMutex != NULL)
  76. {
  77. ReleaseMutex(FMutex);
  78. }
  79. }
  80. private:
  81. HANDLE FMutex;
  82. };
  83. //---------------------------------------------------------------------------
  84. class TWindowLock
  85. {
  86. public:
  87. TWindowLock(TCustomScpExplorerForm * Form) :
  88. FForm(Form)
  89. {
  90. FForm->LockWindow();
  91. }
  92. ~TWindowLock()
  93. {
  94. FForm->UnlockWindow();
  95. }
  96. private:
  97. TCustomScpExplorerForm * FForm;
  98. };
  99. //---------------------------------------------------------------------------
  100. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  101. FForm(Form)
  102. {
  103. FForm->BatchStart(FBatchStorage);
  104. }
  105. //---------------------------------------------------------------------------
  106. TAutoBatch::~TAutoBatch()
  107. {
  108. FForm->BatchEnd(FBatchStorage);
  109. }
  110. //---------------------------------------------------------------------------
  111. struct TTransferOperationParam
  112. {
  113. TTransferOperationParam();
  114. UnicodeString TargetDirectory;
  115. bool Temp;
  116. bool DragDrop;
  117. int Options;
  118. TAutoSwitch Queue;
  119. };
  120. //---------------------------------------------------------------------------
  121. TTransferOperationParam::TTransferOperationParam()
  122. {
  123. Temp = false;
  124. DragDrop = false;
  125. Options = 0;
  126. Queue = asAuto;
  127. }
  128. //---------------------------------------------------------------------------
  129. class TTransferPresetNoteData : public TObject
  130. {
  131. public:
  132. UnicodeString Message;
  133. };
  134. //---------------------------------------------------------------------------
  135. class TTerminalNoteData : public TObject
  136. {
  137. public:
  138. TManagedTerminal * Terminal;
  139. };
  140. //---------------------------------------------------------------------------
  141. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  142. FFormRestored(false),
  143. TForm(Owner)
  144. {
  145. AddStartupSequence(L"F");
  146. FInvalid = false;
  147. FCurrentSide = osRemote;
  148. FEverShown = false;
  149. FDocks = new TList();
  150. InitControls();
  151. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  152. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  153. NonVisualDataModule->ScpExplorer = this;
  154. RestoreParams();
  155. ConfigurationChanged();
  156. RemoteDirView->Invalidate();
  157. FAutoOperation = false;
  158. FOnFileOperationFinished = NULL;
  159. FPrimaryOperation = foNone;
  160. FForceExecution = false;
  161. FIgnoreNextDialogChar = 0;
  162. FErrorList = NULL;
  163. FSynchronizeProgressForm = NULL;
  164. FProgressForm = NULL;
  165. FRefreshLocalDirectory = false;
  166. FRefreshRemoteDirectory = false;
  167. FDDExtMapFile = NULL;
  168. // CreateMutexW keeps failing with ERROR_NOACCESS
  169. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  170. DebugAssert(FDDExtMutex != NULL);
  171. FDDTargetControl = NULL;
  172. FDelayedDeletionTimer = NULL;
  173. FDelayedDeletionList = new TStringList();
  174. FDDFileList = NULL;
  175. FPendingTempSpaceWarn = false;
  176. FCapturedLog = NULL;
  177. FDragDropOperation = false;
  178. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  179. FAllowTransferPresetAutoSelect = true;
  180. FSessionChanging = false;
  181. FCopyParamDefault = L"";
  182. FSynchronizeController = NULL;
  183. FPendingQueueActionItem = NULL;
  184. FLockLevel = 0;
  185. FLockSuspendLevel = 0;
  186. FDisabledOnLockSuspend = false;
  187. FAlternativeDelete = false;
  188. FTrayIcon = new ::TTrayIcon(0);
  189. FTrayIcon->OnClick = TrayIconClick;
  190. FMaxQueueLength = 0;
  191. FLastContextPopupScreenPoint = TPoint(-1, -1);
  192. FTransferResumeList = NULL;
  193. FDeletedFiles = NULL;
  194. FMoveToQueue = false;
  195. StandaloneOperation = false;
  196. FOnFeedSynchronizeError = NULL;
  197. FOnSynchronizeAbort = NULL;
  198. FSynchronizeTerminal = NULL;
  199. FNeedSession = false;
  200. FDoNotIdleCurrentTerminal = 0;
  201. FIncrementalSearching = 0;
  202. FIncrementalSearchHaveNext = false;
  203. FQueueFileList.reset(new TQueueFileList());
  204. FProgressSide = osCurrent;
  205. FCalculateSizeOperation = NULL;
  206. FDownloadingFromClipboard = false;
  207. FClipboardFakeMonitorsPendingReset = false;
  208. FEditorManager = new TEditorManager();
  209. FEditorManager->OnFileChange = ExecutedFileChanged;
  210. FEditorManager->OnFileReload = ExecutedFileReload;
  211. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  212. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  213. FLocalEditors = new TList();
  214. FQueueStatus = NULL;
  215. FQueueStatusSection = new TCriticalSection();
  216. FQueueStatusInvalidated = false;
  217. FQueueItemInvalidated = false;
  218. FQueueStatusUpdating = false;
  219. FQueueActedItem = NULL;
  220. FQueueController = new TQueueController(QueueView3);
  221. FQueueScrollOnDragOver = new TListViewScrollOnDragOver(QueueView3, true);
  222. FUserActionTimer = new TTimer(this);
  223. FUserActionTimer->Enabled = false;
  224. FUserActionTimer->Interval = 10;
  225. FUserActionTimer->OnTimer = UserActionTimer;
  226. FNotes = new TStringList();
  227. FNoteTimer = new TTimer(this);
  228. FNoteTimer->Enabled = false;
  229. FNoteTimer->OnTimer = NoteTimer;
  230. FOnNoteClick = NULL;
  231. FOle32Library = LoadLibrary(L"Ole32.dll");
  232. FDragMoveCursor = FOle32Library != NULL ?
  233. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  234. UseSystemSettings(this);
  235. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  236. static_cast<TObject*>(GetComponent(fcTransferList)));
  237. DebugAssert(TransferList != NULL);
  238. FTransferListHoverIndex = -1;
  239. TransferList->OnChange = TransferListChange;
  240. TransferList->OnDrawItem = TransferListDrawItem;
  241. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  242. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  243. UseDesktopFont(SessionsPageControl);
  244. UpdateSessionsPageControlHeight();
  245. UseDesktopFont(RemoteDirView);
  246. UseDesktopFont(RemoteDriveView);
  247. UseDesktopFont(QueueView3);
  248. UseDesktopFont(QueueFileList);
  249. UseDesktopFont(QueueLabel);
  250. UseDesktopFont(RemoteStatusBar);
  251. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  252. QueueSplitter->ShowHint = true;
  253. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  254. QueueFileListSplitter->ShowHint = true;
  255. RemotePanelSplitter->ShowHint = true;
  256. UpdateImages();
  257. FCustomCommandMenu = CreateTBXPopupMenu(this);
  258. FCustomCommandLocalFileList = NULL;
  259. FCustomCommandRemoteFileList = NULL;
  260. FSessionColors = new TPngImageList(this);
  261. FSessionColors->ColorDepth = cd32Bit;
  262. AddFixedSessionImages();
  263. SessionsPageControl->Images = FSessionColors;
  264. UpdateQueueLabel();
  265. CheckStoreTransition();
  266. CreateHiddenWindow();
  267. StartUpdates();
  268. }
  269. //---------------------------------------------------------------------------
  270. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  271. {
  272. FInvalid = true;
  273. // this has to be one of the very first things to do
  274. StopUpdateThread();
  275. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  276. TTerminalManager::Instance()->ScpExplorer = NULL;
  277. delete FCustomCommandLocalFileList;
  278. delete FCustomCommandRemoteFileList;
  279. delete FCustomCommandMenu;
  280. delete FTrayIcon;
  281. FTrayIcon = NULL;
  282. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  283. delete FEditorManager;
  284. ForceCloseLocalEditors();
  285. delete FLocalEditors;
  286. HideFileFindDialog();
  287. if (FDelayedDeletionTimer)
  288. {
  289. DoDelayedDeletion(NULL);
  290. SAFE_DESTROY(FDelayedDeletionTimer);
  291. }
  292. SAFE_DESTROY(FDelayedDeletionList);
  293. // sometimes we do not get DDEnd so the list is not released
  294. SAFE_DESTROY(FDDFileList);
  295. DebugAssert(FSynchronizeController == NULL);
  296. CloseHandle(FDDExtMutex);
  297. FDDExtMutex = NULL;
  298. FreeLibrary(FOle32Library);
  299. FOle32Library = NULL;
  300. FDragMoveCursor = NULL;
  301. DebugAssert(!FErrorList);
  302. if (FEverShown)
  303. {
  304. // when window is never shown (like when running command-line operation),
  305. // particularly window site is not restored correctly (BoundsRect value set
  306. // in RestoreForm gets lost during handle allocation), so we do not want
  307. // it to be stored
  308. StoreParams();
  309. }
  310. ManagedSession = NULL;
  311. Queue = NULL;
  312. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  313. NonVisualDataModule->ScpExplorer = NULL;
  314. delete FQueueScrollOnDragOver;
  315. FQueueScrollOnDragOver = NULL;
  316. delete FQueueController;
  317. FQueueController = NULL;
  318. delete FQueueStatusSection;
  319. FQueueStatusSection = NULL;
  320. delete FQueueStatus;
  321. FQueueStatus = NULL;
  322. delete FUserActionTimer;
  323. FUserActionTimer = NULL;
  324. delete FNoteTimer;
  325. delete FNotes;
  326. delete FNoteData;
  327. SAFE_DESTROY(FDocks);
  328. SessionsPageControl->Images = NULL;
  329. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  330. SAFE_DESTROY(FSessionColors);
  331. SAFE_DESTROY(FSessionsDragDropFilesEx);
  332. SAFE_DESTROY(FHistoryMenu[0][0]);
  333. SAFE_DESTROY(FHistoryMenu[0][1]);
  334. SAFE_DESTROY(FHistoryMenu[1][0]);
  335. SAFE_DESTROY(FHistoryMenu[1][1]);
  336. if (FHiddenWindow != NULL)
  337. {
  338. DestroyWindow(FHiddenWindow);
  339. FHiddenWindow = NULL;
  340. }
  341. }
  342. //---------------------------------------------------------------------------
  343. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  344. {
  345. std::unique_ptr<TSessionData> Data;
  346. if (!Session.IsEmpty())
  347. {
  348. bool DefaultsOnly;
  349. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  350. }
  351. TTerminalManager * Manager = TTerminalManager::Instance();
  352. for (int Index = 0; Index < Manager->Count; Index++)
  353. {
  354. TTerminal * Terminal = Manager->Sessions[Index];
  355. if (Session.IsEmpty() ||
  356. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  357. {
  358. if (Path.IsEmpty())
  359. {
  360. Terminal->ClearCaches();
  361. }
  362. else
  363. {
  364. Terminal->DirectoryModified(Path, true);
  365. }
  366. }
  367. }
  368. // We should flag a pending refresh for the background terminals or busy foreground terminals
  369. if (HasActiveTerminal() &&
  370. CanCommandLineFromAnotherInstance() &&
  371. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  372. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  373. {
  374. Terminal->ReloadDirectory();
  375. }
  376. }
  377. //---------------------------------------------------------------------------
  378. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  379. {
  380. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  381. AppLogFmt(L"Received copy data message with size %d", (int(CopyData->cbData)));
  382. size_t MessageSize = sizeof(TCopyDataMessage);
  383. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  384. if (Result)
  385. {
  386. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  387. Result = (Message.Version == TCopyDataMessage::Version1);
  388. if (Result)
  389. {
  390. switch (Message.Command)
  391. {
  392. case TCopyDataMessage::CommandCanCommandLine:
  393. Result = CanCommandLineFromAnotherInstance();
  394. if (Result)
  395. {
  396. AppLog(L"Can process command-line from other instance");
  397. }
  398. else
  399. {
  400. AppLog(L"Cannot process command-line from other instance");
  401. }
  402. break;
  403. case TCopyDataMessage::CommandCommandLine:
  404. {
  405. UnicodeString CommandLine(Message.CommandLine);
  406. Result = CommandLineFromAnotherInstance(CommandLine);
  407. }
  408. break;
  409. case TCopyDataMessage::RefreshPanel:
  410. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  411. break;
  412. case TCopyDataMessage::MainWindowCheck:
  413. Result = true;
  414. break;
  415. default:
  416. Result = false;
  417. break;
  418. }
  419. }
  420. }
  421. Message.Result = Result ? 1 : 0;
  422. }
  423. //---------------------------------------------------------------------------
  424. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  425. {
  426. WNDCLASS WindowClass = {0};
  427. WindowClass.lpfnWndProc = DefWindowProc;
  428. WindowClass.hInstance = HInstance;
  429. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  430. FHiddenWindow = NULL;
  431. if (RegisterClass(&WindowClass))
  432. {
  433. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  434. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  435. }
  436. }
  437. //---------------------------------------------------------------------------
  438. bool __fastcall TCustomScpExplorerForm::CanConsole()
  439. {
  440. return
  441. HasActiveTerminal() &&
  442. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  443. }
  444. //---------------------------------------------------------------------------
  445. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  446. {
  447. bool Result = Visible && !NonVisualDataModule->Busy;
  448. return Result;
  449. }
  450. //---------------------------------------------------------------------------
  451. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  452. const UnicodeString & CommandLine)
  453. {
  454. TProgramParams Params(CommandLine);
  455. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  456. if (Result)
  457. {
  458. NonVisualDataModule->StartBusy();
  459. try
  460. {
  461. // this action is initiated from another process,
  462. // so it's likely that our window is not visible,
  463. // and user won't see what is going on
  464. Application->BringToFront();
  465. // reload sessions as we may be asked to open a session
  466. // just stored by another instance
  467. StoredSessions->Reload();
  468. UnicodeString SessionName = Params.Param[1];
  469. std::unique_ptr<TObjectList> DataList(new TObjectList());
  470. try
  471. {
  472. UnicodeString DownloadFile; // unused
  473. int Flags = GetCommandLineParseUrlFlags(&Params);
  474. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  475. }
  476. catch (EAbort &)
  477. {
  478. // Happens when opening session in PuTTY and in other situations,
  479. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  480. // to process the command-line on its own (or to pass it to yet another instance)
  481. }
  482. if (DataList->Count > 0)
  483. {
  484. TTerminalManager * Manager = TTerminalManager::Instance();
  485. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  486. }
  487. }
  488. __finally
  489. {
  490. NonVisualDataModule->EndBusy();
  491. }
  492. }
  493. return Result;
  494. }
  495. //---------------------------------------------------------------------------
  496. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  497. {
  498. TManagedTerminal * Result;
  499. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  500. {
  501. Result = FManagedSession;
  502. }
  503. else
  504. {
  505. Result = NULL;
  506. }
  507. return Result;
  508. }
  509. //---------------------------------------------------------------------------
  510. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  511. {
  512. if (FManagedSession != value)
  513. {
  514. SessionChanging();
  515. DoSetManagedSession(value, false);
  516. }
  517. }
  518. //---------------------------------------------------------------------------
  519. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  520. {
  521. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  522. FManagedSession = value;
  523. {
  524. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  525. FAllowTransferPresetAutoSelect = false;
  526. TAutoFlag SessionChangingFlag(FSessionChanging);
  527. SessionChanged(Replace);
  528. }
  529. if (Terminal != NULL)
  530. {
  531. TransferPresetAutoSelect();
  532. }
  533. // Update app and tab titles, prevented above by FSessionChanging.
  534. UpdateControls();
  535. }
  536. //---------------------------------------------------------------------------
  537. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  538. {
  539. DoSetManagedSession(value, true);
  540. }
  541. //---------------------------------------------------------------------------
  542. void __fastcall TCustomScpExplorerForm::SessionChanging()
  543. {
  544. if (ManagedSession != NULL)
  545. {
  546. UpdateSession(ManagedSession);
  547. }
  548. }
  549. //---------------------------------------------------------------------------
  550. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  551. {
  552. if (ManagedSession != NULL)
  553. {
  554. UpdateSessionColor(static_cast<TColor>(ManagedSession->StateData->Color));
  555. }
  556. if (!TTerminalManager::Instance()->Updating)
  557. {
  558. SessionListChanged();
  559. }
  560. if (Terminal != NULL)
  561. {
  562. TObject * State = NULL;
  563. if (WinConfiguration->PreservePanelState)
  564. {
  565. State = Terminal->RemoteExplorerState;
  566. DirView(osRemote)->AnnounceState(State);
  567. }
  568. try
  569. {
  570. if (Replaced)
  571. {
  572. RemoteDirView->ReplaceTerminal(Terminal);
  573. }
  574. else
  575. {
  576. RemoteDirView->Terminal = Terminal;
  577. }
  578. if (Terminal->Active)
  579. {
  580. Terminal->RefreshDirectory();
  581. }
  582. if (State != NULL)
  583. {
  584. DirView(osRemote)->RestoreState(State);
  585. }
  586. }
  587. __finally
  588. {
  589. DirView(osRemote)->AnnounceState(NULL);
  590. }
  591. }
  592. else
  593. {
  594. RemoteDirView->Terminal = NULL;
  595. }
  596. InitStatusBar();
  597. UpdateTransferList();
  598. // Update panels Enable state before refreshing the labels
  599. UpdateControls();
  600. }
  601. //---------------------------------------------------------------------------
  602. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  603. {
  604. if (Queue != value)
  605. {
  606. if (FQueue != NULL)
  607. {
  608. FQueue->OnListUpdate = NULL;
  609. FQueue->OnQueueItemUpdate = NULL;
  610. }
  611. FQueue = value;
  612. if (FQueue != NULL)
  613. {
  614. DebugAssert(FQueue->OnListUpdate == NULL);
  615. FQueue->OnListUpdate = QueueListUpdate;
  616. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  617. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  618. }
  619. QueueChanged();
  620. }
  621. }
  622. //---------------------------------------------------------------------------
  623. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  624. TListItem * Item)
  625. {
  626. if (FQueueActedItem == Item)
  627. {
  628. FQueueActedItem = NULL;
  629. if ((QueueView3->PopupMenu != NULL) &&
  630. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  631. {
  632. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  633. ReleaseCapture();
  634. }
  635. }
  636. if (Item->Data == FPendingQueueActionItem)
  637. {
  638. FPendingQueueActionItem = NULL;
  639. }
  640. }
  641. //---------------------------------------------------------------------------
  642. bool TCustomScpExplorerForm::IsAnythingQueued()
  643. {
  644. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  645. }
  646. //---------------------------------------------------------------------------
  647. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  648. {
  649. {
  650. TGuard Guard(FQueueStatusSection);
  651. FQueueStatusInvalidated = false;
  652. if (FQueue != NULL)
  653. {
  654. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  655. }
  656. }
  657. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  658. {
  659. FMaxQueueLength = FQueueStatus->Count;
  660. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  661. }
  662. {
  663. TAutoFlag Flag(FQueueStatusUpdating);
  664. FQueueController->UpdateQueueStatus(FQueueStatus);
  665. }
  666. UpdateQueueFileList();
  667. SetQueueProgress();
  668. UpdateQueueView();
  669. bool IsEmpty = !IsAnythingQueued();
  670. if (IsEmpty && (Terminal != NULL))
  671. {
  672. TOnceDoneOperation OnceDoneOperation =
  673. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  674. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  675. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  676. {
  677. FQueue->Enabled = false;
  678. }
  679. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  680. {
  681. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  682. }
  683. }
  684. }
  685. //---------------------------------------------------------------------------
  686. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  687. {
  688. UnicodeString Result;
  689. if (FQueueStatus != NULL)
  690. {
  691. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  692. {
  693. TFileOperationProgressType * ProgressData =
  694. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  695. if ((ProgressData != NULL) && ProgressData->InProgress)
  696. {
  697. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  698. }
  699. }
  700. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  701. {
  702. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  703. }
  704. }
  705. return Result;
  706. }
  707. //---------------------------------------------------------------------------
  708. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  709. {
  710. ComponentVisible[fcQueueView] =
  711. !IsLocalBrowserMode() &&
  712. ((WinConfiguration->QueueView.Show == qvShow) ||
  713. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  714. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  715. }
  716. //---------------------------------------------------------------------------
  717. void __fastcall TCustomScpExplorerForm::QueueChanged()
  718. {
  719. if (FQueueStatus != NULL)
  720. {
  721. // UpdateFileList implementation relies on the status being recreated when session changes
  722. delete FQueueStatus;
  723. FQueueStatus = NULL;
  724. }
  725. UpdateQueueStatus(true);
  726. }
  727. //---------------------------------------------------------------------------
  728. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  729. {
  730. if (FQueue == Queue)
  731. {
  732. FQueueStatusInvalidated = true;
  733. }
  734. }
  735. //---------------------------------------------------------------------------
  736. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  737. TQueueItem * Item)
  738. {
  739. if (FQueue == Queue)
  740. {
  741. TGuard Guard(FQueueStatusSection);
  742. // this may be running in parallel with QueueChanged
  743. if (FQueueStatus != NULL)
  744. {
  745. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  746. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  747. {
  748. FRefreshLocalDirectory = (QueueItem == NULL) ||
  749. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  750. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  751. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  752. }
  753. if (QueueItem != NULL)
  754. {
  755. QueueItem->UserData = (void*)true;
  756. FQueueItemInvalidated = true;
  757. }
  758. }
  759. }
  760. }
  761. //---------------------------------------------------------------------------
  762. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  763. {
  764. // during standalone editing, we have no way to see/control queue,
  765. // so we have to always popup prompts automatically
  766. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  767. }
  768. //---------------------------------------------------------------------------
  769. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  770. {
  771. if (FQueueStatus != NULL)
  772. {
  773. bool QueueAutoPopup = IsQueueAutoPopup();
  774. bool NeedRefresh = FQueueController->NeedRefresh();
  775. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  776. FQueueItemInvalidated = false;
  777. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  778. bool Updated = false;
  779. TQueueItemProxy * QueueItem;
  780. bool WasUserAction;
  781. for (int Index = 0; Index < Limit; Index++)
  782. {
  783. bool Update = false;
  784. QueueItem = FQueueStatus->Items[Index];
  785. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  786. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  787. (FPendingQueueActionItem == NULL))
  788. {
  789. FPendingQueueActionItem = QueueItem;
  790. FUserActionTimer->Enabled = true;
  791. }
  792. if ((bool)QueueItem->UserData)
  793. {
  794. QueueItem->UserData = (void*)false;
  795. QueueItem->Update();
  796. Updated = true;
  797. Update = true;
  798. }
  799. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  800. {
  801. Update = true;
  802. }
  803. if (Update)
  804. {
  805. FQueueController->RefreshQueueItem(QueueItem);
  806. }
  807. }
  808. if (Updated)
  809. {
  810. NonVisualDataModule->UpdateNonVisibleActions();
  811. SetQueueProgress();
  812. UpdateQueueFileList();
  813. }
  814. }
  815. }
  816. //---------------------------------------------------------------------------
  817. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  818. {
  819. if (FTaskbarList != NULL)
  820. {
  821. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  822. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  823. }
  824. }
  825. //---------------------------------------------------------------------------
  826. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  827. {
  828. if (Progress >= 0)
  829. {
  830. if (FTaskbarList != NULL)
  831. {
  832. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  833. }
  834. }
  835. else
  836. {
  837. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  838. }
  839. }
  840. //---------------------------------------------------------------------------
  841. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  842. {
  843. int OverallProgress;
  844. // FProgressForm is null when this is called from SetQueueProgress
  845. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  846. {
  847. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  848. }
  849. else if (!ProgressData->IsIndeterminate())
  850. {
  851. OverallProgress = ProgressData->OverallProgress();
  852. }
  853. else
  854. {
  855. OverallProgress = -1;
  856. }
  857. SetTaskbarListProgressValue(OverallProgress);
  858. }
  859. //---------------------------------------------------------------------------
  860. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  861. {
  862. TTerminalManager::Instance()->QueueStatusUpdated();
  863. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  864. {
  865. // Do not animate task bar button, when items are queued, but processing is disabled
  866. if ((FQueueStatus != NULL) && (FQueueStatus->ActivePrimaryCount > 0))
  867. {
  868. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  869. {
  870. TFileOperationProgressType * ProgressData = NULL;
  871. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  872. {
  873. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  874. }
  875. if ((ProgressData != NULL) &&
  876. ProgressData->InProgress)
  877. {
  878. SetTaskbarListProgressValue(ProgressData);
  879. }
  880. else
  881. {
  882. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  883. }
  884. }
  885. else
  886. {
  887. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  888. }
  889. }
  890. else
  891. {
  892. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  893. }
  894. }
  895. UpdateQueueLabel();
  896. }
  897. //---------------------------------------------------------------------------
  898. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  899. {
  900. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  901. if (FQueueStatus != NULL)
  902. {
  903. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  904. if (ActiveAndPendingPrimaryCount > 0)
  905. {
  906. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  907. }
  908. }
  909. QueueLabel->Caption = Caption;
  910. }
  911. //---------------------------------------------------------------------------
  912. void TCustomScpExplorerForm::CheckStoreTransition()
  913. {
  914. if (WinConfiguration->StoreTransition == stInit)
  915. {
  916. if (IsUWP())
  917. {
  918. WinConfiguration->StoreTransition = stStoreFresh;
  919. AppLog(L"Fresh UWP/store installation");
  920. }
  921. else
  922. {
  923. WinConfiguration->StoreTransition = stStandard;
  924. AppLog(L"Fresh standard installation");
  925. }
  926. }
  927. else if ((WinConfiguration->StoreTransition == stStandard) && IsUWP())
  928. {
  929. WinConfiguration->StoreTransition = stStoreMigrated;
  930. WinConfiguration->FirstRun = WinConfiguration->FirstRun + L"=>" + StandardDatestamp();
  931. AppLog(L"Standard installation migrated to store installation");
  932. }
  933. if (WinConfiguration->StoreTransition == stStoreMigrated)
  934. {
  935. TTBXToolbar * Toolbar = new TTBXToolbar(MessageDock);
  936. Toolbar->Parent = MessageDock;
  937. Toolbar->FullSize = true;
  938. Toolbar->Color = Application->HintColor;
  939. TTBXLabelItem * LabelItem;
  940. LabelItem = new TTBXLabelItem(Toolbar);
  941. LabelItem->Margin = ScaleByTextHeight(MessageDock, 2); // Is this automatically rescaled?
  942. LabelItem->Caption = LoadStr(STORE_MIGRATION_THANKS);
  943. Toolbar->Items->Add(LabelItem);
  944. TTBXCustomItem * LinkItem = new TTBXItem(Toolbar);
  945. LinkItem->FontSettings->Color = LinkColor;
  946. LinkItem->Caption = LoadStr(STORE_MIGRATION_LEARN);
  947. LinkItem->OnClick = StoreTransitionLinkClick;
  948. Toolbar->Items->Add(LinkItem);
  949. AddMenuSeparator(Toolbar->Items);
  950. TTBCustomItem * CloseItem = new TTBXItem(Toolbar);
  951. CloseItem->Caption = LoadStr(MESSAGE_DISMISS);
  952. CloseItem->OnClick = StoreTransitionCloseClick;
  953. Toolbar->Items->Add(CloseItem);
  954. GiveTBItemPriority(CloseItem);
  955. }
  956. }
  957. //---------------------------------------------------------------------------
  958. void __fastcall TCustomScpExplorerForm::StoreTransitionLinkClick(TObject *)
  959. {
  960. ShowHelp(HELP_STORE_TRANSITION);
  961. }
  962. //---------------------------------------------------------------------------
  963. void __fastcall TCustomScpExplorerForm::StoreTransitionCloseClick(TObject * Sender)
  964. {
  965. TTBCustomItem * Item = DebugNotNull(dynamic_cast<TTBCustomItem *>(Sender));
  966. TTBXToolbar * Toolbar = DebugNotNull(dynamic_cast<TTBXToolbar *>(DebugNotNull(DebugNotNull(Item->Parent)->ParentComponent)));
  967. delete Toolbar;
  968. WinConfiguration->StoreTransition = stStoreAcknowledged;
  969. UpdateControls();
  970. }
  971. //---------------------------------------------------------------------------
  972. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  973. {
  974. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  975. static_cast<TComponent*>(GetComponent(fcTransferList)));
  976. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  977. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  978. TransferList->Strings->BeginUpdate();
  979. try
  980. {
  981. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  982. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  983. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  984. if (FTransferDropDownHint.IsEmpty())
  985. {
  986. FTransferDropDownHint = TransferDropDown->Hint;
  987. }
  988. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  989. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  990. TransferDropDown->Text = StripHotkey(Name);
  991. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  992. (FTransferDropDownHint, StripHotkey(Name),
  993. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  994. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  995. // update the label, otherwise when it is updated only on the first draw
  996. // of the list, it is drawn "bold" for some reason
  997. FTransferListHoverIndex = TransferList->ItemIndex;
  998. UpdateTransferLabel();
  999. }
  1000. __finally
  1001. {
  1002. TransferList->Strings->EndUpdate();
  1003. }
  1004. }
  1005. //---------------------------------------------------------------------------
  1006. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  1007. {
  1008. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  1009. DebugAssert(Toolbar != NULL);
  1010. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  1011. }
  1012. //---------------------------------------------------------------------------
  1013. void __fastcall TCustomScpExplorerForm::UpdateActions()
  1014. {
  1015. TForm::UpdateActions();
  1016. if (ComponentVisible[fcCustomCommandsBand])
  1017. {
  1018. UpdateCustomCommandsToolbar();
  1019. }
  1020. }
  1021. //---------------------------------------------------------------------------
  1022. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  1023. {
  1024. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  1025. }
  1026. //---------------------------------------------------------------------------
  1027. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  1028. {
  1029. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  1030. {
  1031. DirView->RowSelect = WinConfiguration->FullRowSelect;
  1032. // Without this, the panel turns gray and unused part of header turns black
  1033. DirView->Perform(CM_COLORCHANGED, 0, 0);
  1034. }
  1035. }
  1036. //---------------------------------------------------------------------------
  1037. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  1038. {
  1039. Color = GetBtnFaceColor();
  1040. DebugAssert(Configuration && RemoteDirView);
  1041. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1042. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  1043. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  1044. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  1045. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1046. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1047. RemoteDirView->AlwaysSortDirectoriesByName = WinConfiguration->AlwaysSortDirectoriesByName;
  1048. UpdateRowSelect(RemoteDirView);
  1049. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1050. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  1051. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  1052. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1053. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1054. UpdateSessionsPageControlHeight();
  1055. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1056. UpdateToolbarDisplayMode();
  1057. UpdateTransferList();
  1058. if (Terminal != NULL)
  1059. {
  1060. TransferPresetAutoSelect();
  1061. }
  1062. UpdateQueueView();
  1063. UpdateControls();
  1064. // this can be called even before constuctor finishes.
  1065. if (FEditorManager != NULL)
  1066. {
  1067. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  1068. }
  1069. // this can be called even before constuctor finishes.
  1070. if (FLocalEditors != NULL)
  1071. {
  1072. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  1073. {
  1074. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  1075. }
  1076. }
  1077. if (ComponentVisible[fcCustomCommandsBand])
  1078. {
  1079. UpdateCustomCommandsToolbar();
  1080. }
  1081. // show only when keeping queue items forever,
  1082. // otherwise, it's enough to have it in the context menu
  1083. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1084. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1085. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1086. {
  1087. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1088. FFileColorsCurrent = WinConfiguration->FileColors;
  1089. FileColorsChanged();
  1090. }
  1091. }
  1092. //---------------------------------------------------------------------------
  1093. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1094. {
  1095. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1096. DirView->Invalidate();
  1097. }
  1098. //---------------------------------------------------------------------------
  1099. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1100. {
  1101. DoFileColorsChanged(RemoteDirView);
  1102. }
  1103. //---------------------------------------------------------------------------
  1104. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1105. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1106. void * /*Arg*/)
  1107. {
  1108. if (Token != NULL)
  1109. {
  1110. TForm * Editor = dynamic_cast<TForm*>(Token);
  1111. DebugAssert(Editor != NULL);
  1112. ReconfigureEditorForm(Editor);
  1113. }
  1114. }
  1115. //---------------------------------------------------------------------------
  1116. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1117. {
  1118. WinConfiguration->DDTransferConfirmation = asOn;
  1119. }
  1120. //---------------------------------------------------------------------------
  1121. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1122. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1123. {
  1124. // noop
  1125. }
  1126. //---------------------------------------------------------------------------
  1127. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1128. {
  1129. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1130. }
  1131. //---------------------------------------------------------------------------
  1132. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1133. {
  1134. if (DoNotShowAgain)
  1135. {
  1136. if (DragDrop)
  1137. {
  1138. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1139. {
  1140. PopupTrayBalloon(
  1141. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1142. }
  1143. WinConfiguration->DDTransferConfirmation = asOff;
  1144. }
  1145. else
  1146. {
  1147. WinConfiguration->ConfirmTransferring = false;
  1148. }
  1149. }
  1150. else
  1151. {
  1152. // User explicitly unchecked "do not show again",
  1153. // so show him the dialog the next time
  1154. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1155. {
  1156. WinConfiguration->DDTransferConfirmation = asOn;
  1157. }
  1158. }
  1159. }
  1160. //---------------------------------------------------------------------------
  1161. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1162. TTransferDirection Direction, TTransferType Type, bool Temp,
  1163. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1164. bool Confirm, bool DragDrop, int Options)
  1165. {
  1166. bool Result = true;
  1167. DebugAssert(HasActiveTerminal());
  1168. // these parameters are known in advance
  1169. int Params =
  1170. FLAGMASK(Type == ttMove, cpDelete);
  1171. bool ToTemp = (Temp && (Direction == tdToLocal));
  1172. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1173. {
  1174. bool DisableNewerOnly =
  1175. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1176. ToTemp;
  1177. Options |=
  1178. FLAGMASK(ToTemp, coTemp) |
  1179. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1180. coDoNotShowAgain;
  1181. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1182. int CopyParamAttrs = (Direction == tdToRemote ?
  1183. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1184. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1185. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1186. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1187. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1188. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1189. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1190. if (Result)
  1191. {
  1192. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1193. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1194. }
  1195. }
  1196. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1197. {
  1198. Configuration->Usage->Inc(L"TransfersOnBackground");
  1199. // these parameter are known only after transfer dialog
  1200. Params |=
  1201. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1202. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1203. Result = false;
  1204. ClearTransferSourceSelection(Direction);
  1205. }
  1206. return Result;
  1207. }
  1208. //---------------------------------------------------------------------------
  1209. void TCustomScpExplorerForm::ClearOperationSelection(TOperationSide Side)
  1210. {
  1211. if (FOnFileOperationFinished != NULL)
  1212. {
  1213. FOnFileOperationFinished(Side, UnicodeString(), true);
  1214. }
  1215. else
  1216. {
  1217. if (HasDirView[Side])
  1218. {
  1219. DirView(Side)->SelectAll(smNone);
  1220. }
  1221. }
  1222. }
  1223. //---------------------------------------------------------------------------
  1224. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1225. {
  1226. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1227. ClearOperationSelection(Side);
  1228. }
  1229. //---------------------------------------------------------------------------
  1230. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1231. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1232. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1233. int Params)
  1234. {
  1235. DebugAssert(Queue != NULL);
  1236. bool SingleFile = false;
  1237. if (FileList->Count == 1)
  1238. {
  1239. if (Direction == tdToRemote)
  1240. {
  1241. UnicodeString FileName = FileList->Strings[0];
  1242. SingleFile = FileExists(ApiPath(FileName));
  1243. }
  1244. else
  1245. {
  1246. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1247. SingleFile = !File->IsDirectory;
  1248. }
  1249. }
  1250. TQueueItem * QueueItem;
  1251. if (Direction == tdToRemote)
  1252. {
  1253. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1254. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1255. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1256. }
  1257. else
  1258. {
  1259. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1260. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1261. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1262. }
  1263. AddQueueItem(Queue, QueueItem, Terminal);
  1264. }
  1265. //---------------------------------------------------------------------------
  1266. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1267. {
  1268. if (Queue->IsEmpty)
  1269. {
  1270. Terminal->QueueOperationStart = Now();
  1271. }
  1272. Queue->AddItem(QueueItem);
  1273. }
  1274. //---------------------------------------------------------------------------
  1275. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1276. {
  1277. }
  1278. //---------------------------------------------------------------------------
  1279. void __fastcall TCustomScpExplorerForm::InitControls()
  1280. {
  1281. DebugAssert(FDocks != NULL);
  1282. for (int Index = 0; Index < ComponentCount; Index++)
  1283. {
  1284. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1285. if ((Dock != NULL) && (Dock->Tag == 0))
  1286. {
  1287. FDocks->Add(Dock);
  1288. }
  1289. }
  1290. CollectItemsWithTextDisplayMode(this);
  1291. }
  1292. //---------------------------------------------------------------------------
  1293. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1294. {
  1295. ConfigurationChanged();
  1296. QueuePanel->Height =
  1297. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1298. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1299. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1300. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1301. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1302. QueueFileList->Height =
  1303. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1304. if (QueueFileList->Visible)
  1305. {
  1306. AdjustQueueLayout();
  1307. }
  1308. UpdateCustomCommandsToolbar();
  1309. }
  1310. //---------------------------------------------------------------------------
  1311. void __fastcall TCustomScpExplorerForm::StoreParams()
  1312. {
  1313. int APixelsPerInch = GetControlPixelsPerInch(this);
  1314. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1315. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1316. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1317. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1318. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1319. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1320. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1321. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1322. }
  1323. //---------------------------------------------------------------------------
  1324. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1325. {
  1326. if (!FFormRestored)
  1327. {
  1328. FFormRestored = true;
  1329. RestoreFormParams();
  1330. }
  1331. TForm::CreateParams(Params);
  1332. }
  1333. //---------------------------------------------------------------------------
  1334. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1335. {
  1336. DebugAssert(FDocks != NULL);
  1337. for (int Index = 0; Index < FDocks->Count; Index++)
  1338. {
  1339. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1340. }
  1341. }
  1342. //---------------------------------------------------------------------------
  1343. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1344. {
  1345. SetDockAllowDrag(true);
  1346. while (!ButtonsStr.IsEmpty())
  1347. {
  1348. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1349. UnicodeString Name = CutToChar(S, L'=', true);
  1350. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1351. {
  1352. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1353. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1354. {
  1355. while (!S.IsEmpty())
  1356. {
  1357. UnicodeString S2 = CutToChar(S, L',', true);
  1358. UnicodeString Name = CutToChar(S2, L':', true);
  1359. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1360. {
  1361. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1362. if (SameText(GetToolbarItemName(Item), Name))
  1363. {
  1364. // forward compatibility
  1365. UnicodeString S3 = CutToChar(S2, L':', true);
  1366. bool Visible = StrToIntDef(S3, 0);
  1367. Item->Visible = Visible;
  1368. }
  1369. }
  1370. }
  1371. }
  1372. }
  1373. }
  1374. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1375. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1376. }
  1377. //---------------------------------------------------------------------------
  1378. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1379. {
  1380. UnicodeString Result;
  1381. SetDockAllowDrag(true);
  1382. Result = ::GetToolbarsLayoutStr(this);
  1383. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1384. return Result;
  1385. }
  1386. //---------------------------------------------------------------------------
  1387. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1388. {
  1389. UnicodeString Result;
  1390. if (Item->Action != NULL)
  1391. {
  1392. Result = Item->Action->Name;
  1393. Result = RemoveSuffix(Result, L"Action", true);
  1394. }
  1395. else
  1396. {
  1397. Result = Item->Name;
  1398. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1399. Result = RemoveSuffix(Result, L"Item", true);
  1400. }
  1401. return Result;
  1402. }
  1403. //---------------------------------------------------------------------------
  1404. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1405. {
  1406. UnicodeString Result;
  1407. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1408. {
  1409. UnicodeString ToolbarStr;
  1410. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1411. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1412. {
  1413. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1414. {
  1415. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1416. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1417. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1418. // and save the changes here only.
  1419. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1420. {
  1421. UnicodeString Name = GetToolbarItemName(Item);
  1422. DebugAssert(Name.Pos(L"TBX") == 0);
  1423. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1424. }
  1425. }
  1426. }
  1427. if (!ToolbarStr.IsEmpty())
  1428. {
  1429. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1430. }
  1431. }
  1432. return Result;
  1433. }
  1434. //---------------------------------------------------------------------------
  1435. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1436. {
  1437. DebugAssert(FProgressForm == NULL);
  1438. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1439. bool AllowMoveToQueue = (FTransferResumeList != NULL) || (FDeletedFiles != NULL);
  1440. FProgressForm = new TProgressForm(Application, AllowMoveToQueue, AllowSkip, SynchronizeProgress);
  1441. FProgressForm->DeleteLocalToRecycleBin =
  1442. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1443. FProgressForm->DeleteRemoteToRecycleBin =
  1444. HasActiveTerminal() &&
  1445. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1446. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1447. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1448. LockWindow();
  1449. // Actually, do not know what hides the progress once the operation finishes
  1450. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1451. SetTaskbarListProgressState(TBPF_NORMAL);
  1452. }
  1453. //---------------------------------------------------------------------------
  1454. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1455. {
  1456. SAFE_DESTROY(FProgressForm);
  1457. UnlockWindow();
  1458. SetQueueProgress();
  1459. }
  1460. //---------------------------------------------------------------------------
  1461. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1462. TFileOperationProgressType & ProgressData)
  1463. {
  1464. // Download to temporary local directory
  1465. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1466. {
  1467. bool Continue = true;
  1468. FPendingTempSpaceWarn = false;
  1469. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1470. if (!Continue)
  1471. {
  1472. Abort();
  1473. }
  1474. }
  1475. // operation is being executed and we still didn't show up progress form
  1476. if (ProgressData.InProgress && !FProgressForm)
  1477. {
  1478. CreateProgressForm(NULL);
  1479. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1480. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1481. }
  1482. // operation is finished (or terminated), so we hide progress form
  1483. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1484. {
  1485. DestroyProgressForm();
  1486. if (ProgressData.Operation == foCalculateSize)
  1487. {
  1488. // When calculating size before transfer, the abort caused by
  1489. // cancel flag set due to "MoveToQueue" below,
  1490. // is not propagated back to us in ExecuteFileOperation,
  1491. // as it is silently swallowed.
  1492. // So we explicitly (re)throw it here.
  1493. if (FMoveToQueue)
  1494. {
  1495. Abort();
  1496. }
  1497. }
  1498. else
  1499. {
  1500. if ((ProgressData.Cancel == csContinue) &&
  1501. !FAutoOperation)
  1502. {
  1503. OperationComplete(ProgressData.StartTime);
  1504. }
  1505. }
  1506. }
  1507. if (FProgressForm &&
  1508. // Can happen only during synchronization, when ending one of the operations
  1509. ProgressData.InProgress)
  1510. {
  1511. FProgressForm->SetProgressData(ProgressData);
  1512. DebugAssert(ProgressData.InProgress);
  1513. SetTaskbarListProgressValue(&ProgressData);
  1514. if (FProgressForm->Cancel > csContinue)
  1515. {
  1516. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1517. if (FProgressForm->Cancel == csCancelFile)
  1518. {
  1519. FProgressForm->ClearCancel();
  1520. }
  1521. // cancel cancels even the move
  1522. FMoveToQueue = false;
  1523. }
  1524. else if (FProgressForm->MoveToQueue)
  1525. {
  1526. FMoveToQueue = true;
  1527. ProgressData.SetCancelAtLeast(csCancel);
  1528. }
  1529. if ((FTransferResumeList != NULL) &&
  1530. ProgressData.InProgress &&
  1531. ProgressData.IsTransfer() &&
  1532. !ProgressData.FullFileName.IsEmpty())
  1533. {
  1534. if ((FTransferResumeList->Count == 0) ||
  1535. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1536. {
  1537. // note that we do not recognize directories from files here
  1538. FTransferResumeList->Add(ProgressData.FullFileName);
  1539. }
  1540. }
  1541. }
  1542. }
  1543. //---------------------------------------------------------------------------
  1544. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1545. const TDateTime & StartTime)
  1546. {
  1547. if (GUIConfiguration->BeepOnFinish &&
  1548. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1549. {
  1550. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1551. DWORD Sound;
  1552. if (!ExtractFileExt(BeepSound).IsEmpty())
  1553. {
  1554. Sound = SND_FILENAME;
  1555. }
  1556. else
  1557. {
  1558. Sound = SND_ALIAS;
  1559. }
  1560. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1561. }
  1562. }
  1563. //---------------------------------------------------------------------------
  1564. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1565. TFileOperationProgressType & ProgressData)
  1566. {
  1567. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1568. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1569. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1570. FileOperationProgress(ProgressData);
  1571. }
  1572. //---------------------------------------------------------------------------
  1573. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1574. {
  1575. UnicodeString Result;
  1576. if (FProgressForm != NULL)
  1577. {
  1578. Result = FProgressForm->ProgressStr();
  1579. }
  1580. return Result;
  1581. }
  1582. //---------------------------------------------------------------------------
  1583. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1584. bool DragDrop)
  1585. {
  1586. return (!DragDrop && (DropSourceControl == NULL)) ||
  1587. (DropSourceControl == DirView(osOther));
  1588. }
  1589. //---------------------------------------------------------------------------
  1590. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1591. {
  1592. TCustomDirView * ADirView = DirView(Side);
  1593. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1594. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1595. // this can happen when local drive is unplugged in the middle of the operation
  1596. if (Item != NULL)
  1597. {
  1598. if (Unselect)
  1599. {
  1600. Item->Selected = false;
  1601. }
  1602. ADirView->MakeProgressVisible(Item);
  1603. }
  1604. return Item;
  1605. }
  1606. //---------------------------------------------------------------------------
  1607. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1608. TFileOperation Operation, TOperationSide Side,
  1609. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1610. TOnceDoneOperation & OnceDoneOperation)
  1611. {
  1612. if (!FAutoOperation)
  1613. {
  1614. // no selection on "/upload", form serves only as event handler
  1615. // (it is not displayed)
  1616. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1617. ((Operation == FPrimaryOperation) ||
  1618. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1619. {
  1620. TOperationSide DViewSide;
  1621. if ((FProgressSide != osCurrent) &&
  1622. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1623. {
  1624. // assume the operation is over the focused panel
  1625. DViewSide = FProgressSide;
  1626. }
  1627. else
  1628. {
  1629. DViewSide = Side;
  1630. }
  1631. if (FOnFileOperationFinished != NULL)
  1632. {
  1633. FOnFileOperationFinished(DViewSide, FileName, Success);
  1634. }
  1635. else
  1636. {
  1637. VisualiseOperationFinished(DViewSide, FileName, Success);
  1638. }
  1639. }
  1640. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1641. {
  1642. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1643. {
  1644. if (Side == osLocal)
  1645. {
  1646. Configuration->Usage->Inc(L"UploadedFiles");
  1647. }
  1648. else
  1649. {
  1650. Configuration->Usage->Inc(L"DownloadedFiles");
  1651. }
  1652. }
  1653. }
  1654. }
  1655. if (Success && (FSynchronizeController != NULL))
  1656. {
  1657. DebugAssert(!IsSideLocalBrowser(osRemote));
  1658. if (Operation == foCopy)
  1659. {
  1660. DebugAssert(Side == osLocal);
  1661. FSynchronizeController->LogOperation(soUpload, FileName);
  1662. }
  1663. else if (Operation == foDelete)
  1664. {
  1665. DebugAssert(Side == osRemote);
  1666. FSynchronizeController->LogOperation(soDelete, FileName);
  1667. }
  1668. }
  1669. if (FProgressForm)
  1670. {
  1671. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1672. }
  1673. }
  1674. //---------------------------------------------------------------------------
  1675. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1676. TFileOperation Operation, TOperationSide Side,
  1677. bool Temp, const UnicodeString & FileName, Boolean Success,
  1678. TOnceDoneOperation & OnceDoneOperation)
  1679. {
  1680. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1681. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1682. OnceDoneOperation);
  1683. }
  1684. //---------------------------------------------------------------------------
  1685. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1686. {
  1687. return false;
  1688. }
  1689. //---------------------------------------------------------------------------
  1690. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1691. {
  1692. return false;
  1693. }
  1694. //---------------------------------------------------------------------------
  1695. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1696. {
  1697. return false;
  1698. }
  1699. //---------------------------------------------------------------------------
  1700. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1701. {
  1702. return NULL;
  1703. }
  1704. //---------------------------------------------------------------------------
  1705. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1706. {
  1707. DebugAssert(GetSide(Side) == osRemote);
  1708. DebugUsedParam(Side);
  1709. return RemoteDirView;
  1710. }
  1711. //---------------------------------------------------------------------------
  1712. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1713. {
  1714. DebugAssert(GetSide(Side) == osRemote);
  1715. DebugUsedParam(Side);
  1716. return RemoteDriveView;
  1717. }
  1718. //---------------------------------------------------------------------------
  1719. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1720. {
  1721. DebugAssert(GetSide(Side) == osRemote);
  1722. DebugUsedParam(Side);
  1723. return HasActiveTerminal();
  1724. }
  1725. //---------------------------------------------------------------------------
  1726. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1727. TOperationSide Side, int FilesOnly)
  1728. {
  1729. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1730. }
  1731. //---------------------------------------------------------------------------
  1732. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1733. TOperationSide Side, int FilesOnly)
  1734. {
  1735. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1736. }
  1737. //---------------------------------------------------------------------------
  1738. struct THistoryItemData
  1739. {
  1740. short int Side;
  1741. short int Index;
  1742. };
  1743. //---------------------------------------------------------------------------
  1744. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1745. {
  1746. DirView(Side)->HistoryGo(Index);
  1747. }
  1748. //---------------------------------------------------------------------------
  1749. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1750. {
  1751. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1752. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1753. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1754. }
  1755. //---------------------------------------------------------------------------
  1756. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1757. bool Back)
  1758. {
  1759. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1760. {
  1761. TCustomDirView * DView = DirView(Side);
  1762. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1763. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1764. if (ICount > 10)
  1765. {
  1766. ICount = 10;
  1767. }
  1768. Menu->Items->Clear();
  1769. THistoryItemData Data;
  1770. Data.Side = Side;
  1771. for (short int i = 1; i <= ICount; i++)
  1772. {
  1773. TTBCustomItem * Item = new TTBXItem(Menu);
  1774. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1775. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1776. Item->Hint = DView->HistoryPath[Data.Index];
  1777. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1778. Item->Tag = *reinterpret_cast<int*>(&Data);
  1779. Item->OnClick = HistoryItemClick;
  1780. Menu->Items->Add(Item);
  1781. }
  1782. }
  1783. }
  1784. //---------------------------------------------------------------------------
  1785. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1786. TOperationSide Side, bool Back)
  1787. {
  1788. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1789. {
  1790. // workaround
  1791. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1792. // operator new allocates memory in Pascal code, but calls inline
  1793. // contructor in C++, leading in problems, the function does
  1794. // both in Pascal code
  1795. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1796. UpdateHistoryMenu(Side, Back);
  1797. }
  1798. return FHistoryMenu[Side == osLocal][Back];
  1799. }
  1800. //---------------------------------------------------------------------------
  1801. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1802. TCustomDirView *Sender)
  1803. {
  1804. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1805. UpdateHistoryMenu(Side, true);
  1806. UpdateHistoryMenu(Side, false);
  1807. }
  1808. //---------------------------------------------------------------------------
  1809. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1810. {
  1811. // remote custom commands can be executed only if the server supports shell commands
  1812. // or have secondary shell
  1813. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1814. }
  1815. //---------------------------------------------------------------------------
  1816. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1817. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1818. {
  1819. // -1 = hidden, 0 = disabled, 1 = enabled
  1820. int Result;
  1821. TFileCustomCommand RemoteCustomCommand;
  1822. TLocalCustomCommand LocalCustomCommand;
  1823. TFileCustomCommand * NonInteractiveCustomCommand =
  1824. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1825. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1826. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1827. if (FLAGCLEAR(Command.Params, ccLocal))
  1828. {
  1829. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1830. // custom command that does not operate with files can be executed anytime ...
  1831. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1832. {
  1833. // ... but do not show such command in remote file menu
  1834. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1835. {
  1836. Result = AllowedState;
  1837. }
  1838. else
  1839. {
  1840. Result = -1;
  1841. }
  1842. }
  1843. else
  1844. {
  1845. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1846. {
  1847. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1848. }
  1849. else
  1850. {
  1851. Result = -1;
  1852. }
  1853. }
  1854. }
  1855. else
  1856. {
  1857. // custom command that does not operate with files can be executed anytime
  1858. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1859. {
  1860. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1861. }
  1862. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1863. {
  1864. if ((ListType == ccltAll) || (ListType == ccltFile))
  1865. {
  1866. // special case is "diff"-style command that can be executed over any side,
  1867. // if we have both sides
  1868. Result =
  1869. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1870. // directly (instead of EnableSelectedOperation) to pass
  1871. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1872. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1873. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1874. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1875. }
  1876. else if (ListType == ccltBoth)
  1877. {
  1878. DebugAssert(!IsLocalBrowserMode());
  1879. Result = 1;
  1880. }
  1881. else
  1882. {
  1883. Result = -1;
  1884. }
  1885. }
  1886. else
  1887. {
  1888. if ((ListType == ccltAll) ||
  1889. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1890. {
  1891. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1892. }
  1893. else
  1894. {
  1895. Result = -1;
  1896. }
  1897. }
  1898. if ((Result > 0) &&
  1899. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1900. !HasActiveTerminal())
  1901. {
  1902. Result = 0;
  1903. }
  1904. }
  1905. return Result;
  1906. }
  1907. //---------------------------------------------------------------------------
  1908. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1909. TStrings * FileList, const TCustomCommandType & ACommand,
  1910. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1911. {
  1912. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1913. {
  1914. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1915. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1916. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1917. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1918. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1919. bool Capture =
  1920. FLAGSET(ACommand.Params, ccShowResults) ||
  1921. FLAGSET(ACommand.Params, ccCopyResults) ||
  1922. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1923. TCaptureOutputEvent OutputEvent = NULL;
  1924. DebugAssert(FCapturedLog == NULL);
  1925. if (Capture)
  1926. {
  1927. FCapturedLog = new TStringList();
  1928. OutputEvent = TerminalCaptureLog;
  1929. }
  1930. try
  1931. {
  1932. if (!RemoteCustomCommand.IsFileCommand(Command))
  1933. {
  1934. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1935. OutputEvent);
  1936. }
  1937. else
  1938. {
  1939. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1940. }
  1941. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1942. {
  1943. if (FLAGSET(ACommand.Params, ccCopyResults))
  1944. {
  1945. CopyToClipboard(FCapturedLog);
  1946. }
  1947. if (FLAGSET(ACommand.Params, ccShowResults))
  1948. {
  1949. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1950. }
  1951. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1952. {
  1953. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1954. }
  1955. }
  1956. }
  1957. __finally
  1958. {
  1959. SAFE_DESTROY(FCapturedLog);
  1960. }
  1961. }
  1962. }
  1963. //---------------------------------------------------------------------------
  1964. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1965. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1966. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1967. {
  1968. DebugAssert(!IsLocalBrowserMode());
  1969. TStrings * LocalFileList = NULL;
  1970. TStrings * RemoteFileList = NULL;
  1971. TStrings * RemoteFileListFull = NULL;
  1972. try
  1973. {
  1974. if (LocalFileCommand)
  1975. {
  1976. if (ALocalFileList == NULL)
  1977. {
  1978. DebugAssert(HasDirView[osLocal]);
  1979. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1980. // directly (instead of EnableSelectedOperation) to pass
  1981. // false to FocusedFileOnlyWhenFocused
  1982. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1983. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1984. }
  1985. else
  1986. {
  1987. LocalFileList = ALocalFileList;
  1988. }
  1989. if (FileListCommand)
  1990. {
  1991. if (LocalFileList->Count != 1)
  1992. {
  1993. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1994. }
  1995. }
  1996. else
  1997. {
  1998. if ((LocalFileList->Count != 1) &&
  1999. (FileList->Count != 1) &&
  2000. (LocalFileList->Count != FileList->Count))
  2001. {
  2002. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  2003. }
  2004. }
  2005. }
  2006. UnicodeString RootTempDir;
  2007. UnicodeString TempDir;
  2008. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  2009. if (!RemoteFiles)
  2010. {
  2011. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true, true);
  2012. }
  2013. try
  2014. {
  2015. TDateTimes RemoteFileTimes;
  2016. if (RemoteFiles)
  2017. {
  2018. RemoteFileList = FileList;
  2019. }
  2020. else
  2021. {
  2022. RemoteFileList = new TStringList();
  2023. TMakeLocalFileListParams MakeFileListParam;
  2024. MakeFileListParam.FileList = RemoteFileList;
  2025. MakeFileListParam.FileTimes = &RemoteFileTimes;
  2026. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2027. MakeFileListParam.Recursive =
  2028. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  2029. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2030. if (!MakeFileListParam.Recursive)
  2031. {
  2032. RemoteFileTimes.clear();
  2033. RemoteFileListFull = new TStringList();
  2034. MakeFileListParam.FileList = RemoteFileListFull;
  2035. MakeFileListParam.Recursive = true;
  2036. MakeFileListParam.IncludeDirs = false;
  2037. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2038. }
  2039. else
  2040. {
  2041. RemoteFileListFull = RemoteFileList;
  2042. }
  2043. }
  2044. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  2045. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2046. if (!NonBlocking)
  2047. {
  2048. int Count;
  2049. if (FileListCommand)
  2050. {
  2051. Count = 1;
  2052. }
  2053. else if (LocalFileCommand)
  2054. {
  2055. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  2056. }
  2057. else
  2058. {
  2059. Count = RemoteFileList->Count;
  2060. }
  2061. Progress.Start(foCustomCommand, osRemote, Count);
  2062. DebugAssert(FProgressForm != NULL);
  2063. FProgressForm->ReadOnly = true;
  2064. }
  2065. try
  2066. {
  2067. if (FileListCommand)
  2068. {
  2069. UnicodeString LocalFile;
  2070. // MakeFileList does not delimit filenames
  2071. UnicodeString FileList = MakeFileList(RemoteFileList);
  2072. if (LocalFileCommand)
  2073. {
  2074. DebugAssert(LocalFileList->Count == 1);
  2075. LocalFile = LocalFileList->Strings[0];
  2076. }
  2077. TLocalCustomCommand CustomCommand(Data,
  2078. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  2079. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  2080. if (NonBlocking)
  2081. {
  2082. DebugAssert(!POutput);
  2083. ExecuteShellChecked(ShellCommand);
  2084. }
  2085. else
  2086. {
  2087. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2088. }
  2089. }
  2090. else if (LocalFileCommand)
  2091. {
  2092. if (LocalFileList->Count == 1)
  2093. {
  2094. UnicodeString LocalFile = LocalFileList->Strings[0];
  2095. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2096. {
  2097. UnicodeString FileName = RemoteFileList->Strings[Index];
  2098. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2099. Progress.SetFile(FileNameForProgress);
  2100. TLocalCustomCommand CustomCommand(Data,
  2101. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2102. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2103. TOnceDoneOperation OnceDoneOperation;
  2104. // Do not unselect anything, as it won't work with two panels anyway
  2105. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2106. }
  2107. }
  2108. else if (RemoteFileList->Count == 1)
  2109. {
  2110. UnicodeString FileName = RemoteFileList->Strings[0];
  2111. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2112. {
  2113. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2114. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2115. Progress.SetFile(LocalFileNameOnly);
  2116. TLocalCustomCommand CustomCommand(
  2117. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2118. FileName, LocalFileName, L"");
  2119. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2120. TOnceDoneOperation OnceDoneOperation;
  2121. // Do not unselect anything, as it won't work with two panels anyway
  2122. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2123. }
  2124. }
  2125. else
  2126. {
  2127. if (LocalFileList->Count != RemoteFileList->Count)
  2128. {
  2129. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2130. }
  2131. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2132. {
  2133. UnicodeString FileName = RemoteFileList->Strings[Index];
  2134. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2135. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2136. Progress.SetFile(LocalFileNameOnly); // sic
  2137. TLocalCustomCommand CustomCommand(
  2138. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2139. FileName, LocalFileName, L"");
  2140. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2141. TOnceDoneOperation OnceDoneOperation;
  2142. // Do not unselect anything, as it won't work with two panels anyway
  2143. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2144. }
  2145. }
  2146. }
  2147. else
  2148. {
  2149. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2150. {
  2151. UnicodeString FileName = RemoteFileList->Strings[Index];
  2152. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2153. Progress.SetFile(FileNameForProgress);
  2154. TLocalCustomCommand CustomCommand(Data,
  2155. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2156. FileName, L"", L"");
  2157. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2158. TOnceDoneOperation OnceDoneOperation;
  2159. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2160. }
  2161. }
  2162. }
  2163. __finally
  2164. {
  2165. if (!NonBlocking)
  2166. {
  2167. Progress.Stop();
  2168. }
  2169. }
  2170. DebugAssert(!FAutoOperation);
  2171. if (!RemoteFiles)
  2172. {
  2173. TempDir = IncludeTrailingBackslash(TempDir);
  2174. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2175. {
  2176. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2177. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2178. // Skip directories, process only nested files.
  2179. // The check is redundant as FileAge fails for directories anyway.
  2180. !DirectoryExists(FileName))
  2181. {
  2182. UnicodeString RemoteDir =
  2183. UnixExtractFileDir(
  2184. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2185. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2186. TDateTime NewTime;
  2187. if (FileAge(FileName, NewTime) &&
  2188. (NewTime != RemoteFileTimes[Index]))
  2189. {
  2190. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2191. TemporaryFileCopyParam(CopyParam);
  2192. CopyParam.FileMask = L"";
  2193. FAutoOperation = true;
  2194. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2195. TemporaryFilesList->Add(FileName);
  2196. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2197. }
  2198. }
  2199. }
  2200. }
  2201. }
  2202. __finally
  2203. {
  2204. FAutoOperation = false;
  2205. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2206. {
  2207. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2208. }
  2209. }
  2210. }
  2211. __finally
  2212. {
  2213. if (RemoteFileList != FileList)
  2214. {
  2215. delete RemoteFileList;
  2216. }
  2217. if (LocalFileList != ALocalFileList)
  2218. {
  2219. delete LocalFileList;
  2220. }
  2221. if (RemoteFileListFull != RemoteFileList)
  2222. {
  2223. delete RemoteFileListFull;
  2224. }
  2225. }
  2226. }
  2227. //---------------------------------------------------------------------------
  2228. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2229. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2230. bool FileListCommand, UnicodeString * POutput)
  2231. {
  2232. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2233. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2234. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2235. {
  2236. UnicodeString FileName = SelectedFileList->Strings[Index];
  2237. if (DirectoryExists(FileName))
  2238. {
  2239. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2240. {
  2241. LocalFileList->Add(FileName);
  2242. }
  2243. if (FLAGSET(ACommand.Params, ccRecursive))
  2244. {
  2245. TMakeLocalFileListParams MakeFileListParam;
  2246. MakeFileListParam.FileList = LocalFileList.get();
  2247. MakeFileListParam.FileTimes = NULL;
  2248. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2249. MakeFileListParam.Recursive = true;
  2250. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2251. }
  2252. }
  2253. else
  2254. {
  2255. LocalFileList->Add(FileName);
  2256. }
  2257. }
  2258. UnicodeString RemotePath;
  2259. if (Terminal != NULL)
  2260. {
  2261. RemotePath = Terminal->CurrentDirectory;
  2262. }
  2263. if (FileListCommand)
  2264. {
  2265. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2266. TLocalCustomCommand CustomCommand(
  2267. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2268. L"", L"", FileList);
  2269. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2270. }
  2271. else
  2272. {
  2273. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2274. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2275. DebugAssert(FProgressForm != NULL);
  2276. FProgressForm->ReadOnly = true;
  2277. try
  2278. {
  2279. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2280. {
  2281. UnicodeString FileName = LocalFileList->Strings[Index];
  2282. Progress.SetFile(FileName);
  2283. TLocalCustomCommand CustomCommand(
  2284. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2285. FileName, L"", L"");
  2286. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2287. TOnceDoneOperation OnceDoneOperation;
  2288. Progress.Finish(FileName, true, OnceDoneOperation);
  2289. }
  2290. }
  2291. __finally
  2292. {
  2293. Progress.Stop();
  2294. }
  2295. }
  2296. }
  2297. //---------------------------------------------------------------------------
  2298. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2299. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2300. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2301. {
  2302. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2303. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2304. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2305. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2306. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2307. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2308. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2309. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2310. std::unique_ptr<UnicodeString> POutput;
  2311. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2312. FLAGSET(ACommand.Params, ccCopyResults))
  2313. {
  2314. POutput.reset(new UnicodeString());
  2315. }
  2316. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2317. FProgressSide = FCurrentSide;
  2318. if (!LocalCustomCommand.IsFileCommand(Command))
  2319. {
  2320. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2321. }
  2322. // remote files?
  2323. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2324. {
  2325. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2326. }
  2327. // local files
  2328. else
  2329. {
  2330. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2331. }
  2332. if (POutput.get() != NULL)
  2333. {
  2334. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2335. int P = POutput->Pos(sLineBreak);
  2336. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2337. {
  2338. POutput->SetLength(P - 1);
  2339. }
  2340. // Copy even empty output to clipboard
  2341. if (FLAGSET(ACommand.Params, ccCopyResults))
  2342. {
  2343. CopyToClipboard(*POutput);
  2344. }
  2345. // But do not show an empty message box.
  2346. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2347. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2348. !POutput->IsEmpty())
  2349. {
  2350. TClipboardHandler ClipboardHandler;
  2351. ClipboardHandler.Text = *POutput;
  2352. TMessageParams Params;
  2353. TQueryButtonAlias Aliases[1];
  2354. Aliases[0].Button = qaRetry;
  2355. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2356. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2357. Params.Aliases = Aliases;
  2358. Params.AliasesCount = LENOF(Aliases);
  2359. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2360. }
  2361. }
  2362. }
  2363. //---------------------------------------------------------------------------
  2364. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2365. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2366. {
  2367. TCustomCommandData Data;
  2368. UnicodeString Site;
  2369. UnicodeString RemotePath;
  2370. if (HasActiveTerminal())
  2371. {
  2372. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2373. Data = TCustomCommandData(SessionData.get());
  2374. Site = Terminal->SessionData->SessionKey;
  2375. RemotePath = Terminal->CurrentDirectory;
  2376. }
  2377. UnicodeString HelpKeyword = ACommand.HomePage;
  2378. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2379. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2380. {
  2381. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2382. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2383. {
  2384. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2385. }
  2386. else
  2387. {
  2388. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2389. }
  2390. if (!DoCustomCommandOptionsDialog(
  2391. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2392. {
  2393. Abort();
  2394. }
  2395. }
  2396. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2397. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2398. {
  2399. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2400. }
  2401. else
  2402. {
  2403. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2404. }
  2405. }
  2406. //---------------------------------------------------------------------------
  2407. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2408. const TCustomCommandType & Command)
  2409. {
  2410. DebugAssert(FCustomCommandLocalFileList != NULL);
  2411. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2412. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2413. TStrings * LocalFileList = new TStringList();
  2414. TStrings * RemoteFileList = new TStringList();
  2415. try
  2416. {
  2417. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2418. {
  2419. LocalFileList->Clear();
  2420. LocalFileList->AddObject(
  2421. FCustomCommandLocalFileList->Strings[Index],
  2422. FCustomCommandLocalFileList->Objects[Index]);
  2423. RemoteFileList->Clear();
  2424. RemoteFileList->AddObject(
  2425. FCustomCommandRemoteFileList->Strings[Index],
  2426. FCustomCommandRemoteFileList->Objects[Index]);
  2427. CustomCommand(RemoteFileList, Command, LocalFileList);
  2428. }
  2429. }
  2430. __finally
  2431. {
  2432. delete LocalFileList;
  2433. delete RemoteFileList;
  2434. }
  2435. }
  2436. //---------------------------------------------------------------------------
  2437. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2438. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2439. {
  2440. delete FCustomCommandLocalFileList;
  2441. delete FCustomCommandRemoteFileList;
  2442. // takeover ownership,
  2443. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2444. FCustomCommandLocalFileList = LocalFileList;
  2445. FCustomCommandRemoteFileList = RemoteFileList;
  2446. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2447. if (Button != NULL)
  2448. {
  2449. FCustomCommandMenu->Items->Clear();
  2450. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2451. MenuPopup(FCustomCommandMenu, Button);
  2452. }
  2453. else
  2454. {
  2455. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2456. }
  2457. }
  2458. //---------------------------------------------------------------------------
  2459. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2460. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2461. {
  2462. DebugAssert(FCapturedLog != NULL);
  2463. if ((OutputType == cotOutput) || (OutputType == cotError))
  2464. {
  2465. FCapturedLog->Add(AddedLine);
  2466. }
  2467. }
  2468. //---------------------------------------------------------------------------
  2469. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2470. TOperationSide Side)
  2471. {
  2472. return
  2473. (Side == osOther) &&
  2474. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2475. }
  2476. //---------------------------------------------------------------------------
  2477. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2478. TOperationSide Side, TTBXCustomItem * Item,
  2479. TResolvedDoubleClickAction DoubleClickAction1, TResolvedDoubleClickAction DoubleClickAction2)
  2480. {
  2481. TCustomDirView * DView = DirView(Side);
  2482. bool IsDefault = false;
  2483. if (DView->ItemFocused != NULL)
  2484. {
  2485. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  2486. TResolvedDoubleClickAction DefaultAction =
  2487. WinConfiguration->ResolveDoubleClickAction(DView->ItemIsDirectory(DView->ItemFocused), ATerminal);
  2488. IsDefault = (DefaultAction == DoubleClickAction1) || (DefaultAction == DoubleClickAction2);
  2489. }
  2490. TTBItemOptions O = Item->Options;
  2491. if (IsDefault)
  2492. {
  2493. Item->Options = O << tboDefault;
  2494. }
  2495. else
  2496. {
  2497. Item->Options = O >> tboDefault;
  2498. }
  2499. }
  2500. //---------------------------------------------------------------------------
  2501. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2502. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2503. {
  2504. TCustomDirView * DView = DirView(Side);
  2505. TListItem * Item = DView->ItemFocused;
  2506. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2507. EnableFocusedOperation[Side])
  2508. {
  2509. TPoint ClientPoint;
  2510. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2511. TPoint(0, 0) : MousePos;
  2512. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2513. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2514. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2515. }
  2516. }
  2517. //---------------------------------------------------------------------------
  2518. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2519. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2520. {
  2521. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, rdcaChangeDir, rdcaOpen);
  2522. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, rdcaEdit, rdcaNone);
  2523. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, rdcaCopy, rdcaNone);
  2524. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2525. Handled = true;
  2526. }
  2527. //---------------------------------------------------------------------------
  2528. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2529. {
  2530. }
  2531. //---------------------------------------------------------------------------
  2532. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2533. {
  2534. DebugAssert(FErrorList == NULL);
  2535. if (WinConfiguration->ContinueOnError)
  2536. {
  2537. FErrorList = new TStringList();
  2538. Configuration->Usage->Inc(L"ContinuationsOnError");
  2539. }
  2540. NonVisualDataModule->StartBusy();
  2541. }
  2542. //---------------------------------------------------------------------------
  2543. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2544. {
  2545. NonVisualDataModule->EndBusy();
  2546. if (FErrorList)
  2547. {
  2548. HandleErrorList(FErrorList);
  2549. }
  2550. }
  2551. //---------------------------------------------------------------------------
  2552. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2553. const TCopyParamType & CopyParam)
  2554. {
  2555. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2556. {
  2557. Configuration->Usage->Inc(L"FileMaskUses");
  2558. }
  2559. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2560. {
  2561. Configuration->Usage->Inc(L"OperationMaskUses");
  2562. }
  2563. }
  2564. //---------------------------------------------------------------------------
  2565. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2566. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2567. {
  2568. DebugAssert(!IsLocalBrowserMode());
  2569. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2570. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2571. TTransferOperationParam DefaultParam;
  2572. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2573. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2574. switch (Param.Queue)
  2575. {
  2576. case asOn:
  2577. CopyParam.Queue = true;
  2578. break;
  2579. case asOff:
  2580. CopyParam.Queue = false;
  2581. break;
  2582. case asAuto:
  2583. default:
  2584. // keep default
  2585. break;
  2586. }
  2587. bool Result =
  2588. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2589. if (Result)
  2590. {
  2591. DebugAssert(Terminal);
  2592. bool SelectionRestored = false;
  2593. TCustomDirView * DView = NULL;
  2594. if (HasDirView[Side])
  2595. {
  2596. DView = DirView(Side);
  2597. DView->SaveSelection();
  2598. DView->SaveSelectedNames();
  2599. }
  2600. UpdateCopyParamCounters(CopyParam);
  2601. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2602. DebugAssert(FTransferResumeList == NULL);
  2603. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2604. FMoveToQueue = false;
  2605. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2606. try
  2607. {
  2608. TStrings * PermanentFileList;
  2609. std::unique_ptr<TStrings> PermanentFileListOwner;
  2610. try
  2611. {
  2612. if (Side == osLocal)
  2613. {
  2614. PermanentFileList = FileList;
  2615. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2616. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2617. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2618. if (Operation == foMove)
  2619. {
  2620. ReloadLocalDirectory();
  2621. if (DView != NULL)
  2622. {
  2623. DView->RestoreSelection();
  2624. }
  2625. SelectionRestored = true;
  2626. }
  2627. }
  2628. else
  2629. {
  2630. // Clone the file list as it may refer to current directory files,
  2631. // which get destroyed, when the source directory is reloaded after foMove operation.
  2632. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2633. // But for a hotfix, we are not going to do such a big change.
  2634. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2635. PermanentFileList = PermanentFileListOwner.get();
  2636. try
  2637. {
  2638. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2639. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2640. }
  2641. __finally
  2642. {
  2643. if (Operation == foMove)
  2644. {
  2645. if (DView != NULL)
  2646. {
  2647. DView->RestoreSelection();
  2648. }
  2649. SelectionRestored = true;
  2650. }
  2651. ReloadLocalDirectory(Param.TargetDirectory);
  2652. }
  2653. }
  2654. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2655. if (FMoveToQueue)
  2656. {
  2657. Abort();
  2658. }
  2659. }
  2660. catch (EAbort &)
  2661. {
  2662. if (FMoveToQueue)
  2663. {
  2664. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2665. DebugAssert(CopyParam.TransferSkipList == NULL);
  2666. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2667. if (TransferResumeList->Count > 0)
  2668. {
  2669. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2670. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2671. }
  2672. CopyParam.TransferSkipList = TransferResumeList.release();
  2673. // not really needed, just to keep it consistent with TransferResumeList
  2674. FTransferResumeList = NULL;
  2675. FMoveToQueue = false;
  2676. Configuration->Usage->Inc("MovesToBackground");
  2677. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2678. ClearTransferSourceSelection(Direction);
  2679. }
  2680. throw;
  2681. }
  2682. }
  2683. __finally
  2684. {
  2685. if (!SelectionRestored && (DView != NULL))
  2686. {
  2687. DView->DiscardSavedSelection();
  2688. }
  2689. FTransferResumeList = NULL;
  2690. }
  2691. }
  2692. return Result;
  2693. }
  2694. //---------------------------------------------------------------------------
  2695. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2696. TOperationSide Side, TStrings * FileList, void * Param)
  2697. {
  2698. DebugAssert(FileList->Count);
  2699. // We deliberately do not toggle alternative flag (Param), but use OR,
  2700. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2701. // shortcut of CurrentDeleteAlternativeAction
  2702. bool Alternative =
  2703. bool(Param) || UseAlternativeFunction();
  2704. bool Recycle;
  2705. if (IsSideLocalBrowser(Side))
  2706. {
  2707. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2708. }
  2709. else
  2710. {
  2711. Recycle =
  2712. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2713. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2714. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2715. }
  2716. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2717. if (!Result)
  2718. {
  2719. UnicodeString Query;
  2720. if (FileList->Count == 1)
  2721. {
  2722. if (IsSideLocalBrowser(Side))
  2723. {
  2724. Query = ExtractFileName(FileList->Strings[0]);
  2725. }
  2726. else
  2727. {
  2728. Query = UnixExtractFileName(FileList->Strings[0]);
  2729. }
  2730. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2731. }
  2732. else
  2733. {
  2734. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2735. }
  2736. TMessageParams Params(mpNeverAskAgainCheck);
  2737. Params.ImageName = L"Delete file";
  2738. unsigned int Answer =
  2739. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2740. if (Answer == qaNeverAskAgain)
  2741. {
  2742. Result = true;
  2743. if (Recycle)
  2744. {
  2745. WinConfiguration->ConfirmRecycling = false;
  2746. }
  2747. else
  2748. {
  2749. WinConfiguration->ConfirmDeleting = false;
  2750. }
  2751. }
  2752. else
  2753. {
  2754. Result = (Answer == qaOK);
  2755. }
  2756. }
  2757. if (Result)
  2758. {
  2759. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2760. }
  2761. return Result;
  2762. }
  2763. //---------------------------------------------------------------------------
  2764. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2765. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2766. {
  2767. TAutoBatch AutoBatch(this);
  2768. bool Result;
  2769. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2770. {
  2771. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2772. }
  2773. else if (Operation == foRename)
  2774. {
  2775. DebugAssert(DirView(Side)->ItemFocused);
  2776. DirView(Side)->ItemFocused->EditCaption();
  2777. Result = true;
  2778. }
  2779. else if (Operation == foDelete)
  2780. {
  2781. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2782. }
  2783. else if (Operation == foSetProperties)
  2784. {
  2785. DirView(osRemote)->SaveSelectedNames();
  2786. Result = SetProperties(Side, FileList);
  2787. }
  2788. else if (Operation == foCustomCommand)
  2789. {
  2790. DebugAssert(Param);
  2791. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2792. DirView(osRemote)->SaveSelectedNames();
  2793. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2794. CustomCommand(FileList, *Command, NULL);
  2795. Result = true;
  2796. }
  2797. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2798. {
  2799. DebugAssert(!IsSideLocalBrowser(Side));
  2800. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2801. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2802. }
  2803. else if (Operation == foLock)
  2804. {
  2805. DebugAssert(!IsSideLocalBrowser(Side));
  2806. LockFiles(FileList, true);
  2807. Result = true;
  2808. }
  2809. else if (Operation == foUnlock)
  2810. {
  2811. DebugAssert(!IsSideLocalBrowser(Side));
  2812. LockFiles(FileList, false);
  2813. Result = true;
  2814. }
  2815. else
  2816. {
  2817. DebugFail();
  2818. }
  2819. return Result;
  2820. }
  2821. //---------------------------------------------------------------------------
  2822. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2823. {
  2824. if (Side == osCurrent)
  2825. {
  2826. Side = FCurrentSide;
  2827. }
  2828. return Side;
  2829. }
  2830. //---------------------------------------------------------------------------
  2831. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2832. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2833. {
  2834. Side = GetSide(Side);
  2835. bool FullPath = IsSideLocalBrowser(Side);
  2836. TCustomDirView * ADirView = DirView(Side);
  2837. std::unique_ptr<TStringList> FileList(new TStringList());
  2838. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2839. bool ReloadProperties =
  2840. !IsSideLocalBrowser(Side) && (Operation == foSetProperties) &&
  2841. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2842. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2843. if (Result && ReloadProperties)
  2844. {
  2845. std::unique_ptr<TStringList> FileList2(new TStringList());
  2846. FileList->CaseSensitive = true;
  2847. FileList->Sort();
  2848. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2849. {
  2850. TListItem * Item = ADirView->Items->Item[Index];
  2851. UnicodeString FileName = ADirView->ItemFileName(Item);
  2852. int Index2;
  2853. if (FileList->Find(FileName, Index2))
  2854. {
  2855. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2856. }
  2857. }
  2858. LoadFilesProperties(FileList2.get());
  2859. }
  2860. return Result;
  2861. }
  2862. //---------------------------------------------------------------------------
  2863. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2864. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2865. bool NoConfirmation, void * Param)
  2866. {
  2867. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2868. {
  2869. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2870. {
  2871. DebugAssert(!IsLocalBrowserMode());
  2872. if (GetSide(Side) == osLocal)
  2873. {
  2874. Configuration->Usage->Inc(L"UploadsCommand");
  2875. }
  2876. else
  2877. {
  2878. Configuration->Usage->Inc(L"DownloadsCommand");
  2879. }
  2880. }
  2881. }
  2882. }
  2883. //---------------------------------------------------------------------------
  2884. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2885. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2886. {
  2887. TTransferOperationParam Param;
  2888. if ((WinConfiguration->Interface != ifCommander) ||
  2889. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2890. {
  2891. Flags &= ~cocShortCutHint;
  2892. }
  2893. TCustomDirView * DView = DirView(Side);
  2894. Param.Options =
  2895. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2896. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2897. if (FLAGSET(Flags, cocQueue))
  2898. {
  2899. Param.Queue = asOn;
  2900. }
  2901. else if (FLAGSET(Flags, cocNonQueue))
  2902. {
  2903. Param.Queue = asOff;
  2904. }
  2905. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2906. }
  2907. //---------------------------------------------------------------------------
  2908. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2909. {
  2910. int Count = ErrorList->Count;
  2911. try
  2912. {
  2913. if (ErrorList->Count)
  2914. {
  2915. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2916. if (MessageDialog(Message, qtError,
  2917. qaOK | qaCancel, HELP_NONE) == qaOK)
  2918. {
  2919. unsigned int Answer;
  2920. int Index = 0;
  2921. do
  2922. {
  2923. DebugAssert(Index >= 0 && Index < Count);
  2924. TQueryButtonAlias Aliases[2];
  2925. Aliases[0].Button = qaYes;
  2926. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2927. Aliases[1].Button = qaNo;
  2928. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2929. TMessageParams Params;
  2930. Params.Aliases = Aliases;
  2931. Params.AliasesCount = LENOF(Aliases);
  2932. int No = Index + 1;
  2933. UnicodeString Message = ErrorList->Strings[Index];
  2934. UnicodeString AMainInstructions;
  2935. UnicodeString Details;
  2936. if (ExtractMainInstructions(Message, AMainInstructions))
  2937. {
  2938. Details = Message;
  2939. Message = AMainInstructions;
  2940. }
  2941. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2942. Answer = MoreMessageDialog(
  2943. Message,
  2944. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2945. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2946. qaOK, HELP_NONE, &Params);
  2947. if (Answer == qaNo)
  2948. {
  2949. Index++;
  2950. }
  2951. if (Answer == qaYes)
  2952. {
  2953. Index--;
  2954. }
  2955. }
  2956. while (Answer != qaOK);
  2957. }
  2958. }
  2959. }
  2960. __finally
  2961. {
  2962. TStrings * List = ErrorList;
  2963. ErrorList = NULL;
  2964. for (int i = 0; i < Count; i++)
  2965. {
  2966. delete List->Objects[i];
  2967. }
  2968. delete List;
  2969. }
  2970. }
  2971. //---------------------------------------------------------------------------
  2972. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2973. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2974. {
  2975. // needed for checking filemasks, as there's no directory object
  2976. // associated with the file object
  2977. File->FullFileName = FullFileName;
  2978. TFileMasks::TParams MaskParams;
  2979. MaskParams.Size = File->Size;
  2980. MaskParams.Modification = File->Modification;
  2981. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2982. }
  2983. //---------------------------------------------------------------------------
  2984. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2985. {
  2986. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2987. Side = GetSide(Side);
  2988. TCustomDirView * DView = DirView(Side);
  2989. TListItem * FocusedItem = DView->ItemFocused;
  2990. UnicodeString Name;
  2991. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2992. {
  2993. Name = DView->ItemFileName(FocusedItem);
  2994. }
  2995. else
  2996. {
  2997. Name = LoadStr(NEW_FILE);
  2998. }
  2999. UnicodeString Names = Name;
  3000. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  3001. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  3002. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  3003. {
  3004. while (!Names.IsEmpty())
  3005. {
  3006. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  3007. CustomWinConfiguration->History[L"EditFile"] = History.get();
  3008. UnicodeString TargetFileName;
  3009. UnicodeString LocalFileName;
  3010. UnicodeString RootTempDir;
  3011. UnicodeString TempDir;
  3012. UnicodeString RemoteDirectory;
  3013. bool ExistingFile = false;
  3014. if (!IsSideLocalBrowser(Side))
  3015. {
  3016. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  3017. TRemoteFile * File = NULL;
  3018. if (Terminal->FileExists(Name, &File) &&
  3019. !File->IsDirectory)
  3020. {
  3021. try
  3022. {
  3023. ExecuteRemoteFile(Name, File, efDefaultEditor);
  3024. ExistingFile = true;
  3025. }
  3026. __finally
  3027. {
  3028. delete File;
  3029. }
  3030. }
  3031. if (!ExistingFile)
  3032. {
  3033. RemoteDirectory = UnixExtractFilePath(Name);
  3034. TemporaryDirectoryForRemoteFiles(
  3035. RemoteDirectory, GUIConfiguration->CurrentCopyParam, false, TempDir, RootTempDir);
  3036. TargetFileName = UnixExtractFileName(Name);
  3037. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3038. LocalFileName = TempDir +
  3039. // We probably do not want to trim the VMS version here
  3040. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  3041. }
  3042. }
  3043. else
  3044. {
  3045. if (ExtractFilePath(Name).IsEmpty())
  3046. {
  3047. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  3048. }
  3049. else
  3050. {
  3051. LocalFileName = ExpandFileName(Name);
  3052. }
  3053. TargetFileName = ExtractFileName(Name);
  3054. }
  3055. if (!ExistingFile)
  3056. {
  3057. bool NewFile = !FileExists(ApiPath(LocalFileName));
  3058. if (NewFile)
  3059. {
  3060. int File = FileCreate(ApiPath(LocalFileName));
  3061. if (File < 0)
  3062. {
  3063. if (!RootTempDir.IsEmpty())
  3064. {
  3065. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3066. }
  3067. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  3068. }
  3069. else
  3070. {
  3071. FileClose(File);
  3072. }
  3073. }
  3074. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  3075. const TEditorData * ExternalEditor = NULL;
  3076. TFileMasks::TParams MaskParams; // size not known
  3077. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  3078. false, MaskParams);
  3079. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  3080. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  3081. }
  3082. }
  3083. }
  3084. }
  3085. //---------------------------------------------------------------------------
  3086. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  3087. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  3088. {
  3089. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3090. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3091. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3092. return
  3093. ((ExecuteFileBy == efInternalEditor)) ||
  3094. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  3095. }
  3096. //---------------------------------------------------------------------------
  3097. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  3098. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  3099. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  3100. UnicodeString RemoteDirectory, bool NewFile)
  3101. {
  3102. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3103. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3104. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3105. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3106. Side = GetSide(Side);
  3107. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3108. if (!IsSideLocalBrowser(Side))
  3109. {
  3110. Data->Terminal = Terminal;
  3111. Data->Queue = Queue;
  3112. Data->SessionData = CloneCurrentSessionData();
  3113. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3114. Data->RemoteDirectory = RemoteDirectory;
  3115. Data->SessionName = Terminal->SessionData->SessionName;
  3116. Data->LocalRootDirectory = LocalRootDirectory;
  3117. Data->OriginalFileName = OriginalFileName;
  3118. Data->Command = L""; // will be changed later for external editor
  3119. }
  3120. if (ExecuteFileBy == efInternalEditor)
  3121. {
  3122. if (!IsSideLocalBrowser(Side))
  3123. {
  3124. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3125. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3126. TForm * Editor;
  3127. try
  3128. {
  3129. Editor =
  3130. ShowEditorForm(
  3131. FileName, this,
  3132. FEditorManager->FileChanged, FEditorManager->FileReload, FEditorManager->FileClosed,
  3133. SaveAllInternalEditors, AnyInternalEditorModified,
  3134. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3135. }
  3136. catch(...)
  3137. {
  3138. if (!LocalRootDirectory.IsEmpty())
  3139. {
  3140. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  3141. }
  3142. throw;
  3143. }
  3144. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3145. AppLogFmt(L"Opened internal editor with \"%s\"", (FileName));
  3146. }
  3147. else
  3148. {
  3149. DebugAssert(!StandaloneOperation);
  3150. TForm * Editor =
  3151. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3152. SaveAllInternalEditors, AnyInternalEditorModified,
  3153. L"", false, SessionColor, -1, NewFile);
  3154. FLocalEditors->Add(Editor);
  3155. }
  3156. }
  3157. else
  3158. {
  3159. HANDLE Process;
  3160. if (ExecuteFileBy == efExternalEditor)
  3161. {
  3162. UnicodeString Program, Params, Dir;
  3163. Data->Command = ExternalEditor->ExternalEditor;
  3164. ReformatFileNameCommand(Data->Command);
  3165. SplitCommand(Data->Command, Program, Params, Dir);
  3166. Params = ExpandFileNameCommand(Params, FileName);
  3167. Program = ExpandEnvironmentVariables(Program);
  3168. if (!ExecuteShell(Program, Params, Process))
  3169. {
  3170. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3171. }
  3172. }
  3173. else
  3174. {
  3175. DebugAssert(!IsSideLocalBrowser(Side));
  3176. if (!ExecuteShell(FileName, L"", Process))
  3177. {
  3178. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3179. }
  3180. }
  3181. if (IsSideLocalBrowser(Side) ||
  3182. ((ExecuteFileBy == efShell) &&
  3183. !WinConfiguration->Editor.SDIShellEditor) ||
  3184. ((ExecuteFileBy == efExternalEditor) &&
  3185. !ExternalEditor->SDIExternalEditor))
  3186. {
  3187. // no need for handle
  3188. if (Process != NULL)
  3189. {
  3190. DebugCheck(CloseHandle(Process));
  3191. }
  3192. Process = INVALID_HANDLE_VALUE;
  3193. }
  3194. else
  3195. {
  3196. if (Process == NULL)
  3197. {
  3198. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3199. }
  3200. }
  3201. if (!IsSideLocalBrowser(Side))
  3202. {
  3203. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3204. AppLogFmt(L"Opened external editor with remote file \"%s\".", (FileName));
  3205. }
  3206. }
  3207. }
  3208. //---------------------------------------------------------------------------
  3209. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3210. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3211. void * /*Arg*/)
  3212. {
  3213. if (Token != NULL)
  3214. {
  3215. EditorFormFileSave(static_cast<TForm *>(Token));
  3216. }
  3217. }
  3218. //---------------------------------------------------------------------------
  3219. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3220. {
  3221. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3222. {
  3223. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3224. }
  3225. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3226. }
  3227. //---------------------------------------------------------------------------
  3228. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3229. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3230. void * Arg)
  3231. {
  3232. if ((Token != NULL) &&
  3233. IsEditorFormModified(static_cast<TForm *>(Token)))
  3234. {
  3235. *static_cast<bool *>(Arg) = true;
  3236. }
  3237. }
  3238. //---------------------------------------------------------------------------
  3239. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3240. TObject * /*Sender*/, bool & Modified)
  3241. {
  3242. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3243. {
  3244. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3245. {
  3246. Modified = true;
  3247. }
  3248. }
  3249. if (!Modified)
  3250. {
  3251. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3252. }
  3253. }
  3254. //---------------------------------------------------------------------------
  3255. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3256. {
  3257. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3258. }
  3259. //---------------------------------------------------------------------------
  3260. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3261. const UnicodeString & RemoteDirectory, const TCopyParamType & CopyParam, bool Simple,
  3262. UnicodeString & Result, UnicodeString & RootDirectory)
  3263. {
  3264. if (!WinConfiguration->TemporaryDirectoryDeterministic || Simple)
  3265. {
  3266. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3267. Result = RootDirectory;
  3268. }
  3269. else
  3270. {
  3271. RootDirectory = L"";
  3272. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3273. Result = IncludeTrailingBackslash(Result);
  3274. }
  3275. if (WinConfiguration->TemporaryDirectoryAppendSession && !Simple)
  3276. {
  3277. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3278. }
  3279. if (WinConfiguration->TemporaryDirectoryAppendPath && !Simple)
  3280. {
  3281. UnicodeString ARemoteDirectory = RemoteDirectory;
  3282. if (!ARemoteDirectory.IsEmpty() && (ARemoteDirectory[1] == L'/'))
  3283. {
  3284. ARemoteDirectory.Delete(1, 1);
  3285. }
  3286. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(ARemoteDirectory)));
  3287. }
  3288. if (!ForceDirectories(ApiPath(Result)))
  3289. {
  3290. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3291. }
  3292. }
  3293. //---------------------------------------------------------------------------
  3294. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3295. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3296. bool GetTargetNames, bool AutoOperation, bool SimpleTempDir)
  3297. {
  3298. DebugAssert(!IsLocalBrowserMode());
  3299. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3300. if (ForceText)
  3301. {
  3302. CopyParam.TransferMode = tmAscii;
  3303. }
  3304. TemporaryFileCopyParam(CopyParam);
  3305. if (TempDir.IsEmpty())
  3306. {
  3307. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, SimpleTempDir, TempDir, RootTempDir);
  3308. }
  3309. DebugAssert(!FAutoOperation);
  3310. FAutoOperation = AutoOperation;
  3311. Terminal->ExceptionOnFail = true;
  3312. try
  3313. {
  3314. try
  3315. {
  3316. // turn off confirmations, as for MDI editors we may possibly download
  3317. // the same file over
  3318. Terminal->CopyToLocal(
  3319. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3320. if (GetTargetNames)
  3321. {
  3322. for (int i = 0; i < FileList->Count; i++)
  3323. {
  3324. FileList->Strings[i] =
  3325. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3326. }
  3327. }
  3328. }
  3329. catch(...)
  3330. {
  3331. if (!RootTempDir.IsEmpty())
  3332. {
  3333. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3334. }
  3335. throw;
  3336. }
  3337. }
  3338. __finally
  3339. {
  3340. FAutoOperation = false;
  3341. Terminal->ExceptionOnFail = false;
  3342. }
  3343. }
  3344. //---------------------------------------------------------------------------
  3345. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3346. {
  3347. // Do not waste time checking for default editor list the next time,
  3348. // or testing if default editor is not notepad.
  3349. bool TryNextTime = false;
  3350. UnicodeString UsageState;
  3351. if (!WinConfiguration->EditorList->IsDefaultList())
  3352. {
  3353. UsageState = "H";
  3354. }
  3355. else
  3356. {
  3357. UnicodeString Executable;
  3358. UnicodeString ExecutableDescription;
  3359. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3360. {
  3361. UnicodeString Message =
  3362. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3363. unsigned int Answer =
  3364. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3365. if (Answer != qaOK)
  3366. {
  3367. UsageState = "R";
  3368. }
  3369. else
  3370. {
  3371. UsageState = "A";
  3372. TEditorData EditorData;
  3373. EditorData.Editor = edExternal;
  3374. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3375. EditorData.ExternalEditorOptionsAutodetect();
  3376. TEditorList EditorList;
  3377. EditorList = *WinConfiguration->EditorList;
  3378. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3379. WinConfiguration->EditorList = &EditorList;
  3380. }
  3381. }
  3382. }
  3383. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3384. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3385. }
  3386. //---------------------------------------------------------------------------
  3387. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3388. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3389. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3390. {
  3391. if (ExecuteFileBy == efDefaultEditor)
  3392. {
  3393. if (!WinConfiguration->OfferedEditorAutoConfig)
  3394. {
  3395. EditorAutoConfig();
  3396. }
  3397. const TEditorPreferences * Editor =
  3398. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3399. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3400. {
  3401. ExecuteFileBy = efInternalEditor;
  3402. ExternalEditor = NULL;
  3403. }
  3404. else if (Editor->Data->Editor == edOpen)
  3405. {
  3406. ExecuteFileBy = efShell;
  3407. ExternalEditor = NULL;
  3408. }
  3409. else
  3410. {
  3411. ExecuteFileBy = efExternalEditor;
  3412. ExternalEditor = Editor->Data;
  3413. }
  3414. }
  3415. }
  3416. //---------------------------------------------------------------------------
  3417. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3418. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3419. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3420. {
  3421. UnicodeString OriginalFileName;
  3422. UnicodeString LocalRootDirectory;
  3423. UnicodeString RemoteDirectory;
  3424. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3425. IsSideLocalBrowser(Side), MaskParams);
  3426. UnicodeString Counter;
  3427. UnicodeString LocalFileName;
  3428. bool Handled = false;
  3429. if (!IsSideLocalBrowser(Side))
  3430. {
  3431. // We need to trim VMS version here, so that we use name without version
  3432. // when uploading back to create a new version of the file
  3433. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3434. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3435. TObject * Token = NULL;
  3436. UnicodeString LocalDirectory;
  3437. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3438. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3439. LocalDirectory))
  3440. {
  3441. if (Token != NULL)
  3442. {
  3443. TForm * Form = dynamic_cast<TForm *>(Token);
  3444. if (Form->WindowState == wsMinimized)
  3445. {
  3446. ShowWindow(Form->Handle, SW_RESTORE);
  3447. }
  3448. else
  3449. {
  3450. Form->SetFocus();
  3451. }
  3452. Handled = true;
  3453. }
  3454. else
  3455. {
  3456. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3457. }
  3458. }
  3459. if (!Handled)
  3460. {
  3461. TStringList * FileList1 = new TStringList();
  3462. try
  3463. {
  3464. FileList1->AddObject(FullFileName, Object);
  3465. TemporarilyDownloadFiles(FileList1,
  3466. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3467. LocalRootDirectory, LocalDirectory, true, true, false);
  3468. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3469. }
  3470. __finally
  3471. {
  3472. delete FileList1;
  3473. }
  3474. switch (ExecuteFileBy)
  3475. {
  3476. case efShell:
  3477. Counter = "RemoteFilesExecuted";
  3478. break;
  3479. case efInternalEditor:
  3480. Counter = "RemoteFilesOpenedInInternalEditor";
  3481. break;
  3482. case efExternalEditor:
  3483. Counter = "RemoteFilesOpenedInExternalEditor";
  3484. break;
  3485. default:
  3486. DebugFail();
  3487. }
  3488. }
  3489. }
  3490. else
  3491. {
  3492. LocalFileName = FullFileName;
  3493. OriginalFileName = ExtractFileName(FullFileName);
  3494. switch (ExecuteFileBy)
  3495. {
  3496. case efShell:
  3497. Counter = "LocalFilesExecuted";
  3498. break;
  3499. case efInternalEditor:
  3500. Counter = "LocalFilesOpenedInInternalEditor";
  3501. break;
  3502. case efExternalEditor:
  3503. Counter = "LocalFilesOpenedInExternalEditor";
  3504. break;
  3505. default:
  3506. DebugFail();
  3507. }
  3508. }
  3509. if (!Handled)
  3510. {
  3511. Configuration->Usage->Inc(Counter);
  3512. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3513. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3514. }
  3515. }
  3516. //---------------------------------------------------------------------------
  3517. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3518. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3519. bool AllSelected, bool OnFocused)
  3520. {
  3521. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3522. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3523. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3524. Side = GetSide(Side);
  3525. TCustomDirView * DView = DirView(Side);
  3526. TStrings * FileList =
  3527. AllSelected ?
  3528. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3529. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3530. try
  3531. {
  3532. DebugAssert(AllSelected || (FileList->Count == 1));
  3533. for (int i = 0; i < FileList->Count; i++)
  3534. {
  3535. UnicodeString ListFileName = FileList->Strings[i];
  3536. UnicodeString FileNameOnly =
  3537. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3538. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3539. if (!DView->ItemIsDirectory(Item))
  3540. {
  3541. UnicodeString FullFileName;
  3542. if (!IsSideLocalBrowser(Side))
  3543. {
  3544. FullFileName = DirView(Side)->Path + ListFileName;
  3545. }
  3546. else
  3547. {
  3548. FullFileName = ListFileName;
  3549. }
  3550. TObject * Object = FileList->Objects[i];
  3551. TFileMasks::TParams MaskParams;
  3552. MaskParams.Size = DView->ItemFileSize(Item);
  3553. TDateTimePrecision Precision;
  3554. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3555. std::unique_ptr<TRemoteFile> FileOwner;
  3556. if (!IsSideLocalBrowser(Side))
  3557. {
  3558. TRemoteFile * File = NULL;
  3559. Terminal->FileExists(FullFileName, &File);
  3560. if (File != NULL)
  3561. {
  3562. File->FullFileName = FullFileName;
  3563. Object = File;
  3564. FileOwner.reset(File);
  3565. MaskParams.Size = File->Size;
  3566. MaskParams.Modification = File->Modification;
  3567. }
  3568. }
  3569. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName, Object, MaskParams);
  3570. }
  3571. }
  3572. }
  3573. __finally
  3574. {
  3575. delete FileList;
  3576. }
  3577. }
  3578. //---------------------------------------------------------------------------
  3579. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3580. {
  3581. CopyParam.FileNameCase = ncNoChange;
  3582. CopyParam.PreserveRights = false;
  3583. CopyParam.PreserveReadOnly = false;
  3584. CopyParam.ReplaceInvalidChars = true;
  3585. CopyParam.IncludeFileMask = TFileMasks();
  3586. CopyParam.NewerOnly = false;
  3587. CopyParam.FileMask = L"";
  3588. }
  3589. //---------------------------------------------------------------------------
  3590. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
  3591. const UnicodeString & FileName, TEditedFileData * Data, HANDLE UploadCompleteEvent, bool & Retry)
  3592. {
  3593. TTerminalManager * Manager = TTerminalManager::Instance();
  3594. if (!IsActiveTerminal(Data->Terminal))
  3595. {
  3596. if (!NonVisualDataModule->Busy)
  3597. {
  3598. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3599. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3600. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3601. if (Data->Terminal == NULL)
  3602. {
  3603. TManagedTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3604. if (SameSiteTerminal != NULL)
  3605. {
  3606. UnicodeString Message =
  3607. FMTLOAD(EDIT_SESSION_REATTACH,
  3608. (FileNameOnly, Data->SessionName, FileNameOnly));
  3609. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3610. {
  3611. Data->Terminal = SameSiteTerminal;
  3612. Data->Queue = Manager->FindQueueForTerminal(SameSiteTerminal);
  3613. Data->SessionName = SameSiteTerminal->SessionData->SessionName;
  3614. // We might also overwrite session data
  3615. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3616. }
  3617. else
  3618. {
  3619. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3620. Abort();
  3621. }
  3622. }
  3623. }
  3624. // foreground session should reconnect itself
  3625. else if (Terminal != Data->Terminal)
  3626. {
  3627. UnicodeString Message =
  3628. MainInstructions(
  3629. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3630. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3631. {
  3632. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3633. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3634. }
  3635. else
  3636. {
  3637. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3638. Abort();
  3639. }
  3640. }
  3641. }
  3642. if (!IsActiveTerminal(Data->Terminal))
  3643. {
  3644. Configuration->Usage->Inc(L"EditInactiveSession");
  3645. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3646. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3647. (ExtractFileName(FileName), Data->SessionName)));
  3648. }
  3649. }
  3650. TStrings * FileList = new TStringList();
  3651. try
  3652. {
  3653. FileList->Add(FileName);
  3654. // Consider using the same settings (preset) as when the file was downloaded.
  3655. // More over this does not reflect the actual session that will do the upload.
  3656. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3657. TemporaryFileCopyParam(CopyParam);
  3658. if (Data->ForceText)
  3659. {
  3660. CopyParam.TransferMode = tmAscii;
  3661. }
  3662. // so i do not need to worry if masking algorithm works in all cases
  3663. // ("" means "copy file name", no masking is actually done)
  3664. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3665. {
  3666. CopyParam.FileMask = L"";
  3667. }
  3668. else
  3669. {
  3670. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3671. }
  3672. int Params = cpNoConfirmation | cpTemporary;
  3673. if (Data->Terminal->IsCapable[fcBackgroundTransfers])
  3674. {
  3675. DebugAssert(Data->Queue != NULL);
  3676. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3677. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3678. QueueItem->CompleteEvent = UploadCompleteEvent;
  3679. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3680. }
  3681. else
  3682. {
  3683. if (NonVisualDataModule->Busy)
  3684. {
  3685. Retry = true;
  3686. }
  3687. else
  3688. {
  3689. Data->Terminal->CopyToRemote(FileList, Data->RemoteDirectory, &CopyParam, Params, NULL);
  3690. SetEvent(UploadCompleteEvent);
  3691. }
  3692. }
  3693. }
  3694. __finally
  3695. {
  3696. delete FileList;
  3697. }
  3698. }
  3699. //---------------------------------------------------------------------------
  3700. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3701. const UnicodeString FileName, const TEditedFileData * Data)
  3702. {
  3703. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3704. DebugAssert(!NonVisualDataModule->Busy);
  3705. if (!IsActiveTerminal(Data->Terminal))
  3706. {
  3707. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3708. (ExtractFileName(FileName), Data->SessionName)));
  3709. }
  3710. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3711. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3712. NonVisualDataModule->StartBusy();
  3713. try
  3714. {
  3715. std::unique_ptr<TRemoteFile> File;
  3716. UnicodeString RemoteFileName =
  3717. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3718. Terminal->ExceptionOnFail = true;
  3719. try
  3720. {
  3721. TRemoteFile * AFile = NULL;
  3722. Terminal->ReadFile(RemoteFileName, AFile);
  3723. File.reset(AFile);
  3724. if (!File->HaveFullFileName)
  3725. {
  3726. File->FullFileName = RemoteFileName;
  3727. }
  3728. }
  3729. __finally
  3730. {
  3731. Terminal->ExceptionOnFail = false;
  3732. }
  3733. std::unique_ptr<TStrings> FileList(new TStringList());
  3734. FileList->AddObject(RemoteFileName, File.get());
  3735. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3736. UnicodeString TempDir = ExtractFilePath(FileName);
  3737. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, true, false);
  3738. // sanity check, the target file name should be still the same
  3739. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3740. }
  3741. __finally
  3742. {
  3743. NonVisualDataModule->EndBusy();
  3744. // it actually may not exist anymore...
  3745. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3746. }
  3747. }
  3748. //---------------------------------------------------------------------------
  3749. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3750. const TEditedFileData * Data, bool & KeepOpen)
  3751. {
  3752. // Command is set for external editors only (not for "shell" open).
  3753. if (!Data->Command.IsEmpty())
  3754. {
  3755. bool AnyFound = false;
  3756. bool AnyMDI = false;
  3757. bool AnyNonMDI = false;
  3758. bool AnyDetect = false;
  3759. TEditorList * EditorList = new TEditorList();
  3760. try
  3761. {
  3762. *EditorList = *WinConfiguration->EditorList;
  3763. for (int i = 0; i < EditorList->Count; i++)
  3764. {
  3765. const TEditorPreferences * Editor = EditorList->Editors[i];
  3766. if ((Editor->Data->Editor == edExternal) &&
  3767. (Editor->Data->ExternalEditor == Data->Command))
  3768. {
  3769. AnyFound = true;
  3770. if (Editor->Data->SDIExternalEditor)
  3771. {
  3772. AnyNonMDI = true;
  3773. if (Editor->Data->DetectMDIExternalEditor)
  3774. {
  3775. AnyDetect = true;
  3776. }
  3777. }
  3778. else
  3779. {
  3780. AnyMDI = true;
  3781. }
  3782. }
  3783. }
  3784. bool EnableMDI = false;
  3785. bool DisableDetect = false;
  3786. if (AnyMDI)
  3787. {
  3788. KeepOpen = true;
  3789. if (AnyNonMDI)
  3790. {
  3791. // there is at least one instance of the editor with MDI support enabled,
  3792. // and one with disabled, enable it for all instances
  3793. EnableMDI = true;
  3794. }
  3795. }
  3796. else if (AnyFound && !AnyDetect)
  3797. {
  3798. // at least once instance found but all have MDI autodetection disabled
  3799. // => close the file (default action)
  3800. }
  3801. else
  3802. {
  3803. // no instance of the editor has MDI support enabled
  3804. TMessageParams Params;
  3805. if (AnyFound)
  3806. {
  3807. // there is at least one instance of the editor
  3808. Params.Params |= mpNeverAskAgainCheck;
  3809. }
  3810. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3811. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3812. switch (Answer)
  3813. {
  3814. case qaNeverAskAgain:
  3815. DisableDetect = true;
  3816. break;
  3817. case qaNo:
  3818. EnableMDI = true;
  3819. KeepOpen = true;
  3820. break;
  3821. }
  3822. }
  3823. if (AnyFound && (EnableMDI || DisableDetect))
  3824. {
  3825. bool Changed = false;
  3826. for (int i = 0; i < EditorList->Count; i++)
  3827. {
  3828. const TEditorPreferences * Editor = EditorList->Editors[i];
  3829. if ((Editor->Data->Editor == edExternal) &&
  3830. (Editor->Data->ExternalEditor == Data->Command) &&
  3831. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3832. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3833. {
  3834. Changed = true;
  3835. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3836. if (EnableMDI)
  3837. {
  3838. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3839. }
  3840. if (DisableDetect)
  3841. {
  3842. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3843. }
  3844. EditorList->Change(i, UpdatedEditor);
  3845. }
  3846. }
  3847. if (Changed)
  3848. {
  3849. WinConfiguration->EditorList = EditorList;
  3850. }
  3851. }
  3852. }
  3853. __finally
  3854. {
  3855. delete EditorList;
  3856. }
  3857. }
  3858. else
  3859. {
  3860. // "open" case
  3861. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3862. qaOK, HELP_APP_EARLY_CLOSED);
  3863. }
  3864. }
  3865. //---------------------------------------------------------------------------
  3866. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3867. {
  3868. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3869. }
  3870. //---------------------------------------------------------------------------
  3871. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3872. {
  3873. SideEnter(osRemote);
  3874. }
  3875. //---------------------------------------------------------------------------
  3876. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3877. {
  3878. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3879. SideEnter(osRemote);
  3880. }
  3881. //---------------------------------------------------------------------------
  3882. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3883. {
  3884. FCurrentSide = Side;
  3885. if (Visible)
  3886. {
  3887. UpdateControls();
  3888. }
  3889. }
  3890. //---------------------------------------------------------------------------
  3891. void __fastcall TCustomScpExplorerForm::FileDeleted(TOperationSide Side, const UnicodeString & FileName, bool Success)
  3892. {
  3893. VisualiseOperationFinished(Side, FileName, Success);
  3894. if (DebugAlwaysTrue(FDeletedFiles != NULL) && Success)
  3895. {
  3896. FDeletedFiles->Add(FileName);
  3897. }
  3898. }
  3899. //---------------------------------------------------------------------------
  3900. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3901. TStrings * FileList, bool Alternative)
  3902. {
  3903. TCustomDirView * DView = DirView(Side);
  3904. DView->SaveSelection();
  3905. DView->SaveSelectedNames();
  3906. DebugAssert(!FAlternativeDelete);
  3907. FAlternativeDelete = Alternative;
  3908. try
  3909. {
  3910. if (!IsSideLocalBrowser(Side))
  3911. {
  3912. DebugAssert(Terminal != NULL);
  3913. // Clone the file list as it may refer to current directory files, which get destroyed, when the directory is reloaded after the operation
  3914. std::unique_ptr<TStrings> PermanentFileList(TRemoteFileList::CloneStrings(FileList));
  3915. std::unique_ptr<TStringList> DeletedFiles(new TStringList());
  3916. DeletedFiles->CaseSensitive = DebugAlwaysTrue(!IsSideLocalBrowser(Side));
  3917. int Params = FLAGMASK(Alternative, dfAlternative);
  3918. try
  3919. {
  3920. TValueRestorer<TStrings *> DeletedFilesRestorer(FDeletedFiles);
  3921. DebugAssert(FDeletedFiles == NULL);
  3922. FDeletedFiles = DeletedFiles.get();
  3923. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  3924. FOnFileOperationFinished = FileDeleted;
  3925. FMoveToQueue = false;
  3926. Terminal->DeleteFiles(FileList, Params);
  3927. // Probably not needed for deleting, just for consistency with transfer code
  3928. if (FMoveToQueue)
  3929. {
  3930. Abort();
  3931. }
  3932. }
  3933. catch (EAbort &)
  3934. {
  3935. if (FMoveToQueue)
  3936. {
  3937. DeletedFiles->Sorted = true;
  3938. for (int Index = 0; Index < PermanentFileList->Count; Index++)
  3939. {
  3940. if (DeletedFiles->IndexOf(PermanentFileList->Strings[Index]) >= 0)
  3941. {
  3942. // We should always be deleting the first item => what can be used to optimize this code, if needed
  3943. DebugAssert(Index == 0);
  3944. PermanentFileList->Delete(Index);
  3945. Index--;
  3946. }
  3947. }
  3948. Configuration->Usage->Inc("MovesToBackgroundDelete");
  3949. TQueueItem * QueueItem = new TDeleteQueueItem(Terminal, PermanentFileList.get(), Params);
  3950. AddQueueItem(Queue, QueueItem, Terminal);
  3951. ClearOperationSelection(osRemote);
  3952. }
  3953. }
  3954. }
  3955. else
  3956. {
  3957. if (IsLocalBrowserMode())
  3958. {
  3959. Configuration->Usage->Inc(L"LocalLocalDeletes");
  3960. }
  3961. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  3962. FProgressSide = FCurrentSide;
  3963. try
  3964. {
  3965. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3966. }
  3967. __finally
  3968. {
  3969. ReloadLocalDirectory();
  3970. }
  3971. }
  3972. FAlternativeDelete = false;
  3973. }
  3974. catch(...)
  3975. {
  3976. FAlternativeDelete = false;
  3977. DView->DiscardSavedSelection();
  3978. throw;
  3979. }
  3980. DView->RestoreSelection();
  3981. }
  3982. //---------------------------------------------------------------------------
  3983. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3984. {
  3985. DebugAssert(Terminal);
  3986. DebugAssert(!IsLocalBrowserMode());
  3987. RemoteDirView->SaveSelection();
  3988. RemoteDirView->SaveSelectedNames();
  3989. try
  3990. {
  3991. if (Lock)
  3992. {
  3993. Terminal->LockFiles(FileList);
  3994. }
  3995. else
  3996. {
  3997. Terminal->UnlockFiles(FileList);
  3998. }
  3999. }
  4000. catch(...)
  4001. {
  4002. RemoteDirView->DiscardSavedSelection();
  4003. throw;
  4004. }
  4005. RemoteDirView->RestoreSelection();
  4006. }
  4007. //---------------------------------------------------------------------------
  4008. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  4009. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  4010. bool NoConfirmation, bool Move)
  4011. {
  4012. DebugAssert(Terminal != NULL);
  4013. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  4014. DebugAssert(!IsLocalBrowserMode());
  4015. if (Session == NULL)
  4016. {
  4017. Session = Terminal;
  4018. }
  4019. if (Session == Terminal)
  4020. {
  4021. if (RemoteDriveView->DropTarget != NULL)
  4022. {
  4023. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  4024. }
  4025. else if (RemoteDirView->DropTarget != NULL)
  4026. {
  4027. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  4028. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  4029. }
  4030. else
  4031. {
  4032. Target = RemoteDirView->Path;
  4033. }
  4034. }
  4035. else
  4036. {
  4037. Target = GetSessionPath(Session, osRemote);
  4038. }
  4039. Target = UnixIncludeTrailingBackslash(Target);
  4040. if (FileList->Count == 1)
  4041. {
  4042. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  4043. }
  4044. else
  4045. {
  4046. FileMask = AnyMask;
  4047. }
  4048. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  4049. bool Result = true;
  4050. if (!NoConfirmation)
  4051. {
  4052. bool Multi = (FileList->Count > 1);
  4053. if (Move)
  4054. {
  4055. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  4056. }
  4057. else
  4058. {
  4059. std::unique_ptr<TStrings> Sessions(new TStringList);
  4060. std::unique_ptr<TStrings> Directories(new TStringList);
  4061. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  4062. for (int Index = 0; Index < SessionList->Count; Index++)
  4063. {
  4064. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  4065. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  4066. {
  4067. Sessions->AddObject(SessionList->Strings[Index], ASession);
  4068. Directories->Add(GetSessionPath(ASession, osRemote));
  4069. }
  4070. }
  4071. TDirectRemoteCopy AllowDirectCopy;
  4072. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  4073. {
  4074. DebugAssert(DirectCopy);
  4075. AllowDirectCopy = drcAllow;
  4076. }
  4077. else if (Terminal->IsCapable[fcSecondaryShell])
  4078. {
  4079. DebugAssert(DirectCopy);
  4080. AllowDirectCopy = drcConfirmCommandSession;
  4081. }
  4082. else
  4083. {
  4084. DebugAssert(!DirectCopy);
  4085. AllowDirectCopy = drcDisallow;
  4086. }
  4087. void * ASession = Session;
  4088. Result = DoRemoteCopyDialog(
  4089. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal);
  4090. Session = static_cast<TManagedTerminal *>(ASession);
  4091. }
  4092. }
  4093. return Result;
  4094. }
  4095. //---------------------------------------------------------------------------
  4096. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  4097. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  4098. {
  4099. DebugAssert(!IsLocalBrowserMode());
  4100. bool DirectCopy;
  4101. UnicodeString Target, FileMask;
  4102. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  4103. if (Result)
  4104. {
  4105. if (!Move && !DirectCopy)
  4106. {
  4107. Configuration->Usage->Inc("RemoteCopyTemp");
  4108. UnicodeString RootTempDir;
  4109. UnicodeString TempDir;
  4110. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  4111. TStrings * TemporaryFilesList = new TStringList();
  4112. try
  4113. {
  4114. TMakeLocalFileListParams MakeFileListParam;
  4115. MakeFileListParam.FileList = TemporaryFilesList;
  4116. MakeFileListParam.FileTimes = NULL;
  4117. MakeFileListParam.IncludeDirs = true;
  4118. MakeFileListParam.Recursive = false;
  4119. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  4120. TTerminalManager::Instance()->ActiveSession = Session;
  4121. if (TemporaryFilesList->Count > 0)
  4122. {
  4123. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4124. CopyParam.FileMask = FileMask;
  4125. DebugAssert(!FAutoOperation);
  4126. FAutoOperation = true;
  4127. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  4128. }
  4129. }
  4130. __finally
  4131. {
  4132. delete TemporaryFilesList;
  4133. FAutoOperation = false;
  4134. if (!RootTempDir.IsEmpty())
  4135. {
  4136. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  4137. }
  4138. }
  4139. }
  4140. else
  4141. {
  4142. RemoteDirView->SaveSelection();
  4143. RemoteDirView->SaveSelectedNames();
  4144. try
  4145. {
  4146. if (Move)
  4147. {
  4148. Configuration->Usage->Inc("RemoteMove");
  4149. Terminal->MoveFiles(FileList, Target, FileMask, false);
  4150. }
  4151. else
  4152. {
  4153. Configuration->Usage->Inc("RemoteCopyDirect");
  4154. DebugAssert(DirectCopy);
  4155. DebugAssert(Session == Terminal);
  4156. if (Terminal->IsCapable[fcRemoteCopy] ||
  4157. Terminal->CommandSessionOpened ||
  4158. CommandSessionFallback())
  4159. {
  4160. Terminal->CopyFiles(FileList, Target, FileMask);
  4161. }
  4162. }
  4163. }
  4164. catch(...)
  4165. {
  4166. RemoteDirView->DiscardSavedSelection();
  4167. throw;
  4168. }
  4169. RemoteDirView->RestoreSelection();
  4170. }
  4171. }
  4172. return Result;
  4173. }
  4174. //---------------------------------------------------------------------------
  4175. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4176. const UnicodeString & Path, TRemoteProperties & Properties)
  4177. {
  4178. DebugAssert(!IsLocalBrowserMode());
  4179. Properties.Valid = Properties.Valid << vpEncrypt;
  4180. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4181. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4182. }
  4183. //---------------------------------------------------------------------------
  4184. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4185. {
  4186. Side = GetSide(Side);
  4187. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4188. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4189. UnicodeString Name = LoadStr(NEW_FOLDER);
  4190. int AllowedChanges =
  4191. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4192. bool SaveSettings = false;
  4193. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4194. {
  4195. TWindowLock Lock(this);
  4196. if (!IsSideLocalBrowser(Side))
  4197. {
  4198. if (SaveSettings)
  4199. {
  4200. GUIConfiguration->NewDirectoryProperties = Properties;
  4201. }
  4202. CreateRemoteDirectory(Name, Properties);
  4203. }
  4204. else
  4205. {
  4206. DirView(Side)->CreateDirectory(Name);
  4207. }
  4208. }
  4209. }
  4210. //---------------------------------------------------------------------------
  4211. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4212. {
  4213. TWindowLock Lock(this);
  4214. DirView(Side)->ExecuteHomeDirectory();
  4215. }
  4216. //---------------------------------------------------------------------------
  4217. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4218. {
  4219. DoOpenDirectoryDialog(odBrowse, Side);
  4220. }
  4221. //---------------------------------------------------------------------------
  4222. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4223. {
  4224. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4225. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4226. bool Result = !Path.IsEmpty();
  4227. if (Result)
  4228. {
  4229. TryOpenDirectory(Side, Path);
  4230. }
  4231. return Result;
  4232. }
  4233. //---------------------------------------------------------------------------
  4234. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4235. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4236. bool & Close)
  4237. {
  4238. // terminal can be already closed (e.g. dropped connection)
  4239. if (Terminal != NULL)
  4240. {
  4241. try
  4242. {
  4243. TCalculateSizeParams Params;
  4244. Params.Stats = &Stats;
  4245. Terminal->CalculateFilesSize(FileList, Size, Params);
  4246. }
  4247. catch(...)
  4248. {
  4249. if (!Terminal->Active)
  4250. {
  4251. Close = true;
  4252. }
  4253. throw;
  4254. }
  4255. }
  4256. }
  4257. //---------------------------------------------------------------------------
  4258. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4259. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4260. bool & Close)
  4261. {
  4262. if ((Terminal != NULL) && // terminal can be already closed (e.g. dropped connection)
  4263. EnsureCommandSessionFallback(fcCalculatingChecksum))
  4264. {
  4265. Configuration->Usage->Inc(L"ChecksumCalculated");
  4266. try
  4267. {
  4268. Terminal->CalculateFilesChecksum(Alg, FileList, OnCalculatedChecksum);
  4269. }
  4270. catch(...)
  4271. {
  4272. if (!Terminal->Active)
  4273. {
  4274. Close = true;
  4275. }
  4276. throw;
  4277. }
  4278. }
  4279. }
  4280. //---------------------------------------------------------------------------
  4281. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4282. {
  4283. if (Terminal->LoadFilesProperties(FileList))
  4284. {
  4285. RemoteDirView->Invalidate();
  4286. }
  4287. }
  4288. //---------------------------------------------------------------------------
  4289. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4290. {
  4291. bool Result;
  4292. if (!IsSideLocalBrowser(Side))
  4293. {
  4294. TRemoteTokenList * GroupList = NULL;
  4295. TRemoteTokenList * UserList = NULL;
  4296. try
  4297. {
  4298. TRemoteProperties CurrentProperties;
  4299. LoadFilesProperties(FileList);
  4300. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4301. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4302. if (CapableGroupChanging || CapableOwnerChanging)
  4303. {
  4304. if (CapableGroupChanging)
  4305. {
  4306. GroupList = Terminal->Groups->Duplicate();
  4307. }
  4308. if (CapableOwnerChanging)
  4309. {
  4310. UserList = Terminal->Users->Duplicate();
  4311. }
  4312. TRemoteDirectory * Files = Terminal->Files;
  4313. int Count = Files->Count;
  4314. if (Count > 100)
  4315. {
  4316. Count = 100;
  4317. }
  4318. for (int Index = 0; Index < Count; Index++)
  4319. {
  4320. TRemoteFile * File = Files->Files[Index];
  4321. if (CapableGroupChanging)
  4322. {
  4323. GroupList->AddUnique(File->Group);
  4324. }
  4325. if (CapableOwnerChanging)
  4326. {
  4327. UserList->AddUnique(File->Owner);
  4328. }
  4329. }
  4330. // if we haven't collected tokens for all files in current directory,
  4331. // make sure we collect them at least for all selected files.
  4332. // (note that so far the files in FileList has to be from current direcotry)
  4333. if (Count < Files->Count)
  4334. {
  4335. for (int Index = 0; Index < FileList->Count; Index++)
  4336. {
  4337. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4338. if (CapableGroupChanging)
  4339. {
  4340. GroupList->AddUnique(File->Group);
  4341. }
  4342. if (CapableOwnerChanging)
  4343. {
  4344. UserList->AddUnique(File->Owner);
  4345. }
  4346. }
  4347. }
  4348. }
  4349. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4350. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4351. for (int Index = 0; (Index < FileList->Count) && CapableAclChanging; Index++)
  4352. {
  4353. if (dynamic_cast<TRemoteFile *>(FileList->Objects[Index])->IsDirectory)
  4354. {
  4355. CapableAclChanging = false;
  4356. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4357. }
  4358. }
  4359. int Flags = 0;
  4360. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4361. if (CapableAclChanging) Flags |= cpAcl;
  4362. if (CapableOwnerChanging) Flags |= cpOwner;
  4363. if (CapableGroupChanging) Flags |= cpGroup;
  4364. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4365. if (Terminal->IsCapable[fcCalculatingChecksum] ||
  4366. (Terminal->IsCapable[fcSecondaryShell] && !Terminal->IsEncryptingFiles()))
  4367. {
  4368. CalculateChecksumEvent = CalculateChecksum;
  4369. }
  4370. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4371. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4372. TRemoteProperties NewProperties = CurrentProperties;
  4373. Result =
  4374. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4375. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4376. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4377. CalculateSize, CalculateChecksumEvent);
  4378. if (Result)
  4379. {
  4380. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4381. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4382. }
  4383. }
  4384. __finally
  4385. {
  4386. delete GroupList;
  4387. delete UserList;
  4388. }
  4389. }
  4390. else
  4391. {
  4392. DirView(Side)->DisplayPropertiesMenu();
  4393. Result = true;
  4394. }
  4395. return Result;
  4396. }
  4397. //---------------------------------------------------------------------------
  4398. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4399. {
  4400. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4401. {
  4402. FIgnoreNextDialogChar = Key;
  4403. }
  4404. Key = 0;
  4405. }
  4406. //---------------------------------------------------------------------------
  4407. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4408. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4409. {
  4410. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4411. if (Command != NULL)
  4412. {
  4413. KeyProcessed(Key, Shift);
  4414. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4415. {
  4416. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4417. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4418. false, false, const_cast<TCustomCommandType *>(Command));
  4419. }
  4420. }
  4421. }
  4422. //---------------------------------------------------------------------------
  4423. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4424. {
  4425. if (!NonVisualDataModule->Busy)
  4426. {
  4427. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4428. {
  4429. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4430. }
  4431. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4432. {
  4433. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4434. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4435. {
  4436. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4437. // But so far it's not a problem, as it does what we want.
  4438. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4439. if (((Action->ShortCut == KeyShortCut) ||
  4440. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4441. AllowedAction(Action, aaShortCut))
  4442. {
  4443. // Has to be called before the action as the dialog char is already in queue.
  4444. // So when the action consumes message queue, we already need to have the
  4445. // FIgnoreNextDialogChar set
  4446. KeyProcessed(Key, Shift);
  4447. // Reset reference to previous component (when menu/toolbar was clicked).
  4448. // Needed to detect that action was invoked by keyboard shortcut
  4449. // in TNonVisualDataModule::ExplorerActionsExecute
  4450. Action->ActionComponent = NULL;
  4451. Action->Execute();
  4452. return;
  4453. }
  4454. }
  4455. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4456. {
  4457. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4458. {
  4459. KeyProcessed(Key, Shift);
  4460. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4461. return;
  4462. }
  4463. }
  4464. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4465. {
  4466. KeyProcessed(Key, Shift);
  4467. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4468. }
  4469. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4470. if (IsCustomShortCut(CustomShortCut))
  4471. {
  4472. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4473. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4474. if (WinConfiguration->SharedBookmarks != NULL)
  4475. {
  4476. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4477. if ((Bookmark != NULL) &&
  4478. OpenBookmark(FCurrentSide, Bookmark))
  4479. {
  4480. KeyProcessed(Key, Shift);
  4481. }
  4482. }
  4483. }
  4484. }
  4485. }
  4486. TForm::KeyDown(Key, Shift);
  4487. }
  4488. //---------------------------------------------------------------------------
  4489. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4490. {
  4491. bool OpenedTerminal = false;
  4492. UnicodeString ProtocolName;
  4493. UnicodeString ProtocolNameHint;
  4494. UnicodeString SecurityProtocolName;
  4495. UnicodeString DurationHint;
  4496. // See the comment in TScpCommanderForm::UpdateControls()
  4497. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4498. if (ActiveTerminal != NULL)
  4499. {
  4500. // Active is not enough here
  4501. if (ActiveTerminal->Status == ssOpened)
  4502. {
  4503. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4504. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4505. if (FileSystemInfo.ProtocolName.IsEmpty())
  4506. {
  4507. ProtocolName = SessionInfo.ProtocolName;
  4508. }
  4509. else
  4510. {
  4511. ProtocolName = FileSystemInfo.ProtocolName;
  4512. }
  4513. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4514. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4515. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4516. OpenedTerminal = true;
  4517. }
  4518. else if (ActiveTerminal->Status > ssClosed)
  4519. {
  4520. ProtocolName = LoadStr(STATUS_CONNECTING);
  4521. }
  4522. else
  4523. {
  4524. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4525. }
  4526. }
  4527. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4528. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4529. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4530. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4531. if (ProtocolStatusPanel->Caption != ProtocolName)
  4532. {
  4533. ProtocolStatusPanel->Caption = ProtocolName;
  4534. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4535. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4536. }
  4537. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4538. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4539. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4540. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4541. SecurityStatusPanel->Enabled = SecurityEnabled;
  4542. UnicodeString SecurityProtocolHint;
  4543. if (SecurityEnabled)
  4544. {
  4545. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4546. }
  4547. else
  4548. {
  4549. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4550. }
  4551. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4552. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4553. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4554. DurationStatusPanel->Hint = DurationHint;
  4555. UpdateStatusBar();
  4556. }
  4557. //---------------------------------------------------------------------------
  4558. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4559. {
  4560. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4561. DebugAssert(SessionStatusBar != NULL);
  4562. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4563. if (!FNote.IsEmpty())
  4564. {
  4565. NoteStatusPanel->Caption = FNote;
  4566. }
  4567. else
  4568. {
  4569. UpdateStatusPanelText(NoteStatusPanel);
  4570. }
  4571. NoteStatusPanel->Hint = FNoteHints;
  4572. UnicodeString LoginTime;
  4573. // See the comment in TScpCommanderForm::UpdateControls()
  4574. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4575. // Active is not enough here
  4576. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4577. {
  4578. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4579. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4580. }
  4581. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4582. DurationStatusPanel->Caption = LoginTime;
  4583. }
  4584. //---------------------------------------------------------------------------
  4585. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4586. {
  4587. Panel->Caption = L"";
  4588. }
  4589. //---------------------------------------------------------------------------
  4590. void __fastcall TCustomScpExplorerForm::Idle()
  4591. {
  4592. if (FShowing ||
  4593. // Particularly for
  4594. // - Standalone editing
  4595. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4596. // while "keeping remote directory up to date".
  4597. StandaloneOperation)
  4598. {
  4599. FEditorManager->Check();
  4600. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4601. // that needs to know if queue view is visible (and it may be closed after queue update)
  4602. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4603. }
  4604. if (FShowing)
  4605. {
  4606. if (!NonVisualDataModule->Busy &&
  4607. // Menu is opened or drag&drop is going on
  4608. (Mouse->Capture == NULL))
  4609. {
  4610. if (FRefreshRemoteDirectory)
  4611. {
  4612. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4613. {
  4614. Terminal->RefreshDirectory();
  4615. }
  4616. FRefreshRemoteDirectory = false;
  4617. }
  4618. if (FRefreshLocalDirectory)
  4619. {
  4620. ReloadLocalDirectory();
  4621. FRefreshLocalDirectory = false;
  4622. }
  4623. if (WinConfiguration->RefreshRemotePanel)
  4624. {
  4625. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4626. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4627. {
  4628. RemoteDirView->ReloadDirectory();
  4629. }
  4630. }
  4631. }
  4632. // TB2 shows accels on Alt key down, but hides it only after menu modal loop finishes.
  4633. // But if Alt is used to switch apps or for keyboard shortcut, the menu modal loop does not happen and accelerators stay displayed.
  4634. // It's difficult to fix this in TB2, as there's no "up" equivalent to CM_DIALOGKEY, which shows the accels.
  4635. // And the Alt key can be released even when other window or application is active, so handling the key up in explorer window won't do either.
  4636. TTBCustomToolbar * MenuToolbar = DebugNotNull(dynamic_cast<TTBCustomToolbar *>(GetComponent(fcMenu)));
  4637. MenuToolbar->Idle();
  4638. }
  4639. if (FShowing || StandaloneOperation)
  4640. {
  4641. if (FQueueStatusInvalidated)
  4642. {
  4643. UpdateQueueStatus(false);
  4644. }
  4645. RefreshQueueItems();
  4646. }
  4647. if (FShowing)
  4648. {
  4649. UpdateStatusBar();
  4650. }
  4651. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4652. {
  4653. FClipboardFakeMonitorsPendingReset = false;
  4654. FClipboardFakeMonitors.reset(NULL);
  4655. }
  4656. FIgnoreNextDialogChar = 0;
  4657. }
  4658. //---------------------------------------------------------------------------
  4659. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4660. {
  4661. try
  4662. {
  4663. FUserActionTimer->Enabled = false;
  4664. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4665. {
  4666. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4667. {
  4668. FPendingQueueActionItem->ProcessUserAction();
  4669. }
  4670. }
  4671. }
  4672. __finally
  4673. {
  4674. FPendingQueueActionItem = NULL;
  4675. }
  4676. }
  4677. //---------------------------------------------------------------------------
  4678. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4679. {
  4680. if (WinConfiguration->MinimizeToTray)
  4681. {
  4682. UpdateTrayIcon();
  4683. FTrayIcon->Visible = true;
  4684. if (Visible)
  4685. {
  4686. ShowWindow(Handle, SW_HIDE);
  4687. }
  4688. }
  4689. }
  4690. //---------------------------------------------------------------------------
  4691. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4692. {
  4693. // WORKAROUND
  4694. // When restoring maximized window from minimization,
  4695. // rarely some controls do not align properly.
  4696. // Two instances seen (both for Commander):
  4697. // - When restoring, window is temporarily narrower (not maximized),
  4698. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4699. // Once maximized already, top dock shinks back, but the session PageControl,
  4700. // do not align up, leaving space between TopDock and PageControl.
  4701. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4702. for (int Index = 0; Index < ControlCount; Index++)
  4703. {
  4704. RealignControl(Controls[Index]);
  4705. }
  4706. if (FTrayIcon->Visible)
  4707. {
  4708. FTrayIcon->Visible = false;
  4709. if (Visible)
  4710. {
  4711. ShowWindow(Handle, SW_SHOW);
  4712. }
  4713. }
  4714. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4715. {
  4716. FNeedSession = false;
  4717. NonVisualDataModule->StartBusy();
  4718. try
  4719. {
  4720. NeedSession(true);
  4721. }
  4722. __finally
  4723. {
  4724. NonVisualDataModule->EndBusy();
  4725. }
  4726. }
  4727. }
  4728. //---------------------------------------------------------------------------
  4729. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4730. {
  4731. FTrayIcon->Hint = GetMainForm()->Caption;
  4732. }
  4733. //---------------------------------------------------------------------------
  4734. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4735. {
  4736. UpdateTrayIcon();
  4737. }
  4738. //---------------------------------------------------------------------------
  4739. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4740. {
  4741. // workaround
  4742. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4743. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4744. // But WM_ACTIVATEAPP message can be received even when
  4745. // the main window is minimized to the tray and internal editor window is focused
  4746. // (after another application was previously active)
  4747. if (::IsIconic(Handle))
  4748. {
  4749. if (!IsAppIconic())
  4750. {
  4751. SetAppIconic(true);
  4752. }
  4753. }
  4754. ::ApplicationRestore();
  4755. Application->BringToFront();
  4756. }
  4757. //---------------------------------------------------------------------------
  4758. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4759. {
  4760. RestoreApp();
  4761. }
  4762. //---------------------------------------------------------------------------
  4763. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4764. {
  4765. if (OpenInNewWindow())
  4766. {
  4767. ExecuteNewInstance(SessionUrl);
  4768. }
  4769. else
  4770. {
  4771. TTerminalManager::Instance()->NewSession(SessionUrl);
  4772. }
  4773. }
  4774. //---------------------------------------------------------------------------
  4775. void TCustomScpExplorerForm::NewTab(TOperationSide)
  4776. {
  4777. NewSession();
  4778. }
  4779. //---------------------------------------------------------------------------
  4780. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4781. {
  4782. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4783. StoredSessions->NewSession(SessionName, SessionData);
  4784. // modified only, explicit
  4785. StoredSessions->Save(false, true);
  4786. // encode session name because of slashes in hierarchical sessions
  4787. return EncodeUrlString(SessionName);
  4788. }
  4789. //---------------------------------------------------------------------------
  4790. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4791. {
  4792. // current working directories become defaults here, what is not right
  4793. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4794. return SaveHiddenDuplicateSession(SessionData.get());
  4795. }
  4796. //---------------------------------------------------------------------------
  4797. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4798. {
  4799. if (OpenInNewWindow())
  4800. {
  4801. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4802. }
  4803. else
  4804. {
  4805. // current working directories become defaults here, what is not right
  4806. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4807. TTerminalManager * Manager = TTerminalManager::Instance();
  4808. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4809. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4810. // We definitelly want these
  4811. ASession->Disconnected = ManagedSession->Disconnected;
  4812. // Not sure about these two
  4813. ASession->Permanent = ManagedSession->Permanent;
  4814. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4815. Manager->ActiveSession = ASession;
  4816. }
  4817. }
  4818. //---------------------------------------------------------------------------
  4819. void __fastcall TCustomScpExplorerForm::RenameSession()
  4820. {
  4821. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4822. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME) &&
  4823. // When submitting the default name, do not fix it
  4824. (Name != ManagedSession->SessionData->SessionName))
  4825. {
  4826. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4827. TSessionData::ValidateName(Name);
  4828. // This is inconsistent with how color (for example) is handled.
  4829. ManagedSession->SessionData->Name = Name;
  4830. UpdateControls();
  4831. // Add/Remove distinguishing paths from sessions of the same name.
  4832. SessionListChanged();
  4833. }
  4834. }
  4835. //---------------------------------------------------------------------------
  4836. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4837. {
  4838. DebugAssert(Queue != NULL);
  4839. bool Result = Queue->IsEmpty;
  4840. if (!Result)
  4841. {
  4842. SetFocus();
  4843. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4844. }
  4845. return Result;
  4846. }
  4847. //---------------------------------------------------------------------------
  4848. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4849. {
  4850. return CanCloseQueue(FQueue);
  4851. }
  4852. //---------------------------------------------------------------------------
  4853. void __fastcall TCustomScpExplorerForm::CloseSession()
  4854. {
  4855. if (CanCloseQueue())
  4856. {
  4857. TTerminalManager::Instance()->FreeActiveTerminal();
  4858. }
  4859. }
  4860. //---------------------------------------------------------------------------
  4861. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4862. {
  4863. if (CanCloseQueue())
  4864. {
  4865. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4866. }
  4867. }
  4868. //---------------------------------------------------------------------------
  4869. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4870. {
  4871. DetachTerminal(Terminal);
  4872. RemoteDirView->Terminal = Terminal;
  4873. UpdateRemotePathComboBox(false);
  4874. InitStatusBar();
  4875. UpdateControls();
  4876. }
  4877. //---------------------------------------------------------------------------
  4878. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4879. {
  4880. InitStatusBar();
  4881. }
  4882. //---------------------------------------------------------------------------
  4883. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4884. {
  4885. if (DebugAlwaysTrue(!Terminal->Active))
  4886. {
  4887. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4888. }
  4889. }
  4890. //---------------------------------------------------------------------------
  4891. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4892. {
  4893. if (OpenInNewWindow())
  4894. {
  4895. // encode session name because of slashes in hierarchical sessions
  4896. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4897. }
  4898. else
  4899. {
  4900. TTerminalManager * Manager = TTerminalManager::Instance();
  4901. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4902. Manager->ActiveSession = Terminal;
  4903. }
  4904. }
  4905. //---------------------------------------------------------------------------
  4906. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  4907. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  4908. {
  4909. TTerminalManager * Manager = TTerminalManager::Instance();
  4910. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4911. // This asks for a master password. Maybe we should postpone asking until a connection is actually opened.
  4912. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4913. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  4914. {
  4915. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  4916. // FirstSession can be null, if some of the
  4917. if (!ConnectFirstTerminal && (FirstSession != NULL))
  4918. {
  4919. FirstSession->Disconnected = true;
  4920. FirstSession->DisconnectedTemporarily = true;
  4921. }
  4922. Manager->ActiveSession = FirstSession;
  4923. }
  4924. }
  4925. //---------------------------------------------------------------------------
  4926. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4927. {
  4928. if (OpenInNewWindow())
  4929. {
  4930. ExecuteNewInstance(Name);
  4931. }
  4932. else
  4933. {
  4934. DoOpenFolderOrWorkspace(Name, true, false);
  4935. }
  4936. }
  4937. //---------------------------------------------------------------------------
  4938. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4939. bool &CanClose)
  4940. {
  4941. // See the comment in CloseApp()
  4942. if (NonVisualDataModule->Busy)
  4943. {
  4944. CanClose = false;
  4945. }
  4946. else
  4947. {
  4948. TTerminalManager * Manager = TTerminalManager::Instance();
  4949. int ActiveSessions = 0;
  4950. for (int Index = 0; Index < Manager->Count; Index++)
  4951. {
  4952. if (Manager->Sessions[Index]->Active)
  4953. {
  4954. ActiveSessions++;
  4955. }
  4956. }
  4957. // Confirm, when there's at least one active remote session or
  4958. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4959. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4960. WinConfiguration->ConfirmClosingSession)
  4961. {
  4962. unsigned int Result;
  4963. TMessageParams Params(mpNeverAskAgainCheck);
  4964. UnicodeString Message;
  4965. int Answers = qaOK | qaCancel;
  4966. // The current session is the only active session
  4967. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4968. {
  4969. if (!WinConfiguration->AutoSaveWorkspace)
  4970. {
  4971. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4972. }
  4973. else
  4974. {
  4975. Message = LoadStr(CLOSE_SESSION);
  4976. }
  4977. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4978. }
  4979. // Multiple active sessions or one active session, but it's not the current one
  4980. else if (ActiveSessions > 0)
  4981. {
  4982. if (!WinConfiguration->AutoSaveWorkspace)
  4983. {
  4984. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4985. }
  4986. else
  4987. {
  4988. Message = LoadStr(CLOSE_SESSIONS);
  4989. }
  4990. }
  4991. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  4992. else
  4993. {
  4994. DebugAlwaysTrue(Manager->Count > 1);
  4995. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  4996. Message = LoadStr(CLOSE_WORKSPACE);
  4997. }
  4998. UnicodeString Note;
  4999. if (WinConfiguration->AutoSaveWorkspace)
  5000. {
  5001. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  5002. }
  5003. else
  5004. {
  5005. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  5006. Answers = qaYes | qaNo | qaCancel;
  5007. }
  5008. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  5009. SetFocus();
  5010. Result = MessageDialog(Message, qtConfirmation,
  5011. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  5012. if (Result == qaNeverAskAgain)
  5013. {
  5014. WinConfiguration->ConfirmClosingSession = false;
  5015. }
  5016. if (Result == qaNo)
  5017. {
  5018. CanClose = SaveWorkspace(true);
  5019. // note that the workspace will be saved redundatly again from FormClose
  5020. }
  5021. else
  5022. {
  5023. CanClose =
  5024. (Result == qaOK) ||
  5025. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  5026. (Result == qaNeverAskAgain);
  5027. }
  5028. }
  5029. if (CanClose && HasActiveTerminal())
  5030. {
  5031. CanClose = CanCloseQueue();
  5032. }
  5033. }
  5034. if (CanClose)
  5035. {
  5036. CanClose =
  5037. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  5038. FEditorManager->CloseExternalFilesWithoutProcess();
  5039. if (CanClose)
  5040. {
  5041. while (CanClose && (FLocalEditors->Count > 0))
  5042. {
  5043. int PrevCount = FLocalEditors->Count;
  5044. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  5045. CanClose = (FLocalEditors->Count < PrevCount);
  5046. }
  5047. if (CanClose)
  5048. {
  5049. CanClose = FEditorManager->Empty(true);
  5050. if (!CanClose)
  5051. {
  5052. SetFocus();
  5053. CanClose =
  5054. (MessageDialog(
  5055. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  5056. }
  5057. }
  5058. }
  5059. }
  5060. }
  5061. //---------------------------------------------------------------------------
  5062. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  5063. {
  5064. TForm * Form = dynamic_cast<TForm *>(Sender);
  5065. DebugAssert(Form != NULL);
  5066. Form->Close();
  5067. }
  5068. //---------------------------------------------------------------------------
  5069. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  5070. {
  5071. TForm * Form = dynamic_cast<TForm *>(Sender);
  5072. delete Form;
  5073. }
  5074. //---------------------------------------------------------------------------
  5075. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  5076. {
  5077. while (FLocalEditors->Count > 0)
  5078. {
  5079. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  5080. }
  5081. }
  5082. //---------------------------------------------------------------------------
  5083. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  5084. TObject *Sender)
  5085. {
  5086. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  5087. try
  5088. {
  5089. SetProperties(osRemote, FileList);
  5090. }
  5091. __finally
  5092. {
  5093. delete FileList;
  5094. }
  5095. }
  5096. //---------------------------------------------------------------------------
  5097. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  5098. {
  5099. if (value)
  5100. {
  5101. if (Component == fcCustomCommandsBand)
  5102. {
  5103. UpdateCustomCommandsToolbar();
  5104. }
  5105. if (Component == fcQueueFileList)
  5106. {
  5107. DebugAssert(!QueueFileListSplitter->Visible);
  5108. AdjustQueueLayout();
  5109. UpdateQueueFileList();
  5110. }
  5111. }
  5112. }
  5113. //---------------------------------------------------------------------------
  5114. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  5115. {
  5116. int MinHeightLimit =
  5117. GetStaticQueuePanelComponentsHeight() +
  5118. QueueFileListSplitter->Height +
  5119. QueueFileList->Height +
  5120. GetMinQueueViewHeight();
  5121. if (QueuePanel->ClientHeight < MinHeightLimit)
  5122. {
  5123. QueuePanel->ClientHeight = MinHeightLimit;
  5124. }
  5125. }
  5126. //---------------------------------------------------------------------------
  5127. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  5128. {
  5129. TControl * Control = GetComponent(Component);
  5130. DebugAssert(Control);
  5131. bool Changed = (Control->Visible != value);
  5132. if (Changed)
  5133. {
  5134. ComponentShowing(Component, value);
  5135. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  5136. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  5137. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  5138. if (value)
  5139. {
  5140. int RemainingHeight = Control->Parent->ClientHeight;
  5141. int RemainingWidth = Control->Parent->ClientWidth;
  5142. for (int i = 0; i < Control->Parent->ControlCount; i++)
  5143. {
  5144. TControl * ChildControl = Control->Parent->Controls[i];
  5145. if (ChildControl->Visible)
  5146. {
  5147. switch (ChildControl->Align)
  5148. {
  5149. case alTop:
  5150. case alBottom:
  5151. RemainingHeight -= ChildControl->Height;
  5152. break;
  5153. case alLeft:
  5154. case alRight:
  5155. RemainingWidth -= ChildControl->Width;
  5156. break;
  5157. }
  5158. }
  5159. }
  5160. int Reserve = ScaleByTextHeight(this, 32);
  5161. // queue in explorer, trees in commander
  5162. if (Control->Height > RemainingHeight - Reserve)
  5163. {
  5164. Control->Height = RemainingHeight / 2;
  5165. }
  5166. if (Control->Width > RemainingWidth - Reserve)
  5167. {
  5168. Control->Width = RemainingWidth / 2;
  5169. }
  5170. }
  5171. Control->Visible = value;
  5172. if (WasFocused && Visible)
  5173. {
  5174. DirView(osCurrent)->SetFocus();
  5175. }
  5176. FixControlsPlacement();
  5177. }
  5178. }
  5179. //---------------------------------------------------------------------------
  5180. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  5181. {
  5182. TControl * Control = GetComponent(Component);
  5183. if (Control == NULL)
  5184. {
  5185. return false;
  5186. }
  5187. else
  5188. {
  5189. return Control->Visible;
  5190. }
  5191. }
  5192. //---------------------------------------------------------------------------
  5193. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  5194. {
  5195. bool Result;
  5196. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  5197. {
  5198. Result = !IsUWP();
  5199. }
  5200. else
  5201. {
  5202. Result = true;
  5203. }
  5204. return Result;
  5205. }
  5206. //---------------------------------------------------------------------------
  5207. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  5208. {
  5209. if (DirView->ItemFocused != NULL)
  5210. {
  5211. DirView->ItemFocused->MakeVisible(false);
  5212. }
  5213. }
  5214. //---------------------------------------------------------------------------
  5215. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5216. {
  5217. MakeFocusedItemVisible(RemoteDirView);
  5218. QueueSplitter->Visible = QueuePanel->Visible;
  5219. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5220. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5221. TControl * TopControlsOrder[] =
  5222. { TopDock, MessageDock, SessionsPageControl };
  5223. SetVerticalControlsOrder(TopControlsOrder, LENOF(TopControlsOrder));
  5224. TControl * QueueControlsOrder[] =
  5225. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5226. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5227. }
  5228. //---------------------------------------------------------------------------
  5229. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5230. {
  5231. switch (Component) {
  5232. case fcStatusBar: return RemoteStatusBar;
  5233. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5234. case fcQueueView: return QueuePanel;
  5235. case fcQueueToolbar: return QueueDock;
  5236. case fcRemoteTree: return RemoteDrivePanel;
  5237. case fcSessionsTabs: return SessionsPageControl;
  5238. case fcQueueFileList: return QueueFileList;
  5239. default: return NULL;
  5240. }
  5241. }
  5242. //---------------------------------------------------------------------------
  5243. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5244. TObject *Sender, TListColumn *Column, TPoint &Point)
  5245. {
  5246. DebugAssert(NonVisualDataModule && Column && Sender);
  5247. NonVisualDataModule->ListColumn = Column;
  5248. TPopupMenu * DirViewColumnMenu;
  5249. if (Sender == DirView(osOther))
  5250. {
  5251. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5252. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible = (Column->Index == uvName);
  5253. bool SizeColumn = (Column->Index == uvSize);
  5254. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible = SizeColumn;
  5255. NonVisualDataModule->RemoteCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5256. }
  5257. else
  5258. {
  5259. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5260. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible = (Column->Index == dvName);
  5261. bool SizeColumn = (Column->Index == dvSize);
  5262. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible = SizeColumn;
  5263. NonVisualDataModule->LocalCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5264. }
  5265. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5266. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5267. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5268. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5269. }
  5270. //---------------------------------------------------------------------------
  5271. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5272. TObject *Sender, TListItem *Item, bool &AllowExec)
  5273. {
  5274. DoDirViewExecFile(Sender, Item, AllowExec);
  5275. }
  5276. //---------------------------------------------------------------------------
  5277. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5278. TListItem * Item, bool & AllowExec)
  5279. {
  5280. DebugAssert(Sender && Item && Configuration);
  5281. DebugAssert(AllowExec);
  5282. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5283. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5284. bool Remote = !IsSideLocalBrowser(Side);
  5285. DebugAssert(Item == ADirView->ItemFocused);
  5286. bool IsDirectory = ADirView->ItemIsDirectory(Item);
  5287. TResolvedDoubleClickAction Action;
  5288. if (FForceExecution)
  5289. {
  5290. Action = IsDirectory ? rdcaChangeDir : rdcaOpen;
  5291. }
  5292. else
  5293. {
  5294. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  5295. Action = WinConfiguration->ResolveDoubleClickAction(IsDirectory, ATerminal);
  5296. }
  5297. if (Action == rdcaCopy)
  5298. {
  5299. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5300. UnlockWindow();
  5301. try
  5302. {
  5303. if (IsLocalBrowserMode())
  5304. {
  5305. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5306. }
  5307. else
  5308. {
  5309. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5310. }
  5311. }
  5312. __finally
  5313. {
  5314. LockWindow();
  5315. }
  5316. AllowExec = false;
  5317. }
  5318. else if (Action == rdcaEdit)
  5319. {
  5320. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5321. {
  5322. ExecuteFile(Side, efDefaultEditor);
  5323. AllowExec = false;
  5324. }
  5325. }
  5326. else if (Action == rdcaOpen)
  5327. {
  5328. if (DebugAlwaysTrue(!IsDirectory) && Remote && !WinConfiguration->DisableOpenEdit)
  5329. {
  5330. ExecuteFile(osRemote, efShell);
  5331. AllowExec = false;
  5332. }
  5333. }
  5334. }
  5335. //---------------------------------------------------------------------------
  5336. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5337. {
  5338. return ((Side == osRemote) || (Side == osCurrent));
  5339. }
  5340. //---------------------------------------------------------------------------
  5341. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5342. {
  5343. DebugFail();
  5344. }
  5345. //---------------------------------------------------------------------------
  5346. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5347. {
  5348. DebugFail();
  5349. }
  5350. //---------------------------------------------------------------------------
  5351. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5352. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5353. {
  5354. TSynchronizeParamType Params;
  5355. Params.LocalDirectory = LocalDirectory;
  5356. Params.RemoteDirectory = RemoteDirectory;
  5357. int UnusedParams =
  5358. (GUIConfiguration->SynchronizeParams &
  5359. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5360. TTerminal::spCaseSensitive));
  5361. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5362. Params.Options = GUIConfiguration->SynchronizeOptions;
  5363. bool SaveSettings = false;
  5364. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5365. &DoSynchronizeTooManyDirectories);
  5366. DebugAssert(FSynchronizeController == NULL);
  5367. FSynchronizeController = &Controller;
  5368. bool Result;
  5369. try
  5370. {
  5371. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5372. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5373. int Options =
  5374. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5375. DebugAssert(FOnFeedSynchronizeError == NULL);
  5376. DebugAssert(FOnSynchronizeAbort == NULL);
  5377. DebugAssert(FSynchronizeTerminal == NULL);
  5378. FSynchronizeTerminal = Terminal;
  5379. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5380. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5381. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5382. if (Result)
  5383. {
  5384. if (SaveSettings)
  5385. {
  5386. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5387. GUIConfiguration->SynchronizeOptions = Params.Options;
  5388. }
  5389. else
  5390. {
  5391. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5392. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5393. FLAGSET(Params.Options, soSynchronize))
  5394. {
  5395. GUIConfiguration->SynchronizeOptions =
  5396. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5397. soSynchronize;
  5398. }
  5399. }
  5400. LocalDirectory = Params.LocalDirectory;
  5401. RemoteDirectory = Params.RemoteDirectory;
  5402. }
  5403. }
  5404. __finally
  5405. {
  5406. FSynchronizeController = NULL;
  5407. DebugAssert(FOnFeedSynchronizeError == NULL);
  5408. FOnFeedSynchronizeError = NULL;
  5409. DebugAssert(FOnSynchronizeAbort == NULL);
  5410. FOnSynchronizeAbort = NULL;
  5411. FSynchronizeTerminal = NULL;
  5412. }
  5413. return Result;
  5414. }
  5415. //---------------------------------------------------------------------------
  5416. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5417. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5418. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5419. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5420. TSynchronizeOptions * Options, bool Full)
  5421. {
  5422. // Keep-up-to-date mode
  5423. if (Terminal->Status == ssOpened)
  5424. {
  5425. try
  5426. {
  5427. int PParams = Params.Params;
  5428. if (!Full)
  5429. {
  5430. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5431. if (FLAGSET(Params.Options, soRecurse))
  5432. {
  5433. PParams |= TTerminal::spSubDirs;
  5434. }
  5435. }
  5436. else
  5437. {
  5438. // if keepuptodate is non-recursive,
  5439. // full sync before has to be non-recursive as well
  5440. if (FLAGCLEAR(Params.Options, soRecurse))
  5441. {
  5442. PParams |= TTerminal::spNoRecurse;
  5443. }
  5444. }
  5445. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5446. PParams, Checklist, Options);
  5447. }
  5448. catch (Exception & E)
  5449. {
  5450. if (!Terminal->Active)
  5451. {
  5452. ShowExtendedExceptionEx(Terminal, &E);
  5453. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5454. if (!Terminal->Active)
  5455. {
  5456. throw;
  5457. }
  5458. }
  5459. else
  5460. {
  5461. if (FLAGSET(Params.Options, soContinueOnError) &&
  5462. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5463. {
  5464. // noop, already logged in MoreMessageDialog
  5465. }
  5466. else
  5467. {
  5468. // We get mostly EAbort here, so this is noop.
  5469. // Exception is an error when listing directories while looking for differences.
  5470. ShowExtendedExceptionEx(Terminal, &E);
  5471. throw;
  5472. }
  5473. }
  5474. }
  5475. }
  5476. }
  5477. //---------------------------------------------------------------------------
  5478. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5479. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5480. const UnicodeString ErrorStr)
  5481. {
  5482. if (!Directory.IsEmpty())
  5483. {
  5484. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5485. }
  5486. else
  5487. {
  5488. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5489. }
  5490. }
  5491. //---------------------------------------------------------------------------
  5492. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5493. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5494. {
  5495. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5496. {
  5497. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5498. }
  5499. else
  5500. {
  5501. TMessageParams Params(mpNeverAskAgainCheck);
  5502. unsigned int Result = MessageDialog(
  5503. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5504. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5505. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5506. {
  5507. MaxDirectories *= 2;
  5508. if (Result == qaNeverAskAgain)
  5509. {
  5510. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5511. }
  5512. }
  5513. else
  5514. {
  5515. Abort();
  5516. }
  5517. }
  5518. }
  5519. //---------------------------------------------------------------------------
  5520. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5521. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5522. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5523. TSynchronizeOptions * Options)
  5524. {
  5525. // Keep-up-to-date mode
  5526. DebugAssert(!FAutoOperation);
  5527. void * BatchStorage;
  5528. BatchStart(BatchStorage);
  5529. FAutoOperation = true;
  5530. bool AnyOperation = false;
  5531. TDateTime StartTime = Now();
  5532. TSynchronizeChecklist * AChecklist = NULL;
  5533. try
  5534. {
  5535. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5536. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5537. {
  5538. FSynchronizeProgressForm->Start();
  5539. }
  5540. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5541. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5542. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5543. Options);
  5544. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5545. SAFE_DESTROY(FSynchronizeProgressForm);
  5546. AnyOperation = (AChecklist->CheckedCount > 0);
  5547. if (AnyOperation)
  5548. {
  5549. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5550. CreateProgressForm(&SynchronizeProgress);
  5551. try
  5552. {
  5553. Terminal->SynchronizeApply(
  5554. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5555. SynchronizeProcessedItem, NULL, NULL, NULL);
  5556. }
  5557. __finally
  5558. {
  5559. DestroyProgressForm();
  5560. }
  5561. }
  5562. }
  5563. __finally
  5564. {
  5565. if (Checklist == NULL)
  5566. {
  5567. delete AChecklist;
  5568. }
  5569. else
  5570. {
  5571. *Checklist = AChecklist;
  5572. }
  5573. FAutoOperation = false;
  5574. SAFE_DESTROY(FSynchronizeProgressForm);
  5575. BatchEnd(BatchStorage);
  5576. ReloadLocalDirectory();
  5577. if (AnyOperation)
  5578. {
  5579. OperationComplete(StartTime);
  5580. }
  5581. }
  5582. }
  5583. //---------------------------------------------------------------------------
  5584. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5585. {
  5586. // can be called from command line
  5587. return Visible &&
  5588. ((DirView(osRemote)->SelCount > 0) ||
  5589. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5590. }
  5591. //---------------------------------------------------------------------------
  5592. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5593. {
  5594. LogSynchronizeEvent(Terminal, Message);
  5595. }
  5596. //---------------------------------------------------------------------------
  5597. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5598. int Params, TSynchronizeOptions & Options)
  5599. {
  5600. DebugAssert(!IsLocalBrowserMode());
  5601. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5602. {
  5603. Options.Filter = new TStringList();
  5604. Options.Filter->CaseSensitive = false;
  5605. Options.Filter->Duplicates = Types::dupAccept;
  5606. if (DirView(osRemote)->SelCount > 0)
  5607. {
  5608. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5609. }
  5610. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5611. {
  5612. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5613. }
  5614. Options.Filter->Sort();
  5615. }
  5616. }
  5617. //---------------------------------------------------------------------------
  5618. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5619. {
  5620. TCopyParamType Defaults;
  5621. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5622. ConfigStorage->AccessMode = smRead;
  5623. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5624. {
  5625. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5626. }
  5627. ConfigStorage.reset(NULL);
  5628. std::unique_ptr<TStringList> Options(new TStringList());
  5629. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5630. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5631. UnicodeString Result;
  5632. if (Options->Count > 0)
  5633. {
  5634. Result =
  5635. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5636. }
  5637. return Result;
  5638. }
  5639. //---------------------------------------------------------------------------
  5640. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5641. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5642. {
  5643. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5644. UnicodeString AdditionalParams =
  5645. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5646. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5647. SerializeCopyParamForCommandLine(CopyParams),
  5648. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5649. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5650. ExecuteNewInstance(SessionName, AdditionalParams);
  5651. }
  5652. //---------------------------------------------------------------------------
  5653. struct TSynchronizeParams
  5654. {
  5655. UnicodeString LocalDirectory;
  5656. UnicodeString RemoteDirectory;
  5657. TSynchronizeMode Mode;
  5658. TSynchronizeChecklist * Checklist;
  5659. int Params;
  5660. TCopyParamType * CopyParam;
  5661. TDateTime CollectElapsed;
  5662. TDateTime * StartTime;
  5663. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5664. };
  5665. //---------------------------------------------------------------------------
  5666. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5667. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5668. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5669. {
  5670. DebugAssert(!FAutoOperation);
  5671. void * BatchStorage;
  5672. BatchStart(BatchStorage);
  5673. FAutoOperation = true;
  5674. TFileOperationStatistics Statistics;
  5675. TDateTime Start = Now();
  5676. try
  5677. {
  5678. Params.OnProcessedItem = OnProcessedItem;
  5679. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5680. CreateProgressForm(&SynchronizeProgress);
  5681. Terminal->SynchronizeApply(
  5682. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5683. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5684. &Statistics);
  5685. }
  5686. __finally
  5687. {
  5688. Params.OnProcessedItem = NULL;
  5689. FAutoOperation = false;
  5690. DestroyProgressForm();
  5691. BatchEnd(BatchStorage);
  5692. ReloadLocalDirectory();
  5693. }
  5694. if (WinConfiguration->SynchronizeSummary)
  5695. {
  5696. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5697. // The statistics should be 0 anyway in this case
  5698. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5699. {
  5700. Message += L"\n";
  5701. if (Statistics.FilesUploaded > 0)
  5702. {
  5703. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5704. }
  5705. if (Statistics.FilesDownloaded > 0)
  5706. {
  5707. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5708. }
  5709. if (Statistics.FilesDeletedLocal > 0)
  5710. {
  5711. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5712. }
  5713. if (Statistics.FilesDeletedRemote > 0)
  5714. {
  5715. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5716. }
  5717. }
  5718. TDateTime Elapsed = (Now() - Start);
  5719. Message +=
  5720. L"\n" +
  5721. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5722. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5723. TMessageParams Params(mpNeverAskAgainCheck);
  5724. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5725. if (Result == qaNeverAskAgain)
  5726. {
  5727. WinConfiguration->SynchronizeSummary = false;
  5728. }
  5729. }
  5730. }
  5731. //---------------------------------------------------------------------------
  5732. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5733. {
  5734. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5735. {
  5736. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5737. }
  5738. // Not set in keep-up-to-date mode - Synchronize() method
  5739. if (Token != NULL)
  5740. {
  5741. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5742. if (Params.OnProcessedItem != NULL)
  5743. {
  5744. Params.OnProcessedItem(NULL, ChecklistItem);
  5745. }
  5746. }
  5747. }
  5748. //---------------------------------------------------------------------------
  5749. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5750. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5751. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5752. {
  5753. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5754. *Params.StartTime = Now();
  5755. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5756. }
  5757. //---------------------------------------------------------------------------
  5758. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5759. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5760. {
  5761. // terminal can be already closed (e.g. dropped connection)
  5762. if (Terminal != NULL)
  5763. {
  5764. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5765. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5766. }
  5767. }
  5768. //---------------------------------------------------------------------------
  5769. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5770. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5771. {
  5772. DebugAssert(!IsLocalBrowserMode());
  5773. TAutoBatch AutoBatch(this);
  5774. TAutoFlag AutoOperationFlag(FAutoOperation);
  5775. if (Side == osRemote)
  5776. {
  5777. std::unique_ptr<TStrings> FileList(new TStringList());
  5778. FileList->AddObject(FileName, RemoteFile);
  5779. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5780. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5781. RemoteDirView->SaveSelection();
  5782. RemoteDirView->SaveSelectedNames();
  5783. try
  5784. {
  5785. Terminal->MoveFiles(FileList.get(), Target, FileMask, false);
  5786. }
  5787. catch(...)
  5788. {
  5789. RemoteDirView->DiscardSavedSelection();
  5790. throw;
  5791. }
  5792. RemoteDirView->RestoreSelection();
  5793. }
  5794. else if (DebugAlwaysTrue(Side == osLocal))
  5795. {
  5796. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5797. {
  5798. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5799. }
  5800. UnicodeString Directory = ExtractFileDir(FileName);
  5801. ReloadLocalDirectory(Directory);
  5802. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5803. if (!SamePaths(Directory, NewDirectory))
  5804. {
  5805. ReloadLocalDirectory(NewDirectory);
  5806. }
  5807. }
  5808. }
  5809. //---------------------------------------------------------------------------
  5810. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5811. {
  5812. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5813. if (Side == osLocal)
  5814. {
  5815. if (Action == TSynchronizeChecklist::saDownloadNew)
  5816. {
  5817. OpenFolderInExplorer(LocalPath);
  5818. }
  5819. else
  5820. {
  5821. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5822. }
  5823. }
  5824. else if (DebugAlwaysTrue(Side == osRemote))
  5825. {
  5826. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5827. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5828. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5829. if (!LocalPath.IsEmpty())
  5830. {
  5831. SessionData->LocalDirectory = LocalPath;
  5832. }
  5833. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5834. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5835. }
  5836. }
  5837. //---------------------------------------------------------------------------
  5838. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5839. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5840. const TCopyParamType * CopyParams)
  5841. {
  5842. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5843. UnicodeString AdditionalParams =
  5844. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5845. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5846. SerializeCopyParamForCommandLine(CopyParams),
  5847. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5848. LocalDirectory, RemoteDirectory, Mode, Params));
  5849. ExecuteNewInstance(SessionName, AdditionalParams);
  5850. }
  5851. //---------------------------------------------------------------------------
  5852. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5853. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5854. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5855. {
  5856. int Result;
  5857. bool SaveSettings = false;
  5858. int Options =
  5859. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5860. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5861. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5862. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5863. bool Continue =
  5864. (UseDefaults == 0) ||
  5865. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5866. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5867. if (Continue)
  5868. {
  5869. Configuration->Usage->Inc(L"Synchronizations");
  5870. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  5871. UpdateCopyParamCounters(CopyParam);
  5872. TSynchronizeOptions SynchronizeOptions;
  5873. GetSynchronizeOptions(Params, SynchronizeOptions);
  5874. if (SaveSettings)
  5875. {
  5876. GUIConfiguration->SynchronizeParams = Params;
  5877. }
  5878. else
  5879. {
  5880. SaveMode = false;
  5881. }
  5882. TDateTime StartTime = Now();
  5883. TSynchronizeChecklist * Checklist = NULL;
  5884. try
  5885. {
  5886. DebugAssert(!FAutoOperation);
  5887. FAutoOperation = true;
  5888. try
  5889. {
  5890. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5891. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5892. int Files = -1;
  5893. if (DataList->Count >= 1)
  5894. {
  5895. Files = StrToIntDef(DataList->Strings[0], Files);
  5896. }
  5897. else
  5898. {
  5899. DataList->Add(UnicodeString());
  5900. }
  5901. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5902. FSynchronizeProgressForm->Start();
  5903. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5904. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5905. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5906. &SynchronizeOptions);
  5907. if (Terminal->SessionData->CacheDirectories)
  5908. {
  5909. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5910. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5911. }
  5912. }
  5913. __finally
  5914. {
  5915. FAutoOperation = false;
  5916. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5917. SAFE_DESTROY(FSynchronizeProgressForm);
  5918. }
  5919. TSynchronizeParams SynchronizeParams;
  5920. SynchronizeParams.LocalDirectory = LocalDirectory;
  5921. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5922. SynchronizeParams.Mode = Mode;
  5923. SynchronizeParams.CopyParam = &CopyParam;
  5924. SynchronizeParams.Params = Params;
  5925. SynchronizeParams.Checklist = Checklist;
  5926. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5927. SynchronizeParams.StartTime = &StartTime;
  5928. SynchronizeParams.OnProcessedItem = NULL;
  5929. Result = Checklist->Count;
  5930. if (Checklist->Count > 0)
  5931. {
  5932. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5933. {
  5934. if (!DoSynchronizeChecklistDialog(
  5935. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5936. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5937. {
  5938. Result = -1;
  5939. }
  5940. }
  5941. else
  5942. {
  5943. FullSynchronize(SynchronizeParams, NULL, NULL);
  5944. }
  5945. }
  5946. else
  5947. {
  5948. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5949. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5950. }
  5951. }
  5952. __finally
  5953. {
  5954. delete Checklist;
  5955. }
  5956. OperationComplete(StartTime);
  5957. }
  5958. else
  5959. {
  5960. Result = -1;
  5961. }
  5962. return Result;
  5963. }
  5964. //---------------------------------------------------------------------------
  5965. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5966. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5967. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5968. {
  5969. if (Collect)
  5970. {
  5971. DebugAssert(FSynchronizeProgressForm != NULL);
  5972. if (!FSynchronizeProgressForm->Started)
  5973. {
  5974. FSynchronizeProgressForm->Start();
  5975. }
  5976. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5977. SetTaskbarListProgressValue(CompareProgress);
  5978. }
  5979. else
  5980. {
  5981. DebugAssert(FSynchronizeProgressForm == NULL);
  5982. DebugAssert(FProgressForm != NULL);
  5983. }
  5984. }
  5985. //---------------------------------------------------------------------------
  5986. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5987. {
  5988. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  5989. TRemoteFile * File = NULL;
  5990. Terminal->ReadFile(FullFileName, File);
  5991. if (File != NULL)
  5992. {
  5993. std::unique_ptr<TRemoteFile> FileOwner(File);
  5994. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5995. Application->ShowMainForm = false;
  5996. Application->Run();
  5997. }
  5998. }
  5999. //---------------------------------------------------------------------------
  6000. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  6001. {
  6002. DebugFail();
  6003. }
  6004. //---------------------------------------------------------------------------
  6005. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  6006. {
  6007. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  6008. SessionData->Assign(ManagedSession->SessionData);
  6009. UpdateSessionData(SessionData.get());
  6010. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  6011. if (IsActiveTerminal(ManagedSession))
  6012. {
  6013. ManagedSession->UpdateSessionCredentials(SessionData.get());
  6014. }
  6015. if (!ManagedSession->SessionData->HasSessionName())
  6016. {
  6017. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  6018. // once IsWorkspace is cleared
  6019. SessionData->Name = UnicodeString();
  6020. }
  6021. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  6022. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  6023. SessionData->IsWorkspace = false;
  6024. return SessionData.release();
  6025. }
  6026. //---------------------------------------------------------------------------
  6027. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  6028. {
  6029. TSessionData * SessionData = CloneCurrentSessionData();
  6030. try
  6031. {
  6032. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  6033. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  6034. }
  6035. __finally
  6036. {
  6037. delete SessionData;
  6038. }
  6039. }
  6040. //---------------------------------------------------------------------------
  6041. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  6042. {
  6043. TTerminalManager * Manager = TTerminalManager::Instance();
  6044. std::unique_ptr<TObjectList> DataList(new TObjectList());
  6045. if (DebugAlwaysTrue(ManagedSession != NULL))
  6046. {
  6047. // Update ManagedSession->StateData
  6048. UpdateSession(ManagedSession);
  6049. }
  6050. Manager->SaveWorkspace(DataList.get());
  6051. return DataList.release();
  6052. }
  6053. //---------------------------------------------------------------------------
  6054. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  6055. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  6056. {
  6057. WinConfiguration->LastWorkspace = Name;
  6058. if (!SavePasswords)
  6059. {
  6060. for (int Index = 0; Index < DataList->Count; Index++)
  6061. {
  6062. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6063. if (SessionData->Link.IsEmpty())
  6064. {
  6065. SessionData->ClearSessionPasswords();
  6066. }
  6067. }
  6068. }
  6069. StoredSessions->NewWorkspace(Name, DataList);
  6070. // modified only
  6071. StoredSessions->Save(false, Explicit);
  6072. }
  6073. //---------------------------------------------------------------------------
  6074. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  6075. {
  6076. return
  6077. DefaultStr(WinConfiguration->LastWorkspace,
  6078. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  6079. }
  6080. //---------------------------------------------------------------------------
  6081. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  6082. {
  6083. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  6084. bool AnyNonStoredSessionWithPassword = false;
  6085. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  6086. bool AllNonStoredSessionsAnonymous = true;
  6087. TTerminalManager * Manager = TTerminalManager::Instance();
  6088. for (int Index = 0; Index < DataList->Count; Index++)
  6089. {
  6090. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6091. if (SessionData->Link.IsEmpty())
  6092. {
  6093. if (SessionData->HasAnySessionPassword())
  6094. {
  6095. AnyNonStoredSessionWithPassword = true;
  6096. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  6097. {
  6098. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  6099. }
  6100. }
  6101. if (!SameText(SessionData->UserName, AnonymousUserName))
  6102. {
  6103. AllNonStoredSessionsAnonymous = false;
  6104. }
  6105. }
  6106. }
  6107. bool SavePasswords;
  6108. bool * PSavePasswords;
  6109. bool NotRecommendedSavingPasswords =
  6110. !CustomWinConfiguration->UseMasterPassword &&
  6111. !AllNonStoredSessionsAnonymous;
  6112. if (Configuration->DisablePasswordStoring ||
  6113. !AnyNonStoredSessionWithPassword)
  6114. {
  6115. PSavePasswords = NULL;
  6116. SavePasswords = false;
  6117. }
  6118. else
  6119. {
  6120. PSavePasswords = &SavePasswords;
  6121. SavePasswords =
  6122. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  6123. !NotRecommendedSavingPasswords;
  6124. }
  6125. UnicodeString Name = WorkspaceName();
  6126. bool CreateShortcut = false;
  6127. bool Result =
  6128. DoSaveWorkspaceDialog(
  6129. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  6130. EnableAutoSave);
  6131. if (Result)
  6132. {
  6133. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  6134. if (CreateShortcut)
  6135. {
  6136. TOperationVisualizer Visualizer;
  6137. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  6138. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  6139. }
  6140. if (EnableAutoSave)
  6141. {
  6142. WinConfiguration->AutoSaveWorkspace = true;
  6143. WinConfiguration->AutoWorkspace = Name;
  6144. if (PSavePasswords != NULL)
  6145. {
  6146. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  6147. }
  6148. }
  6149. }
  6150. return Result;
  6151. }
  6152. //---------------------------------------------------------------------------
  6153. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  6154. {
  6155. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  6156. DebugAssert(Session == ManagedSession);
  6157. if (!IsLocalBrowserMode())
  6158. {
  6159. SAFE_DESTROY(Session->RemoteExplorerState);
  6160. if (WinConfiguration->PreservePanelState)
  6161. {
  6162. Session->RemoteExplorerState = RemoteDirView->SaveState();
  6163. }
  6164. }
  6165. UpdateSessionData(Session->StateData);
  6166. }
  6167. //---------------------------------------------------------------------------
  6168. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  6169. {
  6170. // Keep in sync with TSessionData::CopyStateData
  6171. DebugAssert(Data != NULL);
  6172. // This is inconsistent with how color (for example) is handled.
  6173. Data->Name = ManagedSession->SessionData->Name;
  6174. if (!IsLocalBrowserMode())
  6175. {
  6176. // cannot use RemoteDirView->Path, because it is empty if connection
  6177. // was already closed
  6178. // also only peek, we may not be connected at all atm
  6179. // so make sure we do not try retrieving current directory from the server
  6180. // (particularly with FTP)
  6181. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  6182. if (!ACurrentDirectory.IsEmpty())
  6183. {
  6184. Data->RemoteDirectory = ACurrentDirectory;
  6185. }
  6186. }
  6187. Data->Color = SessionColor;
  6188. }
  6189. //---------------------------------------------------------------------------
  6190. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  6191. {
  6192. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  6193. DebugAssert(Toolbar != NULL);
  6194. for (int i = 0; i < Toolbar->Items->Count; i++)
  6195. {
  6196. TTBXCustomDropDownItem * DropDownItem;
  6197. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6198. if (DropDownItem != NULL)
  6199. {
  6200. ToolbarItemResize(DropDownItem,
  6201. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  6202. Toolbar->NonClientWidth);
  6203. break;
  6204. }
  6205. }
  6206. }
  6207. //---------------------------------------------------------------------------
  6208. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6209. {
  6210. Item->EditWidth = Width;
  6211. }
  6212. //---------------------------------------------------------------------------
  6213. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6214. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6215. {
  6216. for (int i = 0; i < Toolbar->Items->Count; i++)
  6217. {
  6218. TTBXCustomDropDownItem * DropDownItem;
  6219. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6220. if (DropDownItem != NULL)
  6221. {
  6222. ASize.x -= DropDownItem->EditWidth;
  6223. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  6224. }
  6225. }
  6226. }
  6227. //---------------------------------------------------------------------------
  6228. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6229. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6230. {
  6231. if (WinConfiguration->DDWarnLackOfTempSpace)
  6232. {
  6233. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6234. if (!ADrive.IsEmpty())
  6235. {
  6236. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6237. DebugAssert(RequiredSpace >= 0);
  6238. __int64 RequiredWithReserve;
  6239. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6240. if (FreeSpace < RequiredWithReserve)
  6241. {
  6242. unsigned int Result;
  6243. TMessageParams Params(mpNeverAskAgainCheck);
  6244. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6245. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6246. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6247. if (Result == qaNeverAskAgain)
  6248. {
  6249. WinConfiguration->DDWarnLackOfTempSpace = false;
  6250. }
  6251. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6252. }
  6253. }
  6254. }
  6255. }
  6256. //---------------------------------------------------------------------------
  6257. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6258. {
  6259. DoOpenDirectoryDialog(odAddBookmark, Side);
  6260. }
  6261. //---------------------------------------------------------------------------
  6262. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6263. {
  6264. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6265. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6266. try
  6267. {
  6268. TCustomDirView * DView = DirView(Side);
  6269. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6270. {
  6271. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6272. }
  6273. }
  6274. catch (...)
  6275. {
  6276. delete VisitedDirectories;
  6277. throw;
  6278. }
  6279. return VisitedDirectories;
  6280. }
  6281. //---------------------------------------------------------------------------
  6282. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6283. TOpenDirectoryMode Mode, TOperationSide Side)
  6284. {
  6285. bool Continue = true;
  6286. if (Mode == odAddBookmark)
  6287. {
  6288. TMessageParams Params(mpNeverAskAgainCheck);
  6289. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6290. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6291. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6292. unsigned int Answer =
  6293. MessageDialog(Message,
  6294. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6295. if (Answer == qaNeverAskAgain)
  6296. {
  6297. Continue = true;
  6298. WinConfiguration->UseSharedBookmarks = true;
  6299. }
  6300. else if (Answer == qaOK)
  6301. {
  6302. Continue = true;
  6303. WinConfiguration->UseSharedBookmarks = false;
  6304. }
  6305. else
  6306. {
  6307. Continue = false;
  6308. }
  6309. }
  6310. if (Continue)
  6311. {
  6312. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6313. try
  6314. {
  6315. UnicodeString Name = DirView(Side)->PathName;
  6316. bool Repeat;
  6317. do
  6318. {
  6319. Repeat = false;
  6320. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6321. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6322. // do not allow switching to location profiles,
  6323. // if we are not connected
  6324. HasDirView[osLocal] && (Terminal != NULL)))
  6325. {
  6326. TWindowLock Lock(this);
  6327. if (!TryOpenDirectory(Side, Name))
  6328. {
  6329. Repeat = true;
  6330. Mode = odBrowse;
  6331. }
  6332. }
  6333. }
  6334. while (Repeat);
  6335. }
  6336. __finally
  6337. {
  6338. delete VisitedDirectories;
  6339. }
  6340. }
  6341. }
  6342. //---------------------------------------------------------------------------
  6343. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6344. {
  6345. DebugAssert(!Terminal->CommandSessionOpened);
  6346. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6347. }
  6348. //---------------------------------------------------------------------------
  6349. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6350. {
  6351. bool Result = Terminal->IsCapable[Capability] ||
  6352. Terminal->CommandSessionOpened;
  6353. if (!Result)
  6354. {
  6355. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6356. if (!GUIConfiguration->ConfirmCommandSession)
  6357. {
  6358. Result = true;
  6359. }
  6360. else
  6361. {
  6362. TMessageParams Params(mpNeverAskAgainCheck);
  6363. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6364. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6365. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6366. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6367. if (Answer == qaNeverAskAgain)
  6368. {
  6369. GUIConfiguration->ConfirmCommandSession = false;
  6370. Result = true;
  6371. }
  6372. else if (Answer == qaOK)
  6373. {
  6374. Result = true;
  6375. }
  6376. }
  6377. if (Result)
  6378. {
  6379. Result = CommandSessionFallback();
  6380. }
  6381. }
  6382. return Result;
  6383. }
  6384. //---------------------------------------------------------------------------
  6385. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6386. {
  6387. if (EnsureCommandSessionFallback(fcAnyCommand))
  6388. {
  6389. DoConsoleDialog(Terminal, Command);
  6390. }
  6391. }
  6392. //---------------------------------------------------------------------------
  6393. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6394. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6395. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6396. {
  6397. if (!IsSideLocalBrowser(osRemote) &&
  6398. IsFileControl(DropSourceControl, osRemote) &&
  6399. (FDDExtMapFile != NULL))
  6400. {
  6401. Accept = true;
  6402. }
  6403. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6404. DebugAssert(FDDTargetControl != NULL);
  6405. }
  6406. //---------------------------------------------------------------------------
  6407. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6408. _di_IDataObject DataObj, int KeyState,
  6409. const TPoint & Point, int & Effect, bool & Accept)
  6410. {
  6411. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6412. }
  6413. //---------------------------------------------------------------------------
  6414. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6415. _di_IDataObject DataObj, int KeyState,
  6416. const TPoint & Point, int & Effect, bool & Accept)
  6417. {
  6418. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6419. }
  6420. //---------------------------------------------------------------------------
  6421. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6422. TObject * Sender)
  6423. {
  6424. DebugUsedParam(Sender);
  6425. DebugAssert(FDDTargetControl == Sender);
  6426. FDDTargetControl = NULL;
  6427. }
  6428. //---------------------------------------------------------------------------
  6429. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6430. {
  6431. FileControlDDDragLeave(SessionsPageControl);
  6432. }
  6433. //---------------------------------------------------------------------------
  6434. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6435. {
  6436. FileControlDDDragLeave(QueueView3);
  6437. }
  6438. //---------------------------------------------------------------------------
  6439. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6440. {
  6441. DebugAssert(!IsSideLocalBrowser(Side));
  6442. DebugUsedParam(Side);
  6443. bool Edit = false;
  6444. TRemoteFile * File = NULL;
  6445. UnicodeString FileName;
  6446. UnicodeString PointTo;
  6447. bool SymbolicLink = true;
  6448. if (RemoteDirView->ItemFocused)
  6449. {
  6450. DebugAssert(RemoteDirView->ItemFocused->Data);
  6451. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6452. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6453. if (Edit)
  6454. {
  6455. FileName = File->FileName;
  6456. PointTo = File->LinkTo;
  6457. }
  6458. else if (!File->IsParentDirectory)
  6459. {
  6460. PointTo = File->FileName;
  6461. }
  6462. }
  6463. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6464. Terminal->IsCapable[fcHardLink]))
  6465. {
  6466. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6467. if (Edit)
  6468. {
  6469. DebugAssert(File->FileName == FileName);
  6470. int Params = dfNoRecursive;
  6471. Terminal->ExceptionOnFail = true;
  6472. try
  6473. {
  6474. Terminal->DeleteFile(L"", File, &Params);
  6475. }
  6476. __finally
  6477. {
  6478. Terminal->ExceptionOnFail = false;
  6479. }
  6480. }
  6481. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6482. }
  6483. }
  6484. //---------------------------------------------------------------------------
  6485. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6486. {
  6487. return
  6488. (IsSideLocalBrowser(Side) ||
  6489. (HasActiveTerminal() &&
  6490. Terminal->ResolvingSymlinks &&
  6491. Terminal->IsCapable[fcSymbolicLink]));
  6492. }
  6493. //---------------------------------------------------------------------------
  6494. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6495. {
  6496. return
  6497. !IsSideLocalBrowser(FCurrentSide) &&
  6498. (RemoteDirView->ItemFocused != NULL) &&
  6499. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6500. Terminal->SessionData->ResolveSymlinks;
  6501. }
  6502. //---------------------------------------------------------------------------
  6503. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6504. {
  6505. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6506. FForceExecution = true;
  6507. try
  6508. {
  6509. DirView(osCurrent)->ExecuteCurrentFile();
  6510. }
  6511. __finally
  6512. {
  6513. FForceExecution = false;
  6514. }
  6515. }
  6516. //---------------------------------------------------------------------------
  6517. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6518. {
  6519. TEditorData ExternalEditor;
  6520. ExternalEditor.Editor = edExternal;
  6521. bool Remember = false;
  6522. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6523. {
  6524. if (Remember)
  6525. {
  6526. TEditorList * EditorList = new TEditorList;
  6527. try
  6528. {
  6529. *EditorList = *WinConfiguration->EditorList;
  6530. bool Found = false;
  6531. int i = 0;
  6532. while (!Found && (i < EditorList->Count))
  6533. {
  6534. const TEditorPreferences * Editor = EditorList->Editors[i];
  6535. if ((Editor->Data->Editor == edExternal) &&
  6536. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6537. {
  6538. Found = true;
  6539. }
  6540. i++;
  6541. }
  6542. if (!Found)
  6543. {
  6544. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6545. WinConfiguration->EditorList = EditorList;
  6546. }
  6547. }
  6548. __finally
  6549. {
  6550. delete EditorList;
  6551. }
  6552. }
  6553. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6554. }
  6555. }
  6556. //---------------------------------------------------------------------------
  6557. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6558. {
  6559. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6560. if (FFileFindTerminal == ATerminal)
  6561. {
  6562. FFileFindTerminal = NULL;
  6563. HideFileFindDialog();
  6564. }
  6565. if (FClipboardTerminal == ATerminal)
  6566. {
  6567. ClipboardClear(); // implies ClipboardStop
  6568. }
  6569. if ((FOnSynchronizeAbort != NULL) &&
  6570. (ATerminal == FManagedSession))
  6571. {
  6572. FOnSynchronizeAbort(NULL);
  6573. }
  6574. }
  6575. //---------------------------------------------------------------------------
  6576. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6577. {
  6578. DetachTerminal(Sender);
  6579. }
  6580. //---------------------------------------------------------------------------
  6581. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6582. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6583. {
  6584. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6585. DebugAssert(Terminal != NULL);
  6586. if (Data->Terminal == Terminal)
  6587. {
  6588. Data->Terminal = NULL;
  6589. }
  6590. }
  6591. //---------------------------------------------------------------------------
  6592. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6593. {
  6594. return NULL;
  6595. }
  6596. //---------------------------------------------------------------------------
  6597. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6598. {
  6599. UpdateControls();
  6600. SessionColor = TColor(0);
  6601. UpdateRemotePathComboBox(false);
  6602. NeedSession(false);
  6603. }
  6604. //---------------------------------------------------------------------------
  6605. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6606. {
  6607. try
  6608. {
  6609. // Cache, as the login dialog can change its value.
  6610. // See also DoShow. Also see TTerminalManager::NewSession.
  6611. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6612. try
  6613. {
  6614. if (ShowLogin)
  6615. {
  6616. bool ReloadSessions = !Startup;
  6617. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6618. }
  6619. }
  6620. __finally
  6621. {
  6622. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6623. // (so there was no chance for the user to open any session yet)
  6624. if ((ShowLogin || !Startup) &&
  6625. !WinConfiguration->KeepOpenWhenNoSession &&
  6626. // this rules out the implicit Commander's local-local browser
  6627. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6628. {
  6629. TerminateApplication();
  6630. }
  6631. }
  6632. }
  6633. catch (EAbort &)
  6634. {
  6635. // swallow
  6636. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6637. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6638. }
  6639. }
  6640. //---------------------------------------------------------------------------
  6641. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6642. {
  6643. TTerminalManager * Manager = TTerminalManager::Instance();
  6644. DebugAssert(!Manager->Updating);
  6645. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6646. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6647. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6648. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6649. try
  6650. {
  6651. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6652. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6653. {
  6654. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6655. }
  6656. for (int Index = 0; Index <= Manager->Count; Index++)
  6657. {
  6658. TThemeTabSheet * TabSheet;
  6659. if (Index >= SessionsPageControl->PageCount)
  6660. {
  6661. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6662. TabSheet->PageControl = SessionsPageControl;
  6663. }
  6664. else
  6665. {
  6666. TabSheet = SessionsPageControl->Pages[Index];
  6667. }
  6668. bool IsSessionTab = (Index < Manager->Count);
  6669. if (IsSessionTab)
  6670. {
  6671. TTerminal * Terminal = Manager->Sessions[Index];
  6672. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6673. TabSheet->CaptionTruncation = tttEllipsis;
  6674. UpdateSessionTab(TabSheet);
  6675. }
  6676. else
  6677. {
  6678. TabSheet->Tag = 0; // not really needed
  6679. TabSheet->Shadowed = false;
  6680. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6681. TabSheet->CaptionTruncation = tttNoText;
  6682. // 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)
  6683. UpdateNewTabTab();
  6684. }
  6685. }
  6686. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6687. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6688. {
  6689. SessionsPageControl->UpdateTabsCaptionTruncation();
  6690. }
  6691. }
  6692. __finally
  6693. {
  6694. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6695. }
  6696. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6697. }
  6698. //---------------------------------------------------------------------------
  6699. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6700. {
  6701. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6702. }
  6703. //---------------------------------------------------------------------------
  6704. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6705. {
  6706. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6707. UnicodeString TabCaption;
  6708. if (WinConfiguration->SelectiveToolbarText)
  6709. {
  6710. TabCaption = GetNewTabTabCaption();
  6711. }
  6712. TabSheet->BaseCaption = TabCaption;
  6713. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6714. }
  6715. //---------------------------------------------------------------------------
  6716. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6717. {
  6718. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6719. }
  6720. //---------------------------------------------------------------------------
  6721. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6722. {
  6723. DebugFail();
  6724. return UnicodeString();
  6725. }
  6726. //---------------------------------------------------------------------------
  6727. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6728. {
  6729. if (DebugAlwaysTrue(TabSheet != NULL))
  6730. {
  6731. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6732. if (DebugAlwaysTrue(ASession != NULL))
  6733. {
  6734. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6735. if (ASession->LocalBrowser)
  6736. {
  6737. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6738. }
  6739. else
  6740. {
  6741. TabSheet->ImageIndex = AddSessionColor(Color);
  6742. }
  6743. TTerminalManager * Manager = TTerminalManager::Instance();
  6744. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6745. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6746. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6747. }
  6748. }
  6749. }
  6750. //---------------------------------------------------------------------------
  6751. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6752. {
  6753. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6754. }
  6755. //---------------------------------------------------------------------------
  6756. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6757. {
  6758. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6759. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6760. bool Result = (Session != NULL);
  6761. if (Result)
  6762. {
  6763. TTerminalManager::Instance()->ActiveSession = Session;
  6764. }
  6765. else
  6766. {
  6767. try
  6768. {
  6769. NewTab();
  6770. }
  6771. __finally
  6772. {
  6773. SessionListChanged();
  6774. }
  6775. FSessionsPageControlNewTabTime = Now();
  6776. }
  6777. return Result;
  6778. }
  6779. //---------------------------------------------------------------------------
  6780. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6781. {
  6782. TWindowLock Lock(this);
  6783. SessionTabSwitched();
  6784. }
  6785. //---------------------------------------------------------------------------
  6786. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6787. {
  6788. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6789. DebugAssert(TransferList != NULL);
  6790. UnicodeString Name;
  6791. if (TransferList->ItemIndex <= 0)
  6792. {
  6793. Name = L"";
  6794. }
  6795. else
  6796. {
  6797. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6798. }
  6799. if (FCopyParamAutoSelected.IsEmpty())
  6800. {
  6801. // if previous preset was not autoselected, make new preset the "default"
  6802. FCopyParamDefault = Name;
  6803. }
  6804. GUIConfiguration->CopyParamCurrent = Name;
  6805. }
  6806. //---------------------------------------------------------------------------
  6807. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6808. {
  6809. // sanity check
  6810. bool ExistingPreset =
  6811. (FTransferListHoverIndex >= 0) &&
  6812. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6813. DebugAssert(ExistingPreset);
  6814. if (ExistingPreset)
  6815. {
  6816. HDC DC = GetDC(0);
  6817. TCanvas * Canvas = new TCanvas();
  6818. try
  6819. {
  6820. Canvas->Handle = DC;
  6821. Canvas->Font = GetToolbarFont(this);
  6822. UnicodeString Name;
  6823. if (FTransferListHoverIndex == 0)
  6824. {
  6825. Name = L"";
  6826. }
  6827. else
  6828. {
  6829. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6830. }
  6831. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6832. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6833. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6834. static_cast<TObject*>(GetComponent(fcTransferList)));
  6835. UnicodeString InfoStr =
  6836. GUIConfiguration->CopyParamPreset[Name].
  6837. GetInfoStr(L"; ",
  6838. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6839. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6840. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6841. {
  6842. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6843. {
  6844. InfoStr.SetLength(InfoStr.Length() - 1);
  6845. }
  6846. InfoStr += Ellipsis;
  6847. }
  6848. // UpdateCaption does not cause invalidation of whole submenu, while
  6849. // setting Caption property does.
  6850. // also it probably does not resize the label, even if necessary
  6851. // (we do not want that anyway)
  6852. TransferLabel->UpdateCaption(InfoStr);
  6853. }
  6854. __finally
  6855. {
  6856. Canvas->Handle = NULL;
  6857. ReleaseDC(0, DC);
  6858. delete Canvas;
  6859. }
  6860. }
  6861. }
  6862. //---------------------------------------------------------------------------
  6863. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6864. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6865. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6866. {
  6867. if (FTransferListHoverIndex != AHoverIndex)
  6868. {
  6869. FTransferListHoverIndex = AHoverIndex;
  6870. UpdateTransferLabel();
  6871. }
  6872. }
  6873. //---------------------------------------------------------------------------
  6874. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6875. {
  6876. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6877. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6878. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6879. {
  6880. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6881. {
  6882. if (!NonVisualDataModule->Busy)
  6883. {
  6884. OpenDirectory(GetSide(osCurrent));
  6885. }
  6886. Message.Result = 1;
  6887. }
  6888. else
  6889. {
  6890. TForm::Dispatch(&Message);
  6891. }
  6892. }
  6893. else
  6894. {
  6895. TForm::Dispatch(&Message);
  6896. }
  6897. }
  6898. //---------------------------------------------------------------------------
  6899. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6900. {
  6901. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6902. if ((FIgnoreNextDialogChar != 0) &&
  6903. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6904. {
  6905. Message.Result = 1;
  6906. }
  6907. else
  6908. {
  6909. TForm::Dispatch(&Message);
  6910. }
  6911. FIgnoreNextDialogChar = 0;
  6912. }
  6913. //---------------------------------------------------------------------------
  6914. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6915. {
  6916. // The four low-order bits are used internally by Windows
  6917. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6918. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6919. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6920. // 61730 - restore thru double click - undocumented
  6921. // 61490 - maximize thru double click - undocumented
  6922. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6923. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6924. {
  6925. SysResizing(Cmd);
  6926. }
  6927. TForm::Dispatch(&Message);
  6928. }
  6929. //---------------------------------------------------------------------------
  6930. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6931. {
  6932. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6933. // Also there no point returning TRUE as we are not able to
  6934. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6935. // Hence the process termination might be safer :)
  6936. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6937. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6938. {
  6939. Message.Result = FALSE;
  6940. }
  6941. else
  6942. {
  6943. Message.Result = TRUE;
  6944. }
  6945. // Do not call default handling as that triggers OnCloseQuery,
  6946. // where our implementation will popup confirmation dialogs, what we do not want,
  6947. // as per Vista guidelines:
  6948. // https://learn.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6949. }
  6950. //---------------------------------------------------------------------------
  6951. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6952. {
  6953. if (Message.EndSession && IsApplicationMinimized())
  6954. {
  6955. // WORKAROUND
  6956. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6957. // when it receives WM_ENDSESSION,
  6958. // but that sometimes (particularly when application is minimized) cause crashes.
  6959. // Crash popups message that blocks log off.
  6960. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6961. // so we call ExitProcess() immediately, not even trying to cleanup.
  6962. // It still causes beep, so there's likely some popup, but it does not block
  6963. // log off.
  6964. ExitProcess(0);
  6965. }
  6966. TForm::Dispatch(&Message);
  6967. }
  6968. #ifdef _DEBUG
  6969. //---------------------------------------------------------------------------
  6970. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6971. {
  6972. TForm::Dispatch(&Message);
  6973. }
  6974. #endif
  6975. //---------------------------------------------------------------------------
  6976. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6977. {
  6978. }
  6979. //---------------------------------------------------------------------------
  6980. void __fastcall TCustomScpExplorerForm::DoShow()
  6981. {
  6982. // Called from TCustomScpExplorerForm::CMShowingChanged
  6983. // only now are the controls resized finally, so the size constraints
  6984. // will not conflict with possibly very small window size
  6985. RestoreFormParams();
  6986. // This indicates that the form was rescaled after parameters were loaded and
  6987. // as most probably the parameters were actually saved with the current form scaling
  6988. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6989. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6990. // and no rounding problems will occur.
  6991. // See also RestoreForm.
  6992. // (we should be safe to call RestoreParams unconditionally)
  6993. if (PixelsPerInch != Screen->PixelsPerInch)
  6994. {
  6995. RestoreParams();
  6996. }
  6997. FixControlsPlacement();
  6998. if (Position == poDefaultPosOnly)
  6999. {
  7000. CutFormToDesktop(this);
  7001. }
  7002. TForm::DoShow();
  7003. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  7004. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  7005. // This was previously in NeedSession, where other related code is.
  7006. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  7007. // unpleasantly shows briefly.
  7008. if ((ManagedSession == NULL) &&
  7009. !WinConfiguration->ShowLoginWhenNoSession &&
  7010. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  7011. // This detects if workspace was saved the last time the main widow was closed
  7012. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  7013. {
  7014. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  7015. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  7016. }
  7017. // Before main window displays
  7018. if (ManagedSession == NULL)
  7019. {
  7020. StartingWithoutSession();
  7021. }
  7022. UpdatePixelsPerInchMainWindowCounter();
  7023. FShowing = true;
  7024. }
  7025. //---------------------------------------------------------------------------
  7026. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  7027. {
  7028. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  7029. }
  7030. //---------------------------------------------------------------------------
  7031. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  7032. {
  7033. SessionListChanged();
  7034. InitStatusBar();
  7035. UpdateControls();
  7036. }
  7037. //---------------------------------------------------------------------------
  7038. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  7039. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  7040. TNotifyEvent OnBalloonClick, TObject * UserData)
  7041. {
  7042. bool Do;
  7043. UnicodeString Message;
  7044. if (E == NULL)
  7045. {
  7046. Message = Str;
  7047. Do = true;
  7048. }
  7049. else
  7050. {
  7051. Do = ExceptionMessage(E, Message);
  7052. }
  7053. if (Do && WinConfiguration->BalloonNotifications)
  7054. {
  7055. UnicodeString Title;
  7056. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  7057. {
  7058. Message = TrimLeft(Message);
  7059. }
  7060. else
  7061. {
  7062. Message = UnformatMessage(Message);
  7063. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  7064. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  7065. Title = LoadResourceString(Captions[Type]);
  7066. if (Terminal != NULL)
  7067. {
  7068. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7069. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  7070. }
  7071. }
  7072. if (Seconds == 0)
  7073. {
  7074. Seconds = WinConfiguration->NotificationsTimeout;
  7075. }
  7076. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  7077. }
  7078. }
  7079. //---------------------------------------------------------------------------
  7080. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  7081. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  7082. UnicodeString HelpKeyword, const TMessageParams * Params,
  7083. TTerminal * Terminal)
  7084. {
  7085. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  7086. (FOnFeedSynchronizeError != NULL)) &&
  7087. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  7088. {
  7089. if (FOnFeedSynchronizeError != NULL)
  7090. {
  7091. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  7092. }
  7093. else
  7094. {
  7095. DebugAssert(FErrorList != NULL);
  7096. TStringList * MoreMessagesCopy = NULL;
  7097. if (MoreMessages)
  7098. {
  7099. MoreMessagesCopy = new TStringList();
  7100. MoreMessagesCopy->Assign(MoreMessages);
  7101. }
  7102. FErrorList->AddObject(Message, MoreMessagesCopy);
  7103. }
  7104. PopupTrayBalloon(Terminal, Message, Type);
  7105. return ContinueAnswer(Answers);
  7106. }
  7107. else
  7108. {
  7109. bool UseBalloon = FTrayIcon->Visible;
  7110. if (UseBalloon)
  7111. {
  7112. PopupTrayBalloon(Terminal, Message, Type);
  7113. }
  7114. unsigned int Result;
  7115. try
  7116. {
  7117. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  7118. }
  7119. __finally
  7120. {
  7121. // cancel only balloon we popped up, otherwise we may cancel notification
  7122. // balloon that was there before the message dialog
  7123. // (such as "dd confirmation opt in balloon)
  7124. if (UseBalloon)
  7125. {
  7126. FTrayIcon->CancelBalloon();
  7127. }
  7128. }
  7129. return Result;
  7130. }
  7131. }
  7132. //---------------------------------------------------------------------------
  7133. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  7134. TTerminal * Terminal, Exception * E)
  7135. {
  7136. if (FTrayIcon->Visible)
  7137. {
  7138. PopupTrayBalloon(Terminal, L"", qtError, E);
  7139. }
  7140. // particularly prevent opening new session from jump list,
  7141. // while exception is shown
  7142. NonVisualDataModule->StartBusy();
  7143. try
  7144. {
  7145. ShowExtendedExceptionEx(Terminal, E);
  7146. }
  7147. __finally
  7148. {
  7149. NonVisualDataModule->EndBusy();
  7150. FTrayIcon->CancelBalloon();
  7151. }
  7152. }
  7153. //---------------------------------------------------------------------------
  7154. void __fastcall TCustomScpExplorerForm::SessionReady()
  7155. {
  7156. InitStatusBar();
  7157. // cannot rely on active page being page for active terminal,
  7158. // as it can happen that active page is the "new session" page
  7159. // (e.g. when reconnecting active terminal, while login dialog
  7160. // invoked from "new session" page is modal)
  7161. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  7162. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  7163. }
  7164. //---------------------------------------------------------------------------
  7165. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  7166. TTerminal * Terminal, Exception * E)
  7167. {
  7168. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  7169. if (!Terminal->Active)
  7170. {
  7171. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  7172. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  7173. {
  7174. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  7175. }
  7176. }
  7177. }
  7178. //---------------------------------------------------------------------------
  7179. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  7180. UnicodeString Message, TQueryType Type,
  7181. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  7182. {
  7183. if ((E == NULL) ||
  7184. ExceptionMessage(E, Message))
  7185. {
  7186. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  7187. if (Important)
  7188. {
  7189. Seconds *= 5;
  7190. }
  7191. UnicodeString NoteMessage(UnformatMessage(Message));
  7192. if (Terminal != NULL)
  7193. {
  7194. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7195. NoteMessage = FORMAT(L"%s: %s",
  7196. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  7197. }
  7198. if (WinConfiguration->BalloonNotifications)
  7199. {
  7200. AddNote(NoteMessage);
  7201. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  7202. }
  7203. else
  7204. {
  7205. FlashOnBackground();
  7206. PostNote(NoteMessage, Seconds, OnClick, UserData);
  7207. }
  7208. }
  7209. }
  7210. //---------------------------------------------------------------------------
  7211. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7212. {
  7213. RestoreApp();
  7214. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7215. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7216. !NonVisualDataModule->Busy)
  7217. {
  7218. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7219. TTerminalManager::Instance()->ActiveSession = Terminal;
  7220. if (!ComponentVisible[fcQueueView])
  7221. {
  7222. ToggleQueueVisibility();
  7223. GoToQueue();
  7224. }
  7225. }
  7226. }
  7227. //---------------------------------------------------------------------------
  7228. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7229. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7230. {
  7231. UnicodeString Message;
  7232. TNotifyEvent OnClick = NULL;
  7233. TObject * UserData = NULL;
  7234. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7235. switch (Event)
  7236. {
  7237. case qeEmptyButMonitored:
  7238. if ((ATerminal != Terminal) || QueueInvisible)
  7239. {
  7240. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7241. OnClick = QueueEmptyNoteClicked;
  7242. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7243. TerminalNoteData->Terminal = ATerminal;
  7244. UserData = TerminalNoteData;
  7245. }
  7246. break;
  7247. case qeEmpty:
  7248. OperationComplete(ATerminal->QueueOperationStart);
  7249. break;
  7250. case qePendingUserAction:
  7251. if ((ATerminal != Terminal) ||
  7252. (QueueInvisible && !IsQueueAutoPopup()))
  7253. {
  7254. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7255. }
  7256. break;
  7257. default:
  7258. DebugFail();
  7259. }
  7260. if (!Message.IsEmpty())
  7261. {
  7262. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7263. }
  7264. }
  7265. //---------------------------------------------------------------------------
  7266. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7267. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7268. {
  7269. if (FDDExtMapFile != NULL)
  7270. {
  7271. CloseHandle(FDDExtMapFile);
  7272. FDDExtMapFile = NULL;
  7273. }
  7274. if (WinConfiguration->DDFakeFile)
  7275. {
  7276. DDFakeFileInitDrag(FileList, Created);
  7277. }
  7278. }
  7279. //---------------------------------------------------------------------------
  7280. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7281. {
  7282. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7283. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7284. {
  7285. FFakeFileDropTarget = FileName;
  7286. }
  7287. }
  7288. //---------------------------------------------------------------------------
  7289. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7290. {
  7291. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7292. if (!ForceDirectories(ApiPath(Result)))
  7293. {
  7294. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7295. }
  7296. FileSetAttr(ApiPath(Result), faHidden);
  7297. return Result;
  7298. }
  7299. //---------------------------------------------------------------------------
  7300. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7301. bool & Created)
  7302. {
  7303. FFakeFileDropTarget = UnicodeString();
  7304. FDragFakeDirectory = CreateFakeTransferDirectory();
  7305. FileList->AddItem(NULL, FDragFakeDirectory);
  7306. Created = true;
  7307. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7308. {
  7309. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7310. }
  7311. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7312. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7313. {
  7314. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7315. TDragExtCommStruct* CommStruct;
  7316. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7317. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7318. DebugAssert(CommStruct != NULL);
  7319. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7320. CommStruct->Dragging = true;
  7321. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7322. LENOF(CommStruct->DropDest));
  7323. NULL_TERMINATE(CommStruct->DropDest);
  7324. UnmapViewOfFile(CommStruct);
  7325. }
  7326. }
  7327. //---------------------------------------------------------------------------
  7328. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7329. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7330. {
  7331. bool Result;
  7332. DebugAssert(!IsLocalBrowserMode());
  7333. if (Sender == RemoteDirView)
  7334. {
  7335. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7336. }
  7337. else
  7338. {
  7339. DebugAssert(Sender == RemoteDriveView);
  7340. TStrings * FileList = RemoteDriveView->DragFileList();
  7341. try
  7342. {
  7343. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7344. }
  7345. __finally
  7346. {
  7347. delete FileList;
  7348. }
  7349. }
  7350. if (FDDTargetControl == RemoteDriveView)
  7351. {
  7352. RemoteDriveView->UpdateDropTarget();
  7353. }
  7354. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7355. // or from tree view to dir view.
  7356. // foMove happens when file/dir is dragged from remote tree view outside of
  7357. // application via dragex
  7358. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7359. (DropSourceControl == RemoteDriveView))
  7360. {
  7361. RemoteDriveView->UpdateDropSource();
  7362. }
  7363. return Result;
  7364. }
  7365. //---------------------------------------------------------------------------
  7366. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7367. {
  7368. DebugAssert(!IsLocalBrowserMode());
  7369. // This also handles drops of remote files to queue.
  7370. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7371. SAFE_DESTROY(FDDFileList);
  7372. if (!FFakeFileDropTarget.IsEmpty())
  7373. {
  7374. RemoveDir(ApiPath(FFakeFileDropTarget));
  7375. }
  7376. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7377. {
  7378. try
  7379. {
  7380. TDragResult DDResult = (Sender == RemoteDirView) ?
  7381. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7382. // Focus is moved to the target application,
  7383. // but as we are going to present the UI, we need to steal the focus back.
  7384. // This most likely won't work though (windows does not allow application
  7385. // to steal focus most of the time)
  7386. Application->BringToFront();
  7387. // On older version of Windows we never got drMove here, see also comment below.
  7388. // On Windows 10, we get the "move".
  7389. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7390. {
  7391. UnicodeString TargetDirectory;
  7392. TFileOperation Operation;
  7393. // drInvalid may mean drMove, see comment below
  7394. switch (DDResult)
  7395. {
  7396. case drCopy:
  7397. Operation = foCopy;
  7398. break;
  7399. case drMove:
  7400. Operation = foMove;
  7401. break;
  7402. default:
  7403. DebugFail();
  7404. case drInvalid:
  7405. // prefer "copy" for safety
  7406. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7407. break;
  7408. }
  7409. TTransferOperationParam Param;
  7410. UnicodeString CounterName;
  7411. bool ForceQueue;
  7412. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7413. {
  7414. // we get drInvalid both if d&d was intercepted by ddext,
  7415. // and when users drops on no-drop location.
  7416. // we tell the difference by existence of response from ddext,
  7417. // so we ignore absence of response in this case
  7418. if (DDResult != drInvalid)
  7419. {
  7420. // here we know that the extension is installed,
  7421. // as it is checked as soon as drag&drop starts from
  7422. // RemoteFileControlDDCreateDragFileList
  7423. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7424. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7425. {
  7426. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7427. }
  7428. else
  7429. {
  7430. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7431. }
  7432. }
  7433. }
  7434. else
  7435. {
  7436. // download using fake file
  7437. Param.Temp = false;
  7438. Param.DragDrop = true;
  7439. if (ForceQueue)
  7440. {
  7441. Param.Queue = asOn;
  7442. }
  7443. if (Sender == RemoteDirView)
  7444. {
  7445. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7446. }
  7447. if (RemoteFileControlFileOperation(Sender, Operation,
  7448. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7449. {
  7450. Configuration->Usage->Inc(CounterName);
  7451. }
  7452. }
  7453. }
  7454. }
  7455. __finally
  7456. {
  7457. CloseHandle(FDDExtMapFile);
  7458. FDDExtMapFile = NULL;
  7459. delete FDragFakeMonitors;
  7460. FDragFakeMonitors = NULL;
  7461. RemoveDir(ApiPath(FDragFakeDirectory));
  7462. FDragFakeDirectory = L"";
  7463. FFakeFileDropTarget = UnicodeString();
  7464. }
  7465. }
  7466. if (FDragDropSshTerminate.get() != NULL)
  7467. {
  7468. FDragDropSshTerminate->Rethrow();
  7469. }
  7470. }
  7471. //---------------------------------------------------------------------------
  7472. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7473. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7474. {
  7475. // Remember drop effect so we know (when user drops files), if we copy or move
  7476. FLastDropEffect = dwEffect;
  7477. }
  7478. //---------------------------------------------------------------------------
  7479. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7480. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7481. {
  7482. bool Result;
  7483. if (FDDTargetControl == QueueView3)
  7484. {
  7485. Directory = DefaultDownloadTargetDirectory();
  7486. Result = true;
  7487. CounterName = L"DownloadsDragDropQueue";
  7488. ForceQueue = true;
  7489. }
  7490. else if (!FFakeFileDropTarget.IsEmpty())
  7491. {
  7492. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7493. Result = true;
  7494. ForceQueue = false;
  7495. CounterName = L"DownloadsDragDropFakeFile";
  7496. }
  7497. else
  7498. {
  7499. ForceQueue = false;
  7500. Enabled = false;
  7501. try
  7502. {
  7503. int Timer = 0;
  7504. Result = false;
  7505. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7506. {
  7507. {
  7508. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7509. TDragExtCommStruct* CommStruct;
  7510. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7511. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7512. DebugAssert(CommStruct != NULL);
  7513. Result = !CommStruct->Dragging;
  7514. if (Result)
  7515. {
  7516. Directory = ExtractFilePath(CommStruct->DropDest);
  7517. CounterName = L"DownloadsDragDropExternalExt";
  7518. }
  7519. UnmapViewOfFile(CommStruct);
  7520. }
  7521. if (!Result)
  7522. {
  7523. Sleep(50);
  7524. Timer += 50;
  7525. Application->ProcessMessages();
  7526. }
  7527. }
  7528. }
  7529. __finally
  7530. {
  7531. Enabled = true;
  7532. }
  7533. }
  7534. return Result;
  7535. }
  7536. //---------------------------------------------------------------------------
  7537. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7538. const UnicodeString TempDir, int SecDelay)
  7539. {
  7540. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7541. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7542. if (FDelayedDeletionTimer == NULL)
  7543. {
  7544. DebugAssert(HandleAllocated());
  7545. FDelayedDeletionTimer = new TTimer(this);
  7546. FDelayedDeletionTimer->Interval = 10000;
  7547. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7548. }
  7549. else
  7550. {
  7551. FDelayedDeletionTimer->Enabled = true;
  7552. }
  7553. }
  7554. //---------------------------------------------------------------------------
  7555. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7556. {
  7557. DebugAssert(FDelayedDeletionList != NULL);
  7558. TDateTime N = Now();
  7559. TDateTime Alert;
  7560. UnicodeString Directory;
  7561. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7562. {
  7563. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7564. if ((N >= Alert) || (Sender == NULL))
  7565. {
  7566. Directory = FDelayedDeletionList->Strings[Index];
  7567. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7568. {
  7569. FDelayedDeletionList->Delete(Index);
  7570. }
  7571. }
  7572. }
  7573. if (FDelayedDeletionList->Count == 0)
  7574. {
  7575. FDelayedDeletionTimer->Enabled = false;
  7576. }
  7577. }
  7578. //---------------------------------------------------------------------------
  7579. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7580. {
  7581. DebugAssert(!IsLocalBrowserMode());
  7582. if (IsFileControl(FDDTargetControl, osRemote) ||
  7583. (FDDTargetControl == SessionsPageControl))
  7584. {
  7585. TManagedTerminal * TargetTerminal = NULL;
  7586. TFileOperation Operation = foNone;
  7587. if (FDDTargetControl == SessionsPageControl)
  7588. {
  7589. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7590. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7591. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7592. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7593. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7594. {
  7595. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7596. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7597. {
  7598. Operation = foRemoteMove;
  7599. }
  7600. else
  7601. {
  7602. Operation = foRemoteCopy;
  7603. }
  7604. }
  7605. }
  7606. else
  7607. {
  7608. if (FLastDropEffect == DROPEFFECT_MOVE)
  7609. {
  7610. Operation = foRemoteMove;
  7611. }
  7612. else if (FLastDropEffect == DROPEFFECT_COPY)
  7613. {
  7614. Operation = foRemoteCopy;
  7615. }
  7616. }
  7617. if (Operation != foNone)
  7618. {
  7619. RemoteFileControlFileOperation(DropSourceControl,
  7620. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7621. }
  7622. // abort drag&drop
  7623. Abort();
  7624. }
  7625. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7626. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7627. (FDDTargetControl != QueueView3))
  7628. {
  7629. DebugAssert(!FDragTempDir.IsEmpty());
  7630. TTransferType Type;
  7631. UnicodeString TempDir = FDragTempDir;
  7632. // We clear FDragTempDir before calling
  7633. // just in case it fail (raises exception)
  7634. FDragTempDir = L"";
  7635. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7636. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7637. // empty directory parameter means temp directory -> don't display it!
  7638. UnicodeString TargetDir = L"";
  7639. int Options = 0;
  7640. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7641. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7642. {
  7643. Abort();
  7644. }
  7645. // TargetDir is set when dropped on local file control
  7646. // (this was workaround for legacy dirview event handling, now it should be
  7647. // made prettier)
  7648. if (TargetDir.IsEmpty())
  7649. {
  7650. TargetDir = TempDir;
  7651. if (ForceDirectories(ApiPath(TargetDir)))
  7652. {
  7653. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7654. FPendingTempSpaceWarn = true;
  7655. try
  7656. {
  7657. FDragDropOperation = true;
  7658. int Params = cpTemporary |
  7659. (Type == ttMove ? cpDelete : 0);
  7660. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7661. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7662. }
  7663. __finally
  7664. {
  7665. FDragDropOperation = false;
  7666. FPendingTempSpaceWarn = false;
  7667. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7668. }
  7669. }
  7670. else
  7671. {
  7672. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7673. }
  7674. }
  7675. }
  7676. }
  7677. //---------------------------------------------------------------------------
  7678. void __fastcall TCustomScpExplorerForm::DDDownload(
  7679. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  7680. {
  7681. DebugAssert(!IsLocalBrowserMode());
  7682. TAutoBatch AutoBatch(this);
  7683. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  7684. UpdateCopyParamCounters(*CopyParam);
  7685. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7686. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7687. {
  7688. RemoteDriveView->UpdateDropSource();
  7689. }
  7690. }
  7691. //---------------------------------------------------------------------------
  7692. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7693. {
  7694. public:
  7695. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7696. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7697. {
  7698. }
  7699. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7700. {
  7701. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7702. TDataObjectFilesEx::AllowData(FormatEtc);
  7703. }
  7704. };
  7705. //---------------------------------------------------------------------------
  7706. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7707. TObject * Sender, TDataObject *& DataObject)
  7708. {
  7709. if (FDDExtMapFile != NULL)
  7710. {
  7711. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7712. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7713. {
  7714. Abort();
  7715. }
  7716. if (FileList->Count > 0)
  7717. {
  7718. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7719. if (!FilesObject->IsValid(true, true))
  7720. {
  7721. FilesObject->_Release();
  7722. }
  7723. else
  7724. {
  7725. DataObject = FilesObject;
  7726. }
  7727. }
  7728. }
  7729. }
  7730. //---------------------------------------------------------------------------
  7731. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7732. {
  7733. DebugFail();
  7734. }
  7735. //---------------------------------------------------------------------------
  7736. void __fastcall TCustomScpExplorerForm::GoToTree()
  7737. {
  7738. ComponentVisible[fcRemoteTree] = true;
  7739. DriveView(osRemote)->SetFocus();
  7740. }
  7741. //---------------------------------------------------------------------------
  7742. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7743. TPanelExport Export)
  7744. {
  7745. TCustomDirView * DirView = this->DirView(Side);
  7746. std::unique_ptr<TStrings> ExportData(new TStringList());
  7747. switch (Export)
  7748. {
  7749. case pePath:
  7750. ExportData->Add(DirView->PathName);
  7751. break;
  7752. case peFileList:
  7753. case peFullFileList:
  7754. {
  7755. bool FullPath = (Export == peFullFileList);
  7756. DirView->CreateFileList(false, FullPath, ExportData.get());
  7757. for (int Index = 0; Index < ExportData->Count; Index++)
  7758. {
  7759. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7760. {
  7761. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7762. }
  7763. }
  7764. }
  7765. break;
  7766. default:
  7767. DebugFail();
  7768. }
  7769. return ExportData.release();
  7770. }
  7771. //---------------------------------------------------------------------------
  7772. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7773. TPanelExport Export, TPanelExportDestination Destination)
  7774. {
  7775. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7776. PanelExportStore(Side, Export, Destination, ExportData.get());
  7777. }
  7778. //---------------------------------------------------------------------------
  7779. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7780. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7781. TStrings * ExportData)
  7782. {
  7783. if (Destination == pedClipboard)
  7784. {
  7785. TInstantOperationVisualizer Visualizer;
  7786. CopyToClipboard(ExportData);
  7787. }
  7788. else
  7789. {
  7790. DebugFail();
  7791. }
  7792. }
  7793. //---------------------------------------------------------------------------
  7794. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7795. {
  7796. TCustomDirView * DirView = this->DirView(Side);
  7797. UnicodeString Mask = DirView->Mask;
  7798. if (DoFilterMaskDialog(DirView, Mask))
  7799. {
  7800. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7801. Configuration->Usage->Inc(L"Filters");
  7802. }
  7803. }
  7804. //---------------------------------------------------------------------------
  7805. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7806. {
  7807. return FQueueController->DefaultOperation();
  7808. }
  7809. //---------------------------------------------------------------------------
  7810. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7811. TQueueOperation Operation, void ** Param)
  7812. {
  7813. switch (Operation)
  7814. {
  7815. case qoPreferences:
  7816. return true;
  7817. case qoGoTo:
  7818. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7819. case qoOnceEmpty:
  7820. return IsAnythingQueued();
  7821. default:
  7822. return FQueueController->AllowOperation(Operation, Param);
  7823. }
  7824. }
  7825. //---------------------------------------------------------------------------
  7826. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7827. {
  7828. if (DebugAlwaysTrue(QueueView3->Visible))
  7829. {
  7830. QueueView3->SetFocus();
  7831. }
  7832. }
  7833. //---------------------------------------------------------------------------
  7834. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7835. TQueueOperation Operation, void * Param)
  7836. {
  7837. if (Operation == qoGoTo)
  7838. {
  7839. GoToQueue();
  7840. }
  7841. else if (Operation == qoPreferences)
  7842. {
  7843. PreferencesDialog(pmQueue);
  7844. }
  7845. else
  7846. {
  7847. FQueueController->ExecuteOperation(Operation, Param);
  7848. }
  7849. }
  7850. //---------------------------------------------------------------------------
  7851. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7852. {
  7853. return (Queue != NULL) && Queue->Enabled;
  7854. }
  7855. //---------------------------------------------------------------------------
  7856. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7857. {
  7858. DebugAssert(Queue != NULL);
  7859. if (Queue != NULL)
  7860. {
  7861. Queue->Enabled = !Queue->Enabled;
  7862. }
  7863. }
  7864. //---------------------------------------------------------------------------
  7865. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7866. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7867. {
  7868. FQueueActedItem = QueueView3->ItemFocused;
  7869. }
  7870. //---------------------------------------------------------------------------
  7871. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7872. {
  7873. return TopDock->Height + (MessageDock->Visible ? MessageDock->Height : 0) + QueueSplitter->Height;
  7874. }
  7875. //---------------------------------------------------------------------------
  7876. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7877. {
  7878. return
  7879. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7880. (QueueDock->Visible ? QueueDock->Height : 0) +
  7881. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7882. }
  7883. //---------------------------------------------------------------------------
  7884. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7885. {
  7886. return ScaleByTextHeight(this, 48);
  7887. }
  7888. //---------------------------------------------------------------------------
  7889. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7890. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7891. {
  7892. // when queue is hidden by double-clicking splitter, stray attempt to
  7893. // resize the panel with strange value arrives, make sure it is ignored
  7894. if (ComponentVisible[fcQueueView])
  7895. {
  7896. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7897. if (NewSize > MaxHeightLimit)
  7898. {
  7899. NewSize = MaxHeightLimit;
  7900. }
  7901. int MinHeightLimit =
  7902. GetStaticQueuePanelComponentsHeight() +
  7903. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7904. GetMinQueueViewHeight();
  7905. if (NewSize < MinHeightLimit)
  7906. {
  7907. NewSize = MinHeightLimit;
  7908. }
  7909. }
  7910. else
  7911. {
  7912. Accept = false;
  7913. }
  7914. }
  7915. //---------------------------------------------------------------------------
  7916. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7917. {
  7918. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7919. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7920. if (QueueViewHeight < GetMinQueueViewHeight())
  7921. {
  7922. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7923. }
  7924. }
  7925. //---------------------------------------------------------------------------
  7926. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7927. TDragObject *& /*DragObject*/)
  7928. {
  7929. FQueueActedItem = QueueView3->ItemFocused;
  7930. FQueueScrollOnDragOver->StartDrag();
  7931. }
  7932. //---------------------------------------------------------------------------
  7933. void __fastcall TCustomScpExplorerForm::QueueView3EndDrag(
  7934. TObject *, TObject * DebugUsedArg(Target), int DebugUsedArg(X), int DebugUsedArg(Y))
  7935. {
  7936. FQueueScrollOnDragOver->EndDrag();
  7937. }
  7938. //---------------------------------------------------------------------------
  7939. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7940. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7941. {
  7942. Accept = true;
  7943. if (Source == QueueView3)
  7944. {
  7945. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7946. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7947. if (Accept)
  7948. {
  7949. TQueueItemProxy * QueueItem;
  7950. TQueueItemProxy * DestQueueItem;
  7951. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7952. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7953. Accept = (QueueItem != DestQueueItem) &&
  7954. (QueueItem->Status == TQueueItem::qsPending) &&
  7955. (DestQueueItem->Status == TQueueItem::qsPending);
  7956. }
  7957. FQueueScrollOnDragOver->DragOver(TPoint(X, Y));
  7958. }
  7959. }
  7960. //---------------------------------------------------------------------------
  7961. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7962. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7963. {
  7964. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7965. {
  7966. TQueueItemProxy * QueueItem;
  7967. TQueueItemProxy * DestQueueItem;
  7968. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7969. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7970. QueueItem->Move(DestQueueItem);
  7971. }
  7972. }
  7973. //---------------------------------------------------------------------------
  7974. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7975. {
  7976. if ((QueueView3->ItemFocused == NULL) &&
  7977. (QueueView3->Items->Count > 0))
  7978. {
  7979. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7980. }
  7981. QueueLabelUpdateStatus();
  7982. }
  7983. //---------------------------------------------------------------------------
  7984. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7985. {
  7986. QueueLabelUpdateStatus();
  7987. }
  7988. //---------------------------------------------------------------------------
  7989. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7990. {
  7991. QueueLabel->UpdateStatus();
  7992. }
  7993. //---------------------------------------------------------------------------
  7994. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7995. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7996. {
  7997. if (Selected)
  7998. {
  7999. NonVisualDataModule->UpdateNonVisibleActions();
  8000. }
  8001. }
  8002. //---------------------------------------------------------------------------
  8003. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  8004. {
  8005. TDragDropFilesEx * Result = NULL;
  8006. if (Sender == SessionsPageControl)
  8007. {
  8008. Result = FSessionsDragDropFilesEx;
  8009. }
  8010. else if (Sender == QueueView3)
  8011. {
  8012. Result = FQueueDragDropFilesEx;
  8013. }
  8014. else
  8015. {
  8016. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8017. if (DirView != NULL)
  8018. {
  8019. Result = DirView->DragDropFilesEx;
  8020. }
  8021. else
  8022. {
  8023. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  8024. if (DriveView != NULL)
  8025. {
  8026. Result = DriveView->DragDropFilesEx;
  8027. }
  8028. }
  8029. }
  8030. DebugAssert(Result != NULL);
  8031. return Result;
  8032. }
  8033. //---------------------------------------------------------------------------
  8034. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  8035. {
  8036. return (DView->SelCount == DView->FilesCount);
  8037. }
  8038. //---------------------------------------------------------------------------
  8039. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  8040. {
  8041. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  8042. }
  8043. //---------------------------------------------------------------------------
  8044. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  8045. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  8046. {
  8047. DebugAssert(!IsLocalBrowserMode());
  8048. TFileOperation Operation;
  8049. switch (Effect)
  8050. {
  8051. case DROPEFFECT_MOVE:
  8052. Operation = foMove;
  8053. break;
  8054. case DROPEFFECT_COPY:
  8055. // occures on WinXP (reported by user)
  8056. default:
  8057. Operation = foCopy;
  8058. break;
  8059. };
  8060. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  8061. // see a comment in TUnixDirView::PerformItemDragDropOperation
  8062. if (DragDropFilesEx->FileList->Count > 0)
  8063. {
  8064. TStrings * FileList = new TStringList();
  8065. try
  8066. {
  8067. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  8068. {
  8069. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  8070. }
  8071. FDragDropOperation = true;
  8072. TTransferOperationParam Param;
  8073. Param.TargetDirectory = TargetPath;
  8074. // upload, no temp dirs
  8075. Param.Temp = false;
  8076. Param.DragDrop = DragDrop;
  8077. Param.Options =
  8078. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  8079. if (ForceQueue)
  8080. {
  8081. Param.Queue = asOn;
  8082. }
  8083. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  8084. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  8085. {
  8086. if (IsFileControl(DropSourceControl, osLocal))
  8087. {
  8088. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  8089. }
  8090. else
  8091. {
  8092. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  8093. }
  8094. }
  8095. }
  8096. __finally
  8097. {
  8098. FDragDropOperation = false;
  8099. delete FileList;
  8100. }
  8101. }
  8102. }
  8103. //---------------------------------------------------------------------------
  8104. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  8105. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  8106. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  8107. {
  8108. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  8109. }
  8110. //---------------------------------------------------------------------------
  8111. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  8112. TObject * Sender, int grfKeyState, int & dwEffect)
  8113. {
  8114. DebugAssert(!IsLocalBrowserMode());
  8115. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  8116. if ((dwEffect != DROPEFFECT_NONE) &&
  8117. IsFileControl(DropSourceControl, osRemote))
  8118. {
  8119. // do not allow drop on remote panel (on free space, still allow drop on directories)
  8120. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  8121. (RemoteDirView->DropTarget == NULL))
  8122. {
  8123. dwEffect = DROPEFFECT_NONE;
  8124. }
  8125. else
  8126. {
  8127. if (dwEffect == DROPEFFECT_COPY)
  8128. {
  8129. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  8130. // currently we support copying always (at least via temporary directory);
  8131. // remove associated checks once this all proves stable and working
  8132. bool CopyCapable = true;
  8133. // if we do not support neither of operations, there's no discussion
  8134. if (!MoveCapable && !CopyCapable)
  8135. {
  8136. dwEffect = DROPEFFECT_NONE;
  8137. }
  8138. else
  8139. {
  8140. // The default effect inside remote panel is move,
  8141. // unless we do not support it, but support copy
  8142. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  8143. {
  8144. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  8145. }
  8146. else
  8147. {
  8148. // with ctrl-down, we want copy unless it is not supported
  8149. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  8150. }
  8151. }
  8152. }
  8153. }
  8154. }
  8155. }
  8156. //---------------------------------------------------------------------------
  8157. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  8158. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  8159. {
  8160. DebugAssert(!IsLocalBrowserMode());
  8161. if (FDDTotalSize >= 0)
  8162. {
  8163. if (File->IsDirectory)
  8164. {
  8165. FDDTotalSize = -1;
  8166. }
  8167. else
  8168. {
  8169. FDDTotalSize += File->Size;
  8170. }
  8171. }
  8172. DebugAssert(!FDragTempDir.IsEmpty());
  8173. // TODO: this is quite ineffective
  8174. // TODO: what if invalid character replacement is disabled?
  8175. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  8176. UnicodeString TransferFileName;
  8177. if (Sender == RemoteDriveView)
  8178. {
  8179. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  8180. }
  8181. else
  8182. {
  8183. TransferFileName = File->FileName;
  8184. }
  8185. FDDFileList->AddObject(TransferFileName, File);
  8186. }
  8187. //---------------------------------------------------------------------------
  8188. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  8189. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  8190. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  8191. {
  8192. // sometimes we do not get DDEnd so the list is not released
  8193. SAFE_DESTROY(FDDFileList);
  8194. FDDFileList = new TStringList();
  8195. FDragTempDir = WinConfiguration->TemporaryDir();
  8196. FDDTotalSize = 0;
  8197. FDragDropSshTerminate.reset(NULL);
  8198. }
  8199. //---------------------------------------------------------------------------
  8200. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  8201. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  8202. HRESULT & Result)
  8203. {
  8204. if (Result == DRAGDROP_S_DROP)
  8205. {
  8206. try
  8207. {
  8208. GlobalDragImageList->HideDragImage();
  8209. try
  8210. {
  8211. RemoteFileControlDDTargetDrop();
  8212. }
  8213. catch(ESshTerminate & E)
  8214. {
  8215. FDragDropSshTerminate.reset(E.Clone());
  8216. }
  8217. }
  8218. catch (Exception &E)
  8219. {
  8220. // If downloading fails we need to cancel drag&drop, otherwise
  8221. // Explorer shows error
  8222. // But by the way exception probably never reach this point as
  8223. // it's catched on way.
  8224. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8225. Result = DRAGDROP_S_CANCEL;
  8226. ShowExtendedException(Terminal, &E);
  8227. }
  8228. }
  8229. }
  8230. //---------------------------------------------------------------------------
  8231. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8232. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8233. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8234. {
  8235. TFileMasks::TParams MaskParams;
  8236. MaskParams.Size = Size;
  8237. MaskParams.Modification = Modification;
  8238. // this does not re-parse the mask if it is the same as the last time
  8239. FDirViewMatchMask = Masks;
  8240. bool ImplicitMatch;
  8241. Matches =
  8242. // Local and RecurseInclude parameters have no effect as there's no path.
  8243. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  8244. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  8245. (AllowImplicitMatches || !ImplicitMatch);
  8246. }
  8247. //---------------------------------------------------------------------------
  8248. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8249. TObject * Sender, TListItem * Item, WORD & Indexes)
  8250. {
  8251. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8252. UnicodeString Ext;
  8253. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  8254. {
  8255. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  8256. }
  8257. else
  8258. {
  8259. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  8260. }
  8261. if (SameText(Ext, Configuration->PartialExt))
  8262. {
  8263. Indexes |= oiPartial;
  8264. }
  8265. }
  8266. //---------------------------------------------------------------------------
  8267. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8268. {
  8269. return
  8270. DirViewEnabled(osCurrent) &&
  8271. DirView(osCurrent)->CanPasteFromClipBoard();
  8272. }
  8273. //---------------------------------------------------------------------------
  8274. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8275. {
  8276. bool Result = false;
  8277. if (CanPasteToDirViewFromClipBoard())
  8278. {
  8279. Result = true;
  8280. }
  8281. else
  8282. {
  8283. UnicodeString ClipboardText;
  8284. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8285. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8286. {
  8287. if (StoredSessions->IsUrl(ClipboardText))
  8288. {
  8289. Result = true;
  8290. }
  8291. else
  8292. {
  8293. Result = DirViewEnabled(osCurrent);
  8294. }
  8295. }
  8296. }
  8297. return Result;
  8298. }
  8299. //---------------------------------------------------------------------------
  8300. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8301. {
  8302. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8303. if (DoesClipboardContainOurFiles())
  8304. {
  8305. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8306. {
  8307. DebugAssert(!IsLocalBrowserMode());
  8308. TTerminalManager * Manager = TTerminalManager::Instance();
  8309. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8310. Manager->ActiveSession = FClipboardTerminal;
  8311. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8312. }
  8313. }
  8314. else if (CanPasteToDirViewFromClipBoard())
  8315. {
  8316. DirView(osCurrent)->PasteFromClipBoard();
  8317. }
  8318. else
  8319. {
  8320. UnicodeString ClipboardText;
  8321. if (NonEmptyTextFromClipboard(ClipboardText))
  8322. {
  8323. if (StoredSessions->IsUrl(ClipboardText))
  8324. {
  8325. NewSession(ClipboardText);
  8326. }
  8327. else
  8328. {
  8329. DirView(osCurrent)->Path = ClipboardText;
  8330. }
  8331. }
  8332. }
  8333. }
  8334. //---------------------------------------------------------------------------
  8335. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8336. {
  8337. // TBD
  8338. }
  8339. //---------------------------------------------------------------------------
  8340. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8341. {
  8342. TCustomDirView * ADirView = DirView(Side);
  8343. ADirView->SelectAll(Mode);
  8344. ADirView->SetFocus();
  8345. }
  8346. //---------------------------------------------------------------------------
  8347. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8348. {
  8349. TCustomDirView * ADirView = DirView(Side);
  8350. TFileFilter Filter;
  8351. DefaultFileFilter(Filter);
  8352. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8353. {
  8354. Configuration->Usage->Inc(L"MaskSelections");
  8355. ADirView->SelectFiles(Filter, Select);
  8356. ADirView->SetFocus();
  8357. }
  8358. }
  8359. //---------------------------------------------------------------------------
  8360. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8361. {
  8362. TCustomDirView * ADirView = DirView(Side);
  8363. ADirView->RestoreSelectedNames();
  8364. ADirView->SetFocus();
  8365. }
  8366. //---------------------------------------------------------------------------
  8367. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8368. {
  8369. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8370. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8371. {
  8372. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8373. UnicodeString Ext;
  8374. if (!IsSideLocalBrowser(osCurrent))
  8375. {
  8376. Ext = UnixExtractFileExt(FileName);
  8377. }
  8378. else
  8379. {
  8380. Ext = ExtractFileExt(FileName);
  8381. }
  8382. if (Ext.IsEmpty())
  8383. {
  8384. Ext = L".";
  8385. }
  8386. TFileFilter Filter;
  8387. Filter.Masks = FORMAT(L"*%s", (Ext));
  8388. Filter.Directories = false;
  8389. CurrentDirView->SelectFiles(Filter, Select);
  8390. }
  8391. }
  8392. //---------------------------------------------------------------------------
  8393. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8394. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8395. {
  8396. UnicodeString Result;
  8397. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8398. {
  8399. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8400. }
  8401. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8402. {
  8403. // noop
  8404. }
  8405. else
  8406. {
  8407. Result =
  8408. FMTLOAD(FILE_INFO_FORMAT,
  8409. (FormatBytes(FileInfo.SelectedSize),
  8410. FormatBytes(FileInfo.FilesSize),
  8411. FormatNumber(FileInfo.SelectedCount),
  8412. FormatNumber(FileInfo.FilesCount)));
  8413. }
  8414. return Result;
  8415. }
  8416. //---------------------------------------------------------------------------
  8417. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8418. TTBXStatusPanel * Panel, TOperationSide Side)
  8419. {
  8420. if (Panel->Index == 1)
  8421. {
  8422. ToggleShowHiddenFiles();
  8423. }
  8424. else if (Panel->Index == 2)
  8425. {
  8426. Filter(Side);
  8427. }
  8428. }
  8429. //---------------------------------------------------------------------------
  8430. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8431. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8432. {
  8433. DebugAssert(!StatusBar->SimplePanel);
  8434. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8435. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8436. }
  8437. //---------------------------------------------------------------------------
  8438. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8439. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8440. {
  8441. DebugAssert(StatusBar->Panels->Count >= 3);
  8442. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8443. if (FileInfo.HiddenCount > 0)
  8444. {
  8445. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8446. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8447. }
  8448. else
  8449. {
  8450. HiddenFilesPanel->ViewPriority = 0;
  8451. // not really necessary, just to cleanup no-longer-valid data
  8452. HiddenFilesPanel->Caption = L"";
  8453. }
  8454. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8455. if (FileInfo.FilteredCount > 0)
  8456. {
  8457. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8458. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8459. }
  8460. else
  8461. {
  8462. FilteredFilesPanel->ViewPriority = 0;
  8463. // not really necessary, just to cleanup no-longer-valid data
  8464. FilteredFilesPanel->Caption = L"";
  8465. }
  8466. }
  8467. //---------------------------------------------------------------------------
  8468. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8469. TObject * /*Sender*/)
  8470. {
  8471. if (DirView(osOther)->Enabled)
  8472. {
  8473. DirView(osOther)->SetFocus();
  8474. }
  8475. }
  8476. //---------------------------------------------------------------------------
  8477. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8478. {
  8479. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8480. switch (Config.Show)
  8481. {
  8482. case qvShow:
  8483. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8484. {
  8485. Config.Show = qvHide;
  8486. }
  8487. else
  8488. {
  8489. Config.Show = Config.LastHideShow;
  8490. }
  8491. break;
  8492. case qvHideWhenEmpty:
  8493. if (ComponentVisible[fcQueueView])
  8494. {
  8495. Config.Show = qvHide;
  8496. }
  8497. else
  8498. {
  8499. Config.LastHideShow = Config.Show;
  8500. Config.Show = qvShow;
  8501. }
  8502. break;
  8503. case qvHide:
  8504. Config.LastHideShow = Config.Show;
  8505. Config.Show = qvShow;
  8506. break;
  8507. }
  8508. WinConfiguration->QueueView = Config;
  8509. }
  8510. //---------------------------------------------------------------------------
  8511. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8512. {
  8513. UnicodeString Result;
  8514. if (Terminal != NULL)
  8515. {
  8516. switch (WinConfiguration->PathInCaption)
  8517. {
  8518. case picShort:
  8519. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8520. break;
  8521. case picFull:
  8522. Result = Terminal->CurrentDirectory;
  8523. break;
  8524. }
  8525. }
  8526. return Result;
  8527. }
  8528. //---------------------------------------------------------------------------
  8529. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8530. {
  8531. TColor Result = GetWindowColor(FSessionColor);
  8532. return Result;
  8533. }
  8534. //---------------------------------------------------------------------------
  8535. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8536. {
  8537. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8538. return Result;
  8539. }
  8540. //---------------------------------------------------------------------------
  8541. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8542. {
  8543. if (!FSessionChanging)
  8544. {
  8545. TTerminalManager::Instance()->UpdateAppTitle();
  8546. }
  8547. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8548. // background to gray on Windows 7 (works on Windows 10).
  8549. // See also EnableControl
  8550. if (Showing)
  8551. {
  8552. if (ManagedSession != NULL)
  8553. {
  8554. // Update path when it changes
  8555. if ((SessionsPageControl->ActivePage != NULL) &&
  8556. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8557. {
  8558. UpdateSessionTab(SessionsPageControl->ActivePage);
  8559. }
  8560. }
  8561. NonVisualDataModule->ReconnectSessionAction->Update();
  8562. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8563. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8564. CenterReconnectToolbar();
  8565. bool HasTerminal = HasActiveTerminal();
  8566. RemoteDirView->Enabled = HasTerminal;
  8567. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8568. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8569. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8570. RemoteDriveView->Color = RemoteDirView->Color;
  8571. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8572. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8573. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8574. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8575. QueueFileList->Enabled = QueueView3->Enabled;
  8576. QueueFileList->Color = QueueView3->Color;
  8577. QueueFileList->Font->Color = QueueView3->Font->Color;
  8578. QueueLabelUpdateStatus();
  8579. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8580. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8581. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8582. {
  8583. UpdateDarkMode();
  8584. }
  8585. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8586. MessageDock->Visible = (MessageDock->ToolbarCount > 0);
  8587. }
  8588. }
  8589. //---------------------------------------------------------------------------
  8590. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8591. {
  8592. UpdateControls();
  8593. }
  8594. //---------------------------------------------------------------------------
  8595. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8596. TObject * Sender)
  8597. {
  8598. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8599. DebugAssert(DirView != NULL);
  8600. DoDirViewLoaded(DirView);
  8601. TransferPresetAutoSelect();
  8602. }
  8603. //---------------------------------------------------------------------------
  8604. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8605. {
  8606. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8607. // first run after installation
  8608. if (double(Updates.LastCheck) == 0)
  8609. {
  8610. AppLog(L"First run, scheduling updates check for the next run");
  8611. // make sure next time there will be an update (if enabled)
  8612. Updates.LastCheck = TDateTime(1);
  8613. WinConfiguration->Updates = Updates;
  8614. }
  8615. else
  8616. {
  8617. TDateTime Period;
  8618. if (IsUWP() && !IsOfficialPackage())
  8619. {
  8620. Period = DefaultUpdatesPeriod;
  8621. AppLog(L"Thirdparty UWP package, using default updates check period");
  8622. }
  8623. else
  8624. {
  8625. Period = Updates.Period;
  8626. }
  8627. AppLogFmt(L"Updates check period: %.2f", (double(Period)));
  8628. if (double(Period) > 0)
  8629. {
  8630. TDateTime Interval = Now() - Updates.LastCheck;
  8631. AppLogFmt(L"Interval since the last updates check: %.2f", (double(Interval)));
  8632. if (Interval >= Period)
  8633. {
  8634. StartUpdateThread(UpdatesChecked);
  8635. }
  8636. }
  8637. }
  8638. }
  8639. //---------------------------------------------------------------------------
  8640. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8641. {
  8642. AppLog(L"Updates check done");
  8643. UnicodeString Message;
  8644. bool New;
  8645. TQueryType Type;
  8646. GetUpdatesMessage(Message, New, Type, false);
  8647. if (!Message.IsEmpty())
  8648. {
  8649. AppLogFmt(L"Updates check message: %s", (Message));
  8650. if (New)
  8651. {
  8652. AppLog(L"New version detected");
  8653. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8654. }
  8655. if (!New && (Type != qtWarning))
  8656. {
  8657. AppLog(L"Posting non-critical updates note");
  8658. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8659. }
  8660. else
  8661. {
  8662. AppLog(L"Posting new version or critical message notification");
  8663. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8664. }
  8665. if (New)
  8666. {
  8667. Configuration->Usage->Inc(L"UpdateNotifications");
  8668. }
  8669. }
  8670. }
  8671. //---------------------------------------------------------------------------
  8672. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8673. {
  8674. RestoreApp();
  8675. bool CanDisplay = !NonVisualDataModule->Busy;
  8676. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8677. {
  8678. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8679. }
  8680. if (CanDisplay)
  8681. {
  8682. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8683. // Probably not needed as CheckForUpdates is blocking since we started using neon for HTTP implementation.
  8684. NonVisualDataModule->StartBusy();
  8685. try
  8686. {
  8687. CheckForUpdates(true);
  8688. }
  8689. __finally
  8690. {
  8691. NonVisualDataModule->EndBusy();
  8692. }
  8693. }
  8694. else
  8695. {
  8696. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8697. }
  8698. }
  8699. //---------------------------------------------------------------------------
  8700. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8701. TCopyParamRuleData & Data)
  8702. {
  8703. DebugAssert(Terminal != NULL);
  8704. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8705. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8706. Data.RemoteDirectory = RemoteDirView->PathName;
  8707. }
  8708. //---------------------------------------------------------------------------
  8709. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8710. {
  8711. // Terminal can be null when we are changing local directory implicitly,
  8712. // because it has been deleted, while no session is connected
  8713. // (Login dialog is open)
  8714. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8715. {
  8716. DebugAssert(!IsLocalBrowserMode());
  8717. TCopyParamRuleData Data;
  8718. GetTransferPresetAutoSelectData(Data);
  8719. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8720. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8721. if (CopyParamIndex < 0)
  8722. {
  8723. // there is no preset that matches autoselection
  8724. // set preset that we consider "default"
  8725. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8726. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8727. }
  8728. else
  8729. {
  8730. // there is preset matching autoselection
  8731. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8732. if (CopyParamName == FCopyParamAutoSelected)
  8733. {
  8734. // autoselected the same preset as the last time
  8735. // make no change (i.e. preserve custom user preset, if any)
  8736. }
  8737. else
  8738. {
  8739. // autoselected the different preset then the last time (or there
  8740. // was default preset set)
  8741. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8742. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8743. }
  8744. }
  8745. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8746. {
  8747. if (CopyParamIndex >= 0)
  8748. {
  8749. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8750. }
  8751. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8752. try
  8753. {
  8754. int Fmt =
  8755. (CopyParamIndex < 0) ?
  8756. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8757. COPY_PARAM_AUTOSELECTED;
  8758. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8759. Data->Message = MainInstructions(Message);
  8760. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8761. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8762. CopyParamAttrs);
  8763. if (CopyParamIndex >= 0)
  8764. {
  8765. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8766. Info = FORMAT(L"%s\n \n%s", (Info,
  8767. FMTLOAD(COPY_PARAM_RULE,
  8768. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8769. }
  8770. Data->Message += L"\n\n" + Info;
  8771. if (WinConfiguration->CopyParamAutoSelectNotice)
  8772. {
  8773. TransferPresetNoteMessage(Data, true);
  8774. }
  8775. else
  8776. {
  8777. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8778. Data = NULL; // ownership passed
  8779. }
  8780. }
  8781. __finally
  8782. {
  8783. delete Data;
  8784. }
  8785. }
  8786. }
  8787. }
  8788. //---------------------------------------------------------------------------
  8789. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8790. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8791. {
  8792. DebugAssert(NoteData != NULL);
  8793. DebugAssert(!IsLocalBrowserMode());
  8794. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8795. TQueryButtonAlias Aliases[1];
  8796. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8797. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8798. Params.Aliases = Aliases;
  8799. Params.AliasesCount = LENOF(Aliases);
  8800. unsigned int Result =
  8801. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8802. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8803. switch (Result)
  8804. {
  8805. case qaNeverAskAgain:
  8806. DebugAssert(AllowNeverAskAgain);
  8807. WinConfiguration->CopyParamAutoSelectNotice = false;
  8808. break;
  8809. case qaIgnore:
  8810. PreferencesDialog(pmPresets);
  8811. break;
  8812. }
  8813. }
  8814. //---------------------------------------------------------------------------
  8815. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8816. {
  8817. // as of now useless, as this is used for notes only, never for balloons, ...
  8818. RestoreApp();
  8819. // .. and we should never be busy here
  8820. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8821. {
  8822. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8823. }
  8824. }
  8825. //---------------------------------------------------------------------------
  8826. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8827. TPreferencesMode APreferencesMode)
  8828. {
  8829. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8830. TCopyParamRuleData Data;
  8831. if (Terminal != NULL)
  8832. {
  8833. PreferencesData.reset(new TPreferencesDialogData());
  8834. GetTransferPresetAutoSelectData(Data);
  8835. PreferencesData->CopyParamRuleData = &Data;
  8836. }
  8837. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8838. }
  8839. //---------------------------------------------------------------------------
  8840. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8841. const TCustomCommandType & Command)
  8842. {
  8843. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8844. {
  8845. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8846. }
  8847. }
  8848. //---------------------------------------------------------------------------
  8849. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8850. {
  8851. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8852. TCustomCommandType Command;
  8853. // make sure we use local custom command when remote are not supported
  8854. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8855. {
  8856. Command = FLastCustomCommand;
  8857. }
  8858. else
  8859. {
  8860. Command.Params = Command.Params | ccLocal;
  8861. }
  8862. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8863. FEditingFocusedAdHocCommand = OnFocused;
  8864. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8865. int Options =
  8866. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8867. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8868. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8869. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8870. {
  8871. FLastCustomCommand = Command;
  8872. UpdateCustomCommandsToolbar();
  8873. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8874. }
  8875. }
  8876. //---------------------------------------------------------------------------
  8877. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8878. {
  8879. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8880. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8881. DebugAssert(State > 0);
  8882. if (State <= 0)
  8883. {
  8884. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8885. }
  8886. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8887. }
  8888. //---------------------------------------------------------------------------
  8889. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8890. TCustomCommandType & Command, int & State)
  8891. {
  8892. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8893. if (Result)
  8894. {
  8895. Command = FLastCustomCommand;
  8896. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8897. }
  8898. return Result;
  8899. }
  8900. //---------------------------------------------------------------------------
  8901. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8902. {
  8903. ResetIncrementalSearch();
  8904. }
  8905. //---------------------------------------------------------------------------
  8906. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8907. {
  8908. DebugAssert(ComponentVisible[Component]);
  8909. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8910. }
  8911. //---------------------------------------------------------------------------
  8912. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8913. {
  8914. // when queue panel is resized here directly, the status bar is stretched
  8915. // over whole space the panel occupied
  8916. PostComponentHide(fcQueueView);
  8917. }
  8918. //---------------------------------------------------------------------------
  8919. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8920. {
  8921. ComponentVisible[fcQueueFileList] = false;
  8922. }
  8923. //---------------------------------------------------------------------------
  8924. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8925. {
  8926. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8927. RefreshColorMode();
  8928. ResetSysDarkTheme();
  8929. ConfigurationChanged();
  8930. ConfigureInterface();
  8931. // Should be called for all controls
  8932. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8933. }
  8934. //---------------------------------------------------------------------------
  8935. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8936. {
  8937. // Do not handle, when shutting down anyway (maybe also when not setup completely yet?)
  8938. if (!FInvalid &&
  8939. (Message.LParam != 0) &&
  8940. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8941. {
  8942. ThemeChanged();
  8943. }
  8944. TForm::Dispatch(&Message);
  8945. }
  8946. //---------------------------------------------------------------------------
  8947. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8948. {
  8949. TMessage * M = static_cast<TMessage*>(Message);
  8950. switch (M->Msg)
  8951. {
  8952. case CM_DIALOGCHAR:
  8953. CMDialogChar(*M);
  8954. break;
  8955. case WM_APPCOMMAND:
  8956. WMAppCommand(*M);
  8957. break;
  8958. case WM_SYSCOMMAND:
  8959. WMSysCommand(*M);
  8960. break;
  8961. case WM_QUERYENDSESSION:
  8962. WMQueryEndSession(*M);
  8963. break;
  8964. case WM_ENDSESSION:
  8965. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8966. break;
  8967. #ifdef _DEBUG
  8968. case WM_WINDOWPOSCHANGED:
  8969. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8970. break;
  8971. case CM_CUSTOMSTYLECHANGED:
  8972. TForm::Dispatch(Message);
  8973. break;
  8974. #endif
  8975. case WM_COMPONENT_HIDE:
  8976. {
  8977. Byte Component = static_cast<Byte>(M->WParam);
  8978. // sanity check
  8979. if (ComponentVisible[Component])
  8980. {
  8981. // special treatment
  8982. if (Component == fcQueueView)
  8983. {
  8984. ToggleQueueVisibility();
  8985. DebugAssert(!ComponentVisible[fcQueueView]);
  8986. }
  8987. else
  8988. {
  8989. ComponentVisible[Component] = false;
  8990. }
  8991. }
  8992. }
  8993. break;
  8994. case WM_COPYDATA:
  8995. WMCopyData(*M);
  8996. break;
  8997. case WM_MANAGES_CAPTION:
  8998. // caption managed in TTerminalManager::UpdateAppTitle()
  8999. M->Result = 1;
  9000. break;
  9001. case WM_WANTS_MOUSEWHEEL:
  9002. M->Result = 1;
  9003. break;
  9004. case CM_SHOWINGCHANGED:
  9005. CMShowingChanged(*M);
  9006. break;
  9007. case WM_CLOSE:
  9008. WMClose(*M);
  9009. break;
  9010. case WM_DPICHANGED:
  9011. WMDpiChanged(*M);
  9012. break;
  9013. case CM_DPICHANGED:
  9014. CMDpiChanged(*M);
  9015. break;
  9016. case WM_SETTINGCHANGE:
  9017. WMSettingChange(*M);
  9018. break;
  9019. case CM_DIALOGKEY:
  9020. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  9021. break;
  9022. default:
  9023. TForm::Dispatch(Message);
  9024. break;
  9025. }
  9026. }
  9027. //---------------------------------------------------------------------------
  9028. void __fastcall TCustomScpExplorerForm::UpdateImages()
  9029. {
  9030. // noop
  9031. }
  9032. //---------------------------------------------------------------------------
  9033. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  9034. {
  9035. TForm::Dispatch(&Message);
  9036. FSessionColors->Clear();
  9037. AddFixedSessionImages();
  9038. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  9039. UpdateImages();
  9040. }
  9041. //---------------------------------------------------------------------------
  9042. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  9043. {
  9044. TForm::Dispatch(&Message);
  9045. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  9046. UpdatePixelsPerInchMainWindowCounter();
  9047. }
  9048. //---------------------------------------------------------------------------
  9049. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  9050. {
  9051. // Cannot close window while we are busy.
  9052. // We cannot test this in FormCloseQuery as that is called also from
  9053. // Close(), which is called by CloseApplicationAction. So we can be busy
  9054. // there already even, when it is legitimate to close the application.
  9055. // Possibly a better place to handle this would be WMSysCommand.
  9056. if (NonVisualDataModule->Busy)
  9057. {
  9058. Message.Result = 1;
  9059. }
  9060. else
  9061. {
  9062. TForm::Dispatch(&Message);
  9063. }
  9064. }
  9065. //---------------------------------------------------------------------------
  9066. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  9067. {
  9068. if (!FShowingChanged)
  9069. {
  9070. AddStartupSequence(L"D");
  9071. FShowingChanged = true;
  9072. }
  9073. // Ignoring implicit local-local browser of Commander, opened from DoShow
  9074. bool NoSession = (ManagedSession == NULL);
  9075. // This calls TCustomScpExplorerForm::DoShow
  9076. TForm::Dispatch(&Message);
  9077. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  9078. if (Showing && !FStarted)
  9079. {
  9080. FStarted = true;
  9081. InterfaceStarted();
  9082. }
  9083. if (Showing && NoSession)
  9084. {
  9085. // When we are starting minimized (i.e. from an installer),
  9086. // postpone showing Login dialog until we get restored.
  9087. // Otherwise the Login dialog (and Authentication window) show restored
  9088. // over invisible (minimized) main window.
  9089. if (WindowState == wsMinimized)
  9090. {
  9091. FNeedSession = true;
  9092. }
  9093. else
  9094. {
  9095. // This happens before application ever goes idle, so the toolbars would
  9096. // stay enabled (initial state) until the Login dialog is dismissed.
  9097. UpdateActions();
  9098. NonVisualDataModule->StartBusy();
  9099. try
  9100. {
  9101. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  9102. // otherwise the dialog does not receive focus.
  9103. // With Commander interface the ProcessMessages is called already
  9104. // by TDriveView, but with Explorer interface, we need to call it explicily
  9105. Application->ProcessMessages();
  9106. // do not reload sessions, they have been loaded just now (optimization)
  9107. NeedSession(true);
  9108. }
  9109. __finally
  9110. {
  9111. NonVisualDataModule->EndBusy();
  9112. }
  9113. }
  9114. }
  9115. }
  9116. //---------------------------------------------------------------------------
  9117. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  9118. {
  9119. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  9120. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  9121. }
  9122. //---------------------------------------------------------------------------
  9123. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  9124. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  9125. int & MaxHeight)
  9126. {
  9127. // workaround for bug in TWinControl.CalcConstraints
  9128. // Check for empty rect (restore from iconic state) is done there only after
  9129. // call to AdjustClientRect, which enlarges the rect (for forms).
  9130. TRect R = GetClientRect();
  9131. // when restoring from iconic state, place no restrictions
  9132. if (IsRectEmpty(R))
  9133. {
  9134. MinWidth = 0;
  9135. MinHeight = 0;
  9136. MaxWidth = 32000;
  9137. MaxHeight = 32000;
  9138. }
  9139. }
  9140. //---------------------------------------------------------------------------
  9141. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  9142. TSpaceAvailable & ASpaceAvailable, bool & Close)
  9143. {
  9144. // terminal can be already closed (e.g. dropped connection)
  9145. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  9146. {
  9147. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  9148. try
  9149. {
  9150. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  9151. }
  9152. catch(...)
  9153. {
  9154. if (!Terminal->Active)
  9155. {
  9156. Close = true;
  9157. }
  9158. throw;
  9159. }
  9160. }
  9161. }
  9162. //---------------------------------------------------------------------------
  9163. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  9164. {
  9165. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  9166. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  9167. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  9168. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  9169. {
  9170. OnGetSpaceAvailable = GetSpaceAvailable;
  9171. }
  9172. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  9173. OnGetSpaceAvailable);
  9174. }
  9175. //---------------------------------------------------------------------------
  9176. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  9177. {
  9178. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  9179. Terminal->FillSessionDataForCode(Data.get());
  9180. return Data.release();
  9181. }
  9182. //---------------------------------------------------------------------------
  9183. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  9184. {
  9185. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  9186. DoGenerateUrlDialog(Data.get(), Paths);
  9187. }
  9188. //---------------------------------------------------------------------------
  9189. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  9190. {
  9191. GenerateUrl(NULL);
  9192. }
  9193. //---------------------------------------------------------------------------
  9194. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  9195. {
  9196. std::unique_ptr<TStrings> Paths(new TStringList());
  9197. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  9198. GenerateUrl(Paths.get());
  9199. }
  9200. //---------------------------------------------------------------------------
  9201. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  9202. {
  9203. FSessionColor = value;
  9204. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  9205. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  9206. static_cast<TObject *>(GetComponent(fcColorMenu)));
  9207. DebugAssert(ColorItem != NULL);
  9208. ColorItem->Color = C;
  9209. NonVisualDataModule->ColorMenuItem->Color = C;
  9210. UpdateControls();
  9211. }
  9212. //---------------------------------------------------------------------------
  9213. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  9214. {
  9215. if (value != FSessionColor)
  9216. {
  9217. UpdateSessionColor(value);
  9218. // Is null when called from LastTerminalClosed
  9219. if (Terminal != NULL)
  9220. {
  9221. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  9222. }
  9223. }
  9224. }
  9225. //---------------------------------------------------------------------------
  9226. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  9227. {
  9228. bool Result = FNoteTimer->Enabled;
  9229. if (Result)
  9230. {
  9231. // cannot cancel note too early
  9232. bool NotEarly =
  9233. (Now() - FNoteShown >
  9234. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  9235. if (Force || NotEarly)
  9236. {
  9237. FNoteTimer->Enabled = false;
  9238. FNote = L"";
  9239. SAFE_DESTROY(FNoteData);
  9240. FOnNoteClick = NULL;
  9241. FNoteHints = FNotes->Text;
  9242. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  9243. UpdateStatusBar();
  9244. }
  9245. }
  9246. return Result;
  9247. }
  9248. //---------------------------------------------------------------------------
  9249. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  9250. {
  9251. DebugAssert(FNoteTimer->Enabled);
  9252. CancelNote(true);
  9253. }
  9254. //---------------------------------------------------------------------------
  9255. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9256. {
  9257. FNoteHints = FNotes->Text.TrimRight();
  9258. }
  9259. //---------------------------------------------------------------------------
  9260. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9261. {
  9262. int P = Note.Pos(L"\n");
  9263. if (P > 0)
  9264. {
  9265. Note.SetLength(P - 1);
  9266. }
  9267. FNotes->Add(FORMAT(L"[%s] %s",
  9268. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9269. while (FNotes->Count > 10)
  9270. {
  9271. FNotes->Delete(0);
  9272. }
  9273. if (UpdateNow)
  9274. {
  9275. UpdateNoteHints();
  9276. UpdateStatusBar();
  9277. }
  9278. }
  9279. //---------------------------------------------------------------------------
  9280. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  9281. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9282. {
  9283. int P = Note.Pos(L"\n");
  9284. if (P > 0)
  9285. {
  9286. Note.SetLength(P - 1);
  9287. }
  9288. UpdateNoteHints();
  9289. FNote = Note;
  9290. SAFE_DESTROY(FNoteData);
  9291. FNoteData = NoteData;
  9292. FOnNoteClick = OnNoteClick;
  9293. AddNote(Note, false);
  9294. UpdateStatusBar();
  9295. FNoteShown = Now();
  9296. FNoteTimer->Enabled = false;
  9297. if (Seconds == 0)
  9298. {
  9299. Seconds = WinConfiguration->NotificationsTimeout;
  9300. }
  9301. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9302. FNoteTimer->Enabled = true;
  9303. }
  9304. //---------------------------------------------------------------------------
  9305. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9306. {
  9307. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9308. }
  9309. //---------------------------------------------------------------------------
  9310. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9311. {
  9312. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9313. {
  9314. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9315. }
  9316. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9317. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9318. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9319. 0, NULL, NULL);
  9320. }
  9321. //---------------------------------------------------------------------------
  9322. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9323. {
  9324. bool NewShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9325. UnicodeString Note =
  9326. FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1), (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, (NewShowHiddenFiles ? 2 : 3))));
  9327. PostNote(Note, 0, NULL, NULL);
  9328. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9329. WinConfiguration->ShowHiddenFiles = NewShowHiddenFiles;
  9330. }
  9331. //---------------------------------------------------------------------------
  9332. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9333. {
  9334. WinConfiguration->FormatSizeBytes = Style;
  9335. }
  9336. //---------------------------------------------------------------------------
  9337. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9338. {
  9339. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9340. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9341. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9342. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9343. }
  9344. //---------------------------------------------------------------------------
  9345. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9346. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9347. {
  9348. if (Panel->Index == 0)
  9349. {
  9350. if (FOnNoteClick != NULL)
  9351. {
  9352. // prevent the user data from being freed by possible call
  9353. // to CancelNote or PostNote during call to OnNoteClick
  9354. std::unique_ptr<TObject> NoteData(FNoteData);
  9355. TNotifyEvent OnNoteClick = FOnNoteClick;
  9356. FNoteData = NULL;
  9357. // need to cancel the note as we are going to delete its user data
  9358. CancelNote(true);
  9359. OnNoteClick(NoteData.get());
  9360. }
  9361. }
  9362. if (HasActiveTerminal() &&
  9363. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9364. {
  9365. FileSystemInfo();
  9366. }
  9367. }
  9368. //---------------------------------------------------------------------------
  9369. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9370. {
  9371. // workaround:
  9372. // 1) for unknown reason, disabling window, while minimized,
  9373. // prevents it from restoring, even if it was enabled again meanwhile
  9374. // 2) when disabling the main window, while another has focus
  9375. // minimize is no longer possible
  9376. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9377. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9378. // Shouldn't we use IsApplicationMinimized() here?
  9379. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9380. {
  9381. Enabled = false;
  9382. }
  9383. FLockLevel++;
  9384. }
  9385. //---------------------------------------------------------------------------
  9386. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9387. {
  9388. DebugAssert(FLockLevel > 0);
  9389. FLockLevel--;
  9390. if (FLockLevel == 0)
  9391. {
  9392. DebugAssert(FLockSuspendLevel == 0);
  9393. Enabled = true;
  9394. }
  9395. }
  9396. //---------------------------------------------------------------------------
  9397. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9398. {
  9399. // We need to make sure that window is enabled when the last modal window closes
  9400. // otherwise focus is not restored correctly.
  9401. // So we re-enable the window when modal window opens and
  9402. // disable it back after is closes
  9403. if (FLockLevel > 0)
  9404. {
  9405. // while we have nesting counter, we know that we never be called
  9406. // recursivelly as Application->OnModalBegin is called only
  9407. // for the top-level modal window
  9408. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9409. {
  9410. // won't be disabled when conditions in LockWindow() were not satisfied
  9411. FDisabledOnLockSuspend = !Enabled;
  9412. // When minimized to tray (or actually when set to SW_HIDE),
  9413. // setting Enabled makes the window focusable even when there's
  9414. // modal window over it
  9415. if (!FTrayIcon->Visible)
  9416. {
  9417. Enabled = true;
  9418. }
  9419. }
  9420. FLockSuspendLevel++;
  9421. }
  9422. }
  9423. //---------------------------------------------------------------------------
  9424. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9425. {
  9426. if (FLockSuspendLevel > 0)
  9427. {
  9428. DebugAssert(FLockLevel > 0);
  9429. FLockSuspendLevel--;
  9430. // see comment in SuspendWindowLock
  9431. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9432. {
  9433. // Note that window can be enabled here, when we were minized to tray when
  9434. // was SuspendWindowLock() called.
  9435. // We should possibly do the same check as in LockWindow(),
  9436. // if it is ever possible that the consitions change between
  9437. // SuspendWindowLock() and ResumeWindowLock()
  9438. Enabled = !FDisabledOnLockSuspend;
  9439. }
  9440. }
  9441. }
  9442. //---------------------------------------------------------------------------
  9443. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9444. {
  9445. TTBXComboBoxItem * RemotePathComboBox =
  9446. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9447. if (!TextOnly)
  9448. {
  9449. TStrings * Items = RemotePathComboBox->Strings;
  9450. Items->BeginUpdate();
  9451. try
  9452. {
  9453. Items->Clear();
  9454. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9455. {
  9456. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9457. while (!IsUnixRootPath(APath))
  9458. {
  9459. int P = APath.LastDelimiter(L'/');
  9460. DebugAssert(P >= 0);
  9461. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9462. APath.SetLength(P - 1);
  9463. }
  9464. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9465. }
  9466. }
  9467. __finally
  9468. {
  9469. Items->EndUpdate();
  9470. }
  9471. }
  9472. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9473. // Setting ItemIndex to -1 does not reset its text
  9474. if (RemotePathComboBox->Strings->Count == 0)
  9475. {
  9476. RemotePathComboBox->Text = L"";
  9477. }
  9478. }
  9479. //---------------------------------------------------------------------------
  9480. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9481. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9482. {
  9483. if (AIndex < 0)
  9484. {
  9485. AIndex = Sender->ItemIndex;
  9486. }
  9487. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9488. }
  9489. //---------------------------------------------------------------------------
  9490. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9491. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9492. int & ImageIndex)
  9493. {
  9494. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9495. }
  9496. //---------------------------------------------------------------------------
  9497. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9498. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9499. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9500. {
  9501. if (!IsLocalBrowserMode())
  9502. {
  9503. ARect.Left += (10 * AIndex);
  9504. }
  9505. }
  9506. //---------------------------------------------------------------------------
  9507. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9508. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9509. {
  9510. AWidth += (10 * AIndex);
  9511. }
  9512. //---------------------------------------------------------------------------
  9513. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9514. {
  9515. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9516. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9517. int Index = RemotePathComboBox->ItemIndex;
  9518. while (Index < RemotePathComboBox->Strings->Count - 1)
  9519. {
  9520. APath = UnixExtractFileDir(APath);
  9521. Index++;
  9522. }
  9523. // VanDyke style paths
  9524. if (APath.IsEmpty())
  9525. {
  9526. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9527. APath = ROOTDIRECTORY;
  9528. }
  9529. if (RemoteDirView->Path != APath)
  9530. {
  9531. RemoteDirView->Path = APath;
  9532. }
  9533. }
  9534. //---------------------------------------------------------------------------
  9535. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9536. {
  9537. DoRemotePathComboBoxItemClick(Sender);
  9538. }
  9539. //---------------------------------------------------------------------------
  9540. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9541. {
  9542. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9543. DebugUsedParam(Sender);
  9544. UpdateRemotePathComboBox(true);
  9545. }
  9546. //---------------------------------------------------------------------------
  9547. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9548. {
  9549. DoRemotePathComboBoxCancel(Sender);
  9550. }
  9551. //---------------------------------------------------------------------------
  9552. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9553. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9554. {
  9555. ResetIncrementalSearch();
  9556. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9557. DebugAssert(DirView != NULL);
  9558. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9559. {
  9560. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9561. // OnEditing is called also from TCustomListView::CanEdit
  9562. if (Edit != NULL)
  9563. {
  9564. EditSelectBaseName(Edit);
  9565. }
  9566. }
  9567. }
  9568. //---------------------------------------------------------------------------
  9569. //---------------------------------------------------------------------------
  9570. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9571. {
  9572. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9573. Result->AutoDetectDnD = false;
  9574. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9575. Result->RenderDataOn = rdoEnterAndDropSync;
  9576. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9577. return Result;
  9578. }
  9579. //---------------------------------------------------------------------------
  9580. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9581. {
  9582. if (IsWin10Build(2004))
  9583. {
  9584. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9585. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9586. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9587. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9588. }
  9589. }
  9590. //---------------------------------------------------------------------------
  9591. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9592. {
  9593. TForm::CreateWnd();
  9594. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9595. UpdateDarkMode();
  9596. if (FSessionsDragDropFilesEx == NULL)
  9597. {
  9598. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9599. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9600. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9601. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9602. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9603. }
  9604. if (FQueueDragDropFilesEx == NULL)
  9605. {
  9606. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9607. // No need to set OnDragOver as we do not have any restrictions
  9608. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9609. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9610. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9611. }
  9612. }
  9613. //---------------------------------------------------------------------------
  9614. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9615. {
  9616. TForm::DestroyWnd();
  9617. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9618. FQueueDragDropFilesEx->DragDropControl = NULL;
  9619. }
  9620. //---------------------------------------------------------------------------
  9621. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9622. {
  9623. SideEnter(FCurrentSide);
  9624. FEverShown = true;
  9625. }
  9626. //---------------------------------------------------------------------------
  9627. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9628. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9629. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9630. {
  9631. if (!NonVisualDataModule->Busy)
  9632. {
  9633. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9634. Configuration->Usage->Inc(L"FileFinds");
  9635. LockWindow(true);
  9636. NonVisualDataModule->StartBusy();
  9637. WinConfiguration->LockedInterface = true;
  9638. try
  9639. {
  9640. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9641. }
  9642. __finally
  9643. {
  9644. WinConfiguration->LockedInterface = false;
  9645. NonVisualDataModule->EndBusy();
  9646. UnlockWindow();
  9647. }
  9648. }
  9649. }
  9650. //---------------------------------------------------------------------------
  9651. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9652. {
  9653. if (!NonVisualDataModule->Busy)
  9654. {
  9655. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9656. SetFocus();
  9657. RemoteDirView->Path = UnixExtractFilePath(Path);
  9658. UnicodeString FileName = UnixExtractFileName(Path);
  9659. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9660. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9661. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9662. if (Item == NULL)
  9663. {
  9664. ReloadDirectory(osRemote);
  9665. Item = RemoteDirView->FindFileItem(FileName);
  9666. }
  9667. if (Item != NULL)
  9668. {
  9669. RemoteDirView->ItemFocused = Item;
  9670. RemoteDirView->ItemFocused->MakeVisible(false);
  9671. RemoteDirView->SetFocus();
  9672. }
  9673. }
  9674. }
  9675. //---------------------------------------------------------------------------
  9676. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9677. {
  9678. bool Result = !NonVisualDataModule->Busy;
  9679. if (Result)
  9680. {
  9681. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9682. }
  9683. return Result;
  9684. }
  9685. //---------------------------------------------------------------------------
  9686. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9687. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9688. {
  9689. if (CanOperateOnFoundFiles(ATerminal))
  9690. {
  9691. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9692. FOnFileOperationFinished = OnFileOperationFinished;
  9693. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9694. }
  9695. }
  9696. //---------------------------------------------------------------------------
  9697. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9698. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9699. {
  9700. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9701. }
  9702. //---------------------------------------------------------------------------
  9703. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9704. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9705. {
  9706. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9707. }
  9708. //---------------------------------------------------------------------------
  9709. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9710. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9711. {
  9712. if (CanOperateOnFoundFiles(ATerminal))
  9713. {
  9714. for (int Index = 0; Index < FileList->Count; Index++)
  9715. {
  9716. UnicodeString FileName = FileList->Strings[Index];
  9717. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9718. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9719. OnFileOperationFinished(osRemote, FileName, true);
  9720. }
  9721. }
  9722. }
  9723. //---------------------------------------------------------------------------
  9724. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9725. {
  9726. FFileFindTerminal = Terminal;
  9727. ShowFileFindDialog(
  9728. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9729. DoDownloadFoundFiles, DoEditFoundFiles);
  9730. }
  9731. //---------------------------------------------------------------------------
  9732. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9733. {
  9734. FTaskbarList = TaskbarList;
  9735. }
  9736. //---------------------------------------------------------------------------
  9737. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9738. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9739. {
  9740. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9741. if (Index >= 0)
  9742. {
  9743. if (Button == mbLeft)
  9744. {
  9745. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9746. // If switching tab (switching session) takes long enough for user
  9747. // to actually release the button, "mouse down" is still raised,
  9748. // but we do not get "mouse up" event, so dragging is not cancelled,
  9749. // prevent that by not beginning dragging in the first place.
  9750. int Key = ((GetSystemMetrics(SM_SWAPBUTTON) == 0) ? VK_LBUTTON : VK_RBUTTON);
  9751. if (IsKeyPressed(Key))
  9752. {
  9753. // when user clicks the "+", we get mouse down only after the session
  9754. // is closed, when new session tab is already on X:Y, so dragging
  9755. // starts, prevent that
  9756. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9757. {
  9758. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9759. if (Terminal != NULL)
  9760. {
  9761. SessionsPageControl->BeginDrag(false);
  9762. }
  9763. }
  9764. }
  9765. }
  9766. else if (Button == mbMiddle)
  9767. {
  9768. // ignore middle-click for "New session tab" and the last local-local tab
  9769. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9770. if ((Session != NULL) && CanCloseSession(Session))
  9771. {
  9772. CloseSessionTab(Index);
  9773. }
  9774. }
  9775. }
  9776. }
  9777. //---------------------------------------------------------------------------
  9778. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9779. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9780. {
  9781. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9782. // do not allow dropping on the "+" tab
  9783. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9784. if ((TargetTerminal != NULL) &&
  9785. (SessionsPageControl->ActivePage->PageIndex != Index))
  9786. {
  9787. Configuration->Usage->Inc(L"SessionTabMoves");
  9788. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9789. // but we want to actually prevent that to avoid flicker
  9790. SessionsPageControl->ActivePage->PageIndex = Index;
  9791. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9792. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9793. }
  9794. }
  9795. //---------------------------------------------------------------------------
  9796. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9797. TObject * Sender, TObject * Source, int X, int Y,
  9798. TDragState /*State*/, bool & Accept)
  9799. {
  9800. Accept = (Sender == Source);
  9801. if (Accept)
  9802. {
  9803. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9804. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9805. // do not allow dragging to the "+" tab
  9806. Accept = (Terminal != NULL);
  9807. }
  9808. }
  9809. //---------------------------------------------------------------------------
  9810. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9811. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9812. {
  9813. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9814. if (Index < 0)
  9815. {
  9816. Effect = DROPEFFECT_NONE;
  9817. }
  9818. else
  9819. {
  9820. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9821. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9822. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9823. {
  9824. Effect = DROPEFFECT_NONE;
  9825. }
  9826. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9827. {
  9828. Effect = DROPEFFECT_COPY;
  9829. }
  9830. }
  9831. }
  9832. //---------------------------------------------------------------------------
  9833. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9834. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9835. {
  9836. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9837. // do not allow dropping on local-local, disconnected and the "+" tab
  9838. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9839. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9840. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9841. {
  9842. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9843. if (!IsFileControl(DropSourceControl, osRemote))
  9844. {
  9845. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9846. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9847. // Why don't we use Terminal->CurrentDirectory directly?
  9848. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9849. // do not force queue, drag drop
  9850. false, true);
  9851. }
  9852. }
  9853. }
  9854. //---------------------------------------------------------------------------
  9855. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9856. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9857. {
  9858. // Downloads are handled in RemoteFileControlDDEnd
  9859. if (!IsFileControl(DropSourceControl, osRemote))
  9860. {
  9861. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9862. Terminal->CurrentDirectory,
  9863. // force queue, drag drop
  9864. true, true);
  9865. }
  9866. }
  9867. //---------------------------------------------------------------------------
  9868. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9869. {
  9870. FShowing = false;
  9871. // Do not save empty workspace
  9872. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9873. {
  9874. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9875. UnicodeString Name = WorkspaceName();
  9876. DoSaveWorkspace(
  9877. Name, DataList.get(),
  9878. !Configuration->DisablePasswordStoring &&
  9879. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9880. WinConfiguration->LastStoredSession = Name;
  9881. }
  9882. }
  9883. //---------------------------------------------------------------------------
  9884. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9885. {
  9886. TManagedTerminal * ManagedTerminal =
  9887. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9888. if (ManagedTerminal != NULL)
  9889. {
  9890. ManagedTerminal->DirectoryLoaded = Now();
  9891. }
  9892. }
  9893. //---------------------------------------------------------------------------
  9894. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9895. TListItem * /*Item*/, bool /*Selected*/)
  9896. {
  9897. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9898. switch (DirView->LastSelectMethod)
  9899. {
  9900. case smKeyboard:
  9901. Configuration->Usage->Inc(L"KeyboardSelections");
  9902. break;
  9903. case smMouse:
  9904. Configuration->Usage->Inc(L"MouseSelections");
  9905. break;
  9906. }
  9907. }
  9908. //---------------------------------------------------------------------------
  9909. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9910. {
  9911. if (Color != 0)
  9912. {
  9913. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9914. }
  9915. else
  9916. {
  9917. return FSessionTabImageIndex;
  9918. }
  9919. }
  9920. //---------------------------------------------------------------------------
  9921. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9922. {
  9923. TPngImageCollectionItem * Item =
  9924. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9925. FSessionColors->AddPng(Item->PngImage);
  9926. return FSessionColors->Count - 1;
  9927. }
  9928. //---------------------------------------------------------------------------
  9929. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9930. {
  9931. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9932. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  9933. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  9934. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9935. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9936. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  9937. }
  9938. //---------------------------------------------------------------------------
  9939. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  9940. {
  9941. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  9942. }
  9943. //---------------------------------------------------------------------------
  9944. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  9945. {
  9946. switch (Side)
  9947. {
  9948. case osLocal:
  9949. return FNewLocalTabTabImageIndex;
  9950. case osRemote:
  9951. case osCurrent:
  9952. return FNewRemoteTabTabImageIndex;
  9953. default:
  9954. DebugFail();
  9955. return -1;
  9956. }
  9957. }
  9958. //---------------------------------------------------------------------------
  9959. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9960. {
  9961. for (int Index = 0; Index < Control->ControlCount; Index++)
  9962. {
  9963. TControl * AControl = Control->Controls[Index];
  9964. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9965. if (WinControl != NULL)
  9966. {
  9967. CollectItemsWithTextDisplayMode(WinControl);
  9968. }
  9969. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9970. if (Toolbar != NULL)
  9971. {
  9972. // we care for top-level items only
  9973. TTBCustomItem * Items = Toolbar->Items;
  9974. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9975. {
  9976. TTBCustomItem * Item = Items->Items[ItemIndex];
  9977. if (((Item->DisplayMode == nbdmImageAndText) ||
  9978. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9979. EligibleForImageDisplayMode(Item))
  9980. {
  9981. FItemsWithTextDisplayMode.insert(Item);
  9982. }
  9983. }
  9984. }
  9985. }
  9986. }
  9987. //---------------------------------------------------------------------------
  9988. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9989. {
  9990. return true;
  9991. }
  9992. //---------------------------------------------------------------------------
  9993. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9994. {
  9995. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9996. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9997. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9998. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9999. bool Result = true;
  10000. while (Result && (i != FItemsWithTextDisplayMode.end()))
  10001. {
  10002. TTBCustomItem * Item = *i;
  10003. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  10004. if (Label != NULL)
  10005. {
  10006. // optimization
  10007. if (Label->Visible == SelectiveToolbarText)
  10008. {
  10009. Result = false;
  10010. }
  10011. else
  10012. {
  10013. Label->Visible = SelectiveToolbarText;
  10014. }
  10015. }
  10016. else
  10017. {
  10018. // optimization
  10019. if (Item->DisplayMode == DisplayMode)
  10020. {
  10021. Result = false;
  10022. }
  10023. else
  10024. {
  10025. Item->DisplayMode = DisplayMode;
  10026. }
  10027. }
  10028. ++i;
  10029. }
  10030. UpdateNewTabTab();
  10031. return Result;
  10032. }
  10033. //---------------------------------------------------------------------------
  10034. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  10035. {
  10036. DebugFail();
  10037. }
  10038. //---------------------------------------------------------------------------
  10039. bool __fastcall TCustomScpExplorerForm::IsBusy()
  10040. {
  10041. // Among other, a lock level is non zero, while directory is being loaded,
  10042. // even when it happens as a result of dirview navigation,
  10043. // i.e. when TNonVisualDataModule is NOT FBusy.
  10044. // That's why the TNonVisualDataModule::GetBusy calls this method.
  10045. // Among other this prevents a panel auto update to occur while
  10046. // directory is changing.
  10047. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  10048. }
  10049. //---------------------------------------------------------------------------
  10050. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  10051. {
  10052. // While the window is disabled, we still seem to process menu shortcuts at least,
  10053. // so stop it at least here.
  10054. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  10055. bool Result;
  10056. if (Allowed == aaUpdate)
  10057. {
  10058. Result = true;
  10059. }
  10060. else
  10061. {
  10062. Result = !NonVisualDataModule->Busy;
  10063. if (Result && (Allowed == aaShortCut))
  10064. {
  10065. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  10066. Action->Update();
  10067. Result = Action->Enabled;
  10068. }
  10069. }
  10070. return Result;
  10071. }
  10072. //---------------------------------------------------------------------------
  10073. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  10074. {
  10075. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  10076. }
  10077. //---------------------------------------------------------------------------
  10078. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  10079. {
  10080. // This is somewhat redundant to LockWindow() call from
  10081. // TTerminalManager::TerminalReadDirectoryProgress.
  10082. // But disabling window is known not to block keyboard shorcuts
  10083. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  10084. if (Busy > 0)
  10085. {
  10086. if (NonVisualDataModule->Busy)
  10087. {
  10088. State = false;
  10089. }
  10090. else
  10091. {
  10092. NonVisualDataModule->StartBusy();
  10093. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  10094. LockWindow();
  10095. }
  10096. }
  10097. else if (Busy < 0)
  10098. {
  10099. UnlockWindow();
  10100. NonVisualDataModule->EndBusy();
  10101. }
  10102. else
  10103. {
  10104. State = NonVisualDataModule->Busy;
  10105. }
  10106. }
  10107. //---------------------------------------------------------------------------
  10108. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  10109. {
  10110. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  10111. if (Index >= 0)
  10112. {
  10113. TPopupMenu * PopupMenu = NULL;
  10114. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10115. if (Session != NULL)
  10116. {
  10117. SessionsPageControl->ActivePageIndex = Index;
  10118. if (DebugAlwaysTrue(SessionTabSwitched()))
  10119. {
  10120. // copied from TControl.WMContextMenu
  10121. SendCancelMode(SessionsPageControl);
  10122. // explicit popup instead of using PopupMenu property
  10123. // to avoid menu to popup somewhere within SessionTabSwitched above,
  10124. // while connecting yet not-connected session and hence
  10125. // allowing an access to commands over not-completely connected session
  10126. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  10127. }
  10128. }
  10129. else
  10130. {
  10131. if (SupportsLocalBrowser())
  10132. {
  10133. PopupMenu = NonVisualDataModule->NewTabPopup;
  10134. }
  10135. }
  10136. if (PopupMenu != NULL)
  10137. {
  10138. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  10139. PopupMenu->PopupComponent = SessionsPageControl;
  10140. PopupMenu->Popup(Point.x, Point.y);
  10141. }
  10142. }
  10143. Handled = true;
  10144. }
  10145. //---------------------------------------------------------------------------
  10146. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  10147. {
  10148. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  10149. }
  10150. //---------------------------------------------------------------------------
  10151. void __fastcall TCustomScpExplorerForm::ChangePassword()
  10152. {
  10153. Configuration->Usage->Inc(L"PasswordChanges");
  10154. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  10155. ChangePasswordData->DisableAuthentationsExceptPassword();
  10156. ChangePasswordData->ChangePassword = true;
  10157. std::unique_ptr<TTerminal> ChangePasswordSession(
  10158. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  10159. ChangePasswordSession->AutoReadDirectory = false;
  10160. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  10161. {
  10162. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  10163. }
  10164. else
  10165. {
  10166. Configuration->Usage->Inc(L"PasswordChangesFailed");
  10167. }
  10168. }
  10169. //---------------------------------------------------------------------------
  10170. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  10171. {
  10172. // No nice way to assert SSH2
  10173. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  10174. }
  10175. //---------------------------------------------------------------------------
  10176. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  10177. {
  10178. TOperationVisualizer OperationVisualizer;
  10179. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  10180. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  10181. }
  10182. //---------------------------------------------------------------------------
  10183. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  10184. {
  10185. TForm::ChangeScale(M, D);
  10186. int APixelsPerInch = GetControlPixelsPerInch(this);
  10187. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  10188. try
  10189. {
  10190. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  10191. }
  10192. __finally
  10193. {
  10194. GUIConfiguration->ChangeResourceModule(ResourceModule);
  10195. }
  10196. }
  10197. //---------------------------------------------------------------------------
  10198. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  10199. {
  10200. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  10201. }
  10202. //---------------------------------------------------------------------
  10203. class TPublicControl : public TControl
  10204. {
  10205. friend class TCustomScpExplorerForm;
  10206. };
  10207. //---------------------------------------------------------------------------
  10208. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  10209. {
  10210. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  10211. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  10212. }
  10213. //---------------------------------------------------------------------------
  10214. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  10215. {
  10216. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  10217. OpenBookmark(Side, Bookmark);
  10218. }
  10219. //---------------------------------------------------------------------------
  10220. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  10221. {
  10222. DoBookmarkClick(osLocal, Sender);
  10223. }
  10224. //---------------------------------------------------------------------------
  10225. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  10226. {
  10227. DoBookmarkClick(osRemote, Sender);
  10228. }
  10229. //---------------------------------------------------------------------------
  10230. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  10231. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  10232. {
  10233. if (BookmarkList != NULL)
  10234. {
  10235. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  10236. if (IsLocalBrowserMode())
  10237. {
  10238. Side = osLocal;
  10239. }
  10240. if (!WinConfiguration->UseLocationProfiles)
  10241. {
  10242. std::unique_ptr<TStringList> Directories(new TStringList());
  10243. Directories->CaseSensitive = (Side == osRemote);
  10244. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10245. {
  10246. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10247. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  10248. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  10249. {
  10250. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10251. Item->Caption = EscapeHotkey(Directory);
  10252. Item->ShortCut = Bookmark->ShortCut;
  10253. Item->OnClick = OnBookmarkClick;
  10254. Item->Tag = reinterpret_cast<int>(Bookmark);
  10255. Directories->Add(Directory);
  10256. Menu->Add(Item.release());
  10257. }
  10258. }
  10259. }
  10260. else
  10261. {
  10262. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10263. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10264. {
  10265. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10266. if (!Bookmark->Node.IsEmpty())
  10267. {
  10268. Folders->Add(Bookmark->Node);
  10269. }
  10270. }
  10271. for (int Index = 0; Index < Folders->Count; Index++)
  10272. {
  10273. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10274. Item->Caption = Folders->Strings[Index];
  10275. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10276. Folders->Objects[Index] = Item.get();
  10277. Menu->Add(Item.release());
  10278. }
  10279. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10280. {
  10281. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10282. TTBCustomItem * Parent;
  10283. if (!Bookmark->Node.IsEmpty())
  10284. {
  10285. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10286. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10287. }
  10288. else
  10289. {
  10290. Parent = Menu;
  10291. }
  10292. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10293. Item->Caption = Bookmark->Name;
  10294. Item->ShortCut = Bookmark->ShortCut;
  10295. Item->OnClick = OnBookmarkClick;
  10296. Item->Tag = reinterpret_cast<int>(Bookmark);
  10297. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10298. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10299. {
  10300. // No hint for location profiles that are actually mere "bookmarks"
  10301. }
  10302. else
  10303. {
  10304. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10305. UnicodeString LongHint;
  10306. if (!Bookmark->Local.IsEmpty())
  10307. {
  10308. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10309. }
  10310. if (!Bookmark->Remote.IsEmpty())
  10311. {
  10312. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10313. }
  10314. AddToList(Hint, LongHint, L"|");
  10315. Item->Hint = Hint;
  10316. }
  10317. Parent->Add(Item.release());
  10318. }
  10319. }
  10320. AddMenuSeparator(Menu);
  10321. }
  10322. }
  10323. //---------------------------------------------------------------------------
  10324. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10325. {
  10326. Menu->Clear();
  10327. std::unique_ptr<TTBCustomItem> Item;
  10328. Item.reset(new TTBXItem(Owner));
  10329. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10330. Item->Options = Item->Options << tboDefault;
  10331. Menu->Add(Item.release());
  10332. AddMenuSeparator(Menu);
  10333. if (Terminal != NULL)
  10334. {
  10335. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10336. }
  10337. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10338. Item.reset(new TTBXItem(Owner));
  10339. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10340. Menu->Add(Item.release());
  10341. }
  10342. //---------------------------------------------------------------------------
  10343. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10344. {
  10345. bool Result = true;
  10346. bool Remote = !IsSideLocalBrowser(Side);
  10347. try
  10348. {
  10349. UnicodeString APath = Path;
  10350. if (Remote)
  10351. {
  10352. Terminal->ExceptionOnFail = true;
  10353. }
  10354. else
  10355. {
  10356. APath = ExpandEnvironmentVariables(Path);
  10357. }
  10358. try
  10359. {
  10360. DirView(Side)->Path = APath;
  10361. }
  10362. __finally
  10363. {
  10364. if (Remote)
  10365. {
  10366. Terminal->ExceptionOnFail = false;
  10367. }
  10368. }
  10369. }
  10370. catch (Exception & E)
  10371. {
  10372. if (!Remote || Terminal->Active)
  10373. {
  10374. ShowExtendedExceptionEx(Terminal, &E);
  10375. Result = false;
  10376. }
  10377. else
  10378. {
  10379. throw;
  10380. }
  10381. }
  10382. return Result;
  10383. }
  10384. //---------------------------------------------------------------------------
  10385. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10386. {
  10387. // Make sure there some feedback even when loading tiny local folders
  10388. TInstantOperationVisualizer Visualizer;
  10389. DirView(Side)->ReloadDirectory();
  10390. }
  10391. //---------------------------------------------------------------------------
  10392. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10393. {
  10394. if (Index == SessionsPageControl->TabIndex)
  10395. {
  10396. CloseSession();
  10397. }
  10398. else
  10399. {
  10400. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10401. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10402. TTerminalManager * Manager = TTerminalManager::Instance();
  10403. if (!Terminal->Active)
  10404. {
  10405. Manager->FreeTerminal(Terminal);
  10406. }
  10407. else
  10408. {
  10409. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10410. if (CanCloseQueue(Queue))
  10411. {
  10412. Manager->FreeTerminal(Terminal);
  10413. }
  10414. }
  10415. }
  10416. }
  10417. //---------------------------------------------------------------------------
  10418. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10419. {
  10420. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10421. if (Session == NULL)
  10422. {
  10423. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10424. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10425. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10426. //P = SessionsPageControl->ClientToScreen(P);
  10427. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10428. }
  10429. else
  10430. {
  10431. CloseSessionTab(Index);
  10432. }
  10433. }
  10434. //---------------------------------------------------------------------------
  10435. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10436. {
  10437. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10438. {
  10439. TInstantOperationVisualizer Visualizer;
  10440. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10441. // before we create a new data object
  10442. ClipboardClear();
  10443. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10444. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10445. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10446. // Intermediate variable to WORKAROUND a crash in compiler
  10447. TObjectList * ClipboardFakeMonitors =
  10448. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10449. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10450. if (FClipboardDragDropFilesEx.get() == NULL)
  10451. {
  10452. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10453. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10454. }
  10455. FClipboardDragDropFilesEx->FileList->Clear();
  10456. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10457. FClipboardDragDropFilesEx->CopyToClipboard();
  10458. FClipboardTerminal = Terminal;
  10459. // Need full paths, as cwd can be different once files are pasted
  10460. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10461. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10462. {
  10463. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10464. }
  10465. }
  10466. }
  10467. //---------------------------------------------------------------------------
  10468. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10469. {
  10470. if (OpenClipboard(0))
  10471. {
  10472. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10473. CloseClipboard();
  10474. }
  10475. }
  10476. //---------------------------------------------------------------------------
  10477. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10478. {
  10479. return !FClipboardFakeDirectory.IsEmpty();
  10480. }
  10481. //---------------------------------------------------------------------------
  10482. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10483. {
  10484. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10485. FClipboardFakeDirectory = UnicodeString();
  10486. FClipboardTerminal = NULL;
  10487. if (FDownloadingFromClipboard)
  10488. {
  10489. // We are called by the monitor, so attempt to release it would deadlock
  10490. FClipboardFakeMonitorsPendingReset = true;
  10491. }
  10492. else
  10493. {
  10494. FClipboardFakeMonitors.reset(NULL);
  10495. }
  10496. }
  10497. //---------------------------------------------------------------------------
  10498. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10499. {
  10500. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10501. {
  10502. ClipboardStop();
  10503. }
  10504. }
  10505. //---------------------------------------------------------------------------
  10506. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10507. {
  10508. TTerminalManager * Manager = TTerminalManager::Instance();
  10509. Manager->ActiveSession = FClipboardTerminal;
  10510. TTransferOperationParam Params;
  10511. Params.TargetDirectory = TargetDirectory;
  10512. Params.DragDrop = DragDrop;
  10513. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10514. }
  10515. //---------------------------------------------------------------------------
  10516. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10517. {
  10518. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10519. // (as it pumps a Windows message queue while being shutted down)
  10520. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10521. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10522. {
  10523. // Can fail as it can be e.g. locked by AV, so we retry that later
  10524. bool Removed = RemoveDir(ApiPath(FileName));
  10525. try
  10526. {
  10527. if (!NonVisualDataModule->Busy)
  10528. {
  10529. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10530. FClipboardFakeMonitorsPendingReset = false;
  10531. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10532. TAutoFlag Flag(FDownloadingFromClipboard);
  10533. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10534. }
  10535. }
  10536. __finally
  10537. {
  10538. if (!Removed)
  10539. {
  10540. RemoveDir(ApiPath(FileName));
  10541. }
  10542. }
  10543. }
  10544. }
  10545. //---------------------------------------------------------------------------
  10546. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10547. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10548. {
  10549. TFileMasks::TParams MaskParams;
  10550. MaskParams.Size = Size;
  10551. MaskParams.Modification = Modification;
  10552. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10553. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10554. // TUnixDirView::ItemFullFileName returns trailing backslash for directories, what filemask cannot deal with
  10555. if (!Local && Directory)
  10556. {
  10557. FileName = UnixExcludeTrailingBackslash(FileName);
  10558. }
  10559. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10560. {
  10561. bool ImplicitMatch;
  10562. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10563. !ImplicitMatch)
  10564. {
  10565. Color = Iter->Color;
  10566. break;
  10567. }
  10568. }
  10569. }
  10570. //---------------------------------------------------------------------------
  10571. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10572. {
  10573. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10574. if (!DirView->IsEditing())
  10575. {
  10576. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10577. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10578. // while searching
  10579. if (Key == VK_BACK)
  10580. {
  10581. if (!FIncrementalSearch.IsEmpty())
  10582. {
  10583. if (FIncrementalSearch.Length() == 1)
  10584. {
  10585. ResetIncrementalSearch();
  10586. }
  10587. else
  10588. {
  10589. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10590. IncrementalSearch(NewText, false, false);
  10591. }
  10592. Key = 0;
  10593. }
  10594. }
  10595. }
  10596. }
  10597. //---------------------------------------------------------------------------
  10598. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10599. {
  10600. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10601. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10602. {
  10603. // Filter control sequences.
  10604. // When not searching yet, prefer use of the space for toggling file selection
  10605. // (so we cannot incrementally search for a string starting with a space).
  10606. if ((Key > VK_SPACE) ||
  10607. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10608. {
  10609. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10610. Key = 0;
  10611. }
  10612. }
  10613. }
  10614. //---------------------------------------------------------------------------
  10615. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10616. {
  10617. if (!FIncrementalSearch.IsEmpty())
  10618. {
  10619. FIncrementalSearch = L"";
  10620. DirView(osCurrent)->UpdateStatusBar();
  10621. }
  10622. }
  10623. //---------------------------------------------------------------------------
  10624. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10625. {
  10626. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10627. if (Item == NULL)
  10628. {
  10629. MessageBeep(MB_ICONHAND);
  10630. }
  10631. else
  10632. {
  10633. TCustomDirView * ADirView = DirView(osCurrent);
  10634. {
  10635. TAutoNestingCounter Guard(FIncrementalSearching);
  10636. ADirView->FocusItem(Item);
  10637. if (ADirView->NortonLike == nlOff)
  10638. {
  10639. if ((ADirView->Selected != Item) ||
  10640. (ADirView->SelCount > 1))
  10641. {
  10642. ADirView->ClearSelection();
  10643. ADirView->Selected = Item;
  10644. }
  10645. }
  10646. }
  10647. FIncrementalSearch = Text;
  10648. Item->MakeVisible(false);
  10649. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10650. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10651. ADirView->UpdateStatusBar();
  10652. }
  10653. }
  10654. //---------------------------------------------------------------------------
  10655. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10656. {
  10657. int Index = Item->Index;
  10658. if (!Reverse)
  10659. {
  10660. Index++;
  10661. if (Index >= Item->Owner->Count)
  10662. {
  10663. Index = 0;
  10664. }
  10665. }
  10666. else
  10667. {
  10668. Index--;
  10669. if (Index < 0)
  10670. {
  10671. Index = Item->Owner->Count - 1;
  10672. }
  10673. }
  10674. return Item->Owner->Item[Index];
  10675. }
  10676. //---------------------------------------------------------------------------
  10677. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10678. {
  10679. TCustomDirView * ADirView = DirView(osCurrent);
  10680. if (ADirView->Items->Count == 0)
  10681. {
  10682. return NULL;
  10683. }
  10684. else
  10685. {
  10686. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10687. TListItem * Item = CurrentItem;
  10688. if (SkipCurrent)
  10689. {
  10690. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10691. }
  10692. while (true)
  10693. {
  10694. bool Matches = false;
  10695. switch (WinConfiguration->PanelSearch)
  10696. {
  10697. case isNameStartOnly:
  10698. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item).SubString(1, Text.Length()), Text);
  10699. break;
  10700. case isName:
  10701. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item), Text);
  10702. break;
  10703. case isAll:
  10704. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10705. int Index = 0;
  10706. while ((Index < ColCount) && !Matches)
  10707. {
  10708. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10709. if (Visible)
  10710. {
  10711. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10712. }
  10713. Index++;
  10714. }
  10715. break;
  10716. }
  10717. if (Matches)
  10718. {
  10719. return Item;
  10720. }
  10721. Item = GetNextFile(Item, Reverse);
  10722. if (Item == CurrentItem)
  10723. {
  10724. return NULL;
  10725. }
  10726. }
  10727. }
  10728. }
  10729. //---------------------------------------------------------------------------
  10730. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10731. {
  10732. ResetIncrementalSearch();
  10733. }
  10734. //---------------------------------------------------------------------------
  10735. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10736. {
  10737. if (Message.CharCode == VK_TAB)
  10738. {
  10739. if (!FIncrementalSearch.IsEmpty())
  10740. {
  10741. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10742. bool Reverse = Shift.Contains(ssShift);
  10743. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10744. Message.Result = 1;
  10745. return;
  10746. }
  10747. }
  10748. else if (Message.CharCode == VK_ESCAPE)
  10749. {
  10750. if (!FIncrementalSearch.IsEmpty())
  10751. {
  10752. ResetIncrementalSearch();
  10753. Message.Result = 1;
  10754. return;
  10755. }
  10756. }
  10757. TForm::Dispatch(&Message);
  10758. }
  10759. //---------------------------------------------------------------------------
  10760. void __fastcall TCustomScpExplorerForm::Deactivate()
  10761. {
  10762. TForm::Deactivate();
  10763. // E.g. when switching to an internal editor window
  10764. ResetIncrementalSearch();
  10765. }
  10766. //---------------------------------------------------------------------------
  10767. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10768. {
  10769. // When switching to another application
  10770. ResetIncrementalSearch();
  10771. }
  10772. //---------------------------------------------------------------------------
  10773. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10774. {
  10775. ResetIncrementalSearch();
  10776. }
  10777. //---------------------------------------------------------------------------
  10778. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10779. {
  10780. if (FIncrementalSearching <= 0)
  10781. {
  10782. ResetIncrementalSearch();
  10783. }
  10784. }
  10785. //---------------------------------------------------------------------------
  10786. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10787. {
  10788. CountClicksForWindowPrint(this);
  10789. }
  10790. //---------------------------------------------------------------------------
  10791. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10792. {
  10793. CenterReconnectToolbar();
  10794. }
  10795. //---------------------------------------------------------------------------
  10796. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10797. {
  10798. if (RemoteDirView->ItemFocused != NULL)
  10799. {
  10800. RemoteDirView->ItemFocused->Selected = true;
  10801. }
  10802. }
  10803. //---------------------------------------------------------------------------
  10804. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10805. {
  10806. if (!FQueueStatusUpdating)
  10807. {
  10808. bool Refresh;
  10809. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10810. {
  10811. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10812. }
  10813. else
  10814. {
  10815. if (FQueueFileList->GetCount() > 0)
  10816. {
  10817. FQueueFileList->Clear();
  10818. Refresh = true;
  10819. }
  10820. }
  10821. int Count = FQueueFileList->GetCount();
  10822. if (QueueFileList->Items->Count != Count)
  10823. {
  10824. DebugAssert(Refresh);
  10825. QueueFileList->Items->Count = Count;
  10826. }
  10827. if (Refresh)
  10828. {
  10829. QueueFileList->Invalidate();
  10830. }
  10831. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10832. }
  10833. }
  10834. //---------------------------------------------------------------------------
  10835. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10836. {
  10837. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10838. }
  10839. //---------------------------------------------------------------------------
  10840. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10841. {
  10842. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10843. // See TSynchronizeChecklistDialog::UpdateTimer
  10844. if (QueueFileList->Items->Count > 0)
  10845. {
  10846. QueueFileList->Items->Item[0]->MakeVisible(false);
  10847. }
  10848. UpdateQueueFileList();
  10849. }
  10850. //---------------------------------------------------------------------------
  10851. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10852. {
  10853. Active = QueueView3->Focused() || QueueFileList->Focused();
  10854. }
  10855. //---------------------------------------------------------------------------
  10856. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10857. {
  10858. QueueLabelUpdateStatus();
  10859. }
  10860. //---------------------------------------------------------------------------
  10861. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10862. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10863. {
  10864. int State = FQueueFileList->GetState(Item->Index);
  10865. if (State == qfsProcessed)
  10866. {
  10867. Sender->Canvas->Font->Color = clGrayText;
  10868. }
  10869. }
  10870. //---------------------------------------------------------------------------
  10871. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10872. {
  10873. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10874. QueueFileList->Column[0]->Width =
  10875. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10876. }
  10877. //---------------------------------------------------------------------------
  10878. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10879. {
  10880. QueueFileListColumnAutoSize();
  10881. }
  10882. //---------------------------------------------------------------------------
  10883. void __fastcall TCustomScpExplorerForm::CloseApp()
  10884. {
  10885. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10886. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10887. // So we clear the flag here.
  10888. DebugAssert(NonVisualDataModule->Busy);
  10889. NonVisualDataModule->EndBusy();
  10890. try
  10891. {
  10892. Close();
  10893. }
  10894. __finally
  10895. {
  10896. NonVisualDataModule->StartBusy();
  10897. }
  10898. }
  10899. //---------------------------------------------------------------------------
  10900. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10901. {
  10902. return (Terminal != NULL) && Terminal->Active;
  10903. }
  10904. //---------------------------------------------------------------------------
  10905. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  10906. {
  10907. return (ManagedSession != NULL);
  10908. }
  10909. //---------------------------------------------------------------------------
  10910. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10911. {
  10912. return IsActiveTerminal(Terminal);
  10913. }
  10914. //---------------------------------------------------------------------------
  10915. void TCustomScpExplorerForm::LocalLocalCopy(
  10916. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  10917. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  10918. {
  10919. DebugFail();
  10920. }
  10921. //---------------------------------------------------------------------------
  10922. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  10923. {
  10924. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  10925. }
  10926. //---------------------------------------------------------------------------
  10927. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  10928. {
  10929. // Changing tab list triggers the OnResize.
  10930. if (!FUpdatingSessionTabs)
  10931. {
  10932. // UpdateTabsCaptionTruncation should be enough
  10933. SessionListChanged(true);
  10934. }
  10935. }
  10936. //---------------------------------------------------------------------------
  10937. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  10938. {
  10939. if (ASession == ManagedSession)
  10940. {
  10941. UpdateSession(ASession);
  10942. }
  10943. TSessionData * Data = ASession->StateData;
  10944. UnicodeString Result;
  10945. if (Side == osLocal)
  10946. {
  10947. Result = Data->LocalDirectory;
  10948. }
  10949. else if (DebugAlwaysTrue(Side == osOther))
  10950. {
  10951. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  10952. }
  10953. return Result;
  10954. }
  10955. //---------------------------------------------------------------------------
  10956. UnicodeString TCustomScpExplorerForm::GetTabHintSessionDetails(TManagedTerminal * ASession)
  10957. {
  10958. UnicodeString Title = TTerminalManager::Instance()->GetSessionTitle(ASession, false);
  10959. UnicodeString Result = ASession->SessionData->DefaultSessionName;
  10960. if (Title == Result)
  10961. {
  10962. Result = EmptyStr;
  10963. }
  10964. else
  10965. {
  10966. Result += L"\n";
  10967. }
  10968. return Result;
  10969. }
  10970. //---------------------------------------------------------------------------
  10971. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  10972. {
  10973. UnicodeString Result = GetTabHintSessionDetails(ASession);
  10974. if (!ASession->Active)
  10975. {
  10976. Result += LoadStr(STATUS_NOT_CONNECTED2);
  10977. }
  10978. else
  10979. {
  10980. Result += GetSessionPath(ASession, osRemote);
  10981. }
  10982. return Result;
  10983. }
  10984. //---------------------------------------------------------------------------
  10985. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  10986. {
  10987. return LoadStr(NEW_REMOTE_TAB_HINT);
  10988. }
  10989. //---------------------------------------------------------------------------
  10990. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  10991. {
  10992. if (Index >= 0)
  10993. {
  10994. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10995. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  10996. if (TabSession == NULL)
  10997. {
  10998. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  10999. if (NewTabTabCaption != TabSheet->Caption)
  11000. {
  11001. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  11002. }
  11003. }
  11004. else
  11005. {
  11006. TTerminalManager * Manager = TTerminalManager::Instance();
  11007. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  11008. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  11009. }
  11010. }
  11011. }
  11012. //---------------------------------------------------------------------------
  11013. typedef std::vector<TListItem *> TListItemsVector;
  11014. struct TCalculateSizeOperation
  11015. {
  11016. TCalculateSizeStats Stats;
  11017. TListItemsVector ListItems;
  11018. size_t Index;
  11019. };
  11020. //---------------------------------------------------------------------------
  11021. void TCustomScpExplorerForm::CalculateDirectorySizes(TOperationSide Side)
  11022. {
  11023. TCalculateSizeOperation CalculateSizeOperation;
  11024. Side = GetSide(Side);
  11025. TCustomDirView * ADirView = DirView(Side);
  11026. bool LocalSide = IsSideLocalBrowser(Side);
  11027. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, LocalSide, NULL, true));
  11028. std::unique_ptr<TStrings> FileList(new TStringList());
  11029. for (int Index = 0; Index < AllFileList->Count; Index++)
  11030. {
  11031. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  11032. if (ADirView->ItemIsDirectory(Item))
  11033. {
  11034. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  11035. CalculateSizeOperation.ListItems.push_back(Item);
  11036. }
  11037. }
  11038. CalculateSizeOperation.Index = 0;
  11039. __int64 Size = 0;
  11040. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation);
  11041. FCalculateSizeOperation = &CalculateSizeOperation;
  11042. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  11043. FOnFileOperationFinished = DirectorySizeCalculated;
  11044. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation);
  11045. FPrimaryOperation = foCalculateSize;
  11046. TCalculatedSizes CalculatedSizes;
  11047. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  11048. if (LocalSide)
  11049. {
  11050. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  11051. FProgressSide = Side;
  11052. ManagedSession->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  11053. }
  11054. else
  11055. {
  11056. TCalculateSizeParams Params;
  11057. Params.Stats = &CalculateSizeOperation.Stats;
  11058. Params.UseCache = true;
  11059. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  11060. }
  11061. }
  11062. //---------------------------------------------------------------------------
  11063. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(TOperationSide Side, const UnicodeString & FileName, bool Success)
  11064. {
  11065. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  11066. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  11067. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  11068. // optimization
  11069. bool GotExpectedItem =
  11070. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  11071. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  11072. int Index = -1;
  11073. if (DebugAlwaysTrue(GotExpectedItem))
  11074. {
  11075. Index = FCalculateSizeOperation->Index;
  11076. FCalculateSizeOperation->Index++;
  11077. }
  11078. else
  11079. {
  11080. TListItemsVector::iterator I =
  11081. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  11082. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  11083. {
  11084. Index = I - FCalculateSizeOperation->ListItems.begin();
  11085. }
  11086. }
  11087. if (DebugAlwaysTrue(Index >= 0) && Success)
  11088. {
  11089. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  11090. ADirView->SetItemCalculatedSize(Item, Size);
  11091. }
  11092. }
  11093. //---------------------------------------------------------------------------
  11094. void __fastcall TCustomScpExplorerForm::MessageDockRequestDock(TObject *, TTBCustomDockableWindow *, bool & Accept)
  11095. {
  11096. Accept = false;
  11097. }
  11098. //---------------------------------------------------------------------------