CustomScpExplorer.cpp 368 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <Bookmarks.h>
  8. #include <Interface.h>
  9. #include <Exceptions.h>
  10. #include <CoreMain.h>
  11. #include <FileSystems.h>
  12. #include <TextsCore.h>
  13. #include <TextsWin.h>
  14. #include <HelpWin.h>
  15. #include <VCLCommon.h>
  16. #include <Progress.h>
  17. #include <SynchronizeProgress.h>
  18. #include <DragExt.h>
  19. #include <WinApi.h>
  20. #include "GUITools.h"
  21. #include "NonVisual.h"
  22. #include "Glyphs.h"
  23. #include "Tools.h"
  24. #include "WinConfiguration.h"
  25. #include "TerminalManager.h"
  26. #include "EditorManager.h"
  27. #include "ProgParams.h"
  28. #include "Setup.h"
  29. #include <Consts.hpp>
  30. #include <DateUtils.hpp>
  31. #include <TB2Common.hpp>
  32. #include <DirectoryMonitor.hpp>
  33. #include <System.IOUtils.hpp>
  34. //---------------------------------------------------------------------------
  35. #pragma package(smart_init)
  36. #pragma link "CustomDirView"
  37. #pragma link "CustomUnixDirView"
  38. #pragma link "IEListView"
  39. #pragma link "NortonLikeListView"
  40. #pragma link "UnixDirView"
  41. #pragma link "CustomDriveView"
  42. #pragma link "UnixDriveView"
  43. #pragma link "CustomDriveView"
  44. #pragma link "TB2Dock"
  45. #pragma link "TBXStatusBars"
  46. #pragma link "TB2Item"
  47. #pragma link "TB2Toolbar"
  48. #pragma link "ThemePageControl"
  49. #pragma link "PathLabel"
  50. #pragma resource "*.dfm"
  51. //---------------------------------------------------------------------------
  52. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  53. static const int SessionPanelCount = 3;
  54. //---------------------------------------------------------------------------
  55. class TMutexGuard
  56. {
  57. public:
  58. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  59. int Timeout = 5000)
  60. {
  61. FMutex = NULL;
  62. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  63. if (WaitResult == WAIT_TIMEOUT)
  64. {
  65. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  66. }
  67. else
  68. {
  69. FMutex = AMutex;
  70. }
  71. }
  72. ~TMutexGuard()
  73. {
  74. if (FMutex != NULL)
  75. {
  76. ReleaseMutex(FMutex);
  77. }
  78. }
  79. private:
  80. HANDLE FMutex;
  81. };
  82. //---------------------------------------------------------------------------
  83. class TWindowLock
  84. {
  85. public:
  86. TWindowLock(TCustomScpExplorerForm * Form) :
  87. FForm(Form)
  88. {
  89. FForm->LockWindow();
  90. }
  91. ~TWindowLock()
  92. {
  93. FForm->UnlockWindow();
  94. }
  95. private:
  96. TCustomScpExplorerForm * FForm;
  97. };
  98. //---------------------------------------------------------------------------
  99. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  100. FForm(Form)
  101. {
  102. FForm->BatchStart(FBatchStorage);
  103. }
  104. //---------------------------------------------------------------------------
  105. TAutoBatch::~TAutoBatch()
  106. {
  107. FForm->BatchEnd(FBatchStorage);
  108. }
  109. //---------------------------------------------------------------------------
  110. struct TTransferOperationParam
  111. {
  112. TTransferOperationParam();
  113. UnicodeString TargetDirectory;
  114. bool Temp;
  115. bool DragDrop;
  116. int Options;
  117. TAutoSwitch Queue;
  118. };
  119. //---------------------------------------------------------------------------
  120. TTransferOperationParam::TTransferOperationParam()
  121. {
  122. Temp = false;
  123. DragDrop = false;
  124. Options = 0;
  125. Queue = asAuto;
  126. }
  127. //---------------------------------------------------------------------------
  128. class TTransferPresetNoteData : public TObject
  129. {
  130. public:
  131. UnicodeString Message;
  132. };
  133. //---------------------------------------------------------------------------
  134. class TTerminalNoteData : public TObject
  135. {
  136. public:
  137. TManagedTerminal * Terminal;
  138. };
  139. //---------------------------------------------------------------------------
  140. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  141. FFormRestored(false),
  142. TForm(Owner)
  143. {
  144. FInvalid = false;
  145. FCurrentSide = osRemote;
  146. FEverShown = false;
  147. FDocks = new TList();
  148. InitControls();
  149. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  150. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  151. NonVisualDataModule->ScpExplorer = this;
  152. RestoreParams();
  153. ConfigurationChanged();
  154. RemoteDirView->Invalidate();
  155. FAutoOperation = false;
  156. FOnFileOperationFinished = NULL;
  157. FForceExecution = false;
  158. FIgnoreNextDialogChar = 0;
  159. FErrorList = NULL;
  160. FSynchronizeProgressForm = NULL;
  161. FProgressForm = NULL;
  162. FRefreshLocalDirectory = false;
  163. FRefreshRemoteDirectory = false;
  164. FDDExtMapFile = NULL;
  165. // CreateMutexW keeps failing with ERROR_NOACCESS
  166. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  167. DebugAssert(FDDExtMutex != NULL);
  168. FDDTargetControl = NULL;
  169. FDelayedDeletionTimer = NULL;
  170. FDelayedDeletionList = new TStringList();
  171. FDDFileList = NULL;
  172. FPendingTempSpaceWarn = false;
  173. FCapturedLog = NULL;
  174. FDragDropOperation = false;
  175. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  176. FAllowTransferPresetAutoSelect = true;
  177. FSessionChanging = false;
  178. FCopyParamDefault = L"";
  179. FSynchronizeController = NULL;
  180. FPendingQueueActionItem = NULL;
  181. FLockLevel = 0;
  182. FLockSuspendLevel = 0;
  183. FDisabledOnLockSuspend = false;
  184. FAlternativeDelete = false;
  185. FTrayIcon = new ::TTrayIcon(0);
  186. FTrayIcon->OnClick = TrayIconClick;
  187. FMaxQueueLength = 0;
  188. FLastContextPopupScreenPoint = TPoint(-1, -1);
  189. FTransferResumeList = NULL;
  190. FMoveToQueue = false;
  191. FStandaloneEditing = false;
  192. FOnFeedSynchronizeError = NULL;
  193. FNeedSession = false;
  194. FDoNotIdleCurrentTerminal = 0;
  195. FIncrementalSearching = 0;
  196. FIncrementalSearchHaveNext = false;
  197. FQueueFileList.reset(new TQueueFileList());
  198. FProgressSide = osCurrent;
  199. FEditorManager = new TEditorManager();
  200. FEditorManager->OnFileChange = ExecutedFileChanged;
  201. FEditorManager->OnFileReload = ExecutedFileReload;
  202. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  203. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  204. FLocalEditors = new TList();
  205. FQueueStatus = NULL;
  206. FQueueStatusSection = new TCriticalSection();
  207. FQueueStatusInvalidated = false;
  208. FQueueItemInvalidated = false;
  209. FQueueStatusUpdating = false;
  210. FQueueActedItem = NULL;
  211. FQueueController = new TQueueController(QueueView3);
  212. FUserActionTimer = new TTimer(this);
  213. FUserActionTimer->Enabled = false;
  214. FUserActionTimer->Interval = 10;
  215. FUserActionTimer->OnTimer = UserActionTimer;
  216. FNotes = new TStringList();
  217. FNoteTimer = new TTimer(this);
  218. FNoteTimer->Enabled = false;
  219. FNoteTimer->OnTimer = NoteTimer;
  220. FOnNoteClick = NULL;
  221. FOle32Library = LoadLibrary(L"Ole32.dll");
  222. FDragMoveCursor = FOle32Library != NULL ?
  223. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  224. UseSystemSettings(this);
  225. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  226. static_cast<TObject*>(GetComponent(fcTransferList)));
  227. DebugAssert(TransferList != NULL);
  228. FTransferListHoverIndex = -1;
  229. TransferList->OnChange = TransferListChange;
  230. TransferList->OnDrawItem = TransferListDrawItem;
  231. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  232. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  233. UseDesktopFont(SessionsPageControl);
  234. UpdateSessionsPageControlHeight();
  235. UseDesktopFont(RemoteDirView);
  236. UseDesktopFont(RemoteDriveView);
  237. UseDesktopFont(QueueView3);
  238. UseDesktopFont(QueueFileList);
  239. UseDesktopFont(QueueLabel);
  240. UseDesktopFont(RemoteStatusBar);
  241. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  242. QueueSplitter->ShowHint = true;
  243. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  244. QueueFileListSplitter->ShowHint = true;
  245. RemotePanelSplitter->ShowHint = true;
  246. UpdateImages();
  247. FCustomCommandMenu = CreateTBXPopupMenu(this);
  248. FCustomCommandLocalFileList = NULL;
  249. FCustomCommandRemoteFileList = NULL;
  250. FSessionColors = new TPngImageList(this);
  251. FSessionColors->ColorDepth = cd32Bit;
  252. AddFixedSessionImages();
  253. SessionsPageControl->Images = FSessionColors;
  254. UpdateQueueLabel();
  255. CreateHiddenWindow();
  256. StartUpdates();
  257. }
  258. //---------------------------------------------------------------------------
  259. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  260. {
  261. FInvalid = true;
  262. // this has to be one of the very first things to do
  263. StopUpdateThread();
  264. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  265. TTerminalManager::Instance()->ScpExplorer = NULL;
  266. delete FCustomCommandLocalFileList;
  267. delete FCustomCommandRemoteFileList;
  268. delete FCustomCommandMenu;
  269. delete FTrayIcon;
  270. FTrayIcon = NULL;
  271. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  272. delete FEditorManager;
  273. ForceCloseLocalEditors();
  274. delete FLocalEditors;
  275. HideFileFindDialog();
  276. if (FDelayedDeletionTimer)
  277. {
  278. DoDelayedDeletion(NULL);
  279. SAFE_DESTROY(FDelayedDeletionTimer);
  280. }
  281. SAFE_DESTROY(FDelayedDeletionList);
  282. // sometimes we do not get DDEnd so the list is not released
  283. SAFE_DESTROY(FDDFileList);
  284. DebugAssert(FSynchronizeController == NULL);
  285. CloseHandle(FDDExtMutex);
  286. FDDExtMutex = NULL;
  287. FreeLibrary(FOle32Library);
  288. FOle32Library = NULL;
  289. FDragMoveCursor = NULL;
  290. DebugAssert(!FErrorList);
  291. if (FEverShown)
  292. {
  293. // when window is never shown (like when running command-line operation),
  294. // particularly window site is not restored correctly (BoundsRect value set
  295. // in RestoreForm gets lost during handle allocation), so we do not want
  296. // it to be stored
  297. StoreParams();
  298. }
  299. ManagedSession = NULL;
  300. Queue = NULL;
  301. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  302. NonVisualDataModule->ScpExplorer = NULL;
  303. delete FQueueController;
  304. FQueueController = NULL;
  305. delete FQueueStatusSection;
  306. FQueueStatusSection = NULL;
  307. delete FQueueStatus;
  308. FQueueStatus = NULL;
  309. delete FUserActionTimer;
  310. FUserActionTimer = NULL;
  311. delete FNoteTimer;
  312. delete FNotes;
  313. delete FNoteData;
  314. SAFE_DESTROY(FDocks);
  315. SessionsPageControl->Images = NULL;
  316. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  317. SAFE_DESTROY(FSessionColors);
  318. SAFE_DESTROY(FSessionsDragDropFilesEx);
  319. SAFE_DESTROY(FHistoryMenu[0][0]);
  320. SAFE_DESTROY(FHistoryMenu[0][1]);
  321. SAFE_DESTROY(FHistoryMenu[1][0]);
  322. SAFE_DESTROY(FHistoryMenu[1][1]);
  323. if (FHiddenWindow != NULL)
  324. {
  325. DestroyWindow(FHiddenWindow);
  326. FHiddenWindow = NULL;
  327. }
  328. }
  329. //---------------------------------------------------------------------------
  330. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  331. {
  332. std::unique_ptr<TSessionData> Data;
  333. if (!Session.IsEmpty())
  334. {
  335. bool DefaultsOnly;
  336. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  337. }
  338. TTerminalManager * Manager = TTerminalManager::Instance();
  339. for (int Index = 0; Index < Manager->Count; Index++)
  340. {
  341. TTerminal * Terminal = Manager->Sessions[Index];
  342. if (Session.IsEmpty() ||
  343. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  344. {
  345. if (Path.IsEmpty())
  346. {
  347. Terminal->ClearCaches();
  348. }
  349. else
  350. {
  351. Terminal->DirectoryModified(Path, true);
  352. }
  353. }
  354. }
  355. // We should flag a pending refresh for the background terminals or busy foreground terminals
  356. if (HasActiveTerminal() &&
  357. CanCommandLineFromAnotherInstance() &&
  358. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  359. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  360. {
  361. Terminal->ReloadDirectory();
  362. }
  363. }
  364. //---------------------------------------------------------------------------
  365. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  366. {
  367. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  368. size_t MessageSize = sizeof(TCopyDataMessage);
  369. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  370. if (Result)
  371. {
  372. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  373. Result = (Message.Version == TCopyDataMessage::Version1);
  374. if (Result)
  375. {
  376. switch (Message.Command)
  377. {
  378. case TCopyDataMessage::CommandCanCommandLine:
  379. Result = CanCommandLineFromAnotherInstance();
  380. break;
  381. case TCopyDataMessage::CommandCommandLine:
  382. {
  383. UnicodeString CommandLine(Message.CommandLine);
  384. Result = CommandLineFromAnotherInstance(CommandLine);
  385. }
  386. break;
  387. case TCopyDataMessage::RefreshPanel:
  388. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  389. break;
  390. case TCopyDataMessage::MainWindowCheck:
  391. Result = true;
  392. break;
  393. default:
  394. Result = false;
  395. break;
  396. }
  397. }
  398. }
  399. Message.Result = Result ? 1 : 0;
  400. }
  401. //---------------------------------------------------------------------------
  402. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  403. {
  404. WNDCLASS WindowClass = {0};
  405. WindowClass.lpfnWndProc = DefWindowProc;
  406. WindowClass.hInstance = HInstance;
  407. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  408. FHiddenWindow = NULL;
  409. if (RegisterClass(&WindowClass))
  410. {
  411. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  412. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  413. }
  414. }
  415. //---------------------------------------------------------------------------
  416. bool __fastcall TCustomScpExplorerForm::CanConsole()
  417. {
  418. return
  419. HasActiveTerminal() &&
  420. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  421. }
  422. //---------------------------------------------------------------------------
  423. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  424. {
  425. bool Result = Visible && !NonVisualDataModule->Busy;
  426. return Result;
  427. }
  428. //---------------------------------------------------------------------------
  429. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  430. const UnicodeString & CommandLine)
  431. {
  432. TProgramParams Params(CommandLine);
  433. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  434. if (Result)
  435. {
  436. NonVisualDataModule->StartBusy();
  437. try
  438. {
  439. // this action is initiated from another process,
  440. // so it's likely that our window is not visible,
  441. // and user won't see what is going on
  442. Application->BringToFront();
  443. // reload sessions as we may be asked to open a session
  444. // just stored by another instance
  445. StoredSessions->Reload();
  446. UnicodeString SessionName = Params.Param[1];
  447. std::unique_ptr<TObjectList> DataList(new TObjectList());
  448. UnicodeString DownloadFile; // unused
  449. try
  450. {
  451. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  452. }
  453. catch (EAbort &)
  454. {
  455. // Happens when opening session in PuTTY and in other situations,
  456. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  457. // to process the command-line on its own (or to pass it to yet another instance)
  458. }
  459. if (DataList->Count > 0)
  460. {
  461. TTerminalManager * Manager = TTerminalManager::Instance();
  462. Manager->ActiveSession = Manager->NewTerminals(DataList.get());
  463. }
  464. }
  465. __finally
  466. {
  467. NonVisualDataModule->EndBusy();
  468. }
  469. }
  470. return Result;
  471. }
  472. //---------------------------------------------------------------------------
  473. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  474. {
  475. TManagedTerminal * Result;
  476. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  477. {
  478. Result = FManagedSession;
  479. }
  480. else
  481. {
  482. Result = NULL;
  483. }
  484. return Result;
  485. }
  486. //---------------------------------------------------------------------------
  487. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  488. {
  489. if (FManagedSession != value)
  490. {
  491. SessionChanging();
  492. DoSetManagedSession(value, false);
  493. }
  494. }
  495. //---------------------------------------------------------------------------
  496. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  497. {
  498. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  499. FManagedSession = value;
  500. {
  501. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  502. FAllowTransferPresetAutoSelect = false;
  503. TAutoFlag SessionChangingFlag(FSessionChanging);
  504. SessionChanged(Replace);
  505. }
  506. if (Terminal != NULL)
  507. {
  508. TransferPresetAutoSelect();
  509. }
  510. // Update app and tab titles, prevented above by FSessionChanging.
  511. UpdateControls();
  512. }
  513. //---------------------------------------------------------------------------
  514. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  515. {
  516. DoSetManagedSession(value, true);
  517. }
  518. //---------------------------------------------------------------------------
  519. void __fastcall TCustomScpExplorerForm::SessionChanging()
  520. {
  521. if (ManagedSession != NULL)
  522. {
  523. UpdateSession(ManagedSession);
  524. }
  525. }
  526. //---------------------------------------------------------------------------
  527. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  528. {
  529. if (Terminal != NULL)
  530. {
  531. UpdateSessionColor((TColor)Terminal->StateData->Color);
  532. }
  533. if (!TTerminalManager::Instance()->Updating)
  534. {
  535. SessionListChanged();
  536. }
  537. if (Terminal != NULL)
  538. {
  539. if (Replaced)
  540. {
  541. RemoteDirView->ReplaceTerminal(Terminal);
  542. }
  543. else
  544. {
  545. RemoteDirView->Terminal = Terminal;
  546. }
  547. if (Terminal->Active)
  548. {
  549. Terminal->RefreshDirectory();
  550. }
  551. if (WinConfiguration->PreservePanelState)
  552. {
  553. DirView(osRemote)->RestoreState(Terminal->RemoteExplorerState);
  554. }
  555. }
  556. else
  557. {
  558. RemoteDirView->Terminal = NULL;
  559. }
  560. InitStatusBar();
  561. UpdateTransferList();
  562. // Update panels Enable state before refreshing the labels
  563. UpdateControls();
  564. }
  565. //---------------------------------------------------------------------------
  566. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  567. {
  568. if (Queue != value)
  569. {
  570. if (FQueue != NULL)
  571. {
  572. FQueue->OnListUpdate = NULL;
  573. FQueue->OnQueueItemUpdate = NULL;
  574. }
  575. FQueue = value;
  576. if (FQueue != NULL)
  577. {
  578. DebugAssert(FQueue->OnListUpdate == NULL);
  579. FQueue->OnListUpdate = QueueListUpdate;
  580. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  581. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  582. }
  583. QueueChanged();
  584. }
  585. }
  586. //---------------------------------------------------------------------------
  587. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  588. TListItem * Item)
  589. {
  590. if (FQueueActedItem == Item)
  591. {
  592. FQueueActedItem = NULL;
  593. if ((QueueView3->PopupMenu != NULL) &&
  594. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  595. {
  596. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  597. ReleaseCapture();
  598. }
  599. }
  600. if (Item->Data == FPendingQueueActionItem)
  601. {
  602. FPendingQueueActionItem = NULL;
  603. }
  604. }
  605. //---------------------------------------------------------------------------
  606. bool TCustomScpExplorerForm::IsAnythingQueued()
  607. {
  608. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  609. }
  610. //---------------------------------------------------------------------------
  611. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  612. {
  613. {
  614. TGuard Guard(FQueueStatusSection);
  615. FQueueStatusInvalidated = false;
  616. if (FQueue != NULL)
  617. {
  618. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  619. }
  620. }
  621. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  622. {
  623. FMaxQueueLength = FQueueStatus->Count;
  624. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  625. }
  626. {
  627. TAutoFlag Flag(FQueueStatusUpdating);
  628. FQueueController->UpdateQueueStatus(FQueueStatus);
  629. }
  630. UpdateQueueFileList();
  631. SetQueueProgress();
  632. UpdateQueueView();
  633. bool IsEmpty = !IsAnythingQueued();
  634. if (IsEmpty && (Terminal != NULL))
  635. {
  636. TOnceDoneOperation OnceDoneOperation =
  637. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  638. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  639. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  640. {
  641. FQueue->Enabled = false;
  642. }
  643. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  644. {
  645. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  646. }
  647. }
  648. }
  649. //---------------------------------------------------------------------------
  650. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  651. {
  652. UnicodeString Result;
  653. if (FQueueStatus != NULL)
  654. {
  655. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  656. {
  657. TFileOperationProgressType * ProgressData =
  658. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  659. if ((ProgressData != NULL) && ProgressData->InProgress)
  660. {
  661. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  662. }
  663. }
  664. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  665. {
  666. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  667. }
  668. }
  669. return Result;
  670. }
  671. //---------------------------------------------------------------------------
  672. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  673. {
  674. ComponentVisible[fcQueueView] =
  675. (WinConfiguration->QueueView.Show == qvShow) ||
  676. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  677. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  678. }
  679. //---------------------------------------------------------------------------
  680. void __fastcall TCustomScpExplorerForm::QueueChanged()
  681. {
  682. if (FQueueStatus != NULL)
  683. {
  684. // UpdateFileList implementation relies on the status being recreated when session changes
  685. delete FQueueStatus;
  686. FQueueStatus = NULL;
  687. }
  688. UpdateQueueStatus(true);
  689. }
  690. //---------------------------------------------------------------------------
  691. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  692. {
  693. if (FQueue == Queue)
  694. {
  695. FQueueStatusInvalidated = true;
  696. }
  697. }
  698. //---------------------------------------------------------------------------
  699. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  700. TQueueItem * Item)
  701. {
  702. if (FQueue == Queue)
  703. {
  704. TGuard Guard(FQueueStatusSection);
  705. // this may be running in parallel with QueueChanged
  706. if (FQueueStatus != NULL)
  707. {
  708. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  709. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  710. {
  711. FRefreshLocalDirectory = (QueueItem == NULL) ||
  712. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  713. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  714. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  715. }
  716. if (QueueItem != NULL)
  717. {
  718. QueueItem->UserData = (void*)true;
  719. FQueueItemInvalidated = true;
  720. }
  721. }
  722. }
  723. }
  724. //---------------------------------------------------------------------------
  725. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  726. {
  727. // during standalone editing, we have no way to see/control queue,
  728. // so we have to always popup prompts automatically
  729. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  730. }
  731. //---------------------------------------------------------------------------
  732. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  733. {
  734. if (FQueueStatus != NULL)
  735. {
  736. bool QueueAutoPopup = IsQueueAutoPopup();
  737. bool NeedRefresh = FQueueController->NeedRefresh();
  738. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  739. FQueueItemInvalidated = false;
  740. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  741. bool Updated = false;
  742. TQueueItemProxy * QueueItem;
  743. bool WasUserAction;
  744. for (int Index = 0; Index < Limit; Index++)
  745. {
  746. bool Update = false;
  747. QueueItem = FQueueStatus->Items[Index];
  748. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  749. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  750. (FPendingQueueActionItem == NULL))
  751. {
  752. FPendingQueueActionItem = QueueItem;
  753. FUserActionTimer->Enabled = true;
  754. }
  755. if ((bool)QueueItem->UserData)
  756. {
  757. QueueItem->UserData = (void*)false;
  758. QueueItem->Update();
  759. Updated = true;
  760. Update = true;
  761. }
  762. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  763. {
  764. Update = true;
  765. }
  766. if (Update)
  767. {
  768. FQueueController->RefreshQueueItem(QueueItem);
  769. }
  770. }
  771. if (Updated)
  772. {
  773. NonVisualDataModule->UpdateNonVisibleActions();
  774. SetQueueProgress();
  775. UpdateQueueFileList();
  776. }
  777. }
  778. }
  779. //---------------------------------------------------------------------------
  780. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  781. {
  782. if (FTaskbarList != NULL)
  783. {
  784. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  785. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  786. }
  787. }
  788. //---------------------------------------------------------------------------
  789. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  790. {
  791. if (Progress >= 0)
  792. {
  793. if (FTaskbarList != NULL)
  794. {
  795. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  796. }
  797. }
  798. else
  799. {
  800. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  801. }
  802. }
  803. //---------------------------------------------------------------------------
  804. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  805. {
  806. int OverallProgress;
  807. // FProgressForm is null when this is called from SetQueueProgress
  808. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  809. {
  810. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  811. }
  812. else if (!TFileOperationProgressType::IsIndeterminateOperation(ProgressData->Operation))
  813. {
  814. OverallProgress = ProgressData->OverallProgress();
  815. }
  816. else
  817. {
  818. OverallProgress = -1;
  819. }
  820. SetTaskbarListProgressValue(OverallProgress);
  821. }
  822. //---------------------------------------------------------------------------
  823. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  824. {
  825. TTerminalManager::Instance()->QueueStatusUpdated();
  826. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  827. {
  828. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  829. {
  830. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  831. {
  832. TFileOperationProgressType * ProgressData = NULL;
  833. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  834. {
  835. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  836. }
  837. if ((ProgressData != NULL) &&
  838. ProgressData->InProgress)
  839. {
  840. SetTaskbarListProgressValue(ProgressData);
  841. }
  842. else
  843. {
  844. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  845. }
  846. }
  847. else
  848. {
  849. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  850. }
  851. }
  852. else
  853. {
  854. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  855. }
  856. }
  857. UpdateQueueLabel();
  858. }
  859. //---------------------------------------------------------------------------
  860. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  861. {
  862. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  863. if (FQueueStatus != NULL)
  864. {
  865. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  866. if (ActiveAndPendingPrimaryCount > 0)
  867. {
  868. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  869. }
  870. }
  871. QueueLabel->Caption = Caption;
  872. }
  873. //---------------------------------------------------------------------------
  874. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  875. {
  876. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  877. static_cast<TComponent*>(GetComponent(fcTransferList)));
  878. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  879. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  880. TransferList->Strings->BeginUpdate();
  881. try
  882. {
  883. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  884. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  885. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  886. if (FTransferDropDownHint.IsEmpty())
  887. {
  888. FTransferDropDownHint = TransferDropDown->Hint;
  889. }
  890. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  891. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  892. TransferDropDown->Text = StripHotkey(Name);
  893. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  894. (FTransferDropDownHint, StripHotkey(Name),
  895. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  896. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  897. // update the label, otherwise when it is updated only on the first draw
  898. // of the list, it is drawn "bold" for some reason
  899. FTransferListHoverIndex = TransferList->ItemIndex;
  900. UpdateTransferLabel();
  901. }
  902. __finally
  903. {
  904. TransferList->Strings->EndUpdate();
  905. }
  906. }
  907. //---------------------------------------------------------------------------
  908. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  909. {
  910. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  911. DebugAssert(Toolbar != NULL);
  912. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  913. }
  914. //---------------------------------------------------------------------------
  915. void __fastcall TCustomScpExplorerForm::UpdateActions()
  916. {
  917. TForm::UpdateActions();
  918. if (ComponentVisible[fcCustomCommandsBand])
  919. {
  920. UpdateCustomCommandsToolbar();
  921. }
  922. }
  923. //---------------------------------------------------------------------------
  924. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  925. {
  926. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  927. }
  928. //---------------------------------------------------------------------------
  929. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  930. {
  931. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  932. {
  933. DirView->RowSelect = WinConfiguration->FullRowSelect;
  934. // selection is not redrawn automatically when RowSelect changes
  935. DirView->Invalidate();
  936. }
  937. }
  938. //---------------------------------------------------------------------------
  939. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  940. {
  941. Color = GetBtnFaceColor();
  942. DebugAssert(Configuration && RemoteDirView);
  943. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  944. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  945. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  946. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  947. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  948. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  949. UpdateRowSelect(RemoteDirView);
  950. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  951. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  952. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  953. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  954. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  955. UpdateSessionsPageControlHeight();
  956. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  957. UpdateToolbarDisplayMode();
  958. UpdateTransferList();
  959. if (Terminal != NULL)
  960. {
  961. TransferPresetAutoSelect();
  962. }
  963. UpdateQueueView();
  964. UpdateControls();
  965. // this can be called even before constuctor finishes.
  966. if (FEditorManager != NULL)
  967. {
  968. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  969. }
  970. // this can be called even before constuctor finishes.
  971. if (FLocalEditors != NULL)
  972. {
  973. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  974. {
  975. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  976. }
  977. }
  978. if (ComponentVisible[fcCustomCommandsBand])
  979. {
  980. UpdateCustomCommandsToolbar();
  981. }
  982. // show only when keeping queue items forever,
  983. // otherwise, its enough to have in in the context menu
  984. QueueDeleteAllDoneQueueToolbarItem->Visible =
  985. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  986. if (FFileColorsCurrent != WinConfiguration->FileColors)
  987. {
  988. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  989. FFileColorsCurrent = WinConfiguration->FileColors;
  990. FileColorsChanged();
  991. }
  992. }
  993. //---------------------------------------------------------------------------
  994. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  995. {
  996. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  997. DirView->Invalidate();
  998. }
  999. //---------------------------------------------------------------------------
  1000. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1001. {
  1002. DoFileColorsChanged(RemoteDirView);
  1003. }
  1004. //---------------------------------------------------------------------------
  1005. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1006. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1007. void * /*Arg*/)
  1008. {
  1009. if (Token != NULL)
  1010. {
  1011. TForm * Editor = dynamic_cast<TForm*>(Token);
  1012. DebugAssert(Editor != NULL);
  1013. ReconfigureEditorForm(Editor);
  1014. }
  1015. }
  1016. //---------------------------------------------------------------------------
  1017. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1018. {
  1019. WinConfiguration->DDTransferConfirmation = asOn;
  1020. }
  1021. //---------------------------------------------------------------------------
  1022. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1023. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1024. {
  1025. // noop
  1026. }
  1027. //---------------------------------------------------------------------------
  1028. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1029. {
  1030. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1031. }
  1032. //---------------------------------------------------------------------------
  1033. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1034. {
  1035. if (DoNotShowAgain)
  1036. {
  1037. if (DragDrop)
  1038. {
  1039. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1040. {
  1041. PopupTrayBalloon(
  1042. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1043. }
  1044. WinConfiguration->DDTransferConfirmation = asOff;
  1045. }
  1046. else
  1047. {
  1048. WinConfiguration->ConfirmTransferring = false;
  1049. }
  1050. }
  1051. else
  1052. {
  1053. // User explicitly unchecked "do not show again",
  1054. // so show him the dialog the next time
  1055. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1056. {
  1057. WinConfiguration->DDTransferConfirmation = asOn;
  1058. }
  1059. }
  1060. }
  1061. //---------------------------------------------------------------------------
  1062. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1063. TTransferDirection Direction, TTransferType Type, bool Temp,
  1064. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1065. bool Confirm, bool DragDrop, int Options)
  1066. {
  1067. bool Result = true;
  1068. DebugAssert(HasActiveTerminal());
  1069. // these parameters are known in advance
  1070. int Params =
  1071. FLAGMASK(Type == ttMove, cpDelete);
  1072. bool ToTemp = (Temp && (Direction == tdToLocal));
  1073. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1074. {
  1075. bool DisableNewerOnly =
  1076. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1077. ToTemp;
  1078. Options |=
  1079. FLAGMASK(ToTemp, coTemp) |
  1080. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1081. coDoNotShowAgain;
  1082. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1083. int CopyParamAttrs = (Direction == tdToRemote ?
  1084. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1085. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1086. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1087. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1088. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1089. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1090. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1091. if (Result)
  1092. {
  1093. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1094. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1095. }
  1096. }
  1097. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1098. {
  1099. Configuration->Usage->Inc(L"TransfersOnBackground");
  1100. // these parameter are known only after transfer dialog
  1101. Params |=
  1102. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1103. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1104. Result = false;
  1105. ClearTransferSourceSelection(Direction);
  1106. }
  1107. return Result;
  1108. }
  1109. //---------------------------------------------------------------------------
  1110. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1111. {
  1112. if (FOnFileOperationFinished != NULL)
  1113. {
  1114. FOnFileOperationFinished(UnicodeString(), true);
  1115. }
  1116. else
  1117. {
  1118. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1119. if (HasDirView[Side])
  1120. {
  1121. DirView(Side)->SelectAll(smNone);
  1122. }
  1123. }
  1124. }
  1125. //---------------------------------------------------------------------------
  1126. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1127. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1128. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1129. int Params)
  1130. {
  1131. DebugAssert(Queue != NULL);
  1132. bool SingleFile = false;
  1133. if (FileList->Count == 1)
  1134. {
  1135. if (Direction == tdToRemote)
  1136. {
  1137. UnicodeString FileName = FileList->Strings[0];
  1138. SingleFile = FileExists(ApiPath(FileName));
  1139. }
  1140. else
  1141. {
  1142. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1143. SingleFile = !File->IsDirectory;
  1144. }
  1145. }
  1146. TQueueItem * QueueItem;
  1147. if (Direction == tdToRemote)
  1148. {
  1149. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1150. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1151. }
  1152. else
  1153. {
  1154. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1155. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1156. }
  1157. AddQueueItem(Queue, QueueItem, Terminal);
  1158. }
  1159. //---------------------------------------------------------------------------
  1160. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1161. {
  1162. if (Queue->IsEmpty)
  1163. {
  1164. Terminal->QueueOperationStart = Now();
  1165. }
  1166. Queue->AddItem(QueueItem);
  1167. }
  1168. //---------------------------------------------------------------------------
  1169. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1170. {
  1171. }
  1172. //---------------------------------------------------------------------------
  1173. void __fastcall TCustomScpExplorerForm::InitControls()
  1174. {
  1175. DebugAssert(FDocks != NULL);
  1176. for (int Index = 0; Index < ComponentCount; Index++)
  1177. {
  1178. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1179. if ((Dock != NULL) && (Dock->Tag == 0))
  1180. {
  1181. FDocks->Add(Dock);
  1182. }
  1183. }
  1184. CollectItemsWithTextDisplayMode(this);
  1185. }
  1186. //---------------------------------------------------------------------------
  1187. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1188. {
  1189. ConfigurationChanged();
  1190. QueuePanel->Height =
  1191. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1192. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1193. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1194. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1195. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1196. QueueFileList->Height =
  1197. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1198. if (QueueFileList->Visible)
  1199. {
  1200. AdjustQueueLayout();
  1201. }
  1202. UpdateCustomCommandsToolbar();
  1203. }
  1204. //---------------------------------------------------------------------------
  1205. void __fastcall TCustomScpExplorerForm::StoreParams()
  1206. {
  1207. int APixelsPerInch = GetControlPixelsPerInch(this);
  1208. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1209. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1210. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1211. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1212. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1213. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1214. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1215. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1216. }
  1217. //---------------------------------------------------------------------------
  1218. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1219. {
  1220. if (!FFormRestored)
  1221. {
  1222. FFormRestored = true;
  1223. RestoreFormParams();
  1224. }
  1225. TForm::CreateParams(Params);
  1226. }
  1227. //---------------------------------------------------------------------------
  1228. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1229. {
  1230. DebugAssert(FDocks != NULL);
  1231. for (int Index = 0; Index < FDocks->Count; Index++)
  1232. {
  1233. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1234. }
  1235. }
  1236. //---------------------------------------------------------------------------
  1237. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1238. {
  1239. SetDockAllowDrag(true);
  1240. while (!ButtonsStr.IsEmpty())
  1241. {
  1242. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1243. UnicodeString Name = CutToChar(S, L'=', true);
  1244. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1245. {
  1246. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1247. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1248. {
  1249. while (!S.IsEmpty())
  1250. {
  1251. UnicodeString S2 = CutToChar(S, L',', true);
  1252. UnicodeString Name = CutToChar(S2, L':', true);
  1253. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1254. {
  1255. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1256. if (SameText(GetToolbarItemName(Item), Name))
  1257. {
  1258. // forward compatibility
  1259. UnicodeString S3 = CutToChar(S2, L':', true);
  1260. bool Visible = StrToIntDef(S3, 0);
  1261. Item->Visible = Visible;
  1262. }
  1263. }
  1264. }
  1265. }
  1266. }
  1267. }
  1268. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1269. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1270. }
  1271. //---------------------------------------------------------------------------
  1272. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1273. {
  1274. UnicodeString Result;
  1275. SetDockAllowDrag(true);
  1276. Result = ::GetToolbarsLayoutStr(this);
  1277. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1278. return Result;
  1279. }
  1280. //---------------------------------------------------------------------------
  1281. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1282. {
  1283. UnicodeString Result;
  1284. if (Item->Action != NULL)
  1285. {
  1286. Result = Item->Action->Name;
  1287. Result = RemoveSuffix(Result, L"Action", true);
  1288. }
  1289. else
  1290. {
  1291. Result = Item->Name;
  1292. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1293. Result = RemoveSuffix(Result, L"Item", true);
  1294. }
  1295. return Result;
  1296. }
  1297. //---------------------------------------------------------------------------
  1298. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1299. {
  1300. UnicodeString Result;
  1301. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1302. {
  1303. UnicodeString ToolbarStr;
  1304. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1305. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1306. {
  1307. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1308. {
  1309. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1310. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1311. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1312. // and save the changes here only.
  1313. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1314. {
  1315. UnicodeString Name = GetToolbarItemName(Item);
  1316. DebugAssert(Name.Pos(L"TBX") == 0);
  1317. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1318. }
  1319. }
  1320. }
  1321. if (!ToolbarStr.IsEmpty())
  1322. {
  1323. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1324. }
  1325. }
  1326. return Result;
  1327. }
  1328. //---------------------------------------------------------------------------
  1329. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1330. {
  1331. DebugAssert(FProgressForm == NULL);
  1332. bool AllowSkip = (Terminal != NULL) ? Terminal->IsCapable[fcSkipTransfer] : false;
  1333. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1334. FProgressForm->DeleteLocalToRecycleBin =
  1335. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1336. FProgressForm->DeleteRemoteToRecycleBin =
  1337. (Terminal != NULL) &&
  1338. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1339. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1340. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1341. LockWindow();
  1342. // Actually, do not know what hides the progress once the operation finishes
  1343. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1344. SetTaskbarListProgressState(TBPF_NORMAL);
  1345. }
  1346. //---------------------------------------------------------------------------
  1347. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1348. {
  1349. UnlockWindow();
  1350. SAFE_DESTROY(FProgressForm);
  1351. SetQueueProgress();
  1352. }
  1353. //---------------------------------------------------------------------------
  1354. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1355. TFileOperationProgressType & ProgressData)
  1356. {
  1357. // Download to temporary local directory
  1358. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1359. {
  1360. bool Continue = true;
  1361. FPendingTempSpaceWarn = false;
  1362. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1363. if (!Continue)
  1364. {
  1365. Abort();
  1366. }
  1367. }
  1368. // operation is being executed and we still didn't show up progress form
  1369. if (ProgressData.InProgress && !FProgressForm)
  1370. {
  1371. CreateProgressForm(NULL);
  1372. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1373. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1374. }
  1375. // operation is finished (or terminated), so we hide progress form
  1376. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1377. {
  1378. DestroyProgressForm();
  1379. if (ProgressData.Operation == foCalculateSize)
  1380. {
  1381. // When calculating size before transfer, the abort caused by
  1382. // cancel flag set due to "MoveToQueue" below,
  1383. // is not propagated back to us in ExecuteFileOperation,
  1384. // as it is silently swallowed.
  1385. // So we explicitly (re)throw it here.
  1386. if (FMoveToQueue)
  1387. {
  1388. Abort();
  1389. }
  1390. }
  1391. else
  1392. {
  1393. if ((ProgressData.Cancel == csContinue) &&
  1394. !FAutoOperation)
  1395. {
  1396. OperationComplete(ProgressData.StartTime);
  1397. }
  1398. }
  1399. }
  1400. if (FProgressForm &&
  1401. // Can happen only during synchronization, when ending one of the operations
  1402. ProgressData.InProgress)
  1403. {
  1404. FProgressForm->SetProgressData(ProgressData);
  1405. DebugAssert(ProgressData.InProgress);
  1406. SetTaskbarListProgressValue(&ProgressData);
  1407. if (FProgressForm->Cancel > csContinue)
  1408. {
  1409. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1410. if (FProgressForm->Cancel == csCancelFile)
  1411. {
  1412. FProgressForm->ClearCancel();
  1413. }
  1414. // cancel cancels even the move
  1415. FMoveToQueue = false;
  1416. }
  1417. else if (FProgressForm->MoveToQueue)
  1418. {
  1419. FMoveToQueue = true;
  1420. ProgressData.SetCancelAtLeast(csCancel);
  1421. }
  1422. if ((FTransferResumeList != NULL) &&
  1423. ProgressData.InProgress &&
  1424. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1425. !ProgressData.FullFileName.IsEmpty())
  1426. {
  1427. if ((FTransferResumeList->Count == 0) ||
  1428. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1429. {
  1430. // note that we do not recognize directories from files here
  1431. FTransferResumeList->Add(ProgressData.FullFileName);
  1432. }
  1433. }
  1434. }
  1435. }
  1436. //---------------------------------------------------------------------------
  1437. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1438. const TDateTime & StartTime)
  1439. {
  1440. if (GUIConfiguration->BeepOnFinish &&
  1441. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1442. {
  1443. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1444. DWORD Sound;
  1445. if (!ExtractFileExt(BeepSound).IsEmpty())
  1446. {
  1447. Sound = SND_FILENAME;
  1448. }
  1449. else
  1450. {
  1451. Sound = SND_ALIAS;
  1452. }
  1453. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1454. }
  1455. }
  1456. //---------------------------------------------------------------------------
  1457. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1458. TFileOperationProgressType & ProgressData)
  1459. {
  1460. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1461. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1462. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1463. FileOperationProgress(ProgressData);
  1464. }
  1465. //---------------------------------------------------------------------------
  1466. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1467. {
  1468. UnicodeString Result;
  1469. if (FProgressForm != NULL)
  1470. {
  1471. Result = FProgressForm->ProgressStr();
  1472. }
  1473. return Result;
  1474. }
  1475. //---------------------------------------------------------------------------
  1476. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1477. bool DragDrop)
  1478. {
  1479. return (!DragDrop && (DropSourceControl == NULL)) ||
  1480. (DropSourceControl == DirView(osOther));
  1481. }
  1482. //---------------------------------------------------------------------------
  1483. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1484. TFileOperation Operation, TOperationSide Side,
  1485. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1486. TOnceDoneOperation & OnceDoneOperation)
  1487. {
  1488. if (!FAutoOperation)
  1489. {
  1490. // no selection on "/upload", form serves only as event handler
  1491. // (it is not displayed)
  1492. if (PanelOperation(Side, FDragDropOperation) &&
  1493. Visible && (Operation != foCalculateSize) &&
  1494. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1495. {
  1496. if (FOnFileOperationFinished != NULL)
  1497. {
  1498. FOnFileOperationFinished(FileName, Success);
  1499. }
  1500. else
  1501. {
  1502. TOperationSide DViewSide;
  1503. if ((FProgressSide != osCurrent) &&
  1504. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1505. {
  1506. // assume the operation is over the focused panel
  1507. DViewSide = FProgressSide;
  1508. }
  1509. else
  1510. {
  1511. DViewSide = Side;
  1512. }
  1513. TCustomDirView * DView = DirView(DViewSide);
  1514. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(DViewSide));
  1515. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1516. // this can happen when local drive is unplugged in the middle of the operation
  1517. if (Item != NULL)
  1518. {
  1519. if (Success) Item->Selected = false;
  1520. DView->MakeProgressVisible(Item);
  1521. }
  1522. }
  1523. }
  1524. if ((Operation == foCopy) || (Operation == foMove))
  1525. {
  1526. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1527. {
  1528. if (Side == osLocal)
  1529. {
  1530. Configuration->Usage->Inc(L"UploadedFiles");
  1531. }
  1532. else
  1533. {
  1534. Configuration->Usage->Inc(L"DownloadedFiles");
  1535. }
  1536. }
  1537. }
  1538. }
  1539. if (Success && (FSynchronizeController != NULL))
  1540. {
  1541. DebugAssert(!IsSideLocalBrowser(osRemote));
  1542. if (Operation == foCopy)
  1543. {
  1544. DebugAssert(Side == osLocal);
  1545. FSynchronizeController->LogOperation(soUpload, FileName);
  1546. }
  1547. else if (Operation == foDelete)
  1548. {
  1549. DebugAssert(Side == osRemote);
  1550. FSynchronizeController->LogOperation(soDelete, FileName);
  1551. }
  1552. }
  1553. if (FProgressForm)
  1554. {
  1555. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1556. }
  1557. }
  1558. //---------------------------------------------------------------------------
  1559. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1560. TFileOperation Operation, TOperationSide Side,
  1561. bool Temp, const UnicodeString & FileName, Boolean Success,
  1562. TOnceDoneOperation & OnceDoneOperation)
  1563. {
  1564. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1565. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1566. OnceDoneOperation);
  1567. }
  1568. //---------------------------------------------------------------------------
  1569. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1570. {
  1571. return false;
  1572. }
  1573. //---------------------------------------------------------------------------
  1574. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1575. {
  1576. return false;
  1577. }
  1578. //---------------------------------------------------------------------------
  1579. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1580. {
  1581. return NULL;
  1582. }
  1583. //---------------------------------------------------------------------------
  1584. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1585. {
  1586. DebugAssert(GetSide(Side) == osRemote);
  1587. DebugUsedParam(Side);
  1588. return RemoteDirView;
  1589. }
  1590. //---------------------------------------------------------------------------
  1591. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1592. {
  1593. DebugAssert(GetSide(Side) == osRemote);
  1594. DebugUsedParam(Side);
  1595. return RemoteDriveView;
  1596. }
  1597. //---------------------------------------------------------------------------
  1598. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1599. {
  1600. DebugAssert(GetSide(Side) == osRemote);
  1601. DebugUsedParam(Side);
  1602. return HasActiveTerminal();
  1603. }
  1604. //---------------------------------------------------------------------------
  1605. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1606. TOperationSide Side, int FilesOnly)
  1607. {
  1608. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1609. }
  1610. //---------------------------------------------------------------------------
  1611. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1612. TOperationSide Side, int FilesOnly)
  1613. {
  1614. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1615. }
  1616. //---------------------------------------------------------------------------
  1617. struct THistoryItemData
  1618. {
  1619. short int Side;
  1620. short int Index;
  1621. };
  1622. //---------------------------------------------------------------------------
  1623. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1624. {
  1625. DirView(Side)->HistoryGo(Index);
  1626. }
  1627. //---------------------------------------------------------------------------
  1628. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1629. {
  1630. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1631. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1632. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1633. }
  1634. //---------------------------------------------------------------------------
  1635. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1636. bool Back)
  1637. {
  1638. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1639. {
  1640. TCustomDirView * DView = DirView(Side);
  1641. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1642. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1643. if (ICount > 10)
  1644. {
  1645. ICount = 10;
  1646. }
  1647. Menu->Items->Clear();
  1648. THistoryItemData Data;
  1649. Data.Side = Side;
  1650. for (short int i = 1; i <= ICount; i++)
  1651. {
  1652. TTBCustomItem * Item = new TTBXItem(Menu);
  1653. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1654. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1655. Item->Hint = DView->HistoryPath[Data.Index];
  1656. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1657. Item->Tag = *reinterpret_cast<int*>(&Data);
  1658. Item->OnClick = HistoryItemClick;
  1659. Menu->Items->Add(Item);
  1660. }
  1661. }
  1662. }
  1663. //---------------------------------------------------------------------------
  1664. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1665. TOperationSide Side, bool Back)
  1666. {
  1667. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1668. {
  1669. // workaround
  1670. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1671. // operator new allocates memory in Pascal code, but calls inline
  1672. // contructor in C++, leading in problems, the function does
  1673. // both in Pascal code
  1674. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1675. UpdateHistoryMenu(Side, Back);
  1676. }
  1677. return FHistoryMenu[Side == osLocal][Back];
  1678. }
  1679. //---------------------------------------------------------------------------
  1680. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1681. TCustomDirView *Sender)
  1682. {
  1683. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1684. UpdateHistoryMenu(Side, true);
  1685. UpdateHistoryMenu(Side, false);
  1686. }
  1687. //---------------------------------------------------------------------------
  1688. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1689. {
  1690. // remote custom commands can be executed only if the server supports shell commands
  1691. // or have secondary shell
  1692. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1693. }
  1694. //---------------------------------------------------------------------------
  1695. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1696. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1697. {
  1698. // -1 = hidden, 0 = disabled, 1 = enabled
  1699. int Result;
  1700. TFileCustomCommand RemoteCustomCommand;
  1701. TLocalCustomCommand LocalCustomCommand;
  1702. TFileCustomCommand * NonInteractiveCustomCommand =
  1703. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1704. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1705. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1706. if (FLAGCLEAR(Command.Params, ccLocal))
  1707. {
  1708. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1709. // custom command that does not operate with files can be executed anytime ...
  1710. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1711. {
  1712. // ... but do not show such command in remote file menu
  1713. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1714. {
  1715. Result = AllowedState;
  1716. }
  1717. else
  1718. {
  1719. Result = -1;
  1720. }
  1721. }
  1722. else
  1723. {
  1724. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1725. {
  1726. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1727. }
  1728. else
  1729. {
  1730. Result = -1;
  1731. }
  1732. }
  1733. }
  1734. else
  1735. {
  1736. // custom command that does not operate with files can be executed anytime
  1737. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1738. {
  1739. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1740. }
  1741. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1742. {
  1743. if ((ListType == ccltAll) || (ListType == ccltFile))
  1744. {
  1745. // special case is "diff"-style command that can be executed over any side,
  1746. // if we have both sides
  1747. Result =
  1748. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1749. // directly (instead of EnableSelectedOperation) to pass
  1750. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1751. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1752. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1753. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1754. }
  1755. else if (ListType == ccltBoth)
  1756. {
  1757. DebugAssert(!IsLocalBrowserMode());
  1758. Result = 1;
  1759. }
  1760. else
  1761. {
  1762. Result = -1;
  1763. }
  1764. }
  1765. else
  1766. {
  1767. if ((ListType == ccltAll) ||
  1768. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1769. {
  1770. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1771. }
  1772. else
  1773. {
  1774. Result = -1;
  1775. }
  1776. }
  1777. if ((Result > 0) &&
  1778. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1779. !HasActiveTerminal())
  1780. {
  1781. Result = 0;
  1782. }
  1783. }
  1784. return Result;
  1785. }
  1786. //---------------------------------------------------------------------------
  1787. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1788. TStrings * FileList, const TCustomCommandType & ACommand,
  1789. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1790. {
  1791. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1792. {
  1793. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1794. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1795. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1796. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1797. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1798. bool Capture =
  1799. FLAGSET(ACommand.Params, ccShowResults) ||
  1800. FLAGSET(ACommand.Params, ccCopyResults) ||
  1801. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1802. TCaptureOutputEvent OutputEvent = NULL;
  1803. DebugAssert(FCapturedLog == NULL);
  1804. if (Capture)
  1805. {
  1806. FCapturedLog = new TStringList();
  1807. OutputEvent = TerminalCaptureLog;
  1808. }
  1809. try
  1810. {
  1811. if (!RemoteCustomCommand.IsFileCommand(Command))
  1812. {
  1813. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1814. OutputEvent);
  1815. }
  1816. else
  1817. {
  1818. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1819. }
  1820. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1821. {
  1822. if (FLAGSET(ACommand.Params, ccCopyResults))
  1823. {
  1824. CopyToClipboard(FCapturedLog);
  1825. }
  1826. if (FLAGSET(ACommand.Params, ccShowResults))
  1827. {
  1828. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1829. }
  1830. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1831. {
  1832. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1833. }
  1834. }
  1835. }
  1836. __finally
  1837. {
  1838. SAFE_DESTROY(FCapturedLog);
  1839. }
  1840. }
  1841. }
  1842. //---------------------------------------------------------------------------
  1843. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1844. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1845. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1846. {
  1847. DebugAssert(!IsLocalBrowserMode());
  1848. TStrings * LocalFileList = NULL;
  1849. TStrings * RemoteFileList = NULL;
  1850. TStrings * RemoteFileListFull = NULL;
  1851. try
  1852. {
  1853. if (LocalFileCommand)
  1854. {
  1855. if (ALocalFileList == NULL)
  1856. {
  1857. DebugAssert(HasDirView[osLocal]);
  1858. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1859. // directly (instead of EnableSelectedOperation) to pass
  1860. // false to FocusedFileOnlyWhenFocused
  1861. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1862. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1863. }
  1864. else
  1865. {
  1866. LocalFileList = ALocalFileList;
  1867. }
  1868. if (FileListCommand)
  1869. {
  1870. if (LocalFileList->Count != 1)
  1871. {
  1872. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1873. }
  1874. }
  1875. else
  1876. {
  1877. if ((LocalFileList->Count != 1) &&
  1878. (FileList->Count != 1) &&
  1879. (LocalFileList->Count != FileList->Count))
  1880. {
  1881. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1882. }
  1883. }
  1884. }
  1885. UnicodeString RootTempDir;
  1886. UnicodeString TempDir;
  1887. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1888. if (!RemoteFiles)
  1889. {
  1890. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1891. }
  1892. try
  1893. {
  1894. TDateTimes RemoteFileTimes;
  1895. if (RemoteFiles)
  1896. {
  1897. RemoteFileList = FileList;
  1898. }
  1899. else
  1900. {
  1901. RemoteFileList = new TStringList();
  1902. TMakeLocalFileListParams MakeFileListParam;
  1903. MakeFileListParam.FileList = RemoteFileList;
  1904. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1905. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1906. MakeFileListParam.Recursive =
  1907. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1908. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1909. if (!MakeFileListParam.Recursive)
  1910. {
  1911. RemoteFileTimes.clear();
  1912. RemoteFileListFull = new TStringList();
  1913. MakeFileListParam.FileList = RemoteFileListFull;
  1914. MakeFileListParam.Recursive = true;
  1915. MakeFileListParam.IncludeDirs = false;
  1916. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1917. }
  1918. else
  1919. {
  1920. RemoteFileListFull = RemoteFileList;
  1921. }
  1922. }
  1923. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1924. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1925. if (!NonBlocking)
  1926. {
  1927. int Count;
  1928. if (FileListCommand)
  1929. {
  1930. Count = 1;
  1931. }
  1932. else if (LocalFileCommand)
  1933. {
  1934. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1935. }
  1936. else
  1937. {
  1938. Count = RemoteFileList->Count;
  1939. }
  1940. Progress.Start(foCustomCommand, osRemote, Count);
  1941. DebugAssert(FProgressForm != NULL);
  1942. FProgressForm->ReadOnly = true;
  1943. }
  1944. try
  1945. {
  1946. if (FileListCommand)
  1947. {
  1948. UnicodeString LocalFile;
  1949. // MakeFileList does not delimit filenames
  1950. UnicodeString FileList = MakeFileList(RemoteFileList);
  1951. if (LocalFileCommand)
  1952. {
  1953. DebugAssert(LocalFileList->Count == 1);
  1954. LocalFile = LocalFileList->Strings[0];
  1955. }
  1956. TLocalCustomCommand CustomCommand(Data,
  1957. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1958. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1959. if (NonBlocking)
  1960. {
  1961. DebugAssert(!POutput);
  1962. ExecuteShellChecked(ShellCommand);
  1963. }
  1964. else
  1965. {
  1966. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1967. }
  1968. }
  1969. else if (LocalFileCommand)
  1970. {
  1971. if (LocalFileList->Count == 1)
  1972. {
  1973. UnicodeString LocalFile = LocalFileList->Strings[0];
  1974. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1975. {
  1976. UnicodeString FileName = RemoteFileList->Strings[Index];
  1977. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1978. Progress.SetFile(FileNameForProgress);
  1979. TLocalCustomCommand CustomCommand(Data,
  1980. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1981. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1982. TOnceDoneOperation OnceDoneOperation;
  1983. // Do not unselect anything, as it won't work with two panels anyway
  1984. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1985. }
  1986. }
  1987. else if (RemoteFileList->Count == 1)
  1988. {
  1989. UnicodeString FileName = RemoteFileList->Strings[0];
  1990. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1991. {
  1992. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1993. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1994. Progress.SetFile(LocalFileNameOnly);
  1995. TLocalCustomCommand CustomCommand(
  1996. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1997. FileName, LocalFileName, L"");
  1998. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1999. TOnceDoneOperation OnceDoneOperation;
  2000. // Do not unselect anything, as it won't work with two panels anyway
  2001. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2002. }
  2003. }
  2004. else
  2005. {
  2006. if (LocalFileList->Count != RemoteFileList->Count)
  2007. {
  2008. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2009. }
  2010. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2011. {
  2012. UnicodeString FileName = RemoteFileList->Strings[Index];
  2013. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2014. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2015. Progress.SetFile(LocalFileNameOnly); // sic
  2016. TLocalCustomCommand CustomCommand(
  2017. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2018. FileName, LocalFileName, L"");
  2019. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2020. TOnceDoneOperation OnceDoneOperation;
  2021. // Do not unselect anything, as it won't work with two panels anyway
  2022. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2023. }
  2024. }
  2025. }
  2026. else
  2027. {
  2028. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2029. {
  2030. UnicodeString FileName = RemoteFileList->Strings[Index];
  2031. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2032. Progress.SetFile(FileNameForProgress);
  2033. TLocalCustomCommand CustomCommand(Data,
  2034. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2035. FileName, L"", L"");
  2036. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2037. TOnceDoneOperation OnceDoneOperation;
  2038. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2039. }
  2040. }
  2041. }
  2042. __finally
  2043. {
  2044. if (!NonBlocking)
  2045. {
  2046. Progress.Stop();
  2047. }
  2048. }
  2049. DebugAssert(!FAutoOperation);
  2050. if (!RemoteFiles)
  2051. {
  2052. TempDir = IncludeTrailingBackslash(TempDir);
  2053. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2054. {
  2055. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2056. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2057. // Skip directories, process only nested files.
  2058. // The check is redundant as FileAge fails for directories anyway.
  2059. !DirectoryExists(FileName))
  2060. {
  2061. UnicodeString RemoteDir =
  2062. UnixExtractFileDir(
  2063. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2064. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2065. TDateTime NewTime;
  2066. if (FileAge(FileName, NewTime) &&
  2067. (NewTime != RemoteFileTimes[Index]))
  2068. {
  2069. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2070. TemporaryFileCopyParam(CopyParam);
  2071. CopyParam.FileMask = L"";
  2072. FAutoOperation = true;
  2073. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2074. TemporaryFilesList->Add(FileName);
  2075. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2076. }
  2077. }
  2078. }
  2079. }
  2080. }
  2081. __finally
  2082. {
  2083. FAutoOperation = false;
  2084. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2085. {
  2086. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2087. }
  2088. }
  2089. }
  2090. __finally
  2091. {
  2092. if (RemoteFileList != FileList)
  2093. {
  2094. delete RemoteFileList;
  2095. }
  2096. if (LocalFileList != ALocalFileList)
  2097. {
  2098. delete LocalFileList;
  2099. }
  2100. if (RemoteFileListFull != RemoteFileList)
  2101. {
  2102. delete RemoteFileListFull;
  2103. }
  2104. }
  2105. }
  2106. //---------------------------------------------------------------------------
  2107. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2108. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2109. bool FileListCommand, UnicodeString * POutput)
  2110. {
  2111. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2112. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2113. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2114. {
  2115. UnicodeString FileName = SelectedFileList->Strings[Index];
  2116. if (DirectoryExists(FileName))
  2117. {
  2118. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2119. {
  2120. LocalFileList->Add(FileName);
  2121. }
  2122. if (FLAGSET(ACommand.Params, ccRecursive))
  2123. {
  2124. TMakeLocalFileListParams MakeFileListParam;
  2125. MakeFileListParam.FileList = LocalFileList.get();
  2126. MakeFileListParam.FileTimes = NULL;
  2127. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2128. MakeFileListParam.Recursive = true;
  2129. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2130. }
  2131. }
  2132. else
  2133. {
  2134. LocalFileList->Add(FileName);
  2135. }
  2136. }
  2137. UnicodeString RemotePath;
  2138. if (Terminal != NULL)
  2139. {
  2140. RemotePath = Terminal->CurrentDirectory;
  2141. }
  2142. if (FileListCommand)
  2143. {
  2144. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2145. TLocalCustomCommand CustomCommand(
  2146. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2147. L"", L"", FileList);
  2148. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2149. }
  2150. else
  2151. {
  2152. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2153. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2154. DebugAssert(FProgressForm != NULL);
  2155. FProgressForm->ReadOnly = true;
  2156. try
  2157. {
  2158. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2159. {
  2160. UnicodeString FileName = LocalFileList->Strings[Index];
  2161. Progress.SetFile(FileName);
  2162. TLocalCustomCommand CustomCommand(
  2163. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2164. FileName, L"", L"");
  2165. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2166. TOnceDoneOperation OnceDoneOperation;
  2167. Progress.Finish(FileName, true, OnceDoneOperation);
  2168. }
  2169. }
  2170. __finally
  2171. {
  2172. Progress.Stop();
  2173. }
  2174. }
  2175. }
  2176. //---------------------------------------------------------------------------
  2177. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2178. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2179. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2180. {
  2181. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2182. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2183. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2184. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2185. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2186. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2187. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2188. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2189. std::unique_ptr<UnicodeString> POutput;
  2190. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2191. FLAGSET(ACommand.Params, ccCopyResults))
  2192. {
  2193. POutput.reset(new UnicodeString());
  2194. }
  2195. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2196. FProgressSide = FCurrentSide;
  2197. if (!LocalCustomCommand.IsFileCommand(Command))
  2198. {
  2199. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2200. }
  2201. // remote files?
  2202. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2203. {
  2204. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2205. }
  2206. // local files
  2207. else
  2208. {
  2209. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2210. }
  2211. if (POutput.get() != NULL)
  2212. {
  2213. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2214. int P = POutput->Pos(sLineBreak);
  2215. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2216. {
  2217. POutput->SetLength(P - 1);
  2218. }
  2219. // Copy even empty output to clipboard
  2220. if (FLAGSET(ACommand.Params, ccCopyResults))
  2221. {
  2222. CopyToClipboard(*POutput);
  2223. }
  2224. // But do not show an empty message box.
  2225. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2226. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2227. !POutput->IsEmpty())
  2228. {
  2229. TClipboardHandler ClipboardHandler;
  2230. ClipboardHandler.Text = *POutput;
  2231. TMessageParams Params;
  2232. TQueryButtonAlias Aliases[1];
  2233. Aliases[0].Button = qaRetry;
  2234. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2235. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2236. Params.Aliases = Aliases;
  2237. Params.AliasesCount = LENOF(Aliases);
  2238. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2239. }
  2240. }
  2241. }
  2242. //---------------------------------------------------------------------------
  2243. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2244. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2245. {
  2246. TCustomCommandData Data;
  2247. UnicodeString Site;
  2248. UnicodeString RemotePath;
  2249. if (HasActiveTerminal())
  2250. {
  2251. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2252. Data = TCustomCommandData(SessionData.get());
  2253. Site = Terminal->SessionData->SessionKey;
  2254. RemotePath = Terminal->CurrentDirectory;
  2255. }
  2256. UnicodeString HelpKeyword = ACommand.HomePage;
  2257. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2258. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2259. {
  2260. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2261. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2262. {
  2263. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2264. }
  2265. else
  2266. {
  2267. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2268. }
  2269. if (!DoCustomCommandOptionsDialog(
  2270. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2271. {
  2272. Abort();
  2273. }
  2274. }
  2275. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2276. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2277. {
  2278. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2279. }
  2280. else
  2281. {
  2282. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2283. }
  2284. }
  2285. //---------------------------------------------------------------------------
  2286. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2287. const TCustomCommandType & Command)
  2288. {
  2289. DebugAssert(FCustomCommandLocalFileList != NULL);
  2290. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2291. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2292. TStrings * LocalFileList = new TStringList();
  2293. TStrings * RemoteFileList = new TStringList();
  2294. try
  2295. {
  2296. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2297. {
  2298. LocalFileList->Clear();
  2299. LocalFileList->AddObject(
  2300. FCustomCommandLocalFileList->Strings[Index],
  2301. FCustomCommandLocalFileList->Objects[Index]);
  2302. RemoteFileList->Clear();
  2303. RemoteFileList->AddObject(
  2304. FCustomCommandRemoteFileList->Strings[Index],
  2305. FCustomCommandRemoteFileList->Objects[Index]);
  2306. CustomCommand(RemoteFileList, Command, LocalFileList);
  2307. }
  2308. }
  2309. __finally
  2310. {
  2311. delete LocalFileList;
  2312. delete RemoteFileList;
  2313. }
  2314. }
  2315. //---------------------------------------------------------------------------
  2316. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2317. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2318. {
  2319. delete FCustomCommandLocalFileList;
  2320. delete FCustomCommandRemoteFileList;
  2321. // takeover ownership,
  2322. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2323. FCustomCommandLocalFileList = LocalFileList;
  2324. FCustomCommandRemoteFileList = RemoteFileList;
  2325. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2326. if (Button != NULL)
  2327. {
  2328. FCustomCommandMenu->Items->Clear();
  2329. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2330. MenuPopup(FCustomCommandMenu, Button);
  2331. }
  2332. else
  2333. {
  2334. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2335. }
  2336. }
  2337. //---------------------------------------------------------------------------
  2338. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2339. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2340. {
  2341. DebugAssert(FCapturedLog != NULL);
  2342. if ((OutputType == cotOutput) || (OutputType == cotError))
  2343. {
  2344. FCapturedLog->Add(AddedLine);
  2345. }
  2346. }
  2347. //---------------------------------------------------------------------------
  2348. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2349. TOperationSide Side)
  2350. {
  2351. return
  2352. (Side == osOther) &&
  2353. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2354. }
  2355. //---------------------------------------------------------------------------
  2356. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2357. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2358. {
  2359. TTBItemOptions O;
  2360. O = Item->Options;
  2361. TCustomDirView * DView = DirView(Side);
  2362. if ((DView->ItemFocused != NULL) &&
  2363. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2364. // when resolving links is disabled, default action is to enter the directory,
  2365. // no matter what DoubleClickAction is configured to
  2366. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2367. // Can only Edit files, but can Open/Copy even directories
  2368. ((DoubleClickAction != dcaEdit) ||
  2369. !DView->ItemIsDirectory(DView->ItemFocused)))
  2370. {
  2371. Item->Options = O << tboDefault;
  2372. }
  2373. else
  2374. {
  2375. Item->Options = O >> tboDefault;
  2376. }
  2377. }
  2378. //---------------------------------------------------------------------------
  2379. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2380. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2381. {
  2382. TCustomDirView * DView = DirView(Side);
  2383. TListItem * Item = DView->ItemFocused;
  2384. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2385. EnableFocusedOperation[Side])
  2386. {
  2387. TPoint ClientPoint;
  2388. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2389. TPoint(0, 0) : MousePos;
  2390. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2391. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2392. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2393. }
  2394. }
  2395. //---------------------------------------------------------------------------
  2396. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2397. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2398. {
  2399. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2400. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2401. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2402. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2403. Handled = true;
  2404. }
  2405. //---------------------------------------------------------------------------
  2406. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2407. {
  2408. }
  2409. //---------------------------------------------------------------------------
  2410. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2411. {
  2412. DebugAssert(FErrorList == NULL);
  2413. if (WinConfiguration->ContinueOnError)
  2414. {
  2415. FErrorList = new TStringList();
  2416. Configuration->Usage->Inc(L"ContinuationsOnError");
  2417. }
  2418. NonVisualDataModule->StartBusy();
  2419. }
  2420. //---------------------------------------------------------------------------
  2421. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2422. {
  2423. NonVisualDataModule->EndBusy();
  2424. if (FErrorList)
  2425. {
  2426. HandleErrorList(FErrorList);
  2427. }
  2428. }
  2429. //---------------------------------------------------------------------------
  2430. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2431. const TCopyParamType & CopyParam)
  2432. {
  2433. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2434. {
  2435. Configuration->Usage->Inc(L"FileMaskUses");
  2436. }
  2437. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2438. {
  2439. Configuration->Usage->Inc(L"OperationMaskUses");
  2440. }
  2441. }
  2442. //---------------------------------------------------------------------------
  2443. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2444. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2445. {
  2446. DebugAssert(!IsLocalBrowserMode());
  2447. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2448. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2449. TTransferOperationParam DefaultParam;
  2450. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2451. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2452. switch (Param.Queue)
  2453. {
  2454. case asOn:
  2455. CopyParam.Queue = true;
  2456. break;
  2457. case asOff:
  2458. CopyParam.Queue = false;
  2459. break;
  2460. case asAuto:
  2461. default:
  2462. // keep default
  2463. break;
  2464. }
  2465. bool Result =
  2466. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2467. if (Result)
  2468. {
  2469. DebugAssert(Terminal);
  2470. bool SelectionRestored = false;
  2471. TCustomDirView * DView = NULL;
  2472. if (HasDirView[Side])
  2473. {
  2474. DView = DirView(Side);
  2475. DView->SaveSelection();
  2476. DView->SaveSelectedNames();
  2477. }
  2478. UpdateCopyParamCounters(CopyParam);
  2479. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2480. DebugAssert(FTransferResumeList == NULL);
  2481. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2482. FMoveToQueue = false;
  2483. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2484. try
  2485. {
  2486. TStrings * PermanentFileList;
  2487. std::unique_ptr<TStrings> PermanentFileListOwner;
  2488. try
  2489. {
  2490. if (Side == osLocal)
  2491. {
  2492. PermanentFileList = FileList;
  2493. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2494. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2495. if (Operation == foMove)
  2496. {
  2497. ReloadLocalDirectory();
  2498. if (DView != NULL)
  2499. {
  2500. DView->RestoreSelection();
  2501. }
  2502. SelectionRestored = true;
  2503. }
  2504. }
  2505. else
  2506. {
  2507. // Clone the file list as it may refer to current directory files,
  2508. // which get destroyed, when the source directory is reloaded after foMove operation.
  2509. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2510. // But for a hotfix, we are not going to do such a big change.
  2511. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2512. PermanentFileList = PermanentFileListOwner.get();
  2513. try
  2514. {
  2515. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2516. }
  2517. __finally
  2518. {
  2519. if (Operation == foMove)
  2520. {
  2521. if (DView != NULL)
  2522. {
  2523. DView->RestoreSelection();
  2524. }
  2525. SelectionRestored = true;
  2526. }
  2527. ReloadLocalDirectory(Param.TargetDirectory);
  2528. }
  2529. }
  2530. }
  2531. catch (EAbort &)
  2532. {
  2533. if (FMoveToQueue)
  2534. {
  2535. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2536. DebugAssert(CopyParam.TransferSkipList == NULL);
  2537. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2538. if (TransferResumeList->Count > 0)
  2539. {
  2540. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2541. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2542. }
  2543. CopyParam.TransferSkipList = TransferResumeList.release();
  2544. // not really needed, just to keep it consistent with TransferResumeList
  2545. FTransferResumeList = NULL;
  2546. FMoveToQueue = false;
  2547. Configuration->Usage->Inc("MovesToBackground");
  2548. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2549. ClearTransferSourceSelection(Direction);
  2550. }
  2551. throw;
  2552. }
  2553. }
  2554. __finally
  2555. {
  2556. if (!SelectionRestored && (DView != NULL))
  2557. {
  2558. DView->DiscardSavedSelection();
  2559. }
  2560. FTransferResumeList = NULL;
  2561. }
  2562. }
  2563. return Result;
  2564. }
  2565. //---------------------------------------------------------------------------
  2566. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2567. TOperationSide Side, TStrings * FileList, void * Param)
  2568. {
  2569. DebugAssert(FileList->Count);
  2570. // We deliberately do not toggle alternative flag (Param), but use OR,
  2571. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2572. // shortcut of CurrentDeleteAlternativeAction
  2573. bool Alternative =
  2574. bool(Param) || UseAlternativeFunction();
  2575. bool Recycle;
  2576. if (IsSideLocalBrowser(Side))
  2577. {
  2578. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2579. }
  2580. else
  2581. {
  2582. Recycle =
  2583. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2584. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2585. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2586. }
  2587. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2588. if (!Result)
  2589. {
  2590. UnicodeString Query;
  2591. if (FileList->Count == 1)
  2592. {
  2593. if (IsSideLocalBrowser(Side))
  2594. {
  2595. Query = ExtractFileName(FileList->Strings[0]);
  2596. }
  2597. else
  2598. {
  2599. Query = UnixExtractFileName(FileList->Strings[0]);
  2600. }
  2601. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2602. }
  2603. else
  2604. {
  2605. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2606. }
  2607. TMessageParams Params(mpNeverAskAgainCheck);
  2608. Params.ImageName = L"Delete file";
  2609. unsigned int Answer =
  2610. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2611. if (Answer == qaNeverAskAgain)
  2612. {
  2613. Result = true;
  2614. if (Recycle)
  2615. {
  2616. WinConfiguration->ConfirmRecycling = false;
  2617. }
  2618. else
  2619. {
  2620. WinConfiguration->ConfirmDeleting = false;
  2621. }
  2622. }
  2623. else
  2624. {
  2625. Result = (Answer == qaOK);
  2626. }
  2627. }
  2628. if (Result)
  2629. {
  2630. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2631. }
  2632. return Result;
  2633. }
  2634. //---------------------------------------------------------------------------
  2635. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2636. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2637. {
  2638. TAutoBatch AutoBatch(this);
  2639. bool Result;
  2640. if ((Operation == foCopy) || (Operation == foMove))
  2641. {
  2642. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2643. }
  2644. else if (Operation == foRename)
  2645. {
  2646. DebugAssert(DirView(Side)->ItemFocused);
  2647. DirView(Side)->ItemFocused->EditCaption();
  2648. Result = true;
  2649. }
  2650. else if (Operation == foDelete)
  2651. {
  2652. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2653. }
  2654. else if (Operation == foSetProperties)
  2655. {
  2656. DirView(osRemote)->SaveSelectedNames();
  2657. Result = SetProperties(Side, FileList);
  2658. }
  2659. else if (Operation == foCustomCommand)
  2660. {
  2661. DebugAssert(Param);
  2662. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2663. DirView(osRemote)->SaveSelectedNames();
  2664. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2665. CustomCommand(FileList, *Command, NULL);
  2666. Result = true;
  2667. }
  2668. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2669. {
  2670. DebugAssert(!IsSideLocalBrowser(Side));
  2671. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2672. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2673. }
  2674. else if (Operation == foLock)
  2675. {
  2676. DebugAssert(!IsSideLocalBrowser(Side));
  2677. LockFiles(FileList, true);
  2678. Result = true;
  2679. }
  2680. else if (Operation == foUnlock)
  2681. {
  2682. DebugAssert(!IsSideLocalBrowser(Side));
  2683. LockFiles(FileList, false);
  2684. Result = true;
  2685. }
  2686. else
  2687. {
  2688. DebugFail();
  2689. }
  2690. return Result;
  2691. }
  2692. //---------------------------------------------------------------------------
  2693. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2694. {
  2695. if (Side == osCurrent)
  2696. {
  2697. Side = FCurrentSide;
  2698. }
  2699. return Side;
  2700. }
  2701. //---------------------------------------------------------------------------
  2702. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2703. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2704. {
  2705. Side = GetSide(Side);
  2706. bool Result;
  2707. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, IsSideLocalBrowser(Side), NULL);
  2708. try
  2709. {
  2710. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2711. }
  2712. __finally
  2713. {
  2714. delete FileList;
  2715. }
  2716. return Result;
  2717. }
  2718. //---------------------------------------------------------------------------
  2719. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2720. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2721. bool NoConfirmation, void * Param)
  2722. {
  2723. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2724. {
  2725. if ((Operation == foCopy) || (Operation == foMove))
  2726. {
  2727. DebugAssert(!IsLocalBrowserMode());
  2728. if (GetSide(Side) == osLocal)
  2729. {
  2730. Configuration->Usage->Inc(L"UploadsCommand");
  2731. }
  2732. else
  2733. {
  2734. Configuration->Usage->Inc(L"DownloadsCommand");
  2735. }
  2736. }
  2737. }
  2738. }
  2739. //---------------------------------------------------------------------------
  2740. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2741. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2742. {
  2743. TTransferOperationParam Param;
  2744. if ((WinConfiguration->Interface != ifCommander) ||
  2745. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2746. {
  2747. Flags &= ~cocShortCutHint;
  2748. }
  2749. TCustomDirView * DView = DirView(Side);
  2750. Param.Options =
  2751. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2752. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2753. if (FLAGSET(Flags, cocQueue))
  2754. {
  2755. Param.Queue = asOn;
  2756. }
  2757. else if (FLAGSET(Flags, cocNonQueue))
  2758. {
  2759. Param.Queue = asOff;
  2760. }
  2761. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2762. }
  2763. //---------------------------------------------------------------------------
  2764. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2765. {
  2766. try
  2767. {
  2768. if (ErrorList->Count)
  2769. {
  2770. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2771. if (MessageDialog(Message, qtError,
  2772. qaOK | qaCancel, HELP_NONE) == qaOK)
  2773. {
  2774. unsigned int Answer;
  2775. int Index = 0;
  2776. do
  2777. {
  2778. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2779. TQueryButtonAlias Aliases[2];
  2780. Aliases[0].Button = qaYes;
  2781. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2782. Aliases[1].Button = qaNo;
  2783. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2784. TMessageParams Params;
  2785. Params.Aliases = Aliases;
  2786. Params.AliasesCount = LENOF(Aliases);
  2787. Answer = MoreMessageDialog(
  2788. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2789. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2790. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2791. qaOK, HELP_NONE, &Params);
  2792. if (Answer == qaNo)
  2793. {
  2794. Index++;
  2795. }
  2796. if (Answer == qaYes)
  2797. {
  2798. Index--;
  2799. }
  2800. }
  2801. while (Answer != qaOK);
  2802. }
  2803. }
  2804. }
  2805. __finally
  2806. {
  2807. TStrings * List = ErrorList;
  2808. ErrorList = NULL;
  2809. for (int i = 0; i < List->Count; i++)
  2810. {
  2811. delete List->Objects[i];
  2812. }
  2813. delete List;
  2814. }
  2815. }
  2816. //---------------------------------------------------------------------------
  2817. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2818. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2819. {
  2820. // needed for checking filemasks, as there's no directory object
  2821. // associated with the file object
  2822. File->FullFileName = FullFileName;
  2823. TFileMasks::TParams MaskParams;
  2824. MaskParams.Size = File->Size;
  2825. MaskParams.Modification = File->Modification;
  2826. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2827. }
  2828. //---------------------------------------------------------------------------
  2829. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2830. {
  2831. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2832. Side = GetSide(Side);
  2833. TCustomDirView * DView = DirView(Side);
  2834. TListItem * FocusedItem = DView->ItemFocused;
  2835. UnicodeString Name;
  2836. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2837. {
  2838. Name = DView->ItemFileName(FocusedItem);
  2839. }
  2840. else
  2841. {
  2842. Name = LoadStr(NEW_FILE);
  2843. }
  2844. UnicodeString Names = Name;
  2845. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2846. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2847. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  2848. {
  2849. while (!Names.IsEmpty())
  2850. {
  2851. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2852. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2853. UnicodeString TargetFileName;
  2854. UnicodeString LocalFileName;
  2855. UnicodeString RootTempDir;
  2856. UnicodeString TempDir;
  2857. UnicodeString RemoteDirectory;
  2858. bool ExistingFile = false;
  2859. if (!IsSideLocalBrowser(Side))
  2860. {
  2861. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  2862. TRemoteFile * File = NULL;
  2863. if (Terminal->FileExists(Name, &File) &&
  2864. !File->IsDirectory)
  2865. {
  2866. try
  2867. {
  2868. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2869. ExistingFile = true;
  2870. }
  2871. __finally
  2872. {
  2873. delete File;
  2874. }
  2875. }
  2876. if (!ExistingFile)
  2877. {
  2878. RemoteDirectory = UnixExtractFilePath(Name);
  2879. TemporaryDirectoryForRemoteFiles(
  2880. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2881. TargetFileName = UnixExtractFileName(Name);
  2882. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2883. LocalFileName = TempDir +
  2884. // We probably do not want to trim the VMS version here
  2885. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2886. }
  2887. }
  2888. else
  2889. {
  2890. if (ExtractFilePath(Name).IsEmpty())
  2891. {
  2892. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2893. }
  2894. else
  2895. {
  2896. LocalFileName = ExpandFileName(Name);
  2897. }
  2898. TargetFileName = ExtractFileName(Name);
  2899. }
  2900. if (!ExistingFile)
  2901. {
  2902. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2903. if (NewFile)
  2904. {
  2905. int File = FileCreate(ApiPath(LocalFileName));
  2906. if (File < 0)
  2907. {
  2908. if (!RootTempDir.IsEmpty())
  2909. {
  2910. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2911. }
  2912. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2913. }
  2914. else
  2915. {
  2916. FileClose(File);
  2917. }
  2918. }
  2919. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2920. const TEditorData * ExternalEditor = NULL;
  2921. TFileMasks::TParams MaskParams; // size not known
  2922. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2923. false, MaskParams);
  2924. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2925. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  2926. }
  2927. }
  2928. }
  2929. }
  2930. //---------------------------------------------------------------------------
  2931. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2932. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2933. {
  2934. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2935. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2936. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2937. return
  2938. ((ExecuteFileBy == efInternalEditor)) ||
  2939. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2940. }
  2941. //---------------------------------------------------------------------------
  2942. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2943. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2944. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2945. UnicodeString RemoteDirectory, bool NewFile)
  2946. {
  2947. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2948. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2949. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2950. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2951. Side = GetSide(Side);
  2952. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2953. if (!IsSideLocalBrowser(Side))
  2954. {
  2955. Data->Terminal = Terminal;
  2956. Data->Queue = Queue;
  2957. Data->SessionData = CloneCurrentSessionData();
  2958. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2959. Data->RemoteDirectory = RemoteDirectory;
  2960. Data->SessionName = Terminal->SessionData->SessionName;
  2961. Data->LocalRootDirectory = LocalRootDirectory;
  2962. Data->OriginalFileName = OriginalFileName;
  2963. Data->Command = L""; // will be changed later for external editor
  2964. }
  2965. if (ExecuteFileBy == efInternalEditor)
  2966. {
  2967. if (!IsSideLocalBrowser(Side))
  2968. {
  2969. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2970. L" - " + Terminal->SessionData->SessionName;
  2971. TForm * Editor;
  2972. try
  2973. {
  2974. TNotifyEvent OnFileChanged = NULL;
  2975. TNotifyEvent OnSaveAll = NULL;
  2976. TAnyModifiedEvent OnAnyModified = NULL;
  2977. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2978. if (Terminal->IsCapable[fcBackgroundTransfers])
  2979. {
  2980. OnFileChanged = FEditorManager->FileChanged;
  2981. OnSaveAll = SaveAllInternalEditors;
  2982. OnAnyModified = AnyInternalEditorModified;
  2983. }
  2984. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2985. FEditorManager->FileReload, FEditorManager->FileClosed,
  2986. OnSaveAll, OnAnyModified,
  2987. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  2988. }
  2989. catch(...)
  2990. {
  2991. if (!LocalRootDirectory.IsEmpty())
  2992. {
  2993. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2994. }
  2995. throw;
  2996. }
  2997. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2998. }
  2999. else
  3000. {
  3001. DebugAssert(!FStandaloneEditing);
  3002. TForm * Editor =
  3003. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3004. SaveAllInternalEditors, AnyInternalEditorModified,
  3005. L"", false, SessionColor, -1, NewFile);
  3006. FLocalEditors->Add(Editor);
  3007. }
  3008. }
  3009. else
  3010. {
  3011. HANDLE Process;
  3012. if (ExecuteFileBy == efExternalEditor)
  3013. {
  3014. UnicodeString Program, Params, Dir;
  3015. Data->Command = ExternalEditor->ExternalEditor;
  3016. ReformatFileNameCommand(Data->Command);
  3017. SplitCommand(Data->Command, Program, Params, Dir);
  3018. Params = ExpandFileNameCommand(Params, FileName);
  3019. Program = ExpandEnvironmentVariables(Program);
  3020. if (!ExecuteShell(Program, Params, Process))
  3021. {
  3022. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3023. }
  3024. }
  3025. else
  3026. {
  3027. DebugAssert(!IsSideLocalBrowser(Side));
  3028. if (!ExecuteShell(FileName, L"", Process))
  3029. {
  3030. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3031. }
  3032. }
  3033. if (IsSideLocalBrowser(Side) ||
  3034. ((ExecuteFileBy == efShell) &&
  3035. !WinConfiguration->Editor.SDIShellEditor) ||
  3036. ((ExecuteFileBy == efExternalEditor) &&
  3037. !ExternalEditor->SDIExternalEditor))
  3038. {
  3039. // no need for handle
  3040. if (Process != NULL)
  3041. {
  3042. DebugCheck(CloseHandle(Process));
  3043. }
  3044. Process = INVALID_HANDLE_VALUE;
  3045. }
  3046. else
  3047. {
  3048. if (Process == NULL)
  3049. {
  3050. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3051. }
  3052. }
  3053. if (!IsSideLocalBrowser(Side))
  3054. {
  3055. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3056. }
  3057. }
  3058. }
  3059. //---------------------------------------------------------------------------
  3060. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3061. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3062. void * /*Arg*/)
  3063. {
  3064. if (Token != NULL)
  3065. {
  3066. EditorFormFileSave(static_cast<TForm *>(Token));
  3067. }
  3068. }
  3069. //---------------------------------------------------------------------------
  3070. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3071. {
  3072. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3073. {
  3074. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3075. }
  3076. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3077. }
  3078. //---------------------------------------------------------------------------
  3079. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3080. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3081. void * Arg)
  3082. {
  3083. if ((Token != NULL) &&
  3084. IsEditorFormModified(static_cast<TForm *>(Token)))
  3085. {
  3086. *static_cast<bool *>(Arg) = true;
  3087. }
  3088. }
  3089. //---------------------------------------------------------------------------
  3090. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3091. TObject * /*Sender*/, bool & Modified)
  3092. {
  3093. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3094. {
  3095. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3096. {
  3097. Modified = true;
  3098. }
  3099. }
  3100. if (!Modified)
  3101. {
  3102. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3103. }
  3104. }
  3105. //---------------------------------------------------------------------------
  3106. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3107. {
  3108. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3109. }
  3110. //---------------------------------------------------------------------------
  3111. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3112. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3113. UnicodeString & Result, UnicodeString & RootDirectory)
  3114. {
  3115. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3116. {
  3117. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3118. Result = RootDirectory;
  3119. }
  3120. else
  3121. {
  3122. RootDirectory = L"";
  3123. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3124. Result = IncludeTrailingBackslash(Result);
  3125. }
  3126. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3127. {
  3128. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3129. }
  3130. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3131. {
  3132. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3133. {
  3134. RemoteDirectory.Delete(1, 1);
  3135. }
  3136. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3137. }
  3138. if (!ForceDirectories(ApiPath(Result)))
  3139. {
  3140. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3141. }
  3142. }
  3143. //---------------------------------------------------------------------------
  3144. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3145. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3146. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3147. {
  3148. DebugAssert(!IsLocalBrowserMode());
  3149. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3150. if (ForceText)
  3151. {
  3152. CopyParam.TransferMode = tmAscii;
  3153. }
  3154. TemporaryFileCopyParam(CopyParam);
  3155. if (AllFiles)
  3156. {
  3157. CopyParam.IncludeFileMask = TFileMasks();
  3158. }
  3159. if (TempDir.IsEmpty())
  3160. {
  3161. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3162. }
  3163. DebugAssert(!FAutoOperation);
  3164. FAutoOperation = AutoOperation;
  3165. Terminal->ExceptionOnFail = true;
  3166. try
  3167. {
  3168. try
  3169. {
  3170. // turn off confirmations, as for MDI editors we may possibly download
  3171. // the same file over
  3172. Terminal->CopyToLocal(
  3173. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3174. if (GetTargetNames)
  3175. {
  3176. for (int i = 0; i < FileList->Count; i++)
  3177. {
  3178. FileList->Strings[i] =
  3179. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3180. }
  3181. }
  3182. }
  3183. catch(...)
  3184. {
  3185. if (!RootTempDir.IsEmpty())
  3186. {
  3187. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3188. }
  3189. throw;
  3190. }
  3191. }
  3192. __finally
  3193. {
  3194. FAutoOperation = false;
  3195. Terminal->ExceptionOnFail = false;
  3196. }
  3197. }
  3198. //---------------------------------------------------------------------------
  3199. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3200. {
  3201. // Do not waste time checking for default editor list the next time,
  3202. // or testing if default editor is not notepad.
  3203. bool TryNextTime = false;
  3204. UnicodeString UsageState;
  3205. if (!WinConfiguration->EditorList->IsDefaultList())
  3206. {
  3207. UsageState = "H";
  3208. }
  3209. else
  3210. {
  3211. UnicodeString Executable;
  3212. UnicodeString ExecutableDescription;
  3213. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3214. {
  3215. UnicodeString Message =
  3216. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3217. unsigned int Answer =
  3218. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3219. if (Answer != qaOK)
  3220. {
  3221. UsageState = "R";
  3222. }
  3223. else
  3224. {
  3225. UsageState = "A";
  3226. TEditorData EditorData;
  3227. EditorData.Editor = edExternal;
  3228. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3229. EditorData.ExternalEditorOptionsAutodetect();
  3230. TEditorList EditorList;
  3231. EditorList = *WinConfiguration->EditorList;
  3232. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3233. WinConfiguration->EditorList = &EditorList;
  3234. }
  3235. }
  3236. }
  3237. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3238. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3239. }
  3240. //---------------------------------------------------------------------------
  3241. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3242. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3243. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3244. {
  3245. if (ExecuteFileBy == efDefaultEditor)
  3246. {
  3247. if (!WinConfiguration->OfferedEditorAutoConfig)
  3248. {
  3249. EditorAutoConfig();
  3250. }
  3251. const TEditorPreferences * Editor =
  3252. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3253. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3254. {
  3255. ExecuteFileBy = efInternalEditor;
  3256. ExternalEditor = NULL;
  3257. }
  3258. else if (Editor->Data->Editor == edOpen)
  3259. {
  3260. ExecuteFileBy = efShell;
  3261. ExternalEditor = NULL;
  3262. }
  3263. else
  3264. {
  3265. ExecuteFileBy = efExternalEditor;
  3266. ExternalEditor = Editor->Data;
  3267. }
  3268. }
  3269. }
  3270. //---------------------------------------------------------------------------
  3271. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3272. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3273. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3274. {
  3275. UnicodeString OriginalFileName;
  3276. UnicodeString LocalRootDirectory;
  3277. UnicodeString RemoteDirectory;
  3278. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3279. IsSideLocalBrowser(Side), MaskParams);
  3280. UnicodeString Counter;
  3281. UnicodeString LocalFileName;
  3282. bool Handled = false;
  3283. if (!IsSideLocalBrowser(Side))
  3284. {
  3285. // We need to trim VMS version here, so that we use name without version
  3286. // when uploading back to create a new version of the file
  3287. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3288. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3289. TObject * Token = NULL;
  3290. UnicodeString LocalDirectory;
  3291. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3292. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3293. LocalDirectory))
  3294. {
  3295. if (Token != NULL)
  3296. {
  3297. TForm * Form = dynamic_cast<TForm *>(Token);
  3298. if (Form->WindowState == wsMinimized)
  3299. {
  3300. ShowWindow(Form->Handle, SW_RESTORE);
  3301. }
  3302. else
  3303. {
  3304. Form->SetFocus();
  3305. }
  3306. Handled = true;
  3307. }
  3308. else
  3309. {
  3310. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3311. }
  3312. }
  3313. if (!Handled)
  3314. {
  3315. TStringList * FileList1 = new TStringList();
  3316. try
  3317. {
  3318. FileList1->AddObject(FullFileName, Object);
  3319. TemporarilyDownloadFiles(FileList1,
  3320. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3321. LocalRootDirectory, LocalDirectory, true, true, true);
  3322. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3323. }
  3324. __finally
  3325. {
  3326. delete FileList1;
  3327. }
  3328. switch (ExecuteFileBy)
  3329. {
  3330. case efShell:
  3331. Counter = "RemoteFilesExecuted";
  3332. break;
  3333. case efInternalEditor:
  3334. Counter = "RemoteFilesOpenedInInternalEditor";
  3335. break;
  3336. case efExternalEditor:
  3337. Counter = "RemoteFilesOpenedInExternalEditor";
  3338. break;
  3339. default:
  3340. DebugFail();
  3341. }
  3342. }
  3343. }
  3344. else
  3345. {
  3346. LocalFileName = FullFileName;
  3347. OriginalFileName = ExtractFileName(FullFileName);
  3348. switch (ExecuteFileBy)
  3349. {
  3350. case efShell:
  3351. Counter = "LocalFilesExecuted";
  3352. break;
  3353. case efInternalEditor:
  3354. Counter = "LocalFilesOpenedInInternalEditor";
  3355. break;
  3356. case efExternalEditor:
  3357. Counter = "LocalFilesOpenedInExternalEditor";
  3358. break;
  3359. default:
  3360. DebugFail();
  3361. }
  3362. }
  3363. if (!Handled)
  3364. {
  3365. Configuration->Usage->Inc(Counter);
  3366. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3367. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3368. }
  3369. }
  3370. //---------------------------------------------------------------------------
  3371. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3372. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3373. bool AllSelected, bool OnFocused)
  3374. {
  3375. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3376. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3377. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3378. Side = GetSide(Side);
  3379. TCustomDirView * DView = DirView(Side);
  3380. TStrings * FileList =
  3381. AllSelected ?
  3382. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3383. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3384. try
  3385. {
  3386. DebugAssert(AllSelected || (FileList->Count == 1));
  3387. for (int i = 0; i < FileList->Count; i++)
  3388. {
  3389. UnicodeString ListFileName = FileList->Strings[i];
  3390. UnicodeString FileNameOnly =
  3391. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3392. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3393. if (!DView->ItemIsDirectory(Item))
  3394. {
  3395. UnicodeString FullFileName;
  3396. if (!IsSideLocalBrowser(Side))
  3397. {
  3398. FullFileName = DirView(Side)->Path + ListFileName;
  3399. }
  3400. else
  3401. {
  3402. FullFileName = ListFileName;
  3403. }
  3404. TFileMasks::TParams MaskParams;
  3405. MaskParams.Size = DView->ItemFileSize(Item);
  3406. TDateTimePrecision Precision;
  3407. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3408. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3409. FileList->Objects[i], MaskParams);
  3410. }
  3411. }
  3412. }
  3413. __finally
  3414. {
  3415. delete FileList;
  3416. }
  3417. }
  3418. //---------------------------------------------------------------------------
  3419. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3420. {
  3421. CopyParam.FileNameCase = ncNoChange;
  3422. CopyParam.PreserveRights = false;
  3423. CopyParam.PreserveReadOnly = false;
  3424. CopyParam.ReplaceInvalidChars = true;
  3425. CopyParam.IncludeFileMask = TFileMasks();
  3426. CopyParam.NewerOnly = false;
  3427. CopyParam.FileMask = L"";
  3428. }
  3429. //---------------------------------------------------------------------------
  3430. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3431. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3432. {
  3433. TTerminalManager * Manager = TTerminalManager::Instance();
  3434. if (!IsActiveTerminal(Data->Terminal))
  3435. {
  3436. if (!NonVisualDataModule->Busy)
  3437. {
  3438. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3439. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3440. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3441. if (Data->Terminal == NULL)
  3442. {
  3443. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3444. if (SameSiteTerminal != NULL)
  3445. {
  3446. UnicodeString Message =
  3447. FMTLOAD(EDIT_SESSION_REATTACH,
  3448. (FileNameOnly, Data->SessionName, FileNameOnly));
  3449. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3450. {
  3451. Data->Terminal = Terminal;
  3452. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3453. Data->SessionName = Terminal->SessionData->SessionName;
  3454. // We might also overwrite session data
  3455. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3456. }
  3457. else
  3458. {
  3459. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3460. Abort();
  3461. }
  3462. }
  3463. }
  3464. // foreground session should reconnect itself
  3465. else if (Terminal != Data->Terminal)
  3466. {
  3467. UnicodeString Message =
  3468. MainInstructions(
  3469. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3470. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3471. {
  3472. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3473. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3474. }
  3475. else
  3476. {
  3477. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3478. Abort();
  3479. }
  3480. }
  3481. }
  3482. if (!IsActiveTerminal(Data->Terminal))
  3483. {
  3484. Configuration->Usage->Inc(L"EditInactiveSession");
  3485. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3486. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3487. (ExtractFileName(FileName), Data->SessionName)));
  3488. }
  3489. }
  3490. TStrings * FileList = new TStringList();
  3491. try
  3492. {
  3493. FileList->Add(FileName);
  3494. // consider using the same settings (preset) as when the file was downloaded
  3495. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3496. TemporaryFileCopyParam(CopyParam);
  3497. if (Data->ForceText)
  3498. {
  3499. CopyParam.TransferMode = tmAscii;
  3500. }
  3501. // so i do not need to worry if masking algorithm works in all cases
  3502. // ("" means "copy file name", no masking is actually done)
  3503. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3504. {
  3505. CopyParam.FileMask = L"";
  3506. }
  3507. else
  3508. {
  3509. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3510. }
  3511. DebugAssert(Data->Queue != NULL);
  3512. int Params = cpNoConfirmation | cpTemporary;
  3513. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3514. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3515. QueueItem->CompleteEvent = UploadCompleteEvent;
  3516. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3517. }
  3518. __finally
  3519. {
  3520. delete FileList;
  3521. }
  3522. }
  3523. //---------------------------------------------------------------------------
  3524. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3525. const UnicodeString FileName, const TEditedFileData * Data)
  3526. {
  3527. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3528. DebugAssert(!NonVisualDataModule->Busy);
  3529. if (!IsActiveTerminal(Data->Terminal))
  3530. {
  3531. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3532. (ExtractFileName(FileName), Data->SessionName)));
  3533. }
  3534. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3535. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3536. NonVisualDataModule->StartBusy();
  3537. try
  3538. {
  3539. std::unique_ptr<TRemoteFile> File;
  3540. UnicodeString RemoteFileName =
  3541. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3542. Terminal->ExceptionOnFail = true;
  3543. try
  3544. {
  3545. TRemoteFile * AFile = NULL;
  3546. Terminal->ReadFile(RemoteFileName, AFile);
  3547. File.reset(AFile);
  3548. if (!File->HaveFullFileName)
  3549. {
  3550. File->FullFileName = RemoteFileName;
  3551. }
  3552. }
  3553. __finally
  3554. {
  3555. Terminal->ExceptionOnFail = false;
  3556. }
  3557. std::unique_ptr<TStrings> FileList(new TStringList());
  3558. FileList->AddObject(RemoteFileName, File.get());
  3559. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3560. UnicodeString TempDir = ExtractFilePath(FileName);
  3561. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3562. TempDir, true, true, true);
  3563. // sanity check, the target file name should be still the same
  3564. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3565. }
  3566. __finally
  3567. {
  3568. NonVisualDataModule->EndBusy();
  3569. // it actually may not exist anymore...
  3570. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3571. }
  3572. }
  3573. //---------------------------------------------------------------------------
  3574. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3575. const TEditedFileData * Data, bool & KeepOpen)
  3576. {
  3577. // Command is set for external editors only (not for "shell" open).
  3578. if (!Data->Command.IsEmpty())
  3579. {
  3580. bool AnyFound = false;
  3581. bool AnyMDI = false;
  3582. bool AnyNonMDI = false;
  3583. bool AnyDetect = false;
  3584. TEditorList * EditorList = new TEditorList();
  3585. try
  3586. {
  3587. *EditorList = *WinConfiguration->EditorList;
  3588. for (int i = 0; i < EditorList->Count; i++)
  3589. {
  3590. const TEditorPreferences * Editor = EditorList->Editors[i];
  3591. if ((Editor->Data->Editor == edExternal) &&
  3592. (Editor->Data->ExternalEditor == Data->Command))
  3593. {
  3594. AnyFound = true;
  3595. if (Editor->Data->SDIExternalEditor)
  3596. {
  3597. AnyNonMDI = true;
  3598. if (Editor->Data->DetectMDIExternalEditor)
  3599. {
  3600. AnyDetect = true;
  3601. }
  3602. }
  3603. else
  3604. {
  3605. AnyMDI = true;
  3606. }
  3607. }
  3608. }
  3609. bool EnableMDI = false;
  3610. bool DisableDetect = false;
  3611. if (AnyMDI)
  3612. {
  3613. KeepOpen = true;
  3614. if (AnyNonMDI)
  3615. {
  3616. // there is at least one instance of the editor with MDI support enabled,
  3617. // and one with disabled, enable it for all instances
  3618. EnableMDI = true;
  3619. }
  3620. }
  3621. else if (AnyFound && !AnyDetect)
  3622. {
  3623. // at least once instance found but all have MDI autodetection disabled
  3624. // => close the file (default action)
  3625. }
  3626. else
  3627. {
  3628. // no instance of the editor has MDI support enabled
  3629. TMessageParams Params;
  3630. if (AnyFound)
  3631. {
  3632. // there is at least one instance of the editor
  3633. Params.Params |= mpNeverAskAgainCheck;
  3634. }
  3635. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3636. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3637. switch (Answer)
  3638. {
  3639. case qaNeverAskAgain:
  3640. DisableDetect = true;
  3641. break;
  3642. case qaNo:
  3643. EnableMDI = true;
  3644. KeepOpen = true;
  3645. break;
  3646. }
  3647. }
  3648. if (AnyFound && (EnableMDI || DisableDetect))
  3649. {
  3650. bool Changed = false;
  3651. for (int i = 0; i < EditorList->Count; i++)
  3652. {
  3653. const TEditorPreferences * Editor = EditorList->Editors[i];
  3654. if ((Editor->Data->Editor == edExternal) &&
  3655. (Editor->Data->ExternalEditor == Data->Command) &&
  3656. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3657. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3658. {
  3659. Changed = true;
  3660. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3661. if (EnableMDI)
  3662. {
  3663. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3664. }
  3665. if (DisableDetect)
  3666. {
  3667. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3668. }
  3669. EditorList->Change(i, UpdatedEditor);
  3670. }
  3671. }
  3672. if (Changed)
  3673. {
  3674. WinConfiguration->EditorList = EditorList;
  3675. }
  3676. }
  3677. }
  3678. __finally
  3679. {
  3680. delete EditorList;
  3681. }
  3682. }
  3683. else
  3684. {
  3685. // "open" case
  3686. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3687. qaOK, HELP_APP_EARLY_CLOSED);
  3688. }
  3689. }
  3690. //---------------------------------------------------------------------------
  3691. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3692. {
  3693. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3694. }
  3695. //---------------------------------------------------------------------------
  3696. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3697. {
  3698. SideEnter(osRemote);
  3699. }
  3700. //---------------------------------------------------------------------------
  3701. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3702. {
  3703. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3704. SideEnter(osRemote);
  3705. }
  3706. //---------------------------------------------------------------------------
  3707. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3708. {
  3709. FCurrentSide = Side;
  3710. if (Visible)
  3711. {
  3712. UpdateControls();
  3713. }
  3714. }
  3715. //---------------------------------------------------------------------------
  3716. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3717. TStrings * FileList, bool Alternative)
  3718. {
  3719. TCustomDirView * DView = DirView(Side);
  3720. DView->SaveSelection();
  3721. DView->SaveSelectedNames();
  3722. DebugAssert(!FAlternativeDelete);
  3723. FAlternativeDelete = Alternative;
  3724. try
  3725. {
  3726. if (!IsSideLocalBrowser(Side))
  3727. {
  3728. DebugAssert(Terminal != NULL);
  3729. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3730. }
  3731. else
  3732. {
  3733. if (IsLocalBrowserMode())
  3734. {
  3735. Configuration->Usage->Inc(L"LocalLocalDeletes");
  3736. }
  3737. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  3738. FProgressSide = FCurrentSide;
  3739. try
  3740. {
  3741. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3742. }
  3743. __finally
  3744. {
  3745. ReloadLocalDirectory();
  3746. }
  3747. }
  3748. FAlternativeDelete = false;
  3749. }
  3750. catch(...)
  3751. {
  3752. FAlternativeDelete = false;
  3753. DView->DiscardSavedSelection();
  3754. throw;
  3755. }
  3756. DView->RestoreSelection();
  3757. }
  3758. //---------------------------------------------------------------------------
  3759. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3760. {
  3761. DebugAssert(Terminal);
  3762. DebugAssert(!IsLocalBrowserMode());
  3763. RemoteDirView->SaveSelection();
  3764. RemoteDirView->SaveSelectedNames();
  3765. try
  3766. {
  3767. if (Lock)
  3768. {
  3769. Terminal->LockFiles(FileList);
  3770. }
  3771. else
  3772. {
  3773. Terminal->UnlockFiles(FileList);
  3774. }
  3775. }
  3776. catch(...)
  3777. {
  3778. RemoteDirView->DiscardSavedSelection();
  3779. throw;
  3780. }
  3781. RemoteDirView->RestoreSelection();
  3782. }
  3783. //---------------------------------------------------------------------------
  3784. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3785. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3786. bool NoConfirmation, bool Move)
  3787. {
  3788. DebugAssert(Terminal != NULL);
  3789. DebugAssert(!IsLocalBrowserMode());
  3790. // update Terminal->StateData->RemoteDirectory
  3791. UpdateSession(Terminal);
  3792. if (Session == NULL)
  3793. {
  3794. Session = TTerminalManager::Instance()->ActiveTerminal;
  3795. }
  3796. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3797. {
  3798. if (RemoteDriveView->DropTarget != NULL)
  3799. {
  3800. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3801. }
  3802. else if (RemoteDirView->DropTarget != NULL)
  3803. {
  3804. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3805. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3806. }
  3807. else
  3808. {
  3809. Target = RemoteDirView->Path;
  3810. }
  3811. }
  3812. else
  3813. {
  3814. Target = Session->StateData->RemoteDirectory;
  3815. }
  3816. Target = UnixIncludeTrailingBackslash(Target);
  3817. if (FileList->Count == 1)
  3818. {
  3819. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3820. }
  3821. else
  3822. {
  3823. FileMask = AnyMask;
  3824. }
  3825. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  3826. bool Result = true;
  3827. if (!NoConfirmation)
  3828. {
  3829. bool Multi = (FileList->Count > 1);
  3830. if (Move)
  3831. {
  3832. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3833. }
  3834. else
  3835. {
  3836. TStrings * Sessions = TTerminalManager::Instance()->SessionList;
  3837. TStrings * Directories = new TStringList;
  3838. try
  3839. {
  3840. for (int Index = 0; Index < Sessions->Count; Index++)
  3841. {
  3842. TManagedTerminal * Terminal =
  3843. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3844. Directories->Add(Terminal->StateData->RemoteDirectory);
  3845. }
  3846. TDirectRemoteCopy AllowDirectCopy;
  3847. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  3848. {
  3849. DebugAssert(DirectCopy);
  3850. AllowDirectCopy = drcAllow;
  3851. }
  3852. else if (Terminal->IsCapable[fcSecondaryShell])
  3853. {
  3854. DebugAssert(DirectCopy);
  3855. AllowDirectCopy = drcConfirmCommandSession;
  3856. }
  3857. else
  3858. {
  3859. DebugAssert(!DirectCopy);
  3860. AllowDirectCopy = drcDisallow;
  3861. }
  3862. void * ASession = Session;
  3863. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3864. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3865. Session = static_cast<TManagedTerminal *>(ASession);
  3866. }
  3867. __finally
  3868. {
  3869. delete Directories;
  3870. }
  3871. }
  3872. }
  3873. return Result;
  3874. }
  3875. //---------------------------------------------------------------------------
  3876. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3877. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3878. {
  3879. DebugAssert(!IsLocalBrowserMode());
  3880. bool DirectCopy;
  3881. UnicodeString Target, FileMask;
  3882. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3883. if (Result)
  3884. {
  3885. if (!Move && !DirectCopy)
  3886. {
  3887. Configuration->Usage->Inc("RemoteCopyTemp");
  3888. UnicodeString RootTempDir;
  3889. UnicodeString TempDir;
  3890. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3891. TStrings * TemporaryFilesList = new TStringList();
  3892. try
  3893. {
  3894. TMakeLocalFileListParams MakeFileListParam;
  3895. MakeFileListParam.FileList = TemporaryFilesList;
  3896. MakeFileListParam.FileTimes = NULL;
  3897. MakeFileListParam.IncludeDirs = true;
  3898. MakeFileListParam.Recursive = false;
  3899. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  3900. TTerminalManager::Instance()->ActiveSession = Session;
  3901. if (TemporaryFilesList->Count > 0)
  3902. {
  3903. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3904. CopyParam.FileMask = FileMask;
  3905. DebugAssert(!FAutoOperation);
  3906. FAutoOperation = true;
  3907. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3908. }
  3909. }
  3910. __finally
  3911. {
  3912. delete TemporaryFilesList;
  3913. FAutoOperation = false;
  3914. if (!RootTempDir.IsEmpty())
  3915. {
  3916. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3917. }
  3918. }
  3919. }
  3920. else
  3921. {
  3922. RemoteDirView->SaveSelection();
  3923. RemoteDirView->SaveSelectedNames();
  3924. try
  3925. {
  3926. if (Move)
  3927. {
  3928. Configuration->Usage->Inc("RemoteMove");
  3929. Terminal->MoveFiles(FileList, Target, FileMask);
  3930. }
  3931. else
  3932. {
  3933. Configuration->Usage->Inc("RemoteCopyDirect");
  3934. DebugAssert(DirectCopy);
  3935. DebugAssert(Session == Terminal);
  3936. if (Terminal->IsCapable[fcRemoteCopy] ||
  3937. Terminal->CommandSessionOpened ||
  3938. CommandSessionFallback())
  3939. {
  3940. Terminal->CopyFiles(FileList, Target, FileMask);
  3941. }
  3942. }
  3943. }
  3944. catch(...)
  3945. {
  3946. RemoteDirView->DiscardSavedSelection();
  3947. throw;
  3948. }
  3949. RemoteDirView->RestoreSelection();
  3950. }
  3951. }
  3952. return Result;
  3953. }
  3954. //---------------------------------------------------------------------------
  3955. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3956. const UnicodeString & Path, TRemoteProperties & Properties)
  3957. {
  3958. DebugAssert(!IsLocalBrowserMode());
  3959. Properties.Valid = Properties.Valid << vpEncrypt;
  3960. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3961. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3962. }
  3963. //---------------------------------------------------------------------------
  3964. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3965. {
  3966. Side = GetSide(Side);
  3967. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3968. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  3969. UnicodeString Name = LoadStr(NEW_FOLDER);
  3970. int AllowedChanges =
  3971. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  3972. bool SaveSettings = false;
  3973. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3974. {
  3975. TWindowLock Lock(this);
  3976. if (!IsSideLocalBrowser(Side))
  3977. {
  3978. if (SaveSettings)
  3979. {
  3980. GUIConfiguration->NewDirectoryProperties = Properties;
  3981. }
  3982. CreateRemoteDirectory(Name, Properties);
  3983. }
  3984. else
  3985. {
  3986. DirView(Side)->CreateDirectory(Name);
  3987. }
  3988. }
  3989. }
  3990. //---------------------------------------------------------------------------
  3991. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3992. {
  3993. TWindowLock Lock(this);
  3994. DirView(Side)->ExecuteHomeDirectory();
  3995. }
  3996. //---------------------------------------------------------------------------
  3997. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3998. {
  3999. DoOpenDirectoryDialog(odBrowse, Side);
  4000. }
  4001. //---------------------------------------------------------------------------
  4002. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4003. {
  4004. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4005. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4006. bool Result = !Path.IsEmpty();
  4007. if (Result)
  4008. {
  4009. // While we might get here when the session is closed (from location profiles),
  4010. // it's not a problem as the Path setter is noop then
  4011. DirView(Side)->Path = Path;
  4012. }
  4013. return Result;
  4014. }
  4015. //---------------------------------------------------------------------------
  4016. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4017. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4018. bool & Close)
  4019. {
  4020. // terminal can be already closed (e.g. dropped connection)
  4021. if (Terminal != NULL)
  4022. {
  4023. try
  4024. {
  4025. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  4026. }
  4027. catch(...)
  4028. {
  4029. if (!Terminal->Active)
  4030. {
  4031. Close = true;
  4032. }
  4033. throw;
  4034. }
  4035. }
  4036. }
  4037. //---------------------------------------------------------------------------
  4038. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4039. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4040. bool & Close)
  4041. {
  4042. // terminal can be already closed (e.g. dropped connection)
  4043. if (Terminal != NULL)
  4044. {
  4045. Configuration->Usage->Inc(L"ChecksumCalculated");
  4046. try
  4047. {
  4048. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  4049. }
  4050. catch(...)
  4051. {
  4052. if (!Terminal->Active)
  4053. {
  4054. Close = true;
  4055. }
  4056. throw;
  4057. }
  4058. }
  4059. }
  4060. //---------------------------------------------------------------------------
  4061. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4062. {
  4063. bool Result;
  4064. if (!IsSideLocalBrowser(Side))
  4065. {
  4066. TRemoteTokenList * GroupList = NULL;
  4067. TRemoteTokenList * UserList = NULL;
  4068. try
  4069. {
  4070. TRemoteProperties CurrentProperties;
  4071. if (Terminal->LoadFilesProperties(FileList))
  4072. {
  4073. RemoteDirView->Invalidate();
  4074. }
  4075. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4076. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4077. if (CapableGroupChanging || CapableOwnerChanging)
  4078. {
  4079. if (CapableGroupChanging)
  4080. {
  4081. GroupList = Terminal->Groups->Duplicate();
  4082. }
  4083. if (CapableOwnerChanging)
  4084. {
  4085. UserList = Terminal->Users->Duplicate();
  4086. }
  4087. TRemoteDirectory * Files = Terminal->Files;
  4088. int Count = Files->Count;
  4089. if (Count > 100)
  4090. {
  4091. Count = 100;
  4092. }
  4093. for (int Index = 0; Index < Count; Index++)
  4094. {
  4095. TRemoteFile * File = Files->Files[Index];
  4096. if (CapableGroupChanging)
  4097. {
  4098. GroupList->AddUnique(File->Group);
  4099. }
  4100. if (CapableOwnerChanging)
  4101. {
  4102. UserList->AddUnique(File->Owner);
  4103. }
  4104. }
  4105. // if we haven't collected tokens for all files in current directory,
  4106. // make sure we collect them at least for all selected files.
  4107. // (note that so far the files in FileList has to be from current direcotry)
  4108. if (Count < Files->Count)
  4109. {
  4110. for (int Index = 0; Index < FileList->Count; Index++)
  4111. {
  4112. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4113. if (CapableGroupChanging)
  4114. {
  4115. GroupList->AddUnique(File->Group);
  4116. }
  4117. if (CapableOwnerChanging)
  4118. {
  4119. UserList->AddUnique(File->Owner);
  4120. }
  4121. }
  4122. }
  4123. }
  4124. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4125. int Flags = 0;
  4126. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4127. if (CapableOwnerChanging) Flags |= cpOwner;
  4128. if (CapableGroupChanging) Flags |= cpGroup;
  4129. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4130. if (Terminal->IsCapable[fcCalculatingChecksum])
  4131. {
  4132. CalculateChecksumEvent = CalculateChecksum;
  4133. }
  4134. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4135. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4136. TRemoteProperties NewProperties = CurrentProperties;
  4137. Result =
  4138. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4139. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4140. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4141. CalculateSize, CalculateChecksumEvent);
  4142. if (Result)
  4143. {
  4144. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4145. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4146. }
  4147. }
  4148. __finally
  4149. {
  4150. delete GroupList;
  4151. delete UserList;
  4152. }
  4153. }
  4154. else
  4155. {
  4156. DirView(Side)->DisplayPropertiesMenu();
  4157. Result = true;
  4158. }
  4159. return Result;
  4160. }
  4161. //---------------------------------------------------------------------------
  4162. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4163. {
  4164. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4165. {
  4166. FIgnoreNextDialogChar = Key;
  4167. }
  4168. Key = 0;
  4169. }
  4170. //---------------------------------------------------------------------------
  4171. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4172. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4173. {
  4174. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4175. if (Command != NULL)
  4176. {
  4177. KeyProcessed(Key, Shift);
  4178. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4179. {
  4180. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4181. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4182. false, false, const_cast<TCustomCommandType *>(Command));
  4183. }
  4184. }
  4185. }
  4186. //---------------------------------------------------------------------------
  4187. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4188. {
  4189. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4190. {
  4191. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4192. }
  4193. if (!DirView(osCurrent)->IsEditing())
  4194. {
  4195. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4196. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4197. {
  4198. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4199. // But so far it's not a problem, as it does what we want.
  4200. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4201. if (((Action->ShortCut == KeyShortCut) ||
  4202. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4203. AllowedAction(Action, aaShortCut))
  4204. {
  4205. // Has to be called before the action as the dialog char is already in queue.
  4206. // So when the action consumes message queue, we already need to have the
  4207. // FIgnoreNextDialogChar set
  4208. KeyProcessed(Key, Shift);
  4209. // Reset reference to previous component (when menu/toolbar was clicked).
  4210. // Needed to detect that action was invoked by keyboard shortcut
  4211. // in TNonVisualDataModule::ExplorerActionsExecute
  4212. Action->ActionComponent = NULL;
  4213. Action->Execute();
  4214. return;
  4215. }
  4216. }
  4217. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4218. {
  4219. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4220. {
  4221. KeyProcessed(Key, Shift);
  4222. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4223. return;
  4224. }
  4225. }
  4226. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4227. {
  4228. KeyProcessed(Key, Shift);
  4229. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4230. }
  4231. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4232. if (IsCustomShortCut(CustomShortCut))
  4233. {
  4234. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4235. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4236. if (WinConfiguration->SharedBookmarks != NULL)
  4237. {
  4238. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4239. if ((Bookmark != NULL) &&
  4240. OpenBookmark(FCurrentSide, Bookmark))
  4241. {
  4242. KeyProcessed(Key, Shift);
  4243. }
  4244. }
  4245. }
  4246. }
  4247. TForm::KeyDown(Key, Shift);
  4248. }
  4249. //---------------------------------------------------------------------------
  4250. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4251. {
  4252. bool OpenedTerminal = false;
  4253. UnicodeString ProtocolName;
  4254. UnicodeString ProtocolNameHint;
  4255. UnicodeString SecurityProtocolName;
  4256. UnicodeString DurationHint;
  4257. // See the comment in TScpCommanderForm::UpdateControls()
  4258. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4259. if (ActiveTerminal != NULL)
  4260. {
  4261. // Active is not enough here
  4262. if (ActiveTerminal->Status == ssOpened)
  4263. {
  4264. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4265. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4266. if (FileSystemInfo.ProtocolName.IsEmpty())
  4267. {
  4268. ProtocolName = SessionInfo.ProtocolName;
  4269. }
  4270. else
  4271. {
  4272. ProtocolName = FileSystemInfo.ProtocolName;
  4273. }
  4274. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4275. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4276. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4277. OpenedTerminal = true;
  4278. }
  4279. else if (ActiveTerminal->Status > ssClosed)
  4280. {
  4281. ProtocolName = LoadStr(STATUS_CONNECTING);
  4282. }
  4283. else
  4284. {
  4285. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4286. }
  4287. }
  4288. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4289. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4290. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4291. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4292. if (ProtocolStatusPanel->Caption != ProtocolName)
  4293. {
  4294. ProtocolStatusPanel->Caption = ProtocolName;
  4295. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4296. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4297. }
  4298. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4299. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4300. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4301. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4302. SecurityStatusPanel->Enabled = SecurityEnabled;
  4303. UnicodeString SecurityProtocolHint;
  4304. if (SecurityEnabled)
  4305. {
  4306. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4307. }
  4308. else
  4309. {
  4310. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4311. }
  4312. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4313. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4314. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4315. DurationStatusPanel->Hint = DurationHint;
  4316. UpdateStatusBar();
  4317. }
  4318. //---------------------------------------------------------------------------
  4319. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4320. {
  4321. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4322. DebugAssert(SessionStatusBar != NULL);
  4323. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4324. if (!FNote.IsEmpty())
  4325. {
  4326. NoteStatusPanel->Caption = FNote;
  4327. }
  4328. else
  4329. {
  4330. UpdateStatusPanelText(NoteStatusPanel);
  4331. }
  4332. NoteStatusPanel->Hint = FNoteHints;
  4333. UnicodeString LoginTime;
  4334. // See the comment in TScpCommanderForm::UpdateControls()
  4335. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4336. // Active is not enough here
  4337. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4338. {
  4339. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4340. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4341. }
  4342. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4343. DurationStatusPanel->Caption = LoginTime;
  4344. }
  4345. //---------------------------------------------------------------------------
  4346. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4347. {
  4348. Panel->Caption = L"";
  4349. }
  4350. //---------------------------------------------------------------------------
  4351. void __fastcall TCustomScpExplorerForm::Idle()
  4352. {
  4353. if (FShowing || FStandaloneEditing)
  4354. {
  4355. FEditorManager->Check();
  4356. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4357. // that needs to know if queue view is visible (and it may be closed after queue update)
  4358. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4359. }
  4360. if (FShowing)
  4361. {
  4362. if (!NonVisualDataModule->Busy &&
  4363. // Menu is opened or drag&drop is going on
  4364. (Mouse->Capture == NULL))
  4365. {
  4366. if (FRefreshRemoteDirectory)
  4367. {
  4368. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4369. {
  4370. Terminal->RefreshDirectory();
  4371. }
  4372. FRefreshRemoteDirectory = false;
  4373. }
  4374. if (FRefreshLocalDirectory)
  4375. {
  4376. ReloadLocalDirectory();
  4377. FRefreshLocalDirectory = false;
  4378. }
  4379. if (WinConfiguration->RefreshRemotePanel)
  4380. {
  4381. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4382. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4383. {
  4384. RemoteDirView->ReloadDirectory();
  4385. }
  4386. }
  4387. }
  4388. }
  4389. if (FShowing || FStandaloneEditing)
  4390. {
  4391. if (FQueueStatusInvalidated)
  4392. {
  4393. UpdateQueueStatus(false);
  4394. }
  4395. RefreshQueueItems();
  4396. }
  4397. if (FShowing)
  4398. {
  4399. UpdateStatusBar();
  4400. }
  4401. FIgnoreNextDialogChar = 0;
  4402. }
  4403. //---------------------------------------------------------------------------
  4404. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4405. {
  4406. try
  4407. {
  4408. FUserActionTimer->Enabled = false;
  4409. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4410. {
  4411. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4412. {
  4413. FPendingQueueActionItem->ProcessUserAction();
  4414. }
  4415. }
  4416. }
  4417. __finally
  4418. {
  4419. FPendingQueueActionItem = NULL;
  4420. }
  4421. }
  4422. //---------------------------------------------------------------------------
  4423. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4424. {
  4425. if (WinConfiguration->MinimizeToTray)
  4426. {
  4427. UpdateTrayIcon();
  4428. FTrayIcon->Visible = true;
  4429. if (Visible)
  4430. {
  4431. ShowWindow(Handle, SW_HIDE);
  4432. }
  4433. }
  4434. }
  4435. //---------------------------------------------------------------------------
  4436. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4437. {
  4438. // WORKAROUND
  4439. // When restoring maximized window from minimization,
  4440. // rarely some controls do not align properly.
  4441. // Two instances seen (both for Commander):
  4442. // - When restoring, window is temporarily narrower (not maximized),
  4443. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4444. // Once maximized already, top dock shinks back, but the session PageControl,
  4445. // do not align up, leaving space between TopDock and PageControl.
  4446. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4447. for (int Index = 0; Index < ControlCount; Index++)
  4448. {
  4449. RealignControl(Controls[Index]);
  4450. }
  4451. if (FTrayIcon->Visible)
  4452. {
  4453. FTrayIcon->Visible = false;
  4454. if (Visible)
  4455. {
  4456. ShowWindow(Handle, SW_SHOW);
  4457. }
  4458. }
  4459. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4460. {
  4461. FNeedSession = false;
  4462. NonVisualDataModule->StartBusy();
  4463. try
  4464. {
  4465. NeedSession(true);
  4466. }
  4467. __finally
  4468. {
  4469. NonVisualDataModule->EndBusy();
  4470. }
  4471. }
  4472. }
  4473. //---------------------------------------------------------------------------
  4474. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4475. {
  4476. FTrayIcon->Hint = GetMainForm()->Caption;
  4477. }
  4478. //---------------------------------------------------------------------------
  4479. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4480. {
  4481. UpdateTrayIcon();
  4482. }
  4483. //---------------------------------------------------------------------------
  4484. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4485. {
  4486. // workaround
  4487. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4488. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4489. // But WM_ACTIVATEAPP message can be received even when
  4490. // the main window is minimized to the tray and internal editor window is focused
  4491. // (after another application was previously active)
  4492. if (::IsIconic(Handle))
  4493. {
  4494. if (!IsAppIconic())
  4495. {
  4496. SetAppIconic(true);
  4497. }
  4498. }
  4499. ::ApplicationRestore();
  4500. Application->BringToFront();
  4501. }
  4502. //---------------------------------------------------------------------------
  4503. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4504. {
  4505. RestoreApp();
  4506. }
  4507. //---------------------------------------------------------------------------
  4508. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4509. {
  4510. if (OpenInNewWindow())
  4511. {
  4512. ExecuteNewInstance(SessionUrl);
  4513. }
  4514. else
  4515. {
  4516. TTerminalManager::Instance()->NewSession(SessionUrl);
  4517. }
  4518. }
  4519. //---------------------------------------------------------------------------
  4520. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4521. {
  4522. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4523. StoredSessions->NewSession(SessionName, SessionData);
  4524. // modified only, explicit
  4525. StoredSessions->Save(false, true);
  4526. // encode session name because of slashes in hierarchical sessions
  4527. return EncodeUrlString(SessionName);
  4528. }
  4529. //---------------------------------------------------------------------------
  4530. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4531. {
  4532. // current working directories become defaults here, what is not right
  4533. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4534. return SaveHiddenDuplicateSession(SessionData.get());
  4535. }
  4536. //---------------------------------------------------------------------------
  4537. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4538. {
  4539. if (OpenInNewWindow())
  4540. {
  4541. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4542. }
  4543. else
  4544. {
  4545. // current working directories become defaults here, what is not right
  4546. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4547. TTerminalManager * Manager = TTerminalManager::Instance();
  4548. TManagedTerminal * ATerminal = Manager->NewManagedTerminal(SessionData.get());
  4549. // We definitelly want this
  4550. ATerminal->Disconnected = Terminal->Disconnected;
  4551. // Not sure about these two
  4552. ATerminal->Permanent = Terminal->Permanent;
  4553. ATerminal->DisconnectedTemporarily = Terminal->DisconnectedTemporarily; // maybe
  4554. Manager->ActiveSession = ATerminal;
  4555. }
  4556. }
  4557. //---------------------------------------------------------------------------
  4558. void __fastcall TCustomScpExplorerForm::RenameSession()
  4559. {
  4560. UnicodeString Name = Terminal->SessionData->SessionName;
  4561. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4562. {
  4563. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4564. TSessionData::ValidateName(Name);
  4565. // This is inconsistent with how color (for example) is handled.
  4566. Terminal->SessionData->Name = Name;
  4567. UpdateControls();
  4568. // Add/Remove distinguishing paths from sessions of the same name.
  4569. SessionListChanged();
  4570. }
  4571. }
  4572. //---------------------------------------------------------------------------
  4573. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4574. {
  4575. DebugAssert(Queue != NULL);
  4576. bool Result = Queue->IsEmpty;
  4577. if (!Result)
  4578. {
  4579. SetFocus();
  4580. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4581. }
  4582. return Result;
  4583. }
  4584. //---------------------------------------------------------------------------
  4585. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4586. {
  4587. return CanCloseQueue(FQueue);
  4588. }
  4589. //---------------------------------------------------------------------------
  4590. void __fastcall TCustomScpExplorerForm::CloseSession()
  4591. {
  4592. if (CanCloseQueue())
  4593. {
  4594. TTerminalManager::Instance()->FreeActiveTerminal();
  4595. }
  4596. }
  4597. //---------------------------------------------------------------------------
  4598. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4599. {
  4600. if (CanCloseQueue())
  4601. {
  4602. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4603. }
  4604. }
  4605. //---------------------------------------------------------------------------
  4606. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4607. {
  4608. DetachTerminal(Terminal);
  4609. RemoteDirView->Terminal = Terminal;
  4610. UpdateRemotePathComboBox(false);
  4611. InitStatusBar();
  4612. UpdateControls();
  4613. }
  4614. //---------------------------------------------------------------------------
  4615. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4616. {
  4617. InitStatusBar();
  4618. }
  4619. //---------------------------------------------------------------------------
  4620. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4621. {
  4622. if (DebugAlwaysTrue(!Terminal->Active))
  4623. {
  4624. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4625. }
  4626. }
  4627. //---------------------------------------------------------------------------
  4628. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4629. {
  4630. if (OpenInNewWindow())
  4631. {
  4632. // encode session name because of slashes in hierarchical sessions
  4633. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4634. }
  4635. else
  4636. {
  4637. TTerminalManager * Manager = TTerminalManager::Instance();
  4638. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4639. Manager->ActiveSession = Terminal;
  4640. }
  4641. }
  4642. //---------------------------------------------------------------------------
  4643. void __fastcall TCustomScpExplorerForm::DoOpenFolderOrWorkspace(const UnicodeString & Name, bool ConnectFirstTerminal)
  4644. {
  4645. TTerminalManager * Manager = TTerminalManager::Instance();
  4646. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4647. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4648. TManagedTerminal * FirstTerminal = Manager->NewTerminals(DataList.get());
  4649. // FirstTerminal can be null, if some of the
  4650. if (!ConnectFirstTerminal && (FirstTerminal != NULL))
  4651. {
  4652. FirstTerminal->Disconnected = true;
  4653. FirstTerminal->DisconnectedTemporarily = true;
  4654. }
  4655. Manager->ActiveSession = FirstTerminal;
  4656. }
  4657. //---------------------------------------------------------------------------
  4658. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4659. {
  4660. if (OpenInNewWindow())
  4661. {
  4662. ExecuteNewInstance(Name);
  4663. }
  4664. else
  4665. {
  4666. DoOpenFolderOrWorkspace(Name, true);
  4667. }
  4668. }
  4669. //---------------------------------------------------------------------------
  4670. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4671. bool &CanClose)
  4672. {
  4673. // See the comment in CloseApp()
  4674. if (NonVisualDataModule->Busy)
  4675. {
  4676. CanClose = false;
  4677. }
  4678. else
  4679. {
  4680. TTerminalManager * Manager = TTerminalManager::Instance();
  4681. int ActiveSessions = 0;
  4682. for (int Index = 0; Index < Manager->Count; Index++)
  4683. {
  4684. if (Manager->Sessions[Index]->Active)
  4685. {
  4686. ActiveSessions++;
  4687. }
  4688. }
  4689. // Confirm, when there's at least one active remote session or
  4690. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4691. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4692. WinConfiguration->ConfirmClosingSession)
  4693. {
  4694. unsigned int Result;
  4695. TMessageParams Params(mpNeverAskAgainCheck);
  4696. UnicodeString Message;
  4697. int Answers = qaOK | qaCancel;
  4698. // The current session is the only active session
  4699. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4700. {
  4701. if (!WinConfiguration->AutoSaveWorkspace)
  4702. {
  4703. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4704. }
  4705. else
  4706. {
  4707. Message = LoadStr(CLOSE_SESSION);
  4708. }
  4709. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4710. }
  4711. // Multiple active sessions or one active session, but it's not the current one
  4712. else if (ActiveSessions > 0)
  4713. {
  4714. if (!WinConfiguration->AutoSaveWorkspace)
  4715. {
  4716. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4717. }
  4718. else
  4719. {
  4720. Message = LoadStr(CLOSE_SESSIONS);
  4721. }
  4722. }
  4723. // Multiple tabs, but all are intactive or local, and saving of worspace is not enabled
  4724. else
  4725. {
  4726. DebugAlwaysTrue(Manager->Count > 1);
  4727. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  4728. Message = LoadStr(CLOSE_WORKSPACE);
  4729. }
  4730. UnicodeString Note;
  4731. if (WinConfiguration->AutoSaveWorkspace)
  4732. {
  4733. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4734. }
  4735. else
  4736. {
  4737. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4738. Answers = qaYes | qaNo | qaCancel;
  4739. }
  4740. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4741. SetFocus();
  4742. Result = MessageDialog(Message, qtConfirmation,
  4743. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4744. if (Result == qaNeverAskAgain)
  4745. {
  4746. WinConfiguration->ConfirmClosingSession = false;
  4747. }
  4748. if (Result == qaNo)
  4749. {
  4750. CanClose = SaveWorkspace(true);
  4751. // note that the workspace will be saved redundatly again from FormClose
  4752. }
  4753. else
  4754. {
  4755. CanClose =
  4756. (Result == qaOK) ||
  4757. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4758. (Result == qaNeverAskAgain);
  4759. }
  4760. }
  4761. if (CanClose)
  4762. {
  4763. CanClose = CanCloseQueue();
  4764. }
  4765. }
  4766. if (CanClose)
  4767. {
  4768. CanClose =
  4769. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4770. FEditorManager->CloseExternalFilesWithoutProcess();
  4771. if (CanClose)
  4772. {
  4773. while (CanClose && (FLocalEditors->Count > 0))
  4774. {
  4775. int PrevCount = FLocalEditors->Count;
  4776. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4777. CanClose = (FLocalEditors->Count < PrevCount);
  4778. }
  4779. if (CanClose)
  4780. {
  4781. CanClose = FEditorManager->Empty(true);
  4782. if (!CanClose)
  4783. {
  4784. SetFocus();
  4785. CanClose =
  4786. (MessageDialog(
  4787. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4788. }
  4789. }
  4790. }
  4791. }
  4792. }
  4793. //---------------------------------------------------------------------------
  4794. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4795. {
  4796. TForm * Form = dynamic_cast<TForm *>(Sender);
  4797. DebugAssert(Form != NULL);
  4798. Form->Close();
  4799. }
  4800. //---------------------------------------------------------------------------
  4801. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4802. {
  4803. TForm * Form = dynamic_cast<TForm *>(Sender);
  4804. delete Form;
  4805. }
  4806. //---------------------------------------------------------------------------
  4807. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4808. {
  4809. while (FLocalEditors->Count > 0)
  4810. {
  4811. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4812. }
  4813. }
  4814. //---------------------------------------------------------------------------
  4815. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4816. TObject *Sender)
  4817. {
  4818. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4819. try
  4820. {
  4821. SetProperties(osRemote, FileList);
  4822. }
  4823. __finally
  4824. {
  4825. delete FileList;
  4826. }
  4827. }
  4828. //---------------------------------------------------------------------------
  4829. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4830. {
  4831. if (value)
  4832. {
  4833. if (Component == fcCustomCommandsBand)
  4834. {
  4835. UpdateCustomCommandsToolbar();
  4836. }
  4837. if (Component == fcQueueFileList)
  4838. {
  4839. DebugAssert(!QueueFileListSplitter->Visible);
  4840. AdjustQueueLayout();
  4841. UpdateQueueFileList();
  4842. }
  4843. }
  4844. }
  4845. //---------------------------------------------------------------------------
  4846. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  4847. {
  4848. int MinHeightLimit =
  4849. GetStaticQueuePanelComponentsHeight() +
  4850. QueueFileListSplitter->Height +
  4851. QueueFileList->Height +
  4852. GetMinQueueViewHeight();
  4853. if (QueuePanel->ClientHeight < MinHeightLimit)
  4854. {
  4855. QueuePanel->ClientHeight = MinHeightLimit;
  4856. }
  4857. }
  4858. //---------------------------------------------------------------------------
  4859. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4860. {
  4861. TControl * Control = GetComponent(Component);
  4862. DebugAssert(Control);
  4863. bool Changed = (Control->Visible != value);
  4864. if (Changed)
  4865. {
  4866. ComponentShowing(Component, value);
  4867. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4868. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4869. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4870. if (value)
  4871. {
  4872. int RemainingHeight = Control->Parent->ClientHeight;
  4873. int RemainingWidth = Control->Parent->ClientWidth;
  4874. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4875. {
  4876. TControl * ChildControl = Control->Parent->Controls[i];
  4877. if (ChildControl->Visible)
  4878. {
  4879. switch (ChildControl->Align)
  4880. {
  4881. case alTop:
  4882. case alBottom:
  4883. RemainingHeight -= ChildControl->Height;
  4884. break;
  4885. case alLeft:
  4886. case alRight:
  4887. RemainingWidth -= ChildControl->Width;
  4888. break;
  4889. }
  4890. }
  4891. }
  4892. int Reserve = ScaleByTextHeight(this, 32);
  4893. // queue in explorer, trees in commander
  4894. if (Control->Height > RemainingHeight - Reserve)
  4895. {
  4896. Control->Height = RemainingHeight / 2;
  4897. }
  4898. if (Control->Width > RemainingWidth - Reserve)
  4899. {
  4900. Control->Width = RemainingWidth / 2;
  4901. }
  4902. }
  4903. Control->Visible = value;
  4904. if (WasFocused && Visible)
  4905. {
  4906. DirView(osCurrent)->SetFocus();
  4907. }
  4908. FixControlsPlacement();
  4909. }
  4910. }
  4911. //---------------------------------------------------------------------------
  4912. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4913. {
  4914. TControl * Control = GetComponent(Component);
  4915. if (Control == NULL)
  4916. {
  4917. return false;
  4918. }
  4919. else
  4920. {
  4921. return Control->Visible;
  4922. }
  4923. }
  4924. //---------------------------------------------------------------------------
  4925. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4926. {
  4927. bool Result;
  4928. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4929. {
  4930. Result = !IsUWP();
  4931. }
  4932. else
  4933. {
  4934. Result = true;
  4935. }
  4936. return Result;
  4937. }
  4938. //---------------------------------------------------------------------------
  4939. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  4940. {
  4941. if (DirView->ItemFocused != NULL)
  4942. {
  4943. DirView->ItemFocused->MakeVisible(false);
  4944. }
  4945. }
  4946. //---------------------------------------------------------------------------
  4947. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4948. {
  4949. MakeFocusedItemVisible(RemoteDirView);
  4950. QueueSplitter->Visible = QueuePanel->Visible;
  4951. QueueFileListSplitter->Visible = QueueFileList->Visible;
  4952. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4953. TControl * QueueControlsOrder[] =
  4954. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  4955. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  4956. }
  4957. //---------------------------------------------------------------------------
  4958. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4959. {
  4960. switch (Component) {
  4961. case fcStatusBar: return RemoteStatusBar;
  4962. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4963. case fcQueueView: return QueuePanel;
  4964. case fcQueueToolbar: return QueueDock;
  4965. case fcRemoteTree: return RemoteDrivePanel;
  4966. case fcSessionsTabs: return SessionsPageControl;
  4967. case fcQueueFileList: return QueueFileList;
  4968. default: return NULL;
  4969. }
  4970. }
  4971. //---------------------------------------------------------------------------
  4972. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4973. TObject *Sender, TListColumn *Column, TPoint &Point)
  4974. {
  4975. DebugAssert(NonVisualDataModule && Column && Sender);
  4976. NonVisualDataModule->ListColumn = Column;
  4977. TPopupMenu * DirViewColumnMenu;
  4978. if (Sender == DirView(osOther))
  4979. {
  4980. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4981. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4982. (Column->Index == uvName);
  4983. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4984. (Column->Index == uvSize);
  4985. }
  4986. else
  4987. {
  4988. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4989. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4990. (Column->Index == dvName);
  4991. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4992. (Column->Index == dvSize);
  4993. }
  4994. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4995. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4996. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4997. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4998. }
  4999. //---------------------------------------------------------------------------
  5000. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5001. TObject *Sender, TListItem *Item, bool &AllowExec)
  5002. {
  5003. DoDirViewExecFile(Sender, Item, AllowExec);
  5004. }
  5005. //---------------------------------------------------------------------------
  5006. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5007. TListItem * Item, bool & AllowExec)
  5008. {
  5009. DebugAssert(Sender && Item && Configuration);
  5010. DebugAssert(AllowExec);
  5011. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5012. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5013. bool Remote = !IsSideLocalBrowser(Side);
  5014. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles();
  5015. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  5016. // on files only (not directories)
  5017. // and only when symlinks are resolved (apply to remote panel only)
  5018. if (!ADirView->ItemIsDirectory(Item) &&
  5019. (ResolvedSymlinks || FForceExecution))
  5020. {
  5021. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  5022. !FForceExecution && ResolvedSymlinks)
  5023. {
  5024. if (WinConfiguration->DoubleClickAction == dcaCopy)
  5025. {
  5026. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5027. UnlockWindow();
  5028. try
  5029. {
  5030. if (IsLocalBrowserMode())
  5031. {
  5032. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5033. }
  5034. else
  5035. {
  5036. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5037. }
  5038. }
  5039. __finally
  5040. {
  5041. LockWindow();
  5042. }
  5043. AllowExec = false;
  5044. }
  5045. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  5046. {
  5047. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5048. {
  5049. ExecuteFile(Side, efDefaultEditor);
  5050. AllowExec = false;
  5051. }
  5052. }
  5053. else
  5054. {
  5055. DebugFail();
  5056. }
  5057. }
  5058. // if we have not done anything special, fall back to default behavior
  5059. if (AllowExec)
  5060. {
  5061. if (Remote && !WinConfiguration->DisableOpenEdit)
  5062. {
  5063. ExecuteFile(osRemote, efShell);
  5064. AllowExec = false;
  5065. }
  5066. }
  5067. }
  5068. }
  5069. //---------------------------------------------------------------------------
  5070. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5071. {
  5072. return ((Side == osRemote) || (Side == osCurrent));
  5073. }
  5074. //---------------------------------------------------------------------------
  5075. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5076. {
  5077. DebugFail();
  5078. }
  5079. //---------------------------------------------------------------------------
  5080. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5081. {
  5082. DebugFail();
  5083. }
  5084. //---------------------------------------------------------------------------
  5085. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5086. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5087. {
  5088. TSynchronizeParamType Params;
  5089. Params.LocalDirectory = LocalDirectory;
  5090. Params.RemoteDirectory = RemoteDirectory;
  5091. int UnusedParams =
  5092. (GUIConfiguration->SynchronizeParams &
  5093. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5094. TTerminal::spCaseSensitive));
  5095. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5096. Params.Options = GUIConfiguration->SynchronizeOptions;
  5097. bool SaveSettings = false;
  5098. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5099. &DoSynchronizeTooManyDirectories);
  5100. DebugAssert(FSynchronizeController == NULL);
  5101. FSynchronizeController = &Controller;
  5102. bool Result;
  5103. try
  5104. {
  5105. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5106. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5107. int Options =
  5108. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5109. DebugAssert(FOnFeedSynchronizeError == NULL);
  5110. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5111. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5112. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  5113. if (Result)
  5114. {
  5115. if (SaveSettings)
  5116. {
  5117. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5118. GUIConfiguration->SynchronizeOptions = Params.Options;
  5119. }
  5120. else
  5121. {
  5122. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5123. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5124. FLAGSET(Params.Options, soSynchronize))
  5125. {
  5126. GUIConfiguration->SynchronizeOptions =
  5127. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5128. soSynchronize;
  5129. }
  5130. }
  5131. LocalDirectory = Params.LocalDirectory;
  5132. RemoteDirectory = Params.RemoteDirectory;
  5133. }
  5134. }
  5135. __finally
  5136. {
  5137. FSynchronizeController = NULL;
  5138. DebugAssert(FOnFeedSynchronizeError == NULL);
  5139. FOnFeedSynchronizeError = NULL;
  5140. }
  5141. return Result;
  5142. }
  5143. //---------------------------------------------------------------------------
  5144. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5145. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5146. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5147. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5148. TSynchronizeOptions * Options, bool Full)
  5149. {
  5150. // Keep-up-to-date mode
  5151. if (Terminal->Status == ssOpened)
  5152. {
  5153. try
  5154. {
  5155. int PParams = Params.Params;
  5156. if (!Full)
  5157. {
  5158. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5159. if (FLAGSET(Params.Options, soRecurse))
  5160. {
  5161. PParams |= TTerminal::spSubDirs;
  5162. }
  5163. }
  5164. else
  5165. {
  5166. // if keepuptodate is non-recursive,
  5167. // full sync before has to be non-recursive as well
  5168. if (FLAGCLEAR(Params.Options, soRecurse))
  5169. {
  5170. PParams |= TTerminal::spNoRecurse;
  5171. }
  5172. }
  5173. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5174. PParams, Checklist, Options);
  5175. }
  5176. catch (Exception & E)
  5177. {
  5178. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  5179. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5180. {
  5181. // noop, already logged in MoreMessageDialog
  5182. }
  5183. else
  5184. {
  5185. ShowExtendedExceptionEx(Terminal, &E);
  5186. throw;
  5187. }
  5188. }
  5189. }
  5190. }
  5191. //---------------------------------------------------------------------------
  5192. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5193. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5194. const UnicodeString ErrorStr)
  5195. {
  5196. if (!Directory.IsEmpty())
  5197. {
  5198. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5199. }
  5200. else
  5201. {
  5202. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5203. }
  5204. }
  5205. //---------------------------------------------------------------------------
  5206. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5207. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5208. {
  5209. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5210. {
  5211. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5212. }
  5213. else
  5214. {
  5215. TMessageParams Params(mpNeverAskAgainCheck);
  5216. unsigned int Result = MessageDialog(
  5217. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5218. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5219. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5220. {
  5221. MaxDirectories *= 2;
  5222. if (Result == qaNeverAskAgain)
  5223. {
  5224. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5225. }
  5226. }
  5227. else
  5228. {
  5229. Abort();
  5230. }
  5231. }
  5232. }
  5233. //---------------------------------------------------------------------------
  5234. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5235. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5236. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5237. TSynchronizeOptions * Options)
  5238. {
  5239. // Keep-up-to-date mode
  5240. DebugAssert(!FAutoOperation);
  5241. void * BatchStorage;
  5242. BatchStart(BatchStorage);
  5243. FAutoOperation = true;
  5244. bool AnyOperation = false;
  5245. TDateTime StartTime = Now();
  5246. TSynchronizeChecklist * AChecklist = NULL;
  5247. try
  5248. {
  5249. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5250. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5251. {
  5252. FSynchronizeProgressForm->Start();
  5253. }
  5254. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5255. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5256. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5257. Options);
  5258. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5259. SAFE_DESTROY(FSynchronizeProgressForm);
  5260. AnyOperation = (AChecklist->CheckedCount > 0);
  5261. if (AnyOperation)
  5262. {
  5263. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5264. CreateProgressForm(&SynchronizeProgress);
  5265. try
  5266. {
  5267. Terminal->SynchronizeApply(
  5268. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5269. SynchronizeProcessedItem, NULL, NULL, NULL);
  5270. }
  5271. __finally
  5272. {
  5273. DestroyProgressForm();
  5274. }
  5275. }
  5276. }
  5277. __finally
  5278. {
  5279. if (Checklist == NULL)
  5280. {
  5281. delete AChecklist;
  5282. }
  5283. else
  5284. {
  5285. *Checklist = AChecklist;
  5286. }
  5287. FAutoOperation = false;
  5288. SAFE_DESTROY(FSynchronizeProgressForm);
  5289. BatchEnd(BatchStorage);
  5290. ReloadLocalDirectory();
  5291. if (AnyOperation)
  5292. {
  5293. OperationComplete(StartTime);
  5294. }
  5295. }
  5296. }
  5297. //---------------------------------------------------------------------------
  5298. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5299. {
  5300. // can be called from command line
  5301. return Visible &&
  5302. ((DirView(osRemote)->SelCount > 0) ||
  5303. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5304. }
  5305. //---------------------------------------------------------------------------
  5306. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5307. {
  5308. LogSynchronizeEvent(Terminal, Message);
  5309. }
  5310. //---------------------------------------------------------------------------
  5311. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5312. int Params, TSynchronizeOptions & Options)
  5313. {
  5314. DebugAssert(!IsLocalBrowserMode());
  5315. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5316. {
  5317. Options.Filter = new TStringList();
  5318. Options.Filter->CaseSensitive = false;
  5319. Options.Filter->Duplicates = Types::dupAccept;
  5320. if (DirView(osRemote)->SelCount > 0)
  5321. {
  5322. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5323. }
  5324. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5325. {
  5326. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5327. }
  5328. Options.Filter->Sort();
  5329. }
  5330. }
  5331. //---------------------------------------------------------------------------
  5332. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5333. {
  5334. TCopyParamType Defaults;
  5335. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5336. ConfigStorage->AccessMode = smRead;
  5337. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5338. {
  5339. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5340. }
  5341. ConfigStorage.reset(NULL);
  5342. std::unique_ptr<TStringList> Options(new TStringList());
  5343. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5344. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5345. UnicodeString Result;
  5346. if (Options->Count > 0)
  5347. {
  5348. Result =
  5349. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5350. }
  5351. return Result;
  5352. }
  5353. //---------------------------------------------------------------------------
  5354. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5355. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5356. {
  5357. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5358. UnicodeString AdditionalParams =
  5359. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5360. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5361. SerializeCopyParamForCommandLine(CopyParams),
  5362. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5363. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5364. ExecuteNewInstance(SessionName, AdditionalParams);
  5365. }
  5366. //---------------------------------------------------------------------------
  5367. struct TSynchronizeParams
  5368. {
  5369. UnicodeString LocalDirectory;
  5370. UnicodeString RemoteDirectory;
  5371. TSynchronizeMode Mode;
  5372. TSynchronizeChecklist * Checklist;
  5373. int Params;
  5374. TCopyParamType * CopyParam;
  5375. TDateTime CollectElapsed;
  5376. TDateTime * StartTime;
  5377. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5378. };
  5379. //---------------------------------------------------------------------------
  5380. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5381. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5382. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5383. {
  5384. DebugAssert(!FAutoOperation);
  5385. void * BatchStorage;
  5386. BatchStart(BatchStorage);
  5387. FAutoOperation = true;
  5388. TFileOperationStatistics Statistics;
  5389. TDateTime Start = Now();
  5390. try
  5391. {
  5392. Params.OnProcessedItem = OnProcessedItem;
  5393. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5394. CreateProgressForm(&SynchronizeProgress);
  5395. Terminal->SynchronizeApply(
  5396. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5397. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5398. &Statistics);
  5399. }
  5400. __finally
  5401. {
  5402. Params.OnProcessedItem = NULL;
  5403. FAutoOperation = false;
  5404. DestroyProgressForm();
  5405. BatchEnd(BatchStorage);
  5406. ReloadLocalDirectory();
  5407. }
  5408. if (WinConfiguration->SynchronizeSummary)
  5409. {
  5410. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5411. // The statistics should be 0 anyway in this case
  5412. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5413. {
  5414. Message += L"\n";
  5415. if (Statistics.FilesUploaded > 0)
  5416. {
  5417. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5418. }
  5419. if (Statistics.FilesDownloaded > 0)
  5420. {
  5421. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5422. }
  5423. if (Statistics.FilesDeletedLocal > 0)
  5424. {
  5425. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5426. }
  5427. if (Statistics.FilesDeletedRemote > 0)
  5428. {
  5429. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5430. }
  5431. }
  5432. TDateTime Elapsed = (Now() - Start);
  5433. Message +=
  5434. L"\n" +
  5435. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5436. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5437. TMessageParams Params(mpNeverAskAgainCheck);
  5438. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5439. if (Result == qaNeverAskAgain)
  5440. {
  5441. WinConfiguration->SynchronizeSummary = false;
  5442. }
  5443. }
  5444. }
  5445. //---------------------------------------------------------------------------
  5446. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5447. {
  5448. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5449. {
  5450. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5451. }
  5452. // Not set in keep-up-to-date mode - Synchronize() method
  5453. if (Token != NULL)
  5454. {
  5455. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5456. if (Params.OnProcessedItem != NULL)
  5457. {
  5458. Params.OnProcessedItem(NULL, ChecklistItem);
  5459. }
  5460. }
  5461. }
  5462. //---------------------------------------------------------------------------
  5463. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5464. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5465. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5466. {
  5467. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5468. *Params.StartTime = Now();
  5469. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5470. }
  5471. //---------------------------------------------------------------------------
  5472. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5473. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5474. {
  5475. // terminal can be already closed (e.g. dropped connection)
  5476. if (Terminal != NULL)
  5477. {
  5478. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5479. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5480. }
  5481. }
  5482. //---------------------------------------------------------------------------
  5483. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5484. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5485. {
  5486. DebugAssert(!IsLocalBrowserMode());
  5487. TAutoBatch AutoBatch(this);
  5488. TAutoFlag AutoOperationFlag(FAutoOperation);
  5489. if (Side == osRemote)
  5490. {
  5491. std::unique_ptr<TStrings> FileList(new TStringList());
  5492. FileList->AddObject(FileName, RemoteFile);
  5493. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5494. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5495. RemoteDirView->SaveSelection();
  5496. RemoteDirView->SaveSelectedNames();
  5497. try
  5498. {
  5499. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5500. }
  5501. catch(...)
  5502. {
  5503. RemoteDirView->DiscardSavedSelection();
  5504. throw;
  5505. }
  5506. RemoteDirView->RestoreSelection();
  5507. }
  5508. else if (DebugAlwaysTrue(Side == osLocal))
  5509. {
  5510. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5511. {
  5512. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5513. }
  5514. UnicodeString Directory = ExtractFileDir(FileName);
  5515. ReloadLocalDirectory(Directory);
  5516. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5517. if (!SamePaths(Directory, NewDirectory))
  5518. {
  5519. ReloadLocalDirectory(NewDirectory);
  5520. }
  5521. }
  5522. }
  5523. //---------------------------------------------------------------------------
  5524. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5525. {
  5526. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5527. if (Side == osLocal)
  5528. {
  5529. if (Action == TSynchronizeChecklist::saDownloadNew)
  5530. {
  5531. OpenFolderInExplorer(LocalPath);
  5532. }
  5533. else
  5534. {
  5535. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5536. }
  5537. }
  5538. else if (DebugAlwaysTrue(Side == osRemote))
  5539. {
  5540. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5541. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5542. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5543. if (!LocalPath.IsEmpty())
  5544. {
  5545. SessionData->LocalDirectory = LocalPath;
  5546. }
  5547. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5548. ExecuteNewInstance(SessionName, FORMAT(L"%s=%s", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5549. }
  5550. }
  5551. //---------------------------------------------------------------------------
  5552. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5553. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5554. const TCopyParamType * CopyParams)
  5555. {
  5556. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5557. UnicodeString AdditionalParams =
  5558. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5559. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5560. SerializeCopyParamForCommandLine(CopyParams),
  5561. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5562. LocalDirectory, RemoteDirectory, Mode, Params));
  5563. ExecuteNewInstance(SessionName, AdditionalParams);
  5564. }
  5565. //---------------------------------------------------------------------------
  5566. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5567. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5568. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5569. {
  5570. int Result;
  5571. bool SaveSettings = false;
  5572. int Options =
  5573. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5574. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5575. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5576. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5577. bool Continue =
  5578. (UseDefaults == 0) ||
  5579. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5580. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5581. if (Continue)
  5582. {
  5583. Configuration->Usage->Inc(L"Synchronizations");
  5584. UpdateCopyParamCounters(CopyParam);
  5585. TSynchronizeOptions SynchronizeOptions;
  5586. GetSynchronizeOptions(Params, SynchronizeOptions);
  5587. if (SaveSettings)
  5588. {
  5589. GUIConfiguration->SynchronizeParams = Params;
  5590. }
  5591. else
  5592. {
  5593. SaveMode = false;
  5594. }
  5595. TDateTime StartTime = Now();
  5596. TSynchronizeChecklist * Checklist = NULL;
  5597. try
  5598. {
  5599. DebugAssert(!FAutoOperation);
  5600. FAutoOperation = true;
  5601. try
  5602. {
  5603. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5604. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5605. int Files = -1;
  5606. if (DataList->Count >= 1)
  5607. {
  5608. Files = StrToIntDef(DataList->Strings[0], Files);
  5609. }
  5610. else
  5611. {
  5612. DataList->Add(UnicodeString());
  5613. }
  5614. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5615. FSynchronizeProgressForm->Start();
  5616. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5617. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5618. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5619. &SynchronizeOptions);
  5620. if (Terminal->SessionData->CacheDirectories)
  5621. {
  5622. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5623. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5624. }
  5625. }
  5626. __finally
  5627. {
  5628. FAutoOperation = false;
  5629. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5630. SAFE_DESTROY(FSynchronizeProgressForm);
  5631. }
  5632. TSynchronizeParams SynchronizeParams;
  5633. SynchronizeParams.LocalDirectory = LocalDirectory;
  5634. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5635. SynchronizeParams.Mode = Mode;
  5636. SynchronizeParams.CopyParam = &CopyParam;
  5637. SynchronizeParams.Params = Params;
  5638. SynchronizeParams.Checklist = Checklist;
  5639. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5640. SynchronizeParams.StartTime = &StartTime;
  5641. SynchronizeParams.OnProcessedItem = NULL;
  5642. Result = Checklist->Count;
  5643. if (Checklist->Count > 0)
  5644. {
  5645. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5646. {
  5647. if (!DoSynchronizeChecklistDialog(
  5648. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5649. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5650. {
  5651. Result = -1;
  5652. }
  5653. }
  5654. else
  5655. {
  5656. FullSynchronize(SynchronizeParams, NULL, NULL);
  5657. }
  5658. }
  5659. else
  5660. {
  5661. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5662. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5663. }
  5664. }
  5665. __finally
  5666. {
  5667. delete Checklist;
  5668. }
  5669. OperationComplete(StartTime);
  5670. }
  5671. else
  5672. {
  5673. Result = -1;
  5674. }
  5675. return Result;
  5676. }
  5677. //---------------------------------------------------------------------------
  5678. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5679. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5680. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5681. {
  5682. if (Collect)
  5683. {
  5684. DebugAssert(FSynchronizeProgressForm != NULL);
  5685. if (!FSynchronizeProgressForm->Started)
  5686. {
  5687. FSynchronizeProgressForm->Start();
  5688. }
  5689. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5690. SetTaskbarListProgressValue(CompareProgress);
  5691. }
  5692. else
  5693. {
  5694. DebugAssert(FSynchronizeProgressForm == NULL);
  5695. DebugAssert(FProgressForm != NULL);
  5696. }
  5697. }
  5698. //---------------------------------------------------------------------------
  5699. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5700. {
  5701. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  5702. TRemoteFile * File = NULL;
  5703. Terminal->ReadFile(FullFileName, File);
  5704. if (File != NULL)
  5705. {
  5706. std::unique_ptr<TRemoteFile> FileOwner(File);
  5707. TAutoFlag Flag(FStandaloneEditing);
  5708. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5709. Application->ShowMainForm = false;
  5710. Application->Run();
  5711. }
  5712. }
  5713. //---------------------------------------------------------------------------
  5714. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  5715. {
  5716. DebugFail();
  5717. }
  5718. //---------------------------------------------------------------------------
  5719. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5720. {
  5721. DebugAssert(!IsLocalBrowserMode()); // untested
  5722. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5723. SessionData->Assign(Terminal->SessionData);
  5724. UpdateSessionData(SessionData.get());
  5725. Terminal->UpdateSessionCredentials(SessionData.get());
  5726. if (!Terminal->SessionData->HasSessionName())
  5727. {
  5728. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5729. // once IsWorkspace is cleared
  5730. SessionData->Name = UnicodeString();
  5731. SessionData->IsWorkspace = false;
  5732. }
  5733. return SessionData.release();
  5734. }
  5735. //---------------------------------------------------------------------------
  5736. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5737. {
  5738. TSessionData * SessionData = CloneCurrentSessionData();
  5739. try
  5740. {
  5741. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5742. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5743. }
  5744. __finally
  5745. {
  5746. delete SessionData;
  5747. }
  5748. }
  5749. //---------------------------------------------------------------------------
  5750. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5751. {
  5752. TTerminalManager * Manager = TTerminalManager::Instance();
  5753. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5754. if (DebugAlwaysTrue(ManagedSession != NULL))
  5755. {
  5756. // Update ManagedSession->StateData
  5757. UpdateSession(ManagedSession);
  5758. }
  5759. Manager->SaveWorkspace(DataList.get());
  5760. return DataList.release();
  5761. }
  5762. //---------------------------------------------------------------------------
  5763. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5764. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5765. {
  5766. WinConfiguration->LastWorkspace = Name;
  5767. if (!SavePasswords)
  5768. {
  5769. for (int Index = 0; Index < DataList->Count; Index++)
  5770. {
  5771. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5772. if (SessionData->Link.IsEmpty())
  5773. {
  5774. SessionData->ClearSessionPasswords();
  5775. }
  5776. }
  5777. }
  5778. StoredSessions->NewWorkspace(Name, DataList);
  5779. // modified only
  5780. StoredSessions->Save(false, Explicit);
  5781. }
  5782. //---------------------------------------------------------------------------
  5783. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5784. {
  5785. return
  5786. DefaultStr(WinConfiguration->LastWorkspace,
  5787. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5788. }
  5789. //---------------------------------------------------------------------------
  5790. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5791. {
  5792. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5793. bool AnyNonStoredSessionWithPassword = false;
  5794. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5795. bool AllNonStoredSessionsAnonymous = true;
  5796. TTerminalManager * Manager = TTerminalManager::Instance();
  5797. for (int Index = 0; Index < DataList->Count; Index++)
  5798. {
  5799. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5800. if (SessionData->Link.IsEmpty())
  5801. {
  5802. if (SessionData->HasAnySessionPassword())
  5803. {
  5804. AnyNonStoredSessionWithPassword = true;
  5805. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  5806. {
  5807. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5808. }
  5809. }
  5810. if (!SameText(SessionData->UserName, AnonymousUserName))
  5811. {
  5812. AllNonStoredSessionsAnonymous = false;
  5813. }
  5814. }
  5815. }
  5816. bool SavePasswords;
  5817. bool * PSavePasswords;
  5818. bool NotRecommendedSavingPasswords =
  5819. !CustomWinConfiguration->UseMasterPassword &&
  5820. !AllNonStoredSessionsAnonymous;
  5821. if (Configuration->DisablePasswordStoring ||
  5822. !AnyNonStoredSessionWithPassword)
  5823. {
  5824. PSavePasswords = NULL;
  5825. SavePasswords = false;
  5826. }
  5827. else
  5828. {
  5829. PSavePasswords = &SavePasswords;
  5830. SavePasswords =
  5831. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5832. !NotRecommendedSavingPasswords;
  5833. }
  5834. UnicodeString Name = WorkspaceName();
  5835. bool CreateShortcut = false;
  5836. bool Result =
  5837. DoSaveWorkspaceDialog(
  5838. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5839. EnableAutoSave);
  5840. if (Result)
  5841. {
  5842. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5843. if (CreateShortcut)
  5844. {
  5845. TOperationVisualizer Visualizer;
  5846. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5847. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5848. }
  5849. if (EnableAutoSave)
  5850. {
  5851. WinConfiguration->AutoSaveWorkspace = true;
  5852. WinConfiguration->AutoWorkspace = Name;
  5853. if (PSavePasswords != NULL)
  5854. {
  5855. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5856. }
  5857. }
  5858. }
  5859. return Result;
  5860. }
  5861. //---------------------------------------------------------------------------
  5862. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  5863. {
  5864. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  5865. DebugAssert(Session == ManagedSession);
  5866. if (!IsLocalBrowserMode())
  5867. {
  5868. SAFE_DESTROY(Session->RemoteExplorerState);
  5869. if (WinConfiguration->PreservePanelState)
  5870. {
  5871. Session->RemoteExplorerState = RemoteDirView->SaveState();
  5872. }
  5873. }
  5874. UpdateSessionData(Session->StateData);
  5875. }
  5876. //---------------------------------------------------------------------------
  5877. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5878. {
  5879. // Keep in sync with TSessionData::CopyStateData
  5880. DebugAssert(Data != NULL);
  5881. // This is inconsistent with how color (for example) is handled.
  5882. Data->Name = ManagedSession->SessionData->Name;
  5883. if (!IsLocalBrowserMode())
  5884. {
  5885. // cannot use RemoteDirView->Path, because it is empty if connection
  5886. // was already closed
  5887. // also only peek, we may not be connected at all atm
  5888. // so make sure we do not try retrieving current directory from the server
  5889. // (particularly with FTP)
  5890. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5891. if (!ACurrentDirectory.IsEmpty())
  5892. {
  5893. Data->RemoteDirectory = ACurrentDirectory;
  5894. }
  5895. }
  5896. Data->Color = SessionColor;
  5897. }
  5898. //---------------------------------------------------------------------------
  5899. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5900. {
  5901. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5902. DebugAssert(Toolbar != NULL);
  5903. for (int i = 0; i < Toolbar->Items->Count; i++)
  5904. {
  5905. TTBXCustomDropDownItem * DropDownItem;
  5906. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5907. if (DropDownItem != NULL)
  5908. {
  5909. ToolbarItemResize(DropDownItem,
  5910. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5911. Toolbar->NonClientWidth);
  5912. break;
  5913. }
  5914. }
  5915. }
  5916. //---------------------------------------------------------------------------
  5917. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5918. {
  5919. Item->EditWidth = Width;
  5920. }
  5921. //---------------------------------------------------------------------------
  5922. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5923. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5924. {
  5925. for (int i = 0; i < Toolbar->Items->Count; i++)
  5926. {
  5927. TTBXCustomDropDownItem * DropDownItem;
  5928. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5929. if (DropDownItem != NULL)
  5930. {
  5931. ASize.x -= DropDownItem->EditWidth;
  5932. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5933. }
  5934. }
  5935. }
  5936. //---------------------------------------------------------------------------
  5937. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5938. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5939. {
  5940. if (WinConfiguration->DDWarnLackOfTempSpace)
  5941. {
  5942. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5943. if (!ADrive.IsEmpty())
  5944. {
  5945. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5946. DebugAssert(RequiredSpace >= 0);
  5947. __int64 RequiredWithReserve;
  5948. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5949. if (FreeSpace < RequiredWithReserve)
  5950. {
  5951. unsigned int Result;
  5952. TMessageParams Params(mpNeverAskAgainCheck);
  5953. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5954. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5955. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5956. if (Result == qaNeverAskAgain)
  5957. {
  5958. WinConfiguration->DDWarnLackOfTempSpace = false;
  5959. }
  5960. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5961. }
  5962. }
  5963. }
  5964. }
  5965. //---------------------------------------------------------------------------
  5966. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5967. {
  5968. DoOpenDirectoryDialog(odAddBookmark, Side);
  5969. }
  5970. //---------------------------------------------------------------------------
  5971. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5972. {
  5973. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5974. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  5975. try
  5976. {
  5977. TCustomDirView * DView = DirView(Side);
  5978. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5979. {
  5980. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5981. }
  5982. }
  5983. catch (...)
  5984. {
  5985. delete VisitedDirectories;
  5986. throw;
  5987. }
  5988. return VisitedDirectories;
  5989. }
  5990. //---------------------------------------------------------------------------
  5991. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5992. TOpenDirectoryMode Mode, TOperationSide Side)
  5993. {
  5994. bool Continue = true;
  5995. if (Mode == odAddBookmark)
  5996. {
  5997. TMessageParams Params(mpNeverAskAgainCheck);
  5998. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5999. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6000. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6001. unsigned int Answer =
  6002. MessageDialog(Message,
  6003. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6004. if (Answer == qaNeverAskAgain)
  6005. {
  6006. Continue = true;
  6007. WinConfiguration->UseSharedBookmarks = true;
  6008. }
  6009. else if (Answer == qaOK)
  6010. {
  6011. Continue = true;
  6012. WinConfiguration->UseSharedBookmarks = false;
  6013. }
  6014. else
  6015. {
  6016. Continue = false;
  6017. }
  6018. }
  6019. if (Continue)
  6020. {
  6021. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6022. try
  6023. {
  6024. UnicodeString Name = DirView(Side)->PathName;
  6025. bool Repeat;
  6026. do
  6027. {
  6028. Repeat = false;
  6029. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6030. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6031. // do not allow switching to location profiles,
  6032. // if we are not connected
  6033. HasDirView[osLocal] && (Terminal != NULL)))
  6034. {
  6035. TWindowLock Lock(this);
  6036. if (!TryOpenDirectory(Side, Name))
  6037. {
  6038. Repeat = true;
  6039. Mode = odBrowse;
  6040. }
  6041. }
  6042. }
  6043. while (Repeat);
  6044. }
  6045. __finally
  6046. {
  6047. delete VisitedDirectories;
  6048. }
  6049. }
  6050. }
  6051. //---------------------------------------------------------------------------
  6052. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6053. {
  6054. DebugAssert(!Terminal->CommandSessionOpened);
  6055. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6056. }
  6057. //---------------------------------------------------------------------------
  6058. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6059. {
  6060. bool Result = Terminal->IsCapable[Capability] ||
  6061. Terminal->CommandSessionOpened;
  6062. if (!Result)
  6063. {
  6064. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6065. if (!GUIConfiguration->ConfirmCommandSession)
  6066. {
  6067. Result = true;
  6068. }
  6069. else
  6070. {
  6071. TMessageParams Params(mpNeverAskAgainCheck);
  6072. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6073. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6074. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6075. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6076. if (Answer == qaNeverAskAgain)
  6077. {
  6078. GUIConfiguration->ConfirmCommandSession = false;
  6079. Result = true;
  6080. }
  6081. else if (Answer == qaOK)
  6082. {
  6083. Result = true;
  6084. }
  6085. }
  6086. if (Result)
  6087. {
  6088. Result = CommandSessionFallback();
  6089. }
  6090. }
  6091. return Result;
  6092. }
  6093. //---------------------------------------------------------------------------
  6094. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6095. {
  6096. if (EnsureCommandSessionFallback(fcAnyCommand))
  6097. {
  6098. DoConsoleDialog(Terminal, Command);
  6099. }
  6100. }
  6101. //---------------------------------------------------------------------------
  6102. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6103. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6104. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6105. {
  6106. if (!IsSideLocalBrowser(osRemote) &&
  6107. IsFileControl(DropSourceControl, osRemote) &&
  6108. (FDDExtMapFile != NULL))
  6109. {
  6110. Accept = true;
  6111. }
  6112. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6113. DebugAssert(FDDTargetControl != NULL);
  6114. }
  6115. //---------------------------------------------------------------------------
  6116. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6117. _di_IDataObject DataObj, int KeyState,
  6118. const TPoint & Point, int & Effect, bool & Accept)
  6119. {
  6120. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6121. }
  6122. //---------------------------------------------------------------------------
  6123. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6124. _di_IDataObject DataObj, int KeyState,
  6125. const TPoint & Point, int & Effect, bool & Accept)
  6126. {
  6127. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6128. }
  6129. //---------------------------------------------------------------------------
  6130. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6131. TObject * Sender)
  6132. {
  6133. DebugUsedParam(Sender);
  6134. DebugAssert(FDDTargetControl == Sender);
  6135. FDDTargetControl = NULL;
  6136. }
  6137. //---------------------------------------------------------------------------
  6138. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6139. {
  6140. FileControlDDDragLeave(SessionsPageControl);
  6141. }
  6142. //---------------------------------------------------------------------------
  6143. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6144. {
  6145. FileControlDDDragLeave(QueueView3);
  6146. }
  6147. //---------------------------------------------------------------------------
  6148. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6149. {
  6150. DebugAssert(!IsSideLocalBrowser(Side));
  6151. DebugUsedParam(Side);
  6152. bool Edit = false;
  6153. TRemoteFile * File = NULL;
  6154. UnicodeString FileName;
  6155. UnicodeString PointTo;
  6156. bool SymbolicLink = true;
  6157. if (RemoteDirView->ItemFocused)
  6158. {
  6159. DebugAssert(RemoteDirView->ItemFocused->Data);
  6160. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6161. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6162. if (Edit)
  6163. {
  6164. FileName = File->FileName;
  6165. PointTo = File->LinkTo;
  6166. }
  6167. else
  6168. {
  6169. PointTo = File->FileName;
  6170. }
  6171. }
  6172. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6173. Terminal->IsCapable[fcHardLink]))
  6174. {
  6175. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6176. if (Edit)
  6177. {
  6178. DebugAssert(File->FileName == FileName);
  6179. int Params = dfNoRecursive;
  6180. Terminal->ExceptionOnFail = true;
  6181. try
  6182. {
  6183. Terminal->DeleteFile(L"", File, &Params);
  6184. }
  6185. __finally
  6186. {
  6187. Terminal->ExceptionOnFail = false;
  6188. }
  6189. }
  6190. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6191. }
  6192. }
  6193. //---------------------------------------------------------------------------
  6194. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6195. {
  6196. return
  6197. (IsSideLocalBrowser(Side) ||
  6198. (HasActiveTerminal() &&
  6199. Terminal->ResolvingSymlinks &&
  6200. Terminal->IsCapable[fcSymbolicLink]));
  6201. }
  6202. //---------------------------------------------------------------------------
  6203. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6204. {
  6205. return
  6206. !IsSideLocalBrowser(FCurrentSide) &&
  6207. (RemoteDirView->ItemFocused != NULL) &&
  6208. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6209. Terminal->SessionData->ResolveSymlinks;
  6210. }
  6211. //---------------------------------------------------------------------------
  6212. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6213. {
  6214. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6215. FForceExecution = true;
  6216. try
  6217. {
  6218. DirView(osCurrent)->ExecuteCurrentFile();
  6219. }
  6220. __finally
  6221. {
  6222. FForceExecution = false;
  6223. }
  6224. }
  6225. //---------------------------------------------------------------------------
  6226. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6227. {
  6228. TEditorData ExternalEditor;
  6229. ExternalEditor.Editor = edExternal;
  6230. bool Remember = false;
  6231. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6232. {
  6233. if (Remember)
  6234. {
  6235. TEditorList * EditorList = new TEditorList;
  6236. try
  6237. {
  6238. *EditorList = *WinConfiguration->EditorList;
  6239. bool Found = false;
  6240. int i = 0;
  6241. while (!Found && (i < EditorList->Count))
  6242. {
  6243. const TEditorPreferences * Editor = EditorList->Editors[i];
  6244. if ((Editor->Data->Editor == edExternal) &&
  6245. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6246. {
  6247. Found = true;
  6248. }
  6249. i++;
  6250. }
  6251. if (!Found)
  6252. {
  6253. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6254. WinConfiguration->EditorList = EditorList;
  6255. }
  6256. }
  6257. __finally
  6258. {
  6259. delete EditorList;
  6260. }
  6261. }
  6262. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6263. }
  6264. }
  6265. //---------------------------------------------------------------------------
  6266. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6267. {
  6268. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6269. if (FFileFindTerminal == ATerminal)
  6270. {
  6271. FFileFindTerminal = NULL;
  6272. HideFileFindDialog();
  6273. }
  6274. if (FClipboardTerminal == ATerminal)
  6275. {
  6276. ClipboardClear(); // implies ClipboardStop
  6277. }
  6278. }
  6279. //---------------------------------------------------------------------------
  6280. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6281. {
  6282. DetachTerminal(Sender);
  6283. }
  6284. //---------------------------------------------------------------------------
  6285. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6286. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6287. {
  6288. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6289. DebugAssert(Terminal != NULL);
  6290. if (Data->Terminal == Terminal)
  6291. {
  6292. Data->Terminal = NULL;
  6293. }
  6294. }
  6295. //---------------------------------------------------------------------------
  6296. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6297. {
  6298. return NULL;
  6299. }
  6300. //---------------------------------------------------------------------------
  6301. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6302. {
  6303. UpdateControls();
  6304. SessionColor = TColor(0);
  6305. UpdateRemotePathComboBox(false);
  6306. NeedSession(false);
  6307. }
  6308. //---------------------------------------------------------------------------
  6309. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6310. {
  6311. try
  6312. {
  6313. // Cache, as the login dialog can change its value.
  6314. // See also DoShow. Also see TTerminalManager::NewSession.
  6315. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6316. try
  6317. {
  6318. if (ShowLogin)
  6319. {
  6320. bool ReloadSessions = !Startup;
  6321. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6322. }
  6323. }
  6324. __finally
  6325. {
  6326. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6327. // (so there was no chance for the user to open any session yet)
  6328. if ((ShowLogin || !Startup) &&
  6329. !WinConfiguration->KeepOpenWhenNoSession &&
  6330. // this rules out the implicit Commander's local-local browser
  6331. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6332. {
  6333. TerminateApplication();
  6334. }
  6335. }
  6336. }
  6337. catch (EAbort &)
  6338. {
  6339. // swallow
  6340. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6341. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6342. }
  6343. }
  6344. //---------------------------------------------------------------------------
  6345. void __fastcall TCustomScpExplorerForm::SessionListChanged()
  6346. {
  6347. TTerminalManager * Manager = TTerminalManager::Instance();
  6348. DebugAssert(!Manager->Updating);
  6349. TStrings * SessionList = Manager->SessionList;
  6350. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6351. Configuration->Usage->SetMax(L"MaxOpenedSessions", SessionList->Count);
  6352. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6353. try
  6354. {
  6355. while ((SessionsPageControl->PageCount > SessionList->Count + 1) ||
  6356. // Clear the design time unthemed tab
  6357. ((SessionsPageControl->PageCount > 0) &&
  6358. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6359. {
  6360. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6361. }
  6362. for (int Index = 0; Index <= SessionList->Count; Index++)
  6363. {
  6364. TThemeTabSheet * TabSheet;
  6365. if (Index >= SessionsPageControl->PageCount)
  6366. {
  6367. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6368. TabSheet->PageControl = SessionsPageControl;
  6369. }
  6370. else
  6371. {
  6372. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6373. }
  6374. bool IsSessionTab = (Index < SessionList->Count);
  6375. if (IsSessionTab)
  6376. {
  6377. TTerminal * Terminal = dynamic_cast<TTerminal *>(SessionList->Objects[Index]);
  6378. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6379. UpdateSessionTab(TabSheet);
  6380. }
  6381. else
  6382. {
  6383. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6384. TabSheet->Tag = 0; // not really needed
  6385. TabSheet->Shadowed = false;
  6386. TabSheet->ShowCloseButton = false;
  6387. // 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)
  6388. UpdateNewSessionTab();
  6389. }
  6390. }
  6391. }
  6392. __finally
  6393. {
  6394. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6395. }
  6396. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6397. }
  6398. //---------------------------------------------------------------------------
  6399. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6400. {
  6401. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6402. DebugAssert(TabSheet->ImageIndex == 0);
  6403. TabSheet->Caption =
  6404. WinConfiguration->SelectiveToolbarText ?
  6405. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6406. UnicodeString();
  6407. }
  6408. //---------------------------------------------------------------------------
  6409. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6410. {
  6411. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6412. }
  6413. //---------------------------------------------------------------------------
  6414. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6415. {
  6416. DebugFail();
  6417. return UnicodeString();
  6418. }
  6419. //---------------------------------------------------------------------------
  6420. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6421. {
  6422. if (DebugAlwaysTrue(TabSheet != NULL))
  6423. {
  6424. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6425. if (DebugAlwaysTrue(ASession != NULL))
  6426. {
  6427. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6428. if (ASession->LocalBrowser)
  6429. {
  6430. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6431. }
  6432. else
  6433. {
  6434. TabSheet->ImageIndex = AddSessionColor(Color);
  6435. }
  6436. TTerminalManager * Manager = TTerminalManager::Instance();
  6437. UnicodeString TabCaption = Manager->GetSessionTitle(ASession, true);
  6438. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6439. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6440. {
  6441. ThemeTabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6442. ThemeTabSheet->ShowCloseButton = CanCloseSession(ASession);
  6443. if (ThemeTabSheet->ShowCloseButton)
  6444. {
  6445. TabCaption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6446. }
  6447. }
  6448. TabSheet->Caption = TabCaption;
  6449. }
  6450. }
  6451. }
  6452. //---------------------------------------------------------------------------
  6453. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6454. {
  6455. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6456. }
  6457. //---------------------------------------------------------------------------
  6458. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6459. {
  6460. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6461. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6462. bool Result = (Session != NULL);
  6463. if (Result)
  6464. {
  6465. TTerminalManager::Instance()->ActiveSession = Session;
  6466. }
  6467. else
  6468. {
  6469. try
  6470. {
  6471. NewSession();
  6472. }
  6473. __finally
  6474. {
  6475. SessionListChanged();
  6476. }
  6477. FSessionsPageControlNewSessionTime = Now();
  6478. }
  6479. return Result;
  6480. }
  6481. //---------------------------------------------------------------------------
  6482. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6483. {
  6484. SessionTabSwitched();
  6485. }
  6486. //---------------------------------------------------------------------------
  6487. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6488. {
  6489. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6490. DebugAssert(TransferList != NULL);
  6491. UnicodeString Name;
  6492. if (TransferList->ItemIndex <= 0)
  6493. {
  6494. Name = L"";
  6495. }
  6496. else
  6497. {
  6498. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6499. }
  6500. if (FCopyParamAutoSelected.IsEmpty())
  6501. {
  6502. // if previous preset was not autoselected, make new preset the "default"
  6503. FCopyParamDefault = Name;
  6504. }
  6505. GUIConfiguration->CopyParamCurrent = Name;
  6506. }
  6507. //---------------------------------------------------------------------------
  6508. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6509. {
  6510. // sanity check
  6511. bool ExistingPreset =
  6512. (FTransferListHoverIndex >= 0) &&
  6513. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6514. DebugAssert(ExistingPreset);
  6515. if (ExistingPreset)
  6516. {
  6517. HDC DC = GetDC(0);
  6518. TCanvas * Canvas = new TCanvas();
  6519. try
  6520. {
  6521. Canvas->Handle = DC;
  6522. Canvas->Font = GetToolbarFont(this);
  6523. UnicodeString Name;
  6524. if (FTransferListHoverIndex == 0)
  6525. {
  6526. Name = L"";
  6527. }
  6528. else
  6529. {
  6530. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6531. }
  6532. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6533. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6534. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6535. static_cast<TObject*>(GetComponent(fcTransferList)));
  6536. UnicodeString InfoStr =
  6537. GUIConfiguration->CopyParamPreset[Name].
  6538. GetInfoStr(L"; ",
  6539. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6540. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6541. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6542. {
  6543. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6544. {
  6545. InfoStr.SetLength(InfoStr.Length() - 1);
  6546. }
  6547. InfoStr += Ellipsis;
  6548. }
  6549. // UpdateCaption does not cause invalidation of whole submenu, while
  6550. // setting Caption property does.
  6551. // also it probably does not resize the label, even if necessary
  6552. // (we do not want that anyway)
  6553. TransferLabel->UpdateCaption(InfoStr);
  6554. }
  6555. __finally
  6556. {
  6557. Canvas->Handle = NULL;
  6558. ReleaseDC(0, DC);
  6559. delete Canvas;
  6560. }
  6561. }
  6562. }
  6563. //---------------------------------------------------------------------------
  6564. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6565. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6566. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6567. {
  6568. if (FTransferListHoverIndex != AHoverIndex)
  6569. {
  6570. FTransferListHoverIndex = AHoverIndex;
  6571. UpdateTransferLabel();
  6572. }
  6573. }
  6574. //---------------------------------------------------------------------------
  6575. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6576. {
  6577. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6578. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6579. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6580. {
  6581. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6582. {
  6583. if (!NonVisualDataModule->Busy)
  6584. {
  6585. OpenDirectory(GetSide(osCurrent));
  6586. }
  6587. Message.Result = 1;
  6588. }
  6589. else
  6590. {
  6591. TForm::Dispatch(&Message);
  6592. }
  6593. }
  6594. else
  6595. {
  6596. TForm::Dispatch(&Message);
  6597. }
  6598. }
  6599. //---------------------------------------------------------------------------
  6600. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6601. {
  6602. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6603. if ((FIgnoreNextDialogChar != 0) &&
  6604. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6605. {
  6606. Message.Result = 1;
  6607. }
  6608. else
  6609. {
  6610. TForm::Dispatch(&Message);
  6611. }
  6612. FIgnoreNextDialogChar = 0;
  6613. }
  6614. //---------------------------------------------------------------------------
  6615. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6616. {
  6617. // The four low-order bits are used internally by Windows
  6618. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6619. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6620. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6621. // 61730 - restore thru double click - undocumented
  6622. // 61490 - maximize thru double click - undocumented
  6623. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6624. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6625. {
  6626. SysResizing(Cmd);
  6627. }
  6628. TForm::Dispatch(&Message);
  6629. }
  6630. //---------------------------------------------------------------------------
  6631. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6632. {
  6633. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6634. // Also there no point returning TRUE as we are not able to
  6635. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6636. // Hence the process termination might be safer :)
  6637. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6638. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6639. {
  6640. Message.Result = FALSE;
  6641. }
  6642. else
  6643. {
  6644. Message.Result = TRUE;
  6645. }
  6646. // Do not call default handling as that triggers OnCloseQuery,
  6647. // where our implementation will popup confirmation dialogs, what we do not want,
  6648. // as per Vista guidelines:
  6649. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6650. }
  6651. //---------------------------------------------------------------------------
  6652. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6653. {
  6654. if (Message.EndSession && IsApplicationMinimized())
  6655. {
  6656. // WORKAROUND
  6657. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6658. // when it receives WM_ENDSESSION,
  6659. // but that sometimes (particularly when application is minimized) cause crashes.
  6660. // Crash popups message that blocks log off.
  6661. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6662. // so we call ExitProcess() immediately, not even trying to cleanup.
  6663. // It still causes beep, so there's likely some popup, but it does not block
  6664. // log off.
  6665. ExitProcess(0);
  6666. }
  6667. TForm::Dispatch(&Message);
  6668. }
  6669. #ifdef _DEBUG
  6670. //---------------------------------------------------------------------------
  6671. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6672. {
  6673. TForm::Dispatch(&Message);
  6674. }
  6675. #endif
  6676. //---------------------------------------------------------------------------
  6677. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6678. {
  6679. }
  6680. //---------------------------------------------------------------------------
  6681. void __fastcall TCustomScpExplorerForm::DoShow()
  6682. {
  6683. // Called from TCustomScpExplorerForm::CMShowingChanged
  6684. // only now are the controls resized finally, so the size constraints
  6685. // will not conflict with possibly very small window size
  6686. RestoreFormParams();
  6687. // This indicates that the form was rescaled after parameters were loaded and
  6688. // as most probably the parameters were actually saved with the current form scaling
  6689. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6690. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6691. // and no rounding problems will occur.
  6692. // See also RestoreForm.
  6693. // (we should be safe to call RestoreParams unconditionally)
  6694. if (PixelsPerInch != Screen->PixelsPerInch)
  6695. {
  6696. RestoreParams();
  6697. }
  6698. FixControlsPlacement();
  6699. if (Position == poDefaultPosOnly)
  6700. {
  6701. CutFormToDesktop(this);
  6702. }
  6703. TForm::DoShow();
  6704. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6705. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6706. // This was previously in NeedSession, where other related code is.
  6707. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  6708. // unpleasanly shows briefly.
  6709. if (!WinConfiguration->ShowLoginWhenNoSession &&
  6710. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6711. // This detects if workspace was saved the last time the main widow was closed
  6712. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6713. {
  6714. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false);
  6715. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6716. }
  6717. // Before main window displays
  6718. if (ManagedSession == NULL)
  6719. {
  6720. StartingWithoutSession();
  6721. }
  6722. UpdatePixelsPerInchMainWindowCounter();
  6723. FShowing = true;
  6724. }
  6725. //---------------------------------------------------------------------------
  6726. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6727. {
  6728. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6729. }
  6730. //---------------------------------------------------------------------------
  6731. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  6732. {
  6733. SessionListChanged();
  6734. InitStatusBar();
  6735. UpdateControls();
  6736. }
  6737. //---------------------------------------------------------------------------
  6738. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6739. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6740. TNotifyEvent OnBalloonClick, TObject * UserData)
  6741. {
  6742. bool Do;
  6743. UnicodeString Message;
  6744. if (E == NULL)
  6745. {
  6746. Message = Str;
  6747. Do = true;
  6748. }
  6749. else
  6750. {
  6751. Do = ExceptionMessage(E, Message);
  6752. }
  6753. if (Do && WinConfiguration->BalloonNotifications)
  6754. {
  6755. UnicodeString Title;
  6756. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6757. {
  6758. Message = TrimLeft(Message);
  6759. }
  6760. else
  6761. {
  6762. Message = UnformatMessage(Message);
  6763. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6764. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6765. Title = LoadResourceString(Captions[Type]);
  6766. if (Terminal != NULL)
  6767. {
  6768. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  6769. Title = FORMAT(L"%s - %s",
  6770. (TTerminalManager::Instance()->GetSessionTitle(Session, true), Title));
  6771. }
  6772. }
  6773. if (Seconds == 0)
  6774. {
  6775. Seconds = WinConfiguration->NotificationsTimeout;
  6776. }
  6777. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6778. }
  6779. }
  6780. //---------------------------------------------------------------------------
  6781. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6782. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6783. UnicodeString HelpKeyword, const TMessageParams * Params,
  6784. TTerminal * Terminal)
  6785. {
  6786. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6787. (FOnFeedSynchronizeError != NULL)) &&
  6788. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6789. {
  6790. if (FOnFeedSynchronizeError != NULL)
  6791. {
  6792. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6793. }
  6794. else
  6795. {
  6796. DebugAssert(FErrorList != NULL);
  6797. TStringList * MoreMessagesCopy = NULL;
  6798. if (MoreMessages)
  6799. {
  6800. MoreMessagesCopy = new TStringList();
  6801. MoreMessagesCopy->Assign(MoreMessages);
  6802. }
  6803. FErrorList->AddObject(Message, MoreMessagesCopy);
  6804. }
  6805. PopupTrayBalloon(Terminal, Message, Type);
  6806. return ContinueAnswer(Answers);
  6807. }
  6808. else
  6809. {
  6810. bool UseBalloon = FTrayIcon->Visible;
  6811. if (UseBalloon)
  6812. {
  6813. PopupTrayBalloon(Terminal, Message, Type);
  6814. }
  6815. unsigned int Result;
  6816. try
  6817. {
  6818. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6819. }
  6820. __finally
  6821. {
  6822. // cancel only balloon we popped up, otherwise we may cancel notification
  6823. // balloon that was there before the message dialog
  6824. // (such as "dd confirmation opt in balloon)
  6825. if (UseBalloon)
  6826. {
  6827. FTrayIcon->CancelBalloon();
  6828. }
  6829. }
  6830. return Result;
  6831. }
  6832. }
  6833. //---------------------------------------------------------------------------
  6834. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6835. TTerminal * Terminal, Exception * E)
  6836. {
  6837. if (FTrayIcon->Visible)
  6838. {
  6839. PopupTrayBalloon(Terminal, L"", qtError, E);
  6840. }
  6841. // particularly prevent opening new session from jump list,
  6842. // while exception is shown
  6843. NonVisualDataModule->StartBusy();
  6844. try
  6845. {
  6846. ShowExtendedExceptionEx(Terminal, E);
  6847. }
  6848. __finally
  6849. {
  6850. NonVisualDataModule->EndBusy();
  6851. FTrayIcon->CancelBalloon();
  6852. }
  6853. }
  6854. //---------------------------------------------------------------------------
  6855. void __fastcall TCustomScpExplorerForm::SessionReady()
  6856. {
  6857. InitStatusBar();
  6858. // cannot rely on active page being page for active terminal,
  6859. // as it can happen that active page is the "new session" page
  6860. // (e.g. when reconnecting active terminal, while login dialog
  6861. // invoked from "new session" page is modal)
  6862. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  6863. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  6864. }
  6865. //---------------------------------------------------------------------------
  6866. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6867. TTerminal * Terminal, Exception * E)
  6868. {
  6869. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6870. if (!Terminal->Active)
  6871. {
  6872. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6873. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6874. {
  6875. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6876. }
  6877. }
  6878. }
  6879. //---------------------------------------------------------------------------
  6880. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6881. UnicodeString Message, TQueryType Type,
  6882. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6883. {
  6884. if ((E == NULL) ||
  6885. ExceptionMessage(E, Message))
  6886. {
  6887. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6888. if (Important)
  6889. {
  6890. Seconds *= 5;
  6891. }
  6892. UnicodeString NoteMessage(UnformatMessage(Message));
  6893. if (Terminal != NULL)
  6894. {
  6895. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  6896. NoteMessage = FORMAT(L"%s: %s",
  6897. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  6898. }
  6899. if (WinConfiguration->BalloonNotifications)
  6900. {
  6901. AddNote(NoteMessage);
  6902. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6903. }
  6904. else
  6905. {
  6906. FlashOnBackground();
  6907. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6908. }
  6909. }
  6910. }
  6911. //---------------------------------------------------------------------------
  6912. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6913. {
  6914. RestoreApp();
  6915. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6916. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6917. !NonVisualDataModule->Busy)
  6918. {
  6919. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6920. TTerminalManager::Instance()->ActiveSession = Terminal;
  6921. if (!ComponentVisible[fcQueueView])
  6922. {
  6923. ToggleQueueVisibility();
  6924. GoToQueue();
  6925. }
  6926. }
  6927. }
  6928. //---------------------------------------------------------------------------
  6929. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6930. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6931. {
  6932. UnicodeString Message;
  6933. TNotifyEvent OnClick = NULL;
  6934. TObject * UserData = NULL;
  6935. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6936. switch (Event)
  6937. {
  6938. case qeEmptyButMonitored:
  6939. if ((ATerminal != Terminal) || QueueInvisible)
  6940. {
  6941. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6942. OnClick = QueueEmptyNoteClicked;
  6943. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6944. TerminalNoteData->Terminal = ATerminal;
  6945. UserData = TerminalNoteData;
  6946. }
  6947. break;
  6948. case qeEmpty:
  6949. OperationComplete(ATerminal->QueueOperationStart);
  6950. break;
  6951. case qePendingUserAction:
  6952. if ((ATerminal != Terminal) ||
  6953. (QueueInvisible && !IsQueueAutoPopup()))
  6954. {
  6955. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6956. }
  6957. break;
  6958. default:
  6959. DebugFail();
  6960. }
  6961. if (!Message.IsEmpty())
  6962. {
  6963. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6964. }
  6965. }
  6966. //---------------------------------------------------------------------------
  6967. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6968. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6969. {
  6970. if (FDDExtMapFile != NULL)
  6971. {
  6972. CloseHandle(FDDExtMapFile);
  6973. FDDExtMapFile = NULL;
  6974. }
  6975. if (WinConfiguration->DDFakeFile)
  6976. {
  6977. DDFakeFileInitDrag(FileList, Created);
  6978. }
  6979. }
  6980. //---------------------------------------------------------------------------
  6981. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6982. {
  6983. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6984. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6985. {
  6986. FFakeFileDropTarget = FileName;
  6987. }
  6988. }
  6989. //---------------------------------------------------------------------------
  6990. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6991. {
  6992. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6993. if (!ForceDirectories(ApiPath(Result)))
  6994. {
  6995. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6996. }
  6997. FileSetAttr(ApiPath(Result), faHidden);
  6998. return Result;
  6999. }
  7000. //---------------------------------------------------------------------------
  7001. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7002. bool & Created)
  7003. {
  7004. FFakeFileDropTarget = UnicodeString();
  7005. FDragFakeDirectory = CreateFakeTransferDirectory();
  7006. FileList->AddItem(NULL, FDragFakeDirectory);
  7007. Created = true;
  7008. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7009. {
  7010. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7011. }
  7012. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7013. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7014. {
  7015. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7016. TDragExtCommStruct* CommStruct;
  7017. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7018. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7019. DebugAssert(CommStruct != NULL);
  7020. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7021. CommStruct->Dragging = true;
  7022. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7023. LENOF(CommStruct->DropDest));
  7024. NULL_TERMINATE(CommStruct->DropDest);
  7025. UnmapViewOfFile(CommStruct);
  7026. }
  7027. }
  7028. //---------------------------------------------------------------------------
  7029. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7030. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7031. {
  7032. bool Result;
  7033. DebugAssert(!IsLocalBrowserMode());
  7034. if (Sender == RemoteDirView)
  7035. {
  7036. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7037. }
  7038. else
  7039. {
  7040. DebugAssert(Sender == RemoteDriveView);
  7041. TStrings * FileList = RemoteDriveView->DragFileList();
  7042. try
  7043. {
  7044. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7045. }
  7046. __finally
  7047. {
  7048. delete FileList;
  7049. }
  7050. }
  7051. if (FDDTargetControl == RemoteDriveView)
  7052. {
  7053. RemoteDriveView->UpdateDropTarget();
  7054. }
  7055. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7056. // or from tree view to dir view.
  7057. // foMove happens when file/dir is dragged from remote tree view outside of
  7058. // application via dragex
  7059. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7060. (DropSourceControl == RemoteDriveView))
  7061. {
  7062. RemoteDriveView->UpdateDropSource();
  7063. }
  7064. return Result;
  7065. }
  7066. //---------------------------------------------------------------------------
  7067. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7068. {
  7069. DebugAssert(!IsLocalBrowserMode());
  7070. // This also handles drops of remote files to queue.
  7071. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7072. SAFE_DESTROY(FDDFileList);
  7073. if (!FFakeFileDropTarget.IsEmpty())
  7074. {
  7075. RemoveDir(ApiPath(FFakeFileDropTarget));
  7076. }
  7077. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7078. {
  7079. try
  7080. {
  7081. TDragResult DDResult = (Sender == RemoteDirView) ?
  7082. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7083. // Focus is moved to the target application,
  7084. // but as we are going to present the UI, we need to steal the focus back.
  7085. // This most likely won't work though (windows does not allow application
  7086. // to steal focus most of the time)
  7087. Application->BringToFront();
  7088. // On older version of Windows we never got drMove here, see also comment below.
  7089. // On Windows 10, we get the "move".
  7090. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7091. {
  7092. UnicodeString TargetDirectory;
  7093. TFileOperation Operation;
  7094. // drInvalid may mean drMove, see comment below
  7095. switch (DDResult)
  7096. {
  7097. case drCopy:
  7098. Operation = foCopy;
  7099. break;
  7100. case drMove:
  7101. Operation = foMove;
  7102. break;
  7103. default:
  7104. DebugFail();
  7105. case drInvalid:
  7106. // prefer "copy" for safety
  7107. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7108. break;
  7109. }
  7110. TTransferOperationParam Param;
  7111. UnicodeString CounterName;
  7112. bool ForceQueue;
  7113. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7114. {
  7115. // we get drInvalid both if d&d was intercepted by ddext,
  7116. // and when users drops on no-drop location.
  7117. // we tell the difference by existence of response from ddext,
  7118. // so we ignore absence of response in this case
  7119. if (DDResult != drInvalid)
  7120. {
  7121. // here we know that the extension is installed,
  7122. // as it is checked as soon as drag&drop starts from
  7123. // RemoteFileControlDDCreateDragFileList
  7124. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7125. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7126. {
  7127. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7128. }
  7129. else
  7130. {
  7131. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7132. }
  7133. }
  7134. }
  7135. else
  7136. {
  7137. // download using fake file
  7138. Param.Temp = false;
  7139. Param.DragDrop = true;
  7140. if (ForceQueue)
  7141. {
  7142. Param.Queue = asOn;
  7143. }
  7144. if (Sender == RemoteDirView)
  7145. {
  7146. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7147. }
  7148. if (RemoteFileControlFileOperation(Sender, Operation,
  7149. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7150. {
  7151. Configuration->Usage->Inc(CounterName);
  7152. }
  7153. }
  7154. }
  7155. }
  7156. __finally
  7157. {
  7158. CloseHandle(FDDExtMapFile);
  7159. FDDExtMapFile = NULL;
  7160. delete FDragFakeMonitors;
  7161. FDragFakeMonitors = NULL;
  7162. RemoveDir(ApiPath(FDragFakeDirectory));
  7163. FDragFakeDirectory = L"";
  7164. FFakeFileDropTarget = UnicodeString();
  7165. }
  7166. }
  7167. if (FDragDropSshTerminate.get() != NULL)
  7168. {
  7169. FDragDropSshTerminate->Rethrow();
  7170. }
  7171. }
  7172. //---------------------------------------------------------------------------
  7173. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7174. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7175. {
  7176. // Remember drop effect so we know (when user drops files), if we copy or move
  7177. FLastDropEffect = dwEffect;
  7178. }
  7179. //---------------------------------------------------------------------------
  7180. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7181. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7182. {
  7183. bool Result;
  7184. if (FDDTargetControl == QueueView3)
  7185. {
  7186. Directory = DefaultDownloadTargetDirectory();
  7187. Result = true;
  7188. CounterName = L"DownloadsDragDropQueue";
  7189. ForceQueue = true;
  7190. }
  7191. else if (!FFakeFileDropTarget.IsEmpty())
  7192. {
  7193. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7194. Result = true;
  7195. ForceQueue = false;
  7196. CounterName = L"DownloadsDragDropFakeFile";
  7197. }
  7198. else
  7199. {
  7200. ForceQueue = false;
  7201. Enabled = false;
  7202. try
  7203. {
  7204. int Timer = 0;
  7205. Result = false;
  7206. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7207. {
  7208. {
  7209. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7210. TDragExtCommStruct* CommStruct;
  7211. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7212. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7213. DebugAssert(CommStruct != NULL);
  7214. Result = !CommStruct->Dragging;
  7215. if (Result)
  7216. {
  7217. Directory = ExtractFilePath(CommStruct->DropDest);
  7218. CounterName = L"DownloadsDragDropExternalExt";
  7219. }
  7220. UnmapViewOfFile(CommStruct);
  7221. }
  7222. if (!Result)
  7223. {
  7224. Sleep(50);
  7225. Timer += 50;
  7226. Application->ProcessMessages();
  7227. }
  7228. }
  7229. }
  7230. __finally
  7231. {
  7232. Enabled = true;
  7233. }
  7234. }
  7235. return Result;
  7236. }
  7237. //---------------------------------------------------------------------------
  7238. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7239. const UnicodeString TempDir, int SecDelay)
  7240. {
  7241. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7242. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7243. if (FDelayedDeletionTimer == NULL)
  7244. {
  7245. DebugAssert(HandleAllocated());
  7246. FDelayedDeletionTimer = new TTimer(this);
  7247. FDelayedDeletionTimer->Interval = 10000;
  7248. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7249. }
  7250. else
  7251. {
  7252. FDelayedDeletionTimer->Enabled = true;
  7253. }
  7254. }
  7255. //---------------------------------------------------------------------------
  7256. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7257. {
  7258. DebugAssert(FDelayedDeletionList != NULL);
  7259. TDateTime N = Now();
  7260. TDateTime Alert;
  7261. UnicodeString Directory;
  7262. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7263. {
  7264. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7265. if ((N >= Alert) || (Sender == NULL))
  7266. {
  7267. Directory = FDelayedDeletionList->Strings[Index];
  7268. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7269. {
  7270. FDelayedDeletionList->Delete(Index);
  7271. }
  7272. }
  7273. }
  7274. if (FDelayedDeletionList->Count == 0)
  7275. {
  7276. FDelayedDeletionTimer->Enabled = false;
  7277. }
  7278. }
  7279. //---------------------------------------------------------------------------
  7280. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7281. {
  7282. DebugAssert(!IsLocalBrowserMode());
  7283. if (IsFileControl(FDDTargetControl, osRemote) ||
  7284. (FDDTargetControl == SessionsPageControl))
  7285. {
  7286. TTerminal * TargetTerminal = NULL;
  7287. TFileOperation Operation = foNone;
  7288. if (FDDTargetControl == SessionsPageControl)
  7289. {
  7290. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7291. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7292. // do not allow dropping on the "+" tab
  7293. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7294. if (TargetTerminal != NULL)
  7295. {
  7296. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7297. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7298. {
  7299. Operation = foRemoteMove;
  7300. }
  7301. else
  7302. {
  7303. Operation = foRemoteCopy;
  7304. }
  7305. }
  7306. }
  7307. else
  7308. {
  7309. if (FLastDropEffect == DROPEFFECT_MOVE)
  7310. {
  7311. Operation = foRemoteMove;
  7312. }
  7313. else if (FLastDropEffect == DROPEFFECT_COPY)
  7314. {
  7315. Operation = foRemoteCopy;
  7316. }
  7317. }
  7318. if (Operation != foNone)
  7319. {
  7320. RemoteFileControlFileOperation(DropSourceControl,
  7321. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7322. }
  7323. // abort drag&drop
  7324. Abort();
  7325. }
  7326. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7327. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7328. (FDDTargetControl != QueueView3))
  7329. {
  7330. DebugAssert(!FDragTempDir.IsEmpty());
  7331. TTransferType Type;
  7332. UnicodeString TempDir = FDragTempDir;
  7333. // We clear FDragTempDir before calling
  7334. // just in case it fail (raises exception)
  7335. FDragTempDir = L"";
  7336. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7337. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7338. // empty directory parameter means temp directory -> don't display it!
  7339. UnicodeString TargetDir = L"";
  7340. int Options = 0;
  7341. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7342. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7343. {
  7344. Abort();
  7345. }
  7346. // TargetDir is set when dropped on local file control
  7347. // (this was workaround for legacy dirview event handling, now it should be
  7348. // made prettier)
  7349. if (TargetDir.IsEmpty())
  7350. {
  7351. TargetDir = TempDir;
  7352. if (ForceDirectories(ApiPath(TargetDir)))
  7353. {
  7354. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7355. FPendingTempSpaceWarn = true;
  7356. try
  7357. {
  7358. FDragDropOperation = true;
  7359. int Params = cpTemporary |
  7360. (Type == ttMove ? cpDelete : 0);
  7361. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7362. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7363. }
  7364. __finally
  7365. {
  7366. FDragDropOperation = false;
  7367. FPendingTempSpaceWarn = false;
  7368. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7369. }
  7370. }
  7371. else
  7372. {
  7373. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7374. }
  7375. }
  7376. }
  7377. }
  7378. //---------------------------------------------------------------------------
  7379. void __fastcall TCustomScpExplorerForm::DDDownload(
  7380. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7381. {
  7382. DebugAssert(!IsLocalBrowserMode());
  7383. TAutoBatch AutoBatch(this);
  7384. UpdateCopyParamCounters(*CopyParam);
  7385. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7386. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7387. {
  7388. RemoteDriveView->UpdateDropSource();
  7389. }
  7390. }
  7391. //---------------------------------------------------------------------------
  7392. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7393. {
  7394. public:
  7395. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7396. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7397. {
  7398. }
  7399. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7400. {
  7401. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7402. TDataObjectFilesEx::AllowData(FormatEtc);
  7403. }
  7404. };
  7405. //---------------------------------------------------------------------------
  7406. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7407. TObject * Sender, TDataObject *& DataObject)
  7408. {
  7409. if (FDDExtMapFile != NULL)
  7410. {
  7411. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7412. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7413. {
  7414. Abort();
  7415. }
  7416. if (FileList->Count > 0)
  7417. {
  7418. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7419. if (!FilesObject->IsValid(true, true))
  7420. {
  7421. FilesObject->_Release();
  7422. }
  7423. else
  7424. {
  7425. DataObject = FilesObject;
  7426. }
  7427. }
  7428. }
  7429. }
  7430. //---------------------------------------------------------------------------
  7431. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7432. {
  7433. DebugFail();
  7434. }
  7435. //---------------------------------------------------------------------------
  7436. void __fastcall TCustomScpExplorerForm::GoToTree()
  7437. {
  7438. ComponentVisible[fcRemoteTree] = true;
  7439. DriveView(osRemote)->SetFocus();
  7440. }
  7441. //---------------------------------------------------------------------------
  7442. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7443. TPanelExport Export)
  7444. {
  7445. TCustomDirView * DirView = this->DirView(Side);
  7446. std::unique_ptr<TStrings> ExportData(new TStringList());
  7447. switch (Export)
  7448. {
  7449. case pePath:
  7450. ExportData->Add(DirView->PathName);
  7451. break;
  7452. case peFileList:
  7453. case peFullFileList:
  7454. {
  7455. bool FullPath = (Export == peFullFileList);
  7456. DirView->CreateFileList(false, FullPath, ExportData.get());
  7457. for (int Index = 0; Index < ExportData->Count; Index++)
  7458. {
  7459. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7460. {
  7461. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7462. }
  7463. }
  7464. }
  7465. break;
  7466. default:
  7467. DebugFail();
  7468. }
  7469. return ExportData.release();
  7470. }
  7471. //---------------------------------------------------------------------------
  7472. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7473. TPanelExport Export, TPanelExportDestination Destination)
  7474. {
  7475. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7476. PanelExportStore(Side, Export, Destination, ExportData.get());
  7477. }
  7478. //---------------------------------------------------------------------------
  7479. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7480. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7481. TStrings * ExportData)
  7482. {
  7483. if (Destination == pedClipboard)
  7484. {
  7485. TInstantOperationVisualizer Visualizer;
  7486. CopyToClipboard(ExportData);
  7487. }
  7488. else
  7489. {
  7490. DebugFail();
  7491. }
  7492. }
  7493. //---------------------------------------------------------------------------
  7494. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7495. {
  7496. TCustomDirView * DirView = this->DirView(Side);
  7497. UnicodeString Mask = DirView->Mask;
  7498. if (DoFilterMaskDialog(DirView, Mask))
  7499. {
  7500. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7501. Configuration->Usage->Inc(L"Filters");
  7502. }
  7503. }
  7504. //---------------------------------------------------------------------------
  7505. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7506. {
  7507. return FQueueController->DefaultOperation();
  7508. }
  7509. //---------------------------------------------------------------------------
  7510. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7511. TQueueOperation Operation, void ** Param)
  7512. {
  7513. switch (Operation)
  7514. {
  7515. case qoPreferences:
  7516. return true;
  7517. case qoGoTo:
  7518. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7519. case qoOnceEmpty:
  7520. return IsAnythingQueued();
  7521. default:
  7522. return FQueueController->AllowOperation(Operation, Param);
  7523. }
  7524. }
  7525. //---------------------------------------------------------------------------
  7526. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7527. {
  7528. if (DebugAlwaysTrue(QueueView3->Visible))
  7529. {
  7530. QueueView3->SetFocus();
  7531. }
  7532. }
  7533. //---------------------------------------------------------------------------
  7534. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7535. TQueueOperation Operation, void * Param)
  7536. {
  7537. if (Operation == qoGoTo)
  7538. {
  7539. GoToQueue();
  7540. }
  7541. else if (Operation == qoPreferences)
  7542. {
  7543. PreferencesDialog(pmQueue);
  7544. }
  7545. else
  7546. {
  7547. FQueueController->ExecuteOperation(Operation, Param);
  7548. }
  7549. }
  7550. //---------------------------------------------------------------------------
  7551. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7552. {
  7553. return (Queue != NULL) && Queue->Enabled;
  7554. }
  7555. //---------------------------------------------------------------------------
  7556. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7557. {
  7558. DebugAssert(Queue != NULL);
  7559. if (Queue != NULL)
  7560. {
  7561. Queue->Enabled = !Queue->Enabled;
  7562. }
  7563. }
  7564. //---------------------------------------------------------------------------
  7565. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7566. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7567. {
  7568. FQueueActedItem = QueueView3->ItemFocused;
  7569. }
  7570. //---------------------------------------------------------------------------
  7571. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7572. {
  7573. return TopDock->Height + QueueSplitter->Height;
  7574. }
  7575. //---------------------------------------------------------------------------
  7576. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7577. {
  7578. return
  7579. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7580. (QueueDock->Visible ? QueueDock->Height : 0) +
  7581. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7582. }
  7583. //---------------------------------------------------------------------------
  7584. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7585. {
  7586. return ScaleByTextHeight(this, 48);
  7587. }
  7588. //---------------------------------------------------------------------------
  7589. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7590. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7591. {
  7592. // when queue is hidden by double-clicking splitter, stray attempt to
  7593. // resize the panel with strange value arrives, make sure it is ignored
  7594. if (ComponentVisible[fcQueueView])
  7595. {
  7596. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7597. if (NewSize > MaxHeightLimit)
  7598. {
  7599. NewSize = MaxHeightLimit;
  7600. }
  7601. int MinHeightLimit =
  7602. GetStaticQueuePanelComponentsHeight() +
  7603. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7604. GetMinQueueViewHeight();
  7605. if (NewSize < MinHeightLimit)
  7606. {
  7607. NewSize = MinHeightLimit;
  7608. }
  7609. }
  7610. else
  7611. {
  7612. Accept = false;
  7613. }
  7614. }
  7615. //---------------------------------------------------------------------------
  7616. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7617. {
  7618. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7619. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7620. if (QueueViewHeight < GetMinQueueViewHeight())
  7621. {
  7622. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7623. }
  7624. }
  7625. //---------------------------------------------------------------------------
  7626. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7627. TDragObject *& /*DragObject*/)
  7628. {
  7629. FQueueActedItem = QueueView3->ItemFocused;
  7630. }
  7631. //---------------------------------------------------------------------------
  7632. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7633. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7634. {
  7635. Accept = true;
  7636. if (Source == QueueView3)
  7637. {
  7638. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7639. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7640. if (Accept)
  7641. {
  7642. TQueueItemProxy * QueueItem;
  7643. TQueueItemProxy * DestQueueItem;
  7644. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7645. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7646. Accept = (QueueItem != DestQueueItem) &&
  7647. (QueueItem->Status == TQueueItem::qsPending) &&
  7648. (DestQueueItem->Status == TQueueItem::qsPending);
  7649. }
  7650. }
  7651. }
  7652. //---------------------------------------------------------------------------
  7653. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7654. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7655. {
  7656. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7657. {
  7658. TQueueItemProxy * QueueItem;
  7659. TQueueItemProxy * DestQueueItem;
  7660. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7661. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7662. QueueItem->Move(DestQueueItem);
  7663. }
  7664. }
  7665. //---------------------------------------------------------------------------
  7666. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7667. {
  7668. if ((QueueView3->ItemFocused == NULL) &&
  7669. (QueueView3->Items->Count > 0))
  7670. {
  7671. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7672. }
  7673. QueueLabelUpdateStatus();
  7674. }
  7675. //---------------------------------------------------------------------------
  7676. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7677. {
  7678. QueueLabelUpdateStatus();
  7679. }
  7680. //---------------------------------------------------------------------------
  7681. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7682. {
  7683. QueueLabel->UpdateStatus();
  7684. }
  7685. //---------------------------------------------------------------------------
  7686. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7687. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7688. {
  7689. if (Selected)
  7690. {
  7691. NonVisualDataModule->UpdateNonVisibleActions();
  7692. }
  7693. }
  7694. //---------------------------------------------------------------------------
  7695. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7696. {
  7697. TDragDropFilesEx * Result = NULL;
  7698. if (Sender == SessionsPageControl)
  7699. {
  7700. Result = FSessionsDragDropFilesEx;
  7701. }
  7702. else if (Sender == QueueView3)
  7703. {
  7704. Result = FQueueDragDropFilesEx;
  7705. }
  7706. else
  7707. {
  7708. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7709. if (DirView != NULL)
  7710. {
  7711. Result = DirView->DragDropFilesEx;
  7712. }
  7713. else
  7714. {
  7715. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7716. if (DriveView != NULL)
  7717. {
  7718. Result = DriveView->DragDropFilesEx;
  7719. }
  7720. }
  7721. }
  7722. DebugAssert(Result != NULL);
  7723. return Result;
  7724. }
  7725. //---------------------------------------------------------------------------
  7726. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7727. {
  7728. return (DView->SelCount == DView->FilesCount);
  7729. }
  7730. //---------------------------------------------------------------------------
  7731. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7732. {
  7733. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7734. }
  7735. //---------------------------------------------------------------------------
  7736. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7737. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7738. {
  7739. DebugAssert(!IsLocalBrowserMode());
  7740. TFileOperation Operation;
  7741. switch (Effect)
  7742. {
  7743. case DROPEFFECT_MOVE:
  7744. Operation = foMove;
  7745. break;
  7746. case DROPEFFECT_COPY:
  7747. // occures on WinXP (reported by user)
  7748. default:
  7749. Operation = foCopy;
  7750. break;
  7751. };
  7752. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7753. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7754. if (DragDropFilesEx->FileList->Count > 0)
  7755. {
  7756. TStrings * FileList = new TStringList();
  7757. try
  7758. {
  7759. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7760. {
  7761. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7762. }
  7763. FDragDropOperation = true;
  7764. TTransferOperationParam Param;
  7765. Param.TargetDirectory = TargetPath;
  7766. // upload, no temp dirs
  7767. Param.Temp = false;
  7768. Param.DragDrop = DragDrop;
  7769. Param.Options =
  7770. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7771. if (ForceQueue)
  7772. {
  7773. Param.Queue = asOn;
  7774. }
  7775. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7776. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7777. {
  7778. if (IsFileControl(DropSourceControl, osLocal))
  7779. {
  7780. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7781. }
  7782. else
  7783. {
  7784. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7785. }
  7786. }
  7787. }
  7788. __finally
  7789. {
  7790. FDragDropOperation = false;
  7791. delete FileList;
  7792. }
  7793. }
  7794. }
  7795. //---------------------------------------------------------------------------
  7796. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7797. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7798. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7799. {
  7800. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7801. }
  7802. //---------------------------------------------------------------------------
  7803. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7804. TObject * Sender, int grfKeyState, int & dwEffect)
  7805. {
  7806. DebugAssert(!IsLocalBrowserMode());
  7807. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7808. if ((dwEffect != DROPEFFECT_NONE) &&
  7809. IsFileControl(DropSourceControl, osRemote))
  7810. {
  7811. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7812. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7813. (RemoteDirView->DropTarget == NULL))
  7814. {
  7815. dwEffect = DROPEFFECT_NONE;
  7816. }
  7817. else
  7818. {
  7819. if (dwEffect == DROPEFFECT_COPY)
  7820. {
  7821. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  7822. // currently we support copying always (at least via temporary directory);
  7823. // remove associated checks once this all proves stable and working
  7824. bool CopyCapable = true;
  7825. // if we do not support neither of operations, there's no discussion
  7826. if (!MoveCapable && !CopyCapable)
  7827. {
  7828. dwEffect = DROPEFFECT_NONE;
  7829. }
  7830. else
  7831. {
  7832. // The default effect inside remote panel is move,
  7833. // unless we do not support it, but support copy
  7834. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7835. {
  7836. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7837. }
  7838. else
  7839. {
  7840. // with ctrl-down, we want copy unless it is not supported
  7841. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7842. }
  7843. }
  7844. }
  7845. }
  7846. }
  7847. }
  7848. //---------------------------------------------------------------------------
  7849. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7850. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7851. {
  7852. DebugAssert(!IsLocalBrowserMode());
  7853. if (FDDTotalSize >= 0)
  7854. {
  7855. if (File->IsDirectory)
  7856. {
  7857. FDDTotalSize = -1;
  7858. }
  7859. else
  7860. {
  7861. FDDTotalSize += File->Size;
  7862. }
  7863. }
  7864. DebugAssert(!FDragTempDir.IsEmpty());
  7865. // TODO: this is quite ineffective
  7866. // TODO: what if invalid character replacement is disabled?
  7867. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7868. UnicodeString TransferFileName;
  7869. if (Sender == RemoteDriveView)
  7870. {
  7871. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7872. }
  7873. else
  7874. {
  7875. TransferFileName = File->FileName;
  7876. }
  7877. FDDFileList->AddObject(TransferFileName, File);
  7878. }
  7879. //---------------------------------------------------------------------------
  7880. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7881. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7882. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7883. {
  7884. // sometimes we do not get DDEnd so the list is not released
  7885. SAFE_DESTROY(FDDFileList);
  7886. FDDFileList = new TStringList();
  7887. FDragTempDir = WinConfiguration->TemporaryDir();
  7888. FDDTotalSize = 0;
  7889. FDragDropSshTerminate.reset(NULL);
  7890. }
  7891. //---------------------------------------------------------------------------
  7892. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7893. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7894. HRESULT & Result)
  7895. {
  7896. if (Result == DRAGDROP_S_DROP)
  7897. {
  7898. try
  7899. {
  7900. GlobalDragImageList->HideDragImage();
  7901. try
  7902. {
  7903. RemoteFileControlDDTargetDrop();
  7904. }
  7905. catch(ESshTerminate & E)
  7906. {
  7907. FDragDropSshTerminate.reset(E.Clone());
  7908. }
  7909. }
  7910. catch (Exception &E)
  7911. {
  7912. // If downloading fails we need to cancel drag&drop, otherwise
  7913. // Explorer shows error
  7914. // But by the way exception probably never reach this point as
  7915. // it's catched on way.
  7916. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7917. Result = DRAGDROP_S_CANCEL;
  7918. ShowExtendedException(Terminal, &E);
  7919. }
  7920. }
  7921. }
  7922. //---------------------------------------------------------------------------
  7923. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7924. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7925. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7926. {
  7927. TFileMasks::TParams MaskParams;
  7928. MaskParams.Size = Size;
  7929. MaskParams.Modification = Modification;
  7930. // this does not re-parse the mask if it is the same as the last time
  7931. FDirViewMatchMask = Masks;
  7932. bool ImplicitMatch;
  7933. Matches =
  7934. // RecurseInclude parameter has no effect as the Path is empty
  7935. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7936. (AllowImplicitMatches || !ImplicitMatch);
  7937. }
  7938. //---------------------------------------------------------------------------
  7939. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7940. TObject * Sender, TListItem * Item, WORD & Indexes)
  7941. {
  7942. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7943. UnicodeString Ext;
  7944. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7945. {
  7946. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7947. }
  7948. else
  7949. {
  7950. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7951. }
  7952. if (SameText(Ext, Configuration->PartialExt))
  7953. {
  7954. Indexes |= oiPartial;
  7955. }
  7956. }
  7957. //---------------------------------------------------------------------------
  7958. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7959. {
  7960. return
  7961. DirViewEnabled(osCurrent) &&
  7962. DirView(osCurrent)->CanPasteFromClipBoard();
  7963. }
  7964. //---------------------------------------------------------------------------
  7965. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7966. {
  7967. bool Result = false;
  7968. if (CanPasteToDirViewFromClipBoard())
  7969. {
  7970. Result = true;
  7971. }
  7972. else
  7973. {
  7974. UnicodeString ClipboardText;
  7975. if (NonEmptyTextFromClipboard(ClipboardText) &&
  7976. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  7977. {
  7978. if (StoredSessions->IsUrl(ClipboardText))
  7979. {
  7980. Result = true;
  7981. }
  7982. else
  7983. {
  7984. Result = DirViewEnabled(osCurrent);
  7985. }
  7986. }
  7987. }
  7988. return Result;
  7989. }
  7990. //---------------------------------------------------------------------------
  7991. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7992. {
  7993. if (DoesClipboardContainOurFiles())
  7994. {
  7995. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7996. {
  7997. DebugAssert(!IsLocalBrowserMode());
  7998. TTerminalManager * Manager = TTerminalManager::Instance();
  7999. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  8000. Manager->ActiveSession = FClipboardTerminal;
  8001. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  8002. }
  8003. }
  8004. else if (CanPasteToDirViewFromClipBoard())
  8005. {
  8006. DirView(osCurrent)->PasteFromClipBoard();
  8007. }
  8008. else
  8009. {
  8010. UnicodeString ClipboardText;
  8011. if (NonEmptyTextFromClipboard(ClipboardText))
  8012. {
  8013. if (StoredSessions->IsUrl(ClipboardText))
  8014. {
  8015. NewSession(ClipboardText);
  8016. }
  8017. else
  8018. {
  8019. DirView(osCurrent)->Path = ClipboardText;
  8020. }
  8021. }
  8022. }
  8023. }
  8024. //---------------------------------------------------------------------------
  8025. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8026. {
  8027. // TBD
  8028. }
  8029. //---------------------------------------------------------------------------
  8030. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8031. {
  8032. TCustomDirView * ADirView = DirView(Side);
  8033. ADirView->SelectAll(Mode);
  8034. ADirView->SetFocus();
  8035. }
  8036. //---------------------------------------------------------------------------
  8037. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8038. {
  8039. TCustomDirView * ADirView = DirView(Side);
  8040. TFileFilter Filter;
  8041. DefaultFileFilter(Filter);
  8042. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8043. {
  8044. Configuration->Usage->Inc(L"MaskSelections");
  8045. ADirView->SelectFiles(Filter, Select);
  8046. ADirView->SetFocus();
  8047. }
  8048. }
  8049. //---------------------------------------------------------------------------
  8050. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8051. {
  8052. TCustomDirView * ADirView = DirView(Side);
  8053. ADirView->RestoreSelectedNames();
  8054. ADirView->SetFocus();
  8055. }
  8056. //---------------------------------------------------------------------------
  8057. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8058. {
  8059. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8060. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8061. {
  8062. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8063. UnicodeString Ext;
  8064. if (!IsSideLocalBrowser(osCurrent))
  8065. {
  8066. Ext = UnixExtractFileExt(FileName);
  8067. }
  8068. else
  8069. {
  8070. Ext = ExtractFileExt(FileName);
  8071. }
  8072. if (Ext.IsEmpty())
  8073. {
  8074. Ext = L".";
  8075. }
  8076. TFileFilter Filter;
  8077. Filter.Masks = FORMAT(L"*%s", (Ext));
  8078. Filter.Directories = false;
  8079. CurrentDirView->SelectFiles(Filter, Select);
  8080. }
  8081. }
  8082. //---------------------------------------------------------------------------
  8083. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8084. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8085. {
  8086. UnicodeString Result;
  8087. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8088. {
  8089. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8090. }
  8091. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8092. {
  8093. // noop
  8094. }
  8095. else
  8096. {
  8097. Result =
  8098. FMTLOAD(FILE_INFO_FORMAT,
  8099. (FormatBytes(FileInfo.SelectedSize),
  8100. FormatBytes(FileInfo.FilesSize),
  8101. FormatNumber(FileInfo.SelectedCount),
  8102. FormatNumber(FileInfo.FilesCount)));
  8103. }
  8104. return Result;
  8105. }
  8106. //---------------------------------------------------------------------------
  8107. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8108. TTBXStatusPanel * Panel, TOperationSide Side)
  8109. {
  8110. if (Panel->Index == 1)
  8111. {
  8112. ToggleShowHiddenFiles();
  8113. }
  8114. else if (Panel->Index == 2)
  8115. {
  8116. Filter(Side);
  8117. }
  8118. }
  8119. //---------------------------------------------------------------------------
  8120. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8121. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8122. {
  8123. DebugAssert(!StatusBar->SimplePanel);
  8124. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8125. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8126. }
  8127. //---------------------------------------------------------------------------
  8128. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8129. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8130. {
  8131. DebugAssert(StatusBar->Panels->Count >= 3);
  8132. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8133. if (FileInfo.HiddenCount > 0)
  8134. {
  8135. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8136. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8137. }
  8138. else
  8139. {
  8140. HiddenFilesPanel->ViewPriority = 0;
  8141. // not really necessary, just to cleanup no-longer-valid data
  8142. HiddenFilesPanel->Caption = L"";
  8143. }
  8144. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8145. if (FileInfo.FilteredCount > 0)
  8146. {
  8147. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8148. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8149. }
  8150. else
  8151. {
  8152. FilteredFilesPanel->ViewPriority = 0;
  8153. // not really necessary, just to cleanup no-longer-valid data
  8154. FilteredFilesPanel->Caption = L"";
  8155. }
  8156. }
  8157. //---------------------------------------------------------------------------
  8158. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8159. TObject * /*Sender*/)
  8160. {
  8161. if (DirView(osOther)->Enabled)
  8162. {
  8163. DirView(osOther)->SetFocus();
  8164. }
  8165. }
  8166. //---------------------------------------------------------------------------
  8167. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8168. {
  8169. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8170. switch (Config.Show)
  8171. {
  8172. case qvShow:
  8173. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8174. {
  8175. Config.Show = qvHide;
  8176. }
  8177. else
  8178. {
  8179. Config.Show = Config.LastHideShow;
  8180. }
  8181. break;
  8182. case qvHideWhenEmpty:
  8183. if (ComponentVisible[fcQueueView])
  8184. {
  8185. Config.Show = qvHide;
  8186. }
  8187. else
  8188. {
  8189. Config.LastHideShow = Config.Show;
  8190. Config.Show = qvShow;
  8191. }
  8192. break;
  8193. case qvHide:
  8194. Config.LastHideShow = Config.Show;
  8195. Config.Show = qvShow;
  8196. break;
  8197. }
  8198. WinConfiguration->QueueView = Config;
  8199. }
  8200. //---------------------------------------------------------------------------
  8201. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8202. {
  8203. UnicodeString Result;
  8204. if (Terminal != NULL)
  8205. {
  8206. switch (WinConfiguration->PathInCaption)
  8207. {
  8208. case picShort:
  8209. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8210. break;
  8211. case picFull:
  8212. Result = Terminal->CurrentDirectory;
  8213. break;
  8214. }
  8215. }
  8216. return Result;
  8217. }
  8218. //---------------------------------------------------------------------------
  8219. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8220. {
  8221. TColor Result = GetWindowColor(FSessionColor);
  8222. return Result;
  8223. }
  8224. //---------------------------------------------------------------------------
  8225. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8226. {
  8227. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8228. return Result;
  8229. }
  8230. //---------------------------------------------------------------------------
  8231. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8232. {
  8233. if (!FSessionChanging)
  8234. {
  8235. TTerminalManager::Instance()->UpdateAppTitle();
  8236. }
  8237. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  8238. // background to gray on Windows 7 (works on Windows 10).
  8239. // See also EnableControl
  8240. if (Showing)
  8241. {
  8242. if (ManagedSession != NULL)
  8243. {
  8244. // Update path when it changes
  8245. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8246. {
  8247. UpdateSessionTab(SessionsPageControl->ActivePage);
  8248. }
  8249. }
  8250. NonVisualDataModule->ReconnectSessionAction->Update();
  8251. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8252. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8253. CenterReconnectToolbar();
  8254. bool HasTerminal = HasActiveTerminal();
  8255. RemoteDirView->Enabled = HasTerminal;
  8256. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8257. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8258. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8259. RemoteDriveView->Color = RemoteDirView->Color;
  8260. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8261. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8262. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8263. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8264. QueueFileList->Enabled = QueueView3->Enabled;
  8265. QueueFileList->Color = QueueView3->Color;
  8266. QueueFileList->Font->Color = QueueView3->Font->Color;
  8267. QueueLabelUpdateStatus();
  8268. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8269. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8270. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8271. {
  8272. UpdateDarkMode();
  8273. }
  8274. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8275. }
  8276. }
  8277. //---------------------------------------------------------------------------
  8278. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8279. {
  8280. UpdateControls();
  8281. }
  8282. //---------------------------------------------------------------------------
  8283. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8284. TObject * Sender)
  8285. {
  8286. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8287. DebugAssert(DirView != NULL);
  8288. DoDirViewLoaded(DirView);
  8289. TransferPresetAutoSelect();
  8290. }
  8291. //---------------------------------------------------------------------------
  8292. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8293. {
  8294. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8295. // first run after installation
  8296. if (double(Updates.LastCheck) == 0)
  8297. {
  8298. // make sure next time there will be an update (if enabled)
  8299. Updates.LastCheck = TDateTime(1);
  8300. WinConfiguration->Updates = Updates;
  8301. }
  8302. else if ((double(Updates.Period) > 0) &&
  8303. (Now() - Updates.LastCheck >= Updates.Period))
  8304. {
  8305. TThreadMethod OnUpdatesChecked = NULL;
  8306. if (!IsUWP())
  8307. {
  8308. OnUpdatesChecked = UpdatesChecked;
  8309. }
  8310. StartUpdateThread(OnUpdatesChecked);
  8311. }
  8312. }
  8313. //---------------------------------------------------------------------------
  8314. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8315. {
  8316. UnicodeString Message;
  8317. bool New;
  8318. TQueryType Type;
  8319. GetUpdatesMessage(Message, New, Type, false);
  8320. if (!Message.IsEmpty())
  8321. {
  8322. if (New)
  8323. {
  8324. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8325. }
  8326. if (!New && (Type != qtWarning))
  8327. {
  8328. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8329. }
  8330. else
  8331. {
  8332. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8333. }
  8334. if (New)
  8335. {
  8336. Configuration->Usage->Inc(L"UpdateNotifications");
  8337. }
  8338. }
  8339. }
  8340. //---------------------------------------------------------------------------
  8341. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8342. {
  8343. RestoreApp();
  8344. bool CanDisplay = !NonVisualDataModule->Busy;
  8345. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8346. {
  8347. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8348. }
  8349. if (CanDisplay)
  8350. {
  8351. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8352. CheckForUpdates(true);
  8353. }
  8354. else
  8355. {
  8356. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8357. }
  8358. }
  8359. //---------------------------------------------------------------------------
  8360. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8361. TCopyParamRuleData & Data)
  8362. {
  8363. DebugAssert(Terminal != NULL);
  8364. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8365. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8366. Data.RemoteDirectory = RemoteDirView->PathName;
  8367. }
  8368. //---------------------------------------------------------------------------
  8369. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8370. {
  8371. // Terminal can be null when we are changing local directory implicitly,
  8372. // because it has been deleted, while no session is connected
  8373. // (Login dialog is open)
  8374. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8375. {
  8376. DebugAssert(!IsLocalBrowserMode());
  8377. TCopyParamRuleData Data;
  8378. GetTransferPresetAutoSelectData(Data);
  8379. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8380. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8381. if (CopyParamIndex < 0)
  8382. {
  8383. // there is no preset that matches autoselection
  8384. // set preset that we consider "default"
  8385. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8386. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8387. }
  8388. else
  8389. {
  8390. // there is preset matching autoselection
  8391. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8392. if (CopyParamName == FCopyParamAutoSelected)
  8393. {
  8394. // autoselected the same preset as the last time
  8395. // make no change (i.e. preserve custom user preset, if any)
  8396. }
  8397. else
  8398. {
  8399. // autoselected the different preset then the last time (or there
  8400. // was default preset set)
  8401. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8402. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8403. }
  8404. }
  8405. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8406. {
  8407. if (CopyParamIndex >= 0)
  8408. {
  8409. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8410. }
  8411. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8412. try
  8413. {
  8414. int Fmt =
  8415. (CopyParamIndex < 0) ?
  8416. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8417. COPY_PARAM_AUTOSELECTED;
  8418. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8419. Data->Message = MainInstructions(Message);
  8420. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8421. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8422. CopyParamAttrs);
  8423. if (CopyParamIndex >= 0)
  8424. {
  8425. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8426. Info = FORMAT(L"%s\n \n%s", (Info,
  8427. FMTLOAD(COPY_PARAM_RULE,
  8428. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8429. }
  8430. Data->Message += L"\n\n" + Info;
  8431. if (WinConfiguration->CopyParamAutoSelectNotice)
  8432. {
  8433. TransferPresetNoteMessage(Data, true);
  8434. }
  8435. else
  8436. {
  8437. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8438. Data = NULL; // ownership passed
  8439. }
  8440. }
  8441. __finally
  8442. {
  8443. delete Data;
  8444. }
  8445. }
  8446. }
  8447. }
  8448. //---------------------------------------------------------------------------
  8449. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8450. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8451. {
  8452. DebugAssert(NoteData != NULL);
  8453. DebugAssert(!IsLocalBrowserMode());
  8454. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8455. TQueryButtonAlias Aliases[1];
  8456. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8457. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8458. Params.Aliases = Aliases;
  8459. Params.AliasesCount = LENOF(Aliases);
  8460. unsigned int Result =
  8461. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8462. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8463. switch (Result)
  8464. {
  8465. case qaNeverAskAgain:
  8466. DebugAssert(AllowNeverAskAgain);
  8467. WinConfiguration->CopyParamAutoSelectNotice = false;
  8468. break;
  8469. case qaIgnore:
  8470. PreferencesDialog(pmPresets);
  8471. break;
  8472. }
  8473. }
  8474. //---------------------------------------------------------------------------
  8475. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8476. {
  8477. // as of now useless, as this is used for notes only, never for balloons, ...
  8478. RestoreApp();
  8479. // .. and we should never be busy here
  8480. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8481. {
  8482. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8483. }
  8484. }
  8485. //---------------------------------------------------------------------------
  8486. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8487. TPreferencesMode APreferencesMode)
  8488. {
  8489. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8490. TCopyParamRuleData Data;
  8491. if (Terminal != NULL)
  8492. {
  8493. PreferencesData.reset(new TPreferencesDialogData());
  8494. GetTransferPresetAutoSelectData(Data);
  8495. PreferencesData->CopyParamRuleData = &Data;
  8496. }
  8497. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8498. }
  8499. //---------------------------------------------------------------------------
  8500. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8501. const TCustomCommandType & Command)
  8502. {
  8503. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8504. {
  8505. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8506. }
  8507. }
  8508. //---------------------------------------------------------------------------
  8509. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8510. {
  8511. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8512. TCustomCommandType Command;
  8513. // make sure we use local custom command when remote are not supported
  8514. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8515. {
  8516. Command = FLastCustomCommand;
  8517. }
  8518. else
  8519. {
  8520. Command.Params = Command.Params | ccLocal;
  8521. }
  8522. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8523. FEditingFocusedAdHocCommand = OnFocused;
  8524. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8525. int Options =
  8526. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8527. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8528. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8529. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8530. {
  8531. FLastCustomCommand = Command;
  8532. UpdateCustomCommandsToolbar();
  8533. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8534. }
  8535. }
  8536. //---------------------------------------------------------------------------
  8537. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8538. {
  8539. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8540. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8541. DebugAssert(State > 0);
  8542. if (State <= 0)
  8543. {
  8544. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8545. }
  8546. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8547. }
  8548. //---------------------------------------------------------------------------
  8549. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8550. TCustomCommandType & Command, int & State)
  8551. {
  8552. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8553. if (Result)
  8554. {
  8555. Command = FLastCustomCommand;
  8556. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8557. }
  8558. return Result;
  8559. }
  8560. //---------------------------------------------------------------------------
  8561. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8562. {
  8563. ResetIncrementalSearch();
  8564. }
  8565. //---------------------------------------------------------------------------
  8566. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8567. {
  8568. DebugAssert(ComponentVisible[Component]);
  8569. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8570. }
  8571. //---------------------------------------------------------------------------
  8572. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8573. {
  8574. // when queue panel is resized here directly, the status bar is stretched
  8575. // over whole space the panel occupied
  8576. PostComponentHide(fcQueueView);
  8577. }
  8578. //---------------------------------------------------------------------------
  8579. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8580. {
  8581. ComponentVisible[fcQueueFileList] = false;
  8582. }
  8583. //---------------------------------------------------------------------------
  8584. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8585. {
  8586. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8587. RefreshColorMode();
  8588. ResetSysDarkTheme();
  8589. ConfigurationChanged();
  8590. ConfigureInterface();
  8591. // Should be called for all controls
  8592. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8593. }
  8594. //---------------------------------------------------------------------------
  8595. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8596. {
  8597. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8598. if (!FInvalid &&
  8599. (Message.LParam != 0) &&
  8600. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8601. {
  8602. ThemeChanged();
  8603. }
  8604. TForm::Dispatch(&Message);
  8605. }
  8606. //---------------------------------------------------------------------------
  8607. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8608. {
  8609. TMessage * M = static_cast<TMessage*>(Message);
  8610. switch (M->Msg)
  8611. {
  8612. case CM_DIALOGCHAR:
  8613. CMDialogChar(*M);
  8614. break;
  8615. case WM_APPCOMMAND:
  8616. WMAppCommand(*M);
  8617. break;
  8618. case WM_SYSCOMMAND:
  8619. WMSysCommand(*M);
  8620. break;
  8621. case WM_QUERYENDSESSION:
  8622. WMQueryEndSession(*M);
  8623. break;
  8624. case WM_ENDSESSION:
  8625. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8626. break;
  8627. #ifdef _DEBUG
  8628. case WM_WINDOWPOSCHANGED:
  8629. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8630. break;
  8631. case CM_CUSTOMSTYLECHANGED:
  8632. TForm::Dispatch(Message);
  8633. break;
  8634. #endif
  8635. case WM_COMPONENT_HIDE:
  8636. {
  8637. Byte Component = static_cast<Byte>(M->WParam);
  8638. // sanity check
  8639. if (ComponentVisible[Component])
  8640. {
  8641. // special treatment
  8642. if (Component == fcQueueView)
  8643. {
  8644. ToggleQueueVisibility();
  8645. DebugAssert(!ComponentVisible[fcQueueView]);
  8646. }
  8647. else
  8648. {
  8649. ComponentVisible[Component] = false;
  8650. }
  8651. }
  8652. }
  8653. break;
  8654. case WM_COPYDATA:
  8655. WMCopyData(*M);
  8656. break;
  8657. case WM_MANAGES_CAPTION:
  8658. // caption managed in TTerminalManager::UpdateAppTitle()
  8659. M->Result = 1;
  8660. break;
  8661. case WM_WANTS_MOUSEWHEEL:
  8662. M->Result = 1;
  8663. break;
  8664. case CM_SHOWINGCHANGED:
  8665. CMShowingChanged(*M);
  8666. break;
  8667. case WM_CLOSE:
  8668. WMClose(*M);
  8669. break;
  8670. case WM_DPICHANGED:
  8671. WMDpiChanged(*M);
  8672. break;
  8673. case CM_DPICHANGED:
  8674. CMDpiChanged(*M);
  8675. break;
  8676. case WM_SETTINGCHANGE:
  8677. WMSettingChange(*M);
  8678. break;
  8679. case CM_DIALOGKEY:
  8680. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8681. break;
  8682. default:
  8683. TForm::Dispatch(Message);
  8684. break;
  8685. }
  8686. }
  8687. //---------------------------------------------------------------------------
  8688. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8689. {
  8690. // noop
  8691. }
  8692. //---------------------------------------------------------------------------
  8693. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8694. {
  8695. TForm::Dispatch(&Message);
  8696. FSessionColors->Clear();
  8697. AddFixedSessionImages();
  8698. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8699. UpdateImages();
  8700. }
  8701. //---------------------------------------------------------------------------
  8702. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8703. {
  8704. TForm::Dispatch(&Message);
  8705. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8706. UpdatePixelsPerInchMainWindowCounter();
  8707. }
  8708. //---------------------------------------------------------------------------
  8709. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8710. {
  8711. // Cannot close window while we are busy.
  8712. // We cannot test this in FormCloseQuery as that is called also from
  8713. // Close(), which is called by CloseApplicationAction. So we can be busy
  8714. // there already even, when it is legitimate to close the application.
  8715. // Possibly a better place to handle this would be WMSysCommand.
  8716. if (NonVisualDataModule->Busy)
  8717. {
  8718. Message.Result = 1;
  8719. }
  8720. else
  8721. {
  8722. TForm::Dispatch(&Message);
  8723. }
  8724. }
  8725. //---------------------------------------------------------------------------
  8726. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8727. {
  8728. // Ignoring implicit local-local browser of Commander, opened from DoShow
  8729. bool NoSession = (ManagedSession == NULL);
  8730. // This calls TCustomScpExplorerForm::DoShow
  8731. TForm::Dispatch(&Message);
  8732. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  8733. if (Showing && !FStarted)
  8734. {
  8735. FStarted = true;
  8736. InterfaceStarted();
  8737. }
  8738. if (Showing && NoSession)
  8739. {
  8740. // When we are starting minimized (i.e. from an installer),
  8741. // postpone showing Login dialog until we get restored.
  8742. // Otherwise the Login dialog (and Authentication window) show restored
  8743. // over invisible (minimized) main window.
  8744. if (WindowState == wsMinimized)
  8745. {
  8746. FNeedSession = true;
  8747. }
  8748. else
  8749. {
  8750. // This happens before application ever goes idle, so the toolbars would
  8751. // stay enabled (initial state) until the Login dialog is dismissed.
  8752. UpdateActions();
  8753. NonVisualDataModule->StartBusy();
  8754. try
  8755. {
  8756. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8757. // otherwise the dialog does not receive focus.
  8758. // With Commander interface the ProcessMessages is called already
  8759. // by TDriveView, but with Explorer interface, we need to call it explicily
  8760. Application->ProcessMessages();
  8761. // do not reload sessions, they have been loaded just now (optimization)
  8762. NeedSession(true);
  8763. }
  8764. __finally
  8765. {
  8766. NonVisualDataModule->EndBusy();
  8767. }
  8768. }
  8769. }
  8770. }
  8771. //---------------------------------------------------------------------------
  8772. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8773. {
  8774. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8775. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8776. }
  8777. //---------------------------------------------------------------------------
  8778. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8779. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8780. int & MaxHeight)
  8781. {
  8782. // workaround for bug in TWinControl.CalcConstraints
  8783. // Check for empty rect (restore from iconic state) is done there only after
  8784. // call to AdjustClientRect, which enlarges the rect (for forms).
  8785. TRect R = GetClientRect();
  8786. // when restoring from iconic state, place no restrictions
  8787. if (IsRectEmpty(R))
  8788. {
  8789. MinWidth = 0;
  8790. MinHeight = 0;
  8791. MaxWidth = 32000;
  8792. MaxHeight = 32000;
  8793. }
  8794. }
  8795. //---------------------------------------------------------------------------
  8796. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8797. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8798. {
  8799. // terminal can be already closed (e.g. dropped connection)
  8800. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8801. {
  8802. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8803. try
  8804. {
  8805. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8806. }
  8807. catch(...)
  8808. {
  8809. if (!Terminal->Active)
  8810. {
  8811. Close = true;
  8812. }
  8813. throw;
  8814. }
  8815. }
  8816. }
  8817. //---------------------------------------------------------------------------
  8818. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8819. {
  8820. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8821. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8822. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8823. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8824. {
  8825. OnGetSpaceAvailable = GetSpaceAvailable;
  8826. }
  8827. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8828. OnGetSpaceAvailable);
  8829. }
  8830. //---------------------------------------------------------------------------
  8831. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8832. {
  8833. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8834. Terminal->FillSessionDataForCode(Data.get());
  8835. return Data.release();
  8836. }
  8837. //---------------------------------------------------------------------------
  8838. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8839. {
  8840. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8841. DoGenerateUrlDialog(Data.get(), Paths);
  8842. }
  8843. //---------------------------------------------------------------------------
  8844. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8845. {
  8846. GenerateUrl(NULL);
  8847. }
  8848. //---------------------------------------------------------------------------
  8849. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8850. {
  8851. std::unique_ptr<TStrings> Paths(new TStringList());
  8852. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8853. GenerateUrl(Paths.get());
  8854. }
  8855. //---------------------------------------------------------------------------
  8856. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8857. {
  8858. FSessionColor = value;
  8859. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8860. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8861. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8862. DebugAssert(ColorItem != NULL);
  8863. ColorItem->Color = C;
  8864. NonVisualDataModule->ColorMenuItem->Color = C;
  8865. UpdateControls();
  8866. }
  8867. //---------------------------------------------------------------------------
  8868. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8869. {
  8870. if (value != FSessionColor)
  8871. {
  8872. UpdateSessionColor(value);
  8873. // Is null when called from LastTerminalClosed
  8874. if (Terminal != NULL)
  8875. {
  8876. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8877. }
  8878. }
  8879. }
  8880. //---------------------------------------------------------------------------
  8881. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8882. {
  8883. bool Result = FNoteTimer->Enabled;
  8884. if (Result)
  8885. {
  8886. // cannot cancel note too early
  8887. bool NotEarly =
  8888. (Now() - FNoteShown >
  8889. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8890. if (Force || NotEarly)
  8891. {
  8892. FNoteTimer->Enabled = false;
  8893. FNote = L"";
  8894. SAFE_DESTROY(FNoteData);
  8895. FOnNoteClick = NULL;
  8896. FNoteHints = FNotes->Text;
  8897. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8898. UpdateStatusBar();
  8899. }
  8900. }
  8901. return Result;
  8902. }
  8903. //---------------------------------------------------------------------------
  8904. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8905. {
  8906. DebugAssert(FNoteTimer->Enabled);
  8907. CancelNote(true);
  8908. }
  8909. //---------------------------------------------------------------------------
  8910. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  8911. {
  8912. FNoteHints = FNotes->Text.TrimRight();
  8913. }
  8914. //---------------------------------------------------------------------------
  8915. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8916. {
  8917. int P = Note.Pos(L"\n");
  8918. if (P > 0)
  8919. {
  8920. Note.SetLength(P - 1);
  8921. }
  8922. FNotes->Add(FORMAT(L"[%s] %s",
  8923. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8924. while (FNotes->Count > 10)
  8925. {
  8926. FNotes->Delete(0);
  8927. }
  8928. if (UpdateNow)
  8929. {
  8930. UpdateNoteHints();
  8931. UpdateStatusBar();
  8932. }
  8933. }
  8934. //---------------------------------------------------------------------------
  8935. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8936. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8937. {
  8938. int P = Note.Pos(L"\n");
  8939. if (P > 0)
  8940. {
  8941. Note.SetLength(P - 1);
  8942. }
  8943. UpdateNoteHints();
  8944. FNote = Note;
  8945. SAFE_DESTROY(FNoteData);
  8946. FNoteData = NoteData;
  8947. FOnNoteClick = OnNoteClick;
  8948. AddNote(Note, false);
  8949. UpdateStatusBar();
  8950. FNoteShown = Now();
  8951. FNoteTimer->Enabled = false;
  8952. if (Seconds == 0)
  8953. {
  8954. Seconds = WinConfiguration->NotificationsTimeout;
  8955. }
  8956. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8957. FNoteTimer->Enabled = true;
  8958. }
  8959. //---------------------------------------------------------------------------
  8960. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8961. {
  8962. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8963. }
  8964. //---------------------------------------------------------------------------
  8965. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8966. {
  8967. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  8968. {
  8969. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8970. }
  8971. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8972. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8973. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  8974. 0, NULL, NULL);
  8975. }
  8976. //---------------------------------------------------------------------------
  8977. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8978. {
  8979. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8980. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8981. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8982. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  8983. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8984. }
  8985. //---------------------------------------------------------------------------
  8986. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8987. {
  8988. WinConfiguration->FormatSizeBytes = Style;
  8989. }
  8990. //---------------------------------------------------------------------------
  8991. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8992. {
  8993. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8994. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8995. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8996. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8997. }
  8998. //---------------------------------------------------------------------------
  8999. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9000. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9001. {
  9002. if (Panel->Index == 0)
  9003. {
  9004. if (FOnNoteClick != NULL)
  9005. {
  9006. // prevent the user data from being freed by possible call
  9007. // to CancelNote or PostNote during call to OnNoteClick
  9008. std::unique_ptr<TObject> NoteData(FNoteData);
  9009. TNotifyEvent OnNoteClick = FOnNoteClick;
  9010. FNoteData = NULL;
  9011. // need to cancel the note as we are going to delete its user data
  9012. CancelNote(true);
  9013. OnNoteClick(NoteData.get());
  9014. }
  9015. }
  9016. if (HasActiveTerminal() &&
  9017. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9018. {
  9019. FileSystemInfo();
  9020. }
  9021. }
  9022. //---------------------------------------------------------------------------
  9023. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9024. {
  9025. // workaround:
  9026. // 1) for unknown reason, disabling window, while minimized,
  9027. // prevents it from restoring, even if it was enabled again meanwhile
  9028. // 2) when disabling the main window, while another has focus
  9029. // minimize is no longer possible
  9030. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9031. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9032. // Shouldn't we use IsApplicationMinimized() here?
  9033. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9034. {
  9035. Enabled = false;
  9036. }
  9037. FLockLevel++;
  9038. }
  9039. //---------------------------------------------------------------------------
  9040. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9041. {
  9042. DebugAssert(FLockLevel > 0);
  9043. FLockLevel--;
  9044. if (FLockLevel == 0)
  9045. {
  9046. DebugAssert(FLockSuspendLevel == 0);
  9047. Enabled = true;
  9048. }
  9049. }
  9050. //---------------------------------------------------------------------------
  9051. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9052. {
  9053. // We need to make sure that window is enabled when the last modal window closes
  9054. // otherwise focus is not restored correctly.
  9055. // So we re-enable the window when modal window opens and
  9056. // disable it back after is closes
  9057. if (FLockLevel > 0)
  9058. {
  9059. // while we have nesting counter, we know that we never be called
  9060. // recursivelly as Application->OnModalBegin is called only
  9061. // for the top-level modal window
  9062. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9063. {
  9064. // won't be disabled when conditions in LockWindow() were not satisfied
  9065. FDisabledOnLockSuspend = !Enabled;
  9066. // When minimized to tray (or actually when set to SW_HIDE),
  9067. // setting Enabled makes the window focusable even when there's
  9068. // modal window over it
  9069. if (!FTrayIcon->Visible)
  9070. {
  9071. Enabled = true;
  9072. }
  9073. }
  9074. FLockSuspendLevel++;
  9075. }
  9076. }
  9077. //---------------------------------------------------------------------------
  9078. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9079. {
  9080. if (FLockSuspendLevel > 0)
  9081. {
  9082. DebugAssert(FLockLevel > 0);
  9083. FLockSuspendLevel--;
  9084. // see comment in SuspendWindowLock
  9085. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9086. {
  9087. // Note that window can be enabled here, when we were minized to tray when
  9088. // was SuspendWindowLock() called.
  9089. // We should possibly do the same check as in LockWindow(),
  9090. // if it is ever possible that the consitions change between
  9091. // SuspendWindowLock() and ResumeWindowLock()
  9092. Enabled = !FDisabledOnLockSuspend;
  9093. }
  9094. }
  9095. }
  9096. //---------------------------------------------------------------------------
  9097. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9098. {
  9099. TTBXComboBoxItem * RemotePathComboBox =
  9100. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9101. if (!TextOnly)
  9102. {
  9103. TStrings * Items = RemotePathComboBox->Strings;
  9104. Items->BeginUpdate();
  9105. try
  9106. {
  9107. Items->Clear();
  9108. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9109. {
  9110. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9111. while (!IsUnixRootPath(APath))
  9112. {
  9113. int P = APath.LastDelimiter(L'/');
  9114. DebugAssert(P >= 0);
  9115. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9116. APath.SetLength(P - 1);
  9117. }
  9118. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9119. }
  9120. }
  9121. __finally
  9122. {
  9123. Items->EndUpdate();
  9124. }
  9125. }
  9126. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9127. // Setting ItemIndex to -1 does not reset its text
  9128. if (RemotePathComboBox->Strings->Count == 0)
  9129. {
  9130. RemotePathComboBox->Text = L"";
  9131. }
  9132. }
  9133. //---------------------------------------------------------------------------
  9134. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9135. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9136. {
  9137. if (AIndex < 0)
  9138. {
  9139. AIndex = Sender->ItemIndex;
  9140. }
  9141. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9142. }
  9143. //---------------------------------------------------------------------------
  9144. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9145. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9146. int & ImageIndex)
  9147. {
  9148. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9149. }
  9150. //---------------------------------------------------------------------------
  9151. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9152. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9153. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9154. {
  9155. if (!IsLocalBrowserMode())
  9156. {
  9157. ARect.Left += (10 * AIndex);
  9158. }
  9159. }
  9160. //---------------------------------------------------------------------------
  9161. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9162. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9163. {
  9164. AWidth += (10 * AIndex);
  9165. }
  9166. //---------------------------------------------------------------------------
  9167. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9168. {
  9169. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9170. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9171. int Index = RemotePathComboBox->ItemIndex;
  9172. while (Index < RemotePathComboBox->Strings->Count - 1)
  9173. {
  9174. APath = UnixExtractFileDir(APath);
  9175. Index++;
  9176. }
  9177. // VanDyke style paths
  9178. if (APath.IsEmpty())
  9179. {
  9180. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9181. APath = ROOTDIRECTORY;
  9182. }
  9183. if (RemoteDirView->Path != APath)
  9184. {
  9185. RemoteDirView->Path = APath;
  9186. }
  9187. }
  9188. //---------------------------------------------------------------------------
  9189. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9190. {
  9191. DoRemotePathComboBoxItemClick(Sender);
  9192. }
  9193. //---------------------------------------------------------------------------
  9194. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9195. {
  9196. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9197. DebugUsedParam(Sender);
  9198. UpdateRemotePathComboBox(true);
  9199. }
  9200. //---------------------------------------------------------------------------
  9201. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9202. {
  9203. DoRemotePathComboBoxCancel(Sender);
  9204. }
  9205. //---------------------------------------------------------------------------
  9206. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9207. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9208. {
  9209. ResetIncrementalSearch();
  9210. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9211. DebugAssert(DirView != NULL);
  9212. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9213. {
  9214. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9215. // OnEditing is called also from TCustomListView::CanEdit
  9216. if (Edit != NULL)
  9217. {
  9218. EditSelectBaseName(Edit);
  9219. }
  9220. }
  9221. }
  9222. //---------------------------------------------------------------------------
  9223. //---------------------------------------------------------------------------
  9224. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9225. {
  9226. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9227. Result->AutoDetectDnD = false;
  9228. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9229. Result->RenderDataOn = rdoEnterAndDropSync;
  9230. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9231. return Result;
  9232. }
  9233. //---------------------------------------------------------------------------
  9234. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9235. {
  9236. if (IsWin10Build(2004))
  9237. {
  9238. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9239. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9240. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9241. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9242. }
  9243. }
  9244. //---------------------------------------------------------------------------
  9245. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9246. {
  9247. TForm::CreateWnd();
  9248. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9249. UpdateDarkMode();
  9250. if (FSessionsDragDropFilesEx == NULL)
  9251. {
  9252. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9253. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9254. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9255. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9256. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9257. }
  9258. if (FQueueDragDropFilesEx == NULL)
  9259. {
  9260. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9261. // No need to set OnDragOver as we do not have any restrictions
  9262. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9263. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9264. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9265. }
  9266. }
  9267. //---------------------------------------------------------------------------
  9268. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9269. {
  9270. TForm::DestroyWnd();
  9271. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9272. FQueueDragDropFilesEx->DragDropControl = NULL;
  9273. }
  9274. //---------------------------------------------------------------------------
  9275. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9276. {
  9277. SideEnter(FCurrentSide);
  9278. FEverShown = true;
  9279. }
  9280. //---------------------------------------------------------------------------
  9281. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9282. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9283. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9284. {
  9285. if (!NonVisualDataModule->Busy)
  9286. {
  9287. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9288. Configuration->Usage->Inc(L"FileFinds");
  9289. LockWindow(true);
  9290. NonVisualDataModule->StartBusy();
  9291. WinConfiguration->LockedInterface = true;
  9292. try
  9293. {
  9294. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9295. }
  9296. __finally
  9297. {
  9298. WinConfiguration->LockedInterface = false;
  9299. NonVisualDataModule->EndBusy();
  9300. UnlockWindow();
  9301. }
  9302. }
  9303. }
  9304. //---------------------------------------------------------------------------
  9305. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9306. {
  9307. if (!NonVisualDataModule->Busy)
  9308. {
  9309. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9310. SetFocus();
  9311. RemoteDirView->Path = UnixExtractFilePath(Path);
  9312. UnicodeString FileName = UnixExtractFileName(Path);
  9313. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9314. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9315. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9316. if (Item == NULL)
  9317. {
  9318. ReloadDirectory(osRemote);
  9319. Item = RemoteDirView->FindFileItem(FileName);
  9320. }
  9321. if (Item != NULL)
  9322. {
  9323. RemoteDirView->ItemFocused = Item;
  9324. RemoteDirView->ItemFocused->MakeVisible(false);
  9325. RemoteDirView->SetFocus();
  9326. }
  9327. }
  9328. }
  9329. //---------------------------------------------------------------------------
  9330. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9331. {
  9332. bool Result = !NonVisualDataModule->Busy;
  9333. if (Result)
  9334. {
  9335. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9336. }
  9337. return Result;
  9338. }
  9339. //---------------------------------------------------------------------------
  9340. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9341. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9342. {
  9343. if (CanOperateOnFoundFiles(ATerminal))
  9344. {
  9345. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9346. FOnFileOperationFinished = OnFileOperationFinished;
  9347. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9348. }
  9349. }
  9350. //---------------------------------------------------------------------------
  9351. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9352. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9353. {
  9354. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9355. }
  9356. //---------------------------------------------------------------------------
  9357. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9358. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9359. {
  9360. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9361. }
  9362. //---------------------------------------------------------------------------
  9363. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9364. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9365. {
  9366. if (CanOperateOnFoundFiles(ATerminal))
  9367. {
  9368. for (int Index = 0; Index < FileList->Count; Index++)
  9369. {
  9370. UnicodeString FileName = FileList->Strings[Index];
  9371. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9372. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9373. OnFileOperationFinished(FileName, true);
  9374. }
  9375. }
  9376. }
  9377. //---------------------------------------------------------------------------
  9378. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9379. {
  9380. FFileFindTerminal = Terminal;
  9381. ShowFileFindDialog(
  9382. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9383. DoDownloadFoundFiles, DoEditFoundFiles);
  9384. }
  9385. //---------------------------------------------------------------------------
  9386. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9387. {
  9388. FTaskbarList = TaskbarList;
  9389. }
  9390. //---------------------------------------------------------------------------
  9391. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9392. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9393. {
  9394. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9395. if (Index >= 0)
  9396. {
  9397. if (Button == mbLeft)
  9398. {
  9399. // "Mouse down" is raised only after tab is switched.
  9400. // If switching tab (switching session) takes long enough for user
  9401. // to actually release the button, "mouse down" is still raised,
  9402. // but we do not get "mouse up" event, so dragging is not cancelled,
  9403. // prevent that by not beginning dragging in the first place.
  9404. if (IsKeyPressed(VK_LBUTTON))
  9405. {
  9406. // when user clicks the "+", we get mouse down only after the session
  9407. // is closed, when new session tab is already on X:Y, so dragging
  9408. // starts, prevent that
  9409. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  9410. {
  9411. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9412. if (Terminal != NULL)
  9413. {
  9414. SessionsPageControl->BeginDrag(false);
  9415. }
  9416. }
  9417. }
  9418. }
  9419. else if (Button == mbMiddle)
  9420. {
  9421. // ignore middle-click for "New session tab"
  9422. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9423. if (Terminal != NULL)
  9424. {
  9425. CloseSessionTab(Index);
  9426. }
  9427. }
  9428. }
  9429. }
  9430. //---------------------------------------------------------------------------
  9431. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9432. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9433. {
  9434. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9435. // do not allow dropping on the "+" tab
  9436. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9437. if ((TargetTerminal != NULL) &&
  9438. (SessionsPageControl->ActivePage->PageIndex != Index))
  9439. {
  9440. Configuration->Usage->Inc(L"SessionTabMoves");
  9441. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9442. // but we want to actually prevent that to avoid flicker
  9443. SessionsPageControl->ActivePage->PageIndex = Index;
  9444. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9445. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9446. }
  9447. }
  9448. //---------------------------------------------------------------------------
  9449. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9450. TObject * Sender, TObject * Source, int X, int Y,
  9451. TDragState /*State*/, bool & Accept)
  9452. {
  9453. Accept = (Sender == Source);
  9454. if (Accept)
  9455. {
  9456. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9457. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9458. // do not allow dragging to the "+" tab
  9459. Accept = (Terminal != NULL);
  9460. }
  9461. }
  9462. //---------------------------------------------------------------------------
  9463. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9464. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9465. {
  9466. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9467. if (Index < 0)
  9468. {
  9469. Effect = DROPEFFECT_NONE;
  9470. }
  9471. else
  9472. {
  9473. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9474. // do not allow dropping on the "+" tab or on local-local tabs
  9475. if ((TargetSession == NULL) || TargetSession->LocalBrowser)
  9476. {
  9477. Effect = DROPEFFECT_NONE;
  9478. }
  9479. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9480. {
  9481. Effect = DROPEFFECT_COPY;
  9482. }
  9483. }
  9484. }
  9485. //---------------------------------------------------------------------------
  9486. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9487. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9488. {
  9489. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9490. // do not allow dropping on the "+" tab
  9491. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9492. if (TargetTerminal != NULL)
  9493. {
  9494. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9495. if (!IsFileControl(DropSourceControl, osRemote))
  9496. {
  9497. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9498. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9499. // Why don't we use Terminal->CurrentDirectory directly?
  9500. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9501. // do not force queue, drag drop
  9502. false, true);
  9503. }
  9504. }
  9505. }
  9506. //---------------------------------------------------------------------------
  9507. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9508. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9509. {
  9510. // Downloads are handled in RemoteFileControlDDEnd
  9511. if (!IsFileControl(DropSourceControl, osRemote))
  9512. {
  9513. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9514. Terminal->CurrentDirectory,
  9515. // force queue, drag drop
  9516. true, true);
  9517. }
  9518. }
  9519. //---------------------------------------------------------------------------
  9520. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9521. {
  9522. FShowing = false;
  9523. // Do not save empty workspace
  9524. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9525. {
  9526. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9527. UnicodeString Name = WorkspaceName();
  9528. DoSaveWorkspace(
  9529. Name, DataList.get(),
  9530. !Configuration->DisablePasswordStoring &&
  9531. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9532. WinConfiguration->LastStoredSession = Name;
  9533. }
  9534. }
  9535. //---------------------------------------------------------------------------
  9536. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9537. {
  9538. TManagedTerminal * ManagedTerminal =
  9539. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9540. if (ManagedTerminal != NULL)
  9541. {
  9542. ManagedTerminal->DirectoryLoaded = Now();
  9543. }
  9544. }
  9545. //---------------------------------------------------------------------------
  9546. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9547. TListItem * /*Item*/, bool /*Selected*/)
  9548. {
  9549. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9550. switch (DirView->LastSelectMethod)
  9551. {
  9552. case smKeyboard:
  9553. Configuration->Usage->Inc(L"KeyboardSelections");
  9554. break;
  9555. case smMouse:
  9556. Configuration->Usage->Inc(L"MouseSelections");
  9557. break;
  9558. }
  9559. }
  9560. //---------------------------------------------------------------------------
  9561. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9562. {
  9563. if (Color != 0)
  9564. {
  9565. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9566. }
  9567. else
  9568. {
  9569. return FSessionTabImageIndex;
  9570. }
  9571. }
  9572. //---------------------------------------------------------------------------
  9573. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9574. {
  9575. TPngImageCollectionItem * Item =
  9576. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9577. FSessionColors->AddPng(Item->PngImage);
  9578. return FSessionColors->Count - 1;
  9579. }
  9580. //---------------------------------------------------------------------------
  9581. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9582. {
  9583. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9584. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9585. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9586. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9587. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  9588. }
  9589. //---------------------------------------------------------------------------
  9590. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9591. {
  9592. for (int Index = 0; Index < Control->ControlCount; Index++)
  9593. {
  9594. TControl * AControl = Control->Controls[Index];
  9595. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9596. if (WinControl != NULL)
  9597. {
  9598. CollectItemsWithTextDisplayMode(WinControl);
  9599. }
  9600. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9601. if (Toolbar != NULL)
  9602. {
  9603. // we care for top-level items only
  9604. TTBCustomItem * Items = Toolbar->Items;
  9605. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9606. {
  9607. TTBCustomItem * Item = Items->Items[ItemIndex];
  9608. if (((Item->DisplayMode == nbdmImageAndText) ||
  9609. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9610. EligibleForImageDisplayMode(Item))
  9611. {
  9612. FItemsWithTextDisplayMode.insert(Item);
  9613. }
  9614. }
  9615. }
  9616. }
  9617. }
  9618. //---------------------------------------------------------------------------
  9619. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9620. {
  9621. return true;
  9622. }
  9623. //---------------------------------------------------------------------------
  9624. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9625. {
  9626. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9627. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9628. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9629. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9630. bool Result = true;
  9631. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9632. {
  9633. TTBCustomItem * Item = *i;
  9634. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9635. if (Label != NULL)
  9636. {
  9637. // optimization
  9638. if (Label->Visible == SelectiveToolbarText)
  9639. {
  9640. Result = false;
  9641. }
  9642. else
  9643. {
  9644. Label->Visible = SelectiveToolbarText;
  9645. }
  9646. }
  9647. else
  9648. {
  9649. // optimization
  9650. if (Item->DisplayMode == DisplayMode)
  9651. {
  9652. Result = false;
  9653. }
  9654. else
  9655. {
  9656. Item->DisplayMode = DisplayMode;
  9657. }
  9658. }
  9659. ++i;
  9660. }
  9661. if (Result)
  9662. {
  9663. UpdateNewSessionTab();
  9664. }
  9665. return Result;
  9666. }
  9667. //---------------------------------------------------------------------------
  9668. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9669. {
  9670. DebugFail();
  9671. }
  9672. //---------------------------------------------------------------------------
  9673. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9674. {
  9675. // Among other, a lock level is non zero, while directory is being loaded,
  9676. // even when it happens as a result of dirview navigation,
  9677. // i.e. when TNonVisualDataModule is NOT FBusy.
  9678. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9679. // Among other this prevents a panel auto update to occur while
  9680. // directory is changing.
  9681. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9682. }
  9683. //---------------------------------------------------------------------------
  9684. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  9685. {
  9686. // While the window is disabled, we still seem to process menu shortcuts at least,
  9687. // so stop it at least here.
  9688. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9689. bool Result;
  9690. if (Allowed == aaUpdate)
  9691. {
  9692. Result = true;
  9693. }
  9694. else
  9695. {
  9696. Result = !NonVisualDataModule->Busy;
  9697. if (Result && (Allowed == aaShortCut))
  9698. {
  9699. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  9700. Action->Update();
  9701. Result = Action->Enabled;
  9702. }
  9703. }
  9704. return Result;
  9705. }
  9706. //---------------------------------------------------------------------------
  9707. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9708. {
  9709. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9710. }
  9711. //---------------------------------------------------------------------------
  9712. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9713. {
  9714. // This is somewhat redundant to LockWindow() call from
  9715. // TTerminalManager::TerminalReadDirectoryProgress.
  9716. // But disabling window is known not to block keyboard shorcuts
  9717. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9718. if (Busy > 0)
  9719. {
  9720. if (NonVisualDataModule->Busy)
  9721. {
  9722. State = false;
  9723. }
  9724. else
  9725. {
  9726. NonVisualDataModule->StartBusy();
  9727. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9728. LockWindow();
  9729. }
  9730. }
  9731. else if (Busy < 0)
  9732. {
  9733. UnlockWindow();
  9734. NonVisualDataModule->EndBusy();
  9735. }
  9736. else
  9737. {
  9738. State = NonVisualDataModule->Busy;
  9739. }
  9740. }
  9741. //---------------------------------------------------------------------------
  9742. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9743. {
  9744. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9745. // no context menu for "New session tab"
  9746. if ((Index >= 0) && (GetSessionTabSession(SessionsPageControl->Pages[Index]) != NULL))
  9747. {
  9748. SessionsPageControl->ActivePageIndex = Index;
  9749. if (DebugAlwaysTrue(SessionTabSwitched()))
  9750. {
  9751. // copied from TControl.WMContextMenu
  9752. SendCancelMode(SessionsPageControl);
  9753. // explicit popup instead of using PopupMenu property
  9754. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9755. // while connecting yet not-connected session and hence
  9756. // allowing an access to commands over not-completelly connected session
  9757. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9758. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9759. PopupMenu->PopupComponent = SessionsPageControl;
  9760. PopupMenu->Popup(Point.x, Point.y);
  9761. }
  9762. }
  9763. Handled = true;
  9764. }
  9765. //---------------------------------------------------------------------------
  9766. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9767. {
  9768. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  9769. }
  9770. //---------------------------------------------------------------------------
  9771. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9772. {
  9773. Configuration->Usage->Inc(L"PasswordChanges");
  9774. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9775. ChangePasswordData->DisableAuthentationsExceptPassword();
  9776. ChangePasswordData->ChangePassword = true;
  9777. std::unique_ptr<TTerminal> ChangePasswordSession(
  9778. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9779. ChangePasswordSession->AutoReadDirectory = false;
  9780. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9781. {
  9782. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9783. }
  9784. else
  9785. {
  9786. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9787. }
  9788. }
  9789. //---------------------------------------------------------------------------
  9790. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9791. {
  9792. // No nice way to assert SSH2
  9793. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  9794. }
  9795. //---------------------------------------------------------------------------
  9796. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9797. {
  9798. TOperationVisualizer OperationVisualizer;
  9799. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9800. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9801. }
  9802. //---------------------------------------------------------------------------
  9803. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9804. {
  9805. TForm::ChangeScale(M, D);
  9806. int APixelsPerInch = GetControlPixelsPerInch(this);
  9807. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9808. try
  9809. {
  9810. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9811. }
  9812. __finally
  9813. {
  9814. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9815. }
  9816. }
  9817. //---------------------------------------------------------------------------
  9818. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9819. {
  9820. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9821. }
  9822. //---------------------------------------------------------------------
  9823. class TPublicControl : public TControl
  9824. {
  9825. friend class TCustomScpExplorerForm;
  9826. };
  9827. //---------------------------------------------------------------------------
  9828. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9829. {
  9830. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9831. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9832. }
  9833. //---------------------------------------------------------------------------
  9834. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9835. {
  9836. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9837. OpenBookmark(Side, Bookmark);
  9838. }
  9839. //---------------------------------------------------------------------------
  9840. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9841. {
  9842. DoBookmarkClick(osLocal, Sender);
  9843. }
  9844. //---------------------------------------------------------------------------
  9845. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9846. {
  9847. DoBookmarkClick(osRemote, Sender);
  9848. }
  9849. //---------------------------------------------------------------------------
  9850. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9851. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9852. {
  9853. if (BookmarkList != NULL)
  9854. {
  9855. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9856. if (IsLocalBrowserMode())
  9857. {
  9858. Side = osLocal;
  9859. }
  9860. if (!WinConfiguration->UseLocationProfiles)
  9861. {
  9862. std::unique_ptr<TStringList> Directories(new TStringList());
  9863. Directories->CaseSensitive = (Side == osRemote);
  9864. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9865. {
  9866. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9867. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9868. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9869. {
  9870. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9871. Item->Caption = EscapeHotkey(Directory);
  9872. Item->ShortCut = Bookmark->ShortCut;
  9873. Item->OnClick = OnBookmarkClick;
  9874. Item->Tag = reinterpret_cast<int>(Bookmark);
  9875. Directories->Add(Directory);
  9876. Menu->Add(Item.release());
  9877. }
  9878. }
  9879. }
  9880. else
  9881. {
  9882. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9883. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9884. {
  9885. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9886. if (!Bookmark->Node.IsEmpty())
  9887. {
  9888. Folders->Add(Bookmark->Node);
  9889. }
  9890. }
  9891. for (int Index = 0; Index < Folders->Count; Index++)
  9892. {
  9893. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9894. Item->Caption = Folders->Strings[Index];
  9895. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  9896. Folders->Objects[Index] = Item.get();
  9897. Menu->Add(Item.release());
  9898. }
  9899. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9900. {
  9901. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9902. TTBCustomItem * Parent;
  9903. if (!Bookmark->Node.IsEmpty())
  9904. {
  9905. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9906. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9907. }
  9908. else
  9909. {
  9910. Parent = Menu;
  9911. }
  9912. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9913. Item->Caption = Bookmark->Name;
  9914. Item->ShortCut = Bookmark->ShortCut;
  9915. Item->OnClick = OnBookmarkClick;
  9916. Item->Tag = reinterpret_cast<int>(Bookmark);
  9917. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9918. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9919. {
  9920. // No hint for location profiles that are actually mere "bookmarks"
  9921. }
  9922. else
  9923. {
  9924. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9925. UnicodeString LongHint;
  9926. if (!Bookmark->Local.IsEmpty())
  9927. {
  9928. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9929. }
  9930. if (!Bookmark->Remote.IsEmpty())
  9931. {
  9932. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9933. }
  9934. AddToList(Hint, LongHint, L"|");
  9935. Item->Hint = Hint;
  9936. }
  9937. Parent->Add(Item.release());
  9938. }
  9939. }
  9940. AddMenuSeparator(Menu);
  9941. }
  9942. }
  9943. //---------------------------------------------------------------------------
  9944. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9945. {
  9946. Menu->Clear();
  9947. std::unique_ptr<TTBCustomItem> Item;
  9948. Item.reset(new TTBXItem(Owner));
  9949. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9950. Item->Options = Item->Options << tboDefault;
  9951. Menu->Add(Item.release());
  9952. AddMenuSeparator(Menu);
  9953. if (Terminal != NULL)
  9954. {
  9955. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9956. }
  9957. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9958. Item.reset(new TTBXItem(Owner));
  9959. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  9960. Menu->Add(Item.release());
  9961. }
  9962. //---------------------------------------------------------------------------
  9963. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9964. {
  9965. bool Result = true;
  9966. bool Remote = !IsSideLocalBrowser(Side);
  9967. try
  9968. {
  9969. if (Remote)
  9970. {
  9971. Terminal->ExceptionOnFail = true;
  9972. }
  9973. try
  9974. {
  9975. DirView(Side)->Path = Path;
  9976. }
  9977. __finally
  9978. {
  9979. if (Remote)
  9980. {
  9981. Terminal->ExceptionOnFail = false;
  9982. }
  9983. }
  9984. }
  9985. catch (Exception & E)
  9986. {
  9987. if (!Remote || Terminal->Active)
  9988. {
  9989. ShowExtendedExceptionEx(Terminal, &E);
  9990. Result = false;
  9991. }
  9992. else
  9993. {
  9994. throw;
  9995. }
  9996. }
  9997. return Result;
  9998. }
  9999. //---------------------------------------------------------------------------
  10000. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10001. {
  10002. // Make sure there some feedback even when loading tiny local folders
  10003. TInstantOperationVisualizer Visualizer;
  10004. DirView(Side)->ReloadDirectory();
  10005. }
  10006. //---------------------------------------------------------------------------
  10007. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10008. {
  10009. if (Index == SessionsPageControl->TabIndex)
  10010. {
  10011. CloseSession();
  10012. }
  10013. else
  10014. {
  10015. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10016. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10017. TTerminalManager * Manager = TTerminalManager::Instance();
  10018. if (!Terminal->Active)
  10019. {
  10020. Manager->FreeTerminal(Terminal);
  10021. }
  10022. else
  10023. {
  10024. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10025. if (CanCloseQueue(Queue))
  10026. {
  10027. Manager->FreeTerminal(Terminal);
  10028. }
  10029. }
  10030. }
  10031. }
  10032. //---------------------------------------------------------------------------
  10033. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  10034. {
  10035. CloseSessionTab(Index);
  10036. }
  10037. //---------------------------------------------------------------------------
  10038. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10039. {
  10040. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10041. {
  10042. TInstantOperationVisualizer Visualizer;
  10043. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10044. // before we create a new data object
  10045. ClipboardClear();
  10046. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10047. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10048. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10049. // Intermediate variable to WORKAROUND a crash in compiler
  10050. TObjectList * ClipboardFakeMonitors =
  10051. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10052. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10053. if (FClipboardDragDropFilesEx.get() == NULL)
  10054. {
  10055. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10056. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10057. }
  10058. FClipboardDragDropFilesEx->FileList->Clear();
  10059. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10060. FClipboardDragDropFilesEx->CopyToClipboard();
  10061. FClipboardTerminal = Terminal;
  10062. // Need full paths, as cwd can be different once files are pasted
  10063. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10064. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10065. {
  10066. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10067. }
  10068. }
  10069. }
  10070. //---------------------------------------------------------------------------
  10071. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10072. {
  10073. if (OpenClipboard(0))
  10074. {
  10075. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10076. CloseClipboard();
  10077. }
  10078. }
  10079. //---------------------------------------------------------------------------
  10080. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10081. {
  10082. return !FClipboardFakeDirectory.IsEmpty();
  10083. }
  10084. //---------------------------------------------------------------------------
  10085. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10086. {
  10087. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10088. FClipboardFakeDirectory = UnicodeString();
  10089. FClipboardTerminal = NULL;
  10090. // Also called in Idle()
  10091. FClipboardFakeMonitors.reset(NULL);
  10092. }
  10093. //---------------------------------------------------------------------------
  10094. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10095. {
  10096. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10097. {
  10098. ClipboardStop();
  10099. }
  10100. }
  10101. //---------------------------------------------------------------------------
  10102. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10103. {
  10104. TTerminalManager * Manager = TTerminalManager::Instance();
  10105. Manager->ActiveSession = FClipboardTerminal;
  10106. TTransferOperationParam Params;
  10107. Params.TargetDirectory = TargetDirectory;
  10108. Params.DragDrop = DragDrop;
  10109. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10110. }
  10111. //---------------------------------------------------------------------------
  10112. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10113. {
  10114. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  10115. // (as it pumps a Windows message queue while being shutted down)
  10116. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10117. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10118. {
  10119. // Can fail as it can be e.g. locked by AV, so we retry that later
  10120. bool Removed = RemoveDir(ApiPath(FileName));
  10121. try
  10122. {
  10123. if (!NonVisualDataModule->Busy)
  10124. {
  10125. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10126. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10127. }
  10128. }
  10129. __finally
  10130. {
  10131. if (!Removed)
  10132. {
  10133. RemoveDir(ApiPath(FileName));
  10134. }
  10135. }
  10136. }
  10137. }
  10138. //---------------------------------------------------------------------------
  10139. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10140. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10141. {
  10142. TFileMasks::TParams MaskParams;
  10143. MaskParams.Size = Size;
  10144. MaskParams.Modification = Modification;
  10145. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10146. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10147. {
  10148. bool ImplicitMatch;
  10149. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  10150. !ImplicitMatch)
  10151. {
  10152. Color = Iter->Color;
  10153. break;
  10154. }
  10155. }
  10156. }
  10157. //---------------------------------------------------------------------------
  10158. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10159. {
  10160. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10161. if (!DirView->IsEditing())
  10162. {
  10163. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10164. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10165. // while searching
  10166. if (Key == VK_BACK)
  10167. {
  10168. if (!FIncrementalSearch.IsEmpty())
  10169. {
  10170. if (FIncrementalSearch.Length() == 1)
  10171. {
  10172. ResetIncrementalSearch();
  10173. }
  10174. else
  10175. {
  10176. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10177. IncrementalSearch(NewText, false, false);
  10178. }
  10179. Key = 0;
  10180. }
  10181. }
  10182. }
  10183. }
  10184. //---------------------------------------------------------------------------
  10185. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10186. {
  10187. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10188. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10189. {
  10190. // Filter control sequences.
  10191. // When not searching yet, prefer use of the space for toggling file selection
  10192. // (so we cannot incrementally search for a string starting with a space).
  10193. if ((Key > VK_SPACE) ||
  10194. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10195. {
  10196. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10197. Key = 0;
  10198. }
  10199. }
  10200. }
  10201. //---------------------------------------------------------------------------
  10202. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10203. {
  10204. if (!FIncrementalSearch.IsEmpty())
  10205. {
  10206. FIncrementalSearch = L"";
  10207. DirView(osCurrent)->UpdateStatusBar();
  10208. }
  10209. }
  10210. //---------------------------------------------------------------------------
  10211. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10212. {
  10213. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10214. if (Item == NULL)
  10215. {
  10216. MessageBeep(MB_ICONHAND);
  10217. }
  10218. else
  10219. {
  10220. TCustomDirView * ADirView = DirView(osCurrent);
  10221. {
  10222. TAutoNestingCounter Guard(FIncrementalSearching);
  10223. ADirView->FocusItem(Item);
  10224. if (ADirView->NortonLike == nlOff)
  10225. {
  10226. if ((ADirView->Selected != Item) ||
  10227. (ADirView->SelCount > 1))
  10228. {
  10229. ADirView->ClearSelection();
  10230. ADirView->Selected = Item;
  10231. }
  10232. }
  10233. }
  10234. FIncrementalSearch = Text;
  10235. Item->MakeVisible(false);
  10236. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10237. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10238. ADirView->UpdateStatusBar();
  10239. }
  10240. }
  10241. //---------------------------------------------------------------------------
  10242. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10243. {
  10244. int Index = Item->Index;
  10245. if (!Reverse)
  10246. {
  10247. Index++;
  10248. if (Index >= Item->Owner->Count)
  10249. {
  10250. Index = 0;
  10251. }
  10252. }
  10253. else
  10254. {
  10255. Index--;
  10256. if (Index < 0)
  10257. {
  10258. Index = Item->Owner->Count - 1;
  10259. }
  10260. }
  10261. return Item->Owner->Item[Index];
  10262. }
  10263. //---------------------------------------------------------------------------
  10264. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10265. {
  10266. TCustomDirView * ADirView = DirView(osCurrent);
  10267. if (ADirView->Items->Count == 0)
  10268. {
  10269. return NULL;
  10270. }
  10271. else
  10272. {
  10273. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10274. TListItem * Item = CurrentItem;
  10275. if (SkipCurrent)
  10276. {
  10277. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10278. }
  10279. while (true)
  10280. {
  10281. bool Matches = false;
  10282. switch (WinConfiguration->PanelSearch)
  10283. {
  10284. case isNameStartOnly:
  10285. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  10286. break;
  10287. case isName:
  10288. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  10289. break;
  10290. case isAll:
  10291. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10292. int Index = 0;
  10293. while ((Index < ColCount) && !Matches)
  10294. {
  10295. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10296. if (Visible)
  10297. {
  10298. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10299. }
  10300. Index++;
  10301. }
  10302. break;
  10303. }
  10304. if (Matches)
  10305. {
  10306. return Item;
  10307. }
  10308. Item = GetNextFile(Item, Reverse);
  10309. if (Item == CurrentItem)
  10310. {
  10311. return NULL;
  10312. }
  10313. }
  10314. }
  10315. }
  10316. //---------------------------------------------------------------------------
  10317. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10318. {
  10319. ResetIncrementalSearch();
  10320. }
  10321. //---------------------------------------------------------------------------
  10322. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10323. {
  10324. if (Message.CharCode == VK_TAB)
  10325. {
  10326. if (!FIncrementalSearch.IsEmpty())
  10327. {
  10328. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10329. bool Reverse = Shift.Contains(ssShift);
  10330. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10331. Message.Result = 1;
  10332. return;
  10333. }
  10334. }
  10335. else if (Message.CharCode == VK_ESCAPE)
  10336. {
  10337. if (!FIncrementalSearch.IsEmpty())
  10338. {
  10339. ResetIncrementalSearch();
  10340. Message.Result = 1;
  10341. return;
  10342. }
  10343. }
  10344. TForm::Dispatch(&Message);
  10345. }
  10346. //---------------------------------------------------------------------------
  10347. void __fastcall TCustomScpExplorerForm::Deactivate()
  10348. {
  10349. TForm::Deactivate();
  10350. // E.g. when switching to an internal editor window
  10351. ResetIncrementalSearch();
  10352. }
  10353. //---------------------------------------------------------------------------
  10354. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10355. {
  10356. // When switching to another application
  10357. ResetIncrementalSearch();
  10358. }
  10359. //---------------------------------------------------------------------------
  10360. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10361. {
  10362. ResetIncrementalSearch();
  10363. }
  10364. //---------------------------------------------------------------------------
  10365. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10366. {
  10367. if (FIncrementalSearching <= 0)
  10368. {
  10369. ResetIncrementalSearch();
  10370. }
  10371. }
  10372. //---------------------------------------------------------------------------
  10373. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10374. {
  10375. CountClicksForWindowPrint(this);
  10376. }
  10377. //---------------------------------------------------------------------------
  10378. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10379. {
  10380. CenterReconnectToolbar();
  10381. }
  10382. //---------------------------------------------------------------------------
  10383. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10384. {
  10385. if (RemoteDirView->ItemFocused != NULL)
  10386. {
  10387. RemoteDirView->ItemFocused->Selected = true;
  10388. }
  10389. }
  10390. //---------------------------------------------------------------------------
  10391. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10392. {
  10393. if (!FQueueStatusUpdating)
  10394. {
  10395. bool Refresh;
  10396. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10397. {
  10398. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10399. }
  10400. else
  10401. {
  10402. if (FQueueFileList->GetCount() > 0)
  10403. {
  10404. FQueueFileList->Clear();
  10405. Refresh = true;
  10406. }
  10407. }
  10408. int Count = FQueueFileList->GetCount();
  10409. if (QueueFileList->Items->Count != Count)
  10410. {
  10411. DebugAssert(Refresh);
  10412. QueueFileList->Items->Count = Count;
  10413. }
  10414. if (Refresh)
  10415. {
  10416. QueueFileList->Invalidate();
  10417. }
  10418. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10419. }
  10420. }
  10421. //---------------------------------------------------------------------------
  10422. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10423. {
  10424. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10425. }
  10426. //---------------------------------------------------------------------------
  10427. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10428. {
  10429. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10430. // See TSynchronizeChecklistDialog::UpdateTimer
  10431. if (QueueFileList->Items->Count > 0)
  10432. {
  10433. QueueFileList->Items->Item[0]->MakeVisible(false);
  10434. }
  10435. UpdateQueueFileList();
  10436. }
  10437. //---------------------------------------------------------------------------
  10438. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10439. {
  10440. Active = QueueView3->Focused() || QueueFileList->Focused();
  10441. }
  10442. //---------------------------------------------------------------------------
  10443. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10444. {
  10445. QueueLabelUpdateStatus();
  10446. }
  10447. //---------------------------------------------------------------------------
  10448. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10449. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10450. {
  10451. int State = FQueueFileList->GetState(Item->Index);
  10452. if (State == qfsProcessed)
  10453. {
  10454. Sender->Canvas->Font->Color = clGrayText;
  10455. }
  10456. }
  10457. //---------------------------------------------------------------------------
  10458. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10459. {
  10460. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10461. QueueFileList->Column[0]->Width =
  10462. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10463. }
  10464. //---------------------------------------------------------------------------
  10465. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10466. {
  10467. QueueFileListColumnAutoSize();
  10468. }
  10469. //---------------------------------------------------------------------------
  10470. void __fastcall TCustomScpExplorerForm::CloseApp()
  10471. {
  10472. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10473. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10474. // So we clear the flag here.
  10475. DebugAssert(NonVisualDataModule->Busy);
  10476. NonVisualDataModule->EndBusy();
  10477. try
  10478. {
  10479. Close();
  10480. }
  10481. __finally
  10482. {
  10483. NonVisualDataModule->StartBusy();
  10484. }
  10485. }
  10486. //---------------------------------------------------------------------------
  10487. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10488. {
  10489. return (Terminal != NULL) && Terminal->Active;
  10490. }
  10491. //---------------------------------------------------------------------------
  10492. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  10493. {
  10494. return (ManagedSession != NULL);
  10495. }
  10496. //---------------------------------------------------------------------------
  10497. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10498. {
  10499. return IsActiveTerminal(Terminal);
  10500. }
  10501. //---------------------------------------------------------------------------
  10502. void TCustomScpExplorerForm::LocalLocalCopy(
  10503. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  10504. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  10505. {
  10506. DebugFail();
  10507. }
  10508. //---------------------------------------------------------------------------
  10509. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  10510. {
  10511. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  10512. }