CustomScpExplorer.cpp 416 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424
  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. FEditorManager = new TEditorManager();
  211. FEditorManager->OnFileChange = ExecutedFileChanged;
  212. FEditorManager->OnFileReload = ExecutedFileReload;
  213. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  214. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  215. FLocalEditors = new TList();
  216. FQueueStatus = NULL;
  217. FQueueStatusSection = new TCriticalSection();
  218. FQueueStatusInvalidated = false;
  219. FQueueItemInvalidated = false;
  220. FQueueStatusUpdating = false;
  221. FQueueActedItem = NULL;
  222. FQueueController = new TQueueController(QueueView3);
  223. FQueueScrollOnDragOver = new TListViewScrollOnDragOver(QueueView3, true);
  224. FUserActionTimer = new TTimer(this);
  225. FUserActionTimer->Enabled = false;
  226. FUserActionTimer->Interval = 10;
  227. FUserActionTimer->OnTimer = UserActionTimer;
  228. FNotes = new TStringList();
  229. FNoteTimer = new TTimer(this);
  230. FNoteTimer->Enabled = false;
  231. FNoteTimer->OnTimer = NoteTimer;
  232. FOnNoteClick = NULL;
  233. FOle32Library = LoadLibrary(L"Ole32.dll");
  234. FDragMoveCursor = FOle32Library != NULL ?
  235. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  236. UseSystemSettings(this);
  237. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  238. static_cast<TObject*>(GetComponent(fcTransferList)));
  239. DebugAssert(TransferList != NULL);
  240. FTransferListHoverIndex = -1;
  241. TransferList->OnChange = TransferListChange;
  242. TransferList->OnDrawItem = TransferListDrawItem;
  243. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  244. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  245. UseDesktopFont(SessionsPageControl);
  246. UseDesktopFont(RemoteDirView);
  247. UseDesktopFont(RemoteDriveView);
  248. UseDesktopFont(QueueView3);
  249. UseDesktopFont(QueueFileList);
  250. UseDesktopFont(QueueLabel);
  251. UseDesktopFont(RemoteStatusBar);
  252. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  253. QueueSplitter->ShowHint = true;
  254. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  255. QueueFileListSplitter->ShowHint = true;
  256. RemotePanelSplitter->ShowHint = true;
  257. UpdateImages();
  258. FCustomCommandMenu = CreateTBXPopupMenu(this);
  259. FCustomCommandLocalFileList = NULL;
  260. FCustomCommandRemoteFileList = NULL;
  261. FSessionColors = new TPngImageList(this);
  262. FSessionColors->ColorDepth = cd32Bit;
  263. AddFixedSessionImages();
  264. SessionsPageControl->Images = FSessionColors;
  265. // Needed for Explorer (Commander calls it implicitly on its own)
  266. UpdateSessionsPageControlHeight();
  267. UpdateQueueLabel();
  268. CheckStoreTransition();
  269. CreateHiddenWindow();
  270. StartUpdates();
  271. // Start loading drive data, so that they are ready once we need them for StartCreationDirectoryMonitorsOnEachDrive
  272. // (necessary for "explorer" only, as "commander" loads it for its drive menu)
  273. DriveInfo->NeedData();
  274. }
  275. //---------------------------------------------------------------------------
  276. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  277. {
  278. AppLog(L"Destroying main window");
  279. FInvalid = true;
  280. ReleaseHiContrastTheme();
  281. if (FClipboardTerminal != NULL)
  282. {
  283. ClipboardClear(); // implies ClipboardStop
  284. }
  285. // this has to be one of the very first things to do
  286. StopUpdateThread();
  287. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  288. TTerminalManager::Instance()->ScpExplorer = NULL;
  289. delete FCustomCommandLocalFileList;
  290. delete FCustomCommandRemoteFileList;
  291. delete FCustomCommandMenu;
  292. delete FTrayIcon;
  293. FTrayIcon = NULL;
  294. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  295. delete FEditorManager;
  296. ForceCloseLocalEditors();
  297. delete FLocalEditors;
  298. HideFileFindDialog();
  299. if (FDelayedDeletionTimer)
  300. {
  301. DoDelayedDeletion(NULL);
  302. SAFE_DESTROY(FDelayedDeletionTimer);
  303. }
  304. SAFE_DESTROY(FDelayedDeletionList);
  305. // sometimes we do not get DDEnd so the list is not released
  306. SAFE_DESTROY(FDDFileList);
  307. DebugAssert(FSynchronizeController == NULL);
  308. CloseHandle(FDDExtMutex);
  309. FDDExtMutex = NULL;
  310. FreeLibrary(FOle32Library);
  311. FOle32Library = NULL;
  312. FDragMoveCursor = NULL;
  313. DebugAssert(!FErrorList);
  314. if (FEverShown)
  315. {
  316. // when window is never shown (like when running command-line operation),
  317. // particularly window site is not restored correctly (BoundsRect value set
  318. // in RestoreForm gets lost during handle allocation), so we do not want
  319. // it to be stored
  320. AppLog(L"Storing main window configuration");
  321. StoreParams();
  322. }
  323. ManagedSession = NULL;
  324. Queue = NULL;
  325. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  326. NonVisualDataModule->ScpExplorer = NULL;
  327. delete FQueueScrollOnDragOver;
  328. FQueueScrollOnDragOver = NULL;
  329. delete FQueueController;
  330. FQueueController = NULL;
  331. delete FQueueStatusSection;
  332. FQueueStatusSection = NULL;
  333. delete FQueueStatus;
  334. FQueueStatus = NULL;
  335. delete FUserActionTimer;
  336. FUserActionTimer = NULL;
  337. delete FNoteTimer;
  338. delete FNotes;
  339. delete FNoteData;
  340. SAFE_DESTROY(FDocks);
  341. SessionsPageControl->Images = NULL;
  342. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  343. SAFE_DESTROY(FSessionColors);
  344. SAFE_DESTROY(FSessionsDragDropFilesEx);
  345. SAFE_DESTROY(FHistoryMenu[0][0]);
  346. SAFE_DESTROY(FHistoryMenu[0][1]);
  347. SAFE_DESTROY(FHistoryMenu[1][0]);
  348. SAFE_DESTROY(FHistoryMenu[1][1]);
  349. if (FHiddenWindow != NULL)
  350. {
  351. DestroyWindow(FHiddenWindow);
  352. FHiddenWindow = NULL;
  353. }
  354. AppLog(L"Main window destroyed");
  355. }
  356. //---------------------------------------------------------------------------
  357. void TCustomScpExplorerForm::ReleaseHiContrastTheme()
  358. {
  359. if (FHiContrastTheme != NULL)
  360. {
  361. ReleaseTBXTheme(FHiContrastTheme);
  362. FHiContrastTheme = NULL;
  363. }
  364. }
  365. //---------------------------------------------------------------------------
  366. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  367. {
  368. std::unique_ptr<TSessionData> Data;
  369. if (!Session.IsEmpty())
  370. {
  371. bool DefaultsOnly;
  372. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  373. }
  374. TTerminalManager * Manager = TTerminalManager::Instance();
  375. for (int Index = 0; Index < Manager->Count; Index++)
  376. {
  377. TTerminal * Terminal = Manager->Sessions[Index];
  378. if (Session.IsEmpty() ||
  379. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  380. {
  381. if (Path.IsEmpty())
  382. {
  383. Terminal->ClearCaches();
  384. }
  385. else
  386. {
  387. Terminal->DirectoryModified(Path, true);
  388. }
  389. }
  390. }
  391. // We should flag a pending refresh for the background terminals or busy foreground terminals
  392. if (HasActiveTerminal() &&
  393. CanCommandLineFromAnotherInstance() &&
  394. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  395. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  396. {
  397. Terminal->ReloadDirectory();
  398. }
  399. }
  400. //---------------------------------------------------------------------------
  401. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  402. {
  403. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  404. AppLogFmt(L"Received copy data message with size %d", (int(CopyData->cbData)));
  405. size_t MessageSize = sizeof(TCopyDataMessage);
  406. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  407. if (Result)
  408. {
  409. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  410. Result = (Message.Version == TCopyDataMessage::Version1);
  411. if (Result)
  412. {
  413. switch (Message.Command)
  414. {
  415. case TCopyDataMessage::CommandCanCommandLine:
  416. Result = CanCommandLineFromAnotherInstance();
  417. if (Result)
  418. {
  419. AppLog(L"Can process command-line from other instance");
  420. }
  421. else
  422. {
  423. AppLog(L"Cannot process command-line from other instance");
  424. }
  425. break;
  426. case TCopyDataMessage::CommandCommandLine:
  427. {
  428. UnicodeString CommandLine(Message.CommandLine);
  429. Result = CommandLineFromAnotherInstance(CommandLine);
  430. }
  431. break;
  432. case TCopyDataMessage::RefreshPanel:
  433. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  434. break;
  435. case TCopyDataMessage::MainWindowCheck:
  436. Result = true;
  437. break;
  438. default:
  439. Result = false;
  440. break;
  441. }
  442. }
  443. }
  444. Message.Result = Result ? 1 : 0;
  445. }
  446. //---------------------------------------------------------------------------
  447. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  448. {
  449. WNDCLASS WindowClass = {0};
  450. WindowClass.lpfnWndProc = DefWindowProc;
  451. WindowClass.hInstance = HInstance;
  452. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  453. FHiddenWindow = NULL;
  454. if (RegisterClass(&WindowClass))
  455. {
  456. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  457. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  458. }
  459. }
  460. //---------------------------------------------------------------------------
  461. bool __fastcall TCustomScpExplorerForm::CanConsole()
  462. {
  463. return
  464. HasActiveTerminal() &&
  465. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  466. }
  467. //---------------------------------------------------------------------------
  468. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  469. {
  470. bool Result = Visible && !NonVisualDataModule->Busy;
  471. return Result;
  472. }
  473. //---------------------------------------------------------------------------
  474. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  475. const UnicodeString & CommandLine)
  476. {
  477. TProgramParams Params(CommandLine);
  478. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  479. if (Result)
  480. {
  481. NonVisualDataModule->StartBusy();
  482. try
  483. {
  484. // this action is initiated from another process,
  485. // so it's likely that our window is not visible,
  486. // and user won't see what is going on
  487. Application->BringToFront();
  488. // reload sessions as we may be asked to open a session
  489. // just stored by another instance
  490. StoredSessions->Reload();
  491. UnicodeString SessionName = Params.Param[1];
  492. std::unique_ptr<TObjectList> DataList(new TObjectList());
  493. try
  494. {
  495. UnicodeString DownloadFile; // unused
  496. int Flags = GetCommandLineParseUrlFlags(&Params);
  497. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  498. }
  499. catch (EAbort &)
  500. {
  501. // Happens when opening session in PuTTY and in other situations,
  502. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  503. // to process the command-line on its own (or to pass it to yet another instance)
  504. }
  505. if (DataList->Count > 0)
  506. {
  507. TTerminalManager * Manager = TTerminalManager::Instance();
  508. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  509. }
  510. }
  511. __finally
  512. {
  513. NonVisualDataModule->EndBusy();
  514. }
  515. }
  516. return Result;
  517. }
  518. //---------------------------------------------------------------------------
  519. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  520. {
  521. TManagedTerminal * Result;
  522. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  523. {
  524. Result = FManagedSession;
  525. }
  526. else
  527. {
  528. Result = NULL;
  529. }
  530. return Result;
  531. }
  532. //---------------------------------------------------------------------------
  533. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  534. {
  535. if (FManagedSession != value)
  536. {
  537. TWindowLock Lock(this);
  538. SessionChanging();
  539. DoSetManagedSession(value, false);
  540. }
  541. }
  542. //---------------------------------------------------------------------------
  543. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  544. {
  545. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  546. FManagedSession = value;
  547. {
  548. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect, false);
  549. TAutoFlag SessionChangingFlag(FSessionChanging);
  550. SessionChanged(Replace);
  551. }
  552. if (Terminal != NULL)
  553. {
  554. TransferPresetAutoSelect();
  555. }
  556. // Update app and tab titles, prevented above by FSessionChanging.
  557. UpdateControls();
  558. }
  559. //---------------------------------------------------------------------------
  560. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  561. {
  562. FEditorManager->ProcessFiles(FileTerminalReplaced, value);
  563. DoSetManagedSession(value, true);
  564. }
  565. //---------------------------------------------------------------------------
  566. void __fastcall TCustomScpExplorerForm::SessionChanging()
  567. {
  568. if (ManagedSession != NULL)
  569. {
  570. UpdateSession(ManagedSession);
  571. }
  572. }
  573. //---------------------------------------------------------------------------
  574. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  575. {
  576. if (ManagedSession != NULL)
  577. {
  578. UpdateSessionColor(static_cast<TColor>(ManagedSession->StateData->Color));
  579. }
  580. if (!TTerminalManager::Instance()->Updating)
  581. {
  582. SessionListChanged();
  583. }
  584. if (Terminal != NULL)
  585. {
  586. TObject * State = NULL;
  587. if (WinConfiguration->PreservePanelState)
  588. {
  589. State = Terminal->RemoteExplorerState;
  590. DirView(osRemote)->AnnounceState(State);
  591. }
  592. try
  593. {
  594. if (Replaced)
  595. {
  596. RemoteDirView->ReplaceTerminal(Terminal);
  597. }
  598. else
  599. {
  600. RemoteDirView->Terminal = Terminal;
  601. }
  602. if (Terminal->Active)
  603. {
  604. Terminal->RefreshDirectory();
  605. }
  606. if (State != NULL)
  607. {
  608. DirView(osRemote)->RestoreState(State);
  609. }
  610. }
  611. __finally
  612. {
  613. DirView(osRemote)->AnnounceState(NULL);
  614. }
  615. }
  616. else
  617. {
  618. RemoteDirView->Terminal = NULL;
  619. }
  620. InitStatusBar();
  621. UpdateTransferList();
  622. // Update panels Enable state before refreshing the labels
  623. UpdateControls();
  624. }
  625. //---------------------------------------------------------------------------
  626. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  627. {
  628. if (Queue != value)
  629. {
  630. if (FQueue != NULL)
  631. {
  632. FQueue->OnListUpdate = NULL;
  633. FQueue->OnQueueItemUpdate = NULL;
  634. }
  635. FQueue = value;
  636. if (FQueue != NULL)
  637. {
  638. DebugAssert(FQueue->OnListUpdate == NULL);
  639. FQueue->OnListUpdate = QueueListUpdate;
  640. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  641. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  642. }
  643. QueueChanged();
  644. }
  645. }
  646. //---------------------------------------------------------------------------
  647. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  648. TListItem * Item)
  649. {
  650. if (FQueueActedItem == Item)
  651. {
  652. FQueueActedItem = NULL;
  653. if ((QueueView3->PopupMenu != NULL) &&
  654. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  655. {
  656. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  657. ReleaseCapture();
  658. }
  659. }
  660. if (Item->Data == FPendingQueueActionItem)
  661. {
  662. FPendingQueueActionItem = NULL;
  663. }
  664. }
  665. //---------------------------------------------------------------------------
  666. bool TCustomScpExplorerForm::IsAnythingQueued()
  667. {
  668. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  669. }
  670. //---------------------------------------------------------------------------
  671. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  672. {
  673. {
  674. TGuard Guard(FQueueStatusSection);
  675. FQueueStatusInvalidated = false;
  676. if (FQueue != NULL)
  677. {
  678. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  679. }
  680. }
  681. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  682. {
  683. FMaxQueueLength = FQueueStatus->Count;
  684. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  685. }
  686. {
  687. TAutoFlag Flag(FQueueStatusUpdating);
  688. FQueueController->UpdateQueueStatus(FQueueStatus);
  689. }
  690. UpdateQueueFileList();
  691. SetQueueProgress();
  692. UpdateQueueView();
  693. bool IsEmpty = !IsAnythingQueued();
  694. if (IsEmpty && (Terminal != NULL))
  695. {
  696. TOnceDoneOperation OnceDoneOperation =
  697. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  698. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  699. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  700. {
  701. FQueue->Enabled = false;
  702. }
  703. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  704. {
  705. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  706. }
  707. }
  708. }
  709. //---------------------------------------------------------------------------
  710. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  711. {
  712. UnicodeString Result;
  713. if (FQueueStatus != NULL)
  714. {
  715. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  716. {
  717. TFileOperationProgressType * ProgressData =
  718. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  719. if ((ProgressData != NULL) && ProgressData->InProgress)
  720. {
  721. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  722. }
  723. }
  724. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  725. {
  726. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  727. }
  728. }
  729. return Result;
  730. }
  731. //---------------------------------------------------------------------------
  732. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  733. {
  734. ComponentVisible[fcQueueView] =
  735. !IsLocalBrowserMode() &&
  736. ((WinConfiguration->QueueView.Show == qvShow) ||
  737. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  738. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  739. }
  740. //---------------------------------------------------------------------------
  741. void __fastcall TCustomScpExplorerForm::QueueChanged()
  742. {
  743. if (FQueueStatus != NULL)
  744. {
  745. // UpdateFileList implementation relies on the status being recreated when session changes
  746. delete FQueueStatus;
  747. FQueueStatus = NULL;
  748. }
  749. UpdateQueueStatus(true);
  750. }
  751. //---------------------------------------------------------------------------
  752. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  753. {
  754. if (FQueue == Queue)
  755. {
  756. FQueueStatusInvalidated = true;
  757. }
  758. }
  759. //---------------------------------------------------------------------------
  760. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  761. TQueueItem * Item)
  762. {
  763. if (FQueue == Queue)
  764. {
  765. TGuard Guard(FQueueStatusSection);
  766. // this may be running in parallel with QueueChanged
  767. if (FQueueStatus != NULL)
  768. {
  769. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  770. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  771. {
  772. FRefreshLocalDirectory = (QueueItem == NULL) ||
  773. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  774. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  775. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  776. }
  777. if (QueueItem != NULL)
  778. {
  779. QueueItem->UserData = (void*)true;
  780. FQueueItemInvalidated = true;
  781. }
  782. }
  783. }
  784. }
  785. //---------------------------------------------------------------------------
  786. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  787. {
  788. // during standalone editing, we have no way to see/control queue,
  789. // so we have to always popup prompts automatically
  790. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  791. }
  792. //---------------------------------------------------------------------------
  793. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  794. {
  795. if (FQueueStatus != NULL)
  796. {
  797. bool QueueAutoPopup = IsQueueAutoPopup();
  798. bool NeedRefresh = FQueueController->NeedRefresh();
  799. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  800. FQueueItemInvalidated = false;
  801. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  802. bool Updated = false;
  803. TQueueItemProxy * QueueItem;
  804. bool WasUserAction;
  805. for (int Index = 0; Index < Limit; Index++)
  806. {
  807. bool Update = false;
  808. QueueItem = FQueueStatus->Items[Index];
  809. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  810. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  811. (FPendingQueueActionItem == NULL))
  812. {
  813. FPendingQueueActionItem = QueueItem;
  814. FUserActionTimer->Enabled = true;
  815. }
  816. if ((bool)QueueItem->UserData)
  817. {
  818. QueueItem->UserData = (void*)false;
  819. QueueItem->Update();
  820. Updated = true;
  821. Update = true;
  822. }
  823. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  824. {
  825. Update = true;
  826. }
  827. if (Update)
  828. {
  829. FQueueController->RefreshQueueItem(QueueItem);
  830. }
  831. }
  832. if (Updated)
  833. {
  834. NonVisualDataModule->UpdateNonVisibleActions();
  835. SetQueueProgress();
  836. UpdateQueueFileList();
  837. }
  838. }
  839. }
  840. //---------------------------------------------------------------------------
  841. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  842. {
  843. if (FTaskbarList != NULL)
  844. {
  845. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  846. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  847. }
  848. }
  849. //---------------------------------------------------------------------------
  850. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  851. {
  852. if (Progress >= 0)
  853. {
  854. if (FTaskbarList != NULL)
  855. {
  856. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  857. }
  858. }
  859. else
  860. {
  861. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  862. }
  863. }
  864. //---------------------------------------------------------------------------
  865. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  866. {
  867. int OverallProgress;
  868. // FProgressForm is null when this is called from SetQueueProgress
  869. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  870. {
  871. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  872. }
  873. else if (!ProgressData->IsIndeterminate())
  874. {
  875. OverallProgress = ProgressData->OverallProgress();
  876. }
  877. else
  878. {
  879. OverallProgress = -1;
  880. }
  881. SetTaskbarListProgressValue(OverallProgress);
  882. }
  883. //---------------------------------------------------------------------------
  884. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  885. {
  886. TTerminalManager::Instance()->QueueStatusUpdated();
  887. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  888. {
  889. // Do not animate task bar button, when items are queued, but processing is disabled
  890. if ((FQueueStatus != NULL) && (FQueueStatus->ActivePrimaryCount > 0))
  891. {
  892. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  893. {
  894. TFileOperationProgressType * ProgressData = NULL;
  895. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  896. {
  897. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  898. }
  899. if ((ProgressData != NULL) &&
  900. ProgressData->InProgress)
  901. {
  902. SetTaskbarListProgressValue(ProgressData);
  903. }
  904. else
  905. {
  906. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  907. }
  908. }
  909. else
  910. {
  911. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  912. }
  913. }
  914. else
  915. {
  916. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  917. }
  918. }
  919. UpdateQueueLabel();
  920. }
  921. //---------------------------------------------------------------------------
  922. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  923. {
  924. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  925. if (FQueueStatus != NULL)
  926. {
  927. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  928. if (ActiveAndPendingPrimaryCount > 0)
  929. {
  930. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  931. }
  932. }
  933. QueueLabel->Caption = Caption;
  934. }
  935. //---------------------------------------------------------------------------
  936. void TCustomScpExplorerForm::CheckStoreTransition()
  937. {
  938. if (WinConfiguration->StoreTransition == stInit)
  939. {
  940. if (IsUWP())
  941. {
  942. WinConfiguration->StoreTransition = stStoreFresh;
  943. AppLog(L"Fresh UWP/store installation");
  944. }
  945. else
  946. {
  947. WinConfiguration->StoreTransition = stStandard;
  948. AppLog(L"Fresh standard installation");
  949. }
  950. }
  951. else if ((WinConfiguration->StoreTransition == stStandard) && IsUWP())
  952. {
  953. WinConfiguration->StoreTransition = stStoreMigrated;
  954. WinConfiguration->FirstRun = WinConfiguration->FirstRun + L"=>" + StandardDatestamp();
  955. AppLog(L"Standard installation migrated to store installation");
  956. }
  957. if (WinConfiguration->StoreTransition == stStoreMigrated)
  958. {
  959. TTBXToolbar * Toolbar = new TTBXToolbar(MessageDock);
  960. Toolbar->Parent = MessageDock;
  961. Toolbar->FullSize = true;
  962. Toolbar->Color = Application->HintColor;
  963. TTBXLabelItem * LabelItem;
  964. LabelItem = new TTBXLabelItem(Toolbar);
  965. // Convert to explicit RGB "black" color, to make sure it is contrasting even in Dark theme
  966. // (TTBXOfficeXPTheme.GetStandardColor would convert clWindow to "white" in dark theme)
  967. LabelItem->FontSettings->Color = static_cast<TColor>(GetSysColor(clWindow));
  968. LabelItem->Margin = ScaleByTextHeight(MessageDock, 2); // Is this automatically rescaled?
  969. LabelItem->Caption = LoadStr(STORE_MIGRATION_THANKS);
  970. Toolbar->Items->Add(LabelItem);
  971. TTBXCustomItem * LinkItem = new TTBXItem(Toolbar);
  972. LinkItem->FontSettings->Color = LinkColor;
  973. LinkItem->Caption = LoadStr(STORE_MIGRATION_LEARN);
  974. LinkItem->OnClick = StoreTransitionLinkClick;
  975. Toolbar->Items->Add(LinkItem);
  976. AddMenuSeparator(Toolbar->Items);
  977. TTBXCustomItem * CloseItem = new TTBXItem(Toolbar);
  978. CloseItem->FontSettings->Color = static_cast<TColor>(GetSysColor(clWindow));
  979. CloseItem->Caption = LoadStr(MESSAGE_DISMISS);
  980. CloseItem->OnClick = StoreTransitionCloseClick;
  981. Toolbar->Items->Add(CloseItem);
  982. GiveTBItemPriority(CloseItem);
  983. }
  984. }
  985. //---------------------------------------------------------------------------
  986. void __fastcall TCustomScpExplorerForm::StoreTransitionLinkClick(TObject *)
  987. {
  988. ShowHelp(HELP_STORE_TRANSITION);
  989. }
  990. //---------------------------------------------------------------------------
  991. void __fastcall TCustomScpExplorerForm::StoreTransitionCloseClick(TObject * Sender)
  992. {
  993. TTBCustomItem * Item = DebugNotNull(dynamic_cast<TTBCustomItem *>(Sender));
  994. TTBXToolbar * Toolbar = DebugNotNull(dynamic_cast<TTBXToolbar *>(DebugNotNull(DebugNotNull(Item->Parent)->ParentComponent)));
  995. delete Toolbar;
  996. WinConfiguration->StoreTransition = stStoreAcknowledged;
  997. UpdateControls();
  998. }
  999. //---------------------------------------------------------------------------
  1000. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  1001. {
  1002. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  1003. static_cast<TComponent*>(GetComponent(fcTransferList)));
  1004. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  1005. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  1006. TransferList->Strings->BeginUpdate();
  1007. try
  1008. {
  1009. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  1010. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  1011. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  1012. if (FTransferDropDownHint.IsEmpty())
  1013. {
  1014. FTransferDropDownHint = TransferDropDown->Hint;
  1015. }
  1016. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  1017. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  1018. TransferDropDown->Text = StripHotkey(Name);
  1019. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  1020. (FTransferDropDownHint, StripHotkey(Name),
  1021. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  1022. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  1023. // update the label, otherwise when it is updated only on the first draw
  1024. // of the list, it is drawn "bold" for some reason
  1025. FTransferListHoverIndex = TransferList->ItemIndex;
  1026. UpdateTransferLabel();
  1027. }
  1028. __finally
  1029. {
  1030. TransferList->Strings->EndUpdate();
  1031. }
  1032. }
  1033. //---------------------------------------------------------------------------
  1034. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  1035. {
  1036. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  1037. DebugAssert(Toolbar != NULL);
  1038. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  1039. }
  1040. //---------------------------------------------------------------------------
  1041. void __fastcall TCustomScpExplorerForm::UpdateActions()
  1042. {
  1043. TForm::UpdateActions();
  1044. if (ComponentVisible[fcCustomCommandsBand])
  1045. {
  1046. UpdateCustomCommandsToolbar();
  1047. }
  1048. }
  1049. //---------------------------------------------------------------------------
  1050. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  1051. {
  1052. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  1053. }
  1054. //---------------------------------------------------------------------------
  1055. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  1056. {
  1057. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  1058. {
  1059. DirView->RowSelect = WinConfiguration->FullRowSelect;
  1060. // Without this, the panel turns gray and unused part of header turns black
  1061. DirView->Perform(CM_COLORCHANGED, 0, 0);
  1062. }
  1063. }
  1064. //---------------------------------------------------------------------------
  1065. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  1066. {
  1067. Color = GetBtnFaceColor();
  1068. DebugAssert(Configuration && RemoteDirView);
  1069. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1070. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  1071. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  1072. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  1073. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1074. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1075. RemoteDirView->AlwaysSortDirectoriesByName = WinConfiguration->AlwaysSortDirectoriesByName;
  1076. UpdateRowSelect(RemoteDirView);
  1077. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1078. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  1079. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  1080. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1081. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1082. UpdateSessionsPageControlHeight();
  1083. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1084. UpdateToolbarDisplayMode();
  1085. UpdateTransferList();
  1086. if (Terminal != NULL)
  1087. {
  1088. TransferPresetAutoSelect();
  1089. }
  1090. UpdateQueueView();
  1091. UpdateControls();
  1092. // this can be called even before constuctor finishes.
  1093. if (FEditorManager != NULL)
  1094. {
  1095. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  1096. }
  1097. // this can be called even before constuctor finishes.
  1098. if (FLocalEditors != NULL)
  1099. {
  1100. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  1101. {
  1102. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  1103. }
  1104. }
  1105. if (ComponentVisible[fcCustomCommandsBand])
  1106. {
  1107. UpdateCustomCommandsToolbar();
  1108. }
  1109. // show only when keeping queue items forever,
  1110. // otherwise, it's enough to have it in the context menu
  1111. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1112. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1113. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1114. {
  1115. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1116. FFileColorsCurrent = WinConfiguration->FileColors;
  1117. FileColorsChanged();
  1118. }
  1119. if ((GetNewTabTab()->CaptionTruncation != GetNewTabTabCaptionTruncation()) &&
  1120. (FSessionColors != NULL))
  1121. {
  1122. SessionListChanged();
  1123. }
  1124. if (GlyphsModule->LargerToolbar != WinConfiguration->LargerToolbar)
  1125. {
  1126. GlyphsModule->LargerToolbar = WinConfiguration->LargerToolbar;
  1127. if (FSessionColors != NULL)
  1128. {
  1129. RegenerateSessionColorsImageList();
  1130. UpdateSessionsPageControlHeight();
  1131. }
  1132. }
  1133. }
  1134. //---------------------------------------------------------------------------
  1135. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1136. {
  1137. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1138. DirView->Invalidate();
  1139. }
  1140. //---------------------------------------------------------------------------
  1141. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1142. {
  1143. DoFileColorsChanged(RemoteDirView);
  1144. }
  1145. //---------------------------------------------------------------------------
  1146. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1147. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1148. void * /*Arg*/)
  1149. {
  1150. if (Token != NULL)
  1151. {
  1152. TForm * Editor = dynamic_cast<TForm*>(Token);
  1153. DebugAssert(Editor != NULL);
  1154. ReconfigureEditorForm(Editor);
  1155. }
  1156. }
  1157. //---------------------------------------------------------------------------
  1158. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1159. {
  1160. WinConfiguration->DDTransferConfirmation = asOn;
  1161. }
  1162. //---------------------------------------------------------------------------
  1163. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1164. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1165. {
  1166. // noop
  1167. }
  1168. //---------------------------------------------------------------------------
  1169. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1170. {
  1171. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1172. }
  1173. //---------------------------------------------------------------------------
  1174. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1175. {
  1176. if (DoNotShowAgain)
  1177. {
  1178. if (DragDrop)
  1179. {
  1180. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1181. {
  1182. PopupTrayBalloon(
  1183. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1184. }
  1185. WinConfiguration->DDTransferConfirmation = asOff;
  1186. }
  1187. else
  1188. {
  1189. WinConfiguration->ConfirmTransferring = false;
  1190. }
  1191. }
  1192. else
  1193. {
  1194. // User explicitly unchecked "do not show again",
  1195. // so show him the dialog the next time
  1196. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1197. {
  1198. WinConfiguration->DDTransferConfirmation = asOn;
  1199. }
  1200. }
  1201. }
  1202. //---------------------------------------------------------------------------
  1203. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1204. TTransferDirection Direction, TTransferType Type, bool Temp,
  1205. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1206. bool Confirm, bool DragDrop, int Options)
  1207. {
  1208. bool Result = true;
  1209. DebugAssert(HasActiveTerminal());
  1210. // these parameters are known in advance
  1211. int Params =
  1212. FLAGMASK(Type == ttMove, cpDelete);
  1213. bool ToTemp = (Temp && (Direction == tdToLocal));
  1214. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1215. {
  1216. bool DisableNewerOnly =
  1217. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1218. ToTemp;
  1219. Options |=
  1220. FLAGMASK(ToTemp, coTemp) |
  1221. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1222. coDoNotShowAgain;
  1223. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1224. int CopyParamAttrs = (Direction == tdToRemote ?
  1225. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1226. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1227. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1228. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1229. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1230. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1231. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1232. if (Result)
  1233. {
  1234. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1235. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1236. }
  1237. }
  1238. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1239. {
  1240. Configuration->Usage->Inc(L"TransfersOnBackground");
  1241. // these parameter are known only after transfer dialog
  1242. Params |=
  1243. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1244. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1245. Result = false;
  1246. ClearTransferSourceSelection(Direction);
  1247. }
  1248. return Result;
  1249. }
  1250. //---------------------------------------------------------------------------
  1251. void TCustomScpExplorerForm::ClearOperationSelection(TOperationSide Side)
  1252. {
  1253. if (FOnFileOperationFinished != NULL)
  1254. {
  1255. FOnFileOperationFinished(Side, UnicodeString(), true, true);
  1256. }
  1257. else
  1258. {
  1259. if (HasDirView[Side])
  1260. {
  1261. DirView(Side)->SelectAll(smNone);
  1262. }
  1263. }
  1264. }
  1265. //---------------------------------------------------------------------------
  1266. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1267. {
  1268. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1269. ClearOperationSelection(Side);
  1270. }
  1271. //---------------------------------------------------------------------------
  1272. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1273. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1274. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1275. int Params)
  1276. {
  1277. DebugAssert(Queue != NULL);
  1278. TQueueItem * QueueItem;
  1279. if (Direction == tdToRemote)
  1280. {
  1281. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1282. QueueItem =
  1283. new TUploadQueueItem(
  1284. Terminal, FileList, TargetDirectory, &CopyParam, Params, CopyParam.QueueParallel);
  1285. }
  1286. else
  1287. {
  1288. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1289. QueueItem =
  1290. new TDownloadQueueItem(
  1291. Terminal, FileList, TargetDirectory, &CopyParam, Params, CopyParam.QueueParallel);
  1292. }
  1293. AddQueueItem(Queue, QueueItem, Terminal);
  1294. }
  1295. //---------------------------------------------------------------------------
  1296. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1297. {
  1298. if (Queue->IsEmpty)
  1299. {
  1300. Terminal->QueueOperationStart = Now();
  1301. }
  1302. Queue->AddItem(QueueItem);
  1303. }
  1304. //---------------------------------------------------------------------------
  1305. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1306. {
  1307. }
  1308. //---------------------------------------------------------------------------
  1309. void __fastcall TCustomScpExplorerForm::InitControls()
  1310. {
  1311. DebugAssert(FDocks != NULL);
  1312. for (int Index = 0; Index < ComponentCount; Index++)
  1313. {
  1314. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1315. if ((Dock != NULL) && (Dock->Tag == 0))
  1316. {
  1317. FDocks->Add(Dock);
  1318. }
  1319. }
  1320. CollectItemsWithTextDisplayMode(this);
  1321. }
  1322. //---------------------------------------------------------------------------
  1323. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1324. {
  1325. ConfigurationChanged();
  1326. QueuePanel->Height =
  1327. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1328. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1329. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1330. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1331. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1332. QueueFileList->Height =
  1333. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1334. if (QueueFileList->Visible)
  1335. {
  1336. AdjustQueueLayout();
  1337. }
  1338. UpdateCustomCommandsToolbar();
  1339. }
  1340. //---------------------------------------------------------------------------
  1341. void __fastcall TCustomScpExplorerForm::StoreParams()
  1342. {
  1343. int APixelsPerInch = GetControlPixelsPerInch(this);
  1344. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1345. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1346. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1347. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1348. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1349. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1350. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1351. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1352. }
  1353. //---------------------------------------------------------------------------
  1354. void __fastcall TCustomScpExplorerForm::Loaded()
  1355. {
  1356. // Loaded is called in more deterministic moment than previously used CreateParams
  1357. if (!FFormRestored)
  1358. {
  1359. FFormRestored = true;
  1360. RestoreFormParams();
  1361. }
  1362. TForm::Loaded();
  1363. }
  1364. //---------------------------------------------------------------------------
  1365. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1366. {
  1367. DebugAssert(FDocks != NULL);
  1368. for (int Index = 0; Index < FDocks->Count; Index++)
  1369. {
  1370. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1371. }
  1372. }
  1373. //---------------------------------------------------------------------------
  1374. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1375. {
  1376. SetDockAllowDrag(true);
  1377. while (!ButtonsStr.IsEmpty())
  1378. {
  1379. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1380. UnicodeString Name = CutToChar(S, L'=', true);
  1381. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1382. {
  1383. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1384. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1385. {
  1386. while (!S.IsEmpty())
  1387. {
  1388. UnicodeString S2 = CutToChar(S, L',', true);
  1389. UnicodeString Name = CutToChar(S2, L':', true);
  1390. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1391. {
  1392. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1393. if (SameText(GetToolbarItemName(Item), Name))
  1394. {
  1395. // forward compatibility
  1396. UnicodeString S3 = CutToChar(S2, L':', true);
  1397. bool Visible = StrToIntDef(S3, 0);
  1398. Item->Visible = Visible;
  1399. }
  1400. }
  1401. }
  1402. }
  1403. }
  1404. }
  1405. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1406. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1407. }
  1408. //---------------------------------------------------------------------------
  1409. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1410. {
  1411. UnicodeString Result;
  1412. SetDockAllowDrag(true);
  1413. Result = ::GetToolbarsLayoutStr(this);
  1414. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1415. return Result;
  1416. }
  1417. //---------------------------------------------------------------------------
  1418. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1419. {
  1420. UnicodeString Result;
  1421. if (Item->Action != NULL)
  1422. {
  1423. Result = Item->Action->Name;
  1424. Result = RemoveSuffix(Result, L"Action", true);
  1425. }
  1426. else
  1427. {
  1428. Result = Item->Name;
  1429. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1430. Result = RemoveSuffix(Result, L"Item", true);
  1431. }
  1432. return Result;
  1433. }
  1434. //---------------------------------------------------------------------------
  1435. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1436. {
  1437. UnicodeString Result;
  1438. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1439. {
  1440. UnicodeString ToolbarStr;
  1441. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1442. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1443. {
  1444. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1445. {
  1446. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1447. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1448. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1449. // and save the changes here only.
  1450. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1451. {
  1452. UnicodeString Name = GetToolbarItemName(Item);
  1453. DebugAssert(Name.Pos(L"TBX") == 0);
  1454. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1455. }
  1456. }
  1457. }
  1458. if (!ToolbarStr.IsEmpty())
  1459. {
  1460. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1461. }
  1462. }
  1463. return Result;
  1464. }
  1465. //---------------------------------------------------------------------------
  1466. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1467. {
  1468. DebugAssert(FProgressForm == NULL);
  1469. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1470. bool AllowMoveToQueue = (FTransferResumeList != NULL) || (FDeletedFiles != NULL);
  1471. FProgressForm = new TProgressForm(Application, AllowMoveToQueue, AllowSkip, SynchronizeProgress);
  1472. FProgressForm->DeleteLocalToRecycleBin =
  1473. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1474. FProgressForm->DeleteRemoteToRecycleBin =
  1475. HasActiveTerminal() &&
  1476. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1477. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1478. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1479. LockWindow();
  1480. // Actually, do not know what hides the progress once the operation finishes
  1481. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1482. SetTaskbarListProgressState(TBPF_NORMAL);
  1483. }
  1484. //---------------------------------------------------------------------------
  1485. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1486. {
  1487. UnlockWindow();
  1488. SAFE_DESTROY(FProgressForm);
  1489. SetQueueProgress();
  1490. }
  1491. //---------------------------------------------------------------------------
  1492. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1493. TFileOperationProgressType & ProgressData)
  1494. {
  1495. // Download to temporary local directory
  1496. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1497. {
  1498. bool Continue = true;
  1499. FPendingTempSpaceWarn = false;
  1500. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1501. if (!Continue)
  1502. {
  1503. Abort();
  1504. }
  1505. }
  1506. // operation is being executed and we still didn't show up progress form
  1507. if (ProgressData.InProgress && !FProgressForm)
  1508. {
  1509. CreateProgressForm(NULL);
  1510. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1511. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1512. }
  1513. // operation is finished (or terminated), so we hide progress form
  1514. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1515. {
  1516. DestroyProgressForm();
  1517. if (ProgressData.Operation == foCalculateSize)
  1518. {
  1519. // When calculating size before transfer, the abort caused by
  1520. // cancel flag set due to "MoveToQueue" below,
  1521. // is not propagated back to us in ExecuteFileOperation,
  1522. // as it is silently swallowed.
  1523. // So we explicitly (re)throw it here.
  1524. if (FMoveToQueue)
  1525. {
  1526. Abort();
  1527. }
  1528. }
  1529. else
  1530. {
  1531. if ((ProgressData.Cancel == csContinue) &&
  1532. !FAutoOperation)
  1533. {
  1534. OperationComplete(ProgressData.StartTime);
  1535. }
  1536. }
  1537. }
  1538. if (FProgressForm &&
  1539. // Can happen only during synchronization, when ending one of the operations
  1540. ProgressData.InProgress)
  1541. {
  1542. FProgressForm->SetProgressData(ProgressData);
  1543. DebugAssert(ProgressData.InProgress);
  1544. SetTaskbarListProgressValue(&ProgressData);
  1545. if (FProgressForm->Cancel > csContinue)
  1546. {
  1547. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1548. if (FProgressForm->Cancel == csCancelFile)
  1549. {
  1550. FProgressForm->ClearCancel();
  1551. }
  1552. // cancel cancels even the move
  1553. FMoveToQueue = false;
  1554. }
  1555. else if (FProgressForm->MoveToQueue)
  1556. {
  1557. FMoveToQueue = true;
  1558. ProgressData.SetCancelAtLeast(csCancel);
  1559. }
  1560. if ((FTransferResumeList != NULL) &&
  1561. ProgressData.InProgress &&
  1562. ProgressData.IsTransfer() &&
  1563. !ProgressData.FullFileName.IsEmpty())
  1564. {
  1565. if ((FTransferResumeList->Count == 0) ||
  1566. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1567. {
  1568. // note that we do not recognize directories from files here
  1569. FTransferResumeList->Add(ProgressData.FullFileName);
  1570. }
  1571. }
  1572. }
  1573. }
  1574. //---------------------------------------------------------------------------
  1575. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1576. const TDateTime & StartTime)
  1577. {
  1578. if (GUIConfiguration->BeepOnFinish &&
  1579. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1580. {
  1581. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1582. DWORD Sound;
  1583. if (!ExtractFileExt(BeepSound).IsEmpty())
  1584. {
  1585. Sound = SND_FILENAME;
  1586. }
  1587. else
  1588. {
  1589. Sound = SND_ALIAS;
  1590. }
  1591. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1592. }
  1593. }
  1594. //---------------------------------------------------------------------------
  1595. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1596. TFileOperationProgressType & ProgressData)
  1597. {
  1598. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1599. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1600. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1601. FileOperationProgress(ProgressData);
  1602. }
  1603. //---------------------------------------------------------------------------
  1604. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1605. {
  1606. UnicodeString Result;
  1607. if (FProgressForm != NULL)
  1608. {
  1609. Result = FProgressForm->ProgressStr();
  1610. }
  1611. return Result;
  1612. }
  1613. //---------------------------------------------------------------------------
  1614. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1615. bool DragDrop)
  1616. {
  1617. return (!DragDrop && (DropSourceControl == NULL)) ||
  1618. (DropSourceControl == DirView(osOther));
  1619. }
  1620. //---------------------------------------------------------------------------
  1621. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1622. {
  1623. TCustomDirView * ADirView = DirView(Side);
  1624. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1625. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1626. // this can happen when local drive is unplugged in the middle of the operation
  1627. if (Item != NULL)
  1628. {
  1629. if (Unselect)
  1630. {
  1631. Item->Selected = false;
  1632. }
  1633. ADirView->MakeProgressVisible(Item);
  1634. }
  1635. return Item;
  1636. }
  1637. //---------------------------------------------------------------------------
  1638. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1639. TFileOperation Operation, TOperationSide Side,
  1640. bool /*Temp*/, const UnicodeString & FileName, bool Success, bool NotCancelled,
  1641. TOnceDoneOperation & OnceDoneOperation)
  1642. {
  1643. if (!FAutoOperation)
  1644. {
  1645. // no selection on "/upload", form serves only as event handler
  1646. // (it is not displayed)
  1647. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1648. ((Operation == FPrimaryOperation) ||
  1649. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1650. {
  1651. TOperationSide DViewSide;
  1652. if ((FProgressSide != osCurrent) &&
  1653. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1654. {
  1655. // assume the operation is over the focused panel
  1656. DViewSide = FProgressSide;
  1657. }
  1658. else
  1659. {
  1660. DViewSide = Side;
  1661. }
  1662. if (FOnFileOperationFinished != NULL)
  1663. {
  1664. FOnFileOperationFinished(DViewSide, FileName, Success, NotCancelled);
  1665. }
  1666. else
  1667. {
  1668. VisualiseOperationFinished(DViewSide, FileName, Success && NotCancelled);
  1669. }
  1670. }
  1671. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1672. {
  1673. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1674. {
  1675. if (Side == osLocal)
  1676. {
  1677. Configuration->Usage->Inc(L"UploadedFiles");
  1678. }
  1679. else
  1680. {
  1681. Configuration->Usage->Inc(L"DownloadedFiles");
  1682. }
  1683. }
  1684. }
  1685. }
  1686. if (Success && (FSynchronizeController != NULL))
  1687. {
  1688. DebugAssert(!IsSideLocalBrowser(osRemote));
  1689. if (Operation == foCopy)
  1690. {
  1691. DebugAssert(Side == osLocal);
  1692. FSynchronizeController->LogOperation(soUpload, FileName);
  1693. }
  1694. else if (Operation == foDelete)
  1695. {
  1696. DebugAssert(Side == osRemote);
  1697. FSynchronizeController->LogOperation(soDelete, FileName);
  1698. }
  1699. }
  1700. if (FProgressForm)
  1701. {
  1702. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1703. }
  1704. }
  1705. //---------------------------------------------------------------------------
  1706. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1707. TFileOperation Operation, TOperationSide Side,
  1708. bool Temp, const UnicodeString & FileName, bool Success, bool NotCancelled,
  1709. TOnceDoneOperation & OnceDoneOperation)
  1710. {
  1711. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1712. DoOperationFinished(Operation, Side, Temp, FileName, Success, NotCancelled, OnceDoneOperation);
  1713. }
  1714. //---------------------------------------------------------------------------
  1715. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1716. {
  1717. return false;
  1718. }
  1719. //---------------------------------------------------------------------------
  1720. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1721. {
  1722. return false;
  1723. }
  1724. //---------------------------------------------------------------------------
  1725. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1726. {
  1727. return false;
  1728. }
  1729. //---------------------------------------------------------------------------
  1730. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1731. {
  1732. return NULL;
  1733. }
  1734. //---------------------------------------------------------------------------
  1735. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1736. {
  1737. DebugAssert(GetSide(Side) == osRemote);
  1738. DebugUsedParam(Side);
  1739. return RemoteDirView;
  1740. }
  1741. //---------------------------------------------------------------------------
  1742. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1743. {
  1744. DebugAssert(GetSide(Side) == osRemote);
  1745. DebugUsedParam(Side);
  1746. return RemoteDriveView;
  1747. }
  1748. //---------------------------------------------------------------------------
  1749. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1750. {
  1751. DebugAssert(GetSide(Side) == osRemote);
  1752. DebugUsedParam(Side);
  1753. return HasActiveTerminal();
  1754. }
  1755. //---------------------------------------------------------------------------
  1756. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1757. TOperationSide Side, int FilesOnly)
  1758. {
  1759. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1760. }
  1761. //---------------------------------------------------------------------------
  1762. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1763. TOperationSide Side, int FilesOnly)
  1764. {
  1765. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1766. }
  1767. //---------------------------------------------------------------------------
  1768. struct THistoryItemData
  1769. {
  1770. short int Side;
  1771. short int Index;
  1772. };
  1773. //---------------------------------------------------------------------------
  1774. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1775. {
  1776. DirView(Side)->HistoryGo(Index);
  1777. }
  1778. //---------------------------------------------------------------------------
  1779. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1780. {
  1781. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1782. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1783. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1784. }
  1785. //---------------------------------------------------------------------------
  1786. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1787. bool Back)
  1788. {
  1789. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1790. {
  1791. TCustomDirView * DView = DirView(Side);
  1792. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1793. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1794. if (ICount > 10)
  1795. {
  1796. ICount = 10;
  1797. }
  1798. Menu->Items->Clear();
  1799. THistoryItemData Data;
  1800. Data.Side = Side;
  1801. for (short int i = 1; i <= ICount; i++)
  1802. {
  1803. TTBCustomItem * Item = new TTBXItem(Menu);
  1804. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1805. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1806. Item->Hint = DView->HistoryPath[Data.Index];
  1807. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1808. Item->Tag = *reinterpret_cast<int*>(&Data);
  1809. Item->OnClick = HistoryItemClick;
  1810. Menu->Items->Add(Item);
  1811. }
  1812. }
  1813. }
  1814. //---------------------------------------------------------------------------
  1815. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1816. TOperationSide Side, bool Back)
  1817. {
  1818. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1819. {
  1820. // workaround
  1821. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1822. // operator new allocates memory in Pascal code, but calls inline
  1823. // constructor in C++, leading in problems, the function does
  1824. // both in Pascal code
  1825. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1826. UpdateHistoryMenu(Side, Back);
  1827. }
  1828. return FHistoryMenu[Side == osLocal][Back];
  1829. }
  1830. //---------------------------------------------------------------------------
  1831. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1832. TCustomDirView *Sender)
  1833. {
  1834. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1835. UpdateHistoryMenu(Side, true);
  1836. UpdateHistoryMenu(Side, false);
  1837. }
  1838. //---------------------------------------------------------------------------
  1839. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1840. {
  1841. // remote custom commands can be executed only if the server supports shell commands
  1842. // or have secondary shell
  1843. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1844. }
  1845. //---------------------------------------------------------------------------
  1846. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1847. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1848. {
  1849. // -1 = hidden, 0 = disabled, 1 = enabled
  1850. int Result;
  1851. TFileCustomCommand RemoteCustomCommand;
  1852. TLocalCustomCommand LocalCustomCommand;
  1853. TFileCustomCommand * NonInteractiveCustomCommand =
  1854. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1855. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1856. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1857. if (FLAGCLEAR(Command.Params, ccLocal))
  1858. {
  1859. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1860. // custom command that does not operate with files can be executed anytime ...
  1861. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1862. {
  1863. // ... but do not show such command in remote file menu
  1864. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1865. {
  1866. Result = AllowedState;
  1867. }
  1868. else
  1869. {
  1870. Result = -1;
  1871. }
  1872. }
  1873. else
  1874. {
  1875. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1876. {
  1877. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1878. }
  1879. else
  1880. {
  1881. Result = -1;
  1882. }
  1883. }
  1884. }
  1885. else
  1886. {
  1887. // custom command that does not operate with files can be executed anytime
  1888. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1889. {
  1890. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1891. }
  1892. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1893. {
  1894. if ((ListType == ccltAll) || (ListType == ccltFile))
  1895. {
  1896. // special case is "diff"-style command that can be executed over any side,
  1897. // if we have both sides
  1898. Result =
  1899. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1900. // directly (instead of EnableSelectedOperation) to pass
  1901. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1902. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1903. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1904. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1905. }
  1906. else if (ListType == ccltBoth)
  1907. {
  1908. DebugAssert(!IsLocalBrowserMode());
  1909. Result = 1;
  1910. }
  1911. else
  1912. {
  1913. Result = -1;
  1914. }
  1915. }
  1916. else
  1917. {
  1918. if ((ListType == ccltAll) ||
  1919. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1920. {
  1921. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1922. }
  1923. else
  1924. {
  1925. Result = -1;
  1926. }
  1927. }
  1928. if ((Result > 0) &&
  1929. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1930. !HasActiveTerminal())
  1931. {
  1932. Result = 0;
  1933. }
  1934. }
  1935. return Result;
  1936. }
  1937. //---------------------------------------------------------------------------
  1938. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1939. TStrings * FileList, const TCustomCommandType & ACommand,
  1940. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1941. {
  1942. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1943. {
  1944. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1945. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1946. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1947. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1948. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1949. bool Capture =
  1950. FLAGSET(ACommand.Params, ccShowResults) ||
  1951. FLAGSET(ACommand.Params, ccCopyResults) ||
  1952. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1953. TCaptureOutputEvent OutputEvent = NULL;
  1954. DebugAssert(FCapturedLog == NULL);
  1955. if (Capture)
  1956. {
  1957. FCapturedLog = new TStringList();
  1958. OutputEvent = TerminalCaptureLog;
  1959. }
  1960. try
  1961. {
  1962. if (!RemoteCustomCommand.IsFileCommand(Command))
  1963. {
  1964. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1965. OutputEvent);
  1966. }
  1967. else
  1968. {
  1969. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1970. }
  1971. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1972. {
  1973. if (FLAGSET(ACommand.Params, ccCopyResults))
  1974. {
  1975. CopyToClipboard(FCapturedLog);
  1976. }
  1977. if (FLAGSET(ACommand.Params, ccShowResults))
  1978. {
  1979. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1980. }
  1981. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1982. {
  1983. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1984. }
  1985. }
  1986. }
  1987. __finally
  1988. {
  1989. SAFE_DESTROY(FCapturedLog);
  1990. }
  1991. }
  1992. }
  1993. //---------------------------------------------------------------------------
  1994. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1995. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1996. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1997. {
  1998. DebugAssert(!IsLocalBrowserMode());
  1999. TStrings * LocalFileList = NULL;
  2000. TStrings * RemoteFileList = NULL;
  2001. TStrings * RemoteFileListFull = NULL;
  2002. try
  2003. {
  2004. if (LocalFileCommand)
  2005. {
  2006. if (ALocalFileList == NULL)
  2007. {
  2008. DebugAssert(HasDirView[osLocal]);
  2009. // Cannot have focus on both panels, so we have to call AnyFileSelected
  2010. // directly (instead of EnableSelectedOperation) to pass
  2011. // false to FocusedFileOnlyWhenFocused
  2012. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  2013. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  2014. }
  2015. else
  2016. {
  2017. LocalFileList = ALocalFileList;
  2018. }
  2019. if (FileListCommand)
  2020. {
  2021. if (LocalFileList->Count != 1)
  2022. {
  2023. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  2024. }
  2025. }
  2026. else
  2027. {
  2028. if ((LocalFileList->Count != 1) &&
  2029. (FileList->Count != 1) &&
  2030. (LocalFileList->Count != FileList->Count))
  2031. {
  2032. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  2033. }
  2034. }
  2035. }
  2036. UnicodeString RootTempDir;
  2037. UnicodeString TempDir;
  2038. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  2039. if (!RemoteFiles)
  2040. {
  2041. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true, true);
  2042. }
  2043. try
  2044. {
  2045. TDateTimes RemoteFileTimes;
  2046. if (RemoteFiles)
  2047. {
  2048. RemoteFileList = FileList;
  2049. }
  2050. else
  2051. {
  2052. RemoteFileList = new TStringList();
  2053. TMakeLocalFileListParams MakeFileListParam;
  2054. MakeFileListParam.FileList = RemoteFileList;
  2055. MakeFileListParam.FileTimes = &RemoteFileTimes;
  2056. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2057. MakeFileListParam.Recursive =
  2058. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  2059. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2060. if (!MakeFileListParam.Recursive)
  2061. {
  2062. RemoteFileTimes.clear();
  2063. RemoteFileListFull = new TStringList();
  2064. MakeFileListParam.FileList = RemoteFileListFull;
  2065. MakeFileListParam.Recursive = true;
  2066. MakeFileListParam.IncludeDirs = false;
  2067. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2068. }
  2069. else
  2070. {
  2071. RemoteFileListFull = RemoteFileList;
  2072. }
  2073. }
  2074. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  2075. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2076. if (!NonBlocking)
  2077. {
  2078. int Count;
  2079. if (FileListCommand)
  2080. {
  2081. Count = 1;
  2082. }
  2083. else if (LocalFileCommand)
  2084. {
  2085. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  2086. }
  2087. else
  2088. {
  2089. Count = RemoteFileList->Count;
  2090. }
  2091. Progress.Start(foCustomCommand, osRemote, Count);
  2092. DebugAssert(FProgressForm != NULL);
  2093. FProgressForm->ReadOnly = true;
  2094. }
  2095. try
  2096. {
  2097. if (FileListCommand)
  2098. {
  2099. UnicodeString LocalFile;
  2100. // MakeFileList does not delimit filenames
  2101. UnicodeString FileList = MakeFileList(RemoteFileList);
  2102. if (LocalFileCommand)
  2103. {
  2104. DebugAssert(LocalFileList->Count == 1);
  2105. LocalFile = LocalFileList->Strings[0];
  2106. }
  2107. TLocalCustomCommand CustomCommand(Data,
  2108. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  2109. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  2110. if (NonBlocking)
  2111. {
  2112. DebugAssert(!POutput);
  2113. ExecuteShellChecked(ShellCommand);
  2114. }
  2115. else
  2116. {
  2117. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2118. }
  2119. }
  2120. else if (LocalFileCommand)
  2121. {
  2122. if (LocalFileList->Count == 1)
  2123. {
  2124. UnicodeString LocalFile = LocalFileList->Strings[0];
  2125. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2126. {
  2127. UnicodeString FileName = RemoteFileList->Strings[Index];
  2128. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2129. Progress.SetFile(FileNameForProgress);
  2130. TLocalCustomCommand CustomCommand(Data,
  2131. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2132. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2133. TOnceDoneOperation OnceDoneOperation;
  2134. // Do not unselect anything, as it won't work with two panels anyway
  2135. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2136. }
  2137. }
  2138. else if (RemoteFileList->Count == 1)
  2139. {
  2140. UnicodeString FileName = RemoteFileList->Strings[0];
  2141. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2142. {
  2143. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2144. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2145. Progress.SetFile(LocalFileNameOnly);
  2146. TLocalCustomCommand CustomCommand(
  2147. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2148. FileName, LocalFileName, L"");
  2149. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2150. TOnceDoneOperation OnceDoneOperation;
  2151. // Do not unselect anything, as it won't work with two panels anyway
  2152. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2153. }
  2154. }
  2155. else
  2156. {
  2157. if (LocalFileList->Count != RemoteFileList->Count)
  2158. {
  2159. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2160. }
  2161. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2162. {
  2163. UnicodeString FileName = RemoteFileList->Strings[Index];
  2164. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2165. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2166. Progress.SetFile(LocalFileNameOnly); // sic
  2167. TLocalCustomCommand CustomCommand(
  2168. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2169. FileName, LocalFileName, L"");
  2170. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2171. TOnceDoneOperation OnceDoneOperation;
  2172. // Do not unselect anything, as it won't work with two panels anyway
  2173. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2174. }
  2175. }
  2176. }
  2177. else
  2178. {
  2179. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2180. {
  2181. UnicodeString FileName = RemoteFileList->Strings[Index];
  2182. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2183. Progress.SetFile(FileNameForProgress);
  2184. TLocalCustomCommand CustomCommand(Data,
  2185. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2186. FileName, L"", L"");
  2187. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2188. TOnceDoneOperation OnceDoneOperation;
  2189. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2190. }
  2191. }
  2192. }
  2193. __finally
  2194. {
  2195. if (!NonBlocking)
  2196. {
  2197. Progress.Stop();
  2198. }
  2199. }
  2200. DebugAssert(!FAutoOperation);
  2201. if (!RemoteFiles)
  2202. {
  2203. TempDir = IncludeTrailingBackslash(TempDir);
  2204. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2205. {
  2206. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2207. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2208. // Skip directories, process only nested files.
  2209. // The check is redundant as FileAge fails for directories anyway.
  2210. !DirectoryExists(FileName))
  2211. {
  2212. UnicodeString RemoteDir =
  2213. UnixExtractFileDir(
  2214. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2215. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2216. TSearchRecSmart SearchRec;
  2217. if (FileSearchRec(FileName, SearchRec) &&
  2218. (SearchRec.GetLastWriteTime() != RemoteFileTimes[Index]))
  2219. {
  2220. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2221. TemporaryFileCopyParam(CopyParam);
  2222. CopyParam.FileMask = L"";
  2223. FAutoOperation = true;
  2224. FMoveToQueue = false;
  2225. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2226. TemporaryFilesList->Add(FileName);
  2227. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2228. }
  2229. }
  2230. }
  2231. }
  2232. }
  2233. __finally
  2234. {
  2235. FAutoOperation = false;
  2236. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2237. {
  2238. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  2239. }
  2240. }
  2241. }
  2242. __finally
  2243. {
  2244. if (RemoteFileList != FileList)
  2245. {
  2246. delete RemoteFileList;
  2247. }
  2248. if (LocalFileList != ALocalFileList)
  2249. {
  2250. delete LocalFileList;
  2251. }
  2252. if (RemoteFileListFull != RemoteFileList)
  2253. {
  2254. delete RemoteFileListFull;
  2255. }
  2256. }
  2257. }
  2258. //---------------------------------------------------------------------------
  2259. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2260. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2261. bool FileListCommand, UnicodeString * POutput)
  2262. {
  2263. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2264. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2265. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2266. {
  2267. UnicodeString FileName = SelectedFileList->Strings[Index];
  2268. if (DirectoryExists(FileName))
  2269. {
  2270. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2271. {
  2272. LocalFileList->Add(FileName);
  2273. }
  2274. if (FLAGSET(ACommand.Params, ccRecursive))
  2275. {
  2276. TMakeLocalFileListParams MakeFileListParam;
  2277. MakeFileListParam.FileList = LocalFileList.get();
  2278. MakeFileListParam.FileTimes = NULL;
  2279. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2280. MakeFileListParam.Recursive = true;
  2281. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2282. }
  2283. }
  2284. else
  2285. {
  2286. LocalFileList->Add(FileName);
  2287. }
  2288. }
  2289. UnicodeString RemotePath;
  2290. if (Terminal != NULL)
  2291. {
  2292. RemotePath = Terminal->CurrentDirectory;
  2293. }
  2294. if (FileListCommand)
  2295. {
  2296. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2297. TLocalCustomCommand CustomCommand(
  2298. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2299. L"", L"", FileList);
  2300. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2301. }
  2302. else
  2303. {
  2304. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2305. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2306. DebugAssert(FProgressForm != NULL);
  2307. FProgressForm->ReadOnly = true;
  2308. try
  2309. {
  2310. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2311. {
  2312. UnicodeString FileName = LocalFileList->Strings[Index];
  2313. Progress.SetFile(FileName);
  2314. TLocalCustomCommand CustomCommand(
  2315. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2316. FileName, L"", L"");
  2317. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2318. TOnceDoneOperation OnceDoneOperation;
  2319. Progress.Finish(FileName, true, OnceDoneOperation);
  2320. }
  2321. }
  2322. __finally
  2323. {
  2324. Progress.Stop();
  2325. }
  2326. }
  2327. }
  2328. //---------------------------------------------------------------------------
  2329. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2330. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2331. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2332. {
  2333. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2334. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2335. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2336. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2337. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2338. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2339. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2340. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2341. std::unique_ptr<UnicodeString> POutput;
  2342. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2343. FLAGSET(ACommand.Params, ccCopyResults))
  2344. {
  2345. POutput.reset(new UnicodeString());
  2346. }
  2347. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide, FCurrentSide);
  2348. if (!LocalCustomCommand.IsFileCommand(Command))
  2349. {
  2350. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2351. }
  2352. // remote files?
  2353. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2354. {
  2355. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2356. }
  2357. // local files
  2358. else
  2359. {
  2360. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2361. }
  2362. if (POutput.get() != NULL)
  2363. {
  2364. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2365. int P = POutput->Pos(sLineBreak);
  2366. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2367. {
  2368. POutput->SetLength(P - 1);
  2369. }
  2370. // Copy even empty output to clipboard
  2371. if (FLAGSET(ACommand.Params, ccCopyResults))
  2372. {
  2373. CopyToClipboard(*POutput);
  2374. }
  2375. // But do not show an empty message box.
  2376. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2377. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2378. !POutput->IsEmpty())
  2379. {
  2380. TClipboardHandler ClipboardHandler;
  2381. ClipboardHandler.Text = *POutput;
  2382. TMessageParams Params;
  2383. TQueryButtonAlias Aliases[1];
  2384. Aliases[0].Button = qaRetry;
  2385. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2386. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2387. Params.Aliases = Aliases;
  2388. Params.AliasesCount = LENOF(Aliases);
  2389. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2390. }
  2391. }
  2392. }
  2393. //---------------------------------------------------------------------------
  2394. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2395. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2396. {
  2397. TCustomCommandData Data;
  2398. UnicodeString Site;
  2399. UnicodeString RemotePath;
  2400. if (HasActiveTerminal())
  2401. {
  2402. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2403. Data = TCustomCommandData(SessionData.get());
  2404. Site = Terminal->SessionData->SessionKey;
  2405. RemotePath = Terminal->CurrentDirectory;
  2406. }
  2407. UnicodeString HelpKeyword = ACommand.HomePage;
  2408. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2409. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2410. {
  2411. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2412. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2413. {
  2414. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2415. }
  2416. else
  2417. {
  2418. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2419. }
  2420. if (!DoCustomCommandOptionsDialog(
  2421. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2422. {
  2423. Abort();
  2424. }
  2425. }
  2426. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2427. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2428. {
  2429. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2430. }
  2431. else
  2432. {
  2433. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2434. }
  2435. }
  2436. //---------------------------------------------------------------------------
  2437. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2438. const TCustomCommandType & Command)
  2439. {
  2440. DebugAssert(FCustomCommandLocalFileList != NULL);
  2441. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2442. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2443. TStrings * LocalFileList = new TStringList();
  2444. TStrings * RemoteFileList = new TStringList();
  2445. try
  2446. {
  2447. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2448. {
  2449. LocalFileList->Clear();
  2450. LocalFileList->AddObject(
  2451. FCustomCommandLocalFileList->Strings[Index],
  2452. FCustomCommandLocalFileList->Objects[Index]);
  2453. RemoteFileList->Clear();
  2454. RemoteFileList->AddObject(
  2455. FCustomCommandRemoteFileList->Strings[Index],
  2456. FCustomCommandRemoteFileList->Objects[Index]);
  2457. CustomCommand(RemoteFileList, Command, LocalFileList);
  2458. }
  2459. }
  2460. __finally
  2461. {
  2462. delete LocalFileList;
  2463. delete RemoteFileList;
  2464. }
  2465. }
  2466. //---------------------------------------------------------------------------
  2467. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2468. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2469. {
  2470. delete FCustomCommandLocalFileList;
  2471. delete FCustomCommandRemoteFileList;
  2472. // takeover ownership,
  2473. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2474. FCustomCommandLocalFileList = LocalFileList;
  2475. FCustomCommandRemoteFileList = RemoteFileList;
  2476. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2477. if (Button != NULL)
  2478. {
  2479. FCustomCommandMenu->Items->Clear();
  2480. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2481. MenuPopup(FCustomCommandMenu, Button);
  2482. }
  2483. else
  2484. {
  2485. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2486. }
  2487. }
  2488. //---------------------------------------------------------------------------
  2489. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2490. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2491. {
  2492. DebugAssert(FCapturedLog != NULL);
  2493. if ((OutputType == cotOutput) || (OutputType == cotError))
  2494. {
  2495. FCapturedLog->Add(AddedLine);
  2496. }
  2497. }
  2498. //---------------------------------------------------------------------------
  2499. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2500. TOperationSide Side)
  2501. {
  2502. return
  2503. (Side == osOther) &&
  2504. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2505. }
  2506. //---------------------------------------------------------------------------
  2507. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2508. TOperationSide Side, TTBXCustomItem * Item,
  2509. TResolvedDoubleClickAction DoubleClickAction1, TResolvedDoubleClickAction DoubleClickAction2)
  2510. {
  2511. TCustomDirView * DView = DirView(Side);
  2512. bool IsDefault = false;
  2513. if (DView->ItemFocused != NULL)
  2514. {
  2515. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  2516. TResolvedDoubleClickAction DefaultAction =
  2517. WinConfiguration->ResolveDoubleClickAction(DView->ItemIsDirectory(DView->ItemFocused), ATerminal);
  2518. IsDefault = (DefaultAction == DoubleClickAction1) || (DefaultAction == DoubleClickAction2);
  2519. }
  2520. TTBItemOptions O = Item->Options;
  2521. if (IsDefault)
  2522. {
  2523. Item->Options = O << tboDefault;
  2524. }
  2525. else
  2526. {
  2527. Item->Options = O >> tboDefault;
  2528. }
  2529. }
  2530. //---------------------------------------------------------------------------
  2531. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2532. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2533. {
  2534. TCustomDirView * DView = DirView(Side);
  2535. TListItem * Item = DView->ItemFocused;
  2536. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2537. EnableFocusedOperation[Side])
  2538. {
  2539. TPoint ClientPoint;
  2540. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2541. TPoint(0, 0) : MousePos;
  2542. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2543. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2544. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2545. }
  2546. }
  2547. //---------------------------------------------------------------------------
  2548. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2549. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2550. {
  2551. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, rdcaChangeDir, rdcaOpen);
  2552. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, rdcaEdit, rdcaNone);
  2553. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, rdcaCopy, rdcaNone);
  2554. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2555. Handled = true;
  2556. }
  2557. //---------------------------------------------------------------------------
  2558. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2559. {
  2560. }
  2561. //---------------------------------------------------------------------------
  2562. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2563. {
  2564. DebugAssert(FErrorList == NULL);
  2565. if (WinConfiguration->ContinueOnError)
  2566. {
  2567. FErrorList = new TStringList();
  2568. Configuration->Usage->Inc(L"ContinuationsOnError");
  2569. }
  2570. NonVisualDataModule->StartBusy();
  2571. }
  2572. //---------------------------------------------------------------------------
  2573. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2574. {
  2575. NonVisualDataModule->EndBusy();
  2576. if (FErrorList)
  2577. {
  2578. HandleErrorList(FErrorList);
  2579. }
  2580. }
  2581. //---------------------------------------------------------------------------
  2582. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2583. const TCopyParamType & CopyParam)
  2584. {
  2585. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2586. {
  2587. Configuration->Usage->Inc(L"FileMaskUses");
  2588. }
  2589. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2590. {
  2591. Configuration->Usage->Inc(L"OperationMaskUses");
  2592. }
  2593. }
  2594. //---------------------------------------------------------------------------
  2595. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2596. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2597. {
  2598. DebugAssert(!IsLocalBrowserMode());
  2599. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2600. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2601. TTransferOperationParam DefaultParam;
  2602. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2603. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2604. switch (Param.Queue)
  2605. {
  2606. case asOn:
  2607. CopyParam.Queue = true;
  2608. break;
  2609. case asOff:
  2610. CopyParam.Queue = false;
  2611. break;
  2612. case asAuto:
  2613. default:
  2614. // keep default
  2615. break;
  2616. }
  2617. bool Result =
  2618. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2619. if (Result)
  2620. {
  2621. DebugAssert(Terminal);
  2622. bool SelectionRestored = false;
  2623. TCustomDirView * DView = NULL;
  2624. if (HasDirView[Side])
  2625. {
  2626. DView = DirView(Side);
  2627. DView->SaveSelection();
  2628. DView->SaveSelectedNames();
  2629. }
  2630. UpdateCopyParamCounters(CopyParam);
  2631. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2632. DebugAssert(FTransferResumeList == NULL);
  2633. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2634. FMoveToQueue = false;
  2635. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2636. try
  2637. {
  2638. TStrings * PermanentFileList;
  2639. std::unique_ptr<TStrings> PermanentFileListOwner;
  2640. try
  2641. {
  2642. if (Side == osLocal)
  2643. {
  2644. PermanentFileList = FileList;
  2645. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2646. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2647. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2648. if (Operation == foMove)
  2649. {
  2650. ReloadLocalDirectory();
  2651. if (DView != NULL)
  2652. {
  2653. DView->RestoreSelection();
  2654. }
  2655. SelectionRestored = true;
  2656. }
  2657. }
  2658. else
  2659. {
  2660. // Clone the file list as it may refer to current directory files,
  2661. // which get destroyed, when the source directory is reloaded after foMove operation.
  2662. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2663. // But for a hotfix, we are not going to do such a big change.
  2664. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2665. PermanentFileList = PermanentFileListOwner.get();
  2666. try
  2667. {
  2668. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2669. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2670. }
  2671. __finally
  2672. {
  2673. if (Operation == foMove)
  2674. {
  2675. if (DView != NULL)
  2676. {
  2677. DView->RestoreSelection();
  2678. }
  2679. SelectionRestored = true;
  2680. }
  2681. ReloadLocalDirectory(Param.TargetDirectory);
  2682. }
  2683. }
  2684. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2685. if (FMoveToQueue)
  2686. {
  2687. Abort();
  2688. }
  2689. }
  2690. catch (EAbort &)
  2691. {
  2692. if (FMoveToQueue)
  2693. {
  2694. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2695. DebugAssert(CopyParam.TransferSkipList == NULL);
  2696. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2697. if (TransferResumeList->Count > 0)
  2698. {
  2699. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2700. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2701. }
  2702. CopyParam.TransferSkipList = TransferResumeList.release();
  2703. // not really needed, just to keep it consistent with TransferResumeList
  2704. FTransferResumeList = NULL;
  2705. FMoveToQueue = false;
  2706. Configuration->Usage->Inc("MovesToBackground");
  2707. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2708. ClearTransferSourceSelection(Direction);
  2709. }
  2710. throw;
  2711. }
  2712. }
  2713. __finally
  2714. {
  2715. if (!SelectionRestored && (DView != NULL))
  2716. {
  2717. DView->DiscardSavedSelection();
  2718. }
  2719. FTransferResumeList = NULL;
  2720. }
  2721. }
  2722. return Result;
  2723. }
  2724. //---------------------------------------------------------------------------
  2725. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2726. TOperationSide Side, TStrings * FileList, void * Param)
  2727. {
  2728. DebugAssert(FileList->Count);
  2729. // We deliberately do not toggle alternative flag (Param), but use OR,
  2730. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2731. // shortcut of CurrentDeleteAlternativeAction
  2732. bool Alternative =
  2733. bool(Param) || UseAlternativeFunction();
  2734. bool Recycle;
  2735. if (IsSideLocalBrowser(Side))
  2736. {
  2737. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2738. }
  2739. else
  2740. {
  2741. Recycle =
  2742. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2743. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2744. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2745. }
  2746. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2747. if (!Result)
  2748. {
  2749. UnicodeString Query;
  2750. if (FileList->Count == 1)
  2751. {
  2752. if (IsSideLocalBrowser(Side))
  2753. {
  2754. Query = ExtractFileName(FileList->Strings[0]);
  2755. }
  2756. else
  2757. {
  2758. Query = UnixExtractFileName(FileList->Strings[0]);
  2759. }
  2760. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2761. }
  2762. else
  2763. {
  2764. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2765. }
  2766. TMessageParams Params(mpNeverAskAgainCheck);
  2767. Params.ImageName = L"Delete file";
  2768. unsigned int Answer =
  2769. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2770. if (Answer == qaNeverAskAgain)
  2771. {
  2772. Result = true;
  2773. if (Recycle)
  2774. {
  2775. WinConfiguration->ConfirmRecycling = false;
  2776. }
  2777. else
  2778. {
  2779. WinConfiguration->ConfirmDeleting = false;
  2780. }
  2781. }
  2782. else
  2783. {
  2784. Result = (Answer == qaOK);
  2785. }
  2786. }
  2787. if (Result)
  2788. {
  2789. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2790. }
  2791. return Result;
  2792. }
  2793. //---------------------------------------------------------------------------
  2794. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2795. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2796. {
  2797. TAutoBatch AutoBatch(this);
  2798. bool Result;
  2799. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2800. {
  2801. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2802. }
  2803. else if (Operation == foRename)
  2804. {
  2805. DebugAssert(DirView(Side)->ItemFocused);
  2806. DirView(Side)->ItemFocused->EditCaption();
  2807. Result = true;
  2808. }
  2809. else if (Operation == foDelete)
  2810. {
  2811. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2812. }
  2813. else if (Operation == foSetProperties)
  2814. {
  2815. DirView(osRemote)->SaveSelectedNames();
  2816. Result = SetProperties(Side, FileList);
  2817. }
  2818. else if (Operation == foCustomCommand)
  2819. {
  2820. DebugAssert(Param);
  2821. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2822. DirView(osRemote)->SaveSelectedNames();
  2823. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2824. CustomCommand(FileList, *Command, NULL);
  2825. Result = true;
  2826. }
  2827. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2828. {
  2829. DebugAssert(!IsSideLocalBrowser(Side));
  2830. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2831. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2832. }
  2833. else if (Operation == foLock)
  2834. {
  2835. DebugAssert(!IsSideLocalBrowser(Side));
  2836. LockFiles(FileList, true);
  2837. Result = true;
  2838. }
  2839. else if (Operation == foUnlock)
  2840. {
  2841. DebugAssert(!IsSideLocalBrowser(Side));
  2842. LockFiles(FileList, false);
  2843. Result = true;
  2844. }
  2845. else
  2846. {
  2847. DebugFail();
  2848. }
  2849. return Result;
  2850. }
  2851. //---------------------------------------------------------------------------
  2852. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2853. {
  2854. if (Side == osCurrent)
  2855. {
  2856. Side = FCurrentSide;
  2857. }
  2858. return Side;
  2859. }
  2860. //---------------------------------------------------------------------------
  2861. TOperationSide TCustomScpExplorerForm::GetOtherSide(TOperationSide Side)
  2862. {
  2863. TOperationSide Result;
  2864. switch (GetSide(Side))
  2865. {
  2866. case osLocal:
  2867. Result = osOther;
  2868. break;
  2869. case osOther:
  2870. Result = osLocal;
  2871. break;
  2872. default:
  2873. DebugFail();
  2874. Abort();
  2875. }
  2876. return Result;
  2877. }
  2878. //---------------------------------------------------------------------------
  2879. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2880. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2881. {
  2882. Side = GetSide(Side);
  2883. bool FullPath = IsSideLocalBrowser(Side);
  2884. TCustomDirView * ADirView = DirView(Side);
  2885. std::unique_ptr<TStringList> FileList(new TStringList());
  2886. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2887. bool ReloadProperties =
  2888. !IsSideLocalBrowser(Side) && (Operation == foSetProperties) &&
  2889. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2890. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2891. if (Result && ReloadProperties)
  2892. {
  2893. std::unique_ptr<TStringList> FileList2(new TStringList());
  2894. FileList->CaseSensitive = true;
  2895. FileList->Sort();
  2896. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2897. {
  2898. TListItem * Item = ADirView->Items->Item[Index];
  2899. UnicodeString FileName = ADirView->ItemFileName(Item);
  2900. int Index2;
  2901. if (FileList->Find(FileName, Index2))
  2902. {
  2903. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2904. }
  2905. }
  2906. LoadFilesProperties(FileList2.get());
  2907. }
  2908. return Result;
  2909. }
  2910. //---------------------------------------------------------------------------
  2911. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2912. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2913. bool NoConfirmation, void * Param)
  2914. {
  2915. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2916. {
  2917. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2918. {
  2919. DebugAssert(!IsLocalBrowserMode());
  2920. if (GetSide(Side) == osLocal)
  2921. {
  2922. Configuration->Usage->Inc(L"UploadsCommand");
  2923. }
  2924. else
  2925. {
  2926. Configuration->Usage->Inc(L"DownloadsCommand");
  2927. }
  2928. }
  2929. }
  2930. }
  2931. //---------------------------------------------------------------------------
  2932. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2933. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2934. {
  2935. TTransferOperationParam Param;
  2936. if ((WinConfiguration->Interface != ifCommander) ||
  2937. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2938. {
  2939. Flags &= ~cocShortCutHint;
  2940. }
  2941. TCustomDirView * DView = DirView(Side);
  2942. Param.Options =
  2943. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2944. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2945. if (FLAGSET(Flags, cocQueue))
  2946. {
  2947. Param.Queue = asOn;
  2948. }
  2949. else if (FLAGSET(Flags, cocNonQueue))
  2950. {
  2951. Param.Queue = asOff;
  2952. }
  2953. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2954. }
  2955. //---------------------------------------------------------------------------
  2956. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2957. {
  2958. int Count = ErrorList->Count;
  2959. try
  2960. {
  2961. if (ErrorList->Count)
  2962. {
  2963. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2964. if (MessageDialog(Message, qtError,
  2965. qaOK | qaCancel, HELP_NONE) == qaOK)
  2966. {
  2967. unsigned int Answer;
  2968. int Index = 0;
  2969. do
  2970. {
  2971. DebugAssert(Index >= 0 && Index < Count);
  2972. TQueryButtonAlias Aliases[2];
  2973. Aliases[0].Button = qaYes;
  2974. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2975. Aliases[1].Button = qaNo;
  2976. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2977. TMessageParams Params;
  2978. Params.Aliases = Aliases;
  2979. Params.AliasesCount = LENOF(Aliases);
  2980. int No = Index + 1;
  2981. UnicodeString Message = ErrorList->Strings[Index];
  2982. UnicodeString AMainInstructions;
  2983. UnicodeString Details;
  2984. if (ExtractMainInstructions(Message, AMainInstructions))
  2985. {
  2986. Details = Message;
  2987. Message = AMainInstructions;
  2988. }
  2989. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2990. Answer = MoreMessageDialog(
  2991. Message,
  2992. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2993. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2994. qaOK, HELP_NONE, &Params);
  2995. if (Answer == qaNo)
  2996. {
  2997. Index++;
  2998. }
  2999. if (Answer == qaYes)
  3000. {
  3001. Index--;
  3002. }
  3003. }
  3004. while (Answer != qaOK);
  3005. }
  3006. }
  3007. }
  3008. __finally
  3009. {
  3010. TStrings * List = ErrorList;
  3011. ErrorList = NULL;
  3012. for (int i = 0; i < Count; i++)
  3013. {
  3014. delete List->Objects[i];
  3015. }
  3016. delete List;
  3017. }
  3018. }
  3019. //---------------------------------------------------------------------------
  3020. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  3021. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  3022. {
  3023. // needed for checking filemasks, as there's no directory object
  3024. // associated with the file object
  3025. File->FullFileName = FullFileName;
  3026. TFileMasks::TParams MaskParams;
  3027. MaskParams.Size = File->Size;
  3028. MaskParams.Modification = File->Modification;
  3029. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  3030. }
  3031. //---------------------------------------------------------------------------
  3032. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  3033. {
  3034. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3035. Side = GetSide(Side);
  3036. TCustomDirView * DView = DirView(Side);
  3037. TListItem * FocusedItem = DView->ItemFocused;
  3038. UnicodeString Name;
  3039. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  3040. {
  3041. Name = DView->ItemFileName(FocusedItem);
  3042. }
  3043. else
  3044. {
  3045. Name = LoadStr(NEW_FILE);
  3046. }
  3047. UnicodeString Names = Name;
  3048. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  3049. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  3050. HELP_EDIT_NEW, History.get(), true, NULL, true, 444))
  3051. {
  3052. while (!Names.IsEmpty())
  3053. {
  3054. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  3055. CustomWinConfiguration->History[L"EditFile"] = History.get();
  3056. UnicodeString TargetFileName;
  3057. UnicodeString LocalFileName;
  3058. UnicodeString RootTempDir;
  3059. UnicodeString TempDir;
  3060. UnicodeString RemoteDirectory;
  3061. bool ExistingFile = false;
  3062. if (!IsSideLocalBrowser(Side))
  3063. {
  3064. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  3065. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(Name));
  3066. if ((File.get() != NULL) &&
  3067. !File->IsDirectory)
  3068. {
  3069. ExecuteRemoteFile(Name, File.get(), efDefaultEditor);
  3070. ExistingFile = true;
  3071. }
  3072. File.reset(NULL);
  3073. if (!ExistingFile)
  3074. {
  3075. RemoteDirectory = UnixExtractFilePath(Name);
  3076. TemporaryDirectoryForRemoteFiles(
  3077. RemoteDirectory, GUIConfiguration->CurrentCopyParam, false, TempDir, RootTempDir);
  3078. TargetFileName = UnixExtractFileName(Name);
  3079. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3080. LocalFileName = TempDir +
  3081. // We probably do not want to trim the VMS version here
  3082. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  3083. }
  3084. }
  3085. else
  3086. {
  3087. if (ExtractFilePath(Name).IsEmpty())
  3088. {
  3089. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  3090. }
  3091. else
  3092. {
  3093. LocalFileName = ExpandFileName(Name);
  3094. }
  3095. TargetFileName = ExtractFileName(Name);
  3096. }
  3097. if (!ExistingFile)
  3098. {
  3099. bool NewFile = !FileExists(ApiPath(LocalFileName));
  3100. if (NewFile)
  3101. {
  3102. int File = FileCreate(ApiPath(LocalFileName));
  3103. if (File < 0)
  3104. {
  3105. if (!RootTempDir.IsEmpty())
  3106. {
  3107. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  3108. }
  3109. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  3110. }
  3111. else
  3112. {
  3113. FileClose(File);
  3114. }
  3115. }
  3116. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  3117. const TEditorData * ExternalEditor = NULL;
  3118. TFileMasks::TParams MaskParams; // size not known
  3119. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  3120. false, MaskParams);
  3121. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  3122. ExternalEditor, RootTempDir, RemoteDirectory, NewFile, TDateTime());
  3123. }
  3124. }
  3125. }
  3126. }
  3127. //---------------------------------------------------------------------------
  3128. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  3129. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  3130. {
  3131. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3132. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3133. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3134. return
  3135. ((ExecuteFileBy == efInternalEditor)) ||
  3136. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  3137. }
  3138. //---------------------------------------------------------------------------
  3139. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  3140. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  3141. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  3142. UnicodeString RemoteDirectory, bool NewFile, const TDateTime & SourceTimestamp)
  3143. {
  3144. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3145. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3146. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3147. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3148. Side = GetSide(Side);
  3149. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3150. if (!IsSideLocalBrowser(Side))
  3151. {
  3152. Data->Terminal = Terminal;
  3153. Data->SessionData = CloneCurrentSessionData();
  3154. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3155. Data->RemoteDirectory = RemoteDirectory;
  3156. Data->SessionName = Terminal->SessionData->SessionName;
  3157. Data->LocalRootDirectory = LocalRootDirectory;
  3158. Data->OriginalFileName = OriginalFileName;
  3159. Data->Command = L""; // will be changed later for external editor
  3160. // Empty SourceTimestamp indicates that checking was disabled when the file was opened
  3161. if (WinConfiguration->EditorCheckNotModified)
  3162. {
  3163. Data->SourceTimestamp = SourceTimestamp;
  3164. }
  3165. }
  3166. if (ExecuteFileBy == efInternalEditor)
  3167. {
  3168. if (!IsSideLocalBrowser(Side))
  3169. {
  3170. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3171. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3172. TForm * Editor;
  3173. try
  3174. {
  3175. Editor =
  3176. ShowEditorForm(
  3177. FileName, this,
  3178. FEditorManager->FileChanged, FEditorManager->FileReload, FEditorManager->FileClosed,
  3179. SaveAllInternalEditors, AnyInternalEditorModified,
  3180. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3181. }
  3182. catch(...)
  3183. {
  3184. if (!LocalRootDirectory.IsEmpty())
  3185. {
  3186. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory));
  3187. }
  3188. throw;
  3189. }
  3190. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3191. AppLogFmt(L"Opened internal editor with \"%s\"", (FileName));
  3192. }
  3193. else
  3194. {
  3195. DebugAssert(!StandaloneOperation);
  3196. TForm * Editor =
  3197. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3198. SaveAllInternalEditors, AnyInternalEditorModified,
  3199. L"", false, SessionColor, -1, NewFile);
  3200. FLocalEditors->Add(Editor);
  3201. }
  3202. }
  3203. else
  3204. {
  3205. HANDLE Process;
  3206. if (ExecuteFileBy == efExternalEditor)
  3207. {
  3208. UnicodeString Program, Params, Dir;
  3209. Data->Command = ExternalEditor->ExternalEditor;
  3210. ReformatFileNameCommand(Data->Command);
  3211. SplitCommand(Data->Command, Program, Params, Dir);
  3212. Params = ExpandFileNameCommand(Params, FileName);
  3213. Program = ExpandEnvironmentVariables(Program);
  3214. if (!ExecuteShell(Program, Params, Process))
  3215. {
  3216. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3217. }
  3218. }
  3219. else
  3220. {
  3221. DebugAssert(!IsSideLocalBrowser(Side));
  3222. if (!ExecuteShell(FileName, L"", Process))
  3223. {
  3224. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3225. }
  3226. }
  3227. if (IsSideLocalBrowser(Side) ||
  3228. ((ExecuteFileBy == efShell) &&
  3229. !WinConfiguration->Editor.SDIShellEditor) ||
  3230. ((ExecuteFileBy == efExternalEditor) &&
  3231. !ExternalEditor->SDIExternalEditor))
  3232. {
  3233. // no need for handle
  3234. if (Process != NULL)
  3235. {
  3236. DebugCheck(CloseHandle(Process));
  3237. }
  3238. Process = INVALID_HANDLE_VALUE;
  3239. }
  3240. else
  3241. {
  3242. if (Process == NULL)
  3243. {
  3244. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3245. }
  3246. }
  3247. if (!IsSideLocalBrowser(Side))
  3248. {
  3249. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3250. AppLogFmt(L"Opened external editor with remote file \"%s\".", (FileName));
  3251. }
  3252. }
  3253. }
  3254. //---------------------------------------------------------------------------
  3255. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3256. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3257. void * /*Arg*/)
  3258. {
  3259. if (Token != NULL)
  3260. {
  3261. EditorFormFileSave(static_cast<TForm *>(Token));
  3262. }
  3263. }
  3264. //---------------------------------------------------------------------------
  3265. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3266. {
  3267. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3268. {
  3269. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3270. }
  3271. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3272. }
  3273. //---------------------------------------------------------------------------
  3274. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3275. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3276. void * Arg)
  3277. {
  3278. if ((Token != NULL) &&
  3279. IsEditorFormModified(static_cast<TForm *>(Token)))
  3280. {
  3281. *static_cast<bool *>(Arg) = true;
  3282. }
  3283. }
  3284. //---------------------------------------------------------------------------
  3285. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3286. TObject * /*Sender*/, bool & Modified)
  3287. {
  3288. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3289. {
  3290. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3291. {
  3292. Modified = true;
  3293. }
  3294. }
  3295. if (!Modified)
  3296. {
  3297. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3298. }
  3299. }
  3300. //---------------------------------------------------------------------------
  3301. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3302. {
  3303. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3304. }
  3305. //---------------------------------------------------------------------------
  3306. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3307. const UnicodeString & RemoteDirectory, const TCopyParamType & CopyParam, bool Simple,
  3308. UnicodeString & Result, UnicodeString & RootDirectory)
  3309. {
  3310. if (!WinConfiguration->TemporaryDirectoryDeterministic || Simple)
  3311. {
  3312. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3313. Result = RootDirectory;
  3314. }
  3315. else
  3316. {
  3317. RootDirectory = L"";
  3318. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3319. Result = IncludeTrailingBackslash(Result);
  3320. }
  3321. if (WinConfiguration->TemporaryDirectoryAppendSession && !Simple)
  3322. {
  3323. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3324. }
  3325. if (WinConfiguration->TemporaryDirectoryAppendPath && !Simple)
  3326. {
  3327. UnicodeString ARemoteDirectory = RemoteDirectory;
  3328. if (!ARemoteDirectory.IsEmpty() && (ARemoteDirectory[1] == L'/'))
  3329. {
  3330. ARemoteDirectory.Delete(1, 1);
  3331. }
  3332. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(ARemoteDirectory)));
  3333. }
  3334. if (!ForceDirectories(ApiPath(Result)))
  3335. {
  3336. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3337. }
  3338. }
  3339. //---------------------------------------------------------------------------
  3340. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3341. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3342. bool GetTargetNames, bool AutoOperation, bool SimpleTempDir)
  3343. {
  3344. DebugAssert(!IsLocalBrowserMode());
  3345. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3346. if (ForceText)
  3347. {
  3348. CopyParam.TransferMode = tmAscii;
  3349. }
  3350. TemporaryFileCopyParam(CopyParam);
  3351. if (TempDir.IsEmpty())
  3352. {
  3353. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, SimpleTempDir, TempDir, RootTempDir);
  3354. }
  3355. DebugAssert(!FAutoOperation);
  3356. FAutoOperation = AutoOperation;
  3357. FMoveToQueue = false;
  3358. Terminal->ExceptionOnFail = true;
  3359. try
  3360. {
  3361. try
  3362. {
  3363. // turn off confirmations, as for MDI editors we may possibly download
  3364. // the same file over
  3365. Terminal->CopyToLocal(
  3366. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3367. if (GetTargetNames)
  3368. {
  3369. for (int i = 0; i < FileList->Count; i++)
  3370. {
  3371. FileList->Strings[i] =
  3372. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3373. }
  3374. }
  3375. }
  3376. catch(...)
  3377. {
  3378. if (!RootTempDir.IsEmpty())
  3379. {
  3380. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  3381. }
  3382. throw;
  3383. }
  3384. }
  3385. __finally
  3386. {
  3387. FAutoOperation = false;
  3388. Terminal->ExceptionOnFail = false;
  3389. }
  3390. }
  3391. //---------------------------------------------------------------------------
  3392. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3393. {
  3394. // Do not waste time checking for default editor list the next time,
  3395. // or testing if default editor is not notepad.
  3396. bool TryNextTime = false;
  3397. UnicodeString UsageState;
  3398. if (!WinConfiguration->EditorList->IsDefaultList())
  3399. {
  3400. UsageState = "H";
  3401. }
  3402. else
  3403. {
  3404. UnicodeString Executable;
  3405. UnicodeString ExecutableDescription;
  3406. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3407. {
  3408. UnicodeString Message =
  3409. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3410. unsigned int Answer =
  3411. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3412. if (Answer != qaOK)
  3413. {
  3414. UsageState = "R";
  3415. }
  3416. else
  3417. {
  3418. UsageState = "A";
  3419. TEditorData EditorData;
  3420. EditorData.Editor = edExternal;
  3421. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3422. EditorData.ExternalEditorOptionsAutodetect();
  3423. TEditorList EditorList;
  3424. EditorList = *WinConfiguration->EditorList;
  3425. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3426. WinConfiguration->EditorList = &EditorList;
  3427. }
  3428. }
  3429. }
  3430. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3431. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3432. }
  3433. //---------------------------------------------------------------------------
  3434. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3435. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3436. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3437. {
  3438. if (ExecuteFileBy == efDefaultEditor)
  3439. {
  3440. if (!WinConfiguration->OfferedEditorAutoConfig)
  3441. {
  3442. EditorAutoConfig();
  3443. }
  3444. const TEditorPreferences * Editor =
  3445. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3446. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3447. {
  3448. ExecuteFileBy = efInternalEditor;
  3449. ExternalEditor = NULL;
  3450. }
  3451. else if (Editor->Data->Editor == edOpen)
  3452. {
  3453. ExecuteFileBy = efShell;
  3454. ExternalEditor = NULL;
  3455. }
  3456. else
  3457. {
  3458. ExecuteFileBy = efExternalEditor;
  3459. ExternalEditor = Editor->Data;
  3460. }
  3461. }
  3462. }
  3463. //---------------------------------------------------------------------------
  3464. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3465. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3466. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3467. {
  3468. UnicodeString OriginalFileName;
  3469. UnicodeString LocalRootDirectory;
  3470. UnicodeString RemoteDirectory;
  3471. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3472. IsSideLocalBrowser(Side), MaskParams);
  3473. UnicodeString Counter;
  3474. UnicodeString LocalFileName;
  3475. bool Handled = false;
  3476. if (!IsSideLocalBrowser(Side))
  3477. {
  3478. // We need to trim VMS version here, so that we use name without version
  3479. // when uploading back to create a new version of the file
  3480. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3481. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3482. TObject * Token = NULL;
  3483. UnicodeString LocalDirectory;
  3484. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3485. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3486. LocalDirectory))
  3487. {
  3488. if (Token != NULL)
  3489. {
  3490. TForm * Form = dynamic_cast<TForm *>(Token);
  3491. if (Form->WindowState == wsMinimized)
  3492. {
  3493. ShowWindow(Form->Handle, SW_RESTORE);
  3494. }
  3495. else
  3496. {
  3497. Form->SetFocus();
  3498. }
  3499. Handled = true;
  3500. }
  3501. else
  3502. {
  3503. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3504. }
  3505. }
  3506. if (!Handled)
  3507. {
  3508. TStringList * FileList1 = new TStringList();
  3509. try
  3510. {
  3511. FileList1->AddObject(FullFileName, Object);
  3512. TemporarilyDownloadFiles(FileList1,
  3513. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3514. LocalRootDirectory, LocalDirectory, true, true, false);
  3515. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3516. }
  3517. __finally
  3518. {
  3519. delete FileList1;
  3520. }
  3521. switch (ExecuteFileBy)
  3522. {
  3523. case efShell:
  3524. Counter = "RemoteFilesExecuted";
  3525. break;
  3526. case efInternalEditor:
  3527. Counter = "RemoteFilesOpenedInInternalEditor";
  3528. break;
  3529. case efExternalEditor:
  3530. Counter = "RemoteFilesOpenedInExternalEditor";
  3531. break;
  3532. default:
  3533. DebugFail();
  3534. }
  3535. }
  3536. }
  3537. else
  3538. {
  3539. LocalFileName = FullFileName;
  3540. OriginalFileName = ExtractFileName(FullFileName);
  3541. switch (ExecuteFileBy)
  3542. {
  3543. case efShell:
  3544. Counter = "LocalFilesExecuted";
  3545. break;
  3546. case efInternalEditor:
  3547. Counter = "LocalFilesOpenedInInternalEditor";
  3548. break;
  3549. case efExternalEditor:
  3550. Counter = "LocalFilesOpenedInExternalEditor";
  3551. break;
  3552. default:
  3553. DebugFail();
  3554. }
  3555. }
  3556. if (!Handled)
  3557. {
  3558. Configuration->Usage->Inc(Counter);
  3559. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3560. ExternalEditor, LocalRootDirectory, RemoteDirectory, false, MaskParams.Modification);
  3561. }
  3562. }
  3563. //---------------------------------------------------------------------------
  3564. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3565. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3566. bool AllSelected, bool OnFocused)
  3567. {
  3568. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3569. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3570. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3571. Side = GetSide(Side);
  3572. TCustomDirView * DView = DirView(Side);
  3573. TStrings * FileList =
  3574. AllSelected ?
  3575. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3576. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3577. try
  3578. {
  3579. DebugAssert(AllSelected || (FileList->Count == 1));
  3580. for (int i = 0; i < FileList->Count; i++)
  3581. {
  3582. UnicodeString ListFileName = FileList->Strings[i];
  3583. UnicodeString FileNameOnly =
  3584. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3585. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3586. if (!DView->ItemIsDirectory(Item))
  3587. {
  3588. UnicodeString FullFileName;
  3589. if (!IsSideLocalBrowser(Side))
  3590. {
  3591. FullFileName = DirView(Side)->Path + ListFileName;
  3592. }
  3593. else
  3594. {
  3595. FullFileName = ListFileName;
  3596. }
  3597. TObject * Object = FileList->Objects[i];
  3598. TFileMasks::TParams MaskParams;
  3599. MaskParams.Size = DView->ItemFileSize(Item);
  3600. TDateTimePrecision Precision;
  3601. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3602. std::unique_ptr<TRemoteFile> File;
  3603. if (!IsSideLocalBrowser(Side))
  3604. {
  3605. File.reset(Terminal->TryReadFile(FullFileName));
  3606. if (File.get() != NULL)
  3607. {
  3608. File->FullFileName = FullFileName;
  3609. Object = File.get();
  3610. MaskParams.Size = File->Size;
  3611. MaskParams.Modification = File->Modification;
  3612. }
  3613. }
  3614. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName, Object, MaskParams);
  3615. }
  3616. }
  3617. }
  3618. __finally
  3619. {
  3620. delete FileList;
  3621. }
  3622. }
  3623. //---------------------------------------------------------------------------
  3624. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3625. {
  3626. CopyParam.FileNameCase = ncNoChange;
  3627. CopyParam.PreserveRights = false;
  3628. CopyParam.PreserveReadOnly = false;
  3629. CopyParam.ReplaceInvalidChars = true;
  3630. CopyParam.IncludeFileMask = TFileMasks();
  3631. CopyParam.NewerOnly = false;
  3632. CopyParam.FileMask = L"";
  3633. }
  3634. //---------------------------------------------------------------------------
  3635. bool TCustomScpExplorerForm::EditorCheckNotModified(const TEditedFileData * Data)
  3636. {
  3637. return WinConfiguration->EditorCheckNotModified && (Data->SourceTimestamp != TDateTime());
  3638. }
  3639. //---------------------------------------------------------------------------
  3640. class TEditorUploadQueueItem : public TUploadQueueItem
  3641. {
  3642. public:
  3643. __fastcall TEditorUploadQueueItem(
  3644. TTerminal * Terminal, TStrings * FilesToCopy, const UnicodeString & TargetDir,
  3645. const TCopyParamType * CopyParam, int Params) :
  3646. TUploadQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, false)
  3647. {
  3648. }
  3649. protected:
  3650. virtual void __fastcall DoTransferExecute(TTerminal * Terminal, TParallelOperation * ParallelOperation)
  3651. {
  3652. TUploadQueueItem::DoTransferExecute(Terminal, ParallelOperation);
  3653. TTerminalManager::Instance()->ScpExplorer->EditedFileUploaded(Terminal, CompleteEvent);
  3654. }
  3655. };
  3656. //---------------------------------------------------------------------------
  3657. void TCustomScpExplorerForm::EditedFileUploaded(TTerminal * ATerminal, HANDLE UploadCompleteEvent)
  3658. {
  3659. if (WinConfiguration->EditorCheckNotModified) // optimization
  3660. {
  3661. UnicodeString RemoteFilePath;
  3662. {
  3663. TGuard Guard(FEditorManager->Section);
  3664. TEditedFileData * Data = FEditorManager->FindByUploadCompleteEvent(UploadCompleteEvent);
  3665. if (DebugAlwaysTrue(Data != NULL) &&
  3666. EditorCheckNotModified(Data))
  3667. {
  3668. RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3669. }
  3670. }
  3671. if (!RemoteFilePath.IsEmpty())
  3672. {
  3673. std::unique_ptr<TRemoteFile> File(ATerminal->TryReadFile(RemoteFilePath));
  3674. if (File.get() != NULL)
  3675. {
  3676. TGuard Guard(FEditorManager->Section);
  3677. TEditedFileData * Data = FEditorManager->FindByUploadCompleteEvent(UploadCompleteEvent);
  3678. if (DebugAlwaysTrue(Data != NULL))
  3679. {
  3680. Data->SourceTimestamp = File->Modification;
  3681. AppLogFmt(L"Uploaded edited remote file timestamp: %s", (StandardTimestamp(Data->SourceTimestamp)));
  3682. }
  3683. }
  3684. }
  3685. }
  3686. }
  3687. //---------------------------------------------------------------------------
  3688. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
  3689. const UnicodeString & FileName, const TDateTime & Timestamp, TEditedFileData * Data,
  3690. HANDLE UploadCompleteEvent, bool & Retry)
  3691. {
  3692. TTerminalManager * Manager = TTerminalManager::Instance();
  3693. bool IsInactive = !IsActiveTerminal(Data->Terminal);
  3694. if ((Manager->ActiveTerminal == Data->Terminal) &&
  3695. Manager->ScheduleTerminalReconnnect(Data->Terminal))
  3696. {
  3697. AppLog(L"Scheduled session reconnect, will retry the upload later");
  3698. Retry = true;
  3699. }
  3700. else if (Manager->IsReconnectingTerminal(Data->Terminal))
  3701. {
  3702. AppLog(L"Session is being reconnected, will retry the upload later");
  3703. Retry = true;
  3704. }
  3705. else if (IsInactive &&
  3706. GUIConfiguration->SessionReopenAutoInactive &&
  3707. (Data->Terminal->NextInactiveReconnect != TDateTime()))
  3708. {
  3709. if (Data->Terminal->LastInactiveSave != Timestamp)
  3710. {
  3711. AppLog(L"Inactive session reconnect is pending, rescheduling it for as soon as possible to retry the upload");
  3712. Data->Terminal->NextInactiveReconnect = Now();
  3713. Data->Terminal->LastInactiveSave = Timestamp;
  3714. }
  3715. else
  3716. {
  3717. AppLog(L"Inactive session reconnect is pending, will retry the upload later");
  3718. }
  3719. Retry = true;
  3720. }
  3721. else if (IsInactive)
  3722. {
  3723. if (!NonVisualDataModule->Busy)
  3724. {
  3725. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3726. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3727. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3728. if (Data->Terminal == NULL)
  3729. {
  3730. TManagedTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3731. if (SameSiteTerminal != NULL)
  3732. {
  3733. UnicodeString Message =
  3734. FMTLOAD(EDIT_SESSION_REATTACH,
  3735. (FileNameOnly, Data->SessionName, FileNameOnly));
  3736. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3737. {
  3738. Data->Terminal = SameSiteTerminal;
  3739. Data->SessionName = SameSiteTerminal->SessionData->SessionName;
  3740. // We might also overwrite session data
  3741. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3742. }
  3743. else
  3744. {
  3745. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3746. Abort();
  3747. }
  3748. }
  3749. }
  3750. // Foreground session should reconnect itself (except for those disconnected explicitly by the user)
  3751. else if (DebugAlwaysTrue(Terminal->Disconnected || (Terminal != Data->Terminal)))
  3752. {
  3753. UnicodeString Message =
  3754. MainInstructions(
  3755. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3756. TMessageParams Params(FLAGMASK(!GUIConfiguration->SessionReopenAutoInactive, mpNeverAskAgainCheck));
  3757. Params.NeverAskAgainTitle = LoadStr(ALWAYS_RECONNECT);
  3758. unsigned int Answer = MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_NONE, &Params);
  3759. if (Answer == qaNeverAskAgain)
  3760. {
  3761. GUIConfiguration->SessionReopenAutoInactive = true;
  3762. Answer = qaOK;
  3763. }
  3764. if (Answer == qaOK)
  3765. {
  3766. if (Terminal->Disconnected)
  3767. {
  3768. Manager->ActiveSession = Terminal;
  3769. ReconnectSession();
  3770. // Was it reconnected? (if not, Data->Terminal should be nulled now)
  3771. if (!IsActiveTerminal(Data->Terminal) || (Terminal != Data->Terminal))
  3772. {
  3773. Abort();
  3774. }
  3775. }
  3776. else
  3777. {
  3778. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3779. }
  3780. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3781. }
  3782. else
  3783. {
  3784. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3785. Abort();
  3786. }
  3787. }
  3788. }
  3789. if (!IsActiveTerminal(Data->Terminal))
  3790. {
  3791. Configuration->Usage->Inc(L"EditInactiveSession");
  3792. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3793. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3794. (ExtractFileName(FileName), Data->SessionName)));
  3795. }
  3796. }
  3797. if (!Retry && EditorCheckNotModified(Data))
  3798. {
  3799. if (NonVisualDataModule->Busy)
  3800. {
  3801. Retry = true;
  3802. }
  3803. else
  3804. {
  3805. UnicodeString RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3806. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(RemoteFilePath));
  3807. if (File.get() != NULL)
  3808. {
  3809. AppLogFmt(L"Edited remote file timestamp: %s, Original timestamp: %s", (StandardTimestamp(File->Modification), StandardTimestamp(Data->SourceTimestamp)));
  3810. if (File->Modification != Data->SourceTimestamp)
  3811. {
  3812. UnicodeString Message = MainInstructions(LoadStr(EDIT_CHANGED_EXTERNALLY));
  3813. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) != qaOK)
  3814. {
  3815. Abort();
  3816. }
  3817. }
  3818. }
  3819. }
  3820. }
  3821. if (!Retry)
  3822. {
  3823. TStrings * FileList = new TStringList();
  3824. try
  3825. {
  3826. FileList->Add(FileName);
  3827. // Consider using the same settings (preset) as when the file was downloaded.
  3828. // More over this does not reflect the actual session that will do the upload.
  3829. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3830. TemporaryFileCopyParam(CopyParam);
  3831. if (Data->ForceText)
  3832. {
  3833. CopyParam.TransferMode = tmAscii;
  3834. }
  3835. // so i do not need to worry if masking algorithm works in all cases
  3836. // ("" means "copy file name", no masking is actually done)
  3837. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3838. {
  3839. CopyParam.FileMask = L"";
  3840. }
  3841. else
  3842. {
  3843. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3844. }
  3845. int Params = cpNoConfirmation | cpTemporary;
  3846. if (Data->Terminal->IsCapable[fcBackgroundTransfers])
  3847. {
  3848. TTerminalQueue * AQueue = Manager->FindQueueForTerminal(Data->Terminal);
  3849. TQueueItem * QueueItem =
  3850. new TEditorUploadQueueItem(Data->Terminal, FileList, Data->RemoteDirectory, &CopyParam, Params);
  3851. QueueItem->CompleteEvent = UploadCompleteEvent;
  3852. AddQueueItem(AQueue, QueueItem, Data->Terminal);
  3853. }
  3854. else
  3855. {
  3856. if (NonVisualDataModule->Busy)
  3857. {
  3858. Retry = true;
  3859. }
  3860. else
  3861. {
  3862. FMoveToQueue = false;
  3863. Data->Terminal->CopyToRemote(FileList, Data->RemoteDirectory, &CopyParam, Params, NULL);
  3864. SetEvent(UploadCompleteEvent);
  3865. }
  3866. }
  3867. }
  3868. __finally
  3869. {
  3870. delete FileList;
  3871. }
  3872. }
  3873. }
  3874. //---------------------------------------------------------------------------
  3875. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3876. const UnicodeString & FileName, TEditedFileData * Data)
  3877. {
  3878. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3879. DebugAssert(!NonVisualDataModule->Busy);
  3880. if (!IsActiveTerminal(Data->Terminal))
  3881. {
  3882. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3883. (ExtractFileName(FileName), Data->SessionName)));
  3884. }
  3885. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3886. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3887. NonVisualDataModule->StartBusy();
  3888. try
  3889. {
  3890. std::unique_ptr<TRemoteFile> File;
  3891. UnicodeString RemoteFileName =
  3892. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3893. Terminal->ExceptionOnFail = true;
  3894. try
  3895. {
  3896. File.reset(Terminal->ReadFile(RemoteFileName));
  3897. if (!File->HaveFullFileName)
  3898. {
  3899. File->FullFileName = RemoteFileName;
  3900. }
  3901. }
  3902. __finally
  3903. {
  3904. Terminal->ExceptionOnFail = false;
  3905. }
  3906. std::unique_ptr<TStrings> FileList(new TStringList());
  3907. FileList->AddObject(RemoteFileName, File.get());
  3908. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3909. UnicodeString TempDir = ExtractFilePath(FileName);
  3910. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, true, false);
  3911. if (EditorCheckNotModified(Data))
  3912. {
  3913. Data->SourceTimestamp = File->Modification;
  3914. }
  3915. // sanity check, the target file name should be still the same
  3916. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3917. }
  3918. __finally
  3919. {
  3920. NonVisualDataModule->EndBusy();
  3921. // it actually may not exist anymore...
  3922. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3923. }
  3924. }
  3925. //---------------------------------------------------------------------------
  3926. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3927. const TEditedFileData * Data, bool & KeepOpen)
  3928. {
  3929. // Command is set for external editors only (not for "shell" open).
  3930. if (!Data->Command.IsEmpty())
  3931. {
  3932. bool AnyFound = false;
  3933. bool AnyMDI = false;
  3934. bool AnyNonMDI = false;
  3935. bool AnyDetect = false;
  3936. TEditorList * EditorList = new TEditorList();
  3937. try
  3938. {
  3939. *EditorList = *WinConfiguration->EditorList;
  3940. for (int i = 0; i < EditorList->Count; i++)
  3941. {
  3942. const TEditorPreferences * Editor = EditorList->Editors[i];
  3943. if ((Editor->Data->Editor == edExternal) &&
  3944. (Editor->Data->ExternalEditor == Data->Command))
  3945. {
  3946. AnyFound = true;
  3947. if (Editor->Data->SDIExternalEditor)
  3948. {
  3949. AnyNonMDI = true;
  3950. if (Editor->Data->DetectMDIExternalEditor)
  3951. {
  3952. AnyDetect = true;
  3953. }
  3954. }
  3955. else
  3956. {
  3957. AnyMDI = true;
  3958. }
  3959. }
  3960. }
  3961. bool EnableMDI = false;
  3962. bool DisableDetect = false;
  3963. if (AnyMDI)
  3964. {
  3965. KeepOpen = true;
  3966. if (AnyNonMDI)
  3967. {
  3968. // there is at least one instance of the editor with MDI support enabled,
  3969. // and one with disabled, enable it for all instances
  3970. EnableMDI = true;
  3971. }
  3972. }
  3973. else if (AnyFound && !AnyDetect)
  3974. {
  3975. // at least once instance found but all have MDI autodetection disabled
  3976. // => close the file (default action)
  3977. }
  3978. else
  3979. {
  3980. // no instance of the editor has MDI support enabled
  3981. TMessageParams Params;
  3982. if (AnyFound)
  3983. {
  3984. // there is at least one instance of the editor
  3985. Params.Params |= mpNeverAskAgainCheck;
  3986. }
  3987. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3988. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3989. switch (Answer)
  3990. {
  3991. case qaNeverAskAgain:
  3992. DisableDetect = true;
  3993. break;
  3994. case qaNo:
  3995. EnableMDI = true;
  3996. KeepOpen = true;
  3997. break;
  3998. }
  3999. }
  4000. if (AnyFound && (EnableMDI || DisableDetect))
  4001. {
  4002. bool Changed = false;
  4003. for (int i = 0; i < EditorList->Count; i++)
  4004. {
  4005. const TEditorPreferences * Editor = EditorList->Editors[i];
  4006. if ((Editor->Data->Editor == edExternal) &&
  4007. (Editor->Data->ExternalEditor == Data->Command) &&
  4008. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  4009. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  4010. {
  4011. Changed = true;
  4012. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  4013. if (EnableMDI)
  4014. {
  4015. UpdatedEditor->GetData()->SDIExternalEditor = false;
  4016. }
  4017. if (DisableDetect)
  4018. {
  4019. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  4020. }
  4021. EditorList->Change(i, UpdatedEditor);
  4022. }
  4023. }
  4024. if (Changed)
  4025. {
  4026. WinConfiguration->EditorList = EditorList;
  4027. }
  4028. }
  4029. }
  4030. __finally
  4031. {
  4032. delete EditorList;
  4033. }
  4034. }
  4035. else
  4036. {
  4037. // "open" case
  4038. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  4039. qaOK, HELP_APP_EARLY_CLOSED);
  4040. }
  4041. }
  4042. //---------------------------------------------------------------------------
  4043. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  4044. {
  4045. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  4046. }
  4047. //---------------------------------------------------------------------------
  4048. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  4049. {
  4050. SideEnter(osRemote);
  4051. }
  4052. //---------------------------------------------------------------------------
  4053. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  4054. {
  4055. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  4056. SideEnter(osRemote);
  4057. }
  4058. //---------------------------------------------------------------------------
  4059. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  4060. {
  4061. FCurrentSide = Side;
  4062. if (Visible)
  4063. {
  4064. UpdateControls();
  4065. }
  4066. }
  4067. //---------------------------------------------------------------------------
  4068. void __fastcall TCustomScpExplorerForm::FileDeleted(
  4069. TOperationSide Side, const UnicodeString & FileName, bool Success, bool NotCancelled)
  4070. {
  4071. VisualiseOperationFinished(Side, FileName, Success && NotCancelled);
  4072. if (DebugAlwaysTrue(FDeletedFiles != NULL) && Success)
  4073. {
  4074. FDeletedFiles->Add(FileName);
  4075. }
  4076. }
  4077. //---------------------------------------------------------------------------
  4078. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  4079. TStrings * FileList, bool Alternative)
  4080. {
  4081. TCustomDirView * DView = DirView(Side);
  4082. DView->SaveSelection();
  4083. DView->SaveSelectedNames();
  4084. DebugAssert(!FAlternativeDelete);
  4085. FAlternativeDelete = Alternative;
  4086. try
  4087. {
  4088. int Params = FLAGMASK(Alternative, dfAlternative);
  4089. bool Remote = !IsSideLocalBrowser(Side);
  4090. std::unique_ptr<TStringList> DeletedFiles;
  4091. std::unique_ptr<TStrings> PermanentFileList;
  4092. bool AllowMoveToQueue = (Terminal != NULL);
  4093. if (AllowMoveToQueue)
  4094. {
  4095. DeletedFiles.reset(new TStringList());
  4096. DeletedFiles->CaseSensitive = Remote;
  4097. }
  4098. try
  4099. {
  4100. DebugAssert(FDeletedFiles == NULL);
  4101. TValueRestorer<TStrings *> DeletedFilesRestorer(FDeletedFiles, DeletedFiles.get());
  4102. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  4103. if (AllowMoveToQueue)
  4104. {
  4105. FOnFileOperationFinished = FileDeleted;
  4106. }
  4107. FMoveToQueue = false;
  4108. if (Remote)
  4109. {
  4110. DebugAssert(AllowMoveToQueue);
  4111. // Clone the file list as it may refer to current directory files, which get destroyed, when the directory is reloaded after the operation
  4112. PermanentFileList.reset(TRemoteFileList::CloneStrings(FileList));
  4113. DebugAssert(Terminal != NULL);
  4114. Terminal->DeleteFiles(FileList, Params);
  4115. }
  4116. else
  4117. {
  4118. if (AllowMoveToQueue) // optimization)
  4119. {
  4120. PermanentFileList.reset(CloneStrings(FileList));
  4121. }
  4122. if (IsLocalBrowserMode())
  4123. {
  4124. Configuration->Usage->Inc(L"LocalLocalDeletes");
  4125. }
  4126. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide, FCurrentSide);
  4127. try
  4128. {
  4129. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, Params);
  4130. }
  4131. __finally
  4132. {
  4133. ReloadLocalDirectory();
  4134. }
  4135. }
  4136. // Probably not needed for deleting, just for consistency with transfer code
  4137. if (FMoveToQueue)
  4138. {
  4139. Abort();
  4140. }
  4141. }
  4142. catch (EAbort &)
  4143. {
  4144. if (FMoveToQueue && DebugAlwaysTrue(AllowMoveToQueue) && DebugAlwaysTrue(Terminal != NULL))
  4145. {
  4146. FMoveToQueue = false;
  4147. DeletedFiles->Sorted = true;
  4148. for (int Index = 0; Index < PermanentFileList->Count; Index++)
  4149. {
  4150. if (DeletedFiles->IndexOf(PermanentFileList->Strings[Index]) >= 0)
  4151. {
  4152. // We should always be deleting the first item => what can be used to optimize this code, if needed
  4153. DebugAssert(Index == 0);
  4154. PermanentFileList->Delete(Index);
  4155. Index--;
  4156. }
  4157. }
  4158. if (Remote)
  4159. {
  4160. Configuration->Usage->Inc("MovesToBackgroundDelete");
  4161. TQueueItem * QueueItem = new TRemoteDeleteQueueItem(Terminal, PermanentFileList.get(), Params);
  4162. AddQueueItem(Queue, QueueItem, Terminal);
  4163. }
  4164. else
  4165. {
  4166. Configuration->Usage->Inc("MovesToBackgroundDeleteLocal");
  4167. TQueueItem * QueueItem = new TLocalDeleteQueueItem(PermanentFileList.get(), Params);
  4168. AddQueueItem(Queue, QueueItem, Terminal);
  4169. }
  4170. ClearOperationSelection(Side);
  4171. }
  4172. }
  4173. FAlternativeDelete = false;
  4174. }
  4175. catch(...)
  4176. {
  4177. FAlternativeDelete = false;
  4178. DView->DiscardSavedSelection();
  4179. throw;
  4180. }
  4181. DView->RestoreSelection();
  4182. }
  4183. //---------------------------------------------------------------------------
  4184. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  4185. {
  4186. DebugAssert(Terminal);
  4187. DebugAssert(!IsLocalBrowserMode());
  4188. RemoteDirView->SaveSelection();
  4189. RemoteDirView->SaveSelectedNames();
  4190. try
  4191. {
  4192. if (Lock)
  4193. {
  4194. Terminal->LockFiles(FileList);
  4195. }
  4196. else
  4197. {
  4198. Terminal->UnlockFiles(FileList);
  4199. }
  4200. }
  4201. catch(...)
  4202. {
  4203. RemoteDirView->DiscardSavedSelection();
  4204. throw;
  4205. }
  4206. RemoteDirView->RestoreSelection();
  4207. }
  4208. //---------------------------------------------------------------------------
  4209. bool TCustomScpExplorerForm::DoDirectoryExists(void * Session, const UnicodeString & Directory)
  4210. {
  4211. TTerminal * ATerminal = (Session == NULL) ? Terminal : reinterpret_cast<TTerminal *>(Session);
  4212. bool Result =
  4213. IsActiveTerminal(ATerminal) &&
  4214. ATerminal->DirectoryExists(Directory);
  4215. return Result;
  4216. }
  4217. //---------------------------------------------------------------------------
  4218. bool TCustomScpExplorerForm::NeedSecondarySessionForRemoteCopy(TStrings * FileList)
  4219. {
  4220. bool CopyDirsOnSecondarySession = Terminal->IsCapable[fcSecondaryShell];
  4221. return
  4222. !Terminal->IsCapable[fcRemoteCopy] ||
  4223. (CopyDirsOnSecondarySession && TRemoteFileList::AnyDirectory(FileList));
  4224. }
  4225. //---------------------------------------------------------------------------
  4226. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  4227. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  4228. bool NoConfirmation, bool Move)
  4229. {
  4230. DebugAssert(Terminal != NULL);
  4231. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  4232. DebugAssert(!IsLocalBrowserMode());
  4233. if (Session == NULL)
  4234. {
  4235. Session = Terminal;
  4236. }
  4237. bool TargetConfirmed = false;
  4238. if (Session == Terminal)
  4239. {
  4240. if (RemoteDriveView->DropTarget != NULL)
  4241. {
  4242. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  4243. TargetConfirmed = true;
  4244. }
  4245. else if (RemoteDirView->DropTarget != NULL)
  4246. {
  4247. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  4248. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  4249. TargetConfirmed = true;
  4250. }
  4251. else
  4252. {
  4253. Target = RemoteDirView->Path;
  4254. }
  4255. }
  4256. else
  4257. {
  4258. Target = GetSessionPath(Session, osRemote);
  4259. }
  4260. Target = UnixIncludeTrailingBackslash(Target);
  4261. if (FileList->Count == 1)
  4262. {
  4263. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  4264. }
  4265. else
  4266. {
  4267. FileMask = AnyMask;
  4268. }
  4269. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  4270. bool Result = true;
  4271. if (!NoConfirmation)
  4272. {
  4273. bool Multi = (FileList->Count > 1);
  4274. if (Move)
  4275. {
  4276. Result = DoRemoteMoveDialog(Multi, Target, FileMask, DoDirectoryExists);
  4277. }
  4278. else
  4279. {
  4280. std::unique_ptr<TStrings> Sessions(new TStringList);
  4281. std::unique_ptr<TStrings> Directories(new TStringList);
  4282. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  4283. for (int Index = 0; Index < SessionList->Count; Index++)
  4284. {
  4285. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  4286. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  4287. {
  4288. Sessions->AddObject(SessionList->Strings[Index], ASession);
  4289. Directories->Add(GetSessionPath(ASession, osRemote));
  4290. }
  4291. }
  4292. bool AnyDirectory = false;
  4293. bool CopyDirsOnSecondarySession = Terminal->IsCapable[fcSecondaryShell];
  4294. if (CopyDirsOnSecondarySession && !Terminal->CommandSessionOpened) // optimization
  4295. {
  4296. for (int Index = 0; !AnyDirectory && (Index < FileList->Count); Index++)
  4297. {
  4298. AnyDirectory = DebugNotNull(dynamic_cast<TRemoteFile *>(FileList->Objects[Index]))->IsDirectory;
  4299. }
  4300. }
  4301. TDirectRemoteCopy AllowDirectCopy;
  4302. if (Terminal->CommandSessionOpened || !NeedSecondarySessionForRemoteCopy(FileList))
  4303. {
  4304. DebugAssert(DirectCopy);
  4305. AllowDirectCopy = drcAllow;
  4306. }
  4307. else if (Terminal->IsCapable[fcSecondaryShell])
  4308. {
  4309. DebugAssert(DirectCopy);
  4310. AllowDirectCopy = Terminal->IsCapable[fcRemoteCopy] ? drcConfirmCommandSessionDirs : drcConfirmCommandSession;
  4311. }
  4312. else
  4313. {
  4314. DebugAssert(!DirectCopy);
  4315. AllowDirectCopy = drcDisallow;
  4316. }
  4317. void * ASession = Session;
  4318. Result = DoRemoteCopyDialog(
  4319. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal,
  4320. DoDirectoryExists, TargetConfirmed);
  4321. Session = static_cast<TManagedTerminal *>(ASession);
  4322. }
  4323. }
  4324. return Result;
  4325. }
  4326. //---------------------------------------------------------------------------
  4327. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  4328. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  4329. {
  4330. DebugAssert(!IsLocalBrowserMode());
  4331. bool DirectCopy;
  4332. UnicodeString Target, FileMask;
  4333. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  4334. if (Result)
  4335. {
  4336. if (!Move && !DirectCopy)
  4337. {
  4338. Configuration->Usage->Inc("RemoteCopyTemp");
  4339. UnicodeString RootTempDir;
  4340. UnicodeString TempDir;
  4341. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  4342. TStrings * TemporaryFilesList = new TStringList();
  4343. try
  4344. {
  4345. TMakeLocalFileListParams MakeFileListParam;
  4346. MakeFileListParam.FileList = TemporaryFilesList;
  4347. MakeFileListParam.FileTimes = NULL;
  4348. MakeFileListParam.IncludeDirs = true;
  4349. MakeFileListParam.Recursive = false;
  4350. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  4351. TTerminalManager::Instance()->ActiveSession = Session;
  4352. if (TemporaryFilesList->Count > 0)
  4353. {
  4354. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4355. CopyParam.FileMask = FileMask;
  4356. DebugAssert(!FAutoOperation);
  4357. FAutoOperation = true;
  4358. FMoveToQueue = false;
  4359. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  4360. }
  4361. }
  4362. __finally
  4363. {
  4364. delete TemporaryFilesList;
  4365. FAutoOperation = false;
  4366. if (!RootTempDir.IsEmpty())
  4367. {
  4368. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  4369. }
  4370. }
  4371. }
  4372. else
  4373. {
  4374. RemoteDirView->SaveSelection();
  4375. RemoteDirView->SaveSelectedNames();
  4376. try
  4377. {
  4378. if (Move)
  4379. {
  4380. Configuration->Usage->Inc("RemoteMove");
  4381. Terminal->MoveFiles(FileList, Target, FileMask, false);
  4382. }
  4383. else
  4384. {
  4385. Configuration->Usage->Inc("RemoteCopyDirect");
  4386. DebugAssert(DirectCopy);
  4387. DebugAssert(Session == Terminal);
  4388. if (!NeedSecondarySessionForRemoteCopy(FileList) ||
  4389. Terminal->CommandSessionOpened ||
  4390. CommandSessionFallback())
  4391. {
  4392. Terminal->CopyFiles(FileList, Target, FileMask, false);
  4393. }
  4394. }
  4395. }
  4396. catch(...)
  4397. {
  4398. RemoteDirView->DiscardSavedSelection();
  4399. throw;
  4400. }
  4401. RemoteDirView->RestoreSelection();
  4402. }
  4403. }
  4404. return Result;
  4405. }
  4406. //---------------------------------------------------------------------------
  4407. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4408. const UnicodeString & Path, TRemoteProperties & Properties)
  4409. {
  4410. DebugAssert(!IsLocalBrowserMode());
  4411. Properties.Valid = Properties.Valid << vpEncrypt;
  4412. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4413. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4414. }
  4415. //---------------------------------------------------------------------------
  4416. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4417. {
  4418. Side = GetSide(Side);
  4419. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4420. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4421. UnicodeString Name = LoadStr(NEW_FOLDER);
  4422. int AllowedChanges =
  4423. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4424. bool SaveSettings = false;
  4425. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4426. {
  4427. TWindowLock Lock(this);
  4428. if (!IsSideLocalBrowser(Side))
  4429. {
  4430. if (SaveSettings)
  4431. {
  4432. GUIConfiguration->NewDirectoryProperties = Properties;
  4433. }
  4434. CreateRemoteDirectory(Name, Properties);
  4435. }
  4436. else
  4437. {
  4438. DirView(Side)->CreateDirectory(Name);
  4439. }
  4440. }
  4441. }
  4442. //---------------------------------------------------------------------------
  4443. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4444. {
  4445. TWindowLock Lock(this);
  4446. DirView(Side)->ExecuteHomeDirectory();
  4447. }
  4448. //---------------------------------------------------------------------------
  4449. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4450. {
  4451. DoOpenDirectoryDialog(odBrowse, Side);
  4452. }
  4453. //---------------------------------------------------------------------------
  4454. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4455. {
  4456. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4457. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4458. bool Result = !Path.IsEmpty();
  4459. if (Result)
  4460. {
  4461. TryOpenDirectory(Side, Path);
  4462. }
  4463. return Result;
  4464. }
  4465. //---------------------------------------------------------------------------
  4466. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4467. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4468. bool & Close)
  4469. {
  4470. // terminal can be already closed (e.g. dropped connection)
  4471. if (Terminal != NULL)
  4472. {
  4473. try
  4474. {
  4475. TCalculateSizeParams Params;
  4476. Params.Stats = &Stats;
  4477. Terminal->CalculateFilesSize(FileList, Size, Params);
  4478. }
  4479. catch(...)
  4480. {
  4481. if (!Terminal->Active)
  4482. {
  4483. Close = true;
  4484. }
  4485. throw;
  4486. }
  4487. }
  4488. }
  4489. //---------------------------------------------------------------------------
  4490. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4491. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4492. bool & Close)
  4493. {
  4494. if ((Terminal != NULL) && // terminal can be already closed (e.g. dropped connection)
  4495. EnsureCommandSessionFallback(fcCalculatingChecksum))
  4496. {
  4497. Configuration->Usage->Inc(L"ChecksumCalculated");
  4498. try
  4499. {
  4500. Terminal->CalculateFilesChecksum(Alg, FileList, OnCalculatedChecksum);
  4501. }
  4502. catch(...)
  4503. {
  4504. if (!Terminal->Active)
  4505. {
  4506. Close = true;
  4507. }
  4508. throw;
  4509. }
  4510. }
  4511. }
  4512. //---------------------------------------------------------------------------
  4513. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4514. {
  4515. if (Terminal->LoadFilesProperties(FileList))
  4516. {
  4517. RemoteDirView->Invalidate();
  4518. }
  4519. }
  4520. //---------------------------------------------------------------------------
  4521. bool TCustomScpExplorerForm::CanCalculateChecksum()
  4522. {
  4523. return
  4524. Terminal->IsCapable[fcCalculatingChecksum] ||
  4525. (Terminal->IsCapable[fcSecondaryShell] && !Terminal->IsEncryptingFiles());
  4526. }
  4527. //---------------------------------------------------------------------------
  4528. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4529. {
  4530. bool Result;
  4531. if (!IsSideLocalBrowser(Side))
  4532. {
  4533. TRemoteTokenList * GroupList = NULL;
  4534. TRemoteTokenList * UserList = NULL;
  4535. try
  4536. {
  4537. TRemoteProperties CurrentProperties;
  4538. LoadFilesProperties(FileList);
  4539. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4540. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4541. if (CapableGroupChanging || CapableOwnerChanging)
  4542. {
  4543. if (CapableGroupChanging)
  4544. {
  4545. GroupList = Terminal->Groups->Duplicate();
  4546. }
  4547. if (CapableOwnerChanging)
  4548. {
  4549. UserList = Terminal->Users->Duplicate();
  4550. }
  4551. TRemoteDirectory * Files = Terminal->Files;
  4552. int Count = Files->Count;
  4553. if (Count > 100)
  4554. {
  4555. Count = 100;
  4556. }
  4557. for (int Index = 0; Index < Count; Index++)
  4558. {
  4559. TRemoteFile * File = Files->Files[Index];
  4560. if (CapableGroupChanging)
  4561. {
  4562. GroupList->AddUnique(File->Group);
  4563. }
  4564. if (CapableOwnerChanging)
  4565. {
  4566. UserList->AddUnique(File->Owner);
  4567. }
  4568. }
  4569. // if we haven't collected tokens for all files in current directory,
  4570. // make sure we collect them at least for all selected files.
  4571. // (note that so far the files in FileList has to be from current direcotry)
  4572. if (Count < Files->Count)
  4573. {
  4574. for (int Index = 0; Index < FileList->Count; Index++)
  4575. {
  4576. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4577. if (CapableGroupChanging)
  4578. {
  4579. GroupList->AddUnique(File->Group);
  4580. }
  4581. if (CapableOwnerChanging)
  4582. {
  4583. UserList->AddUnique(File->Owner);
  4584. }
  4585. }
  4586. }
  4587. }
  4588. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4589. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4590. if (CapableAclChanging && TRemoteFileList::AnyDirectory(FileList))
  4591. {
  4592. CapableAclChanging = false;
  4593. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4594. }
  4595. int Flags = 0;
  4596. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4597. if (CapableAclChanging) Flags |= cpAcl;
  4598. if (CapableOwnerChanging) Flags |= cpOwner;
  4599. if (CapableGroupChanging) Flags |= cpGroup;
  4600. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4601. if (CanCalculateChecksum())
  4602. {
  4603. CalculateChecksumEvent = CalculateChecksum;
  4604. }
  4605. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4606. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4607. int Options =
  4608. FLAGMASK(Terminal->IsCapable[fcGroupOwnerChangingByID], poUserGroupByID) |
  4609. FLAGMASK(Terminal->IsCapable[fcTags], poTags);
  4610. TRemoteProperties NewProperties = CurrentProperties;
  4611. Result =
  4612. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4613. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags, Options,
  4614. CalculateSize, CalculateChecksumEvent);
  4615. if (Result)
  4616. {
  4617. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4618. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4619. }
  4620. }
  4621. __finally
  4622. {
  4623. delete GroupList;
  4624. delete UserList;
  4625. }
  4626. }
  4627. else
  4628. {
  4629. DirView(Side)->DisplayPropertiesMenu();
  4630. Result = true;
  4631. }
  4632. return Result;
  4633. }
  4634. //---------------------------------------------------------------------------
  4635. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4636. {
  4637. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4638. {
  4639. FIgnoreNextDialogChar = Key;
  4640. }
  4641. Key = 0;
  4642. }
  4643. //---------------------------------------------------------------------------
  4644. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4645. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4646. {
  4647. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4648. if (Command != NULL)
  4649. {
  4650. KeyProcessed(Key, Shift);
  4651. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4652. {
  4653. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4654. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4655. false, false, const_cast<TCustomCommandType *>(Command));
  4656. }
  4657. }
  4658. }
  4659. //---------------------------------------------------------------------------
  4660. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4661. {
  4662. if (!NonVisualDataModule->Busy)
  4663. {
  4664. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4665. {
  4666. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4667. }
  4668. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4669. {
  4670. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4671. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4672. {
  4673. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4674. // But so far it's not a problem, as it does what we want.
  4675. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4676. if (((Action->ShortCut == KeyShortCut) ||
  4677. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4678. AllowedAction(Action, aaShortCut))
  4679. {
  4680. // Has to be called before the action as the dialog char is already in queue.
  4681. // So when the action consumes message queue, we already need to have the
  4682. // FIgnoreNextDialogChar set
  4683. KeyProcessed(Key, Shift);
  4684. // Reset reference to previous component (when menu/toolbar was clicked).
  4685. // Needed to detect that action was invoked by keyboard shortcut
  4686. // in TNonVisualDataModule::ExplorerActionsExecute
  4687. Action->ActionComponent = NULL;
  4688. Action->Execute();
  4689. return;
  4690. }
  4691. }
  4692. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4693. {
  4694. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4695. {
  4696. KeyProcessed(Key, Shift);
  4697. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4698. return;
  4699. }
  4700. }
  4701. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4702. {
  4703. KeyProcessed(Key, Shift);
  4704. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4705. }
  4706. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4707. if (IsCustomShortCut(CustomShortCut))
  4708. {
  4709. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4710. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4711. if (WinConfiguration->SharedBookmarks != NULL)
  4712. {
  4713. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4714. if ((Bookmark != NULL) &&
  4715. OpenBookmark(FCurrentSide, Bookmark))
  4716. {
  4717. KeyProcessed(Key, Shift);
  4718. }
  4719. }
  4720. }
  4721. }
  4722. }
  4723. TForm::KeyDown(Key, Shift);
  4724. }
  4725. //---------------------------------------------------------------------------
  4726. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4727. {
  4728. bool OpenedTerminal = false;
  4729. UnicodeString ProtocolName;
  4730. UnicodeString ProtocolNameHint;
  4731. UnicodeString SecurityProtocolName;
  4732. UnicodeString DurationHint;
  4733. // See the comment in TScpCommanderForm::UpdateControls()
  4734. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4735. if (ActiveTerminal != NULL)
  4736. {
  4737. // Active is not enough here
  4738. if (ActiveTerminal->Status == ssOpened)
  4739. {
  4740. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4741. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4742. if (FileSystemInfo.ProtocolName.IsEmpty())
  4743. {
  4744. ProtocolName = SessionInfo.ProtocolName;
  4745. }
  4746. else
  4747. {
  4748. ProtocolName = FileSystemInfo.ProtocolName;
  4749. }
  4750. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4751. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4752. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4753. OpenedTerminal = true;
  4754. }
  4755. else if (ActiveTerminal->Status > ssClosed)
  4756. {
  4757. ProtocolName = LoadStr(STATUS_CONNECTING);
  4758. }
  4759. else
  4760. {
  4761. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4762. }
  4763. }
  4764. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4765. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4766. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4767. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4768. if (ProtocolStatusPanel->Caption != ProtocolName)
  4769. {
  4770. ProtocolStatusPanel->Caption = ProtocolName;
  4771. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4772. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4773. }
  4774. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4775. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4776. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4777. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4778. SecurityStatusPanel->Enabled = SecurityEnabled;
  4779. UnicodeString SecurityProtocolHint;
  4780. if (SecurityEnabled)
  4781. {
  4782. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4783. }
  4784. else
  4785. {
  4786. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4787. }
  4788. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4789. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4790. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4791. DurationStatusPanel->Hint = DurationHint;
  4792. UpdateStatusBar();
  4793. }
  4794. //---------------------------------------------------------------------------
  4795. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4796. {
  4797. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4798. DebugAssert(SessionStatusBar != NULL);
  4799. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4800. if (!FNote.IsEmpty())
  4801. {
  4802. NoteStatusPanel->Caption = FNote;
  4803. }
  4804. else
  4805. {
  4806. UpdateStatusPanelText(NoteStatusPanel);
  4807. }
  4808. NoteStatusPanel->Hint = FNoteHints;
  4809. UnicodeString LoginTime;
  4810. // See the comment in TScpCommanderForm::UpdateControls()
  4811. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4812. // Active is not enough here
  4813. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4814. {
  4815. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4816. LoginTime = FormatDateTimeSpan(Now() - SessionInfo.LoginTime);
  4817. }
  4818. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4819. DurationStatusPanel->Caption = LoginTime;
  4820. }
  4821. //---------------------------------------------------------------------------
  4822. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4823. {
  4824. Panel->Caption = L"";
  4825. }
  4826. //---------------------------------------------------------------------------
  4827. void __fastcall TCustomScpExplorerForm::Idle()
  4828. {
  4829. if (FShowing ||
  4830. // Particularly for
  4831. // - Standalone editing
  4832. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4833. // while "keeping remote directory up to date".
  4834. StandaloneOperation)
  4835. {
  4836. FEditorManager->Check();
  4837. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4838. // that needs to know if queue view is visible (and it may be closed after queue update)
  4839. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4840. }
  4841. if (FShowing)
  4842. {
  4843. if (!NonVisualDataModule->Busy &&
  4844. // Menu is opened or drag&drop is going on
  4845. (Mouse->Capture == NULL))
  4846. {
  4847. if (FRefreshRemoteDirectory)
  4848. {
  4849. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4850. {
  4851. Terminal->RefreshDirectory();
  4852. }
  4853. FRefreshRemoteDirectory = false;
  4854. }
  4855. if (FRefreshLocalDirectory)
  4856. {
  4857. ReloadLocalDirectory();
  4858. FRefreshLocalDirectory = false;
  4859. }
  4860. if (WinConfiguration->RefreshRemotePanel)
  4861. {
  4862. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4863. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4864. {
  4865. RemoteDirView->ReloadDirectory();
  4866. }
  4867. }
  4868. }
  4869. // TB2 shows accels on Alt key down, but hides it only after menu modal loop finishes.
  4870. // But if Alt is used to switch apps or for keyboard shortcut, the menu modal loop does not happen and accelerators stay displayed.
  4871. // It's difficult to fix this in TB2, as there's no "up" equivalent to CM_DIALOGKEY, which shows the accels.
  4872. // 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.
  4873. TTBCustomToolbar * MenuToolbar = DebugNotNull(dynamic_cast<TTBCustomToolbar *>(GetComponent(fcMenu)));
  4874. MenuToolbar->Idle();
  4875. }
  4876. if (FShowing || StandaloneOperation)
  4877. {
  4878. if (FQueueStatusInvalidated)
  4879. {
  4880. UpdateQueueStatus(false);
  4881. }
  4882. RefreshQueueItems();
  4883. }
  4884. if (FShowing)
  4885. {
  4886. UpdateStatusBar();
  4887. }
  4888. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4889. {
  4890. FClipboardFakeMonitorsPendingReset = false;
  4891. FClipboardFakeMonitors.reset(NULL);
  4892. }
  4893. FIgnoreNextDialogChar = 0;
  4894. }
  4895. //---------------------------------------------------------------------------
  4896. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4897. {
  4898. try
  4899. {
  4900. FUserActionTimer->Enabled = false;
  4901. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4902. {
  4903. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4904. {
  4905. FPendingQueueActionItem->ProcessUserAction();
  4906. }
  4907. }
  4908. }
  4909. __finally
  4910. {
  4911. FPendingQueueActionItem = NULL;
  4912. }
  4913. }
  4914. //---------------------------------------------------------------------------
  4915. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4916. {
  4917. if (WinConfiguration->MinimizeToTray)
  4918. {
  4919. UpdateTrayIcon();
  4920. FTrayIcon->Visible = true;
  4921. if (Visible)
  4922. {
  4923. ShowWindow(Handle, SW_HIDE);
  4924. }
  4925. }
  4926. }
  4927. //---------------------------------------------------------------------------
  4928. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4929. {
  4930. // WORKAROUND
  4931. // When restoring maximized window from minimization,
  4932. // rarely some controls do not align properly.
  4933. // Two instances seen (both for Commander):
  4934. // - When restoring, window is temporarily narrower (not maximized),
  4935. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4936. // Once maximized already, top dock shinks back, but the session PageControl,
  4937. // do not align up, leaving space between TopDock and PageControl.
  4938. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4939. for (int Index = 0; Index < ControlCount; Index++)
  4940. {
  4941. RealignControl(Controls[Index]);
  4942. }
  4943. if (FTrayIcon->Visible)
  4944. {
  4945. FTrayIcon->Visible = false;
  4946. if (Visible)
  4947. {
  4948. ShowWindow(Handle, SW_SHOW);
  4949. }
  4950. }
  4951. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4952. {
  4953. FNeedSession = false;
  4954. NonVisualDataModule->StartBusy();
  4955. try
  4956. {
  4957. NeedSession(true);
  4958. }
  4959. __finally
  4960. {
  4961. NonVisualDataModule->EndBusy();
  4962. }
  4963. }
  4964. }
  4965. //---------------------------------------------------------------------------
  4966. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4967. {
  4968. FTrayIcon->Hint = GetMainForm()->Caption;
  4969. }
  4970. //---------------------------------------------------------------------------
  4971. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4972. {
  4973. UpdateTrayIcon();
  4974. }
  4975. //---------------------------------------------------------------------------
  4976. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4977. {
  4978. // workaround
  4979. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4980. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4981. // But WM_ACTIVATEAPP message can be received even when
  4982. // the main window is minimized to the tray and internal editor window is focused
  4983. // (after another application was previously active)
  4984. if (::IsIconic(Handle))
  4985. {
  4986. if (!IsAppIconic())
  4987. {
  4988. SetAppIconic(true);
  4989. }
  4990. }
  4991. ::ApplicationRestore();
  4992. Application->BringToFront();
  4993. }
  4994. //---------------------------------------------------------------------------
  4995. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4996. {
  4997. RestoreApp();
  4998. }
  4999. //---------------------------------------------------------------------------
  5000. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  5001. {
  5002. if (OpenInNewWindow())
  5003. {
  5004. ExecuteNewInstance(SessionUrl);
  5005. }
  5006. else
  5007. {
  5008. TTerminalManager::Instance()->NewSession(SessionUrl);
  5009. }
  5010. }
  5011. //---------------------------------------------------------------------------
  5012. void TCustomScpExplorerForm::NewTab(TOperationSide, bool)
  5013. {
  5014. NewSession();
  5015. }
  5016. //---------------------------------------------------------------------------
  5017. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  5018. {
  5019. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  5020. StoredSessions->NewSession(SessionName, SessionData);
  5021. // modified only, explicit
  5022. StoredSessions->Save(false, true);
  5023. // encode session name because of slashes in hierarchical sessions
  5024. return EncodeUrlString(SessionName);
  5025. }
  5026. //---------------------------------------------------------------------------
  5027. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  5028. {
  5029. // current working directories become defaults here, what is not right
  5030. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5031. return SaveHiddenDuplicateSession(SessionData.get());
  5032. }
  5033. //---------------------------------------------------------------------------
  5034. void __fastcall TCustomScpExplorerForm::DuplicateTab()
  5035. {
  5036. if (OpenInNewWindow())
  5037. {
  5038. ExecuteNewInstance(CreateHiddenDuplicateSession());
  5039. }
  5040. else
  5041. {
  5042. // current working directories become defaults here, what is not right
  5043. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5044. TTerminalManager * Manager = TTerminalManager::Instance();
  5045. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  5046. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  5047. // We definitely want these
  5048. ASession->Disconnected = ManagedSession->Disconnected;
  5049. // Not sure about these two
  5050. ASession->Permanent = ManagedSession->Permanent;
  5051. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  5052. Manager->ActiveSession = ASession;
  5053. }
  5054. }
  5055. //---------------------------------------------------------------------------
  5056. void __fastcall TCustomScpExplorerForm::RenameTab()
  5057. {
  5058. UnicodeString Name = ManagedSession->SessionData->SessionName;
  5059. if (InputDialog(LoadStr(RENAME_TAB_TITLE), LoadStr(RENAME_TAB_PROMPT), Name, HELP_TAB_RENAME) &&
  5060. // When submitting the default name, do not fix it
  5061. (Name != ManagedSession->SessionData->SessionName))
  5062. {
  5063. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  5064. TSessionData::ValidateName(Name);
  5065. // This is inconsistent with how color (for example) is handled.
  5066. ManagedSession->SessionData->Name = Name;
  5067. UpdateControls();
  5068. // Add/Remove distinguishing paths from sessions of the same name.
  5069. SessionListChanged();
  5070. }
  5071. }
  5072. //---------------------------------------------------------------------------
  5073. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  5074. {
  5075. DebugAssert(Queue != NULL);
  5076. bool Result = Queue->IsEmpty;
  5077. if (!Result)
  5078. {
  5079. SetFocus();
  5080. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  5081. }
  5082. return Result;
  5083. }
  5084. //---------------------------------------------------------------------------
  5085. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  5086. {
  5087. return CanCloseQueue(FQueue);
  5088. }
  5089. //---------------------------------------------------------------------------
  5090. void __fastcall TCustomScpExplorerForm::CloseTab()
  5091. {
  5092. if (CanCloseQueue())
  5093. {
  5094. TTerminalManager::Instance()->FreeActiveTerminal();
  5095. }
  5096. }
  5097. //---------------------------------------------------------------------------
  5098. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  5099. {
  5100. if (CanCloseQueue())
  5101. {
  5102. TTerminalManager::Instance()->DisconnectActiveTerminal();
  5103. }
  5104. }
  5105. //---------------------------------------------------------------------------
  5106. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  5107. {
  5108. DetachTerminal(Terminal);
  5109. RemoteDirView->Terminal = Terminal;
  5110. UpdateRemotePathComboBox(false);
  5111. InitStatusBar();
  5112. UpdateControls();
  5113. }
  5114. //---------------------------------------------------------------------------
  5115. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  5116. {
  5117. InitStatusBar();
  5118. }
  5119. //---------------------------------------------------------------------------
  5120. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  5121. {
  5122. if (DebugAlwaysTrue(!Terminal->Active))
  5123. {
  5124. TTerminalManager::Instance()->ReconnectActiveTerminal();
  5125. }
  5126. }
  5127. //---------------------------------------------------------------------------
  5128. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  5129. {
  5130. if (OpenInNewWindow())
  5131. {
  5132. // encode session name because of slashes in hierarchical sessions
  5133. ExecuteNewInstance(EncodeUrlString(Data->Name));
  5134. }
  5135. else
  5136. {
  5137. TTerminalManager * Manager = TTerminalManager::Instance();
  5138. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  5139. Manager->ActiveSession = Terminal;
  5140. }
  5141. }
  5142. //---------------------------------------------------------------------------
  5143. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  5144. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  5145. {
  5146. TTerminalManager * Manager = TTerminalManager::Instance();
  5147. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5148. // This asks for a master password. Maybe we should postpone asking until a connection is actually opened.
  5149. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  5150. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  5151. {
  5152. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  5153. // FirstSession can be null, if none of the workspace sites exist anymore
  5154. // or if all workspace sessions are not suported by the interface
  5155. // (when trying to open all local-local workspace in "explorer" interface)
  5156. if (FirstSession != NULL)
  5157. {
  5158. if (!ConnectFirstTerminal)
  5159. {
  5160. FirstSession->Disconnected = true;
  5161. FirstSession->DisconnectedTemporarily = true;
  5162. }
  5163. Manager->ActiveSession = FirstSession;
  5164. }
  5165. }
  5166. }
  5167. //---------------------------------------------------------------------------
  5168. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  5169. {
  5170. if (OpenInNewWindow())
  5171. {
  5172. ExecuteNewInstance(Name);
  5173. }
  5174. else
  5175. {
  5176. DoOpenFolderOrWorkspace(Name, true, false);
  5177. }
  5178. }
  5179. //---------------------------------------------------------------------------
  5180. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  5181. bool &CanClose)
  5182. {
  5183. AppLog(L"Windows close query");
  5184. // See the comment in CloseApp()
  5185. if (NonVisualDataModule->Busy)
  5186. {
  5187. CanClose = false;
  5188. }
  5189. else
  5190. {
  5191. TTerminalManager * Manager = TTerminalManager::Instance();
  5192. int ActiveSessions = 0;
  5193. for (int Index = 0; Index < Manager->Count; Index++)
  5194. {
  5195. if (Manager->Sessions[Index]->Active)
  5196. {
  5197. ActiveSessions++;
  5198. }
  5199. }
  5200. // Confirm, when there's at least one active remote session or
  5201. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  5202. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  5203. WinConfiguration->ConfirmClosingSession)
  5204. {
  5205. unsigned int Result;
  5206. TMessageParams Params(mpNeverAskAgainCheck);
  5207. UnicodeString Message;
  5208. int Answers = qaOK | qaCancel;
  5209. // The current session is the only active session
  5210. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  5211. {
  5212. if (!WinConfiguration->AutoSaveWorkspace)
  5213. {
  5214. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  5215. }
  5216. else
  5217. {
  5218. Message = LoadStr(CLOSE_SESSION);
  5219. }
  5220. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  5221. }
  5222. // Multiple active sessions or one active session, but it's not the current one
  5223. else if (ActiveSessions > 0)
  5224. {
  5225. if (!WinConfiguration->AutoSaveWorkspace)
  5226. {
  5227. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  5228. }
  5229. else
  5230. {
  5231. Message = LoadStr(CLOSE_SESSIONS);
  5232. }
  5233. }
  5234. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  5235. else
  5236. {
  5237. DebugAlwaysTrue(Manager->Count > 1);
  5238. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  5239. Message = LoadStr(CLOSE_WORKSPACE);
  5240. }
  5241. UnicodeString Note;
  5242. if (WinConfiguration->AutoSaveWorkspace)
  5243. {
  5244. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  5245. }
  5246. else
  5247. {
  5248. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  5249. Answers = qaYes | qaNo | qaCancel;
  5250. }
  5251. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  5252. SetFocus();
  5253. Result = MessageDialog(Message, qtConfirmation,
  5254. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  5255. if (Result == qaNeverAskAgain)
  5256. {
  5257. WinConfiguration->ConfirmClosingSession = false;
  5258. }
  5259. if (Result == qaNo)
  5260. {
  5261. CanClose = SaveWorkspace(true);
  5262. // note that the workspace will be saved redundatly again from FormClose
  5263. }
  5264. else
  5265. {
  5266. CanClose =
  5267. (Result == qaOK) ||
  5268. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  5269. (Result == qaNeverAskAgain);
  5270. }
  5271. }
  5272. if (CanClose && HasActiveTerminal())
  5273. {
  5274. CanClose = CanCloseQueue();
  5275. }
  5276. }
  5277. if (CanClose)
  5278. {
  5279. CanClose =
  5280. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  5281. FEditorManager->CloseExternalFilesWithoutProcess();
  5282. if (CanClose)
  5283. {
  5284. while (CanClose && (FLocalEditors->Count > 0))
  5285. {
  5286. int PrevCount = FLocalEditors->Count;
  5287. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  5288. CanClose = (FLocalEditors->Count < PrevCount);
  5289. }
  5290. if (CanClose)
  5291. {
  5292. CanClose = FEditorManager->Empty(true);
  5293. if (!CanClose)
  5294. {
  5295. SetFocus();
  5296. CanClose =
  5297. (MessageDialog(
  5298. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  5299. }
  5300. }
  5301. }
  5302. }
  5303. }
  5304. //---------------------------------------------------------------------------
  5305. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  5306. {
  5307. TForm * Form = dynamic_cast<TForm *>(Sender);
  5308. DebugAssert(Form != NULL);
  5309. Form->Close();
  5310. }
  5311. //---------------------------------------------------------------------------
  5312. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  5313. {
  5314. TForm * Form = dynamic_cast<TForm *>(Sender);
  5315. delete Form;
  5316. }
  5317. //---------------------------------------------------------------------------
  5318. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  5319. {
  5320. while (FLocalEditors->Count > 0)
  5321. {
  5322. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  5323. }
  5324. }
  5325. //---------------------------------------------------------------------------
  5326. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  5327. TObject *Sender)
  5328. {
  5329. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  5330. try
  5331. {
  5332. SetProperties(osRemote, FileList);
  5333. }
  5334. __finally
  5335. {
  5336. delete FileList;
  5337. }
  5338. }
  5339. //---------------------------------------------------------------------------
  5340. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  5341. {
  5342. if (value)
  5343. {
  5344. if (Component == fcCustomCommandsBand)
  5345. {
  5346. UpdateCustomCommandsToolbar();
  5347. }
  5348. if (Component == fcQueueFileList)
  5349. {
  5350. DebugAssert(!QueueFileListSplitter->Visible);
  5351. AdjustQueueLayout();
  5352. UpdateQueueFileList();
  5353. }
  5354. }
  5355. }
  5356. //---------------------------------------------------------------------------
  5357. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  5358. {
  5359. int MinHeightLimit =
  5360. GetStaticQueuePanelComponentsHeight() +
  5361. QueueFileListSplitter->Height +
  5362. QueueFileList->Height +
  5363. GetMinQueueViewHeight();
  5364. if (QueuePanel->ClientHeight < MinHeightLimit)
  5365. {
  5366. QueuePanel->ClientHeight = MinHeightLimit;
  5367. }
  5368. }
  5369. //---------------------------------------------------------------------------
  5370. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  5371. {
  5372. TControl * Control = GetComponent(Component);
  5373. DebugAssert(Control);
  5374. bool Changed = (Control->Visible != value);
  5375. if (Changed)
  5376. {
  5377. ComponentShowing(Component, value);
  5378. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  5379. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  5380. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  5381. if (value)
  5382. {
  5383. int RemainingHeight = Control->Parent->ClientHeight;
  5384. int RemainingWidth = Control->Parent->ClientWidth;
  5385. for (int i = 0; i < Control->Parent->ControlCount; i++)
  5386. {
  5387. TControl * ChildControl = Control->Parent->Controls[i];
  5388. if (ChildControl->Visible)
  5389. {
  5390. switch (ChildControl->Align)
  5391. {
  5392. case alTop:
  5393. case alBottom:
  5394. RemainingHeight -= ChildControl->Height;
  5395. break;
  5396. case alLeft:
  5397. case alRight:
  5398. RemainingWidth -= ChildControl->Width;
  5399. break;
  5400. }
  5401. }
  5402. }
  5403. int Reserve = ScaleByTextHeight(this, 32);
  5404. // queue in explorer, trees in commander
  5405. if (Control->Height > RemainingHeight - Reserve)
  5406. {
  5407. Control->Height = RemainingHeight / 2;
  5408. }
  5409. if (Control->Width > RemainingWidth - Reserve)
  5410. {
  5411. Control->Width = RemainingWidth / 2;
  5412. }
  5413. }
  5414. Control->Visible = value;
  5415. if (WasFocused && Visible)
  5416. {
  5417. DirView(osCurrent)->SetFocus();
  5418. }
  5419. FixControlsPlacement();
  5420. }
  5421. }
  5422. //---------------------------------------------------------------------------
  5423. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  5424. {
  5425. TControl * Control = GetComponent(Component);
  5426. if (Control == NULL)
  5427. {
  5428. return false;
  5429. }
  5430. else
  5431. {
  5432. return Control->Visible;
  5433. }
  5434. }
  5435. //---------------------------------------------------------------------------
  5436. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  5437. {
  5438. bool Result;
  5439. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  5440. {
  5441. Result = !IsUWP();
  5442. }
  5443. else
  5444. {
  5445. Result = true;
  5446. }
  5447. return Result;
  5448. }
  5449. //---------------------------------------------------------------------------
  5450. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  5451. {
  5452. if (DirView->ItemFocused != NULL)
  5453. {
  5454. DirView->ItemFocused->MakeVisible(false);
  5455. }
  5456. }
  5457. //---------------------------------------------------------------------------
  5458. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5459. {
  5460. MakeFocusedItemVisible(RemoteDirView);
  5461. QueueSplitter->Visible = QueuePanel->Visible;
  5462. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5463. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5464. TControl * TopControlsOrder[] =
  5465. { TopDock, MessageDock, SessionsPageControl };
  5466. SetVerticalControlsOrder(TopControlsOrder, LENOF(TopControlsOrder));
  5467. TControl * QueueControlsOrder[] =
  5468. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5469. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5470. }
  5471. //---------------------------------------------------------------------------
  5472. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5473. {
  5474. switch (Component) {
  5475. case fcStatusBar: return RemoteStatusBar;
  5476. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5477. case fcQueueView: return QueuePanel;
  5478. case fcQueueToolbar: return QueueDock;
  5479. case fcRemoteTree: return RemoteDrivePanel;
  5480. case fcSessionsTabs: return SessionsPageControl;
  5481. case fcQueueFileList: return QueueFileList;
  5482. default: return NULL;
  5483. }
  5484. }
  5485. //---------------------------------------------------------------------------
  5486. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5487. TObject *Sender, TListColumn *Column, TPoint &Point)
  5488. {
  5489. DebugAssert(NonVisualDataModule && Column && Sender);
  5490. NonVisualDataModule->ListColumn = Column;
  5491. TPopupMenu * DirViewColumnMenu;
  5492. if (Sender == DirView(osOther))
  5493. {
  5494. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5495. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible = (Column->Index == uvName);
  5496. bool SizeColumn = (Column->Index == uvSize);
  5497. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible = SizeColumn;
  5498. NonVisualDataModule->RemoteCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5499. }
  5500. else
  5501. {
  5502. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5503. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible = (Column->Index == dvName);
  5504. bool SizeColumn = (Column->Index == dvSize);
  5505. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible = SizeColumn;
  5506. NonVisualDataModule->LocalCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5507. }
  5508. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5509. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5510. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5511. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5512. }
  5513. //---------------------------------------------------------------------------
  5514. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5515. TObject *Sender, TListItem *Item, bool &AllowExec)
  5516. {
  5517. DoDirViewExecFile(Sender, Item, AllowExec);
  5518. }
  5519. //---------------------------------------------------------------------------
  5520. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5521. TListItem * Item, bool & AllowExec)
  5522. {
  5523. DebugAssert(Sender && Item && Configuration);
  5524. DebugAssert(AllowExec);
  5525. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5526. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5527. bool Remote = !IsSideLocalBrowser(Side);
  5528. DebugAssert(Item == ADirView->ItemFocused);
  5529. bool IsDirectory = ADirView->ItemIsDirectory(Item);
  5530. TResolvedDoubleClickAction Action;
  5531. if (FForceExecution)
  5532. {
  5533. Action = IsDirectory ? rdcaChangeDir : rdcaOpen;
  5534. }
  5535. else
  5536. {
  5537. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  5538. Action = WinConfiguration->ResolveDoubleClickAction(IsDirectory, ATerminal);
  5539. }
  5540. if (Action == rdcaCopy)
  5541. {
  5542. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5543. UnlockWindow();
  5544. try
  5545. {
  5546. if (IsLocalBrowserMode())
  5547. {
  5548. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5549. }
  5550. else
  5551. {
  5552. if (HasActiveTerminal())
  5553. {
  5554. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5555. }
  5556. }
  5557. }
  5558. __finally
  5559. {
  5560. LockWindow();
  5561. }
  5562. AllowExec = false;
  5563. }
  5564. else if (Action == rdcaEdit)
  5565. {
  5566. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5567. {
  5568. ExecuteFile(Side, efDefaultEditor);
  5569. AllowExec = false;
  5570. }
  5571. }
  5572. else if (Action == rdcaOpen)
  5573. {
  5574. if (DebugAlwaysTrue(!IsDirectory) && Remote && !WinConfiguration->DisableOpenEdit)
  5575. {
  5576. ExecuteFile(osRemote, efShell);
  5577. AllowExec = false;
  5578. }
  5579. }
  5580. }
  5581. //---------------------------------------------------------------------------
  5582. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5583. {
  5584. return ((Side == osRemote) || (Side == osCurrent));
  5585. }
  5586. //---------------------------------------------------------------------------
  5587. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5588. {
  5589. DebugFail();
  5590. }
  5591. //---------------------------------------------------------------------------
  5592. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5593. {
  5594. DebugFail();
  5595. }
  5596. //---------------------------------------------------------------------------
  5597. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5598. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5599. {
  5600. TSynchronizeParamType Params;
  5601. Params.LocalDirectory = LocalDirectory;
  5602. Params.RemoteDirectory = RemoteDirectory;
  5603. int UnusedParams =
  5604. (GUIConfiguration->SynchronizeParams &
  5605. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5606. TTerminal::spCaseSensitive | TTerminal::spMirror | TTerminal::spByChecksum));
  5607. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5608. Params.Options = GUIConfiguration->SynchronizeOptions;
  5609. bool SaveSettings = false;
  5610. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5611. &DoSynchronizeTooManyDirectories);
  5612. DebugAssert(FSynchronizeController == NULL);
  5613. FSynchronizeController = &Controller;
  5614. bool Result;
  5615. try
  5616. {
  5617. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5618. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5619. int Options =
  5620. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5621. DebugAssert(FOnFeedSynchronizeError == NULL);
  5622. DebugAssert(FOnSynchronizeAbort == NULL);
  5623. DebugAssert(FSynchronizeTerminal == NULL);
  5624. FSynchronizeTerminal = Terminal;
  5625. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5626. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5627. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5628. if (Result)
  5629. {
  5630. if (SaveSettings)
  5631. {
  5632. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5633. GUIConfiguration->SynchronizeOptions = Params.Options;
  5634. }
  5635. else
  5636. {
  5637. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5638. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5639. FLAGSET(Params.Options, soSynchronize))
  5640. {
  5641. GUIConfiguration->SynchronizeOptions =
  5642. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5643. soSynchronize;
  5644. }
  5645. }
  5646. LocalDirectory = Params.LocalDirectory;
  5647. RemoteDirectory = Params.RemoteDirectory;
  5648. }
  5649. }
  5650. __finally
  5651. {
  5652. FSynchronizeController = NULL;
  5653. DebugAssert(FOnFeedSynchronizeError == NULL);
  5654. FOnFeedSynchronizeError = NULL;
  5655. DebugAssert(FOnSynchronizeAbort == NULL);
  5656. FOnSynchronizeAbort = NULL;
  5657. FSynchronizeTerminal = NULL;
  5658. }
  5659. return Result;
  5660. }
  5661. //---------------------------------------------------------------------------
  5662. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5663. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5664. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5665. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5666. TSynchronizeOptions * Options, bool Full)
  5667. {
  5668. // Keep-up-to-date mode
  5669. if (Terminal->Status == ssOpened)
  5670. {
  5671. try
  5672. {
  5673. int PParams = Params.Params;
  5674. if (!Full)
  5675. {
  5676. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5677. if (FLAGSET(Params.Options, soRecurse))
  5678. {
  5679. PParams |= TTerminal::spSubDirs;
  5680. }
  5681. }
  5682. else
  5683. {
  5684. // if keepuptodate is non-recursive,
  5685. // full sync before has to be non-recursive as well
  5686. if (FLAGCLEAR(Params.Options, soRecurse))
  5687. {
  5688. PParams |= TTerminal::spNoRecurse;
  5689. }
  5690. }
  5691. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5692. PParams, Checklist, Options);
  5693. }
  5694. catch (Exception & E)
  5695. {
  5696. if (!Terminal->Active)
  5697. {
  5698. ShowExtendedExceptionEx(Terminal, &E);
  5699. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5700. if (!Terminal->Active)
  5701. {
  5702. throw;
  5703. }
  5704. }
  5705. else
  5706. {
  5707. if (FLAGSET(Params.Options, soContinueOnError) &&
  5708. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5709. {
  5710. // noop, already logged in MoreMessageDialog
  5711. }
  5712. else
  5713. {
  5714. // We get mostly EAbort here, so this is noop.
  5715. // Exception is an error when listing directories while looking for differences.
  5716. ShowExtendedExceptionEx(Terminal, &E);
  5717. throw;
  5718. }
  5719. }
  5720. }
  5721. }
  5722. }
  5723. //---------------------------------------------------------------------------
  5724. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5725. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5726. const UnicodeString ErrorStr)
  5727. {
  5728. if (!Directory.IsEmpty())
  5729. {
  5730. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5731. }
  5732. else
  5733. {
  5734. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5735. }
  5736. }
  5737. //---------------------------------------------------------------------------
  5738. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5739. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5740. {
  5741. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5742. {
  5743. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5744. }
  5745. else
  5746. {
  5747. TMessageParams Params(mpNeverAskAgainCheck);
  5748. unsigned int Result = MessageDialog(
  5749. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5750. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5751. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5752. {
  5753. MaxDirectories *= 2;
  5754. if (Result == qaNeverAskAgain)
  5755. {
  5756. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5757. }
  5758. }
  5759. else
  5760. {
  5761. Abort();
  5762. }
  5763. }
  5764. }
  5765. //---------------------------------------------------------------------------
  5766. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5767. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5768. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5769. TSynchronizeOptions * Options)
  5770. {
  5771. // Keep-up-to-date mode
  5772. DebugAssert(!FAutoOperation);
  5773. void * BatchStorage;
  5774. BatchStart(BatchStorage);
  5775. FAutoOperation = true;
  5776. bool AnyOperation = false;
  5777. TDateTime StartTime = Now();
  5778. TSynchronizeChecklist * AChecklist = NULL;
  5779. try
  5780. {
  5781. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5782. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5783. {
  5784. FSynchronizeProgressForm->Start();
  5785. }
  5786. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5787. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5788. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5789. Options);
  5790. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5791. SAFE_DESTROY(FSynchronizeProgressForm);
  5792. AnyOperation = (AChecklist->CheckedCount > 0);
  5793. if (AnyOperation)
  5794. {
  5795. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5796. CreateProgressForm(&SynchronizeProgress);
  5797. try
  5798. {
  5799. Terminal->SynchronizeApply(
  5800. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5801. SynchronizeProcessedItem, NULL, NULL, NULL);
  5802. }
  5803. __finally
  5804. {
  5805. DestroyProgressForm();
  5806. }
  5807. }
  5808. }
  5809. __finally
  5810. {
  5811. if (Checklist == NULL)
  5812. {
  5813. delete AChecklist;
  5814. }
  5815. else
  5816. {
  5817. *Checklist = AChecklist;
  5818. }
  5819. FAutoOperation = false;
  5820. SAFE_DESTROY(FSynchronizeProgressForm);
  5821. BatchEnd(BatchStorage);
  5822. ReloadLocalDirectory();
  5823. if (AnyOperation)
  5824. {
  5825. OperationComplete(StartTime);
  5826. }
  5827. }
  5828. }
  5829. //---------------------------------------------------------------------------
  5830. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5831. {
  5832. // can be called from command line
  5833. return Visible &&
  5834. ((DirView(osRemote)->SelCount > 0) ||
  5835. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5836. }
  5837. //---------------------------------------------------------------------------
  5838. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5839. {
  5840. LogSynchronizeEvent(Terminal, Message);
  5841. }
  5842. //---------------------------------------------------------------------------
  5843. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5844. int Params, TSynchronizeOptions & Options)
  5845. {
  5846. DebugAssert(!IsLocalBrowserMode());
  5847. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5848. {
  5849. Options.Filter = new TStringList();
  5850. Options.Filter->CaseSensitive = false;
  5851. Options.Filter->Duplicates = Types::dupAccept;
  5852. if (DirView(osRemote)->SelCount > 0)
  5853. {
  5854. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5855. }
  5856. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5857. {
  5858. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5859. }
  5860. Options.Filter->Sort();
  5861. }
  5862. }
  5863. //---------------------------------------------------------------------------
  5864. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5865. {
  5866. TCopyParamType Defaults;
  5867. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5868. ConfigStorage->AccessMode = smRead;
  5869. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5870. {
  5871. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5872. }
  5873. ConfigStorage.reset(NULL);
  5874. std::unique_ptr<TStringList> Options(new TStringList());
  5875. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5876. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5877. UnicodeString Result;
  5878. if (Options->Count > 0)
  5879. {
  5880. Result =
  5881. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5882. }
  5883. return Result;
  5884. }
  5885. //---------------------------------------------------------------------------
  5886. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5887. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5888. {
  5889. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5890. UnicodeString AdditionalParams =
  5891. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5892. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5893. SerializeCopyParamForCommandLine(CopyParams),
  5894. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5895. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5896. ExecuteNewInstance(SessionName, AdditionalParams);
  5897. }
  5898. //---------------------------------------------------------------------------
  5899. struct TSynchronizeParams
  5900. {
  5901. UnicodeString LocalDirectory;
  5902. UnicodeString RemoteDirectory;
  5903. TSynchronizeMode Mode;
  5904. TSynchronizeChecklist * Checklist;
  5905. int Params;
  5906. TCopyParamType * CopyParam;
  5907. TDateTime CollectElapsed;
  5908. TDateTime * StartTime;
  5909. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5910. };
  5911. //---------------------------------------------------------------------------
  5912. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5913. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5914. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5915. {
  5916. DebugAssert(!FAutoOperation);
  5917. void * BatchStorage;
  5918. BatchStart(BatchStorage);
  5919. FAutoOperation = true;
  5920. TFileOperationStatistics Statistics;
  5921. TDateTime Start = Now();
  5922. try
  5923. {
  5924. Params.OnProcessedItem = OnProcessedItem;
  5925. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5926. CreateProgressForm(&SynchronizeProgress);
  5927. Terminal->SynchronizeApply(
  5928. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5929. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5930. &Statistics);
  5931. }
  5932. __finally
  5933. {
  5934. Params.OnProcessedItem = NULL;
  5935. FAutoOperation = false;
  5936. DestroyProgressForm();
  5937. BatchEnd(BatchStorage);
  5938. ReloadLocalDirectory();
  5939. }
  5940. if (WinConfiguration->SynchronizeSummary)
  5941. {
  5942. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5943. // The statistics should be 0 anyway in this case
  5944. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5945. {
  5946. Message += L"\n";
  5947. if (Statistics.FilesUploaded > 0)
  5948. {
  5949. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5950. }
  5951. if (Statistics.FilesDownloaded > 0)
  5952. {
  5953. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5954. }
  5955. if (Statistics.FilesDeletedLocal > 0)
  5956. {
  5957. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5958. }
  5959. if (Statistics.FilesDeletedRemote > 0)
  5960. {
  5961. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5962. }
  5963. }
  5964. TDateTime Elapsed = (Now() - Start);
  5965. Message +=
  5966. L"\n" +
  5967. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Params.CollectElapsed))) + L"\n" +
  5968. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Elapsed)));
  5969. TMessageParams Params(mpNeverAskAgainCheck);
  5970. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5971. if (Result == qaNeverAskAgain)
  5972. {
  5973. WinConfiguration->SynchronizeSummary = false;
  5974. }
  5975. }
  5976. }
  5977. //---------------------------------------------------------------------------
  5978. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5979. {
  5980. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5981. {
  5982. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5983. }
  5984. // Not set in keep-up-to-date mode - Synchronize() method
  5985. if (Token != NULL)
  5986. {
  5987. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5988. if (Params.OnProcessedItem != NULL)
  5989. {
  5990. Params.OnProcessedItem(NULL, ChecklistItem);
  5991. }
  5992. }
  5993. }
  5994. //---------------------------------------------------------------------------
  5995. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5996. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5997. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5998. {
  5999. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  6000. *Params.StartTime = Now();
  6001. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  6002. }
  6003. //---------------------------------------------------------------------------
  6004. void TCustomScpExplorerForm::DoQueueSynchronize(void * Token)
  6005. {
  6006. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  6007. int CopyParams = TTerminal::GetSynchronizeCopyParams(Params.Params);
  6008. TCopyParamType SyncCopyParam = TTerminal::GetSynchronizeCopyParam(Params.CopyParam, Params.Params);
  6009. bool Parallel = GUIConfiguration->CurrentCopyParam.QueueParallel;
  6010. int Index = 0;
  6011. const TSynchronizeChecklist::TItem * ChecklistItem;
  6012. while (Params.Checklist->GetNextChecked(Index, ChecklistItem))
  6013. {
  6014. TQueueItem * QueueItem = Terminal->SynchronizeToQueue(ChecklistItem, &SyncCopyParam, CopyParams, Parallel);
  6015. AddQueueItem(Queue, QueueItem, Terminal);
  6016. }
  6017. }
  6018. //---------------------------------------------------------------------------
  6019. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  6020. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  6021. {
  6022. // terminal can be already closed (e.g. dropped connection)
  6023. if (Terminal != NULL)
  6024. {
  6025. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  6026. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  6027. }
  6028. }
  6029. //---------------------------------------------------------------------------
  6030. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  6031. TOperationSide Side, TStrings * FileList, const UnicodeString & NewFileName, bool TargetIsDirectory, void * Token)
  6032. {
  6033. DebugAssert(!IsLocalBrowserMode());
  6034. TAutoBatch AutoBatch(this);
  6035. TAutoFlag AutoOperationFlag(FAutoOperation);
  6036. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  6037. if (Side == osRemote)
  6038. {
  6039. UnicodeString Target;
  6040. UnicodeString FileMask;
  6041. if (TargetIsDirectory)
  6042. {
  6043. Target = NewFileName;
  6044. Terminal->CreateTargetDirectory(Target, faDirectory, Params.CopyParam);
  6045. }
  6046. else
  6047. {
  6048. Target = UnixExtractFileDir(NewFileName);
  6049. FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  6050. }
  6051. RemoteDirView->SaveSelection();
  6052. RemoteDirView->SaveSelectedNames();
  6053. try
  6054. {
  6055. Terminal->MoveFiles(FileList, Target, FileMask, false);
  6056. }
  6057. catch(...)
  6058. {
  6059. RemoteDirView->DiscardSavedSelection();
  6060. throw;
  6061. }
  6062. RemoteDirView->RestoreSelection();
  6063. }
  6064. else if (DebugAlwaysTrue(Side == osLocal))
  6065. {
  6066. std::unique_ptr<TStrings> Directories(CreateSortedStringList());
  6067. if (TargetIsDirectory)
  6068. {
  6069. if (!ForceDirectories(ApiPath(NewFileName)))
  6070. {
  6071. throw EOSExtException(FMTLOAD(CREATE_DIR_ERROR, (NewFileName)));
  6072. }
  6073. Directories->Add(NewFileName);
  6074. }
  6075. else
  6076. {
  6077. Directories->Add(ExtractFileDir(NewFileName));
  6078. }
  6079. for (int Index = 0; Index < FileList->Count; Index++)
  6080. {
  6081. UnicodeString FileName = FileList->Strings[Index];
  6082. UnicodeString Directory = ExtractFileDir(FileName);
  6083. Directories->Add(Directory);
  6084. UnicodeString NewFilePath = NewFileName;
  6085. if (TargetIsDirectory)
  6086. {
  6087. NewFilePath = TPath::Combine(NewFilePath, ExtractFileName(FileName));
  6088. }
  6089. if (!MoveFile(ApiPath(FileName).c_str(), ApiPath(NewFilePath).c_str()))
  6090. {
  6091. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFilePath)));
  6092. }
  6093. }
  6094. for (int Index = 0; Index < Directories->Count; Index++)
  6095. {
  6096. ReloadLocalDirectory(Directories->Strings[Index]);
  6097. }
  6098. }
  6099. }
  6100. //---------------------------------------------------------------------------
  6101. void __fastcall TCustomScpExplorerForm::DoSynchronizeExplore(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  6102. {
  6103. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  6104. if (Side == osLocal)
  6105. {
  6106. if (Action == TSynchronizeChecklist::saDownloadNew)
  6107. {
  6108. OpenFolderInExplorer(LocalPath);
  6109. }
  6110. else
  6111. {
  6112. OpenFileInExplorer(Item->ForceGetLocalPath());
  6113. }
  6114. }
  6115. else if (DebugAlwaysTrue(Side == osRemote))
  6116. {
  6117. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  6118. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  6119. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  6120. if (!LocalPath.IsEmpty())
  6121. {
  6122. SessionData->LocalDirectory = LocalPath;
  6123. }
  6124. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  6125. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(EXPLORE_SWITCH), Item->GetFileName())));
  6126. }
  6127. }
  6128. //---------------------------------------------------------------------------
  6129. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  6130. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  6131. const TCopyParamType * CopyParams)
  6132. {
  6133. UnicodeString SessionName = CreateHiddenDuplicateSession();
  6134. UnicodeString AdditionalParams =
  6135. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  6136. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  6137. SerializeCopyParamForCommandLine(CopyParams),
  6138. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  6139. LocalDirectory, RemoteDirectory, Mode, Params));
  6140. ExecuteNewInstance(SessionName, AdditionalParams);
  6141. }
  6142. //---------------------------------------------------------------------------
  6143. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  6144. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  6145. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  6146. {
  6147. int Result;
  6148. bool SaveSettings = false;
  6149. int Options =
  6150. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  6151. FLAGMASK(!CanCalculateChecksum(), fsoDisableByChecksum) |
  6152. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  6153. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  6154. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  6155. bool Continue =
  6156. ((UseDefaults == 0) ||
  6157. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  6158. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults)) &&
  6159. (FLAGCLEAR(Params, TTerminal::spByChecksum) || EnsureCommandSessionFallback(fcCalculatingChecksum));
  6160. if (Continue)
  6161. {
  6162. Configuration->Usage->Inc(L"Synchronizations");
  6163. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  6164. UpdateCopyParamCounters(CopyParam);
  6165. TSynchronizeOptions SynchronizeOptions;
  6166. GetSynchronizeOptions(Params, SynchronizeOptions);
  6167. if (SaveSettings)
  6168. {
  6169. GUIConfiguration->SynchronizeParams = Params;
  6170. }
  6171. else
  6172. {
  6173. SaveMode = false;
  6174. }
  6175. TDateTime StartTime = Now();
  6176. TSynchronizeChecklist * Checklist = NULL;
  6177. try
  6178. {
  6179. DebugAssert(!FAutoOperation);
  6180. FAutoOperation = true;
  6181. try
  6182. {
  6183. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  6184. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  6185. int Files = -1;
  6186. if (DataList->Count >= 1)
  6187. {
  6188. Files = StrToIntDef(DataList->Strings[0], Files);
  6189. }
  6190. else
  6191. {
  6192. DataList->Add(UnicodeString());
  6193. }
  6194. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  6195. FSynchronizeProgressForm->Start();
  6196. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  6197. static_cast<TTerminal::TSynchronizeMode>(Mode),
  6198. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  6199. &SynchronizeOptions);
  6200. if (Terminal->SessionData->CacheDirectories)
  6201. {
  6202. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  6203. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  6204. }
  6205. }
  6206. __finally
  6207. {
  6208. FAutoOperation = false;
  6209. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  6210. SAFE_DESTROY(FSynchronizeProgressForm);
  6211. }
  6212. TSynchronizeParams SynchronizeParams;
  6213. SynchronizeParams.LocalDirectory = LocalDirectory;
  6214. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  6215. SynchronizeParams.Mode = Mode;
  6216. SynchronizeParams.CopyParam = &CopyParam;
  6217. SynchronizeParams.Params = Params;
  6218. SynchronizeParams.Checklist = Checklist;
  6219. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  6220. SynchronizeParams.StartTime = &StartTime;
  6221. SynchronizeParams.OnProcessedItem = NULL;
  6222. Result = Checklist->Count;
  6223. if (Checklist->Count > 0)
  6224. {
  6225. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  6226. {
  6227. TQueueSynchronizeEvent OnQueueSynchronize = NULL;
  6228. if (Visible && FLAGCLEAR(Params, TTerminal::spTimestamp))
  6229. {
  6230. OnQueueSynchronize = DoQueueSynchronize;
  6231. }
  6232. if (!DoSynchronizeChecklistDialog(
  6233. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  6234. OnQueueSynchronize, DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeExplore,
  6235. &SynchronizeParams))
  6236. {
  6237. Result = -1;
  6238. }
  6239. }
  6240. else
  6241. {
  6242. FullSynchronize(SynchronizeParams, NULL, NULL);
  6243. }
  6244. }
  6245. else
  6246. {
  6247. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  6248. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  6249. }
  6250. }
  6251. __finally
  6252. {
  6253. delete Checklist;
  6254. }
  6255. OperationComplete(StartTime);
  6256. }
  6257. else
  6258. {
  6259. Result = -1;
  6260. }
  6261. return Result;
  6262. }
  6263. //---------------------------------------------------------------------------
  6264. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  6265. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  6266. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  6267. {
  6268. if (Collect)
  6269. {
  6270. DebugAssert(FSynchronizeProgressForm != NULL);
  6271. if (!FSynchronizeProgressForm->Started)
  6272. {
  6273. FSynchronizeProgressForm->Start();
  6274. }
  6275. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  6276. SetTaskbarListProgressValue(CompareProgress);
  6277. }
  6278. else
  6279. {
  6280. DebugAssert(FSynchronizeProgressForm == NULL);
  6281. DebugAssert(FProgressForm != NULL);
  6282. }
  6283. }
  6284. //---------------------------------------------------------------------------
  6285. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  6286. {
  6287. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  6288. std::unique_ptr<TRemoteFile> File(Terminal->ReadFile(FullFileName));
  6289. if (File.get() != NULL)
  6290. {
  6291. ExecuteRemoteFile(FullFileName, File.get(), efInternalEditor);
  6292. Application->ShowMainForm = false;
  6293. Application->Run();
  6294. }
  6295. }
  6296. //---------------------------------------------------------------------------
  6297. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  6298. {
  6299. DebugFail();
  6300. }
  6301. //---------------------------------------------------------------------------
  6302. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  6303. {
  6304. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  6305. SessionData->Assign(ManagedSession->SessionData);
  6306. UpdateSessionData(SessionData.get());
  6307. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  6308. if (IsActiveTerminal(ManagedSession))
  6309. {
  6310. ManagedSession->UpdateSessionCredentials(SessionData.get());
  6311. }
  6312. if (!ManagedSession->SessionData->HasSessionName())
  6313. {
  6314. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  6315. // once IsWorkspace is cleared
  6316. SessionData->Name = UnicodeString();
  6317. }
  6318. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  6319. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  6320. SessionData->IsWorkspace = false;
  6321. return SessionData.release();
  6322. }
  6323. //---------------------------------------------------------------------------
  6324. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  6325. {
  6326. TSessionData * SessionData = CloneCurrentSessionData();
  6327. try
  6328. {
  6329. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  6330. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  6331. }
  6332. __finally
  6333. {
  6334. delete SessionData;
  6335. }
  6336. }
  6337. //---------------------------------------------------------------------------
  6338. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  6339. {
  6340. TTerminalManager * Manager = TTerminalManager::Instance();
  6341. std::unique_ptr<TObjectList> DataList(new TObjectList());
  6342. if (DebugAlwaysTrue(ManagedSession != NULL))
  6343. {
  6344. // Update ManagedSession->StateData
  6345. UpdateSession(ManagedSession);
  6346. }
  6347. Manager->SaveWorkspace(DataList.get());
  6348. return DataList.release();
  6349. }
  6350. //---------------------------------------------------------------------------
  6351. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  6352. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  6353. {
  6354. WinConfiguration->LastWorkspace = Name;
  6355. if (!SavePasswords)
  6356. {
  6357. for (int Index = 0; Index < DataList->Count; Index++)
  6358. {
  6359. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6360. if (SessionData->Link.IsEmpty())
  6361. {
  6362. SessionData->ClearSessionPasswords();
  6363. }
  6364. }
  6365. }
  6366. StoredSessions->NewWorkspace(Name, DataList);
  6367. // modified only
  6368. StoredSessions->Save(false, Explicit);
  6369. }
  6370. //---------------------------------------------------------------------------
  6371. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  6372. {
  6373. return
  6374. DefaultStr(WinConfiguration->LastWorkspace,
  6375. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  6376. }
  6377. //---------------------------------------------------------------------------
  6378. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  6379. {
  6380. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  6381. bool AnyNonStoredSessionWithPassword = false;
  6382. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  6383. bool AllNonStoredSessionsAnonymous = true;
  6384. TTerminalManager * Manager = TTerminalManager::Instance();
  6385. for (int Index = 0; Index < DataList->Count; Index++)
  6386. {
  6387. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6388. if (SessionData->Link.IsEmpty())
  6389. {
  6390. if (SessionData->HasAnySessionPassword())
  6391. {
  6392. AnyNonStoredSessionWithPassword = true;
  6393. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  6394. {
  6395. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  6396. }
  6397. }
  6398. if (!SameText(SessionData->UserName, AnonymousUserName))
  6399. {
  6400. AllNonStoredSessionsAnonymous = false;
  6401. }
  6402. }
  6403. }
  6404. bool SavePasswords;
  6405. bool * PSavePasswords;
  6406. bool NotRecommendedSavingPasswords =
  6407. !CustomWinConfiguration->UseMasterPassword &&
  6408. !AllNonStoredSessionsAnonymous;
  6409. if (Configuration->DisablePasswordStoring ||
  6410. !AnyNonStoredSessionWithPassword)
  6411. {
  6412. PSavePasswords = NULL;
  6413. SavePasswords = false;
  6414. }
  6415. else
  6416. {
  6417. PSavePasswords = &SavePasswords;
  6418. SavePasswords =
  6419. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  6420. !NotRecommendedSavingPasswords;
  6421. }
  6422. UnicodeString Name = WorkspaceName();
  6423. bool CreateShortcut = false;
  6424. bool Result =
  6425. DoSaveWorkspaceDialog(
  6426. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  6427. EnableAutoSave);
  6428. if (Result)
  6429. {
  6430. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  6431. if (CreateShortcut)
  6432. {
  6433. TOperationVisualizer Visualizer;
  6434. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  6435. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  6436. }
  6437. if (EnableAutoSave)
  6438. {
  6439. WinConfiguration->AutoSaveWorkspace = true;
  6440. WinConfiguration->AutoWorkspace = Name;
  6441. if (PSavePasswords != NULL)
  6442. {
  6443. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  6444. }
  6445. }
  6446. }
  6447. return Result;
  6448. }
  6449. //---------------------------------------------------------------------------
  6450. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  6451. {
  6452. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  6453. DebugAssert(Session == ManagedSession);
  6454. if (!IsLocalBrowserMode())
  6455. {
  6456. SAFE_DESTROY(Session->RemoteExplorerState);
  6457. if (WinConfiguration->PreservePanelState)
  6458. {
  6459. Session->RemoteExplorerState = RemoteDirView->SaveState();
  6460. }
  6461. }
  6462. UpdateSessionData(Session->StateData);
  6463. }
  6464. //---------------------------------------------------------------------------
  6465. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  6466. {
  6467. // Keep in sync with TSessionData::CopyStateData
  6468. DebugAssert(Data != NULL);
  6469. // This is inconsistent with how color (for example) is handled.
  6470. Data->Name = ManagedSession->SessionData->Name;
  6471. if (!IsLocalBrowserMode())
  6472. {
  6473. // cannot use RemoteDirView->Path, because it is empty if connection
  6474. // was already closed
  6475. // also only peek, we may not be connected at all atm
  6476. // so make sure we do not try retrieving current directory from the server
  6477. // (particularly with FTP)
  6478. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  6479. if (!ACurrentDirectory.IsEmpty())
  6480. {
  6481. Data->RemoteDirectory = ACurrentDirectory;
  6482. }
  6483. }
  6484. Data->Color = SessionColor;
  6485. }
  6486. //---------------------------------------------------------------------------
  6487. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  6488. {
  6489. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  6490. DebugAssert(Toolbar != NULL);
  6491. for (int i = 0; i < Toolbar->Items->Count; i++)
  6492. {
  6493. TTBXCustomDropDownItem * DropDownItem;
  6494. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6495. if (DropDownItem != NULL)
  6496. {
  6497. ToolbarItemResize(DropDownItem,
  6498. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  6499. Toolbar->NonClientWidth);
  6500. break;
  6501. }
  6502. }
  6503. }
  6504. //---------------------------------------------------------------------------
  6505. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6506. {
  6507. Item->EditWidth = Width;
  6508. }
  6509. //---------------------------------------------------------------------------
  6510. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6511. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6512. {
  6513. for (int i = 0; i < Toolbar->Items->Count; i++)
  6514. {
  6515. TTBXCustomDropDownItem * DropDownItem;
  6516. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6517. if (DropDownItem != NULL)
  6518. {
  6519. ASize.x -= DropDownItem->EditWidth;
  6520. TControl * Control = this;
  6521. ASize.x += ScaleByTextHeight(Control, 50) /* minimal combo width */;
  6522. }
  6523. }
  6524. }
  6525. //---------------------------------------------------------------------------
  6526. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6527. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6528. {
  6529. if (WinConfiguration->DDWarnLackOfTempSpace)
  6530. {
  6531. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6532. if (!ADrive.IsEmpty())
  6533. {
  6534. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6535. DebugAssert(RequiredSpace >= 0);
  6536. __int64 RequiredWithReserve;
  6537. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6538. if (FreeSpace < RequiredWithReserve)
  6539. {
  6540. unsigned int Result;
  6541. TMessageParams Params(mpNeverAskAgainCheck);
  6542. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6543. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6544. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6545. if (Result == qaNeverAskAgain)
  6546. {
  6547. WinConfiguration->DDWarnLackOfTempSpace = false;
  6548. }
  6549. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6550. }
  6551. }
  6552. }
  6553. }
  6554. //---------------------------------------------------------------------------
  6555. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6556. {
  6557. DoOpenDirectoryDialog(odAddBookmark, Side);
  6558. }
  6559. //---------------------------------------------------------------------------
  6560. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6561. {
  6562. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6563. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6564. try
  6565. {
  6566. TCustomDirView * DView = DirView(Side);
  6567. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6568. {
  6569. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6570. }
  6571. }
  6572. catch (...)
  6573. {
  6574. delete VisitedDirectories;
  6575. throw;
  6576. }
  6577. return VisitedDirectories;
  6578. }
  6579. //---------------------------------------------------------------------------
  6580. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6581. TOpenDirectoryMode Mode, TOperationSide Side)
  6582. {
  6583. bool Continue = true;
  6584. if (Mode == odAddBookmark)
  6585. {
  6586. TMessageParams Params(mpNeverAskAgainCheck);
  6587. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6588. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6589. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6590. unsigned int Answer =
  6591. MessageDialog(Message,
  6592. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6593. if (Answer == qaNeverAskAgain)
  6594. {
  6595. Continue = true;
  6596. WinConfiguration->UseSharedBookmarks = true;
  6597. }
  6598. else if (Answer == qaOK)
  6599. {
  6600. Continue = true;
  6601. WinConfiguration->UseSharedBookmarks = false;
  6602. }
  6603. else
  6604. {
  6605. Continue = false;
  6606. }
  6607. }
  6608. if (Continue)
  6609. {
  6610. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6611. try
  6612. {
  6613. UnicodeString Name = DirView(Side)->PathName;
  6614. bool Repeat;
  6615. do
  6616. {
  6617. Repeat = false;
  6618. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6619. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6620. // do not allow switching to location profiles,
  6621. // if we are not connected
  6622. HasDirView[osLocal] && IsActiveTerminal(Terminal)))
  6623. {
  6624. TWindowLock Lock(this);
  6625. if (!TryOpenDirectory(Side, Name))
  6626. {
  6627. Repeat = true;
  6628. Mode = odBrowse;
  6629. }
  6630. }
  6631. }
  6632. while (Repeat);
  6633. }
  6634. __finally
  6635. {
  6636. delete VisitedDirectories;
  6637. }
  6638. }
  6639. }
  6640. //---------------------------------------------------------------------------
  6641. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6642. {
  6643. DebugAssert(!Terminal->CommandSessionOpened);
  6644. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6645. }
  6646. //---------------------------------------------------------------------------
  6647. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6648. {
  6649. bool Result = Terminal->IsCapable[Capability] ||
  6650. Terminal->CommandSessionOpened;
  6651. if (!Result)
  6652. {
  6653. // We might get here for example when "checksum" synchronization is requested from command-line
  6654. // on server that supports neither checksum nor shell
  6655. if (!Terminal->IsCapable[fcSecondaryShell])
  6656. {
  6657. NotSupported();
  6658. }
  6659. else if (!GUIConfiguration->ConfirmCommandSession)
  6660. {
  6661. Result = true;
  6662. }
  6663. else
  6664. {
  6665. TMessageParams Params(mpNeverAskAgainCheck);
  6666. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6667. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6668. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6669. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6670. if (Answer == qaNeverAskAgain)
  6671. {
  6672. GUIConfiguration->ConfirmCommandSession = false;
  6673. Result = true;
  6674. }
  6675. else if (Answer == qaOK)
  6676. {
  6677. Result = true;
  6678. }
  6679. }
  6680. if (Result)
  6681. {
  6682. Result = CommandSessionFallback();
  6683. }
  6684. }
  6685. return Result;
  6686. }
  6687. //---------------------------------------------------------------------------
  6688. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6689. {
  6690. if (EnsureCommandSessionFallback(fcAnyCommand))
  6691. {
  6692. DoConsoleDialog(Terminal, Command);
  6693. }
  6694. }
  6695. //---------------------------------------------------------------------------
  6696. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6697. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6698. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6699. {
  6700. if (!IsSideLocalBrowser(osRemote) &&
  6701. IsFileControl(DropSourceControl, osRemote) &&
  6702. (FDDExtMapFile != NULL))
  6703. {
  6704. Accept = true;
  6705. }
  6706. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6707. DebugAssert(FDDTargetControl != NULL);
  6708. }
  6709. //---------------------------------------------------------------------------
  6710. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6711. _di_IDataObject DataObj, int KeyState,
  6712. const TPoint & Point, int & Effect, bool & Accept)
  6713. {
  6714. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6715. }
  6716. //---------------------------------------------------------------------------
  6717. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6718. _di_IDataObject DataObj, int KeyState,
  6719. const TPoint & Point, int & Effect, bool & Accept)
  6720. {
  6721. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6722. }
  6723. //---------------------------------------------------------------------------
  6724. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6725. TObject * Sender)
  6726. {
  6727. DebugUsedParam(Sender);
  6728. DebugAssert(FDDTargetControl == Sender);
  6729. FDDTargetControl = NULL;
  6730. }
  6731. //---------------------------------------------------------------------------
  6732. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave(int)
  6733. {
  6734. FileControlDDDragLeave(SessionsPageControl);
  6735. }
  6736. //---------------------------------------------------------------------------
  6737. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave(int)
  6738. {
  6739. FileControlDDDragLeave(QueueView3);
  6740. }
  6741. //---------------------------------------------------------------------------
  6742. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6743. {
  6744. DebugAssert(!IsSideLocalBrowser(Side));
  6745. DebugUsedParam(Side);
  6746. bool Edit = false;
  6747. TRemoteFile * File = NULL;
  6748. UnicodeString FileName;
  6749. UnicodeString PointTo;
  6750. bool SymbolicLink = true;
  6751. if (RemoteDirView->ItemFocused)
  6752. {
  6753. DebugAssert(RemoteDirView->ItemFocused->Data);
  6754. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6755. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6756. if (Edit)
  6757. {
  6758. FileName = File->FileName;
  6759. PointTo = File->LinkTo;
  6760. }
  6761. else if (!File->IsParentDirectory)
  6762. {
  6763. PointTo = File->FileName;
  6764. }
  6765. }
  6766. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6767. Terminal->IsCapable[fcHardLink]))
  6768. {
  6769. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6770. if (Edit)
  6771. {
  6772. DebugAssert(File->FileName == FileName);
  6773. int Params = dfNoRecursive;
  6774. Terminal->ExceptionOnFail = true;
  6775. try
  6776. {
  6777. Terminal->DeleteFile(L"", File, &Params);
  6778. }
  6779. __finally
  6780. {
  6781. Terminal->ExceptionOnFail = false;
  6782. }
  6783. }
  6784. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6785. }
  6786. }
  6787. //---------------------------------------------------------------------------
  6788. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6789. {
  6790. return
  6791. (IsSideLocalBrowser(Side) ||
  6792. (HasActiveTerminal() &&
  6793. Terminal->ResolvingSymlinks &&
  6794. Terminal->IsCapable[fcSymbolicLink]));
  6795. }
  6796. //---------------------------------------------------------------------------
  6797. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6798. {
  6799. return
  6800. !IsSideLocalBrowser(FCurrentSide) &&
  6801. (RemoteDirView->ItemFocused != NULL) &&
  6802. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6803. Terminal->SessionData->ResolveSymlinks;
  6804. }
  6805. //---------------------------------------------------------------------------
  6806. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6807. {
  6808. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6809. FForceExecution = true;
  6810. try
  6811. {
  6812. DirView(osCurrent)->ExecuteCurrentFile();
  6813. }
  6814. __finally
  6815. {
  6816. FForceExecution = false;
  6817. }
  6818. }
  6819. //---------------------------------------------------------------------------
  6820. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6821. {
  6822. TEditorData ExternalEditor;
  6823. ExternalEditor.Editor = edExternal;
  6824. bool Remember = false;
  6825. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6826. {
  6827. if (Remember)
  6828. {
  6829. TEditorList * EditorList = new TEditorList;
  6830. try
  6831. {
  6832. *EditorList = *WinConfiguration->EditorList;
  6833. bool Found = false;
  6834. int i = 0;
  6835. while (!Found && (i < EditorList->Count))
  6836. {
  6837. const TEditorPreferences * Editor = EditorList->Editors[i];
  6838. if ((Editor->Data->Editor == edExternal) &&
  6839. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6840. {
  6841. Found = true;
  6842. }
  6843. i++;
  6844. }
  6845. if (!Found)
  6846. {
  6847. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6848. WinConfiguration->EditorList = EditorList;
  6849. }
  6850. }
  6851. __finally
  6852. {
  6853. delete EditorList;
  6854. }
  6855. }
  6856. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6857. }
  6858. }
  6859. //---------------------------------------------------------------------------
  6860. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6861. {
  6862. if (FFileFindTerminal == ATerminal)
  6863. {
  6864. FFileFindTerminal = NULL;
  6865. HideFileFindDialog();
  6866. }
  6867. if (FClipboardTerminal == ATerminal)
  6868. {
  6869. ClipboardClear(); // implies ClipboardStop
  6870. }
  6871. if ((FOnSynchronizeAbort != NULL) &&
  6872. (ATerminal == FManagedSession))
  6873. {
  6874. FOnSynchronizeAbort(NULL);
  6875. }
  6876. }
  6877. //---------------------------------------------------------------------------
  6878. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6879. {
  6880. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  6881. DetachTerminal(Sender);
  6882. }
  6883. //---------------------------------------------------------------------------
  6884. void __fastcall TCustomScpExplorerForm::FileTerminalReplaced(
  6885. const UnicodeString FileName, TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6886. {
  6887. TManagedTerminal * ATerminal = static_cast<TManagedTerminal *>(Arg);
  6888. if (Data->Terminal == Terminal)
  6889. {
  6890. Data->Terminal = ATerminal;
  6891. }
  6892. }
  6893. //---------------------------------------------------------------------------
  6894. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6895. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6896. {
  6897. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6898. DebugAssert(Terminal != NULL);
  6899. if (Data->Terminal == Terminal)
  6900. {
  6901. Data->Terminal = NULL;
  6902. }
  6903. }
  6904. //---------------------------------------------------------------------------
  6905. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6906. {
  6907. return NULL;
  6908. }
  6909. //---------------------------------------------------------------------------
  6910. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6911. {
  6912. UpdateControls();
  6913. SessionColor = TColor(0);
  6914. UpdateRemotePathComboBox(false);
  6915. NeedSession(false);
  6916. }
  6917. //---------------------------------------------------------------------------
  6918. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6919. {
  6920. try
  6921. {
  6922. // Cache, as the login dialog can change its value.
  6923. // See also DoShow. Also see TTerminalManager::NewSession.
  6924. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6925. try
  6926. {
  6927. if (ShowLogin)
  6928. {
  6929. bool ReloadSessions = !Startup;
  6930. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6931. }
  6932. }
  6933. __finally
  6934. {
  6935. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6936. // (so there was no chance for the user to open any session yet)
  6937. if ((ShowLogin || !Startup) &&
  6938. !WinConfiguration->KeepOpenWhenNoSession &&
  6939. // this rules out the implicit Commander's local-local browser
  6940. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6941. {
  6942. TerminateApplication();
  6943. }
  6944. }
  6945. }
  6946. catch (EAbort &)
  6947. {
  6948. // swallow
  6949. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6950. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6951. }
  6952. }
  6953. //---------------------------------------------------------------------------
  6954. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6955. {
  6956. TTerminalManager * Manager = TTerminalManager::Instance();
  6957. DebugAssert(!Manager->Updating);
  6958. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6959. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6960. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6961. SessionsPageControl->LockDrawing();
  6962. try
  6963. {
  6964. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6965. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6966. {
  6967. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6968. }
  6969. for (int Index = 0; Index <= Manager->Count; Index++)
  6970. {
  6971. TThemeTabSheet * TabSheet;
  6972. if (Index >= SessionsPageControl->PageCount)
  6973. {
  6974. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6975. TabSheet->PageControl = SessionsPageControl;
  6976. }
  6977. else
  6978. {
  6979. TabSheet = SessionsPageControl->Pages[Index];
  6980. }
  6981. bool IsSessionTab = (Index < Manager->Count);
  6982. if (IsSessionTab)
  6983. {
  6984. TTerminal * Terminal = Manager->Sessions[Index];
  6985. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6986. TabSheet->CaptionTruncation = WinConfiguration->SessionTabCaptionTruncation ? tttEllipsis : tttNone;
  6987. UpdateSessionTab(TabSheet);
  6988. }
  6989. else
  6990. {
  6991. TabSheet->Tag = 0; // not really needed
  6992. TabSheet->Shadowed = false;
  6993. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6994. TabSheet->CaptionTruncation = GetNewTabTabCaptionTruncation();
  6995. // 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)
  6996. UpdateNewTabTab();
  6997. }
  6998. }
  6999. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  7000. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  7001. {
  7002. SessionsPageControl->UpdateTabsCaptionTruncation();
  7003. }
  7004. }
  7005. __finally
  7006. {
  7007. SessionsPageControl->UnlockDrawing();
  7008. }
  7009. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  7010. }
  7011. //---------------------------------------------------------------------------
  7012. TThemeTabCaptionTruncation TCustomScpExplorerForm::GetNewTabTabCaptionTruncation()
  7013. {
  7014. return WinConfiguration->SessionTabCaptionTruncation ? tttNoText : tttNone;
  7015. }
  7016. //---------------------------------------------------------------------------
  7017. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  7018. {
  7019. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  7020. }
  7021. //---------------------------------------------------------------------------
  7022. TThemeTabSheet * TCustomScpExplorerForm::GetNewTabTab()
  7023. {
  7024. return SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  7025. }
  7026. //---------------------------------------------------------------------------
  7027. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  7028. {
  7029. TThemeTabSheet * TabSheet = GetNewTabTab();
  7030. UnicodeString TabCaption;
  7031. if (WinConfiguration->SelectiveToolbarText)
  7032. {
  7033. TabCaption = GetNewTabTabCaption();
  7034. }
  7035. TabSheet->BaseCaption = TabCaption;
  7036. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  7037. }
  7038. //---------------------------------------------------------------------------
  7039. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  7040. {
  7041. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  7042. }
  7043. //---------------------------------------------------------------------------
  7044. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  7045. {
  7046. DebugFail();
  7047. return UnicodeString();
  7048. }
  7049. //---------------------------------------------------------------------------
  7050. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  7051. {
  7052. if (DebugAlwaysTrue(TabSheet != NULL))
  7053. {
  7054. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  7055. if (DebugAlwaysTrue(ASession != NULL))
  7056. {
  7057. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  7058. if (ASession->LocalBrowser)
  7059. {
  7060. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  7061. }
  7062. else
  7063. {
  7064. TabSheet->ImageIndex = AddSessionColor(Color);
  7065. }
  7066. TTerminalManager * Manager = TTerminalManager::Instance();
  7067. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  7068. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  7069. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  7070. }
  7071. }
  7072. }
  7073. //---------------------------------------------------------------------------
  7074. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  7075. {
  7076. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  7077. }
  7078. //---------------------------------------------------------------------------
  7079. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  7080. {
  7081. DebugAssert(SessionsPageControl->ActivePage != NULL);
  7082. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  7083. bool Result = (Session != NULL);
  7084. if (Result)
  7085. {
  7086. TTerminalManager::Instance()->ActiveSession = Session;
  7087. }
  7088. else
  7089. {
  7090. try
  7091. {
  7092. NewTab();
  7093. }
  7094. __finally
  7095. {
  7096. SessionListChanged();
  7097. }
  7098. FSessionsPageControlNewTabTime = Now();
  7099. }
  7100. return Result;
  7101. }
  7102. //---------------------------------------------------------------------------
  7103. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  7104. {
  7105. TWindowLock Lock(this);
  7106. SessionTabSwitched();
  7107. }
  7108. //---------------------------------------------------------------------------
  7109. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  7110. {
  7111. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  7112. DebugAssert(TransferList != NULL);
  7113. UnicodeString Name;
  7114. if (TransferList->ItemIndex <= 0)
  7115. {
  7116. Name = L"";
  7117. }
  7118. else
  7119. {
  7120. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  7121. }
  7122. if (FCopyParamAutoSelected.IsEmpty())
  7123. {
  7124. // if previous preset was not autoselected, make new preset the "default"
  7125. FCopyParamDefault = Name;
  7126. }
  7127. GUIConfiguration->CopyParamCurrent = Name;
  7128. }
  7129. //---------------------------------------------------------------------------
  7130. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  7131. {
  7132. // sanity check
  7133. bool ExistingPreset =
  7134. (FTransferListHoverIndex >= 0) &&
  7135. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  7136. DebugAssert(ExistingPreset);
  7137. if (ExistingPreset)
  7138. {
  7139. HDC DC = GetDC(0);
  7140. TCanvas * Canvas = new TCanvas();
  7141. try
  7142. {
  7143. Canvas->Handle = DC;
  7144. Canvas->Font = GetToolbarFont(this);
  7145. UnicodeString Name;
  7146. if (FTransferListHoverIndex == 0)
  7147. {
  7148. Name = L"";
  7149. }
  7150. else
  7151. {
  7152. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  7153. }
  7154. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  7155. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  7156. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  7157. static_cast<TObject*>(GetComponent(fcTransferList)));
  7158. UnicodeString InfoStr =
  7159. GUIConfiguration->CopyParamPreset[Name].
  7160. GetInfoStr(L"; ",
  7161. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  7162. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  7163. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  7164. {
  7165. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  7166. {
  7167. InfoStr.SetLength(InfoStr.Length() - 1);
  7168. }
  7169. InfoStr += Ellipsis;
  7170. }
  7171. // UpdateCaption does not cause invalidation of whole submenu, while
  7172. // setting Caption property does.
  7173. // also it probably does not resize the label, even if necessary
  7174. // (we do not want that anyway)
  7175. TransferLabel->UpdateCaption(InfoStr);
  7176. }
  7177. __finally
  7178. {
  7179. Canvas->Handle = NULL;
  7180. ReleaseDC(0, DC);
  7181. delete Canvas;
  7182. }
  7183. }
  7184. }
  7185. //---------------------------------------------------------------------------
  7186. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  7187. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  7188. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  7189. {
  7190. if (FTransferListHoverIndex != AHoverIndex)
  7191. {
  7192. FTransferListHoverIndex = AHoverIndex;
  7193. UpdateTransferLabel();
  7194. }
  7195. }
  7196. //---------------------------------------------------------------------------
  7197. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  7198. {
  7199. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  7200. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  7201. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  7202. {
  7203. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  7204. {
  7205. if (!NonVisualDataModule->Busy)
  7206. {
  7207. OpenDirectory(GetSide(osCurrent));
  7208. }
  7209. Message.Result = 1;
  7210. }
  7211. else
  7212. {
  7213. TForm::Dispatch(&Message);
  7214. }
  7215. }
  7216. else
  7217. {
  7218. TForm::Dispatch(&Message);
  7219. }
  7220. }
  7221. //---------------------------------------------------------------------------
  7222. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  7223. {
  7224. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  7225. if ((FIgnoreNextDialogChar != 0) &&
  7226. (towupper(Message.CharCode) == towupper(FIgnoreNextDialogChar)))
  7227. {
  7228. Message.Result = 1;
  7229. }
  7230. else
  7231. {
  7232. TForm::Dispatch(&Message);
  7233. }
  7234. FIgnoreNextDialogChar = 0;
  7235. }
  7236. //---------------------------------------------------------------------------
  7237. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  7238. {
  7239. // The four low-order bits are used internally by Windows
  7240. unsigned int Cmd = (Message.WParam & 0xFFF0);
  7241. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  7242. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  7243. // 61730 - restore thru double click - undocumented
  7244. // 61490 - maximize thru double click - undocumented
  7245. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  7246. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  7247. {
  7248. SysResizing(Cmd);
  7249. }
  7250. TForm::Dispatch(&Message);
  7251. }
  7252. //---------------------------------------------------------------------------
  7253. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  7254. {
  7255. AppLog(L"Windows session end query");
  7256. // We were actually never able to make ENDSESSION_CRITICAL happen.
  7257. // Also there no point returning TRUE as we are not able to
  7258. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  7259. // Hence the process termination might be safer :)
  7260. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  7261. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  7262. {
  7263. Message.Result = FALSE;
  7264. }
  7265. else
  7266. {
  7267. Message.Result = TRUE;
  7268. }
  7269. // Do not call default handling as that triggers OnCloseQuery,
  7270. // where our implementation will popup confirmation dialogs, what we do not want,
  7271. // as per Vista guidelines:
  7272. // https://learn.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  7273. }
  7274. //---------------------------------------------------------------------------
  7275. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  7276. {
  7277. AppLog(L"Windows session end");
  7278. if (Message.EndSession && IsApplicationMinimized())
  7279. {
  7280. // WORKAROUND
  7281. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  7282. // when it receives WM_ENDSESSION,
  7283. // but that sometimes (particularly when application is minimized) cause crashes.
  7284. // Crash popups message that blocks log off.
  7285. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  7286. // so we call ExitProcess() immediately, not even trying to cleanup.
  7287. // It still causes beep, so there's likely some popup, but it does not block
  7288. // log off.
  7289. ExitProcess(0);
  7290. }
  7291. TForm::Dispatch(&Message);
  7292. }
  7293. #ifdef _DEBUG
  7294. //---------------------------------------------------------------------------
  7295. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  7296. {
  7297. TForm::Dispatch(&Message);
  7298. }
  7299. #endif
  7300. //---------------------------------------------------------------------------
  7301. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  7302. {
  7303. }
  7304. //---------------------------------------------------------------------------
  7305. void __fastcall TCustomScpExplorerForm::DoShow()
  7306. {
  7307. // Called from TCustomScpExplorerForm::CMShowingChanged
  7308. // only now are the controls resized finally, so the size constraints
  7309. // will not conflict with possibly very small window size
  7310. RestoreFormParams();
  7311. // This indicates that the form was rescaled after parameters were loaded and
  7312. // as most probably the parameters were actually saved with the current form scaling
  7313. // (windows was the last time likely closed on the same monitor, where it is starting now),
  7314. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  7315. // and no rounding problems will occur.
  7316. // See also RestoreForm.
  7317. // (we should be safe to call RestoreParams unconditionally)
  7318. if (PixelsPerInch != Screen->PixelsPerInch)
  7319. {
  7320. RestoreParams();
  7321. }
  7322. FixControlsPlacement();
  7323. if (Position == poDefaultPosOnly)
  7324. {
  7325. CutFormToDesktop(this);
  7326. }
  7327. TForm::DoShow();
  7328. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  7329. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  7330. // This was previously in NeedSession, where other related code is.
  7331. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  7332. // unpleasantly shows briefly.
  7333. if ((ManagedSession == NULL) &&
  7334. !WinConfiguration->ShowLoginWhenNoSession &&
  7335. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  7336. // This detects if workspace was saved the last time the main widow was closed
  7337. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  7338. {
  7339. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  7340. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  7341. }
  7342. // Before main window displays
  7343. if (ManagedSession == NULL)
  7344. {
  7345. StartingWithoutSession();
  7346. }
  7347. UpdatePixelsPerInchMainWindowCounter();
  7348. FShowing = true;
  7349. }
  7350. //---------------------------------------------------------------------------
  7351. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  7352. {
  7353. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  7354. }
  7355. //---------------------------------------------------------------------------
  7356. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  7357. {
  7358. SessionListChanged();
  7359. InitStatusBar();
  7360. UpdateControls();
  7361. }
  7362. //---------------------------------------------------------------------------
  7363. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  7364. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  7365. TNotifyEvent OnBalloonClick, TObject * UserData)
  7366. {
  7367. bool Do;
  7368. UnicodeString Message;
  7369. if (E == NULL)
  7370. {
  7371. Message = Str;
  7372. Do = true;
  7373. }
  7374. else
  7375. {
  7376. Do = ExceptionMessage(E, Message);
  7377. }
  7378. if (Do && WinConfiguration->BalloonNotifications)
  7379. {
  7380. UnicodeString Title;
  7381. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  7382. {
  7383. Message = TrimLeft(Message);
  7384. }
  7385. else
  7386. {
  7387. Message = UnformatMessage(Message);
  7388. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  7389. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  7390. Title = LoadResourceString(Captions[Type]);
  7391. if (Terminal != NULL)
  7392. {
  7393. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal->PrimaryTerminal));
  7394. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  7395. }
  7396. }
  7397. if (Seconds == 0)
  7398. {
  7399. Seconds = WinConfiguration->NotificationsTimeout;
  7400. }
  7401. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  7402. }
  7403. }
  7404. //---------------------------------------------------------------------------
  7405. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  7406. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  7407. UnicodeString HelpKeyword, const TMessageParams * Params,
  7408. TTerminal * Terminal)
  7409. {
  7410. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  7411. (FOnFeedSynchronizeError != NULL)) &&
  7412. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  7413. {
  7414. if (FOnFeedSynchronizeError != NULL)
  7415. {
  7416. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  7417. }
  7418. else
  7419. {
  7420. DebugAssert(FErrorList != NULL);
  7421. TStringList * MoreMessagesCopy = NULL;
  7422. if (MoreMessages)
  7423. {
  7424. MoreMessagesCopy = new TStringList();
  7425. MoreMessagesCopy->Assign(MoreMessages);
  7426. }
  7427. FErrorList->AddObject(Message, MoreMessagesCopy);
  7428. }
  7429. PopupTrayBalloon(Terminal, Message, Type);
  7430. return ContinueAnswer(Answers);
  7431. }
  7432. else
  7433. {
  7434. bool UseBalloon = FTrayIcon->Visible;
  7435. if (UseBalloon)
  7436. {
  7437. PopupTrayBalloon(Terminal, Message, Type);
  7438. }
  7439. unsigned int Result;
  7440. try
  7441. {
  7442. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  7443. }
  7444. __finally
  7445. {
  7446. // cancel only balloon we popped up, otherwise we may cancel notification
  7447. // balloon that was there before the message dialog
  7448. // (such as "dd confirmation opt in balloon)
  7449. if (UseBalloon)
  7450. {
  7451. FTrayIcon->CancelBalloon();
  7452. }
  7453. }
  7454. return Result;
  7455. }
  7456. }
  7457. //---------------------------------------------------------------------------
  7458. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  7459. TTerminal * Terminal, Exception * E)
  7460. {
  7461. if (FTrayIcon->Visible)
  7462. {
  7463. PopupTrayBalloon(Terminal, L"", qtError, E);
  7464. }
  7465. // particularly prevent opening new session from jump list,
  7466. // while exception is shown
  7467. NonVisualDataModule->StartBusy();
  7468. try
  7469. {
  7470. ShowExtendedExceptionEx(Terminal, E);
  7471. }
  7472. __finally
  7473. {
  7474. NonVisualDataModule->EndBusy();
  7475. FTrayIcon->CancelBalloon();
  7476. }
  7477. }
  7478. //---------------------------------------------------------------------------
  7479. void __fastcall TCustomScpExplorerForm::SessionReady()
  7480. {
  7481. InitStatusBar();
  7482. // cannot rely on active page being page for active terminal,
  7483. // as it can happen that active page is the "new session" page
  7484. // (e.g. when reconnecting active terminal, while login dialog
  7485. // invoked from "new session" page is modal)
  7486. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  7487. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  7488. }
  7489. //---------------------------------------------------------------------------
  7490. void TCustomScpExplorerForm::InactiveTerminalNotify(
  7491. TManagedTerminal * Terminal, const UnicodeString & Message, TQueryType Type, Exception * E)
  7492. {
  7493. Notify(Terminal, Message, Type, false, NULL, NULL, E);
  7494. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  7495. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  7496. {
  7497. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  7498. }
  7499. }
  7500. //---------------------------------------------------------------------------
  7501. void TCustomScpExplorerForm::Notify(
  7502. TManagedTerminal * Terminal, const UnicodeString & AMessage, TQueryType Type,
  7503. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  7504. {
  7505. UnicodeString Message = AMessage;
  7506. if ((E == NULL) ||
  7507. ExceptionMessage(E, Message))
  7508. {
  7509. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  7510. if (Important)
  7511. {
  7512. Seconds *= 5;
  7513. }
  7514. UnicodeString NoteMessage(UnformatMessage(Message));
  7515. if (Terminal != NULL)
  7516. {
  7517. NoteMessage = TTerminalManager::Instance()->FormatTerminalNoteMessage(Terminal, NoteMessage);
  7518. }
  7519. if (WinConfiguration->BalloonNotifications)
  7520. {
  7521. AddNote(NoteMessage);
  7522. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  7523. }
  7524. else
  7525. {
  7526. FlashOnBackground();
  7527. PostNote(NoteMessage, Seconds, OnClick, UserData);
  7528. }
  7529. }
  7530. }
  7531. //---------------------------------------------------------------------------
  7532. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7533. {
  7534. RestoreApp();
  7535. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7536. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7537. !NonVisualDataModule->Busy)
  7538. {
  7539. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7540. TTerminalManager::Instance()->ActiveSession = Terminal;
  7541. if (!ComponentVisible[fcQueueView])
  7542. {
  7543. ToggleQueueVisibility();
  7544. GoToQueue();
  7545. }
  7546. }
  7547. }
  7548. //---------------------------------------------------------------------------
  7549. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7550. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7551. {
  7552. UnicodeString Message;
  7553. TNotifyEvent OnClick = NULL;
  7554. TObject * UserData = NULL;
  7555. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7556. switch (Event)
  7557. {
  7558. case qeEmptyButMonitored:
  7559. if ((ATerminal != Terminal) || QueueInvisible)
  7560. {
  7561. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7562. OnClick = QueueEmptyNoteClicked;
  7563. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7564. TerminalNoteData->Terminal = ATerminal;
  7565. UserData = TerminalNoteData;
  7566. }
  7567. break;
  7568. case qeEmpty:
  7569. OperationComplete(ATerminal->QueueOperationStart);
  7570. break;
  7571. case qePendingUserAction:
  7572. if ((ATerminal != Terminal) ||
  7573. (QueueInvisible && !IsQueueAutoPopup()))
  7574. {
  7575. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7576. }
  7577. break;
  7578. default:
  7579. DebugFail();
  7580. }
  7581. if (!Message.IsEmpty())
  7582. {
  7583. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7584. }
  7585. }
  7586. //---------------------------------------------------------------------------
  7587. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7588. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7589. {
  7590. if (FDDExtMapFile != NULL)
  7591. {
  7592. CloseHandle(FDDExtMapFile);
  7593. FDDExtMapFile = NULL;
  7594. }
  7595. if (WinConfiguration->DDFakeFile)
  7596. {
  7597. DDFakeFileInitDrag(FileList, Created);
  7598. }
  7599. }
  7600. //---------------------------------------------------------------------------
  7601. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7602. {
  7603. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7604. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7605. {
  7606. FFakeFileDropTarget = FileName;
  7607. }
  7608. }
  7609. //---------------------------------------------------------------------------
  7610. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7611. {
  7612. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7613. if (!ForceDirectories(ApiPath(Result)))
  7614. {
  7615. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7616. }
  7617. FileSetAttr(ApiPath(Result), faHidden);
  7618. return Result;
  7619. }
  7620. //---------------------------------------------------------------------------
  7621. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7622. bool & Created)
  7623. {
  7624. FFakeFileDropTarget = UnicodeString();
  7625. FDragFakeDirectory = CreateFakeTransferDirectory();
  7626. FileList->AddItem(NULL, FDragFakeDirectory);
  7627. Created = true;
  7628. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7629. {
  7630. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7631. }
  7632. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7633. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7634. {
  7635. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7636. TDragExtCommStruct* CommStruct;
  7637. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7638. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7639. DebugAssert(CommStruct != NULL);
  7640. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7641. CommStruct->Dragging = true;
  7642. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7643. LENOF(CommStruct->DropDest));
  7644. NULL_TERMINATE(CommStruct->DropDest);
  7645. UnmapViewOfFile(CommStruct);
  7646. }
  7647. }
  7648. //---------------------------------------------------------------------------
  7649. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7650. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7651. {
  7652. bool Result;
  7653. DebugAssert(!IsLocalBrowserMode());
  7654. if (Sender == RemoteDirView)
  7655. {
  7656. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7657. }
  7658. else
  7659. {
  7660. DebugAssert(Sender == RemoteDriveView);
  7661. TStrings * FileList = RemoteDriveView->DragFileList();
  7662. try
  7663. {
  7664. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7665. }
  7666. __finally
  7667. {
  7668. delete FileList;
  7669. }
  7670. }
  7671. if (FDDTargetControl == RemoteDriveView)
  7672. {
  7673. RemoteDriveView->UpdateDropTarget();
  7674. }
  7675. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7676. // or from tree view to dir view.
  7677. // foMove happens when file/dir is dragged from remote tree view outside of
  7678. // application via dragex
  7679. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7680. (DropSourceControl == RemoteDriveView))
  7681. {
  7682. RemoteDriveView->UpdateDropSource();
  7683. }
  7684. return Result;
  7685. }
  7686. //---------------------------------------------------------------------------
  7687. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7688. {
  7689. DebugAssert(!IsLocalBrowserMode());
  7690. // This also handles drops of remote files to queue.
  7691. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7692. SAFE_DESTROY(FDDFileList);
  7693. if (!FFakeFileDropTarget.IsEmpty())
  7694. {
  7695. RemoveDir(ApiPath(FFakeFileDropTarget));
  7696. }
  7697. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7698. {
  7699. try
  7700. {
  7701. TDragResult DDResult = (Sender == RemoteDirView) ?
  7702. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7703. // Focus is moved to the target application,
  7704. // but as we are going to present the UI, we need to steal the focus back.
  7705. // This most likely won't work though (windows does not allow application
  7706. // to steal focus most of the time)
  7707. Application->BringToFront();
  7708. // On older version of Windows we never got drMove here, see also comment below.
  7709. // On Windows 10, we get the "move".
  7710. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7711. {
  7712. UnicodeString TargetDirectory;
  7713. TFileOperation Operation;
  7714. // drInvalid may mean drMove, see comment below
  7715. switch (DDResult)
  7716. {
  7717. case drCopy:
  7718. Operation = foCopy;
  7719. break;
  7720. case drMove:
  7721. Operation = foMove;
  7722. break;
  7723. default:
  7724. DebugFail();
  7725. case drInvalid:
  7726. // prefer "copy" for safety
  7727. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7728. break;
  7729. }
  7730. TTransferOperationParam Param;
  7731. UnicodeString CounterName;
  7732. bool ForceQueue;
  7733. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7734. {
  7735. // we get drInvalid both if d&d was intercepted by ddext,
  7736. // and when users drops on no-drop location.
  7737. // we tell the difference by existence of response from ddext,
  7738. // so we ignore absence of response in this case
  7739. if (DDResult != drInvalid)
  7740. {
  7741. // here we know that the extension is installed,
  7742. // as it is checked as soon as drag&drop starts from
  7743. // RemoteFileControlDDCreateDragFileList
  7744. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7745. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7746. {
  7747. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7748. }
  7749. else
  7750. {
  7751. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7752. }
  7753. }
  7754. }
  7755. else
  7756. {
  7757. // download using fake file
  7758. Param.Temp = false;
  7759. Param.DragDrop = true;
  7760. if (ForceQueue)
  7761. {
  7762. Param.Queue = asOn;
  7763. }
  7764. if (Sender == RemoteDirView)
  7765. {
  7766. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7767. }
  7768. if (RemoteFileControlFileOperation(Sender, Operation,
  7769. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7770. {
  7771. Configuration->Usage->Inc(CounterName);
  7772. }
  7773. }
  7774. }
  7775. }
  7776. __finally
  7777. {
  7778. CloseHandle(FDDExtMapFile);
  7779. FDDExtMapFile = NULL;
  7780. delete FDragFakeMonitors;
  7781. FDragFakeMonitors = NULL;
  7782. RemoveDir(ApiPath(FDragFakeDirectory));
  7783. FDragFakeDirectory = L"";
  7784. FFakeFileDropTarget = UnicodeString();
  7785. }
  7786. }
  7787. if (FDragDropSshTerminate.get() != NULL)
  7788. {
  7789. FDragDropSshTerminate->Rethrow();
  7790. }
  7791. }
  7792. //---------------------------------------------------------------------------
  7793. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7794. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7795. {
  7796. // Remember drop effect so we know (when user drops files), if we copy or move
  7797. FLastDropEffect = dwEffect;
  7798. }
  7799. //---------------------------------------------------------------------------
  7800. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7801. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7802. {
  7803. bool Result;
  7804. if (FDDTargetControl == QueueView3)
  7805. {
  7806. Directory = DefaultDownloadTargetDirectory();
  7807. Result = true;
  7808. CounterName = L"DownloadsDragDropQueue";
  7809. ForceQueue = true;
  7810. }
  7811. else if (!FFakeFileDropTarget.IsEmpty())
  7812. {
  7813. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7814. Result = true;
  7815. ForceQueue = false;
  7816. CounterName = L"DownloadsDragDropFakeFile";
  7817. }
  7818. else
  7819. {
  7820. ForceQueue = false;
  7821. Enabled = false;
  7822. try
  7823. {
  7824. int Timer = 0;
  7825. Result = false;
  7826. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7827. {
  7828. {
  7829. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7830. TDragExtCommStruct* CommStruct;
  7831. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7832. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7833. DebugAssert(CommStruct != NULL);
  7834. Result = !CommStruct->Dragging;
  7835. if (Result)
  7836. {
  7837. Directory = ExtractFilePath(CommStruct->DropDest);
  7838. CounterName = L"DownloadsDragDropExternalExt";
  7839. }
  7840. UnmapViewOfFile(CommStruct);
  7841. }
  7842. if (!Result)
  7843. {
  7844. Sleep(50);
  7845. Timer += 50;
  7846. Application->ProcessMessages();
  7847. }
  7848. }
  7849. }
  7850. __finally
  7851. {
  7852. Enabled = true;
  7853. }
  7854. }
  7855. return Result;
  7856. }
  7857. //---------------------------------------------------------------------------
  7858. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7859. const UnicodeString TempDir, int SecDelay)
  7860. {
  7861. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7862. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7863. if (FDelayedDeletionTimer == NULL)
  7864. {
  7865. DebugAssert(HandleAllocated());
  7866. FDelayedDeletionTimer = new TTimer(this);
  7867. FDelayedDeletionTimer->Interval = 10000;
  7868. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7869. }
  7870. else
  7871. {
  7872. FDelayedDeletionTimer->Enabled = true;
  7873. }
  7874. }
  7875. //---------------------------------------------------------------------------
  7876. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7877. {
  7878. DebugAssert(FDelayedDeletionList != NULL);
  7879. TDateTime N = Now();
  7880. TDateTime Alert;
  7881. UnicodeString Directory;
  7882. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7883. {
  7884. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7885. if ((N >= Alert) || (Sender == NULL))
  7886. {
  7887. Directory = FDelayedDeletionList->Strings[Index];
  7888. if (RecursiveDeleteFile(ExcludeTrailingBackslash(Directory)))
  7889. {
  7890. FDelayedDeletionList->Delete(Index);
  7891. }
  7892. }
  7893. }
  7894. if (FDelayedDeletionList->Count == 0)
  7895. {
  7896. FDelayedDeletionTimer->Enabled = false;
  7897. }
  7898. }
  7899. //---------------------------------------------------------------------------
  7900. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7901. {
  7902. DebugAssert(!IsLocalBrowserMode());
  7903. if (IsFileControl(FDDTargetControl, osRemote) ||
  7904. (FDDTargetControl == SessionsPageControl))
  7905. {
  7906. TManagedTerminal * TargetTerminal = NULL;
  7907. TFileOperation Operation = foNone;
  7908. if (FDDTargetControl == SessionsPageControl)
  7909. {
  7910. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7911. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7912. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7913. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7914. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7915. {
  7916. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7917. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7918. {
  7919. Operation = foRemoteMove;
  7920. }
  7921. else
  7922. {
  7923. Operation = foRemoteCopy;
  7924. }
  7925. }
  7926. }
  7927. else
  7928. {
  7929. if (FLastDropEffect == DROPEFFECT_MOVE)
  7930. {
  7931. Operation = foRemoteMove;
  7932. }
  7933. else if (FLastDropEffect == DROPEFFECT_COPY)
  7934. {
  7935. Operation = foRemoteCopy;
  7936. }
  7937. }
  7938. if (Operation != foNone)
  7939. {
  7940. RemoteFileControlFileOperation(DropSourceControl,
  7941. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7942. }
  7943. // abort drag&drop
  7944. Abort();
  7945. }
  7946. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7947. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7948. (FDDTargetControl != QueueView3))
  7949. {
  7950. DebugAssert(!FDragTempDir.IsEmpty());
  7951. TTransferType Type;
  7952. UnicodeString TempDir = FDragTempDir;
  7953. // We clear FDragTempDir before calling
  7954. // just in case it fail (raises exception)
  7955. FDragTempDir = L"";
  7956. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7957. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7958. // empty directory parameter means temp directory -> don't display it!
  7959. UnicodeString TargetDir = L"";
  7960. int Options = 0;
  7961. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7962. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7963. {
  7964. Abort();
  7965. }
  7966. // TargetDir is set when dropped on local file control
  7967. // (this was workaround for legacy dirview event handling, now it should be
  7968. // made prettier)
  7969. if (TargetDir.IsEmpty())
  7970. {
  7971. TargetDir = TempDir;
  7972. if (ForceDirectories(ApiPath(TargetDir)))
  7973. {
  7974. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7975. FPendingTempSpaceWarn = true;
  7976. try
  7977. {
  7978. FDragDropOperation = true;
  7979. int Params = cpTemporary |
  7980. (Type == ttMove ? cpDelete : 0);
  7981. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7982. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7983. }
  7984. __finally
  7985. {
  7986. FDragDropOperation = false;
  7987. FPendingTempSpaceWarn = false;
  7988. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7989. }
  7990. }
  7991. else
  7992. {
  7993. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7994. }
  7995. }
  7996. }
  7997. }
  7998. //---------------------------------------------------------------------------
  7999. void __fastcall TCustomScpExplorerForm::DDDownload(
  8000. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  8001. {
  8002. DebugAssert(!IsLocalBrowserMode());
  8003. TAutoBatch AutoBatch(this);
  8004. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  8005. UpdateCopyParamCounters(*CopyParam);
  8006. FMoveToQueue = false;
  8007. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  8008. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  8009. {
  8010. RemoteDriveView->UpdateDropSource();
  8011. }
  8012. }
  8013. //---------------------------------------------------------------------------
  8014. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  8015. {
  8016. public:
  8017. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  8018. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  8019. {
  8020. }
  8021. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  8022. {
  8023. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  8024. TDataObjectFilesEx::AllowData(FormatEtc);
  8025. }
  8026. };
  8027. //---------------------------------------------------------------------------
  8028. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  8029. TObject * Sender, TDataObject *& DataObject)
  8030. {
  8031. if (FDDExtMapFile != NULL)
  8032. {
  8033. TFileList * FileList = DragDropFiles(Sender)->FileList;
  8034. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  8035. {
  8036. Abort();
  8037. }
  8038. if (FileList->Count > 0)
  8039. {
  8040. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  8041. if (!FilesObject->IsValid(true, true))
  8042. {
  8043. FilesObject->_Release();
  8044. }
  8045. else
  8046. {
  8047. DataObject = FilesObject;
  8048. }
  8049. }
  8050. }
  8051. }
  8052. //---------------------------------------------------------------------------
  8053. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  8054. {
  8055. DebugFail();
  8056. }
  8057. //---------------------------------------------------------------------------
  8058. void __fastcall TCustomScpExplorerForm::GoToTree()
  8059. {
  8060. ComponentVisible[fcRemoteTree] = true;
  8061. DriveView(osRemote)->SetFocus();
  8062. }
  8063. //---------------------------------------------------------------------------
  8064. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  8065. TPanelExport Export)
  8066. {
  8067. TCustomDirView * DirView = this->DirView(Side);
  8068. std::unique_ptr<TStrings> ExportData(new TStringList());
  8069. switch (Export)
  8070. {
  8071. case pePath:
  8072. ExportData->Add(DirView->PathName);
  8073. break;
  8074. case peFileList:
  8075. case peFullFileList:
  8076. {
  8077. bool FullPath = (Export == peFullFileList);
  8078. DirView->CreateFileList(false, FullPath, ExportData.get());
  8079. for (int Index = 0; Index < ExportData->Count; Index++)
  8080. {
  8081. if (ExportData->Strings[Index].Pos(L" ") > 0)
  8082. {
  8083. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  8084. }
  8085. }
  8086. }
  8087. break;
  8088. default:
  8089. DebugFail();
  8090. }
  8091. return ExportData.release();
  8092. }
  8093. //---------------------------------------------------------------------------
  8094. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  8095. TPanelExport Export, TPanelExportDestination Destination)
  8096. {
  8097. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  8098. PanelExportStore(Side, Export, Destination, ExportData.get());
  8099. }
  8100. //---------------------------------------------------------------------------
  8101. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  8102. TPanelExport /*Export*/, TPanelExportDestination Destination,
  8103. TStrings * ExportData)
  8104. {
  8105. if (Destination == pedClipboard)
  8106. {
  8107. TInstantOperationVisualizer Visualizer;
  8108. CopyToClipboard(ExportData);
  8109. }
  8110. else
  8111. {
  8112. DebugFail();
  8113. }
  8114. }
  8115. //---------------------------------------------------------------------------
  8116. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  8117. {
  8118. TCustomDirView * DirView = this->DirView(Side);
  8119. UnicodeString Mask = DirView->Mask;
  8120. if (DoFilterMaskDialog(DirView, Mask))
  8121. {
  8122. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  8123. Configuration->Usage->Inc(L"Filters");
  8124. }
  8125. }
  8126. //---------------------------------------------------------------------------
  8127. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  8128. {
  8129. return FQueueController->DefaultOperation();
  8130. }
  8131. //---------------------------------------------------------------------------
  8132. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  8133. TQueueOperation Operation, void ** Param)
  8134. {
  8135. switch (Operation)
  8136. {
  8137. case qoPreferences:
  8138. return true;
  8139. case qoGoTo:
  8140. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  8141. case qoOnceEmpty:
  8142. return IsAnythingQueued();
  8143. default:
  8144. return FQueueController->AllowOperation(Operation, Param);
  8145. }
  8146. }
  8147. //---------------------------------------------------------------------------
  8148. void __fastcall TCustomScpExplorerForm::GoToQueue()
  8149. {
  8150. if (DebugAlwaysTrue(QueueView3->Visible))
  8151. {
  8152. QueueView3->SetFocus();
  8153. }
  8154. }
  8155. //---------------------------------------------------------------------------
  8156. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  8157. TQueueOperation Operation, void * Param)
  8158. {
  8159. if (Operation == qoGoTo)
  8160. {
  8161. GoToQueue();
  8162. }
  8163. else if (Operation == qoPreferences)
  8164. {
  8165. PreferencesDialog(pmQueue);
  8166. }
  8167. else
  8168. {
  8169. FQueueController->ExecuteOperation(Operation, Param);
  8170. }
  8171. }
  8172. //---------------------------------------------------------------------------
  8173. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  8174. {
  8175. return (Queue != NULL) && Queue->Enabled;
  8176. }
  8177. //---------------------------------------------------------------------------
  8178. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  8179. {
  8180. DebugAssert(Queue != NULL);
  8181. if (Queue != NULL)
  8182. {
  8183. Queue->Enabled = !Queue->Enabled;
  8184. }
  8185. }
  8186. //---------------------------------------------------------------------------
  8187. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  8188. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  8189. {
  8190. FQueueActedItem = QueueView3->ItemFocused;
  8191. }
  8192. //---------------------------------------------------------------------------
  8193. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  8194. {
  8195. return TopDock->Height + (MessageDock->Visible ? MessageDock->Height : 0) + QueueSplitter->Height;
  8196. }
  8197. //---------------------------------------------------------------------------
  8198. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  8199. {
  8200. return
  8201. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  8202. (QueueDock->Visible ? QueueDock->Height : 0) +
  8203. (QueueLabel->Visible ? QueueLabel->Height : 0);
  8204. }
  8205. //---------------------------------------------------------------------------
  8206. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  8207. {
  8208. return ScaleByTextHeight(this, 48);
  8209. }
  8210. //---------------------------------------------------------------------------
  8211. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  8212. TObject * /*Sender*/, int & NewSize, bool & Accept)
  8213. {
  8214. // when queue is hidden by double-clicking splitter, stray attempt to
  8215. // resize the panel with strange value arrives, make sure it is ignored
  8216. if (ComponentVisible[fcQueueView])
  8217. {
  8218. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  8219. if (NewSize > MaxHeightLimit)
  8220. {
  8221. NewSize = MaxHeightLimit;
  8222. }
  8223. int MinHeightLimit =
  8224. GetStaticQueuePanelComponentsHeight() +
  8225. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  8226. GetMinQueueViewHeight();
  8227. if (NewSize < MinHeightLimit)
  8228. {
  8229. NewSize = MinHeightLimit;
  8230. }
  8231. }
  8232. else
  8233. {
  8234. Accept = false;
  8235. }
  8236. }
  8237. //---------------------------------------------------------------------------
  8238. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  8239. {
  8240. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  8241. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  8242. if (QueueViewHeight < GetMinQueueViewHeight())
  8243. {
  8244. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  8245. }
  8246. }
  8247. //---------------------------------------------------------------------------
  8248. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  8249. TDragObject *& /*DragObject*/)
  8250. {
  8251. FQueueActedItem = QueueView3->ItemFocused;
  8252. FQueueScrollOnDragOver->StartDrag();
  8253. }
  8254. //---------------------------------------------------------------------------
  8255. void __fastcall TCustomScpExplorerForm::QueueView3EndDrag(
  8256. TObject *, TObject * DebugUsedArg(Target), int DebugUsedArg(X), int DebugUsedArg(Y))
  8257. {
  8258. FQueueScrollOnDragOver->EndDrag();
  8259. }
  8260. //---------------------------------------------------------------------------
  8261. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  8262. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  8263. {
  8264. Accept = true;
  8265. if (Source == QueueView3)
  8266. {
  8267. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  8268. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  8269. if (Accept)
  8270. {
  8271. TQueueItemProxy * QueueItem;
  8272. TQueueItemProxy * DestQueueItem;
  8273. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  8274. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  8275. Accept = (QueueItem != DestQueueItem) &&
  8276. (QueueItem->Status == TQueueItem::qsPending) &&
  8277. (DestQueueItem->Status == TQueueItem::qsPending);
  8278. }
  8279. FQueueScrollOnDragOver->DragOver(TPoint(X, Y));
  8280. }
  8281. }
  8282. //---------------------------------------------------------------------------
  8283. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  8284. TObject * /*Source*/, int /*X*/, int /*Y*/)
  8285. {
  8286. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  8287. {
  8288. TQueueItemProxy * QueueItem;
  8289. TQueueItemProxy * DestQueueItem;
  8290. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  8291. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  8292. QueueItem->Move(DestQueueItem);
  8293. }
  8294. }
  8295. //---------------------------------------------------------------------------
  8296. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  8297. {
  8298. if ((QueueView3->ItemFocused == NULL) &&
  8299. (QueueView3->Items->Count > 0))
  8300. {
  8301. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  8302. }
  8303. QueueLabelUpdateStatus();
  8304. }
  8305. //---------------------------------------------------------------------------
  8306. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  8307. {
  8308. QueueLabelUpdateStatus();
  8309. }
  8310. //---------------------------------------------------------------------------
  8311. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  8312. {
  8313. QueueLabel->UpdateStatus();
  8314. }
  8315. //---------------------------------------------------------------------------
  8316. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  8317. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  8318. {
  8319. if (Selected)
  8320. {
  8321. NonVisualDataModule->UpdateNonVisibleActions();
  8322. }
  8323. }
  8324. //---------------------------------------------------------------------------
  8325. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  8326. {
  8327. TDragDropFilesEx * Result = NULL;
  8328. if (Sender == SessionsPageControl)
  8329. {
  8330. Result = FSessionsDragDropFilesEx;
  8331. }
  8332. else if (Sender == QueueView3)
  8333. {
  8334. Result = FQueueDragDropFilesEx;
  8335. }
  8336. else
  8337. {
  8338. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8339. if (DirView != NULL)
  8340. {
  8341. Result = DirView->DragDropFilesEx;
  8342. }
  8343. else
  8344. {
  8345. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  8346. if (DriveView != NULL)
  8347. {
  8348. Result = DriveView->DragDropFilesEx;
  8349. }
  8350. }
  8351. }
  8352. DebugAssert(Result != NULL);
  8353. return Result;
  8354. }
  8355. //---------------------------------------------------------------------------
  8356. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  8357. {
  8358. return (DView->SelCount == DView->FilesCount);
  8359. }
  8360. //---------------------------------------------------------------------------
  8361. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  8362. {
  8363. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  8364. }
  8365. //---------------------------------------------------------------------------
  8366. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  8367. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  8368. {
  8369. DebugAssert(!IsLocalBrowserMode());
  8370. TFileOperation Operation;
  8371. switch (Effect)
  8372. {
  8373. case DROPEFFECT_MOVE:
  8374. Operation = foMove;
  8375. break;
  8376. case DROPEFFECT_COPY:
  8377. // occures on WinXP (reported by user)
  8378. default:
  8379. Operation = foCopy;
  8380. break;
  8381. };
  8382. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  8383. // see a comment in TUnixDirView::PerformItemDragDropOperation
  8384. if (DragDropFilesEx->FileList->Count > 0)
  8385. {
  8386. TStrings * FileList = new TStringList();
  8387. try
  8388. {
  8389. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  8390. {
  8391. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  8392. }
  8393. FDragDropOperation = true;
  8394. TTransferOperationParam Param;
  8395. Param.TargetDirectory = TargetPath;
  8396. // upload, no temp dirs
  8397. Param.Temp = false;
  8398. Param.DragDrop = DragDrop;
  8399. Param.Options =
  8400. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  8401. if (ForceQueue)
  8402. {
  8403. Param.Queue = asOn;
  8404. }
  8405. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  8406. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  8407. {
  8408. if (IsFileControl(DropSourceControl, osLocal))
  8409. {
  8410. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  8411. }
  8412. else
  8413. {
  8414. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  8415. }
  8416. }
  8417. }
  8418. __finally
  8419. {
  8420. FDragDropOperation = false;
  8421. delete FileList;
  8422. }
  8423. }
  8424. }
  8425. //---------------------------------------------------------------------------
  8426. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  8427. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  8428. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  8429. {
  8430. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  8431. }
  8432. //---------------------------------------------------------------------------
  8433. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  8434. TObject * Sender, int grfKeyState, int & dwEffect)
  8435. {
  8436. DebugAssert(!IsLocalBrowserMode());
  8437. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  8438. if ((dwEffect != DROPEFFECT_NONE) &&
  8439. IsFileControl(DropSourceControl, osRemote))
  8440. {
  8441. // do not allow drop on remote panel (on free space, still allow drop on directories)
  8442. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  8443. (RemoteDirView->DropTarget == NULL))
  8444. {
  8445. dwEffect = DROPEFFECT_NONE;
  8446. }
  8447. else
  8448. {
  8449. if (dwEffect == DROPEFFECT_COPY)
  8450. {
  8451. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  8452. // currently we support copying always (at least via temporary directory);
  8453. // remove associated checks once this all proves stable and working
  8454. bool CopyCapable = true;
  8455. // if we do not support neither of operations, there's no discussion
  8456. if (!MoveCapable && !CopyCapable)
  8457. {
  8458. dwEffect = DROPEFFECT_NONE;
  8459. }
  8460. else
  8461. {
  8462. // The default effect inside remote panel is move,
  8463. // unless we do not support it, but support copy
  8464. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  8465. {
  8466. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  8467. }
  8468. else
  8469. {
  8470. // with ctrl-down, we want copy unless it is not supported
  8471. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  8472. }
  8473. }
  8474. }
  8475. }
  8476. }
  8477. }
  8478. //---------------------------------------------------------------------------
  8479. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  8480. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  8481. {
  8482. DebugAssert(!IsLocalBrowserMode());
  8483. if (FDDTotalSize >= 0)
  8484. {
  8485. if (File->IsDirectory)
  8486. {
  8487. FDDTotalSize = -1;
  8488. }
  8489. else
  8490. {
  8491. FDDTotalSize += File->Size;
  8492. }
  8493. }
  8494. DebugAssert(!FDragTempDir.IsEmpty());
  8495. // TODO: this is quite ineffective
  8496. // TODO: what if invalid character replacement is disabled?
  8497. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  8498. UnicodeString TransferFileName;
  8499. if (Sender == RemoteDriveView)
  8500. {
  8501. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  8502. }
  8503. else
  8504. {
  8505. TransferFileName = File->FileName;
  8506. }
  8507. FDDFileList->AddObject(TransferFileName, File);
  8508. }
  8509. //---------------------------------------------------------------------------
  8510. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  8511. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  8512. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  8513. {
  8514. // sometimes we do not get DDEnd so the list is not released
  8515. SAFE_DESTROY(FDDFileList);
  8516. FDDFileList = new TStringList();
  8517. FDragTempDir = WinConfiguration->TemporaryDir();
  8518. FDDTotalSize = 0;
  8519. FDragDropSshTerminate.reset(NULL);
  8520. }
  8521. //---------------------------------------------------------------------------
  8522. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  8523. TObject * /*Sender*/, LongBool /*FEscapePressed*/, int /*grfKeyState*/,
  8524. HRESULT & Result)
  8525. {
  8526. if (Result == DRAGDROP_S_DROP)
  8527. {
  8528. try
  8529. {
  8530. GlobalDragImageList->HideDragImage();
  8531. try
  8532. {
  8533. RemoteFileControlDDTargetDrop();
  8534. }
  8535. catch(ESshTerminate & E)
  8536. {
  8537. FDragDropSshTerminate.reset(E.Clone());
  8538. }
  8539. }
  8540. catch (Exception &E)
  8541. {
  8542. // If downloading fails we need to cancel drag&drop, otherwise
  8543. // Explorer shows error
  8544. // But by the way exception probably never reach this point as
  8545. // it's catched on way.
  8546. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8547. Result = DRAGDROP_S_CANCEL;
  8548. ShowExtendedException(Terminal, &E);
  8549. }
  8550. }
  8551. }
  8552. //---------------------------------------------------------------------------
  8553. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8554. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8555. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8556. {
  8557. TFileMasks::TParams MaskParams;
  8558. MaskParams.Size = Size;
  8559. MaskParams.Modification = Modification;
  8560. // this does not re-parse the mask if it is the same as the last time
  8561. FDirViewMatchMask = Masks;
  8562. bool ImplicitMatch;
  8563. Matches =
  8564. // Local and RecurseInclude parameters have no effect as there's no path.
  8565. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  8566. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  8567. (AllowImplicitMatches || !ImplicitMatch);
  8568. }
  8569. //---------------------------------------------------------------------------
  8570. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8571. TObject * Sender, TListItem * Item, WORD & Indexes)
  8572. {
  8573. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8574. if (GetPartialFileExtLen(DirView->ItemFileName(Item)) > 0)
  8575. {
  8576. Indexes |= oiPartial;
  8577. }
  8578. }
  8579. //---------------------------------------------------------------------------
  8580. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8581. {
  8582. return
  8583. DirViewEnabled(osCurrent) &&
  8584. DirView(osCurrent)->CanPasteFromClipBoard();
  8585. }
  8586. //---------------------------------------------------------------------------
  8587. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8588. {
  8589. bool Result = false;
  8590. if (CanPasteToDirViewFromClipBoard())
  8591. {
  8592. Result = true;
  8593. }
  8594. else
  8595. {
  8596. UnicodeString ClipboardText;
  8597. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8598. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8599. {
  8600. if (StoredSessions->IsUrl(ClipboardText))
  8601. {
  8602. Result = true;
  8603. }
  8604. else
  8605. {
  8606. Result = DirViewEnabled(osCurrent);
  8607. }
  8608. }
  8609. }
  8610. return Result;
  8611. }
  8612. //---------------------------------------------------------------------------
  8613. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8614. {
  8615. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8616. if (DoesClipboardContainOurFiles())
  8617. {
  8618. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8619. {
  8620. DebugAssert(!IsLocalBrowserMode());
  8621. TTerminalManager * Manager = TTerminalManager::Instance();
  8622. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8623. Manager->ActiveSession = FClipboardTerminal;
  8624. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8625. }
  8626. }
  8627. else if (CanPasteToDirViewFromClipBoard())
  8628. {
  8629. DirView(osCurrent)->PasteFromClipBoard();
  8630. }
  8631. else
  8632. {
  8633. UnicodeString ClipboardText;
  8634. if (NonEmptyTextFromClipboard(ClipboardText))
  8635. {
  8636. if (StoredSessions->IsUrl(ClipboardText))
  8637. {
  8638. NewSession(ClipboardText);
  8639. }
  8640. else
  8641. {
  8642. DirView(osCurrent)->Path = ClipboardText;
  8643. }
  8644. }
  8645. }
  8646. }
  8647. //---------------------------------------------------------------------------
  8648. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8649. {
  8650. // TBD
  8651. }
  8652. //---------------------------------------------------------------------------
  8653. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8654. {
  8655. TCustomDirView * ADirView = DirView(Side);
  8656. ADirView->SelectAll(Mode);
  8657. ADirView->SetFocus();
  8658. }
  8659. //---------------------------------------------------------------------------
  8660. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8661. {
  8662. TCustomDirView * ADirView = DirView(Side);
  8663. TFileFilter Filter;
  8664. DefaultFileFilter(Filter);
  8665. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8666. {
  8667. Configuration->Usage->Inc(L"MaskSelections");
  8668. ADirView->SelectFiles(Filter, Select);
  8669. ADirView->SetFocus();
  8670. }
  8671. }
  8672. //---------------------------------------------------------------------------
  8673. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8674. {
  8675. TCustomDirView * ADirView = DirView(Side);
  8676. ADirView->RestoreSelectedNames();
  8677. ADirView->SetFocus();
  8678. }
  8679. //---------------------------------------------------------------------------
  8680. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8681. {
  8682. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8683. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8684. {
  8685. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8686. UnicodeString Ext;
  8687. if (!IsSideLocalBrowser(osCurrent))
  8688. {
  8689. Ext = UnixExtractFileExt(FileName);
  8690. }
  8691. else
  8692. {
  8693. Ext = ExtractFileExt(FileName);
  8694. }
  8695. if (Ext.IsEmpty())
  8696. {
  8697. Ext = L".";
  8698. }
  8699. TFileFilter Filter;
  8700. Filter.Masks = FORMAT(L"*%s", (Ext));
  8701. Filter.Directories = false;
  8702. CurrentDirView->SelectFiles(Filter, Select);
  8703. }
  8704. }
  8705. //---------------------------------------------------------------------------
  8706. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8707. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8708. {
  8709. UnicodeString Result;
  8710. if (FIncrementalSearchState.Searching && (Side == GetSide(osCurrent)))
  8711. {
  8712. Result = FormatIncrementalSearchStatus(FIncrementalSearchState);
  8713. }
  8714. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8715. {
  8716. // noop
  8717. }
  8718. else
  8719. {
  8720. Result =
  8721. FMTLOAD(FILE_INFO_FORMAT,
  8722. (FormatBytes(FileInfo.SelectedSize),
  8723. FormatBytes(FileInfo.FilesSize),
  8724. FormatNumber(FileInfo.SelectedCount),
  8725. FormatNumber(FileInfo.FilesCount)));
  8726. }
  8727. return Result;
  8728. }
  8729. //---------------------------------------------------------------------------
  8730. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8731. TTBXStatusPanel * Panel, TOperationSide Side)
  8732. {
  8733. if (Panel->Index == 1)
  8734. {
  8735. ToggleShowHiddenFiles();
  8736. }
  8737. else if (Panel->Index == 2)
  8738. {
  8739. Filter(Side);
  8740. }
  8741. }
  8742. //---------------------------------------------------------------------------
  8743. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8744. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8745. {
  8746. DebugAssert(!StatusBar->SimplePanel);
  8747. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8748. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8749. }
  8750. //---------------------------------------------------------------------------
  8751. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8752. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8753. {
  8754. DebugAssert(StatusBar->Panels->Count >= 3);
  8755. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8756. if (FileInfo.HiddenCount > 0)
  8757. {
  8758. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8759. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8760. }
  8761. else
  8762. {
  8763. HiddenFilesPanel->ViewPriority = 0;
  8764. // not really necessary, just to cleanup no-longer-valid data
  8765. HiddenFilesPanel->Caption = L"";
  8766. }
  8767. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8768. if (FileInfo.FilteredCount > 0)
  8769. {
  8770. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8771. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8772. }
  8773. else
  8774. {
  8775. FilteredFilesPanel->ViewPriority = 0;
  8776. // not really necessary, just to cleanup no-longer-valid data
  8777. FilteredFilesPanel->Caption = L"";
  8778. }
  8779. }
  8780. //---------------------------------------------------------------------------
  8781. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8782. TObject * /*Sender*/)
  8783. {
  8784. if (DirView(osOther)->Enabled)
  8785. {
  8786. DirView(osOther)->SetFocus();
  8787. }
  8788. }
  8789. //---------------------------------------------------------------------------
  8790. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8791. {
  8792. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8793. switch (Config.Show)
  8794. {
  8795. case qvShow:
  8796. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8797. {
  8798. Config.Show = qvHide;
  8799. }
  8800. else
  8801. {
  8802. Config.Show = Config.LastHideShow;
  8803. }
  8804. break;
  8805. case qvHideWhenEmpty:
  8806. if (ComponentVisible[fcQueueView])
  8807. {
  8808. Config.Show = qvHide;
  8809. }
  8810. else
  8811. {
  8812. Config.LastHideShow = Config.Show;
  8813. Config.Show = qvShow;
  8814. }
  8815. break;
  8816. case qvHide:
  8817. Config.LastHideShow = Config.Show;
  8818. Config.Show = qvShow;
  8819. break;
  8820. }
  8821. WinConfiguration->QueueView = Config;
  8822. }
  8823. //---------------------------------------------------------------------------
  8824. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8825. {
  8826. UnicodeString Result;
  8827. if (Terminal != NULL)
  8828. {
  8829. switch (WinConfiguration->PathInCaption)
  8830. {
  8831. case picShort:
  8832. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8833. break;
  8834. case picFull:
  8835. Result = Terminal->CurrentDirectory;
  8836. break;
  8837. }
  8838. }
  8839. return Result;
  8840. }
  8841. //---------------------------------------------------------------------------
  8842. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8843. {
  8844. TColor Result = GetWindowColor(FSessionColor);
  8845. return Result;
  8846. }
  8847. //---------------------------------------------------------------------------
  8848. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8849. {
  8850. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8851. return Result;
  8852. }
  8853. //---------------------------------------------------------------------------
  8854. void TCustomScpExplorerForm::UpdatePanelControls(TCustomDirView * ADirView, TCustomDriveView * ADriveView)
  8855. {
  8856. bool UseDarkTheme = WinConfiguration->UseDarkTheme();
  8857. ADirView->DarkMode = UseDarkTheme;
  8858. ADriveView->DarkMode = UseDarkTheme;
  8859. }
  8860. //---------------------------------------------------------------------------
  8861. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8862. {
  8863. if (!FSessionChanging)
  8864. {
  8865. TTerminalManager::Instance()->UpdateAppTitle();
  8866. }
  8867. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8868. // background to gray on Windows 7 (works on Windows 10).
  8869. // See also EnableControl
  8870. if (Showing)
  8871. {
  8872. if (ManagedSession != NULL)
  8873. {
  8874. // Update path when it changes
  8875. if ((SessionsPageControl->ActivePage != NULL) &&
  8876. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8877. {
  8878. UpdateSessionTab(SessionsPageControl->ActivePage);
  8879. }
  8880. }
  8881. NonVisualDataModule->ReconnectSessionAction->Update();
  8882. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8883. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8884. CenterReconnectToolbar();
  8885. bool HasTerminal = HasActiveTerminal();
  8886. RemoteDirView->Enabled = HasTerminal;
  8887. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8888. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8889. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8890. RemoteDriveView->Color = RemoteDirView->Color;
  8891. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8892. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8893. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8894. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8895. QueueFileList->Enabled = QueueView3->Enabled;
  8896. QueueFileList->Color = QueueView3->Color;
  8897. QueueFileList->Font->Color = QueueView3->Font->Color;
  8898. QueueLabelUpdateStatus();
  8899. UpdatePanelControls(RemoteDirView, RemoteDriveView);
  8900. bool UseDarkTheme = WinConfiguration->UseDarkTheme();
  8901. if (FImmersiveDarkMode != UseDarkTheme)
  8902. {
  8903. UpdateDarkMode();
  8904. }
  8905. SessionsPageControl->TabTheme = UseDarkTheme ? CurrentTheme : NULL;
  8906. UnicodeString CurrentHiContrastThemeName = (FHiContrastTheme != NULL) ? FHiContrastTheme->Name : EmptyStr;
  8907. bool HiContrast = WinConfiguration->HiContrast;
  8908. UnicodeString NewHiContrastThemeName = HiContrast ? GetThemeName(!UseDarkTheme) : EmptyStr;
  8909. if (CurrentHiContrastThemeName != NewHiContrastThemeName)
  8910. {
  8911. ReleaseHiContrastTheme();
  8912. if (!NewHiContrastThemeName.IsEmpty())
  8913. {
  8914. FHiContrastTheme = GetTBXTheme(NewHiContrastThemeName);
  8915. }
  8916. }
  8917. SessionsPageControl->ActiveTabTheme = HiContrast ? DebugNotNull(FHiContrastTheme) : NULL;
  8918. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8919. MessageDock->Visible = (MessageDock->ToolbarCount > 0);
  8920. }
  8921. }
  8922. //---------------------------------------------------------------------------
  8923. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8924. {
  8925. UpdateControls();
  8926. }
  8927. //---------------------------------------------------------------------------
  8928. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8929. TObject * Sender)
  8930. {
  8931. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8932. DebugAssert(DirView != NULL);
  8933. DoDirViewLoaded(DirView);
  8934. TransferPresetAutoSelect();
  8935. TUnixDirView * UnixDirView = dynamic_cast<TUnixDirView *>(DirView);
  8936. if (UnixDirView != NULL)
  8937. {
  8938. TManagedTerminal * ATerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(UnixDirView->Terminal));
  8939. TTerminalManager::Instance()->TerminalLoadedDirectory(ATerminal);
  8940. }
  8941. }
  8942. //---------------------------------------------------------------------------
  8943. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8944. {
  8945. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8946. // first run after installation
  8947. if (double(Updates.LastCheck) == 0)
  8948. {
  8949. AppLog(L"First run, scheduling updates check for the next run");
  8950. // make sure next time there will be an update (if enabled)
  8951. Updates.LastCheck = TDateTime(1);
  8952. WinConfiguration->Updates = Updates;
  8953. }
  8954. else
  8955. {
  8956. TDateTime Period;
  8957. if (IsUWP() && !IsOfficialPackage())
  8958. {
  8959. Period = DefaultUpdatesPeriod;
  8960. AppLog(L"Thirdparty UWP package, using default updates check period");
  8961. }
  8962. else
  8963. {
  8964. Period = Updates.Period;
  8965. }
  8966. AppLogFmt(L"Updates check period: %.2f", (double(Period)));
  8967. if (double(Period) > 0)
  8968. {
  8969. TDateTime Interval = Now() - Updates.LastCheck;
  8970. AppLogFmt(L"Interval since the last updates check: %.2f", (double(Interval)));
  8971. if (Interval >= Period)
  8972. {
  8973. StartUpdateThread(UpdatesChecked);
  8974. }
  8975. }
  8976. }
  8977. }
  8978. //---------------------------------------------------------------------------
  8979. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8980. {
  8981. AppLog(L"Updates check done");
  8982. UnicodeString Message;
  8983. bool New;
  8984. TQueryType Type;
  8985. GetUpdatesMessage(Message, New, Type, false);
  8986. if (!Message.IsEmpty())
  8987. {
  8988. AppLogFmt(L"Updates check message: %s", (Message));
  8989. if (New)
  8990. {
  8991. AppLog(L"New version detected");
  8992. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8993. }
  8994. if (!New && (Type != qtWarning))
  8995. {
  8996. AppLog(L"Posting non-critical updates note");
  8997. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked);
  8998. }
  8999. else
  9000. {
  9001. AppLog(L"Posting new version or critical message notification");
  9002. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  9003. }
  9004. if (New)
  9005. {
  9006. Configuration->Usage->Inc(L"UpdateNotifications");
  9007. }
  9008. }
  9009. }
  9010. //---------------------------------------------------------------------------
  9011. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  9012. {
  9013. RestoreApp();
  9014. bool CanDisplay = !NonVisualDataModule->Busy;
  9015. if (!CanDisplay && (Screen->ActiveForm != NULL))
  9016. {
  9017. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  9018. }
  9019. if (CanDisplay)
  9020. {
  9021. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  9022. // Probably not needed as CheckForUpdates is blocking since we started using neon for HTTP implementation.
  9023. NonVisualDataModule->StartBusy();
  9024. try
  9025. {
  9026. CheckForUpdates(true);
  9027. }
  9028. __finally
  9029. {
  9030. NonVisualDataModule->EndBusy();
  9031. }
  9032. }
  9033. else
  9034. {
  9035. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  9036. }
  9037. }
  9038. //---------------------------------------------------------------------------
  9039. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  9040. TCopyParamRuleData & Data)
  9041. {
  9042. DebugAssert(Terminal != NULL);
  9043. Data.HostName = Terminal->SessionData->HostNameExpanded;
  9044. Data.UserName = Terminal->SessionData->UserNameExpanded;
  9045. Data.RemoteDirectory = RemoteDirView->PathName;
  9046. }
  9047. //---------------------------------------------------------------------------
  9048. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  9049. {
  9050. // Terminal can be null when we are changing local directory implicitly,
  9051. // because it has been deleted, while no session is connected
  9052. // (Login dialog is open)
  9053. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  9054. {
  9055. DebugAssert(!IsLocalBrowserMode());
  9056. TCopyParamRuleData Data;
  9057. GetTransferPresetAutoSelectData(Data);
  9058. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  9059. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  9060. if (CopyParamIndex < 0)
  9061. {
  9062. // there is no preset that matches autoselection
  9063. // set preset that we consider "default"
  9064. FCopyParamAutoSelected = L""; // forget last autoselected preset
  9065. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  9066. }
  9067. else
  9068. {
  9069. // there is preset matching autoselection
  9070. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  9071. if (CopyParamName == FCopyParamAutoSelected)
  9072. {
  9073. // autoselected the same preset as the last time
  9074. // make no change (i.e. preserve custom user preset, if any)
  9075. }
  9076. else
  9077. {
  9078. // autoselected the different preset then the last time (or there
  9079. // was default preset set)
  9080. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  9081. GUIConfiguration->CopyParamCurrent = CopyParamName;
  9082. }
  9083. }
  9084. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  9085. {
  9086. if (CopyParamIndex >= 0)
  9087. {
  9088. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  9089. }
  9090. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  9091. try
  9092. {
  9093. int Fmt =
  9094. (CopyParamIndex < 0) ?
  9095. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  9096. COPY_PARAM_AUTOSELECTED;
  9097. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  9098. Data->Message = MainInstructions(Message);
  9099. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  9100. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  9101. CopyParamAttrs);
  9102. if (CopyParamIndex >= 0)
  9103. {
  9104. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  9105. Info = FORMAT(L"%s\n \n%s", (Info,
  9106. FMTLOAD(COPY_PARAM_RULE,
  9107. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  9108. }
  9109. Data->Message += L"\n\n" + Info;
  9110. if (WinConfiguration->CopyParamAutoSelectNotice)
  9111. {
  9112. TransferPresetNoteMessage(Data, true);
  9113. }
  9114. else
  9115. {
  9116. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  9117. Data = NULL; // ownership passed
  9118. }
  9119. }
  9120. __finally
  9121. {
  9122. delete Data;
  9123. }
  9124. }
  9125. }
  9126. }
  9127. //---------------------------------------------------------------------------
  9128. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  9129. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  9130. {
  9131. DebugAssert(NoteData != NULL);
  9132. DebugAssert(!IsLocalBrowserMode());
  9133. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  9134. TQueryButtonAlias Aliases[1];
  9135. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  9136. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  9137. Params.Aliases = Aliases;
  9138. Params.AliasesCount = LENOF(Aliases);
  9139. unsigned int Result =
  9140. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  9141. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  9142. switch (Result)
  9143. {
  9144. case qaNeverAskAgain:
  9145. DebugAssert(AllowNeverAskAgain);
  9146. WinConfiguration->CopyParamAutoSelectNotice = false;
  9147. break;
  9148. case qaIgnore:
  9149. PreferencesDialog(pmPresets);
  9150. break;
  9151. }
  9152. }
  9153. //---------------------------------------------------------------------------
  9154. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  9155. {
  9156. // as of now useless, as this is used for notes only, never for balloons, ...
  9157. RestoreApp();
  9158. // .. and we should never be busy here
  9159. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  9160. {
  9161. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  9162. }
  9163. }
  9164. //---------------------------------------------------------------------------
  9165. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  9166. TPreferencesMode APreferencesMode)
  9167. {
  9168. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  9169. TCopyParamRuleData Data;
  9170. if (Terminal != NULL)
  9171. {
  9172. PreferencesData.reset(new TPreferencesDialogData());
  9173. GetTransferPresetAutoSelectData(Data);
  9174. PreferencesData->CopyParamRuleData = &Data;
  9175. }
  9176. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  9177. }
  9178. //---------------------------------------------------------------------------
  9179. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  9180. const TCustomCommandType & Command)
  9181. {
  9182. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  9183. {
  9184. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  9185. }
  9186. }
  9187. //---------------------------------------------------------------------------
  9188. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  9189. {
  9190. bool RemoteAllowed = CustomCommandRemoteAllowed();
  9191. TCustomCommandType Command;
  9192. // make sure we use local custom command when remote are not supported
  9193. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  9194. {
  9195. Command = FLastCustomCommand;
  9196. }
  9197. else
  9198. {
  9199. Command.Params = Command.Params | ccLocal;
  9200. }
  9201. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  9202. FEditingFocusedAdHocCommand = OnFocused;
  9203. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  9204. int Options =
  9205. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  9206. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  9207. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  9208. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  9209. {
  9210. FLastCustomCommand = Command;
  9211. UpdateCustomCommandsToolbar();
  9212. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  9213. }
  9214. }
  9215. //---------------------------------------------------------------------------
  9216. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  9217. {
  9218. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  9219. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  9220. DebugAssert(State > 0);
  9221. if (State <= 0)
  9222. {
  9223. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  9224. }
  9225. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  9226. }
  9227. //---------------------------------------------------------------------------
  9228. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  9229. TCustomCommandType & Command, int & State)
  9230. {
  9231. bool Result = !FLastCustomCommand.Command.IsEmpty();
  9232. if (Result)
  9233. {
  9234. Command = FLastCustomCommand;
  9235. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  9236. }
  9237. return Result;
  9238. }
  9239. //---------------------------------------------------------------------------
  9240. void __fastcall TCustomScpExplorerForm::BeforeAction()
  9241. {
  9242. ResetIncrementalSearch();
  9243. }
  9244. //---------------------------------------------------------------------------
  9245. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  9246. {
  9247. DebugAssert(ComponentVisible[Component]);
  9248. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  9249. }
  9250. //---------------------------------------------------------------------------
  9251. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  9252. {
  9253. // when queue panel is resized here directly, the status bar is stretched
  9254. // over whole space the panel occupied
  9255. PostComponentHide(fcQueueView);
  9256. }
  9257. //---------------------------------------------------------------------------
  9258. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  9259. {
  9260. ComponentVisible[fcQueueFileList] = false;
  9261. }
  9262. //---------------------------------------------------------------------------
  9263. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  9264. {
  9265. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  9266. RefreshColorMode();
  9267. ResetSysDarkTheme();
  9268. ConfigurationChanged();
  9269. ConfigureInterface();
  9270. // Should be called for all controls
  9271. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  9272. }
  9273. //---------------------------------------------------------------------------
  9274. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  9275. {
  9276. // Do not handle, when shutting down anyway (maybe also when not setup completely yet?)
  9277. if (!FInvalid &&
  9278. (Message.LParam != 0) &&
  9279. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  9280. {
  9281. ThemeChanged();
  9282. }
  9283. TForm::Dispatch(&Message);
  9284. }
  9285. //---------------------------------------------------------------------------
  9286. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  9287. {
  9288. TMessage * M = static_cast<TMessage*>(Message);
  9289. switch (M->Msg)
  9290. {
  9291. case CM_DIALOGCHAR:
  9292. CMDialogChar(*M);
  9293. break;
  9294. case WM_APPCOMMAND:
  9295. WMAppCommand(*M);
  9296. break;
  9297. case WM_SYSCOMMAND:
  9298. WMSysCommand(*M);
  9299. break;
  9300. case WM_QUERYENDSESSION:
  9301. WMQueryEndSession(*M);
  9302. break;
  9303. case WM_ENDSESSION:
  9304. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  9305. break;
  9306. #ifdef _DEBUG
  9307. case WM_WINDOWPOSCHANGED:
  9308. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  9309. break;
  9310. case CM_CUSTOMSTYLECHANGED:
  9311. TForm::Dispatch(Message);
  9312. break;
  9313. #endif
  9314. case WM_COMPONENT_HIDE:
  9315. {
  9316. Byte Component = static_cast<Byte>(M->WParam);
  9317. // sanity check
  9318. if (ComponentVisible[Component])
  9319. {
  9320. // special treatment
  9321. if (Component == fcQueueView)
  9322. {
  9323. ToggleQueueVisibility();
  9324. DebugAssert(!ComponentVisible[fcQueueView]);
  9325. }
  9326. else
  9327. {
  9328. ComponentVisible[Component] = false;
  9329. }
  9330. }
  9331. }
  9332. break;
  9333. case WM_COPYDATA:
  9334. WMCopyData(*M);
  9335. break;
  9336. case WM_MANAGES_CAPTION:
  9337. // caption managed in TTerminalManager::UpdateAppTitle()
  9338. M->Result = 1;
  9339. break;
  9340. case WM_WANTS_MOUSEWHEEL:
  9341. M->Result = 1;
  9342. break;
  9343. case CM_SHOWINGCHANGED:
  9344. CMShowingChanged(*M);
  9345. break;
  9346. case WM_CLOSE:
  9347. WMClose(*M);
  9348. break;
  9349. case WM_DPICHANGED:
  9350. WMDpiChanged(*M);
  9351. break;
  9352. case WM_SETTINGCHANGE:
  9353. WMSettingChange(*M);
  9354. break;
  9355. case CM_DIALOGKEY:
  9356. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  9357. break;
  9358. case WM_PASTE_FILES:
  9359. PasteFiles();
  9360. break;
  9361. case WM_IS_HIDDEN:
  9362. // To treat master password prompt as a main window while showing it when workspace is loading while
  9363. // the exporer is showing up (Visible is true already)
  9364. M->Result = !FShowing;
  9365. break;
  9366. case WM_SHOWWINDOW:
  9367. if (M->WParam)
  9368. {
  9369. AddStartupSequence(L"J");
  9370. }
  9371. TForm::Dispatch(Message);
  9372. break;
  9373. case WM_QUEUE_CALLBACK:
  9374. WMQueueCallback(*M);
  9375. break;
  9376. default:
  9377. TForm::Dispatch(Message);
  9378. break;
  9379. }
  9380. }
  9381. //---------------------------------------------------------------------------
  9382. void __fastcall TCustomScpExplorerForm::UpdateImages()
  9383. {
  9384. // noop
  9385. }
  9386. //---------------------------------------------------------------------------
  9387. void TCustomScpExplorerForm::RegenerateSessionColorsImageList()
  9388. {
  9389. FSessionColors->Clear();
  9390. AddFixedSessionImages();
  9391. ::RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  9392. UpdateImages();
  9393. }
  9394. //---------------------------------------------------------------------------
  9395. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  9396. {
  9397. TForm::Dispatch(&Message);
  9398. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  9399. UpdatePixelsPerInchMainWindowCounter();
  9400. }
  9401. //---------------------------------------------------------------------------
  9402. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  9403. {
  9404. AppLog(L"Main window WM_CLOSE");
  9405. // Cannot close window while we are busy.
  9406. // We cannot test this in FormCloseQuery as that is called also from
  9407. // Close(), which is called by CloseApplicationAction. So we can be busy
  9408. // there already even, when it is legitimate to close the application.
  9409. // Possibly a better place to handle this would be WMSysCommand.
  9410. if (NonVisualDataModule->Busy)
  9411. {
  9412. Message.Result = 1;
  9413. }
  9414. else
  9415. {
  9416. TForm::Dispatch(&Message);
  9417. }
  9418. }
  9419. //---------------------------------------------------------------------------
  9420. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  9421. {
  9422. if (!FShowingChanged)
  9423. {
  9424. AddStartupSequence(L"D");
  9425. FShowingChanged = true;
  9426. }
  9427. // Ignoring implicit local-local browser of Commander, opened from DoShow
  9428. bool NoSession = (ManagedSession == NULL);
  9429. // This calls TCustomScpExplorerForm::DoShow
  9430. TForm::Dispatch(&Message);
  9431. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  9432. if (Showing && !FStarted)
  9433. {
  9434. FStarted = true;
  9435. InterfaceStarted();
  9436. }
  9437. if (Showing && NoSession)
  9438. {
  9439. // When we are starting minimized (i.e. from an installer),
  9440. // postpone showing Login dialog until we get restored.
  9441. // Otherwise the Login dialog (and Authentication window) show restored
  9442. // over invisible (minimized) main window.
  9443. if (WindowState == wsMinimized)
  9444. {
  9445. FNeedSession = true;
  9446. }
  9447. else
  9448. {
  9449. // This happens before application ever goes idle, so the toolbars would
  9450. // stay enabled (initial state) until the Login dialog is dismissed.
  9451. UpdateActions();
  9452. NonVisualDataModule->StartBusy();
  9453. try
  9454. {
  9455. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  9456. // otherwise the dialog does not receive focus.
  9457. // With Commander interface the ProcessMessages is called already
  9458. // by TDriveView, but with Explorer interface, we need to call it explicily
  9459. Application->ProcessMessages();
  9460. // do not reload sessions, they have been loaded just now (optimization)
  9461. NeedSession(true);
  9462. }
  9463. __finally
  9464. {
  9465. NonVisualDataModule->EndBusy();
  9466. }
  9467. }
  9468. }
  9469. }
  9470. //---------------------------------------------------------------------------
  9471. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  9472. {
  9473. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  9474. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  9475. }
  9476. //---------------------------------------------------------------------------
  9477. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  9478. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  9479. int & MaxHeight)
  9480. {
  9481. // workaround for bug in TWinControl.CalcConstraints
  9482. // Check for empty rect (restore from iconic state) is done there only after
  9483. // call to AdjustClientRect, which enlarges the rect (for forms).
  9484. TRect R = GetClientRect();
  9485. // when restoring from iconic state, place no restrictions
  9486. if (IsRectEmpty(R))
  9487. {
  9488. MinWidth = 0;
  9489. MinHeight = 0;
  9490. MaxWidth = 32000;
  9491. MaxHeight = 32000;
  9492. }
  9493. }
  9494. //---------------------------------------------------------------------------
  9495. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  9496. TSpaceAvailable & ASpaceAvailable, bool & Close)
  9497. {
  9498. // terminal can be already closed (e.g. dropped connection)
  9499. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  9500. {
  9501. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  9502. try
  9503. {
  9504. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  9505. }
  9506. catch(...)
  9507. {
  9508. if (!Terminal->Active)
  9509. {
  9510. Close = true;
  9511. }
  9512. throw;
  9513. }
  9514. }
  9515. }
  9516. //---------------------------------------------------------------------------
  9517. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  9518. {
  9519. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  9520. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  9521. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  9522. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  9523. {
  9524. OnGetSpaceAvailable = GetSpaceAvailable;
  9525. }
  9526. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  9527. OnGetSpaceAvailable);
  9528. }
  9529. //---------------------------------------------------------------------------
  9530. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  9531. {
  9532. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  9533. Terminal->FillSessionDataForCode(Data.get());
  9534. return Data.release();
  9535. }
  9536. //---------------------------------------------------------------------------
  9537. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  9538. {
  9539. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  9540. DoGenerateUrlDialog(Data.get(), Paths);
  9541. }
  9542. //---------------------------------------------------------------------------
  9543. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  9544. {
  9545. GenerateUrl(NULL);
  9546. }
  9547. //---------------------------------------------------------------------------
  9548. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  9549. {
  9550. std::unique_ptr<TStrings> Paths(new TStringList());
  9551. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  9552. GenerateUrl(Paths.get());
  9553. }
  9554. //---------------------------------------------------------------------------
  9555. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  9556. {
  9557. FSessionColor = value;
  9558. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  9559. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  9560. static_cast<TObject *>(GetComponent(fcColorMenu)));
  9561. DebugAssert(ColorItem != NULL);
  9562. ColorItem->Color = C;
  9563. NonVisualDataModule->ColorMenuItem->Color = C;
  9564. UpdateControls();
  9565. }
  9566. //---------------------------------------------------------------------------
  9567. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  9568. {
  9569. if (value != FSessionColor)
  9570. {
  9571. UpdateSessionColor(value);
  9572. // Is null when called from LastTerminalClosed
  9573. if (Terminal != NULL)
  9574. {
  9575. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  9576. }
  9577. }
  9578. }
  9579. //---------------------------------------------------------------------------
  9580. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  9581. {
  9582. bool Result = FNoteTimer->Enabled;
  9583. if (Result)
  9584. {
  9585. // cannot cancel note too early
  9586. bool NotEarly =
  9587. (Now() - FNoteShown >
  9588. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  9589. if (Force || NotEarly)
  9590. {
  9591. FNoteTimer->Enabled = false;
  9592. FNote = L"";
  9593. SAFE_DESTROY(FNoteData);
  9594. FOnNoteClick = NULL;
  9595. FNoteHints = FNotes->Text;
  9596. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  9597. UpdateStatusBar();
  9598. }
  9599. }
  9600. return Result;
  9601. }
  9602. //---------------------------------------------------------------------------
  9603. void TCustomScpExplorerForm::CancelNote(const UnicodeString & Note)
  9604. {
  9605. if (FNote == Note)
  9606. {
  9607. CancelNote(true);
  9608. }
  9609. }
  9610. //---------------------------------------------------------------------------
  9611. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  9612. {
  9613. DebugAssert(FNoteTimer->Enabled);
  9614. CancelNote(true);
  9615. }
  9616. //---------------------------------------------------------------------------
  9617. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9618. {
  9619. FNoteHints = FNotes->Text.TrimRight();
  9620. }
  9621. //---------------------------------------------------------------------------
  9622. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9623. {
  9624. int P = Note.Pos(L"\n");
  9625. if (P > 0)
  9626. {
  9627. Note.SetLength(P - 1);
  9628. }
  9629. FNotes->Add(FORMAT(L"[%s] %s",
  9630. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9631. while (FNotes->Count > 10)
  9632. {
  9633. FNotes->Delete(0);
  9634. }
  9635. if (UpdateNow)
  9636. {
  9637. UpdateNoteHints();
  9638. UpdateStatusBar();
  9639. }
  9640. }
  9641. //---------------------------------------------------------------------------
  9642. void TCustomScpExplorerForm::PostNote(
  9643. const UnicodeString & ANote, unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9644. {
  9645. UnicodeString Note = ANote;
  9646. int P = Note.Pos(L"\n");
  9647. if (P > 0)
  9648. {
  9649. Note.SetLength(P - 1);
  9650. }
  9651. UpdateNoteHints();
  9652. FNote = Note;
  9653. SAFE_DESTROY(FNoteData);
  9654. FNoteData = NoteData;
  9655. FOnNoteClick = OnNoteClick;
  9656. AddNote(Note, false);
  9657. UpdateStatusBar();
  9658. FNoteShown = Now();
  9659. FNoteTimer->Enabled = false;
  9660. if (Seconds == 0)
  9661. {
  9662. Seconds = WinConfiguration->NotificationsTimeout;
  9663. }
  9664. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9665. FNoteTimer->Enabled = true;
  9666. }
  9667. //---------------------------------------------------------------------------
  9668. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9669. {
  9670. PostNote(LoadStr(DIRECTORY_READING_CANCELLED));
  9671. }
  9672. //---------------------------------------------------------------------------
  9673. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9674. {
  9675. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9676. {
  9677. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9678. }
  9679. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9680. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9681. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))));
  9682. }
  9683. //---------------------------------------------------------------------------
  9684. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9685. {
  9686. bool NewShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9687. UnicodeString Note =
  9688. FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1), (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, (NewShowHiddenFiles ? 2 : 3))));
  9689. PostNote(Note);
  9690. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9691. WinConfiguration->ShowHiddenFiles = NewShowHiddenFiles;
  9692. }
  9693. //---------------------------------------------------------------------------
  9694. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9695. {
  9696. WinConfiguration->FormatSizeBytes = Style;
  9697. }
  9698. //---------------------------------------------------------------------------
  9699. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9700. {
  9701. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9702. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9703. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9704. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))));
  9705. }
  9706. //---------------------------------------------------------------------------
  9707. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9708. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9709. {
  9710. if (Panel->Index == 0)
  9711. {
  9712. if (FOnNoteClick != NULL)
  9713. {
  9714. // prevent the user data from being freed by possible call
  9715. // to CancelNote or PostNote during call to OnNoteClick
  9716. std::unique_ptr<TObject> NoteData(FNoteData);
  9717. TNotifyEvent OnNoteClick = FOnNoteClick;
  9718. FNoteData = NULL;
  9719. // need to cancel the note as we are going to delete its user data
  9720. CancelNote(true);
  9721. OnNoteClick(NoteData.get());
  9722. }
  9723. }
  9724. if (HasActiveTerminal() &&
  9725. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9726. {
  9727. FileSystemInfo();
  9728. }
  9729. }
  9730. //---------------------------------------------------------------------------
  9731. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9732. {
  9733. // workaround:
  9734. // 1) for unknown reason, disabling window, while minimized,
  9735. // prevents it from restoring, even if it was enabled again meanwhile
  9736. // 2) when disabling the main window, while another has focus
  9737. // minimize is no longer possible
  9738. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9739. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9740. // Shouldn't we use IsApplicationMinimized() here?
  9741. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9742. {
  9743. Enabled = false;
  9744. }
  9745. FLockLevel++;
  9746. }
  9747. //---------------------------------------------------------------------------
  9748. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9749. {
  9750. DebugAssert(FLockLevel > 0);
  9751. FLockLevel--;
  9752. if (FLockLevel == 0)
  9753. {
  9754. DebugAssert(FLockSuspendLevel == 0);
  9755. Enabled = true;
  9756. // VCL_COPY (TCustomForm.SetWindowFocus)
  9757. if (Active && (ActiveControl != NULL))
  9758. {
  9759. ::SetFocus(ActiveControl->Handle);
  9760. if (GetFocus() == ActiveControl->Handle)
  9761. {
  9762. ActiveControl->Perform(CM_UIACTIVATE, 0, 0);
  9763. }
  9764. }
  9765. }
  9766. }
  9767. //---------------------------------------------------------------------------
  9768. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9769. {
  9770. // We need to make sure that window is enabled when the last modal window closes
  9771. // otherwise focus is not restored correctly.
  9772. // So we re-enable the window when modal window opens and
  9773. // disable it back after is closes
  9774. if (FLockLevel > 0)
  9775. {
  9776. // while we have nesting counter, we know that we never be called
  9777. // recursivelly as Application->OnModalBegin is called only
  9778. // for the top-level modal window
  9779. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9780. {
  9781. // won't be disabled when conditions in LockWindow() were not satisfied
  9782. FDisabledOnLockSuspend = !Enabled;
  9783. // When minimized to tray (or actually when set to SW_HIDE),
  9784. // setting Enabled makes the window focusable even when there's
  9785. // modal window over it
  9786. if (!FTrayIcon->Visible)
  9787. {
  9788. Enabled = true;
  9789. }
  9790. }
  9791. FLockSuspendLevel++;
  9792. }
  9793. }
  9794. //---------------------------------------------------------------------------
  9795. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9796. {
  9797. if (FLockSuspendLevel > 0)
  9798. {
  9799. DebugAssert(FLockLevel > 0);
  9800. FLockSuspendLevel--;
  9801. // see comment in SuspendWindowLock
  9802. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9803. {
  9804. // Note that window can be enabled here, when we were minized to tray when
  9805. // was SuspendWindowLock() called.
  9806. // We should possibly do the same check as in LockWindow(),
  9807. // if it is ever possible that the consitions change between
  9808. // SuspendWindowLock() and ResumeWindowLock()
  9809. Enabled = !FDisabledOnLockSuspend;
  9810. }
  9811. }
  9812. }
  9813. //---------------------------------------------------------------------------
  9814. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9815. {
  9816. TTBXComboBoxItem * RemotePathComboBox =
  9817. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9818. if (!TextOnly)
  9819. {
  9820. TStrings * Items = RemotePathComboBox->Strings;
  9821. Items->BeginUpdate();
  9822. try
  9823. {
  9824. Items->Clear();
  9825. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9826. {
  9827. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9828. while (!IsUnixRootPath(APath))
  9829. {
  9830. int P = APath.LastDelimiter(L'/');
  9831. DebugAssert(P >= 0);
  9832. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9833. APath.SetLength(P - 1);
  9834. }
  9835. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9836. }
  9837. }
  9838. __finally
  9839. {
  9840. Items->EndUpdate();
  9841. }
  9842. }
  9843. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9844. // Setting ItemIndex to -1 does not reset its text
  9845. if (RemotePathComboBox->Strings->Count == 0)
  9846. {
  9847. RemotePathComboBox->Text = L"";
  9848. }
  9849. }
  9850. //---------------------------------------------------------------------------
  9851. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9852. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9853. {
  9854. if (AIndex < 0)
  9855. {
  9856. AIndex = Sender->ItemIndex;
  9857. }
  9858. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9859. }
  9860. //---------------------------------------------------------------------------
  9861. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9862. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9863. int & ImageIndex)
  9864. {
  9865. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9866. }
  9867. //---------------------------------------------------------------------------
  9868. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9869. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9870. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9871. {
  9872. if (!IsLocalBrowserMode())
  9873. {
  9874. ARect.Left += (10 * AIndex);
  9875. }
  9876. }
  9877. //---------------------------------------------------------------------------
  9878. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9879. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9880. {
  9881. AWidth += (10 * AIndex);
  9882. }
  9883. //---------------------------------------------------------------------------
  9884. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9885. {
  9886. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9887. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9888. int Index = RemotePathComboBox->ItemIndex;
  9889. while (Index < RemotePathComboBox->Strings->Count - 1)
  9890. {
  9891. APath = UnixExtractFileDir(APath);
  9892. Index++;
  9893. }
  9894. // VanDyke style paths
  9895. if (APath.IsEmpty())
  9896. {
  9897. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9898. APath = ROOTDIRECTORY;
  9899. }
  9900. if (RemoteDirView->Path != APath)
  9901. {
  9902. RemoteDirView->Path = APath;
  9903. }
  9904. }
  9905. //---------------------------------------------------------------------------
  9906. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9907. {
  9908. DoRemotePathComboBoxItemClick(Sender);
  9909. }
  9910. //---------------------------------------------------------------------------
  9911. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9912. {
  9913. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9914. DebugUsedParam(Sender);
  9915. UpdateRemotePathComboBox(true);
  9916. }
  9917. //---------------------------------------------------------------------------
  9918. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9919. {
  9920. DoRemotePathComboBoxCancel(Sender);
  9921. }
  9922. //---------------------------------------------------------------------------
  9923. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9924. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9925. {
  9926. ResetIncrementalSearch();
  9927. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9928. DebugAssert(DirView != NULL);
  9929. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9930. {
  9931. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9932. // OnEditing is called also from TCustomListView::CanEdit
  9933. if (Edit != NULL)
  9934. {
  9935. EditSelectBaseName(Edit);
  9936. }
  9937. }
  9938. }
  9939. //---------------------------------------------------------------------------
  9940. //---------------------------------------------------------------------------
  9941. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9942. {
  9943. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9944. Result->AutoDetectDnD = false;
  9945. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9946. Result->RenderDataOn = rdoEnterAndDropSync;
  9947. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9948. return Result;
  9949. }
  9950. //---------------------------------------------------------------------------
  9951. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9952. {
  9953. if (IsWin10Build(19041))
  9954. {
  9955. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9956. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9957. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9958. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9959. }
  9960. }
  9961. //---------------------------------------------------------------------------
  9962. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9963. {
  9964. TForm::CreateWnd();
  9965. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9966. UpdateDarkMode();
  9967. if (FSessionsDragDropFilesEx == NULL)
  9968. {
  9969. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9970. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9971. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9972. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9973. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9974. }
  9975. if (FQueueDragDropFilesEx == NULL)
  9976. {
  9977. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9978. // No need to set OnDragOver as we do not have any restrictions
  9979. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9980. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9981. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9982. }
  9983. }
  9984. //---------------------------------------------------------------------------
  9985. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9986. {
  9987. TForm::DestroyWnd();
  9988. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9989. FQueueDragDropFilesEx->DragDropControl = NULL;
  9990. }
  9991. //---------------------------------------------------------------------------
  9992. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9993. {
  9994. SideEnter(FCurrentSide);
  9995. FEverShown = true;
  9996. }
  9997. //---------------------------------------------------------------------------
  9998. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9999. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  10000. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  10001. {
  10002. if (!NonVisualDataModule->Busy)
  10003. {
  10004. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  10005. Configuration->Usage->Inc(L"FileFinds");
  10006. LockWindow(true);
  10007. NonVisualDataModule->StartBusy();
  10008. WinConfiguration->LockedInterface = true;
  10009. try
  10010. {
  10011. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  10012. }
  10013. __finally
  10014. {
  10015. WinConfiguration->LockedInterface = false;
  10016. NonVisualDataModule->EndBusy();
  10017. UnlockWindow();
  10018. }
  10019. }
  10020. }
  10021. //---------------------------------------------------------------------------
  10022. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  10023. {
  10024. if (!NonVisualDataModule->Busy)
  10025. {
  10026. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  10027. SetFocus();
  10028. RemoteDirView->Path = UnixExtractFilePath(Path);
  10029. UnicodeString FileName = UnixExtractFileName(Path);
  10030. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  10031. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  10032. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  10033. if (Item == NULL)
  10034. {
  10035. ReloadDirectory(osRemote);
  10036. Item = RemoteDirView->FindFileItem(FileName);
  10037. }
  10038. if (Item != NULL)
  10039. {
  10040. RemoteDirView->ItemFocused = Item;
  10041. RemoteDirView->ItemFocused->MakeVisible(false);
  10042. RemoteDirView->SetFocus();
  10043. }
  10044. }
  10045. }
  10046. //---------------------------------------------------------------------------
  10047. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  10048. {
  10049. bool Result = !NonVisualDataModule->Busy;
  10050. if (Result)
  10051. {
  10052. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  10053. }
  10054. return Result;
  10055. }
  10056. //---------------------------------------------------------------------------
  10057. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  10058. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  10059. {
  10060. if (CanOperateOnFoundFiles(ATerminal))
  10061. {
  10062. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished, OnFileOperationFinished);
  10063. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  10064. }
  10065. }
  10066. //---------------------------------------------------------------------------
  10067. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  10068. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  10069. {
  10070. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  10071. }
  10072. //---------------------------------------------------------------------------
  10073. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  10074. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  10075. {
  10076. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  10077. }
  10078. //---------------------------------------------------------------------------
  10079. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  10080. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  10081. {
  10082. if (CanOperateOnFoundFiles(ATerminal))
  10083. {
  10084. for (int Index = 0; Index < FileList->Count; Index++)
  10085. {
  10086. UnicodeString FileName = FileList->Strings[Index];
  10087. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  10088. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  10089. OnFileOperationFinished(osRemote, FileName, true, true);
  10090. }
  10091. }
  10092. }
  10093. //---------------------------------------------------------------------------
  10094. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  10095. {
  10096. FFileFindTerminal = Terminal;
  10097. ShowFileFindDialog(
  10098. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  10099. DoDownloadFoundFiles, DoEditFoundFiles);
  10100. }
  10101. //---------------------------------------------------------------------------
  10102. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  10103. {
  10104. FTaskbarList = TaskbarList;
  10105. }
  10106. //---------------------------------------------------------------------------
  10107. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  10108. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  10109. {
  10110. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  10111. if (Index >= 0)
  10112. {
  10113. if (Button == mbLeft)
  10114. {
  10115. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  10116. // If switching tab (switching session) takes long enough for user
  10117. // to actually release the button, "mouse down" is still raised,
  10118. // but we do not get "mouse up" event, so dragging is not cancelled,
  10119. // prevent that by not beginning dragging in the first place.
  10120. int Key = ((GetSystemMetrics(SM_SWAPBUTTON) == 0) ? VK_LBUTTON : VK_RBUTTON);
  10121. if (IsKeyPressed(Key))
  10122. {
  10123. // when user clicks the "+", we get mouse down only after the session
  10124. // is closed, when new session tab is already on X:Y, so dragging
  10125. // starts, prevent that
  10126. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  10127. {
  10128. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10129. if (Terminal != NULL)
  10130. {
  10131. SessionsPageControl->BeginDrag(false);
  10132. }
  10133. }
  10134. }
  10135. }
  10136. else if (Button == mbMiddle)
  10137. {
  10138. // ignore middle-click for "New session tab" and the last local-local tab
  10139. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10140. if ((Session != NULL) && CanCloseSession(Session))
  10141. {
  10142. CloseSessionTab(Index);
  10143. }
  10144. }
  10145. }
  10146. }
  10147. //---------------------------------------------------------------------------
  10148. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  10149. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  10150. {
  10151. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  10152. // do not allow dropping on the "+" tab
  10153. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10154. if ((TargetTerminal != NULL) &&
  10155. (SessionsPageControl->ActivePage->PageIndex != Index))
  10156. {
  10157. Configuration->Usage->Inc(L"SessionTabMoves");
  10158. // this is almost redundant as we would recreate tabs in SessionListChanged,
  10159. // but we want to actually prevent that to avoid flicker
  10160. SessionsPageControl->ActivePage->PageIndex = Index;
  10161. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  10162. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  10163. }
  10164. }
  10165. //---------------------------------------------------------------------------
  10166. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  10167. TObject * Sender, TObject * Source, int X, int Y,
  10168. TDragState /*State*/, bool & Accept)
  10169. {
  10170. Accept = (Sender == Source);
  10171. if (Accept)
  10172. {
  10173. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  10174. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10175. // do not allow dragging to the "+" tab
  10176. Accept = (Terminal != NULL);
  10177. }
  10178. }
  10179. //---------------------------------------------------------------------------
  10180. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  10181. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  10182. {
  10183. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  10184. if (Index < 0)
  10185. {
  10186. Effect = DROPEFFECT_NONE;
  10187. }
  10188. else
  10189. {
  10190. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10191. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  10192. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  10193. {
  10194. Effect = DROPEFFECT_NONE;
  10195. }
  10196. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  10197. {
  10198. Effect = DROPEFFECT_COPY;
  10199. }
  10200. }
  10201. }
  10202. //---------------------------------------------------------------------------
  10203. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  10204. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  10205. {
  10206. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  10207. // do not allow dropping on local-local, disconnected and the "+" tab
  10208. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  10209. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10210. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  10211. {
  10212. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  10213. if (!IsFileControl(DropSourceControl, osRemote))
  10214. {
  10215. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  10216. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  10217. // Why don't we use Terminal->CurrentDirectory directly?
  10218. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  10219. // do not force queue, drag drop
  10220. false, true);
  10221. }
  10222. }
  10223. }
  10224. //---------------------------------------------------------------------------
  10225. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  10226. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  10227. {
  10228. // Downloads are handled in RemoteFileControlDDEnd
  10229. if (!IsFileControl(DropSourceControl, osRemote))
  10230. {
  10231. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  10232. Terminal->CurrentDirectory,
  10233. // force queue, drag drop
  10234. true, true);
  10235. }
  10236. }
  10237. //---------------------------------------------------------------------------
  10238. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  10239. {
  10240. FShowing = false;
  10241. // Do not save empty workspace
  10242. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  10243. {
  10244. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  10245. UnicodeString Name = WorkspaceName();
  10246. DoSaveWorkspace(
  10247. Name, DataList.get(),
  10248. !Configuration->DisablePasswordStoring &&
  10249. WinConfiguration->AutoSaveWorkspacePasswords, false);
  10250. WinConfiguration->LastStoredSession = Name;
  10251. }
  10252. AppLog(L"Closing main window");
  10253. }
  10254. //---------------------------------------------------------------------------
  10255. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  10256. {
  10257. TManagedTerminal * ManagedTerminal =
  10258. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  10259. if (ManagedTerminal != NULL)
  10260. {
  10261. ManagedTerminal->DirectoryLoaded = Now();
  10262. }
  10263. }
  10264. //---------------------------------------------------------------------------
  10265. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  10266. TListItem * /*Item*/, bool /*Selected*/)
  10267. {
  10268. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  10269. switch (DirView->LastSelectMethod)
  10270. {
  10271. case smKeyboard:
  10272. Configuration->Usage->Inc(L"KeyboardSelections");
  10273. break;
  10274. case smMouse:
  10275. Configuration->Usage->Inc(L"MouseSelections");
  10276. break;
  10277. }
  10278. }
  10279. //---------------------------------------------------------------------------
  10280. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  10281. {
  10282. if (Color != 0)
  10283. {
  10284. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  10285. }
  10286. else
  10287. {
  10288. return FSessionTabImageIndex;
  10289. }
  10290. }
  10291. //---------------------------------------------------------------------------
  10292. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  10293. {
  10294. TPngImageCollectionItem * Item =
  10295. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  10296. FSessionColors->AddPng(Item->PngImage);
  10297. return FSessionColors->Count - 1;
  10298. }
  10299. //---------------------------------------------------------------------------
  10300. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  10301. {
  10302. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  10303. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  10304. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  10305. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  10306. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  10307. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  10308. }
  10309. //---------------------------------------------------------------------------
  10310. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  10311. {
  10312. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  10313. }
  10314. //---------------------------------------------------------------------------
  10315. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  10316. {
  10317. switch (Side)
  10318. {
  10319. case osLocal:
  10320. return FNewLocalTabTabImageIndex;
  10321. case osRemote:
  10322. case osCurrent:
  10323. return FNewRemoteTabTabImageIndex;
  10324. default:
  10325. DebugFail();
  10326. return -1;
  10327. }
  10328. }
  10329. //---------------------------------------------------------------------------
  10330. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  10331. {
  10332. for (int Index = 0; Index < Control->ControlCount; Index++)
  10333. {
  10334. TControl * AControl = Control->Controls[Index];
  10335. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  10336. if (WinControl != NULL)
  10337. {
  10338. CollectItemsWithTextDisplayMode(WinControl);
  10339. }
  10340. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  10341. if (Toolbar != NULL)
  10342. {
  10343. // we care for top-level items only
  10344. TTBCustomItem * Items = Toolbar->Items;
  10345. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  10346. {
  10347. TTBCustomItem * Item = Items->Items[ItemIndex];
  10348. if (((Item->DisplayMode == nbdmImageAndText) ||
  10349. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  10350. EligibleForImageDisplayMode(Item))
  10351. {
  10352. FItemsWithTextDisplayMode.insert(Item);
  10353. }
  10354. }
  10355. }
  10356. }
  10357. }
  10358. //---------------------------------------------------------------------------
  10359. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  10360. {
  10361. return true;
  10362. }
  10363. //---------------------------------------------------------------------------
  10364. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  10365. {
  10366. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  10367. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  10368. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  10369. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  10370. bool Result = true;
  10371. while (Result && (i != FItemsWithTextDisplayMode.end()))
  10372. {
  10373. TTBCustomItem * Item = *i;
  10374. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  10375. if (Label != NULL)
  10376. {
  10377. // optimization
  10378. if (Label->Visible == SelectiveToolbarText)
  10379. {
  10380. Result = false;
  10381. }
  10382. else
  10383. {
  10384. Label->Visible = SelectiveToolbarText;
  10385. }
  10386. }
  10387. else
  10388. {
  10389. // optimization
  10390. if (Item->DisplayMode == DisplayMode)
  10391. {
  10392. Result = false;
  10393. }
  10394. else
  10395. {
  10396. Item->DisplayMode = DisplayMode;
  10397. }
  10398. }
  10399. ++i;
  10400. }
  10401. UpdateNewTabTab();
  10402. return Result;
  10403. }
  10404. //---------------------------------------------------------------------------
  10405. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  10406. {
  10407. DebugFail();
  10408. }
  10409. //---------------------------------------------------------------------------
  10410. bool __fastcall TCustomScpExplorerForm::IsBusy()
  10411. {
  10412. // Among other, a lock level is non zero, while directory is being loaded,
  10413. // even when it happens as a result of dirview navigation,
  10414. // i.e. when TNonVisualDataModule is NOT FBusy.
  10415. // That's why the TNonVisualDataModule::GetBusy calls this method.
  10416. // Among other this prevents a panel auto update to occur while
  10417. // directory is changing.
  10418. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  10419. }
  10420. //---------------------------------------------------------------------------
  10421. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  10422. {
  10423. // While the window is disabled, we still seem to process menu shortcuts at least,
  10424. // so stop it at least here.
  10425. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  10426. bool Result;
  10427. if (Allowed == aaUpdate)
  10428. {
  10429. Result = true;
  10430. }
  10431. else
  10432. {
  10433. Result = !NonVisualDataModule->Busy;
  10434. if (Result && (Allowed == aaShortCut))
  10435. {
  10436. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  10437. Action->Update();
  10438. Result = Action->Enabled;
  10439. }
  10440. }
  10441. return Result;
  10442. }
  10443. //---------------------------------------------------------------------------
  10444. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  10445. {
  10446. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  10447. }
  10448. //---------------------------------------------------------------------------
  10449. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  10450. {
  10451. // This is somewhat redundant to LockWindow() call from
  10452. // TTerminalManager::TerminalReadDirectoryProgress.
  10453. // But disabling window is known not to block keyboard shorcuts
  10454. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  10455. if (Busy > 0)
  10456. {
  10457. if (NonVisualDataModule->Busy)
  10458. {
  10459. State = false;
  10460. }
  10461. else
  10462. {
  10463. NonVisualDataModule->StartBusy();
  10464. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  10465. LockWindow();
  10466. }
  10467. }
  10468. else if (Busy < 0)
  10469. {
  10470. UnlockWindow();
  10471. NonVisualDataModule->EndBusy();
  10472. }
  10473. else
  10474. {
  10475. State = NonVisualDataModule->Busy;
  10476. }
  10477. }
  10478. //---------------------------------------------------------------------------
  10479. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  10480. {
  10481. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  10482. if (Index >= 0)
  10483. {
  10484. TPopupMenu * PopupMenu = NULL;
  10485. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10486. if (Session != NULL)
  10487. {
  10488. SessionsPageControl->ActivePageIndex = Index;
  10489. if (DebugAlwaysTrue(SessionTabSwitched()))
  10490. {
  10491. // copied from TControl.WMContextMenu
  10492. SendCancelMode(SessionsPageControl);
  10493. // explicit popup instead of using PopupMenu property
  10494. // to avoid menu to popup somewhere within SessionTabSwitched above,
  10495. // while connecting yet not-connected session and hence
  10496. // allowing an access to commands over not-completely connected session
  10497. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  10498. }
  10499. }
  10500. else
  10501. {
  10502. if (SupportsLocalBrowser())
  10503. {
  10504. PopupMenu = NonVisualDataModule->NewTabPopup;
  10505. }
  10506. }
  10507. if (PopupMenu != NULL)
  10508. {
  10509. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  10510. PopupMenu->PopupComponent = SessionsPageControl;
  10511. PopupMenu->Popup(Point.x, Point.y);
  10512. }
  10513. }
  10514. Handled = true;
  10515. }
  10516. //---------------------------------------------------------------------------
  10517. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  10518. {
  10519. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  10520. }
  10521. //---------------------------------------------------------------------------
  10522. void __fastcall TCustomScpExplorerForm::ChangePassword()
  10523. {
  10524. Configuration->Usage->Inc(L"PasswordChanges");
  10525. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  10526. ChangePasswordData->DisableAuthentationsExceptPassword();
  10527. ChangePasswordData->ChangePassword = true;
  10528. std::unique_ptr<TTerminal> ChangePasswordSession(
  10529. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  10530. ChangePasswordSession->AutoReadDirectory = false;
  10531. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  10532. {
  10533. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  10534. }
  10535. else
  10536. {
  10537. Configuration->Usage->Inc(L"PasswordChangesFailed");
  10538. }
  10539. }
  10540. //---------------------------------------------------------------------------
  10541. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  10542. {
  10543. // No nice way to assert SSH2
  10544. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  10545. }
  10546. //---------------------------------------------------------------------------
  10547. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  10548. {
  10549. TOperationVisualizer OperationVisualizer;
  10550. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  10551. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  10552. }
  10553. //---------------------------------------------------------------------------
  10554. void __fastcall TCustomScpExplorerForm::FormAfterMonitorDpiChanged(TObject *, int OldDPI, int NewDPI)
  10555. {
  10556. DebugUsedParam(OldDPI);
  10557. DebugAssert(NewDPI == GetControlPixelsPerInch(this));
  10558. GlyphsModule->PixelsPerInch = NewDPI;
  10559. // Can be called too early when primary monitor has non-system dpi
  10560. if (FSessionColors != NULL)
  10561. {
  10562. RegenerateSessionColorsImageList();
  10563. }
  10564. UpdateTabsSize();
  10565. UpdateSessionsPageControlHeight();
  10566. QueueFileListColumnAutoSize();
  10567. }
  10568. //---------------------------------------------------------------------------
  10569. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  10570. {
  10571. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  10572. }
  10573. //---------------------------------------------------------------------
  10574. class TPublicControl : public TControl
  10575. {
  10576. friend class TCustomScpExplorerForm;
  10577. };
  10578. //---------------------------------------------------------------------------
  10579. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  10580. {
  10581. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  10582. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  10583. }
  10584. //---------------------------------------------------------------------------
  10585. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  10586. {
  10587. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  10588. OpenBookmark(Side, Bookmark);
  10589. }
  10590. //---------------------------------------------------------------------------
  10591. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  10592. {
  10593. DoBookmarkClick(osLocal, Sender);
  10594. }
  10595. //---------------------------------------------------------------------------
  10596. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  10597. {
  10598. DoBookmarkClick(osRemote, Sender);
  10599. }
  10600. //---------------------------------------------------------------------------
  10601. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  10602. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  10603. {
  10604. if (BookmarkList != NULL)
  10605. {
  10606. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  10607. if (IsLocalBrowserMode())
  10608. {
  10609. Side = osLocal;
  10610. }
  10611. if (!WinConfiguration->UseLocationProfiles)
  10612. {
  10613. std::unique_ptr<TStringList> Directories(new TStringList());
  10614. Directories->CaseSensitive = (Side == osRemote);
  10615. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10616. {
  10617. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10618. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  10619. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  10620. {
  10621. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10622. Item->Caption = EscapeHotkey(Directory);
  10623. Item->ShortCut = Bookmark->ShortCut;
  10624. Item->OnClick = OnBookmarkClick;
  10625. Item->Tag = reinterpret_cast<int>(Bookmark);
  10626. Directories->Add(Directory);
  10627. Menu->Add(Item.release());
  10628. }
  10629. }
  10630. }
  10631. else
  10632. {
  10633. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10634. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10635. {
  10636. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10637. if (!Bookmark->Node.IsEmpty())
  10638. {
  10639. Folders->Add(Bookmark->Node);
  10640. }
  10641. }
  10642. for (int Index = 0; Index < Folders->Count; Index++)
  10643. {
  10644. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10645. Item->Caption = Folders->Strings[Index];
  10646. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10647. Folders->Objects[Index] = Item.get();
  10648. Menu->Add(Item.release());
  10649. }
  10650. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10651. {
  10652. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10653. TTBCustomItem * Parent;
  10654. if (!Bookmark->Node.IsEmpty())
  10655. {
  10656. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10657. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10658. }
  10659. else
  10660. {
  10661. Parent = Menu;
  10662. }
  10663. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10664. Item->Caption = Bookmark->Name;
  10665. Item->ShortCut = Bookmark->ShortCut;
  10666. Item->OnClick = OnBookmarkClick;
  10667. Item->Tag = reinterpret_cast<int>(Bookmark);
  10668. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10669. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10670. {
  10671. // No hint for location profiles that are actually mere "bookmarks"
  10672. }
  10673. else
  10674. {
  10675. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10676. UnicodeString LongHint;
  10677. if (!Bookmark->Local.IsEmpty())
  10678. {
  10679. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10680. }
  10681. if (!Bookmark->Remote.IsEmpty())
  10682. {
  10683. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10684. }
  10685. AddToList(Hint, LongHint, L"|");
  10686. Item->Hint = Hint;
  10687. }
  10688. Parent->Add(Item.release());
  10689. }
  10690. }
  10691. AddMenuSeparator(Menu);
  10692. }
  10693. }
  10694. //---------------------------------------------------------------------------
  10695. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10696. {
  10697. Menu->Clear();
  10698. std::unique_ptr<TTBCustomItem> Item;
  10699. Item.reset(new TTBXItem(Owner));
  10700. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10701. Item->Options = Item->Options << tboDefault;
  10702. Menu->Add(Item.release());
  10703. AddMenuSeparator(Menu);
  10704. if (Terminal != NULL)
  10705. {
  10706. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10707. }
  10708. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10709. Item.reset(new TTBXItem(Owner));
  10710. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10711. Menu->Add(Item.release());
  10712. }
  10713. //---------------------------------------------------------------------------
  10714. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10715. {
  10716. bool Result = true;
  10717. bool Remote = !IsSideLocalBrowser(Side);
  10718. try
  10719. {
  10720. UnicodeString APath = Path;
  10721. if (Remote)
  10722. {
  10723. Terminal->ExceptionOnFail = true;
  10724. }
  10725. else
  10726. {
  10727. APath = ExpandEnvironmentVariables(Path);
  10728. }
  10729. try
  10730. {
  10731. DirView(Side)->Path = APath;
  10732. }
  10733. __finally
  10734. {
  10735. if (Remote)
  10736. {
  10737. Terminal->ExceptionOnFail = false;
  10738. }
  10739. }
  10740. }
  10741. catch (Exception & E)
  10742. {
  10743. if (!Remote || Terminal->Active)
  10744. {
  10745. ShowExtendedExceptionEx(Terminal, &E);
  10746. Result = false;
  10747. }
  10748. else
  10749. {
  10750. throw;
  10751. }
  10752. }
  10753. return Result;
  10754. }
  10755. //---------------------------------------------------------------------------
  10756. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10757. {
  10758. // Make sure there some feedback even when loading tiny local folders
  10759. TInstantOperationVisualizer Visualizer;
  10760. DirView(Side)->ReloadDirectory();
  10761. }
  10762. //---------------------------------------------------------------------------
  10763. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10764. {
  10765. if (Index == SessionsPageControl->TabIndex)
  10766. {
  10767. CloseTab();
  10768. }
  10769. else
  10770. {
  10771. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10772. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10773. TTerminalManager * Manager = TTerminalManager::Instance();
  10774. if (!Terminal->Active)
  10775. {
  10776. Manager->FreeTerminal(Terminal);
  10777. }
  10778. else
  10779. {
  10780. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10781. if (CanCloseQueue(Queue))
  10782. {
  10783. Manager->FreeTerminal(Terminal);
  10784. }
  10785. }
  10786. }
  10787. }
  10788. //---------------------------------------------------------------------------
  10789. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10790. {
  10791. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10792. if (Session == NULL)
  10793. {
  10794. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10795. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10796. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10797. //P = SessionsPageControl->ClientToScreen(P);
  10798. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10799. }
  10800. else
  10801. {
  10802. CloseSessionTab(Index);
  10803. }
  10804. }
  10805. //---------------------------------------------------------------------------
  10806. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10807. {
  10808. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10809. {
  10810. TInstantOperationVisualizer Visualizer;
  10811. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10812. // before we create a new data object
  10813. ClipboardClear();
  10814. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10815. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10816. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10817. // Intermediate variable to WORKAROUND a crash in compiler
  10818. TObjectList * ClipboardFakeMonitors =
  10819. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10820. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10821. if (FClipboardDragDropFilesEx.get() == NULL)
  10822. {
  10823. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10824. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10825. }
  10826. FClipboardDragDropFilesEx->FileList->Clear();
  10827. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10828. FClipboardDragDropFilesEx->CopyToClipboard();
  10829. FClipboardTerminal = Terminal;
  10830. // Need full paths, as cwd can be different once files are pasted
  10831. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10832. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10833. {
  10834. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10835. }
  10836. AppLogFmt(L"Created fake clipboard directory \"%s\" and started drive monitors", (FClipboardFakeDirectory));
  10837. }
  10838. }
  10839. //---------------------------------------------------------------------------
  10840. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10841. {
  10842. if (OpenClipboard(0))
  10843. {
  10844. AppLog(L"Clearing clipboard");
  10845. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10846. CloseClipboard();
  10847. }
  10848. }
  10849. //---------------------------------------------------------------------------
  10850. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10851. {
  10852. return !FClipboardFakeDirectory.IsEmpty();
  10853. }
  10854. //---------------------------------------------------------------------------
  10855. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10856. {
  10857. bool Deleted = RemoveDir(ApiPath(FClipboardFakeDirectory));
  10858. AppLogFmt(L"Clearing clipboard, deleted clipboard temporary folder \"%s\" - %d", (FClipboardFakeDirectory, int(Deleted)));
  10859. FClipboardFakeDirectory = UnicodeString();
  10860. FClipboardTerminal = NULL;
  10861. if (FDownloadingFromClipboard)
  10862. {
  10863. // We are called by the monitor, so attempt to release it would deadlock
  10864. FClipboardFakeMonitorsPendingReset = true;
  10865. }
  10866. else
  10867. {
  10868. FClipboardFakeMonitors.reset(NULL);
  10869. }
  10870. }
  10871. //---------------------------------------------------------------------------
  10872. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10873. {
  10874. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10875. {
  10876. ClipboardStop();
  10877. }
  10878. }
  10879. //---------------------------------------------------------------------------
  10880. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10881. {
  10882. TTerminalManager * Manager = TTerminalManager::Instance();
  10883. Manager->ActiveSession = FClipboardTerminal;
  10884. TTransferOperationParam Params;
  10885. Params.TargetDirectory = TargetDirectory;
  10886. Params.DragDrop = DragDrop;
  10887. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10888. }
  10889. //---------------------------------------------------------------------------
  10890. void TCustomScpExplorerForm::PasteFiles()
  10891. {
  10892. // Guard against possible race conditions
  10893. if (!FClipboardPasteTarget.IsEmpty())
  10894. {
  10895. // ClipboardFakeCreated might be called while we are here, so make sure we finish operating on the same path we started with
  10896. UnicodeString Target = FClipboardPasteTarget;
  10897. FClipboardPasteTarget = EmptyStr;
  10898. // Can fail as it can be e.g. locked by AV, so we retry that later
  10899. bool Removed = RemoveDir(ApiPath(Target));
  10900. AppLogFmt(L"First attempt to delete pasted fake clipboard directory \"%s\" - %d", (Target, int(Removed)));
  10901. try
  10902. {
  10903. if (NonVisualDataModule->Busy)
  10904. {
  10905. AppLog(L"Is busy, cannot paste");
  10906. }
  10907. else
  10908. {
  10909. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10910. FClipboardFakeMonitorsPendingReset = false;
  10911. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10912. TAutoFlag Flag(FDownloadingFromClipboard);
  10913. AppLog(L"Downloading to paste target...");
  10914. ClipboardDownload(ExtractFilePath(Target), NoConfirmation, true);
  10915. AppLog(L"Downloaded to paste target...");
  10916. }
  10917. }
  10918. __finally
  10919. {
  10920. if (!Removed)
  10921. {
  10922. Removed = RemoveDir(ApiPath(Target));
  10923. AppLogFmt(L"Second attempt to delete pasted fake clipboard directory \"%s\" - %d", (Target, int(Removed)));
  10924. }
  10925. }
  10926. }
  10927. }
  10928. //---------------------------------------------------------------------------
  10929. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10930. {
  10931. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10932. // (as it pumps a Windows message queue while being shutted down)
  10933. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10934. // Is can happen that creation of our temporary directory is detected (even though we are creating it before starting the monitors).
  10935. // It tent do happen with later copy-pastes, probably because everything is cached already and monitors start quickly.
  10936. !IsPathToSameFile(FClipboardFakeDirectory, FileName) &&
  10937. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10938. {
  10939. AppLogFmt(L"Fake clipboard directory pasted to \"%s\"", (FileName));
  10940. FClipboardPasteTarget = FileName;
  10941. // Particularly when pasting to external HDD, the fake directory is strangely recreated if removed here in the monitor callback.
  10942. // Postponing removing (or actually whole pasting operation) seems to help.
  10943. PostMessage(Handle, WM_PASTE_FILES, 0, 0);
  10944. }
  10945. }
  10946. //---------------------------------------------------------------------------
  10947. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10948. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10949. {
  10950. TFileMasks::TParams MaskParams;
  10951. MaskParams.Size = Size;
  10952. MaskParams.Modification = Modification;
  10953. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10954. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10955. // TUnixDirView::ItemFullFileName returns trailing backslash for directories, what filemask cannot deal with
  10956. if (!Local && Directory)
  10957. {
  10958. FileName = UnixExcludeTrailingBackslash(FileName);
  10959. }
  10960. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10961. {
  10962. bool ImplicitMatch;
  10963. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10964. !ImplicitMatch)
  10965. {
  10966. Color = Iter->Color;
  10967. break;
  10968. }
  10969. }
  10970. }
  10971. //---------------------------------------------------------------------------
  10972. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10973. {
  10974. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10975. if (!DirView->IsEditing())
  10976. {
  10977. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10978. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10979. // while searching
  10980. if (Key == VK_BACK)
  10981. {
  10982. if (FIncrementalSearchState.Searching)
  10983. {
  10984. if (FIncrementalSearchState.Text.Length() <= 1)
  10985. {
  10986. ResetIncrementalSearch();
  10987. }
  10988. else
  10989. {
  10990. UnicodeString NewText = LeftStr(FIncrementalSearchState.Text, FIncrementalSearchState.Text.Length() - 1);
  10991. IncrementalSearch(NewText, false, false);
  10992. }
  10993. Key = 0;
  10994. }
  10995. }
  10996. }
  10997. }
  10998. //---------------------------------------------------------------------------
  10999. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  11000. {
  11001. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  11002. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  11003. {
  11004. // Filter control sequences.
  11005. // When not searching yet, prefer use of the space for toggling file selection
  11006. // (so we cannot incrementally search for a string starting with a space).
  11007. if ((Key > VK_SPACE) ||
  11008. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && FIncrementalSearchState.Searching))
  11009. {
  11010. IncrementalSearch(FIncrementalSearchState.Text + Key, false, false);
  11011. Key = 0;
  11012. }
  11013. }
  11014. }
  11015. //---------------------------------------------------------------------------
  11016. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  11017. {
  11018. if (FIncrementalSearchState.Searching)
  11019. {
  11020. FIncrementalSearchState.Reset();
  11021. DirView(osCurrent)->UpdateStatusBar();
  11022. }
  11023. }
  11024. //---------------------------------------------------------------------------
  11025. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  11026. {
  11027. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  11028. if (Item == NULL)
  11029. {
  11030. MessageBeep(MB_ICONHAND);
  11031. }
  11032. else
  11033. {
  11034. TCustomDirView * ADirView = DirView(osCurrent);
  11035. {
  11036. TAutoNestingCounter Guard(FIncrementalSearching);
  11037. ADirView->FocusItem(Item);
  11038. if (ADirView->NortonLike == nlOff)
  11039. {
  11040. if ((ADirView->Selected != Item) ||
  11041. (ADirView->SelCount > 1))
  11042. {
  11043. ADirView->ClearSelection();
  11044. ADirView->Selected = Item;
  11045. }
  11046. }
  11047. }
  11048. FIncrementalSearchState.Searching = true;
  11049. FIncrementalSearchState.Text = Text;
  11050. Item->MakeVisible(false);
  11051. TListItem * NextItem = SearchFile(Text, true, Reverse);
  11052. FIncrementalSearchState.HaveNext = (NextItem != NULL) && (NextItem != Item);
  11053. ADirView->UpdateStatusBar();
  11054. }
  11055. }
  11056. //---------------------------------------------------------------------------
  11057. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  11058. {
  11059. int Index = Item->Index;
  11060. if (!Reverse)
  11061. {
  11062. Index++;
  11063. if (Index >= Item->Owner->Count)
  11064. {
  11065. Index = 0;
  11066. }
  11067. }
  11068. else
  11069. {
  11070. Index--;
  11071. if (Index < 0)
  11072. {
  11073. Index = Item->Owner->Count - 1;
  11074. }
  11075. }
  11076. return Item->Owner->Item[Index];
  11077. }
  11078. //---------------------------------------------------------------------------
  11079. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  11080. {
  11081. TCustomDirView * ADirView = DirView(osCurrent);
  11082. if (ADirView->Items->Count == 0)
  11083. {
  11084. return NULL;
  11085. }
  11086. else
  11087. {
  11088. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  11089. TListItem * Item = CurrentItem;
  11090. if (SkipCurrent)
  11091. {
  11092. Item = DebugNotNull(GetNextFile(Item, Reverse));
  11093. }
  11094. while (true)
  11095. {
  11096. bool Matches = false;
  11097. switch (WinConfiguration->PanelSearch)
  11098. {
  11099. case isNameStartOnly:
  11100. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item).SubString(1, Text.Length()), Text);
  11101. break;
  11102. case isName:
  11103. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item), Text);
  11104. break;
  11105. case isAll:
  11106. int ColCount = (ADirView->DirViewStyle == dvsReport) ? ADirView->ColProperties->Count : 1;
  11107. int Index = 0;
  11108. while ((Index < ColCount) && !Matches)
  11109. {
  11110. bool Visible = (ADirView->DirViewStyle == dvsReport) ? ADirView->ColProperties->Visible[Index] : true;
  11111. if (Visible)
  11112. {
  11113. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  11114. }
  11115. Index++;
  11116. }
  11117. break;
  11118. }
  11119. if (Matches)
  11120. {
  11121. return Item;
  11122. }
  11123. Item = GetNextFile(Item, Reverse);
  11124. if (Item == CurrentItem)
  11125. {
  11126. return NULL;
  11127. }
  11128. }
  11129. }
  11130. }
  11131. //---------------------------------------------------------------------------
  11132. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  11133. {
  11134. ResetIncrementalSearch();
  11135. }
  11136. //---------------------------------------------------------------------------
  11137. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  11138. {
  11139. if (Message.CharCode == VK_TAB)
  11140. {
  11141. if (!FIncrementalSearchState.Text.IsEmpty())
  11142. {
  11143. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  11144. bool Reverse = Shift.Contains(ssShift);
  11145. IncrementalSearch(FIncrementalSearchState.Text, true, Reverse);
  11146. Message.Result = 1;
  11147. return;
  11148. }
  11149. }
  11150. else if (Message.CharCode == VK_ESCAPE)
  11151. {
  11152. if (FIncrementalSearchState.Searching)
  11153. {
  11154. ResetIncrementalSearch();
  11155. Message.Result = 1;
  11156. return;
  11157. }
  11158. }
  11159. TForm::Dispatch(&Message);
  11160. }
  11161. //---------------------------------------------------------------------------
  11162. void __fastcall TCustomScpExplorerForm::Deactivate()
  11163. {
  11164. TForm::Deactivate();
  11165. // E.g. when switching to an internal editor window
  11166. ResetIncrementalSearch();
  11167. }
  11168. //---------------------------------------------------------------------------
  11169. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  11170. {
  11171. // When switching to another application
  11172. ResetIncrementalSearch();
  11173. }
  11174. //---------------------------------------------------------------------------
  11175. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  11176. {
  11177. ResetIncrementalSearch();
  11178. }
  11179. //---------------------------------------------------------------------------
  11180. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  11181. {
  11182. if (FIncrementalSearching <= 0)
  11183. {
  11184. ResetIncrementalSearch();
  11185. }
  11186. }
  11187. //---------------------------------------------------------------------------
  11188. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  11189. {
  11190. CountClicksForWindowPrint(this);
  11191. }
  11192. //---------------------------------------------------------------------------
  11193. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  11194. {
  11195. CenterReconnectToolbar();
  11196. }
  11197. //---------------------------------------------------------------------------
  11198. void TCustomScpExplorerForm::DoExploreFile(TCustomDirView * DirView, const UnicodeString & FileName)
  11199. {
  11200. if (!FileName.IsEmpty())
  11201. {
  11202. DirView->FocusByName(FileName);
  11203. if (DirView->ItemFocused != NULL)
  11204. {
  11205. DirView->ItemFocused->Selected = true;
  11206. }
  11207. }
  11208. }
  11209. //---------------------------------------------------------------------------
  11210. void __fastcall TCustomScpExplorerForm::ExploreFile(const UnicodeString & FileName)
  11211. {
  11212. DoExploreFile(RemoteDirView, FileName);
  11213. }
  11214. //---------------------------------------------------------------------------
  11215. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  11216. {
  11217. if (!FQueueStatusUpdating)
  11218. {
  11219. bool Refresh;
  11220. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  11221. {
  11222. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  11223. }
  11224. else
  11225. {
  11226. if (FQueueFileList->GetCount() > 0)
  11227. {
  11228. FQueueFileList->Clear();
  11229. Refresh = true;
  11230. }
  11231. }
  11232. int Count = FQueueFileList->GetCount();
  11233. if (QueueFileList->Items->Count != Count)
  11234. {
  11235. DebugAssert(Refresh);
  11236. QueueFileList->Items->Count = Count;
  11237. }
  11238. if (Refresh)
  11239. {
  11240. QueueFileList->Invalidate();
  11241. }
  11242. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  11243. }
  11244. }
  11245. //---------------------------------------------------------------------------
  11246. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  11247. {
  11248. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  11249. }
  11250. //---------------------------------------------------------------------------
  11251. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  11252. {
  11253. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  11254. // See TSynchronizeChecklistDialog::UpdateTimer
  11255. if (QueueFileList->Items->Count > 0)
  11256. {
  11257. QueueFileList->Items->Item[0]->MakeVisible(false);
  11258. }
  11259. UpdateQueueFileList();
  11260. }
  11261. //---------------------------------------------------------------------------
  11262. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  11263. {
  11264. Active = QueueView3->Focused() || QueueFileList->Focused();
  11265. }
  11266. //---------------------------------------------------------------------------
  11267. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  11268. {
  11269. QueueLabelUpdateStatus();
  11270. }
  11271. //---------------------------------------------------------------------------
  11272. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  11273. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  11274. {
  11275. int State = FQueueFileList->GetState(Item->Index);
  11276. if (State == qfsProcessed)
  11277. {
  11278. Sender->Canvas->Font->Color = clGrayText;
  11279. }
  11280. }
  11281. //---------------------------------------------------------------------------
  11282. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  11283. {
  11284. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  11285. QueueFileList->Column[0]->Width =
  11286. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  11287. }
  11288. //---------------------------------------------------------------------------
  11289. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  11290. {
  11291. QueueFileListColumnAutoSize();
  11292. }
  11293. //---------------------------------------------------------------------------
  11294. void __fastcall TCustomScpExplorerForm::CloseApp()
  11295. {
  11296. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  11297. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  11298. // So we clear the flag here.
  11299. DebugAssert(NonVisualDataModule->Busy);
  11300. NonVisualDataModule->EndBusy();
  11301. try
  11302. {
  11303. Close();
  11304. }
  11305. __finally
  11306. {
  11307. NonVisualDataModule->StartBusy();
  11308. }
  11309. }
  11310. //---------------------------------------------------------------------------
  11311. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  11312. {
  11313. return TTerminalManager::Instance()->IsActiveTerminal(Terminal);
  11314. }
  11315. //---------------------------------------------------------------------------
  11316. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  11317. {
  11318. return (ManagedSession != NULL);
  11319. }
  11320. //---------------------------------------------------------------------------
  11321. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  11322. {
  11323. return IsActiveTerminal(Terminal);
  11324. }
  11325. //---------------------------------------------------------------------------
  11326. void TCustomScpExplorerForm::LocalLocalCopy(
  11327. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  11328. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  11329. {
  11330. DebugFail();
  11331. }
  11332. //---------------------------------------------------------------------------
  11333. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  11334. {
  11335. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  11336. }
  11337. //---------------------------------------------------------------------------
  11338. void TCustomScpExplorerForm::UpdateTabsSize()
  11339. {
  11340. // 1) Changing tab list triggers the OnResize.
  11341. // 2) Is called in TForm constructor, when we are not ready to call SessionListChanged (particularly when starting with colored session)
  11342. if (!FUpdatingSessionTabs && (FSessionColors != NULL))
  11343. {
  11344. // UpdateTabsCaptionTruncation should be enough
  11345. SessionListChanged(true);
  11346. }
  11347. }
  11348. //---------------------------------------------------------------------------
  11349. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  11350. {
  11351. UpdateTabsSize();
  11352. }
  11353. //---------------------------------------------------------------------------
  11354. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  11355. {
  11356. if (ASession == ManagedSession)
  11357. {
  11358. UpdateSession(ASession);
  11359. }
  11360. TSessionData * Data = ASession->StateData;
  11361. UnicodeString Result;
  11362. if (Side == osLocal)
  11363. {
  11364. Result = Data->LocalDirectory;
  11365. }
  11366. else if (DebugAlwaysTrue(Side == osOther))
  11367. {
  11368. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  11369. }
  11370. return Result;
  11371. }
  11372. //---------------------------------------------------------------------------
  11373. UnicodeString TCustomScpExplorerForm::GetTabHintSessionDetails(TManagedTerminal * ASession)
  11374. {
  11375. UnicodeString Title = TTerminalManager::Instance()->GetSessionTitle(ASession, false);
  11376. UnicodeString Result = ASession->SessionData->DefaultSessionName;
  11377. if (Title == Result)
  11378. {
  11379. Result = EmptyStr;
  11380. }
  11381. else
  11382. {
  11383. Result += L"\n";
  11384. }
  11385. return Result;
  11386. }
  11387. //---------------------------------------------------------------------------
  11388. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  11389. {
  11390. UnicodeString Result = GetTabHintSessionDetails(ASession);
  11391. if (!ASession->Active)
  11392. {
  11393. Result += LoadStr(STATUS_NOT_CONNECTED2);
  11394. }
  11395. else
  11396. {
  11397. Result += GetSessionPath(ASession, osRemote);
  11398. }
  11399. return Result;
  11400. }
  11401. //---------------------------------------------------------------------------
  11402. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  11403. {
  11404. return LoadStr(NEW_REMOTE_TAB_HINT);
  11405. }
  11406. //---------------------------------------------------------------------------
  11407. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  11408. {
  11409. if (Index >= 0)
  11410. {
  11411. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  11412. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  11413. if (TabSession == NULL)
  11414. {
  11415. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  11416. if (NewTabTabCaption != TabSheet->Caption)
  11417. {
  11418. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  11419. }
  11420. }
  11421. else
  11422. {
  11423. TTerminalManager * Manager = TTerminalManager::Instance();
  11424. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  11425. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  11426. }
  11427. }
  11428. }
  11429. //---------------------------------------------------------------------------
  11430. typedef std::vector<TListItem *> TListItemsVector;
  11431. struct TCalculateSizeOperation
  11432. {
  11433. TCalculateSizeStats Stats;
  11434. TListItemsVector ListItems;
  11435. size_t Index;
  11436. };
  11437. //---------------------------------------------------------------------------
  11438. void TCustomScpExplorerForm::CalculateDirectorySizes(TOperationSide Side)
  11439. {
  11440. TCalculateSizeOperation CalculateSizeOperation;
  11441. Side = GetSide(Side);
  11442. TCustomDirView * ADirView = DirView(Side);
  11443. bool LocalSide = IsSideLocalBrowser(Side);
  11444. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, LocalSide, NULL, true));
  11445. std::unique_ptr<TStrings> FileList(new TStringList());
  11446. for (int Index = 0; Index < AllFileList->Count; Index++)
  11447. {
  11448. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  11449. if (ADirView->ItemIsDirectory(Item))
  11450. {
  11451. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  11452. CalculateSizeOperation.ListItems.push_back(Item);
  11453. }
  11454. }
  11455. CalculateSizeOperation.Index = 0;
  11456. __int64 Size = 0;
  11457. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation, &CalculateSizeOperation);
  11458. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  11459. FOnFileOperationFinished = DirectorySizeCalculated;
  11460. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation, foCalculateSize);
  11461. TCalculatedSizes CalculatedSizes;
  11462. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  11463. if (LocalSide)
  11464. {
  11465. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide, Side);
  11466. ManagedSession->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  11467. }
  11468. else
  11469. {
  11470. TCalculateSizeParams Params;
  11471. Params.Stats = &CalculateSizeOperation.Stats;
  11472. Params.UseCache = true;
  11473. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  11474. }
  11475. }
  11476. //---------------------------------------------------------------------------
  11477. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(
  11478. TOperationSide Side, const UnicodeString & FileName, bool Success, bool NotCancelled)
  11479. {
  11480. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  11481. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  11482. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  11483. // optimization
  11484. bool GotExpectedItem =
  11485. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  11486. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  11487. int Index = -1;
  11488. if (DebugAlwaysTrue(GotExpectedItem))
  11489. {
  11490. Index = FCalculateSizeOperation->Index;
  11491. FCalculateSizeOperation->Index++;
  11492. }
  11493. else
  11494. {
  11495. TListItemsVector::iterator I =
  11496. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  11497. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  11498. {
  11499. Index = I - FCalculateSizeOperation->ListItems.begin();
  11500. }
  11501. }
  11502. if (DebugAlwaysTrue(Index >= 0) && Success && NotCancelled)
  11503. {
  11504. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  11505. ADirView->SetItemCalculatedSize(Item, Size);
  11506. }
  11507. }
  11508. //---------------------------------------------------------------------------
  11509. void __fastcall TCustomScpExplorerForm::MessageDockRequestDock(TObject *, TTBCustomDockableWindow *, bool & Accept)
  11510. {
  11511. Accept = false;
  11512. }
  11513. //---------------------------------------------------------------------------
  11514. void TCustomScpExplorerForm::AutoSizeColumns(TOperationSide Side)
  11515. {
  11516. // Send Ctrl+Gray+ to the list view
  11517. TKeyboardState KeyState;
  11518. GetKeyboardState(KeyState);
  11519. TKeyboardState NewKeyState;
  11520. memset(NewKeyState, 0, sizeof(NewKeyState));
  11521. NewKeyState[VK_CONTROL] = 0x81;
  11522. SetKeyboardState(NewKeyState);
  11523. SendMessage(DirView(Side)->Handle, WM_KEYDOWN, VK_ADD, 0);
  11524. SetKeyboardState(KeyState);
  11525. }
  11526. //---------------------------------------------------------------------------
  11527. void TCustomScpExplorerForm::QueueResetLayoutColumns()
  11528. {
  11529. LoadListViewStr(QueueView3, QueueViewLayoutDefault);
  11530. }
  11531. //---------------------------------------------------------------------------
  11532. void * TCustomScpExplorerForm::SaveFocus()
  11533. {
  11534. return NULL;
  11535. }
  11536. //---------------------------------------------------------------------------
  11537. void TCustomScpExplorerForm::RestoreFocus(void * Focus)
  11538. {
  11539. DebugUsedParam(Focus);
  11540. DebugAssert(Focus == NULL);
  11541. }
  11542. //---------------------------------------------------------------------------
  11543. void TCustomScpExplorerForm::IncrementalSearchStart()
  11544. {
  11545. TCustomDirView * ADirView = DirView(osCurrent);
  11546. ADirView->SetFocus();
  11547. if (!FIncrementalSearchState.Searching)
  11548. {
  11549. FIncrementalSearchState.Searching = true;
  11550. FIncrementalSearchState.HaveNext = false;
  11551. DebugAssert(FIncrementalSearchState.Text.IsEmpty());
  11552. ADirView->UpdateStatusBar();
  11553. }
  11554. }
  11555. //---------------------------------------------------------------------------
  11556. void __fastcall TCustomScpExplorerForm::RemoteDirViewThumbnailNeeded(
  11557. TUnixDirView *, TListItem * Item, TRemoteFile * File, const TSize & Size, TBitmap *& Bitmap)
  11558. {
  11559. // though when downloading to temporary folder, encryption should not be a problem
  11560. if (Terminal->IsCapable[fcBackgroundTransfers] &&
  11561. !File->IsDirectory)
  11562. {
  11563. UnicodeString FileName = File->FullFileName;
  11564. TFileMasks::TParams MaskParams;
  11565. MaskParams.Size = File->Size;
  11566. MaskParams.Modification = File->Modification;
  11567. if ((File->Size <= static_cast<__int64>(WinConfiguration->RemoteThumbnailSizeLimit) * 1024) &&
  11568. FRemoteThumbnailMask.Matches(FileName, false, File->IsDirectory, &MaskParams))
  11569. {
  11570. Bitmap = TTerminalManager::Instance()->ThumbnailNeeded(Terminal, Item->Index, File, Size);
  11571. }
  11572. }
  11573. }
  11574. //---------------------------------------------------------------------------
  11575. TThumbnailDownloadQueueItem * TCustomScpExplorerForm::AddThumbnailDownloadQueueItem(TManagedTerminal * ATerminal)
  11576. {
  11577. DebugAssert(ATerminal == Terminal);
  11578. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  11579. TemporaryFileCopyParam(CopyParam);
  11580. CopyParam.TransferMode = tmBinary;
  11581. UnicodeString SourceDir = ATerminal->CurrentDirectory;
  11582. UnicodeString TempDir, RootTempDir;
  11583. TemporaryDirectoryForRemoteFiles(SourceDir, CopyParam, true, TempDir, RootTempDir);
  11584. TTerminalQueue * AQueue = TTerminalManager::Instance()->FindQueueForTerminal(ATerminal);
  11585. DebugAssert((Queue == NULL) || (AQueue == Queue)); // we might get here before queue is set
  11586. TThumbnailDownloadQueueItem * QueueItem =
  11587. new TThumbnailDownloadQueueItem(this, Terminal, SourceDir, TempDir, &CopyParam);
  11588. AddQueueItem(AQueue, QueueItem, ATerminal);
  11589. return QueueItem;
  11590. }
  11591. //---------------------------------------------------------------------------
  11592. enum { qccRemoteItemVisible, qccRemoteItemRedraw };
  11593. typedef std::pair<int, UnicodeString> TRemoteItemVisibleData;
  11594. //---------------------------------------------------------------------------
  11595. void TCustomScpExplorerForm::PostThumbnailVisibleQueueQuery(int Index, const UnicodeString & FileName)
  11596. {
  11597. // The queue might be destroyed before the WM_QUEUE_CALLBACK is processed
  11598. TRemoteItemVisibleData * Data = new TRemoteItemVisibleData();
  11599. Data->first = Index;
  11600. Data->second = FileName;
  11601. PostMessage(Handle, WM_QUEUE_CALLBACK, qccRemoteItemVisible, reinterpret_cast<WPARAM>(Data));
  11602. }
  11603. //---------------------------------------------------------------------------
  11604. void TCustomScpExplorerForm::PostThumbnailDrawRequest(int Index)
  11605. {
  11606. PostMessage(Handle, WM_QUEUE_CALLBACK, qccRemoteItemRedraw, Index);
  11607. }
  11608. //---------------------------------------------------------------------------
  11609. void TCustomScpExplorerForm::WMQueueCallback(TMessage & Message)
  11610. {
  11611. int Command = Message.WParam;
  11612. if (Command == qccRemoteItemVisible)
  11613. {
  11614. TRemoteItemVisibleData * Data = reinterpret_cast<TRemoteItemVisibleData *>(Message.LParam);
  11615. int Index = Data->first;
  11616. UnicodeString FileName = Data->second;
  11617. delete Data;
  11618. TGuard Guard(Terminal->ThumbnailsSection.get());
  11619. bool Visible = False;
  11620. if (Terminal->ThumbnailsEnabled &&
  11621. // though we should better also disable thumbnails once we switch to different view style
  11622. (RemoteDirView->DirViewStyle == dvsThumbnail))
  11623. {
  11624. // The message might be processed, while we are already loading a different directory
  11625. if ((Index >= 0) &&
  11626. (Index < RemoteDirView->Items->Count))
  11627. {
  11628. TListItem * Item = RemoteDirView->Items->Item[Index];
  11629. Visible =
  11630. (RemoteDirView->ItemFullFileName(Item) == FileName) &&
  11631. ListView_IsItemVisible(RemoteDirView->Handle, Index);
  11632. }
  11633. }
  11634. Terminal->ThumbnailVisible(Index, FileName, Visible);
  11635. }
  11636. else if (Command == qccRemoteItemRedraw)
  11637. {
  11638. int Index = Message.LParam;
  11639. if ((Index >= 0) &&
  11640. (Index < RemoteDirView->Items->Count))
  11641. {
  11642. RemoteDirView->InvalidateItem(RemoteDirView->Items->Item[Index]);
  11643. }
  11644. }
  11645. }
  11646. //---------------------------------------------------------------------------
  11647. void TCustomScpExplorerForm::InitializeRemoteThumbnailMask()
  11648. {
  11649. UnicodeString DefaultRemoteThumbnailMask = L"*.jpg;*.jpeg;*.gif;*.png;*.svg;*.bmp;*.raw;*.ico;*.heic;*.webp";
  11650. UnicodeString RemoteThumbnailMask = WinConfiguration->RemoteThumbnailMask;
  11651. if (RemoteThumbnailMask.IsEmpty() ||
  11652. StartsStr(L"+", RemoteThumbnailMask) || StartsStr(L"-", RemoteThumbnailMask) || StartsStr(L"*", RemoteThumbnailMask))
  11653. {
  11654. std::unique_ptr<TStrings> RemoteThumbnailMasks(new TStringList());
  11655. while (!DefaultRemoteThumbnailMask.IsEmpty())
  11656. {
  11657. RemoteThumbnailMasks->Add(CutToChar(DefaultRemoteThumbnailMask, L';', true));
  11658. }
  11659. RemoteThumbnailMasks.reset(ProcessFeatures(RemoteThumbnailMasks.get(), RemoteThumbnailMask));
  11660. RemoteThumbnailMask = EmptyStr;
  11661. for (int Index = 0; Index < RemoteThumbnailMasks->Count; Index++)
  11662. {
  11663. AddToList(RemoteThumbnailMask, RemoteThumbnailMasks->Strings[Index], L";");
  11664. }
  11665. }
  11666. FRemoteThumbnailMask.Masks = RemoteThumbnailMask;
  11667. }
  11668. //---------------------------------------------------------------------------
  11669. void __fastcall TCustomScpExplorerForm::RemoteDirViewStartLoading(TObject *)
  11670. {
  11671. Terminal->StartLoadingDirectory();
  11672. }
  11673. //---------------------------------------------------------------------------
  11674. void __fastcall TCustomScpExplorerForm::RemoteDirViewStartReading(TObject *)
  11675. {
  11676. Terminal->DisableThumbnails();
  11677. }
  11678. //---------------------------------------------------------------------------
  11679. void TCustomScpExplorerForm::ChangeDirViewStyle(TOperationSide Side, TDirViewStyle DirViewStyle)
  11680. {
  11681. TCustomDirView * ADirView = DirView(Side);
  11682. if (ADirView->DirViewStyle != DirViewStyle)
  11683. {
  11684. if (DirViewStyle == dvsThumbnail)
  11685. {
  11686. if (dynamic_cast<TUnixDirView *>(ADirView) != NULL)
  11687. {
  11688. Configuration->Usage->Inc(L"ThumbnailsRemote");
  11689. }
  11690. else
  11691. {
  11692. Configuration->Usage->Inc(L"ThumbnailsLocal");
  11693. }
  11694. }
  11695. ADirView->DirViewStyle = DirViewStyle;
  11696. UpdateControls();
  11697. }
  11698. }