CustomScpExplorer.cpp 417 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385
  1. //---------------------------------------------------------------------------
  2. #include <WinPCH.h>
  3. #pragma hdrstop
  4. #include "CustomScpExplorer.h"
  5. #include <Bookmarks.h>
  6. #include <FileSystems.h>
  7. #include <Progress.h>
  8. #include <SynchronizeProgress.h>
  9. #include <DragExt.h>
  10. #include <WinApi.h>
  11. #include "NonVisual.h"
  12. #include "Glyphs.h"
  13. #include "TerminalManager.h"
  14. #include "EditorManager.h"
  15. #include "ProgParams.h"
  16. #include "Setup.h"
  17. #include <TB2Common.hpp>
  18. #include <DirectoryMonitor.hpp>
  19. #include <mmsystem.h>
  20. //---------------------------------------------------------------------------
  21. #pragma package(smart_init)
  22. #pragma link "CustomDirView"
  23. #pragma link "CustomUnixDirView"
  24. #pragma link "IEListView"
  25. #pragma link "NortonLikeListView"
  26. #pragma link "UnixDirView"
  27. #pragma link "CustomDriveView"
  28. #pragma link "UnixDriveView"
  29. #pragma link "CustomDriveView"
  30. #pragma link "TB2Dock"
  31. #pragma link "TBXStatusBars"
  32. #pragma link "TB2Item"
  33. #pragma link "TB2Toolbar"
  34. #pragma link "ThemePageControl"
  35. #pragma link "PathLabel"
  36. #pragma resource "*.dfm"
  37. //---------------------------------------------------------------------------
  38. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  39. #define WM_PASTE_FILES (WM_WINSCP_USER + 14)
  40. static const int SessionPanelCount = 3;
  41. //---------------------------------------------------------------------------
  42. class TMutexGuard
  43. {
  44. public:
  45. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  46. int Timeout = 5000)
  47. {
  48. FMutex = NULL;
  49. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  50. if (WaitResult == WAIT_TIMEOUT)
  51. {
  52. throw Exception(LoadStr(Message));
  53. }
  54. else
  55. {
  56. FMutex = AMutex;
  57. }
  58. }
  59. ~TMutexGuard()
  60. {
  61. if (FMutex != NULL)
  62. {
  63. ReleaseMutex(FMutex);
  64. }
  65. }
  66. private:
  67. HANDLE FMutex;
  68. };
  69. //---------------------------------------------------------------------------
  70. class TWindowLock
  71. {
  72. public:
  73. TWindowLock(TCustomScpExplorerForm * Form) :
  74. FForm(Form)
  75. {
  76. FForm->LockWindow();
  77. }
  78. ~TWindowLock()
  79. {
  80. FForm->UnlockWindow();
  81. }
  82. private:
  83. TCustomScpExplorerForm * FForm;
  84. };
  85. //---------------------------------------------------------------------------
  86. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  87. FForm(Form)
  88. {
  89. FForm->BatchStart(FBatchStorage);
  90. }
  91. //---------------------------------------------------------------------------
  92. TAutoBatch::~TAutoBatch()
  93. {
  94. FForm->BatchEnd(FBatchStorage);
  95. }
  96. //---------------------------------------------------------------------------
  97. struct TTransferOperationParam
  98. {
  99. TTransferOperationParam();
  100. UnicodeString TargetDirectory;
  101. bool Temp;
  102. bool DragDrop;
  103. int Options;
  104. TAutoSwitch Queue;
  105. };
  106. //---------------------------------------------------------------------------
  107. TTransferOperationParam::TTransferOperationParam()
  108. {
  109. Temp = false;
  110. DragDrop = false;
  111. Options = 0;
  112. Queue = asAuto;
  113. }
  114. //---------------------------------------------------------------------------
  115. class TTransferPresetNoteData : public TObject
  116. {
  117. public:
  118. UnicodeString Message;
  119. };
  120. //---------------------------------------------------------------------------
  121. class TTerminalNoteData : public TObject
  122. {
  123. public:
  124. TManagedTerminal * Terminal;
  125. };
  126. //---------------------------------------------------------------------------
  127. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  128. TForm(Owner),
  129. FFormRestored(false)
  130. {
  131. AddStartupSequence(L"F");
  132. FInvalid = false;
  133. FCurrentSide = osRemote;
  134. FEverShown = false;
  135. FDocks = new TList();
  136. InitControls();
  137. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  138. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  139. NonVisualDataModule->ScpExplorer = this;
  140. RestoreParams();
  141. ConfigurationChanged();
  142. RemoteDirView->Invalidate();
  143. FAutoOperation = false;
  144. FOnFileOperationFinished = NULL;
  145. FPrimaryOperation = foNone;
  146. FForceExecution = false;
  147. FIgnoreNextDialogChar = 0;
  148. FErrorList = NULL;
  149. FSynchronizeProgressForm = NULL;
  150. FProgressForm = NULL;
  151. FRefreshLocalDirectory = false;
  152. FRefreshRemoteDirectory = false;
  153. FDDExtMapFile = NULL;
  154. // CreateMutexW keeps failing with ERROR_NOACCESS
  155. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  156. DebugAssert(FDDExtMutex != NULL);
  157. FDDTargetControl = NULL;
  158. FDelayedDeletionTimer = NULL;
  159. FDelayedDeletionList = new TStringList();
  160. FDDFileList = NULL;
  161. FPendingTempSpaceWarn = false;
  162. FCapturedLog = NULL;
  163. FDragDropOperation = false;
  164. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  165. FAllowTransferPresetAutoSelect = true;
  166. FSessionChanging = false;
  167. FCopyParamDefault = L"";
  168. FSynchronizeController = NULL;
  169. FPendingQueueActionItem = NULL;
  170. FLockLevel = 0;
  171. FLockSuspendLevel = 0;
  172. FDisabledOnLockSuspend = false;
  173. FAlternativeDelete = false;
  174. FTrayIcon = new ::TTrayIcon(0);
  175. FTrayIcon->OnClick = TrayIconClick;
  176. FMaxQueueLength = 0;
  177. FLastContextPopupScreenPoint = TPoint(-1, -1);
  178. FTransferResumeList = NULL;
  179. FDeletedFiles = NULL;
  180. FMoveToQueue = false;
  181. StandaloneOperation = false;
  182. FOnFeedSynchronizeError = NULL;
  183. FOnSynchronizeAbort = NULL;
  184. FSynchronizeTerminal = NULL;
  185. FNeedSession = false;
  186. FDoNotIdleCurrentTerminal = 0;
  187. FIncrementalSearching = 0;
  188. FQueueFileList.reset(new TQueueFileList());
  189. FProgressSide = osCurrent;
  190. FCalculateSizeOperation = NULL;
  191. FDownloadingFromClipboard = false;
  192. FClipboardFakeMonitorsPendingReset = false;
  193. FHiContrastTheme = NULL;
  194. InitializeRemoteThumbnailMask();
  195. GlyphsModule->PixelsPerInch = CurrentPPI;
  196. FEditorManager = new TEditorManager();
  197. FEditorManager->OnFileChange = ExecutedFileChanged;
  198. FEditorManager->OnFileReload = ExecutedFileReload;
  199. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  200. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  201. FLocalEditors = new TList();
  202. FQueueStatus = NULL;
  203. FQueueStatusSection = new TCriticalSection();
  204. FQueueStatusInvalidated = false;
  205. FQueueItemInvalidated = false;
  206. FQueueStatusUpdating = false;
  207. FQueueActedItem = NULL;
  208. FQueueController = new TQueueController(QueueView3);
  209. FQueueScrollOnDragOver = new TListViewScrollOnDragOver(QueueView3, true);
  210. FUserActionTimer = new TTimer(this);
  211. FUserActionTimer->Enabled = false;
  212. FUserActionTimer->Interval = 10;
  213. FUserActionTimer->OnTimer = UserActionTimer;
  214. FNotes = new TStringList();
  215. FNoteTimer = new TTimer(this);
  216. FNoteTimer->Enabled = false;
  217. FNoteTimer->OnTimer = NoteTimer;
  218. FOnNoteClick = NULL;
  219. FOle32Library = LoadLibrary(L"Ole32.dll");
  220. FDragMoveCursor = FOle32Library != NULL ?
  221. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  222. UseSystemSettings(this);
  223. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  224. static_cast<TObject*>(GetComponent(fcTransferList)));
  225. DebugAssert(TransferList != NULL);
  226. FTransferListHoverIndex = -1;
  227. TransferList->OnChange = TransferListChange;
  228. TransferList->OnDrawItem = TransferListDrawItem;
  229. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  230. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  231. UseDesktopFont(SessionsPageControl);
  232. UseDesktopFont(RemoteDirView);
  233. UseDesktopFont(RemoteDriveView);
  234. UseDesktopFont(QueueView3);
  235. UseDesktopFont(QueueFileList);
  236. UseDesktopFont(QueueLabel);
  237. UseDesktopFont(RemoteStatusBar);
  238. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  239. QueueSplitter->ShowHint = true;
  240. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  241. QueueFileListSplitter->ShowHint = true;
  242. RemotePanelSplitter->ShowHint = true;
  243. UpdateImages();
  244. FCustomCommandMenu = CreateTBXPopupMenu(this);
  245. FCustomCommandLocalFileList = NULL;
  246. FCustomCommandRemoteFileList = NULL;
  247. FSessionColors = new TPngImageList(this);
  248. FSessionColors->ColorDepth = cd32Bit;
  249. AddFixedSessionImages();
  250. SessionsPageControl->Images = FSessionColors;
  251. // Needed for Explorer (Commander calls it implicitly on its own)
  252. UpdateSessionsPageControlHeight();
  253. UpdateQueueLabel();
  254. CheckStoreTransition();
  255. CreateHiddenWindow();
  256. StartUpdates();
  257. // Start loading drive data, so that they are ready once we need them for StartCreationDirectoryMonitorsOnEachDrive
  258. // (necessary for "explorer" only, as "commander" loads it for its drive menu)
  259. DriveInfo->NeedData();
  260. UsesCustomColorMode(this);
  261. }
  262. //---------------------------------------------------------------------------
  263. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  264. {
  265. AppLog(L"Destroying main window");
  266. FInvalid = true;
  267. ReleaseHiContrastTheme();
  268. if (FClipboardTerminal != NULL)
  269. {
  270. ClipboardClear(); // implies ClipboardStop
  271. }
  272. // this has to be one of the very first things to do
  273. StopUpdateThread();
  274. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  275. TTerminalManager::Instance()->ScpExplorer = NULL;
  276. delete FCustomCommandLocalFileList;
  277. delete FCustomCommandRemoteFileList;
  278. delete FCustomCommandMenu;
  279. delete FTrayIcon;
  280. FTrayIcon = NULL;
  281. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  282. delete FEditorManager;
  283. ForceCloseLocalEditors();
  284. delete FLocalEditors;
  285. HideFileFindDialog();
  286. if (FDelayedDeletionTimer)
  287. {
  288. DoDelayedDeletion(NULL);
  289. SAFE_DESTROY(FDelayedDeletionTimer);
  290. }
  291. SAFE_DESTROY(FDelayedDeletionList);
  292. // sometimes we do not get DDEnd so the list is not released
  293. SAFE_DESTROY(FDDFileList);
  294. DebugAssert(FSynchronizeController == NULL);
  295. CloseHandle(FDDExtMutex);
  296. FDDExtMutex = NULL;
  297. FreeLibrary(FOle32Library);
  298. FOle32Library = NULL;
  299. FDragMoveCursor = NULL;
  300. DebugAssert(!FErrorList);
  301. if (FEverShown)
  302. {
  303. // when window is never shown (like when running command-line operation),
  304. // particularly window site is not restored correctly (BoundsRect value set
  305. // in RestoreForm gets lost during handle allocation), so we do not want
  306. // it to be stored
  307. AppLog(L"Storing main window configuration");
  308. StoreParams();
  309. }
  310. ManagedSession = NULL;
  311. Queue = NULL;
  312. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  313. NonVisualDataModule->ScpExplorer = NULL;
  314. delete FQueueScrollOnDragOver;
  315. FQueueScrollOnDragOver = NULL;
  316. delete FQueueController;
  317. FQueueController = NULL;
  318. delete FQueueStatusSection;
  319. FQueueStatusSection = NULL;
  320. delete FQueueStatus;
  321. FQueueStatus = NULL;
  322. delete FUserActionTimer;
  323. FUserActionTimer = NULL;
  324. delete FNoteTimer;
  325. delete FNotes;
  326. delete FNoteData;
  327. SAFE_DESTROY(FDocks);
  328. SessionsPageControl->Images = NULL;
  329. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  330. SAFE_DESTROY(FSessionColors);
  331. SAFE_DESTROY(FSessionsDragDropFilesEx);
  332. SAFE_DESTROY(FHistoryMenu[0][0]);
  333. SAFE_DESTROY(FHistoryMenu[0][1]);
  334. SAFE_DESTROY(FHistoryMenu[1][0]);
  335. SAFE_DESTROY(FHistoryMenu[1][1]);
  336. if (FHiddenWindow != NULL)
  337. {
  338. DestroyWindow(FHiddenWindow);
  339. FHiddenWindow = NULL;
  340. }
  341. AppLog(L"Main window destroyed");
  342. }
  343. //---------------------------------------------------------------------------
  344. void TCustomScpExplorerForm::ReleaseHiContrastTheme()
  345. {
  346. if (FHiContrastTheme != NULL)
  347. {
  348. ReleaseTBXTheme(FHiContrastTheme);
  349. FHiContrastTheme = NULL;
  350. }
  351. }
  352. //---------------------------------------------------------------------------
  353. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  354. {
  355. std::unique_ptr<TSessionData> Data;
  356. if (!Session.IsEmpty())
  357. {
  358. bool DefaultsOnly;
  359. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  360. }
  361. TTerminalManager * Manager = TTerminalManager::Instance();
  362. for (int Index = 0; Index < Manager->Count; Index++)
  363. {
  364. TTerminal * Terminal = Manager->Sessions[Index];
  365. if (Session.IsEmpty() ||
  366. Manager->IsAvailableTerminalForSite(Terminal, Data.get()))
  367. {
  368. if (Path.IsEmpty())
  369. {
  370. Terminal->ClearCaches();
  371. }
  372. else
  373. {
  374. Terminal->DirectoryModified(Path, true);
  375. }
  376. }
  377. }
  378. // We should flag a pending refresh for the background terminals or busy foreground terminals
  379. if (HasAvailableTerminal() &&
  380. CanCommandLineFromAnotherInstance() &&
  381. (Session.IsEmpty() || Manager->IsAvailableTerminalForSite(Terminal, Data.get())) &&
  382. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  383. {
  384. Terminal->ReloadDirectory();
  385. }
  386. }
  387. //---------------------------------------------------------------------------
  388. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  389. {
  390. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  391. AppLogFmt(L"Received copy data message with size %d", (int(CopyData->cbData)));
  392. size_t MessageSize = sizeof(TCopyDataMessage);
  393. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  394. if (Result)
  395. {
  396. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  397. Result = (Message.Version == TCopyDataMessage::Version1);
  398. if (Result)
  399. {
  400. switch (Message.Command)
  401. {
  402. case TCopyDataMessage::CommandCanCommandLine:
  403. Result = CanCommandLineFromAnotherInstance();
  404. if (Result)
  405. {
  406. AppLog(L"Can process command-line from other instance");
  407. }
  408. else
  409. {
  410. AppLog(L"Cannot process command-line from other instance");
  411. }
  412. break;
  413. case TCopyDataMessage::CommandCommandLine:
  414. {
  415. UnicodeString CommandLine(Message.Data.CommandLine);
  416. Result = CommandLineFromAnotherInstance(CommandLine);
  417. }
  418. break;
  419. case TCopyDataMessage::RefreshPanel:
  420. RefreshPanel(Message.Data.Refresh.Session, Message.Data.Refresh.Path);
  421. break;
  422. case TCopyDataMessage::MainWindowCheck:
  423. Result = true;
  424. break;
  425. default:
  426. Result = false;
  427. break;
  428. }
  429. }
  430. }
  431. Message.Result = Result ? 1 : 0;
  432. }
  433. //---------------------------------------------------------------------------
  434. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  435. {
  436. WNDCLASS WindowClass = {};
  437. WindowClass.lpfnWndProc = DefWindowProc;
  438. WindowClass.hInstance = HInstance;
  439. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  440. FHiddenWindow = NULL;
  441. if (RegisterClass(&WindowClass))
  442. {
  443. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  444. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  445. }
  446. }
  447. //---------------------------------------------------------------------------
  448. bool __fastcall TCustomScpExplorerForm::CanConsole()
  449. {
  450. return
  451. HasAvailableTerminal() &&
  452. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  453. }
  454. //---------------------------------------------------------------------------
  455. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  456. {
  457. bool Result = Visible && !NonVisualDataModule->Busy;
  458. return Result;
  459. }
  460. //---------------------------------------------------------------------------
  461. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  462. const UnicodeString & CommandLine)
  463. {
  464. TProgramParams Params(CommandLine);
  465. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  466. if (Result)
  467. {
  468. NonVisualDataModule->StartBusy();
  469. try
  470. {
  471. // this action is initiated from another process,
  472. // so it's likely that our window is not visible,
  473. // and user won't see what is going on
  474. Application->BringToFront();
  475. // reload sessions as we may be asked to open a session
  476. // just stored by another instance
  477. StoredSessions->Reload();
  478. UnicodeString SessionName = Params.Param[1];
  479. std::unique_ptr<TObjectList> DataList(new TObjectList());
  480. try
  481. {
  482. UnicodeString DownloadFile; // unused
  483. int Flags = GetCommandLineParseUrlFlags(&Params);
  484. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  485. }
  486. catch (EAbort &)
  487. {
  488. // Happens when opening session in PuTTY and in other situations,
  489. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  490. // to process the command-line on its own (or to pass it to yet another instance)
  491. }
  492. if (DataList->Count > 0)
  493. {
  494. TTerminalManager * Manager = TTerminalManager::Instance();
  495. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  496. }
  497. }
  498. __finally
  499. {
  500. NonVisualDataModule->EndBusy();
  501. }
  502. }
  503. return Result;
  504. }
  505. //---------------------------------------------------------------------------
  506. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  507. {
  508. TManagedTerminal * Result;
  509. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  510. {
  511. Result = FManagedSession;
  512. }
  513. else
  514. {
  515. Result = NULL;
  516. }
  517. return Result;
  518. }
  519. //---------------------------------------------------------------------------
  520. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  521. {
  522. if (FManagedSession != value)
  523. {
  524. TWindowLock Lock(this);
  525. SessionChanging();
  526. DoSetManagedSession(value, false);
  527. }
  528. }
  529. //---------------------------------------------------------------------------
  530. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  531. {
  532. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  533. FManagedSession = value;
  534. {
  535. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect, false);
  536. TAutoFlag SessionChangingFlag(FSessionChanging);
  537. SessionChanged(Replace);
  538. }
  539. if (Terminal != NULL)
  540. {
  541. TransferPresetAutoSelect();
  542. }
  543. // Update app and tab titles, prevented above by FSessionChanging.
  544. UpdateControls();
  545. }
  546. //---------------------------------------------------------------------------
  547. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  548. {
  549. FEditorManager->ProcessFiles(FileTerminalReplaced, value);
  550. DoSetManagedSession(value, true);
  551. }
  552. //---------------------------------------------------------------------------
  553. void __fastcall TCustomScpExplorerForm::SessionChanging()
  554. {
  555. if (ManagedSession != NULL)
  556. {
  557. UpdateSession(ManagedSession);
  558. }
  559. }
  560. //---------------------------------------------------------------------------
  561. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  562. {
  563. if (ManagedSession != NULL)
  564. {
  565. UpdateSessionColor(static_cast<TColor>(ManagedSession->StateData->Color));
  566. }
  567. if (!TTerminalManager::Instance()->Updating)
  568. {
  569. SessionListChanged();
  570. }
  571. if (Terminal != NULL)
  572. {
  573. TObject * State = NULL;
  574. if (WinConfiguration->PreservePanelState)
  575. {
  576. State = Terminal->RemoteExplorerState;
  577. DirView(osRemote)->AnnounceState(State);
  578. }
  579. try
  580. {
  581. if (Replaced)
  582. {
  583. RemoteDirView->ReplaceTerminal(Terminal);
  584. }
  585. else
  586. {
  587. RemoteDirView->Terminal = Terminal;
  588. }
  589. if (Terminal->Active)
  590. {
  591. Terminal->RefreshDirectory();
  592. }
  593. if (State != NULL)
  594. {
  595. DirView(osRemote)->RestoreState(State);
  596. }
  597. }
  598. __finally
  599. {
  600. DirView(osRemote)->AnnounceState(NULL);
  601. }
  602. }
  603. else
  604. {
  605. RemoteDirView->Terminal = NULL;
  606. }
  607. InitStatusBar();
  608. UpdateTransferList();
  609. // Update panels Enable state before refreshing the labels
  610. UpdateControls();
  611. }
  612. //---------------------------------------------------------------------------
  613. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  614. {
  615. if (Queue != value)
  616. {
  617. if (FQueue != NULL)
  618. {
  619. FQueue->OnListUpdate = NULL;
  620. FQueue->OnQueueItemUpdate = NULL;
  621. }
  622. FQueue = value;
  623. if (FQueue != NULL)
  624. {
  625. DebugAssert(FQueue->OnListUpdate == NULL);
  626. FQueue->OnListUpdate = QueueListUpdate;
  627. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  628. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  629. }
  630. QueueChanged();
  631. }
  632. }
  633. //---------------------------------------------------------------------------
  634. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  635. TListItem * Item)
  636. {
  637. if (FQueueActedItem == Item)
  638. {
  639. FQueueActedItem = NULL;
  640. if ((QueueView3->PopupMenu != NULL) &&
  641. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  642. {
  643. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  644. ReleaseCapture();
  645. }
  646. }
  647. if (Item->Data == FPendingQueueActionItem)
  648. {
  649. FPendingQueueActionItem = NULL;
  650. }
  651. }
  652. //---------------------------------------------------------------------------
  653. bool TCustomScpExplorerForm::IsAnythingQueued()
  654. {
  655. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  656. }
  657. //---------------------------------------------------------------------------
  658. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  659. {
  660. {
  661. TGuard Guard(FQueueStatusSection);
  662. FQueueStatusInvalidated = false;
  663. if (FQueue != NULL)
  664. {
  665. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  666. }
  667. }
  668. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  669. {
  670. FMaxQueueLength = FQueueStatus->Count;
  671. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  672. }
  673. {
  674. TAutoFlag Flag(FQueueStatusUpdating);
  675. FQueueController->UpdateQueueStatus(FQueueStatus);
  676. }
  677. UpdateQueueFileList();
  678. SetQueueProgress();
  679. UpdateQueueView();
  680. bool IsEmpty = !IsAnythingQueued();
  681. if (IsEmpty && (Terminal != NULL))
  682. {
  683. TOnceDoneOperation OnceDoneOperation =
  684. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  685. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  686. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  687. {
  688. FQueue->Enabled = false;
  689. }
  690. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  691. {
  692. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  693. }
  694. }
  695. }
  696. //---------------------------------------------------------------------------
  697. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  698. {
  699. UnicodeString Result;
  700. if (FQueueStatus != NULL)
  701. {
  702. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  703. {
  704. TFileOperationProgressType * ProgressData =
  705. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  706. if ((ProgressData != NULL) && ProgressData->InProgress)
  707. {
  708. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  709. }
  710. }
  711. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  712. {
  713. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  714. }
  715. }
  716. return Result;
  717. }
  718. //---------------------------------------------------------------------------
  719. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  720. {
  721. ComponentVisible[fcQueueView] =
  722. !IsLocalBrowserMode() &&
  723. ((WinConfiguration->QueueView.Show == qvShow) ||
  724. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  725. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  726. }
  727. //---------------------------------------------------------------------------
  728. void __fastcall TCustomScpExplorerForm::QueueChanged()
  729. {
  730. if (FQueueStatus != NULL)
  731. {
  732. // UpdateFileList implementation relies on the status being recreated when session changes
  733. delete FQueueStatus;
  734. FQueueStatus = NULL;
  735. }
  736. UpdateQueueStatus(true);
  737. }
  738. //---------------------------------------------------------------------------
  739. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  740. {
  741. if (FQueue == Queue)
  742. {
  743. FQueueStatusInvalidated = true;
  744. }
  745. }
  746. //---------------------------------------------------------------------------
  747. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  748. TQueueItem * Item)
  749. {
  750. if (FQueue == Queue)
  751. {
  752. TGuard Guard(FQueueStatusSection);
  753. // this may be running in parallel with QueueChanged
  754. if (FQueueStatus != NULL)
  755. {
  756. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  757. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  758. {
  759. FRefreshLocalDirectory = (QueueItem == NULL) ||
  760. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  761. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  762. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  763. }
  764. if (QueueItem != NULL)
  765. {
  766. QueueItem->UserData = (void*)true;
  767. FQueueItemInvalidated = true;
  768. }
  769. }
  770. }
  771. }
  772. //---------------------------------------------------------------------------
  773. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  774. {
  775. // during standalone editing, we have no way to see/control queue,
  776. // so we have to always popup prompts automatically
  777. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  778. }
  779. //---------------------------------------------------------------------------
  780. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  781. {
  782. if (FQueueStatus != NULL)
  783. {
  784. bool QueueAutoPopup = IsQueueAutoPopup();
  785. bool NeedRefresh = FQueueController->NeedRefresh();
  786. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  787. FQueueItemInvalidated = false;
  788. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  789. bool Updated = false;
  790. TQueueItemProxy * QueueItem;
  791. bool WasUserAction;
  792. for (int Index = 0; Index < Limit; Index++)
  793. {
  794. bool Update = false;
  795. QueueItem = FQueueStatus->Items[Index];
  796. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  797. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  798. (FPendingQueueActionItem == NULL))
  799. {
  800. FPendingQueueActionItem = QueueItem;
  801. FUserActionTimer->Enabled = true;
  802. }
  803. if ((bool)QueueItem->UserData)
  804. {
  805. QueueItem->UserData = (void*)false;
  806. QueueItem->Update();
  807. Updated = true;
  808. Update = true;
  809. }
  810. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  811. {
  812. Update = true;
  813. }
  814. if (Update)
  815. {
  816. FQueueController->RefreshQueueItem(QueueItem);
  817. }
  818. }
  819. if (Updated)
  820. {
  821. NonVisualDataModule->UpdateNonVisibleActions();
  822. SetQueueProgress();
  823. UpdateQueueFileList();
  824. }
  825. }
  826. }
  827. //---------------------------------------------------------------------------
  828. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  829. {
  830. if (FTaskbarList != NULL)
  831. {
  832. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  833. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  834. }
  835. }
  836. //---------------------------------------------------------------------------
  837. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  838. {
  839. if (Progress >= 0)
  840. {
  841. if (FTaskbarList != NULL)
  842. {
  843. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  844. }
  845. }
  846. else
  847. {
  848. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  849. }
  850. }
  851. //---------------------------------------------------------------------------
  852. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  853. {
  854. int OverallProgress;
  855. // FProgressForm is null when this is called from SetQueueProgress
  856. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  857. {
  858. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  859. }
  860. else if (!ProgressData->IsIndeterminate())
  861. {
  862. OverallProgress = ProgressData->OverallProgress();
  863. }
  864. else
  865. {
  866. OverallProgress = -1;
  867. }
  868. SetTaskbarListProgressValue(OverallProgress);
  869. }
  870. //---------------------------------------------------------------------------
  871. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  872. {
  873. TTerminalManager::Instance()->QueueStatusUpdated();
  874. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  875. {
  876. // Do not animate task bar button, when items are queued, but processing is disabled
  877. if ((FQueueStatus != NULL) && (FQueueStatus->ActivePrimaryCount > 0))
  878. {
  879. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  880. {
  881. TFileOperationProgressType * ProgressData = NULL;
  882. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  883. {
  884. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  885. }
  886. if ((ProgressData != NULL) &&
  887. ProgressData->InProgress)
  888. {
  889. SetTaskbarListProgressValue(ProgressData);
  890. }
  891. else
  892. {
  893. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  894. }
  895. }
  896. else
  897. {
  898. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  899. }
  900. }
  901. else
  902. {
  903. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  904. }
  905. }
  906. UpdateQueueLabel();
  907. }
  908. //---------------------------------------------------------------------------
  909. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  910. {
  911. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  912. if (FQueueStatus != NULL)
  913. {
  914. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  915. if (ActiveAndPendingPrimaryCount > 0)
  916. {
  917. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  918. }
  919. }
  920. QueueLabel->Caption = Caption;
  921. }
  922. //---------------------------------------------------------------------------
  923. void TCustomScpExplorerForm::CheckStoreTransition()
  924. {
  925. if (WinConfiguration->StoreTransition == stInit)
  926. {
  927. if (IsUWP())
  928. {
  929. WinConfiguration->StoreTransition = stStoreFresh;
  930. AppLog(L"Fresh UWP/store installation");
  931. }
  932. else
  933. {
  934. WinConfiguration->StoreTransition = stStandard;
  935. AppLog(L"Fresh standard installation");
  936. }
  937. }
  938. else if ((WinConfiguration->StoreTransition == stStandard) && IsUWP())
  939. {
  940. WinConfiguration->StoreTransition = stStoreMigrated;
  941. WinConfiguration->FirstRun = WinConfiguration->FirstRun + L"=>" + StandardDatestamp();
  942. AppLog(L"Standard installation migrated to store installation");
  943. }
  944. if (WinConfiguration->StoreTransition == stStoreMigrated)
  945. {
  946. TTBXToolbar * Toolbar = new TTBXToolbar(MessageDock);
  947. Toolbar->Parent = MessageDock;
  948. Toolbar->FullSize = true;
  949. Toolbar->Color = Application->HintColor;
  950. TTBXLabelItem * LabelItem;
  951. LabelItem = new TTBXLabelItem(Toolbar);
  952. // Convert to explicit RGB "black" color, to make sure it is contrasting even in Dark theme
  953. // (TTBXOfficeXPTheme.GetStandardColor would convert clWindow to "white" in dark theme)
  954. LabelItem->FontSettings->Color = static_cast<TColor>(GetSysColor(clWindow));
  955. LabelItem->Margin = ScaleByTextHeight(MessageDock, 2); // Is this automatically rescaled?
  956. LabelItem->Caption = LoadStr(STORE_MIGRATION_THANKS);
  957. Toolbar->Items->Add(LabelItem);
  958. TTBXCustomItem * LinkItem = new TTBXItem(Toolbar);
  959. LinkItem->FontSettings->Color = GetLightLinkColor(); // the background is not themed yet
  960. LinkItem->Caption = LoadStr(STORE_MIGRATION_LEARN);
  961. LinkItem->OnClick = StoreTransitionLinkClick;
  962. Toolbar->Items->Add(LinkItem);
  963. AddMenuSeparator(Toolbar->Items);
  964. TTBXCustomItem * CloseItem = new TTBXItem(Toolbar);
  965. CloseItem->FontSettings->Color = static_cast<TColor>(GetSysColor(clWindow));
  966. CloseItem->Caption = LoadStr(MESSAGE_DISMISS);
  967. CloseItem->OnClick = StoreTransitionCloseClick;
  968. Toolbar->Items->Add(CloseItem);
  969. GiveTBItemPriority(CloseItem);
  970. }
  971. }
  972. //---------------------------------------------------------------------------
  973. void __fastcall TCustomScpExplorerForm::StoreTransitionLinkClick(TObject *)
  974. {
  975. ShowHelp(HELP_STORE_TRANSITION);
  976. }
  977. //---------------------------------------------------------------------------
  978. void __fastcall TCustomScpExplorerForm::StoreTransitionCloseClick(TObject * Sender)
  979. {
  980. TTBCustomItem * Item = DebugNotNull(dynamic_cast<TTBCustomItem *>(Sender));
  981. TTBXToolbar * Toolbar = DebugNotNull(dynamic_cast<TTBXToolbar *>(DebugNotNull(DebugNotNull(Item->Parent)->ParentComponent)));
  982. delete Toolbar;
  983. WinConfiguration->StoreTransition = stStoreAcknowledged;
  984. UpdateControls();
  985. }
  986. //---------------------------------------------------------------------------
  987. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  988. {
  989. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  990. static_cast<TComponent*>(GetComponent(fcTransferList)));
  991. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  992. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  993. TransferList->Strings->BeginUpdate();
  994. try
  995. {
  996. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  997. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  998. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  999. if (FTransferDropDownHint.IsEmpty())
  1000. {
  1001. FTransferDropDownHint = TransferDropDown->Hint;
  1002. }
  1003. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  1004. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  1005. TransferDropDown->Text = StripHotkey(Name);
  1006. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  1007. (FTransferDropDownHint, StripHotkey(Name),
  1008. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  1009. FLAGMASK(HasAvailableTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  1010. // update the label, otherwise when it is updated only on the first draw
  1011. // of the list, it is drawn "bold" for some reason
  1012. FTransferListHoverIndex = TransferList->ItemIndex;
  1013. UpdateTransferLabel();
  1014. }
  1015. __finally
  1016. {
  1017. TransferList->Strings->EndUpdate();
  1018. }
  1019. }
  1020. //---------------------------------------------------------------------------
  1021. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  1022. {
  1023. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  1024. DebugAssert(Toolbar != NULL);
  1025. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  1026. }
  1027. //---------------------------------------------------------------------------
  1028. void __fastcall TCustomScpExplorerForm::UpdateActions()
  1029. {
  1030. TForm::UpdateActions();
  1031. if (ComponentVisible[fcCustomCommandsBand])
  1032. {
  1033. UpdateCustomCommandsToolbar();
  1034. }
  1035. }
  1036. //---------------------------------------------------------------------------
  1037. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  1038. {
  1039. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  1040. }
  1041. //---------------------------------------------------------------------------
  1042. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  1043. {
  1044. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  1045. {
  1046. DirView->RowSelect = WinConfiguration->FullRowSelect;
  1047. // Without this, the panel turns gray and unused part of header turns black
  1048. DirView->Perform(CM_COLORCHANGED, 0, 0);
  1049. }
  1050. }
  1051. //---------------------------------------------------------------------------
  1052. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  1053. {
  1054. Color = GetBtnFaceColor();
  1055. DebugAssert(Configuration && RemoteDirView);
  1056. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1057. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  1058. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  1059. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  1060. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1061. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1062. RemoteDirView->AlwaysSortDirectoriesByName = WinConfiguration->AlwaysSortDirectoriesByName;
  1063. UpdateRowSelect(RemoteDirView);
  1064. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1065. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  1066. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  1067. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1068. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1069. UpdateSessionsPageControlHeight();
  1070. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1071. UpdateToolbarDisplayMode();
  1072. UpdateTransferList();
  1073. if (Terminal != NULL)
  1074. {
  1075. TransferPresetAutoSelect();
  1076. }
  1077. UpdateQueueView();
  1078. UpdateControls();
  1079. // this can be called even before constuctor finishes.
  1080. if (FEditorManager != NULL)
  1081. {
  1082. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  1083. }
  1084. // this can be called even before constuctor finishes.
  1085. if (FLocalEditors != NULL)
  1086. {
  1087. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  1088. {
  1089. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  1090. }
  1091. }
  1092. if (ComponentVisible[fcCustomCommandsBand])
  1093. {
  1094. UpdateCustomCommandsToolbar();
  1095. }
  1096. // show only when keeping queue items forever,
  1097. // otherwise, it's enough to have it in the context menu
  1098. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1099. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1100. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1101. {
  1102. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1103. FFileColorsCurrent = WinConfiguration->FileColors;
  1104. FileColorsChanged();
  1105. }
  1106. if ((GetNewTabTab()->CaptionTruncation != GetNewTabTabCaptionTruncation()) &&
  1107. (FSessionColors != NULL))
  1108. {
  1109. SessionListChanged();
  1110. }
  1111. if (GlyphsModule->LargerToolbar != WinConfiguration->LargerToolbar)
  1112. {
  1113. GlyphsModule->LargerToolbar = WinConfiguration->LargerToolbar;
  1114. if (FSessionColors != NULL)
  1115. {
  1116. RegenerateSessionColorsImageList();
  1117. UpdateSessionsPageControlHeight();
  1118. }
  1119. }
  1120. }
  1121. //---------------------------------------------------------------------------
  1122. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1123. {
  1124. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1125. DirView->Invalidate();
  1126. }
  1127. //---------------------------------------------------------------------------
  1128. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1129. {
  1130. DoFileColorsChanged(RemoteDirView);
  1131. }
  1132. //---------------------------------------------------------------------------
  1133. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1134. const UnicodeString DebugUsedArg(FileName), TEditedFileData * /*Data*/, TObject * Token,
  1135. void * /*Arg*/)
  1136. {
  1137. if (Token != NULL)
  1138. {
  1139. TForm * Editor = dynamic_cast<TForm*>(Token);
  1140. DebugAssert(Editor != NULL);
  1141. ReconfigureEditorForm(Editor);
  1142. }
  1143. }
  1144. //---------------------------------------------------------------------------
  1145. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1146. {
  1147. WinConfiguration->DDTransferConfirmation = asOn;
  1148. }
  1149. //---------------------------------------------------------------------------
  1150. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1151. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1152. {
  1153. // noop
  1154. }
  1155. //---------------------------------------------------------------------------
  1156. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1157. {
  1158. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1159. }
  1160. //---------------------------------------------------------------------------
  1161. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1162. {
  1163. if (DoNotShowAgain)
  1164. {
  1165. if (DragDrop)
  1166. {
  1167. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1168. {
  1169. PopupTrayBalloon(
  1170. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1171. }
  1172. WinConfiguration->DDTransferConfirmation = asOff;
  1173. }
  1174. else
  1175. {
  1176. WinConfiguration->ConfirmTransferring = false;
  1177. }
  1178. }
  1179. else
  1180. {
  1181. // User explicitly unchecked "do not show again",
  1182. // so show him the dialog the next time
  1183. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1184. {
  1185. WinConfiguration->DDTransferConfirmation = asOn;
  1186. }
  1187. }
  1188. }
  1189. //---------------------------------------------------------------------------
  1190. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1191. TTransferDirection Direction, TTransferType Type, bool Temp,
  1192. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1193. bool Confirm, bool DragDrop, int Options)
  1194. {
  1195. bool Result = true;
  1196. DebugAssert(HasAvailableTerminal());
  1197. // these parameters are known in advance
  1198. int Params =
  1199. FLAGMASK(Type == ttMove, cpDelete);
  1200. bool ToTemp = (Temp && (Direction == tdToLocal));
  1201. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1202. {
  1203. bool DisableNewerOnly =
  1204. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1205. ToTemp;
  1206. Options |=
  1207. FLAGMASK(ToTemp, coTemp) |
  1208. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1209. coDoNotShowAgain;
  1210. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1211. int CopyParamAttrs = (Direction == tdToRemote ?
  1212. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1213. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1214. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1215. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1216. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1217. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1218. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1219. if (Result)
  1220. {
  1221. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1222. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1223. }
  1224. }
  1225. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1226. {
  1227. Configuration->Usage->Inc(L"TransfersOnBackground");
  1228. // these parameter are known only after transfer dialog
  1229. Params |=
  1230. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1231. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1232. Result = false;
  1233. ClearTransferSourceSelection(Direction);
  1234. }
  1235. return Result;
  1236. }
  1237. //---------------------------------------------------------------------------
  1238. void TCustomScpExplorerForm::ClearOperationSelection(TOperationSide Side)
  1239. {
  1240. if (FOnFileOperationFinished != NULL)
  1241. {
  1242. FOnFileOperationFinished(Side, UnicodeString(), true, true);
  1243. }
  1244. else
  1245. {
  1246. if (HasDirView[Side])
  1247. {
  1248. DirView(Side)->SelectAll(smNone);
  1249. }
  1250. }
  1251. }
  1252. //---------------------------------------------------------------------------
  1253. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1254. {
  1255. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1256. ClearOperationSelection(Side);
  1257. }
  1258. //---------------------------------------------------------------------------
  1259. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1260. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1261. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1262. int Params)
  1263. {
  1264. DebugAssert(Queue != NULL);
  1265. TQueueItem * QueueItem;
  1266. if (Direction == tdToRemote)
  1267. {
  1268. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1269. QueueItem =
  1270. new TUploadQueueItem(
  1271. Terminal, FileList, TargetDirectory, &CopyParam, Params, CopyParam.QueueParallel);
  1272. }
  1273. else
  1274. {
  1275. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1276. QueueItem =
  1277. new TDownloadQueueItem(
  1278. Terminal, FileList, TargetDirectory, &CopyParam, Params, CopyParam.QueueParallel);
  1279. }
  1280. AddQueueItem(Queue, QueueItem, Terminal);
  1281. }
  1282. //---------------------------------------------------------------------------
  1283. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1284. {
  1285. if (Queue->IsEmpty)
  1286. {
  1287. Terminal->QueueOperationStart = Now();
  1288. }
  1289. Queue->AddItem(QueueItem);
  1290. }
  1291. //---------------------------------------------------------------------------
  1292. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1293. {
  1294. }
  1295. //---------------------------------------------------------------------------
  1296. void __fastcall TCustomScpExplorerForm::InitControls()
  1297. {
  1298. DebugAssert(FDocks != NULL);
  1299. for (int Index = 0; Index < ComponentCount; Index++)
  1300. {
  1301. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1302. if ((Dock != NULL) && (Dock->Tag == 0))
  1303. {
  1304. FDocks->Add(Dock);
  1305. }
  1306. }
  1307. CollectItemsWithTextDisplayMode(this);
  1308. }
  1309. //---------------------------------------------------------------------------
  1310. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1311. {
  1312. ConfigurationChanged();
  1313. QueuePanel->Height =
  1314. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1315. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1316. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1317. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1318. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1319. QueueFileList->Height =
  1320. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1321. if (QueueFileList->Visible)
  1322. {
  1323. AdjustQueueLayout();
  1324. }
  1325. UpdateCustomCommandsToolbar();
  1326. }
  1327. //---------------------------------------------------------------------------
  1328. void __fastcall TCustomScpExplorerForm::StoreParams()
  1329. {
  1330. int APixelsPerInch = GetControlPixelsPerInch(this);
  1331. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1332. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1333. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1334. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1335. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1336. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1337. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1338. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1339. }
  1340. //---------------------------------------------------------------------------
  1341. void __fastcall TCustomScpExplorerForm::Loaded()
  1342. {
  1343. // Loaded is called in more deterministic moment than previously used CreateParams
  1344. if (!FFormRestored)
  1345. {
  1346. FFormRestored = true;
  1347. RestoreFormParams();
  1348. }
  1349. TForm::Loaded();
  1350. }
  1351. //---------------------------------------------------------------------------
  1352. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1353. {
  1354. DebugAssert(FDocks != NULL);
  1355. for (int Index = 0; Index < FDocks->Count; Index++)
  1356. {
  1357. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1358. }
  1359. }
  1360. //---------------------------------------------------------------------------
  1361. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1362. {
  1363. SetDockAllowDrag(true);
  1364. while (!ButtonsStr.IsEmpty())
  1365. {
  1366. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1367. UnicodeString Name = CutToChar(S, L'=', true);
  1368. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1369. {
  1370. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1371. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1372. {
  1373. while (!S.IsEmpty())
  1374. {
  1375. UnicodeString S2 = CutToChar(S, L',', true);
  1376. UnicodeString Name = CutToChar(S2, L':', true);
  1377. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1378. {
  1379. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1380. if (SameText(GetToolbarItemName(Item), Name))
  1381. {
  1382. // forward compatibility
  1383. UnicodeString S3 = CutToChar(S2, L':', true);
  1384. bool Visible = StrToIntDef(S3, 0);
  1385. Item->Visible = Visible;
  1386. }
  1387. }
  1388. }
  1389. }
  1390. }
  1391. }
  1392. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1393. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1394. }
  1395. //---------------------------------------------------------------------------
  1396. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1397. {
  1398. UnicodeString Result;
  1399. SetDockAllowDrag(true);
  1400. Result = ::GetToolbarsLayoutStr(this);
  1401. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1402. return Result;
  1403. }
  1404. //---------------------------------------------------------------------------
  1405. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1406. {
  1407. UnicodeString Result;
  1408. if (Item->Action != NULL)
  1409. {
  1410. Result = Item->Action->Name;
  1411. Result = RemoveSuffix(Result, L"Action", true);
  1412. }
  1413. else
  1414. {
  1415. Result = Item->Name;
  1416. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1417. Result = RemoveSuffix(Result, L"Item", true);
  1418. }
  1419. return Result;
  1420. }
  1421. //---------------------------------------------------------------------------
  1422. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1423. {
  1424. UnicodeString Result;
  1425. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1426. {
  1427. UnicodeString ToolbarStr;
  1428. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1429. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1430. {
  1431. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1432. {
  1433. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1434. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1435. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1436. // and save the changes here only.
  1437. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1438. {
  1439. UnicodeString Name = GetToolbarItemName(Item);
  1440. DebugAssert(Name.Pos(L"TBX") == 0);
  1441. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1442. }
  1443. }
  1444. }
  1445. if (!ToolbarStr.IsEmpty())
  1446. {
  1447. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1448. }
  1449. }
  1450. return Result;
  1451. }
  1452. //---------------------------------------------------------------------------
  1453. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1454. {
  1455. DebugAssert(FProgressForm == NULL);
  1456. bool AllowSkip = HasAvailableTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1457. bool AllowMoveToQueue = (FTransferResumeList != NULL) || (FDeletedFiles != NULL);
  1458. FProgressForm = new TProgressForm(Application, AllowMoveToQueue, AllowSkip, SynchronizeProgress);
  1459. FProgressForm->DeleteLocalToRecycleBin =
  1460. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1461. FProgressForm->DeleteRemoteToRecycleBin =
  1462. HasAvailableTerminal() &&
  1463. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1464. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1465. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1466. LockWindow();
  1467. // Actually, do not know what hides the progress once the operation finishes
  1468. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1469. SetTaskbarListProgressState(TBPF_NORMAL);
  1470. }
  1471. //---------------------------------------------------------------------------
  1472. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1473. {
  1474. UnlockWindow();
  1475. SAFE_DESTROY(FProgressForm);
  1476. SetQueueProgress();
  1477. }
  1478. //---------------------------------------------------------------------------
  1479. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1480. TFileOperationProgressType & ProgressData)
  1481. {
  1482. // Download to temporary local directory
  1483. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1484. {
  1485. bool Continue = true;
  1486. FPendingTempSpaceWarn = false;
  1487. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1488. if (!Continue)
  1489. {
  1490. Abort();
  1491. }
  1492. }
  1493. // operation is being executed and we still didn't show up progress form
  1494. if (ProgressData.InProgress && !FProgressForm)
  1495. {
  1496. CreateProgressForm(NULL);
  1497. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1498. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1499. }
  1500. // operation is finished (or terminated), so we hide progress form
  1501. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1502. {
  1503. DestroyProgressForm();
  1504. if (ProgressData.Operation == foCalculateSize)
  1505. {
  1506. // When calculating size before transfer, the abort caused by
  1507. // cancel flag set due to "MoveToQueue" below,
  1508. // is not propagated back to us in ExecuteFileOperation,
  1509. // as it is silently swallowed.
  1510. // So we explicitly (re)throw it here.
  1511. if (FMoveToQueue)
  1512. {
  1513. Abort();
  1514. }
  1515. }
  1516. else
  1517. {
  1518. if ((ProgressData.Cancel == csContinue) &&
  1519. !FAutoOperation)
  1520. {
  1521. OperationComplete(ProgressData.StartTime);
  1522. }
  1523. }
  1524. }
  1525. if (FProgressForm &&
  1526. // Can happen only during synchronization, when ending one of the operations
  1527. ProgressData.InProgress)
  1528. {
  1529. FProgressForm->SetProgressData(ProgressData);
  1530. DebugAssert(ProgressData.InProgress);
  1531. SetTaskbarListProgressValue(&ProgressData);
  1532. if (FProgressForm->Cancel > csContinue)
  1533. {
  1534. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1535. if (FProgressForm->Cancel == csCancelFile)
  1536. {
  1537. FProgressForm->ClearCancel();
  1538. }
  1539. // cancel cancels even the move
  1540. FMoveToQueue = false;
  1541. }
  1542. else if (FProgressForm->MoveToQueue)
  1543. {
  1544. FMoveToQueue = true;
  1545. ProgressData.SetCancelAtLeast(csCancel);
  1546. }
  1547. if ((FTransferResumeList != NULL) &&
  1548. ProgressData.InProgress &&
  1549. ProgressData.IsTransfer() &&
  1550. !ProgressData.FullFileName.IsEmpty())
  1551. {
  1552. if ((FTransferResumeList->Count == 0) ||
  1553. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1554. {
  1555. // note that we do not recognize directories from files here
  1556. FTransferResumeList->Add(ProgressData.FullFileName);
  1557. }
  1558. }
  1559. }
  1560. }
  1561. //---------------------------------------------------------------------------
  1562. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1563. const TDateTime & StartTime)
  1564. {
  1565. if (GUIConfiguration->BeepOnFinish &&
  1566. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1567. {
  1568. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1569. DWORD Sound;
  1570. if (!ExtractFileExt(BeepSound).IsEmpty())
  1571. {
  1572. Sound = SND_FILENAME;
  1573. }
  1574. else
  1575. {
  1576. Sound = SND_ALIAS;
  1577. }
  1578. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1579. }
  1580. }
  1581. //---------------------------------------------------------------------------
  1582. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1583. TFileOperationProgressType & ProgressData)
  1584. {
  1585. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1586. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1587. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1588. FileOperationProgress(ProgressData);
  1589. }
  1590. //---------------------------------------------------------------------------
  1591. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1592. {
  1593. UnicodeString Result;
  1594. if (FProgressForm != NULL)
  1595. {
  1596. Result = FProgressForm->ProgressStr();
  1597. }
  1598. return Result;
  1599. }
  1600. //---------------------------------------------------------------------------
  1601. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1602. bool DragDrop)
  1603. {
  1604. return (!DragDrop && (DropSourceControl == NULL)) ||
  1605. (DropSourceControl == DirView(osOther));
  1606. }
  1607. //---------------------------------------------------------------------------
  1608. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1609. {
  1610. TCustomDirView * ADirView = DirView(Side);
  1611. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1612. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1613. // this can happen when local drive is unplugged in the middle of the operation
  1614. if (Item != NULL)
  1615. {
  1616. if (Unselect)
  1617. {
  1618. Item->Selected = false;
  1619. }
  1620. ADirView->MakeProgressVisible(Item);
  1621. }
  1622. return Item;
  1623. }
  1624. //---------------------------------------------------------------------------
  1625. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1626. TFileOperation Operation, TOperationSide Side,
  1627. bool /*Temp*/, const UnicodeString & FileName, bool Success, bool NotCancelled,
  1628. TOnceDoneOperation & OnceDoneOperation)
  1629. {
  1630. if (!FAutoOperation)
  1631. {
  1632. // no selection on "/upload", form serves only as event handler
  1633. // (it is not displayed)
  1634. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1635. ((Operation == FPrimaryOperation) ||
  1636. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1637. {
  1638. TOperationSide DViewSide;
  1639. if ((FProgressSide != osCurrent) &&
  1640. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1641. {
  1642. // assume the operation is over the focused panel
  1643. DViewSide = FProgressSide;
  1644. }
  1645. else
  1646. {
  1647. DViewSide = Side;
  1648. }
  1649. if (FOnFileOperationFinished != NULL)
  1650. {
  1651. FOnFileOperationFinished(DViewSide, FileName, Success, NotCancelled);
  1652. }
  1653. else
  1654. {
  1655. VisualiseOperationFinished(DViewSide, FileName, Success && NotCancelled);
  1656. }
  1657. }
  1658. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1659. {
  1660. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1661. {
  1662. if (Side == osLocal)
  1663. {
  1664. Configuration->Usage->Inc(L"UploadedFiles");
  1665. }
  1666. else
  1667. {
  1668. Configuration->Usage->Inc(L"DownloadedFiles");
  1669. }
  1670. }
  1671. }
  1672. }
  1673. if (Success && (FSynchronizeController != NULL))
  1674. {
  1675. DebugAssert(!IsSideLocalBrowser(osRemote));
  1676. if (Operation == foCopy)
  1677. {
  1678. DebugAssert(Side == osLocal);
  1679. FSynchronizeController->LogOperation(soUpload, FileName);
  1680. }
  1681. else if (Operation == foDelete)
  1682. {
  1683. DebugAssert(Side == osRemote);
  1684. FSynchronizeController->LogOperation(soDelete, FileName);
  1685. }
  1686. }
  1687. if (FProgressForm)
  1688. {
  1689. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1690. }
  1691. }
  1692. //---------------------------------------------------------------------------
  1693. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1694. TFileOperation Operation, TOperationSide Side,
  1695. bool Temp, const UnicodeString & FileName, bool Success, bool NotCancelled,
  1696. TOnceDoneOperation & OnceDoneOperation)
  1697. {
  1698. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1699. DoOperationFinished(Operation, Side, Temp, FileName, Success, NotCancelled, OnceDoneOperation);
  1700. }
  1701. //---------------------------------------------------------------------------
  1702. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1703. {
  1704. return false;
  1705. }
  1706. //---------------------------------------------------------------------------
  1707. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1708. {
  1709. return false;
  1710. }
  1711. //---------------------------------------------------------------------------
  1712. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1713. {
  1714. return false;
  1715. }
  1716. //---------------------------------------------------------------------------
  1717. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1718. {
  1719. return NULL;
  1720. }
  1721. //---------------------------------------------------------------------------
  1722. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1723. {
  1724. DebugAssert(GetSide(Side) == osRemote);
  1725. DebugUsedParam(Side);
  1726. return RemoteDirView;
  1727. }
  1728. //---------------------------------------------------------------------------
  1729. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1730. {
  1731. DebugAssert(GetSide(Side) == osRemote);
  1732. DebugUsedParam(Side);
  1733. return RemoteDriveView;
  1734. }
  1735. //---------------------------------------------------------------------------
  1736. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1737. {
  1738. DebugAssert(GetSide(Side) == osRemote);
  1739. DebugUsedParam(Side);
  1740. return HasAvailableTerminal();
  1741. }
  1742. //---------------------------------------------------------------------------
  1743. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1744. TOperationSide Side, int FilesOnly)
  1745. {
  1746. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1747. }
  1748. //---------------------------------------------------------------------------
  1749. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1750. TOperationSide Side, int FilesOnly)
  1751. {
  1752. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1753. }
  1754. //---------------------------------------------------------------------------
  1755. struct THistoryItemData
  1756. {
  1757. short int Side;
  1758. short int Index;
  1759. };
  1760. //---------------------------------------------------------------------------
  1761. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1762. {
  1763. DirView(Side)->HistoryGo(Index);
  1764. }
  1765. //---------------------------------------------------------------------------
  1766. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1767. {
  1768. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1769. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1770. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1771. }
  1772. //---------------------------------------------------------------------------
  1773. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1774. bool Back)
  1775. {
  1776. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1777. {
  1778. TCustomDirView * DView = DirView(Side);
  1779. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1780. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1781. if (ICount > 10)
  1782. {
  1783. ICount = 10;
  1784. }
  1785. Menu->Items->Clear();
  1786. THistoryItemData Data;
  1787. Data.Side = Side;
  1788. for (short int i = 1; i <= ICount; i++)
  1789. {
  1790. TTBCustomItem * Item = new TTBXItem(Menu);
  1791. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1792. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1793. Item->Hint = DView->HistoryPath[Data.Index];
  1794. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1795. Item->Tag = *reinterpret_cast<int*>(&Data);
  1796. Item->OnClick = HistoryItemClick;
  1797. Menu->Items->Add(Item);
  1798. }
  1799. }
  1800. }
  1801. //---------------------------------------------------------------------------
  1802. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1803. TOperationSide Side, bool Back)
  1804. {
  1805. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1806. {
  1807. // workaround
  1808. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1809. // operator new allocates memory in Pascal code, but calls inline
  1810. // constructor in C++, leading in problems, the function does
  1811. // both in Pascal code
  1812. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1813. UpdateHistoryMenu(Side, Back);
  1814. }
  1815. return FHistoryMenu[Side == osLocal][Back];
  1816. }
  1817. //---------------------------------------------------------------------------
  1818. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1819. TCustomDirView *Sender)
  1820. {
  1821. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1822. UpdateHistoryMenu(Side, true);
  1823. UpdateHistoryMenu(Side, false);
  1824. }
  1825. //---------------------------------------------------------------------------
  1826. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1827. {
  1828. // remote custom commands can be executed only if the server supports shell commands
  1829. // or have secondary shell
  1830. return HasAvailableTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1831. }
  1832. //---------------------------------------------------------------------------
  1833. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1834. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1835. {
  1836. // -1 = hidden, 0 = disabled, 1 = enabled
  1837. int Result;
  1838. TFileCustomCommand RemoteCustomCommand;
  1839. TLocalCustomCommand LocalCustomCommand;
  1840. TFileCustomCommand * NonInteractiveCustomCommand =
  1841. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1842. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1843. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1844. if (FLAGCLEAR(Command.Params, ccLocal))
  1845. {
  1846. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1847. // custom command that does not operate with files can be executed anytime ...
  1848. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1849. {
  1850. // ... but do not show such command in remote file menu
  1851. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1852. {
  1853. Result = AllowedState;
  1854. }
  1855. else
  1856. {
  1857. Result = -1;
  1858. }
  1859. }
  1860. else
  1861. {
  1862. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1863. {
  1864. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1865. }
  1866. else
  1867. {
  1868. Result = -1;
  1869. }
  1870. }
  1871. }
  1872. else
  1873. {
  1874. // custom command that does not operate with files can be executed anytime
  1875. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1876. {
  1877. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1878. }
  1879. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1880. {
  1881. if ((ListType == ccltAll) || (ListType == ccltFile))
  1882. {
  1883. // special case is "diff"-style command that can be executed over any side,
  1884. // if we have both sides
  1885. Result =
  1886. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1887. // directly (instead of EnableSelectedOperation) to pass
  1888. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1889. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1890. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1891. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1892. }
  1893. else if (ListType == ccltBoth)
  1894. {
  1895. DebugAssert(!IsLocalBrowserMode());
  1896. Result = 1;
  1897. }
  1898. else
  1899. {
  1900. Result = -1;
  1901. }
  1902. }
  1903. else
  1904. {
  1905. if ((ListType == ccltAll) ||
  1906. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1907. {
  1908. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1909. }
  1910. else
  1911. {
  1912. Result = -1;
  1913. }
  1914. }
  1915. if ((Result > 0) &&
  1916. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1917. !HasAvailableTerminal())
  1918. {
  1919. Result = 0;
  1920. }
  1921. }
  1922. return Result;
  1923. }
  1924. //---------------------------------------------------------------------------
  1925. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1926. TStrings * FileList, const TCustomCommandType & ACommand,
  1927. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1928. {
  1929. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1930. {
  1931. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1932. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1933. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1934. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1935. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1936. bool Capture =
  1937. FLAGSET(ACommand.Params, ccShowResults) ||
  1938. FLAGSET(ACommand.Params, ccCopyResults) ||
  1939. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1940. TCaptureOutputEvent OutputEvent = NULL;
  1941. DebugAssert(FCapturedLog == NULL);
  1942. TObjectReleaser<TStrings> CapturedLogReleaser(FCapturedLog);
  1943. if (Capture)
  1944. {
  1945. CapturedLogReleaser.Set(new TStringList());
  1946. OutputEvent = TerminalCaptureLog;
  1947. }
  1948. if (!RemoteCustomCommand.IsFileCommand(Command))
  1949. {
  1950. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1951. OutputEvent);
  1952. }
  1953. else
  1954. {
  1955. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1956. }
  1957. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1958. {
  1959. if (FLAGSET(ACommand.Params, ccCopyResults))
  1960. {
  1961. CopyToClipboard(FCapturedLog);
  1962. }
  1963. if (FLAGSET(ACommand.Params, ccShowResults))
  1964. {
  1965. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1966. }
  1967. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1968. {
  1969. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1970. }
  1971. }
  1972. }
  1973. }
  1974. //---------------------------------------------------------------------------
  1975. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1976. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1977. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1978. {
  1979. DebugAssert(!IsLocalBrowserMode());
  1980. TStrings * LocalFileList = NULL;
  1981. TStrings * RemoteFileList = NULL;
  1982. TStrings * RemoteFileListFull = NULL;
  1983. try
  1984. {
  1985. if (LocalFileCommand)
  1986. {
  1987. if (ALocalFileList == NULL)
  1988. {
  1989. DebugAssert(HasDirView[osLocal]);
  1990. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1991. // directly (instead of EnableSelectedOperation) to pass
  1992. // false to FocusedFileOnlyWhenFocused
  1993. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1994. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1995. }
  1996. else
  1997. {
  1998. LocalFileList = ALocalFileList;
  1999. }
  2000. if (FileListCommand)
  2001. {
  2002. if (LocalFileList->Count != 1)
  2003. {
  2004. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  2005. }
  2006. }
  2007. else
  2008. {
  2009. if ((LocalFileList->Count != 1) &&
  2010. (FileList->Count != 1) &&
  2011. (LocalFileList->Count != FileList->Count))
  2012. {
  2013. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  2014. }
  2015. }
  2016. }
  2017. UnicodeString RootTempDir;
  2018. UnicodeString TempDir;
  2019. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  2020. if (!RemoteFiles)
  2021. {
  2022. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, true, true);
  2023. DebugAssert(RootTempDir == TempDir);
  2024. }
  2025. try
  2026. {
  2027. TDateTimes RemoteFileTimes;
  2028. if (RemoteFiles)
  2029. {
  2030. RemoteFileList = FileList;
  2031. }
  2032. else
  2033. {
  2034. RemoteFileList = new TStringList();
  2035. TMakeLocalFileListParams MakeFileListParam;
  2036. MakeFileListParam.FileList = RemoteFileList;
  2037. MakeFileListParam.FileTimes = &RemoteFileTimes;
  2038. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2039. MakeFileListParam.Recursive =
  2040. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  2041. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2042. if (!MakeFileListParam.Recursive)
  2043. {
  2044. RemoteFileTimes.clear();
  2045. RemoteFileListFull = new TStringList();
  2046. MakeFileListParam.FileList = RemoteFileListFull;
  2047. MakeFileListParam.Recursive = true;
  2048. MakeFileListParam.IncludeDirs = false;
  2049. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2050. }
  2051. else
  2052. {
  2053. RemoteFileListFull = RemoteFileList;
  2054. }
  2055. }
  2056. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  2057. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2058. if (!NonBlocking)
  2059. {
  2060. int Count;
  2061. if (FileListCommand)
  2062. {
  2063. Count = 1;
  2064. }
  2065. else if (LocalFileCommand)
  2066. {
  2067. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  2068. }
  2069. else
  2070. {
  2071. Count = RemoteFileList->Count;
  2072. }
  2073. Progress.Start(foCustomCommand, osRemote, Count);
  2074. DebugAssert(FProgressForm != NULL);
  2075. FProgressForm->ReadOnly = true;
  2076. }
  2077. try
  2078. {
  2079. if (FileListCommand)
  2080. {
  2081. UnicodeString LocalFile;
  2082. // MakeFileList does not delimit filenames
  2083. UnicodeString FileList = MakeFileList(RemoteFileList);
  2084. if (LocalFileCommand)
  2085. {
  2086. DebugAssert(LocalFileList->Count == 1);
  2087. LocalFile = LocalFileList->Strings[0];
  2088. }
  2089. TLocalCustomCommand CustomCommand(Data,
  2090. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  2091. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  2092. if (NonBlocking)
  2093. {
  2094. DebugAssert(!POutput);
  2095. ExecuteShellChecked(ShellCommand);
  2096. }
  2097. else
  2098. {
  2099. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2100. }
  2101. }
  2102. else if (LocalFileCommand)
  2103. {
  2104. if (LocalFileList->Count == 1)
  2105. {
  2106. UnicodeString LocalFile = LocalFileList->Strings[0];
  2107. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2108. {
  2109. UnicodeString FileName = RemoteFileList->Strings[Index];
  2110. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2111. Progress.SetFile(FileNameForProgress);
  2112. TLocalCustomCommand CustomCommand(Data,
  2113. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2114. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2115. TOnceDoneOperation OnceDoneOperation;
  2116. // Do not unselect anything, as it won't work with two panels anyway
  2117. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2118. }
  2119. }
  2120. else if (RemoteFileList->Count == 1)
  2121. {
  2122. UnicodeString FileName = RemoteFileList->Strings[0];
  2123. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2124. {
  2125. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2126. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2127. Progress.SetFile(LocalFileNameOnly);
  2128. TLocalCustomCommand CustomCommand(
  2129. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2130. FileName, LocalFileName, L"");
  2131. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2132. TOnceDoneOperation OnceDoneOperation;
  2133. // Do not unselect anything, as it won't work with two panels anyway
  2134. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2135. }
  2136. }
  2137. else
  2138. {
  2139. if (LocalFileList->Count != RemoteFileList->Count)
  2140. {
  2141. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2142. }
  2143. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2144. {
  2145. UnicodeString FileName = RemoteFileList->Strings[Index];
  2146. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2147. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2148. Progress.SetFile(LocalFileNameOnly); // sic
  2149. TLocalCustomCommand CustomCommand(
  2150. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2151. FileName, LocalFileName, L"");
  2152. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2153. TOnceDoneOperation OnceDoneOperation;
  2154. // Do not unselect anything, as it won't work with two panels anyway
  2155. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2156. }
  2157. }
  2158. }
  2159. else
  2160. {
  2161. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2162. {
  2163. UnicodeString FileName = RemoteFileList->Strings[Index];
  2164. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2165. Progress.SetFile(FileNameForProgress);
  2166. TLocalCustomCommand CustomCommand(Data,
  2167. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2168. FileName, L"", L"");
  2169. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2170. TOnceDoneOperation OnceDoneOperation;
  2171. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2172. }
  2173. }
  2174. }
  2175. __finally
  2176. {
  2177. if (!NonBlocking)
  2178. {
  2179. Progress.Stop();
  2180. }
  2181. }
  2182. DebugAssert(!FAutoOperation);
  2183. if (!RemoteFiles)
  2184. {
  2185. TempDir = IncludeTrailingBackslash(TempDir);
  2186. typedef std::map<UnicodeString, UnicodeString> TTempNameMap;
  2187. TTempNameMap TempNameMap;
  2188. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2189. {
  2190. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2191. TSearchRecSmart SearchRec;
  2192. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2193. FileSearchRec(FileName, SearchRec) &&
  2194. // Skip directories, process only nested files.
  2195. !SearchRec.IsDirectory() &&
  2196. (SearchRec.GetLastWriteTime() != RemoteFileTimes[Index]))
  2197. {
  2198. if (TempNameMap.empty()) // optimization (create map only once it is really needed)
  2199. {
  2200. TCopyParamType CopyParam = TemporaryFileCopyParam();
  2201. for (int Index2 = 0; Index2 < FileList->Count; Index2++)
  2202. {
  2203. UnicodeString RemotePath = FileList->Strings[Index2];
  2204. UnicodeString TempName = GetTempLocalName(RemotePath, CopyParam);
  2205. TempNameMap.insert(std::make_pair(TempName.UpperCase(), RemotePath));
  2206. }
  2207. }
  2208. UnicodeString RelPath = MidStr(FileName, TempDir.Length() + 1);
  2209. UnicodeString TempName = CutToChar(RelPath, L'\\', false).UpperCase();
  2210. TTempNameMap::const_iterator TempNameI = TempNameMap.find(TempName);
  2211. if (DebugAlwaysTrue(TempNameI != TempNameMap.end()))
  2212. {
  2213. TCopyParamType CopyParam = TemporaryFileCopyParam();
  2214. CopyParam.FileMask = L"";
  2215. FAutoOperation = true;
  2216. FMoveToQueue = false;
  2217. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2218. TemporaryFilesList->Add(FileName);
  2219. UnicodeString RemoteDir = UnixExtractFileDir(UnixCombinePathsSmart(TempNameI->second, ToUnixPath(RelPath)));
  2220. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2221. }
  2222. }
  2223. }
  2224. }
  2225. }
  2226. __finally
  2227. {
  2228. FAutoOperation = false;
  2229. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2230. {
  2231. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  2232. }
  2233. }
  2234. }
  2235. __finally
  2236. {
  2237. if (RemoteFileList != FileList)
  2238. {
  2239. delete RemoteFileList;
  2240. }
  2241. if (LocalFileList != ALocalFileList)
  2242. {
  2243. delete LocalFileList;
  2244. }
  2245. if (RemoteFileListFull != RemoteFileList)
  2246. {
  2247. delete RemoteFileListFull;
  2248. }
  2249. }
  2250. }
  2251. //---------------------------------------------------------------------------
  2252. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2253. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2254. bool FileListCommand, UnicodeString * POutput)
  2255. {
  2256. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2257. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2258. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2259. {
  2260. UnicodeString FileName = SelectedFileList->Strings[Index];
  2261. if (DirectoryExists(FileName))
  2262. {
  2263. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2264. {
  2265. LocalFileList->Add(FileName);
  2266. }
  2267. if (FLAGSET(ACommand.Params, ccRecursive))
  2268. {
  2269. TMakeLocalFileListParams MakeFileListParam;
  2270. MakeFileListParam.FileList = LocalFileList.get();
  2271. MakeFileListParam.FileTimes = NULL;
  2272. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2273. MakeFileListParam.Recursive = true;
  2274. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2275. }
  2276. }
  2277. else
  2278. {
  2279. LocalFileList->Add(FileName);
  2280. }
  2281. }
  2282. UnicodeString RemotePath;
  2283. if (Terminal != NULL)
  2284. {
  2285. RemotePath = Terminal->CurrentDirectory;
  2286. }
  2287. if (FileListCommand)
  2288. {
  2289. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2290. TLocalCustomCommand CustomCommand(
  2291. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2292. L"", L"", FileList);
  2293. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2294. }
  2295. else
  2296. {
  2297. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2298. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2299. DebugAssert(FProgressForm != NULL);
  2300. FProgressForm->ReadOnly = true;
  2301. try
  2302. {
  2303. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2304. {
  2305. UnicodeString FileName = LocalFileList->Strings[Index];
  2306. Progress.SetFile(FileName);
  2307. TLocalCustomCommand CustomCommand(
  2308. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2309. FileName, L"", L"");
  2310. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2311. TOnceDoneOperation OnceDoneOperation;
  2312. Progress.Finish(FileName, true, OnceDoneOperation);
  2313. }
  2314. }
  2315. __finally
  2316. {
  2317. Progress.Stop();
  2318. }
  2319. }
  2320. }
  2321. //---------------------------------------------------------------------------
  2322. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2323. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2324. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2325. {
  2326. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2327. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2328. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2329. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2330. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2331. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2332. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2333. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2334. std::unique_ptr<UnicodeString> POutput;
  2335. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2336. FLAGSET(ACommand.Params, ccCopyResults))
  2337. {
  2338. POutput.reset(new UnicodeString());
  2339. }
  2340. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide, FCurrentSide);
  2341. if (!LocalCustomCommand.IsFileCommand(Command))
  2342. {
  2343. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2344. }
  2345. // remote files?
  2346. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2347. {
  2348. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2349. }
  2350. // local files
  2351. else
  2352. {
  2353. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2354. }
  2355. if (POutput.get() != NULL)
  2356. {
  2357. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2358. int P = POutput->Pos(sLineBreak);
  2359. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2360. {
  2361. POutput->SetLength(P - 1);
  2362. }
  2363. // Copy even empty output to clipboard
  2364. if (FLAGSET(ACommand.Params, ccCopyResults))
  2365. {
  2366. CopyToClipboard(*POutput);
  2367. }
  2368. // But do not show an empty message box.
  2369. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2370. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2371. !POutput->IsEmpty())
  2372. {
  2373. TClipboardHandler ClipboardHandler;
  2374. ClipboardHandler.Text = *POutput;
  2375. TMessageParams Params;
  2376. TQueryButtonAlias Aliases[1];
  2377. Aliases[0].Button = qaRetry;
  2378. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2379. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2380. Params.Aliases = Aliases;
  2381. Params.AliasesCount = LENOF(Aliases);
  2382. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2383. }
  2384. }
  2385. }
  2386. //---------------------------------------------------------------------------
  2387. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2388. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2389. {
  2390. TCustomCommandData Data;
  2391. UnicodeString Site;
  2392. UnicodeString RemotePath;
  2393. if (HasAvailableTerminal())
  2394. {
  2395. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2396. Data = TCustomCommandData(SessionData.get());
  2397. Site = Terminal->SessionData->SessionKey;
  2398. RemotePath = Terminal->CurrentDirectory;
  2399. }
  2400. UnicodeString HelpKeyword = ACommand.HomePage;
  2401. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2402. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2403. {
  2404. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2405. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2406. {
  2407. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2408. }
  2409. else
  2410. {
  2411. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2412. }
  2413. if (!DoCustomCommandOptionsDialog(
  2414. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2415. {
  2416. Abort();
  2417. }
  2418. }
  2419. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2420. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2421. {
  2422. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2423. }
  2424. else
  2425. {
  2426. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2427. }
  2428. }
  2429. //---------------------------------------------------------------------------
  2430. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2431. const TCustomCommandType & Command)
  2432. {
  2433. DebugAssert(FCustomCommandLocalFileList != NULL);
  2434. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2435. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2436. TStrings * LocalFileList = new TStringList();
  2437. TStrings * RemoteFileList = new TStringList();
  2438. try
  2439. {
  2440. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2441. {
  2442. LocalFileList->Clear();
  2443. LocalFileList->AddObject(
  2444. FCustomCommandLocalFileList->Strings[Index],
  2445. FCustomCommandLocalFileList->Objects[Index]);
  2446. RemoteFileList->Clear();
  2447. RemoteFileList->AddObject(
  2448. FCustomCommandRemoteFileList->Strings[Index],
  2449. FCustomCommandRemoteFileList->Objects[Index]);
  2450. CustomCommand(RemoteFileList, Command, LocalFileList);
  2451. }
  2452. }
  2453. __finally
  2454. {
  2455. delete LocalFileList;
  2456. delete RemoteFileList;
  2457. }
  2458. }
  2459. //---------------------------------------------------------------------------
  2460. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2461. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2462. {
  2463. delete FCustomCommandLocalFileList;
  2464. delete FCustomCommandRemoteFileList;
  2465. // takeover ownership,
  2466. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2467. FCustomCommandLocalFileList = LocalFileList;
  2468. FCustomCommandRemoteFileList = RemoteFileList;
  2469. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2470. if (Button != NULL)
  2471. {
  2472. FCustomCommandMenu->Items->Clear();
  2473. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2474. MenuPopup(FCustomCommandMenu, Button);
  2475. }
  2476. else
  2477. {
  2478. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2479. }
  2480. }
  2481. //---------------------------------------------------------------------------
  2482. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2483. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2484. {
  2485. DebugAssert(FCapturedLog != NULL);
  2486. if ((OutputType == cotOutput) || (OutputType == cotError))
  2487. {
  2488. FCapturedLog->Add(AddedLine);
  2489. }
  2490. }
  2491. //---------------------------------------------------------------------------
  2492. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2493. TOperationSide Side)
  2494. {
  2495. return
  2496. (Side == osOther) &&
  2497. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2498. }
  2499. //---------------------------------------------------------------------------
  2500. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2501. TOperationSide Side, TTBXCustomItem * Item,
  2502. TResolvedDoubleClickAction DoubleClickAction1, TResolvedDoubleClickAction DoubleClickAction2)
  2503. {
  2504. TCustomDirView * DView = DirView(Side);
  2505. bool IsDefault = false;
  2506. if (DView->ItemFocused != NULL)
  2507. {
  2508. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  2509. TResolvedDoubleClickAction DefaultAction =
  2510. WinConfiguration->ResolveDoubleClickAction(DView->ItemIsDirectory(DView->ItemFocused), ATerminal);
  2511. IsDefault = (DefaultAction == DoubleClickAction1) || (DefaultAction == DoubleClickAction2);
  2512. }
  2513. TTBItemOptions O = Item->Options;
  2514. if (IsDefault)
  2515. {
  2516. Item->Options = O << tboDefault;
  2517. }
  2518. else
  2519. {
  2520. Item->Options = O >> tboDefault;
  2521. }
  2522. }
  2523. //---------------------------------------------------------------------------
  2524. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2525. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2526. {
  2527. TCustomDirView * DView = DirView(Side);
  2528. TListItem * Item = DView->ItemFocused;
  2529. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2530. EnableFocusedOperation[Side])
  2531. {
  2532. TPoint ClientPoint;
  2533. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2534. TPoint(0, 0) : MousePos;
  2535. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2536. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2537. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2538. }
  2539. }
  2540. //---------------------------------------------------------------------------
  2541. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2542. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2543. {
  2544. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, rdcaChangeDir, rdcaOpen);
  2545. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, rdcaEdit, rdcaNone);
  2546. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, rdcaCopy, rdcaNone);
  2547. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2548. Handled = true;
  2549. }
  2550. //---------------------------------------------------------------------------
  2551. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString DebugUsedArg(Directory))
  2552. {
  2553. }
  2554. //---------------------------------------------------------------------------
  2555. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2556. {
  2557. DebugAssert(FErrorList == NULL);
  2558. if (WinConfiguration->ContinueOnError)
  2559. {
  2560. FErrorList = new TStringList();
  2561. Configuration->Usage->Inc(L"ContinuationsOnError");
  2562. }
  2563. NonVisualDataModule->StartBusy();
  2564. }
  2565. //---------------------------------------------------------------------------
  2566. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2567. {
  2568. NonVisualDataModule->EndBusy();
  2569. if (FErrorList)
  2570. {
  2571. HandleErrorList(FErrorList);
  2572. }
  2573. }
  2574. //---------------------------------------------------------------------------
  2575. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2576. const TCopyParamType & CopyParam)
  2577. {
  2578. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2579. {
  2580. Configuration->Usage->Inc(L"FileMaskUses");
  2581. }
  2582. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2583. {
  2584. Configuration->Usage->Inc(L"OperationMaskUses");
  2585. }
  2586. }
  2587. //---------------------------------------------------------------------------
  2588. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2589. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2590. {
  2591. DebugAssert(!IsLocalBrowserMode());
  2592. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2593. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2594. TTransferOperationParam DefaultParam;
  2595. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2596. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2597. switch (Param.Queue)
  2598. {
  2599. case asOn:
  2600. CopyParam.Queue = true;
  2601. break;
  2602. case asOff:
  2603. CopyParam.Queue = false;
  2604. break;
  2605. case asAuto:
  2606. default:
  2607. // keep default
  2608. break;
  2609. }
  2610. bool Result =
  2611. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2612. if (Result)
  2613. {
  2614. DebugAssert(Terminal);
  2615. bool SelectionRestored = false;
  2616. TCustomDirView * DView = NULL;
  2617. if (HasDirView[Side])
  2618. {
  2619. DView = DirView(Side);
  2620. DView->SaveSelection();
  2621. DView->SaveSelectedNames();
  2622. }
  2623. UpdateCopyParamCounters(CopyParam);
  2624. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2625. DebugAssert(FTransferResumeList == NULL);
  2626. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2627. FMoveToQueue = false;
  2628. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2629. try
  2630. {
  2631. TStrings * PermanentFileList;
  2632. std::unique_ptr<TStrings> PermanentFileListOwner;
  2633. try
  2634. {
  2635. if (Side == osLocal)
  2636. {
  2637. PermanentFileList = FileList;
  2638. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2639. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2640. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2641. if (Operation == foMove)
  2642. {
  2643. ReloadLocalDirectory();
  2644. if (DView != NULL)
  2645. {
  2646. DView->RestoreSelection();
  2647. }
  2648. SelectionRestored = true;
  2649. }
  2650. }
  2651. else
  2652. {
  2653. // Clone the file list as it may refer to current directory files,
  2654. // which get destroyed, when the source directory is reloaded after foMove operation.
  2655. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2656. // But for a hotfix, we are not going to do such a big change.
  2657. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2658. PermanentFileList = PermanentFileListOwner.get();
  2659. try
  2660. {
  2661. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2662. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2663. }
  2664. __finally
  2665. {
  2666. if (Operation == foMove)
  2667. {
  2668. if (DView != NULL)
  2669. {
  2670. DView->RestoreSelection();
  2671. }
  2672. SelectionRestored = true;
  2673. }
  2674. ReloadLocalDirectory(Param.TargetDirectory);
  2675. }
  2676. }
  2677. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2678. if (FMoveToQueue)
  2679. {
  2680. Abort();
  2681. }
  2682. }
  2683. catch (EAbort &)
  2684. {
  2685. if (FMoveToQueue)
  2686. {
  2687. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2688. DebugAssert(CopyParam.TransferSkipList == NULL);
  2689. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2690. if (TransferResumeList->Count > 0)
  2691. {
  2692. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2693. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2694. }
  2695. CopyParam.TransferSkipList = TransferResumeList.release();
  2696. // not really needed, just to keep it consistent with TransferResumeList
  2697. FTransferResumeList = NULL;
  2698. FMoveToQueue = false;
  2699. Configuration->Usage->Inc("MovesToBackground");
  2700. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2701. ClearTransferSourceSelection(Direction);
  2702. }
  2703. throw;
  2704. }
  2705. }
  2706. __finally
  2707. {
  2708. if (!SelectionRestored && (DView != NULL))
  2709. {
  2710. DView->DiscardSavedSelection();
  2711. }
  2712. FTransferResumeList = NULL;
  2713. }
  2714. }
  2715. return Result;
  2716. }
  2717. //---------------------------------------------------------------------------
  2718. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2719. TOperationSide Side, TStrings * FileList, void * Param)
  2720. {
  2721. DebugAssert(FileList->Count);
  2722. // We deliberately do not toggle alternative flag (Param), but use OR,
  2723. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2724. // shortcut of CurrentDeleteAlternativeAction
  2725. bool Alternative =
  2726. bool(Param) || UseAlternativeFunction();
  2727. bool Recycle;
  2728. if (IsSideLocalBrowser(Side))
  2729. {
  2730. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2731. }
  2732. else
  2733. {
  2734. Recycle =
  2735. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2736. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2737. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2738. }
  2739. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2740. if (!Result)
  2741. {
  2742. UnicodeString Query;
  2743. if (FileList->Count == 1)
  2744. {
  2745. if (IsSideLocalBrowser(Side))
  2746. {
  2747. Query = ExtractFileName(FileList->Strings[0]);
  2748. }
  2749. else
  2750. {
  2751. Query = UnixExtractFileName(FileList->Strings[0]);
  2752. }
  2753. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2754. }
  2755. else
  2756. {
  2757. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2758. }
  2759. TMessageParams Params(mpNeverAskAgainCheck);
  2760. Params.ImageName = L"Delete file";
  2761. unsigned int Answer =
  2762. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2763. if (Answer == qaNeverAskAgain)
  2764. {
  2765. Result = true;
  2766. if (Recycle)
  2767. {
  2768. WinConfiguration->ConfirmRecycling = false;
  2769. }
  2770. else
  2771. {
  2772. WinConfiguration->ConfirmDeleting = false;
  2773. }
  2774. }
  2775. else
  2776. {
  2777. Result = (Answer == qaOK);
  2778. }
  2779. }
  2780. if (Result)
  2781. {
  2782. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2783. }
  2784. return Result;
  2785. }
  2786. //---------------------------------------------------------------------------
  2787. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2788. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2789. {
  2790. TAutoBatch AutoBatch(this);
  2791. bool Result;
  2792. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2793. {
  2794. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2795. }
  2796. else if (Operation == foRename)
  2797. {
  2798. DebugAssert(DirView(Side)->ItemFocused);
  2799. DirView(Side)->ItemFocused->EditCaption();
  2800. Result = true;
  2801. }
  2802. else if (Operation == foDelete)
  2803. {
  2804. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2805. }
  2806. else if (Operation == foSetProperties)
  2807. {
  2808. DirView(osRemote)->SaveSelectedNames();
  2809. Result = SetProperties(Side, FileList);
  2810. }
  2811. else if (Operation == foCustomCommand)
  2812. {
  2813. DebugAssert(Param);
  2814. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2815. DirView(osRemote)->SaveSelectedNames();
  2816. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2817. CustomCommand(FileList, *Command, NULL);
  2818. Result = true;
  2819. }
  2820. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2821. {
  2822. DebugAssert(!IsSideLocalBrowser(Side));
  2823. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2824. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2825. }
  2826. else if (Operation == foLock)
  2827. {
  2828. DebugAssert(!IsSideLocalBrowser(Side));
  2829. LockFiles(FileList, true);
  2830. Result = true;
  2831. }
  2832. else if (Operation == foUnlock)
  2833. {
  2834. DebugAssert(!IsSideLocalBrowser(Side));
  2835. LockFiles(FileList, false);
  2836. Result = true;
  2837. }
  2838. else
  2839. {
  2840. DebugFail();
  2841. Result = false; // shut up
  2842. }
  2843. return Result;
  2844. }
  2845. //---------------------------------------------------------------------------
  2846. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2847. {
  2848. if (Side == osCurrent)
  2849. {
  2850. Side = FCurrentSide;
  2851. }
  2852. return Side;
  2853. }
  2854. //---------------------------------------------------------------------------
  2855. TOperationSide TCustomScpExplorerForm::GetOtherSide(TOperationSide Side)
  2856. {
  2857. TOperationSide Result;
  2858. switch (GetSide(Side))
  2859. {
  2860. case osLocal:
  2861. Result = osOther;
  2862. break;
  2863. case osOther:
  2864. Result = osLocal;
  2865. break;
  2866. default:
  2867. Result = osCurrent; // shut up
  2868. DebugFail();
  2869. Abort();
  2870. }
  2871. return Result;
  2872. }
  2873. //---------------------------------------------------------------------------
  2874. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2875. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2876. {
  2877. Side = GetSide(Side);
  2878. bool FullPath = IsSideLocalBrowser(Side);
  2879. TCustomDirView * ADirView = DirView(Side);
  2880. std::unique_ptr<TStringList> FileList(new TStringList());
  2881. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2882. bool ReloadProperties =
  2883. !IsSideLocalBrowser(Side) && (Operation == foSetProperties) &&
  2884. DebugAlwaysTrue(HasAvailableTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2885. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2886. if (Result && ReloadProperties)
  2887. {
  2888. std::unique_ptr<TStringList> FileList2(new TStringList());
  2889. FileList->CaseSensitive = true;
  2890. FileList->Sort();
  2891. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2892. {
  2893. TListItem * Item = ADirView->Items->Item[Index];
  2894. UnicodeString FileName = ADirView->ItemFileName(Item);
  2895. int Index2;
  2896. if (FileList->Find(FileName, Index2))
  2897. {
  2898. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2899. }
  2900. }
  2901. LoadFilesProperties(FileList2.get());
  2902. }
  2903. return Result;
  2904. }
  2905. //---------------------------------------------------------------------------
  2906. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2907. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2908. bool NoConfirmation, void * Param)
  2909. {
  2910. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2911. {
  2912. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2913. {
  2914. DebugAssert(!IsLocalBrowserMode());
  2915. if (GetSide(Side) == osLocal)
  2916. {
  2917. Configuration->Usage->Inc(L"UploadsCommand");
  2918. }
  2919. else
  2920. {
  2921. Configuration->Usage->Inc(L"DownloadsCommand");
  2922. }
  2923. }
  2924. }
  2925. }
  2926. //---------------------------------------------------------------------------
  2927. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2928. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2929. {
  2930. TTransferOperationParam Param;
  2931. if ((WinConfiguration->Interface != ifCommander) ||
  2932. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2933. {
  2934. Flags &= ~cocShortCutHint;
  2935. }
  2936. TCustomDirView * DView = DirView(Side);
  2937. Param.Options =
  2938. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2939. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2940. if (FLAGSET(Flags, cocQueue))
  2941. {
  2942. Param.Queue = asOn;
  2943. }
  2944. else if (FLAGSET(Flags, cocNonQueue))
  2945. {
  2946. Param.Queue = asOff;
  2947. }
  2948. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2949. }
  2950. //---------------------------------------------------------------------------
  2951. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2952. {
  2953. int Count = ErrorList->Count;
  2954. try
  2955. {
  2956. if (ErrorList->Count)
  2957. {
  2958. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2959. if (MessageDialog(Message, qtError,
  2960. qaOK | qaCancel, HELP_NONE) == qaOK)
  2961. {
  2962. unsigned int Answer;
  2963. int Index = 0;
  2964. do
  2965. {
  2966. DebugAssert(Index >= 0 && Index < Count);
  2967. TQueryButtonAlias Aliases[2];
  2968. Aliases[0].Button = qaYes;
  2969. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2970. Aliases[1].Button = qaNo;
  2971. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2972. TMessageParams Params;
  2973. Params.Aliases = Aliases;
  2974. Params.AliasesCount = LENOF(Aliases);
  2975. int No = Index + 1;
  2976. UnicodeString Message = ErrorList->Strings[Index];
  2977. UnicodeString AMainInstructions;
  2978. UnicodeString Details;
  2979. if (ExtractMainInstructions(Message, AMainInstructions))
  2980. {
  2981. Details = Message;
  2982. Message = AMainInstructions;
  2983. }
  2984. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2985. Answer = MoreMessageDialog(
  2986. Message,
  2987. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2988. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2989. qaOK, HELP_NONE, &Params);
  2990. if (Answer == qaNo)
  2991. {
  2992. Index++;
  2993. }
  2994. if (Answer == qaYes)
  2995. {
  2996. Index--;
  2997. }
  2998. }
  2999. while (Answer != qaOK);
  3000. }
  3001. }
  3002. }
  3003. __finally
  3004. {
  3005. TStrings * List = ErrorList;
  3006. ErrorList = NULL;
  3007. for (int i = 0; i < Count; i++)
  3008. {
  3009. delete List->Objects[i];
  3010. }
  3011. delete List;
  3012. }
  3013. }
  3014. //---------------------------------------------------------------------------
  3015. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  3016. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  3017. {
  3018. // needed for checking filemasks, as there's no directory object
  3019. // associated with the file object
  3020. File->FullFileName = FullFileName;
  3021. TFileMasks::TParams MaskParams;
  3022. MaskParams.Size = File->Size;
  3023. MaskParams.Modification = File->Modification;
  3024. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  3025. }
  3026. //---------------------------------------------------------------------------
  3027. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  3028. {
  3029. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3030. Side = GetSide(Side);
  3031. TCustomDirView * DView = DirView(Side);
  3032. TListItem * FocusedItem = DView->ItemFocused;
  3033. UnicodeString Name;
  3034. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  3035. {
  3036. Name = DView->ItemFileName(FocusedItem);
  3037. }
  3038. else
  3039. {
  3040. Name = LoadStr(NEW_FILE);
  3041. }
  3042. UnicodeString Names = Name;
  3043. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  3044. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  3045. HELP_EDIT_NEW, History.get(), true, NULL, true, 444))
  3046. {
  3047. while (!Names.IsEmpty())
  3048. {
  3049. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  3050. CustomWinConfiguration->History[L"EditFile"] = History.get();
  3051. UnicodeString TargetFileName;
  3052. UnicodeString LocalFileName;
  3053. UnicodeString RootTempDir;
  3054. UnicodeString TempDir;
  3055. UnicodeString RemoteDirectory;
  3056. bool ExistingFile = false;
  3057. if (!IsSideLocalBrowser(Side))
  3058. {
  3059. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  3060. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(Name));
  3061. if ((File.get() != NULL) &&
  3062. !File->IsDirectory)
  3063. {
  3064. ExecuteRemoteFile(Name, File.get(), efDefaultEditor);
  3065. ExistingFile = true;
  3066. }
  3067. File.reset(NULL);
  3068. if (!ExistingFile)
  3069. {
  3070. RemoteDirectory = UnixExtractFilePath(Name);
  3071. TemporaryDirectoryForRemoteFiles(
  3072. RemoteDirectory, GUIConfiguration->CurrentCopyParam, false, TempDir, RootTempDir);
  3073. TargetFileName = UnixExtractFileName(Name);
  3074. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3075. LocalFileName = TempDir +
  3076. // We probably do not want to trim the VMS version here
  3077. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  3078. }
  3079. }
  3080. else
  3081. {
  3082. if (ExtractFilePath(Name).IsEmpty())
  3083. {
  3084. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  3085. }
  3086. else
  3087. {
  3088. LocalFileName = ExpandFileName(Name);
  3089. }
  3090. TargetFileName = ExtractFileName(Name);
  3091. }
  3092. if (!ExistingFile)
  3093. {
  3094. bool NewFile = !FileExists(ApiPath(LocalFileName));
  3095. if (NewFile)
  3096. {
  3097. int File = FileCreate(ApiPath(LocalFileName));
  3098. if (File < 0)
  3099. {
  3100. if (!RootTempDir.IsEmpty())
  3101. {
  3102. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  3103. }
  3104. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  3105. }
  3106. else
  3107. {
  3108. FileClose(File);
  3109. }
  3110. }
  3111. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  3112. const TEditorData * ExternalEditor = NULL;
  3113. TFileMasks::TParams MaskParams; // size not known
  3114. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  3115. false, MaskParams);
  3116. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  3117. ExternalEditor, RootTempDir, RemoteDirectory, NewFile, TDateTime());
  3118. }
  3119. }
  3120. }
  3121. }
  3122. //---------------------------------------------------------------------------
  3123. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  3124. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  3125. {
  3126. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3127. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3128. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3129. return
  3130. ((ExecuteFileBy == efInternalEditor)) ||
  3131. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  3132. }
  3133. //---------------------------------------------------------------------------
  3134. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  3135. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  3136. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  3137. UnicodeString RemoteDirectory, bool NewFile, const TDateTime & SourceTimestamp)
  3138. {
  3139. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3140. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3141. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3142. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3143. Side = GetSide(Side);
  3144. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3145. if (!IsSideLocalBrowser(Side))
  3146. {
  3147. Data->Terminal = Terminal;
  3148. Data->SessionData = CloneCurrentSessionData();
  3149. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3150. Data->RemoteDirectory = RemoteDirectory;
  3151. Data->SessionName = Terminal->SessionData->SessionName;
  3152. Data->LocalRootDirectory = LocalRootDirectory;
  3153. Data->OriginalFileName = OriginalFileName;
  3154. Data->Command = L""; // will be changed later for external editor
  3155. // Empty SourceTimestamp indicates that checking was disabled when the file was opened
  3156. if (WinConfiguration->EditorCheckNotModified)
  3157. {
  3158. Data->SourceTimestamp = SourceTimestamp;
  3159. }
  3160. }
  3161. if (ExecuteFileBy == efInternalEditor)
  3162. {
  3163. if (!IsSideLocalBrowser(Side))
  3164. {
  3165. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3166. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3167. TForm * Editor;
  3168. try
  3169. {
  3170. Editor =
  3171. ShowEditorForm(
  3172. FileName, this,
  3173. FEditorManager->FileChanged, FEditorManager->FileReload, FEditorManager->FileClosed,
  3174. SaveAllInternalEditors, AnyInternalEditorModified,
  3175. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3176. }
  3177. catch(...)
  3178. {
  3179. if (!LocalRootDirectory.IsEmpty())
  3180. {
  3181. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory));
  3182. }
  3183. throw;
  3184. }
  3185. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3186. AppLogFmt(L"Opened internal editor with \"%s\"", (FileName));
  3187. }
  3188. else
  3189. {
  3190. DebugAssert(!StandaloneOperation);
  3191. TForm * Editor =
  3192. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3193. SaveAllInternalEditors, AnyInternalEditorModified,
  3194. L"", false, SessionColor, -1, NewFile);
  3195. FLocalEditors->Add(Editor);
  3196. }
  3197. }
  3198. else
  3199. {
  3200. HANDLE Process;
  3201. if (ExecuteFileBy == efExternalEditor)
  3202. {
  3203. UnicodeString Program, Params, Dir;
  3204. Data->Command = ExternalEditor->ExternalEditor;
  3205. ReformatFileNameCommand(Data->Command);
  3206. SplitCommand(Data->Command, Program, Params, Dir);
  3207. Params = ExpandFileNameCommand(Params, FileName);
  3208. Program = ExpandEnvironmentVariables(Program);
  3209. if (!ExecuteShell(Program, Params, Process))
  3210. {
  3211. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3212. }
  3213. }
  3214. else
  3215. {
  3216. DebugAssert(!IsSideLocalBrowser(Side));
  3217. if (!ExecuteShell(FileName, L"", Process))
  3218. {
  3219. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3220. }
  3221. }
  3222. if (IsSideLocalBrowser(Side) ||
  3223. ((ExecuteFileBy == efShell) &&
  3224. !WinConfiguration->Editor.SDIShellEditor) ||
  3225. ((ExecuteFileBy == efExternalEditor) &&
  3226. !ExternalEditor->SDIExternalEditor))
  3227. {
  3228. // no need for handle
  3229. if (Process != NULL)
  3230. {
  3231. DebugCheck(CloseHandle(Process));
  3232. }
  3233. Process = INVALID_HANDLE_VALUE;
  3234. }
  3235. else
  3236. {
  3237. if (Process == NULL)
  3238. {
  3239. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3240. }
  3241. }
  3242. if (!IsSideLocalBrowser(Side))
  3243. {
  3244. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3245. AppLogFmt(L"Opened external editor with remote file \"%s\".", (FileName));
  3246. }
  3247. }
  3248. }
  3249. //---------------------------------------------------------------------------
  3250. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3251. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3252. void * /*Arg*/)
  3253. {
  3254. if (Token != NULL)
  3255. {
  3256. EditorFormFileSave(static_cast<TForm *>(Token));
  3257. }
  3258. }
  3259. //---------------------------------------------------------------------------
  3260. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3261. {
  3262. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3263. {
  3264. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3265. }
  3266. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3267. }
  3268. //---------------------------------------------------------------------------
  3269. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3270. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3271. void * Arg)
  3272. {
  3273. if ((Token != NULL) &&
  3274. IsEditorFormModified(static_cast<TForm *>(Token)))
  3275. {
  3276. *static_cast<bool *>(Arg) = true;
  3277. }
  3278. }
  3279. //---------------------------------------------------------------------------
  3280. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3281. TObject * /*Sender*/, bool & Modified)
  3282. {
  3283. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3284. {
  3285. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3286. {
  3287. Modified = true;
  3288. }
  3289. }
  3290. if (!Modified)
  3291. {
  3292. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3293. }
  3294. }
  3295. //---------------------------------------------------------------------------
  3296. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3297. {
  3298. DebugCheckNotEqual(FLocalEditors->Extract(Sender), NULL);
  3299. }
  3300. //---------------------------------------------------------------------------
  3301. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3302. const UnicodeString & RemoteDirectory, const TCopyParamType & CopyParam, bool Simple,
  3303. UnicodeString & Result, UnicodeString & RootDirectory)
  3304. {
  3305. if (!WinConfiguration->TemporaryDirectoryDeterministic || Simple)
  3306. {
  3307. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3308. Result = RootDirectory;
  3309. }
  3310. else
  3311. {
  3312. RootDirectory = L"";
  3313. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3314. Result = IncludeTrailingBackslash(Result);
  3315. }
  3316. if (WinConfiguration->TemporaryDirectoryAppendSession && !Simple)
  3317. {
  3318. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3319. }
  3320. if (WinConfiguration->TemporaryDirectoryAppendPath && !Simple)
  3321. {
  3322. UnicodeString ARemoteDirectory = RemoteDirectory;
  3323. if (!ARemoteDirectory.IsEmpty() && (ARemoteDirectory[1] == L'/'))
  3324. {
  3325. ARemoteDirectory.Delete(1, 1);
  3326. }
  3327. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(ARemoteDirectory)));
  3328. }
  3329. if (!ForceDirectories(ApiPath(Result)))
  3330. {
  3331. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3332. }
  3333. }
  3334. //---------------------------------------------------------------------------
  3335. UnicodeString TCustomScpExplorerForm::GetTempLocalName(const UnicodeString & Path, const TCopyParamType & CopyParam)
  3336. {
  3337. return Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(Path), osRemote, false);
  3338. }
  3339. //---------------------------------------------------------------------------
  3340. void TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3341. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3342. bool AutoOperation, bool SimpleTempDir)
  3343. {
  3344. DebugAssert(!IsLocalBrowserMode());
  3345. TCopyParamType CopyParam = TemporaryFileCopyParam(ForceText);
  3346. if (TempDir.IsEmpty())
  3347. {
  3348. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, SimpleTempDir, TempDir, RootTempDir);
  3349. }
  3350. DebugAssert(!FAutoOperation);
  3351. FAutoOperation = AutoOperation;
  3352. FMoveToQueue = false;
  3353. Terminal->ExceptionOnFail = true;
  3354. try
  3355. {
  3356. try
  3357. {
  3358. // turn off confirmations, as for MDI editors we may possibly download
  3359. // the same file over
  3360. Terminal->CopyToLocal(
  3361. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3362. }
  3363. catch(...)
  3364. {
  3365. if (!RootTempDir.IsEmpty())
  3366. {
  3367. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  3368. }
  3369. throw;
  3370. }
  3371. }
  3372. __finally
  3373. {
  3374. FAutoOperation = false;
  3375. Terminal->ExceptionOnFail = false;
  3376. }
  3377. }
  3378. //---------------------------------------------------------------------------
  3379. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3380. {
  3381. // Do not waste time checking for default editor list the next time,
  3382. // or testing if default editor is not notepad.
  3383. bool TryNextTime = false;
  3384. UnicodeString UsageState;
  3385. if (!WinConfiguration->EditorList->IsDefaultList())
  3386. {
  3387. UsageState = "H";
  3388. }
  3389. else
  3390. {
  3391. UnicodeString Executable;
  3392. UnicodeString ExecutableDescription;
  3393. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3394. {
  3395. UnicodeString Message =
  3396. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3397. unsigned int Answer =
  3398. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3399. if (Answer != qaOK)
  3400. {
  3401. UsageState = "R";
  3402. }
  3403. else
  3404. {
  3405. UsageState = "A";
  3406. TEditorData EditorData;
  3407. EditorData.Editor = edExternal;
  3408. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3409. EditorData.ExternalEditorOptionsAutodetect();
  3410. TEditorList EditorList;
  3411. EditorList = *WinConfiguration->EditorList;
  3412. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3413. WinConfiguration->EditorList = &EditorList;
  3414. }
  3415. }
  3416. }
  3417. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3418. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3419. }
  3420. //---------------------------------------------------------------------------
  3421. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3422. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3423. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3424. {
  3425. if (ExecuteFileBy == efDefaultEditor)
  3426. {
  3427. if (!WinConfiguration->OfferedEditorAutoConfig)
  3428. {
  3429. EditorAutoConfig();
  3430. }
  3431. const TEditorPreferences * Editor =
  3432. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3433. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3434. {
  3435. ExecuteFileBy = efInternalEditor;
  3436. ExternalEditor = NULL;
  3437. }
  3438. else if (Editor->Data->Editor == edOpen)
  3439. {
  3440. ExecuteFileBy = efShell;
  3441. ExternalEditor = NULL;
  3442. }
  3443. else
  3444. {
  3445. ExecuteFileBy = efExternalEditor;
  3446. ExternalEditor = Editor->Data;
  3447. }
  3448. }
  3449. }
  3450. //---------------------------------------------------------------------------
  3451. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3452. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3453. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3454. {
  3455. UnicodeString OriginalFileName;
  3456. UnicodeString LocalRootDirectory;
  3457. UnicodeString RemoteDirectory;
  3458. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3459. IsSideLocalBrowser(Side), MaskParams);
  3460. UnicodeString Counter;
  3461. UnicodeString LocalFileName;
  3462. bool Handled = false;
  3463. if (!IsSideLocalBrowser(Side))
  3464. {
  3465. // We need to trim VMS version here, so that we use name without version
  3466. // when uploading back to create a new version of the file
  3467. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3468. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3469. TObject * Token = NULL;
  3470. UnicodeString LocalDirectory;
  3471. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3472. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3473. LocalDirectory))
  3474. {
  3475. if (Token != NULL)
  3476. {
  3477. TForm * Form = dynamic_cast<TForm *>(Token);
  3478. if (Form->WindowState == wsMinimized)
  3479. {
  3480. ShowWindow(Form->Handle, SW_RESTORE);
  3481. }
  3482. else
  3483. {
  3484. Form->SetFocus();
  3485. }
  3486. Handled = true;
  3487. }
  3488. else
  3489. {
  3490. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3491. }
  3492. }
  3493. if (!Handled)
  3494. {
  3495. std::unique_ptr<TStrings> FileList1(new TStringList());
  3496. FileList1->AddObject(FullFileName, Object);
  3497. bool ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3498. TemporarilyDownloadFiles(FileList1.get(), ForceText, LocalRootDirectory, LocalDirectory, true, false);
  3499. LocalFileName = LocalDirectory + GetTempLocalName(FullFileName, TemporaryFileCopyParam(ForceText));
  3500. switch (ExecuteFileBy)
  3501. {
  3502. case efShell:
  3503. Counter = "RemoteFilesExecuted";
  3504. break;
  3505. case efInternalEditor:
  3506. Counter = "RemoteFilesOpenedInInternalEditor";
  3507. break;
  3508. case efExternalEditor:
  3509. Counter = "RemoteFilesOpenedInExternalEditor";
  3510. break;
  3511. default:
  3512. DebugFail();
  3513. }
  3514. }
  3515. }
  3516. else
  3517. {
  3518. LocalFileName = FullFileName;
  3519. OriginalFileName = ExtractFileName(FullFileName);
  3520. switch (ExecuteFileBy)
  3521. {
  3522. case efShell:
  3523. Counter = "LocalFilesExecuted";
  3524. break;
  3525. case efInternalEditor:
  3526. Counter = "LocalFilesOpenedInInternalEditor";
  3527. break;
  3528. case efExternalEditor:
  3529. Counter = "LocalFilesOpenedInExternalEditor";
  3530. break;
  3531. default:
  3532. DebugFail();
  3533. }
  3534. }
  3535. if (!Handled)
  3536. {
  3537. Configuration->Usage->Inc(Counter);
  3538. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3539. ExternalEditor, LocalRootDirectory, RemoteDirectory, false, MaskParams.Modification);
  3540. }
  3541. }
  3542. //---------------------------------------------------------------------------
  3543. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3544. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3545. bool AllSelected, bool OnFocused)
  3546. {
  3547. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3548. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3549. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3550. Side = GetSide(Side);
  3551. TCustomDirView * DView = DirView(Side);
  3552. TStrings * FileList =
  3553. AllSelected ?
  3554. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3555. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3556. try
  3557. {
  3558. DebugAssert(AllSelected || (FileList->Count == 1));
  3559. for (int i = 0; i < FileList->Count; i++)
  3560. {
  3561. UnicodeString ListFileName = FileList->Strings[i];
  3562. UnicodeString FileNameOnly =
  3563. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3564. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3565. if (!DView->ItemIsDirectory(Item))
  3566. {
  3567. UnicodeString FullFileName;
  3568. if (!IsSideLocalBrowser(Side))
  3569. {
  3570. FullFileName = DirView(Side)->Path + ListFileName;
  3571. }
  3572. else
  3573. {
  3574. FullFileName = ListFileName;
  3575. }
  3576. TObject * Object = FileList->Objects[i];
  3577. TFileMasks::TParams MaskParams;
  3578. MaskParams.Size = DView->ItemFileSize(Item);
  3579. TDateTimePrecision Precision;
  3580. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3581. std::unique_ptr<TRemoteFile> File;
  3582. if (!IsSideLocalBrowser(Side))
  3583. {
  3584. File.reset(Terminal->TryReadFile(FullFileName));
  3585. if (File.get() != NULL)
  3586. {
  3587. File->FullFileName = FullFileName;
  3588. Object = File.get();
  3589. MaskParams.Size = File->Size;
  3590. MaskParams.Modification = File->Modification;
  3591. }
  3592. }
  3593. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName, Object, MaskParams);
  3594. }
  3595. }
  3596. }
  3597. __finally
  3598. {
  3599. delete FileList;
  3600. }
  3601. }
  3602. //---------------------------------------------------------------------------
  3603. TCopyParamType TCustomScpExplorerForm::TemporaryFileCopyParam(bool ForceText)
  3604. {
  3605. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3606. CopyParam.FileNameCase = ncNoChange;
  3607. CopyParam.PreserveRights = false;
  3608. CopyParam.PreserveReadOnly = false;
  3609. CopyParam.ReplaceInvalidChars = true;
  3610. CopyParam.IncludeFileMask = TFileMasks();
  3611. CopyParam.NewerOnly = false;
  3612. CopyParam.FileMask = L"";
  3613. if (ForceText)
  3614. {
  3615. CopyParam.TransferMode = tmAscii;
  3616. }
  3617. return CopyParam;
  3618. }
  3619. //---------------------------------------------------------------------------
  3620. bool TCustomScpExplorerForm::EditorCheckNotModified(const TEditedFileData * Data)
  3621. {
  3622. return WinConfiguration->EditorCheckNotModified && (Data->SourceTimestamp != TDateTime());
  3623. }
  3624. //---------------------------------------------------------------------------
  3625. class TEditorUploadQueueItem : public TUploadQueueItem
  3626. {
  3627. public:
  3628. __fastcall TEditorUploadQueueItem(
  3629. TTerminal * Terminal, TStrings * FilesToCopy, const UnicodeString & TargetDir,
  3630. const TCopyParamType * CopyParam, int Params) :
  3631. TUploadQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, false)
  3632. {
  3633. }
  3634. protected:
  3635. virtual void __fastcall DoTransferExecute(TTerminal * Terminal, TParallelOperation * ParallelOperation)
  3636. {
  3637. TUploadQueueItem::DoTransferExecute(Terminal, ParallelOperation);
  3638. TTerminalManager::Instance()->ScpExplorer->EditedFileUploaded(Terminal, CompleteEvent);
  3639. }
  3640. };
  3641. //---------------------------------------------------------------------------
  3642. void TCustomScpExplorerForm::EditedFileUploaded(TTerminal * ATerminal, HANDLE UploadCompleteEvent)
  3643. {
  3644. if (WinConfiguration->EditorCheckNotModified) // optimization
  3645. {
  3646. UnicodeString RemoteFilePath;
  3647. {
  3648. TGuard Guard(FEditorManager->Section);
  3649. TEditedFileData * Data = FEditorManager->FindByUploadCompleteEvent(UploadCompleteEvent);
  3650. if (DebugAlwaysTrue(Data != NULL) &&
  3651. EditorCheckNotModified(Data))
  3652. {
  3653. RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3654. }
  3655. }
  3656. if (!RemoteFilePath.IsEmpty())
  3657. {
  3658. std::unique_ptr<TRemoteFile> File(ATerminal->TryReadFile(RemoteFilePath));
  3659. if (File.get() != NULL)
  3660. {
  3661. TGuard Guard(FEditorManager->Section);
  3662. TEditedFileData * Data = FEditorManager->FindByUploadCompleteEvent(UploadCompleteEvent);
  3663. if (DebugAlwaysTrue(Data != NULL))
  3664. {
  3665. Data->SourceTimestamp = File->Modification;
  3666. AppLogFmt(L"Uploaded edited remote file timestamp: %s", (StandardTimestamp(Data->SourceTimestamp)));
  3667. }
  3668. }
  3669. }
  3670. }
  3671. }
  3672. //---------------------------------------------------------------------------
  3673. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
  3674. const UnicodeString & FileName, const TDateTime & Timestamp, TEditedFileData * Data,
  3675. HANDLE UploadCompleteEvent, bool & Retry)
  3676. {
  3677. TTerminalManager * Manager = TTerminalManager::Instance();
  3678. bool IsInavailable = !IsAvailableTerminal(Data->Terminal);
  3679. if ((Manager->ActiveTerminal == Data->Terminal) &&
  3680. Manager->ScheduleTerminalReconnnect(Data->Terminal))
  3681. {
  3682. AppLog(L"Scheduled session reconnect, will retry the upload later");
  3683. Retry = true;
  3684. }
  3685. else if (Manager->IsReconnectingTerminal(Data->Terminal))
  3686. {
  3687. AppLog(L"Session is being reconnected, will retry the upload later");
  3688. Retry = true;
  3689. }
  3690. else if (IsInavailable &&
  3691. GUIConfiguration->SessionReopenAutoInactive &&
  3692. (Data->Terminal->NextInactiveReconnect != TDateTime()))
  3693. {
  3694. if (Data->Terminal->LastInactiveSave != Timestamp)
  3695. {
  3696. AppLog(L"Inactive session reconnect is pending, rescheduling it for as soon as possible to retry the upload");
  3697. Data->Terminal->NextInactiveReconnect = Now();
  3698. Data->Terminal->LastInactiveSave = Timestamp;
  3699. }
  3700. else
  3701. {
  3702. AppLog(L"Inactive session reconnect is pending, will retry the upload later");
  3703. }
  3704. Retry = true;
  3705. }
  3706. else if (IsInavailable)
  3707. {
  3708. if (!NonVisualDataModule->Busy)
  3709. {
  3710. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3711. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3712. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3713. if (Data->Terminal == NULL)
  3714. {
  3715. TManagedTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3716. if (SameSiteTerminal != NULL)
  3717. {
  3718. UnicodeString Message =
  3719. FMTLOAD(EDIT_SESSION_REATTACH,
  3720. (FileNameOnly, Data->SessionName, FileNameOnly));
  3721. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3722. {
  3723. Data->Terminal = SameSiteTerminal;
  3724. Data->SessionName = SameSiteTerminal->SessionData->SessionName;
  3725. // We might also overwrite session data
  3726. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3727. }
  3728. else
  3729. {
  3730. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3731. Abort();
  3732. }
  3733. }
  3734. }
  3735. // Foreground session should reconnect itself (except for those disconnected explicitly by the user)
  3736. else if (DebugAlwaysTrue(Terminal->Disconnected || (Terminal != Data->Terminal)))
  3737. {
  3738. UnicodeString Message =
  3739. MainInstructions(
  3740. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3741. TMessageParams Params(FLAGMASK(!GUIConfiguration->SessionReopenAutoInactive, mpNeverAskAgainCheck));
  3742. Params.NeverAskAgainTitle = LoadStr(ALWAYS_RECONNECT);
  3743. unsigned int Answer = MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_NONE, &Params);
  3744. if (Answer == qaNeverAskAgain)
  3745. {
  3746. GUIConfiguration->SessionReopenAutoInactive = true;
  3747. Answer = qaOK;
  3748. }
  3749. if (Answer == qaOK)
  3750. {
  3751. if (Terminal->Disconnected)
  3752. {
  3753. Manager->ActiveSession = Terminal;
  3754. ReconnectSession();
  3755. // Was it reconnected? (if not, Data->Terminal should be nulled now)
  3756. if (!IsAvailableTerminal(Data->Terminal) || (Terminal != Data->Terminal))
  3757. {
  3758. Abort();
  3759. }
  3760. }
  3761. else
  3762. {
  3763. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3764. }
  3765. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3766. }
  3767. else
  3768. {
  3769. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3770. Abort();
  3771. }
  3772. }
  3773. }
  3774. if (!IsAvailableTerminal(Data->Terminal))
  3775. {
  3776. Configuration->Usage->Inc(L"EditInactiveSession");
  3777. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3778. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3779. (ExtractFileName(FileName), Data->SessionName)));
  3780. }
  3781. }
  3782. if (!Retry && EditorCheckNotModified(Data))
  3783. {
  3784. if (NonVisualDataModule->Busy)
  3785. {
  3786. Retry = true;
  3787. }
  3788. else
  3789. {
  3790. UnicodeString RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3791. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(RemoteFilePath));
  3792. if (File.get() != NULL)
  3793. {
  3794. AppLogFmt(L"Edited remote file timestamp: %s, Original timestamp: %s", (StandardTimestamp(File->Modification), StandardTimestamp(Data->SourceTimestamp)));
  3795. if (File->Modification != Data->SourceTimestamp)
  3796. {
  3797. UnicodeString Message = MainInstructions(LoadStr(EDIT_CHANGED_EXTERNALLY));
  3798. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) != qaOK)
  3799. {
  3800. Abort();
  3801. }
  3802. }
  3803. }
  3804. }
  3805. }
  3806. if (!Retry)
  3807. {
  3808. TStrings * FileList = new TStringList();
  3809. try
  3810. {
  3811. FileList->Add(FileName);
  3812. // Consider using the same settings (preset) as when the file was downloaded.
  3813. // More over this does not reflect the actual session that will do the upload.
  3814. TCopyParamType CopyParam = TemporaryFileCopyParam(Data->ForceText);
  3815. // so i do not need to worry if masking algorithm works in all cases
  3816. // ("" means "copy file name", no masking is actually done)
  3817. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3818. {
  3819. CopyParam.FileMask = L"";
  3820. }
  3821. else
  3822. {
  3823. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3824. }
  3825. int Params = cpNoConfirmation | cpTemporary;
  3826. if (Data->Terminal->IsCapable[fcBackgroundTransfers])
  3827. {
  3828. TTerminalQueue * AQueue = Manager->FindQueueForTerminal(Data->Terminal);
  3829. TQueueItem * QueueItem =
  3830. new TEditorUploadQueueItem(Data->Terminal, FileList, Data->RemoteDirectory, &CopyParam, Params);
  3831. QueueItem->CompleteEvent = UploadCompleteEvent;
  3832. AddQueueItem(AQueue, QueueItem, Data->Terminal);
  3833. }
  3834. else
  3835. {
  3836. if (NonVisualDataModule->Busy)
  3837. {
  3838. Retry = true;
  3839. }
  3840. else
  3841. {
  3842. FMoveToQueue = false;
  3843. Data->Terminal->CopyToRemote(FileList, Data->RemoteDirectory, &CopyParam, Params, NULL);
  3844. SetEvent(UploadCompleteEvent);
  3845. }
  3846. }
  3847. }
  3848. __finally
  3849. {
  3850. delete FileList;
  3851. }
  3852. }
  3853. }
  3854. //---------------------------------------------------------------------------
  3855. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3856. const UnicodeString & FileName, TEditedFileData * Data)
  3857. {
  3858. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3859. DebugAssert(!NonVisualDataModule->Busy);
  3860. if (!IsAvailableTerminal(Data->Terminal))
  3861. {
  3862. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3863. (ExtractFileName(FileName), Data->SessionName)));
  3864. }
  3865. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3866. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3867. NonVisualDataModule->StartBusy();
  3868. try
  3869. {
  3870. std::unique_ptr<TRemoteFile> File;
  3871. UnicodeString RemoteFileName =
  3872. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3873. Terminal->ExceptionOnFail = true;
  3874. try
  3875. {
  3876. File.reset(Terminal->ReadFile(RemoteFileName));
  3877. if (!File->HaveFullFileName)
  3878. {
  3879. File->FullFileName = RemoteFileName;
  3880. }
  3881. }
  3882. __finally
  3883. {
  3884. Terminal->ExceptionOnFail = false;
  3885. }
  3886. std::unique_ptr<TStrings> FileList(new TStringList());
  3887. FileList->AddObject(RemoteFileName, File.get());
  3888. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3889. UnicodeString TempDir = ExtractFilePath(FileName);
  3890. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, false);
  3891. if (EditorCheckNotModified(Data))
  3892. {
  3893. Data->SourceTimestamp = File->Modification;
  3894. }
  3895. }
  3896. __finally
  3897. {
  3898. NonVisualDataModule->EndBusy();
  3899. // it actually may not exist anymore...
  3900. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3901. }
  3902. }
  3903. //---------------------------------------------------------------------------
  3904. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3905. const TEditedFileData * Data, bool & KeepOpen)
  3906. {
  3907. // Command is set for external editors only (not for "shell" open).
  3908. if (!Data->Command.IsEmpty())
  3909. {
  3910. bool AnyFound = false;
  3911. bool AnyMDI = false;
  3912. bool AnyNonMDI = false;
  3913. bool AnyDetect = false;
  3914. TEditorList * EditorList = new TEditorList();
  3915. try
  3916. {
  3917. *EditorList = *WinConfiguration->EditorList;
  3918. for (int i = 0; i < EditorList->Count; i++)
  3919. {
  3920. const TEditorPreferences * Editor = EditorList->Editors[i];
  3921. if ((Editor->Data->Editor == edExternal) &&
  3922. (Editor->Data->ExternalEditor == Data->Command))
  3923. {
  3924. AnyFound = true;
  3925. if (Editor->Data->SDIExternalEditor)
  3926. {
  3927. AnyNonMDI = true;
  3928. if (Editor->Data->DetectMDIExternalEditor)
  3929. {
  3930. AnyDetect = true;
  3931. }
  3932. }
  3933. else
  3934. {
  3935. AnyMDI = true;
  3936. }
  3937. }
  3938. }
  3939. bool EnableMDI = false;
  3940. bool DisableDetect = false;
  3941. if (AnyMDI)
  3942. {
  3943. KeepOpen = true;
  3944. if (AnyNonMDI)
  3945. {
  3946. // there is at least one instance of the editor with MDI support enabled,
  3947. // and one with disabled, enable it for all instances
  3948. EnableMDI = true;
  3949. }
  3950. }
  3951. else if (AnyFound && !AnyDetect)
  3952. {
  3953. // at least once instance found but all have MDI autodetection disabled
  3954. // => close the file (default action)
  3955. }
  3956. else
  3957. {
  3958. // no instance of the editor has MDI support enabled
  3959. TMessageParams Params;
  3960. if (AnyFound)
  3961. {
  3962. // there is at least one instance of the editor
  3963. Params.Params |= mpNeverAskAgainCheck;
  3964. }
  3965. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3966. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3967. switch (Answer)
  3968. {
  3969. case qaNeverAskAgain:
  3970. DisableDetect = true;
  3971. break;
  3972. case qaNo:
  3973. EnableMDI = true;
  3974. KeepOpen = true;
  3975. break;
  3976. }
  3977. }
  3978. if (AnyFound && (EnableMDI || DisableDetect))
  3979. {
  3980. bool Changed = false;
  3981. for (int i = 0; i < EditorList->Count; i++)
  3982. {
  3983. const TEditorPreferences * Editor = EditorList->Editors[i];
  3984. if ((Editor->Data->Editor == edExternal) &&
  3985. (Editor->Data->ExternalEditor == Data->Command) &&
  3986. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3987. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3988. {
  3989. Changed = true;
  3990. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3991. if (EnableMDI)
  3992. {
  3993. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3994. }
  3995. if (DisableDetect)
  3996. {
  3997. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3998. }
  3999. EditorList->Change(i, UpdatedEditor);
  4000. }
  4001. }
  4002. if (Changed)
  4003. {
  4004. WinConfiguration->EditorList = EditorList;
  4005. }
  4006. }
  4007. }
  4008. __finally
  4009. {
  4010. delete EditorList;
  4011. }
  4012. }
  4013. else
  4014. {
  4015. // "open" case
  4016. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  4017. qaOK, HELP_APP_EARLY_CLOSED);
  4018. }
  4019. }
  4020. //---------------------------------------------------------------------------
  4021. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  4022. {
  4023. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  4024. }
  4025. //---------------------------------------------------------------------------
  4026. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  4027. {
  4028. SideEnter(osRemote);
  4029. }
  4030. //---------------------------------------------------------------------------
  4031. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  4032. {
  4033. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  4034. SideEnter(osRemote);
  4035. }
  4036. //---------------------------------------------------------------------------
  4037. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  4038. {
  4039. FCurrentSide = Side;
  4040. if (Visible)
  4041. {
  4042. UpdateControls();
  4043. }
  4044. }
  4045. //---------------------------------------------------------------------------
  4046. void __fastcall TCustomScpExplorerForm::FileDeleted(
  4047. TOperationSide Side, const UnicodeString & FileName, bool Success, bool NotCancelled)
  4048. {
  4049. VisualiseOperationFinished(Side, FileName, Success && NotCancelled);
  4050. if (DebugAlwaysTrue(FDeletedFiles != NULL) && Success)
  4051. {
  4052. FDeletedFiles->Add(FileName);
  4053. }
  4054. }
  4055. //---------------------------------------------------------------------------
  4056. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  4057. TStrings * FileList, bool Alternative)
  4058. {
  4059. TCustomDirView * DView = DirView(Side);
  4060. DView->SaveSelection();
  4061. DView->SaveSelectedNames();
  4062. DebugAssert(!FAlternativeDelete);
  4063. FAlternativeDelete = Alternative;
  4064. try
  4065. {
  4066. int Params = FLAGMASK(Alternative, dfAlternative);
  4067. bool Remote = !IsSideLocalBrowser(Side);
  4068. std::unique_ptr<TStringList> DeletedFiles;
  4069. std::unique_ptr<TStrings> PermanentFileList;
  4070. bool AllowMoveToQueue = (Terminal != NULL);
  4071. if (AllowMoveToQueue)
  4072. {
  4073. DeletedFiles.reset(new TStringList());
  4074. DeletedFiles->CaseSensitive = Remote;
  4075. }
  4076. try
  4077. {
  4078. DebugAssert(FDeletedFiles == NULL);
  4079. TValueRestorer<TStrings *> DeletedFilesRestorer(FDeletedFiles, DeletedFiles.get());
  4080. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  4081. if (AllowMoveToQueue)
  4082. {
  4083. FOnFileOperationFinished = FileDeleted;
  4084. }
  4085. FMoveToQueue = false;
  4086. if (Remote)
  4087. {
  4088. DebugAssert(AllowMoveToQueue);
  4089. // Clone the file list as it may refer to current directory files, which get destroyed, when the directory is reloaded after the operation
  4090. PermanentFileList.reset(TRemoteFileList::CloneStrings(FileList));
  4091. DebugAssert(Terminal != NULL);
  4092. Terminal->DeleteFiles(FileList, Params);
  4093. }
  4094. else
  4095. {
  4096. if (AllowMoveToQueue) // optimization)
  4097. {
  4098. PermanentFileList.reset(CloneStrings(FileList));
  4099. }
  4100. if (IsLocalBrowserMode())
  4101. {
  4102. Configuration->Usage->Inc(L"LocalLocalDeletes");
  4103. }
  4104. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide, FCurrentSide);
  4105. try
  4106. {
  4107. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, Params);
  4108. }
  4109. __finally
  4110. {
  4111. ReloadLocalDirectory();
  4112. }
  4113. }
  4114. // Probably not needed for deleting, just for consistency with transfer code
  4115. if (FMoveToQueue)
  4116. {
  4117. Abort();
  4118. }
  4119. }
  4120. catch (EAbort &)
  4121. {
  4122. if (FMoveToQueue && DebugAlwaysTrue(AllowMoveToQueue) && DebugAlwaysTrue(Terminal != NULL))
  4123. {
  4124. FMoveToQueue = false;
  4125. DeletedFiles->Sorted = true;
  4126. for (int Index = 0; Index < PermanentFileList->Count; Index++)
  4127. {
  4128. if (DeletedFiles->IndexOf(PermanentFileList->Strings[Index]) >= 0)
  4129. {
  4130. // We should always be deleting the first item => what can be used to optimize this code, if needed
  4131. DebugAssert(Index == 0);
  4132. PermanentFileList->Delete(Index);
  4133. Index--;
  4134. }
  4135. }
  4136. if (Remote)
  4137. {
  4138. Configuration->Usage->Inc("MovesToBackgroundDelete");
  4139. TQueueItem * QueueItem = new TRemoteDeleteQueueItem(Terminal, PermanentFileList.get(), Params);
  4140. AddQueueItem(Queue, QueueItem, Terminal);
  4141. }
  4142. else
  4143. {
  4144. Configuration->Usage->Inc("MovesToBackgroundDeleteLocal");
  4145. TQueueItem * QueueItem = new TLocalDeleteQueueItem(PermanentFileList.get(), Params);
  4146. AddQueueItem(Queue, QueueItem, Terminal);
  4147. }
  4148. ClearOperationSelection(Side);
  4149. }
  4150. }
  4151. FAlternativeDelete = false;
  4152. }
  4153. catch(...)
  4154. {
  4155. FAlternativeDelete = false;
  4156. DView->DiscardSavedSelection();
  4157. throw;
  4158. }
  4159. DView->RestoreSelection();
  4160. }
  4161. //---------------------------------------------------------------------------
  4162. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  4163. {
  4164. DebugAssert(Terminal);
  4165. DebugAssert(!IsLocalBrowserMode());
  4166. RemoteDirView->SaveSelection();
  4167. RemoteDirView->SaveSelectedNames();
  4168. try
  4169. {
  4170. if (Lock)
  4171. {
  4172. Terminal->LockFiles(FileList);
  4173. }
  4174. else
  4175. {
  4176. Terminal->UnlockFiles(FileList);
  4177. }
  4178. }
  4179. catch(...)
  4180. {
  4181. RemoteDirView->DiscardSavedSelection();
  4182. throw;
  4183. }
  4184. RemoteDirView->RestoreSelection();
  4185. }
  4186. //---------------------------------------------------------------------------
  4187. bool TCustomScpExplorerForm::DoDirectoryExists(void * Session, const UnicodeString & Directory)
  4188. {
  4189. TTerminal * ATerminal = (Session == NULL) ? Terminal : reinterpret_cast<TTerminal *>(Session);
  4190. bool Result =
  4191. IsAvailableTerminal(ATerminal) &&
  4192. ATerminal->DirectoryExists(Directory);
  4193. return Result;
  4194. }
  4195. //---------------------------------------------------------------------------
  4196. bool TCustomScpExplorerForm::NeedSecondarySessionForRemoteCopy(TStrings * FileList)
  4197. {
  4198. bool CopyDirsOnSecondarySession = Terminal->IsCapable[fcSecondaryShell];
  4199. return
  4200. !Terminal->IsCapable[fcRemoteCopy] ||
  4201. (CopyDirsOnSecondarySession && TRemoteFileList::AnyDirectory(FileList));
  4202. }
  4203. //---------------------------------------------------------------------------
  4204. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  4205. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  4206. bool NoConfirmation, bool Move)
  4207. {
  4208. DebugAssert(Terminal != NULL);
  4209. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  4210. DebugAssert(!IsLocalBrowserMode());
  4211. if (Session == NULL)
  4212. {
  4213. Session = Terminal;
  4214. }
  4215. bool TargetConfirmed = false;
  4216. if (Session == Terminal)
  4217. {
  4218. if (RemoteDriveView->DropTarget != NULL)
  4219. {
  4220. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  4221. TargetConfirmed = true;
  4222. }
  4223. else if (RemoteDirView->DropTarget != NULL)
  4224. {
  4225. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  4226. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  4227. TargetConfirmed = true;
  4228. }
  4229. else
  4230. {
  4231. Target = RemoteDirView->Path;
  4232. }
  4233. }
  4234. else
  4235. {
  4236. Target = GetSessionPath(Session, osRemote);
  4237. }
  4238. Target = UnixIncludeTrailingBackslash(Target);
  4239. if (FileList->Count == 1)
  4240. {
  4241. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  4242. }
  4243. else
  4244. {
  4245. FileMask = AnyMask;
  4246. }
  4247. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  4248. bool Result = true;
  4249. if (!NoConfirmation)
  4250. {
  4251. bool Multi = (FileList->Count > 1);
  4252. if (Move)
  4253. {
  4254. Result = DoRemoteMoveDialog(Multi, Target, FileMask, DoDirectoryExists);
  4255. }
  4256. else
  4257. {
  4258. std::unique_ptr<TStrings> Sessions(new TStringList);
  4259. std::unique_ptr<TStrings> Directories(new TStringList);
  4260. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  4261. for (int Index = 0; Index < SessionList->Count; Index++)
  4262. {
  4263. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  4264. if (IsAvailableTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  4265. {
  4266. Sessions->AddObject(SessionList->Strings[Index], ASession);
  4267. Directories->Add(GetSessionPath(ASession, osRemote));
  4268. }
  4269. }
  4270. bool AnyDirectory = false;
  4271. bool CopyDirsOnSecondarySession = Terminal->IsCapable[fcSecondaryShell];
  4272. if (CopyDirsOnSecondarySession && !Terminal->CommandSessionOpened) // optimization
  4273. {
  4274. for (int Index = 0; !AnyDirectory && (Index < FileList->Count); Index++)
  4275. {
  4276. AnyDirectory = DebugNotNull(dynamic_cast<TRemoteFile *>(FileList->Objects[Index]))->IsDirectory;
  4277. }
  4278. }
  4279. TDirectRemoteCopy AllowDirectCopy;
  4280. if (Terminal->CommandSessionOpened || !NeedSecondarySessionForRemoteCopy(FileList))
  4281. {
  4282. DebugAssert(DirectCopy);
  4283. AllowDirectCopy = drcAllow;
  4284. }
  4285. else if (Terminal->IsCapable[fcSecondaryShell])
  4286. {
  4287. DebugAssert(DirectCopy);
  4288. AllowDirectCopy = Terminal->IsCapable[fcRemoteCopy] ? drcConfirmCommandSessionDirs : drcConfirmCommandSession;
  4289. }
  4290. else
  4291. {
  4292. DebugAssert(!DirectCopy);
  4293. AllowDirectCopy = drcDisallow;
  4294. }
  4295. void * ASession = Session;
  4296. Result = DoRemoteCopyDialog(
  4297. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal,
  4298. DoDirectoryExists, TargetConfirmed);
  4299. Session = static_cast<TManagedTerminal *>(ASession);
  4300. }
  4301. }
  4302. return Result;
  4303. }
  4304. //---------------------------------------------------------------------------
  4305. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  4306. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  4307. {
  4308. DebugAssert(!IsLocalBrowserMode());
  4309. bool DirectCopy;
  4310. UnicodeString Target, FileMask;
  4311. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  4312. if (Result)
  4313. {
  4314. if (!Move && !DirectCopy)
  4315. {
  4316. Configuration->Usage->Inc("RemoteCopyTemp");
  4317. UnicodeString RootTempDir;
  4318. UnicodeString TempDir;
  4319. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true);
  4320. TStrings * TemporaryFilesList = new TStringList();
  4321. try
  4322. {
  4323. TMakeLocalFileListParams MakeFileListParam;
  4324. MakeFileListParam.FileList = TemporaryFilesList;
  4325. MakeFileListParam.FileTimes = NULL;
  4326. MakeFileListParam.IncludeDirs = true;
  4327. MakeFileListParam.Recursive = false;
  4328. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  4329. TTerminalManager::Instance()->ActiveSession = Session;
  4330. if (TemporaryFilesList->Count > 0)
  4331. {
  4332. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4333. CopyParam.FileMask = FileMask;
  4334. DebugAssert(!FAutoOperation);
  4335. FAutoOperation = true;
  4336. FMoveToQueue = false;
  4337. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  4338. }
  4339. }
  4340. __finally
  4341. {
  4342. delete TemporaryFilesList;
  4343. FAutoOperation = false;
  4344. if (!RootTempDir.IsEmpty())
  4345. {
  4346. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  4347. }
  4348. }
  4349. }
  4350. else
  4351. {
  4352. RemoteDirView->SaveSelection();
  4353. RemoteDirView->SaveSelectedNames();
  4354. try
  4355. {
  4356. if (Move)
  4357. {
  4358. Configuration->Usage->Inc("RemoteMove");
  4359. Terminal->MoveFiles(FileList, Target, FileMask, false);
  4360. }
  4361. else
  4362. {
  4363. Configuration->Usage->Inc("RemoteCopyDirect");
  4364. DebugAssert(DirectCopy);
  4365. DebugAssert(Session == Terminal);
  4366. if (!NeedSecondarySessionForRemoteCopy(FileList) ||
  4367. Terminal->CommandSessionOpened ||
  4368. CommandSessionFallback())
  4369. {
  4370. Terminal->CopyFiles(FileList, Target, FileMask, false);
  4371. }
  4372. }
  4373. }
  4374. catch(...)
  4375. {
  4376. RemoteDirView->DiscardSavedSelection();
  4377. throw;
  4378. }
  4379. RemoteDirView->RestoreSelection();
  4380. }
  4381. }
  4382. return Result;
  4383. }
  4384. //---------------------------------------------------------------------------
  4385. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4386. const UnicodeString & Path, TRemoteProperties & Properties)
  4387. {
  4388. DebugAssert(!IsLocalBrowserMode());
  4389. Properties.Valid = Properties.Valid << vpEncrypt;
  4390. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4391. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4392. }
  4393. //---------------------------------------------------------------------------
  4394. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4395. {
  4396. Side = GetSide(Side);
  4397. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4398. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4399. UnicodeString Name = LoadStr(NEW_FOLDER);
  4400. int AllowedChanges =
  4401. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4402. bool SaveSettings = false;
  4403. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4404. {
  4405. TWindowLock Lock(this);
  4406. if (!IsSideLocalBrowser(Side))
  4407. {
  4408. if (SaveSettings)
  4409. {
  4410. GUIConfiguration->NewDirectoryProperties = Properties;
  4411. }
  4412. CreateRemoteDirectory(Name, Properties);
  4413. }
  4414. else
  4415. {
  4416. DirView(Side)->CreateDirectory(Name);
  4417. }
  4418. }
  4419. }
  4420. //---------------------------------------------------------------------------
  4421. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4422. {
  4423. TWindowLock Lock(this);
  4424. DirView(Side)->ExecuteHomeDirectory();
  4425. }
  4426. //---------------------------------------------------------------------------
  4427. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4428. {
  4429. DoOpenDirectoryDialog(odBrowse, Side);
  4430. }
  4431. //---------------------------------------------------------------------------
  4432. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4433. {
  4434. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4435. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4436. bool Result = !Path.IsEmpty();
  4437. if (Result)
  4438. {
  4439. TryOpenDirectory(Side, Path);
  4440. }
  4441. return Result;
  4442. }
  4443. //---------------------------------------------------------------------------
  4444. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4445. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4446. bool & Close)
  4447. {
  4448. // terminal can be already closed (e.g. dropped connection)
  4449. if (Terminal != NULL)
  4450. {
  4451. try
  4452. {
  4453. TCalculateSizeParams Params;
  4454. Params.Stats = &Stats;
  4455. Terminal->CalculateFilesSize(FileList, Size, Params);
  4456. }
  4457. catch(...)
  4458. {
  4459. if (!Terminal->Active)
  4460. {
  4461. Close = true;
  4462. }
  4463. throw;
  4464. }
  4465. }
  4466. }
  4467. //---------------------------------------------------------------------------
  4468. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4469. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4470. bool & Close)
  4471. {
  4472. if ((Terminal != NULL) && // terminal can be already closed (e.g. dropped connection)
  4473. EnsureCommandSessionFallback(fcCalculatingChecksum))
  4474. {
  4475. Configuration->Usage->Inc(L"ChecksumCalculated");
  4476. try
  4477. {
  4478. Terminal->CalculateFilesChecksum(Alg, FileList, OnCalculatedChecksum);
  4479. }
  4480. catch(...)
  4481. {
  4482. if (!Terminal->Active)
  4483. {
  4484. Close = true;
  4485. }
  4486. throw;
  4487. }
  4488. }
  4489. }
  4490. //---------------------------------------------------------------------------
  4491. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4492. {
  4493. if (Terminal->LoadFilesProperties(FileList))
  4494. {
  4495. RemoteDirView->Invalidate();
  4496. }
  4497. }
  4498. //---------------------------------------------------------------------------
  4499. bool TCustomScpExplorerForm::CanCalculateChecksum()
  4500. {
  4501. return
  4502. Terminal->IsCapable[fcCalculatingChecksum] ||
  4503. (Terminal->IsCapable[fcSecondaryShell] && !Terminal->IsEncryptingFiles());
  4504. }
  4505. //---------------------------------------------------------------------------
  4506. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4507. {
  4508. bool Result;
  4509. if (!IsSideLocalBrowser(Side))
  4510. {
  4511. TRemoteTokenList * GroupList = NULL;
  4512. TRemoteTokenList * UserList = NULL;
  4513. try
  4514. {
  4515. TRemoteProperties CurrentProperties;
  4516. LoadFilesProperties(FileList);
  4517. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4518. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4519. if (CapableGroupChanging || CapableOwnerChanging)
  4520. {
  4521. if (CapableGroupChanging)
  4522. {
  4523. GroupList = Terminal->Groups->Duplicate();
  4524. }
  4525. if (CapableOwnerChanging)
  4526. {
  4527. UserList = Terminal->Users->Duplicate();
  4528. }
  4529. TRemoteDirectory * Files = Terminal->Files;
  4530. int Count = Files->Count;
  4531. if (Count > 100)
  4532. {
  4533. Count = 100;
  4534. }
  4535. for (int Index = 0; Index < Count; Index++)
  4536. {
  4537. TRemoteFile * File = Files->Files[Index];
  4538. if (CapableGroupChanging)
  4539. {
  4540. GroupList->AddUnique(File->Group);
  4541. }
  4542. if (CapableOwnerChanging)
  4543. {
  4544. UserList->AddUnique(File->Owner);
  4545. }
  4546. }
  4547. // if we haven't collected tokens for all files in current directory,
  4548. // make sure we collect them at least for all selected files.
  4549. // (note that so far the files in FileList has to be from current direcotry)
  4550. if (Count < Files->Count)
  4551. {
  4552. for (int Index = 0; Index < FileList->Count; Index++)
  4553. {
  4554. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4555. if (CapableGroupChanging)
  4556. {
  4557. GroupList->AddUnique(File->Group);
  4558. }
  4559. if (CapableOwnerChanging)
  4560. {
  4561. UserList->AddUnique(File->Owner);
  4562. }
  4563. }
  4564. }
  4565. }
  4566. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4567. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4568. if (CapableAclChanging && TRemoteFileList::AnyDirectory(FileList))
  4569. {
  4570. CapableAclChanging = false;
  4571. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4572. }
  4573. int Flags = 0;
  4574. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4575. if (CapableAclChanging) Flags |= cpAcl;
  4576. if (CapableOwnerChanging) Flags |= cpOwner;
  4577. if (CapableGroupChanging) Flags |= cpGroup;
  4578. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4579. if (CanCalculateChecksum())
  4580. {
  4581. CalculateChecksumEvent = CalculateChecksum;
  4582. }
  4583. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4584. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4585. int Options =
  4586. FLAGMASK(Terminal->IsCapable[fcGroupOwnerChangingByID], poUserGroupByID) |
  4587. FLAGMASK(Terminal->IsCapable[fcTags], poTags);
  4588. TRemoteProperties NewProperties = CurrentProperties;
  4589. Result =
  4590. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4591. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags, Options,
  4592. CalculateSize, CalculateChecksumEvent);
  4593. if (Result)
  4594. {
  4595. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4596. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4597. }
  4598. }
  4599. __finally
  4600. {
  4601. delete GroupList;
  4602. delete UserList;
  4603. }
  4604. }
  4605. else
  4606. {
  4607. DirView(Side)->DisplayPropertiesMenu();
  4608. Result = true;
  4609. }
  4610. return Result;
  4611. }
  4612. //---------------------------------------------------------------------------
  4613. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4614. {
  4615. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4616. {
  4617. FIgnoreNextDialogChar = Key;
  4618. }
  4619. Key = 0;
  4620. }
  4621. //---------------------------------------------------------------------------
  4622. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4623. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4624. {
  4625. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4626. if (Command != NULL)
  4627. {
  4628. KeyProcessed(Key, Shift);
  4629. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4630. {
  4631. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4632. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4633. false, false, const_cast<TCustomCommandType *>(Command));
  4634. }
  4635. }
  4636. }
  4637. //---------------------------------------------------------------------------
  4638. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4639. {
  4640. if (!NonVisualDataModule->Busy)
  4641. {
  4642. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4643. {
  4644. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4645. }
  4646. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4647. {
  4648. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4649. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4650. {
  4651. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4652. // But so far it's not a problem, as it does what we want.
  4653. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4654. if (((Action->ShortCut == KeyShortCut) ||
  4655. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4656. AllowedAction(Action, aaShortCut))
  4657. {
  4658. // Has to be called before the action as the dialog char is already in queue.
  4659. // So when the action consumes message queue, we already need to have the
  4660. // FIgnoreNextDialogChar set
  4661. KeyProcessed(Key, Shift);
  4662. // Reset reference to previous component (when menu/toolbar was clicked).
  4663. // Needed to detect that action was invoked by keyboard shortcut
  4664. // in TNonVisualDataModule::ExplorerActionsExecute
  4665. Action->ActionComponent = NULL;
  4666. Action->Execute();
  4667. return;
  4668. }
  4669. }
  4670. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4671. {
  4672. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4673. {
  4674. KeyProcessed(Key, Shift);
  4675. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4676. return;
  4677. }
  4678. }
  4679. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4680. {
  4681. KeyProcessed(Key, Shift);
  4682. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4683. }
  4684. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4685. if (IsCustomShortCut(CustomShortCut))
  4686. {
  4687. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4688. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4689. if (WinConfiguration->SharedBookmarks != NULL)
  4690. {
  4691. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4692. if ((Bookmark != NULL) &&
  4693. OpenBookmark(FCurrentSide, Bookmark))
  4694. {
  4695. KeyProcessed(Key, Shift);
  4696. }
  4697. }
  4698. }
  4699. }
  4700. }
  4701. TForm::KeyDown(Key, Shift);
  4702. }
  4703. //---------------------------------------------------------------------------
  4704. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4705. {
  4706. bool OpenedTerminal = false;
  4707. UnicodeString ProtocolName;
  4708. UnicodeString ProtocolNameHint;
  4709. UnicodeString SecurityProtocolName;
  4710. UnicodeString DurationHint;
  4711. // See the comment in TScpCommanderForm::UpdateControls()
  4712. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4713. if (ActiveTerminal != NULL)
  4714. {
  4715. // Active is not enough here
  4716. if (ActiveTerminal->Status == ssOpened)
  4717. {
  4718. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4719. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4720. if (FileSystemInfo.ProtocolName.IsEmpty())
  4721. {
  4722. ProtocolName = SessionInfo.ProtocolName;
  4723. }
  4724. else
  4725. {
  4726. ProtocolName = FileSystemInfo.ProtocolName;
  4727. }
  4728. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4729. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4730. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4731. OpenedTerminal = true;
  4732. }
  4733. else if (ActiveTerminal->Status > ssClosed)
  4734. {
  4735. ProtocolName = LoadStr(STATUS_CONNECTING);
  4736. }
  4737. else
  4738. {
  4739. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4740. }
  4741. }
  4742. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4743. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4744. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4745. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4746. if (ProtocolStatusPanel->Caption != ProtocolName)
  4747. {
  4748. ProtocolStatusPanel->Caption = ProtocolName;
  4749. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4750. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4751. }
  4752. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4753. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4754. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4755. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4756. SecurityStatusPanel->Enabled = SecurityEnabled;
  4757. UnicodeString SecurityProtocolHint;
  4758. if (SecurityEnabled)
  4759. {
  4760. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4761. }
  4762. else
  4763. {
  4764. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4765. }
  4766. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4767. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4768. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4769. DurationStatusPanel->Hint = DurationHint;
  4770. UpdateStatusBar();
  4771. }
  4772. //---------------------------------------------------------------------------
  4773. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4774. {
  4775. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4776. DebugAssert(SessionStatusBar != NULL);
  4777. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4778. if (!FNote.IsEmpty())
  4779. {
  4780. NoteStatusPanel->Caption = FNote;
  4781. }
  4782. else
  4783. {
  4784. UpdateStatusPanelText(NoteStatusPanel);
  4785. }
  4786. NoteStatusPanel->Hint = FNoteHints;
  4787. UnicodeString LoginTime;
  4788. // See the comment in TScpCommanderForm::UpdateControls()
  4789. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4790. // Active is not enough here
  4791. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4792. {
  4793. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4794. LoginTime = FormatDateTimeSpan(Now() - SessionInfo.LoginTime);
  4795. }
  4796. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4797. DurationStatusPanel->Caption = LoginTime;
  4798. }
  4799. //---------------------------------------------------------------------------
  4800. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4801. {
  4802. Panel->Caption = L"";
  4803. }
  4804. //---------------------------------------------------------------------------
  4805. void __fastcall TCustomScpExplorerForm::Idle()
  4806. {
  4807. if (FShowing ||
  4808. // Particularly for
  4809. // - Standalone editing
  4810. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4811. // while "keeping remote directory up to date".
  4812. StandaloneOperation)
  4813. {
  4814. FEditorManager->Check();
  4815. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4816. // that needs to know if queue view is visible (and it may be closed after queue update)
  4817. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4818. }
  4819. if (FShowing)
  4820. {
  4821. if (!NonVisualDataModule->Busy &&
  4822. // Menu is opened or drag&drop is going on
  4823. (Mouse->Capture == NULL))
  4824. {
  4825. if (FRefreshRemoteDirectory)
  4826. {
  4827. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4828. {
  4829. Terminal->RefreshDirectory();
  4830. }
  4831. FRefreshRemoteDirectory = false;
  4832. }
  4833. if (FRefreshLocalDirectory)
  4834. {
  4835. ReloadLocalDirectory();
  4836. FRefreshLocalDirectory = false;
  4837. }
  4838. if (WinConfiguration->RefreshRemotePanel)
  4839. {
  4840. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4841. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4842. {
  4843. RemoteDirView->ReloadDirectory();
  4844. }
  4845. }
  4846. }
  4847. // TB2 shows accels on Alt key down, but hides it only after menu modal loop finishes.
  4848. // But if Alt is used to switch apps or for keyboard shortcut, the menu modal loop does not happen and accelerators stay displayed.
  4849. // It's difficult to fix this in TB2, as there's no "up" equivalent to CM_DIALOGKEY, which shows the accels.
  4850. // 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.
  4851. TTBCustomToolbar * MenuToolbar = DebugNotNull(dynamic_cast<TTBCustomToolbar *>(GetComponent(fcMenu)));
  4852. MenuToolbar->Idle();
  4853. }
  4854. if (FShowing || StandaloneOperation)
  4855. {
  4856. if (FQueueStatusInvalidated)
  4857. {
  4858. UpdateQueueStatus(false);
  4859. }
  4860. RefreshQueueItems();
  4861. }
  4862. if (FShowing)
  4863. {
  4864. UpdateStatusBar();
  4865. }
  4866. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4867. {
  4868. FClipboardFakeMonitorsPendingReset = false;
  4869. FClipboardFakeMonitors.reset(NULL);
  4870. }
  4871. FIgnoreNextDialogChar = 0;
  4872. }
  4873. //---------------------------------------------------------------------------
  4874. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4875. {
  4876. try
  4877. {
  4878. FUserActionTimer->Enabled = false;
  4879. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4880. {
  4881. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4882. {
  4883. FPendingQueueActionItem->ProcessUserAction();
  4884. }
  4885. }
  4886. }
  4887. __finally
  4888. {
  4889. FPendingQueueActionItem = NULL;
  4890. }
  4891. }
  4892. //---------------------------------------------------------------------------
  4893. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4894. {
  4895. if (WinConfiguration->MinimizeToTray)
  4896. {
  4897. UpdateTrayIcon();
  4898. FTrayIcon->Visible = true;
  4899. if (Visible)
  4900. {
  4901. ShowWindow(Handle, SW_HIDE);
  4902. }
  4903. }
  4904. }
  4905. //---------------------------------------------------------------------------
  4906. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4907. {
  4908. // WORKAROUND
  4909. // When restoring maximized window from minimization,
  4910. // rarely some controls do not align properly.
  4911. // Two instances seen (both for Commander):
  4912. // - When restoring, window is temporarily narrower (not maximized),
  4913. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4914. // Once maximized already, top dock shinks back, but the session PageControl,
  4915. // do not align up, leaving space between TopDock and PageControl.
  4916. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4917. for (int Index = 0; Index < ControlCount; Index++)
  4918. {
  4919. RealignControl(Controls[Index]);
  4920. }
  4921. if (FTrayIcon->Visible)
  4922. {
  4923. FTrayIcon->Visible = false;
  4924. if (Visible)
  4925. {
  4926. ShowWindow(Handle, SW_SHOW);
  4927. }
  4928. }
  4929. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4930. {
  4931. FNeedSession = false;
  4932. NonVisualDataModule->StartBusy();
  4933. try
  4934. {
  4935. NeedSession(true);
  4936. }
  4937. __finally
  4938. {
  4939. NonVisualDataModule->EndBusy();
  4940. }
  4941. }
  4942. }
  4943. //---------------------------------------------------------------------------
  4944. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4945. {
  4946. FTrayIcon->Hint = GetMainForm()->Caption;
  4947. }
  4948. //---------------------------------------------------------------------------
  4949. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4950. {
  4951. UpdateTrayIcon();
  4952. }
  4953. //---------------------------------------------------------------------------
  4954. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4955. {
  4956. // workaround
  4957. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4958. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4959. // But WM_ACTIVATEAPP message can be received even when
  4960. // the main window is minimized to the tray and internal editor window is focused
  4961. // (after another application was previously active)
  4962. if (::IsIconic(Handle))
  4963. {
  4964. if (!IsAppIconic())
  4965. {
  4966. SetAppIconic(true);
  4967. }
  4968. }
  4969. ::ApplicationRestore();
  4970. Application->BringToFront();
  4971. }
  4972. //---------------------------------------------------------------------------
  4973. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4974. {
  4975. RestoreApp();
  4976. }
  4977. //---------------------------------------------------------------------------
  4978. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4979. {
  4980. if (OpenInNewWindow())
  4981. {
  4982. ExecuteNewInstance(SessionUrl);
  4983. }
  4984. else
  4985. {
  4986. TTerminalManager::Instance()->NewSession(SessionUrl);
  4987. }
  4988. }
  4989. //---------------------------------------------------------------------------
  4990. void TCustomScpExplorerForm::NewTab(TOperationSide, bool)
  4991. {
  4992. NewSession();
  4993. }
  4994. //---------------------------------------------------------------------------
  4995. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4996. {
  4997. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4998. StoredSessions->NewSession(SessionName, SessionData);
  4999. // modified only, explicit
  5000. StoredSessions->Save(false, true);
  5001. // encode session name because of slashes in hierarchical sessions
  5002. return EncodeUrlString(SessionName);
  5003. }
  5004. //---------------------------------------------------------------------------
  5005. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  5006. {
  5007. // current working directories become defaults here, what is not right
  5008. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5009. return SaveHiddenDuplicateSession(SessionData.get());
  5010. }
  5011. //---------------------------------------------------------------------------
  5012. void __fastcall TCustomScpExplorerForm::DuplicateTab()
  5013. {
  5014. if (OpenInNewWindow())
  5015. {
  5016. ExecuteNewInstance(CreateHiddenDuplicateSession());
  5017. }
  5018. else
  5019. {
  5020. // current working directories become defaults here, what is not right
  5021. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5022. TTerminalManager * Manager = TTerminalManager::Instance();
  5023. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  5024. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  5025. // We definitely want these
  5026. ASession->Disconnected = ManagedSession->Disconnected;
  5027. // Not sure about these two
  5028. ASession->Permanent = ManagedSession->Permanent;
  5029. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  5030. Manager->ActiveSession = ASession;
  5031. }
  5032. }
  5033. //---------------------------------------------------------------------------
  5034. void __fastcall TCustomScpExplorerForm::RenameTab()
  5035. {
  5036. UnicodeString Name = ManagedSession->SessionData->SessionName;
  5037. if (InputDialog(LoadStr(RENAME_TAB_TITLE), LoadStr(RENAME_TAB_PROMPT), Name, HELP_TAB_RENAME) &&
  5038. // When submitting the default name, do not fix it
  5039. (Name != ManagedSession->SessionData->SessionName))
  5040. {
  5041. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  5042. TSessionData::ValidateName(Name);
  5043. // This is inconsistent with how color (for example) is handled.
  5044. ManagedSession->SessionData->Name = Name;
  5045. UpdateControls();
  5046. // Add/Remove distinguishing paths from sessions of the same name.
  5047. SessionListChanged();
  5048. }
  5049. }
  5050. //---------------------------------------------------------------------------
  5051. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  5052. {
  5053. DebugAssert(Queue != NULL);
  5054. bool Result = Queue->IsEmpty;
  5055. if (!Result)
  5056. {
  5057. SetFocus();
  5058. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  5059. }
  5060. return Result;
  5061. }
  5062. //---------------------------------------------------------------------------
  5063. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  5064. {
  5065. return CanCloseQueue(FQueue);
  5066. }
  5067. //---------------------------------------------------------------------------
  5068. void __fastcall TCustomScpExplorerForm::CloseTab()
  5069. {
  5070. if (CanCloseQueue())
  5071. {
  5072. TTerminalManager::Instance()->FreeActiveTerminal();
  5073. }
  5074. }
  5075. //---------------------------------------------------------------------------
  5076. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  5077. {
  5078. if (CanCloseQueue())
  5079. {
  5080. TTerminalManager::Instance()->DisconnectActiveTerminal();
  5081. }
  5082. }
  5083. //---------------------------------------------------------------------------
  5084. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  5085. {
  5086. DetachTerminal(Terminal);
  5087. RemoteDirView->Terminal = Terminal;
  5088. UpdateRemotePathComboBox(false);
  5089. InitStatusBar();
  5090. UpdateControls();
  5091. }
  5092. //---------------------------------------------------------------------------
  5093. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  5094. {
  5095. InitStatusBar();
  5096. }
  5097. //---------------------------------------------------------------------------
  5098. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  5099. {
  5100. if (DebugAlwaysTrue(!Terminal->Active))
  5101. {
  5102. TTerminalManager::Instance()->ReconnectActiveTerminal();
  5103. }
  5104. }
  5105. //---------------------------------------------------------------------------
  5106. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  5107. {
  5108. if (OpenInNewWindow())
  5109. {
  5110. // encode session name because of slashes in hierarchical sessions
  5111. ExecuteNewInstance(EncodeUrlString(Data->Name));
  5112. }
  5113. else
  5114. {
  5115. TTerminalManager * Manager = TTerminalManager::Instance();
  5116. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  5117. Manager->ActiveSession = Terminal;
  5118. }
  5119. }
  5120. //---------------------------------------------------------------------------
  5121. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  5122. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  5123. {
  5124. TTerminalManager * Manager = TTerminalManager::Instance();
  5125. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5126. // This asks for a master password. Maybe we should postpone asking until a connection is actually opened.
  5127. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  5128. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  5129. {
  5130. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  5131. // FirstSession can be null, if none of the workspace sites exist anymore
  5132. // or if all workspace sessions are not suported by the interface
  5133. // (when trying to open all local-local workspace in "explorer" interface)
  5134. if (FirstSession != NULL)
  5135. {
  5136. if (!ConnectFirstTerminal)
  5137. {
  5138. FirstSession->Disconnected = true;
  5139. FirstSession->DisconnectedTemporarily = true;
  5140. }
  5141. Manager->ActiveSession = FirstSession;
  5142. }
  5143. }
  5144. }
  5145. //---------------------------------------------------------------------------
  5146. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  5147. {
  5148. if (OpenInNewWindow())
  5149. {
  5150. ExecuteNewInstance(Name);
  5151. }
  5152. else
  5153. {
  5154. DoOpenFolderOrWorkspace(Name, true, false);
  5155. }
  5156. }
  5157. //---------------------------------------------------------------------------
  5158. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  5159. bool &CanClose)
  5160. {
  5161. AppLog(L"Windows close query");
  5162. // See the comment in CloseApp()
  5163. if (NonVisualDataModule->Busy)
  5164. {
  5165. CanClose = false;
  5166. }
  5167. else
  5168. {
  5169. TTerminalManager * Manager = TTerminalManager::Instance();
  5170. int ActiveSessions = 0;
  5171. for (int Index = 0; Index < Manager->Count; Index++)
  5172. {
  5173. if (Manager->Sessions[Index]->Active)
  5174. {
  5175. ActiveSessions++;
  5176. }
  5177. }
  5178. // Confirm, when there's at least one active remote session or
  5179. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  5180. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  5181. WinConfiguration->ConfirmClosingSession)
  5182. {
  5183. unsigned int Result;
  5184. TMessageParams Params(mpNeverAskAgainCheck);
  5185. UnicodeString Message;
  5186. int Answers = qaOK | qaCancel;
  5187. // The current session is the only active session
  5188. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  5189. {
  5190. if (!WinConfiguration->AutoSaveWorkspace)
  5191. {
  5192. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  5193. }
  5194. else
  5195. {
  5196. Message = LoadStr(CLOSE_SESSION);
  5197. }
  5198. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  5199. }
  5200. // Multiple active sessions or one active session, but it's not the current one
  5201. else if (ActiveSessions > 0)
  5202. {
  5203. if (!WinConfiguration->AutoSaveWorkspace)
  5204. {
  5205. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  5206. }
  5207. else
  5208. {
  5209. Message = LoadStr(CLOSE_SESSIONS);
  5210. }
  5211. }
  5212. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  5213. else
  5214. {
  5215. DebugAssert(Manager->Count > 1);
  5216. DebugAssert(!WinConfiguration->AutoSaveWorkspace);
  5217. Message = LoadStr(CLOSE_WORKSPACE);
  5218. }
  5219. UnicodeString Note;
  5220. if (WinConfiguration->AutoSaveWorkspace)
  5221. {
  5222. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  5223. }
  5224. else
  5225. {
  5226. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  5227. Answers = qaYes | qaNo | qaCancel;
  5228. }
  5229. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  5230. SetFocus();
  5231. Result = MessageDialog(Message, qtConfirmation,
  5232. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  5233. if (Result == qaNeverAskAgain)
  5234. {
  5235. WinConfiguration->ConfirmClosingSession = false;
  5236. }
  5237. if (Result == qaNo)
  5238. {
  5239. CanClose = SaveWorkspace(true);
  5240. // note that the workspace will be saved redundatly again from FormClose
  5241. }
  5242. else
  5243. {
  5244. CanClose =
  5245. (Result == qaOK) ||
  5246. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  5247. (Result == qaNeverAskAgain);
  5248. }
  5249. }
  5250. if (CanClose && HasAvailableTerminal())
  5251. {
  5252. CanClose = CanCloseQueue();
  5253. }
  5254. }
  5255. if (CanClose)
  5256. {
  5257. CanClose =
  5258. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  5259. FEditorManager->CloseExternalFilesWithoutProcess();
  5260. if (CanClose)
  5261. {
  5262. while (CanClose && (FLocalEditors->Count > 0))
  5263. {
  5264. int PrevCount = FLocalEditors->Count;
  5265. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  5266. CanClose = (FLocalEditors->Count < PrevCount);
  5267. }
  5268. if (CanClose)
  5269. {
  5270. CanClose = FEditorManager->Empty(true);
  5271. if (!CanClose)
  5272. {
  5273. SetFocus();
  5274. CanClose =
  5275. (MessageDialog(
  5276. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  5277. }
  5278. }
  5279. }
  5280. }
  5281. }
  5282. //---------------------------------------------------------------------------
  5283. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  5284. {
  5285. TForm * Form = dynamic_cast<TForm *>(Sender);
  5286. DebugAssert(Form != NULL);
  5287. Form->Close();
  5288. }
  5289. //---------------------------------------------------------------------------
  5290. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  5291. {
  5292. TForm * Form = dynamic_cast<TForm *>(Sender);
  5293. delete Form;
  5294. }
  5295. //---------------------------------------------------------------------------
  5296. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  5297. {
  5298. while (FLocalEditors->Count > 0)
  5299. {
  5300. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  5301. }
  5302. }
  5303. //---------------------------------------------------------------------------
  5304. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  5305. TObject *Sender)
  5306. {
  5307. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  5308. try
  5309. {
  5310. SetProperties(osRemote, FileList);
  5311. }
  5312. __finally
  5313. {
  5314. delete FileList;
  5315. }
  5316. }
  5317. //---------------------------------------------------------------------------
  5318. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  5319. {
  5320. if (value)
  5321. {
  5322. if (Component == fcCustomCommandsBand)
  5323. {
  5324. UpdateCustomCommandsToolbar();
  5325. }
  5326. if (Component == fcQueueFileList)
  5327. {
  5328. DebugAssert(!QueueFileListSplitter->Visible);
  5329. AdjustQueueLayout();
  5330. UpdateQueueFileList();
  5331. }
  5332. }
  5333. }
  5334. //---------------------------------------------------------------------------
  5335. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  5336. {
  5337. int MinHeightLimit =
  5338. GetStaticQueuePanelComponentsHeight() +
  5339. QueueFileListSplitter->Height +
  5340. QueueFileList->Height +
  5341. GetMinQueueViewHeight();
  5342. if (QueuePanel->ClientHeight < MinHeightLimit)
  5343. {
  5344. QueuePanel->ClientHeight = MinHeightLimit;
  5345. }
  5346. }
  5347. //---------------------------------------------------------------------------
  5348. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  5349. {
  5350. TControl * Control = GetComponent(Component);
  5351. DebugAssert(Control);
  5352. bool Changed = (Control->Visible != value);
  5353. if (Changed)
  5354. {
  5355. ComponentShowing(Component, value);
  5356. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  5357. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  5358. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  5359. if (value)
  5360. {
  5361. int RemainingHeight = Control->Parent->ClientHeight;
  5362. int RemainingWidth = Control->Parent->ClientWidth;
  5363. for (int i = 0; i < Control->Parent->ControlCount; i++)
  5364. {
  5365. TControl * ChildControl = Control->Parent->Controls[i];
  5366. if (ChildControl->Visible)
  5367. {
  5368. switch (ChildControl->Align)
  5369. {
  5370. case alTop:
  5371. case alBottom:
  5372. RemainingHeight -= ChildControl->Height;
  5373. break;
  5374. case alLeft:
  5375. case alRight:
  5376. RemainingWidth -= ChildControl->Width;
  5377. break;
  5378. }
  5379. }
  5380. }
  5381. int Reserve = ScaleByTextHeight(this, 32);
  5382. // queue in explorer, trees in commander
  5383. if (Control->Height > RemainingHeight - Reserve)
  5384. {
  5385. Control->Height = RemainingHeight / 2;
  5386. }
  5387. if (Control->Width > RemainingWidth - Reserve)
  5388. {
  5389. Control->Width = RemainingWidth / 2;
  5390. }
  5391. }
  5392. Control->Visible = value;
  5393. if (WasFocused && Visible)
  5394. {
  5395. DirView(osCurrent)->SetFocus();
  5396. }
  5397. FixControlsPlacement();
  5398. }
  5399. }
  5400. //---------------------------------------------------------------------------
  5401. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  5402. {
  5403. TControl * Control = GetComponent(Component);
  5404. if (Control == NULL)
  5405. {
  5406. return false;
  5407. }
  5408. else
  5409. {
  5410. return Control->Visible;
  5411. }
  5412. }
  5413. //---------------------------------------------------------------------------
  5414. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  5415. {
  5416. bool Result;
  5417. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  5418. {
  5419. Result = !IsUWP();
  5420. }
  5421. else
  5422. {
  5423. Result = true;
  5424. }
  5425. return Result;
  5426. }
  5427. //---------------------------------------------------------------------------
  5428. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  5429. {
  5430. if (DirView->ItemFocused != NULL)
  5431. {
  5432. DirView->ItemFocused->MakeVisible(false);
  5433. }
  5434. }
  5435. //---------------------------------------------------------------------------
  5436. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5437. {
  5438. MakeFocusedItemVisible(RemoteDirView);
  5439. QueueSplitter->Visible = QueuePanel->Visible;
  5440. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5441. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5442. TControl * TopControlsOrder[] =
  5443. { TopDock, MessageDock, SessionsPageControl };
  5444. SetVerticalControlsOrder(TopControlsOrder, LENOF(TopControlsOrder));
  5445. TControl * QueueControlsOrder[] =
  5446. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5447. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5448. }
  5449. //---------------------------------------------------------------------------
  5450. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5451. {
  5452. switch (Component) {
  5453. case fcStatusBar: return RemoteStatusBar;
  5454. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5455. case fcQueueView: return QueuePanel;
  5456. case fcQueueToolbar: return QueueDock;
  5457. case fcRemoteTree: return RemoteDrivePanel;
  5458. case fcSessionsTabs: return SessionsPageControl;
  5459. case fcQueueFileList: return QueueFileList;
  5460. default: return NULL;
  5461. }
  5462. }
  5463. //---------------------------------------------------------------------------
  5464. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5465. TObject *Sender, TListColumn *Column, TPoint &Point)
  5466. {
  5467. DebugAssert(NonVisualDataModule && Column && Sender);
  5468. NonVisualDataModule->ListColumn = Column;
  5469. TPopupMenu * DirViewColumnMenu;
  5470. if (Sender == DirView(osOther))
  5471. {
  5472. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5473. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible = (Column->Index == uvName);
  5474. bool SizeColumn = (Column->Index == uvSize);
  5475. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible = SizeColumn;
  5476. NonVisualDataModule->RemoteCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5477. }
  5478. else
  5479. {
  5480. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5481. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible = (Column->Index == dvName);
  5482. bool SizeColumn = (Column->Index == dvSize);
  5483. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible = SizeColumn;
  5484. NonVisualDataModule->LocalCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5485. }
  5486. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5487. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5488. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5489. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5490. }
  5491. //---------------------------------------------------------------------------
  5492. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5493. TObject *Sender, TListItem *Item, bool &AllowExec)
  5494. {
  5495. DoDirViewExecFile(Sender, Item, AllowExec);
  5496. }
  5497. //---------------------------------------------------------------------------
  5498. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5499. TListItem * Item, bool & AllowExec)
  5500. {
  5501. DebugAssert(Sender && Item && Configuration);
  5502. DebugAssert(AllowExec);
  5503. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5504. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5505. bool Remote = !IsSideLocalBrowser(Side);
  5506. DebugAssert(Item == ADirView->ItemFocused);
  5507. bool IsDirectory = ADirView->ItemIsDirectory(Item);
  5508. TResolvedDoubleClickAction Action;
  5509. if (FForceExecution)
  5510. {
  5511. Action = IsDirectory ? rdcaChangeDir : rdcaOpen;
  5512. }
  5513. else
  5514. {
  5515. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  5516. Action = WinConfiguration->ResolveDoubleClickAction(IsDirectory, ATerminal);
  5517. }
  5518. if (Action == rdcaCopy)
  5519. {
  5520. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5521. UnlockWindow();
  5522. try
  5523. {
  5524. if (IsLocalBrowserMode())
  5525. {
  5526. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5527. }
  5528. else
  5529. {
  5530. if (HasAvailableTerminal())
  5531. {
  5532. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5533. }
  5534. }
  5535. }
  5536. __finally
  5537. {
  5538. LockWindow();
  5539. }
  5540. AllowExec = false;
  5541. }
  5542. else if (Action == rdcaEdit)
  5543. {
  5544. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5545. {
  5546. ExecuteFile(Side, efDefaultEditor);
  5547. AllowExec = false;
  5548. }
  5549. }
  5550. else if (Action == rdcaOpen)
  5551. {
  5552. if (DebugAlwaysTrue(!IsDirectory) && Remote && !WinConfiguration->DisableOpenEdit)
  5553. {
  5554. ExecuteFile(osRemote, efShell);
  5555. AllowExec = false;
  5556. }
  5557. }
  5558. }
  5559. //---------------------------------------------------------------------------
  5560. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5561. {
  5562. return ((Side == osRemote) || (Side == osCurrent));
  5563. }
  5564. //---------------------------------------------------------------------------
  5565. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5566. {
  5567. DebugFail();
  5568. }
  5569. //---------------------------------------------------------------------------
  5570. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5571. {
  5572. DebugFail();
  5573. }
  5574. //---------------------------------------------------------------------------
  5575. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5576. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5577. {
  5578. TSynchronizeParamType Params;
  5579. Params.LocalDirectory = LocalDirectory;
  5580. Params.RemoteDirectory = RemoteDirectory;
  5581. int UnusedParams =
  5582. (GUIConfiguration->SynchronizeParams &
  5583. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5584. TTerminal::spCaseSensitive | TTerminal::spMirror | TTerminal::spByChecksum));
  5585. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5586. Params.Options = GUIConfiguration->SynchronizeOptions;
  5587. bool SaveSettings = false;
  5588. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5589. &DoSynchronizeTooManyDirectories);
  5590. DebugAssert(FSynchronizeController == NULL);
  5591. FSynchronizeController = &Controller;
  5592. bool Result;
  5593. try
  5594. {
  5595. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5596. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5597. int Options =
  5598. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5599. DebugAssert(FOnFeedSynchronizeError == NULL);
  5600. DebugAssert(FOnSynchronizeAbort == NULL);
  5601. DebugAssert(FSynchronizeTerminal == NULL);
  5602. FSynchronizeTerminal = Terminal;
  5603. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5604. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5605. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5606. if (Result)
  5607. {
  5608. if (SaveSettings)
  5609. {
  5610. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5611. GUIConfiguration->SynchronizeOptions = Params.Options;
  5612. }
  5613. else
  5614. {
  5615. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5616. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5617. FLAGSET(Params.Options, soSynchronize))
  5618. {
  5619. GUIConfiguration->SynchronizeOptions =
  5620. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5621. soSynchronize;
  5622. }
  5623. }
  5624. LocalDirectory = Params.LocalDirectory;
  5625. RemoteDirectory = Params.RemoteDirectory;
  5626. }
  5627. }
  5628. __finally
  5629. {
  5630. FSynchronizeController = NULL;
  5631. DebugAssert(FOnFeedSynchronizeError == NULL);
  5632. FOnFeedSynchronizeError = NULL;
  5633. DebugAssert(FOnSynchronizeAbort == NULL);
  5634. FOnSynchronizeAbort = NULL;
  5635. FSynchronizeTerminal = NULL;
  5636. }
  5637. return Result;
  5638. }
  5639. //---------------------------------------------------------------------------
  5640. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5641. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5642. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5643. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5644. TSynchronizeOptions * Options, bool Full)
  5645. {
  5646. // Keep-up-to-date mode
  5647. if (Terminal->Status == ssOpened)
  5648. {
  5649. try
  5650. {
  5651. int PParams = Params.Params;
  5652. if (!Full)
  5653. {
  5654. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5655. if (FLAGSET(Params.Options, soRecurse))
  5656. {
  5657. PParams |= TTerminal::spSubDirs;
  5658. }
  5659. }
  5660. else
  5661. {
  5662. // if keepuptodate is non-recursive,
  5663. // full sync before has to be non-recursive as well
  5664. if (FLAGCLEAR(Params.Options, soRecurse))
  5665. {
  5666. PParams |= TTerminal::spNoRecurse;
  5667. }
  5668. }
  5669. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5670. PParams, Checklist, Options);
  5671. }
  5672. catch (Exception & E)
  5673. {
  5674. if (!Terminal->Active)
  5675. {
  5676. ShowExtendedExceptionEx(Terminal, &E);
  5677. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5678. if (!Terminal->Active)
  5679. {
  5680. throw;
  5681. }
  5682. }
  5683. else
  5684. {
  5685. if (FLAGSET(Params.Options, soContinueOnError) &&
  5686. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5687. {
  5688. // noop, already logged in MoreMessageDialog
  5689. }
  5690. else
  5691. {
  5692. // We get mostly EAbort here, so this is noop.
  5693. // Exception is an error when listing directories while looking for differences.
  5694. ShowExtendedExceptionEx(Terminal, &E);
  5695. throw;
  5696. }
  5697. }
  5698. }
  5699. }
  5700. }
  5701. //---------------------------------------------------------------------------
  5702. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5703. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5704. const UnicodeString ErrorStr)
  5705. {
  5706. if (!Directory.IsEmpty())
  5707. {
  5708. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5709. }
  5710. else
  5711. {
  5712. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5713. }
  5714. }
  5715. //---------------------------------------------------------------------------
  5716. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5717. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5718. {
  5719. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5720. {
  5721. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5722. }
  5723. else
  5724. {
  5725. TMessageParams Params(mpNeverAskAgainCheck);
  5726. unsigned int Result = MessageDialog(
  5727. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5728. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5729. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5730. {
  5731. MaxDirectories *= 2;
  5732. if (Result == qaNeverAskAgain)
  5733. {
  5734. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5735. }
  5736. }
  5737. else
  5738. {
  5739. Abort();
  5740. }
  5741. }
  5742. }
  5743. //---------------------------------------------------------------------------
  5744. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5745. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5746. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5747. TSynchronizeOptions * Options)
  5748. {
  5749. // Keep-up-to-date mode
  5750. DebugAssert(!FAutoOperation);
  5751. void * BatchStorage;
  5752. BatchStart(BatchStorage);
  5753. FAutoOperation = true;
  5754. bool AnyOperation = false;
  5755. TDateTime StartTime = Now();
  5756. TSynchronizeChecklist * AChecklist = NULL;
  5757. TObjectReleaser<TSynchronizeProgressForm> SynchronizeProgressFormReleaser(FSynchronizeProgressForm, new TSynchronizeProgressForm(Application, true, -1));
  5758. try
  5759. {
  5760. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5761. {
  5762. FSynchronizeProgressForm->Start();
  5763. }
  5764. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5765. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5766. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5767. Options);
  5768. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5769. SAFE_DESTROY(FSynchronizeProgressForm);
  5770. AnyOperation = (AChecklist->CheckedCount > 0);
  5771. if (AnyOperation)
  5772. {
  5773. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5774. CreateProgressForm(&SynchronizeProgress);
  5775. try
  5776. {
  5777. Terminal->SynchronizeApply(
  5778. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5779. SynchronizeProcessedItem, NULL, NULL, NULL);
  5780. }
  5781. __finally
  5782. {
  5783. DestroyProgressForm();
  5784. }
  5785. }
  5786. }
  5787. __finally
  5788. {
  5789. if (Checklist == NULL)
  5790. {
  5791. delete AChecklist;
  5792. }
  5793. else
  5794. {
  5795. *Checklist = AChecklist;
  5796. }
  5797. FAutoOperation = false;
  5798. SynchronizeProgressFormReleaser.Reset();
  5799. BatchEnd(BatchStorage);
  5800. ReloadLocalDirectory();
  5801. if (AnyOperation)
  5802. {
  5803. OperationComplete(StartTime);
  5804. }
  5805. }
  5806. }
  5807. //---------------------------------------------------------------------------
  5808. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5809. {
  5810. // can be called from command line
  5811. return Visible &&
  5812. ((DirView(osRemote)->SelCount > 0) ||
  5813. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5814. }
  5815. //---------------------------------------------------------------------------
  5816. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5817. {
  5818. LogSynchronizeEvent(Terminal, Message);
  5819. }
  5820. //---------------------------------------------------------------------------
  5821. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5822. int Params, TSynchronizeOptions & Options)
  5823. {
  5824. DebugAssert(!IsLocalBrowserMode());
  5825. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5826. {
  5827. Options.Filter = new TStringList();
  5828. Options.Filter->CaseSensitive = false;
  5829. Options.Filter->Duplicates = Types::dupAccept;
  5830. if (DirView(osRemote)->SelCount > 0)
  5831. {
  5832. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5833. }
  5834. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5835. {
  5836. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5837. }
  5838. Options.Filter->Sort();
  5839. }
  5840. }
  5841. //---------------------------------------------------------------------------
  5842. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5843. {
  5844. TCopyParamType Defaults;
  5845. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5846. ConfigStorage->AccessMode = smRead;
  5847. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5848. {
  5849. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5850. }
  5851. ConfigStorage.reset(NULL);
  5852. std::unique_ptr<TStringList> Options(new TStringList());
  5853. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5854. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5855. UnicodeString Result;
  5856. if (Options->Count > 0)
  5857. {
  5858. Result =
  5859. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5860. }
  5861. return Result;
  5862. }
  5863. //---------------------------------------------------------------------------
  5864. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5865. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5866. {
  5867. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5868. UnicodeString AdditionalParams =
  5869. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5870. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5871. SerializeCopyParamForCommandLine(CopyParams),
  5872. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5873. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5874. ExecuteNewInstance(SessionName, AdditionalParams);
  5875. }
  5876. //---------------------------------------------------------------------------
  5877. struct TSynchronizeParams
  5878. {
  5879. UnicodeString LocalDirectory;
  5880. UnicodeString RemoteDirectory;
  5881. TSynchronizeMode Mode;
  5882. TSynchronizeChecklist * Checklist;
  5883. int Params;
  5884. TCopyParamType * CopyParam;
  5885. TDateTime CollectElapsed;
  5886. TDateTime * StartTime;
  5887. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5888. };
  5889. //---------------------------------------------------------------------------
  5890. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5891. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5892. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5893. {
  5894. DebugAssert(!FAutoOperation);
  5895. void * BatchStorage;
  5896. BatchStart(BatchStorage);
  5897. FAutoOperation = true;
  5898. TFileOperationStatistics Statistics;
  5899. TDateTime Start = Now();
  5900. try
  5901. {
  5902. Params.OnProcessedItem = OnProcessedItem;
  5903. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5904. CreateProgressForm(&SynchronizeProgress);
  5905. Terminal->SynchronizeApply(
  5906. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5907. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5908. &Statistics);
  5909. }
  5910. __finally
  5911. {
  5912. Params.OnProcessedItem = NULL;
  5913. FAutoOperation = false;
  5914. DestroyProgressForm();
  5915. BatchEnd(BatchStorage);
  5916. ReloadLocalDirectory();
  5917. }
  5918. if (WinConfiguration->SynchronizeSummary)
  5919. {
  5920. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5921. // The statistics should be 0 anyway in this case
  5922. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5923. {
  5924. Message += L"\n";
  5925. if (Statistics.FilesUploaded > 0)
  5926. {
  5927. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5928. }
  5929. if (Statistics.FilesDownloaded > 0)
  5930. {
  5931. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5932. }
  5933. if (Statistics.FilesDeletedLocal > 0)
  5934. {
  5935. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5936. }
  5937. if (Statistics.FilesDeletedRemote > 0)
  5938. {
  5939. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5940. }
  5941. }
  5942. TDateTime Elapsed = (Now() - Start);
  5943. Message +=
  5944. L"\n" +
  5945. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Params.CollectElapsed))) + L"\n" +
  5946. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Elapsed)));
  5947. TMessageParams Params(mpNeverAskAgainCheck);
  5948. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5949. if (Result == qaNeverAskAgain)
  5950. {
  5951. WinConfiguration->SynchronizeSummary = false;
  5952. }
  5953. }
  5954. }
  5955. //---------------------------------------------------------------------------
  5956. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5957. {
  5958. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5959. {
  5960. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5961. }
  5962. // Not set in keep-up-to-date mode - Synchronize() method
  5963. if (Token != NULL)
  5964. {
  5965. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5966. if (Params.OnProcessedItem != NULL)
  5967. {
  5968. Params.OnProcessedItem(NULL, ChecklistItem);
  5969. }
  5970. }
  5971. }
  5972. //---------------------------------------------------------------------------
  5973. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5974. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5975. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5976. {
  5977. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5978. *Params.StartTime = Now();
  5979. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5980. }
  5981. //---------------------------------------------------------------------------
  5982. void TCustomScpExplorerForm::DoQueueSynchronize(void * Token)
  5983. {
  5984. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5985. int CopyParams = TTerminal::GetSynchronizeCopyParams(Params.Params);
  5986. TCopyParamType SyncCopyParam = TTerminal::GetSynchronizeCopyParam(Params.CopyParam, Params.Params);
  5987. bool Parallel = GUIConfiguration->CurrentCopyParam.QueueParallel;
  5988. int Index = 0;
  5989. const TSynchronizeChecklist::TItem * ChecklistItem;
  5990. while (Params.Checklist->GetNextChecked(Index, ChecklistItem))
  5991. {
  5992. TQueueItem * QueueItem = Terminal->SynchronizeToQueue(ChecklistItem, &SyncCopyParam, CopyParams, Parallel);
  5993. AddQueueItem(Queue, QueueItem, Terminal);
  5994. }
  5995. }
  5996. //---------------------------------------------------------------------------
  5997. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5998. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5999. {
  6000. // terminal can be already closed (e.g. dropped connection)
  6001. if (Terminal != NULL)
  6002. {
  6003. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  6004. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  6005. }
  6006. }
  6007. //---------------------------------------------------------------------------
  6008. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  6009. TOperationSide Side, TStrings * FileList, const UnicodeString & NewFileName, bool TargetIsDirectory, void * Token)
  6010. {
  6011. DebugAssert(!IsLocalBrowserMode());
  6012. TAutoBatch AutoBatch(this);
  6013. TAutoFlag AutoOperationFlag(FAutoOperation);
  6014. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  6015. if (Side == osRemote)
  6016. {
  6017. UnicodeString Target;
  6018. UnicodeString FileMask;
  6019. if (TargetIsDirectory)
  6020. {
  6021. Target = NewFileName;
  6022. Terminal->CreateTargetDirectory(Target, faDirectory, Params.CopyParam);
  6023. }
  6024. else
  6025. {
  6026. Target = UnixExtractFileDir(NewFileName);
  6027. FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  6028. }
  6029. RemoteDirView->SaveSelection();
  6030. RemoteDirView->SaveSelectedNames();
  6031. try
  6032. {
  6033. Terminal->MoveFiles(FileList, Target, FileMask, false);
  6034. }
  6035. catch(...)
  6036. {
  6037. RemoteDirView->DiscardSavedSelection();
  6038. throw;
  6039. }
  6040. RemoteDirView->RestoreSelection();
  6041. }
  6042. else if (DebugAlwaysTrue(Side == osLocal))
  6043. {
  6044. std::unique_ptr<TStrings> Directories(CreateSortedStringList());
  6045. if (TargetIsDirectory)
  6046. {
  6047. if (!ForceDirectories(ApiPath(NewFileName)))
  6048. {
  6049. throw EOSExtException(FMTLOAD(CREATE_DIR_ERROR, (NewFileName)));
  6050. }
  6051. Directories->Add(NewFileName);
  6052. }
  6053. else
  6054. {
  6055. Directories->Add(ExtractFileDir(NewFileName));
  6056. }
  6057. for (int Index = 0; Index < FileList->Count; Index++)
  6058. {
  6059. UnicodeString FileName = FileList->Strings[Index];
  6060. UnicodeString Directory = ExtractFileDir(FileName);
  6061. Directories->Add(Directory);
  6062. UnicodeString NewFilePath = NewFileName;
  6063. if (TargetIsDirectory)
  6064. {
  6065. NewFilePath = TPath::Combine(NewFilePath, ExtractFileName(FileName));
  6066. }
  6067. if (!MoveFile(ApiPath(FileName).c_str(), ApiPath(NewFilePath).c_str()))
  6068. {
  6069. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFilePath)));
  6070. }
  6071. }
  6072. for (int Index = 0; Index < Directories->Count; Index++)
  6073. {
  6074. ReloadLocalDirectory(Directories->Strings[Index]);
  6075. }
  6076. }
  6077. }
  6078. //---------------------------------------------------------------------------
  6079. void __fastcall TCustomScpExplorerForm::DoSynchronizeExplore(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  6080. {
  6081. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  6082. if (Side == osLocal)
  6083. {
  6084. if (Action == TSynchronizeChecklist::saDownloadNew)
  6085. {
  6086. OpenFolderInExplorer(LocalPath);
  6087. }
  6088. else
  6089. {
  6090. OpenFileInExplorer(Item->ForceGetLocalPath());
  6091. }
  6092. }
  6093. else if (DebugAlwaysTrue(Side == osRemote))
  6094. {
  6095. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  6096. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  6097. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  6098. if (!LocalPath.IsEmpty())
  6099. {
  6100. SessionData->LocalDirectory = LocalPath;
  6101. }
  6102. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  6103. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(EXPLORE_SWITCH), Item->GetFileName())));
  6104. }
  6105. }
  6106. //---------------------------------------------------------------------------
  6107. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  6108. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  6109. const TCopyParamType * CopyParams)
  6110. {
  6111. UnicodeString SessionName = CreateHiddenDuplicateSession();
  6112. UnicodeString AdditionalParams =
  6113. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  6114. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  6115. SerializeCopyParamForCommandLine(CopyParams),
  6116. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  6117. LocalDirectory, RemoteDirectory, Mode, Params));
  6118. ExecuteNewInstance(SessionName, AdditionalParams);
  6119. }
  6120. //---------------------------------------------------------------------------
  6121. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  6122. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  6123. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  6124. {
  6125. int Result;
  6126. bool SaveSettings = false;
  6127. int Options =
  6128. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  6129. FLAGMASK(!CanCalculateChecksum(), fsoDisableByChecksum) |
  6130. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  6131. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  6132. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  6133. bool Continue =
  6134. ((UseDefaults == 0) ||
  6135. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  6136. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults)) &&
  6137. (FLAGCLEAR(Params, TTerminal::spByChecksum) || EnsureCommandSessionFallback(fcCalculatingChecksum));
  6138. if (Continue)
  6139. {
  6140. Configuration->Usage->Inc(L"Synchronizations");
  6141. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  6142. UpdateCopyParamCounters(CopyParam);
  6143. TSynchronizeOptions SynchronizeOptions;
  6144. GetSynchronizeOptions(Params, SynchronizeOptions);
  6145. if (SaveSettings)
  6146. {
  6147. GUIConfiguration->SynchronizeParams = Params;
  6148. }
  6149. else
  6150. {
  6151. SaveMode = false;
  6152. }
  6153. TDateTime StartTime = Now();
  6154. TSynchronizeChecklist * Checklist = NULL;
  6155. try
  6156. {
  6157. DebugAssert(!FAutoOperation);
  6158. FAutoOperation = true;
  6159. try
  6160. {
  6161. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  6162. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  6163. int Files = -1;
  6164. if (DataList->Count >= 1)
  6165. {
  6166. Files = StrToIntDef(DataList->Strings[0], Files);
  6167. }
  6168. else
  6169. {
  6170. DataList->Add(UnicodeString());
  6171. }
  6172. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  6173. FSynchronizeProgressForm->Start();
  6174. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  6175. static_cast<TTerminal::TSynchronizeMode>(Mode),
  6176. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  6177. &SynchronizeOptions);
  6178. if (Terminal->SessionData->CacheDirectories)
  6179. {
  6180. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  6181. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  6182. }
  6183. }
  6184. __finally
  6185. {
  6186. FAutoOperation = false;
  6187. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  6188. SAFE_DESTROY(FSynchronizeProgressForm);
  6189. }
  6190. TSynchronizeParams SynchronizeParams;
  6191. SynchronizeParams.LocalDirectory = LocalDirectory;
  6192. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  6193. SynchronizeParams.Mode = Mode;
  6194. SynchronizeParams.CopyParam = &CopyParam;
  6195. SynchronizeParams.Params = Params;
  6196. SynchronizeParams.Checklist = Checklist;
  6197. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  6198. SynchronizeParams.StartTime = &StartTime;
  6199. SynchronizeParams.OnProcessedItem = NULL;
  6200. Result = Checklist->Count;
  6201. if (Checklist->Count > 0)
  6202. {
  6203. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  6204. {
  6205. TQueueSynchronizeEvent OnQueueSynchronize = NULL;
  6206. if (Visible && FLAGCLEAR(Params, TTerminal::spTimestamp))
  6207. {
  6208. OnQueueSynchronize = DoQueueSynchronize;
  6209. }
  6210. if (!DoSynchronizeChecklistDialog(
  6211. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  6212. OnQueueSynchronize, DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeExplore,
  6213. &SynchronizeParams))
  6214. {
  6215. Result = -1;
  6216. }
  6217. }
  6218. else
  6219. {
  6220. FullSynchronize(SynchronizeParams, NULL, NULL);
  6221. }
  6222. }
  6223. else
  6224. {
  6225. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  6226. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  6227. }
  6228. }
  6229. __finally
  6230. {
  6231. delete Checklist;
  6232. }
  6233. OperationComplete(StartTime);
  6234. }
  6235. else
  6236. {
  6237. Result = -1;
  6238. }
  6239. return Result;
  6240. }
  6241. //---------------------------------------------------------------------------
  6242. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  6243. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  6244. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  6245. {
  6246. if (Collect)
  6247. {
  6248. DebugAssert(FSynchronizeProgressForm != NULL);
  6249. if (!FSynchronizeProgressForm->Started)
  6250. {
  6251. FSynchronizeProgressForm->Start();
  6252. }
  6253. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  6254. SetTaskbarListProgressValue(CompareProgress);
  6255. }
  6256. else
  6257. {
  6258. DebugAssert(FSynchronizeProgressForm == NULL);
  6259. DebugAssert(FProgressForm != NULL);
  6260. }
  6261. }
  6262. //---------------------------------------------------------------------------
  6263. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  6264. {
  6265. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  6266. std::unique_ptr<TRemoteFile> File(Terminal->ReadFile(FullFileName));
  6267. if (File.get() != NULL)
  6268. {
  6269. ExecuteRemoteFile(FullFileName, File.get(), efInternalEditor);
  6270. Application->ShowMainForm = false;
  6271. Application->Run();
  6272. }
  6273. }
  6274. //---------------------------------------------------------------------------
  6275. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  6276. {
  6277. DebugFail();
  6278. }
  6279. //---------------------------------------------------------------------------
  6280. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  6281. {
  6282. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  6283. SessionData->Assign(ManagedSession->SessionData);
  6284. UpdateSessionData(SessionData.get());
  6285. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  6286. if (IsAvailableTerminal(ManagedSession))
  6287. {
  6288. ManagedSession->UpdateSessionCredentials(SessionData.get());
  6289. }
  6290. if (!ManagedSession->SessionData->HasSessionName())
  6291. {
  6292. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  6293. // once IsWorkspace is cleared
  6294. SessionData->Name = UnicodeString();
  6295. }
  6296. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  6297. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  6298. SessionData->IsWorkspace = false;
  6299. return SessionData.release();
  6300. }
  6301. //---------------------------------------------------------------------------
  6302. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  6303. {
  6304. TSessionData * SessionData = CloneCurrentSessionData();
  6305. try
  6306. {
  6307. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  6308. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  6309. }
  6310. __finally
  6311. {
  6312. delete SessionData;
  6313. }
  6314. }
  6315. //---------------------------------------------------------------------------
  6316. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  6317. {
  6318. TTerminalManager * Manager = TTerminalManager::Instance();
  6319. std::unique_ptr<TObjectList> DataList(new TObjectList());
  6320. if (DebugAlwaysTrue(ManagedSession != NULL))
  6321. {
  6322. // Update ManagedSession->StateData
  6323. UpdateSession(ManagedSession);
  6324. }
  6325. Manager->SaveWorkspace(DataList.get());
  6326. return DataList.release();
  6327. }
  6328. //---------------------------------------------------------------------------
  6329. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  6330. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  6331. {
  6332. WinConfiguration->LastWorkspace = Name;
  6333. if (!SavePasswords)
  6334. {
  6335. for (int Index = 0; Index < DataList->Count; Index++)
  6336. {
  6337. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6338. if (SessionData->Link.IsEmpty())
  6339. {
  6340. SessionData->ClearSessionPasswords();
  6341. }
  6342. }
  6343. }
  6344. StoredSessions->NewWorkspace(Name, DataList);
  6345. // modified only
  6346. StoredSessions->Save(false, Explicit);
  6347. }
  6348. //---------------------------------------------------------------------------
  6349. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  6350. {
  6351. return
  6352. DefaultStr(WinConfiguration->LastWorkspace,
  6353. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  6354. }
  6355. //---------------------------------------------------------------------------
  6356. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  6357. {
  6358. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  6359. bool AnyNonStoredSessionWithPassword = false;
  6360. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  6361. bool AllNonStoredSessionsAnonymous = true;
  6362. TTerminalManager * Manager = TTerminalManager::Instance();
  6363. for (int Index = 0; Index < DataList->Count; Index++)
  6364. {
  6365. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6366. if (SessionData->Link.IsEmpty())
  6367. {
  6368. if (SessionData->HasAnySessionPassword())
  6369. {
  6370. AnyNonStoredSessionWithPassword = true;
  6371. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  6372. {
  6373. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  6374. }
  6375. }
  6376. if (!SameText(SessionData->UserName, AnonymousUserName))
  6377. {
  6378. AllNonStoredSessionsAnonymous = false;
  6379. }
  6380. }
  6381. }
  6382. bool SavePasswords;
  6383. bool * PSavePasswords;
  6384. bool NotRecommendedSavingPasswords =
  6385. !CustomWinConfiguration->UseMasterPassword &&
  6386. !AllNonStoredSessionsAnonymous;
  6387. if (Configuration->DisablePasswordStoring ||
  6388. !AnyNonStoredSessionWithPassword)
  6389. {
  6390. PSavePasswords = NULL;
  6391. SavePasswords = false;
  6392. }
  6393. else
  6394. {
  6395. PSavePasswords = &SavePasswords;
  6396. SavePasswords =
  6397. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  6398. !NotRecommendedSavingPasswords;
  6399. }
  6400. UnicodeString Name = WorkspaceName();
  6401. bool CreateShortcut = false;
  6402. bool Result =
  6403. DoSaveWorkspaceDialog(
  6404. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  6405. EnableAutoSave);
  6406. if (Result)
  6407. {
  6408. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  6409. if (CreateShortcut)
  6410. {
  6411. TOperationVisualizer Visualizer;
  6412. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  6413. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  6414. }
  6415. if (EnableAutoSave)
  6416. {
  6417. WinConfiguration->AutoSaveWorkspace = true;
  6418. WinConfiguration->AutoWorkspace = Name;
  6419. if (PSavePasswords != NULL)
  6420. {
  6421. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  6422. }
  6423. }
  6424. }
  6425. return Result;
  6426. }
  6427. //---------------------------------------------------------------------------
  6428. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  6429. {
  6430. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  6431. DebugAssert(Session == ManagedSession);
  6432. if (!IsLocalBrowserMode())
  6433. {
  6434. SAFE_DESTROY(Session->RemoteExplorerState);
  6435. if (WinConfiguration->PreservePanelState)
  6436. {
  6437. Session->RemoteExplorerState = RemoteDirView->SaveState();
  6438. }
  6439. }
  6440. UpdateSessionData(Session->StateData);
  6441. }
  6442. //---------------------------------------------------------------------------
  6443. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  6444. {
  6445. // Keep in sync with TSessionData::CopyStateData
  6446. DebugAssert(Data != NULL);
  6447. // This is inconsistent with how color (for example) is handled.
  6448. Data->Name = ManagedSession->SessionData->Name;
  6449. if (!IsLocalBrowserMode())
  6450. {
  6451. // cannot use RemoteDirView->Path, because it is empty if connection
  6452. // was already closed
  6453. // also only peek, we may not be connected at all atm
  6454. // so make sure we do not try retrieving current directory from the server
  6455. // (particularly with FTP)
  6456. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  6457. if (!ACurrentDirectory.IsEmpty())
  6458. {
  6459. Data->RemoteDirectory = ACurrentDirectory;
  6460. }
  6461. }
  6462. Data->Color = SessionColor;
  6463. }
  6464. //---------------------------------------------------------------------------
  6465. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  6466. {
  6467. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  6468. DebugAssert(Toolbar != NULL);
  6469. for (int i = 0; i < Toolbar->Items->Count; i++)
  6470. {
  6471. TTBXCustomDropDownItem * DropDownItem;
  6472. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6473. if (DropDownItem != NULL)
  6474. {
  6475. ToolbarItemResize(DropDownItem,
  6476. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  6477. Toolbar->NonClientWidth);
  6478. break;
  6479. }
  6480. }
  6481. }
  6482. //---------------------------------------------------------------------------
  6483. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6484. {
  6485. Item->EditWidth = Width;
  6486. }
  6487. //---------------------------------------------------------------------------
  6488. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6489. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6490. {
  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. ASize.x -= DropDownItem->EditWidth;
  6498. TControl * Control = this;
  6499. ASize.x += ScaleByTextHeight(Control, 50) /* minimal combo width */;
  6500. }
  6501. }
  6502. }
  6503. //---------------------------------------------------------------------------
  6504. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6505. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6506. {
  6507. if (WinConfiguration->DDWarnLackOfTempSpace)
  6508. {
  6509. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6510. if (!ADrive.IsEmpty())
  6511. {
  6512. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6513. DebugAssert(RequiredSpace >= 0);
  6514. __int64 RequiredWithReserve;
  6515. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6516. if (FreeSpace < RequiredWithReserve)
  6517. {
  6518. unsigned int Result;
  6519. TMessageParams Params(mpNeverAskAgainCheck);
  6520. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6521. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6522. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6523. if (Result == qaNeverAskAgain)
  6524. {
  6525. WinConfiguration->DDWarnLackOfTempSpace = false;
  6526. }
  6527. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6528. }
  6529. }
  6530. }
  6531. }
  6532. //---------------------------------------------------------------------------
  6533. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6534. {
  6535. DoOpenDirectoryDialog(odAddBookmark, Side);
  6536. }
  6537. //---------------------------------------------------------------------------
  6538. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6539. {
  6540. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6541. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6542. try
  6543. {
  6544. TCustomDirView * DView = DirView(Side);
  6545. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6546. {
  6547. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6548. }
  6549. }
  6550. catch (...)
  6551. {
  6552. delete VisitedDirectories;
  6553. throw;
  6554. }
  6555. return VisitedDirectories;
  6556. }
  6557. //---------------------------------------------------------------------------
  6558. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6559. TOpenDirectoryMode Mode, TOperationSide Side)
  6560. {
  6561. bool Continue = true;
  6562. if (Mode == odAddBookmark)
  6563. {
  6564. TMessageParams Params(mpNeverAskAgainCheck);
  6565. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6566. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6567. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6568. unsigned int Answer =
  6569. MessageDialog(Message,
  6570. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6571. if (Answer == qaNeverAskAgain)
  6572. {
  6573. Continue = true;
  6574. WinConfiguration->UseSharedBookmarks = true;
  6575. }
  6576. else if (Answer == qaOK)
  6577. {
  6578. Continue = true;
  6579. WinConfiguration->UseSharedBookmarks = false;
  6580. }
  6581. else
  6582. {
  6583. Continue = false;
  6584. }
  6585. }
  6586. if (Continue)
  6587. {
  6588. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6589. try
  6590. {
  6591. UnicodeString Name = DirView(Side)->PathName;
  6592. bool Repeat;
  6593. do
  6594. {
  6595. Repeat = false;
  6596. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6597. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6598. // do not allow switching to location profiles,
  6599. // if we are not connected
  6600. HasDirView[osLocal] && IsAvailableTerminal(Terminal)))
  6601. {
  6602. TWindowLock Lock(this);
  6603. if (!TryOpenDirectory(Side, Name))
  6604. {
  6605. Repeat = true;
  6606. Mode = odBrowse;
  6607. }
  6608. }
  6609. }
  6610. while (Repeat);
  6611. }
  6612. __finally
  6613. {
  6614. delete VisitedDirectories;
  6615. }
  6616. }
  6617. }
  6618. //---------------------------------------------------------------------------
  6619. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6620. {
  6621. DebugAssert(!Terminal->CommandSessionOpened);
  6622. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6623. }
  6624. //---------------------------------------------------------------------------
  6625. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6626. {
  6627. bool Result = Terminal->IsCapable[Capability] ||
  6628. Terminal->CommandSessionOpened;
  6629. if (!Result)
  6630. {
  6631. // We might get here for example when "checksum" synchronization is requested from command-line
  6632. // on server that supports neither checksum nor shell
  6633. if (!Terminal->IsCapable[fcSecondaryShell])
  6634. {
  6635. NotSupported();
  6636. }
  6637. else if (!GUIConfiguration->ConfirmCommandSession)
  6638. {
  6639. Result = true;
  6640. }
  6641. else
  6642. {
  6643. TMessageParams Params(mpNeverAskAgainCheck);
  6644. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6645. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6646. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6647. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6648. if (Answer == qaNeverAskAgain)
  6649. {
  6650. GUIConfiguration->ConfirmCommandSession = false;
  6651. Result = true;
  6652. }
  6653. else if (Answer == qaOK)
  6654. {
  6655. Result = true;
  6656. }
  6657. }
  6658. if (Result)
  6659. {
  6660. Result = CommandSessionFallback();
  6661. }
  6662. }
  6663. return Result;
  6664. }
  6665. //---------------------------------------------------------------------------
  6666. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6667. {
  6668. if (EnsureCommandSessionFallback(fcAnyCommand))
  6669. {
  6670. DoConsoleDialog(Terminal, Command);
  6671. }
  6672. }
  6673. //---------------------------------------------------------------------------
  6674. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6675. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6676. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6677. {
  6678. if (!IsSideLocalBrowser(osRemote) &&
  6679. IsFileControl(DropSourceControl, osRemote) &&
  6680. (FDDExtMapFile != NULL))
  6681. {
  6682. Accept = true;
  6683. }
  6684. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6685. DebugAssert(FDDTargetControl != NULL);
  6686. }
  6687. //---------------------------------------------------------------------------
  6688. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6689. _di_IDataObject DataObj, int KeyState,
  6690. const TPoint & Point, int & Effect, bool & Accept)
  6691. {
  6692. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6693. }
  6694. //---------------------------------------------------------------------------
  6695. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6696. _di_IDataObject DataObj, int KeyState,
  6697. const TPoint & Point, int & Effect, bool & Accept)
  6698. {
  6699. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6700. }
  6701. //---------------------------------------------------------------------------
  6702. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6703. TObject * Sender)
  6704. {
  6705. DebugUsedParam(Sender);
  6706. DebugAssert(FDDTargetControl == Sender);
  6707. FDDTargetControl = NULL;
  6708. }
  6709. //---------------------------------------------------------------------------
  6710. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave(int)
  6711. {
  6712. FileControlDDDragLeave(SessionsPageControl);
  6713. }
  6714. //---------------------------------------------------------------------------
  6715. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave(int)
  6716. {
  6717. FileControlDDDragLeave(QueueView3);
  6718. }
  6719. //---------------------------------------------------------------------------
  6720. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6721. {
  6722. DebugAssert(!IsSideLocalBrowser(Side));
  6723. DebugUsedParam(Side);
  6724. bool Edit = false;
  6725. TRemoteFile * File = NULL;
  6726. UnicodeString FileName;
  6727. UnicodeString PointTo;
  6728. bool SymbolicLink = true;
  6729. if (RemoteDirView->ItemFocused)
  6730. {
  6731. DebugAssert(RemoteDirView->ItemFocused->Data);
  6732. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6733. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6734. if (Edit)
  6735. {
  6736. FileName = File->FileName;
  6737. PointTo = File->LinkTo;
  6738. }
  6739. else if (!File->IsParentDirectory)
  6740. {
  6741. PointTo = File->FileName;
  6742. }
  6743. }
  6744. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6745. Terminal->IsCapable[fcHardLink]))
  6746. {
  6747. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6748. if (Edit)
  6749. {
  6750. DebugAssert(File->FileName == FileName);
  6751. int Params = dfNoRecursive;
  6752. Terminal->ExceptionOnFail = true;
  6753. try
  6754. {
  6755. Terminal->DeleteFile(L"", File, &Params);
  6756. }
  6757. __finally
  6758. {
  6759. Terminal->ExceptionOnFail = false;
  6760. }
  6761. }
  6762. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6763. }
  6764. }
  6765. //---------------------------------------------------------------------------
  6766. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6767. {
  6768. return
  6769. (IsSideLocalBrowser(Side) ||
  6770. (HasAvailableTerminal() &&
  6771. Terminal->ResolvingSymlinks &&
  6772. Terminal->IsCapable[fcSymbolicLink]));
  6773. }
  6774. //---------------------------------------------------------------------------
  6775. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6776. {
  6777. return
  6778. !IsSideLocalBrowser(FCurrentSide) &&
  6779. (RemoteDirView->ItemFocused != NULL) &&
  6780. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6781. Terminal->SessionData->ResolveSymlinks;
  6782. }
  6783. //---------------------------------------------------------------------------
  6784. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6785. {
  6786. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6787. FForceExecution = true;
  6788. try
  6789. {
  6790. DirView(osCurrent)->ExecuteCurrentFile();
  6791. }
  6792. __finally
  6793. {
  6794. FForceExecution = false;
  6795. }
  6796. }
  6797. //---------------------------------------------------------------------------
  6798. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6799. {
  6800. TEditorData ExternalEditor;
  6801. ExternalEditor.Editor = edExternal;
  6802. bool Remember = false;
  6803. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6804. {
  6805. if (Remember)
  6806. {
  6807. TEditorList * EditorList = new TEditorList;
  6808. try
  6809. {
  6810. *EditorList = *WinConfiguration->EditorList;
  6811. bool Found = false;
  6812. int i = 0;
  6813. while (!Found && (i < EditorList->Count))
  6814. {
  6815. const TEditorPreferences * Editor = EditorList->Editors[i];
  6816. if ((Editor->Data->Editor == edExternal) &&
  6817. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6818. {
  6819. Found = true;
  6820. }
  6821. i++;
  6822. }
  6823. if (!Found)
  6824. {
  6825. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6826. WinConfiguration->EditorList = EditorList;
  6827. }
  6828. }
  6829. __finally
  6830. {
  6831. delete EditorList;
  6832. }
  6833. }
  6834. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6835. }
  6836. }
  6837. //---------------------------------------------------------------------------
  6838. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6839. {
  6840. if (FFileFindTerminal == ATerminal)
  6841. {
  6842. FFileFindTerminal = NULL;
  6843. HideFileFindDialog();
  6844. }
  6845. if (FClipboardTerminal == ATerminal)
  6846. {
  6847. ClipboardClear(); // implies ClipboardStop
  6848. }
  6849. if ((FOnSynchronizeAbort != NULL) &&
  6850. (ATerminal == FManagedSession))
  6851. {
  6852. FOnSynchronizeAbort(NULL);
  6853. }
  6854. }
  6855. //---------------------------------------------------------------------------
  6856. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6857. {
  6858. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  6859. DetachTerminal(Sender);
  6860. }
  6861. //---------------------------------------------------------------------------
  6862. void __fastcall TCustomScpExplorerForm::FileTerminalReplaced(
  6863. const UnicodeString DebugUsedArg(FileName), TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6864. {
  6865. TManagedTerminal * ATerminal = static_cast<TManagedTerminal *>(Arg);
  6866. if (Data->Terminal == Terminal)
  6867. {
  6868. Data->Terminal = ATerminal;
  6869. }
  6870. }
  6871. //---------------------------------------------------------------------------
  6872. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6873. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6874. {
  6875. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6876. DebugUsedParam(FileName);
  6877. DebugAssert(Terminal != NULL);
  6878. if (Data->Terminal == Terminal)
  6879. {
  6880. Data->Terminal = NULL;
  6881. }
  6882. }
  6883. //---------------------------------------------------------------------------
  6884. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6885. {
  6886. return NULL;
  6887. }
  6888. //---------------------------------------------------------------------------
  6889. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6890. {
  6891. UpdateControls();
  6892. SessionColor = TColor(0);
  6893. UpdateRemotePathComboBox(false);
  6894. NeedSession(false);
  6895. }
  6896. //---------------------------------------------------------------------------
  6897. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6898. {
  6899. try
  6900. {
  6901. // Cache, as the login dialog can change its value.
  6902. // See also DoShow. Also see TTerminalManager::NewSession.
  6903. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6904. try
  6905. {
  6906. if (ShowLogin)
  6907. {
  6908. bool ReloadSessions = !Startup;
  6909. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6910. }
  6911. }
  6912. __finally
  6913. {
  6914. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6915. // (so there was no chance for the user to open any session yet)
  6916. if ((ShowLogin || !Startup) &&
  6917. !WinConfiguration->KeepOpenWhenNoSession &&
  6918. // this rules out the implicit Commander's local-local browser
  6919. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6920. {
  6921. TerminateApplication();
  6922. }
  6923. }
  6924. }
  6925. catch (EAbort &)
  6926. {
  6927. // swallow
  6928. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6929. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6930. }
  6931. }
  6932. //---------------------------------------------------------------------------
  6933. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6934. {
  6935. TTerminalManager * Manager = TTerminalManager::Instance();
  6936. DebugAssert(!Manager->Updating);
  6937. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6938. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6939. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6940. SessionsPageControl->LockDrawing();
  6941. try
  6942. {
  6943. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6944. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6945. {
  6946. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6947. }
  6948. for (int Index = 0; Index <= Manager->Count; Index++)
  6949. {
  6950. TThemeTabSheet * TabSheet;
  6951. if (Index >= SessionsPageControl->PageCount)
  6952. {
  6953. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6954. TabSheet->PageControl = SessionsPageControl;
  6955. }
  6956. else
  6957. {
  6958. TabSheet = SessionsPageControl->Pages[Index];
  6959. }
  6960. bool IsSessionTab = (Index < Manager->Count);
  6961. if (IsSessionTab)
  6962. {
  6963. TTerminal * Terminal = Manager->Sessions[Index];
  6964. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6965. TabSheet->CaptionTruncation = WinConfiguration->SessionTabCaptionTruncation ? tttEllipsis : tttNone;
  6966. UpdateSessionTab(TabSheet);
  6967. }
  6968. else
  6969. {
  6970. TabSheet->Tag = 0; // not really needed
  6971. TabSheet->Shadowed = false;
  6972. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6973. TabSheet->CaptionTruncation = GetNewTabTabCaptionTruncation();
  6974. // 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)
  6975. UpdateNewTabTab();
  6976. }
  6977. }
  6978. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6979. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6980. {
  6981. SessionsPageControl->UpdateTabsCaptionTruncation();
  6982. }
  6983. }
  6984. __finally
  6985. {
  6986. SessionsPageControl->UnlockDrawing();
  6987. }
  6988. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6989. }
  6990. //---------------------------------------------------------------------------
  6991. TThemeTabCaptionTruncation TCustomScpExplorerForm::GetNewTabTabCaptionTruncation()
  6992. {
  6993. return WinConfiguration->SessionTabCaptionTruncation ? tttNoText : tttNone;
  6994. }
  6995. //---------------------------------------------------------------------------
  6996. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6997. {
  6998. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6999. }
  7000. //---------------------------------------------------------------------------
  7001. TThemeTabSheet * TCustomScpExplorerForm::GetNewTabTab()
  7002. {
  7003. return SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  7004. }
  7005. //---------------------------------------------------------------------------
  7006. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  7007. {
  7008. TThemeTabSheet * TabSheet = GetNewTabTab();
  7009. UnicodeString TabCaption;
  7010. if (WinConfiguration->SelectiveToolbarText)
  7011. {
  7012. TabCaption = GetNewTabTabCaption();
  7013. }
  7014. TabSheet->BaseCaption = TabCaption;
  7015. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  7016. }
  7017. //---------------------------------------------------------------------------
  7018. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  7019. {
  7020. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  7021. }
  7022. //---------------------------------------------------------------------------
  7023. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  7024. {
  7025. DebugFail();
  7026. return UnicodeString();
  7027. }
  7028. //---------------------------------------------------------------------------
  7029. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  7030. {
  7031. if (DebugAlwaysTrue(TabSheet != NULL))
  7032. {
  7033. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  7034. if (DebugAlwaysTrue(ASession != NULL))
  7035. {
  7036. TColor Color = (ASession == Terminal) ? FSessionColor : static_cast<TColor>(ASession->StateData->Color);
  7037. if (ASession->LocalBrowser)
  7038. {
  7039. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  7040. }
  7041. else
  7042. {
  7043. TabSheet->ImageIndex = AddSessionColor(Color);
  7044. }
  7045. TTerminalManager * Manager = TTerminalManager::Instance();
  7046. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  7047. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  7048. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  7049. }
  7050. }
  7051. }
  7052. //---------------------------------------------------------------------------
  7053. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  7054. {
  7055. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  7056. }
  7057. //---------------------------------------------------------------------------
  7058. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  7059. {
  7060. DebugAssert(SessionsPageControl->ActivePage != NULL);
  7061. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  7062. bool Result = (Session != NULL);
  7063. if (Result)
  7064. {
  7065. TTerminalManager::Instance()->ActiveSession = Session;
  7066. }
  7067. else
  7068. {
  7069. try
  7070. {
  7071. NewTab();
  7072. }
  7073. __finally
  7074. {
  7075. SessionListChanged();
  7076. }
  7077. FSessionsPageControlNewTabTime = Now();
  7078. }
  7079. return Result;
  7080. }
  7081. //---------------------------------------------------------------------------
  7082. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  7083. {
  7084. TWindowLock Lock(this);
  7085. SessionTabSwitched();
  7086. }
  7087. //---------------------------------------------------------------------------
  7088. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  7089. {
  7090. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  7091. DebugAssert(TransferList != NULL);
  7092. UnicodeString Name;
  7093. if (TransferList->ItemIndex <= 0)
  7094. {
  7095. Name = L"";
  7096. }
  7097. else
  7098. {
  7099. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  7100. }
  7101. if (FCopyParamAutoSelected.IsEmpty())
  7102. {
  7103. // if previous preset was not autoselected, make new preset the "default"
  7104. FCopyParamDefault = Name;
  7105. }
  7106. GUIConfiguration->CopyParamCurrent = Name;
  7107. }
  7108. //---------------------------------------------------------------------------
  7109. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  7110. {
  7111. // sanity check
  7112. bool ExistingPreset =
  7113. (FTransferListHoverIndex >= 0) &&
  7114. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  7115. DebugAssert(ExistingPreset);
  7116. if (ExistingPreset)
  7117. {
  7118. HDC DC = GetDC(0);
  7119. TCanvas * Canvas = new TCanvas();
  7120. try
  7121. {
  7122. Canvas->Handle = DC;
  7123. Canvas->Font = GetToolbarFont(this);
  7124. UnicodeString Name;
  7125. if (FTransferListHoverIndex == 0)
  7126. {
  7127. Name = L"";
  7128. }
  7129. else
  7130. {
  7131. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  7132. }
  7133. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  7134. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  7135. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  7136. static_cast<TObject*>(GetComponent(fcTransferList)));
  7137. UnicodeString InfoStr =
  7138. GUIConfiguration->CopyParamPreset[Name].
  7139. GetInfoStr(L"; ",
  7140. FLAGMASK(HasAvailableTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  7141. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  7142. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  7143. {
  7144. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  7145. {
  7146. InfoStr.SetLength(InfoStr.Length() - 1);
  7147. }
  7148. InfoStr += Ellipsis;
  7149. }
  7150. // UpdateCaption does not cause invalidation of whole submenu, while
  7151. // setting Caption property does.
  7152. // also it probably does not resize the label, even if necessary
  7153. // (we do not want that anyway)
  7154. TransferLabel->UpdateCaption(InfoStr);
  7155. }
  7156. __finally
  7157. {
  7158. Canvas->Handle = NULL;
  7159. ReleaseDC(0, DC);
  7160. delete Canvas;
  7161. }
  7162. }
  7163. }
  7164. //---------------------------------------------------------------------------
  7165. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  7166. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  7167. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  7168. {
  7169. if (FTransferListHoverIndex != AHoverIndex)
  7170. {
  7171. FTransferListHoverIndex = AHoverIndex;
  7172. UpdateTransferLabel();
  7173. }
  7174. }
  7175. //---------------------------------------------------------------------------
  7176. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  7177. {
  7178. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  7179. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  7180. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  7181. {
  7182. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  7183. {
  7184. if (!NonVisualDataModule->Busy)
  7185. {
  7186. OpenDirectory(GetSide(osCurrent));
  7187. }
  7188. Message.Result = 1;
  7189. }
  7190. else
  7191. {
  7192. TForm::Dispatch(&Message);
  7193. }
  7194. }
  7195. else
  7196. {
  7197. TForm::Dispatch(&Message);
  7198. }
  7199. }
  7200. //---------------------------------------------------------------------------
  7201. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  7202. {
  7203. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  7204. if ((FIgnoreNextDialogChar != 0) &&
  7205. (towupper(Message.CharCode) == towupper(FIgnoreNextDialogChar)))
  7206. {
  7207. Message.Result = 1;
  7208. }
  7209. else
  7210. {
  7211. TForm::Dispatch(&Message);
  7212. }
  7213. FIgnoreNextDialogChar = 0;
  7214. }
  7215. //---------------------------------------------------------------------------
  7216. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  7217. {
  7218. // The four low-order bits are used internally by Windows
  7219. unsigned int Cmd = (Message.WParam & 0xFFF0);
  7220. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  7221. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  7222. // 61730 - restore thru double click - undocumented
  7223. // 61490 - maximize thru double click - undocumented
  7224. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  7225. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  7226. {
  7227. SysResizing(Cmd);
  7228. }
  7229. TForm::Dispatch(&Message);
  7230. }
  7231. //---------------------------------------------------------------------------
  7232. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  7233. {
  7234. AppLog(L"Windows session end query");
  7235. // We were actually never able to make ENDSESSION_CRITICAL happen.
  7236. // Also there no point returning TRUE as we are not able to
  7237. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  7238. // Hence the process termination might be safer :)
  7239. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  7240. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  7241. {
  7242. Message.Result = FALSE;
  7243. }
  7244. else
  7245. {
  7246. Message.Result = TRUE;
  7247. }
  7248. // Do not call default handling as that triggers OnCloseQuery,
  7249. // where our implementation will popup confirmation dialogs, what we do not want,
  7250. // as per Vista guidelines:
  7251. // https://learn.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  7252. }
  7253. //---------------------------------------------------------------------------
  7254. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  7255. {
  7256. AppLog(L"Windows session end");
  7257. if (Message.EndSession && IsApplicationMinimized())
  7258. {
  7259. // WORKAROUND
  7260. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  7261. // when it receives WM_ENDSESSION,
  7262. // but that sometimes (particularly when application is minimized) cause crashes.
  7263. // Crash popups message that blocks log off.
  7264. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  7265. // so we call ExitProcess() immediately, not even trying to cleanup.
  7266. // It still causes beep, so there's likely some popup, but it does not block
  7267. // log off.
  7268. ExitProcess(0);
  7269. }
  7270. TForm::Dispatch(&Message);
  7271. }
  7272. //---------------------------------------------------------------------------
  7273. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  7274. {
  7275. }
  7276. //---------------------------------------------------------------------------
  7277. void __fastcall TCustomScpExplorerForm::DoShow()
  7278. {
  7279. // Called from TCustomScpExplorerForm::CMShowingChanged
  7280. // only now are the controls resized finally, so the size constraints
  7281. // will not conflict with possibly very small window size
  7282. RestoreFormParams();
  7283. // This indicates that the form was rescaled after parameters were loaded and
  7284. // as most probably the parameters were actually saved with the current form scaling
  7285. // (windows was the last time likely closed on the same monitor, where it is starting now),
  7286. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  7287. // and no rounding problems will occur.
  7288. // See also RestoreForm.
  7289. // (we should be safe to call RestoreParams unconditionally)
  7290. if (PixelsPerInch != Screen->PixelsPerInch)
  7291. {
  7292. RestoreParams();
  7293. }
  7294. FixControlsPlacement();
  7295. if (Position == poDefaultPosOnly)
  7296. {
  7297. CutFormToDesktop(this);
  7298. }
  7299. TForm::DoShow();
  7300. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  7301. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  7302. // This was previously in NeedSession, where other related code is.
  7303. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  7304. // unpleasantly shows briefly.
  7305. if ((ManagedSession == NULL) &&
  7306. !WinConfiguration->ShowLoginWhenNoSession &&
  7307. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  7308. // This detects if workspace was saved the last time the main widow was closed
  7309. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  7310. {
  7311. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  7312. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  7313. }
  7314. // Before main window displays
  7315. if (ManagedSession == NULL)
  7316. {
  7317. StartingWithoutSession();
  7318. }
  7319. UpdatePixelsPerInchMainWindowCounter();
  7320. FShowing = true;
  7321. }
  7322. //---------------------------------------------------------------------------
  7323. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  7324. {
  7325. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  7326. }
  7327. //---------------------------------------------------------------------------
  7328. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  7329. {
  7330. SessionListChanged();
  7331. InitStatusBar();
  7332. UpdateControls();
  7333. }
  7334. //---------------------------------------------------------------------------
  7335. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  7336. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  7337. TNotifyEvent OnBalloonClick, TObject * UserData)
  7338. {
  7339. bool Do;
  7340. UnicodeString Message;
  7341. if (E == NULL)
  7342. {
  7343. Message = Str;
  7344. Do = true;
  7345. }
  7346. else
  7347. {
  7348. Do = ExceptionMessage(E, Message);
  7349. }
  7350. if (Do && WinConfiguration->BalloonNotifications)
  7351. {
  7352. UnicodeString Title;
  7353. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  7354. {
  7355. Message = TrimLeft(Message);
  7356. }
  7357. else
  7358. {
  7359. Message = UnformatMessage(Message);
  7360. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  7361. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  7362. Title = LoadResourceString(Captions[Type]);
  7363. if (Terminal != NULL)
  7364. {
  7365. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal->PrimaryTerminal));
  7366. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  7367. }
  7368. }
  7369. if (Seconds == 0)
  7370. {
  7371. Seconds = WinConfiguration->NotificationsTimeout;
  7372. }
  7373. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  7374. }
  7375. }
  7376. //---------------------------------------------------------------------------
  7377. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  7378. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  7379. UnicodeString HelpKeyword, const TMessageParams * Params,
  7380. TTerminal * Terminal)
  7381. {
  7382. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  7383. (FOnFeedSynchronizeError != NULL)) &&
  7384. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  7385. {
  7386. if (FOnFeedSynchronizeError != NULL)
  7387. {
  7388. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  7389. }
  7390. else
  7391. {
  7392. DebugAssert(FErrorList != NULL);
  7393. TStringList * MoreMessagesCopy = NULL;
  7394. if (MoreMessages)
  7395. {
  7396. MoreMessagesCopy = new TStringList();
  7397. MoreMessagesCopy->Assign(MoreMessages);
  7398. }
  7399. FErrorList->AddObject(Message, MoreMessagesCopy);
  7400. }
  7401. PopupTrayBalloon(Terminal, Message, Type);
  7402. return ContinueAnswer(Answers);
  7403. }
  7404. else
  7405. {
  7406. bool UseBalloon = FTrayIcon->Visible;
  7407. if (UseBalloon)
  7408. {
  7409. PopupTrayBalloon(Terminal, Message, Type);
  7410. }
  7411. unsigned int Result;
  7412. try
  7413. {
  7414. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  7415. }
  7416. __finally
  7417. {
  7418. // cancel only balloon we popped up, otherwise we may cancel notification
  7419. // balloon that was there before the message dialog
  7420. // (such as "dd confirmation opt in balloon)
  7421. if (UseBalloon)
  7422. {
  7423. FTrayIcon->CancelBalloon();
  7424. }
  7425. }
  7426. return Result;
  7427. }
  7428. }
  7429. //---------------------------------------------------------------------------
  7430. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  7431. TTerminal * Terminal, Exception * E)
  7432. {
  7433. if (FTrayIcon->Visible)
  7434. {
  7435. PopupTrayBalloon(Terminal, L"", qtError, E);
  7436. }
  7437. // particularly prevent opening new session from jump list,
  7438. // while exception is shown
  7439. NonVisualDataModule->StartBusy();
  7440. try
  7441. {
  7442. ShowExtendedExceptionEx(Terminal, E);
  7443. }
  7444. __finally
  7445. {
  7446. NonVisualDataModule->EndBusy();
  7447. FTrayIcon->CancelBalloon();
  7448. }
  7449. }
  7450. //---------------------------------------------------------------------------
  7451. void __fastcall TCustomScpExplorerForm::SessionReady()
  7452. {
  7453. InitStatusBar();
  7454. // cannot rely on active page being page for active terminal,
  7455. // as it can happen that active page is the "new session" page
  7456. // (e.g. when reconnecting active terminal, while login dialog
  7457. // invoked from "new session" page is modal)
  7458. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  7459. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  7460. }
  7461. //---------------------------------------------------------------------------
  7462. void TCustomScpExplorerForm::InactiveTerminalNotify(
  7463. TManagedTerminal * Terminal, const UnicodeString & Message, TQueryType Type, Exception * E)
  7464. {
  7465. Notify(Terminal, Message, Type, false, NULL, NULL, E);
  7466. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  7467. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  7468. {
  7469. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  7470. }
  7471. }
  7472. //---------------------------------------------------------------------------
  7473. void TCustomScpExplorerForm::Notify(
  7474. TManagedTerminal * Terminal, const UnicodeString & AMessage, TQueryType Type,
  7475. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  7476. {
  7477. UnicodeString Message = AMessage;
  7478. if ((E == NULL) ||
  7479. ExceptionMessage(E, Message))
  7480. {
  7481. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  7482. if (Important)
  7483. {
  7484. Seconds *= 5;
  7485. }
  7486. UnicodeString NoteMessage(UnformatMessage(Message));
  7487. if (Terminal != NULL)
  7488. {
  7489. NoteMessage = TTerminalManager::Instance()->FormatTerminalNoteMessage(Terminal, NoteMessage);
  7490. }
  7491. if (WinConfiguration->BalloonNotifications)
  7492. {
  7493. AddNote(NoteMessage);
  7494. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  7495. }
  7496. else
  7497. {
  7498. FlashOnBackground();
  7499. PostNote(NoteMessage, Seconds, OnClick, UserData);
  7500. }
  7501. }
  7502. }
  7503. //---------------------------------------------------------------------------
  7504. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7505. {
  7506. RestoreApp();
  7507. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7508. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7509. !NonVisualDataModule->Busy)
  7510. {
  7511. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7512. TTerminalManager::Instance()->ActiveSession = Terminal;
  7513. if (!ComponentVisible[fcQueueView])
  7514. {
  7515. ToggleQueueVisibility();
  7516. GoToQueue();
  7517. }
  7518. }
  7519. }
  7520. //---------------------------------------------------------------------------
  7521. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7522. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7523. {
  7524. UnicodeString Message;
  7525. TNotifyEvent OnClick = NULL;
  7526. TObject * UserData = NULL;
  7527. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7528. switch (Event)
  7529. {
  7530. case qeEmptyButMonitored:
  7531. if ((ATerminal != Terminal) || QueueInvisible)
  7532. {
  7533. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7534. OnClick = QueueEmptyNoteClicked;
  7535. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7536. TerminalNoteData->Terminal = ATerminal;
  7537. UserData = TerminalNoteData;
  7538. }
  7539. break;
  7540. case qeEmpty:
  7541. OperationComplete(ATerminal->QueueOperationStart);
  7542. break;
  7543. case qePendingUserAction:
  7544. if ((ATerminal != Terminal) ||
  7545. (QueueInvisible && !IsQueueAutoPopup()))
  7546. {
  7547. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7548. }
  7549. break;
  7550. default:
  7551. DebugFail();
  7552. }
  7553. if (!Message.IsEmpty())
  7554. {
  7555. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7556. }
  7557. }
  7558. //---------------------------------------------------------------------------
  7559. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7560. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7561. {
  7562. if (FDDExtMapFile != NULL)
  7563. {
  7564. CloseHandle(FDDExtMapFile);
  7565. FDDExtMapFile = NULL;
  7566. }
  7567. if (WinConfiguration->DDFakeFile)
  7568. {
  7569. DDFakeFileInitDrag(FileList, Created);
  7570. }
  7571. }
  7572. //---------------------------------------------------------------------------
  7573. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7574. {
  7575. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7576. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7577. {
  7578. FFakeFileDropTarget = FileName;
  7579. }
  7580. }
  7581. //---------------------------------------------------------------------------
  7582. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7583. {
  7584. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7585. if (!ForceDirectories(ApiPath(Result)))
  7586. {
  7587. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7588. }
  7589. FileSetAttr(ApiPath(Result), faHidden);
  7590. return Result;
  7591. }
  7592. //---------------------------------------------------------------------------
  7593. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7594. bool & Created)
  7595. {
  7596. FFakeFileDropTarget = UnicodeString();
  7597. FDragFakeDirectory = CreateFakeTransferDirectory();
  7598. FileList->AddItem(NULL, FDragFakeDirectory);
  7599. Created = true;
  7600. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7601. {
  7602. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7603. }
  7604. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7605. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7606. {
  7607. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7608. TDragExtCommStruct* CommStruct;
  7609. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7610. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7611. DebugAssert(CommStruct != NULL);
  7612. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7613. CommStruct->Dragging = true;
  7614. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7615. LENOF(CommStruct->DropDest));
  7616. NULL_TERMINATE(CommStruct->DropDest);
  7617. UnmapViewOfFile(CommStruct);
  7618. }
  7619. }
  7620. //---------------------------------------------------------------------------
  7621. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7622. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7623. {
  7624. bool Result;
  7625. DebugAssert(!IsLocalBrowserMode());
  7626. if (Sender == RemoteDirView)
  7627. {
  7628. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7629. }
  7630. else
  7631. {
  7632. DebugAssert(Sender == RemoteDriveView);
  7633. TStrings * FileList = RemoteDriveView->DragFileList();
  7634. try
  7635. {
  7636. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7637. }
  7638. __finally
  7639. {
  7640. delete FileList;
  7641. }
  7642. }
  7643. if (FDDTargetControl == RemoteDriveView)
  7644. {
  7645. RemoteDriveView->UpdateDropTarget();
  7646. }
  7647. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7648. // or from tree view to dir view.
  7649. // foMove happens when file/dir is dragged from remote tree view outside of
  7650. // application via dragex
  7651. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7652. (DropSourceControl == RemoteDriveView))
  7653. {
  7654. RemoteDriveView->UpdateDropSource();
  7655. }
  7656. return Result;
  7657. }
  7658. //---------------------------------------------------------------------------
  7659. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7660. {
  7661. DebugAssert(!IsLocalBrowserMode());
  7662. // This also handles drops of remote files to queue.
  7663. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7664. SAFE_DESTROY(FDDFileList);
  7665. if (!FFakeFileDropTarget.IsEmpty())
  7666. {
  7667. RemoveDir(ApiPath(FFakeFileDropTarget));
  7668. }
  7669. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7670. {
  7671. try
  7672. {
  7673. TDragResult DDResult = (Sender == RemoteDirView) ?
  7674. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7675. // Focus is moved to the target application,
  7676. // but as we are going to present the UI, we need to steal the focus back.
  7677. // This most likely won't work though (windows does not allow application
  7678. // to steal focus most of the time)
  7679. Application->BringToFront();
  7680. // On older version of Windows we never got drMove here, see also comment below.
  7681. // On Windows 10, we get the "move".
  7682. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7683. {
  7684. UnicodeString TargetDirectory;
  7685. TFileOperation Operation;
  7686. // drInvalid may mean drMove, see comment below
  7687. switch (DDResult)
  7688. {
  7689. case drCopy:
  7690. Operation = foCopy;
  7691. break;
  7692. case drMove:
  7693. Operation = foMove;
  7694. break;
  7695. default:
  7696. DebugFail();
  7697. case drInvalid:
  7698. // prefer "copy" for safety
  7699. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7700. break;
  7701. }
  7702. TTransferOperationParam Param;
  7703. UnicodeString CounterName;
  7704. bool ForceQueue;
  7705. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7706. {
  7707. // we get drInvalid both if d&d was intercepted by ddext,
  7708. // and when users drops on no-drop location.
  7709. // we tell the difference by existence of response from ddext,
  7710. // so we ignore absence of response in this case
  7711. if (DDResult != drInvalid)
  7712. {
  7713. // here we know that the extension is installed,
  7714. // as it is checked as soon as drag&drop starts from
  7715. // RemoteFileControlDDCreateDragFileList
  7716. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7717. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7718. {
  7719. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7720. }
  7721. else
  7722. {
  7723. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7724. }
  7725. }
  7726. }
  7727. else
  7728. {
  7729. // download using fake file
  7730. Param.Temp = false;
  7731. Param.DragDrop = true;
  7732. if (ForceQueue)
  7733. {
  7734. Param.Queue = asOn;
  7735. }
  7736. if (Sender == RemoteDirView)
  7737. {
  7738. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7739. }
  7740. if (RemoteFileControlFileOperation(Sender, Operation,
  7741. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7742. {
  7743. Configuration->Usage->Inc(CounterName);
  7744. }
  7745. }
  7746. }
  7747. }
  7748. __finally
  7749. {
  7750. CloseHandle(FDDExtMapFile);
  7751. FDDExtMapFile = NULL;
  7752. delete FDragFakeMonitors;
  7753. FDragFakeMonitors = NULL;
  7754. RemoveDir(ApiPath(FDragFakeDirectory));
  7755. FDragFakeDirectory = L"";
  7756. FFakeFileDropTarget = UnicodeString();
  7757. }
  7758. }
  7759. if (FDragDropSshTerminate.get() != NULL)
  7760. {
  7761. FDragDropSshTerminate->Rethrow();
  7762. }
  7763. }
  7764. //---------------------------------------------------------------------------
  7765. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7766. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7767. {
  7768. // Remember drop effect so we know (when user drops files), if we copy or move
  7769. FLastDropEffect = dwEffect;
  7770. }
  7771. //---------------------------------------------------------------------------
  7772. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7773. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7774. {
  7775. bool Result;
  7776. if (FDDTargetControl == QueueView3)
  7777. {
  7778. Directory = DefaultDownloadTargetDirectory();
  7779. Result = true;
  7780. CounterName = L"DownloadsDragDropQueue";
  7781. ForceQueue = true;
  7782. }
  7783. else if (!FFakeFileDropTarget.IsEmpty())
  7784. {
  7785. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7786. Result = true;
  7787. ForceQueue = false;
  7788. CounterName = L"DownloadsDragDropFakeFile";
  7789. }
  7790. else
  7791. {
  7792. ForceQueue = false;
  7793. Enabled = false;
  7794. try
  7795. {
  7796. int Timer = 0;
  7797. Result = false;
  7798. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7799. {
  7800. {
  7801. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7802. TDragExtCommStruct* CommStruct;
  7803. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7804. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7805. DebugAssert(CommStruct != NULL);
  7806. Result = !CommStruct->Dragging;
  7807. if (Result)
  7808. {
  7809. Directory = ExtractFilePath(CommStruct->DropDest);
  7810. CounterName = L"DownloadsDragDropExternalExt";
  7811. }
  7812. UnmapViewOfFile(CommStruct);
  7813. }
  7814. if (!Result)
  7815. {
  7816. Sleep(50);
  7817. Timer += 50;
  7818. Application->ProcessMessages();
  7819. }
  7820. }
  7821. }
  7822. __finally
  7823. {
  7824. Enabled = true;
  7825. }
  7826. }
  7827. return Result;
  7828. }
  7829. //---------------------------------------------------------------------------
  7830. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7831. const UnicodeString TempDir, int SecDelay)
  7832. {
  7833. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7834. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7835. if (FDelayedDeletionTimer == NULL)
  7836. {
  7837. DebugAssert(HandleAllocated());
  7838. FDelayedDeletionTimer = new TTimer(this);
  7839. FDelayedDeletionTimer->Interval = 10000;
  7840. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7841. }
  7842. else
  7843. {
  7844. FDelayedDeletionTimer->Enabled = true;
  7845. }
  7846. }
  7847. //---------------------------------------------------------------------------
  7848. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7849. {
  7850. DebugAssert(FDelayedDeletionList != NULL);
  7851. TDateTime N = Now();
  7852. TDateTime Alert;
  7853. UnicodeString Directory;
  7854. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7855. {
  7856. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7857. if ((N >= Alert) || (Sender == NULL))
  7858. {
  7859. Directory = FDelayedDeletionList->Strings[Index];
  7860. if (RecursiveDeleteFile(ExcludeTrailingBackslash(Directory)))
  7861. {
  7862. FDelayedDeletionList->Delete(Index);
  7863. }
  7864. }
  7865. }
  7866. if (FDelayedDeletionList->Count == 0)
  7867. {
  7868. FDelayedDeletionTimer->Enabled = false;
  7869. }
  7870. }
  7871. //---------------------------------------------------------------------------
  7872. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7873. {
  7874. DebugAssert(!IsLocalBrowserMode());
  7875. if (IsFileControl(FDDTargetControl, osRemote) ||
  7876. (FDDTargetControl == SessionsPageControl))
  7877. {
  7878. TManagedTerminal * TargetTerminal = NULL;
  7879. TFileOperation Operation = foNone;
  7880. if (FDDTargetControl == SessionsPageControl)
  7881. {
  7882. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7883. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7884. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7885. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7886. if (IsAvailableTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7887. {
  7888. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7889. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7890. {
  7891. Operation = foRemoteMove;
  7892. }
  7893. else
  7894. {
  7895. Operation = foRemoteCopy;
  7896. }
  7897. }
  7898. }
  7899. else
  7900. {
  7901. if (FLastDropEffect == DROPEFFECT_MOVE)
  7902. {
  7903. Operation = foRemoteMove;
  7904. }
  7905. else if (FLastDropEffect == DROPEFFECT_COPY)
  7906. {
  7907. Operation = foRemoteCopy;
  7908. }
  7909. }
  7910. if (Operation != foNone)
  7911. {
  7912. RemoteFileControlFileOperation(DropSourceControl,
  7913. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7914. }
  7915. // abort drag&drop
  7916. Abort();
  7917. }
  7918. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7919. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7920. (FDDTargetControl != QueueView3))
  7921. {
  7922. DebugAssert(!FDragTempDir.IsEmpty());
  7923. TTransferType Type;
  7924. UnicodeString TempDir = FDragTempDir;
  7925. // We clear FDragTempDir before calling
  7926. // just in case it fail (raises exception)
  7927. FDragTempDir = L"";
  7928. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7929. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7930. // empty directory parameter means temp directory -> don't display it!
  7931. UnicodeString TargetDir = L"";
  7932. int Options = 0;
  7933. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7934. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7935. {
  7936. Abort();
  7937. }
  7938. // TargetDir is set when dropped on local file control
  7939. // (this was workaround for legacy dirview event handling, now it should be
  7940. // made prettier)
  7941. if (TargetDir.IsEmpty())
  7942. {
  7943. TargetDir = TempDir;
  7944. if (ForceDirectories(ApiPath(TargetDir)))
  7945. {
  7946. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7947. FPendingTempSpaceWarn = true;
  7948. try
  7949. {
  7950. FDragDropOperation = true;
  7951. int Params = cpTemporary |
  7952. (Type == ttMove ? cpDelete : 0);
  7953. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7954. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7955. }
  7956. __finally
  7957. {
  7958. FDragDropOperation = false;
  7959. FPendingTempSpaceWarn = false;
  7960. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7961. }
  7962. }
  7963. else
  7964. {
  7965. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7966. }
  7967. }
  7968. }
  7969. }
  7970. //---------------------------------------------------------------------------
  7971. void __fastcall TCustomScpExplorerForm::DDDownload(
  7972. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  7973. {
  7974. DebugAssert(!IsLocalBrowserMode());
  7975. TAutoBatch AutoBatch(this);
  7976. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  7977. UpdateCopyParamCounters(*CopyParam);
  7978. FMoveToQueue = false;
  7979. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7980. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7981. {
  7982. RemoteDriveView->UpdateDropSource();
  7983. }
  7984. }
  7985. //---------------------------------------------------------------------------
  7986. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7987. {
  7988. public:
  7989. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7990. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7991. {
  7992. }
  7993. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7994. {
  7995. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7996. TDataObjectFilesEx::AllowData(FormatEtc);
  7997. }
  7998. };
  7999. //---------------------------------------------------------------------------
  8000. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  8001. TObject * Sender, TDataObject *& DataObject)
  8002. {
  8003. if (FDDExtMapFile != NULL)
  8004. {
  8005. TFileList * FileList = DragDropFiles(Sender)->FileList;
  8006. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  8007. {
  8008. Abort();
  8009. }
  8010. if (FileList->Count > 0)
  8011. {
  8012. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  8013. if (!FilesObject->IsValid(true, true))
  8014. {
  8015. FilesObject->_Release();
  8016. }
  8017. else
  8018. {
  8019. DataObject = FilesObject;
  8020. }
  8021. }
  8022. }
  8023. }
  8024. //---------------------------------------------------------------------------
  8025. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  8026. {
  8027. DebugFail();
  8028. }
  8029. //---------------------------------------------------------------------------
  8030. void __fastcall TCustomScpExplorerForm::GoToTree()
  8031. {
  8032. ComponentVisible[fcRemoteTree] = true;
  8033. DriveView(osRemote)->SetFocus();
  8034. }
  8035. //---------------------------------------------------------------------------
  8036. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  8037. TPanelExport Export)
  8038. {
  8039. TCustomDirView * DirView = this->DirView(Side);
  8040. std::unique_ptr<TStrings> ExportData(new TStringList());
  8041. switch (Export)
  8042. {
  8043. case pePath:
  8044. ExportData->Add(DirView->PathName);
  8045. break;
  8046. case peFileList:
  8047. case peFullFileList:
  8048. {
  8049. bool FullPath = (Export == peFullFileList);
  8050. DirView->CreateFileList(false, FullPath, ExportData.get());
  8051. for (int Index = 0; Index < ExportData->Count; Index++)
  8052. {
  8053. if (ExportData->Strings[Index].Pos(L" ") > 0)
  8054. {
  8055. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  8056. }
  8057. }
  8058. }
  8059. break;
  8060. default:
  8061. DebugFail();
  8062. }
  8063. return ExportData.release();
  8064. }
  8065. //---------------------------------------------------------------------------
  8066. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  8067. TPanelExport Export, TPanelExportDestination Destination)
  8068. {
  8069. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  8070. PanelExportStore(Side, Export, Destination, ExportData.get());
  8071. }
  8072. //---------------------------------------------------------------------------
  8073. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  8074. TPanelExport /*Export*/, TPanelExportDestination Destination,
  8075. TStrings * ExportData)
  8076. {
  8077. if (Destination == pedClipboard)
  8078. {
  8079. TInstantOperationVisualizer Visualizer;
  8080. CopyToClipboard(ExportData);
  8081. }
  8082. else
  8083. {
  8084. DebugFail();
  8085. }
  8086. }
  8087. //---------------------------------------------------------------------------
  8088. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  8089. {
  8090. TCustomDirView * DirView = this->DirView(Side);
  8091. UnicodeString Mask = DirView->Mask;
  8092. if (DoFilterMaskDialog(DirView, Mask))
  8093. {
  8094. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  8095. Configuration->Usage->Inc(L"Filters");
  8096. }
  8097. }
  8098. //---------------------------------------------------------------------------
  8099. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  8100. {
  8101. return FQueueController->DefaultOperation();
  8102. }
  8103. //---------------------------------------------------------------------------
  8104. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  8105. TQueueOperation Operation, unsigned long * Param)
  8106. {
  8107. switch (Operation)
  8108. {
  8109. case qoPreferences:
  8110. return true;
  8111. case qoGoTo:
  8112. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  8113. case qoOnceEmpty:
  8114. return IsAnythingQueued();
  8115. default:
  8116. return FQueueController->AllowOperation(Operation, Param);
  8117. }
  8118. }
  8119. //---------------------------------------------------------------------------
  8120. void __fastcall TCustomScpExplorerForm::GoToQueue()
  8121. {
  8122. if (DebugAlwaysTrue(QueueView3->Visible))
  8123. {
  8124. QueueView3->SetFocus();
  8125. }
  8126. }
  8127. //---------------------------------------------------------------------------
  8128. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  8129. TQueueOperation Operation, unsigned long Param)
  8130. {
  8131. if (Operation == qoGoTo)
  8132. {
  8133. GoToQueue();
  8134. }
  8135. else if (Operation == qoPreferences)
  8136. {
  8137. PreferencesDialog(pmQueue);
  8138. }
  8139. else
  8140. {
  8141. FQueueController->ExecuteOperation(Operation, Param);
  8142. }
  8143. }
  8144. //---------------------------------------------------------------------------
  8145. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  8146. {
  8147. return (Queue != NULL) && Queue->Enabled;
  8148. }
  8149. //---------------------------------------------------------------------------
  8150. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  8151. {
  8152. DebugAssert(Queue != NULL);
  8153. if (Queue != NULL)
  8154. {
  8155. Queue->Enabled = !Queue->Enabled;
  8156. }
  8157. }
  8158. //---------------------------------------------------------------------------
  8159. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  8160. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  8161. {
  8162. FQueueActedItem = QueueView3->ItemFocused;
  8163. }
  8164. //---------------------------------------------------------------------------
  8165. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  8166. {
  8167. return TopDock->Height + (MessageDock->Visible ? MessageDock->Height : 0) + QueueSplitter->Height;
  8168. }
  8169. //---------------------------------------------------------------------------
  8170. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  8171. {
  8172. return
  8173. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  8174. (QueueDock->Visible ? QueueDock->Height : 0) +
  8175. (QueueLabel->Visible ? QueueLabel->Height : 0);
  8176. }
  8177. //---------------------------------------------------------------------------
  8178. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  8179. {
  8180. return ScaleByTextHeight(this, 48);
  8181. }
  8182. //---------------------------------------------------------------------------
  8183. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  8184. TObject * /*Sender*/, int & NewSize, bool & Accept)
  8185. {
  8186. // when queue is hidden by double-clicking splitter, stray attempt to
  8187. // resize the panel with strange value arrives, make sure it is ignored
  8188. if (ComponentVisible[fcQueueView])
  8189. {
  8190. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  8191. if (NewSize > MaxHeightLimit)
  8192. {
  8193. NewSize = MaxHeightLimit;
  8194. }
  8195. int MinHeightLimit =
  8196. GetStaticQueuePanelComponentsHeight() +
  8197. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  8198. GetMinQueueViewHeight();
  8199. if (NewSize < MinHeightLimit)
  8200. {
  8201. NewSize = MinHeightLimit;
  8202. }
  8203. }
  8204. else
  8205. {
  8206. Accept = false;
  8207. }
  8208. }
  8209. //---------------------------------------------------------------------------
  8210. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  8211. {
  8212. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  8213. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  8214. if (QueueViewHeight < GetMinQueueViewHeight())
  8215. {
  8216. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  8217. }
  8218. }
  8219. //---------------------------------------------------------------------------
  8220. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  8221. TDragObject *& /*DragObject*/)
  8222. {
  8223. FQueueActedItem = QueueView3->ItemFocused;
  8224. FQueueScrollOnDragOver->StartDrag();
  8225. }
  8226. //---------------------------------------------------------------------------
  8227. void __fastcall TCustomScpExplorerForm::QueueView3EndDrag(
  8228. TObject *, TObject * DebugUsedArg(Target), int DebugUsedArg(X), int DebugUsedArg(Y))
  8229. {
  8230. FQueueScrollOnDragOver->EndDrag();
  8231. }
  8232. //---------------------------------------------------------------------------
  8233. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  8234. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  8235. {
  8236. Accept = true;
  8237. if (Source == QueueView3)
  8238. {
  8239. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  8240. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  8241. if (Accept)
  8242. {
  8243. TQueueItemProxy * QueueItem;
  8244. TQueueItemProxy * DestQueueItem;
  8245. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  8246. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  8247. Accept = (QueueItem != DestQueueItem) &&
  8248. (QueueItem->Status == TQueueItem::qsPending) &&
  8249. (DestQueueItem->Status == TQueueItem::qsPending);
  8250. }
  8251. FQueueScrollOnDragOver->DragOver(TPoint(X, Y));
  8252. }
  8253. }
  8254. //---------------------------------------------------------------------------
  8255. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  8256. TObject * /*Source*/, int /*X*/, int /*Y*/)
  8257. {
  8258. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  8259. {
  8260. TQueueItemProxy * QueueItem;
  8261. TQueueItemProxy * DestQueueItem;
  8262. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  8263. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  8264. QueueItem->Move(DestQueueItem);
  8265. }
  8266. }
  8267. //---------------------------------------------------------------------------
  8268. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  8269. {
  8270. if ((QueueView3->ItemFocused == NULL) &&
  8271. (QueueView3->Items->Count > 0))
  8272. {
  8273. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  8274. }
  8275. QueueLabelUpdateStatus();
  8276. }
  8277. //---------------------------------------------------------------------------
  8278. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  8279. {
  8280. QueueLabelUpdateStatus();
  8281. }
  8282. //---------------------------------------------------------------------------
  8283. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  8284. {
  8285. QueueLabel->UpdateStatus();
  8286. }
  8287. //---------------------------------------------------------------------------
  8288. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  8289. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  8290. {
  8291. if (Selected)
  8292. {
  8293. NonVisualDataModule->UpdateNonVisibleActions();
  8294. }
  8295. }
  8296. //---------------------------------------------------------------------------
  8297. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  8298. {
  8299. TDragDropFilesEx * Result = NULL;
  8300. if (Sender == SessionsPageControl)
  8301. {
  8302. Result = FSessionsDragDropFilesEx;
  8303. }
  8304. else if (Sender == QueueView3)
  8305. {
  8306. Result = FQueueDragDropFilesEx;
  8307. }
  8308. else
  8309. {
  8310. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8311. if (DirView != NULL)
  8312. {
  8313. Result = DirView->DragDropFilesEx;
  8314. }
  8315. else
  8316. {
  8317. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  8318. if (DriveView != NULL)
  8319. {
  8320. Result = DriveView->DragDropFilesEx;
  8321. }
  8322. }
  8323. }
  8324. DebugAssert(Result != NULL);
  8325. return Result;
  8326. }
  8327. //---------------------------------------------------------------------------
  8328. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  8329. {
  8330. return (DView->SelCount == DView->FilesCount);
  8331. }
  8332. //---------------------------------------------------------------------------
  8333. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  8334. {
  8335. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  8336. }
  8337. //---------------------------------------------------------------------------
  8338. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  8339. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  8340. {
  8341. DebugAssert(!IsLocalBrowserMode());
  8342. TFileOperation Operation;
  8343. switch (Effect)
  8344. {
  8345. case DROPEFFECT_MOVE:
  8346. Operation = foMove;
  8347. break;
  8348. case DROPEFFECT_COPY:
  8349. // occures on WinXP (reported by user)
  8350. default:
  8351. Operation = foCopy;
  8352. break;
  8353. };
  8354. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  8355. // see a comment in TUnixDirView::PerformItemDragDropOperation
  8356. if (DragDropFilesEx->FileList->Count > 0)
  8357. {
  8358. TStrings * FileList = new TStringList();
  8359. try
  8360. {
  8361. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  8362. {
  8363. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  8364. }
  8365. FDragDropOperation = true;
  8366. TTransferOperationParam Param;
  8367. Param.TargetDirectory = TargetPath;
  8368. // upload, no temp dirs
  8369. Param.Temp = false;
  8370. Param.DragDrop = DragDrop;
  8371. Param.Options =
  8372. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  8373. if (ForceQueue)
  8374. {
  8375. Param.Queue = asOn;
  8376. }
  8377. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  8378. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  8379. {
  8380. if (IsFileControl(DropSourceControl, osLocal))
  8381. {
  8382. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  8383. }
  8384. else
  8385. {
  8386. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  8387. }
  8388. }
  8389. }
  8390. __finally
  8391. {
  8392. FDragDropOperation = false;
  8393. delete FileList;
  8394. }
  8395. }
  8396. }
  8397. //---------------------------------------------------------------------------
  8398. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  8399. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  8400. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  8401. {
  8402. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  8403. }
  8404. //---------------------------------------------------------------------------
  8405. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  8406. TObject * Sender, int grfKeyState, int & dwEffect)
  8407. {
  8408. DebugAssert(!IsLocalBrowserMode());
  8409. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  8410. if ((dwEffect != DROPEFFECT_NONE) &&
  8411. IsFileControl(DropSourceControl, osRemote))
  8412. {
  8413. // do not allow drop on remote panel (on free space, still allow drop on directories)
  8414. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  8415. (RemoteDirView->DropTarget == NULL))
  8416. {
  8417. dwEffect = DROPEFFECT_NONE;
  8418. }
  8419. else
  8420. {
  8421. if (dwEffect == DROPEFFECT_COPY)
  8422. {
  8423. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  8424. // currently we support copying always (at least via temporary directory);
  8425. // remove associated checks once this all proves stable and working
  8426. bool CopyCapable = true;
  8427. // if we do not support neither of operations, there's no discussion
  8428. if (!MoveCapable && !CopyCapable)
  8429. {
  8430. dwEffect = DROPEFFECT_NONE;
  8431. }
  8432. else
  8433. {
  8434. // The default effect inside remote panel is move,
  8435. // unless we do not support it, but support copy
  8436. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  8437. {
  8438. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  8439. }
  8440. else
  8441. {
  8442. // with ctrl-down, we want copy unless it is not supported
  8443. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  8444. }
  8445. }
  8446. }
  8447. }
  8448. }
  8449. }
  8450. //---------------------------------------------------------------------------
  8451. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  8452. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  8453. {
  8454. DebugAssert(!IsLocalBrowserMode());
  8455. if (FDDTotalSize >= 0)
  8456. {
  8457. if (File->IsDirectory)
  8458. {
  8459. FDDTotalSize = -1;
  8460. }
  8461. else
  8462. {
  8463. FDDTotalSize += File->Size;
  8464. }
  8465. }
  8466. DebugAssert(!FDragTempDir.IsEmpty());
  8467. // TODO: this is quite ineffective
  8468. // TODO: what if invalid character replacement is disabled?
  8469. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  8470. UnicodeString TransferFileName;
  8471. if (Sender == RemoteDriveView)
  8472. {
  8473. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  8474. }
  8475. else
  8476. {
  8477. TransferFileName = File->FileName;
  8478. }
  8479. FDDFileList->AddObject(TransferFileName, File);
  8480. }
  8481. //---------------------------------------------------------------------------
  8482. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  8483. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  8484. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  8485. {
  8486. // sometimes we do not get DDEnd so the list is not released
  8487. SAFE_DESTROY(FDDFileList);
  8488. FDDFileList = new TStringList();
  8489. FDragTempDir = WinConfiguration->TemporaryDir();
  8490. FDDTotalSize = 0;
  8491. FDragDropSshTerminate.reset(NULL);
  8492. }
  8493. //---------------------------------------------------------------------------
  8494. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  8495. TObject * /*Sender*/, LongBool /*FEscapePressed*/, int /*grfKeyState*/,
  8496. HRESULT & Result)
  8497. {
  8498. if (Result == DRAGDROP_S_DROP)
  8499. {
  8500. try
  8501. {
  8502. GlobalDragImageList->HideDragImage();
  8503. try
  8504. {
  8505. RemoteFileControlDDTargetDrop();
  8506. }
  8507. catch(ESshTerminate & E)
  8508. {
  8509. FDragDropSshTerminate.reset(E.Clone());
  8510. }
  8511. }
  8512. catch (Exception &E)
  8513. {
  8514. // If downloading fails we need to cancel drag&drop, otherwise
  8515. // Explorer shows error
  8516. // But by the way exception probably never reach this point as
  8517. // it's catched on way.
  8518. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8519. Result = DRAGDROP_S_CANCEL;
  8520. ShowExtendedException(Terminal, &E);
  8521. }
  8522. }
  8523. }
  8524. //---------------------------------------------------------------------------
  8525. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8526. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8527. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8528. {
  8529. TFileMasks::TParams MaskParams;
  8530. MaskParams.Size = Size;
  8531. MaskParams.Modification = Modification;
  8532. // this does not re-parse the mask if it is the same as the last time
  8533. FDirViewMatchMask = Masks;
  8534. bool ImplicitMatch;
  8535. Matches =
  8536. // Local and RecurseInclude parameters have no effect as there's no path.
  8537. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  8538. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  8539. (AllowImplicitMatches || !ImplicitMatch);
  8540. }
  8541. //---------------------------------------------------------------------------
  8542. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8543. TObject * Sender, TListItem * Item, WORD & Indexes)
  8544. {
  8545. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8546. if (GetPartialFileExtLen(DirView->ItemFileName(Item)) > 0)
  8547. {
  8548. Indexes |= oiPartial;
  8549. }
  8550. }
  8551. //---------------------------------------------------------------------------
  8552. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8553. {
  8554. return
  8555. DirViewEnabled(osCurrent) &&
  8556. DirView(osCurrent)->CanPasteFromClipBoard();
  8557. }
  8558. //---------------------------------------------------------------------------
  8559. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8560. {
  8561. bool Result = false;
  8562. if (CanPasteToDirViewFromClipBoard())
  8563. {
  8564. Result = true;
  8565. }
  8566. else
  8567. {
  8568. UnicodeString ClipboardText;
  8569. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8570. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8571. {
  8572. if (StoredSessions->IsUrl(ClipboardText))
  8573. {
  8574. Result = true;
  8575. }
  8576. else
  8577. {
  8578. Result = DirViewEnabled(osCurrent);
  8579. }
  8580. }
  8581. }
  8582. return Result;
  8583. }
  8584. //---------------------------------------------------------------------------
  8585. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8586. {
  8587. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8588. if (DoesClipboardContainOurFiles())
  8589. {
  8590. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8591. {
  8592. DebugAssert(!IsLocalBrowserMode());
  8593. TTerminalManager * Manager = TTerminalManager::Instance();
  8594. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8595. Manager->ActiveSession = FClipboardTerminal;
  8596. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8597. }
  8598. }
  8599. else if (CanPasteToDirViewFromClipBoard())
  8600. {
  8601. DirView(osCurrent)->PasteFromClipBoard();
  8602. }
  8603. else
  8604. {
  8605. UnicodeString ClipboardText;
  8606. if (NonEmptyTextFromClipboard(ClipboardText))
  8607. {
  8608. if (StoredSessions->IsUrl(ClipboardText))
  8609. {
  8610. NewSession(ClipboardText);
  8611. }
  8612. else
  8613. {
  8614. DirView(osCurrent)->Path = ClipboardText;
  8615. }
  8616. }
  8617. }
  8618. }
  8619. //---------------------------------------------------------------------------
  8620. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8621. {
  8622. // TBD
  8623. }
  8624. //---------------------------------------------------------------------------
  8625. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8626. {
  8627. TCustomDirView * ADirView = DirView(Side);
  8628. ADirView->SelectAll(Mode);
  8629. ADirView->SetFocus();
  8630. }
  8631. //---------------------------------------------------------------------------
  8632. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8633. {
  8634. TCustomDirView * ADirView = DirView(Side);
  8635. TFileFilter Filter;
  8636. DefaultFileFilter(Filter);
  8637. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8638. {
  8639. Configuration->Usage->Inc(L"MaskSelections");
  8640. ADirView->SelectFiles(Filter, Select);
  8641. ADirView->SetFocus();
  8642. }
  8643. }
  8644. //---------------------------------------------------------------------------
  8645. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8646. {
  8647. TCustomDirView * ADirView = DirView(Side);
  8648. ADirView->RestoreSelectedNames();
  8649. ADirView->SetFocus();
  8650. }
  8651. //---------------------------------------------------------------------------
  8652. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8653. {
  8654. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8655. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8656. {
  8657. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8658. UnicodeString Ext;
  8659. if (!IsSideLocalBrowser(osCurrent))
  8660. {
  8661. Ext = UnixExtractFileExt(FileName);
  8662. }
  8663. else
  8664. {
  8665. Ext = ExtractFileExt(FileName);
  8666. }
  8667. if (Ext.IsEmpty())
  8668. {
  8669. Ext = L".";
  8670. }
  8671. TFileFilter Filter;
  8672. Filter.Masks = FORMAT(L"*%s", (Ext));
  8673. Filter.Directories = false;
  8674. CurrentDirView->SelectFiles(Filter, Select);
  8675. }
  8676. }
  8677. //---------------------------------------------------------------------------
  8678. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8679. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8680. {
  8681. UnicodeString Result;
  8682. if (FIncrementalSearchState.Searching && (Side == GetSide(osCurrent)))
  8683. {
  8684. Result = FormatIncrementalSearchStatus(FIncrementalSearchState);
  8685. }
  8686. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8687. {
  8688. // noop
  8689. }
  8690. else
  8691. {
  8692. Result =
  8693. FMTLOAD(FILE_INFO_FORMAT,
  8694. (FormatBytes(FileInfo.SelectedSize),
  8695. FormatBytes(FileInfo.FilesSize),
  8696. FormatNumber(FileInfo.SelectedCount),
  8697. FormatNumber(FileInfo.FilesCount)));
  8698. }
  8699. return Result;
  8700. }
  8701. //---------------------------------------------------------------------------
  8702. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8703. TTBXStatusPanel * Panel, TOperationSide Side)
  8704. {
  8705. if (Panel->Index == 1)
  8706. {
  8707. ToggleShowHiddenFiles();
  8708. }
  8709. else if (Panel->Index == 2)
  8710. {
  8711. Filter(Side);
  8712. }
  8713. }
  8714. //---------------------------------------------------------------------------
  8715. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8716. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8717. {
  8718. DebugAssert(!StatusBar->SimplePanel);
  8719. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8720. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8721. }
  8722. //---------------------------------------------------------------------------
  8723. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8724. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8725. {
  8726. DebugAssert(StatusBar->Panels->Count >= 3);
  8727. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8728. if (FileInfo.HiddenCount > 0)
  8729. {
  8730. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8731. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8732. }
  8733. else
  8734. {
  8735. HiddenFilesPanel->ViewPriority = 0;
  8736. // not really necessary, just to cleanup no-longer-valid data
  8737. HiddenFilesPanel->Caption = L"";
  8738. }
  8739. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8740. if (FileInfo.FilteredCount > 0)
  8741. {
  8742. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8743. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8744. }
  8745. else
  8746. {
  8747. FilteredFilesPanel->ViewPriority = 0;
  8748. // not really necessary, just to cleanup no-longer-valid data
  8749. FilteredFilesPanel->Caption = L"";
  8750. }
  8751. }
  8752. //---------------------------------------------------------------------------
  8753. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8754. TObject * /*Sender*/)
  8755. {
  8756. if (DirView(osOther)->Enabled)
  8757. {
  8758. DirView(osOther)->SetFocus();
  8759. }
  8760. }
  8761. //---------------------------------------------------------------------------
  8762. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8763. {
  8764. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8765. switch (Config.Show)
  8766. {
  8767. case qvShow:
  8768. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8769. {
  8770. Config.Show = qvHide;
  8771. }
  8772. else
  8773. {
  8774. Config.Show = Config.LastHideShow;
  8775. }
  8776. break;
  8777. case qvHideWhenEmpty:
  8778. if (ComponentVisible[fcQueueView])
  8779. {
  8780. Config.Show = qvHide;
  8781. }
  8782. else
  8783. {
  8784. Config.LastHideShow = Config.Show;
  8785. Config.Show = qvShow;
  8786. }
  8787. break;
  8788. case qvHide:
  8789. Config.LastHideShow = Config.Show;
  8790. Config.Show = qvShow;
  8791. break;
  8792. }
  8793. WinConfiguration->QueueView = Config;
  8794. }
  8795. //---------------------------------------------------------------------------
  8796. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8797. {
  8798. UnicodeString Result;
  8799. if (Terminal != NULL)
  8800. {
  8801. switch (WinConfiguration->PathInCaption)
  8802. {
  8803. case picShort:
  8804. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8805. break;
  8806. case picFull:
  8807. Result = Terminal->CurrentDirectory;
  8808. break;
  8809. }
  8810. }
  8811. return Result;
  8812. }
  8813. //---------------------------------------------------------------------------
  8814. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8815. {
  8816. TColor Result = GetWindowColor(FSessionColor);
  8817. return Result;
  8818. }
  8819. //---------------------------------------------------------------------------
  8820. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8821. {
  8822. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8823. return Result;
  8824. }
  8825. //---------------------------------------------------------------------------
  8826. void TCustomScpExplorerForm::UpdatePanelControls(TCustomDirView * ADirView, TCustomDriveView * ADriveView)
  8827. {
  8828. bool UseDarkTheme = WinConfiguration->UseDarkTheme();
  8829. ADirView->DarkMode = UseDarkTheme;
  8830. ADriveView->DarkMode = UseDarkTheme;
  8831. }
  8832. //---------------------------------------------------------------------------
  8833. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8834. {
  8835. if (!FSessionChanging)
  8836. {
  8837. TTerminalManager::Instance()->UpdateAppTitle();
  8838. }
  8839. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8840. // background to gray on Windows 7 (works on Windows 10).
  8841. // See also EnableControl
  8842. if (Showing)
  8843. {
  8844. if (ManagedSession != NULL)
  8845. {
  8846. // Update path when it changes
  8847. if ((SessionsPageControl->ActivePage != NULL) &&
  8848. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8849. {
  8850. UpdateSessionTab(SessionsPageControl->ActivePage);
  8851. }
  8852. }
  8853. NonVisualDataModule->ReconnectSessionAction->Update();
  8854. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8855. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8856. CenterReconnectToolbar();
  8857. // The only place, where we use IsOpenedTerminal and not IsAvailableTerminal
  8858. // (i.e. not checking for reconnecting status)
  8859. // Otherwise it causes flicker [particularly in dark mode] during the short time
  8860. // between the authentication window is closed and FOpeningTerminal is cleared.
  8861. // Should be solved smarter.
  8862. bool HasTerminal = TTerminalManager::Instance()->IsOpenedTerminal(Terminal);
  8863. RemoteDirView->Enabled = HasTerminal;
  8864. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8865. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8866. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8867. RemoteDriveView->Color = RemoteDirView->Color;
  8868. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8869. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8870. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8871. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8872. QueueFileList->Enabled = QueueView3->Enabled;
  8873. QueueFileList->Color = QueueView3->Color;
  8874. QueueFileList->Font->Color = QueueView3->Font->Color;
  8875. QueueLabelUpdateStatus();
  8876. UpdatePanelControls(RemoteDirView, RemoteDriveView);
  8877. bool UseDarkTheme = WinConfiguration->UseDarkTheme();
  8878. if (FImmersiveDarkMode != UseDarkTheme)
  8879. {
  8880. UpdateDarkMode();
  8881. }
  8882. SessionsPageControl->TabTheme = UseDarkTheme ? CurrentTheme : NULL;
  8883. QueueView3->DarkMode = UseDarkTheme;
  8884. UnicodeString CurrentHiContrastThemeName = (FHiContrastTheme != NULL) ? FHiContrastTheme->Name : EmptyStr;
  8885. bool HiContrast = WinConfiguration->HiContrast;
  8886. UnicodeString NewHiContrastThemeName = HiContrast ? GetThemeName(!UseDarkTheme) : EmptyStr;
  8887. if (CurrentHiContrastThemeName != NewHiContrastThemeName)
  8888. {
  8889. ReleaseHiContrastTheme();
  8890. if (!NewHiContrastThemeName.IsEmpty())
  8891. {
  8892. FHiContrastTheme = GetTBXTheme(NewHiContrastThemeName);
  8893. }
  8894. }
  8895. SessionsPageControl->ActiveTabTheme = HiContrast ? DebugNotNull(FHiContrastTheme) : NULL;
  8896. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8897. MessageDock->Visible = (MessageDock->ToolbarCount > 0);
  8898. }
  8899. }
  8900. //---------------------------------------------------------------------------
  8901. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8902. {
  8903. UpdateControls();
  8904. }
  8905. //---------------------------------------------------------------------------
  8906. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8907. TObject * Sender)
  8908. {
  8909. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8910. DebugAssert(DirView != NULL);
  8911. DoDirViewLoaded(DirView);
  8912. TransferPresetAutoSelect();
  8913. TUnixDirView * UnixDirView = dynamic_cast<TUnixDirView *>(DirView);
  8914. if (UnixDirView != NULL)
  8915. {
  8916. TManagedTerminal * ATerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(UnixDirView->Terminal));
  8917. TTerminalManager::Instance()->TerminalLoadedDirectory(ATerminal);
  8918. }
  8919. }
  8920. //---------------------------------------------------------------------------
  8921. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8922. {
  8923. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8924. // first run after installation
  8925. if (double(Updates.LastCheck) == 0)
  8926. {
  8927. AppLog(L"First run, scheduling updates check for the next run");
  8928. // make sure next time there will be an update (if enabled)
  8929. Updates.LastCheck = TDateTime(1);
  8930. WinConfiguration->Updates = Updates;
  8931. }
  8932. else
  8933. {
  8934. TDateTime Period;
  8935. if (IsUWP() && !IsOfficialPackage())
  8936. {
  8937. Period = DefaultUpdatesPeriod;
  8938. AppLog(L"Thirdparty UWP package, using default updates check period");
  8939. }
  8940. else
  8941. {
  8942. Period = Updates.Period;
  8943. }
  8944. AppLogFmt(L"Updates check period: %.2f", (DateTimeToVariant(Period)));
  8945. if (double(Period) > 0)
  8946. {
  8947. TDateTime Interval = Now() - Updates.LastCheck;
  8948. AppLogFmt(L"Interval since the last updates check: %.2f", (DateTimeToVariant(Interval)));
  8949. if (Interval >= Period)
  8950. {
  8951. StartUpdateThread(UpdatesChecked);
  8952. }
  8953. }
  8954. }
  8955. }
  8956. //---------------------------------------------------------------------------
  8957. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8958. {
  8959. AppLog(L"Updates check done");
  8960. UnicodeString Message;
  8961. bool New;
  8962. TQueryType Type;
  8963. GetUpdatesMessage(Message, New, Type, false);
  8964. if (!Message.IsEmpty())
  8965. {
  8966. AppLogFmt(L"Updates check message: %s", (Message));
  8967. if (New)
  8968. {
  8969. AppLog(L"New version detected");
  8970. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8971. }
  8972. if (!New && (Type != qtWarning))
  8973. {
  8974. AppLog(L"Posting non-critical updates note");
  8975. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked);
  8976. }
  8977. else
  8978. {
  8979. AppLog(L"Posting new version or critical message notification");
  8980. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8981. }
  8982. if (New)
  8983. {
  8984. Configuration->Usage->Inc(L"UpdateNotifications");
  8985. }
  8986. }
  8987. }
  8988. //---------------------------------------------------------------------------
  8989. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8990. {
  8991. RestoreApp();
  8992. bool CanDisplay = !NonVisualDataModule->Busy;
  8993. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8994. {
  8995. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8996. }
  8997. if (CanDisplay)
  8998. {
  8999. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  9000. // Probably not needed as CheckForUpdates is blocking since we started using neon for HTTP implementation.
  9001. NonVisualDataModule->StartBusy();
  9002. try
  9003. {
  9004. CheckForUpdates(true);
  9005. }
  9006. __finally
  9007. {
  9008. NonVisualDataModule->EndBusy();
  9009. }
  9010. }
  9011. else
  9012. {
  9013. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  9014. }
  9015. }
  9016. //---------------------------------------------------------------------------
  9017. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  9018. TCopyParamRuleData & Data)
  9019. {
  9020. DebugAssert(Terminal != NULL);
  9021. Data.HostName = Terminal->SessionData->HostNameExpanded;
  9022. Data.UserName = Terminal->SessionData->UserNameExpanded;
  9023. Data.RemoteDirectory = RemoteDirView->PathName;
  9024. }
  9025. //---------------------------------------------------------------------------
  9026. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  9027. {
  9028. // Terminal can be null when we are changing local directory implicitly,
  9029. // because it has been deleted, while no session is connected
  9030. // (Login dialog is open)
  9031. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  9032. {
  9033. DebugAssert(!IsLocalBrowserMode());
  9034. TCopyParamRuleData Data;
  9035. GetTransferPresetAutoSelectData(Data);
  9036. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  9037. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  9038. if (CopyParamIndex < 0)
  9039. {
  9040. // there is no preset that matches autoselection
  9041. // set preset that we consider "default"
  9042. FCopyParamAutoSelected = L""; // forget last autoselected preset
  9043. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  9044. }
  9045. else
  9046. {
  9047. // there is preset matching autoselection
  9048. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  9049. if (CopyParamName == FCopyParamAutoSelected)
  9050. {
  9051. // autoselected the same preset as the last time
  9052. // make no change (i.e. preserve custom user preset, if any)
  9053. }
  9054. else
  9055. {
  9056. // autoselected the different preset then the last time (or there
  9057. // was default preset set)
  9058. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  9059. GUIConfiguration->CopyParamCurrent = CopyParamName;
  9060. }
  9061. }
  9062. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  9063. {
  9064. if (CopyParamIndex >= 0)
  9065. {
  9066. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  9067. }
  9068. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  9069. try
  9070. {
  9071. int Fmt =
  9072. (CopyParamIndex < 0) ?
  9073. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  9074. COPY_PARAM_AUTOSELECTED;
  9075. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  9076. Data->Message = MainInstructions(Message);
  9077. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  9078. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  9079. CopyParamAttrs);
  9080. if (CopyParamIndex >= 0)
  9081. {
  9082. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  9083. Info = FORMAT(L"%s\n \n%s", (Info,
  9084. FMTLOAD(COPY_PARAM_RULE,
  9085. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  9086. }
  9087. Data->Message += L"\n\n" + Info;
  9088. if (WinConfiguration->CopyParamAutoSelectNotice)
  9089. {
  9090. TransferPresetNoteMessage(Data, true);
  9091. }
  9092. else
  9093. {
  9094. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  9095. Data = NULL; // ownership passed
  9096. }
  9097. }
  9098. __finally
  9099. {
  9100. delete Data;
  9101. }
  9102. }
  9103. }
  9104. }
  9105. //---------------------------------------------------------------------------
  9106. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  9107. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  9108. {
  9109. DebugAssert(NoteData != NULL);
  9110. DebugAssert(!IsLocalBrowserMode());
  9111. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  9112. TQueryButtonAlias Aliases[1];
  9113. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  9114. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  9115. Params.Aliases = Aliases;
  9116. Params.AliasesCount = LENOF(Aliases);
  9117. unsigned int Result =
  9118. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  9119. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  9120. switch (Result)
  9121. {
  9122. case qaNeverAskAgain:
  9123. DebugAssert(AllowNeverAskAgain);
  9124. WinConfiguration->CopyParamAutoSelectNotice = false;
  9125. break;
  9126. case qaIgnore:
  9127. PreferencesDialog(pmPresets);
  9128. break;
  9129. }
  9130. }
  9131. //---------------------------------------------------------------------------
  9132. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  9133. {
  9134. // as of now useless, as this is used for notes only, never for balloons, ...
  9135. RestoreApp();
  9136. // .. and we should never be busy here
  9137. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  9138. {
  9139. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  9140. }
  9141. }
  9142. //---------------------------------------------------------------------------
  9143. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  9144. TPreferencesMode APreferencesMode)
  9145. {
  9146. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  9147. TCopyParamRuleData Data;
  9148. if (Terminal != NULL)
  9149. {
  9150. PreferencesData.reset(new TPreferencesDialogData());
  9151. GetTransferPresetAutoSelectData(Data);
  9152. PreferencesData->CopyParamRuleData = &Data;
  9153. }
  9154. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  9155. }
  9156. //---------------------------------------------------------------------------
  9157. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  9158. const TCustomCommandType & Command)
  9159. {
  9160. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  9161. {
  9162. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  9163. }
  9164. }
  9165. //---------------------------------------------------------------------------
  9166. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  9167. {
  9168. bool RemoteAllowed = CustomCommandRemoteAllowed();
  9169. TCustomCommandType Command;
  9170. // make sure we use local custom command when remote are not supported
  9171. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  9172. {
  9173. Command = FLastCustomCommand;
  9174. }
  9175. else
  9176. {
  9177. Command.Params = Command.Params | ccLocal;
  9178. }
  9179. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  9180. FEditingFocusedAdHocCommand = OnFocused;
  9181. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  9182. int Options =
  9183. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  9184. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  9185. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  9186. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  9187. {
  9188. FLastCustomCommand = Command;
  9189. UpdateCustomCommandsToolbar();
  9190. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  9191. }
  9192. }
  9193. //---------------------------------------------------------------------------
  9194. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  9195. {
  9196. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  9197. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  9198. DebugAssert(State > 0);
  9199. if (State <= 0)
  9200. {
  9201. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  9202. }
  9203. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  9204. }
  9205. //---------------------------------------------------------------------------
  9206. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  9207. TCustomCommandType & Command, int & State)
  9208. {
  9209. bool Result = !FLastCustomCommand.Command.IsEmpty();
  9210. if (Result)
  9211. {
  9212. Command = FLastCustomCommand;
  9213. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  9214. }
  9215. return Result;
  9216. }
  9217. //---------------------------------------------------------------------------
  9218. void __fastcall TCustomScpExplorerForm::BeforeAction()
  9219. {
  9220. ResetIncrementalSearch();
  9221. }
  9222. //---------------------------------------------------------------------------
  9223. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  9224. {
  9225. DebugAssert(ComponentVisible[Component]);
  9226. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  9227. }
  9228. //---------------------------------------------------------------------------
  9229. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  9230. {
  9231. // when queue panel is resized here directly, the status bar is stretched
  9232. // over whole space the panel occupied
  9233. PostComponentHide(fcQueueView);
  9234. }
  9235. //---------------------------------------------------------------------------
  9236. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  9237. {
  9238. ComponentVisible[fcQueueFileList] = false;
  9239. }
  9240. //---------------------------------------------------------------------------
  9241. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  9242. {
  9243. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  9244. // Scrollbars colors are refreshed by WM_THEMECHANGED.
  9245. RefreshColorMode();
  9246. ResetSysDarkTheme();
  9247. ConfigurationChanged();
  9248. ConfigureInterface();
  9249. // To update references to recreated/destroyed themes in SessionsPageControl
  9250. UpdateControls();
  9251. }
  9252. //---------------------------------------------------------------------------
  9253. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  9254. {
  9255. // Do not handle, when shutting down anyway (maybe also when not setup completely yet?)
  9256. if (!FInvalid &&
  9257. (Message.LParam != 0) &&
  9258. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  9259. {
  9260. ThemeChanged();
  9261. }
  9262. TForm::Dispatch(&Message);
  9263. }
  9264. //---------------------------------------------------------------------------
  9265. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  9266. {
  9267. TMessage * M = static_cast<TMessage*>(Message);
  9268. switch (M->Msg)
  9269. {
  9270. case CM_DIALOGCHAR:
  9271. CMDialogChar(*M);
  9272. break;
  9273. case WM_APPCOMMAND:
  9274. WMAppCommand(*M);
  9275. break;
  9276. case WM_SYSCOMMAND:
  9277. WMSysCommand(*M);
  9278. break;
  9279. case WM_QUERYENDSESSION:
  9280. WMQueryEndSession(*M);
  9281. break;
  9282. case WM_ENDSESSION:
  9283. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  9284. break;
  9285. case WM_COMPONENT_HIDE:
  9286. {
  9287. Byte Component = static_cast<Byte>(M->WParam);
  9288. // sanity check
  9289. if (ComponentVisible[Component])
  9290. {
  9291. // special treatment
  9292. if (Component == fcQueueView)
  9293. {
  9294. ToggleQueueVisibility();
  9295. DebugAssert(!ComponentVisible[fcQueueView]);
  9296. }
  9297. else
  9298. {
  9299. ComponentVisible[Component] = false;
  9300. }
  9301. }
  9302. }
  9303. break;
  9304. case WM_COPYDATA:
  9305. WMCopyData(*M);
  9306. break;
  9307. case WM_MANAGES_CAPTION:
  9308. // caption managed in TTerminalManager::UpdateAppTitle()
  9309. M->Result = 1;
  9310. break;
  9311. case WM_WANTS_MOUSEWHEEL:
  9312. M->Result = 1;
  9313. break;
  9314. case CM_SHOWINGCHANGED:
  9315. CMShowingChanged(*M);
  9316. break;
  9317. case WM_CLOSE:
  9318. WMClose(*M);
  9319. break;
  9320. case WM_DPICHANGED:
  9321. WMDpiChanged(*M);
  9322. break;
  9323. case WM_SETTINGCHANGE:
  9324. WMSettingChange(*M);
  9325. break;
  9326. case CM_DIALOGKEY:
  9327. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  9328. break;
  9329. case WM_PASTE_FILES:
  9330. PasteFiles();
  9331. break;
  9332. case WM_IS_HIDDEN:
  9333. // To treat master password prompt as a main window while showing it when workspace is loading while
  9334. // the exporer is showing up (Visible is true already)
  9335. M->Result = !FShowing;
  9336. break;
  9337. case WM_SHOWWINDOW:
  9338. if (M->WParam)
  9339. {
  9340. AddStartupSequence(L"J");
  9341. }
  9342. TForm::Dispatch(Message);
  9343. break;
  9344. case WM_QUEUE_CALLBACK:
  9345. WMQueueCallback(*M);
  9346. break;
  9347. default:
  9348. TForm::Dispatch(Message);
  9349. break;
  9350. }
  9351. }
  9352. //---------------------------------------------------------------------------
  9353. void __fastcall TCustomScpExplorerForm::UpdateImages()
  9354. {
  9355. // noop
  9356. }
  9357. //---------------------------------------------------------------------------
  9358. void TCustomScpExplorerForm::RegenerateSessionColorsImageList()
  9359. {
  9360. FSessionColors->Clear();
  9361. AddFixedSessionImages();
  9362. ::RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  9363. UpdateImages();
  9364. }
  9365. //---------------------------------------------------------------------------
  9366. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  9367. {
  9368. TForm::Dispatch(&Message);
  9369. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  9370. UpdatePixelsPerInchMainWindowCounter();
  9371. }
  9372. //---------------------------------------------------------------------------
  9373. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  9374. {
  9375. AppLog(L"Main window WM_CLOSE");
  9376. // Cannot close window while we are busy.
  9377. // We cannot test this in FormCloseQuery as that is called also from
  9378. // Close(), which is called by CloseApplicationAction. So we can be busy
  9379. // there already even, when it is legitimate to close the application.
  9380. // Possibly a better place to handle this would be WMSysCommand.
  9381. if (NonVisualDataModule->Busy)
  9382. {
  9383. Message.Result = 1;
  9384. }
  9385. else
  9386. {
  9387. TForm::Dispatch(&Message);
  9388. }
  9389. }
  9390. //---------------------------------------------------------------------------
  9391. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  9392. {
  9393. if (!FShowingChanged)
  9394. {
  9395. AddStartupSequence(L"D");
  9396. FShowingChanged = true;
  9397. }
  9398. // Ignoring implicit local-local browser of Commander, opened from DoShow
  9399. bool NoSession = (ManagedSession == NULL);
  9400. // This calls TCustomScpExplorerForm::DoShow
  9401. TForm::Dispatch(&Message);
  9402. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  9403. if (Showing && !FStarted)
  9404. {
  9405. FStarted = true;
  9406. InterfaceStarted();
  9407. }
  9408. if (Showing && NoSession)
  9409. {
  9410. // When we are starting minimized (i.e. from an installer),
  9411. // postpone showing Login dialog until we get restored.
  9412. // Otherwise the Login dialog (and Authentication window) show restored
  9413. // over invisible (minimized) main window.
  9414. if (WindowState == wsMinimized)
  9415. {
  9416. FNeedSession = true;
  9417. }
  9418. else
  9419. {
  9420. // This happens before application ever goes idle, so the toolbars would
  9421. // stay enabled (initial state) until the Login dialog is dismissed.
  9422. UpdateActions();
  9423. NonVisualDataModule->StartBusy();
  9424. try
  9425. {
  9426. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  9427. // otherwise the dialog does not receive focus.
  9428. // With Commander interface the ProcessMessages is called already
  9429. // by TDriveView, but with Explorer interface, we need to call it explicily
  9430. Application->ProcessMessages();
  9431. // do not reload sessions, they have been loaded just now (optimization)
  9432. NeedSession(true);
  9433. }
  9434. __finally
  9435. {
  9436. NonVisualDataModule->EndBusy();
  9437. }
  9438. }
  9439. }
  9440. }
  9441. //---------------------------------------------------------------------------
  9442. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  9443. {
  9444. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  9445. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  9446. }
  9447. //---------------------------------------------------------------------------
  9448. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  9449. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  9450. int & MaxHeight)
  9451. {
  9452. // workaround for bug in TWinControl.CalcConstraints
  9453. // Check for empty rect (restore from iconic state) is done there only after
  9454. // call to AdjustClientRect, which enlarges the rect (for forms).
  9455. TRect R = GetClientRect();
  9456. // when restoring from iconic state, place no restrictions
  9457. if (IsRectEmpty(R))
  9458. {
  9459. MinWidth = 0;
  9460. MinHeight = 0;
  9461. MaxWidth = 32000;
  9462. MaxHeight = 32000;
  9463. }
  9464. }
  9465. //---------------------------------------------------------------------------
  9466. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  9467. TSpaceAvailable & ASpaceAvailable, bool & Close)
  9468. {
  9469. // terminal can be already closed (e.g. dropped connection)
  9470. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  9471. {
  9472. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  9473. try
  9474. {
  9475. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  9476. }
  9477. catch(...)
  9478. {
  9479. if (!Terminal->Active)
  9480. {
  9481. Close = true;
  9482. }
  9483. throw;
  9484. }
  9485. }
  9486. }
  9487. //---------------------------------------------------------------------------
  9488. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  9489. {
  9490. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  9491. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  9492. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  9493. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  9494. {
  9495. OnGetSpaceAvailable = GetSpaceAvailable;
  9496. }
  9497. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  9498. OnGetSpaceAvailable);
  9499. }
  9500. //---------------------------------------------------------------------------
  9501. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  9502. {
  9503. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  9504. Terminal->FillSessionDataForCode(Data.get());
  9505. return Data.release();
  9506. }
  9507. //---------------------------------------------------------------------------
  9508. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  9509. {
  9510. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  9511. DoGenerateUrlDialog(Data.get(), Paths);
  9512. }
  9513. //---------------------------------------------------------------------------
  9514. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  9515. {
  9516. GenerateUrl(NULL);
  9517. }
  9518. //---------------------------------------------------------------------------
  9519. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  9520. {
  9521. std::unique_ptr<TStrings> Paths(new TStringList());
  9522. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  9523. GenerateUrl(Paths.get());
  9524. }
  9525. //---------------------------------------------------------------------------
  9526. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  9527. {
  9528. FSessionColor = value;
  9529. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  9530. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  9531. static_cast<TObject *>(GetComponent(fcColorMenu)));
  9532. DebugAssert(ColorItem != NULL);
  9533. ColorItem->Color = C;
  9534. NonVisualDataModule->ColorMenuItem->Color = C;
  9535. UpdateControls();
  9536. }
  9537. //---------------------------------------------------------------------------
  9538. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  9539. {
  9540. if (value != FSessionColor)
  9541. {
  9542. UpdateSessionColor(value);
  9543. // Is null when called from LastTerminalClosed
  9544. if (Terminal != NULL)
  9545. {
  9546. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  9547. }
  9548. }
  9549. }
  9550. //---------------------------------------------------------------------------
  9551. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  9552. {
  9553. bool Result = FNoteTimer->Enabled;
  9554. if (Result)
  9555. {
  9556. // cannot cancel note too early
  9557. bool NotEarly =
  9558. (Now() - FNoteShown >
  9559. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  9560. if (Force || NotEarly)
  9561. {
  9562. FNoteTimer->Enabled = false;
  9563. FNote = L"";
  9564. SAFE_DESTROY(FNoteData);
  9565. FOnNoteClick = NULL;
  9566. FNoteHints = FNotes->Text;
  9567. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  9568. UpdateStatusBar();
  9569. }
  9570. }
  9571. return Result;
  9572. }
  9573. //---------------------------------------------------------------------------
  9574. void TCustomScpExplorerForm::CancelNote(const UnicodeString & Note)
  9575. {
  9576. if (FNote == Note)
  9577. {
  9578. CancelNote(true);
  9579. }
  9580. }
  9581. //---------------------------------------------------------------------------
  9582. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  9583. {
  9584. DebugAssert(FNoteTimer->Enabled);
  9585. CancelNote(true);
  9586. }
  9587. //---------------------------------------------------------------------------
  9588. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9589. {
  9590. FNoteHints = FNotes->Text.TrimRight();
  9591. }
  9592. //---------------------------------------------------------------------------
  9593. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9594. {
  9595. int P = Note.Pos(L"\n");
  9596. if (P > 0)
  9597. {
  9598. Note.SetLength(P - 1);
  9599. }
  9600. FNotes->Add(FORMAT(L"[%s] %s",
  9601. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9602. while (FNotes->Count > 10)
  9603. {
  9604. FNotes->Delete(0);
  9605. }
  9606. if (UpdateNow)
  9607. {
  9608. UpdateNoteHints();
  9609. UpdateStatusBar();
  9610. }
  9611. }
  9612. //---------------------------------------------------------------------------
  9613. void TCustomScpExplorerForm::PostNote(
  9614. const UnicodeString & ANote, unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9615. {
  9616. UnicodeString Note = ANote;
  9617. int P = Note.Pos(L"\n");
  9618. if (P > 0)
  9619. {
  9620. Note.SetLength(P - 1);
  9621. }
  9622. UpdateNoteHints();
  9623. FNote = Note;
  9624. SAFE_DESTROY(FNoteData);
  9625. FNoteData = NoteData;
  9626. FOnNoteClick = OnNoteClick;
  9627. AddNote(Note, false);
  9628. UpdateStatusBar();
  9629. FNoteShown = Now();
  9630. FNoteTimer->Enabled = false;
  9631. if (Seconds == 0)
  9632. {
  9633. Seconds = WinConfiguration->NotificationsTimeout;
  9634. }
  9635. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9636. FNoteTimer->Enabled = true;
  9637. }
  9638. //---------------------------------------------------------------------------
  9639. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9640. {
  9641. PostNote(LoadStr(DIRECTORY_READING_CANCELLED));
  9642. }
  9643. //---------------------------------------------------------------------------
  9644. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9645. {
  9646. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9647. {
  9648. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9649. }
  9650. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9651. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9652. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))));
  9653. }
  9654. //---------------------------------------------------------------------------
  9655. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9656. {
  9657. bool NewShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9658. UnicodeString Note =
  9659. FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1), (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, (NewShowHiddenFiles ? 2 : 3))));
  9660. PostNote(Note);
  9661. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9662. WinConfiguration->ShowHiddenFiles = NewShowHiddenFiles;
  9663. }
  9664. //---------------------------------------------------------------------------
  9665. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9666. {
  9667. WinConfiguration->FormatSizeBytes = Style;
  9668. }
  9669. //---------------------------------------------------------------------------
  9670. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9671. {
  9672. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9673. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9674. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9675. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))));
  9676. }
  9677. //---------------------------------------------------------------------------
  9678. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9679. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9680. {
  9681. if (Panel->Index == 0)
  9682. {
  9683. if (FOnNoteClick != NULL)
  9684. {
  9685. // prevent the user data from being freed by possible call
  9686. // to CancelNote or PostNote during call to OnNoteClick
  9687. std::unique_ptr<TObject> NoteData(FNoteData);
  9688. TNotifyEvent OnNoteClick = FOnNoteClick;
  9689. FNoteData = NULL;
  9690. // need to cancel the note as we are going to delete its user data
  9691. CancelNote(true);
  9692. OnNoteClick(NoteData.get());
  9693. }
  9694. }
  9695. if (HasAvailableTerminal() &&
  9696. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9697. {
  9698. FileSystemInfo();
  9699. }
  9700. }
  9701. //---------------------------------------------------------------------------
  9702. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9703. {
  9704. // workaround:
  9705. // 1) for unknown reason, disabling window, while minimized,
  9706. // prevents it from restoring, even if it was enabled again meanwhile
  9707. // 2) when disabling the main window, while another has focus
  9708. // minimize is no longer possible
  9709. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9710. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9711. // Shouldn't we use IsApplicationMinimized() here?
  9712. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9713. {
  9714. AppLog(L"Disabling window");
  9715. Enabled = false;
  9716. }
  9717. FLockLevel++;
  9718. }
  9719. //---------------------------------------------------------------------------
  9720. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9721. {
  9722. DebugAssert(FLockLevel > 0);
  9723. FLockLevel--;
  9724. if (FLockLevel == 0)
  9725. {
  9726. DebugAssert(FLockSuspendLevel == 0);
  9727. AppLog(L"Re-enabling window");
  9728. Enabled = true;
  9729. // VCL_COPY (TCustomForm.SetWindowFocus)
  9730. if (Active && (ActiveControl != NULL))
  9731. {
  9732. ::SetFocus(ActiveControl->Handle);
  9733. if (GetFocus() == ActiveControl->Handle)
  9734. {
  9735. ActiveControl->Perform(CM_UIACTIVATE, 0, 0);
  9736. }
  9737. }
  9738. }
  9739. }
  9740. //---------------------------------------------------------------------------
  9741. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9742. {
  9743. // We need to make sure that window is enabled when the last modal window closes
  9744. // otherwise focus is not restored correctly.
  9745. // So we re-enable the window when modal window opens and
  9746. // disable it back after is closes
  9747. if (FLockLevel > 0)
  9748. {
  9749. // while we have nesting counter, we know that we never be called
  9750. // recursivelly as Application->OnModalBegin is called only
  9751. // for the top-level modal window
  9752. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9753. {
  9754. // won't be disabled when conditions in LockWindow() were not satisfied
  9755. FDisabledOnLockSuspend = !Enabled;
  9756. // When minimized to tray (or actually when set to SW_HIDE),
  9757. // setting Enabled makes the window focusable even when there's
  9758. // modal window over it
  9759. if (!FTrayIcon->Visible)
  9760. {
  9761. Enabled = true;
  9762. }
  9763. }
  9764. FLockSuspendLevel++;
  9765. }
  9766. }
  9767. //---------------------------------------------------------------------------
  9768. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9769. {
  9770. if (FLockSuspendLevel > 0)
  9771. {
  9772. DebugAssert(FLockLevel > 0);
  9773. FLockSuspendLevel--;
  9774. // see comment in SuspendWindowLock
  9775. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9776. {
  9777. // Note that window can be enabled here, when we were minized to tray when
  9778. // was SuspendWindowLock() called.
  9779. // We should possibly do the same check as in LockWindow(),
  9780. // if it is ever possible that the consitions change between
  9781. // SuspendWindowLock() and ResumeWindowLock()
  9782. Enabled = !FDisabledOnLockSuspend;
  9783. }
  9784. }
  9785. }
  9786. //---------------------------------------------------------------------------
  9787. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9788. {
  9789. TTBXComboBoxItem * RemotePathComboBox =
  9790. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9791. if (!TextOnly)
  9792. {
  9793. TStrings * Items = RemotePathComboBox->Strings;
  9794. Items->BeginUpdate();
  9795. try
  9796. {
  9797. Items->Clear();
  9798. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9799. {
  9800. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9801. while (!IsUnixRootPath(APath))
  9802. {
  9803. int P = APath.LastDelimiter(L'/');
  9804. DebugAssert(P >= 0);
  9805. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9806. APath.SetLength(P - 1);
  9807. }
  9808. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9809. }
  9810. }
  9811. __finally
  9812. {
  9813. Items->EndUpdate();
  9814. }
  9815. }
  9816. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9817. // Setting ItemIndex to -1 does not reset its text
  9818. if (RemotePathComboBox->Strings->Count == 0)
  9819. {
  9820. RemotePathComboBox->Text = L"";
  9821. }
  9822. }
  9823. //---------------------------------------------------------------------------
  9824. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9825. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9826. {
  9827. if (AIndex < 0)
  9828. {
  9829. AIndex = Sender->ItemIndex;
  9830. }
  9831. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9832. }
  9833. //---------------------------------------------------------------------------
  9834. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9835. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9836. int & ImageIndex)
  9837. {
  9838. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9839. }
  9840. //---------------------------------------------------------------------------
  9841. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9842. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9843. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9844. {
  9845. if (!IsLocalBrowserMode())
  9846. {
  9847. ARect.Left += (10 * AIndex);
  9848. }
  9849. }
  9850. //---------------------------------------------------------------------------
  9851. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9852. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9853. {
  9854. AWidth += (10 * AIndex);
  9855. }
  9856. //---------------------------------------------------------------------------
  9857. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9858. {
  9859. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9860. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9861. int Index = RemotePathComboBox->ItemIndex;
  9862. while (Index < RemotePathComboBox->Strings->Count - 1)
  9863. {
  9864. APath = UnixExtractFileDir(APath);
  9865. Index++;
  9866. }
  9867. // VanDyke style paths
  9868. if (APath.IsEmpty())
  9869. {
  9870. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9871. APath = ROOTDIRECTORY;
  9872. }
  9873. if (RemoteDirView->Path != APath)
  9874. {
  9875. RemoteDirView->Path = APath;
  9876. }
  9877. }
  9878. //---------------------------------------------------------------------------
  9879. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9880. {
  9881. DoRemotePathComboBoxItemClick(Sender);
  9882. }
  9883. //---------------------------------------------------------------------------
  9884. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9885. {
  9886. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9887. DebugUsedParam(Sender);
  9888. UpdateRemotePathComboBox(true);
  9889. }
  9890. //---------------------------------------------------------------------------
  9891. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9892. {
  9893. DoRemotePathComboBoxCancel(Sender);
  9894. }
  9895. //---------------------------------------------------------------------------
  9896. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9897. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9898. {
  9899. ResetIncrementalSearch();
  9900. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9901. DebugAssert(DirView != NULL);
  9902. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9903. {
  9904. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9905. // OnEditing is called also from TCustomListView::CanEdit
  9906. if (Edit != NULL)
  9907. {
  9908. EditSelectBaseName(Edit);
  9909. }
  9910. }
  9911. }
  9912. //---------------------------------------------------------------------------
  9913. //---------------------------------------------------------------------------
  9914. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9915. {
  9916. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9917. Result->AutoDetectDnD = false;
  9918. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9919. Result->RenderDataOn = rdoEnterAndDropSync;
  9920. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9921. return Result;
  9922. }
  9923. //---------------------------------------------------------------------------
  9924. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9925. {
  9926. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9927. UseDarkMode(this);
  9928. }
  9929. //---------------------------------------------------------------------------
  9930. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9931. {
  9932. TForm::CreateWnd();
  9933. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9934. UpdateDarkMode();
  9935. if (FSessionsDragDropFilesEx == NULL)
  9936. {
  9937. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9938. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9939. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9940. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9941. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9942. }
  9943. if (FQueueDragDropFilesEx == NULL)
  9944. {
  9945. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9946. // No need to set OnDragOver as we do not have any restrictions
  9947. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9948. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9949. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9950. }
  9951. }
  9952. //---------------------------------------------------------------------------
  9953. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9954. {
  9955. TForm::DestroyWnd();
  9956. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9957. FQueueDragDropFilesEx->DragDropControl = NULL;
  9958. }
  9959. //---------------------------------------------------------------------------
  9960. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9961. {
  9962. SideEnter(FCurrentSide);
  9963. FEverShown = true;
  9964. }
  9965. //---------------------------------------------------------------------------
  9966. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9967. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9968. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9969. {
  9970. if (!NonVisualDataModule->Busy)
  9971. {
  9972. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9973. Configuration->Usage->Inc(L"FileFinds");
  9974. LockWindow(true);
  9975. NonVisualDataModule->StartBusy();
  9976. WinConfiguration->LockedInterface = true;
  9977. try
  9978. {
  9979. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9980. }
  9981. __finally
  9982. {
  9983. WinConfiguration->LockedInterface = false;
  9984. NonVisualDataModule->EndBusy();
  9985. UnlockWindow();
  9986. }
  9987. }
  9988. }
  9989. //---------------------------------------------------------------------------
  9990. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9991. {
  9992. if (!NonVisualDataModule->Busy)
  9993. {
  9994. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9995. SetFocus();
  9996. RemoteDirView->Path = UnixExtractFilePath(Path);
  9997. UnicodeString FileName = UnixExtractFileName(Path);
  9998. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9999. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  10000. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  10001. if (Item == NULL)
  10002. {
  10003. ReloadDirectory(osRemote);
  10004. Item = RemoteDirView->FindFileItem(FileName);
  10005. }
  10006. if (Item != NULL)
  10007. {
  10008. RemoteDirView->ItemFocused = Item;
  10009. RemoteDirView->ItemFocused->MakeVisible(false);
  10010. RemoteDirView->SetFocus();
  10011. }
  10012. }
  10013. }
  10014. //---------------------------------------------------------------------------
  10015. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  10016. {
  10017. bool Result = !NonVisualDataModule->Busy;
  10018. if (Result)
  10019. {
  10020. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  10021. }
  10022. return Result;
  10023. }
  10024. //---------------------------------------------------------------------------
  10025. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  10026. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  10027. {
  10028. if (CanOperateOnFoundFiles(ATerminal))
  10029. {
  10030. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished, OnFileOperationFinished);
  10031. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  10032. }
  10033. }
  10034. //---------------------------------------------------------------------------
  10035. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  10036. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  10037. {
  10038. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  10039. }
  10040. //---------------------------------------------------------------------------
  10041. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  10042. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  10043. {
  10044. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  10045. }
  10046. //---------------------------------------------------------------------------
  10047. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  10048. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  10049. {
  10050. if (CanOperateOnFoundFiles(ATerminal))
  10051. {
  10052. for (int Index = 0; Index < FileList->Count; Index++)
  10053. {
  10054. UnicodeString FileName = FileList->Strings[Index];
  10055. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  10056. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  10057. OnFileOperationFinished(osRemote, FileName, true, true);
  10058. }
  10059. }
  10060. }
  10061. //---------------------------------------------------------------------------
  10062. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  10063. {
  10064. FFileFindTerminal = Terminal;
  10065. ShowFileFindDialog(
  10066. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  10067. DoDownloadFoundFiles, DoEditFoundFiles);
  10068. }
  10069. //---------------------------------------------------------------------------
  10070. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  10071. {
  10072. FTaskbarList = TaskbarList;
  10073. }
  10074. //---------------------------------------------------------------------------
  10075. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  10076. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  10077. {
  10078. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  10079. if (Index >= 0)
  10080. {
  10081. if (Button == mbLeft)
  10082. {
  10083. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  10084. // If switching tab (switching session) takes long enough for user
  10085. // to actually release the button, "mouse down" is still raised,
  10086. // but we do not get "mouse up" event, so dragging is not cancelled,
  10087. // prevent that by not beginning dragging in the first place.
  10088. int Key = ((GetSystemMetrics(SM_SWAPBUTTON) == 0) ? VK_LBUTTON : VK_RBUTTON);
  10089. if (IsKeyPressed(Key))
  10090. {
  10091. // when user clicks the "+", we get mouse down only after the session
  10092. // is closed, when new session tab is already on X:Y, so dragging
  10093. // starts, prevent that
  10094. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  10095. {
  10096. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10097. if (Terminal != NULL)
  10098. {
  10099. SessionsPageControl->BeginDrag(false);
  10100. }
  10101. }
  10102. }
  10103. }
  10104. else if (Button == mbMiddle)
  10105. {
  10106. // ignore middle-click for "New session tab" and the last local-local tab
  10107. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10108. if ((Session != NULL) && CanCloseSession(Session))
  10109. {
  10110. CloseSessionTab(Index);
  10111. }
  10112. }
  10113. }
  10114. }
  10115. //---------------------------------------------------------------------------
  10116. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  10117. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  10118. {
  10119. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  10120. // do not allow dropping on the "+" tab
  10121. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10122. if ((TargetTerminal != NULL) &&
  10123. (SessionsPageControl->ActivePage->PageIndex != Index))
  10124. {
  10125. Configuration->Usage->Inc(L"SessionTabMoves");
  10126. // this is almost redundant as we would recreate tabs in SessionListChanged,
  10127. // but we want to actually prevent that to avoid flicker
  10128. SessionsPageControl->ActivePage->PageIndex = Index;
  10129. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  10130. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  10131. }
  10132. }
  10133. //---------------------------------------------------------------------------
  10134. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  10135. TObject * Sender, TObject * Source, int X, int Y,
  10136. TDragState /*State*/, bool & Accept)
  10137. {
  10138. Accept = (Sender == Source);
  10139. if (Accept)
  10140. {
  10141. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  10142. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10143. // do not allow dragging to the "+" tab
  10144. Accept = (Terminal != NULL);
  10145. }
  10146. }
  10147. //---------------------------------------------------------------------------
  10148. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  10149. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  10150. {
  10151. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  10152. if (Index < 0)
  10153. {
  10154. Effect = DROPEFFECT_NONE;
  10155. }
  10156. else
  10157. {
  10158. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10159. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  10160. if (!IsAvailableTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  10161. {
  10162. Effect = DROPEFFECT_NONE;
  10163. }
  10164. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  10165. {
  10166. Effect = DROPEFFECT_COPY;
  10167. }
  10168. }
  10169. }
  10170. //---------------------------------------------------------------------------
  10171. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  10172. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  10173. {
  10174. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  10175. // do not allow dropping on local-local, disconnected and the "+" tab
  10176. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  10177. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10178. if (DebugAlwaysTrue(IsAvailableTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  10179. {
  10180. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  10181. if (!IsFileControl(DropSourceControl, osRemote))
  10182. {
  10183. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  10184. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  10185. // Why don't we use Terminal->CurrentDirectory directly?
  10186. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  10187. // do not force queue, drag drop
  10188. false, true);
  10189. }
  10190. }
  10191. }
  10192. //---------------------------------------------------------------------------
  10193. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  10194. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  10195. {
  10196. // Downloads are handled in RemoteFileControlDDEnd
  10197. if (!IsFileControl(DropSourceControl, osRemote))
  10198. {
  10199. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  10200. Terminal->CurrentDirectory,
  10201. // force queue, drag drop
  10202. true, true);
  10203. }
  10204. }
  10205. //---------------------------------------------------------------------------
  10206. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  10207. {
  10208. FShowing = false;
  10209. // Do not save empty workspace
  10210. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  10211. {
  10212. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  10213. UnicodeString Name = WorkspaceName();
  10214. DoSaveWorkspace(
  10215. Name, DataList.get(),
  10216. !Configuration->DisablePasswordStoring &&
  10217. WinConfiguration->AutoSaveWorkspacePasswords, false);
  10218. WinConfiguration->LastStoredSession = Name;
  10219. }
  10220. AppLog(L"Closing main window");
  10221. }
  10222. //---------------------------------------------------------------------------
  10223. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  10224. {
  10225. TManagedTerminal * ManagedTerminal =
  10226. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  10227. if (ManagedTerminal != NULL)
  10228. {
  10229. ManagedTerminal->DirectoryLoaded = Now();
  10230. }
  10231. }
  10232. //---------------------------------------------------------------------------
  10233. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  10234. TListItem * /*Item*/, bool /*Selected*/)
  10235. {
  10236. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  10237. switch (DirView->LastSelectMethod)
  10238. {
  10239. case smKeyboard:
  10240. Configuration->Usage->Inc(L"KeyboardSelections");
  10241. break;
  10242. case smMouse:
  10243. Configuration->Usage->Inc(L"MouseSelections");
  10244. break;
  10245. }
  10246. }
  10247. //---------------------------------------------------------------------------
  10248. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  10249. {
  10250. if (Color != 0)
  10251. {
  10252. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  10253. }
  10254. else
  10255. {
  10256. return FSessionTabImageIndex;
  10257. }
  10258. }
  10259. //---------------------------------------------------------------------------
  10260. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  10261. {
  10262. TPngImageCollectionItem * Item =
  10263. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  10264. FSessionColors->AddPng(Item->PngImage);
  10265. return FSessionColors->Count - 1;
  10266. }
  10267. //---------------------------------------------------------------------------
  10268. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  10269. {
  10270. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  10271. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  10272. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  10273. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  10274. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  10275. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  10276. }
  10277. //---------------------------------------------------------------------------
  10278. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  10279. {
  10280. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  10281. }
  10282. //---------------------------------------------------------------------------
  10283. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  10284. {
  10285. switch (Side)
  10286. {
  10287. case osLocal:
  10288. return FNewLocalTabTabImageIndex;
  10289. case osRemote:
  10290. case osCurrent:
  10291. return FNewRemoteTabTabImageIndex;
  10292. default:
  10293. DebugFail();
  10294. return -1;
  10295. }
  10296. }
  10297. //---------------------------------------------------------------------------
  10298. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  10299. {
  10300. for (int Index = 0; Index < Control->ControlCount; Index++)
  10301. {
  10302. TControl * AControl = Control->Controls[Index];
  10303. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  10304. if (WinControl != NULL)
  10305. {
  10306. CollectItemsWithTextDisplayMode(WinControl);
  10307. }
  10308. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  10309. if (Toolbar != NULL)
  10310. {
  10311. // we care for top-level items only
  10312. TTBCustomItem * Items = Toolbar->Items;
  10313. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  10314. {
  10315. TTBCustomItem * Item = Items->Items[ItemIndex];
  10316. if (((Item->DisplayMode == nbdmImageAndText) ||
  10317. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  10318. EligibleForImageDisplayMode(Item))
  10319. {
  10320. FItemsWithTextDisplayMode.insert(Item);
  10321. }
  10322. }
  10323. }
  10324. }
  10325. }
  10326. //---------------------------------------------------------------------------
  10327. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  10328. {
  10329. return true;
  10330. }
  10331. //---------------------------------------------------------------------------
  10332. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  10333. {
  10334. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  10335. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  10336. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  10337. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  10338. bool Result = true;
  10339. while (Result && (i != FItemsWithTextDisplayMode.end()))
  10340. {
  10341. TTBCustomItem * Item = *i;
  10342. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  10343. if (Label != NULL)
  10344. {
  10345. // optimization
  10346. if (Label->Visible == SelectiveToolbarText)
  10347. {
  10348. Result = false;
  10349. }
  10350. else
  10351. {
  10352. Label->Visible = SelectiveToolbarText;
  10353. }
  10354. }
  10355. else
  10356. {
  10357. // optimization
  10358. if (Item->DisplayMode == DisplayMode)
  10359. {
  10360. Result = false;
  10361. }
  10362. else
  10363. {
  10364. Item->DisplayMode = DisplayMode;
  10365. }
  10366. }
  10367. ++i;
  10368. }
  10369. UpdateNewTabTab();
  10370. return Result;
  10371. }
  10372. //---------------------------------------------------------------------------
  10373. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  10374. {
  10375. DebugFail();
  10376. }
  10377. //---------------------------------------------------------------------------
  10378. bool __fastcall TCustomScpExplorerForm::IsBusy()
  10379. {
  10380. // Among other, a lock level is non zero, while directory is being loaded,
  10381. // even when it happens as a result of dirview navigation,
  10382. // i.e. when TNonVisualDataModule is NOT FBusy.
  10383. // That's why the TNonVisualDataModule::GetBusy calls this method.
  10384. // Among other this prevents a panel auto update to occur while
  10385. // directory is changing.
  10386. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  10387. }
  10388. //---------------------------------------------------------------------------
  10389. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  10390. {
  10391. // While the window is disabled, we still seem to process menu shortcuts at least,
  10392. // so stop it at least here.
  10393. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  10394. bool Result;
  10395. if (Allowed == aaUpdate)
  10396. {
  10397. Result = true;
  10398. }
  10399. else
  10400. {
  10401. Result = !NonVisualDataModule->Busy;
  10402. if (Result && (Allowed == aaShortCut))
  10403. {
  10404. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  10405. Action->Update();
  10406. Result = Action->Enabled;
  10407. }
  10408. }
  10409. return Result;
  10410. }
  10411. //---------------------------------------------------------------------------
  10412. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  10413. {
  10414. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  10415. }
  10416. //---------------------------------------------------------------------------
  10417. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  10418. {
  10419. // This is somewhat redundant to LockWindow() call from
  10420. // TTerminalManager::TerminalReadDirectoryProgress.
  10421. // But disabling window is known not to block keyboard shorcuts
  10422. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  10423. if (Busy > 0)
  10424. {
  10425. if (NonVisualDataModule->Busy)
  10426. {
  10427. State = false;
  10428. }
  10429. else
  10430. {
  10431. NonVisualDataModule->StartBusy();
  10432. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  10433. LockWindow();
  10434. }
  10435. }
  10436. else if (Busy < 0)
  10437. {
  10438. UnlockWindow();
  10439. NonVisualDataModule->EndBusy();
  10440. }
  10441. else
  10442. {
  10443. State = NonVisualDataModule->Busy;
  10444. }
  10445. }
  10446. //---------------------------------------------------------------------------
  10447. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  10448. {
  10449. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  10450. if (Index >= 0)
  10451. {
  10452. TPopupMenu * PopupMenu = NULL;
  10453. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10454. if (Session != NULL)
  10455. {
  10456. SessionsPageControl->ActivePageIndex = Index;
  10457. if (DebugAlwaysTrue(SessionTabSwitched()))
  10458. {
  10459. // copied from TControl.WMContextMenu
  10460. SendCancelMode(SessionsPageControl);
  10461. // explicit popup instead of using PopupMenu property
  10462. // to avoid menu to popup somewhere within SessionTabSwitched above,
  10463. // while connecting yet not-connected session and hence
  10464. // allowing an access to commands over not-completely connected session
  10465. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  10466. }
  10467. }
  10468. else
  10469. {
  10470. if (SupportsLocalBrowser())
  10471. {
  10472. PopupMenu = NonVisualDataModule->NewTabPopup;
  10473. }
  10474. }
  10475. if (PopupMenu != NULL)
  10476. {
  10477. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  10478. PopupMenu->PopupComponent = SessionsPageControl;
  10479. PopupMenu->Popup(Point.x, Point.y);
  10480. }
  10481. }
  10482. Handled = true;
  10483. }
  10484. //---------------------------------------------------------------------------
  10485. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  10486. {
  10487. return HasAvailableTerminal() && Terminal->IsCapable[fcChangePassword];
  10488. }
  10489. //---------------------------------------------------------------------------
  10490. void __fastcall TCustomScpExplorerForm::ChangePassword()
  10491. {
  10492. Configuration->Usage->Inc(L"PasswordChanges");
  10493. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  10494. ChangePasswordData->DisableAuthentationsExceptPassword();
  10495. ChangePasswordData->ChangePassword = true;
  10496. std::unique_ptr<TTerminal> ChangePasswordSession(
  10497. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  10498. ChangePasswordSession->AutoReadDirectory = false;
  10499. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  10500. {
  10501. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  10502. }
  10503. else
  10504. {
  10505. Configuration->Usage->Inc(L"PasswordChangesFailed");
  10506. }
  10507. }
  10508. //---------------------------------------------------------------------------
  10509. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  10510. {
  10511. // No nice way to assert SSH2
  10512. return HasAvailableTerminal() && (Terminal->FSProtocol == cfsSFTP);
  10513. }
  10514. //---------------------------------------------------------------------------
  10515. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  10516. {
  10517. TOperationVisualizer OperationVisualizer;
  10518. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  10519. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  10520. }
  10521. //---------------------------------------------------------------------------
  10522. void __fastcall TCustomScpExplorerForm::FormAfterMonitorDpiChanged(TObject *, int OldDPI, int NewDPI)
  10523. {
  10524. DebugUsedParam(OldDPI);
  10525. DebugAssert(NewDPI == GetControlPixelsPerInch(this));
  10526. GlyphsModule->PixelsPerInch = NewDPI;
  10527. // Can be called too early when primary monitor has non-system dpi
  10528. if (FSessionColors != NULL)
  10529. {
  10530. RegenerateSessionColorsImageList();
  10531. }
  10532. UpdateTabsSize();
  10533. UpdateSessionsPageControlHeight();
  10534. QueueFileListColumnAutoSize();
  10535. }
  10536. //---------------------------------------------------------------------------
  10537. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  10538. {
  10539. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  10540. }
  10541. //---------------------------------------------------------------------
  10542. class TPublicControl : public TControl
  10543. {
  10544. friend class TCustomScpExplorerForm;
  10545. };
  10546. //---------------------------------------------------------------------------
  10547. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  10548. {
  10549. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  10550. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  10551. }
  10552. //---------------------------------------------------------------------------
  10553. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  10554. {
  10555. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  10556. OpenBookmark(Side, Bookmark);
  10557. }
  10558. //---------------------------------------------------------------------------
  10559. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  10560. {
  10561. DoBookmarkClick(osLocal, Sender);
  10562. }
  10563. //---------------------------------------------------------------------------
  10564. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  10565. {
  10566. DoBookmarkClick(osRemote, Sender);
  10567. }
  10568. //---------------------------------------------------------------------------
  10569. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  10570. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  10571. {
  10572. if (BookmarkList != NULL)
  10573. {
  10574. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  10575. if (IsLocalBrowserMode())
  10576. {
  10577. Side = osLocal;
  10578. }
  10579. if (!WinConfiguration->UseLocationProfiles)
  10580. {
  10581. std::unique_ptr<TStringList> Directories(new TStringList());
  10582. Directories->CaseSensitive = (Side == osRemote);
  10583. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10584. {
  10585. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10586. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  10587. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  10588. {
  10589. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10590. Item->Caption = EscapeHotkey(Directory);
  10591. Item->ShortCut = Bookmark->ShortCut;
  10592. Item->OnClick = OnBookmarkClick;
  10593. Item->Tag = reinterpret_cast<int>(Bookmark);
  10594. Directories->Add(Directory);
  10595. Menu->Add(Item.release());
  10596. }
  10597. }
  10598. }
  10599. else
  10600. {
  10601. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10602. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10603. {
  10604. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10605. if (!Bookmark->Node.IsEmpty())
  10606. {
  10607. Folders->Add(Bookmark->Node);
  10608. }
  10609. }
  10610. for (int Index = 0; Index < Folders->Count; Index++)
  10611. {
  10612. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10613. Item->Caption = Folders->Strings[Index];
  10614. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10615. Folders->Objects[Index] = Item.get();
  10616. Menu->Add(Item.release());
  10617. }
  10618. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10619. {
  10620. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10621. TTBCustomItem * Parent;
  10622. if (!Bookmark->Node.IsEmpty())
  10623. {
  10624. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10625. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10626. }
  10627. else
  10628. {
  10629. Parent = Menu;
  10630. }
  10631. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10632. Item->Caption = Bookmark->Name;
  10633. Item->ShortCut = Bookmark->ShortCut;
  10634. Item->OnClick = OnBookmarkClick;
  10635. Item->Tag = reinterpret_cast<int>(Bookmark);
  10636. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10637. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10638. {
  10639. // No hint for location profiles that are actually mere "bookmarks"
  10640. }
  10641. else
  10642. {
  10643. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10644. UnicodeString LongHint;
  10645. if (!Bookmark->Local.IsEmpty())
  10646. {
  10647. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10648. }
  10649. if (!Bookmark->Remote.IsEmpty())
  10650. {
  10651. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10652. }
  10653. AddToList(Hint, LongHint, L"|");
  10654. Item->Hint = Hint;
  10655. }
  10656. Parent->Add(Item.release());
  10657. }
  10658. }
  10659. AddMenuSeparator(Menu);
  10660. }
  10661. }
  10662. //---------------------------------------------------------------------------
  10663. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10664. {
  10665. Menu->Clear();
  10666. std::unique_ptr<TTBCustomItem> Item;
  10667. Item.reset(new TTBXItem(Owner));
  10668. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10669. Item->Options = Item->Options << tboDefault;
  10670. Menu->Add(Item.release());
  10671. AddMenuSeparator(Menu);
  10672. if (Terminal != NULL)
  10673. {
  10674. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10675. }
  10676. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10677. Item.reset(new TTBXItem(Owner));
  10678. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10679. Menu->Add(Item.release());
  10680. }
  10681. //---------------------------------------------------------------------------
  10682. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10683. {
  10684. bool Result = true;
  10685. bool Remote = !IsSideLocalBrowser(Side);
  10686. try
  10687. {
  10688. UnicodeString APath = Path;
  10689. if (Remote)
  10690. {
  10691. Terminal->ExceptionOnFail = true;
  10692. }
  10693. else
  10694. {
  10695. APath = ExpandEnvironmentVariables(Path);
  10696. }
  10697. try
  10698. {
  10699. DirView(Side)->Path = APath;
  10700. }
  10701. __finally
  10702. {
  10703. if (Remote)
  10704. {
  10705. Terminal->ExceptionOnFail = false;
  10706. }
  10707. }
  10708. }
  10709. catch (Exception & E)
  10710. {
  10711. if (!Remote || Terminal->Active)
  10712. {
  10713. ShowExtendedExceptionEx(Terminal, &E);
  10714. Result = false;
  10715. }
  10716. else
  10717. {
  10718. throw;
  10719. }
  10720. }
  10721. return Result;
  10722. }
  10723. //---------------------------------------------------------------------------
  10724. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10725. {
  10726. // Make sure there some feedback even when loading tiny local folders
  10727. TInstantOperationVisualizer Visualizer;
  10728. DirView(Side)->ReloadDirectory();
  10729. }
  10730. //---------------------------------------------------------------------------
  10731. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10732. {
  10733. if (Index == SessionsPageControl->TabIndex)
  10734. {
  10735. CloseTab();
  10736. }
  10737. else
  10738. {
  10739. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10740. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10741. TTerminalManager * Manager = TTerminalManager::Instance();
  10742. if (!Terminal->Active)
  10743. {
  10744. Manager->FreeTerminal(Terminal);
  10745. }
  10746. else
  10747. {
  10748. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10749. if (CanCloseQueue(Queue))
  10750. {
  10751. Manager->FreeTerminal(Terminal);
  10752. }
  10753. }
  10754. }
  10755. }
  10756. //---------------------------------------------------------------------------
  10757. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10758. {
  10759. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10760. if (Session == NULL)
  10761. {
  10762. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10763. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10764. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10765. //P = SessionsPageControl->ClientToScreen(P);
  10766. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10767. }
  10768. else
  10769. {
  10770. CloseSessionTab(Index);
  10771. }
  10772. }
  10773. //---------------------------------------------------------------------------
  10774. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10775. {
  10776. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10777. {
  10778. TInstantOperationVisualizer Visualizer;
  10779. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10780. // before we create a new data object
  10781. ClipboardClear();
  10782. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10783. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10784. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10785. // Intermediate variable to WORKAROUND a crash in compiler
  10786. TObjectList * ClipboardFakeMonitors =
  10787. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10788. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10789. if (FClipboardDragDropFilesEx.get() == NULL)
  10790. {
  10791. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10792. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10793. }
  10794. FClipboardDragDropFilesEx->FileList->Clear();
  10795. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10796. FClipboardDragDropFilesEx->CopyToClipboard();
  10797. FClipboardTerminal = Terminal;
  10798. // Need full paths, as cwd can be different once files are pasted
  10799. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10800. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10801. {
  10802. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10803. }
  10804. AppLogFmt(L"Created fake clipboard directory \"%s\" and started drive monitors", (FClipboardFakeDirectory));
  10805. }
  10806. }
  10807. //---------------------------------------------------------------------------
  10808. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10809. {
  10810. if (OpenClipboard(0))
  10811. {
  10812. AppLog(L"Clearing clipboard");
  10813. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10814. CloseClipboard();
  10815. }
  10816. }
  10817. //---------------------------------------------------------------------------
  10818. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10819. {
  10820. return !FClipboardFakeDirectory.IsEmpty();
  10821. }
  10822. //---------------------------------------------------------------------------
  10823. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10824. {
  10825. bool Deleted = RemoveDir(ApiPath(FClipboardFakeDirectory));
  10826. AppLogFmt(L"Clearing clipboard, deleted clipboard temporary folder \"%s\" - %d", (FClipboardFakeDirectory, int(Deleted)));
  10827. FClipboardFakeDirectory = UnicodeString();
  10828. FClipboardTerminal = NULL;
  10829. if (FDownloadingFromClipboard)
  10830. {
  10831. // We are called by the monitor, so attempt to release it would deadlock
  10832. FClipboardFakeMonitorsPendingReset = true;
  10833. }
  10834. else
  10835. {
  10836. FClipboardFakeMonitors.reset(NULL);
  10837. }
  10838. }
  10839. //---------------------------------------------------------------------------
  10840. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10841. {
  10842. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10843. {
  10844. ClipboardStop();
  10845. }
  10846. }
  10847. //---------------------------------------------------------------------------
  10848. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10849. {
  10850. TTerminalManager * Manager = TTerminalManager::Instance();
  10851. Manager->ActiveSession = FClipboardTerminal;
  10852. TTransferOperationParam Params;
  10853. Params.TargetDirectory = TargetDirectory;
  10854. Params.DragDrop = DragDrop;
  10855. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10856. }
  10857. //---------------------------------------------------------------------------
  10858. void TCustomScpExplorerForm::PasteFilesCleanupRetry(const UnicodeString & Target)
  10859. {
  10860. bool Removed = RemoveDir(ApiPath(Target));
  10861. AppLogFmt(L"Second attempt to delete pasted fake clipboard directory \"%s\" - %d", (Target, int(Removed)));
  10862. }
  10863. //---------------------------------------------------------------------------
  10864. void TCustomScpExplorerForm::PasteFiles()
  10865. {
  10866. // Guard against possible race conditions
  10867. if (!FClipboardPasteTarget.IsEmpty())
  10868. {
  10869. // ClipboardFakeCreated might be called while we are here, so make sure we finish operating on the same path we started with
  10870. UnicodeString Target = FClipboardPasteTarget;
  10871. FClipboardPasteTarget = EmptyStr;
  10872. // Can fail as it can be e.g. locked by AV, so we retry that later
  10873. bool Removed = RemoveDir(ApiPath(Target));
  10874. AppLogFmt(L"First attempt to delete pasted fake clipboard directory \"%s\" - %d", (Target, int(Removed)));
  10875. try
  10876. {
  10877. if (NonVisualDataModule->Busy)
  10878. {
  10879. AppLog(L"Is busy, cannot paste");
  10880. }
  10881. else
  10882. {
  10883. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10884. FClipboardFakeMonitorsPendingReset = false;
  10885. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10886. TAutoFlag Flag(FDownloadingFromClipboard);
  10887. AppLog(L"Downloading to paste target...");
  10888. ClipboardDownload(ExtractFilePath(Target), NoConfirmation, true);
  10889. AppLog(L"Downloaded to paste target...");
  10890. }
  10891. }
  10892. __finally
  10893. {
  10894. if (!Removed)
  10895. {
  10896. PasteFilesCleanupRetry(Target);
  10897. }
  10898. }
  10899. }
  10900. }
  10901. //---------------------------------------------------------------------------
  10902. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10903. {
  10904. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10905. // (as it pumps a Windows message queue while being shutted down)
  10906. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10907. // Is can happen that creation of our temporary directory is detected (even though we are creating it before starting the monitors).
  10908. // It tent do happen with later copy-pastes, probably because everything is cached already and monitors start quickly.
  10909. !IsPathToSameFile(FClipboardFakeDirectory, FileName) &&
  10910. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10911. {
  10912. AppLogFmt(L"Fake clipboard directory pasted to \"%s\"", (FileName));
  10913. FClipboardPasteTarget = FileName;
  10914. // Particularly when pasting to external HDD, the fake directory is strangely recreated if removed here in the monitor callback.
  10915. // Postponing removing (or actually whole pasting operation) seems to help.
  10916. PostMessage(Handle, WM_PASTE_FILES, 0, 0);
  10917. }
  10918. }
  10919. //---------------------------------------------------------------------------
  10920. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10921. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10922. {
  10923. TFileMasks::TParams MaskParams;
  10924. MaskParams.Size = Size;
  10925. MaskParams.Modification = Modification;
  10926. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10927. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10928. // TUnixDirView::ItemFullFileName returns trailing backslash for directories, what filemask cannot deal with
  10929. if (!Local && Directory)
  10930. {
  10931. FileName = UnixExcludeTrailingBackslash(FileName);
  10932. }
  10933. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10934. {
  10935. bool ImplicitMatch;
  10936. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10937. !ImplicitMatch)
  10938. {
  10939. Color = Iter->Color;
  10940. break;
  10941. }
  10942. }
  10943. }
  10944. //---------------------------------------------------------------------------
  10945. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10946. {
  10947. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10948. if (!DirView->IsEditing())
  10949. {
  10950. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10951. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10952. // while searching
  10953. if (Key == VK_BACK)
  10954. {
  10955. if (FIncrementalSearchState.Searching)
  10956. {
  10957. if (FIncrementalSearchState.Text.Length() <= 1)
  10958. {
  10959. ResetIncrementalSearch();
  10960. }
  10961. else
  10962. {
  10963. UnicodeString NewText = LeftStr(FIncrementalSearchState.Text, FIncrementalSearchState.Text.Length() - 1);
  10964. IncrementalSearch(NewText, false, false);
  10965. }
  10966. Key = 0;
  10967. }
  10968. }
  10969. }
  10970. }
  10971. //---------------------------------------------------------------------------
  10972. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10973. {
  10974. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10975. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10976. {
  10977. // Filter control sequences.
  10978. // When not searching yet, prefer use of the space for toggling file selection
  10979. // (so we cannot incrementally search for a string starting with a space).
  10980. if ((Key > VK_SPACE) ||
  10981. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && FIncrementalSearchState.Searching))
  10982. {
  10983. IncrementalSearch(FIncrementalSearchState.Text + Key, false, false);
  10984. Key = 0;
  10985. }
  10986. }
  10987. }
  10988. //---------------------------------------------------------------------------
  10989. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10990. {
  10991. if (FIncrementalSearchState.Searching)
  10992. {
  10993. FIncrementalSearchState.Reset();
  10994. DirView(osCurrent)->UpdateStatusBar();
  10995. }
  10996. }
  10997. //---------------------------------------------------------------------------
  10998. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10999. {
  11000. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  11001. if (Item == NULL)
  11002. {
  11003. MessageBeep(MB_ICONHAND);
  11004. }
  11005. else
  11006. {
  11007. TCustomDirView * ADirView = DirView(osCurrent);
  11008. {
  11009. TAutoNestingCounter Guard(FIncrementalSearching);
  11010. ADirView->FocusItem(Item);
  11011. if (ADirView->NortonLike == nlOff)
  11012. {
  11013. if ((ADirView->Selected != Item) ||
  11014. (ADirView->SelCount > 1))
  11015. {
  11016. ADirView->ClearSelection();
  11017. ADirView->Selected = Item;
  11018. }
  11019. }
  11020. }
  11021. FIncrementalSearchState.Searching = true;
  11022. FIncrementalSearchState.Text = Text;
  11023. Item->MakeVisible(false);
  11024. TListItem * NextItem = SearchFile(Text, true, Reverse);
  11025. FIncrementalSearchState.HaveNext = (NextItem != NULL) && (NextItem != Item);
  11026. ADirView->UpdateStatusBar();
  11027. }
  11028. }
  11029. //---------------------------------------------------------------------------
  11030. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  11031. {
  11032. int Index = Item->Index;
  11033. if (!Reverse)
  11034. {
  11035. Index++;
  11036. if (Index >= Item->Owner->Count)
  11037. {
  11038. Index = 0;
  11039. }
  11040. }
  11041. else
  11042. {
  11043. Index--;
  11044. if (Index < 0)
  11045. {
  11046. Index = Item->Owner->Count - 1;
  11047. }
  11048. }
  11049. return Item->Owner->Item[Index];
  11050. }
  11051. //---------------------------------------------------------------------------
  11052. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  11053. {
  11054. TCustomDirView * ADirView = DirView(osCurrent);
  11055. if (ADirView->Items->Count == 0)
  11056. {
  11057. return NULL;
  11058. }
  11059. else
  11060. {
  11061. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  11062. TListItem * Item = CurrentItem;
  11063. if (SkipCurrent)
  11064. {
  11065. Item = DebugNotNull(GetNextFile(Item, Reverse));
  11066. }
  11067. while (true)
  11068. {
  11069. bool Matches = false;
  11070. switch (WinConfiguration->PanelSearch)
  11071. {
  11072. case isNameStartOnly:
  11073. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item).SubString(1, Text.Length()), Text);
  11074. break;
  11075. case isName:
  11076. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item), Text);
  11077. break;
  11078. case isAll:
  11079. int ColCount = (ADirView->DirViewStyle == dvsReport) ? ADirView->ColProperties->Count : 1;
  11080. int Index = 0;
  11081. while ((Index < ColCount) && !Matches)
  11082. {
  11083. bool Visible = (ADirView->DirViewStyle == dvsReport) ? ADirView->ColProperties->Visible[Index] : true;
  11084. if (Visible)
  11085. {
  11086. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  11087. }
  11088. Index++;
  11089. }
  11090. break;
  11091. }
  11092. if (Matches)
  11093. {
  11094. return Item;
  11095. }
  11096. Item = GetNextFile(Item, Reverse);
  11097. if (Item == CurrentItem)
  11098. {
  11099. return NULL;
  11100. }
  11101. }
  11102. }
  11103. }
  11104. //---------------------------------------------------------------------------
  11105. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  11106. {
  11107. ResetIncrementalSearch();
  11108. }
  11109. //---------------------------------------------------------------------------
  11110. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  11111. {
  11112. if (Message.CharCode == VK_TAB)
  11113. {
  11114. if (!FIncrementalSearchState.Text.IsEmpty())
  11115. {
  11116. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  11117. bool Reverse = Shift.Contains(ssShift);
  11118. IncrementalSearch(FIncrementalSearchState.Text, true, Reverse);
  11119. Message.Result = 1;
  11120. return;
  11121. }
  11122. }
  11123. else if (Message.CharCode == VK_ESCAPE)
  11124. {
  11125. if (FIncrementalSearchState.Searching)
  11126. {
  11127. ResetIncrementalSearch();
  11128. Message.Result = 1;
  11129. return;
  11130. }
  11131. }
  11132. TForm::Dispatch(&Message);
  11133. }
  11134. //---------------------------------------------------------------------------
  11135. void __fastcall TCustomScpExplorerForm::Deactivate()
  11136. {
  11137. TForm::Deactivate();
  11138. // E.g. when switching to an internal editor window
  11139. ResetIncrementalSearch();
  11140. }
  11141. //---------------------------------------------------------------------------
  11142. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  11143. {
  11144. // When switching to another application
  11145. ResetIncrementalSearch();
  11146. }
  11147. //---------------------------------------------------------------------------
  11148. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  11149. {
  11150. ResetIncrementalSearch();
  11151. }
  11152. //---------------------------------------------------------------------------
  11153. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  11154. {
  11155. if (FIncrementalSearching <= 0)
  11156. {
  11157. ResetIncrementalSearch();
  11158. }
  11159. }
  11160. //---------------------------------------------------------------------------
  11161. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  11162. {
  11163. CountClicksForWindowPrint(this);
  11164. }
  11165. //---------------------------------------------------------------------------
  11166. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  11167. {
  11168. CenterReconnectToolbar();
  11169. }
  11170. //---------------------------------------------------------------------------
  11171. void TCustomScpExplorerForm::DoExploreFile(TCustomDirView * DirView, const UnicodeString & FileName)
  11172. {
  11173. if (!FileName.IsEmpty())
  11174. {
  11175. DirView->FocusByName(FileName);
  11176. if (DirView->ItemFocused != NULL)
  11177. {
  11178. DirView->ItemFocused->Selected = true;
  11179. }
  11180. }
  11181. }
  11182. //---------------------------------------------------------------------------
  11183. void __fastcall TCustomScpExplorerForm::ExploreFile(const UnicodeString & FileName)
  11184. {
  11185. DoExploreFile(RemoteDirView, FileName);
  11186. }
  11187. //---------------------------------------------------------------------------
  11188. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  11189. {
  11190. if (!FQueueStatusUpdating)
  11191. {
  11192. bool Refresh;
  11193. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  11194. {
  11195. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  11196. }
  11197. else
  11198. {
  11199. Refresh = (FQueueFileList->GetCount() > 0);
  11200. if (Refresh)
  11201. {
  11202. FQueueFileList->Clear();
  11203. }
  11204. }
  11205. int Count = FQueueFileList->GetCount();
  11206. if (QueueFileList->Items->Count != Count)
  11207. {
  11208. DebugAssert(Refresh);
  11209. QueueFileList->Items->Count = Count;
  11210. }
  11211. if (Refresh)
  11212. {
  11213. QueueFileList->Invalidate();
  11214. }
  11215. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  11216. }
  11217. }
  11218. //---------------------------------------------------------------------------
  11219. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  11220. {
  11221. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  11222. }
  11223. //---------------------------------------------------------------------------
  11224. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  11225. {
  11226. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  11227. // See TSynchronizeChecklistDialog::UpdateTimer
  11228. if (QueueFileList->Items->Count > 0)
  11229. {
  11230. QueueFileList->Items->Item[0]->MakeVisible(false);
  11231. }
  11232. UpdateQueueFileList();
  11233. }
  11234. //---------------------------------------------------------------------------
  11235. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  11236. {
  11237. Active = QueueView3->Focused() || QueueFileList->Focused();
  11238. }
  11239. //---------------------------------------------------------------------------
  11240. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  11241. {
  11242. QueueLabelUpdateStatus();
  11243. }
  11244. //---------------------------------------------------------------------------
  11245. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  11246. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  11247. {
  11248. int State = FQueueFileList->GetState(Item->Index);
  11249. if (State == qfsProcessed)
  11250. {
  11251. Sender->Canvas->Font->Color = clGrayText;
  11252. }
  11253. }
  11254. //---------------------------------------------------------------------------
  11255. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  11256. {
  11257. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  11258. QueueFileList->Column[0]->Width =
  11259. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  11260. }
  11261. //---------------------------------------------------------------------------
  11262. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  11263. {
  11264. QueueFileListColumnAutoSize();
  11265. }
  11266. //---------------------------------------------------------------------------
  11267. void __fastcall TCustomScpExplorerForm::CloseApp()
  11268. {
  11269. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  11270. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  11271. // So we clear the flag here.
  11272. DebugAssert(NonVisualDataModule->Busy);
  11273. NonVisualDataModule->EndBusy();
  11274. try
  11275. {
  11276. Close();
  11277. }
  11278. __finally
  11279. {
  11280. NonVisualDataModule->StartBusy();
  11281. }
  11282. }
  11283. //---------------------------------------------------------------------------
  11284. bool TCustomScpExplorerForm::IsAvailableTerminal(TTerminal * Terminal)
  11285. {
  11286. return TTerminalManager::Instance()->IsAvailableTerminal(Terminal);
  11287. }
  11288. //---------------------------------------------------------------------------
  11289. bool TCustomScpExplorerForm::HasAvailableTerminal()
  11290. {
  11291. return IsAvailableTerminal(Terminal);
  11292. }
  11293. //---------------------------------------------------------------------------
  11294. void TCustomScpExplorerForm::LocalLocalCopy(
  11295. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  11296. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  11297. {
  11298. DebugFail();
  11299. }
  11300. //---------------------------------------------------------------------------
  11301. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  11302. {
  11303. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  11304. }
  11305. //---------------------------------------------------------------------------
  11306. void TCustomScpExplorerForm::UpdateTabsSize()
  11307. {
  11308. // 1) Changing tab list triggers the OnResize.
  11309. // 2) Is called in TForm constructor, when we are not ready to call SessionListChanged (particularly when starting with colored session)
  11310. if (!FUpdatingSessionTabs && (FSessionColors != NULL))
  11311. {
  11312. // UpdateTabsCaptionTruncation should be enough
  11313. SessionListChanged(true);
  11314. }
  11315. }
  11316. //---------------------------------------------------------------------------
  11317. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  11318. {
  11319. UpdateTabsSize();
  11320. }
  11321. //---------------------------------------------------------------------------
  11322. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  11323. {
  11324. if (ASession == ManagedSession)
  11325. {
  11326. UpdateSession(ASession);
  11327. }
  11328. TSessionData * Data = ASession->StateData;
  11329. UnicodeString Result;
  11330. if (Side == osLocal)
  11331. {
  11332. Result = Data->LocalDirectory;
  11333. }
  11334. else if (DebugAlwaysTrue(Side == osOther))
  11335. {
  11336. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  11337. }
  11338. return Result;
  11339. }
  11340. //---------------------------------------------------------------------------
  11341. UnicodeString TCustomScpExplorerForm::GetTabHintSessionDetails(TManagedTerminal * ASession)
  11342. {
  11343. UnicodeString Title = TTerminalManager::Instance()->GetSessionTitle(ASession, false);
  11344. UnicodeString Result = ASession->SessionData->DefaultSessionName;
  11345. if (Title == Result)
  11346. {
  11347. Result = EmptyStr;
  11348. }
  11349. else
  11350. {
  11351. Result += L"\n";
  11352. }
  11353. return Result;
  11354. }
  11355. //---------------------------------------------------------------------------
  11356. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  11357. {
  11358. UnicodeString Result = GetTabHintSessionDetails(ASession);
  11359. if (!ASession->Active)
  11360. {
  11361. Result += LoadStr(STATUS_NOT_CONNECTED2);
  11362. }
  11363. else
  11364. {
  11365. Result += GetSessionPath(ASession, osRemote);
  11366. }
  11367. return Result;
  11368. }
  11369. //---------------------------------------------------------------------------
  11370. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  11371. {
  11372. return LoadStr(NEW_REMOTE_TAB_HINT);
  11373. }
  11374. //---------------------------------------------------------------------------
  11375. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  11376. {
  11377. if (Index >= 0)
  11378. {
  11379. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  11380. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  11381. if (TabSession == NULL)
  11382. {
  11383. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  11384. if (NewTabTabCaption != TabSheet->Caption)
  11385. {
  11386. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  11387. }
  11388. }
  11389. else
  11390. {
  11391. TTerminalManager * Manager = TTerminalManager::Instance();
  11392. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  11393. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  11394. }
  11395. }
  11396. }
  11397. //---------------------------------------------------------------------------
  11398. typedef std::vector<TListItem *> TListItemsVector;
  11399. struct TCalculateSizeOperation
  11400. {
  11401. TCalculateSizeStats Stats;
  11402. TListItemsVector ListItems;
  11403. size_t Index;
  11404. };
  11405. //---------------------------------------------------------------------------
  11406. void TCustomScpExplorerForm::CalculateDirectorySizes(TOperationSide Side)
  11407. {
  11408. TCalculateSizeOperation CalculateSizeOperation;
  11409. Side = GetSide(Side);
  11410. TCustomDirView * ADirView = DirView(Side);
  11411. bool LocalSide = IsSideLocalBrowser(Side);
  11412. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, LocalSide, NULL, true));
  11413. std::unique_ptr<TStrings> FileList(new TStringList());
  11414. for (int Index = 0; Index < AllFileList->Count; Index++)
  11415. {
  11416. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  11417. if (ADirView->ItemIsDirectory(Item))
  11418. {
  11419. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  11420. CalculateSizeOperation.ListItems.push_back(Item);
  11421. }
  11422. }
  11423. CalculateSizeOperation.Index = 0;
  11424. __int64 Size = 0;
  11425. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation, &CalculateSizeOperation);
  11426. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  11427. FOnFileOperationFinished = DirectorySizeCalculated;
  11428. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation, foCalculateSize);
  11429. TCalculatedSizes CalculatedSizes;
  11430. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  11431. if (LocalSide)
  11432. {
  11433. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide, Side);
  11434. ManagedSession->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  11435. }
  11436. else
  11437. {
  11438. TCalculateSizeParams Params;
  11439. Params.Stats = &CalculateSizeOperation.Stats;
  11440. Params.UseCache = true;
  11441. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  11442. }
  11443. }
  11444. //---------------------------------------------------------------------------
  11445. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(
  11446. TOperationSide Side, const UnicodeString & FileName, bool Success, bool NotCancelled)
  11447. {
  11448. DebugAssert(FCalculateSizeOperation != NULL);
  11449. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  11450. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  11451. // optimization
  11452. bool GotExpectedItem =
  11453. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  11454. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  11455. int Index = -1;
  11456. if (DebugAlwaysTrue(GotExpectedItem))
  11457. {
  11458. Index = FCalculateSizeOperation->Index;
  11459. FCalculateSizeOperation->Index++;
  11460. }
  11461. else
  11462. {
  11463. TListItemsVector::iterator I =
  11464. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  11465. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  11466. {
  11467. Index = I - FCalculateSizeOperation->ListItems.begin();
  11468. }
  11469. }
  11470. if (DebugAlwaysTrue(Index >= 0) && Success && NotCancelled)
  11471. {
  11472. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  11473. ADirView->SetItemCalculatedSize(Item, Size);
  11474. }
  11475. }
  11476. //---------------------------------------------------------------------------
  11477. void __fastcall TCustomScpExplorerForm::MessageDockRequestDock(TObject *, TTBCustomDockableWindow *, bool & Accept)
  11478. {
  11479. Accept = false;
  11480. }
  11481. //---------------------------------------------------------------------------
  11482. void TCustomScpExplorerForm::AutoSizeColumns(TOperationSide Side)
  11483. {
  11484. // Send Ctrl+Gray+ to the list view
  11485. TKeyboardState KeyState;
  11486. GetKeyboardState(KeyState);
  11487. TKeyboardState NewKeyState;
  11488. memset(NewKeyState, 0, sizeof(NewKeyState));
  11489. NewKeyState[VK_CONTROL] = 0x81;
  11490. SetKeyboardState(NewKeyState);
  11491. SendMessage(DirView(Side)->Handle, WM_KEYDOWN, VK_ADD, 0);
  11492. SetKeyboardState(KeyState);
  11493. }
  11494. //---------------------------------------------------------------------------
  11495. void TCustomScpExplorerForm::QueueResetLayoutColumns()
  11496. {
  11497. LoadListViewStr(QueueView3, QueueViewLayoutDefault);
  11498. }
  11499. //---------------------------------------------------------------------------
  11500. void * TCustomScpExplorerForm::SaveFocus()
  11501. {
  11502. return NULL;
  11503. }
  11504. //---------------------------------------------------------------------------
  11505. void TCustomScpExplorerForm::RestoreFocus(void * Focus)
  11506. {
  11507. DebugUsedParam(Focus);
  11508. DebugAssert(Focus == NULL);
  11509. }
  11510. //---------------------------------------------------------------------------
  11511. void TCustomScpExplorerForm::IncrementalSearchStart()
  11512. {
  11513. TCustomDirView * ADirView = DirView(osCurrent);
  11514. ADirView->SetFocus();
  11515. if (!FIncrementalSearchState.Searching)
  11516. {
  11517. FIncrementalSearchState.Searching = true;
  11518. FIncrementalSearchState.HaveNext = false;
  11519. DebugAssert(FIncrementalSearchState.Text.IsEmpty());
  11520. ADirView->UpdateStatusBar();
  11521. }
  11522. }
  11523. //---------------------------------------------------------------------------
  11524. void __fastcall TCustomScpExplorerForm::RemoteDirViewThumbnailNeeded(
  11525. TUnixDirView *, TListItem * Item, TRemoteFile * File, const TSize & Size, TBitmap *& Bitmap)
  11526. {
  11527. // though when downloading to temporary folder, encryption should not be a problem
  11528. if (Terminal->IsCapable[fcBackgroundTransfers] &&
  11529. !File->IsDirectory)
  11530. {
  11531. UnicodeString FileName = File->FullFileName;
  11532. TFileMasks::TParams MaskParams;
  11533. MaskParams.Size = File->Size;
  11534. MaskParams.Modification = File->Modification;
  11535. if ((File->Size <= static_cast<__int64>(WinConfiguration->RemoteThumbnailSizeLimit) * 1024) &&
  11536. FRemoteThumbnailMask.Matches(FileName, false, File->IsDirectory, &MaskParams))
  11537. {
  11538. Bitmap = TTerminalManager::Instance()->ThumbnailNeeded(Terminal, Item->Index, File, Size);
  11539. }
  11540. }
  11541. }
  11542. //---------------------------------------------------------------------------
  11543. TThumbnailDownloadQueueItem * TCustomScpExplorerForm::AddThumbnailDownloadQueueItem(TManagedTerminal * ATerminal)
  11544. {
  11545. DebugAssert(ATerminal == Terminal);
  11546. TCopyParamType CopyParam = TemporaryFileCopyParam();
  11547. CopyParam.TransferMode = tmBinary;
  11548. UnicodeString SourceDir = ATerminal->CurrentDirectory;
  11549. UnicodeString TempDir, RootTempDir;
  11550. TemporaryDirectoryForRemoteFiles(SourceDir, CopyParam, true, TempDir, RootTempDir);
  11551. TTerminalQueue * AQueue = TTerminalManager::Instance()->FindQueueForTerminal(ATerminal);
  11552. DebugAssert((Queue == NULL) || (AQueue == Queue)); // we might get here before queue is set
  11553. TThumbnailDownloadQueueItem * QueueItem =
  11554. new TThumbnailDownloadQueueItem(this, Terminal, SourceDir, TempDir, &CopyParam);
  11555. AddQueueItem(AQueue, QueueItem, ATerminal);
  11556. return QueueItem;
  11557. }
  11558. //---------------------------------------------------------------------------
  11559. enum { qccRemoteItemVisible, qccRemoteItemRedraw };
  11560. typedef std::pair<int, UnicodeString> TRemoteItemVisibleData;
  11561. //---------------------------------------------------------------------------
  11562. void TCustomScpExplorerForm::PostThumbnailVisibleQueueQuery(int Index, const UnicodeString & FileName)
  11563. {
  11564. // The queue might be destroyed before the WM_QUEUE_CALLBACK is processed
  11565. TRemoteItemVisibleData * Data = new TRemoteItemVisibleData();
  11566. Data->first = Index;
  11567. Data->second = FileName;
  11568. PostMessage(Handle, WM_QUEUE_CALLBACK, qccRemoteItemVisible, reinterpret_cast<WPARAM>(Data));
  11569. }
  11570. //---------------------------------------------------------------------------
  11571. void TCustomScpExplorerForm::PostThumbnailDrawRequest(int Index)
  11572. {
  11573. PostMessage(Handle, WM_QUEUE_CALLBACK, qccRemoteItemRedraw, Index);
  11574. }
  11575. //---------------------------------------------------------------------------
  11576. void TCustomScpExplorerForm::WMQueueCallback(TMessage & Message)
  11577. {
  11578. int Command = Message.WParam;
  11579. if (Command == qccRemoteItemVisible)
  11580. {
  11581. TRemoteItemVisibleData * Data = reinterpret_cast<TRemoteItemVisibleData *>(Message.LParam);
  11582. int Index = Data->first;
  11583. UnicodeString FileName = Data->second;
  11584. delete Data;
  11585. TGuard Guard(Terminal->ThumbnailsSection.get());
  11586. bool Visible = False;
  11587. if (Terminal->ThumbnailsEnabled &&
  11588. // though we should better also disable thumbnails once we switch to different view style
  11589. (RemoteDirView->DirViewStyle == dvsThumbnail))
  11590. {
  11591. // The message might be processed, while we are already loading a different directory
  11592. if ((Index >= 0) &&
  11593. (Index < RemoteDirView->Items->Count))
  11594. {
  11595. TListItem * Item = RemoteDirView->Items->Item[Index];
  11596. Visible =
  11597. (RemoteDirView->ItemFullFileName(Item) == FileName) &&
  11598. ListView_IsItemVisible(RemoteDirView->Handle, Index);
  11599. }
  11600. }
  11601. Terminal->ThumbnailVisible(Index, FileName, Visible);
  11602. }
  11603. else if (Command == qccRemoteItemRedraw)
  11604. {
  11605. int Index = Message.LParam;
  11606. if ((Index >= 0) &&
  11607. (Index < RemoteDirView->Items->Count))
  11608. {
  11609. RemoteDirView->InvalidateItem(RemoteDirView->Items->Item[Index]);
  11610. }
  11611. }
  11612. }
  11613. //---------------------------------------------------------------------------
  11614. void TCustomScpExplorerForm::InitializeRemoteThumbnailMask()
  11615. {
  11616. UnicodeString DefaultRemoteThumbnailMask = L"*.jpg;*.jpeg;*.gif;*.png;*.svg;*.bmp;*.raw;*.ico;*.heic;*.webp";
  11617. UnicodeString RemoteThumbnailMask = WinConfiguration->RemoteThumbnailMask;
  11618. if (RemoteThumbnailMask.IsEmpty() ||
  11619. StartsStr(L"+", RemoteThumbnailMask) || StartsStr(L"-", RemoteThumbnailMask) || StartsStr(L"*", RemoteThumbnailMask))
  11620. {
  11621. std::unique_ptr<TStrings> RemoteThumbnailMasks(new TStringList());
  11622. while (!DefaultRemoteThumbnailMask.IsEmpty())
  11623. {
  11624. RemoteThumbnailMasks->Add(CutToChar(DefaultRemoteThumbnailMask, L';', true));
  11625. }
  11626. RemoteThumbnailMasks.reset(ProcessFeatures(RemoteThumbnailMasks.get(), RemoteThumbnailMask));
  11627. RemoteThumbnailMask = EmptyStr;
  11628. for (int Index = 0; Index < RemoteThumbnailMasks->Count; Index++)
  11629. {
  11630. AddToList(RemoteThumbnailMask, RemoteThumbnailMasks->Strings[Index], L";");
  11631. }
  11632. }
  11633. FRemoteThumbnailMask.Masks = RemoteThumbnailMask;
  11634. }
  11635. //---------------------------------------------------------------------------
  11636. void __fastcall TCustomScpExplorerForm::RemoteDirViewStartLoading(TObject *)
  11637. {
  11638. Terminal->StartLoadingDirectory();
  11639. }
  11640. //---------------------------------------------------------------------------
  11641. void __fastcall TCustomScpExplorerForm::RemoteDirViewStartReading(TObject *)
  11642. {
  11643. Terminal->DisableThumbnails();
  11644. }
  11645. //---------------------------------------------------------------------------
  11646. void TCustomScpExplorerForm::ChangeDirViewStyle(TOperationSide Side, TDirViewStyle DirViewStyle)
  11647. {
  11648. TCustomDirView * ADirView = DirView(Side);
  11649. if (ADirView->DirViewStyle != DirViewStyle)
  11650. {
  11651. if (DirViewStyle == dvsThumbnail)
  11652. {
  11653. if (dynamic_cast<TUnixDirView *>(ADirView) != NULL)
  11654. {
  11655. Configuration->Usage->Inc(L"ThumbnailsRemote");
  11656. }
  11657. else
  11658. {
  11659. Configuration->Usage->Inc(L"ThumbnailsLocal");
  11660. }
  11661. }
  11662. ADirView->DirViewStyle = DirViewStyle;
  11663. UpdateControls();
  11664. }
  11665. }