CustomScpExplorer.cpp 415 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394
  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 "NonVisual.h"
  22. #include "Glyphs.h"
  23. #include "Tools.h"
  24. #include "WinConfiguration.h"
  25. #include "TerminalManager.h"
  26. #include "EditorManager.h"
  27. #include "ProgParams.h"
  28. #include "Setup.h"
  29. #include <Consts.hpp>
  30. #include <DateUtils.hpp>
  31. #include <TB2Common.hpp>
  32. #include <DirectoryMonitor.hpp>
  33. #include <System.IOUtils.hpp>
  34. #include <System.StrUtils.hpp>
  35. //---------------------------------------------------------------------------
  36. #pragma package(smart_init)
  37. #pragma link "CustomDirView"
  38. #pragma link "CustomUnixDirView"
  39. #pragma link "IEListView"
  40. #pragma link "NortonLikeListView"
  41. #pragma link "UnixDirView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "UnixDriveView"
  44. #pragma link "CustomDriveView"
  45. #pragma link "TB2Dock"
  46. #pragma link "TBXStatusBars"
  47. #pragma link "TB2Item"
  48. #pragma link "TB2Toolbar"
  49. #pragma link "ThemePageControl"
  50. #pragma link "PathLabel"
  51. #pragma resource "*.dfm"
  52. //---------------------------------------------------------------------------
  53. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  54. #define WM_PASTE_FILES (WM_WINSCP_USER + 14)
  55. static const int SessionPanelCount = 3;
  56. //---------------------------------------------------------------------------
  57. class TMutexGuard
  58. {
  59. public:
  60. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  61. int Timeout = 5000)
  62. {
  63. FMutex = NULL;
  64. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  65. if (WaitResult == WAIT_TIMEOUT)
  66. {
  67. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  68. }
  69. else
  70. {
  71. FMutex = AMutex;
  72. }
  73. }
  74. ~TMutexGuard()
  75. {
  76. if (FMutex != NULL)
  77. {
  78. ReleaseMutex(FMutex);
  79. }
  80. }
  81. private:
  82. HANDLE FMutex;
  83. };
  84. //---------------------------------------------------------------------------
  85. class TWindowLock
  86. {
  87. public:
  88. TWindowLock(TCustomScpExplorerForm * Form) :
  89. FForm(Form)
  90. {
  91. FForm->LockWindow();
  92. }
  93. ~TWindowLock()
  94. {
  95. FForm->UnlockWindow();
  96. }
  97. private:
  98. TCustomScpExplorerForm * FForm;
  99. };
  100. //---------------------------------------------------------------------------
  101. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  102. FForm(Form)
  103. {
  104. FForm->BatchStart(FBatchStorage);
  105. }
  106. //---------------------------------------------------------------------------
  107. TAutoBatch::~TAutoBatch()
  108. {
  109. FForm->BatchEnd(FBatchStorage);
  110. }
  111. //---------------------------------------------------------------------------
  112. struct TTransferOperationParam
  113. {
  114. TTransferOperationParam();
  115. UnicodeString TargetDirectory;
  116. bool Temp;
  117. bool DragDrop;
  118. int Options;
  119. TAutoSwitch Queue;
  120. };
  121. //---------------------------------------------------------------------------
  122. TTransferOperationParam::TTransferOperationParam()
  123. {
  124. Temp = false;
  125. DragDrop = false;
  126. Options = 0;
  127. Queue = asAuto;
  128. }
  129. //---------------------------------------------------------------------------
  130. class TTransferPresetNoteData : public TObject
  131. {
  132. public:
  133. UnicodeString Message;
  134. };
  135. //---------------------------------------------------------------------------
  136. class TTerminalNoteData : public TObject
  137. {
  138. public:
  139. TManagedTerminal * Terminal;
  140. };
  141. //---------------------------------------------------------------------------
  142. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  143. FFormRestored(false),
  144. TForm(Owner)
  145. {
  146. AddStartupSequence(L"F");
  147. FInvalid = false;
  148. FCurrentSide = osRemote;
  149. FEverShown = false;
  150. FDocks = new TList();
  151. InitControls();
  152. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  153. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  154. NonVisualDataModule->ScpExplorer = this;
  155. RestoreParams();
  156. ConfigurationChanged();
  157. RemoteDirView->Invalidate();
  158. FAutoOperation = false;
  159. FOnFileOperationFinished = NULL;
  160. FPrimaryOperation = foNone;
  161. FForceExecution = false;
  162. FIgnoreNextDialogChar = 0;
  163. FErrorList = NULL;
  164. FSynchronizeProgressForm = NULL;
  165. FProgressForm = NULL;
  166. FRefreshLocalDirectory = false;
  167. FRefreshRemoteDirectory = false;
  168. FDDExtMapFile = NULL;
  169. // CreateMutexW keeps failing with ERROR_NOACCESS
  170. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  171. DebugAssert(FDDExtMutex != NULL);
  172. FDDTargetControl = NULL;
  173. FDelayedDeletionTimer = NULL;
  174. FDelayedDeletionList = new TStringList();
  175. FDDFileList = NULL;
  176. FPendingTempSpaceWarn = false;
  177. FCapturedLog = NULL;
  178. FDragDropOperation = false;
  179. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  180. FAllowTransferPresetAutoSelect = true;
  181. FSessionChanging = false;
  182. FCopyParamDefault = L"";
  183. FSynchronizeController = NULL;
  184. FPendingQueueActionItem = NULL;
  185. FLockLevel = 0;
  186. FLockSuspendLevel = 0;
  187. FDisabledOnLockSuspend = false;
  188. FAlternativeDelete = false;
  189. FTrayIcon = new ::TTrayIcon(0);
  190. FTrayIcon->OnClick = TrayIconClick;
  191. FMaxQueueLength = 0;
  192. FLastContextPopupScreenPoint = TPoint(-1, -1);
  193. FTransferResumeList = NULL;
  194. FDeletedFiles = NULL;
  195. FMoveToQueue = false;
  196. StandaloneOperation = false;
  197. FOnFeedSynchronizeError = NULL;
  198. FOnSynchronizeAbort = NULL;
  199. FSynchronizeTerminal = NULL;
  200. FNeedSession = false;
  201. FDoNotIdleCurrentTerminal = 0;
  202. FIncrementalSearching = 0;
  203. FQueueFileList.reset(new TQueueFileList());
  204. FProgressSide = osCurrent;
  205. FCalculateSizeOperation = NULL;
  206. FDownloadingFromClipboard = false;
  207. FClipboardFakeMonitorsPendingReset = false;
  208. FHiContrastTheme = NULL;
  209. InitializeRemoteThumbnailMask();
  210. GlyphsModule->PixelsPerInch = CurrentPPI;
  211. FEditorManager = new TEditorManager();
  212. FEditorManager->OnFileChange = ExecutedFileChanged;
  213. FEditorManager->OnFileReload = ExecutedFileReload;
  214. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  215. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  216. FLocalEditors = new TList();
  217. FQueueStatus = NULL;
  218. FQueueStatusSection = new TCriticalSection();
  219. FQueueStatusInvalidated = false;
  220. FQueueItemInvalidated = false;
  221. FQueueStatusUpdating = false;
  222. FQueueActedItem = NULL;
  223. FQueueController = new TQueueController(QueueView3);
  224. FQueueScrollOnDragOver = new TListViewScrollOnDragOver(QueueView3, true);
  225. FUserActionTimer = new TTimer(this);
  226. FUserActionTimer->Enabled = false;
  227. FUserActionTimer->Interval = 10;
  228. FUserActionTimer->OnTimer = UserActionTimer;
  229. FNotes = new TStringList();
  230. FNoteTimer = new TTimer(this);
  231. FNoteTimer->Enabled = false;
  232. FNoteTimer->OnTimer = NoteTimer;
  233. FOnNoteClick = NULL;
  234. FOle32Library = LoadLibrary(L"Ole32.dll");
  235. FDragMoveCursor = FOle32Library != NULL ?
  236. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  237. UseSystemSettings(this);
  238. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  239. static_cast<TObject*>(GetComponent(fcTransferList)));
  240. DebugAssert(TransferList != NULL);
  241. FTransferListHoverIndex = -1;
  242. TransferList->OnChange = TransferListChange;
  243. TransferList->OnDrawItem = TransferListDrawItem;
  244. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  245. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  246. UseDesktopFont(SessionsPageControl);
  247. UseDesktopFont(RemoteDirView);
  248. UseDesktopFont(RemoteDriveView);
  249. UseDesktopFont(QueueView3);
  250. UseDesktopFont(QueueFileList);
  251. UseDesktopFont(QueueLabel);
  252. UseDesktopFont(RemoteStatusBar);
  253. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  254. QueueSplitter->ShowHint = true;
  255. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  256. QueueFileListSplitter->ShowHint = true;
  257. RemotePanelSplitter->ShowHint = true;
  258. UpdateImages();
  259. FCustomCommandMenu = CreateTBXPopupMenu(this);
  260. FCustomCommandLocalFileList = NULL;
  261. FCustomCommandRemoteFileList = NULL;
  262. FSessionColors = new TPngImageList(this);
  263. FSessionColors->ColorDepth = cd32Bit;
  264. AddFixedSessionImages();
  265. SessionsPageControl->Images = FSessionColors;
  266. // Needed for Explorer (Commander calls it implicitly on its own)
  267. UpdateSessionsPageControlHeight();
  268. UpdateQueueLabel();
  269. CheckStoreTransition();
  270. CreateHiddenWindow();
  271. StartUpdates();
  272. // Start loading drive data, so that they are ready once we need them for StartCreationDirectoryMonitorsOnEachDrive
  273. // (necessary for "explorer" only, as "commander" loads it for its drive menu)
  274. DriveInfo->NeedData();
  275. }
  276. //---------------------------------------------------------------------------
  277. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  278. {
  279. AppLog(L"Destroying main window");
  280. FInvalid = true;
  281. ReleaseHiContrastTheme();
  282. if (FClipboardTerminal != NULL)
  283. {
  284. ClipboardClear(); // implies ClipboardStop
  285. }
  286. // this has to be one of the very first things to do
  287. StopUpdateThread();
  288. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  289. TTerminalManager::Instance()->ScpExplorer = NULL;
  290. delete FCustomCommandLocalFileList;
  291. delete FCustomCommandRemoteFileList;
  292. delete FCustomCommandMenu;
  293. delete FTrayIcon;
  294. FTrayIcon = NULL;
  295. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  296. delete FEditorManager;
  297. ForceCloseLocalEditors();
  298. delete FLocalEditors;
  299. HideFileFindDialog();
  300. if (FDelayedDeletionTimer)
  301. {
  302. DoDelayedDeletion(NULL);
  303. SAFE_DESTROY(FDelayedDeletionTimer);
  304. }
  305. SAFE_DESTROY(FDelayedDeletionList);
  306. // sometimes we do not get DDEnd so the list is not released
  307. SAFE_DESTROY(FDDFileList);
  308. DebugAssert(FSynchronizeController == NULL);
  309. CloseHandle(FDDExtMutex);
  310. FDDExtMutex = NULL;
  311. FreeLibrary(FOle32Library);
  312. FOle32Library = NULL;
  313. FDragMoveCursor = NULL;
  314. DebugAssert(!FErrorList);
  315. if (FEverShown)
  316. {
  317. // when window is never shown (like when running command-line operation),
  318. // particularly window site is not restored correctly (BoundsRect value set
  319. // in RestoreForm gets lost during handle allocation), so we do not want
  320. // it to be stored
  321. AppLog(L"Storing main window configuration");
  322. StoreParams();
  323. }
  324. ManagedSession = NULL;
  325. Queue = NULL;
  326. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  327. NonVisualDataModule->ScpExplorer = NULL;
  328. delete FQueueScrollOnDragOver;
  329. FQueueScrollOnDragOver = NULL;
  330. delete FQueueController;
  331. FQueueController = NULL;
  332. delete FQueueStatusSection;
  333. FQueueStatusSection = NULL;
  334. delete FQueueStatus;
  335. FQueueStatus = NULL;
  336. delete FUserActionTimer;
  337. FUserActionTimer = NULL;
  338. delete FNoteTimer;
  339. delete FNotes;
  340. delete FNoteData;
  341. SAFE_DESTROY(FDocks);
  342. SessionsPageControl->Images = NULL;
  343. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  344. SAFE_DESTROY(FSessionColors);
  345. SAFE_DESTROY(FSessionsDragDropFilesEx);
  346. SAFE_DESTROY(FHistoryMenu[0][0]);
  347. SAFE_DESTROY(FHistoryMenu[0][1]);
  348. SAFE_DESTROY(FHistoryMenu[1][0]);
  349. SAFE_DESTROY(FHistoryMenu[1][1]);
  350. if (FHiddenWindow != NULL)
  351. {
  352. DestroyWindow(FHiddenWindow);
  353. FHiddenWindow = NULL;
  354. }
  355. AppLog(L"Main window destroyed");
  356. }
  357. //---------------------------------------------------------------------------
  358. void TCustomScpExplorerForm::ReleaseHiContrastTheme()
  359. {
  360. if (FHiContrastTheme != NULL)
  361. {
  362. ReleaseTBXTheme(FHiContrastTheme);
  363. FHiContrastTheme = NULL;
  364. }
  365. }
  366. //---------------------------------------------------------------------------
  367. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  368. {
  369. std::unique_ptr<TSessionData> Data;
  370. if (!Session.IsEmpty())
  371. {
  372. bool DefaultsOnly;
  373. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  374. }
  375. TTerminalManager * Manager = TTerminalManager::Instance();
  376. for (int Index = 0; Index < Manager->Count; Index++)
  377. {
  378. TTerminal * Terminal = Manager->Sessions[Index];
  379. if (Session.IsEmpty() ||
  380. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  381. {
  382. if (Path.IsEmpty())
  383. {
  384. Terminal->ClearCaches();
  385. }
  386. else
  387. {
  388. Terminal->DirectoryModified(Path, true);
  389. }
  390. }
  391. }
  392. // We should flag a pending refresh for the background terminals or busy foreground terminals
  393. if (HasActiveTerminal() &&
  394. CanCommandLineFromAnotherInstance() &&
  395. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  396. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  397. {
  398. Terminal->ReloadDirectory();
  399. }
  400. }
  401. //---------------------------------------------------------------------------
  402. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  403. {
  404. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  405. AppLogFmt(L"Received copy data message with size %d", (int(CopyData->cbData)));
  406. size_t MessageSize = sizeof(TCopyDataMessage);
  407. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  408. if (Result)
  409. {
  410. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  411. Result = (Message.Version == TCopyDataMessage::Version1);
  412. if (Result)
  413. {
  414. switch (Message.Command)
  415. {
  416. case TCopyDataMessage::CommandCanCommandLine:
  417. Result = CanCommandLineFromAnotherInstance();
  418. if (Result)
  419. {
  420. AppLog(L"Can process command-line from other instance");
  421. }
  422. else
  423. {
  424. AppLog(L"Cannot process command-line from other instance");
  425. }
  426. break;
  427. case TCopyDataMessage::CommandCommandLine:
  428. {
  429. UnicodeString CommandLine(Message.CommandLine);
  430. Result = CommandLineFromAnotherInstance(CommandLine);
  431. }
  432. break;
  433. case TCopyDataMessage::RefreshPanel:
  434. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  435. break;
  436. case TCopyDataMessage::MainWindowCheck:
  437. Result = true;
  438. break;
  439. default:
  440. Result = false;
  441. break;
  442. }
  443. }
  444. }
  445. Message.Result = Result ? 1 : 0;
  446. }
  447. //---------------------------------------------------------------------------
  448. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  449. {
  450. WNDCLASS WindowClass = {0};
  451. WindowClass.lpfnWndProc = DefWindowProc;
  452. WindowClass.hInstance = HInstance;
  453. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  454. FHiddenWindow = NULL;
  455. if (RegisterClass(&WindowClass))
  456. {
  457. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  458. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  459. }
  460. }
  461. //---------------------------------------------------------------------------
  462. bool __fastcall TCustomScpExplorerForm::CanConsole()
  463. {
  464. return
  465. HasActiveTerminal() &&
  466. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  467. }
  468. //---------------------------------------------------------------------------
  469. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  470. {
  471. bool Result = Visible && !NonVisualDataModule->Busy;
  472. return Result;
  473. }
  474. //---------------------------------------------------------------------------
  475. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  476. const UnicodeString & CommandLine)
  477. {
  478. TProgramParams Params(CommandLine);
  479. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  480. if (Result)
  481. {
  482. NonVisualDataModule->StartBusy();
  483. try
  484. {
  485. // this action is initiated from another process,
  486. // so it's likely that our window is not visible,
  487. // and user won't see what is going on
  488. Application->BringToFront();
  489. // reload sessions as we may be asked to open a session
  490. // just stored by another instance
  491. StoredSessions->Reload();
  492. UnicodeString SessionName = Params.Param[1];
  493. std::unique_ptr<TObjectList> DataList(new TObjectList());
  494. try
  495. {
  496. UnicodeString DownloadFile; // unused
  497. int Flags = GetCommandLineParseUrlFlags(&Params);
  498. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  499. }
  500. catch (EAbort &)
  501. {
  502. // Happens when opening session in PuTTY and in other situations,
  503. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  504. // to process the command-line on its own (or to pass it to yet another instance)
  505. }
  506. if (DataList->Count > 0)
  507. {
  508. TTerminalManager * Manager = TTerminalManager::Instance();
  509. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  510. }
  511. }
  512. __finally
  513. {
  514. NonVisualDataModule->EndBusy();
  515. }
  516. }
  517. return Result;
  518. }
  519. //---------------------------------------------------------------------------
  520. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  521. {
  522. TManagedTerminal * Result;
  523. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  524. {
  525. Result = FManagedSession;
  526. }
  527. else
  528. {
  529. Result = NULL;
  530. }
  531. return Result;
  532. }
  533. //---------------------------------------------------------------------------
  534. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  535. {
  536. if (FManagedSession != value)
  537. {
  538. TWindowLock Lock(this);
  539. SessionChanging();
  540. DoSetManagedSession(value, false);
  541. }
  542. }
  543. //---------------------------------------------------------------------------
  544. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  545. {
  546. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  547. FManagedSession = value;
  548. {
  549. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect, false);
  550. TAutoFlag SessionChangingFlag(FSessionChanging);
  551. SessionChanged(Replace);
  552. }
  553. if (Terminal != NULL)
  554. {
  555. TransferPresetAutoSelect();
  556. }
  557. // Update app and tab titles, prevented above by FSessionChanging.
  558. UpdateControls();
  559. }
  560. //---------------------------------------------------------------------------
  561. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  562. {
  563. FEditorManager->ProcessFiles(FileTerminalReplaced, value);
  564. DoSetManagedSession(value, true);
  565. }
  566. //---------------------------------------------------------------------------
  567. void __fastcall TCustomScpExplorerForm::SessionChanging()
  568. {
  569. if (ManagedSession != NULL)
  570. {
  571. UpdateSession(ManagedSession);
  572. }
  573. }
  574. //---------------------------------------------------------------------------
  575. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  576. {
  577. if (ManagedSession != NULL)
  578. {
  579. UpdateSessionColor(static_cast<TColor>(ManagedSession->StateData->Color));
  580. }
  581. if (!TTerminalManager::Instance()->Updating)
  582. {
  583. SessionListChanged();
  584. }
  585. if (Terminal != NULL)
  586. {
  587. TObject * State = NULL;
  588. if (WinConfiguration->PreservePanelState)
  589. {
  590. State = Terminal->RemoteExplorerState;
  591. DirView(osRemote)->AnnounceState(State);
  592. }
  593. try
  594. {
  595. if (Replaced)
  596. {
  597. RemoteDirView->ReplaceTerminal(Terminal);
  598. }
  599. else
  600. {
  601. RemoteDirView->Terminal = Terminal;
  602. }
  603. if (Terminal->Active)
  604. {
  605. Terminal->RefreshDirectory();
  606. }
  607. if (State != NULL)
  608. {
  609. DirView(osRemote)->RestoreState(State);
  610. }
  611. }
  612. __finally
  613. {
  614. DirView(osRemote)->AnnounceState(NULL);
  615. }
  616. }
  617. else
  618. {
  619. RemoteDirView->Terminal = NULL;
  620. }
  621. InitStatusBar();
  622. UpdateTransferList();
  623. // Update panels Enable state before refreshing the labels
  624. UpdateControls();
  625. }
  626. //---------------------------------------------------------------------------
  627. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  628. {
  629. if (Queue != value)
  630. {
  631. if (FQueue != NULL)
  632. {
  633. FQueue->OnListUpdate = NULL;
  634. FQueue->OnQueueItemUpdate = NULL;
  635. }
  636. FQueue = value;
  637. if (FQueue != NULL)
  638. {
  639. DebugAssert(FQueue->OnListUpdate == NULL);
  640. FQueue->OnListUpdate = QueueListUpdate;
  641. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  642. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  643. }
  644. QueueChanged();
  645. }
  646. }
  647. //---------------------------------------------------------------------------
  648. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  649. TListItem * Item)
  650. {
  651. if (FQueueActedItem == Item)
  652. {
  653. FQueueActedItem = NULL;
  654. if ((QueueView3->PopupMenu != NULL) &&
  655. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  656. {
  657. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  658. ReleaseCapture();
  659. }
  660. }
  661. if (Item->Data == FPendingQueueActionItem)
  662. {
  663. FPendingQueueActionItem = NULL;
  664. }
  665. }
  666. //---------------------------------------------------------------------------
  667. bool TCustomScpExplorerForm::IsAnythingQueued()
  668. {
  669. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  670. }
  671. //---------------------------------------------------------------------------
  672. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  673. {
  674. {
  675. TGuard Guard(FQueueStatusSection);
  676. FQueueStatusInvalidated = false;
  677. if (FQueue != NULL)
  678. {
  679. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  680. }
  681. }
  682. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  683. {
  684. FMaxQueueLength = FQueueStatus->Count;
  685. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  686. }
  687. {
  688. TAutoFlag Flag(FQueueStatusUpdating);
  689. FQueueController->UpdateQueueStatus(FQueueStatus);
  690. }
  691. UpdateQueueFileList();
  692. SetQueueProgress();
  693. UpdateQueueView();
  694. bool IsEmpty = !IsAnythingQueued();
  695. if (IsEmpty && (Terminal != NULL))
  696. {
  697. TOnceDoneOperation OnceDoneOperation =
  698. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  699. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  700. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  701. {
  702. FQueue->Enabled = false;
  703. }
  704. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  705. {
  706. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  707. }
  708. }
  709. }
  710. //---------------------------------------------------------------------------
  711. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  712. {
  713. UnicodeString Result;
  714. if (FQueueStatus != NULL)
  715. {
  716. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  717. {
  718. TFileOperationProgressType * ProgressData =
  719. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  720. if ((ProgressData != NULL) && ProgressData->InProgress)
  721. {
  722. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  723. }
  724. }
  725. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  726. {
  727. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  728. }
  729. }
  730. return Result;
  731. }
  732. //---------------------------------------------------------------------------
  733. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  734. {
  735. ComponentVisible[fcQueueView] =
  736. !IsLocalBrowserMode() &&
  737. ((WinConfiguration->QueueView.Show == qvShow) ||
  738. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  739. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  740. }
  741. //---------------------------------------------------------------------------
  742. void __fastcall TCustomScpExplorerForm::QueueChanged()
  743. {
  744. if (FQueueStatus != NULL)
  745. {
  746. // UpdateFileList implementation relies on the status being recreated when session changes
  747. delete FQueueStatus;
  748. FQueueStatus = NULL;
  749. }
  750. UpdateQueueStatus(true);
  751. }
  752. //---------------------------------------------------------------------------
  753. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  754. {
  755. if (FQueue == Queue)
  756. {
  757. FQueueStatusInvalidated = true;
  758. }
  759. }
  760. //---------------------------------------------------------------------------
  761. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  762. TQueueItem * Item)
  763. {
  764. if (FQueue == Queue)
  765. {
  766. TGuard Guard(FQueueStatusSection);
  767. // this may be running in parallel with QueueChanged
  768. if (FQueueStatus != NULL)
  769. {
  770. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  771. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  772. {
  773. FRefreshLocalDirectory = (QueueItem == NULL) ||
  774. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  775. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  776. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  777. }
  778. if (QueueItem != NULL)
  779. {
  780. QueueItem->UserData = (void*)true;
  781. FQueueItemInvalidated = true;
  782. }
  783. }
  784. }
  785. }
  786. //---------------------------------------------------------------------------
  787. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  788. {
  789. // during standalone editing, we have no way to see/control queue,
  790. // so we have to always popup prompts automatically
  791. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  792. }
  793. //---------------------------------------------------------------------------
  794. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  795. {
  796. if (FQueueStatus != NULL)
  797. {
  798. bool QueueAutoPopup = IsQueueAutoPopup();
  799. bool NeedRefresh = FQueueController->NeedRefresh();
  800. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  801. FQueueItemInvalidated = false;
  802. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  803. bool Updated = false;
  804. TQueueItemProxy * QueueItem;
  805. bool WasUserAction;
  806. for (int Index = 0; Index < Limit; Index++)
  807. {
  808. bool Update = false;
  809. QueueItem = FQueueStatus->Items[Index];
  810. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  811. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  812. (FPendingQueueActionItem == NULL))
  813. {
  814. FPendingQueueActionItem = QueueItem;
  815. FUserActionTimer->Enabled = true;
  816. }
  817. if ((bool)QueueItem->UserData)
  818. {
  819. QueueItem->UserData = (void*)false;
  820. QueueItem->Update();
  821. Updated = true;
  822. Update = true;
  823. }
  824. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  825. {
  826. Update = true;
  827. }
  828. if (Update)
  829. {
  830. FQueueController->RefreshQueueItem(QueueItem);
  831. }
  832. }
  833. if (Updated)
  834. {
  835. NonVisualDataModule->UpdateNonVisibleActions();
  836. SetQueueProgress();
  837. UpdateQueueFileList();
  838. }
  839. }
  840. }
  841. //---------------------------------------------------------------------------
  842. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  843. {
  844. if (FTaskbarList != NULL)
  845. {
  846. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  847. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  848. }
  849. }
  850. //---------------------------------------------------------------------------
  851. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  852. {
  853. if (Progress >= 0)
  854. {
  855. if (FTaskbarList != NULL)
  856. {
  857. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  858. }
  859. }
  860. else
  861. {
  862. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  863. }
  864. }
  865. //---------------------------------------------------------------------------
  866. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  867. {
  868. int OverallProgress;
  869. // FProgressForm is null when this is called from SetQueueProgress
  870. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  871. {
  872. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  873. }
  874. else if (!ProgressData->IsIndeterminate())
  875. {
  876. OverallProgress = ProgressData->OverallProgress();
  877. }
  878. else
  879. {
  880. OverallProgress = -1;
  881. }
  882. SetTaskbarListProgressValue(OverallProgress);
  883. }
  884. //---------------------------------------------------------------------------
  885. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  886. {
  887. TTerminalManager::Instance()->QueueStatusUpdated();
  888. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  889. {
  890. // Do not animate task bar button, when items are queued, but processing is disabled
  891. if ((FQueueStatus != NULL) && (FQueueStatus->ActivePrimaryCount > 0))
  892. {
  893. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  894. {
  895. TFileOperationProgressType * ProgressData = NULL;
  896. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  897. {
  898. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  899. }
  900. if ((ProgressData != NULL) &&
  901. ProgressData->InProgress)
  902. {
  903. SetTaskbarListProgressValue(ProgressData);
  904. }
  905. else
  906. {
  907. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  908. }
  909. }
  910. else
  911. {
  912. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  913. }
  914. }
  915. else
  916. {
  917. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  918. }
  919. }
  920. UpdateQueueLabel();
  921. }
  922. //---------------------------------------------------------------------------
  923. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  924. {
  925. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  926. if (FQueueStatus != NULL)
  927. {
  928. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  929. if (ActiveAndPendingPrimaryCount > 0)
  930. {
  931. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  932. }
  933. }
  934. QueueLabel->Caption = Caption;
  935. }
  936. //---------------------------------------------------------------------------
  937. void TCustomScpExplorerForm::CheckStoreTransition()
  938. {
  939. if (WinConfiguration->StoreTransition == stInit)
  940. {
  941. if (IsUWP())
  942. {
  943. WinConfiguration->StoreTransition = stStoreFresh;
  944. AppLog(L"Fresh UWP/store installation");
  945. }
  946. else
  947. {
  948. WinConfiguration->StoreTransition = stStandard;
  949. AppLog(L"Fresh standard installation");
  950. }
  951. }
  952. else if ((WinConfiguration->StoreTransition == stStandard) && IsUWP())
  953. {
  954. WinConfiguration->StoreTransition = stStoreMigrated;
  955. WinConfiguration->FirstRun = WinConfiguration->FirstRun + L"=>" + StandardDatestamp();
  956. AppLog(L"Standard installation migrated to store installation");
  957. }
  958. if (WinConfiguration->StoreTransition == stStoreMigrated)
  959. {
  960. TTBXToolbar * Toolbar = new TTBXToolbar(MessageDock);
  961. Toolbar->Parent = MessageDock;
  962. Toolbar->FullSize = true;
  963. Toolbar->Color = Application->HintColor;
  964. TTBXLabelItem * LabelItem;
  965. LabelItem = new TTBXLabelItem(Toolbar);
  966. // Convert to explicit RGB "black" color, to make sure it is contrasting even in Dark theme
  967. // (TTBXOfficeXPTheme.GetStandardColor would convert clWindow to "white" in dark theme)
  968. LabelItem->FontSettings->Color = static_cast<TColor>(GetSysColor(clWindow));
  969. LabelItem->Margin = ScaleByTextHeight(MessageDock, 2); // Is this automatically rescaled?
  970. LabelItem->Caption = LoadStr(STORE_MIGRATION_THANKS);
  971. Toolbar->Items->Add(LabelItem);
  972. TTBXCustomItem * LinkItem = new TTBXItem(Toolbar);
  973. LinkItem->FontSettings->Color = LinkColor;
  974. LinkItem->Caption = LoadStr(STORE_MIGRATION_LEARN);
  975. LinkItem->OnClick = StoreTransitionLinkClick;
  976. Toolbar->Items->Add(LinkItem);
  977. AddMenuSeparator(Toolbar->Items);
  978. TTBXCustomItem * CloseItem = new TTBXItem(Toolbar);
  979. CloseItem->FontSettings->Color = static_cast<TColor>(GetSysColor(clWindow));
  980. CloseItem->Caption = LoadStr(MESSAGE_DISMISS);
  981. CloseItem->OnClick = StoreTransitionCloseClick;
  982. Toolbar->Items->Add(CloseItem);
  983. GiveTBItemPriority(CloseItem);
  984. }
  985. }
  986. //---------------------------------------------------------------------------
  987. void __fastcall TCustomScpExplorerForm::StoreTransitionLinkClick(TObject *)
  988. {
  989. ShowHelp(HELP_STORE_TRANSITION);
  990. }
  991. //---------------------------------------------------------------------------
  992. void __fastcall TCustomScpExplorerForm::StoreTransitionCloseClick(TObject * Sender)
  993. {
  994. TTBCustomItem * Item = DebugNotNull(dynamic_cast<TTBCustomItem *>(Sender));
  995. TTBXToolbar * Toolbar = DebugNotNull(dynamic_cast<TTBXToolbar *>(DebugNotNull(DebugNotNull(Item->Parent)->ParentComponent)));
  996. delete Toolbar;
  997. WinConfiguration->StoreTransition = stStoreAcknowledged;
  998. UpdateControls();
  999. }
  1000. //---------------------------------------------------------------------------
  1001. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  1002. {
  1003. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  1004. static_cast<TComponent*>(GetComponent(fcTransferList)));
  1005. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  1006. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  1007. TransferList->Strings->BeginUpdate();
  1008. try
  1009. {
  1010. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  1011. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  1012. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  1013. if (FTransferDropDownHint.IsEmpty())
  1014. {
  1015. FTransferDropDownHint = TransferDropDown->Hint;
  1016. }
  1017. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  1018. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  1019. TransferDropDown->Text = StripHotkey(Name);
  1020. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  1021. (FTransferDropDownHint, StripHotkey(Name),
  1022. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  1023. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  1024. // update the label, otherwise when it is updated only on the first draw
  1025. // of the list, it is drawn "bold" for some reason
  1026. FTransferListHoverIndex = TransferList->ItemIndex;
  1027. UpdateTransferLabel();
  1028. }
  1029. __finally
  1030. {
  1031. TransferList->Strings->EndUpdate();
  1032. }
  1033. }
  1034. //---------------------------------------------------------------------------
  1035. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  1036. {
  1037. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  1038. DebugAssert(Toolbar != NULL);
  1039. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  1040. }
  1041. //---------------------------------------------------------------------------
  1042. void __fastcall TCustomScpExplorerForm::UpdateActions()
  1043. {
  1044. TForm::UpdateActions();
  1045. if (ComponentVisible[fcCustomCommandsBand])
  1046. {
  1047. UpdateCustomCommandsToolbar();
  1048. }
  1049. }
  1050. //---------------------------------------------------------------------------
  1051. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  1052. {
  1053. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  1054. }
  1055. //---------------------------------------------------------------------------
  1056. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  1057. {
  1058. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  1059. {
  1060. DirView->RowSelect = WinConfiguration->FullRowSelect;
  1061. // Without this, the panel turns gray and unused part of header turns black
  1062. DirView->Perform(CM_COLORCHANGED, 0, 0);
  1063. }
  1064. }
  1065. //---------------------------------------------------------------------------
  1066. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  1067. {
  1068. Color = GetBtnFaceColor();
  1069. DebugAssert(Configuration && RemoteDirView);
  1070. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1071. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  1072. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  1073. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  1074. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1075. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1076. RemoteDirView->AlwaysSortDirectoriesByName = WinConfiguration->AlwaysSortDirectoriesByName;
  1077. UpdateRowSelect(RemoteDirView);
  1078. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1079. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  1080. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  1081. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1082. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1083. UpdateSessionsPageControlHeight();
  1084. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1085. UpdateToolbarDisplayMode();
  1086. UpdateTransferList();
  1087. if (Terminal != NULL)
  1088. {
  1089. TransferPresetAutoSelect();
  1090. }
  1091. UpdateQueueView();
  1092. UpdateControls();
  1093. // this can be called even before constuctor finishes.
  1094. if (FEditorManager != NULL)
  1095. {
  1096. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  1097. }
  1098. // this can be called even before constuctor finishes.
  1099. if (FLocalEditors != NULL)
  1100. {
  1101. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  1102. {
  1103. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  1104. }
  1105. }
  1106. if (ComponentVisible[fcCustomCommandsBand])
  1107. {
  1108. UpdateCustomCommandsToolbar();
  1109. }
  1110. // show only when keeping queue items forever,
  1111. // otherwise, it's enough to have it in the context menu
  1112. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1113. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1114. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1115. {
  1116. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1117. FFileColorsCurrent = WinConfiguration->FileColors;
  1118. FileColorsChanged();
  1119. }
  1120. if ((GetNewTabTab()->CaptionTruncation != GetNewTabTabCaptionTruncation()) &&
  1121. (FSessionColors != NULL))
  1122. {
  1123. SessionListChanged();
  1124. }
  1125. if (GlyphsModule->LargerToolbar != WinConfiguration->LargerToolbar)
  1126. {
  1127. GlyphsModule->LargerToolbar = WinConfiguration->LargerToolbar;
  1128. if (FSessionColors != NULL)
  1129. {
  1130. RegenerateSessionColorsImageList();
  1131. UpdateSessionsPageControlHeight();
  1132. }
  1133. }
  1134. }
  1135. //---------------------------------------------------------------------------
  1136. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1137. {
  1138. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1139. DirView->Invalidate();
  1140. }
  1141. //---------------------------------------------------------------------------
  1142. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1143. {
  1144. DoFileColorsChanged(RemoteDirView);
  1145. }
  1146. //---------------------------------------------------------------------------
  1147. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1148. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1149. void * /*Arg*/)
  1150. {
  1151. if (Token != NULL)
  1152. {
  1153. TForm * Editor = dynamic_cast<TForm*>(Token);
  1154. DebugAssert(Editor != NULL);
  1155. ReconfigureEditorForm(Editor);
  1156. }
  1157. }
  1158. //---------------------------------------------------------------------------
  1159. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1160. {
  1161. WinConfiguration->DDTransferConfirmation = asOn;
  1162. }
  1163. //---------------------------------------------------------------------------
  1164. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1165. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1166. {
  1167. // noop
  1168. }
  1169. //---------------------------------------------------------------------------
  1170. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1171. {
  1172. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1173. }
  1174. //---------------------------------------------------------------------------
  1175. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1176. {
  1177. if (DoNotShowAgain)
  1178. {
  1179. if (DragDrop)
  1180. {
  1181. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1182. {
  1183. PopupTrayBalloon(
  1184. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1185. }
  1186. WinConfiguration->DDTransferConfirmation = asOff;
  1187. }
  1188. else
  1189. {
  1190. WinConfiguration->ConfirmTransferring = false;
  1191. }
  1192. }
  1193. else
  1194. {
  1195. // User explicitly unchecked "do not show again",
  1196. // so show him the dialog the next time
  1197. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1198. {
  1199. WinConfiguration->DDTransferConfirmation = asOn;
  1200. }
  1201. }
  1202. }
  1203. //---------------------------------------------------------------------------
  1204. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1205. TTransferDirection Direction, TTransferType Type, bool Temp,
  1206. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1207. bool Confirm, bool DragDrop, int Options)
  1208. {
  1209. bool Result = true;
  1210. DebugAssert(HasActiveTerminal());
  1211. // these parameters are known in advance
  1212. int Params =
  1213. FLAGMASK(Type == ttMove, cpDelete);
  1214. bool ToTemp = (Temp && (Direction == tdToLocal));
  1215. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1216. {
  1217. bool DisableNewerOnly =
  1218. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1219. ToTemp;
  1220. Options |=
  1221. FLAGMASK(ToTemp, coTemp) |
  1222. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1223. coDoNotShowAgain;
  1224. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1225. int CopyParamAttrs = (Direction == tdToRemote ?
  1226. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1227. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1228. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1229. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1230. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1231. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1232. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1233. if (Result)
  1234. {
  1235. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1236. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1237. }
  1238. }
  1239. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1240. {
  1241. Configuration->Usage->Inc(L"TransfersOnBackground");
  1242. // these parameter are known only after transfer dialog
  1243. Params |=
  1244. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1245. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1246. Result = false;
  1247. ClearTransferSourceSelection(Direction);
  1248. }
  1249. return Result;
  1250. }
  1251. //---------------------------------------------------------------------------
  1252. void TCustomScpExplorerForm::ClearOperationSelection(TOperationSide Side)
  1253. {
  1254. if (FOnFileOperationFinished != NULL)
  1255. {
  1256. FOnFileOperationFinished(Side, UnicodeString(), true, true);
  1257. }
  1258. else
  1259. {
  1260. if (HasDirView[Side])
  1261. {
  1262. DirView(Side)->SelectAll(smNone);
  1263. }
  1264. }
  1265. }
  1266. //---------------------------------------------------------------------------
  1267. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1268. {
  1269. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1270. ClearOperationSelection(Side);
  1271. }
  1272. //---------------------------------------------------------------------------
  1273. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1274. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1275. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1276. int Params)
  1277. {
  1278. DebugAssert(Queue != NULL);
  1279. TQueueItem * QueueItem;
  1280. if (Direction == tdToRemote)
  1281. {
  1282. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1283. QueueItem =
  1284. new TUploadQueueItem(
  1285. Terminal, FileList, TargetDirectory, &CopyParam, Params, CopyParam.QueueParallel);
  1286. }
  1287. else
  1288. {
  1289. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1290. QueueItem =
  1291. new TDownloadQueueItem(
  1292. Terminal, FileList, TargetDirectory, &CopyParam, Params, CopyParam.QueueParallel);
  1293. }
  1294. AddQueueItem(Queue, QueueItem, Terminal);
  1295. }
  1296. //---------------------------------------------------------------------------
  1297. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1298. {
  1299. if (Queue->IsEmpty)
  1300. {
  1301. Terminal->QueueOperationStart = Now();
  1302. }
  1303. Queue->AddItem(QueueItem);
  1304. }
  1305. //---------------------------------------------------------------------------
  1306. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1307. {
  1308. }
  1309. //---------------------------------------------------------------------------
  1310. void __fastcall TCustomScpExplorerForm::InitControls()
  1311. {
  1312. DebugAssert(FDocks != NULL);
  1313. for (int Index = 0; Index < ComponentCount; Index++)
  1314. {
  1315. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1316. if ((Dock != NULL) && (Dock->Tag == 0))
  1317. {
  1318. FDocks->Add(Dock);
  1319. }
  1320. }
  1321. CollectItemsWithTextDisplayMode(this);
  1322. }
  1323. //---------------------------------------------------------------------------
  1324. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1325. {
  1326. ConfigurationChanged();
  1327. QueuePanel->Height =
  1328. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1329. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1330. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1331. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1332. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1333. QueueFileList->Height =
  1334. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1335. if (QueueFileList->Visible)
  1336. {
  1337. AdjustQueueLayout();
  1338. }
  1339. UpdateCustomCommandsToolbar();
  1340. }
  1341. //---------------------------------------------------------------------------
  1342. void __fastcall TCustomScpExplorerForm::StoreParams()
  1343. {
  1344. int APixelsPerInch = GetControlPixelsPerInch(this);
  1345. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1346. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1347. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1348. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1349. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1350. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1351. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1352. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1353. }
  1354. //---------------------------------------------------------------------------
  1355. void __fastcall TCustomScpExplorerForm::Loaded()
  1356. {
  1357. // Loaded is called in more deterministic moment than previously used CreateParams
  1358. if (!FFormRestored)
  1359. {
  1360. FFormRestored = true;
  1361. RestoreFormParams();
  1362. }
  1363. TForm::Loaded();
  1364. }
  1365. //---------------------------------------------------------------------------
  1366. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1367. {
  1368. DebugAssert(FDocks != NULL);
  1369. for (int Index = 0; Index < FDocks->Count; Index++)
  1370. {
  1371. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1372. }
  1373. }
  1374. //---------------------------------------------------------------------------
  1375. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1376. {
  1377. SetDockAllowDrag(true);
  1378. while (!ButtonsStr.IsEmpty())
  1379. {
  1380. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1381. UnicodeString Name = CutToChar(S, L'=', true);
  1382. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1383. {
  1384. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1385. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1386. {
  1387. while (!S.IsEmpty())
  1388. {
  1389. UnicodeString S2 = CutToChar(S, L',', true);
  1390. UnicodeString Name = CutToChar(S2, L':', true);
  1391. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1392. {
  1393. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1394. if (SameText(GetToolbarItemName(Item), Name))
  1395. {
  1396. // forward compatibility
  1397. UnicodeString S3 = CutToChar(S2, L':', true);
  1398. bool Visible = StrToIntDef(S3, 0);
  1399. Item->Visible = Visible;
  1400. }
  1401. }
  1402. }
  1403. }
  1404. }
  1405. }
  1406. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1407. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1408. }
  1409. //---------------------------------------------------------------------------
  1410. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1411. {
  1412. UnicodeString Result;
  1413. SetDockAllowDrag(true);
  1414. Result = ::GetToolbarsLayoutStr(this);
  1415. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1416. return Result;
  1417. }
  1418. //---------------------------------------------------------------------------
  1419. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1420. {
  1421. UnicodeString Result;
  1422. if (Item->Action != NULL)
  1423. {
  1424. Result = Item->Action->Name;
  1425. Result = RemoveSuffix(Result, L"Action", true);
  1426. }
  1427. else
  1428. {
  1429. Result = Item->Name;
  1430. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1431. Result = RemoveSuffix(Result, L"Item", true);
  1432. }
  1433. return Result;
  1434. }
  1435. //---------------------------------------------------------------------------
  1436. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1437. {
  1438. UnicodeString Result;
  1439. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1440. {
  1441. UnicodeString ToolbarStr;
  1442. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1443. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1444. {
  1445. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1446. {
  1447. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1448. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1449. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1450. // and save the changes here only.
  1451. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1452. {
  1453. UnicodeString Name = GetToolbarItemName(Item);
  1454. DebugAssert(Name.Pos(L"TBX") == 0);
  1455. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1456. }
  1457. }
  1458. }
  1459. if (!ToolbarStr.IsEmpty())
  1460. {
  1461. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1462. }
  1463. }
  1464. return Result;
  1465. }
  1466. //---------------------------------------------------------------------------
  1467. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1468. {
  1469. DebugAssert(FProgressForm == NULL);
  1470. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1471. bool AllowMoveToQueue = (FTransferResumeList != NULL) || (FDeletedFiles != NULL);
  1472. FProgressForm = new TProgressForm(Application, AllowMoveToQueue, AllowSkip, SynchronizeProgress);
  1473. FProgressForm->DeleteLocalToRecycleBin =
  1474. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1475. FProgressForm->DeleteRemoteToRecycleBin =
  1476. HasActiveTerminal() &&
  1477. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1478. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1479. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1480. LockWindow();
  1481. // Actually, do not know what hides the progress once the operation finishes
  1482. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1483. SetTaskbarListProgressState(TBPF_NORMAL);
  1484. }
  1485. //---------------------------------------------------------------------------
  1486. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1487. {
  1488. UnlockWindow();
  1489. SAFE_DESTROY(FProgressForm);
  1490. SetQueueProgress();
  1491. }
  1492. //---------------------------------------------------------------------------
  1493. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1494. TFileOperationProgressType & ProgressData)
  1495. {
  1496. // Download to temporary local directory
  1497. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1498. {
  1499. bool Continue = true;
  1500. FPendingTempSpaceWarn = false;
  1501. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1502. if (!Continue)
  1503. {
  1504. Abort();
  1505. }
  1506. }
  1507. // operation is being executed and we still didn't show up progress form
  1508. if (ProgressData.InProgress && !FProgressForm)
  1509. {
  1510. CreateProgressForm(NULL);
  1511. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1512. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1513. }
  1514. // operation is finished (or terminated), so we hide progress form
  1515. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1516. {
  1517. DestroyProgressForm();
  1518. if (ProgressData.Operation == foCalculateSize)
  1519. {
  1520. // When calculating size before transfer, the abort caused by
  1521. // cancel flag set due to "MoveToQueue" below,
  1522. // is not propagated back to us in ExecuteFileOperation,
  1523. // as it is silently swallowed.
  1524. // So we explicitly (re)throw it here.
  1525. if (FMoveToQueue)
  1526. {
  1527. Abort();
  1528. }
  1529. }
  1530. else
  1531. {
  1532. if ((ProgressData.Cancel == csContinue) &&
  1533. !FAutoOperation)
  1534. {
  1535. OperationComplete(ProgressData.StartTime);
  1536. }
  1537. }
  1538. }
  1539. if (FProgressForm &&
  1540. // Can happen only during synchronization, when ending one of the operations
  1541. ProgressData.InProgress)
  1542. {
  1543. FProgressForm->SetProgressData(ProgressData);
  1544. DebugAssert(ProgressData.InProgress);
  1545. SetTaskbarListProgressValue(&ProgressData);
  1546. if (FProgressForm->Cancel > csContinue)
  1547. {
  1548. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1549. if (FProgressForm->Cancel == csCancelFile)
  1550. {
  1551. FProgressForm->ClearCancel();
  1552. }
  1553. // cancel cancels even the move
  1554. FMoveToQueue = false;
  1555. }
  1556. else if (FProgressForm->MoveToQueue)
  1557. {
  1558. FMoveToQueue = true;
  1559. ProgressData.SetCancelAtLeast(csCancel);
  1560. }
  1561. if ((FTransferResumeList != NULL) &&
  1562. ProgressData.InProgress &&
  1563. ProgressData.IsTransfer() &&
  1564. !ProgressData.FullFileName.IsEmpty())
  1565. {
  1566. if ((FTransferResumeList->Count == 0) ||
  1567. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1568. {
  1569. // note that we do not recognize directories from files here
  1570. FTransferResumeList->Add(ProgressData.FullFileName);
  1571. }
  1572. }
  1573. }
  1574. }
  1575. //---------------------------------------------------------------------------
  1576. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1577. const TDateTime & StartTime)
  1578. {
  1579. if (GUIConfiguration->BeepOnFinish &&
  1580. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1581. {
  1582. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1583. DWORD Sound;
  1584. if (!ExtractFileExt(BeepSound).IsEmpty())
  1585. {
  1586. Sound = SND_FILENAME;
  1587. }
  1588. else
  1589. {
  1590. Sound = SND_ALIAS;
  1591. }
  1592. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1593. }
  1594. }
  1595. //---------------------------------------------------------------------------
  1596. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1597. TFileOperationProgressType & ProgressData)
  1598. {
  1599. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1600. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1601. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1602. FileOperationProgress(ProgressData);
  1603. }
  1604. //---------------------------------------------------------------------------
  1605. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1606. {
  1607. UnicodeString Result;
  1608. if (FProgressForm != NULL)
  1609. {
  1610. Result = FProgressForm->ProgressStr();
  1611. }
  1612. return Result;
  1613. }
  1614. //---------------------------------------------------------------------------
  1615. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1616. bool DragDrop)
  1617. {
  1618. return (!DragDrop && (DropSourceControl == NULL)) ||
  1619. (DropSourceControl == DirView(osOther));
  1620. }
  1621. //---------------------------------------------------------------------------
  1622. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1623. {
  1624. TCustomDirView * ADirView = DirView(Side);
  1625. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1626. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1627. // this can happen when local drive is unplugged in the middle of the operation
  1628. if (Item != NULL)
  1629. {
  1630. if (Unselect)
  1631. {
  1632. Item->Selected = false;
  1633. }
  1634. ADirView->MakeProgressVisible(Item);
  1635. }
  1636. return Item;
  1637. }
  1638. //---------------------------------------------------------------------------
  1639. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1640. TFileOperation Operation, TOperationSide Side,
  1641. bool /*Temp*/, const UnicodeString & FileName, bool Success, bool NotCancelled,
  1642. TOnceDoneOperation & OnceDoneOperation)
  1643. {
  1644. if (!FAutoOperation)
  1645. {
  1646. // no selection on "/upload", form serves only as event handler
  1647. // (it is not displayed)
  1648. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1649. ((Operation == FPrimaryOperation) ||
  1650. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1651. {
  1652. TOperationSide DViewSide;
  1653. if ((FProgressSide != osCurrent) &&
  1654. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1655. {
  1656. // assume the operation is over the focused panel
  1657. DViewSide = FProgressSide;
  1658. }
  1659. else
  1660. {
  1661. DViewSide = Side;
  1662. }
  1663. if (FOnFileOperationFinished != NULL)
  1664. {
  1665. FOnFileOperationFinished(DViewSide, FileName, Success, NotCancelled);
  1666. }
  1667. else
  1668. {
  1669. VisualiseOperationFinished(DViewSide, FileName, Success && NotCancelled);
  1670. }
  1671. }
  1672. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1673. {
  1674. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1675. {
  1676. if (Side == osLocal)
  1677. {
  1678. Configuration->Usage->Inc(L"UploadedFiles");
  1679. }
  1680. else
  1681. {
  1682. Configuration->Usage->Inc(L"DownloadedFiles");
  1683. }
  1684. }
  1685. }
  1686. }
  1687. if (Success && (FSynchronizeController != NULL))
  1688. {
  1689. DebugAssert(!IsSideLocalBrowser(osRemote));
  1690. if (Operation == foCopy)
  1691. {
  1692. DebugAssert(Side == osLocal);
  1693. FSynchronizeController->LogOperation(soUpload, FileName);
  1694. }
  1695. else if (Operation == foDelete)
  1696. {
  1697. DebugAssert(Side == osRemote);
  1698. FSynchronizeController->LogOperation(soDelete, FileName);
  1699. }
  1700. }
  1701. if (FProgressForm)
  1702. {
  1703. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1704. }
  1705. }
  1706. //---------------------------------------------------------------------------
  1707. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1708. TFileOperation Operation, TOperationSide Side,
  1709. bool Temp, const UnicodeString & FileName, bool Success, bool NotCancelled,
  1710. TOnceDoneOperation & OnceDoneOperation)
  1711. {
  1712. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1713. DoOperationFinished(Operation, Side, Temp, FileName, Success, NotCancelled, OnceDoneOperation);
  1714. }
  1715. //---------------------------------------------------------------------------
  1716. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1717. {
  1718. return false;
  1719. }
  1720. //---------------------------------------------------------------------------
  1721. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1722. {
  1723. return false;
  1724. }
  1725. //---------------------------------------------------------------------------
  1726. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1727. {
  1728. return false;
  1729. }
  1730. //---------------------------------------------------------------------------
  1731. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1732. {
  1733. return NULL;
  1734. }
  1735. //---------------------------------------------------------------------------
  1736. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1737. {
  1738. DebugAssert(GetSide(Side) == osRemote);
  1739. DebugUsedParam(Side);
  1740. return RemoteDirView;
  1741. }
  1742. //---------------------------------------------------------------------------
  1743. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1744. {
  1745. DebugAssert(GetSide(Side) == osRemote);
  1746. DebugUsedParam(Side);
  1747. return RemoteDriveView;
  1748. }
  1749. //---------------------------------------------------------------------------
  1750. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1751. {
  1752. DebugAssert(GetSide(Side) == osRemote);
  1753. DebugUsedParam(Side);
  1754. return HasActiveTerminal();
  1755. }
  1756. //---------------------------------------------------------------------------
  1757. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1758. TOperationSide Side, int FilesOnly)
  1759. {
  1760. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1761. }
  1762. //---------------------------------------------------------------------------
  1763. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1764. TOperationSide Side, int FilesOnly)
  1765. {
  1766. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1767. }
  1768. //---------------------------------------------------------------------------
  1769. struct THistoryItemData
  1770. {
  1771. short int Side;
  1772. short int Index;
  1773. };
  1774. //---------------------------------------------------------------------------
  1775. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1776. {
  1777. DirView(Side)->HistoryGo(Index);
  1778. }
  1779. //---------------------------------------------------------------------------
  1780. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1781. {
  1782. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1783. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1784. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1785. }
  1786. //---------------------------------------------------------------------------
  1787. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1788. bool Back)
  1789. {
  1790. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1791. {
  1792. TCustomDirView * DView = DirView(Side);
  1793. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1794. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1795. if (ICount > 10)
  1796. {
  1797. ICount = 10;
  1798. }
  1799. Menu->Items->Clear();
  1800. THistoryItemData Data;
  1801. Data.Side = Side;
  1802. for (short int i = 1; i <= ICount; i++)
  1803. {
  1804. TTBCustomItem * Item = new TTBXItem(Menu);
  1805. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1806. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1807. Item->Hint = DView->HistoryPath[Data.Index];
  1808. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1809. Item->Tag = *reinterpret_cast<int*>(&Data);
  1810. Item->OnClick = HistoryItemClick;
  1811. Menu->Items->Add(Item);
  1812. }
  1813. }
  1814. }
  1815. //---------------------------------------------------------------------------
  1816. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1817. TOperationSide Side, bool Back)
  1818. {
  1819. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1820. {
  1821. // workaround
  1822. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1823. // operator new allocates memory in Pascal code, but calls inline
  1824. // constructor in C++, leading in problems, the function does
  1825. // both in Pascal code
  1826. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1827. UpdateHistoryMenu(Side, Back);
  1828. }
  1829. return FHistoryMenu[Side == osLocal][Back];
  1830. }
  1831. //---------------------------------------------------------------------------
  1832. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1833. TCustomDirView *Sender)
  1834. {
  1835. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1836. UpdateHistoryMenu(Side, true);
  1837. UpdateHistoryMenu(Side, false);
  1838. }
  1839. //---------------------------------------------------------------------------
  1840. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1841. {
  1842. // remote custom commands can be executed only if the server supports shell commands
  1843. // or have secondary shell
  1844. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1845. }
  1846. //---------------------------------------------------------------------------
  1847. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1848. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1849. {
  1850. // -1 = hidden, 0 = disabled, 1 = enabled
  1851. int Result;
  1852. TFileCustomCommand RemoteCustomCommand;
  1853. TLocalCustomCommand LocalCustomCommand;
  1854. TFileCustomCommand * NonInteractiveCustomCommand =
  1855. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1856. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1857. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1858. if (FLAGCLEAR(Command.Params, ccLocal))
  1859. {
  1860. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1861. // custom command that does not operate with files can be executed anytime ...
  1862. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1863. {
  1864. // ... but do not show such command in remote file menu
  1865. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1866. {
  1867. Result = AllowedState;
  1868. }
  1869. else
  1870. {
  1871. Result = -1;
  1872. }
  1873. }
  1874. else
  1875. {
  1876. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1877. {
  1878. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1879. }
  1880. else
  1881. {
  1882. Result = -1;
  1883. }
  1884. }
  1885. }
  1886. else
  1887. {
  1888. // custom command that does not operate with files can be executed anytime
  1889. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1890. {
  1891. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1892. }
  1893. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1894. {
  1895. if ((ListType == ccltAll) || (ListType == ccltFile))
  1896. {
  1897. // special case is "diff"-style command that can be executed over any side,
  1898. // if we have both sides
  1899. Result =
  1900. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1901. // directly (instead of EnableSelectedOperation) to pass
  1902. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1903. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1904. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1905. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1906. }
  1907. else if (ListType == ccltBoth)
  1908. {
  1909. DebugAssert(!IsLocalBrowserMode());
  1910. Result = 1;
  1911. }
  1912. else
  1913. {
  1914. Result = -1;
  1915. }
  1916. }
  1917. else
  1918. {
  1919. if ((ListType == ccltAll) ||
  1920. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1921. {
  1922. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1923. }
  1924. else
  1925. {
  1926. Result = -1;
  1927. }
  1928. }
  1929. if ((Result > 0) &&
  1930. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1931. !HasActiveTerminal())
  1932. {
  1933. Result = 0;
  1934. }
  1935. }
  1936. return Result;
  1937. }
  1938. //---------------------------------------------------------------------------
  1939. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1940. TStrings * FileList, const TCustomCommandType & ACommand,
  1941. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1942. {
  1943. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1944. {
  1945. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1946. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1947. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1948. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1949. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1950. bool Capture =
  1951. FLAGSET(ACommand.Params, ccShowResults) ||
  1952. FLAGSET(ACommand.Params, ccCopyResults) ||
  1953. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1954. TCaptureOutputEvent OutputEvent = NULL;
  1955. DebugAssert(FCapturedLog == NULL);
  1956. if (Capture)
  1957. {
  1958. FCapturedLog = new TStringList();
  1959. OutputEvent = TerminalCaptureLog;
  1960. }
  1961. try
  1962. {
  1963. if (!RemoteCustomCommand.IsFileCommand(Command))
  1964. {
  1965. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1966. OutputEvent);
  1967. }
  1968. else
  1969. {
  1970. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1971. }
  1972. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1973. {
  1974. if (FLAGSET(ACommand.Params, ccCopyResults))
  1975. {
  1976. CopyToClipboard(FCapturedLog);
  1977. }
  1978. if (FLAGSET(ACommand.Params, ccShowResults))
  1979. {
  1980. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1981. }
  1982. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1983. {
  1984. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1985. }
  1986. }
  1987. }
  1988. __finally
  1989. {
  1990. SAFE_DESTROY(FCapturedLog);
  1991. }
  1992. }
  1993. }
  1994. //---------------------------------------------------------------------------
  1995. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1996. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1997. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1998. {
  1999. DebugAssert(!IsLocalBrowserMode());
  2000. TStrings * LocalFileList = NULL;
  2001. TStrings * RemoteFileList = NULL;
  2002. TStrings * RemoteFileListFull = NULL;
  2003. try
  2004. {
  2005. if (LocalFileCommand)
  2006. {
  2007. if (ALocalFileList == NULL)
  2008. {
  2009. DebugAssert(HasDirView[osLocal]);
  2010. // Cannot have focus on both panels, so we have to call AnyFileSelected
  2011. // directly (instead of EnableSelectedOperation) to pass
  2012. // false to FocusedFileOnlyWhenFocused
  2013. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  2014. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  2015. }
  2016. else
  2017. {
  2018. LocalFileList = ALocalFileList;
  2019. }
  2020. if (FileListCommand)
  2021. {
  2022. if (LocalFileList->Count != 1)
  2023. {
  2024. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  2025. }
  2026. }
  2027. else
  2028. {
  2029. if ((LocalFileList->Count != 1) &&
  2030. (FileList->Count != 1) &&
  2031. (LocalFileList->Count != FileList->Count))
  2032. {
  2033. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  2034. }
  2035. }
  2036. }
  2037. UnicodeString RootTempDir;
  2038. UnicodeString TempDir;
  2039. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  2040. if (!RemoteFiles)
  2041. {
  2042. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true, true);
  2043. }
  2044. try
  2045. {
  2046. TDateTimes RemoteFileTimes;
  2047. if (RemoteFiles)
  2048. {
  2049. RemoteFileList = FileList;
  2050. }
  2051. else
  2052. {
  2053. RemoteFileList = new TStringList();
  2054. TMakeLocalFileListParams MakeFileListParam;
  2055. MakeFileListParam.FileList = RemoteFileList;
  2056. MakeFileListParam.FileTimes = &RemoteFileTimes;
  2057. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2058. MakeFileListParam.Recursive =
  2059. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  2060. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2061. if (!MakeFileListParam.Recursive)
  2062. {
  2063. RemoteFileTimes.clear();
  2064. RemoteFileListFull = new TStringList();
  2065. MakeFileListParam.FileList = RemoteFileListFull;
  2066. MakeFileListParam.Recursive = true;
  2067. MakeFileListParam.IncludeDirs = false;
  2068. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2069. }
  2070. else
  2071. {
  2072. RemoteFileListFull = RemoteFileList;
  2073. }
  2074. }
  2075. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  2076. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2077. if (!NonBlocking)
  2078. {
  2079. int Count;
  2080. if (FileListCommand)
  2081. {
  2082. Count = 1;
  2083. }
  2084. else if (LocalFileCommand)
  2085. {
  2086. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  2087. }
  2088. else
  2089. {
  2090. Count = RemoteFileList->Count;
  2091. }
  2092. Progress.Start(foCustomCommand, osRemote, Count);
  2093. DebugAssert(FProgressForm != NULL);
  2094. FProgressForm->ReadOnly = true;
  2095. }
  2096. try
  2097. {
  2098. if (FileListCommand)
  2099. {
  2100. UnicodeString LocalFile;
  2101. // MakeFileList does not delimit filenames
  2102. UnicodeString FileList = MakeFileList(RemoteFileList);
  2103. if (LocalFileCommand)
  2104. {
  2105. DebugAssert(LocalFileList->Count == 1);
  2106. LocalFile = LocalFileList->Strings[0];
  2107. }
  2108. TLocalCustomCommand CustomCommand(Data,
  2109. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  2110. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  2111. if (NonBlocking)
  2112. {
  2113. DebugAssert(!POutput);
  2114. ExecuteShellChecked(ShellCommand);
  2115. }
  2116. else
  2117. {
  2118. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2119. }
  2120. }
  2121. else if (LocalFileCommand)
  2122. {
  2123. if (LocalFileList->Count == 1)
  2124. {
  2125. UnicodeString LocalFile = LocalFileList->Strings[0];
  2126. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2127. {
  2128. UnicodeString FileName = RemoteFileList->Strings[Index];
  2129. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2130. Progress.SetFile(FileNameForProgress);
  2131. TLocalCustomCommand CustomCommand(Data,
  2132. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2133. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2134. TOnceDoneOperation OnceDoneOperation;
  2135. // Do not unselect anything, as it won't work with two panels anyway
  2136. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2137. }
  2138. }
  2139. else if (RemoteFileList->Count == 1)
  2140. {
  2141. UnicodeString FileName = RemoteFileList->Strings[0];
  2142. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2143. {
  2144. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2145. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2146. Progress.SetFile(LocalFileNameOnly);
  2147. TLocalCustomCommand CustomCommand(
  2148. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2149. FileName, LocalFileName, L"");
  2150. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2151. TOnceDoneOperation OnceDoneOperation;
  2152. // Do not unselect anything, as it won't work with two panels anyway
  2153. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2154. }
  2155. }
  2156. else
  2157. {
  2158. if (LocalFileList->Count != RemoteFileList->Count)
  2159. {
  2160. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2161. }
  2162. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2163. {
  2164. UnicodeString FileName = RemoteFileList->Strings[Index];
  2165. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2166. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2167. Progress.SetFile(LocalFileNameOnly); // sic
  2168. TLocalCustomCommand CustomCommand(
  2169. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2170. FileName, LocalFileName, L"");
  2171. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2172. TOnceDoneOperation OnceDoneOperation;
  2173. // Do not unselect anything, as it won't work with two panels anyway
  2174. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2175. }
  2176. }
  2177. }
  2178. else
  2179. {
  2180. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2181. {
  2182. UnicodeString FileName = RemoteFileList->Strings[Index];
  2183. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2184. Progress.SetFile(FileNameForProgress);
  2185. TLocalCustomCommand CustomCommand(Data,
  2186. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2187. FileName, L"", L"");
  2188. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2189. TOnceDoneOperation OnceDoneOperation;
  2190. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2191. }
  2192. }
  2193. }
  2194. __finally
  2195. {
  2196. if (!NonBlocking)
  2197. {
  2198. Progress.Stop();
  2199. }
  2200. }
  2201. DebugAssert(!FAutoOperation);
  2202. if (!RemoteFiles)
  2203. {
  2204. TempDir = IncludeTrailingBackslash(TempDir);
  2205. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2206. {
  2207. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2208. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2209. // Skip directories, process only nested files.
  2210. // The check is redundant as FileAge fails for directories anyway.
  2211. !DirectoryExists(FileName))
  2212. {
  2213. UnicodeString RemoteDir =
  2214. UnixExtractFileDir(
  2215. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2216. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2217. TSearchRecSmart SearchRec;
  2218. if (FileSearchRec(FileName, SearchRec) &&
  2219. (SearchRec.GetLastWriteTime() != RemoteFileTimes[Index]))
  2220. {
  2221. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2222. TemporaryFileCopyParam(CopyParam);
  2223. CopyParam.FileMask = L"";
  2224. FAutoOperation = true;
  2225. FMoveToQueue = false;
  2226. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2227. TemporaryFilesList->Add(FileName);
  2228. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2229. }
  2230. }
  2231. }
  2232. }
  2233. }
  2234. __finally
  2235. {
  2236. FAutoOperation = false;
  2237. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2238. {
  2239. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  2240. }
  2241. }
  2242. }
  2243. __finally
  2244. {
  2245. if (RemoteFileList != FileList)
  2246. {
  2247. delete RemoteFileList;
  2248. }
  2249. if (LocalFileList != ALocalFileList)
  2250. {
  2251. delete LocalFileList;
  2252. }
  2253. if (RemoteFileListFull != RemoteFileList)
  2254. {
  2255. delete RemoteFileListFull;
  2256. }
  2257. }
  2258. }
  2259. //---------------------------------------------------------------------------
  2260. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2261. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2262. bool FileListCommand, UnicodeString * POutput)
  2263. {
  2264. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2265. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2266. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2267. {
  2268. UnicodeString FileName = SelectedFileList->Strings[Index];
  2269. if (DirectoryExists(FileName))
  2270. {
  2271. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2272. {
  2273. LocalFileList->Add(FileName);
  2274. }
  2275. if (FLAGSET(ACommand.Params, ccRecursive))
  2276. {
  2277. TMakeLocalFileListParams MakeFileListParam;
  2278. MakeFileListParam.FileList = LocalFileList.get();
  2279. MakeFileListParam.FileTimes = NULL;
  2280. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2281. MakeFileListParam.Recursive = true;
  2282. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2283. }
  2284. }
  2285. else
  2286. {
  2287. LocalFileList->Add(FileName);
  2288. }
  2289. }
  2290. UnicodeString RemotePath;
  2291. if (Terminal != NULL)
  2292. {
  2293. RemotePath = Terminal->CurrentDirectory;
  2294. }
  2295. if (FileListCommand)
  2296. {
  2297. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2298. TLocalCustomCommand CustomCommand(
  2299. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2300. L"", L"", FileList);
  2301. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2302. }
  2303. else
  2304. {
  2305. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2306. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2307. DebugAssert(FProgressForm != NULL);
  2308. FProgressForm->ReadOnly = true;
  2309. try
  2310. {
  2311. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2312. {
  2313. UnicodeString FileName = LocalFileList->Strings[Index];
  2314. Progress.SetFile(FileName);
  2315. TLocalCustomCommand CustomCommand(
  2316. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2317. FileName, L"", L"");
  2318. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2319. TOnceDoneOperation OnceDoneOperation;
  2320. Progress.Finish(FileName, true, OnceDoneOperation);
  2321. }
  2322. }
  2323. __finally
  2324. {
  2325. Progress.Stop();
  2326. }
  2327. }
  2328. }
  2329. //---------------------------------------------------------------------------
  2330. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2331. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2332. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2333. {
  2334. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2335. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2336. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2337. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2338. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2339. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2340. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2341. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2342. std::unique_ptr<UnicodeString> POutput;
  2343. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2344. FLAGSET(ACommand.Params, ccCopyResults))
  2345. {
  2346. POutput.reset(new UnicodeString());
  2347. }
  2348. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide, FCurrentSide);
  2349. if (!LocalCustomCommand.IsFileCommand(Command))
  2350. {
  2351. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2352. }
  2353. // remote files?
  2354. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2355. {
  2356. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2357. }
  2358. // local files
  2359. else
  2360. {
  2361. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2362. }
  2363. if (POutput.get() != NULL)
  2364. {
  2365. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2366. int P = POutput->Pos(sLineBreak);
  2367. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2368. {
  2369. POutput->SetLength(P - 1);
  2370. }
  2371. // Copy even empty output to clipboard
  2372. if (FLAGSET(ACommand.Params, ccCopyResults))
  2373. {
  2374. CopyToClipboard(*POutput);
  2375. }
  2376. // But do not show an empty message box.
  2377. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2378. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2379. !POutput->IsEmpty())
  2380. {
  2381. TClipboardHandler ClipboardHandler;
  2382. ClipboardHandler.Text = *POutput;
  2383. TMessageParams Params;
  2384. TQueryButtonAlias Aliases[1];
  2385. Aliases[0].Button = qaRetry;
  2386. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2387. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2388. Params.Aliases = Aliases;
  2389. Params.AliasesCount = LENOF(Aliases);
  2390. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2391. }
  2392. }
  2393. }
  2394. //---------------------------------------------------------------------------
  2395. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2396. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2397. {
  2398. TCustomCommandData Data;
  2399. UnicodeString Site;
  2400. UnicodeString RemotePath;
  2401. if (HasActiveTerminal())
  2402. {
  2403. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2404. Data = TCustomCommandData(SessionData.get());
  2405. Site = Terminal->SessionData->SessionKey;
  2406. RemotePath = Terminal->CurrentDirectory;
  2407. }
  2408. UnicodeString HelpKeyword = ACommand.HomePage;
  2409. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2410. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2411. {
  2412. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2413. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2414. {
  2415. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2416. }
  2417. else
  2418. {
  2419. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2420. }
  2421. if (!DoCustomCommandOptionsDialog(
  2422. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2423. {
  2424. Abort();
  2425. }
  2426. }
  2427. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2428. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2429. {
  2430. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2431. }
  2432. else
  2433. {
  2434. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2435. }
  2436. }
  2437. //---------------------------------------------------------------------------
  2438. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2439. const TCustomCommandType & Command)
  2440. {
  2441. DebugAssert(FCustomCommandLocalFileList != NULL);
  2442. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2443. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2444. TStrings * LocalFileList = new TStringList();
  2445. TStrings * RemoteFileList = new TStringList();
  2446. try
  2447. {
  2448. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2449. {
  2450. LocalFileList->Clear();
  2451. LocalFileList->AddObject(
  2452. FCustomCommandLocalFileList->Strings[Index],
  2453. FCustomCommandLocalFileList->Objects[Index]);
  2454. RemoteFileList->Clear();
  2455. RemoteFileList->AddObject(
  2456. FCustomCommandRemoteFileList->Strings[Index],
  2457. FCustomCommandRemoteFileList->Objects[Index]);
  2458. CustomCommand(RemoteFileList, Command, LocalFileList);
  2459. }
  2460. }
  2461. __finally
  2462. {
  2463. delete LocalFileList;
  2464. delete RemoteFileList;
  2465. }
  2466. }
  2467. //---------------------------------------------------------------------------
  2468. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2469. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2470. {
  2471. delete FCustomCommandLocalFileList;
  2472. delete FCustomCommandRemoteFileList;
  2473. // takeover ownership,
  2474. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2475. FCustomCommandLocalFileList = LocalFileList;
  2476. FCustomCommandRemoteFileList = RemoteFileList;
  2477. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2478. if (Button != NULL)
  2479. {
  2480. FCustomCommandMenu->Items->Clear();
  2481. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2482. MenuPopup(FCustomCommandMenu, Button);
  2483. }
  2484. else
  2485. {
  2486. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2487. }
  2488. }
  2489. //---------------------------------------------------------------------------
  2490. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2491. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2492. {
  2493. DebugAssert(FCapturedLog != NULL);
  2494. if ((OutputType == cotOutput) || (OutputType == cotError))
  2495. {
  2496. FCapturedLog->Add(AddedLine);
  2497. }
  2498. }
  2499. //---------------------------------------------------------------------------
  2500. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2501. TOperationSide Side)
  2502. {
  2503. return
  2504. (Side == osOther) &&
  2505. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2506. }
  2507. //---------------------------------------------------------------------------
  2508. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2509. TOperationSide Side, TTBXCustomItem * Item,
  2510. TResolvedDoubleClickAction DoubleClickAction1, TResolvedDoubleClickAction DoubleClickAction2)
  2511. {
  2512. TCustomDirView * DView = DirView(Side);
  2513. bool IsDefault = false;
  2514. if (DView->ItemFocused != NULL)
  2515. {
  2516. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  2517. TResolvedDoubleClickAction DefaultAction =
  2518. WinConfiguration->ResolveDoubleClickAction(DView->ItemIsDirectory(DView->ItemFocused), ATerminal);
  2519. IsDefault = (DefaultAction == DoubleClickAction1) || (DefaultAction == DoubleClickAction2);
  2520. }
  2521. TTBItemOptions O = Item->Options;
  2522. if (IsDefault)
  2523. {
  2524. Item->Options = O << tboDefault;
  2525. }
  2526. else
  2527. {
  2528. Item->Options = O >> tboDefault;
  2529. }
  2530. }
  2531. //---------------------------------------------------------------------------
  2532. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2533. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2534. {
  2535. TCustomDirView * DView = DirView(Side);
  2536. TListItem * Item = DView->ItemFocused;
  2537. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2538. EnableFocusedOperation[Side])
  2539. {
  2540. TPoint ClientPoint;
  2541. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2542. TPoint(0, 0) : MousePos;
  2543. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2544. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2545. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2546. }
  2547. }
  2548. //---------------------------------------------------------------------------
  2549. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2550. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2551. {
  2552. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, rdcaChangeDir, rdcaOpen);
  2553. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, rdcaEdit, rdcaNone);
  2554. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, rdcaCopy, rdcaNone);
  2555. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2556. Handled = true;
  2557. }
  2558. //---------------------------------------------------------------------------
  2559. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2560. {
  2561. }
  2562. //---------------------------------------------------------------------------
  2563. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2564. {
  2565. DebugAssert(FErrorList == NULL);
  2566. if (WinConfiguration->ContinueOnError)
  2567. {
  2568. FErrorList = new TStringList();
  2569. Configuration->Usage->Inc(L"ContinuationsOnError");
  2570. }
  2571. NonVisualDataModule->StartBusy();
  2572. }
  2573. //---------------------------------------------------------------------------
  2574. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2575. {
  2576. NonVisualDataModule->EndBusy();
  2577. if (FErrorList)
  2578. {
  2579. HandleErrorList(FErrorList);
  2580. }
  2581. }
  2582. //---------------------------------------------------------------------------
  2583. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2584. const TCopyParamType & CopyParam)
  2585. {
  2586. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2587. {
  2588. Configuration->Usage->Inc(L"FileMaskUses");
  2589. }
  2590. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2591. {
  2592. Configuration->Usage->Inc(L"OperationMaskUses");
  2593. }
  2594. }
  2595. //---------------------------------------------------------------------------
  2596. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2597. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2598. {
  2599. DebugAssert(!IsLocalBrowserMode());
  2600. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2601. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2602. TTransferOperationParam DefaultParam;
  2603. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2604. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2605. switch (Param.Queue)
  2606. {
  2607. case asOn:
  2608. CopyParam.Queue = true;
  2609. break;
  2610. case asOff:
  2611. CopyParam.Queue = false;
  2612. break;
  2613. case asAuto:
  2614. default:
  2615. // keep default
  2616. break;
  2617. }
  2618. bool Result =
  2619. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2620. if (Result)
  2621. {
  2622. DebugAssert(Terminal);
  2623. bool SelectionRestored = false;
  2624. TCustomDirView * DView = NULL;
  2625. if (HasDirView[Side])
  2626. {
  2627. DView = DirView(Side);
  2628. DView->SaveSelection();
  2629. DView->SaveSelectedNames();
  2630. }
  2631. UpdateCopyParamCounters(CopyParam);
  2632. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2633. DebugAssert(FTransferResumeList == NULL);
  2634. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2635. FMoveToQueue = false;
  2636. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2637. try
  2638. {
  2639. TStrings * PermanentFileList;
  2640. std::unique_ptr<TStrings> PermanentFileListOwner;
  2641. try
  2642. {
  2643. if (Side == osLocal)
  2644. {
  2645. PermanentFileList = FileList;
  2646. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2647. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2648. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2649. if (Operation == foMove)
  2650. {
  2651. ReloadLocalDirectory();
  2652. if (DView != NULL)
  2653. {
  2654. DView->RestoreSelection();
  2655. }
  2656. SelectionRestored = true;
  2657. }
  2658. }
  2659. else
  2660. {
  2661. // Clone the file list as it may refer to current directory files,
  2662. // which get destroyed, when the source directory is reloaded after foMove operation.
  2663. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2664. // But for a hotfix, we are not going to do such a big change.
  2665. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2666. PermanentFileList = PermanentFileListOwner.get();
  2667. try
  2668. {
  2669. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2670. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2671. }
  2672. __finally
  2673. {
  2674. if (Operation == foMove)
  2675. {
  2676. if (DView != NULL)
  2677. {
  2678. DView->RestoreSelection();
  2679. }
  2680. SelectionRestored = true;
  2681. }
  2682. ReloadLocalDirectory(Param.TargetDirectory);
  2683. }
  2684. }
  2685. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2686. if (FMoveToQueue)
  2687. {
  2688. Abort();
  2689. }
  2690. }
  2691. catch (EAbort &)
  2692. {
  2693. if (FMoveToQueue)
  2694. {
  2695. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2696. DebugAssert(CopyParam.TransferSkipList == NULL);
  2697. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2698. if (TransferResumeList->Count > 0)
  2699. {
  2700. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2701. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2702. }
  2703. CopyParam.TransferSkipList = TransferResumeList.release();
  2704. // not really needed, just to keep it consistent with TransferResumeList
  2705. FTransferResumeList = NULL;
  2706. FMoveToQueue = false;
  2707. Configuration->Usage->Inc("MovesToBackground");
  2708. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2709. ClearTransferSourceSelection(Direction);
  2710. }
  2711. throw;
  2712. }
  2713. }
  2714. __finally
  2715. {
  2716. if (!SelectionRestored && (DView != NULL))
  2717. {
  2718. DView->DiscardSavedSelection();
  2719. }
  2720. FTransferResumeList = NULL;
  2721. }
  2722. }
  2723. return Result;
  2724. }
  2725. //---------------------------------------------------------------------------
  2726. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2727. TOperationSide Side, TStrings * FileList, void * Param)
  2728. {
  2729. DebugAssert(FileList->Count);
  2730. // We deliberately do not toggle alternative flag (Param), but use OR,
  2731. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2732. // shortcut of CurrentDeleteAlternativeAction
  2733. bool Alternative =
  2734. bool(Param) || UseAlternativeFunction();
  2735. bool Recycle;
  2736. if (IsSideLocalBrowser(Side))
  2737. {
  2738. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2739. }
  2740. else
  2741. {
  2742. Recycle =
  2743. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2744. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2745. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2746. }
  2747. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2748. if (!Result)
  2749. {
  2750. UnicodeString Query;
  2751. if (FileList->Count == 1)
  2752. {
  2753. if (IsSideLocalBrowser(Side))
  2754. {
  2755. Query = ExtractFileName(FileList->Strings[0]);
  2756. }
  2757. else
  2758. {
  2759. Query = UnixExtractFileName(FileList->Strings[0]);
  2760. }
  2761. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2762. }
  2763. else
  2764. {
  2765. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2766. }
  2767. TMessageParams Params(mpNeverAskAgainCheck);
  2768. Params.ImageName = L"Delete file";
  2769. unsigned int Answer =
  2770. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2771. if (Answer == qaNeverAskAgain)
  2772. {
  2773. Result = true;
  2774. if (Recycle)
  2775. {
  2776. WinConfiguration->ConfirmRecycling = false;
  2777. }
  2778. else
  2779. {
  2780. WinConfiguration->ConfirmDeleting = false;
  2781. }
  2782. }
  2783. else
  2784. {
  2785. Result = (Answer == qaOK);
  2786. }
  2787. }
  2788. if (Result)
  2789. {
  2790. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2791. }
  2792. return Result;
  2793. }
  2794. //---------------------------------------------------------------------------
  2795. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2796. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2797. {
  2798. TAutoBatch AutoBatch(this);
  2799. bool Result;
  2800. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2801. {
  2802. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2803. }
  2804. else if (Operation == foRename)
  2805. {
  2806. DebugAssert(DirView(Side)->ItemFocused);
  2807. DirView(Side)->ItemFocused->EditCaption();
  2808. Result = true;
  2809. }
  2810. else if (Operation == foDelete)
  2811. {
  2812. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2813. }
  2814. else if (Operation == foSetProperties)
  2815. {
  2816. DirView(osRemote)->SaveSelectedNames();
  2817. Result = SetProperties(Side, FileList);
  2818. }
  2819. else if (Operation == foCustomCommand)
  2820. {
  2821. DebugAssert(Param);
  2822. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2823. DirView(osRemote)->SaveSelectedNames();
  2824. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2825. CustomCommand(FileList, *Command, NULL);
  2826. Result = true;
  2827. }
  2828. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2829. {
  2830. DebugAssert(!IsSideLocalBrowser(Side));
  2831. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2832. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2833. }
  2834. else if (Operation == foLock)
  2835. {
  2836. DebugAssert(!IsSideLocalBrowser(Side));
  2837. LockFiles(FileList, true);
  2838. Result = true;
  2839. }
  2840. else if (Operation == foUnlock)
  2841. {
  2842. DebugAssert(!IsSideLocalBrowser(Side));
  2843. LockFiles(FileList, false);
  2844. Result = true;
  2845. }
  2846. else
  2847. {
  2848. DebugFail();
  2849. }
  2850. return Result;
  2851. }
  2852. //---------------------------------------------------------------------------
  2853. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2854. {
  2855. if (Side == osCurrent)
  2856. {
  2857. Side = FCurrentSide;
  2858. }
  2859. return Side;
  2860. }
  2861. //---------------------------------------------------------------------------
  2862. TOperationSide TCustomScpExplorerForm::GetOtherSide(TOperationSide Side)
  2863. {
  2864. TOperationSide Result;
  2865. switch (GetSide(Side))
  2866. {
  2867. case osLocal:
  2868. Result = osOther;
  2869. break;
  2870. case osOther:
  2871. Result = osLocal;
  2872. break;
  2873. default:
  2874. DebugFail();
  2875. Abort();
  2876. }
  2877. return Result;
  2878. }
  2879. //---------------------------------------------------------------------------
  2880. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2881. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2882. {
  2883. Side = GetSide(Side);
  2884. bool FullPath = IsSideLocalBrowser(Side);
  2885. TCustomDirView * ADirView = DirView(Side);
  2886. std::unique_ptr<TStringList> FileList(new TStringList());
  2887. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2888. bool ReloadProperties =
  2889. !IsSideLocalBrowser(Side) && (Operation == foSetProperties) &&
  2890. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2891. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2892. if (Result && ReloadProperties)
  2893. {
  2894. std::unique_ptr<TStringList> FileList2(new TStringList());
  2895. FileList->CaseSensitive = true;
  2896. FileList->Sort();
  2897. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2898. {
  2899. TListItem * Item = ADirView->Items->Item[Index];
  2900. UnicodeString FileName = ADirView->ItemFileName(Item);
  2901. int Index2;
  2902. if (FileList->Find(FileName, Index2))
  2903. {
  2904. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2905. }
  2906. }
  2907. LoadFilesProperties(FileList2.get());
  2908. }
  2909. return Result;
  2910. }
  2911. //---------------------------------------------------------------------------
  2912. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2913. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2914. bool NoConfirmation, void * Param)
  2915. {
  2916. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2917. {
  2918. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2919. {
  2920. DebugAssert(!IsLocalBrowserMode());
  2921. if (GetSide(Side) == osLocal)
  2922. {
  2923. Configuration->Usage->Inc(L"UploadsCommand");
  2924. }
  2925. else
  2926. {
  2927. Configuration->Usage->Inc(L"DownloadsCommand");
  2928. }
  2929. }
  2930. }
  2931. }
  2932. //---------------------------------------------------------------------------
  2933. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2934. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2935. {
  2936. TTransferOperationParam Param;
  2937. if ((WinConfiguration->Interface != ifCommander) ||
  2938. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2939. {
  2940. Flags &= ~cocShortCutHint;
  2941. }
  2942. TCustomDirView * DView = DirView(Side);
  2943. Param.Options =
  2944. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2945. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2946. if (FLAGSET(Flags, cocQueue))
  2947. {
  2948. Param.Queue = asOn;
  2949. }
  2950. else if (FLAGSET(Flags, cocNonQueue))
  2951. {
  2952. Param.Queue = asOff;
  2953. }
  2954. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2955. }
  2956. //---------------------------------------------------------------------------
  2957. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2958. {
  2959. int Count = ErrorList->Count;
  2960. try
  2961. {
  2962. if (ErrorList->Count)
  2963. {
  2964. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2965. if (MessageDialog(Message, qtError,
  2966. qaOK | qaCancel, HELP_NONE) == qaOK)
  2967. {
  2968. unsigned int Answer;
  2969. int Index = 0;
  2970. do
  2971. {
  2972. DebugAssert(Index >= 0 && Index < Count);
  2973. TQueryButtonAlias Aliases[2];
  2974. Aliases[0].Button = qaYes;
  2975. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2976. Aliases[1].Button = qaNo;
  2977. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2978. TMessageParams Params;
  2979. Params.Aliases = Aliases;
  2980. Params.AliasesCount = LENOF(Aliases);
  2981. int No = Index + 1;
  2982. UnicodeString Message = ErrorList->Strings[Index];
  2983. UnicodeString AMainInstructions;
  2984. UnicodeString Details;
  2985. if (ExtractMainInstructions(Message, AMainInstructions))
  2986. {
  2987. Details = Message;
  2988. Message = AMainInstructions;
  2989. }
  2990. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2991. Answer = MoreMessageDialog(
  2992. Message,
  2993. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2994. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2995. qaOK, HELP_NONE, &Params);
  2996. if (Answer == qaNo)
  2997. {
  2998. Index++;
  2999. }
  3000. if (Answer == qaYes)
  3001. {
  3002. Index--;
  3003. }
  3004. }
  3005. while (Answer != qaOK);
  3006. }
  3007. }
  3008. }
  3009. __finally
  3010. {
  3011. TStrings * List = ErrorList;
  3012. ErrorList = NULL;
  3013. for (int i = 0; i < Count; i++)
  3014. {
  3015. delete List->Objects[i];
  3016. }
  3017. delete List;
  3018. }
  3019. }
  3020. //---------------------------------------------------------------------------
  3021. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  3022. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  3023. {
  3024. // needed for checking filemasks, as there's no directory object
  3025. // associated with the file object
  3026. File->FullFileName = FullFileName;
  3027. TFileMasks::TParams MaskParams;
  3028. MaskParams.Size = File->Size;
  3029. MaskParams.Modification = File->Modification;
  3030. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  3031. }
  3032. //---------------------------------------------------------------------------
  3033. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  3034. {
  3035. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3036. Side = GetSide(Side);
  3037. TCustomDirView * DView = DirView(Side);
  3038. TListItem * FocusedItem = DView->ItemFocused;
  3039. UnicodeString Name;
  3040. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  3041. {
  3042. Name = DView->ItemFileName(FocusedItem);
  3043. }
  3044. else
  3045. {
  3046. Name = LoadStr(NEW_FILE);
  3047. }
  3048. UnicodeString Names = Name;
  3049. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  3050. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  3051. HELP_EDIT_NEW, History.get(), true, NULL, true, 444))
  3052. {
  3053. while (!Names.IsEmpty())
  3054. {
  3055. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  3056. CustomWinConfiguration->History[L"EditFile"] = History.get();
  3057. UnicodeString TargetFileName;
  3058. UnicodeString LocalFileName;
  3059. UnicodeString RootTempDir;
  3060. UnicodeString TempDir;
  3061. UnicodeString RemoteDirectory;
  3062. bool ExistingFile = false;
  3063. if (!IsSideLocalBrowser(Side))
  3064. {
  3065. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  3066. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(Name));
  3067. if ((File.get() != NULL) &&
  3068. !File->IsDirectory)
  3069. {
  3070. ExecuteRemoteFile(Name, File.get(), efDefaultEditor);
  3071. ExistingFile = true;
  3072. }
  3073. File.reset(NULL);
  3074. if (!ExistingFile)
  3075. {
  3076. RemoteDirectory = UnixExtractFilePath(Name);
  3077. TemporaryDirectoryForRemoteFiles(
  3078. RemoteDirectory, GUIConfiguration->CurrentCopyParam, false, TempDir, RootTempDir);
  3079. TargetFileName = UnixExtractFileName(Name);
  3080. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3081. LocalFileName = TempDir +
  3082. // We probably do not want to trim the VMS version here
  3083. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  3084. }
  3085. }
  3086. else
  3087. {
  3088. if (ExtractFilePath(Name).IsEmpty())
  3089. {
  3090. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  3091. }
  3092. else
  3093. {
  3094. LocalFileName = ExpandFileName(Name);
  3095. }
  3096. TargetFileName = ExtractFileName(Name);
  3097. }
  3098. if (!ExistingFile)
  3099. {
  3100. bool NewFile = !FileExists(ApiPath(LocalFileName));
  3101. if (NewFile)
  3102. {
  3103. int File = FileCreate(ApiPath(LocalFileName));
  3104. if (File < 0)
  3105. {
  3106. if (!RootTempDir.IsEmpty())
  3107. {
  3108. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  3109. }
  3110. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  3111. }
  3112. else
  3113. {
  3114. FileClose(File);
  3115. }
  3116. }
  3117. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  3118. const TEditorData * ExternalEditor = NULL;
  3119. TFileMasks::TParams MaskParams; // size not known
  3120. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  3121. false, MaskParams);
  3122. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  3123. ExternalEditor, RootTempDir, RemoteDirectory, NewFile, TDateTime());
  3124. }
  3125. }
  3126. }
  3127. }
  3128. //---------------------------------------------------------------------------
  3129. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  3130. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  3131. {
  3132. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3133. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3134. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3135. return
  3136. ((ExecuteFileBy == efInternalEditor)) ||
  3137. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  3138. }
  3139. //---------------------------------------------------------------------------
  3140. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  3141. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  3142. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  3143. UnicodeString RemoteDirectory, bool NewFile, const TDateTime & SourceTimestamp)
  3144. {
  3145. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3146. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3147. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3148. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3149. Side = GetSide(Side);
  3150. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3151. if (!IsSideLocalBrowser(Side))
  3152. {
  3153. Data->Terminal = Terminal;
  3154. Data->SessionData = CloneCurrentSessionData();
  3155. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3156. Data->RemoteDirectory = RemoteDirectory;
  3157. Data->SessionName = Terminal->SessionData->SessionName;
  3158. Data->LocalRootDirectory = LocalRootDirectory;
  3159. Data->OriginalFileName = OriginalFileName;
  3160. Data->Command = L""; // will be changed later for external editor
  3161. // Empty SourceTimestamp indicates that checking was disabled when the file was opened
  3162. if (WinConfiguration->EditorCheckNotModified)
  3163. {
  3164. Data->SourceTimestamp = SourceTimestamp;
  3165. }
  3166. }
  3167. if (ExecuteFileBy == efInternalEditor)
  3168. {
  3169. if (!IsSideLocalBrowser(Side))
  3170. {
  3171. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3172. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3173. TForm * Editor;
  3174. try
  3175. {
  3176. Editor =
  3177. ShowEditorForm(
  3178. FileName, this,
  3179. FEditorManager->FileChanged, FEditorManager->FileReload, FEditorManager->FileClosed,
  3180. SaveAllInternalEditors, AnyInternalEditorModified,
  3181. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3182. }
  3183. catch(...)
  3184. {
  3185. if (!LocalRootDirectory.IsEmpty())
  3186. {
  3187. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory));
  3188. }
  3189. throw;
  3190. }
  3191. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3192. AppLogFmt(L"Opened internal editor with \"%s\"", (FileName));
  3193. }
  3194. else
  3195. {
  3196. DebugAssert(!StandaloneOperation);
  3197. TForm * Editor =
  3198. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3199. SaveAllInternalEditors, AnyInternalEditorModified,
  3200. L"", false, SessionColor, -1, NewFile);
  3201. FLocalEditors->Add(Editor);
  3202. }
  3203. }
  3204. else
  3205. {
  3206. HANDLE Process;
  3207. if (ExecuteFileBy == efExternalEditor)
  3208. {
  3209. UnicodeString Program, Params, Dir;
  3210. Data->Command = ExternalEditor->ExternalEditor;
  3211. ReformatFileNameCommand(Data->Command);
  3212. SplitCommand(Data->Command, Program, Params, Dir);
  3213. Params = ExpandFileNameCommand(Params, FileName);
  3214. Program = ExpandEnvironmentVariables(Program);
  3215. if (!ExecuteShell(Program, Params, Process))
  3216. {
  3217. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3218. }
  3219. }
  3220. else
  3221. {
  3222. DebugAssert(!IsSideLocalBrowser(Side));
  3223. if (!ExecuteShell(FileName, L"", Process))
  3224. {
  3225. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3226. }
  3227. }
  3228. if (IsSideLocalBrowser(Side) ||
  3229. ((ExecuteFileBy == efShell) &&
  3230. !WinConfiguration->Editor.SDIShellEditor) ||
  3231. ((ExecuteFileBy == efExternalEditor) &&
  3232. !ExternalEditor->SDIExternalEditor))
  3233. {
  3234. // no need for handle
  3235. if (Process != NULL)
  3236. {
  3237. DebugCheck(CloseHandle(Process));
  3238. }
  3239. Process = INVALID_HANDLE_VALUE;
  3240. }
  3241. else
  3242. {
  3243. if (Process == NULL)
  3244. {
  3245. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3246. }
  3247. }
  3248. if (!IsSideLocalBrowser(Side))
  3249. {
  3250. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3251. AppLogFmt(L"Opened external editor with remote file \"%s\".", (FileName));
  3252. }
  3253. }
  3254. }
  3255. //---------------------------------------------------------------------------
  3256. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3257. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3258. void * /*Arg*/)
  3259. {
  3260. if (Token != NULL)
  3261. {
  3262. EditorFormFileSave(static_cast<TForm *>(Token));
  3263. }
  3264. }
  3265. //---------------------------------------------------------------------------
  3266. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3267. {
  3268. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3269. {
  3270. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3271. }
  3272. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3273. }
  3274. //---------------------------------------------------------------------------
  3275. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3276. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3277. void * Arg)
  3278. {
  3279. if ((Token != NULL) &&
  3280. IsEditorFormModified(static_cast<TForm *>(Token)))
  3281. {
  3282. *static_cast<bool *>(Arg) = true;
  3283. }
  3284. }
  3285. //---------------------------------------------------------------------------
  3286. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3287. TObject * /*Sender*/, bool & Modified)
  3288. {
  3289. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3290. {
  3291. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3292. {
  3293. Modified = true;
  3294. }
  3295. }
  3296. if (!Modified)
  3297. {
  3298. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3299. }
  3300. }
  3301. //---------------------------------------------------------------------------
  3302. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3303. {
  3304. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3305. }
  3306. //---------------------------------------------------------------------------
  3307. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3308. const UnicodeString & RemoteDirectory, const TCopyParamType & CopyParam, bool Simple,
  3309. UnicodeString & Result, UnicodeString & RootDirectory)
  3310. {
  3311. if (!WinConfiguration->TemporaryDirectoryDeterministic || Simple)
  3312. {
  3313. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3314. Result = RootDirectory;
  3315. }
  3316. else
  3317. {
  3318. RootDirectory = L"";
  3319. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3320. Result = IncludeTrailingBackslash(Result);
  3321. }
  3322. if (WinConfiguration->TemporaryDirectoryAppendSession && !Simple)
  3323. {
  3324. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3325. }
  3326. if (WinConfiguration->TemporaryDirectoryAppendPath && !Simple)
  3327. {
  3328. UnicodeString ARemoteDirectory = RemoteDirectory;
  3329. if (!ARemoteDirectory.IsEmpty() && (ARemoteDirectory[1] == L'/'))
  3330. {
  3331. ARemoteDirectory.Delete(1, 1);
  3332. }
  3333. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(ARemoteDirectory)));
  3334. }
  3335. if (!ForceDirectories(ApiPath(Result)))
  3336. {
  3337. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3338. }
  3339. }
  3340. //---------------------------------------------------------------------------
  3341. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3342. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3343. bool GetTargetNames, bool AutoOperation, bool SimpleTempDir)
  3344. {
  3345. DebugAssert(!IsLocalBrowserMode());
  3346. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3347. if (ForceText)
  3348. {
  3349. CopyParam.TransferMode = tmAscii;
  3350. }
  3351. TemporaryFileCopyParam(CopyParam);
  3352. if (TempDir.IsEmpty())
  3353. {
  3354. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, SimpleTempDir, TempDir, RootTempDir);
  3355. }
  3356. DebugAssert(!FAutoOperation);
  3357. FAutoOperation = AutoOperation;
  3358. FMoveToQueue = false;
  3359. Terminal->ExceptionOnFail = true;
  3360. try
  3361. {
  3362. try
  3363. {
  3364. // turn off confirmations, as for MDI editors we may possibly download
  3365. // the same file over
  3366. Terminal->CopyToLocal(
  3367. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3368. if (GetTargetNames)
  3369. {
  3370. for (int i = 0; i < FileList->Count; i++)
  3371. {
  3372. FileList->Strings[i] =
  3373. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3374. }
  3375. }
  3376. }
  3377. catch(...)
  3378. {
  3379. if (!RootTempDir.IsEmpty())
  3380. {
  3381. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  3382. }
  3383. throw;
  3384. }
  3385. }
  3386. __finally
  3387. {
  3388. FAutoOperation = false;
  3389. Terminal->ExceptionOnFail = false;
  3390. }
  3391. }
  3392. //---------------------------------------------------------------------------
  3393. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3394. {
  3395. // Do not waste time checking for default editor list the next time,
  3396. // or testing if default editor is not notepad.
  3397. bool TryNextTime = false;
  3398. UnicodeString UsageState;
  3399. if (!WinConfiguration->EditorList->IsDefaultList())
  3400. {
  3401. UsageState = "H";
  3402. }
  3403. else
  3404. {
  3405. UnicodeString Executable;
  3406. UnicodeString ExecutableDescription;
  3407. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3408. {
  3409. UnicodeString Message =
  3410. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3411. unsigned int Answer =
  3412. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3413. if (Answer != qaOK)
  3414. {
  3415. UsageState = "R";
  3416. }
  3417. else
  3418. {
  3419. UsageState = "A";
  3420. TEditorData EditorData;
  3421. EditorData.Editor = edExternal;
  3422. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3423. EditorData.ExternalEditorOptionsAutodetect();
  3424. TEditorList EditorList;
  3425. EditorList = *WinConfiguration->EditorList;
  3426. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3427. WinConfiguration->EditorList = &EditorList;
  3428. }
  3429. }
  3430. }
  3431. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3432. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3433. }
  3434. //---------------------------------------------------------------------------
  3435. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3436. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3437. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3438. {
  3439. if (ExecuteFileBy == efDefaultEditor)
  3440. {
  3441. if (!WinConfiguration->OfferedEditorAutoConfig)
  3442. {
  3443. EditorAutoConfig();
  3444. }
  3445. const TEditorPreferences * Editor =
  3446. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3447. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3448. {
  3449. ExecuteFileBy = efInternalEditor;
  3450. ExternalEditor = NULL;
  3451. }
  3452. else if (Editor->Data->Editor == edOpen)
  3453. {
  3454. ExecuteFileBy = efShell;
  3455. ExternalEditor = NULL;
  3456. }
  3457. else
  3458. {
  3459. ExecuteFileBy = efExternalEditor;
  3460. ExternalEditor = Editor->Data;
  3461. }
  3462. }
  3463. }
  3464. //---------------------------------------------------------------------------
  3465. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3466. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3467. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3468. {
  3469. UnicodeString OriginalFileName;
  3470. UnicodeString LocalRootDirectory;
  3471. UnicodeString RemoteDirectory;
  3472. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3473. IsSideLocalBrowser(Side), MaskParams);
  3474. UnicodeString Counter;
  3475. UnicodeString LocalFileName;
  3476. bool Handled = false;
  3477. if (!IsSideLocalBrowser(Side))
  3478. {
  3479. // We need to trim VMS version here, so that we use name without version
  3480. // when uploading back to create a new version of the file
  3481. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3482. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3483. TObject * Token = NULL;
  3484. UnicodeString LocalDirectory;
  3485. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3486. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3487. LocalDirectory))
  3488. {
  3489. if (Token != NULL)
  3490. {
  3491. TForm * Form = dynamic_cast<TForm *>(Token);
  3492. if (Form->WindowState == wsMinimized)
  3493. {
  3494. ShowWindow(Form->Handle, SW_RESTORE);
  3495. }
  3496. else
  3497. {
  3498. Form->SetFocus();
  3499. }
  3500. Handled = true;
  3501. }
  3502. else
  3503. {
  3504. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3505. }
  3506. }
  3507. if (!Handled)
  3508. {
  3509. TStringList * FileList1 = new TStringList();
  3510. try
  3511. {
  3512. FileList1->AddObject(FullFileName, Object);
  3513. TemporarilyDownloadFiles(FileList1,
  3514. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3515. LocalRootDirectory, LocalDirectory, true, true, false);
  3516. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3517. }
  3518. __finally
  3519. {
  3520. delete FileList1;
  3521. }
  3522. switch (ExecuteFileBy)
  3523. {
  3524. case efShell:
  3525. Counter = "RemoteFilesExecuted";
  3526. break;
  3527. case efInternalEditor:
  3528. Counter = "RemoteFilesOpenedInInternalEditor";
  3529. break;
  3530. case efExternalEditor:
  3531. Counter = "RemoteFilesOpenedInExternalEditor";
  3532. break;
  3533. default:
  3534. DebugFail();
  3535. }
  3536. }
  3537. }
  3538. else
  3539. {
  3540. LocalFileName = FullFileName;
  3541. OriginalFileName = ExtractFileName(FullFileName);
  3542. switch (ExecuteFileBy)
  3543. {
  3544. case efShell:
  3545. Counter = "LocalFilesExecuted";
  3546. break;
  3547. case efInternalEditor:
  3548. Counter = "LocalFilesOpenedInInternalEditor";
  3549. break;
  3550. case efExternalEditor:
  3551. Counter = "LocalFilesOpenedInExternalEditor";
  3552. break;
  3553. default:
  3554. DebugFail();
  3555. }
  3556. }
  3557. if (!Handled)
  3558. {
  3559. Configuration->Usage->Inc(Counter);
  3560. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3561. ExternalEditor, LocalRootDirectory, RemoteDirectory, false, MaskParams.Modification);
  3562. }
  3563. }
  3564. //---------------------------------------------------------------------------
  3565. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3566. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3567. bool AllSelected, bool OnFocused)
  3568. {
  3569. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3570. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3571. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3572. Side = GetSide(Side);
  3573. TCustomDirView * DView = DirView(Side);
  3574. TStrings * FileList =
  3575. AllSelected ?
  3576. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3577. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3578. try
  3579. {
  3580. DebugAssert(AllSelected || (FileList->Count == 1));
  3581. for (int i = 0; i < FileList->Count; i++)
  3582. {
  3583. UnicodeString ListFileName = FileList->Strings[i];
  3584. UnicodeString FileNameOnly =
  3585. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3586. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3587. if (!DView->ItemIsDirectory(Item))
  3588. {
  3589. UnicodeString FullFileName;
  3590. if (!IsSideLocalBrowser(Side))
  3591. {
  3592. FullFileName = DirView(Side)->Path + ListFileName;
  3593. }
  3594. else
  3595. {
  3596. FullFileName = ListFileName;
  3597. }
  3598. TObject * Object = FileList->Objects[i];
  3599. TFileMasks::TParams MaskParams;
  3600. MaskParams.Size = DView->ItemFileSize(Item);
  3601. TDateTimePrecision Precision;
  3602. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3603. std::unique_ptr<TRemoteFile> File;
  3604. if (!IsSideLocalBrowser(Side))
  3605. {
  3606. File.reset(Terminal->TryReadFile(FullFileName));
  3607. if (File.get() != NULL)
  3608. {
  3609. File->FullFileName = FullFileName;
  3610. Object = File.get();
  3611. MaskParams.Size = File->Size;
  3612. MaskParams.Modification = File->Modification;
  3613. }
  3614. }
  3615. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName, Object, MaskParams);
  3616. }
  3617. }
  3618. }
  3619. __finally
  3620. {
  3621. delete FileList;
  3622. }
  3623. }
  3624. //---------------------------------------------------------------------------
  3625. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3626. {
  3627. CopyParam.FileNameCase = ncNoChange;
  3628. CopyParam.PreserveRights = false;
  3629. CopyParam.PreserveReadOnly = false;
  3630. CopyParam.ReplaceInvalidChars = true;
  3631. CopyParam.IncludeFileMask = TFileMasks();
  3632. CopyParam.NewerOnly = false;
  3633. CopyParam.FileMask = L"";
  3634. }
  3635. //---------------------------------------------------------------------------
  3636. bool TCustomScpExplorerForm::EditorCheckNotModified(const TEditedFileData * Data)
  3637. {
  3638. return WinConfiguration->EditorCheckNotModified && (Data->SourceTimestamp != TDateTime());
  3639. }
  3640. //---------------------------------------------------------------------------
  3641. class TEditorUploadQueueItem : public TUploadQueueItem
  3642. {
  3643. public:
  3644. __fastcall TEditorUploadQueueItem(
  3645. TTerminal * Terminal, TStrings * FilesToCopy, const UnicodeString & TargetDir,
  3646. const TCopyParamType * CopyParam, int Params) :
  3647. TUploadQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, false)
  3648. {
  3649. }
  3650. protected:
  3651. virtual void __fastcall DoTransferExecute(TTerminal * Terminal, TParallelOperation * ParallelOperation)
  3652. {
  3653. TUploadQueueItem::DoTransferExecute(Terminal, ParallelOperation);
  3654. TTerminalManager::Instance()->ScpExplorer->EditedFileUploaded(Terminal, CompleteEvent);
  3655. }
  3656. };
  3657. //---------------------------------------------------------------------------
  3658. void TCustomScpExplorerForm::EditedFileUploaded(TTerminal * ATerminal, HANDLE UploadCompleteEvent)
  3659. {
  3660. if (WinConfiguration->EditorCheckNotModified) // optimization
  3661. {
  3662. UnicodeString RemoteFilePath;
  3663. {
  3664. TGuard Guard(FEditorManager->Section);
  3665. TEditedFileData * Data = FEditorManager->FindByUploadCompleteEvent(UploadCompleteEvent);
  3666. if (DebugAlwaysTrue(Data != NULL) &&
  3667. EditorCheckNotModified(Data))
  3668. {
  3669. RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3670. }
  3671. }
  3672. if (!RemoteFilePath.IsEmpty())
  3673. {
  3674. std::unique_ptr<TRemoteFile> File(ATerminal->TryReadFile(RemoteFilePath));
  3675. if (File.get() != NULL)
  3676. {
  3677. TGuard Guard(FEditorManager->Section);
  3678. TEditedFileData * Data = FEditorManager->FindByUploadCompleteEvent(UploadCompleteEvent);
  3679. if (DebugAlwaysTrue(Data != NULL))
  3680. {
  3681. Data->SourceTimestamp = File->Modification;
  3682. AppLogFmt(L"Uploaded edited remote file timestamp: %s", (StandardTimestamp(Data->SourceTimestamp)));
  3683. }
  3684. }
  3685. }
  3686. }
  3687. }
  3688. //---------------------------------------------------------------------------
  3689. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
  3690. const UnicodeString & FileName, TEditedFileData * Data, HANDLE UploadCompleteEvent, bool & Retry)
  3691. {
  3692. TTerminalManager * Manager = TTerminalManager::Instance();
  3693. if ((Manager->ActiveTerminal == Data->Terminal) &&
  3694. Manager->ScheduleTerminalReconnnect(Data->Terminal))
  3695. {
  3696. AppLog(L"Scheduled session reconnect, will retry the upload later");
  3697. Retry = true;
  3698. }
  3699. else if (!IsActiveTerminal(Data->Terminal))
  3700. {
  3701. if (!NonVisualDataModule->Busy)
  3702. {
  3703. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3704. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3705. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3706. if (Data->Terminal == NULL)
  3707. {
  3708. TManagedTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3709. if (SameSiteTerminal != NULL)
  3710. {
  3711. UnicodeString Message =
  3712. FMTLOAD(EDIT_SESSION_REATTACH,
  3713. (FileNameOnly, Data->SessionName, FileNameOnly));
  3714. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3715. {
  3716. Data->Terminal = SameSiteTerminal;
  3717. Data->SessionName = SameSiteTerminal->SessionData->SessionName;
  3718. // We might also overwrite session data
  3719. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3720. }
  3721. else
  3722. {
  3723. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3724. Abort();
  3725. }
  3726. }
  3727. }
  3728. // Foreground session should reconnect itself (except for those disconnected explicitly by the user)
  3729. else if (DebugAlwaysTrue(Terminal->Disconnected || (Terminal != Data->Terminal)))
  3730. {
  3731. UnicodeString Message =
  3732. MainInstructions(
  3733. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3734. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3735. {
  3736. if (Terminal->Disconnected)
  3737. {
  3738. Manager->ActiveSession = Terminal;
  3739. ReconnectSession();
  3740. // Was it reconnected? (if not, Data->Terminal should be nulled now)
  3741. if (!IsActiveTerminal(Data->Terminal) || (Terminal != Data->Terminal))
  3742. {
  3743. Abort();
  3744. }
  3745. }
  3746. else
  3747. {
  3748. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3749. }
  3750. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3751. }
  3752. else
  3753. {
  3754. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3755. Abort();
  3756. }
  3757. }
  3758. }
  3759. if (!IsActiveTerminal(Data->Terminal))
  3760. {
  3761. Configuration->Usage->Inc(L"EditInactiveSession");
  3762. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3763. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3764. (ExtractFileName(FileName), Data->SessionName)));
  3765. }
  3766. }
  3767. if (!Retry && EditorCheckNotModified(Data))
  3768. {
  3769. if (NonVisualDataModule->Busy)
  3770. {
  3771. Retry = true;
  3772. }
  3773. else
  3774. {
  3775. UnicodeString RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3776. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(RemoteFilePath));
  3777. if (File.get() != NULL)
  3778. {
  3779. AppLogFmt(L"Edited remote file timestamp: %s, Original timestamp: %s", (StandardTimestamp(File->Modification), StandardTimestamp(Data->SourceTimestamp)));
  3780. if (File->Modification != Data->SourceTimestamp)
  3781. {
  3782. UnicodeString Message = MainInstructions(LoadStr(EDIT_CHANGED_EXTERNALLY));
  3783. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) != qaOK)
  3784. {
  3785. Abort();
  3786. }
  3787. }
  3788. }
  3789. }
  3790. }
  3791. if (!Retry)
  3792. {
  3793. TStrings * FileList = new TStringList();
  3794. try
  3795. {
  3796. FileList->Add(FileName);
  3797. // Consider using the same settings (preset) as when the file was downloaded.
  3798. // More over this does not reflect the actual session that will do the upload.
  3799. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3800. TemporaryFileCopyParam(CopyParam);
  3801. if (Data->ForceText)
  3802. {
  3803. CopyParam.TransferMode = tmAscii;
  3804. }
  3805. // so i do not need to worry if masking algorithm works in all cases
  3806. // ("" means "copy file name", no masking is actually done)
  3807. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3808. {
  3809. CopyParam.FileMask = L"";
  3810. }
  3811. else
  3812. {
  3813. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3814. }
  3815. int Params = cpNoConfirmation | cpTemporary;
  3816. if (Data->Terminal->IsCapable[fcBackgroundTransfers])
  3817. {
  3818. TTerminalQueue * AQueue = Manager->FindQueueForTerminal(Data->Terminal);
  3819. TQueueItem * QueueItem =
  3820. new TEditorUploadQueueItem(Data->Terminal, FileList, Data->RemoteDirectory, &CopyParam, Params);
  3821. QueueItem->CompleteEvent = UploadCompleteEvent;
  3822. AddQueueItem(AQueue, QueueItem, Data->Terminal);
  3823. }
  3824. else
  3825. {
  3826. if (NonVisualDataModule->Busy)
  3827. {
  3828. Retry = true;
  3829. }
  3830. else
  3831. {
  3832. FMoveToQueue = false;
  3833. Data->Terminal->CopyToRemote(FileList, Data->RemoteDirectory, &CopyParam, Params, NULL);
  3834. SetEvent(UploadCompleteEvent);
  3835. }
  3836. }
  3837. }
  3838. __finally
  3839. {
  3840. delete FileList;
  3841. }
  3842. }
  3843. }
  3844. //---------------------------------------------------------------------------
  3845. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3846. const UnicodeString & FileName, TEditedFileData * Data)
  3847. {
  3848. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3849. DebugAssert(!NonVisualDataModule->Busy);
  3850. if (!IsActiveTerminal(Data->Terminal))
  3851. {
  3852. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3853. (ExtractFileName(FileName), Data->SessionName)));
  3854. }
  3855. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3856. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3857. NonVisualDataModule->StartBusy();
  3858. try
  3859. {
  3860. std::unique_ptr<TRemoteFile> File;
  3861. UnicodeString RemoteFileName =
  3862. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3863. Terminal->ExceptionOnFail = true;
  3864. try
  3865. {
  3866. File.reset(Terminal->ReadFile(RemoteFileName));
  3867. if (!File->HaveFullFileName)
  3868. {
  3869. File->FullFileName = RemoteFileName;
  3870. }
  3871. }
  3872. __finally
  3873. {
  3874. Terminal->ExceptionOnFail = false;
  3875. }
  3876. std::unique_ptr<TStrings> FileList(new TStringList());
  3877. FileList->AddObject(RemoteFileName, File.get());
  3878. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3879. UnicodeString TempDir = ExtractFilePath(FileName);
  3880. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, true, false);
  3881. if (EditorCheckNotModified(Data))
  3882. {
  3883. Data->SourceTimestamp = File->Modification;
  3884. }
  3885. // sanity check, the target file name should be still the same
  3886. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3887. }
  3888. __finally
  3889. {
  3890. NonVisualDataModule->EndBusy();
  3891. // it actually may not exist anymore...
  3892. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3893. }
  3894. }
  3895. //---------------------------------------------------------------------------
  3896. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3897. const TEditedFileData * Data, bool & KeepOpen)
  3898. {
  3899. // Command is set for external editors only (not for "shell" open).
  3900. if (!Data->Command.IsEmpty())
  3901. {
  3902. bool AnyFound = false;
  3903. bool AnyMDI = false;
  3904. bool AnyNonMDI = false;
  3905. bool AnyDetect = false;
  3906. TEditorList * EditorList = new TEditorList();
  3907. try
  3908. {
  3909. *EditorList = *WinConfiguration->EditorList;
  3910. for (int i = 0; i < EditorList->Count; i++)
  3911. {
  3912. const TEditorPreferences * Editor = EditorList->Editors[i];
  3913. if ((Editor->Data->Editor == edExternal) &&
  3914. (Editor->Data->ExternalEditor == Data->Command))
  3915. {
  3916. AnyFound = true;
  3917. if (Editor->Data->SDIExternalEditor)
  3918. {
  3919. AnyNonMDI = true;
  3920. if (Editor->Data->DetectMDIExternalEditor)
  3921. {
  3922. AnyDetect = true;
  3923. }
  3924. }
  3925. else
  3926. {
  3927. AnyMDI = true;
  3928. }
  3929. }
  3930. }
  3931. bool EnableMDI = false;
  3932. bool DisableDetect = false;
  3933. if (AnyMDI)
  3934. {
  3935. KeepOpen = true;
  3936. if (AnyNonMDI)
  3937. {
  3938. // there is at least one instance of the editor with MDI support enabled,
  3939. // and one with disabled, enable it for all instances
  3940. EnableMDI = true;
  3941. }
  3942. }
  3943. else if (AnyFound && !AnyDetect)
  3944. {
  3945. // at least once instance found but all have MDI autodetection disabled
  3946. // => close the file (default action)
  3947. }
  3948. else
  3949. {
  3950. // no instance of the editor has MDI support enabled
  3951. TMessageParams Params;
  3952. if (AnyFound)
  3953. {
  3954. // there is at least one instance of the editor
  3955. Params.Params |= mpNeverAskAgainCheck;
  3956. }
  3957. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3958. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3959. switch (Answer)
  3960. {
  3961. case qaNeverAskAgain:
  3962. DisableDetect = true;
  3963. break;
  3964. case qaNo:
  3965. EnableMDI = true;
  3966. KeepOpen = true;
  3967. break;
  3968. }
  3969. }
  3970. if (AnyFound && (EnableMDI || DisableDetect))
  3971. {
  3972. bool Changed = false;
  3973. for (int i = 0; i < EditorList->Count; i++)
  3974. {
  3975. const TEditorPreferences * Editor = EditorList->Editors[i];
  3976. if ((Editor->Data->Editor == edExternal) &&
  3977. (Editor->Data->ExternalEditor == Data->Command) &&
  3978. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3979. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3980. {
  3981. Changed = true;
  3982. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3983. if (EnableMDI)
  3984. {
  3985. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3986. }
  3987. if (DisableDetect)
  3988. {
  3989. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3990. }
  3991. EditorList->Change(i, UpdatedEditor);
  3992. }
  3993. }
  3994. if (Changed)
  3995. {
  3996. WinConfiguration->EditorList = EditorList;
  3997. }
  3998. }
  3999. }
  4000. __finally
  4001. {
  4002. delete EditorList;
  4003. }
  4004. }
  4005. else
  4006. {
  4007. // "open" case
  4008. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  4009. qaOK, HELP_APP_EARLY_CLOSED);
  4010. }
  4011. }
  4012. //---------------------------------------------------------------------------
  4013. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  4014. {
  4015. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  4016. }
  4017. //---------------------------------------------------------------------------
  4018. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  4019. {
  4020. SideEnter(osRemote);
  4021. }
  4022. //---------------------------------------------------------------------------
  4023. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  4024. {
  4025. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  4026. SideEnter(osRemote);
  4027. }
  4028. //---------------------------------------------------------------------------
  4029. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  4030. {
  4031. FCurrentSide = Side;
  4032. if (Visible)
  4033. {
  4034. UpdateControls();
  4035. }
  4036. }
  4037. //---------------------------------------------------------------------------
  4038. void __fastcall TCustomScpExplorerForm::FileDeleted(
  4039. TOperationSide Side, const UnicodeString & FileName, bool Success, bool NotCancelled)
  4040. {
  4041. VisualiseOperationFinished(Side, FileName, Success && NotCancelled);
  4042. if (DebugAlwaysTrue(FDeletedFiles != NULL) && Success)
  4043. {
  4044. FDeletedFiles->Add(FileName);
  4045. }
  4046. }
  4047. //---------------------------------------------------------------------------
  4048. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  4049. TStrings * FileList, bool Alternative)
  4050. {
  4051. TCustomDirView * DView = DirView(Side);
  4052. DView->SaveSelection();
  4053. DView->SaveSelectedNames();
  4054. DebugAssert(!FAlternativeDelete);
  4055. FAlternativeDelete = Alternative;
  4056. try
  4057. {
  4058. int Params = FLAGMASK(Alternative, dfAlternative);
  4059. bool Remote = !IsSideLocalBrowser(Side);
  4060. std::unique_ptr<TStringList> DeletedFiles;
  4061. std::unique_ptr<TStrings> PermanentFileList;
  4062. bool AllowMoveToQueue = (Terminal != NULL);
  4063. if (AllowMoveToQueue)
  4064. {
  4065. DeletedFiles.reset(new TStringList());
  4066. DeletedFiles->CaseSensitive = Remote;
  4067. }
  4068. try
  4069. {
  4070. DebugAssert(FDeletedFiles == NULL);
  4071. TValueRestorer<TStrings *> DeletedFilesRestorer(FDeletedFiles, DeletedFiles.get());
  4072. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  4073. if (AllowMoveToQueue)
  4074. {
  4075. FOnFileOperationFinished = FileDeleted;
  4076. }
  4077. FMoveToQueue = false;
  4078. if (Remote)
  4079. {
  4080. DebugAssert(AllowMoveToQueue);
  4081. // Clone the file list as it may refer to current directory files, which get destroyed, when the directory is reloaded after the operation
  4082. PermanentFileList.reset(TRemoteFileList::CloneStrings(FileList));
  4083. DebugAssert(Terminal != NULL);
  4084. Terminal->DeleteFiles(FileList, Params);
  4085. }
  4086. else
  4087. {
  4088. if (AllowMoveToQueue) // optimization)
  4089. {
  4090. PermanentFileList.reset(CloneStrings(FileList));
  4091. }
  4092. if (IsLocalBrowserMode())
  4093. {
  4094. Configuration->Usage->Inc(L"LocalLocalDeletes");
  4095. }
  4096. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide, FCurrentSide);
  4097. try
  4098. {
  4099. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, Params);
  4100. }
  4101. __finally
  4102. {
  4103. ReloadLocalDirectory();
  4104. }
  4105. }
  4106. // Probably not needed for deleting, just for consistency with transfer code
  4107. if (FMoveToQueue)
  4108. {
  4109. Abort();
  4110. }
  4111. }
  4112. catch (EAbort &)
  4113. {
  4114. if (FMoveToQueue && DebugAlwaysTrue(AllowMoveToQueue) && DebugAlwaysTrue(Terminal != NULL))
  4115. {
  4116. FMoveToQueue = false;
  4117. DeletedFiles->Sorted = true;
  4118. for (int Index = 0; Index < PermanentFileList->Count; Index++)
  4119. {
  4120. if (DeletedFiles->IndexOf(PermanentFileList->Strings[Index]) >= 0)
  4121. {
  4122. // We should always be deleting the first item => what can be used to optimize this code, if needed
  4123. DebugAssert(Index == 0);
  4124. PermanentFileList->Delete(Index);
  4125. Index--;
  4126. }
  4127. }
  4128. if (Remote)
  4129. {
  4130. Configuration->Usage->Inc("MovesToBackgroundDelete");
  4131. TQueueItem * QueueItem = new TRemoteDeleteQueueItem(Terminal, PermanentFileList.get(), Params);
  4132. AddQueueItem(Queue, QueueItem, Terminal);
  4133. }
  4134. else
  4135. {
  4136. Configuration->Usage->Inc("MovesToBackgroundDeleteLocal");
  4137. TQueueItem * QueueItem = new TLocalDeleteQueueItem(PermanentFileList.get(), Params);
  4138. AddQueueItem(Queue, QueueItem, Terminal);
  4139. }
  4140. ClearOperationSelection(Side);
  4141. }
  4142. }
  4143. FAlternativeDelete = false;
  4144. }
  4145. catch(...)
  4146. {
  4147. FAlternativeDelete = false;
  4148. DView->DiscardSavedSelection();
  4149. throw;
  4150. }
  4151. DView->RestoreSelection();
  4152. }
  4153. //---------------------------------------------------------------------------
  4154. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  4155. {
  4156. DebugAssert(Terminal);
  4157. DebugAssert(!IsLocalBrowserMode());
  4158. RemoteDirView->SaveSelection();
  4159. RemoteDirView->SaveSelectedNames();
  4160. try
  4161. {
  4162. if (Lock)
  4163. {
  4164. Terminal->LockFiles(FileList);
  4165. }
  4166. else
  4167. {
  4168. Terminal->UnlockFiles(FileList);
  4169. }
  4170. }
  4171. catch(...)
  4172. {
  4173. RemoteDirView->DiscardSavedSelection();
  4174. throw;
  4175. }
  4176. RemoteDirView->RestoreSelection();
  4177. }
  4178. //---------------------------------------------------------------------------
  4179. bool TCustomScpExplorerForm::DoDirectoryExists(void * Session, const UnicodeString & Directory)
  4180. {
  4181. TTerminal * ATerminal = (Session == NULL) ? Terminal : reinterpret_cast<TTerminal *>(Session);
  4182. bool Result =
  4183. IsActiveTerminal(ATerminal) &&
  4184. ATerminal->DirectoryExists(Directory);
  4185. return Result;
  4186. }
  4187. //---------------------------------------------------------------------------
  4188. bool TCustomScpExplorerForm::NeedSecondarySessionForRemoteCopy(TStrings * FileList)
  4189. {
  4190. bool CopyDirsOnSecondarySession = Terminal->IsCapable[fcSecondaryShell];
  4191. return
  4192. !Terminal->IsCapable[fcRemoteCopy] ||
  4193. (CopyDirsOnSecondarySession && TRemoteFileList::AnyDirectory(FileList));
  4194. }
  4195. //---------------------------------------------------------------------------
  4196. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  4197. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  4198. bool NoConfirmation, bool Move)
  4199. {
  4200. DebugAssert(Terminal != NULL);
  4201. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  4202. DebugAssert(!IsLocalBrowserMode());
  4203. if (Session == NULL)
  4204. {
  4205. Session = Terminal;
  4206. }
  4207. bool TargetConfirmed = false;
  4208. if (Session == Terminal)
  4209. {
  4210. if (RemoteDriveView->DropTarget != NULL)
  4211. {
  4212. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  4213. TargetConfirmed = true;
  4214. }
  4215. else if (RemoteDirView->DropTarget != NULL)
  4216. {
  4217. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  4218. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  4219. TargetConfirmed = true;
  4220. }
  4221. else
  4222. {
  4223. Target = RemoteDirView->Path;
  4224. }
  4225. }
  4226. else
  4227. {
  4228. Target = GetSessionPath(Session, osRemote);
  4229. }
  4230. Target = UnixIncludeTrailingBackslash(Target);
  4231. if (FileList->Count == 1)
  4232. {
  4233. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  4234. }
  4235. else
  4236. {
  4237. FileMask = AnyMask;
  4238. }
  4239. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  4240. bool Result = true;
  4241. if (!NoConfirmation)
  4242. {
  4243. bool Multi = (FileList->Count > 1);
  4244. if (Move)
  4245. {
  4246. Result = DoRemoteMoveDialog(Multi, Target, FileMask, DoDirectoryExists);
  4247. }
  4248. else
  4249. {
  4250. std::unique_ptr<TStrings> Sessions(new TStringList);
  4251. std::unique_ptr<TStrings> Directories(new TStringList);
  4252. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  4253. for (int Index = 0; Index < SessionList->Count; Index++)
  4254. {
  4255. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  4256. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  4257. {
  4258. Sessions->AddObject(SessionList->Strings[Index], ASession);
  4259. Directories->Add(GetSessionPath(ASession, osRemote));
  4260. }
  4261. }
  4262. bool AnyDirectory = false;
  4263. bool CopyDirsOnSecondarySession = Terminal->IsCapable[fcSecondaryShell];
  4264. if (CopyDirsOnSecondarySession && !Terminal->CommandSessionOpened) // optimization
  4265. {
  4266. for (int Index = 0; !AnyDirectory && (Index < FileList->Count); Index++)
  4267. {
  4268. AnyDirectory = DebugNotNull(dynamic_cast<TRemoteFile *>(FileList->Objects[Index]))->IsDirectory;
  4269. }
  4270. }
  4271. TDirectRemoteCopy AllowDirectCopy;
  4272. if (Terminal->CommandSessionOpened || !NeedSecondarySessionForRemoteCopy(FileList))
  4273. {
  4274. DebugAssert(DirectCopy);
  4275. AllowDirectCopy = drcAllow;
  4276. }
  4277. else if (Terminal->IsCapable[fcSecondaryShell])
  4278. {
  4279. DebugAssert(DirectCopy);
  4280. AllowDirectCopy = Terminal->IsCapable[fcRemoteCopy] ? drcConfirmCommandSessionDirs : drcConfirmCommandSession;
  4281. }
  4282. else
  4283. {
  4284. DebugAssert(!DirectCopy);
  4285. AllowDirectCopy = drcDisallow;
  4286. }
  4287. void * ASession = Session;
  4288. Result = DoRemoteCopyDialog(
  4289. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal,
  4290. DoDirectoryExists, TargetConfirmed);
  4291. Session = static_cast<TManagedTerminal *>(ASession);
  4292. }
  4293. }
  4294. return Result;
  4295. }
  4296. //---------------------------------------------------------------------------
  4297. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  4298. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  4299. {
  4300. DebugAssert(!IsLocalBrowserMode());
  4301. bool DirectCopy;
  4302. UnicodeString Target, FileMask;
  4303. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  4304. if (Result)
  4305. {
  4306. if (!Move && !DirectCopy)
  4307. {
  4308. Configuration->Usage->Inc("RemoteCopyTemp");
  4309. UnicodeString RootTempDir;
  4310. UnicodeString TempDir;
  4311. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  4312. TStrings * TemporaryFilesList = new TStringList();
  4313. try
  4314. {
  4315. TMakeLocalFileListParams MakeFileListParam;
  4316. MakeFileListParam.FileList = TemporaryFilesList;
  4317. MakeFileListParam.FileTimes = NULL;
  4318. MakeFileListParam.IncludeDirs = true;
  4319. MakeFileListParam.Recursive = false;
  4320. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  4321. TTerminalManager::Instance()->ActiveSession = Session;
  4322. if (TemporaryFilesList->Count > 0)
  4323. {
  4324. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4325. CopyParam.FileMask = FileMask;
  4326. DebugAssert(!FAutoOperation);
  4327. FAutoOperation = true;
  4328. FMoveToQueue = false;
  4329. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  4330. }
  4331. }
  4332. __finally
  4333. {
  4334. delete TemporaryFilesList;
  4335. FAutoOperation = false;
  4336. if (!RootTempDir.IsEmpty())
  4337. {
  4338. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  4339. }
  4340. }
  4341. }
  4342. else
  4343. {
  4344. RemoteDirView->SaveSelection();
  4345. RemoteDirView->SaveSelectedNames();
  4346. try
  4347. {
  4348. if (Move)
  4349. {
  4350. Configuration->Usage->Inc("RemoteMove");
  4351. Terminal->MoveFiles(FileList, Target, FileMask, false);
  4352. }
  4353. else
  4354. {
  4355. Configuration->Usage->Inc("RemoteCopyDirect");
  4356. DebugAssert(DirectCopy);
  4357. DebugAssert(Session == Terminal);
  4358. if (!NeedSecondarySessionForRemoteCopy(FileList) ||
  4359. Terminal->CommandSessionOpened ||
  4360. CommandSessionFallback())
  4361. {
  4362. Terminal->CopyFiles(FileList, Target, FileMask, false);
  4363. }
  4364. }
  4365. }
  4366. catch(...)
  4367. {
  4368. RemoteDirView->DiscardSavedSelection();
  4369. throw;
  4370. }
  4371. RemoteDirView->RestoreSelection();
  4372. }
  4373. }
  4374. return Result;
  4375. }
  4376. //---------------------------------------------------------------------------
  4377. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4378. const UnicodeString & Path, TRemoteProperties & Properties)
  4379. {
  4380. DebugAssert(!IsLocalBrowserMode());
  4381. Properties.Valid = Properties.Valid << vpEncrypt;
  4382. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4383. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4384. }
  4385. //---------------------------------------------------------------------------
  4386. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4387. {
  4388. Side = GetSide(Side);
  4389. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4390. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4391. UnicodeString Name = LoadStr(NEW_FOLDER);
  4392. int AllowedChanges =
  4393. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4394. bool SaveSettings = false;
  4395. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4396. {
  4397. TWindowLock Lock(this);
  4398. if (!IsSideLocalBrowser(Side))
  4399. {
  4400. if (SaveSettings)
  4401. {
  4402. GUIConfiguration->NewDirectoryProperties = Properties;
  4403. }
  4404. CreateRemoteDirectory(Name, Properties);
  4405. }
  4406. else
  4407. {
  4408. DirView(Side)->CreateDirectory(Name);
  4409. }
  4410. }
  4411. }
  4412. //---------------------------------------------------------------------------
  4413. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4414. {
  4415. TWindowLock Lock(this);
  4416. DirView(Side)->ExecuteHomeDirectory();
  4417. }
  4418. //---------------------------------------------------------------------------
  4419. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4420. {
  4421. DoOpenDirectoryDialog(odBrowse, Side);
  4422. }
  4423. //---------------------------------------------------------------------------
  4424. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4425. {
  4426. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4427. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4428. bool Result = !Path.IsEmpty();
  4429. if (Result)
  4430. {
  4431. TryOpenDirectory(Side, Path);
  4432. }
  4433. return Result;
  4434. }
  4435. //---------------------------------------------------------------------------
  4436. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4437. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4438. bool & Close)
  4439. {
  4440. // terminal can be already closed (e.g. dropped connection)
  4441. if (Terminal != NULL)
  4442. {
  4443. try
  4444. {
  4445. TCalculateSizeParams Params;
  4446. Params.Stats = &Stats;
  4447. Terminal->CalculateFilesSize(FileList, Size, Params);
  4448. }
  4449. catch(...)
  4450. {
  4451. if (!Terminal->Active)
  4452. {
  4453. Close = true;
  4454. }
  4455. throw;
  4456. }
  4457. }
  4458. }
  4459. //---------------------------------------------------------------------------
  4460. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4461. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4462. bool & Close)
  4463. {
  4464. if ((Terminal != NULL) && // terminal can be already closed (e.g. dropped connection)
  4465. EnsureCommandSessionFallback(fcCalculatingChecksum))
  4466. {
  4467. Configuration->Usage->Inc(L"ChecksumCalculated");
  4468. try
  4469. {
  4470. Terminal->CalculateFilesChecksum(Alg, FileList, OnCalculatedChecksum);
  4471. }
  4472. catch(...)
  4473. {
  4474. if (!Terminal->Active)
  4475. {
  4476. Close = true;
  4477. }
  4478. throw;
  4479. }
  4480. }
  4481. }
  4482. //---------------------------------------------------------------------------
  4483. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4484. {
  4485. if (Terminal->LoadFilesProperties(FileList))
  4486. {
  4487. RemoteDirView->Invalidate();
  4488. }
  4489. }
  4490. //---------------------------------------------------------------------------
  4491. bool TCustomScpExplorerForm::CanCalculateChecksum()
  4492. {
  4493. return
  4494. Terminal->IsCapable[fcCalculatingChecksum] ||
  4495. (Terminal->IsCapable[fcSecondaryShell] && !Terminal->IsEncryptingFiles());
  4496. }
  4497. //---------------------------------------------------------------------------
  4498. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4499. {
  4500. bool Result;
  4501. if (!IsSideLocalBrowser(Side))
  4502. {
  4503. TRemoteTokenList * GroupList = NULL;
  4504. TRemoteTokenList * UserList = NULL;
  4505. try
  4506. {
  4507. TRemoteProperties CurrentProperties;
  4508. LoadFilesProperties(FileList);
  4509. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4510. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4511. if (CapableGroupChanging || CapableOwnerChanging)
  4512. {
  4513. if (CapableGroupChanging)
  4514. {
  4515. GroupList = Terminal->Groups->Duplicate();
  4516. }
  4517. if (CapableOwnerChanging)
  4518. {
  4519. UserList = Terminal->Users->Duplicate();
  4520. }
  4521. TRemoteDirectory * Files = Terminal->Files;
  4522. int Count = Files->Count;
  4523. if (Count > 100)
  4524. {
  4525. Count = 100;
  4526. }
  4527. for (int Index = 0; Index < Count; Index++)
  4528. {
  4529. TRemoteFile * File = Files->Files[Index];
  4530. if (CapableGroupChanging)
  4531. {
  4532. GroupList->AddUnique(File->Group);
  4533. }
  4534. if (CapableOwnerChanging)
  4535. {
  4536. UserList->AddUnique(File->Owner);
  4537. }
  4538. }
  4539. // if we haven't collected tokens for all files in current directory,
  4540. // make sure we collect them at least for all selected files.
  4541. // (note that so far the files in FileList has to be from current direcotry)
  4542. if (Count < Files->Count)
  4543. {
  4544. for (int Index = 0; Index < FileList->Count; Index++)
  4545. {
  4546. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4547. if (CapableGroupChanging)
  4548. {
  4549. GroupList->AddUnique(File->Group);
  4550. }
  4551. if (CapableOwnerChanging)
  4552. {
  4553. UserList->AddUnique(File->Owner);
  4554. }
  4555. }
  4556. }
  4557. }
  4558. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4559. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4560. if (CapableAclChanging && TRemoteFileList::AnyDirectory(FileList))
  4561. {
  4562. CapableAclChanging = false;
  4563. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4564. }
  4565. int Flags = 0;
  4566. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4567. if (CapableAclChanging) Flags |= cpAcl;
  4568. if (CapableOwnerChanging) Flags |= cpOwner;
  4569. if (CapableGroupChanging) Flags |= cpGroup;
  4570. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4571. if (CanCalculateChecksum())
  4572. {
  4573. CalculateChecksumEvent = CalculateChecksum;
  4574. }
  4575. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4576. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4577. int Options =
  4578. FLAGMASK(Terminal->IsCapable[fcGroupOwnerChangingByID], poUserGroupByID) |
  4579. FLAGMASK(Terminal->IsCapable[fcTags], poTags);
  4580. TRemoteProperties NewProperties = CurrentProperties;
  4581. Result =
  4582. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4583. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags, Options,
  4584. CalculateSize, CalculateChecksumEvent);
  4585. if (Result)
  4586. {
  4587. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4588. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4589. }
  4590. }
  4591. __finally
  4592. {
  4593. delete GroupList;
  4594. delete UserList;
  4595. }
  4596. }
  4597. else
  4598. {
  4599. DirView(Side)->DisplayPropertiesMenu();
  4600. Result = true;
  4601. }
  4602. return Result;
  4603. }
  4604. //---------------------------------------------------------------------------
  4605. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4606. {
  4607. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4608. {
  4609. FIgnoreNextDialogChar = Key;
  4610. }
  4611. Key = 0;
  4612. }
  4613. //---------------------------------------------------------------------------
  4614. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4615. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4616. {
  4617. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4618. if (Command != NULL)
  4619. {
  4620. KeyProcessed(Key, Shift);
  4621. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4622. {
  4623. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4624. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4625. false, false, const_cast<TCustomCommandType *>(Command));
  4626. }
  4627. }
  4628. }
  4629. //---------------------------------------------------------------------------
  4630. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4631. {
  4632. if (!NonVisualDataModule->Busy)
  4633. {
  4634. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4635. {
  4636. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4637. }
  4638. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4639. {
  4640. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4641. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4642. {
  4643. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4644. // But so far it's not a problem, as it does what we want.
  4645. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4646. if (((Action->ShortCut == KeyShortCut) ||
  4647. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4648. AllowedAction(Action, aaShortCut))
  4649. {
  4650. // Has to be called before the action as the dialog char is already in queue.
  4651. // So when the action consumes message queue, we already need to have the
  4652. // FIgnoreNextDialogChar set
  4653. KeyProcessed(Key, Shift);
  4654. // Reset reference to previous component (when menu/toolbar was clicked).
  4655. // Needed to detect that action was invoked by keyboard shortcut
  4656. // in TNonVisualDataModule::ExplorerActionsExecute
  4657. Action->ActionComponent = NULL;
  4658. Action->Execute();
  4659. return;
  4660. }
  4661. }
  4662. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4663. {
  4664. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4665. {
  4666. KeyProcessed(Key, Shift);
  4667. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4668. return;
  4669. }
  4670. }
  4671. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4672. {
  4673. KeyProcessed(Key, Shift);
  4674. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4675. }
  4676. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4677. if (IsCustomShortCut(CustomShortCut))
  4678. {
  4679. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4680. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4681. if (WinConfiguration->SharedBookmarks != NULL)
  4682. {
  4683. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4684. if ((Bookmark != NULL) &&
  4685. OpenBookmark(FCurrentSide, Bookmark))
  4686. {
  4687. KeyProcessed(Key, Shift);
  4688. }
  4689. }
  4690. }
  4691. }
  4692. }
  4693. TForm::KeyDown(Key, Shift);
  4694. }
  4695. //---------------------------------------------------------------------------
  4696. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4697. {
  4698. bool OpenedTerminal = false;
  4699. UnicodeString ProtocolName;
  4700. UnicodeString ProtocolNameHint;
  4701. UnicodeString SecurityProtocolName;
  4702. UnicodeString DurationHint;
  4703. // See the comment in TScpCommanderForm::UpdateControls()
  4704. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4705. if (ActiveTerminal != NULL)
  4706. {
  4707. // Active is not enough here
  4708. if (ActiveTerminal->Status == ssOpened)
  4709. {
  4710. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4711. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4712. if (FileSystemInfo.ProtocolName.IsEmpty())
  4713. {
  4714. ProtocolName = SessionInfo.ProtocolName;
  4715. }
  4716. else
  4717. {
  4718. ProtocolName = FileSystemInfo.ProtocolName;
  4719. }
  4720. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4721. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4722. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4723. OpenedTerminal = true;
  4724. }
  4725. else if (ActiveTerminal->Status > ssClosed)
  4726. {
  4727. ProtocolName = LoadStr(STATUS_CONNECTING);
  4728. }
  4729. else
  4730. {
  4731. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4732. }
  4733. }
  4734. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4735. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4736. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4737. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4738. if (ProtocolStatusPanel->Caption != ProtocolName)
  4739. {
  4740. ProtocolStatusPanel->Caption = ProtocolName;
  4741. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4742. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4743. }
  4744. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4745. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4746. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4747. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4748. SecurityStatusPanel->Enabled = SecurityEnabled;
  4749. UnicodeString SecurityProtocolHint;
  4750. if (SecurityEnabled)
  4751. {
  4752. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4753. }
  4754. else
  4755. {
  4756. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4757. }
  4758. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4759. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4760. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4761. DurationStatusPanel->Hint = DurationHint;
  4762. UpdateStatusBar();
  4763. }
  4764. //---------------------------------------------------------------------------
  4765. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4766. {
  4767. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4768. DebugAssert(SessionStatusBar != NULL);
  4769. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4770. if (!FNote.IsEmpty())
  4771. {
  4772. NoteStatusPanel->Caption = FNote;
  4773. }
  4774. else
  4775. {
  4776. UpdateStatusPanelText(NoteStatusPanel);
  4777. }
  4778. NoteStatusPanel->Hint = FNoteHints;
  4779. UnicodeString LoginTime;
  4780. // See the comment in TScpCommanderForm::UpdateControls()
  4781. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4782. // Active is not enough here
  4783. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4784. {
  4785. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4786. LoginTime = FormatDateTimeSpan(Now() - SessionInfo.LoginTime);
  4787. }
  4788. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4789. DurationStatusPanel->Caption = LoginTime;
  4790. }
  4791. //---------------------------------------------------------------------------
  4792. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4793. {
  4794. Panel->Caption = L"";
  4795. }
  4796. //---------------------------------------------------------------------------
  4797. void __fastcall TCustomScpExplorerForm::Idle()
  4798. {
  4799. if (FShowing ||
  4800. // Particularly for
  4801. // - Standalone editing
  4802. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4803. // while "keeping remote directory up to date".
  4804. StandaloneOperation)
  4805. {
  4806. FEditorManager->Check();
  4807. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4808. // that needs to know if queue view is visible (and it may be closed after queue update)
  4809. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4810. }
  4811. if (FShowing)
  4812. {
  4813. if (!NonVisualDataModule->Busy &&
  4814. // Menu is opened or drag&drop is going on
  4815. (Mouse->Capture == NULL))
  4816. {
  4817. if (FRefreshRemoteDirectory)
  4818. {
  4819. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4820. {
  4821. Terminal->RefreshDirectory();
  4822. }
  4823. FRefreshRemoteDirectory = false;
  4824. }
  4825. if (FRefreshLocalDirectory)
  4826. {
  4827. ReloadLocalDirectory();
  4828. FRefreshLocalDirectory = false;
  4829. }
  4830. if (WinConfiguration->RefreshRemotePanel)
  4831. {
  4832. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4833. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4834. {
  4835. RemoteDirView->ReloadDirectory();
  4836. }
  4837. }
  4838. }
  4839. // TB2 shows accels on Alt key down, but hides it only after menu modal loop finishes.
  4840. // But if Alt is used to switch apps or for keyboard shortcut, the menu modal loop does not happen and accelerators stay displayed.
  4841. // It's difficult to fix this in TB2, as there's no "up" equivalent to CM_DIALOGKEY, which shows the accels.
  4842. // 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.
  4843. TTBCustomToolbar * MenuToolbar = DebugNotNull(dynamic_cast<TTBCustomToolbar *>(GetComponent(fcMenu)));
  4844. MenuToolbar->Idle();
  4845. }
  4846. if (FShowing || StandaloneOperation)
  4847. {
  4848. if (FQueueStatusInvalidated)
  4849. {
  4850. UpdateQueueStatus(false);
  4851. }
  4852. RefreshQueueItems();
  4853. }
  4854. if (FShowing)
  4855. {
  4856. UpdateStatusBar();
  4857. }
  4858. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4859. {
  4860. FClipboardFakeMonitorsPendingReset = false;
  4861. FClipboardFakeMonitors.reset(NULL);
  4862. }
  4863. FIgnoreNextDialogChar = 0;
  4864. }
  4865. //---------------------------------------------------------------------------
  4866. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4867. {
  4868. try
  4869. {
  4870. FUserActionTimer->Enabled = false;
  4871. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4872. {
  4873. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4874. {
  4875. FPendingQueueActionItem->ProcessUserAction();
  4876. }
  4877. }
  4878. }
  4879. __finally
  4880. {
  4881. FPendingQueueActionItem = NULL;
  4882. }
  4883. }
  4884. //---------------------------------------------------------------------------
  4885. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4886. {
  4887. if (WinConfiguration->MinimizeToTray)
  4888. {
  4889. UpdateTrayIcon();
  4890. FTrayIcon->Visible = true;
  4891. if (Visible)
  4892. {
  4893. ShowWindow(Handle, SW_HIDE);
  4894. }
  4895. }
  4896. }
  4897. //---------------------------------------------------------------------------
  4898. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4899. {
  4900. // WORKAROUND
  4901. // When restoring maximized window from minimization,
  4902. // rarely some controls do not align properly.
  4903. // Two instances seen (both for Commander):
  4904. // - When restoring, window is temporarily narrower (not maximized),
  4905. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4906. // Once maximized already, top dock shinks back, but the session PageControl,
  4907. // do not align up, leaving space between TopDock and PageControl.
  4908. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4909. for (int Index = 0; Index < ControlCount; Index++)
  4910. {
  4911. RealignControl(Controls[Index]);
  4912. }
  4913. if (FTrayIcon->Visible)
  4914. {
  4915. FTrayIcon->Visible = false;
  4916. if (Visible)
  4917. {
  4918. ShowWindow(Handle, SW_SHOW);
  4919. }
  4920. }
  4921. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4922. {
  4923. FNeedSession = false;
  4924. NonVisualDataModule->StartBusy();
  4925. try
  4926. {
  4927. NeedSession(true);
  4928. }
  4929. __finally
  4930. {
  4931. NonVisualDataModule->EndBusy();
  4932. }
  4933. }
  4934. }
  4935. //---------------------------------------------------------------------------
  4936. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4937. {
  4938. FTrayIcon->Hint = GetMainForm()->Caption;
  4939. }
  4940. //---------------------------------------------------------------------------
  4941. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4942. {
  4943. UpdateTrayIcon();
  4944. }
  4945. //---------------------------------------------------------------------------
  4946. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4947. {
  4948. // workaround
  4949. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4950. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4951. // But WM_ACTIVATEAPP message can be received even when
  4952. // the main window is minimized to the tray and internal editor window is focused
  4953. // (after another application was previously active)
  4954. if (::IsIconic(Handle))
  4955. {
  4956. if (!IsAppIconic())
  4957. {
  4958. SetAppIconic(true);
  4959. }
  4960. }
  4961. ::ApplicationRestore();
  4962. Application->BringToFront();
  4963. }
  4964. //---------------------------------------------------------------------------
  4965. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4966. {
  4967. RestoreApp();
  4968. }
  4969. //---------------------------------------------------------------------------
  4970. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4971. {
  4972. if (OpenInNewWindow())
  4973. {
  4974. ExecuteNewInstance(SessionUrl);
  4975. }
  4976. else
  4977. {
  4978. TTerminalManager::Instance()->NewSession(SessionUrl);
  4979. }
  4980. }
  4981. //---------------------------------------------------------------------------
  4982. void TCustomScpExplorerForm::NewTab(TOperationSide, bool)
  4983. {
  4984. NewSession();
  4985. }
  4986. //---------------------------------------------------------------------------
  4987. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4988. {
  4989. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4990. StoredSessions->NewSession(SessionName, SessionData);
  4991. // modified only, explicit
  4992. StoredSessions->Save(false, true);
  4993. // encode session name because of slashes in hierarchical sessions
  4994. return EncodeUrlString(SessionName);
  4995. }
  4996. //---------------------------------------------------------------------------
  4997. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4998. {
  4999. // current working directories become defaults here, what is not right
  5000. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5001. return SaveHiddenDuplicateSession(SessionData.get());
  5002. }
  5003. //---------------------------------------------------------------------------
  5004. void __fastcall TCustomScpExplorerForm::DuplicateTab()
  5005. {
  5006. if (OpenInNewWindow())
  5007. {
  5008. ExecuteNewInstance(CreateHiddenDuplicateSession());
  5009. }
  5010. else
  5011. {
  5012. // current working directories become defaults here, what is not right
  5013. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5014. TTerminalManager * Manager = TTerminalManager::Instance();
  5015. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  5016. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  5017. // We definitely want these
  5018. ASession->Disconnected = ManagedSession->Disconnected;
  5019. // Not sure about these two
  5020. ASession->Permanent = ManagedSession->Permanent;
  5021. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  5022. Manager->ActiveSession = ASession;
  5023. }
  5024. }
  5025. //---------------------------------------------------------------------------
  5026. void __fastcall TCustomScpExplorerForm::RenameTab()
  5027. {
  5028. UnicodeString Name = ManagedSession->SessionData->SessionName;
  5029. if (InputDialog(LoadStr(RENAME_TAB_TITLE), LoadStr(RENAME_TAB_PROMPT), Name, HELP_TAB_RENAME) &&
  5030. // When submitting the default name, do not fix it
  5031. (Name != ManagedSession->SessionData->SessionName))
  5032. {
  5033. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  5034. TSessionData::ValidateName(Name);
  5035. // This is inconsistent with how color (for example) is handled.
  5036. ManagedSession->SessionData->Name = Name;
  5037. UpdateControls();
  5038. // Add/Remove distinguishing paths from sessions of the same name.
  5039. SessionListChanged();
  5040. }
  5041. }
  5042. //---------------------------------------------------------------------------
  5043. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  5044. {
  5045. DebugAssert(Queue != NULL);
  5046. bool Result = Queue->IsEmpty;
  5047. if (!Result)
  5048. {
  5049. SetFocus();
  5050. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  5051. }
  5052. return Result;
  5053. }
  5054. //---------------------------------------------------------------------------
  5055. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  5056. {
  5057. return CanCloseQueue(FQueue);
  5058. }
  5059. //---------------------------------------------------------------------------
  5060. void __fastcall TCustomScpExplorerForm::CloseTab()
  5061. {
  5062. if (CanCloseQueue())
  5063. {
  5064. TTerminalManager::Instance()->FreeActiveTerminal();
  5065. }
  5066. }
  5067. //---------------------------------------------------------------------------
  5068. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  5069. {
  5070. if (CanCloseQueue())
  5071. {
  5072. TTerminalManager::Instance()->DisconnectActiveTerminal();
  5073. }
  5074. }
  5075. //---------------------------------------------------------------------------
  5076. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  5077. {
  5078. DetachTerminal(Terminal);
  5079. RemoteDirView->Terminal = Terminal;
  5080. UpdateRemotePathComboBox(false);
  5081. InitStatusBar();
  5082. UpdateControls();
  5083. }
  5084. //---------------------------------------------------------------------------
  5085. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  5086. {
  5087. InitStatusBar();
  5088. }
  5089. //---------------------------------------------------------------------------
  5090. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  5091. {
  5092. if (DebugAlwaysTrue(!Terminal->Active))
  5093. {
  5094. TTerminalManager::Instance()->ReconnectActiveTerminal();
  5095. }
  5096. }
  5097. //---------------------------------------------------------------------------
  5098. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  5099. {
  5100. if (OpenInNewWindow())
  5101. {
  5102. // encode session name because of slashes in hierarchical sessions
  5103. ExecuteNewInstance(EncodeUrlString(Data->Name));
  5104. }
  5105. else
  5106. {
  5107. TTerminalManager * Manager = TTerminalManager::Instance();
  5108. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  5109. Manager->ActiveSession = Terminal;
  5110. }
  5111. }
  5112. //---------------------------------------------------------------------------
  5113. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  5114. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  5115. {
  5116. TTerminalManager * Manager = TTerminalManager::Instance();
  5117. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5118. // This asks for a master password. Maybe we should postpone asking until a connection is actually opened.
  5119. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  5120. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  5121. {
  5122. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  5123. // FirstSession can be null, if none of the workspace sites exist anymore
  5124. // or if all workspace sessions are not suported by the interface
  5125. // (when trying to open all local-local workspace in "explorer" interface)
  5126. if (FirstSession != NULL)
  5127. {
  5128. if (!ConnectFirstTerminal)
  5129. {
  5130. FirstSession->Disconnected = true;
  5131. FirstSession->DisconnectedTemporarily = true;
  5132. }
  5133. Manager->ActiveSession = FirstSession;
  5134. }
  5135. }
  5136. }
  5137. //---------------------------------------------------------------------------
  5138. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  5139. {
  5140. if (OpenInNewWindow())
  5141. {
  5142. ExecuteNewInstance(Name);
  5143. }
  5144. else
  5145. {
  5146. DoOpenFolderOrWorkspace(Name, true, false);
  5147. }
  5148. }
  5149. //---------------------------------------------------------------------------
  5150. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  5151. bool &CanClose)
  5152. {
  5153. AppLog(L"Windows close query");
  5154. // See the comment in CloseApp()
  5155. if (NonVisualDataModule->Busy)
  5156. {
  5157. CanClose = false;
  5158. }
  5159. else
  5160. {
  5161. TTerminalManager * Manager = TTerminalManager::Instance();
  5162. int ActiveSessions = 0;
  5163. for (int Index = 0; Index < Manager->Count; Index++)
  5164. {
  5165. if (Manager->Sessions[Index]->Active)
  5166. {
  5167. ActiveSessions++;
  5168. }
  5169. }
  5170. // Confirm, when there's at least one active remote session or
  5171. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  5172. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  5173. WinConfiguration->ConfirmClosingSession)
  5174. {
  5175. unsigned int Result;
  5176. TMessageParams Params(mpNeverAskAgainCheck);
  5177. UnicodeString Message;
  5178. int Answers = qaOK | qaCancel;
  5179. // The current session is the only active session
  5180. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  5181. {
  5182. if (!WinConfiguration->AutoSaveWorkspace)
  5183. {
  5184. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  5185. }
  5186. else
  5187. {
  5188. Message = LoadStr(CLOSE_SESSION);
  5189. }
  5190. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  5191. }
  5192. // Multiple active sessions or one active session, but it's not the current one
  5193. else if (ActiveSessions > 0)
  5194. {
  5195. if (!WinConfiguration->AutoSaveWorkspace)
  5196. {
  5197. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  5198. }
  5199. else
  5200. {
  5201. Message = LoadStr(CLOSE_SESSIONS);
  5202. }
  5203. }
  5204. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  5205. else
  5206. {
  5207. DebugAlwaysTrue(Manager->Count > 1);
  5208. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  5209. Message = LoadStr(CLOSE_WORKSPACE);
  5210. }
  5211. UnicodeString Note;
  5212. if (WinConfiguration->AutoSaveWorkspace)
  5213. {
  5214. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  5215. }
  5216. else
  5217. {
  5218. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  5219. Answers = qaYes | qaNo | qaCancel;
  5220. }
  5221. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  5222. SetFocus();
  5223. Result = MessageDialog(Message, qtConfirmation,
  5224. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  5225. if (Result == qaNeverAskAgain)
  5226. {
  5227. WinConfiguration->ConfirmClosingSession = false;
  5228. }
  5229. if (Result == qaNo)
  5230. {
  5231. CanClose = SaveWorkspace(true);
  5232. // note that the workspace will be saved redundatly again from FormClose
  5233. }
  5234. else
  5235. {
  5236. CanClose =
  5237. (Result == qaOK) ||
  5238. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  5239. (Result == qaNeverAskAgain);
  5240. }
  5241. }
  5242. if (CanClose && HasActiveTerminal())
  5243. {
  5244. CanClose = CanCloseQueue();
  5245. }
  5246. }
  5247. if (CanClose)
  5248. {
  5249. CanClose =
  5250. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  5251. FEditorManager->CloseExternalFilesWithoutProcess();
  5252. if (CanClose)
  5253. {
  5254. while (CanClose && (FLocalEditors->Count > 0))
  5255. {
  5256. int PrevCount = FLocalEditors->Count;
  5257. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  5258. CanClose = (FLocalEditors->Count < PrevCount);
  5259. }
  5260. if (CanClose)
  5261. {
  5262. CanClose = FEditorManager->Empty(true);
  5263. if (!CanClose)
  5264. {
  5265. SetFocus();
  5266. CanClose =
  5267. (MessageDialog(
  5268. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  5269. }
  5270. }
  5271. }
  5272. }
  5273. }
  5274. //---------------------------------------------------------------------------
  5275. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  5276. {
  5277. TForm * Form = dynamic_cast<TForm *>(Sender);
  5278. DebugAssert(Form != NULL);
  5279. Form->Close();
  5280. }
  5281. //---------------------------------------------------------------------------
  5282. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  5283. {
  5284. TForm * Form = dynamic_cast<TForm *>(Sender);
  5285. delete Form;
  5286. }
  5287. //---------------------------------------------------------------------------
  5288. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  5289. {
  5290. while (FLocalEditors->Count > 0)
  5291. {
  5292. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  5293. }
  5294. }
  5295. //---------------------------------------------------------------------------
  5296. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  5297. TObject *Sender)
  5298. {
  5299. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  5300. try
  5301. {
  5302. SetProperties(osRemote, FileList);
  5303. }
  5304. __finally
  5305. {
  5306. delete FileList;
  5307. }
  5308. }
  5309. //---------------------------------------------------------------------------
  5310. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  5311. {
  5312. if (value)
  5313. {
  5314. if (Component == fcCustomCommandsBand)
  5315. {
  5316. UpdateCustomCommandsToolbar();
  5317. }
  5318. if (Component == fcQueueFileList)
  5319. {
  5320. DebugAssert(!QueueFileListSplitter->Visible);
  5321. AdjustQueueLayout();
  5322. UpdateQueueFileList();
  5323. }
  5324. }
  5325. }
  5326. //---------------------------------------------------------------------------
  5327. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  5328. {
  5329. int MinHeightLimit =
  5330. GetStaticQueuePanelComponentsHeight() +
  5331. QueueFileListSplitter->Height +
  5332. QueueFileList->Height +
  5333. GetMinQueueViewHeight();
  5334. if (QueuePanel->ClientHeight < MinHeightLimit)
  5335. {
  5336. QueuePanel->ClientHeight = MinHeightLimit;
  5337. }
  5338. }
  5339. //---------------------------------------------------------------------------
  5340. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  5341. {
  5342. TControl * Control = GetComponent(Component);
  5343. DebugAssert(Control);
  5344. bool Changed = (Control->Visible != value);
  5345. if (Changed)
  5346. {
  5347. ComponentShowing(Component, value);
  5348. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  5349. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  5350. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  5351. if (value)
  5352. {
  5353. int RemainingHeight = Control->Parent->ClientHeight;
  5354. int RemainingWidth = Control->Parent->ClientWidth;
  5355. for (int i = 0; i < Control->Parent->ControlCount; i++)
  5356. {
  5357. TControl * ChildControl = Control->Parent->Controls[i];
  5358. if (ChildControl->Visible)
  5359. {
  5360. switch (ChildControl->Align)
  5361. {
  5362. case alTop:
  5363. case alBottom:
  5364. RemainingHeight -= ChildControl->Height;
  5365. break;
  5366. case alLeft:
  5367. case alRight:
  5368. RemainingWidth -= ChildControl->Width;
  5369. break;
  5370. }
  5371. }
  5372. }
  5373. int Reserve = ScaleByTextHeight(this, 32);
  5374. // queue in explorer, trees in commander
  5375. if (Control->Height > RemainingHeight - Reserve)
  5376. {
  5377. Control->Height = RemainingHeight / 2;
  5378. }
  5379. if (Control->Width > RemainingWidth - Reserve)
  5380. {
  5381. Control->Width = RemainingWidth / 2;
  5382. }
  5383. }
  5384. Control->Visible = value;
  5385. if (WasFocused && Visible)
  5386. {
  5387. DirView(osCurrent)->SetFocus();
  5388. }
  5389. FixControlsPlacement();
  5390. }
  5391. }
  5392. //---------------------------------------------------------------------------
  5393. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  5394. {
  5395. TControl * Control = GetComponent(Component);
  5396. if (Control == NULL)
  5397. {
  5398. return false;
  5399. }
  5400. else
  5401. {
  5402. return Control->Visible;
  5403. }
  5404. }
  5405. //---------------------------------------------------------------------------
  5406. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  5407. {
  5408. bool Result;
  5409. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  5410. {
  5411. Result = !IsUWP();
  5412. }
  5413. else
  5414. {
  5415. Result = true;
  5416. }
  5417. return Result;
  5418. }
  5419. //---------------------------------------------------------------------------
  5420. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  5421. {
  5422. if (DirView->ItemFocused != NULL)
  5423. {
  5424. DirView->ItemFocused->MakeVisible(false);
  5425. }
  5426. }
  5427. //---------------------------------------------------------------------------
  5428. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5429. {
  5430. MakeFocusedItemVisible(RemoteDirView);
  5431. QueueSplitter->Visible = QueuePanel->Visible;
  5432. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5433. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5434. TControl * TopControlsOrder[] =
  5435. { TopDock, MessageDock, SessionsPageControl };
  5436. SetVerticalControlsOrder(TopControlsOrder, LENOF(TopControlsOrder));
  5437. TControl * QueueControlsOrder[] =
  5438. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5439. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5440. }
  5441. //---------------------------------------------------------------------------
  5442. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5443. {
  5444. switch (Component) {
  5445. case fcStatusBar: return RemoteStatusBar;
  5446. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5447. case fcQueueView: return QueuePanel;
  5448. case fcQueueToolbar: return QueueDock;
  5449. case fcRemoteTree: return RemoteDrivePanel;
  5450. case fcSessionsTabs: return SessionsPageControl;
  5451. case fcQueueFileList: return QueueFileList;
  5452. default: return NULL;
  5453. }
  5454. }
  5455. //---------------------------------------------------------------------------
  5456. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5457. TObject *Sender, TListColumn *Column, TPoint &Point)
  5458. {
  5459. DebugAssert(NonVisualDataModule && Column && Sender);
  5460. NonVisualDataModule->ListColumn = Column;
  5461. TPopupMenu * DirViewColumnMenu;
  5462. if (Sender == DirView(osOther))
  5463. {
  5464. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5465. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible = (Column->Index == uvName);
  5466. bool SizeColumn = (Column->Index == uvSize);
  5467. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible = SizeColumn;
  5468. NonVisualDataModule->RemoteCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5469. }
  5470. else
  5471. {
  5472. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5473. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible = (Column->Index == dvName);
  5474. bool SizeColumn = (Column->Index == dvSize);
  5475. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible = SizeColumn;
  5476. NonVisualDataModule->LocalCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5477. }
  5478. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5479. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5480. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5481. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5482. }
  5483. //---------------------------------------------------------------------------
  5484. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5485. TObject *Sender, TListItem *Item, bool &AllowExec)
  5486. {
  5487. DoDirViewExecFile(Sender, Item, AllowExec);
  5488. }
  5489. //---------------------------------------------------------------------------
  5490. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5491. TListItem * Item, bool & AllowExec)
  5492. {
  5493. DebugAssert(Sender && Item && Configuration);
  5494. DebugAssert(AllowExec);
  5495. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5496. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5497. bool Remote = !IsSideLocalBrowser(Side);
  5498. DebugAssert(Item == ADirView->ItemFocused);
  5499. bool IsDirectory = ADirView->ItemIsDirectory(Item);
  5500. TResolvedDoubleClickAction Action;
  5501. if (FForceExecution)
  5502. {
  5503. Action = IsDirectory ? rdcaChangeDir : rdcaOpen;
  5504. }
  5505. else
  5506. {
  5507. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  5508. Action = WinConfiguration->ResolveDoubleClickAction(IsDirectory, ATerminal);
  5509. }
  5510. if (Action == rdcaCopy)
  5511. {
  5512. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5513. UnlockWindow();
  5514. try
  5515. {
  5516. if (IsLocalBrowserMode())
  5517. {
  5518. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5519. }
  5520. else
  5521. {
  5522. if (HasActiveTerminal())
  5523. {
  5524. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5525. }
  5526. }
  5527. }
  5528. __finally
  5529. {
  5530. LockWindow();
  5531. }
  5532. AllowExec = false;
  5533. }
  5534. else if (Action == rdcaEdit)
  5535. {
  5536. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5537. {
  5538. ExecuteFile(Side, efDefaultEditor);
  5539. AllowExec = false;
  5540. }
  5541. }
  5542. else if (Action == rdcaOpen)
  5543. {
  5544. if (DebugAlwaysTrue(!IsDirectory) && Remote && !WinConfiguration->DisableOpenEdit)
  5545. {
  5546. ExecuteFile(osRemote, efShell);
  5547. AllowExec = false;
  5548. }
  5549. }
  5550. }
  5551. //---------------------------------------------------------------------------
  5552. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5553. {
  5554. return ((Side == osRemote) || (Side == osCurrent));
  5555. }
  5556. //---------------------------------------------------------------------------
  5557. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5558. {
  5559. DebugFail();
  5560. }
  5561. //---------------------------------------------------------------------------
  5562. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5563. {
  5564. DebugFail();
  5565. }
  5566. //---------------------------------------------------------------------------
  5567. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5568. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5569. {
  5570. TSynchronizeParamType Params;
  5571. Params.LocalDirectory = LocalDirectory;
  5572. Params.RemoteDirectory = RemoteDirectory;
  5573. int UnusedParams =
  5574. (GUIConfiguration->SynchronizeParams &
  5575. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5576. TTerminal::spCaseSensitive | TTerminal::spMirror | TTerminal::spByChecksum));
  5577. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5578. Params.Options = GUIConfiguration->SynchronizeOptions;
  5579. bool SaveSettings = false;
  5580. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5581. &DoSynchronizeTooManyDirectories);
  5582. DebugAssert(FSynchronizeController == NULL);
  5583. FSynchronizeController = &Controller;
  5584. bool Result;
  5585. try
  5586. {
  5587. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5588. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5589. int Options =
  5590. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5591. DebugAssert(FOnFeedSynchronizeError == NULL);
  5592. DebugAssert(FOnSynchronizeAbort == NULL);
  5593. DebugAssert(FSynchronizeTerminal == NULL);
  5594. FSynchronizeTerminal = Terminal;
  5595. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5596. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5597. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5598. if (Result)
  5599. {
  5600. if (SaveSettings)
  5601. {
  5602. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5603. GUIConfiguration->SynchronizeOptions = Params.Options;
  5604. }
  5605. else
  5606. {
  5607. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5608. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5609. FLAGSET(Params.Options, soSynchronize))
  5610. {
  5611. GUIConfiguration->SynchronizeOptions =
  5612. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5613. soSynchronize;
  5614. }
  5615. }
  5616. LocalDirectory = Params.LocalDirectory;
  5617. RemoteDirectory = Params.RemoteDirectory;
  5618. }
  5619. }
  5620. __finally
  5621. {
  5622. FSynchronizeController = NULL;
  5623. DebugAssert(FOnFeedSynchronizeError == NULL);
  5624. FOnFeedSynchronizeError = NULL;
  5625. DebugAssert(FOnSynchronizeAbort == NULL);
  5626. FOnSynchronizeAbort = NULL;
  5627. FSynchronizeTerminal = NULL;
  5628. }
  5629. return Result;
  5630. }
  5631. //---------------------------------------------------------------------------
  5632. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5633. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5634. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5635. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5636. TSynchronizeOptions * Options, bool Full)
  5637. {
  5638. // Keep-up-to-date mode
  5639. if (Terminal->Status == ssOpened)
  5640. {
  5641. try
  5642. {
  5643. int PParams = Params.Params;
  5644. if (!Full)
  5645. {
  5646. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5647. if (FLAGSET(Params.Options, soRecurse))
  5648. {
  5649. PParams |= TTerminal::spSubDirs;
  5650. }
  5651. }
  5652. else
  5653. {
  5654. // if keepuptodate is non-recursive,
  5655. // full sync before has to be non-recursive as well
  5656. if (FLAGCLEAR(Params.Options, soRecurse))
  5657. {
  5658. PParams |= TTerminal::spNoRecurse;
  5659. }
  5660. }
  5661. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5662. PParams, Checklist, Options);
  5663. }
  5664. catch (Exception & E)
  5665. {
  5666. if (!Terminal->Active)
  5667. {
  5668. ShowExtendedExceptionEx(Terminal, &E);
  5669. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5670. if (!Terminal->Active)
  5671. {
  5672. throw;
  5673. }
  5674. }
  5675. else
  5676. {
  5677. if (FLAGSET(Params.Options, soContinueOnError) &&
  5678. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5679. {
  5680. // noop, already logged in MoreMessageDialog
  5681. }
  5682. else
  5683. {
  5684. // We get mostly EAbort here, so this is noop.
  5685. // Exception is an error when listing directories while looking for differences.
  5686. ShowExtendedExceptionEx(Terminal, &E);
  5687. throw;
  5688. }
  5689. }
  5690. }
  5691. }
  5692. }
  5693. //---------------------------------------------------------------------------
  5694. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5695. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5696. const UnicodeString ErrorStr)
  5697. {
  5698. if (!Directory.IsEmpty())
  5699. {
  5700. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5701. }
  5702. else
  5703. {
  5704. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5705. }
  5706. }
  5707. //---------------------------------------------------------------------------
  5708. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5709. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5710. {
  5711. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5712. {
  5713. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5714. }
  5715. else
  5716. {
  5717. TMessageParams Params(mpNeverAskAgainCheck);
  5718. unsigned int Result = MessageDialog(
  5719. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5720. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5721. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5722. {
  5723. MaxDirectories *= 2;
  5724. if (Result == qaNeverAskAgain)
  5725. {
  5726. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5727. }
  5728. }
  5729. else
  5730. {
  5731. Abort();
  5732. }
  5733. }
  5734. }
  5735. //---------------------------------------------------------------------------
  5736. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5737. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5738. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5739. TSynchronizeOptions * Options)
  5740. {
  5741. // Keep-up-to-date mode
  5742. DebugAssert(!FAutoOperation);
  5743. void * BatchStorage;
  5744. BatchStart(BatchStorage);
  5745. FAutoOperation = true;
  5746. bool AnyOperation = false;
  5747. TDateTime StartTime = Now();
  5748. TSynchronizeChecklist * AChecklist = NULL;
  5749. try
  5750. {
  5751. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5752. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5753. {
  5754. FSynchronizeProgressForm->Start();
  5755. }
  5756. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5757. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5758. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5759. Options);
  5760. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5761. SAFE_DESTROY(FSynchronizeProgressForm);
  5762. AnyOperation = (AChecklist->CheckedCount > 0);
  5763. if (AnyOperation)
  5764. {
  5765. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5766. CreateProgressForm(&SynchronizeProgress);
  5767. try
  5768. {
  5769. Terminal->SynchronizeApply(
  5770. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5771. SynchronizeProcessedItem, NULL, NULL, NULL);
  5772. }
  5773. __finally
  5774. {
  5775. DestroyProgressForm();
  5776. }
  5777. }
  5778. }
  5779. __finally
  5780. {
  5781. if (Checklist == NULL)
  5782. {
  5783. delete AChecklist;
  5784. }
  5785. else
  5786. {
  5787. *Checklist = AChecklist;
  5788. }
  5789. FAutoOperation = false;
  5790. SAFE_DESTROY(FSynchronizeProgressForm);
  5791. BatchEnd(BatchStorage);
  5792. ReloadLocalDirectory();
  5793. if (AnyOperation)
  5794. {
  5795. OperationComplete(StartTime);
  5796. }
  5797. }
  5798. }
  5799. //---------------------------------------------------------------------------
  5800. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5801. {
  5802. // can be called from command line
  5803. return Visible &&
  5804. ((DirView(osRemote)->SelCount > 0) ||
  5805. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5806. }
  5807. //---------------------------------------------------------------------------
  5808. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5809. {
  5810. LogSynchronizeEvent(Terminal, Message);
  5811. }
  5812. //---------------------------------------------------------------------------
  5813. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5814. int Params, TSynchronizeOptions & Options)
  5815. {
  5816. DebugAssert(!IsLocalBrowserMode());
  5817. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5818. {
  5819. Options.Filter = new TStringList();
  5820. Options.Filter->CaseSensitive = false;
  5821. Options.Filter->Duplicates = Types::dupAccept;
  5822. if (DirView(osRemote)->SelCount > 0)
  5823. {
  5824. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5825. }
  5826. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5827. {
  5828. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5829. }
  5830. Options.Filter->Sort();
  5831. }
  5832. }
  5833. //---------------------------------------------------------------------------
  5834. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5835. {
  5836. TCopyParamType Defaults;
  5837. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5838. ConfigStorage->AccessMode = smRead;
  5839. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5840. {
  5841. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5842. }
  5843. ConfigStorage.reset(NULL);
  5844. std::unique_ptr<TStringList> Options(new TStringList());
  5845. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5846. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5847. UnicodeString Result;
  5848. if (Options->Count > 0)
  5849. {
  5850. Result =
  5851. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5852. }
  5853. return Result;
  5854. }
  5855. //---------------------------------------------------------------------------
  5856. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5857. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5858. {
  5859. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5860. UnicodeString AdditionalParams =
  5861. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5862. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5863. SerializeCopyParamForCommandLine(CopyParams),
  5864. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5865. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5866. ExecuteNewInstance(SessionName, AdditionalParams);
  5867. }
  5868. //---------------------------------------------------------------------------
  5869. struct TSynchronizeParams
  5870. {
  5871. UnicodeString LocalDirectory;
  5872. UnicodeString RemoteDirectory;
  5873. TSynchronizeMode Mode;
  5874. TSynchronizeChecklist * Checklist;
  5875. int Params;
  5876. TCopyParamType * CopyParam;
  5877. TDateTime CollectElapsed;
  5878. TDateTime * StartTime;
  5879. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5880. };
  5881. //---------------------------------------------------------------------------
  5882. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5883. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5884. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5885. {
  5886. DebugAssert(!FAutoOperation);
  5887. void * BatchStorage;
  5888. BatchStart(BatchStorage);
  5889. FAutoOperation = true;
  5890. TFileOperationStatistics Statistics;
  5891. TDateTime Start = Now();
  5892. try
  5893. {
  5894. Params.OnProcessedItem = OnProcessedItem;
  5895. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5896. CreateProgressForm(&SynchronizeProgress);
  5897. Terminal->SynchronizeApply(
  5898. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5899. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5900. &Statistics);
  5901. }
  5902. __finally
  5903. {
  5904. Params.OnProcessedItem = NULL;
  5905. FAutoOperation = false;
  5906. DestroyProgressForm();
  5907. BatchEnd(BatchStorage);
  5908. ReloadLocalDirectory();
  5909. }
  5910. if (WinConfiguration->SynchronizeSummary)
  5911. {
  5912. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5913. // The statistics should be 0 anyway in this case
  5914. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5915. {
  5916. Message += L"\n";
  5917. if (Statistics.FilesUploaded > 0)
  5918. {
  5919. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5920. }
  5921. if (Statistics.FilesDownloaded > 0)
  5922. {
  5923. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5924. }
  5925. if (Statistics.FilesDeletedLocal > 0)
  5926. {
  5927. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5928. }
  5929. if (Statistics.FilesDeletedRemote > 0)
  5930. {
  5931. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5932. }
  5933. }
  5934. TDateTime Elapsed = (Now() - Start);
  5935. Message +=
  5936. L"\n" +
  5937. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Params.CollectElapsed))) + L"\n" +
  5938. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Elapsed)));
  5939. TMessageParams Params(mpNeverAskAgainCheck);
  5940. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5941. if (Result == qaNeverAskAgain)
  5942. {
  5943. WinConfiguration->SynchronizeSummary = false;
  5944. }
  5945. }
  5946. }
  5947. //---------------------------------------------------------------------------
  5948. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5949. {
  5950. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5951. {
  5952. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5953. }
  5954. // Not set in keep-up-to-date mode - Synchronize() method
  5955. if (Token != NULL)
  5956. {
  5957. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5958. if (Params.OnProcessedItem != NULL)
  5959. {
  5960. Params.OnProcessedItem(NULL, ChecklistItem);
  5961. }
  5962. }
  5963. }
  5964. //---------------------------------------------------------------------------
  5965. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5966. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5967. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5968. {
  5969. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5970. *Params.StartTime = Now();
  5971. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5972. }
  5973. //---------------------------------------------------------------------------
  5974. void TCustomScpExplorerForm::DoQueueSynchronize(void * Token)
  5975. {
  5976. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5977. int CopyParams = TTerminal::GetSynchronizeCopyParams(Params.Params);
  5978. TCopyParamType SyncCopyParam = TTerminal::GetSynchronizeCopyParam(Params.CopyParam, Params.Params);
  5979. bool Parallel = GUIConfiguration->CurrentCopyParam.QueueParallel;
  5980. int Index = 0;
  5981. const TSynchronizeChecklist::TItem * ChecklistItem;
  5982. while (Params.Checklist->GetNextChecked(Index, ChecklistItem))
  5983. {
  5984. TQueueItem * QueueItem = Terminal->SynchronizeToQueue(ChecklistItem, &SyncCopyParam, CopyParams, Parallel);
  5985. AddQueueItem(Queue, QueueItem, Terminal);
  5986. }
  5987. }
  5988. //---------------------------------------------------------------------------
  5989. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5990. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5991. {
  5992. // terminal can be already closed (e.g. dropped connection)
  5993. if (Terminal != NULL)
  5994. {
  5995. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5996. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5997. }
  5998. }
  5999. //---------------------------------------------------------------------------
  6000. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  6001. TOperationSide Side, TStrings * FileList, const UnicodeString & NewFileName, bool TargetIsDirectory, void * Token)
  6002. {
  6003. DebugAssert(!IsLocalBrowserMode());
  6004. TAutoBatch AutoBatch(this);
  6005. TAutoFlag AutoOperationFlag(FAutoOperation);
  6006. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  6007. if (Side == osRemote)
  6008. {
  6009. UnicodeString Target;
  6010. UnicodeString FileMask;
  6011. if (TargetIsDirectory)
  6012. {
  6013. Target = NewFileName;
  6014. Terminal->CreateTargetDirectory(Target, faDirectory, Params.CopyParam);
  6015. }
  6016. else
  6017. {
  6018. Target = UnixExtractFileDir(NewFileName);
  6019. FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  6020. }
  6021. RemoteDirView->SaveSelection();
  6022. RemoteDirView->SaveSelectedNames();
  6023. try
  6024. {
  6025. Terminal->MoveFiles(FileList, Target, FileMask, false);
  6026. }
  6027. catch(...)
  6028. {
  6029. RemoteDirView->DiscardSavedSelection();
  6030. throw;
  6031. }
  6032. RemoteDirView->RestoreSelection();
  6033. }
  6034. else if (DebugAlwaysTrue(Side == osLocal))
  6035. {
  6036. std::unique_ptr<TStrings> Directories(CreateSortedStringList());
  6037. if (TargetIsDirectory)
  6038. {
  6039. if (!ForceDirectories(ApiPath(NewFileName)))
  6040. {
  6041. throw EOSExtException(FMTLOAD(CREATE_DIR_ERROR, (NewFileName)));
  6042. }
  6043. Directories->Add(NewFileName);
  6044. }
  6045. else
  6046. {
  6047. Directories->Add(ExtractFileDir(NewFileName));
  6048. }
  6049. for (int Index = 0; Index < FileList->Count; Index++)
  6050. {
  6051. UnicodeString FileName = FileList->Strings[Index];
  6052. UnicodeString Directory = ExtractFileDir(FileName);
  6053. Directories->Add(Directory);
  6054. UnicodeString NewFilePath = NewFileName;
  6055. if (TargetIsDirectory)
  6056. {
  6057. NewFilePath = TPath::Combine(NewFilePath, ExtractFileName(FileName));
  6058. }
  6059. if (!MoveFile(ApiPath(FileName).c_str(), ApiPath(NewFilePath).c_str()))
  6060. {
  6061. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFilePath)));
  6062. }
  6063. }
  6064. for (int Index = 0; Index < Directories->Count; Index++)
  6065. {
  6066. ReloadLocalDirectory(Directories->Strings[Index]);
  6067. }
  6068. }
  6069. }
  6070. //---------------------------------------------------------------------------
  6071. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  6072. {
  6073. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  6074. if (Side == osLocal)
  6075. {
  6076. if (Action == TSynchronizeChecklist::saDownloadNew)
  6077. {
  6078. OpenFolderInExplorer(LocalPath);
  6079. }
  6080. else
  6081. {
  6082. OpenFileInExplorer(CombinePaths(LocalPath, Item->GetFileName()));
  6083. }
  6084. }
  6085. else if (DebugAlwaysTrue(Side == osRemote))
  6086. {
  6087. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  6088. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  6089. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  6090. if (!LocalPath.IsEmpty())
  6091. {
  6092. SessionData->LocalDirectory = LocalPath;
  6093. }
  6094. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  6095. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  6096. }
  6097. }
  6098. //---------------------------------------------------------------------------
  6099. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  6100. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  6101. const TCopyParamType * CopyParams)
  6102. {
  6103. UnicodeString SessionName = CreateHiddenDuplicateSession();
  6104. UnicodeString AdditionalParams =
  6105. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  6106. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  6107. SerializeCopyParamForCommandLine(CopyParams),
  6108. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  6109. LocalDirectory, RemoteDirectory, Mode, Params));
  6110. ExecuteNewInstance(SessionName, AdditionalParams);
  6111. }
  6112. //---------------------------------------------------------------------------
  6113. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  6114. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  6115. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  6116. {
  6117. int Result;
  6118. bool SaveSettings = false;
  6119. int Options =
  6120. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  6121. FLAGMASK(!CanCalculateChecksum(), fsoDisableByChecksum) |
  6122. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  6123. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  6124. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  6125. bool Continue =
  6126. ((UseDefaults == 0) ||
  6127. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  6128. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults)) &&
  6129. (FLAGCLEAR(Params, TTerminal::spByChecksum) || EnsureCommandSessionFallback(fcCalculatingChecksum));
  6130. if (Continue)
  6131. {
  6132. Configuration->Usage->Inc(L"Synchronizations");
  6133. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  6134. UpdateCopyParamCounters(CopyParam);
  6135. TSynchronizeOptions SynchronizeOptions;
  6136. GetSynchronizeOptions(Params, SynchronizeOptions);
  6137. if (SaveSettings)
  6138. {
  6139. GUIConfiguration->SynchronizeParams = Params;
  6140. }
  6141. else
  6142. {
  6143. SaveMode = false;
  6144. }
  6145. TDateTime StartTime = Now();
  6146. TSynchronizeChecklist * Checklist = NULL;
  6147. try
  6148. {
  6149. DebugAssert(!FAutoOperation);
  6150. FAutoOperation = true;
  6151. try
  6152. {
  6153. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  6154. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  6155. int Files = -1;
  6156. if (DataList->Count >= 1)
  6157. {
  6158. Files = StrToIntDef(DataList->Strings[0], Files);
  6159. }
  6160. else
  6161. {
  6162. DataList->Add(UnicodeString());
  6163. }
  6164. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  6165. FSynchronizeProgressForm->Start();
  6166. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  6167. static_cast<TTerminal::TSynchronizeMode>(Mode),
  6168. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  6169. &SynchronizeOptions);
  6170. if (Terminal->SessionData->CacheDirectories)
  6171. {
  6172. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  6173. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  6174. }
  6175. }
  6176. __finally
  6177. {
  6178. FAutoOperation = false;
  6179. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  6180. SAFE_DESTROY(FSynchronizeProgressForm);
  6181. }
  6182. TSynchronizeParams SynchronizeParams;
  6183. SynchronizeParams.LocalDirectory = LocalDirectory;
  6184. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  6185. SynchronizeParams.Mode = Mode;
  6186. SynchronizeParams.CopyParam = &CopyParam;
  6187. SynchronizeParams.Params = Params;
  6188. SynchronizeParams.Checklist = Checklist;
  6189. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  6190. SynchronizeParams.StartTime = &StartTime;
  6191. SynchronizeParams.OnProcessedItem = NULL;
  6192. Result = Checklist->Count;
  6193. if (Checklist->Count > 0)
  6194. {
  6195. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  6196. {
  6197. TQueueSynchronizeEvent OnQueueSynchronize = NULL;
  6198. if (Visible && FLAGCLEAR(Params, TTerminal::spTimestamp))
  6199. {
  6200. OnQueueSynchronize = DoQueueSynchronize;
  6201. }
  6202. if (!DoSynchronizeChecklistDialog(
  6203. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  6204. OnQueueSynchronize, DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse,
  6205. &SynchronizeParams))
  6206. {
  6207. Result = -1;
  6208. }
  6209. }
  6210. else
  6211. {
  6212. FullSynchronize(SynchronizeParams, NULL, NULL);
  6213. }
  6214. }
  6215. else
  6216. {
  6217. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  6218. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  6219. }
  6220. }
  6221. __finally
  6222. {
  6223. delete Checklist;
  6224. }
  6225. OperationComplete(StartTime);
  6226. }
  6227. else
  6228. {
  6229. Result = -1;
  6230. }
  6231. return Result;
  6232. }
  6233. //---------------------------------------------------------------------------
  6234. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  6235. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  6236. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  6237. {
  6238. if (Collect)
  6239. {
  6240. DebugAssert(FSynchronizeProgressForm != NULL);
  6241. if (!FSynchronizeProgressForm->Started)
  6242. {
  6243. FSynchronizeProgressForm->Start();
  6244. }
  6245. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  6246. SetTaskbarListProgressValue(CompareProgress);
  6247. }
  6248. else
  6249. {
  6250. DebugAssert(FSynchronizeProgressForm == NULL);
  6251. DebugAssert(FProgressForm != NULL);
  6252. }
  6253. }
  6254. //---------------------------------------------------------------------------
  6255. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  6256. {
  6257. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  6258. std::unique_ptr<TRemoteFile> File(Terminal->ReadFile(FullFileName));
  6259. if (File.get() != NULL)
  6260. {
  6261. ExecuteRemoteFile(FullFileName, File.get(), efInternalEditor);
  6262. Application->ShowMainForm = false;
  6263. Application->Run();
  6264. }
  6265. }
  6266. //---------------------------------------------------------------------------
  6267. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  6268. {
  6269. DebugFail();
  6270. }
  6271. //---------------------------------------------------------------------------
  6272. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  6273. {
  6274. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  6275. SessionData->Assign(ManagedSession->SessionData);
  6276. UpdateSessionData(SessionData.get());
  6277. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  6278. if (IsActiveTerminal(ManagedSession))
  6279. {
  6280. ManagedSession->UpdateSessionCredentials(SessionData.get());
  6281. }
  6282. if (!ManagedSession->SessionData->HasSessionName())
  6283. {
  6284. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  6285. // once IsWorkspace is cleared
  6286. SessionData->Name = UnicodeString();
  6287. }
  6288. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  6289. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  6290. SessionData->IsWorkspace = false;
  6291. return SessionData.release();
  6292. }
  6293. //---------------------------------------------------------------------------
  6294. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  6295. {
  6296. TSessionData * SessionData = CloneCurrentSessionData();
  6297. try
  6298. {
  6299. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  6300. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  6301. }
  6302. __finally
  6303. {
  6304. delete SessionData;
  6305. }
  6306. }
  6307. //---------------------------------------------------------------------------
  6308. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  6309. {
  6310. TTerminalManager * Manager = TTerminalManager::Instance();
  6311. std::unique_ptr<TObjectList> DataList(new TObjectList());
  6312. if (DebugAlwaysTrue(ManagedSession != NULL))
  6313. {
  6314. // Update ManagedSession->StateData
  6315. UpdateSession(ManagedSession);
  6316. }
  6317. Manager->SaveWorkspace(DataList.get());
  6318. return DataList.release();
  6319. }
  6320. //---------------------------------------------------------------------------
  6321. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  6322. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  6323. {
  6324. WinConfiguration->LastWorkspace = Name;
  6325. if (!SavePasswords)
  6326. {
  6327. for (int Index = 0; Index < DataList->Count; Index++)
  6328. {
  6329. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6330. if (SessionData->Link.IsEmpty())
  6331. {
  6332. SessionData->ClearSessionPasswords();
  6333. }
  6334. }
  6335. }
  6336. StoredSessions->NewWorkspace(Name, DataList);
  6337. // modified only
  6338. StoredSessions->Save(false, Explicit);
  6339. }
  6340. //---------------------------------------------------------------------------
  6341. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  6342. {
  6343. return
  6344. DefaultStr(WinConfiguration->LastWorkspace,
  6345. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  6346. }
  6347. //---------------------------------------------------------------------------
  6348. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  6349. {
  6350. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  6351. bool AnyNonStoredSessionWithPassword = false;
  6352. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  6353. bool AllNonStoredSessionsAnonymous = true;
  6354. TTerminalManager * Manager = TTerminalManager::Instance();
  6355. for (int Index = 0; Index < DataList->Count; Index++)
  6356. {
  6357. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6358. if (SessionData->Link.IsEmpty())
  6359. {
  6360. if (SessionData->HasAnySessionPassword())
  6361. {
  6362. AnyNonStoredSessionWithPassword = true;
  6363. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  6364. {
  6365. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  6366. }
  6367. }
  6368. if (!SameText(SessionData->UserName, AnonymousUserName))
  6369. {
  6370. AllNonStoredSessionsAnonymous = false;
  6371. }
  6372. }
  6373. }
  6374. bool SavePasswords;
  6375. bool * PSavePasswords;
  6376. bool NotRecommendedSavingPasswords =
  6377. !CustomWinConfiguration->UseMasterPassword &&
  6378. !AllNonStoredSessionsAnonymous;
  6379. if (Configuration->DisablePasswordStoring ||
  6380. !AnyNonStoredSessionWithPassword)
  6381. {
  6382. PSavePasswords = NULL;
  6383. SavePasswords = false;
  6384. }
  6385. else
  6386. {
  6387. PSavePasswords = &SavePasswords;
  6388. SavePasswords =
  6389. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  6390. !NotRecommendedSavingPasswords;
  6391. }
  6392. UnicodeString Name = WorkspaceName();
  6393. bool CreateShortcut = false;
  6394. bool Result =
  6395. DoSaveWorkspaceDialog(
  6396. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  6397. EnableAutoSave);
  6398. if (Result)
  6399. {
  6400. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  6401. if (CreateShortcut)
  6402. {
  6403. TOperationVisualizer Visualizer;
  6404. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  6405. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  6406. }
  6407. if (EnableAutoSave)
  6408. {
  6409. WinConfiguration->AutoSaveWorkspace = true;
  6410. WinConfiguration->AutoWorkspace = Name;
  6411. if (PSavePasswords != NULL)
  6412. {
  6413. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  6414. }
  6415. }
  6416. }
  6417. return Result;
  6418. }
  6419. //---------------------------------------------------------------------------
  6420. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  6421. {
  6422. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  6423. DebugAssert(Session == ManagedSession);
  6424. if (!IsLocalBrowserMode())
  6425. {
  6426. SAFE_DESTROY(Session->RemoteExplorerState);
  6427. if (WinConfiguration->PreservePanelState)
  6428. {
  6429. Session->RemoteExplorerState = RemoteDirView->SaveState();
  6430. }
  6431. }
  6432. UpdateSessionData(Session->StateData);
  6433. }
  6434. //---------------------------------------------------------------------------
  6435. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  6436. {
  6437. // Keep in sync with TSessionData::CopyStateData
  6438. DebugAssert(Data != NULL);
  6439. // This is inconsistent with how color (for example) is handled.
  6440. Data->Name = ManagedSession->SessionData->Name;
  6441. if (!IsLocalBrowserMode())
  6442. {
  6443. // cannot use RemoteDirView->Path, because it is empty if connection
  6444. // was already closed
  6445. // also only peek, we may not be connected at all atm
  6446. // so make sure we do not try retrieving current directory from the server
  6447. // (particularly with FTP)
  6448. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  6449. if (!ACurrentDirectory.IsEmpty())
  6450. {
  6451. Data->RemoteDirectory = ACurrentDirectory;
  6452. }
  6453. }
  6454. Data->Color = SessionColor;
  6455. }
  6456. //---------------------------------------------------------------------------
  6457. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  6458. {
  6459. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  6460. DebugAssert(Toolbar != NULL);
  6461. for (int i = 0; i < Toolbar->Items->Count; i++)
  6462. {
  6463. TTBXCustomDropDownItem * DropDownItem;
  6464. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6465. if (DropDownItem != NULL)
  6466. {
  6467. ToolbarItemResize(DropDownItem,
  6468. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  6469. Toolbar->NonClientWidth);
  6470. break;
  6471. }
  6472. }
  6473. }
  6474. //---------------------------------------------------------------------------
  6475. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6476. {
  6477. Item->EditWidth = Width;
  6478. }
  6479. //---------------------------------------------------------------------------
  6480. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6481. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6482. {
  6483. for (int i = 0; i < Toolbar->Items->Count; i++)
  6484. {
  6485. TTBXCustomDropDownItem * DropDownItem;
  6486. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6487. if (DropDownItem != NULL)
  6488. {
  6489. ASize.x -= DropDownItem->EditWidth;
  6490. TControl * Control = this;
  6491. ASize.x += ScaleByTextHeight(Control, 50) /* minimal combo width */;
  6492. }
  6493. }
  6494. }
  6495. //---------------------------------------------------------------------------
  6496. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6497. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6498. {
  6499. if (WinConfiguration->DDWarnLackOfTempSpace)
  6500. {
  6501. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6502. if (!ADrive.IsEmpty())
  6503. {
  6504. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6505. DebugAssert(RequiredSpace >= 0);
  6506. __int64 RequiredWithReserve;
  6507. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6508. if (FreeSpace < RequiredWithReserve)
  6509. {
  6510. unsigned int Result;
  6511. TMessageParams Params(mpNeverAskAgainCheck);
  6512. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6513. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6514. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6515. if (Result == qaNeverAskAgain)
  6516. {
  6517. WinConfiguration->DDWarnLackOfTempSpace = false;
  6518. }
  6519. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6520. }
  6521. }
  6522. }
  6523. }
  6524. //---------------------------------------------------------------------------
  6525. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6526. {
  6527. DoOpenDirectoryDialog(odAddBookmark, Side);
  6528. }
  6529. //---------------------------------------------------------------------------
  6530. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6531. {
  6532. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6533. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6534. try
  6535. {
  6536. TCustomDirView * DView = DirView(Side);
  6537. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6538. {
  6539. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6540. }
  6541. }
  6542. catch (...)
  6543. {
  6544. delete VisitedDirectories;
  6545. throw;
  6546. }
  6547. return VisitedDirectories;
  6548. }
  6549. //---------------------------------------------------------------------------
  6550. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6551. TOpenDirectoryMode Mode, TOperationSide Side)
  6552. {
  6553. bool Continue = true;
  6554. if (Mode == odAddBookmark)
  6555. {
  6556. TMessageParams Params(mpNeverAskAgainCheck);
  6557. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6558. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6559. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6560. unsigned int Answer =
  6561. MessageDialog(Message,
  6562. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6563. if (Answer == qaNeverAskAgain)
  6564. {
  6565. Continue = true;
  6566. WinConfiguration->UseSharedBookmarks = true;
  6567. }
  6568. else if (Answer == qaOK)
  6569. {
  6570. Continue = true;
  6571. WinConfiguration->UseSharedBookmarks = false;
  6572. }
  6573. else
  6574. {
  6575. Continue = false;
  6576. }
  6577. }
  6578. if (Continue)
  6579. {
  6580. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6581. try
  6582. {
  6583. UnicodeString Name = DirView(Side)->PathName;
  6584. bool Repeat;
  6585. do
  6586. {
  6587. Repeat = false;
  6588. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6589. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6590. // do not allow switching to location profiles,
  6591. // if we are not connected
  6592. HasDirView[osLocal] && IsActiveTerminal(Terminal)))
  6593. {
  6594. TWindowLock Lock(this);
  6595. if (!TryOpenDirectory(Side, Name))
  6596. {
  6597. Repeat = true;
  6598. Mode = odBrowse;
  6599. }
  6600. }
  6601. }
  6602. while (Repeat);
  6603. }
  6604. __finally
  6605. {
  6606. delete VisitedDirectories;
  6607. }
  6608. }
  6609. }
  6610. //---------------------------------------------------------------------------
  6611. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6612. {
  6613. DebugAssert(!Terminal->CommandSessionOpened);
  6614. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6615. }
  6616. //---------------------------------------------------------------------------
  6617. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6618. {
  6619. bool Result = Terminal->IsCapable[Capability] ||
  6620. Terminal->CommandSessionOpened;
  6621. if (!Result)
  6622. {
  6623. // We might get here for example when "checksum" synchronization is requested from command-line
  6624. // on server that supports neither checksum nor shell
  6625. if (!Terminal->IsCapable[fcSecondaryShell])
  6626. {
  6627. NotSupported();
  6628. }
  6629. else if (!GUIConfiguration->ConfirmCommandSession)
  6630. {
  6631. Result = true;
  6632. }
  6633. else
  6634. {
  6635. TMessageParams Params(mpNeverAskAgainCheck);
  6636. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6637. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6638. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6639. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6640. if (Answer == qaNeverAskAgain)
  6641. {
  6642. GUIConfiguration->ConfirmCommandSession = false;
  6643. Result = true;
  6644. }
  6645. else if (Answer == qaOK)
  6646. {
  6647. Result = true;
  6648. }
  6649. }
  6650. if (Result)
  6651. {
  6652. Result = CommandSessionFallback();
  6653. }
  6654. }
  6655. return Result;
  6656. }
  6657. //---------------------------------------------------------------------------
  6658. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6659. {
  6660. if (EnsureCommandSessionFallback(fcAnyCommand))
  6661. {
  6662. DoConsoleDialog(Terminal, Command);
  6663. }
  6664. }
  6665. //---------------------------------------------------------------------------
  6666. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6667. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6668. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6669. {
  6670. if (!IsSideLocalBrowser(osRemote) &&
  6671. IsFileControl(DropSourceControl, osRemote) &&
  6672. (FDDExtMapFile != NULL))
  6673. {
  6674. Accept = true;
  6675. }
  6676. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6677. DebugAssert(FDDTargetControl != NULL);
  6678. }
  6679. //---------------------------------------------------------------------------
  6680. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6681. _di_IDataObject DataObj, int KeyState,
  6682. const TPoint & Point, int & Effect, bool & Accept)
  6683. {
  6684. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6685. }
  6686. //---------------------------------------------------------------------------
  6687. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6688. _di_IDataObject DataObj, int KeyState,
  6689. const TPoint & Point, int & Effect, bool & Accept)
  6690. {
  6691. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6692. }
  6693. //---------------------------------------------------------------------------
  6694. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6695. TObject * Sender)
  6696. {
  6697. DebugUsedParam(Sender);
  6698. DebugAssert(FDDTargetControl == Sender);
  6699. FDDTargetControl = NULL;
  6700. }
  6701. //---------------------------------------------------------------------------
  6702. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave(int)
  6703. {
  6704. FileControlDDDragLeave(SessionsPageControl);
  6705. }
  6706. //---------------------------------------------------------------------------
  6707. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave(int)
  6708. {
  6709. FileControlDDDragLeave(QueueView3);
  6710. }
  6711. //---------------------------------------------------------------------------
  6712. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6713. {
  6714. DebugAssert(!IsSideLocalBrowser(Side));
  6715. DebugUsedParam(Side);
  6716. bool Edit = false;
  6717. TRemoteFile * File = NULL;
  6718. UnicodeString FileName;
  6719. UnicodeString PointTo;
  6720. bool SymbolicLink = true;
  6721. if (RemoteDirView->ItemFocused)
  6722. {
  6723. DebugAssert(RemoteDirView->ItemFocused->Data);
  6724. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6725. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6726. if (Edit)
  6727. {
  6728. FileName = File->FileName;
  6729. PointTo = File->LinkTo;
  6730. }
  6731. else if (!File->IsParentDirectory)
  6732. {
  6733. PointTo = File->FileName;
  6734. }
  6735. }
  6736. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6737. Terminal->IsCapable[fcHardLink]))
  6738. {
  6739. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6740. if (Edit)
  6741. {
  6742. DebugAssert(File->FileName == FileName);
  6743. int Params = dfNoRecursive;
  6744. Terminal->ExceptionOnFail = true;
  6745. try
  6746. {
  6747. Terminal->DeleteFile(L"", File, &Params);
  6748. }
  6749. __finally
  6750. {
  6751. Terminal->ExceptionOnFail = false;
  6752. }
  6753. }
  6754. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6755. }
  6756. }
  6757. //---------------------------------------------------------------------------
  6758. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6759. {
  6760. return
  6761. (IsSideLocalBrowser(Side) ||
  6762. (HasActiveTerminal() &&
  6763. Terminal->ResolvingSymlinks &&
  6764. Terminal->IsCapable[fcSymbolicLink]));
  6765. }
  6766. //---------------------------------------------------------------------------
  6767. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6768. {
  6769. return
  6770. !IsSideLocalBrowser(FCurrentSide) &&
  6771. (RemoteDirView->ItemFocused != NULL) &&
  6772. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6773. Terminal->SessionData->ResolveSymlinks;
  6774. }
  6775. //---------------------------------------------------------------------------
  6776. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6777. {
  6778. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6779. FForceExecution = true;
  6780. try
  6781. {
  6782. DirView(osCurrent)->ExecuteCurrentFile();
  6783. }
  6784. __finally
  6785. {
  6786. FForceExecution = false;
  6787. }
  6788. }
  6789. //---------------------------------------------------------------------------
  6790. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6791. {
  6792. TEditorData ExternalEditor;
  6793. ExternalEditor.Editor = edExternal;
  6794. bool Remember = false;
  6795. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6796. {
  6797. if (Remember)
  6798. {
  6799. TEditorList * EditorList = new TEditorList;
  6800. try
  6801. {
  6802. *EditorList = *WinConfiguration->EditorList;
  6803. bool Found = false;
  6804. int i = 0;
  6805. while (!Found && (i < EditorList->Count))
  6806. {
  6807. const TEditorPreferences * Editor = EditorList->Editors[i];
  6808. if ((Editor->Data->Editor == edExternal) &&
  6809. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6810. {
  6811. Found = true;
  6812. }
  6813. i++;
  6814. }
  6815. if (!Found)
  6816. {
  6817. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6818. WinConfiguration->EditorList = EditorList;
  6819. }
  6820. }
  6821. __finally
  6822. {
  6823. delete EditorList;
  6824. }
  6825. }
  6826. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6827. }
  6828. }
  6829. //---------------------------------------------------------------------------
  6830. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6831. {
  6832. if (FFileFindTerminal == ATerminal)
  6833. {
  6834. FFileFindTerminal = NULL;
  6835. HideFileFindDialog();
  6836. }
  6837. if (FClipboardTerminal == ATerminal)
  6838. {
  6839. ClipboardClear(); // implies ClipboardStop
  6840. }
  6841. if ((FOnSynchronizeAbort != NULL) &&
  6842. (ATerminal == FManagedSession))
  6843. {
  6844. FOnSynchronizeAbort(NULL);
  6845. }
  6846. }
  6847. //---------------------------------------------------------------------------
  6848. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6849. {
  6850. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  6851. DetachTerminal(Sender);
  6852. }
  6853. //---------------------------------------------------------------------------
  6854. void __fastcall TCustomScpExplorerForm::FileTerminalReplaced(
  6855. const UnicodeString FileName, TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6856. {
  6857. TManagedTerminal * ATerminal = static_cast<TManagedTerminal *>(Arg);
  6858. if (Data->Terminal == Terminal)
  6859. {
  6860. Data->Terminal = ATerminal;
  6861. }
  6862. }
  6863. //---------------------------------------------------------------------------
  6864. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6865. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6866. {
  6867. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6868. DebugAssert(Terminal != NULL);
  6869. if (Data->Terminal == Terminal)
  6870. {
  6871. Data->Terminal = NULL;
  6872. }
  6873. }
  6874. //---------------------------------------------------------------------------
  6875. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6876. {
  6877. return NULL;
  6878. }
  6879. //---------------------------------------------------------------------------
  6880. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6881. {
  6882. UpdateControls();
  6883. SessionColor = TColor(0);
  6884. UpdateRemotePathComboBox(false);
  6885. NeedSession(false);
  6886. }
  6887. //---------------------------------------------------------------------------
  6888. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6889. {
  6890. try
  6891. {
  6892. // Cache, as the login dialog can change its value.
  6893. // See also DoShow. Also see TTerminalManager::NewSession.
  6894. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6895. try
  6896. {
  6897. if (ShowLogin)
  6898. {
  6899. bool ReloadSessions = !Startup;
  6900. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6901. }
  6902. }
  6903. __finally
  6904. {
  6905. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6906. // (so there was no chance for the user to open any session yet)
  6907. if ((ShowLogin || !Startup) &&
  6908. !WinConfiguration->KeepOpenWhenNoSession &&
  6909. // this rules out the implicit Commander's local-local browser
  6910. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6911. {
  6912. TerminateApplication();
  6913. }
  6914. }
  6915. }
  6916. catch (EAbort &)
  6917. {
  6918. // swallow
  6919. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6920. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6921. }
  6922. }
  6923. //---------------------------------------------------------------------------
  6924. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6925. {
  6926. TTerminalManager * Manager = TTerminalManager::Instance();
  6927. DebugAssert(!Manager->Updating);
  6928. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6929. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6930. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6931. SessionsPageControl->LockDrawing();
  6932. try
  6933. {
  6934. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6935. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6936. {
  6937. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6938. }
  6939. for (int Index = 0; Index <= Manager->Count; Index++)
  6940. {
  6941. TThemeTabSheet * TabSheet;
  6942. if (Index >= SessionsPageControl->PageCount)
  6943. {
  6944. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6945. TabSheet->PageControl = SessionsPageControl;
  6946. }
  6947. else
  6948. {
  6949. TabSheet = SessionsPageControl->Pages[Index];
  6950. }
  6951. bool IsSessionTab = (Index < Manager->Count);
  6952. if (IsSessionTab)
  6953. {
  6954. TTerminal * Terminal = Manager->Sessions[Index];
  6955. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6956. TabSheet->CaptionTruncation = WinConfiguration->SessionTabCaptionTruncation ? tttEllipsis : tttNone;
  6957. UpdateSessionTab(TabSheet);
  6958. }
  6959. else
  6960. {
  6961. TabSheet->Tag = 0; // not really needed
  6962. TabSheet->Shadowed = false;
  6963. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6964. TabSheet->CaptionTruncation = GetNewTabTabCaptionTruncation();
  6965. // 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)
  6966. UpdateNewTabTab();
  6967. }
  6968. }
  6969. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6970. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6971. {
  6972. SessionsPageControl->UpdateTabsCaptionTruncation();
  6973. }
  6974. }
  6975. __finally
  6976. {
  6977. SessionsPageControl->UnlockDrawing();
  6978. }
  6979. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6980. }
  6981. //---------------------------------------------------------------------------
  6982. TThemeTabCaptionTruncation TCustomScpExplorerForm::GetNewTabTabCaptionTruncation()
  6983. {
  6984. return WinConfiguration->SessionTabCaptionTruncation ? tttNoText : tttNone;
  6985. }
  6986. //---------------------------------------------------------------------------
  6987. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6988. {
  6989. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6990. }
  6991. //---------------------------------------------------------------------------
  6992. TThemeTabSheet * TCustomScpExplorerForm::GetNewTabTab()
  6993. {
  6994. return SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6995. }
  6996. //---------------------------------------------------------------------------
  6997. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6998. {
  6999. TThemeTabSheet * TabSheet = GetNewTabTab();
  7000. UnicodeString TabCaption;
  7001. if (WinConfiguration->SelectiveToolbarText)
  7002. {
  7003. TabCaption = GetNewTabTabCaption();
  7004. }
  7005. TabSheet->BaseCaption = TabCaption;
  7006. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  7007. }
  7008. //---------------------------------------------------------------------------
  7009. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  7010. {
  7011. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  7012. }
  7013. //---------------------------------------------------------------------------
  7014. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  7015. {
  7016. DebugFail();
  7017. return UnicodeString();
  7018. }
  7019. //---------------------------------------------------------------------------
  7020. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  7021. {
  7022. if (DebugAlwaysTrue(TabSheet != NULL))
  7023. {
  7024. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  7025. if (DebugAlwaysTrue(ASession != NULL))
  7026. {
  7027. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  7028. if (ASession->LocalBrowser)
  7029. {
  7030. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  7031. }
  7032. else
  7033. {
  7034. TabSheet->ImageIndex = AddSessionColor(Color);
  7035. }
  7036. TTerminalManager * Manager = TTerminalManager::Instance();
  7037. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  7038. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  7039. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  7040. }
  7041. }
  7042. }
  7043. //---------------------------------------------------------------------------
  7044. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  7045. {
  7046. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  7047. }
  7048. //---------------------------------------------------------------------------
  7049. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  7050. {
  7051. DebugAssert(SessionsPageControl->ActivePage != NULL);
  7052. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  7053. bool Result = (Session != NULL);
  7054. if (Result)
  7055. {
  7056. TTerminalManager::Instance()->ActiveSession = Session;
  7057. }
  7058. else
  7059. {
  7060. try
  7061. {
  7062. NewTab();
  7063. }
  7064. __finally
  7065. {
  7066. SessionListChanged();
  7067. }
  7068. FSessionsPageControlNewTabTime = Now();
  7069. }
  7070. return Result;
  7071. }
  7072. //---------------------------------------------------------------------------
  7073. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  7074. {
  7075. TWindowLock Lock(this);
  7076. SessionTabSwitched();
  7077. }
  7078. //---------------------------------------------------------------------------
  7079. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  7080. {
  7081. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  7082. DebugAssert(TransferList != NULL);
  7083. UnicodeString Name;
  7084. if (TransferList->ItemIndex <= 0)
  7085. {
  7086. Name = L"";
  7087. }
  7088. else
  7089. {
  7090. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  7091. }
  7092. if (FCopyParamAutoSelected.IsEmpty())
  7093. {
  7094. // if previous preset was not autoselected, make new preset the "default"
  7095. FCopyParamDefault = Name;
  7096. }
  7097. GUIConfiguration->CopyParamCurrent = Name;
  7098. }
  7099. //---------------------------------------------------------------------------
  7100. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  7101. {
  7102. // sanity check
  7103. bool ExistingPreset =
  7104. (FTransferListHoverIndex >= 0) &&
  7105. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  7106. DebugAssert(ExistingPreset);
  7107. if (ExistingPreset)
  7108. {
  7109. HDC DC = GetDC(0);
  7110. TCanvas * Canvas = new TCanvas();
  7111. try
  7112. {
  7113. Canvas->Handle = DC;
  7114. Canvas->Font = GetToolbarFont(this);
  7115. UnicodeString Name;
  7116. if (FTransferListHoverIndex == 0)
  7117. {
  7118. Name = L"";
  7119. }
  7120. else
  7121. {
  7122. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  7123. }
  7124. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  7125. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  7126. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  7127. static_cast<TObject*>(GetComponent(fcTransferList)));
  7128. UnicodeString InfoStr =
  7129. GUIConfiguration->CopyParamPreset[Name].
  7130. GetInfoStr(L"; ",
  7131. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  7132. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  7133. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  7134. {
  7135. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  7136. {
  7137. InfoStr.SetLength(InfoStr.Length() - 1);
  7138. }
  7139. InfoStr += Ellipsis;
  7140. }
  7141. // UpdateCaption does not cause invalidation of whole submenu, while
  7142. // setting Caption property does.
  7143. // also it probably does not resize the label, even if necessary
  7144. // (we do not want that anyway)
  7145. TransferLabel->UpdateCaption(InfoStr);
  7146. }
  7147. __finally
  7148. {
  7149. Canvas->Handle = NULL;
  7150. ReleaseDC(0, DC);
  7151. delete Canvas;
  7152. }
  7153. }
  7154. }
  7155. //---------------------------------------------------------------------------
  7156. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  7157. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  7158. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  7159. {
  7160. if (FTransferListHoverIndex != AHoverIndex)
  7161. {
  7162. FTransferListHoverIndex = AHoverIndex;
  7163. UpdateTransferLabel();
  7164. }
  7165. }
  7166. //---------------------------------------------------------------------------
  7167. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  7168. {
  7169. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  7170. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  7171. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  7172. {
  7173. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  7174. {
  7175. if (!NonVisualDataModule->Busy)
  7176. {
  7177. OpenDirectory(GetSide(osCurrent));
  7178. }
  7179. Message.Result = 1;
  7180. }
  7181. else
  7182. {
  7183. TForm::Dispatch(&Message);
  7184. }
  7185. }
  7186. else
  7187. {
  7188. TForm::Dispatch(&Message);
  7189. }
  7190. }
  7191. //---------------------------------------------------------------------------
  7192. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  7193. {
  7194. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  7195. if ((FIgnoreNextDialogChar != 0) &&
  7196. (towupper(Message.CharCode) == towupper(FIgnoreNextDialogChar)))
  7197. {
  7198. Message.Result = 1;
  7199. }
  7200. else
  7201. {
  7202. TForm::Dispatch(&Message);
  7203. }
  7204. FIgnoreNextDialogChar = 0;
  7205. }
  7206. //---------------------------------------------------------------------------
  7207. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  7208. {
  7209. // The four low-order bits are used internally by Windows
  7210. unsigned int Cmd = (Message.WParam & 0xFFF0);
  7211. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  7212. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  7213. // 61730 - restore thru double click - undocumented
  7214. // 61490 - maximize thru double click - undocumented
  7215. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  7216. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  7217. {
  7218. SysResizing(Cmd);
  7219. }
  7220. TForm::Dispatch(&Message);
  7221. }
  7222. //---------------------------------------------------------------------------
  7223. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  7224. {
  7225. AppLog(L"Windows session end query");
  7226. // We were actually never able to make ENDSESSION_CRITICAL happen.
  7227. // Also there no point returning TRUE as we are not able to
  7228. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  7229. // Hence the process termination might be safer :)
  7230. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  7231. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  7232. {
  7233. Message.Result = FALSE;
  7234. }
  7235. else
  7236. {
  7237. Message.Result = TRUE;
  7238. }
  7239. // Do not call default handling as that triggers OnCloseQuery,
  7240. // where our implementation will popup confirmation dialogs, what we do not want,
  7241. // as per Vista guidelines:
  7242. // https://learn.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  7243. }
  7244. //---------------------------------------------------------------------------
  7245. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  7246. {
  7247. AppLog(L"Windows session end");
  7248. if (Message.EndSession && IsApplicationMinimized())
  7249. {
  7250. // WORKAROUND
  7251. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  7252. // when it receives WM_ENDSESSION,
  7253. // but that sometimes (particularly when application is minimized) cause crashes.
  7254. // Crash popups message that blocks log off.
  7255. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  7256. // so we call ExitProcess() immediately, not even trying to cleanup.
  7257. // It still causes beep, so there's likely some popup, but it does not block
  7258. // log off.
  7259. ExitProcess(0);
  7260. }
  7261. TForm::Dispatch(&Message);
  7262. }
  7263. #ifdef _DEBUG
  7264. //---------------------------------------------------------------------------
  7265. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  7266. {
  7267. TForm::Dispatch(&Message);
  7268. }
  7269. #endif
  7270. //---------------------------------------------------------------------------
  7271. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  7272. {
  7273. }
  7274. //---------------------------------------------------------------------------
  7275. void __fastcall TCustomScpExplorerForm::DoShow()
  7276. {
  7277. // Called from TCustomScpExplorerForm::CMShowingChanged
  7278. // only now are the controls resized finally, so the size constraints
  7279. // will not conflict with possibly very small window size
  7280. RestoreFormParams();
  7281. // This indicates that the form was rescaled after parameters were loaded and
  7282. // as most probably the parameters were actually saved with the current form scaling
  7283. // (windows was the last time likely closed on the same monitor, where it is starting now),
  7284. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  7285. // and no rounding problems will occur.
  7286. // See also RestoreForm.
  7287. // (we should be safe to call RestoreParams unconditionally)
  7288. if (PixelsPerInch != Screen->PixelsPerInch)
  7289. {
  7290. RestoreParams();
  7291. }
  7292. FixControlsPlacement();
  7293. if (Position == poDefaultPosOnly)
  7294. {
  7295. CutFormToDesktop(this);
  7296. }
  7297. TForm::DoShow();
  7298. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  7299. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  7300. // This was previously in NeedSession, where other related code is.
  7301. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  7302. // unpleasantly shows briefly.
  7303. if ((ManagedSession == NULL) &&
  7304. !WinConfiguration->ShowLoginWhenNoSession &&
  7305. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  7306. // This detects if workspace was saved the last time the main widow was closed
  7307. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  7308. {
  7309. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  7310. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  7311. }
  7312. // Before main window displays
  7313. if (ManagedSession == NULL)
  7314. {
  7315. StartingWithoutSession();
  7316. }
  7317. UpdatePixelsPerInchMainWindowCounter();
  7318. FShowing = true;
  7319. }
  7320. //---------------------------------------------------------------------------
  7321. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  7322. {
  7323. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  7324. }
  7325. //---------------------------------------------------------------------------
  7326. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  7327. {
  7328. SessionListChanged();
  7329. InitStatusBar();
  7330. UpdateControls();
  7331. }
  7332. //---------------------------------------------------------------------------
  7333. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  7334. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  7335. TNotifyEvent OnBalloonClick, TObject * UserData)
  7336. {
  7337. bool Do;
  7338. UnicodeString Message;
  7339. if (E == NULL)
  7340. {
  7341. Message = Str;
  7342. Do = true;
  7343. }
  7344. else
  7345. {
  7346. Do = ExceptionMessage(E, Message);
  7347. }
  7348. if (Do && WinConfiguration->BalloonNotifications)
  7349. {
  7350. UnicodeString Title;
  7351. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  7352. {
  7353. Message = TrimLeft(Message);
  7354. }
  7355. else
  7356. {
  7357. Message = UnformatMessage(Message);
  7358. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  7359. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  7360. Title = LoadResourceString(Captions[Type]);
  7361. if (Terminal != NULL)
  7362. {
  7363. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal->PrimaryTerminal));
  7364. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  7365. }
  7366. }
  7367. if (Seconds == 0)
  7368. {
  7369. Seconds = WinConfiguration->NotificationsTimeout;
  7370. }
  7371. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  7372. }
  7373. }
  7374. //---------------------------------------------------------------------------
  7375. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  7376. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  7377. UnicodeString HelpKeyword, const TMessageParams * Params,
  7378. TTerminal * Terminal)
  7379. {
  7380. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  7381. (FOnFeedSynchronizeError != NULL)) &&
  7382. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  7383. {
  7384. if (FOnFeedSynchronizeError != NULL)
  7385. {
  7386. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  7387. }
  7388. else
  7389. {
  7390. DebugAssert(FErrorList != NULL);
  7391. TStringList * MoreMessagesCopy = NULL;
  7392. if (MoreMessages)
  7393. {
  7394. MoreMessagesCopy = new TStringList();
  7395. MoreMessagesCopy->Assign(MoreMessages);
  7396. }
  7397. FErrorList->AddObject(Message, MoreMessagesCopy);
  7398. }
  7399. PopupTrayBalloon(Terminal, Message, Type);
  7400. return ContinueAnswer(Answers);
  7401. }
  7402. else
  7403. {
  7404. bool UseBalloon = FTrayIcon->Visible;
  7405. if (UseBalloon)
  7406. {
  7407. PopupTrayBalloon(Terminal, Message, Type);
  7408. }
  7409. unsigned int Result;
  7410. try
  7411. {
  7412. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  7413. }
  7414. __finally
  7415. {
  7416. // cancel only balloon we popped up, otherwise we may cancel notification
  7417. // balloon that was there before the message dialog
  7418. // (such as "dd confirmation opt in balloon)
  7419. if (UseBalloon)
  7420. {
  7421. FTrayIcon->CancelBalloon();
  7422. }
  7423. }
  7424. return Result;
  7425. }
  7426. }
  7427. //---------------------------------------------------------------------------
  7428. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  7429. TTerminal * Terminal, Exception * E)
  7430. {
  7431. if (FTrayIcon->Visible)
  7432. {
  7433. PopupTrayBalloon(Terminal, L"", qtError, E);
  7434. }
  7435. // particularly prevent opening new session from jump list,
  7436. // while exception is shown
  7437. NonVisualDataModule->StartBusy();
  7438. try
  7439. {
  7440. ShowExtendedExceptionEx(Terminal, E);
  7441. }
  7442. __finally
  7443. {
  7444. NonVisualDataModule->EndBusy();
  7445. FTrayIcon->CancelBalloon();
  7446. }
  7447. }
  7448. //---------------------------------------------------------------------------
  7449. void __fastcall TCustomScpExplorerForm::SessionReady()
  7450. {
  7451. InitStatusBar();
  7452. // cannot rely on active page being page for active terminal,
  7453. // as it can happen that active page is the "new session" page
  7454. // (e.g. when reconnecting active terminal, while login dialog
  7455. // invoked from "new session" page is modal)
  7456. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  7457. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  7458. }
  7459. //---------------------------------------------------------------------------
  7460. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  7461. TTerminal * Terminal, Exception * E)
  7462. {
  7463. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  7464. if (!Terminal->Active)
  7465. {
  7466. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  7467. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  7468. {
  7469. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  7470. }
  7471. }
  7472. }
  7473. //---------------------------------------------------------------------------
  7474. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  7475. UnicodeString Message, TQueryType Type,
  7476. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  7477. {
  7478. if ((E == NULL) ||
  7479. ExceptionMessage(E, Message))
  7480. {
  7481. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  7482. if (Important)
  7483. {
  7484. Seconds *= 5;
  7485. }
  7486. UnicodeString NoteMessage(UnformatMessage(Message));
  7487. if (Terminal != NULL)
  7488. {
  7489. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7490. NoteMessage = FORMAT(L"%s: %s",
  7491. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  7492. }
  7493. if (WinConfiguration->BalloonNotifications)
  7494. {
  7495. AddNote(NoteMessage);
  7496. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  7497. }
  7498. else
  7499. {
  7500. FlashOnBackground();
  7501. PostNote(NoteMessage, Seconds, OnClick, UserData);
  7502. }
  7503. }
  7504. }
  7505. //---------------------------------------------------------------------------
  7506. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7507. {
  7508. RestoreApp();
  7509. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7510. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7511. !NonVisualDataModule->Busy)
  7512. {
  7513. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7514. TTerminalManager::Instance()->ActiveSession = Terminal;
  7515. if (!ComponentVisible[fcQueueView])
  7516. {
  7517. ToggleQueueVisibility();
  7518. GoToQueue();
  7519. }
  7520. }
  7521. }
  7522. //---------------------------------------------------------------------------
  7523. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7524. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7525. {
  7526. UnicodeString Message;
  7527. TNotifyEvent OnClick = NULL;
  7528. TObject * UserData = NULL;
  7529. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7530. switch (Event)
  7531. {
  7532. case qeEmptyButMonitored:
  7533. if ((ATerminal != Terminal) || QueueInvisible)
  7534. {
  7535. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7536. OnClick = QueueEmptyNoteClicked;
  7537. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7538. TerminalNoteData->Terminal = ATerminal;
  7539. UserData = TerminalNoteData;
  7540. }
  7541. break;
  7542. case qeEmpty:
  7543. OperationComplete(ATerminal->QueueOperationStart);
  7544. break;
  7545. case qePendingUserAction:
  7546. if ((ATerminal != Terminal) ||
  7547. (QueueInvisible && !IsQueueAutoPopup()))
  7548. {
  7549. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7550. }
  7551. break;
  7552. default:
  7553. DebugFail();
  7554. }
  7555. if (!Message.IsEmpty())
  7556. {
  7557. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7558. }
  7559. }
  7560. //---------------------------------------------------------------------------
  7561. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7562. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7563. {
  7564. if (FDDExtMapFile != NULL)
  7565. {
  7566. CloseHandle(FDDExtMapFile);
  7567. FDDExtMapFile = NULL;
  7568. }
  7569. if (WinConfiguration->DDFakeFile)
  7570. {
  7571. DDFakeFileInitDrag(FileList, Created);
  7572. }
  7573. }
  7574. //---------------------------------------------------------------------------
  7575. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7576. {
  7577. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7578. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7579. {
  7580. FFakeFileDropTarget = FileName;
  7581. }
  7582. }
  7583. //---------------------------------------------------------------------------
  7584. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7585. {
  7586. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7587. if (!ForceDirectories(ApiPath(Result)))
  7588. {
  7589. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7590. }
  7591. FileSetAttr(ApiPath(Result), faHidden);
  7592. return Result;
  7593. }
  7594. //---------------------------------------------------------------------------
  7595. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7596. bool & Created)
  7597. {
  7598. FFakeFileDropTarget = UnicodeString();
  7599. FDragFakeDirectory = CreateFakeTransferDirectory();
  7600. FileList->AddItem(NULL, FDragFakeDirectory);
  7601. Created = true;
  7602. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7603. {
  7604. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7605. }
  7606. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7607. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7608. {
  7609. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7610. TDragExtCommStruct* CommStruct;
  7611. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7612. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7613. DebugAssert(CommStruct != NULL);
  7614. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7615. CommStruct->Dragging = true;
  7616. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7617. LENOF(CommStruct->DropDest));
  7618. NULL_TERMINATE(CommStruct->DropDest);
  7619. UnmapViewOfFile(CommStruct);
  7620. }
  7621. }
  7622. //---------------------------------------------------------------------------
  7623. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7624. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7625. {
  7626. bool Result;
  7627. DebugAssert(!IsLocalBrowserMode());
  7628. if (Sender == RemoteDirView)
  7629. {
  7630. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7631. }
  7632. else
  7633. {
  7634. DebugAssert(Sender == RemoteDriveView);
  7635. TStrings * FileList = RemoteDriveView->DragFileList();
  7636. try
  7637. {
  7638. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7639. }
  7640. __finally
  7641. {
  7642. delete FileList;
  7643. }
  7644. }
  7645. if (FDDTargetControl == RemoteDriveView)
  7646. {
  7647. RemoteDriveView->UpdateDropTarget();
  7648. }
  7649. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7650. // or from tree view to dir view.
  7651. // foMove happens when file/dir is dragged from remote tree view outside of
  7652. // application via dragex
  7653. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7654. (DropSourceControl == RemoteDriveView))
  7655. {
  7656. RemoteDriveView->UpdateDropSource();
  7657. }
  7658. return Result;
  7659. }
  7660. //---------------------------------------------------------------------------
  7661. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7662. {
  7663. DebugAssert(!IsLocalBrowserMode());
  7664. // This also handles drops of remote files to queue.
  7665. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7666. SAFE_DESTROY(FDDFileList);
  7667. if (!FFakeFileDropTarget.IsEmpty())
  7668. {
  7669. RemoveDir(ApiPath(FFakeFileDropTarget));
  7670. }
  7671. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7672. {
  7673. try
  7674. {
  7675. TDragResult DDResult = (Sender == RemoteDirView) ?
  7676. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7677. // Focus is moved to the target application,
  7678. // but as we are going to present the UI, we need to steal the focus back.
  7679. // This most likely won't work though (windows does not allow application
  7680. // to steal focus most of the time)
  7681. Application->BringToFront();
  7682. // On older version of Windows we never got drMove here, see also comment below.
  7683. // On Windows 10, we get the "move".
  7684. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7685. {
  7686. UnicodeString TargetDirectory;
  7687. TFileOperation Operation;
  7688. // drInvalid may mean drMove, see comment below
  7689. switch (DDResult)
  7690. {
  7691. case drCopy:
  7692. Operation = foCopy;
  7693. break;
  7694. case drMove:
  7695. Operation = foMove;
  7696. break;
  7697. default:
  7698. DebugFail();
  7699. case drInvalid:
  7700. // prefer "copy" for safety
  7701. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7702. break;
  7703. }
  7704. TTransferOperationParam Param;
  7705. UnicodeString CounterName;
  7706. bool ForceQueue;
  7707. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7708. {
  7709. // we get drInvalid both if d&d was intercepted by ddext,
  7710. // and when users drops on no-drop location.
  7711. // we tell the difference by existence of response from ddext,
  7712. // so we ignore absence of response in this case
  7713. if (DDResult != drInvalid)
  7714. {
  7715. // here we know that the extension is installed,
  7716. // as it is checked as soon as drag&drop starts from
  7717. // RemoteFileControlDDCreateDragFileList
  7718. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7719. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7720. {
  7721. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7722. }
  7723. else
  7724. {
  7725. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7726. }
  7727. }
  7728. }
  7729. else
  7730. {
  7731. // download using fake file
  7732. Param.Temp = false;
  7733. Param.DragDrop = true;
  7734. if (ForceQueue)
  7735. {
  7736. Param.Queue = asOn;
  7737. }
  7738. if (Sender == RemoteDirView)
  7739. {
  7740. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7741. }
  7742. if (RemoteFileControlFileOperation(Sender, Operation,
  7743. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7744. {
  7745. Configuration->Usage->Inc(CounterName);
  7746. }
  7747. }
  7748. }
  7749. }
  7750. __finally
  7751. {
  7752. CloseHandle(FDDExtMapFile);
  7753. FDDExtMapFile = NULL;
  7754. delete FDragFakeMonitors;
  7755. FDragFakeMonitors = NULL;
  7756. RemoveDir(ApiPath(FDragFakeDirectory));
  7757. FDragFakeDirectory = L"";
  7758. FFakeFileDropTarget = UnicodeString();
  7759. }
  7760. }
  7761. if (FDragDropSshTerminate.get() != NULL)
  7762. {
  7763. FDragDropSshTerminate->Rethrow();
  7764. }
  7765. }
  7766. //---------------------------------------------------------------------------
  7767. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7768. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7769. {
  7770. // Remember drop effect so we know (when user drops files), if we copy or move
  7771. FLastDropEffect = dwEffect;
  7772. }
  7773. //---------------------------------------------------------------------------
  7774. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7775. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7776. {
  7777. bool Result;
  7778. if (FDDTargetControl == QueueView3)
  7779. {
  7780. Directory = DefaultDownloadTargetDirectory();
  7781. Result = true;
  7782. CounterName = L"DownloadsDragDropQueue";
  7783. ForceQueue = true;
  7784. }
  7785. else if (!FFakeFileDropTarget.IsEmpty())
  7786. {
  7787. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7788. Result = true;
  7789. ForceQueue = false;
  7790. CounterName = L"DownloadsDragDropFakeFile";
  7791. }
  7792. else
  7793. {
  7794. ForceQueue = false;
  7795. Enabled = false;
  7796. try
  7797. {
  7798. int Timer = 0;
  7799. Result = false;
  7800. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7801. {
  7802. {
  7803. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7804. TDragExtCommStruct* CommStruct;
  7805. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7806. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7807. DebugAssert(CommStruct != NULL);
  7808. Result = !CommStruct->Dragging;
  7809. if (Result)
  7810. {
  7811. Directory = ExtractFilePath(CommStruct->DropDest);
  7812. CounterName = L"DownloadsDragDropExternalExt";
  7813. }
  7814. UnmapViewOfFile(CommStruct);
  7815. }
  7816. if (!Result)
  7817. {
  7818. Sleep(50);
  7819. Timer += 50;
  7820. Application->ProcessMessages();
  7821. }
  7822. }
  7823. }
  7824. __finally
  7825. {
  7826. Enabled = true;
  7827. }
  7828. }
  7829. return Result;
  7830. }
  7831. //---------------------------------------------------------------------------
  7832. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7833. const UnicodeString TempDir, int SecDelay)
  7834. {
  7835. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7836. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7837. if (FDelayedDeletionTimer == NULL)
  7838. {
  7839. DebugAssert(HandleAllocated());
  7840. FDelayedDeletionTimer = new TTimer(this);
  7841. FDelayedDeletionTimer->Interval = 10000;
  7842. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7843. }
  7844. else
  7845. {
  7846. FDelayedDeletionTimer->Enabled = true;
  7847. }
  7848. }
  7849. //---------------------------------------------------------------------------
  7850. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7851. {
  7852. DebugAssert(FDelayedDeletionList != NULL);
  7853. TDateTime N = Now();
  7854. TDateTime Alert;
  7855. UnicodeString Directory;
  7856. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7857. {
  7858. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7859. if ((N >= Alert) || (Sender == NULL))
  7860. {
  7861. Directory = FDelayedDeletionList->Strings[Index];
  7862. if (RecursiveDeleteFile(ExcludeTrailingBackslash(Directory)))
  7863. {
  7864. FDelayedDeletionList->Delete(Index);
  7865. }
  7866. }
  7867. }
  7868. if (FDelayedDeletionList->Count == 0)
  7869. {
  7870. FDelayedDeletionTimer->Enabled = false;
  7871. }
  7872. }
  7873. //---------------------------------------------------------------------------
  7874. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7875. {
  7876. DebugAssert(!IsLocalBrowserMode());
  7877. if (IsFileControl(FDDTargetControl, osRemote) ||
  7878. (FDDTargetControl == SessionsPageControl))
  7879. {
  7880. TManagedTerminal * TargetTerminal = NULL;
  7881. TFileOperation Operation = foNone;
  7882. if (FDDTargetControl == SessionsPageControl)
  7883. {
  7884. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7885. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7886. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7887. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7888. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7889. {
  7890. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7891. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7892. {
  7893. Operation = foRemoteMove;
  7894. }
  7895. else
  7896. {
  7897. Operation = foRemoteCopy;
  7898. }
  7899. }
  7900. }
  7901. else
  7902. {
  7903. if (FLastDropEffect == DROPEFFECT_MOVE)
  7904. {
  7905. Operation = foRemoteMove;
  7906. }
  7907. else if (FLastDropEffect == DROPEFFECT_COPY)
  7908. {
  7909. Operation = foRemoteCopy;
  7910. }
  7911. }
  7912. if (Operation != foNone)
  7913. {
  7914. RemoteFileControlFileOperation(DropSourceControl,
  7915. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7916. }
  7917. // abort drag&drop
  7918. Abort();
  7919. }
  7920. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7921. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7922. (FDDTargetControl != QueueView3))
  7923. {
  7924. DebugAssert(!FDragTempDir.IsEmpty());
  7925. TTransferType Type;
  7926. UnicodeString TempDir = FDragTempDir;
  7927. // We clear FDragTempDir before calling
  7928. // just in case it fail (raises exception)
  7929. FDragTempDir = L"";
  7930. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7931. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7932. // empty directory parameter means temp directory -> don't display it!
  7933. UnicodeString TargetDir = L"";
  7934. int Options = 0;
  7935. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7936. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7937. {
  7938. Abort();
  7939. }
  7940. // TargetDir is set when dropped on local file control
  7941. // (this was workaround for legacy dirview event handling, now it should be
  7942. // made prettier)
  7943. if (TargetDir.IsEmpty())
  7944. {
  7945. TargetDir = TempDir;
  7946. if (ForceDirectories(ApiPath(TargetDir)))
  7947. {
  7948. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7949. FPendingTempSpaceWarn = true;
  7950. try
  7951. {
  7952. FDragDropOperation = true;
  7953. int Params = cpTemporary |
  7954. (Type == ttMove ? cpDelete : 0);
  7955. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7956. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7957. }
  7958. __finally
  7959. {
  7960. FDragDropOperation = false;
  7961. FPendingTempSpaceWarn = false;
  7962. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7963. }
  7964. }
  7965. else
  7966. {
  7967. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7968. }
  7969. }
  7970. }
  7971. }
  7972. //---------------------------------------------------------------------------
  7973. void __fastcall TCustomScpExplorerForm::DDDownload(
  7974. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  7975. {
  7976. DebugAssert(!IsLocalBrowserMode());
  7977. TAutoBatch AutoBatch(this);
  7978. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  7979. UpdateCopyParamCounters(*CopyParam);
  7980. FMoveToQueue = false;
  7981. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7982. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7983. {
  7984. RemoteDriveView->UpdateDropSource();
  7985. }
  7986. }
  7987. //---------------------------------------------------------------------------
  7988. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7989. {
  7990. public:
  7991. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7992. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7993. {
  7994. }
  7995. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7996. {
  7997. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7998. TDataObjectFilesEx::AllowData(FormatEtc);
  7999. }
  8000. };
  8001. //---------------------------------------------------------------------------
  8002. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  8003. TObject * Sender, TDataObject *& DataObject)
  8004. {
  8005. if (FDDExtMapFile != NULL)
  8006. {
  8007. TFileList * FileList = DragDropFiles(Sender)->FileList;
  8008. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  8009. {
  8010. Abort();
  8011. }
  8012. if (FileList->Count > 0)
  8013. {
  8014. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  8015. if (!FilesObject->IsValid(true, true))
  8016. {
  8017. FilesObject->_Release();
  8018. }
  8019. else
  8020. {
  8021. DataObject = FilesObject;
  8022. }
  8023. }
  8024. }
  8025. }
  8026. //---------------------------------------------------------------------------
  8027. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  8028. {
  8029. DebugFail();
  8030. }
  8031. //---------------------------------------------------------------------------
  8032. void __fastcall TCustomScpExplorerForm::GoToTree()
  8033. {
  8034. ComponentVisible[fcRemoteTree] = true;
  8035. DriveView(osRemote)->SetFocus();
  8036. }
  8037. //---------------------------------------------------------------------------
  8038. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  8039. TPanelExport Export)
  8040. {
  8041. TCustomDirView * DirView = this->DirView(Side);
  8042. std::unique_ptr<TStrings> ExportData(new TStringList());
  8043. switch (Export)
  8044. {
  8045. case pePath:
  8046. ExportData->Add(DirView->PathName);
  8047. break;
  8048. case peFileList:
  8049. case peFullFileList:
  8050. {
  8051. bool FullPath = (Export == peFullFileList);
  8052. DirView->CreateFileList(false, FullPath, ExportData.get());
  8053. for (int Index = 0; Index < ExportData->Count; Index++)
  8054. {
  8055. if (ExportData->Strings[Index].Pos(L" ") > 0)
  8056. {
  8057. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  8058. }
  8059. }
  8060. }
  8061. break;
  8062. default:
  8063. DebugFail();
  8064. }
  8065. return ExportData.release();
  8066. }
  8067. //---------------------------------------------------------------------------
  8068. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  8069. TPanelExport Export, TPanelExportDestination Destination)
  8070. {
  8071. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  8072. PanelExportStore(Side, Export, Destination, ExportData.get());
  8073. }
  8074. //---------------------------------------------------------------------------
  8075. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  8076. TPanelExport /*Export*/, TPanelExportDestination Destination,
  8077. TStrings * ExportData)
  8078. {
  8079. if (Destination == pedClipboard)
  8080. {
  8081. TInstantOperationVisualizer Visualizer;
  8082. CopyToClipboard(ExportData);
  8083. }
  8084. else
  8085. {
  8086. DebugFail();
  8087. }
  8088. }
  8089. //---------------------------------------------------------------------------
  8090. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  8091. {
  8092. TCustomDirView * DirView = this->DirView(Side);
  8093. UnicodeString Mask = DirView->Mask;
  8094. if (DoFilterMaskDialog(DirView, Mask))
  8095. {
  8096. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  8097. Configuration->Usage->Inc(L"Filters");
  8098. }
  8099. }
  8100. //---------------------------------------------------------------------------
  8101. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  8102. {
  8103. return FQueueController->DefaultOperation();
  8104. }
  8105. //---------------------------------------------------------------------------
  8106. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  8107. TQueueOperation Operation, void ** Param)
  8108. {
  8109. switch (Operation)
  8110. {
  8111. case qoPreferences:
  8112. return true;
  8113. case qoGoTo:
  8114. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  8115. case qoOnceEmpty:
  8116. return IsAnythingQueued();
  8117. default:
  8118. return FQueueController->AllowOperation(Operation, Param);
  8119. }
  8120. }
  8121. //---------------------------------------------------------------------------
  8122. void __fastcall TCustomScpExplorerForm::GoToQueue()
  8123. {
  8124. if (DebugAlwaysTrue(QueueView3->Visible))
  8125. {
  8126. QueueView3->SetFocus();
  8127. }
  8128. }
  8129. //---------------------------------------------------------------------------
  8130. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  8131. TQueueOperation Operation, void * Param)
  8132. {
  8133. if (Operation == qoGoTo)
  8134. {
  8135. GoToQueue();
  8136. }
  8137. else if (Operation == qoPreferences)
  8138. {
  8139. PreferencesDialog(pmQueue);
  8140. }
  8141. else
  8142. {
  8143. FQueueController->ExecuteOperation(Operation, Param);
  8144. }
  8145. }
  8146. //---------------------------------------------------------------------------
  8147. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  8148. {
  8149. return (Queue != NULL) && Queue->Enabled;
  8150. }
  8151. //---------------------------------------------------------------------------
  8152. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  8153. {
  8154. DebugAssert(Queue != NULL);
  8155. if (Queue != NULL)
  8156. {
  8157. Queue->Enabled = !Queue->Enabled;
  8158. }
  8159. }
  8160. //---------------------------------------------------------------------------
  8161. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  8162. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  8163. {
  8164. FQueueActedItem = QueueView3->ItemFocused;
  8165. }
  8166. //---------------------------------------------------------------------------
  8167. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  8168. {
  8169. return TopDock->Height + (MessageDock->Visible ? MessageDock->Height : 0) + QueueSplitter->Height;
  8170. }
  8171. //---------------------------------------------------------------------------
  8172. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  8173. {
  8174. return
  8175. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  8176. (QueueDock->Visible ? QueueDock->Height : 0) +
  8177. (QueueLabel->Visible ? QueueLabel->Height : 0);
  8178. }
  8179. //---------------------------------------------------------------------------
  8180. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  8181. {
  8182. return ScaleByTextHeight(this, 48);
  8183. }
  8184. //---------------------------------------------------------------------------
  8185. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  8186. TObject * /*Sender*/, int & NewSize, bool & Accept)
  8187. {
  8188. // when queue is hidden by double-clicking splitter, stray attempt to
  8189. // resize the panel with strange value arrives, make sure it is ignored
  8190. if (ComponentVisible[fcQueueView])
  8191. {
  8192. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  8193. if (NewSize > MaxHeightLimit)
  8194. {
  8195. NewSize = MaxHeightLimit;
  8196. }
  8197. int MinHeightLimit =
  8198. GetStaticQueuePanelComponentsHeight() +
  8199. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  8200. GetMinQueueViewHeight();
  8201. if (NewSize < MinHeightLimit)
  8202. {
  8203. NewSize = MinHeightLimit;
  8204. }
  8205. }
  8206. else
  8207. {
  8208. Accept = false;
  8209. }
  8210. }
  8211. //---------------------------------------------------------------------------
  8212. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  8213. {
  8214. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  8215. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  8216. if (QueueViewHeight < GetMinQueueViewHeight())
  8217. {
  8218. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  8219. }
  8220. }
  8221. //---------------------------------------------------------------------------
  8222. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  8223. TDragObject *& /*DragObject*/)
  8224. {
  8225. FQueueActedItem = QueueView3->ItemFocused;
  8226. FQueueScrollOnDragOver->StartDrag();
  8227. }
  8228. //---------------------------------------------------------------------------
  8229. void __fastcall TCustomScpExplorerForm::QueueView3EndDrag(
  8230. TObject *, TObject * DebugUsedArg(Target), int DebugUsedArg(X), int DebugUsedArg(Y))
  8231. {
  8232. FQueueScrollOnDragOver->EndDrag();
  8233. }
  8234. //---------------------------------------------------------------------------
  8235. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  8236. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  8237. {
  8238. Accept = true;
  8239. if (Source == QueueView3)
  8240. {
  8241. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  8242. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  8243. if (Accept)
  8244. {
  8245. TQueueItemProxy * QueueItem;
  8246. TQueueItemProxy * DestQueueItem;
  8247. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  8248. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  8249. Accept = (QueueItem != DestQueueItem) &&
  8250. (QueueItem->Status == TQueueItem::qsPending) &&
  8251. (DestQueueItem->Status == TQueueItem::qsPending);
  8252. }
  8253. FQueueScrollOnDragOver->DragOver(TPoint(X, Y));
  8254. }
  8255. }
  8256. //---------------------------------------------------------------------------
  8257. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  8258. TObject * /*Source*/, int /*X*/, int /*Y*/)
  8259. {
  8260. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  8261. {
  8262. TQueueItemProxy * QueueItem;
  8263. TQueueItemProxy * DestQueueItem;
  8264. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  8265. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  8266. QueueItem->Move(DestQueueItem);
  8267. }
  8268. }
  8269. //---------------------------------------------------------------------------
  8270. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  8271. {
  8272. if ((QueueView3->ItemFocused == NULL) &&
  8273. (QueueView3->Items->Count > 0))
  8274. {
  8275. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  8276. }
  8277. QueueLabelUpdateStatus();
  8278. }
  8279. //---------------------------------------------------------------------------
  8280. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  8281. {
  8282. QueueLabelUpdateStatus();
  8283. }
  8284. //---------------------------------------------------------------------------
  8285. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  8286. {
  8287. QueueLabel->UpdateStatus();
  8288. }
  8289. //---------------------------------------------------------------------------
  8290. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  8291. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  8292. {
  8293. if (Selected)
  8294. {
  8295. NonVisualDataModule->UpdateNonVisibleActions();
  8296. }
  8297. }
  8298. //---------------------------------------------------------------------------
  8299. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  8300. {
  8301. TDragDropFilesEx * Result = NULL;
  8302. if (Sender == SessionsPageControl)
  8303. {
  8304. Result = FSessionsDragDropFilesEx;
  8305. }
  8306. else if (Sender == QueueView3)
  8307. {
  8308. Result = FQueueDragDropFilesEx;
  8309. }
  8310. else
  8311. {
  8312. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8313. if (DirView != NULL)
  8314. {
  8315. Result = DirView->DragDropFilesEx;
  8316. }
  8317. else
  8318. {
  8319. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  8320. if (DriveView != NULL)
  8321. {
  8322. Result = DriveView->DragDropFilesEx;
  8323. }
  8324. }
  8325. }
  8326. DebugAssert(Result != NULL);
  8327. return Result;
  8328. }
  8329. //---------------------------------------------------------------------------
  8330. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  8331. {
  8332. return (DView->SelCount == DView->FilesCount);
  8333. }
  8334. //---------------------------------------------------------------------------
  8335. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  8336. {
  8337. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  8338. }
  8339. //---------------------------------------------------------------------------
  8340. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  8341. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  8342. {
  8343. DebugAssert(!IsLocalBrowserMode());
  8344. TFileOperation Operation;
  8345. switch (Effect)
  8346. {
  8347. case DROPEFFECT_MOVE:
  8348. Operation = foMove;
  8349. break;
  8350. case DROPEFFECT_COPY:
  8351. // occures on WinXP (reported by user)
  8352. default:
  8353. Operation = foCopy;
  8354. break;
  8355. };
  8356. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  8357. // see a comment in TUnixDirView::PerformItemDragDropOperation
  8358. if (DragDropFilesEx->FileList->Count > 0)
  8359. {
  8360. TStrings * FileList = new TStringList();
  8361. try
  8362. {
  8363. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  8364. {
  8365. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  8366. }
  8367. FDragDropOperation = true;
  8368. TTransferOperationParam Param;
  8369. Param.TargetDirectory = TargetPath;
  8370. // upload, no temp dirs
  8371. Param.Temp = false;
  8372. Param.DragDrop = DragDrop;
  8373. Param.Options =
  8374. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  8375. if (ForceQueue)
  8376. {
  8377. Param.Queue = asOn;
  8378. }
  8379. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  8380. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  8381. {
  8382. if (IsFileControl(DropSourceControl, osLocal))
  8383. {
  8384. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  8385. }
  8386. else
  8387. {
  8388. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  8389. }
  8390. }
  8391. }
  8392. __finally
  8393. {
  8394. FDragDropOperation = false;
  8395. delete FileList;
  8396. }
  8397. }
  8398. }
  8399. //---------------------------------------------------------------------------
  8400. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  8401. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  8402. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  8403. {
  8404. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  8405. }
  8406. //---------------------------------------------------------------------------
  8407. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  8408. TObject * Sender, int grfKeyState, int & dwEffect)
  8409. {
  8410. DebugAssert(!IsLocalBrowserMode());
  8411. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  8412. if ((dwEffect != DROPEFFECT_NONE) &&
  8413. IsFileControl(DropSourceControl, osRemote))
  8414. {
  8415. // do not allow drop on remote panel (on free space, still allow drop on directories)
  8416. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  8417. (RemoteDirView->DropTarget == NULL))
  8418. {
  8419. dwEffect = DROPEFFECT_NONE;
  8420. }
  8421. else
  8422. {
  8423. if (dwEffect == DROPEFFECT_COPY)
  8424. {
  8425. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  8426. // currently we support copying always (at least via temporary directory);
  8427. // remove associated checks once this all proves stable and working
  8428. bool CopyCapable = true;
  8429. // if we do not support neither of operations, there's no discussion
  8430. if (!MoveCapable && !CopyCapable)
  8431. {
  8432. dwEffect = DROPEFFECT_NONE;
  8433. }
  8434. else
  8435. {
  8436. // The default effect inside remote panel is move,
  8437. // unless we do not support it, but support copy
  8438. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  8439. {
  8440. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  8441. }
  8442. else
  8443. {
  8444. // with ctrl-down, we want copy unless it is not supported
  8445. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  8446. }
  8447. }
  8448. }
  8449. }
  8450. }
  8451. }
  8452. //---------------------------------------------------------------------------
  8453. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  8454. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  8455. {
  8456. DebugAssert(!IsLocalBrowserMode());
  8457. if (FDDTotalSize >= 0)
  8458. {
  8459. if (File->IsDirectory)
  8460. {
  8461. FDDTotalSize = -1;
  8462. }
  8463. else
  8464. {
  8465. FDDTotalSize += File->Size;
  8466. }
  8467. }
  8468. DebugAssert(!FDragTempDir.IsEmpty());
  8469. // TODO: this is quite ineffective
  8470. // TODO: what if invalid character replacement is disabled?
  8471. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  8472. UnicodeString TransferFileName;
  8473. if (Sender == RemoteDriveView)
  8474. {
  8475. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  8476. }
  8477. else
  8478. {
  8479. TransferFileName = File->FileName;
  8480. }
  8481. FDDFileList->AddObject(TransferFileName, File);
  8482. }
  8483. //---------------------------------------------------------------------------
  8484. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  8485. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  8486. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  8487. {
  8488. // sometimes we do not get DDEnd so the list is not released
  8489. SAFE_DESTROY(FDDFileList);
  8490. FDDFileList = new TStringList();
  8491. FDragTempDir = WinConfiguration->TemporaryDir();
  8492. FDDTotalSize = 0;
  8493. FDragDropSshTerminate.reset(NULL);
  8494. }
  8495. //---------------------------------------------------------------------------
  8496. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  8497. TObject * /*Sender*/, LongBool /*FEscapePressed*/, int /*grfKeyState*/,
  8498. HRESULT & Result)
  8499. {
  8500. if (Result == DRAGDROP_S_DROP)
  8501. {
  8502. try
  8503. {
  8504. GlobalDragImageList->HideDragImage();
  8505. try
  8506. {
  8507. RemoteFileControlDDTargetDrop();
  8508. }
  8509. catch(ESshTerminate & E)
  8510. {
  8511. FDragDropSshTerminate.reset(E.Clone());
  8512. }
  8513. }
  8514. catch (Exception &E)
  8515. {
  8516. // If downloading fails we need to cancel drag&drop, otherwise
  8517. // Explorer shows error
  8518. // But by the way exception probably never reach this point as
  8519. // it's catched on way.
  8520. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8521. Result = DRAGDROP_S_CANCEL;
  8522. ShowExtendedException(Terminal, &E);
  8523. }
  8524. }
  8525. }
  8526. //---------------------------------------------------------------------------
  8527. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8528. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8529. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8530. {
  8531. TFileMasks::TParams MaskParams;
  8532. MaskParams.Size = Size;
  8533. MaskParams.Modification = Modification;
  8534. // this does not re-parse the mask if it is the same as the last time
  8535. FDirViewMatchMask = Masks;
  8536. bool ImplicitMatch;
  8537. Matches =
  8538. // Local and RecurseInclude parameters have no effect as there's no path.
  8539. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  8540. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  8541. (AllowImplicitMatches || !ImplicitMatch);
  8542. }
  8543. //---------------------------------------------------------------------------
  8544. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8545. TObject * Sender, TListItem * Item, WORD & Indexes)
  8546. {
  8547. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8548. if (GetPartialFileExtLen(DirView->ItemFileName(Item)) > 0)
  8549. {
  8550. Indexes |= oiPartial;
  8551. }
  8552. }
  8553. //---------------------------------------------------------------------------
  8554. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8555. {
  8556. return
  8557. DirViewEnabled(osCurrent) &&
  8558. DirView(osCurrent)->CanPasteFromClipBoard();
  8559. }
  8560. //---------------------------------------------------------------------------
  8561. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8562. {
  8563. bool Result = false;
  8564. if (CanPasteToDirViewFromClipBoard())
  8565. {
  8566. Result = true;
  8567. }
  8568. else
  8569. {
  8570. UnicodeString ClipboardText;
  8571. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8572. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8573. {
  8574. if (StoredSessions->IsUrl(ClipboardText))
  8575. {
  8576. Result = true;
  8577. }
  8578. else
  8579. {
  8580. Result = DirViewEnabled(osCurrent);
  8581. }
  8582. }
  8583. }
  8584. return Result;
  8585. }
  8586. //---------------------------------------------------------------------------
  8587. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8588. {
  8589. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8590. if (DoesClipboardContainOurFiles())
  8591. {
  8592. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8593. {
  8594. DebugAssert(!IsLocalBrowserMode());
  8595. TTerminalManager * Manager = TTerminalManager::Instance();
  8596. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8597. Manager->ActiveSession = FClipboardTerminal;
  8598. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8599. }
  8600. }
  8601. else if (CanPasteToDirViewFromClipBoard())
  8602. {
  8603. DirView(osCurrent)->PasteFromClipBoard();
  8604. }
  8605. else
  8606. {
  8607. UnicodeString ClipboardText;
  8608. if (NonEmptyTextFromClipboard(ClipboardText))
  8609. {
  8610. if (StoredSessions->IsUrl(ClipboardText))
  8611. {
  8612. NewSession(ClipboardText);
  8613. }
  8614. else
  8615. {
  8616. DirView(osCurrent)->Path = ClipboardText;
  8617. }
  8618. }
  8619. }
  8620. }
  8621. //---------------------------------------------------------------------------
  8622. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8623. {
  8624. // TBD
  8625. }
  8626. //---------------------------------------------------------------------------
  8627. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8628. {
  8629. TCustomDirView * ADirView = DirView(Side);
  8630. ADirView->SelectAll(Mode);
  8631. ADirView->SetFocus();
  8632. }
  8633. //---------------------------------------------------------------------------
  8634. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8635. {
  8636. TCustomDirView * ADirView = DirView(Side);
  8637. TFileFilter Filter;
  8638. DefaultFileFilter(Filter);
  8639. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8640. {
  8641. Configuration->Usage->Inc(L"MaskSelections");
  8642. ADirView->SelectFiles(Filter, Select);
  8643. ADirView->SetFocus();
  8644. }
  8645. }
  8646. //---------------------------------------------------------------------------
  8647. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8648. {
  8649. TCustomDirView * ADirView = DirView(Side);
  8650. ADirView->RestoreSelectedNames();
  8651. ADirView->SetFocus();
  8652. }
  8653. //---------------------------------------------------------------------------
  8654. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8655. {
  8656. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8657. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8658. {
  8659. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8660. UnicodeString Ext;
  8661. if (!IsSideLocalBrowser(osCurrent))
  8662. {
  8663. Ext = UnixExtractFileExt(FileName);
  8664. }
  8665. else
  8666. {
  8667. Ext = ExtractFileExt(FileName);
  8668. }
  8669. if (Ext.IsEmpty())
  8670. {
  8671. Ext = L".";
  8672. }
  8673. TFileFilter Filter;
  8674. Filter.Masks = FORMAT(L"*%s", (Ext));
  8675. Filter.Directories = false;
  8676. CurrentDirView->SelectFiles(Filter, Select);
  8677. }
  8678. }
  8679. //---------------------------------------------------------------------------
  8680. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8681. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8682. {
  8683. UnicodeString Result;
  8684. if (FIncrementalSearchState.Searching && (Side == GetSide(osCurrent)))
  8685. {
  8686. Result = FormatIncrementalSearchStatus(FIncrementalSearchState);
  8687. }
  8688. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8689. {
  8690. // noop
  8691. }
  8692. else
  8693. {
  8694. Result =
  8695. FMTLOAD(FILE_INFO_FORMAT,
  8696. (FormatBytes(FileInfo.SelectedSize),
  8697. FormatBytes(FileInfo.FilesSize),
  8698. FormatNumber(FileInfo.SelectedCount),
  8699. FormatNumber(FileInfo.FilesCount)));
  8700. }
  8701. return Result;
  8702. }
  8703. //---------------------------------------------------------------------------
  8704. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8705. TTBXStatusPanel * Panel, TOperationSide Side)
  8706. {
  8707. if (Panel->Index == 1)
  8708. {
  8709. ToggleShowHiddenFiles();
  8710. }
  8711. else if (Panel->Index == 2)
  8712. {
  8713. Filter(Side);
  8714. }
  8715. }
  8716. //---------------------------------------------------------------------------
  8717. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8718. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8719. {
  8720. DebugAssert(!StatusBar->SimplePanel);
  8721. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8722. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8723. }
  8724. //---------------------------------------------------------------------------
  8725. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8726. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8727. {
  8728. DebugAssert(StatusBar->Panels->Count >= 3);
  8729. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8730. if (FileInfo.HiddenCount > 0)
  8731. {
  8732. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8733. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8734. }
  8735. else
  8736. {
  8737. HiddenFilesPanel->ViewPriority = 0;
  8738. // not really necessary, just to cleanup no-longer-valid data
  8739. HiddenFilesPanel->Caption = L"";
  8740. }
  8741. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8742. if (FileInfo.FilteredCount > 0)
  8743. {
  8744. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8745. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8746. }
  8747. else
  8748. {
  8749. FilteredFilesPanel->ViewPriority = 0;
  8750. // not really necessary, just to cleanup no-longer-valid data
  8751. FilteredFilesPanel->Caption = L"";
  8752. }
  8753. }
  8754. //---------------------------------------------------------------------------
  8755. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8756. TObject * /*Sender*/)
  8757. {
  8758. if (DirView(osOther)->Enabled)
  8759. {
  8760. DirView(osOther)->SetFocus();
  8761. }
  8762. }
  8763. //---------------------------------------------------------------------------
  8764. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8765. {
  8766. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8767. switch (Config.Show)
  8768. {
  8769. case qvShow:
  8770. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8771. {
  8772. Config.Show = qvHide;
  8773. }
  8774. else
  8775. {
  8776. Config.Show = Config.LastHideShow;
  8777. }
  8778. break;
  8779. case qvHideWhenEmpty:
  8780. if (ComponentVisible[fcQueueView])
  8781. {
  8782. Config.Show = qvHide;
  8783. }
  8784. else
  8785. {
  8786. Config.LastHideShow = Config.Show;
  8787. Config.Show = qvShow;
  8788. }
  8789. break;
  8790. case qvHide:
  8791. Config.LastHideShow = Config.Show;
  8792. Config.Show = qvShow;
  8793. break;
  8794. }
  8795. WinConfiguration->QueueView = Config;
  8796. }
  8797. //---------------------------------------------------------------------------
  8798. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8799. {
  8800. UnicodeString Result;
  8801. if (Terminal != NULL)
  8802. {
  8803. switch (WinConfiguration->PathInCaption)
  8804. {
  8805. case picShort:
  8806. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8807. break;
  8808. case picFull:
  8809. Result = Terminal->CurrentDirectory;
  8810. break;
  8811. }
  8812. }
  8813. return Result;
  8814. }
  8815. //---------------------------------------------------------------------------
  8816. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8817. {
  8818. TColor Result = GetWindowColor(FSessionColor);
  8819. return Result;
  8820. }
  8821. //---------------------------------------------------------------------------
  8822. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8823. {
  8824. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8825. return Result;
  8826. }
  8827. //---------------------------------------------------------------------------
  8828. void TCustomScpExplorerForm::UpdatePanelControls(TCustomDirView * ADirView, TCustomDriveView * ADriveView)
  8829. {
  8830. bool UseDarkTheme = WinConfiguration->UseDarkTheme();
  8831. ADirView->DarkMode = UseDarkTheme;
  8832. ADriveView->DarkMode = UseDarkTheme;
  8833. }
  8834. //---------------------------------------------------------------------------
  8835. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8836. {
  8837. if (!FSessionChanging)
  8838. {
  8839. TTerminalManager::Instance()->UpdateAppTitle();
  8840. }
  8841. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8842. // background to gray on Windows 7 (works on Windows 10).
  8843. // See also EnableControl
  8844. if (Showing)
  8845. {
  8846. if (ManagedSession != NULL)
  8847. {
  8848. // Update path when it changes
  8849. if ((SessionsPageControl->ActivePage != NULL) &&
  8850. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8851. {
  8852. UpdateSessionTab(SessionsPageControl->ActivePage);
  8853. }
  8854. }
  8855. NonVisualDataModule->ReconnectSessionAction->Update();
  8856. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8857. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8858. CenterReconnectToolbar();
  8859. bool HasTerminal = HasActiveTerminal();
  8860. RemoteDirView->Enabled = HasTerminal;
  8861. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8862. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8863. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8864. RemoteDriveView->Color = RemoteDirView->Color;
  8865. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8866. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8867. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8868. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8869. QueueFileList->Enabled = QueueView3->Enabled;
  8870. QueueFileList->Color = QueueView3->Color;
  8871. QueueFileList->Font->Color = QueueView3->Font->Color;
  8872. QueueLabelUpdateStatus();
  8873. UpdatePanelControls(RemoteDirView, RemoteDriveView);
  8874. bool UseDarkTheme = WinConfiguration->UseDarkTheme();
  8875. if (FImmersiveDarkMode != UseDarkTheme)
  8876. {
  8877. UpdateDarkMode();
  8878. }
  8879. SessionsPageControl->TabTheme = UseDarkTheme ? CurrentTheme : NULL;
  8880. UnicodeString CurrentHiContrastThemeName = (FHiContrastTheme != NULL) ? FHiContrastTheme->Name : EmptyStr;
  8881. bool HiContrast = WinConfiguration->HiContrast;
  8882. UnicodeString NewHiContrastThemeName = HiContrast ? GetThemeName(!UseDarkTheme) : EmptyStr;
  8883. if (CurrentHiContrastThemeName != NewHiContrastThemeName)
  8884. {
  8885. ReleaseHiContrastTheme();
  8886. if (!NewHiContrastThemeName.IsEmpty())
  8887. {
  8888. FHiContrastTheme = GetTBXTheme(NewHiContrastThemeName);
  8889. }
  8890. }
  8891. SessionsPageControl->ActiveTabTheme = HiContrast ? DebugNotNull(FHiContrastTheme) : NULL;
  8892. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8893. MessageDock->Visible = (MessageDock->ToolbarCount > 0);
  8894. }
  8895. }
  8896. //---------------------------------------------------------------------------
  8897. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8898. {
  8899. UpdateControls();
  8900. }
  8901. //---------------------------------------------------------------------------
  8902. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8903. TObject * Sender)
  8904. {
  8905. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8906. DebugAssert(DirView != NULL);
  8907. DoDirViewLoaded(DirView);
  8908. TransferPresetAutoSelect();
  8909. TUnixDirView * UnixDirView = dynamic_cast<TUnixDirView *>(DirView);
  8910. if (UnixDirView != NULL)
  8911. {
  8912. TManagedTerminal * ATerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(UnixDirView->Terminal));
  8913. TTerminalManager::Instance()->TerminalLoadedDirectory(ATerminal);
  8914. }
  8915. }
  8916. //---------------------------------------------------------------------------
  8917. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8918. {
  8919. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8920. // first run after installation
  8921. if (double(Updates.LastCheck) == 0)
  8922. {
  8923. AppLog(L"First run, scheduling updates check for the next run");
  8924. // make sure next time there will be an update (if enabled)
  8925. Updates.LastCheck = TDateTime(1);
  8926. WinConfiguration->Updates = Updates;
  8927. }
  8928. else
  8929. {
  8930. TDateTime Period;
  8931. if (IsUWP() && !IsOfficialPackage())
  8932. {
  8933. Period = DefaultUpdatesPeriod;
  8934. AppLog(L"Thirdparty UWP package, using default updates check period");
  8935. }
  8936. else
  8937. {
  8938. Period = Updates.Period;
  8939. }
  8940. AppLogFmt(L"Updates check period: %.2f", (double(Period)));
  8941. if (double(Period) > 0)
  8942. {
  8943. TDateTime Interval = Now() - Updates.LastCheck;
  8944. AppLogFmt(L"Interval since the last updates check: %.2f", (double(Interval)));
  8945. if (Interval >= Period)
  8946. {
  8947. StartUpdateThread(UpdatesChecked);
  8948. }
  8949. }
  8950. }
  8951. }
  8952. //---------------------------------------------------------------------------
  8953. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8954. {
  8955. AppLog(L"Updates check done");
  8956. UnicodeString Message;
  8957. bool New;
  8958. TQueryType Type;
  8959. GetUpdatesMessage(Message, New, Type, false);
  8960. if (!Message.IsEmpty())
  8961. {
  8962. AppLogFmt(L"Updates check message: %s", (Message));
  8963. if (New)
  8964. {
  8965. AppLog(L"New version detected");
  8966. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8967. }
  8968. if (!New && (Type != qtWarning))
  8969. {
  8970. AppLog(L"Posting non-critical updates note");
  8971. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8972. }
  8973. else
  8974. {
  8975. AppLog(L"Posting new version or critical message notification");
  8976. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8977. }
  8978. if (New)
  8979. {
  8980. Configuration->Usage->Inc(L"UpdateNotifications");
  8981. }
  8982. }
  8983. }
  8984. //---------------------------------------------------------------------------
  8985. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8986. {
  8987. RestoreApp();
  8988. bool CanDisplay = !NonVisualDataModule->Busy;
  8989. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8990. {
  8991. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8992. }
  8993. if (CanDisplay)
  8994. {
  8995. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8996. // Probably not needed as CheckForUpdates is blocking since we started using neon for HTTP implementation.
  8997. NonVisualDataModule->StartBusy();
  8998. try
  8999. {
  9000. CheckForUpdates(true);
  9001. }
  9002. __finally
  9003. {
  9004. NonVisualDataModule->EndBusy();
  9005. }
  9006. }
  9007. else
  9008. {
  9009. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  9010. }
  9011. }
  9012. //---------------------------------------------------------------------------
  9013. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  9014. TCopyParamRuleData & Data)
  9015. {
  9016. DebugAssert(Terminal != NULL);
  9017. Data.HostName = Terminal->SessionData->HostNameExpanded;
  9018. Data.UserName = Terminal->SessionData->UserNameExpanded;
  9019. Data.RemoteDirectory = RemoteDirView->PathName;
  9020. }
  9021. //---------------------------------------------------------------------------
  9022. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  9023. {
  9024. // Terminal can be null when we are changing local directory implicitly,
  9025. // because it has been deleted, while no session is connected
  9026. // (Login dialog is open)
  9027. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  9028. {
  9029. DebugAssert(!IsLocalBrowserMode());
  9030. TCopyParamRuleData Data;
  9031. GetTransferPresetAutoSelectData(Data);
  9032. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  9033. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  9034. if (CopyParamIndex < 0)
  9035. {
  9036. // there is no preset that matches autoselection
  9037. // set preset that we consider "default"
  9038. FCopyParamAutoSelected = L""; // forget last autoselected preset
  9039. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  9040. }
  9041. else
  9042. {
  9043. // there is preset matching autoselection
  9044. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  9045. if (CopyParamName == FCopyParamAutoSelected)
  9046. {
  9047. // autoselected the same preset as the last time
  9048. // make no change (i.e. preserve custom user preset, if any)
  9049. }
  9050. else
  9051. {
  9052. // autoselected the different preset then the last time (or there
  9053. // was default preset set)
  9054. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  9055. GUIConfiguration->CopyParamCurrent = CopyParamName;
  9056. }
  9057. }
  9058. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  9059. {
  9060. if (CopyParamIndex >= 0)
  9061. {
  9062. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  9063. }
  9064. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  9065. try
  9066. {
  9067. int Fmt =
  9068. (CopyParamIndex < 0) ?
  9069. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  9070. COPY_PARAM_AUTOSELECTED;
  9071. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  9072. Data->Message = MainInstructions(Message);
  9073. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  9074. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  9075. CopyParamAttrs);
  9076. if (CopyParamIndex >= 0)
  9077. {
  9078. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  9079. Info = FORMAT(L"%s\n \n%s", (Info,
  9080. FMTLOAD(COPY_PARAM_RULE,
  9081. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  9082. }
  9083. Data->Message += L"\n\n" + Info;
  9084. if (WinConfiguration->CopyParamAutoSelectNotice)
  9085. {
  9086. TransferPresetNoteMessage(Data, true);
  9087. }
  9088. else
  9089. {
  9090. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  9091. Data = NULL; // ownership passed
  9092. }
  9093. }
  9094. __finally
  9095. {
  9096. delete Data;
  9097. }
  9098. }
  9099. }
  9100. }
  9101. //---------------------------------------------------------------------------
  9102. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  9103. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  9104. {
  9105. DebugAssert(NoteData != NULL);
  9106. DebugAssert(!IsLocalBrowserMode());
  9107. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  9108. TQueryButtonAlias Aliases[1];
  9109. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  9110. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  9111. Params.Aliases = Aliases;
  9112. Params.AliasesCount = LENOF(Aliases);
  9113. unsigned int Result =
  9114. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  9115. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  9116. switch (Result)
  9117. {
  9118. case qaNeverAskAgain:
  9119. DebugAssert(AllowNeverAskAgain);
  9120. WinConfiguration->CopyParamAutoSelectNotice = false;
  9121. break;
  9122. case qaIgnore:
  9123. PreferencesDialog(pmPresets);
  9124. break;
  9125. }
  9126. }
  9127. //---------------------------------------------------------------------------
  9128. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  9129. {
  9130. // as of now useless, as this is used for notes only, never for balloons, ...
  9131. RestoreApp();
  9132. // .. and we should never be busy here
  9133. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  9134. {
  9135. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  9136. }
  9137. }
  9138. //---------------------------------------------------------------------------
  9139. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  9140. TPreferencesMode APreferencesMode)
  9141. {
  9142. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  9143. TCopyParamRuleData Data;
  9144. if (Terminal != NULL)
  9145. {
  9146. PreferencesData.reset(new TPreferencesDialogData());
  9147. GetTransferPresetAutoSelectData(Data);
  9148. PreferencesData->CopyParamRuleData = &Data;
  9149. }
  9150. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  9151. }
  9152. //---------------------------------------------------------------------------
  9153. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  9154. const TCustomCommandType & Command)
  9155. {
  9156. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  9157. {
  9158. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  9159. }
  9160. }
  9161. //---------------------------------------------------------------------------
  9162. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  9163. {
  9164. bool RemoteAllowed = CustomCommandRemoteAllowed();
  9165. TCustomCommandType Command;
  9166. // make sure we use local custom command when remote are not supported
  9167. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  9168. {
  9169. Command = FLastCustomCommand;
  9170. }
  9171. else
  9172. {
  9173. Command.Params = Command.Params | ccLocal;
  9174. }
  9175. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  9176. FEditingFocusedAdHocCommand = OnFocused;
  9177. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  9178. int Options =
  9179. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  9180. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  9181. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  9182. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  9183. {
  9184. FLastCustomCommand = Command;
  9185. UpdateCustomCommandsToolbar();
  9186. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  9187. }
  9188. }
  9189. //---------------------------------------------------------------------------
  9190. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  9191. {
  9192. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  9193. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  9194. DebugAssert(State > 0);
  9195. if (State <= 0)
  9196. {
  9197. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  9198. }
  9199. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  9200. }
  9201. //---------------------------------------------------------------------------
  9202. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  9203. TCustomCommandType & Command, int & State)
  9204. {
  9205. bool Result = !FLastCustomCommand.Command.IsEmpty();
  9206. if (Result)
  9207. {
  9208. Command = FLastCustomCommand;
  9209. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  9210. }
  9211. return Result;
  9212. }
  9213. //---------------------------------------------------------------------------
  9214. void __fastcall TCustomScpExplorerForm::BeforeAction()
  9215. {
  9216. ResetIncrementalSearch();
  9217. }
  9218. //---------------------------------------------------------------------------
  9219. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  9220. {
  9221. DebugAssert(ComponentVisible[Component]);
  9222. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  9223. }
  9224. //---------------------------------------------------------------------------
  9225. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  9226. {
  9227. // when queue panel is resized here directly, the status bar is stretched
  9228. // over whole space the panel occupied
  9229. PostComponentHide(fcQueueView);
  9230. }
  9231. //---------------------------------------------------------------------------
  9232. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  9233. {
  9234. ComponentVisible[fcQueueFileList] = false;
  9235. }
  9236. //---------------------------------------------------------------------------
  9237. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  9238. {
  9239. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  9240. RefreshColorMode();
  9241. ResetSysDarkTheme();
  9242. ConfigurationChanged();
  9243. ConfigureInterface();
  9244. // To update references to recreated/destroyed themes in SessionsPageControl
  9245. UpdateControls();
  9246. // Should be called for all controls
  9247. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  9248. }
  9249. //---------------------------------------------------------------------------
  9250. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  9251. {
  9252. // Do not handle, when shutting down anyway (maybe also when not setup completely yet?)
  9253. if (!FInvalid &&
  9254. (Message.LParam != 0) &&
  9255. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  9256. {
  9257. ThemeChanged();
  9258. }
  9259. TForm::Dispatch(&Message);
  9260. }
  9261. //---------------------------------------------------------------------------
  9262. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  9263. {
  9264. TMessage * M = static_cast<TMessage*>(Message);
  9265. switch (M->Msg)
  9266. {
  9267. case CM_DIALOGCHAR:
  9268. CMDialogChar(*M);
  9269. break;
  9270. case WM_APPCOMMAND:
  9271. WMAppCommand(*M);
  9272. break;
  9273. case WM_SYSCOMMAND:
  9274. WMSysCommand(*M);
  9275. break;
  9276. case WM_QUERYENDSESSION:
  9277. WMQueryEndSession(*M);
  9278. break;
  9279. case WM_ENDSESSION:
  9280. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  9281. break;
  9282. #ifdef _DEBUG
  9283. case WM_WINDOWPOSCHANGED:
  9284. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  9285. break;
  9286. case CM_CUSTOMSTYLECHANGED:
  9287. TForm::Dispatch(Message);
  9288. break;
  9289. #endif
  9290. case WM_COMPONENT_HIDE:
  9291. {
  9292. Byte Component = static_cast<Byte>(M->WParam);
  9293. // sanity check
  9294. if (ComponentVisible[Component])
  9295. {
  9296. // special treatment
  9297. if (Component == fcQueueView)
  9298. {
  9299. ToggleQueueVisibility();
  9300. DebugAssert(!ComponentVisible[fcQueueView]);
  9301. }
  9302. else
  9303. {
  9304. ComponentVisible[Component] = false;
  9305. }
  9306. }
  9307. }
  9308. break;
  9309. case WM_COPYDATA:
  9310. WMCopyData(*M);
  9311. break;
  9312. case WM_MANAGES_CAPTION:
  9313. // caption managed in TTerminalManager::UpdateAppTitle()
  9314. M->Result = 1;
  9315. break;
  9316. case WM_WANTS_MOUSEWHEEL:
  9317. M->Result = 1;
  9318. break;
  9319. case CM_SHOWINGCHANGED:
  9320. CMShowingChanged(*M);
  9321. break;
  9322. case WM_CLOSE:
  9323. WMClose(*M);
  9324. break;
  9325. case WM_DPICHANGED:
  9326. WMDpiChanged(*M);
  9327. break;
  9328. case WM_SETTINGCHANGE:
  9329. WMSettingChange(*M);
  9330. break;
  9331. case CM_DIALOGKEY:
  9332. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  9333. break;
  9334. case WM_PASTE_FILES:
  9335. PasteFiles();
  9336. break;
  9337. case WM_IS_HIDDEN:
  9338. // To treat master password prompt as a main window while showing it when workspace is loading while
  9339. // the exporer is showing up (Visible is true already)
  9340. M->Result = !FShowing;
  9341. break;
  9342. case WM_SHOWWINDOW:
  9343. if (M->WParam)
  9344. {
  9345. AddStartupSequence(L"J");
  9346. }
  9347. TForm::Dispatch(Message);
  9348. break;
  9349. case WM_QUEUE_CALLBACK:
  9350. WMQueueCallback(*M);
  9351. break;
  9352. default:
  9353. TForm::Dispatch(Message);
  9354. break;
  9355. }
  9356. }
  9357. //---------------------------------------------------------------------------
  9358. void __fastcall TCustomScpExplorerForm::UpdateImages()
  9359. {
  9360. // noop
  9361. }
  9362. //---------------------------------------------------------------------------
  9363. void TCustomScpExplorerForm::RegenerateSessionColorsImageList()
  9364. {
  9365. FSessionColors->Clear();
  9366. AddFixedSessionImages();
  9367. ::RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  9368. UpdateImages();
  9369. }
  9370. //---------------------------------------------------------------------------
  9371. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  9372. {
  9373. TForm::Dispatch(&Message);
  9374. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  9375. UpdatePixelsPerInchMainWindowCounter();
  9376. }
  9377. //---------------------------------------------------------------------------
  9378. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  9379. {
  9380. AppLog(L"Main window WM_CLOSE");
  9381. // Cannot close window while we are busy.
  9382. // We cannot test this in FormCloseQuery as that is called also from
  9383. // Close(), which is called by CloseApplicationAction. So we can be busy
  9384. // there already even, when it is legitimate to close the application.
  9385. // Possibly a better place to handle this would be WMSysCommand.
  9386. if (NonVisualDataModule->Busy)
  9387. {
  9388. Message.Result = 1;
  9389. }
  9390. else
  9391. {
  9392. TForm::Dispatch(&Message);
  9393. }
  9394. }
  9395. //---------------------------------------------------------------------------
  9396. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  9397. {
  9398. if (!FShowingChanged)
  9399. {
  9400. AddStartupSequence(L"D");
  9401. FShowingChanged = true;
  9402. }
  9403. // Ignoring implicit local-local browser of Commander, opened from DoShow
  9404. bool NoSession = (ManagedSession == NULL);
  9405. // This calls TCustomScpExplorerForm::DoShow
  9406. TForm::Dispatch(&Message);
  9407. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  9408. if (Showing && !FStarted)
  9409. {
  9410. FStarted = true;
  9411. InterfaceStarted();
  9412. }
  9413. if (Showing && NoSession)
  9414. {
  9415. // When we are starting minimized (i.e. from an installer),
  9416. // postpone showing Login dialog until we get restored.
  9417. // Otherwise the Login dialog (and Authentication window) show restored
  9418. // over invisible (minimized) main window.
  9419. if (WindowState == wsMinimized)
  9420. {
  9421. FNeedSession = true;
  9422. }
  9423. else
  9424. {
  9425. // This happens before application ever goes idle, so the toolbars would
  9426. // stay enabled (initial state) until the Login dialog is dismissed.
  9427. UpdateActions();
  9428. NonVisualDataModule->StartBusy();
  9429. try
  9430. {
  9431. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  9432. // otherwise the dialog does not receive focus.
  9433. // With Commander interface the ProcessMessages is called already
  9434. // by TDriveView, but with Explorer interface, we need to call it explicily
  9435. Application->ProcessMessages();
  9436. // do not reload sessions, they have been loaded just now (optimization)
  9437. NeedSession(true);
  9438. }
  9439. __finally
  9440. {
  9441. NonVisualDataModule->EndBusy();
  9442. }
  9443. }
  9444. }
  9445. }
  9446. //---------------------------------------------------------------------------
  9447. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  9448. {
  9449. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  9450. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  9451. }
  9452. //---------------------------------------------------------------------------
  9453. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  9454. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  9455. int & MaxHeight)
  9456. {
  9457. // workaround for bug in TWinControl.CalcConstraints
  9458. // Check for empty rect (restore from iconic state) is done there only after
  9459. // call to AdjustClientRect, which enlarges the rect (for forms).
  9460. TRect R = GetClientRect();
  9461. // when restoring from iconic state, place no restrictions
  9462. if (IsRectEmpty(R))
  9463. {
  9464. MinWidth = 0;
  9465. MinHeight = 0;
  9466. MaxWidth = 32000;
  9467. MaxHeight = 32000;
  9468. }
  9469. }
  9470. //---------------------------------------------------------------------------
  9471. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  9472. TSpaceAvailable & ASpaceAvailable, bool & Close)
  9473. {
  9474. // terminal can be already closed (e.g. dropped connection)
  9475. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  9476. {
  9477. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  9478. try
  9479. {
  9480. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  9481. }
  9482. catch(...)
  9483. {
  9484. if (!Terminal->Active)
  9485. {
  9486. Close = true;
  9487. }
  9488. throw;
  9489. }
  9490. }
  9491. }
  9492. //---------------------------------------------------------------------------
  9493. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  9494. {
  9495. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  9496. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  9497. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  9498. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  9499. {
  9500. OnGetSpaceAvailable = GetSpaceAvailable;
  9501. }
  9502. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  9503. OnGetSpaceAvailable);
  9504. }
  9505. //---------------------------------------------------------------------------
  9506. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  9507. {
  9508. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  9509. Terminal->FillSessionDataForCode(Data.get());
  9510. return Data.release();
  9511. }
  9512. //---------------------------------------------------------------------------
  9513. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  9514. {
  9515. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  9516. DoGenerateUrlDialog(Data.get(), Paths);
  9517. }
  9518. //---------------------------------------------------------------------------
  9519. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  9520. {
  9521. GenerateUrl(NULL);
  9522. }
  9523. //---------------------------------------------------------------------------
  9524. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  9525. {
  9526. std::unique_ptr<TStrings> Paths(new TStringList());
  9527. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  9528. GenerateUrl(Paths.get());
  9529. }
  9530. //---------------------------------------------------------------------------
  9531. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  9532. {
  9533. FSessionColor = value;
  9534. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  9535. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  9536. static_cast<TObject *>(GetComponent(fcColorMenu)));
  9537. DebugAssert(ColorItem != NULL);
  9538. ColorItem->Color = C;
  9539. NonVisualDataModule->ColorMenuItem->Color = C;
  9540. UpdateControls();
  9541. }
  9542. //---------------------------------------------------------------------------
  9543. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  9544. {
  9545. if (value != FSessionColor)
  9546. {
  9547. UpdateSessionColor(value);
  9548. // Is null when called from LastTerminalClosed
  9549. if (Terminal != NULL)
  9550. {
  9551. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  9552. }
  9553. }
  9554. }
  9555. //---------------------------------------------------------------------------
  9556. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  9557. {
  9558. bool Result = FNoteTimer->Enabled;
  9559. if (Result)
  9560. {
  9561. // cannot cancel note too early
  9562. bool NotEarly =
  9563. (Now() - FNoteShown >
  9564. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  9565. if (Force || NotEarly)
  9566. {
  9567. FNoteTimer->Enabled = false;
  9568. FNote = L"";
  9569. SAFE_DESTROY(FNoteData);
  9570. FOnNoteClick = NULL;
  9571. FNoteHints = FNotes->Text;
  9572. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  9573. UpdateStatusBar();
  9574. }
  9575. }
  9576. return Result;
  9577. }
  9578. //---------------------------------------------------------------------------
  9579. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  9580. {
  9581. DebugAssert(FNoteTimer->Enabled);
  9582. CancelNote(true);
  9583. }
  9584. //---------------------------------------------------------------------------
  9585. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9586. {
  9587. FNoteHints = FNotes->Text.TrimRight();
  9588. }
  9589. //---------------------------------------------------------------------------
  9590. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9591. {
  9592. int P = Note.Pos(L"\n");
  9593. if (P > 0)
  9594. {
  9595. Note.SetLength(P - 1);
  9596. }
  9597. FNotes->Add(FORMAT(L"[%s] %s",
  9598. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9599. while (FNotes->Count > 10)
  9600. {
  9601. FNotes->Delete(0);
  9602. }
  9603. if (UpdateNow)
  9604. {
  9605. UpdateNoteHints();
  9606. UpdateStatusBar();
  9607. }
  9608. }
  9609. //---------------------------------------------------------------------------
  9610. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  9611. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9612. {
  9613. int P = Note.Pos(L"\n");
  9614. if (P > 0)
  9615. {
  9616. Note.SetLength(P - 1);
  9617. }
  9618. UpdateNoteHints();
  9619. FNote = Note;
  9620. SAFE_DESTROY(FNoteData);
  9621. FNoteData = NoteData;
  9622. FOnNoteClick = OnNoteClick;
  9623. AddNote(Note, false);
  9624. UpdateStatusBar();
  9625. FNoteShown = Now();
  9626. FNoteTimer->Enabled = false;
  9627. if (Seconds == 0)
  9628. {
  9629. Seconds = WinConfiguration->NotificationsTimeout;
  9630. }
  9631. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9632. FNoteTimer->Enabled = true;
  9633. }
  9634. //---------------------------------------------------------------------------
  9635. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9636. {
  9637. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9638. }
  9639. //---------------------------------------------------------------------------
  9640. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9641. {
  9642. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9643. {
  9644. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9645. }
  9646. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9647. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9648. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9649. 0, NULL, NULL);
  9650. }
  9651. //---------------------------------------------------------------------------
  9652. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9653. {
  9654. bool NewShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9655. UnicodeString Note =
  9656. FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1), (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, (NewShowHiddenFiles ? 2 : 3))));
  9657. PostNote(Note, 0, NULL, NULL);
  9658. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9659. WinConfiguration->ShowHiddenFiles = NewShowHiddenFiles;
  9660. }
  9661. //---------------------------------------------------------------------------
  9662. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9663. {
  9664. WinConfiguration->FormatSizeBytes = Style;
  9665. }
  9666. //---------------------------------------------------------------------------
  9667. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9668. {
  9669. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9670. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9671. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9672. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9673. }
  9674. //---------------------------------------------------------------------------
  9675. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9676. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9677. {
  9678. if (Panel->Index == 0)
  9679. {
  9680. if (FOnNoteClick != NULL)
  9681. {
  9682. // prevent the user data from being freed by possible call
  9683. // to CancelNote or PostNote during call to OnNoteClick
  9684. std::unique_ptr<TObject> NoteData(FNoteData);
  9685. TNotifyEvent OnNoteClick = FOnNoteClick;
  9686. FNoteData = NULL;
  9687. // need to cancel the note as we are going to delete its user data
  9688. CancelNote(true);
  9689. OnNoteClick(NoteData.get());
  9690. }
  9691. }
  9692. if (HasActiveTerminal() &&
  9693. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9694. {
  9695. FileSystemInfo();
  9696. }
  9697. }
  9698. //---------------------------------------------------------------------------
  9699. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9700. {
  9701. // workaround:
  9702. // 1) for unknown reason, disabling window, while minimized,
  9703. // prevents it from restoring, even if it was enabled again meanwhile
  9704. // 2) when disabling the main window, while another has focus
  9705. // minimize is no longer possible
  9706. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9707. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9708. // Shouldn't we use IsApplicationMinimized() here?
  9709. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9710. {
  9711. AppLog(L"Disabling window");
  9712. Enabled = false;
  9713. }
  9714. FLockLevel++;
  9715. }
  9716. //---------------------------------------------------------------------------
  9717. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9718. {
  9719. DebugAssert(FLockLevel > 0);
  9720. FLockLevel--;
  9721. if (FLockLevel == 0)
  9722. {
  9723. DebugAssert(FLockSuspendLevel == 0);
  9724. AppLog(L"Re-enabling window");
  9725. Enabled = true;
  9726. // VCL_COPY (TCustomForm.SetWindowFocus)
  9727. if (Active && (ActiveControl != NULL))
  9728. {
  9729. ::SetFocus(ActiveControl->Handle);
  9730. if (GetFocus() == ActiveControl->Handle)
  9731. {
  9732. ActiveControl->Perform(CM_UIACTIVATE, 0, 0);
  9733. }
  9734. }
  9735. }
  9736. }
  9737. //---------------------------------------------------------------------------
  9738. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9739. {
  9740. // We need to make sure that window is enabled when the last modal window closes
  9741. // otherwise focus is not restored correctly.
  9742. // So we re-enable the window when modal window opens and
  9743. // disable it back after is closes
  9744. if (FLockLevel > 0)
  9745. {
  9746. // while we have nesting counter, we know that we never be called
  9747. // recursivelly as Application->OnModalBegin is called only
  9748. // for the top-level modal window
  9749. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9750. {
  9751. // won't be disabled when conditions in LockWindow() were not satisfied
  9752. FDisabledOnLockSuspend = !Enabled;
  9753. // When minimized to tray (or actually when set to SW_HIDE),
  9754. // setting Enabled makes the window focusable even when there's
  9755. // modal window over it
  9756. if (!FTrayIcon->Visible)
  9757. {
  9758. Enabled = true;
  9759. }
  9760. }
  9761. FLockSuspendLevel++;
  9762. }
  9763. }
  9764. //---------------------------------------------------------------------------
  9765. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9766. {
  9767. if (FLockSuspendLevel > 0)
  9768. {
  9769. DebugAssert(FLockLevel > 0);
  9770. FLockSuspendLevel--;
  9771. // see comment in SuspendWindowLock
  9772. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9773. {
  9774. // Note that window can be enabled here, when we were minized to tray when
  9775. // was SuspendWindowLock() called.
  9776. // We should possibly do the same check as in LockWindow(),
  9777. // if it is ever possible that the consitions change between
  9778. // SuspendWindowLock() and ResumeWindowLock()
  9779. Enabled = !FDisabledOnLockSuspend;
  9780. }
  9781. }
  9782. }
  9783. //---------------------------------------------------------------------------
  9784. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9785. {
  9786. TTBXComboBoxItem * RemotePathComboBox =
  9787. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9788. if (!TextOnly)
  9789. {
  9790. TStrings * Items = RemotePathComboBox->Strings;
  9791. Items->BeginUpdate();
  9792. try
  9793. {
  9794. Items->Clear();
  9795. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9796. {
  9797. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9798. while (!IsUnixRootPath(APath))
  9799. {
  9800. int P = APath.LastDelimiter(L'/');
  9801. DebugAssert(P >= 0);
  9802. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9803. APath.SetLength(P - 1);
  9804. }
  9805. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9806. }
  9807. }
  9808. __finally
  9809. {
  9810. Items->EndUpdate();
  9811. }
  9812. }
  9813. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9814. // Setting ItemIndex to -1 does not reset its text
  9815. if (RemotePathComboBox->Strings->Count == 0)
  9816. {
  9817. RemotePathComboBox->Text = L"";
  9818. }
  9819. }
  9820. //---------------------------------------------------------------------------
  9821. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9822. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9823. {
  9824. if (AIndex < 0)
  9825. {
  9826. AIndex = Sender->ItemIndex;
  9827. }
  9828. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9829. }
  9830. //---------------------------------------------------------------------------
  9831. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9832. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9833. int & ImageIndex)
  9834. {
  9835. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9836. }
  9837. //---------------------------------------------------------------------------
  9838. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9839. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9840. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9841. {
  9842. if (!IsLocalBrowserMode())
  9843. {
  9844. ARect.Left += (10 * AIndex);
  9845. }
  9846. }
  9847. //---------------------------------------------------------------------------
  9848. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9849. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9850. {
  9851. AWidth += (10 * AIndex);
  9852. }
  9853. //---------------------------------------------------------------------------
  9854. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9855. {
  9856. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9857. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9858. int Index = RemotePathComboBox->ItemIndex;
  9859. while (Index < RemotePathComboBox->Strings->Count - 1)
  9860. {
  9861. APath = UnixExtractFileDir(APath);
  9862. Index++;
  9863. }
  9864. // VanDyke style paths
  9865. if (APath.IsEmpty())
  9866. {
  9867. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9868. APath = ROOTDIRECTORY;
  9869. }
  9870. if (RemoteDirView->Path != APath)
  9871. {
  9872. RemoteDirView->Path = APath;
  9873. }
  9874. }
  9875. //---------------------------------------------------------------------------
  9876. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9877. {
  9878. DoRemotePathComboBoxItemClick(Sender);
  9879. }
  9880. //---------------------------------------------------------------------------
  9881. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9882. {
  9883. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9884. DebugUsedParam(Sender);
  9885. UpdateRemotePathComboBox(true);
  9886. }
  9887. //---------------------------------------------------------------------------
  9888. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9889. {
  9890. DoRemotePathComboBoxCancel(Sender);
  9891. }
  9892. //---------------------------------------------------------------------------
  9893. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9894. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9895. {
  9896. ResetIncrementalSearch();
  9897. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9898. DebugAssert(DirView != NULL);
  9899. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9900. {
  9901. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9902. // OnEditing is called also from TCustomListView::CanEdit
  9903. if (Edit != NULL)
  9904. {
  9905. EditSelectBaseName(Edit);
  9906. }
  9907. }
  9908. }
  9909. //---------------------------------------------------------------------------
  9910. //---------------------------------------------------------------------------
  9911. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9912. {
  9913. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9914. Result->AutoDetectDnD = false;
  9915. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9916. Result->RenderDataOn = rdoEnterAndDropSync;
  9917. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9918. return Result;
  9919. }
  9920. //---------------------------------------------------------------------------
  9921. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9922. {
  9923. if (IsWin10Build(19041))
  9924. {
  9925. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9926. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9927. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9928. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9929. }
  9930. }
  9931. //---------------------------------------------------------------------------
  9932. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9933. {
  9934. TForm::CreateWnd();
  9935. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9936. UpdateDarkMode();
  9937. if (FSessionsDragDropFilesEx == NULL)
  9938. {
  9939. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9940. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9941. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9942. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9943. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9944. }
  9945. if (FQueueDragDropFilesEx == NULL)
  9946. {
  9947. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9948. // No need to set OnDragOver as we do not have any restrictions
  9949. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9950. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9951. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9952. }
  9953. }
  9954. //---------------------------------------------------------------------------
  9955. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9956. {
  9957. TForm::DestroyWnd();
  9958. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9959. FQueueDragDropFilesEx->DragDropControl = NULL;
  9960. }
  9961. //---------------------------------------------------------------------------
  9962. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9963. {
  9964. SideEnter(FCurrentSide);
  9965. FEverShown = true;
  9966. }
  9967. //---------------------------------------------------------------------------
  9968. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9969. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9970. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9971. {
  9972. if (!NonVisualDataModule->Busy)
  9973. {
  9974. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9975. Configuration->Usage->Inc(L"FileFinds");
  9976. LockWindow(true);
  9977. NonVisualDataModule->StartBusy();
  9978. WinConfiguration->LockedInterface = true;
  9979. try
  9980. {
  9981. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9982. }
  9983. __finally
  9984. {
  9985. WinConfiguration->LockedInterface = false;
  9986. NonVisualDataModule->EndBusy();
  9987. UnlockWindow();
  9988. }
  9989. }
  9990. }
  9991. //---------------------------------------------------------------------------
  9992. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9993. {
  9994. if (!NonVisualDataModule->Busy)
  9995. {
  9996. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9997. SetFocus();
  9998. RemoteDirView->Path = UnixExtractFilePath(Path);
  9999. UnicodeString FileName = UnixExtractFileName(Path);
  10000. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  10001. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  10002. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  10003. if (Item == NULL)
  10004. {
  10005. ReloadDirectory(osRemote);
  10006. Item = RemoteDirView->FindFileItem(FileName);
  10007. }
  10008. if (Item != NULL)
  10009. {
  10010. RemoteDirView->ItemFocused = Item;
  10011. RemoteDirView->ItemFocused->MakeVisible(false);
  10012. RemoteDirView->SetFocus();
  10013. }
  10014. }
  10015. }
  10016. //---------------------------------------------------------------------------
  10017. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  10018. {
  10019. bool Result = !NonVisualDataModule->Busy;
  10020. if (Result)
  10021. {
  10022. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  10023. }
  10024. return Result;
  10025. }
  10026. //---------------------------------------------------------------------------
  10027. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  10028. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  10029. {
  10030. if (CanOperateOnFoundFiles(ATerminal))
  10031. {
  10032. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished, OnFileOperationFinished);
  10033. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  10034. }
  10035. }
  10036. //---------------------------------------------------------------------------
  10037. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  10038. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  10039. {
  10040. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  10041. }
  10042. //---------------------------------------------------------------------------
  10043. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  10044. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  10045. {
  10046. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  10047. }
  10048. //---------------------------------------------------------------------------
  10049. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  10050. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  10051. {
  10052. if (CanOperateOnFoundFiles(ATerminal))
  10053. {
  10054. for (int Index = 0; Index < FileList->Count; Index++)
  10055. {
  10056. UnicodeString FileName = FileList->Strings[Index];
  10057. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  10058. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  10059. OnFileOperationFinished(osRemote, FileName, true, true);
  10060. }
  10061. }
  10062. }
  10063. //---------------------------------------------------------------------------
  10064. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  10065. {
  10066. FFileFindTerminal = Terminal;
  10067. ShowFileFindDialog(
  10068. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  10069. DoDownloadFoundFiles, DoEditFoundFiles);
  10070. }
  10071. //---------------------------------------------------------------------------
  10072. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  10073. {
  10074. FTaskbarList = TaskbarList;
  10075. }
  10076. //---------------------------------------------------------------------------
  10077. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  10078. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  10079. {
  10080. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  10081. if (Index >= 0)
  10082. {
  10083. if (Button == mbLeft)
  10084. {
  10085. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  10086. // If switching tab (switching session) takes long enough for user
  10087. // to actually release the button, "mouse down" is still raised,
  10088. // but we do not get "mouse up" event, so dragging is not cancelled,
  10089. // prevent that by not beginning dragging in the first place.
  10090. int Key = ((GetSystemMetrics(SM_SWAPBUTTON) == 0) ? VK_LBUTTON : VK_RBUTTON);
  10091. if (IsKeyPressed(Key))
  10092. {
  10093. // when user clicks the "+", we get mouse down only after the session
  10094. // is closed, when new session tab is already on X:Y, so dragging
  10095. // starts, prevent that
  10096. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  10097. {
  10098. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10099. if (Terminal != NULL)
  10100. {
  10101. SessionsPageControl->BeginDrag(false);
  10102. }
  10103. }
  10104. }
  10105. }
  10106. else if (Button == mbMiddle)
  10107. {
  10108. // ignore middle-click for "New session tab" and the last local-local tab
  10109. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10110. if ((Session != NULL) && CanCloseSession(Session))
  10111. {
  10112. CloseSessionTab(Index);
  10113. }
  10114. }
  10115. }
  10116. }
  10117. //---------------------------------------------------------------------------
  10118. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  10119. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  10120. {
  10121. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  10122. // do not allow dropping on the "+" tab
  10123. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10124. if ((TargetTerminal != NULL) &&
  10125. (SessionsPageControl->ActivePage->PageIndex != Index))
  10126. {
  10127. Configuration->Usage->Inc(L"SessionTabMoves");
  10128. // this is almost redundant as we would recreate tabs in SessionListChanged,
  10129. // but we want to actually prevent that to avoid flicker
  10130. SessionsPageControl->ActivePage->PageIndex = Index;
  10131. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  10132. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  10133. }
  10134. }
  10135. //---------------------------------------------------------------------------
  10136. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  10137. TObject * Sender, TObject * Source, int X, int Y,
  10138. TDragState /*State*/, bool & Accept)
  10139. {
  10140. Accept = (Sender == Source);
  10141. if (Accept)
  10142. {
  10143. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  10144. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10145. // do not allow dragging to the "+" tab
  10146. Accept = (Terminal != NULL);
  10147. }
  10148. }
  10149. //---------------------------------------------------------------------------
  10150. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  10151. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  10152. {
  10153. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  10154. if (Index < 0)
  10155. {
  10156. Effect = DROPEFFECT_NONE;
  10157. }
  10158. else
  10159. {
  10160. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10161. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  10162. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  10163. {
  10164. Effect = DROPEFFECT_NONE;
  10165. }
  10166. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  10167. {
  10168. Effect = DROPEFFECT_COPY;
  10169. }
  10170. }
  10171. }
  10172. //---------------------------------------------------------------------------
  10173. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  10174. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  10175. {
  10176. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  10177. // do not allow dropping on local-local, disconnected and the "+" tab
  10178. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  10179. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10180. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  10181. {
  10182. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  10183. if (!IsFileControl(DropSourceControl, osRemote))
  10184. {
  10185. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  10186. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  10187. // Why don't we use Terminal->CurrentDirectory directly?
  10188. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  10189. // do not force queue, drag drop
  10190. false, true);
  10191. }
  10192. }
  10193. }
  10194. //---------------------------------------------------------------------------
  10195. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  10196. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  10197. {
  10198. // Downloads are handled in RemoteFileControlDDEnd
  10199. if (!IsFileControl(DropSourceControl, osRemote))
  10200. {
  10201. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  10202. Terminal->CurrentDirectory,
  10203. // force queue, drag drop
  10204. true, true);
  10205. }
  10206. }
  10207. //---------------------------------------------------------------------------
  10208. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  10209. {
  10210. FShowing = false;
  10211. // Do not save empty workspace
  10212. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  10213. {
  10214. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  10215. UnicodeString Name = WorkspaceName();
  10216. DoSaveWorkspace(
  10217. Name, DataList.get(),
  10218. !Configuration->DisablePasswordStoring &&
  10219. WinConfiguration->AutoSaveWorkspacePasswords, false);
  10220. WinConfiguration->LastStoredSession = Name;
  10221. }
  10222. AppLog(L"Closing main window");
  10223. }
  10224. //---------------------------------------------------------------------------
  10225. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  10226. {
  10227. TManagedTerminal * ManagedTerminal =
  10228. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  10229. if (ManagedTerminal != NULL)
  10230. {
  10231. ManagedTerminal->DirectoryLoaded = Now();
  10232. }
  10233. }
  10234. //---------------------------------------------------------------------------
  10235. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  10236. TListItem * /*Item*/, bool /*Selected*/)
  10237. {
  10238. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  10239. switch (DirView->LastSelectMethod)
  10240. {
  10241. case smKeyboard:
  10242. Configuration->Usage->Inc(L"KeyboardSelections");
  10243. break;
  10244. case smMouse:
  10245. Configuration->Usage->Inc(L"MouseSelections");
  10246. break;
  10247. }
  10248. }
  10249. //---------------------------------------------------------------------------
  10250. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  10251. {
  10252. if (Color != 0)
  10253. {
  10254. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  10255. }
  10256. else
  10257. {
  10258. return FSessionTabImageIndex;
  10259. }
  10260. }
  10261. //---------------------------------------------------------------------------
  10262. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  10263. {
  10264. TPngImageCollectionItem * Item =
  10265. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  10266. FSessionColors->AddPng(Item->PngImage);
  10267. return FSessionColors->Count - 1;
  10268. }
  10269. //---------------------------------------------------------------------------
  10270. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  10271. {
  10272. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  10273. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  10274. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  10275. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  10276. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  10277. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  10278. }
  10279. //---------------------------------------------------------------------------
  10280. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  10281. {
  10282. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  10283. }
  10284. //---------------------------------------------------------------------------
  10285. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  10286. {
  10287. switch (Side)
  10288. {
  10289. case osLocal:
  10290. return FNewLocalTabTabImageIndex;
  10291. case osRemote:
  10292. case osCurrent:
  10293. return FNewRemoteTabTabImageIndex;
  10294. default:
  10295. DebugFail();
  10296. return -1;
  10297. }
  10298. }
  10299. //---------------------------------------------------------------------------
  10300. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  10301. {
  10302. for (int Index = 0; Index < Control->ControlCount; Index++)
  10303. {
  10304. TControl * AControl = Control->Controls[Index];
  10305. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  10306. if (WinControl != NULL)
  10307. {
  10308. CollectItemsWithTextDisplayMode(WinControl);
  10309. }
  10310. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  10311. if (Toolbar != NULL)
  10312. {
  10313. // we care for top-level items only
  10314. TTBCustomItem * Items = Toolbar->Items;
  10315. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  10316. {
  10317. TTBCustomItem * Item = Items->Items[ItemIndex];
  10318. if (((Item->DisplayMode == nbdmImageAndText) ||
  10319. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  10320. EligibleForImageDisplayMode(Item))
  10321. {
  10322. FItemsWithTextDisplayMode.insert(Item);
  10323. }
  10324. }
  10325. }
  10326. }
  10327. }
  10328. //---------------------------------------------------------------------------
  10329. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  10330. {
  10331. return true;
  10332. }
  10333. //---------------------------------------------------------------------------
  10334. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  10335. {
  10336. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  10337. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  10338. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  10339. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  10340. bool Result = true;
  10341. while (Result && (i != FItemsWithTextDisplayMode.end()))
  10342. {
  10343. TTBCustomItem * Item = *i;
  10344. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  10345. if (Label != NULL)
  10346. {
  10347. // optimization
  10348. if (Label->Visible == SelectiveToolbarText)
  10349. {
  10350. Result = false;
  10351. }
  10352. else
  10353. {
  10354. Label->Visible = SelectiveToolbarText;
  10355. }
  10356. }
  10357. else
  10358. {
  10359. // optimization
  10360. if (Item->DisplayMode == DisplayMode)
  10361. {
  10362. Result = false;
  10363. }
  10364. else
  10365. {
  10366. Item->DisplayMode = DisplayMode;
  10367. }
  10368. }
  10369. ++i;
  10370. }
  10371. UpdateNewTabTab();
  10372. return Result;
  10373. }
  10374. //---------------------------------------------------------------------------
  10375. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  10376. {
  10377. DebugFail();
  10378. }
  10379. //---------------------------------------------------------------------------
  10380. bool __fastcall TCustomScpExplorerForm::IsBusy()
  10381. {
  10382. // Among other, a lock level is non zero, while directory is being loaded,
  10383. // even when it happens as a result of dirview navigation,
  10384. // i.e. when TNonVisualDataModule is NOT FBusy.
  10385. // That's why the TNonVisualDataModule::GetBusy calls this method.
  10386. // Among other this prevents a panel auto update to occur while
  10387. // directory is changing.
  10388. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  10389. }
  10390. //---------------------------------------------------------------------------
  10391. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  10392. {
  10393. // While the window is disabled, we still seem to process menu shortcuts at least,
  10394. // so stop it at least here.
  10395. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  10396. bool Result;
  10397. if (Allowed == aaUpdate)
  10398. {
  10399. Result = true;
  10400. }
  10401. else
  10402. {
  10403. Result = !NonVisualDataModule->Busy;
  10404. if (Result && (Allowed == aaShortCut))
  10405. {
  10406. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  10407. Action->Update();
  10408. Result = Action->Enabled;
  10409. }
  10410. }
  10411. return Result;
  10412. }
  10413. //---------------------------------------------------------------------------
  10414. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  10415. {
  10416. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  10417. }
  10418. //---------------------------------------------------------------------------
  10419. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  10420. {
  10421. // This is somewhat redundant to LockWindow() call from
  10422. // TTerminalManager::TerminalReadDirectoryProgress.
  10423. // But disabling window is known not to block keyboard shorcuts
  10424. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  10425. if (Busy > 0)
  10426. {
  10427. if (NonVisualDataModule->Busy)
  10428. {
  10429. State = false;
  10430. }
  10431. else
  10432. {
  10433. NonVisualDataModule->StartBusy();
  10434. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  10435. LockWindow();
  10436. }
  10437. }
  10438. else if (Busy < 0)
  10439. {
  10440. UnlockWindow();
  10441. NonVisualDataModule->EndBusy();
  10442. }
  10443. else
  10444. {
  10445. State = NonVisualDataModule->Busy;
  10446. }
  10447. }
  10448. //---------------------------------------------------------------------------
  10449. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  10450. {
  10451. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  10452. if (Index >= 0)
  10453. {
  10454. TPopupMenu * PopupMenu = NULL;
  10455. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10456. if (Session != NULL)
  10457. {
  10458. SessionsPageControl->ActivePageIndex = Index;
  10459. if (DebugAlwaysTrue(SessionTabSwitched()))
  10460. {
  10461. // copied from TControl.WMContextMenu
  10462. SendCancelMode(SessionsPageControl);
  10463. // explicit popup instead of using PopupMenu property
  10464. // to avoid menu to popup somewhere within SessionTabSwitched above,
  10465. // while connecting yet not-connected session and hence
  10466. // allowing an access to commands over not-completely connected session
  10467. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  10468. }
  10469. }
  10470. else
  10471. {
  10472. if (SupportsLocalBrowser())
  10473. {
  10474. PopupMenu = NonVisualDataModule->NewTabPopup;
  10475. }
  10476. }
  10477. if (PopupMenu != NULL)
  10478. {
  10479. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  10480. PopupMenu->PopupComponent = SessionsPageControl;
  10481. PopupMenu->Popup(Point.x, Point.y);
  10482. }
  10483. }
  10484. Handled = true;
  10485. }
  10486. //---------------------------------------------------------------------------
  10487. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  10488. {
  10489. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  10490. }
  10491. //---------------------------------------------------------------------------
  10492. void __fastcall TCustomScpExplorerForm::ChangePassword()
  10493. {
  10494. Configuration->Usage->Inc(L"PasswordChanges");
  10495. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  10496. ChangePasswordData->DisableAuthentationsExceptPassword();
  10497. ChangePasswordData->ChangePassword = true;
  10498. std::unique_ptr<TTerminal> ChangePasswordSession(
  10499. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  10500. ChangePasswordSession->AutoReadDirectory = false;
  10501. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  10502. {
  10503. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  10504. }
  10505. else
  10506. {
  10507. Configuration->Usage->Inc(L"PasswordChangesFailed");
  10508. }
  10509. }
  10510. //---------------------------------------------------------------------------
  10511. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  10512. {
  10513. // No nice way to assert SSH2
  10514. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  10515. }
  10516. //---------------------------------------------------------------------------
  10517. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  10518. {
  10519. TOperationVisualizer OperationVisualizer;
  10520. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  10521. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  10522. }
  10523. //---------------------------------------------------------------------------
  10524. void __fastcall TCustomScpExplorerForm::FormAfterMonitorDpiChanged(TObject *, int OldDPI, int NewDPI)
  10525. {
  10526. DebugUsedParam(OldDPI);
  10527. DebugAssert(NewDPI == GetControlPixelsPerInch(this));
  10528. GlyphsModule->PixelsPerInch = NewDPI;
  10529. // Can be called too early when primary monitor has non-system dpi
  10530. if (FSessionColors != NULL)
  10531. {
  10532. RegenerateSessionColorsImageList();
  10533. }
  10534. UpdateTabsSize();
  10535. UpdateSessionsPageControlHeight();
  10536. QueueFileListColumnAutoSize();
  10537. }
  10538. //---------------------------------------------------------------------------
  10539. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  10540. {
  10541. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  10542. }
  10543. //---------------------------------------------------------------------
  10544. class TPublicControl : public TControl
  10545. {
  10546. friend class TCustomScpExplorerForm;
  10547. };
  10548. //---------------------------------------------------------------------------
  10549. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  10550. {
  10551. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  10552. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  10553. }
  10554. //---------------------------------------------------------------------------
  10555. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  10556. {
  10557. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  10558. OpenBookmark(Side, Bookmark);
  10559. }
  10560. //---------------------------------------------------------------------------
  10561. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  10562. {
  10563. DoBookmarkClick(osLocal, Sender);
  10564. }
  10565. //---------------------------------------------------------------------------
  10566. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  10567. {
  10568. DoBookmarkClick(osRemote, Sender);
  10569. }
  10570. //---------------------------------------------------------------------------
  10571. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  10572. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  10573. {
  10574. if (BookmarkList != NULL)
  10575. {
  10576. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  10577. if (IsLocalBrowserMode())
  10578. {
  10579. Side = osLocal;
  10580. }
  10581. if (!WinConfiguration->UseLocationProfiles)
  10582. {
  10583. std::unique_ptr<TStringList> Directories(new TStringList());
  10584. Directories->CaseSensitive = (Side == osRemote);
  10585. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10586. {
  10587. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10588. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  10589. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  10590. {
  10591. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10592. Item->Caption = EscapeHotkey(Directory);
  10593. Item->ShortCut = Bookmark->ShortCut;
  10594. Item->OnClick = OnBookmarkClick;
  10595. Item->Tag = reinterpret_cast<int>(Bookmark);
  10596. Directories->Add(Directory);
  10597. Menu->Add(Item.release());
  10598. }
  10599. }
  10600. }
  10601. else
  10602. {
  10603. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10604. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10605. {
  10606. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10607. if (!Bookmark->Node.IsEmpty())
  10608. {
  10609. Folders->Add(Bookmark->Node);
  10610. }
  10611. }
  10612. for (int Index = 0; Index < Folders->Count; Index++)
  10613. {
  10614. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10615. Item->Caption = Folders->Strings[Index];
  10616. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10617. Folders->Objects[Index] = Item.get();
  10618. Menu->Add(Item.release());
  10619. }
  10620. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10621. {
  10622. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10623. TTBCustomItem * Parent;
  10624. if (!Bookmark->Node.IsEmpty())
  10625. {
  10626. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10627. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10628. }
  10629. else
  10630. {
  10631. Parent = Menu;
  10632. }
  10633. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10634. Item->Caption = Bookmark->Name;
  10635. Item->ShortCut = Bookmark->ShortCut;
  10636. Item->OnClick = OnBookmarkClick;
  10637. Item->Tag = reinterpret_cast<int>(Bookmark);
  10638. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10639. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10640. {
  10641. // No hint for location profiles that are actually mere "bookmarks"
  10642. }
  10643. else
  10644. {
  10645. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10646. UnicodeString LongHint;
  10647. if (!Bookmark->Local.IsEmpty())
  10648. {
  10649. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10650. }
  10651. if (!Bookmark->Remote.IsEmpty())
  10652. {
  10653. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10654. }
  10655. AddToList(Hint, LongHint, L"|");
  10656. Item->Hint = Hint;
  10657. }
  10658. Parent->Add(Item.release());
  10659. }
  10660. }
  10661. AddMenuSeparator(Menu);
  10662. }
  10663. }
  10664. //---------------------------------------------------------------------------
  10665. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10666. {
  10667. Menu->Clear();
  10668. std::unique_ptr<TTBCustomItem> Item;
  10669. Item.reset(new TTBXItem(Owner));
  10670. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10671. Item->Options = Item->Options << tboDefault;
  10672. Menu->Add(Item.release());
  10673. AddMenuSeparator(Menu);
  10674. if (Terminal != NULL)
  10675. {
  10676. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10677. }
  10678. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10679. Item.reset(new TTBXItem(Owner));
  10680. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10681. Menu->Add(Item.release());
  10682. }
  10683. //---------------------------------------------------------------------------
  10684. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10685. {
  10686. bool Result = true;
  10687. bool Remote = !IsSideLocalBrowser(Side);
  10688. try
  10689. {
  10690. UnicodeString APath = Path;
  10691. if (Remote)
  10692. {
  10693. Terminal->ExceptionOnFail = true;
  10694. }
  10695. else
  10696. {
  10697. APath = ExpandEnvironmentVariables(Path);
  10698. }
  10699. try
  10700. {
  10701. DirView(Side)->Path = APath;
  10702. }
  10703. __finally
  10704. {
  10705. if (Remote)
  10706. {
  10707. Terminal->ExceptionOnFail = false;
  10708. }
  10709. }
  10710. }
  10711. catch (Exception & E)
  10712. {
  10713. if (!Remote || Terminal->Active)
  10714. {
  10715. ShowExtendedExceptionEx(Terminal, &E);
  10716. Result = false;
  10717. }
  10718. else
  10719. {
  10720. throw;
  10721. }
  10722. }
  10723. return Result;
  10724. }
  10725. //---------------------------------------------------------------------------
  10726. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10727. {
  10728. // Make sure there some feedback even when loading tiny local folders
  10729. TInstantOperationVisualizer Visualizer;
  10730. DirView(Side)->ReloadDirectory();
  10731. }
  10732. //---------------------------------------------------------------------------
  10733. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10734. {
  10735. if (Index == SessionsPageControl->TabIndex)
  10736. {
  10737. CloseTab();
  10738. }
  10739. else
  10740. {
  10741. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10742. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10743. TTerminalManager * Manager = TTerminalManager::Instance();
  10744. if (!Terminal->Active)
  10745. {
  10746. Manager->FreeTerminal(Terminal);
  10747. }
  10748. else
  10749. {
  10750. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10751. if (CanCloseQueue(Queue))
  10752. {
  10753. Manager->FreeTerminal(Terminal);
  10754. }
  10755. }
  10756. }
  10757. }
  10758. //---------------------------------------------------------------------------
  10759. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10760. {
  10761. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10762. if (Session == NULL)
  10763. {
  10764. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10765. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10766. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10767. //P = SessionsPageControl->ClientToScreen(P);
  10768. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10769. }
  10770. else
  10771. {
  10772. CloseSessionTab(Index);
  10773. }
  10774. }
  10775. //---------------------------------------------------------------------------
  10776. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10777. {
  10778. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10779. {
  10780. TInstantOperationVisualizer Visualizer;
  10781. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10782. // before we create a new data object
  10783. ClipboardClear();
  10784. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10785. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10786. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10787. // Intermediate variable to WORKAROUND a crash in compiler
  10788. TObjectList * ClipboardFakeMonitors =
  10789. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10790. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10791. if (FClipboardDragDropFilesEx.get() == NULL)
  10792. {
  10793. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10794. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10795. }
  10796. FClipboardDragDropFilesEx->FileList->Clear();
  10797. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10798. FClipboardDragDropFilesEx->CopyToClipboard();
  10799. FClipboardTerminal = Terminal;
  10800. // Need full paths, as cwd can be different once files are pasted
  10801. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10802. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10803. {
  10804. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10805. }
  10806. AppLogFmt(L"Created fake clipboard directory \"%s\" and started drive monitors", (FClipboardFakeDirectory));
  10807. }
  10808. }
  10809. //---------------------------------------------------------------------------
  10810. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10811. {
  10812. if (OpenClipboard(0))
  10813. {
  10814. AppLog(L"Clearing clipboard");
  10815. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10816. CloseClipboard();
  10817. }
  10818. }
  10819. //---------------------------------------------------------------------------
  10820. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10821. {
  10822. return !FClipboardFakeDirectory.IsEmpty();
  10823. }
  10824. //---------------------------------------------------------------------------
  10825. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10826. {
  10827. bool Deleted = RemoveDir(ApiPath(FClipboardFakeDirectory));
  10828. AppLogFmt(L"Clearing clipboard, deleted clipboard temporary folder \"%s\" - %d", (FClipboardFakeDirectory, int(Deleted)));
  10829. FClipboardFakeDirectory = UnicodeString();
  10830. FClipboardTerminal = NULL;
  10831. if (FDownloadingFromClipboard)
  10832. {
  10833. // We are called by the monitor, so attempt to release it would deadlock
  10834. FClipboardFakeMonitorsPendingReset = true;
  10835. }
  10836. else
  10837. {
  10838. FClipboardFakeMonitors.reset(NULL);
  10839. }
  10840. }
  10841. //---------------------------------------------------------------------------
  10842. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10843. {
  10844. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10845. {
  10846. ClipboardStop();
  10847. }
  10848. }
  10849. //---------------------------------------------------------------------------
  10850. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10851. {
  10852. TTerminalManager * Manager = TTerminalManager::Instance();
  10853. Manager->ActiveSession = FClipboardTerminal;
  10854. TTransferOperationParam Params;
  10855. Params.TargetDirectory = TargetDirectory;
  10856. Params.DragDrop = DragDrop;
  10857. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10858. }
  10859. //---------------------------------------------------------------------------
  10860. void TCustomScpExplorerForm::PasteFiles()
  10861. {
  10862. // Guard against possible race conditions
  10863. if (!FClipboardPasteTarget.IsEmpty())
  10864. {
  10865. // ClipboardFakeCreated might be called while we are here, so make sure we finish operating on the same path we started with
  10866. UnicodeString Target = FClipboardPasteTarget;
  10867. FClipboardPasteTarget = EmptyStr;
  10868. // Can fail as it can be e.g. locked by AV, so we retry that later
  10869. bool Removed = RemoveDir(ApiPath(Target));
  10870. AppLogFmt(L"First attempt to delete pasted fake clipboard directory \"%s\" - %d", (Target, int(Removed)));
  10871. try
  10872. {
  10873. if (NonVisualDataModule->Busy)
  10874. {
  10875. AppLog(L"Is busy, cannot paste");
  10876. }
  10877. else
  10878. {
  10879. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10880. FClipboardFakeMonitorsPendingReset = false;
  10881. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10882. TAutoFlag Flag(FDownloadingFromClipboard);
  10883. AppLog(L"Downloading to paste target...");
  10884. ClipboardDownload(ExtractFilePath(Target), NoConfirmation, true);
  10885. AppLog(L"Downloaded to paste target...");
  10886. }
  10887. }
  10888. __finally
  10889. {
  10890. if (!Removed)
  10891. {
  10892. Removed = RemoveDir(ApiPath(Target));
  10893. AppLogFmt(L"Second attempt to delete pasted fake clipboard directory \"%s\" - %d", (Target, int(Removed)));
  10894. }
  10895. }
  10896. }
  10897. }
  10898. //---------------------------------------------------------------------------
  10899. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10900. {
  10901. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10902. // (as it pumps a Windows message queue while being shutted down)
  10903. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10904. // Is can happen that creation of our temporary directory is detected (even though we are creating it before starting the monitors).
  10905. // It tent do happen with later copy-pastes, probably because everything is cached already and monitors start quickly.
  10906. !IsPathToSameFile(FClipboardFakeDirectory, FileName) &&
  10907. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10908. {
  10909. AppLogFmt(L"Fake clipboard directory pasted to \"%s\"", (FileName));
  10910. FClipboardPasteTarget = FileName;
  10911. // Particularly when pasting to external HDD, the fake directory is strangely recreated if removed here in the monitor callback.
  10912. // Postponing removing (or actually whole pasting operation) seems to help.
  10913. PostMessage(Handle, WM_PASTE_FILES, 0, 0);
  10914. }
  10915. }
  10916. //---------------------------------------------------------------------------
  10917. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10918. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10919. {
  10920. TFileMasks::TParams MaskParams;
  10921. MaskParams.Size = Size;
  10922. MaskParams.Modification = Modification;
  10923. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10924. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10925. // TUnixDirView::ItemFullFileName returns trailing backslash for directories, what filemask cannot deal with
  10926. if (!Local && Directory)
  10927. {
  10928. FileName = UnixExcludeTrailingBackslash(FileName);
  10929. }
  10930. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10931. {
  10932. bool ImplicitMatch;
  10933. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10934. !ImplicitMatch)
  10935. {
  10936. Color = Iter->Color;
  10937. break;
  10938. }
  10939. }
  10940. }
  10941. //---------------------------------------------------------------------------
  10942. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10943. {
  10944. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10945. if (!DirView->IsEditing())
  10946. {
  10947. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10948. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10949. // while searching
  10950. if (Key == VK_BACK)
  10951. {
  10952. if (FIncrementalSearchState.Searching)
  10953. {
  10954. if (FIncrementalSearchState.Text.Length() <= 1)
  10955. {
  10956. ResetIncrementalSearch();
  10957. }
  10958. else
  10959. {
  10960. UnicodeString NewText = LeftStr(FIncrementalSearchState.Text, FIncrementalSearchState.Text.Length() - 1);
  10961. IncrementalSearch(NewText, false, false);
  10962. }
  10963. Key = 0;
  10964. }
  10965. }
  10966. }
  10967. }
  10968. //---------------------------------------------------------------------------
  10969. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10970. {
  10971. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10972. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10973. {
  10974. // Filter control sequences.
  10975. // When not searching yet, prefer use of the space for toggling file selection
  10976. // (so we cannot incrementally search for a string starting with a space).
  10977. if ((Key > VK_SPACE) ||
  10978. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && FIncrementalSearchState.Searching))
  10979. {
  10980. IncrementalSearch(FIncrementalSearchState.Text + Key, false, false);
  10981. Key = 0;
  10982. }
  10983. }
  10984. }
  10985. //---------------------------------------------------------------------------
  10986. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10987. {
  10988. if (FIncrementalSearchState.Searching)
  10989. {
  10990. FIncrementalSearchState.Reset();
  10991. DirView(osCurrent)->UpdateStatusBar();
  10992. }
  10993. }
  10994. //---------------------------------------------------------------------------
  10995. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10996. {
  10997. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10998. if (Item == NULL)
  10999. {
  11000. MessageBeep(MB_ICONHAND);
  11001. }
  11002. else
  11003. {
  11004. TCustomDirView * ADirView = DirView(osCurrent);
  11005. {
  11006. TAutoNestingCounter Guard(FIncrementalSearching);
  11007. ADirView->FocusItem(Item);
  11008. if (ADirView->NortonLike == nlOff)
  11009. {
  11010. if ((ADirView->Selected != Item) ||
  11011. (ADirView->SelCount > 1))
  11012. {
  11013. ADirView->ClearSelection();
  11014. ADirView->Selected = Item;
  11015. }
  11016. }
  11017. }
  11018. FIncrementalSearchState.Searching = true;
  11019. FIncrementalSearchState.Text = Text;
  11020. Item->MakeVisible(false);
  11021. TListItem * NextItem = SearchFile(Text, true, Reverse);
  11022. FIncrementalSearchState.HaveNext = (NextItem != NULL) && (NextItem != Item);
  11023. ADirView->UpdateStatusBar();
  11024. }
  11025. }
  11026. //---------------------------------------------------------------------------
  11027. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  11028. {
  11029. int Index = Item->Index;
  11030. if (!Reverse)
  11031. {
  11032. Index++;
  11033. if (Index >= Item->Owner->Count)
  11034. {
  11035. Index = 0;
  11036. }
  11037. }
  11038. else
  11039. {
  11040. Index--;
  11041. if (Index < 0)
  11042. {
  11043. Index = Item->Owner->Count - 1;
  11044. }
  11045. }
  11046. return Item->Owner->Item[Index];
  11047. }
  11048. //---------------------------------------------------------------------------
  11049. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  11050. {
  11051. TCustomDirView * ADirView = DirView(osCurrent);
  11052. if (ADirView->Items->Count == 0)
  11053. {
  11054. return NULL;
  11055. }
  11056. else
  11057. {
  11058. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  11059. TListItem * Item = CurrentItem;
  11060. if (SkipCurrent)
  11061. {
  11062. Item = DebugNotNull(GetNextFile(Item, Reverse));
  11063. }
  11064. while (true)
  11065. {
  11066. bool Matches = false;
  11067. switch (WinConfiguration->PanelSearch)
  11068. {
  11069. case isNameStartOnly:
  11070. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item).SubString(1, Text.Length()), Text);
  11071. break;
  11072. case isName:
  11073. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item), Text);
  11074. break;
  11075. case isAll:
  11076. int ColCount = (ADirView->DirViewStyle == dvsReport) ? ADirView->ColProperties->Count : 1;
  11077. int Index = 0;
  11078. while ((Index < ColCount) && !Matches)
  11079. {
  11080. bool Visible = (ADirView->DirViewStyle == dvsReport) ? ADirView->ColProperties->Visible[Index] : true;
  11081. if (Visible)
  11082. {
  11083. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  11084. }
  11085. Index++;
  11086. }
  11087. break;
  11088. }
  11089. if (Matches)
  11090. {
  11091. return Item;
  11092. }
  11093. Item = GetNextFile(Item, Reverse);
  11094. if (Item == CurrentItem)
  11095. {
  11096. return NULL;
  11097. }
  11098. }
  11099. }
  11100. }
  11101. //---------------------------------------------------------------------------
  11102. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  11103. {
  11104. ResetIncrementalSearch();
  11105. }
  11106. //---------------------------------------------------------------------------
  11107. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  11108. {
  11109. if (Message.CharCode == VK_TAB)
  11110. {
  11111. if (!FIncrementalSearchState.Text.IsEmpty())
  11112. {
  11113. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  11114. bool Reverse = Shift.Contains(ssShift);
  11115. IncrementalSearch(FIncrementalSearchState.Text, true, Reverse);
  11116. Message.Result = 1;
  11117. return;
  11118. }
  11119. }
  11120. else if (Message.CharCode == VK_ESCAPE)
  11121. {
  11122. if (FIncrementalSearchState.Searching)
  11123. {
  11124. ResetIncrementalSearch();
  11125. Message.Result = 1;
  11126. return;
  11127. }
  11128. }
  11129. TForm::Dispatch(&Message);
  11130. }
  11131. //---------------------------------------------------------------------------
  11132. void __fastcall TCustomScpExplorerForm::Deactivate()
  11133. {
  11134. TForm::Deactivate();
  11135. // E.g. when switching to an internal editor window
  11136. ResetIncrementalSearch();
  11137. }
  11138. //---------------------------------------------------------------------------
  11139. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  11140. {
  11141. // When switching to another application
  11142. ResetIncrementalSearch();
  11143. }
  11144. //---------------------------------------------------------------------------
  11145. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  11146. {
  11147. ResetIncrementalSearch();
  11148. }
  11149. //---------------------------------------------------------------------------
  11150. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  11151. {
  11152. if (FIncrementalSearching <= 0)
  11153. {
  11154. ResetIncrementalSearch();
  11155. }
  11156. }
  11157. //---------------------------------------------------------------------------
  11158. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  11159. {
  11160. CountClicksForWindowPrint(this);
  11161. }
  11162. //---------------------------------------------------------------------------
  11163. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  11164. {
  11165. CenterReconnectToolbar();
  11166. }
  11167. //---------------------------------------------------------------------------
  11168. void TCustomScpExplorerForm::DoBrowseFile(TCustomDirView * DirView, const UnicodeString & FileName)
  11169. {
  11170. if (!FileName.IsEmpty())
  11171. {
  11172. DirView->FocusByName(FileName);
  11173. if (DirView->ItemFocused != NULL)
  11174. {
  11175. DirView->ItemFocused->Selected = true;
  11176. }
  11177. }
  11178. }
  11179. //---------------------------------------------------------------------------
  11180. void __fastcall TCustomScpExplorerForm::BrowseFile(const UnicodeString & FileName)
  11181. {
  11182. DoBrowseFile(RemoteDirView, FileName);
  11183. }
  11184. //---------------------------------------------------------------------------
  11185. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  11186. {
  11187. if (!FQueueStatusUpdating)
  11188. {
  11189. bool Refresh;
  11190. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  11191. {
  11192. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  11193. }
  11194. else
  11195. {
  11196. if (FQueueFileList->GetCount() > 0)
  11197. {
  11198. FQueueFileList->Clear();
  11199. Refresh = true;
  11200. }
  11201. }
  11202. int Count = FQueueFileList->GetCount();
  11203. if (QueueFileList->Items->Count != Count)
  11204. {
  11205. DebugAssert(Refresh);
  11206. QueueFileList->Items->Count = Count;
  11207. }
  11208. if (Refresh)
  11209. {
  11210. QueueFileList->Invalidate();
  11211. }
  11212. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  11213. }
  11214. }
  11215. //---------------------------------------------------------------------------
  11216. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  11217. {
  11218. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  11219. }
  11220. //---------------------------------------------------------------------------
  11221. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  11222. {
  11223. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  11224. // See TSynchronizeChecklistDialog::UpdateTimer
  11225. if (QueueFileList->Items->Count > 0)
  11226. {
  11227. QueueFileList->Items->Item[0]->MakeVisible(false);
  11228. }
  11229. UpdateQueueFileList();
  11230. }
  11231. //---------------------------------------------------------------------------
  11232. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  11233. {
  11234. Active = QueueView3->Focused() || QueueFileList->Focused();
  11235. }
  11236. //---------------------------------------------------------------------------
  11237. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  11238. {
  11239. QueueLabelUpdateStatus();
  11240. }
  11241. //---------------------------------------------------------------------------
  11242. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  11243. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  11244. {
  11245. int State = FQueueFileList->GetState(Item->Index);
  11246. if (State == qfsProcessed)
  11247. {
  11248. Sender->Canvas->Font->Color = clGrayText;
  11249. }
  11250. }
  11251. //---------------------------------------------------------------------------
  11252. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  11253. {
  11254. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  11255. QueueFileList->Column[0]->Width =
  11256. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  11257. }
  11258. //---------------------------------------------------------------------------
  11259. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  11260. {
  11261. QueueFileListColumnAutoSize();
  11262. }
  11263. //---------------------------------------------------------------------------
  11264. void __fastcall TCustomScpExplorerForm::CloseApp()
  11265. {
  11266. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  11267. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  11268. // So we clear the flag here.
  11269. DebugAssert(NonVisualDataModule->Busy);
  11270. NonVisualDataModule->EndBusy();
  11271. try
  11272. {
  11273. Close();
  11274. }
  11275. __finally
  11276. {
  11277. NonVisualDataModule->StartBusy();
  11278. }
  11279. }
  11280. //---------------------------------------------------------------------------
  11281. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  11282. {
  11283. return (Terminal != NULL) && Terminal->Active;
  11284. }
  11285. //---------------------------------------------------------------------------
  11286. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  11287. {
  11288. return (ManagedSession != NULL);
  11289. }
  11290. //---------------------------------------------------------------------------
  11291. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  11292. {
  11293. return IsActiveTerminal(Terminal);
  11294. }
  11295. //---------------------------------------------------------------------------
  11296. void TCustomScpExplorerForm::LocalLocalCopy(
  11297. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  11298. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  11299. {
  11300. DebugFail();
  11301. }
  11302. //---------------------------------------------------------------------------
  11303. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  11304. {
  11305. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  11306. }
  11307. //---------------------------------------------------------------------------
  11308. void TCustomScpExplorerForm::UpdateTabsSize()
  11309. {
  11310. // 1) Changing tab list triggers the OnResize.
  11311. // 2) Is called in TForm constructor, when we are not ready to call SessionListChanged (particularly when starting with colored session)
  11312. if (!FUpdatingSessionTabs && (FSessionColors != NULL))
  11313. {
  11314. // UpdateTabsCaptionTruncation should be enough
  11315. SessionListChanged(true);
  11316. }
  11317. }
  11318. //---------------------------------------------------------------------------
  11319. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  11320. {
  11321. UpdateTabsSize();
  11322. }
  11323. //---------------------------------------------------------------------------
  11324. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  11325. {
  11326. if (ASession == ManagedSession)
  11327. {
  11328. UpdateSession(ASession);
  11329. }
  11330. TSessionData * Data = ASession->StateData;
  11331. UnicodeString Result;
  11332. if (Side == osLocal)
  11333. {
  11334. Result = Data->LocalDirectory;
  11335. }
  11336. else if (DebugAlwaysTrue(Side == osOther))
  11337. {
  11338. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  11339. }
  11340. return Result;
  11341. }
  11342. //---------------------------------------------------------------------------
  11343. UnicodeString TCustomScpExplorerForm::GetTabHintSessionDetails(TManagedTerminal * ASession)
  11344. {
  11345. UnicodeString Title = TTerminalManager::Instance()->GetSessionTitle(ASession, false);
  11346. UnicodeString Result = ASession->SessionData->DefaultSessionName;
  11347. if (Title == Result)
  11348. {
  11349. Result = EmptyStr;
  11350. }
  11351. else
  11352. {
  11353. Result += L"\n";
  11354. }
  11355. return Result;
  11356. }
  11357. //---------------------------------------------------------------------------
  11358. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  11359. {
  11360. UnicodeString Result = GetTabHintSessionDetails(ASession);
  11361. if (!ASession->Active)
  11362. {
  11363. Result += LoadStr(STATUS_NOT_CONNECTED2);
  11364. }
  11365. else
  11366. {
  11367. Result += GetSessionPath(ASession, osRemote);
  11368. }
  11369. return Result;
  11370. }
  11371. //---------------------------------------------------------------------------
  11372. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  11373. {
  11374. return LoadStr(NEW_REMOTE_TAB_HINT);
  11375. }
  11376. //---------------------------------------------------------------------------
  11377. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  11378. {
  11379. if (Index >= 0)
  11380. {
  11381. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  11382. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  11383. if (TabSession == NULL)
  11384. {
  11385. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  11386. if (NewTabTabCaption != TabSheet->Caption)
  11387. {
  11388. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  11389. }
  11390. }
  11391. else
  11392. {
  11393. TTerminalManager * Manager = TTerminalManager::Instance();
  11394. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  11395. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  11396. }
  11397. }
  11398. }
  11399. //---------------------------------------------------------------------------
  11400. typedef std::vector<TListItem *> TListItemsVector;
  11401. struct TCalculateSizeOperation
  11402. {
  11403. TCalculateSizeStats Stats;
  11404. TListItemsVector ListItems;
  11405. size_t Index;
  11406. };
  11407. //---------------------------------------------------------------------------
  11408. void TCustomScpExplorerForm::CalculateDirectorySizes(TOperationSide Side)
  11409. {
  11410. TCalculateSizeOperation CalculateSizeOperation;
  11411. Side = GetSide(Side);
  11412. TCustomDirView * ADirView = DirView(Side);
  11413. bool LocalSide = IsSideLocalBrowser(Side);
  11414. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, LocalSide, NULL, true));
  11415. std::unique_ptr<TStrings> FileList(new TStringList());
  11416. for (int Index = 0; Index < AllFileList->Count; Index++)
  11417. {
  11418. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  11419. if (ADirView->ItemIsDirectory(Item))
  11420. {
  11421. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  11422. CalculateSizeOperation.ListItems.push_back(Item);
  11423. }
  11424. }
  11425. CalculateSizeOperation.Index = 0;
  11426. __int64 Size = 0;
  11427. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation, &CalculateSizeOperation);
  11428. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  11429. FOnFileOperationFinished = DirectorySizeCalculated;
  11430. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation, foCalculateSize);
  11431. TCalculatedSizes CalculatedSizes;
  11432. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  11433. if (LocalSide)
  11434. {
  11435. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide, Side);
  11436. ManagedSession->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  11437. }
  11438. else
  11439. {
  11440. TCalculateSizeParams Params;
  11441. Params.Stats = &CalculateSizeOperation.Stats;
  11442. Params.UseCache = true;
  11443. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  11444. }
  11445. }
  11446. //---------------------------------------------------------------------------
  11447. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(
  11448. TOperationSide Side, const UnicodeString & FileName, bool Success, bool NotCancelled)
  11449. {
  11450. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  11451. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  11452. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  11453. // optimization
  11454. bool GotExpectedItem =
  11455. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  11456. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  11457. int Index = -1;
  11458. if (DebugAlwaysTrue(GotExpectedItem))
  11459. {
  11460. Index = FCalculateSizeOperation->Index;
  11461. FCalculateSizeOperation->Index++;
  11462. }
  11463. else
  11464. {
  11465. TListItemsVector::iterator I =
  11466. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  11467. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  11468. {
  11469. Index = I - FCalculateSizeOperation->ListItems.begin();
  11470. }
  11471. }
  11472. if (DebugAlwaysTrue(Index >= 0) && Success && NotCancelled)
  11473. {
  11474. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  11475. ADirView->SetItemCalculatedSize(Item, Size);
  11476. }
  11477. }
  11478. //---------------------------------------------------------------------------
  11479. void __fastcall TCustomScpExplorerForm::MessageDockRequestDock(TObject *, TTBCustomDockableWindow *, bool & Accept)
  11480. {
  11481. Accept = false;
  11482. }
  11483. //---------------------------------------------------------------------------
  11484. void TCustomScpExplorerForm::AutoSizeColumns(TOperationSide Side)
  11485. {
  11486. // Send Ctrl+Gray+ to the list view
  11487. TKeyboardState KeyState;
  11488. GetKeyboardState(KeyState);
  11489. TKeyboardState NewKeyState;
  11490. memset(NewKeyState, 0, sizeof(NewKeyState));
  11491. NewKeyState[VK_CONTROL] = 0x81;
  11492. SetKeyboardState(NewKeyState);
  11493. SendMessage(DirView(Side)->Handle, WM_KEYDOWN, VK_ADD, 0);
  11494. SetKeyboardState(KeyState);
  11495. }
  11496. //---------------------------------------------------------------------------
  11497. void TCustomScpExplorerForm::QueueResetLayoutColumns()
  11498. {
  11499. LoadListViewStr(QueueView3, QueueViewLayoutDefault);
  11500. }
  11501. //---------------------------------------------------------------------------
  11502. void * TCustomScpExplorerForm::SaveFocus()
  11503. {
  11504. return NULL;
  11505. }
  11506. //---------------------------------------------------------------------------
  11507. void TCustomScpExplorerForm::RestoreFocus(void * Focus)
  11508. {
  11509. DebugUsedParam(Focus);
  11510. DebugAssert(Focus == NULL);
  11511. }
  11512. //---------------------------------------------------------------------------
  11513. void TCustomScpExplorerForm::IncrementalSearchStart()
  11514. {
  11515. TCustomDirView * ADirView = DirView(osCurrent);
  11516. ADirView->SetFocus();
  11517. if (!FIncrementalSearchState.Searching)
  11518. {
  11519. FIncrementalSearchState.Searching = true;
  11520. FIncrementalSearchState.HaveNext = false;
  11521. DebugAssert(FIncrementalSearchState.Text.IsEmpty());
  11522. ADirView->UpdateStatusBar();
  11523. }
  11524. }
  11525. //---------------------------------------------------------------------------
  11526. void __fastcall TCustomScpExplorerForm::RemoteDirViewThumbnailNeeded(
  11527. TUnixDirView *, TListItem * Item, TRemoteFile * File, const TSize & Size, TBitmap *& Bitmap)
  11528. {
  11529. // though when downloading to temporary folder, encryption should not be a problem
  11530. if (Terminal->IsCapable[fcBackgroundTransfers] &&
  11531. !File->IsDirectory)
  11532. {
  11533. UnicodeString FileName = File->FullFileName;
  11534. TFileMasks::TParams MaskParams;
  11535. MaskParams.Size = File->Size;
  11536. MaskParams.Modification = File->Modification;
  11537. if ((File->Size <= static_cast<__int64>(WinConfiguration->RemoteThumbnailSizeLimit) * 1024) &&
  11538. FRemoteThumbnailMask.Matches(FileName, false, File->IsDirectory, &MaskParams))
  11539. {
  11540. Bitmap = TTerminalManager::Instance()->ThumbnailNeeded(Terminal, Item->Index, File, Size);
  11541. }
  11542. }
  11543. }
  11544. //---------------------------------------------------------------------------
  11545. TThumbnailDownloadQueueItem * TCustomScpExplorerForm::AddThumbnailDownloadQueueItem(TManagedTerminal * ATerminal)
  11546. {
  11547. DebugAssert(ATerminal == Terminal);
  11548. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  11549. TemporaryFileCopyParam(CopyParam);
  11550. CopyParam.TransferMode = tmBinary;
  11551. UnicodeString SourceDir = ATerminal->CurrentDirectory;
  11552. UnicodeString TempDir, RootTempDir;
  11553. TemporaryDirectoryForRemoteFiles(SourceDir, CopyParam, true, TempDir, RootTempDir);
  11554. TTerminalQueue * AQueue = TTerminalManager::Instance()->FindQueueForTerminal(ATerminal);
  11555. DebugAssert((Queue == NULL) || (AQueue == Queue)); // we might get here before queue is set
  11556. TThumbnailDownloadQueueItem * QueueItem =
  11557. new TThumbnailDownloadQueueItem(this, Terminal, SourceDir, TempDir, &CopyParam);
  11558. AddQueueItem(AQueue, QueueItem, ATerminal);
  11559. return QueueItem;
  11560. }
  11561. //---------------------------------------------------------------------------
  11562. enum { qccRemoteItemVisible, qccRemoteItemRedraw };
  11563. typedef std::pair<int, UnicodeString> TRemoteItemVisibleData;
  11564. //---------------------------------------------------------------------------
  11565. void TCustomScpExplorerForm::PostThumbnailVisibleQueueQuery(int Index, const UnicodeString & FileName)
  11566. {
  11567. // The queue might be destroyed before the WM_QUEUE_CALLBACK is processed
  11568. TRemoteItemVisibleData * Data = new TRemoteItemVisibleData();
  11569. Data->first = Index;
  11570. Data->second = FileName;
  11571. PostMessage(Handle, WM_QUEUE_CALLBACK, qccRemoteItemVisible, reinterpret_cast<WPARAM>(Data));
  11572. }
  11573. //---------------------------------------------------------------------------
  11574. void TCustomScpExplorerForm::PostThumbnailDrawRequest(int Index)
  11575. {
  11576. PostMessage(Handle, WM_QUEUE_CALLBACK, qccRemoteItemRedraw, Index);
  11577. }
  11578. //---------------------------------------------------------------------------
  11579. void TCustomScpExplorerForm::WMQueueCallback(TMessage & Message)
  11580. {
  11581. int Command = Message.WParam;
  11582. if (Command == qccRemoteItemVisible)
  11583. {
  11584. TRemoteItemVisibleData * Data = reinterpret_cast<TRemoteItemVisibleData *>(Message.LParam);
  11585. int Index = Data->first;
  11586. UnicodeString FileName = Data->second;
  11587. delete Data;
  11588. TGuard Guard(Terminal->ThumbnailsSection.get());
  11589. bool Visible = False;
  11590. if (Terminal->ThumbnailsEnabled &&
  11591. // though we should better also disable thumbnails once we switch to different view style
  11592. (RemoteDirView->DirViewStyle == dvsThumbnail))
  11593. {
  11594. // The message might be processed, while we are already loading a different directory
  11595. if ((Index >= 0) &&
  11596. (Index < RemoteDirView->Items->Count))
  11597. {
  11598. TListItem * Item = RemoteDirView->Items->Item[Index];
  11599. Visible =
  11600. (RemoteDirView->ItemFullFileName(Item) == FileName) &&
  11601. ListView_IsItemVisible(RemoteDirView->Handle, Index);
  11602. }
  11603. }
  11604. Terminal->ThumbnailVisible(Index, FileName, Visible);
  11605. }
  11606. else if (Command == qccRemoteItemRedraw)
  11607. {
  11608. int Index = Message.LParam;
  11609. if ((Index >= 0) &&
  11610. (Index < RemoteDirView->Items->Count))
  11611. {
  11612. RemoteDirView->InvalidateItem(RemoteDirView->Items->Item[Index]);
  11613. }
  11614. }
  11615. }
  11616. //---------------------------------------------------------------------------
  11617. void TCustomScpExplorerForm::InitializeRemoteThumbnailMask()
  11618. {
  11619. UnicodeString DefaultRemoteThumbnailMask = L"*.jpg;*.jpeg;*.gif;*.png;*.svg;*.bmp;*.raw;*.ico;*.heic;*.webp";
  11620. UnicodeString RemoteThumbnailMask = WinConfiguration->RemoteThumbnailMask;
  11621. if (RemoteThumbnailMask.IsEmpty() ||
  11622. StartsStr(L"+", RemoteThumbnailMask) || StartsStr(L"-", RemoteThumbnailMask) || StartsStr(L"*", RemoteThumbnailMask))
  11623. {
  11624. std::unique_ptr<TStrings> RemoteThumbnailMasks(new TStringList());
  11625. while (!DefaultRemoteThumbnailMask.IsEmpty())
  11626. {
  11627. RemoteThumbnailMasks->Add(CutToChar(DefaultRemoteThumbnailMask, L';', true));
  11628. }
  11629. RemoteThumbnailMasks.reset(ProcessFeatures(RemoteThumbnailMasks.get(), RemoteThumbnailMask));
  11630. RemoteThumbnailMask = EmptyStr;
  11631. for (int Index = 0; Index < RemoteThumbnailMasks->Count; Index++)
  11632. {
  11633. AddToList(RemoteThumbnailMask, RemoteThumbnailMasks->Strings[Index], L";");
  11634. }
  11635. }
  11636. FRemoteThumbnailMask.Masks = RemoteThumbnailMask;
  11637. }
  11638. //---------------------------------------------------------------------------
  11639. void __fastcall TCustomScpExplorerForm::RemoteDirViewStartLoading(TObject *)
  11640. {
  11641. Terminal->StartLoadingDirectory();
  11642. }
  11643. //---------------------------------------------------------------------------
  11644. void __fastcall TCustomScpExplorerForm::RemoteDirViewStartReading(TObject *)
  11645. {
  11646. Terminal->DisableThumbnails();
  11647. }
  11648. //---------------------------------------------------------------------------
  11649. void TCustomScpExplorerForm::ChangeDirViewStyle(TOperationSide Side, TDirViewStyle DirViewStyle)
  11650. {
  11651. TCustomDirView * ADirView = DirView(Side);
  11652. if (ADirView->DirViewStyle != DirViewStyle)
  11653. {
  11654. if (DirViewStyle == dvsThumbnail)
  11655. {
  11656. if (dynamic_cast<TUnixDirView *>(ADirView) != NULL)
  11657. {
  11658. Configuration->Usage->Inc(L"ThumbnailsRemote");
  11659. }
  11660. else
  11661. {
  11662. Configuration->Usage->Inc(L"ThumbnailsLocal");
  11663. }
  11664. }
  11665. ADirView->DirViewStyle = DirViewStyle;
  11666. UpdateControls();
  11667. }
  11668. }