CustomScpExplorer.cpp 342 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489
  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. //---------------------------------------------------------------------------
  34. #pragma package(smart_init)
  35. #pragma link "CustomDirView"
  36. #pragma link "CustomUnixDirView"
  37. #pragma link "IEListView"
  38. #pragma link "NortonLikeListView"
  39. #pragma link "UnixDirView"
  40. #pragma link "CustomDriveView"
  41. #pragma link "UnixDriveView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "TB2Dock"
  44. #pragma link "TBXStatusBars"
  45. #pragma link "TB2Item"
  46. #pragma link "TB2Toolbar"
  47. #pragma link "ThemePageControl"
  48. #pragma link "PathLabel"
  49. #ifndef NO_RESOURCES
  50. #pragma resource "*.dfm"
  51. #endif
  52. //---------------------------------------------------------------------------
  53. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  54. static const int SessionPanelCount = 4;
  55. //---------------------------------------------------------------------------
  56. class TMutexGuard
  57. {
  58. public:
  59. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  60. int Timeout = 5000)
  61. {
  62. FMutex = NULL;
  63. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  64. if (WaitResult == WAIT_TIMEOUT)
  65. {
  66. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  67. }
  68. else
  69. {
  70. FMutex = AMutex;
  71. }
  72. }
  73. ~TMutexGuard()
  74. {
  75. if (FMutex != NULL)
  76. {
  77. ReleaseMutex(FMutex);
  78. }
  79. }
  80. private:
  81. HANDLE FMutex;
  82. };
  83. //---------------------------------------------------------------------------
  84. class TWindowLock
  85. {
  86. public:
  87. TWindowLock(TCustomScpExplorerForm * Form) :
  88. FForm(Form)
  89. {
  90. FForm->LockWindow();
  91. }
  92. ~TWindowLock()
  93. {
  94. FForm->UnlockWindow();
  95. }
  96. private:
  97. TCustomScpExplorerForm * FForm;
  98. };
  99. //---------------------------------------------------------------------------
  100. class TAutoBatch
  101. {
  102. public:
  103. TAutoBatch(TCustomScpExplorerForm * Form) :
  104. FForm(Form)
  105. {
  106. FForm->BatchStart(FBatchStorage);
  107. }
  108. ~TAutoBatch()
  109. {
  110. FForm->BatchEnd(FBatchStorage);
  111. }
  112. private:
  113. TCustomScpExplorerForm * FForm;
  114. void * FBatchStorage;
  115. };
  116. //---------------------------------------------------------------------------
  117. struct TTransferOperationParam
  118. {
  119. TTransferOperationParam();
  120. UnicodeString TargetDirectory;
  121. bool Temp;
  122. bool DragDrop;
  123. int Options;
  124. TAutoSwitch Queue;
  125. };
  126. //---------------------------------------------------------------------------
  127. TTransferOperationParam::TTransferOperationParam()
  128. {
  129. Temp = false;
  130. DragDrop = false;
  131. Options = 0;
  132. Queue = asAuto;
  133. }
  134. //---------------------------------------------------------------------------
  135. class TTransferPresetNoteData : public TObject
  136. {
  137. public:
  138. UnicodeString Message;
  139. };
  140. //---------------------------------------------------------------------------
  141. class TTerminalNoteData : public TObject
  142. {
  143. public:
  144. TTerminal * Terminal;
  145. };
  146. //---------------------------------------------------------------------------
  147. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  148. FFormRestored(false),
  149. TForm(Owner)
  150. {
  151. FCurrentSide = osRemote;
  152. FEverShown = false;
  153. FDocks = new TList();
  154. RestoreParams();
  155. ConfigurationChanged();
  156. RemoteDirView->Invalidate();
  157. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  158. NonVisualDataModule->ScpExplorer = this;
  159. FAutoOperation = false;
  160. FOnFileOperationFinished = NULL;
  161. FForceExecution = false;
  162. FIgnoreNextDialogChar = 0;
  163. FErrorList = NULL;
  164. FSynchronizeProgressForm = NULL;
  165. FProgressForm = NULL;
  166. FRefreshLocalDirectory = false;
  167. FRefreshRemoteDirectory = false;
  168. FDDExtMapFile = NULL;
  169. // CreateMutexW keeps failing with ERROR_NOACCESS
  170. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  171. DebugAssert(FDDExtMutex != NULL);
  172. FDDTargetControl = NULL;
  173. FDelayedDeletionTimer = NULL;
  174. FDelayedDeletionList = new TStringList();
  175. FDDFileList = NULL;
  176. FPendingTempSpaceWarn = false;
  177. FCapturedLog = NULL;
  178. FDragDropOperation = false;
  179. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  180. FAllowTransferPresetAutoSelect = true;
  181. FCopyParamDefault = L"";
  182. FSynchronizeController = NULL;
  183. FPendingQueueActionItem = NULL;
  184. FLockLevel = 0;
  185. FLockSuspendLevel = 0;
  186. FDisabledOnLockSuspend = false;
  187. FAlternativeDelete = false;
  188. FTrayIcon = new ::TTrayIcon(0);
  189. FTrayIcon->OnClick = TrayIconClick;
  190. FMaxQueueLength = 0;
  191. FLastContextPopupScreenPoint = TPoint(-1, -1);
  192. FTransferResumeList = NULL;
  193. FMoveToQueue = false;
  194. FStandaloneEditing = false;
  195. FOnFeedSynchronizeError = NULL;
  196. FNeedSession = false;
  197. FDoNotIdleCurrentTerminal = 0;
  198. FIncrementalSearching = 0;
  199. FIncrementalSearchHaveNext = false;
  200. FEditorManager = new TEditorManager();
  201. FEditorManager->OnFileChange = ExecutedFileChanged;
  202. FEditorManager->OnFileReload = ExecutedFileReload;
  203. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  204. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  205. FLocalEditors = new TList();
  206. FQueueStatus = NULL;
  207. FQueueStatusSection = new TCriticalSection();
  208. FQueueStatusInvalidated = false;
  209. FQueueItemInvalidated = 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(QueueLabel);
  239. UseDesktopFont(RemoteStatusBar);
  240. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  241. QueueSplitter->ShowHint = true;
  242. RemotePanelSplitter->ShowHint = true;
  243. UpdateImages();
  244. FCustomCommandMenu = CreateTBXPopupMenu(this);
  245. FCustomCommandLocalFileList = NULL;
  246. FCustomCommandRemoteFileList = NULL;
  247. FSessionColors = new TPngImageList(this);
  248. FSessionColors->ColorDepth = cd32Bit;
  249. AddFixedSessionImages();
  250. SessionsPageControl->Images = FSessionColors;
  251. QueueLabel->FocusControl = QueueView3;
  252. UpdateQueueLabel();
  253. FRemoteDirViewWasFocused = true;
  254. CreateHiddenWindow();
  255. StartUpdates();
  256. }
  257. //---------------------------------------------------------------------------
  258. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  259. {
  260. // this has to be one of the very first things to do
  261. StopUpdateThread();
  262. delete FCustomCommandLocalFileList;
  263. delete FCustomCommandRemoteFileList;
  264. delete FCustomCommandMenu;
  265. delete FTrayIcon;
  266. FTrayIcon = NULL;
  267. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  268. delete FEditorManager;
  269. ForceCloseLocalEditors();
  270. delete FLocalEditors;
  271. HideFileFindDialog();
  272. if (FDelayedDeletionTimer)
  273. {
  274. DoDelayedDeletion(NULL);
  275. SAFE_DESTROY(FDelayedDeletionTimer);
  276. }
  277. SAFE_DESTROY(FDelayedDeletionList);
  278. // sometimes we do not get DDEnd so the list is not released
  279. SAFE_DESTROY(FDDFileList);
  280. DebugAssert(FSynchronizeController == NULL);
  281. CloseHandle(FDDExtMutex);
  282. FDDExtMutex = NULL;
  283. FreeLibrary(FOle32Library);
  284. FOle32Library = NULL;
  285. FDragMoveCursor = NULL;
  286. DebugAssert(!FErrorList);
  287. if (FEverShown)
  288. {
  289. // when window is never shown (like when running command-line operation),
  290. // particularly window site is not restored correctly (BoundsRect value set
  291. // in RestoreForm gets lost during handle allocation), so we do not want
  292. // it to be stored
  293. StoreParams();
  294. }
  295. Terminal = NULL;
  296. Queue = NULL;
  297. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  298. NonVisualDataModule->ScpExplorer = NULL;
  299. delete FQueueController;
  300. FQueueController = NULL;
  301. delete FQueueStatusSection;
  302. FQueueStatusSection = NULL;
  303. delete FQueueStatus;
  304. FQueueStatus = NULL;
  305. delete FUserActionTimer;
  306. FUserActionTimer = NULL;
  307. delete FNoteTimer;
  308. delete FNotes;
  309. delete FNoteData;
  310. SAFE_DESTROY(FDocks);
  311. SessionsPageControl->Images = NULL;
  312. // only after clearing Terminal (after DoTerminalListChanged => ... => AddSessionColor is called)
  313. SAFE_DESTROY(FSessionColors);
  314. SAFE_DESTROY(FSessionsDragDropFilesEx);
  315. SAFE_DESTROY(FHistoryMenu[0][0]);
  316. SAFE_DESTROY(FHistoryMenu[0][1]);
  317. SAFE_DESTROY(FHistoryMenu[1][0]);
  318. SAFE_DESTROY(FHistoryMenu[1][1]);
  319. if (FHiddenWindow != NULL)
  320. {
  321. DestroyWindow(FHiddenWindow);
  322. FHiddenWindow = NULL;
  323. }
  324. }
  325. //---------------------------------------------------------------------------
  326. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  327. {
  328. std::unique_ptr<TSessionData> Data;
  329. if (!Session.IsEmpty())
  330. {
  331. bool DefaultsOnly;
  332. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  333. }
  334. TTerminalManager * Manager = TTerminalManager::Instance();
  335. for (int Index = 0; Index < Manager->Count; Index++)
  336. {
  337. TTerminal * Terminal = Manager->Terminals[Index];
  338. if (Session.IsEmpty() ||
  339. Terminal->SessionData->IsSameSite(Data.get()))
  340. {
  341. if (Path.IsEmpty())
  342. {
  343. Terminal->ClearCaches();
  344. }
  345. else
  346. {
  347. Terminal->DirectoryModified(Path, true);
  348. }
  349. }
  350. }
  351. // We should flag a pending refresh for the background terminals or busy foreground terminals
  352. if ((Terminal != NULL) && Terminal->Active &&
  353. CanCommandLineFromAnotherInstance() &&
  354. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  355. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  356. {
  357. Terminal->ReloadDirectory();
  358. }
  359. }
  360. //---------------------------------------------------------------------------
  361. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  362. {
  363. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  364. size_t MessageSize = sizeof(TCopyDataMessage);
  365. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  366. if (Result)
  367. {
  368. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  369. Result = (Message.Version == TCopyDataMessage::Version1);
  370. if (Result)
  371. {
  372. switch (Message.Command)
  373. {
  374. case TCopyDataMessage::CommandCanCommandLine:
  375. Result = CanCommandLineFromAnotherInstance();
  376. break;
  377. case TCopyDataMessage::CommandCommandLine:
  378. {
  379. UnicodeString CommandLine(Message.CommandLine);
  380. Result = CommandLineFromAnotherInstance(CommandLine);
  381. }
  382. break;
  383. case TCopyDataMessage::RefreshPanel:
  384. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  385. break;
  386. case TCopyDataMessage::MainWindowCheck:
  387. Result = true;
  388. break;
  389. default:
  390. Result = false;
  391. break;
  392. }
  393. }
  394. }
  395. Message.Result = Result ? 1 : 0;
  396. }
  397. //---------------------------------------------------------------------------
  398. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  399. {
  400. WNDCLASS WindowClass = {0};
  401. WindowClass.lpfnWndProc = DefWindowProc;
  402. WindowClass.hInstance = HInstance;
  403. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  404. FHiddenWindow = NULL;
  405. if (RegisterClass(&WindowClass))
  406. {
  407. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  408. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  409. }
  410. }
  411. //---------------------------------------------------------------------------
  412. bool __fastcall TCustomScpExplorerForm::CanConsole()
  413. {
  414. return
  415. (Terminal != NULL) &&
  416. Terminal->Active &&
  417. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  418. }
  419. //---------------------------------------------------------------------------
  420. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  421. {
  422. bool Result = Visible && !NonVisualDataModule->Busy;
  423. return Result;
  424. }
  425. //---------------------------------------------------------------------------
  426. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  427. const UnicodeString & CommandLine)
  428. {
  429. TProgramParams Params(CommandLine);
  430. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  431. if (Result)
  432. {
  433. NonVisualDataModule->StartBusy();
  434. try
  435. {
  436. // this action is initiated from another process,
  437. // so it's likely that our window is not visible,
  438. // and user won't see what is going on
  439. Application->BringToFront();
  440. // reload sessions as we may be asked to open a session
  441. // just stored by another instance
  442. StoredSessions->Reload();
  443. UnicodeString SessionName = Params.Param[1];
  444. std::unique_ptr<TObjectList> DataList(new TObjectList());
  445. UnicodeString DownloadFile; // unused
  446. try
  447. {
  448. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  449. }
  450. catch (EAbort &)
  451. {
  452. // Happens when opening session in PuTTY and in other situations,
  453. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  454. // to process the command-line on its own (or to pass it to yet another instance)
  455. }
  456. if (DataList->Count > 0)
  457. {
  458. TTerminalManager * Manager = TTerminalManager::Instance();
  459. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  460. }
  461. }
  462. __finally
  463. {
  464. NonVisualDataModule->EndBusy();
  465. }
  466. }
  467. return Result;
  468. }
  469. //---------------------------------------------------------------------------
  470. void __fastcall TCustomScpExplorerForm::SetTerminal(TTerminal * value)
  471. {
  472. if (FTerminal != value)
  473. {
  474. TerminalChanging();
  475. DoSetTerminal(value, false);
  476. }
  477. }
  478. //---------------------------------------------------------------------------
  479. void __fastcall TCustomScpExplorerForm::DoSetTerminal(TTerminal * value, bool Replace)
  480. {
  481. FTerminal = value;
  482. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  483. FAllowTransferPresetAutoSelect = false;
  484. try
  485. {
  486. TerminalChanged(Replace);
  487. }
  488. __finally
  489. {
  490. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  491. }
  492. if (Terminal != NULL)
  493. {
  494. TransferPresetAutoSelect();
  495. }
  496. }
  497. //---------------------------------------------------------------------------
  498. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TTerminal * value)
  499. {
  500. DoSetTerminal(value, true);
  501. }
  502. //---------------------------------------------------------------------------
  503. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  504. {
  505. if (FTerminal != NULL)
  506. {
  507. UpdateTerminal(Terminal);
  508. }
  509. }
  510. //---------------------------------------------------------------------------
  511. void __fastcall TCustomScpExplorerForm::TerminalChanged(bool Replaced)
  512. {
  513. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  514. if (ManagedTerminal != NULL)
  515. {
  516. UpdateSessionColor((TColor)ManagedTerminal->StateData->Color);
  517. }
  518. DoTerminalListChanged();
  519. if (Replaced)
  520. {
  521. RemoteDirView->ReplaceTerminal(Terminal);
  522. }
  523. else
  524. {
  525. RemoteDirView->Terminal = Terminal;
  526. }
  527. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  528. if (Terminal != NULL)
  529. {
  530. if (Terminal->Active)
  531. {
  532. Terminal->RefreshDirectory();
  533. }
  534. DebugAssert(ManagedTerminal == Terminal);
  535. if (WinConfiguration->PreservePanelState)
  536. {
  537. if (ManagedTerminal->RemoteExplorerState != NULL)
  538. {
  539. RemoteDirView->RestoreState(ManagedTerminal->RemoteExplorerState);
  540. }
  541. else
  542. {
  543. RemoteDirView->ClearState();
  544. }
  545. }
  546. if (!Replaced)
  547. {
  548. InitStatusBar();
  549. }
  550. }
  551. UpdateTransferList();
  552. // Update panels Enable state before refreshing the labels
  553. UpdateControls();
  554. }
  555. //---------------------------------------------------------------------------
  556. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  557. {
  558. if (Queue != value)
  559. {
  560. if (FQueue != NULL)
  561. {
  562. FQueue->OnListUpdate = NULL;
  563. FQueue->OnQueueItemUpdate = NULL;
  564. }
  565. FQueue = value;
  566. if (FQueue != NULL)
  567. {
  568. DebugAssert(FQueue->OnListUpdate == NULL);
  569. FQueue->OnListUpdate = QueueListUpdate;
  570. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  571. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  572. }
  573. QueueChanged();
  574. }
  575. }
  576. //---------------------------------------------------------------------------
  577. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  578. TListItem * Item)
  579. {
  580. if (FQueueActedItem == Item)
  581. {
  582. FQueueActedItem = NULL;
  583. if ((QueueView3->PopupMenu != NULL) &&
  584. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  585. {
  586. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  587. ReleaseCapture();
  588. }
  589. }
  590. if (Item->Data == FPendingQueueActionItem)
  591. {
  592. FPendingQueueActionItem = NULL;
  593. }
  594. }
  595. //---------------------------------------------------------------------------
  596. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  597. {
  598. {
  599. TGuard Guard(FQueueStatusSection);
  600. FQueueStatusInvalidated = false;
  601. if (FQueue != NULL)
  602. {
  603. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  604. }
  605. }
  606. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  607. {
  608. FMaxQueueLength = FQueueStatus->Count;
  609. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  610. }
  611. FQueueController->UpdateQueueStatus(FQueueStatus);
  612. SetQueueProgress();
  613. UpdateQueueView();
  614. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  615. if (IsEmpty && (Terminal != NULL))
  616. {
  617. TOnceDoneOperation OnceDoneOperation =
  618. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  619. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  620. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  621. {
  622. FQueue->Enabled = false;
  623. }
  624. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  625. {
  626. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  627. }
  628. }
  629. }
  630. //---------------------------------------------------------------------------
  631. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  632. {
  633. UnicodeString Result;
  634. if (FQueueStatus != NULL)
  635. {
  636. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  637. {
  638. TFileOperationProgressType * ProgressData =
  639. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  640. if ((ProgressData != NULL) && ProgressData->InProgress)
  641. {
  642. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  643. }
  644. }
  645. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  646. {
  647. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  648. }
  649. }
  650. return Result;
  651. }
  652. //---------------------------------------------------------------------------
  653. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  654. {
  655. ComponentVisible[fcQueueView] =
  656. (WinConfiguration->QueueView.Show == qvShow) ||
  657. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  658. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  659. }
  660. //---------------------------------------------------------------------------
  661. void __fastcall TCustomScpExplorerForm::QueueChanged()
  662. {
  663. if (FQueueStatus != NULL)
  664. {
  665. delete FQueueStatus;
  666. FQueueStatus = NULL;
  667. }
  668. UpdateQueueStatus(true);
  669. }
  670. //---------------------------------------------------------------------------
  671. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  672. {
  673. if (FQueue == Queue)
  674. {
  675. FQueueStatusInvalidated = true;
  676. }
  677. }
  678. //---------------------------------------------------------------------------
  679. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  680. TQueueItem * Item)
  681. {
  682. if (FQueue == Queue)
  683. {
  684. TGuard Guard(FQueueStatusSection);
  685. // this may be running in parallel with QueueChanged
  686. if (FQueueStatus != NULL)
  687. {
  688. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  689. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  690. {
  691. FRefreshLocalDirectory = (QueueItem == NULL) ||
  692. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  693. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  694. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  695. }
  696. if (QueueItem != NULL)
  697. {
  698. QueueItem->UserData = (void*)true;
  699. FQueueItemInvalidated = true;
  700. }
  701. }
  702. }
  703. }
  704. //---------------------------------------------------------------------------
  705. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  706. {
  707. // during standalone editing, we have no way to see/control queue,
  708. // so we have to always popup prompts automatically
  709. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  710. }
  711. //---------------------------------------------------------------------------
  712. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  713. {
  714. if (FQueueStatus != NULL)
  715. {
  716. bool QueueAutoPopup = IsQueueAutoPopup();
  717. bool NeedRefresh = FQueueController->NeedRefresh();
  718. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  719. FQueueItemInvalidated = false;
  720. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  721. bool Updated = false;
  722. TQueueItemProxy * QueueItem;
  723. bool WasUserAction;
  724. for (int Index = 0; Index < Limit; Index++)
  725. {
  726. bool Update = false;
  727. QueueItem = FQueueStatus->Items[Index];
  728. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  729. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  730. (FPendingQueueActionItem == NULL))
  731. {
  732. FPendingQueueActionItem = QueueItem;
  733. FUserActionTimer->Enabled = true;
  734. }
  735. if ((bool)QueueItem->UserData)
  736. {
  737. QueueItem->UserData = (void*)false;
  738. QueueItem->Update();
  739. Updated = true;
  740. Update = true;
  741. }
  742. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  743. {
  744. Update = true;
  745. }
  746. if (Update)
  747. {
  748. FQueueController->RefreshQueueItem(QueueItem);
  749. }
  750. }
  751. if (Updated)
  752. {
  753. NonVisualDataModule->UpdateNonVisibleActions();
  754. SetQueueProgress();
  755. }
  756. }
  757. }
  758. //---------------------------------------------------------------------------
  759. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  760. {
  761. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  762. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  763. }
  764. //---------------------------------------------------------------------------
  765. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  766. {
  767. if (ProgressData->Operation != foCalculateSize)
  768. {
  769. int OverallProgress;
  770. // FProgressForm is null when this is called from SetQueueProgress
  771. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  772. {
  773. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  774. }
  775. else
  776. {
  777. OverallProgress = ProgressData->OverallProgress();
  778. }
  779. // implies TBPF_NORMAL
  780. FTaskbarList->SetProgressValue(GetMainForm()->Handle, OverallProgress, 100);
  781. }
  782. else
  783. {
  784. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  785. }
  786. }
  787. //---------------------------------------------------------------------------
  788. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  789. {
  790. TTerminalManager::Instance()->QueueStatusUpdated();
  791. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  792. {
  793. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  794. {
  795. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  796. {
  797. TFileOperationProgressType * ProgressData = NULL;
  798. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  799. {
  800. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  801. }
  802. if ((ProgressData != NULL) &&
  803. ProgressData->InProgress)
  804. {
  805. SetTaskbarListProgressValue(ProgressData);
  806. }
  807. else
  808. {
  809. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  810. }
  811. }
  812. else
  813. {
  814. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  815. }
  816. }
  817. else
  818. {
  819. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  820. }
  821. }
  822. UpdateQueueLabel();
  823. }
  824. //---------------------------------------------------------------------------
  825. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  826. {
  827. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  828. if (FQueueStatus != NULL)
  829. {
  830. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  831. if (ActiveAndPendingPrimaryCount > 0)
  832. {
  833. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  834. }
  835. }
  836. QueueLabel->Caption = Caption;
  837. }
  838. //---------------------------------------------------------------------------
  839. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  840. {
  841. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  842. static_cast<TComponent*>(GetComponent(fcTransferList)));
  843. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  844. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  845. TransferList->Strings->BeginUpdate();
  846. try
  847. {
  848. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  849. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  850. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  851. if (FTransferDropDownHint.IsEmpty())
  852. {
  853. FTransferDropDownHint = TransferDropDown->Hint;
  854. }
  855. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  856. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  857. TransferDropDown->Text = StripHotkey(Name);
  858. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  859. (FTransferDropDownHint, StripHotkey(Name),
  860. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  861. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General))));
  862. // update the label, otherwise when it is updated only on the first draw
  863. // of the list, it is drawn "bold" for some reason
  864. FTransferListHoverIndex = TransferList->ItemIndex;
  865. UpdateTransferLabel();
  866. }
  867. __finally
  868. {
  869. TransferList->Strings->EndUpdate();
  870. }
  871. }
  872. //---------------------------------------------------------------------------
  873. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  874. {
  875. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  876. DebugAssert(Toolbar != NULL);
  877. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  878. }
  879. //---------------------------------------------------------------------------
  880. void __fastcall TCustomScpExplorerForm::UpdateActions()
  881. {
  882. TForm::UpdateActions();
  883. if (ComponentVisible[fcCustomCommandsBand])
  884. {
  885. UpdateCustomCommandsToolbar();
  886. }
  887. }
  888. //---------------------------------------------------------------------------
  889. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  890. {
  891. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  892. }
  893. //---------------------------------------------------------------------------
  894. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  895. {
  896. Color = GetBtnFaceColor();
  897. DebugAssert(Configuration && RemoteDirView);
  898. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  899. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  900. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  901. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  902. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  903. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  904. {
  905. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  906. // selection is not redrawn automatically when RowSelect changes
  907. RemoteDirView->Invalidate();
  908. }
  909. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  910. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  911. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  912. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  913. UpdateSessionsPageControlHeight();
  914. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  915. UpdateToolbarDisplayMode();
  916. UpdateTransferList();
  917. if (Terminal != NULL)
  918. {
  919. TransferPresetAutoSelect();
  920. }
  921. UpdateQueueView();
  922. UpdateControls();
  923. // this can be called even before constuctor finishes.
  924. if (FEditorManager != NULL)
  925. {
  926. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  927. }
  928. // this can be called even before constuctor finishes.
  929. if (FLocalEditors != NULL)
  930. {
  931. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  932. {
  933. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  934. }
  935. }
  936. if (ComponentVisible[fcCustomCommandsBand])
  937. {
  938. UpdateCustomCommandsToolbar();
  939. }
  940. // show only when keeping queue items forever,
  941. // otherwise, its enough to have in in the context menu
  942. QueueDeleteAllDoneQueueToolbarItem->Visible =
  943. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  944. if (FFileColorsCurrent != WinConfiguration->FileColors)
  945. {
  946. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  947. FFileColorsCurrent = WinConfiguration->FileColors;
  948. FileColorsChanged();
  949. }
  950. }
  951. //---------------------------------------------------------------------------
  952. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  953. {
  954. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  955. DirView->Invalidate();
  956. }
  957. //---------------------------------------------------------------------------
  958. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  959. {
  960. DoFileColorsChanged(RemoteDirView);
  961. }
  962. //---------------------------------------------------------------------------
  963. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  964. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  965. void * /*Arg*/)
  966. {
  967. if (Token != NULL)
  968. {
  969. TForm * Editor = dynamic_cast<TForm*>(Token);
  970. DebugAssert(Editor != NULL);
  971. ReconfigureEditorForm(Editor);
  972. }
  973. }
  974. //---------------------------------------------------------------------------
  975. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  976. {
  977. WinConfiguration->DDTransferConfirmation = asOn;
  978. }
  979. //---------------------------------------------------------------------------
  980. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  981. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  982. {
  983. // noop
  984. }
  985. //---------------------------------------------------------------------------
  986. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  987. TTransferDirection Direction, TTransferType Type, bool Temp,
  988. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  989. bool Confirm, bool DragDrop, int Options)
  990. {
  991. bool Result = true;
  992. DebugAssert(Terminal && Terminal->Active);
  993. // these parameters are known in advance
  994. int Params =
  995. FLAGMASK(Type == ttMove, cpDelete);
  996. bool ToTemp = (Temp && (Direction == tdToLocal));
  997. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  998. {
  999. bool DisableNewerOnly =
  1000. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1001. ToTemp;
  1002. Options |=
  1003. FLAGMASK(ToTemp, coTemp) |
  1004. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  1005. coDoNotShowAgain;
  1006. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1007. int CopyParamAttrs = (Direction == tdToRemote ?
  1008. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1009. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1010. int OutputOptions =
  1011. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1012. cooDoNotShowAgain);
  1013. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1014. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1015. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1016. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions);
  1017. if (Result)
  1018. {
  1019. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1020. {
  1021. if (DragDrop)
  1022. {
  1023. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1024. {
  1025. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1026. NULL, 0, EnableDDTransferConfirmation, NULL);
  1027. }
  1028. WinConfiguration->DDTransferConfirmation = asOff;
  1029. }
  1030. else
  1031. {
  1032. WinConfiguration->ConfirmTransferring = false;
  1033. }
  1034. }
  1035. else
  1036. {
  1037. // User explicitly unchecked "do not show again",
  1038. // so show him the dialog the next time
  1039. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1040. {
  1041. WinConfiguration->DDTransferConfirmation = asOn;
  1042. }
  1043. }
  1044. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1045. }
  1046. }
  1047. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1048. {
  1049. Configuration->Usage->Inc(L"TransfersOnBackground");
  1050. // these parameter are known only after transfer dialog
  1051. Params |=
  1052. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1053. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1054. Result = false;
  1055. ClearTransferSourceSelection(Direction);
  1056. }
  1057. return Result;
  1058. }
  1059. //---------------------------------------------------------------------------
  1060. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1061. {
  1062. if (FOnFileOperationFinished != NULL)
  1063. {
  1064. FOnFileOperationFinished(UnicodeString(), true);
  1065. }
  1066. else
  1067. {
  1068. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1069. if (HasDirView[Side])
  1070. {
  1071. DirView(Side)->SelectAll(smNone);
  1072. }
  1073. }
  1074. }
  1075. //---------------------------------------------------------------------------
  1076. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1077. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1078. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1079. int Params)
  1080. {
  1081. DebugAssert(Queue != NULL);
  1082. bool SingleFile = false;
  1083. if (FileList->Count == 1)
  1084. {
  1085. if (Direction == tdToRemote)
  1086. {
  1087. UnicodeString FileName = FileList->Strings[0];
  1088. SingleFile = FileExists(ApiPath(FileName));
  1089. }
  1090. else
  1091. {
  1092. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1093. SingleFile = !File->IsDirectory;
  1094. }
  1095. }
  1096. TQueueItem * QueueItem;
  1097. if (Direction == tdToRemote)
  1098. {
  1099. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1100. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1101. }
  1102. else
  1103. {
  1104. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1105. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1106. }
  1107. AddQueueItem(Queue, QueueItem, Terminal);
  1108. }
  1109. //---------------------------------------------------------------------------
  1110. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TTerminal * Terminal)
  1111. {
  1112. if (Queue->IsEmpty)
  1113. {
  1114. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  1115. ManagedTerminal->QueueOperationStart = Now();
  1116. }
  1117. Queue->AddItem(QueueItem);
  1118. }
  1119. //---------------------------------------------------------------------------
  1120. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1121. {
  1122. }
  1123. //---------------------------------------------------------------------------
  1124. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1125. {
  1126. DebugAssert(FDocks != NULL);
  1127. for (int Index = 0; Index < ComponentCount; Index++)
  1128. {
  1129. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1130. if ((Dock != NULL) && (Dock->Tag == 0))
  1131. {
  1132. FDocks->Add(Dock);
  1133. }
  1134. }
  1135. CollectItemsWithTextDisplayMode(this);
  1136. ConfigurationChanged();
  1137. QueuePanel->Height =
  1138. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1139. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1140. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1141. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1142. }
  1143. //---------------------------------------------------------------------------
  1144. void __fastcall TCustomScpExplorerForm::StoreParams()
  1145. {
  1146. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1147. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1148. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1149. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1150. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1151. }
  1152. //---------------------------------------------------------------------------
  1153. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1154. {
  1155. if (!FFormRestored)
  1156. {
  1157. FFormRestored = true;
  1158. RestoreFormParams();
  1159. }
  1160. TForm::CreateParams(Params);
  1161. }
  1162. //---------------------------------------------------------------------------
  1163. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1164. {
  1165. DebugAssert(FDocks != NULL);
  1166. for (int Index = 0; Index < FDocks->Count; Index++)
  1167. {
  1168. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1169. }
  1170. }
  1171. //---------------------------------------------------------------------------
  1172. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1173. {
  1174. SetDockAllowDrag(true);
  1175. while (!ButtonsStr.IsEmpty())
  1176. {
  1177. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1178. UnicodeString Name = CutToChar(S, L'=', true);
  1179. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1180. {
  1181. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1182. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1183. {
  1184. while (!S.IsEmpty())
  1185. {
  1186. UnicodeString S2 = CutToChar(S, L',', true);
  1187. UnicodeString Name = CutToChar(S2, L':', true);
  1188. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1189. {
  1190. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1191. if (SameText(GetToolbarItemName(Item), Name))
  1192. {
  1193. // forward compatibility
  1194. UnicodeString S3 = CutToChar(S2, L':', true);
  1195. bool Visible = StrToIntDef(S3, 0);
  1196. Item->Visible = Visible;
  1197. }
  1198. }
  1199. }
  1200. }
  1201. }
  1202. }
  1203. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1204. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1205. }
  1206. //---------------------------------------------------------------------------
  1207. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1208. {
  1209. UnicodeString Result;
  1210. SetDockAllowDrag(true);
  1211. Result = ::GetToolbarsLayoutStr(this);
  1212. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1213. return Result;
  1214. }
  1215. //---------------------------------------------------------------------------
  1216. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1217. {
  1218. UnicodeString Result;
  1219. if (Item->Action != NULL)
  1220. {
  1221. Result = Item->Action->Name;
  1222. Result = RemoveSuffix(Result, L"Action");
  1223. }
  1224. else
  1225. {
  1226. Result = Item->Name;
  1227. Result = RemoveSuffix(Result, L"SubmenuItem");
  1228. Result = RemoveSuffix(Result, L"Item");
  1229. }
  1230. return Result;
  1231. }
  1232. //---------------------------------------------------------------------------
  1233. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1234. {
  1235. UnicodeString Result;
  1236. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1237. {
  1238. UnicodeString ToolbarStr;
  1239. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1240. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1241. {
  1242. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1243. {
  1244. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1245. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1246. // Once we introduce any buttons that aer hidden by default, we would have to remember their initial state
  1247. // and save the changes here only.
  1248. if (!Item->Visible)
  1249. {
  1250. UnicodeString Name = GetToolbarItemName(Item);
  1251. DebugAssert(Name.Pos(L"TBX") == 0);
  1252. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1253. }
  1254. }
  1255. }
  1256. if (!ToolbarStr.IsEmpty())
  1257. {
  1258. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1259. }
  1260. }
  1261. return Result;
  1262. }
  1263. //---------------------------------------------------------------------------
  1264. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1265. {
  1266. DebugAssert(FProgressForm == NULL);
  1267. bool AllowSkip = (Terminal != NULL) ? Terminal->IsCapable[fsSkipTransfer] : false;
  1268. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1269. FProgressForm->DeleteLocalToRecycleBin =
  1270. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1271. FProgressForm->DeleteRemoteToRecycleBin =
  1272. (Terminal != NULL) &&
  1273. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1274. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1275. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1276. LockWindow();
  1277. if (FTaskbarList != NULL)
  1278. {
  1279. // Actually, do not know what hides the progress once the operation finishes
  1280. SetTaskbarListProgressState(TBPF_NORMAL);
  1281. }
  1282. }
  1283. //---------------------------------------------------------------------------
  1284. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1285. {
  1286. UnlockWindow();
  1287. SAFE_DESTROY(FProgressForm);
  1288. SetQueueProgress();
  1289. }
  1290. //---------------------------------------------------------------------------
  1291. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1292. TFileOperationProgressType & ProgressData)
  1293. {
  1294. // Download to temporary local directory
  1295. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1296. {
  1297. bool Continue = true;
  1298. FPendingTempSpaceWarn = false;
  1299. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1300. if (!Continue)
  1301. {
  1302. Abort();
  1303. }
  1304. }
  1305. // operation is being executed and we still didn't show up progress form
  1306. if (ProgressData.InProgress && !FProgressForm)
  1307. {
  1308. CreateProgressForm(NULL);
  1309. // When main window is hidden, synchronisation form does not exist (we do not get here during synchronization - only comparing),
  1310. // we suppose "/upload" or URL download mode
  1311. if (!Visible && (ProgressData.Operation != foCalculateSize) &&
  1312. (ProgressData.Operation != foCalculateChecksum) &&
  1313. // Hack to prevent renames from DoSynchronizeMove (synchronize checklist) closing the connection
  1314. (ProgressData.Operation != foRemoteMove) &&
  1315. (FSynchronizeProgressForm == NULL) &&
  1316. !FStandaloneEditing)
  1317. {
  1318. FProgressForm->OnceDoneOperation = odoDisconnect;
  1319. }
  1320. }
  1321. // operation is finished (or terminated), so we hide progress form
  1322. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1323. {
  1324. DestroyProgressForm();
  1325. if (ProgressData.Operation == foCalculateSize)
  1326. {
  1327. // When calculating size before transfer, the abort caused by
  1328. // cancel flag set due to "MoveToQueue" below,
  1329. // is not propagated back to us in ExecuteFileOperation,
  1330. // as it is silently swallowed.
  1331. // So we explicitly (re)throw it here.
  1332. if (FMoveToQueue)
  1333. {
  1334. Abort();
  1335. }
  1336. }
  1337. else
  1338. {
  1339. if ((ProgressData.Cancel == csContinue) &&
  1340. !FAutoOperation)
  1341. {
  1342. OperationComplete(ProgressData.StartTime);
  1343. }
  1344. }
  1345. }
  1346. if (FProgressForm &&
  1347. // Can happen only during synchronization, when ending one of the operations
  1348. ProgressData.InProgress)
  1349. {
  1350. FProgressForm->SetProgressData(ProgressData);
  1351. if (FTaskbarList != NULL)
  1352. {
  1353. DebugAssert(ProgressData.InProgress);
  1354. SetTaskbarListProgressValue(&ProgressData);
  1355. }
  1356. if (FProgressForm->Cancel > csContinue)
  1357. {
  1358. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1359. if (FProgressForm->Cancel == csCancelFile)
  1360. {
  1361. FProgressForm->ClearCancel();
  1362. }
  1363. // cancel cancels even the move
  1364. FMoveToQueue = false;
  1365. }
  1366. else if (FProgressForm->MoveToQueue)
  1367. {
  1368. FMoveToQueue = true;
  1369. ProgressData.SetCancelAtLeast(csCancel);
  1370. }
  1371. if ((FTransferResumeList != NULL) &&
  1372. ProgressData.InProgress &&
  1373. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1374. !ProgressData.FullFileName.IsEmpty())
  1375. {
  1376. if ((FTransferResumeList->Count == 0) ||
  1377. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1378. {
  1379. // note that we do not recognize directories from files here
  1380. FTransferResumeList->Add(ProgressData.FullFileName);
  1381. }
  1382. }
  1383. }
  1384. }
  1385. //---------------------------------------------------------------------------
  1386. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1387. const TDateTime & StartTime)
  1388. {
  1389. if (GUIConfiguration->BeepOnFinish &&
  1390. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1391. {
  1392. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1393. DWORD Sound;
  1394. if (!ExtractFileExt(BeepSound).IsEmpty())
  1395. {
  1396. Sound = SND_FILENAME;
  1397. }
  1398. else
  1399. {
  1400. Sound = SND_ALIAS;
  1401. }
  1402. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1403. }
  1404. }
  1405. //---------------------------------------------------------------------------
  1406. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1407. TFileOperationProgressType & ProgressData)
  1408. {
  1409. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1410. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1411. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1412. FileOperationProgress(ProgressData);
  1413. }
  1414. //---------------------------------------------------------------------------
  1415. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1416. {
  1417. UnicodeString Result;
  1418. if (FProgressForm != NULL)
  1419. {
  1420. Result = FProgressForm->ProgressStr();
  1421. }
  1422. return Result;
  1423. }
  1424. //---------------------------------------------------------------------------
  1425. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1426. bool DragDrop)
  1427. {
  1428. return (!DragDrop && (DropSourceControl == NULL)) ||
  1429. (DropSourceControl == RemoteDirView);
  1430. }
  1431. //---------------------------------------------------------------------------
  1432. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1433. TFileOperation Operation, TOperationSide Side,
  1434. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1435. TOnceDoneOperation & OnceDoneOperation)
  1436. {
  1437. if (!FAutoOperation)
  1438. {
  1439. // no selection on "/upload", form servers only as event handler
  1440. // (it is not displayed)
  1441. if (PanelOperation(Side, FDragDropOperation) &&
  1442. Visible && (Operation != foCalculateSize) &&
  1443. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1444. {
  1445. if (FOnFileOperationFinished != NULL)
  1446. {
  1447. FOnFileOperationFinished(FileName, Success);
  1448. }
  1449. else
  1450. {
  1451. TCustomDirView * DView = DirView(Side);
  1452. UnicodeString FileNameOnly = ExtractFileName(FileName, (Side == osRemote));
  1453. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1454. // this can happen when local drive is unplugged in the middle of the operation
  1455. if (Item != NULL)
  1456. {
  1457. if (Success) Item->Selected = false;
  1458. DView->MakeProgressVisible(Item);
  1459. }
  1460. }
  1461. }
  1462. if ((Operation == foCopy) || (Operation == foMove))
  1463. {
  1464. if (Side == osLocal)
  1465. {
  1466. Configuration->Usage->Inc(L"UploadedFiles");
  1467. }
  1468. else
  1469. {
  1470. Configuration->Usage->Inc(L"DownloadedFiles");
  1471. }
  1472. }
  1473. }
  1474. if (Success && (FSynchronizeController != NULL))
  1475. {
  1476. if (Operation == foCopy)
  1477. {
  1478. DebugAssert(Side == osLocal);
  1479. FSynchronizeController->LogOperation(soUpload, FileName);
  1480. }
  1481. else if (Operation == foDelete)
  1482. {
  1483. DebugAssert(Side == osRemote);
  1484. FSynchronizeController->LogOperation(soDelete, FileName);
  1485. }
  1486. }
  1487. if (FProgressForm)
  1488. {
  1489. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1490. }
  1491. }
  1492. //---------------------------------------------------------------------------
  1493. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1494. TFileOperation Operation, TOperationSide Side,
  1495. bool Temp, const UnicodeString & FileName, Boolean Success,
  1496. TOnceDoneOperation & OnceDoneOperation)
  1497. {
  1498. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1499. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1500. OnceDoneOperation);
  1501. }
  1502. //---------------------------------------------------------------------------
  1503. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1504. {
  1505. DebugAssert(GetSide(Side) == osRemote);
  1506. DebugUsedParam(Side);
  1507. return RemoteDirView;
  1508. }
  1509. //---------------------------------------------------------------------------
  1510. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1511. {
  1512. DebugAssert(GetSide(Side) == osRemote);
  1513. DebugUsedParam(Side);
  1514. return (Terminal != NULL);
  1515. }
  1516. //---------------------------------------------------------------------------
  1517. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1518. TOperationSide Side, int FilesOnly)
  1519. {
  1520. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1521. }
  1522. //---------------------------------------------------------------------------
  1523. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1524. TOperationSide Side, int FilesOnly)
  1525. {
  1526. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1527. }
  1528. //---------------------------------------------------------------------------
  1529. struct THistoryItemData
  1530. {
  1531. short int Side;
  1532. short int Index;
  1533. };
  1534. //---------------------------------------------------------------------------
  1535. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1536. {
  1537. DirView(Side)->HistoryGo(Index);
  1538. }
  1539. //---------------------------------------------------------------------------
  1540. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1541. {
  1542. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1543. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1544. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1545. }
  1546. //---------------------------------------------------------------------------
  1547. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1548. bool Back)
  1549. {
  1550. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1551. {
  1552. TCustomDirView * DView = DirView(Side);
  1553. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1554. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1555. if (ICount > 10)
  1556. {
  1557. ICount = 10;
  1558. }
  1559. Menu->Items->Clear();
  1560. THistoryItemData Data;
  1561. Data.Side = Side;
  1562. for (short int i = 1; i <= ICount; i++)
  1563. {
  1564. TTBCustomItem * Item = new TTBXItem(Menu);
  1565. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1566. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, (Side == osRemote));
  1567. Item->Hint = DView->HistoryPath[Data.Index];
  1568. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1569. Item->Tag = *reinterpret_cast<int*>(&Data);
  1570. Item->OnClick = HistoryItemClick;
  1571. Menu->Items->Add(Item);
  1572. }
  1573. }
  1574. }
  1575. //---------------------------------------------------------------------------
  1576. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1577. TOperationSide Side, bool Back)
  1578. {
  1579. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1580. {
  1581. // workaround
  1582. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1583. // operator new allocates memory in Pascal code, but calls inline
  1584. // contructor in C++, leading in problems, the function does
  1585. // both in Pascal code
  1586. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1587. UpdateHistoryMenu(Side, Back);
  1588. }
  1589. return FHistoryMenu[Side == osLocal][Back];
  1590. }
  1591. //---------------------------------------------------------------------------
  1592. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1593. TCustomDirView *Sender)
  1594. {
  1595. TOperationSide Side = (Sender == DirView(osRemote) ? osRemote : osLocal);
  1596. UpdateHistoryMenu(Side, true);
  1597. UpdateHistoryMenu(Side, false);
  1598. }
  1599. //---------------------------------------------------------------------------
  1600. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1601. {
  1602. // remote custom commands can be executed only if the server supports shell commands
  1603. // or have secondary shell
  1604. return (FTerminal != NULL) && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1605. }
  1606. //---------------------------------------------------------------------------
  1607. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1608. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1609. {
  1610. int Result;
  1611. TFileCustomCommand RemoteCustomCommand;
  1612. TLocalCustomCommand LocalCustomCommand;
  1613. TFileCustomCommand * NonInteractiveCustomCommand =
  1614. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1615. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1616. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1617. if (FLAGCLEAR(Command.Params, ccLocal))
  1618. {
  1619. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1620. // custom command that does not operate with files can be executed anytime ...
  1621. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1622. {
  1623. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1624. {
  1625. // ... but do not show such command in remote file menu (TODO)
  1626. Result = AllowedState;
  1627. }
  1628. else
  1629. {
  1630. Result = -1;
  1631. }
  1632. }
  1633. else
  1634. {
  1635. if ((ListType == ccltAll) || (ListType == ccltFile))
  1636. {
  1637. Result = ((FCurrentSide == osRemote) && DirView(osRemote)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1638. }
  1639. else
  1640. {
  1641. Result = -1;
  1642. }
  1643. }
  1644. }
  1645. else
  1646. {
  1647. // custom command that does not operate with files can be executed anytime
  1648. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1649. {
  1650. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1651. }
  1652. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1653. {
  1654. if ((ListType == ccltAll) || (ListType == ccltFile))
  1655. {
  1656. // special case is "diff"-style command that can be executed over any side,
  1657. // if we have both sides
  1658. Result =
  1659. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1660. // directly (instead of EnableSelectedOperation) to pass
  1661. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1662. ((HasDirView[osLocal] && DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1663. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1664. }
  1665. else if (ListType == ccltBoth)
  1666. {
  1667. Result = 1;
  1668. }
  1669. else
  1670. {
  1671. Result = -1;
  1672. }
  1673. }
  1674. else
  1675. {
  1676. if ((ListType == ccltAll) || (ListType == ccltFile))
  1677. {
  1678. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1679. }
  1680. else
  1681. {
  1682. Result = -1;
  1683. }
  1684. }
  1685. }
  1686. return Result;
  1687. }
  1688. //---------------------------------------------------------------------------
  1689. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1690. TStrings * FileList, const TCustomCommandType & ACommand,
  1691. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1692. {
  1693. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1694. {
  1695. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1696. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1697. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1698. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1699. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1700. bool Capture =
  1701. FLAGSET(ACommand.Params, ccShowResults) ||
  1702. FLAGSET(ACommand.Params, ccCopyResults) ||
  1703. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1704. TCaptureOutputEvent OutputEvent = NULL;
  1705. DebugAssert(FCapturedLog == NULL);
  1706. if (Capture)
  1707. {
  1708. FCapturedLog = new TStringList();
  1709. OutputEvent = TerminalCaptureLog;
  1710. }
  1711. try
  1712. {
  1713. if (!RemoteCustomCommand.IsFileCommand(Command))
  1714. {
  1715. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1716. OutputEvent);
  1717. }
  1718. else
  1719. {
  1720. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1721. }
  1722. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1723. {
  1724. if (FLAGSET(ACommand.Params, ccCopyResults))
  1725. {
  1726. CopyToClipboard(FCapturedLog);
  1727. }
  1728. if (FLAGSET(ACommand.Params, ccShowResults))
  1729. {
  1730. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1731. }
  1732. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1733. {
  1734. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1735. }
  1736. }
  1737. }
  1738. __finally
  1739. {
  1740. SAFE_DESTROY(FCapturedLog);
  1741. }
  1742. }
  1743. }
  1744. //---------------------------------------------------------------------------
  1745. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1746. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1747. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1748. {
  1749. TStrings * LocalFileList = NULL;
  1750. TStrings * RemoteFileList = NULL;
  1751. TStrings * RemoteFileListFull = NULL;
  1752. try
  1753. {
  1754. if (LocalFileCommand)
  1755. {
  1756. if (ALocalFileList == NULL)
  1757. {
  1758. DebugAssert(HasDirView[osLocal]);
  1759. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1760. // directly (instead of EnableSelectedOperation) to pass
  1761. // false to FocusedFileOnlyWhenFocused
  1762. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1763. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1764. }
  1765. else
  1766. {
  1767. LocalFileList = ALocalFileList;
  1768. }
  1769. if (FileListCommand)
  1770. {
  1771. if (LocalFileList->Count != 1)
  1772. {
  1773. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1774. }
  1775. }
  1776. else
  1777. {
  1778. if ((LocalFileList->Count != 1) &&
  1779. (FileList->Count != 1) &&
  1780. (LocalFileList->Count != FileList->Count))
  1781. {
  1782. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1783. }
  1784. }
  1785. }
  1786. UnicodeString RootTempDir;
  1787. UnicodeString TempDir;
  1788. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1789. if (!RemoteFiles)
  1790. {
  1791. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1792. }
  1793. try
  1794. {
  1795. TDateTimes RemoteFileTimes;
  1796. if (RemoteFiles)
  1797. {
  1798. RemoteFileList = FileList;
  1799. }
  1800. else
  1801. {
  1802. RemoteFileList = new TStringList();
  1803. TMakeLocalFileListParams MakeFileListParam;
  1804. MakeFileListParam.FileList = RemoteFileList;
  1805. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1806. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1807. MakeFileListParam.Recursive =
  1808. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1809. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1810. if (!MakeFileListParam.Recursive)
  1811. {
  1812. RemoteFileTimes.clear();
  1813. RemoteFileListFull = new TStringList();
  1814. MakeFileListParam.FileList = RemoteFileListFull;
  1815. MakeFileListParam.Recursive = true;
  1816. MakeFileListParam.IncludeDirs = false;
  1817. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1818. }
  1819. else
  1820. {
  1821. RemoteFileListFull = RemoteFileList;
  1822. }
  1823. }
  1824. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1825. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1826. if (!NonBlocking)
  1827. {
  1828. int Count;
  1829. if (FileListCommand)
  1830. {
  1831. Count = 1;
  1832. }
  1833. else if (LocalFileCommand)
  1834. {
  1835. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1836. }
  1837. else
  1838. {
  1839. Count = RemoteFileList->Count;
  1840. }
  1841. Progress.Start(foCustomCommand, osRemote, Count);
  1842. DebugAssert(FProgressForm != NULL);
  1843. FProgressForm->ReadOnly = true;
  1844. }
  1845. try
  1846. {
  1847. if (FileListCommand)
  1848. {
  1849. UnicodeString LocalFile;
  1850. // MakeFileList does not delimit filenames
  1851. UnicodeString FileList = MakeFileList(RemoteFileList);
  1852. if (LocalFileCommand)
  1853. {
  1854. DebugAssert(LocalFileList->Count == 1);
  1855. LocalFile = LocalFileList->Strings[0];
  1856. }
  1857. TLocalCustomCommand CustomCommand(Data,
  1858. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1859. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1860. if (NonBlocking)
  1861. {
  1862. DebugAssert(!POutput);
  1863. ExecuteShellChecked(ShellCommand);
  1864. }
  1865. else
  1866. {
  1867. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1868. }
  1869. }
  1870. else if (LocalFileCommand)
  1871. {
  1872. if (LocalFileList->Count == 1)
  1873. {
  1874. UnicodeString LocalFile = LocalFileList->Strings[0];
  1875. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1876. {
  1877. UnicodeString FileName = RemoteFileList->Strings[Index];
  1878. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1879. Progress.SetFile(FileNameForProgress);
  1880. TLocalCustomCommand CustomCommand(Data,
  1881. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1882. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1883. TOnceDoneOperation OnceDoneOperation;
  1884. // Do not unselect anything, as it won't work with two panels anyway
  1885. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1886. }
  1887. }
  1888. else if (RemoteFileList->Count == 1)
  1889. {
  1890. UnicodeString FileName = RemoteFileList->Strings[0];
  1891. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1892. {
  1893. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1894. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1895. Progress.SetFile(LocalFileNameOnly);
  1896. TLocalCustomCommand CustomCommand(
  1897. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1898. FileName, LocalFileName, L"");
  1899. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1900. TOnceDoneOperation OnceDoneOperation;
  1901. // Do not unselect anything, as it won't work with two panels anyway
  1902. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1903. }
  1904. }
  1905. else
  1906. {
  1907. if (LocalFileList->Count != RemoteFileList->Count)
  1908. {
  1909. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1910. }
  1911. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1912. {
  1913. UnicodeString FileName = RemoteFileList->Strings[Index];
  1914. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1915. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1916. Progress.SetFile(LocalFileNameOnly); // sic
  1917. TLocalCustomCommand CustomCommand(
  1918. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1919. FileName, LocalFileName, L"");
  1920. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1921. TOnceDoneOperation OnceDoneOperation;
  1922. // Do not unselect anything, as it won't work with two panels anyway
  1923. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1924. }
  1925. }
  1926. }
  1927. else
  1928. {
  1929. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1930. {
  1931. UnicodeString FileName = RemoteFileList->Strings[Index];
  1932. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1933. Progress.SetFile(FileNameForProgress);
  1934. TLocalCustomCommand CustomCommand(Data,
  1935. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1936. FileName, L"", L"");
  1937. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1938. TOnceDoneOperation OnceDoneOperation;
  1939. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  1940. }
  1941. }
  1942. }
  1943. __finally
  1944. {
  1945. if (!NonBlocking)
  1946. {
  1947. Progress.Stop();
  1948. }
  1949. }
  1950. DebugAssert(!FAutoOperation);
  1951. if (!RemoteFiles)
  1952. {
  1953. TempDir = IncludeTrailingBackslash(TempDir);
  1954. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1955. {
  1956. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1957. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1958. // Skip directories, process only nested files.
  1959. // The check is redundant as FileAge fails for directories anyway.
  1960. !DirectoryExists(FileName))
  1961. {
  1962. UnicodeString RemoteDir =
  1963. UnixExtractFileDir(
  1964. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  1965. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  1966. TDateTime NewTime;
  1967. if (FileAge(FileName, NewTime) &&
  1968. (NewTime != RemoteFileTimes[Index]))
  1969. {
  1970. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  1971. TemporaryFileCopyParam(CopyParam);
  1972. CopyParam.FileMask = L"";
  1973. FAutoOperation = true;
  1974. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  1975. TemporaryFilesList->Add(FileName);
  1976. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  1977. }
  1978. }
  1979. }
  1980. }
  1981. }
  1982. __finally
  1983. {
  1984. FAutoOperation = false;
  1985. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  1986. {
  1987. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  1988. }
  1989. }
  1990. }
  1991. __finally
  1992. {
  1993. if (RemoteFileList != FileList)
  1994. {
  1995. delete RemoteFileList;
  1996. }
  1997. if (LocalFileList != ALocalFileList)
  1998. {
  1999. delete LocalFileList;
  2000. }
  2001. if (RemoteFileListFull != RemoteFileList)
  2002. {
  2003. delete RemoteFileListFull;
  2004. }
  2005. }
  2006. }
  2007. //---------------------------------------------------------------------------
  2008. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2009. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2010. bool FileListCommand, UnicodeString * POutput)
  2011. {
  2012. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  2013. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2014. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2015. {
  2016. UnicodeString FileName = SelectedFileList->Strings[Index];
  2017. if (DirectoryExists(FileName))
  2018. {
  2019. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2020. {
  2021. LocalFileList->Add(FileName);
  2022. }
  2023. if (FLAGSET(ACommand.Params, ccRecursive))
  2024. {
  2025. TMakeLocalFileListParams MakeFileListParam;
  2026. MakeFileListParam.FileList = LocalFileList.get();
  2027. MakeFileListParam.FileTimes = NULL;
  2028. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2029. MakeFileListParam.Recursive = true;
  2030. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2031. }
  2032. }
  2033. else
  2034. {
  2035. LocalFileList->Add(FileName);
  2036. }
  2037. }
  2038. UnicodeString RemotePath;
  2039. if (Terminal != NULL)
  2040. {
  2041. RemotePath = Terminal->CurrentDirectory;
  2042. }
  2043. if (FileListCommand)
  2044. {
  2045. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2046. TLocalCustomCommand CustomCommand(
  2047. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2048. L"", L"", FileList);
  2049. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2050. }
  2051. else
  2052. {
  2053. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2054. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2055. DebugAssert(FProgressForm != NULL);
  2056. FProgressForm->ReadOnly = true;
  2057. try
  2058. {
  2059. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2060. {
  2061. UnicodeString FileName = LocalFileList->Strings[Index];
  2062. Progress.SetFile(FileName);
  2063. TLocalCustomCommand CustomCommand(
  2064. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2065. FileName, L"", L"");
  2066. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2067. TOnceDoneOperation OnceDoneOperation;
  2068. Progress.Finish(FileName, true, OnceDoneOperation);
  2069. }
  2070. }
  2071. __finally
  2072. {
  2073. Progress.Stop();
  2074. }
  2075. }
  2076. }
  2077. //---------------------------------------------------------------------------
  2078. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2079. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2080. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2081. {
  2082. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2083. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2084. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2085. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2086. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2087. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2088. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2089. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2090. std::unique_ptr<UnicodeString> POutput;
  2091. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2092. FLAGSET(ACommand.Params, ccCopyResults))
  2093. {
  2094. POutput.reset(new UnicodeString());
  2095. }
  2096. if (!LocalCustomCommand.IsFileCommand(Command))
  2097. {
  2098. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2099. }
  2100. // remote files?
  2101. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2102. {
  2103. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2104. }
  2105. // local files
  2106. else
  2107. {
  2108. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2109. }
  2110. if (POutput.get() != NULL)
  2111. {
  2112. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2113. int P = POutput->Pos(sLineBreak);
  2114. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2115. {
  2116. POutput->SetLength(P - 1);
  2117. }
  2118. // Copy even empty output to clipboard
  2119. if (FLAGSET(ACommand.Params, ccCopyResults))
  2120. {
  2121. CopyToClipboard(*POutput);
  2122. }
  2123. // But do not show an empty message box.
  2124. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2125. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2126. !POutput->IsEmpty())
  2127. {
  2128. TClipboardHandler ClipboardHandler;
  2129. ClipboardHandler.Text = *POutput;
  2130. TMessageParams Params;
  2131. TQueryButtonAlias Aliases[1];
  2132. Aliases[0].Button = qaRetry;
  2133. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2134. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2135. Params.Aliases = Aliases;
  2136. Params.AliasesCount = LENOF(Aliases);
  2137. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2138. }
  2139. }
  2140. }
  2141. //---------------------------------------------------------------------------
  2142. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2143. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2144. {
  2145. TCustomCommandData Data;
  2146. UnicodeString Site;
  2147. UnicodeString RemotePath;
  2148. if (Terminal != NULL)
  2149. {
  2150. Data = TCustomCommandData(Terminal);
  2151. Site = Terminal->SessionData->SessionKey;
  2152. RemotePath = Terminal->CurrentDirectory;
  2153. }
  2154. UnicodeString HelpKeyword = ACommand.HomePage;
  2155. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2156. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2157. {
  2158. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2159. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2160. {
  2161. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2162. }
  2163. else
  2164. {
  2165. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2166. }
  2167. if (!DoCustomCommandOptionsDialog(
  2168. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2169. {
  2170. Abort();
  2171. }
  2172. }
  2173. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2174. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2175. {
  2176. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2177. }
  2178. else
  2179. {
  2180. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2181. }
  2182. }
  2183. //---------------------------------------------------------------------------
  2184. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2185. const TCustomCommandType & Command)
  2186. {
  2187. DebugAssert(FCustomCommandLocalFileList != NULL);
  2188. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2189. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2190. TStrings * LocalFileList = new TStringList();
  2191. TStrings * RemoteFileList = new TStringList();
  2192. try
  2193. {
  2194. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2195. {
  2196. LocalFileList->Clear();
  2197. LocalFileList->AddObject(
  2198. FCustomCommandLocalFileList->Strings[Index],
  2199. FCustomCommandLocalFileList->Objects[Index]);
  2200. RemoteFileList->Clear();
  2201. RemoteFileList->AddObject(
  2202. FCustomCommandRemoteFileList->Strings[Index],
  2203. FCustomCommandRemoteFileList->Objects[Index]);
  2204. CustomCommand(RemoteFileList, Command, LocalFileList);
  2205. }
  2206. }
  2207. __finally
  2208. {
  2209. delete LocalFileList;
  2210. delete RemoteFileList;
  2211. }
  2212. }
  2213. //---------------------------------------------------------------------------
  2214. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2215. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2216. {
  2217. delete FCustomCommandLocalFileList;
  2218. delete FCustomCommandRemoteFileList;
  2219. // takeover ownership,
  2220. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2221. FCustomCommandLocalFileList = LocalFileList;
  2222. FCustomCommandRemoteFileList = RemoteFileList;
  2223. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2224. if (Button != NULL)
  2225. {
  2226. FCustomCommandMenu->Items->Clear();
  2227. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
  2228. MenuPopup(FCustomCommandMenu, Button);
  2229. }
  2230. else
  2231. {
  2232. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2233. }
  2234. }
  2235. //---------------------------------------------------------------------------
  2236. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2237. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2238. {
  2239. DebugAssert(FCapturedLog != NULL);
  2240. if ((OutputType == cotOutput) || (OutputType == cotError))
  2241. {
  2242. FCapturedLog->Add(AddedLine);
  2243. }
  2244. }
  2245. //---------------------------------------------------------------------------
  2246. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2247. TOperationSide Side)
  2248. {
  2249. return (Side == osRemote) &&
  2250. ((Control == RemoteDirView) || (Control == RemoteDriveView));
  2251. }
  2252. //---------------------------------------------------------------------------
  2253. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2254. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2255. {
  2256. TTBItemOptions O;
  2257. O = Item->Options;
  2258. TCustomDirView * DView = DirView(Side);
  2259. if ((DView->ItemFocused != NULL) &&
  2260. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2261. // when resolving links is disabled, default action is to enter the directory,
  2262. // no matter what DoubleClickAction is configured to
  2263. ((Side != osRemote) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2264. // Can only Edit files, but can Open/Copy even directories
  2265. ((DoubleClickAction != dcaEdit) ||
  2266. !DView->ItemIsDirectory(DView->ItemFocused)))
  2267. {
  2268. Item->Options = O << tboDefault;
  2269. }
  2270. else
  2271. {
  2272. Item->Options = O >> tboDefault;
  2273. }
  2274. }
  2275. //---------------------------------------------------------------------------
  2276. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2277. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2278. {
  2279. TCustomDirView * DView = DirView(Side);
  2280. TListItem * Item = DView->ItemFocused;
  2281. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2282. EnableFocusedOperation[Side])
  2283. {
  2284. TPoint ClientPoint;
  2285. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2286. TPoint(0, 0) : MousePos;
  2287. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2288. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2289. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2290. }
  2291. }
  2292. //---------------------------------------------------------------------------
  2293. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2294. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2295. {
  2296. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2297. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2298. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2299. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2300. Handled = true;
  2301. }
  2302. //---------------------------------------------------------------------------
  2303. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2304. {
  2305. }
  2306. //---------------------------------------------------------------------------
  2307. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2308. {
  2309. DebugAssert(FErrorList == NULL);
  2310. if (WinConfiguration->ContinueOnError)
  2311. {
  2312. FErrorList = new TStringList();
  2313. Configuration->Usage->Inc(L"ContinuationsOnError");
  2314. }
  2315. NonVisualDataModule->StartBusy();
  2316. }
  2317. //---------------------------------------------------------------------------
  2318. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2319. {
  2320. NonVisualDataModule->EndBusy();
  2321. if (FErrorList)
  2322. {
  2323. HandleErrorList(FErrorList);
  2324. }
  2325. }
  2326. //---------------------------------------------------------------------------
  2327. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2328. const TCopyParamType & CopyParam)
  2329. {
  2330. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2331. {
  2332. Configuration->Usage->Inc(L"FileMaskUses");
  2333. }
  2334. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2335. {
  2336. Configuration->Usage->Inc(L"OperationMaskUses");
  2337. }
  2338. }
  2339. //---------------------------------------------------------------------------
  2340. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2341. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2342. {
  2343. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2344. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2345. TTransferOperationParam DefaultParam;
  2346. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2347. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2348. switch (Param.Queue)
  2349. {
  2350. case asOn:
  2351. CopyParam.Queue = true;
  2352. break;
  2353. case asOff:
  2354. CopyParam.Queue = false;
  2355. break;
  2356. case asAuto:
  2357. default:
  2358. // keep default
  2359. break;
  2360. }
  2361. bool Result =
  2362. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2363. if (Result)
  2364. {
  2365. DebugAssert(Terminal);
  2366. bool SelectionRestored = false;
  2367. TCustomDirView * DView = NULL;
  2368. if (HasDirView[Side])
  2369. {
  2370. DView = DirView(Side);
  2371. DView->SaveSelection();
  2372. DView->SaveSelectedNames();
  2373. }
  2374. UpdateCopyParamCounters(CopyParam);
  2375. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2376. DebugAssert(FTransferResumeList == NULL);
  2377. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2378. FMoveToQueue = false;
  2379. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2380. try
  2381. {
  2382. TStrings * PermanentFileList;
  2383. std::unique_ptr<TStrings> PermanentFileListOwner;
  2384. try
  2385. {
  2386. if (Side == osLocal)
  2387. {
  2388. PermanentFileList = FileList;
  2389. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2390. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2391. if (Operation == foMove)
  2392. {
  2393. ReloadLocalDirectory();
  2394. if (DView != NULL)
  2395. {
  2396. DView->RestoreSelection();
  2397. }
  2398. SelectionRestored = true;
  2399. }
  2400. }
  2401. else
  2402. {
  2403. // Clone the file list as it may refer to current directory files,
  2404. // which get destroyed, when the source directory is reloaded after foMove operation.
  2405. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2406. // But for a hotfix, we are not going to do such a big change.
  2407. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2408. PermanentFileList = PermanentFileListOwner.get();
  2409. try
  2410. {
  2411. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2412. }
  2413. __finally
  2414. {
  2415. if (Operation == foMove)
  2416. {
  2417. if (DView != NULL)
  2418. {
  2419. DView->RestoreSelection();
  2420. }
  2421. SelectionRestored = true;
  2422. }
  2423. ReloadLocalDirectory(Param.TargetDirectory);
  2424. }
  2425. }
  2426. }
  2427. catch (EAbort &)
  2428. {
  2429. if (FMoveToQueue)
  2430. {
  2431. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2432. DebugAssert(CopyParam.TransferSkipList == NULL);
  2433. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2434. if (TransferResumeList->Count > 0)
  2435. {
  2436. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2437. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2438. }
  2439. CopyParam.TransferSkipList = TransferResumeList.release();
  2440. // not really needed, just to keep it consistent with TransferResumeList
  2441. FTransferResumeList = NULL;
  2442. FMoveToQueue = false;
  2443. Configuration->Usage->Inc("MovesToBackground");
  2444. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2445. ClearTransferSourceSelection(Direction);
  2446. }
  2447. throw;
  2448. }
  2449. }
  2450. __finally
  2451. {
  2452. if (!SelectionRestored && (DView != NULL))
  2453. {
  2454. DView->DiscardSavedSelection();
  2455. }
  2456. FTransferResumeList = NULL;
  2457. }
  2458. }
  2459. return Result;
  2460. }
  2461. //---------------------------------------------------------------------------
  2462. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2463. TOperationSide Side, TStrings * FileList, void * Param)
  2464. {
  2465. DebugAssert(FileList->Count);
  2466. // We deliberately do not toggle alternative flag (Param), but use OR,
  2467. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2468. // shortcut of CurrentDeleteAlternativeAction
  2469. bool Alternative =
  2470. bool(Param) || UseAlternativeFunction();
  2471. bool Recycle;
  2472. if (Side == osLocal)
  2473. {
  2474. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2475. }
  2476. else
  2477. {
  2478. Recycle =
  2479. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2480. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2481. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2482. }
  2483. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2484. if (!Result)
  2485. {
  2486. UnicodeString Query;
  2487. if (FileList->Count == 1)
  2488. {
  2489. if (Side == osLocal)
  2490. {
  2491. Query = ExtractFileName(FileList->Strings[0]);
  2492. }
  2493. else
  2494. {
  2495. Query = UnixExtractFileName(FileList->Strings[0]);
  2496. }
  2497. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2498. }
  2499. else
  2500. {
  2501. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2502. }
  2503. TMessageParams Params(mpNeverAskAgainCheck);
  2504. Params.ImageName = L"Delete file";
  2505. unsigned int Answer =
  2506. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2507. if (Answer == qaNeverAskAgain)
  2508. {
  2509. Result = true;
  2510. if (Recycle)
  2511. {
  2512. WinConfiguration->ConfirmRecycling = false;
  2513. }
  2514. else
  2515. {
  2516. WinConfiguration->ConfirmDeleting = false;
  2517. }
  2518. }
  2519. else
  2520. {
  2521. Result = (Answer == qaOK);
  2522. }
  2523. }
  2524. if (Result)
  2525. {
  2526. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2527. }
  2528. return Result;
  2529. }
  2530. //---------------------------------------------------------------------------
  2531. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2532. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2533. {
  2534. TAutoBatch AutoBatch(this);
  2535. bool Result;
  2536. if ((Operation == foCopy) || (Operation == foMove))
  2537. {
  2538. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2539. }
  2540. else if (Operation == foRename)
  2541. {
  2542. DebugAssert(DirView(Side)->ItemFocused);
  2543. DirView(Side)->ItemFocused->EditCaption();
  2544. Result = true;
  2545. }
  2546. else if (Operation == foDelete)
  2547. {
  2548. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2549. }
  2550. else if (Operation == foSetProperties)
  2551. {
  2552. RemoteDirView->SaveSelectedNames();
  2553. Result = SetProperties(Side, FileList);
  2554. }
  2555. else if (Operation == foCustomCommand)
  2556. {
  2557. DebugAssert(Param);
  2558. DebugAssert(Side == osRemote);
  2559. RemoteDirView->SaveSelectedNames();
  2560. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2561. CustomCommand(FileList, *Command, NULL);
  2562. Result = true;
  2563. }
  2564. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2565. {
  2566. DebugAssert(Side == osRemote);
  2567. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2568. (Operation == foRemoteMove), reinterpret_cast<TTerminal *>(Param));
  2569. }
  2570. else if (Operation == foLock)
  2571. {
  2572. DebugAssert(Side == osRemote);
  2573. LockFiles(FileList, true);
  2574. Result = true;
  2575. }
  2576. else if (Operation == foUnlock)
  2577. {
  2578. DebugAssert(Side == osRemote);
  2579. LockFiles(FileList, false);
  2580. Result = true;
  2581. }
  2582. else
  2583. {
  2584. DebugFail();
  2585. }
  2586. return Result;
  2587. }
  2588. //---------------------------------------------------------------------------
  2589. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2590. {
  2591. if (Side == osCurrent)
  2592. {
  2593. Side = FCurrentSide;
  2594. }
  2595. return Side;
  2596. }
  2597. //---------------------------------------------------------------------------
  2598. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2599. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2600. {
  2601. Side = GetSide(Side);
  2602. bool Result;
  2603. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, (Side == osLocal), NULL);
  2604. try
  2605. {
  2606. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2607. }
  2608. __finally
  2609. {
  2610. delete FileList;
  2611. }
  2612. return Result;
  2613. }
  2614. //---------------------------------------------------------------------------
  2615. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2616. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2617. bool NoConfirmation, void * Param)
  2618. {
  2619. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2620. {
  2621. if ((Operation == foCopy) || (Operation == foMove))
  2622. {
  2623. if (GetSide(Side) == osLocal)
  2624. {
  2625. Configuration->Usage->Inc(L"UploadsCommand");
  2626. }
  2627. else
  2628. {
  2629. Configuration->Usage->Inc(L"DownloadsCommand");
  2630. }
  2631. }
  2632. }
  2633. }
  2634. //---------------------------------------------------------------------------
  2635. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2636. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2637. {
  2638. TTransferOperationParam Param;
  2639. if ((WinConfiguration->Interface != ifCommander) ||
  2640. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2641. {
  2642. Flags &= ~cocShortCutHint;
  2643. }
  2644. TCustomDirView * DView = DirView(Side);
  2645. Param.Options =
  2646. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2647. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2648. if (FLAGSET(Flags, cocQueue))
  2649. {
  2650. Param.Queue = asOn;
  2651. }
  2652. else if (FLAGSET(Flags, cocNonQueue))
  2653. {
  2654. Param.Queue = asOff;
  2655. }
  2656. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2657. }
  2658. //---------------------------------------------------------------------------
  2659. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2660. {
  2661. try
  2662. {
  2663. if (ErrorList->Count)
  2664. {
  2665. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2666. if (MessageDialog(Message, qtError,
  2667. qaOK | qaCancel, HELP_NONE) == qaOK)
  2668. {
  2669. unsigned int Answer;
  2670. int Index = 0;
  2671. do
  2672. {
  2673. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2674. TQueryButtonAlias Aliases[2];
  2675. Aliases[0].Button = qaYes;
  2676. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2677. Aliases[1].Button = qaNo;
  2678. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2679. TMessageParams Params;
  2680. Params.Aliases = Aliases;
  2681. Params.AliasesCount = LENOF(Aliases);
  2682. Answer = MoreMessageDialog(
  2683. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2684. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2685. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2686. qaOK, HELP_NONE, &Params);
  2687. if (Answer == qaNo)
  2688. {
  2689. Index++;
  2690. }
  2691. if (Answer == qaYes)
  2692. {
  2693. Index--;
  2694. }
  2695. }
  2696. while (Answer != qaOK);
  2697. }
  2698. }
  2699. }
  2700. __finally
  2701. {
  2702. TStrings * List = ErrorList;
  2703. ErrorList = NULL;
  2704. for (int i = 0; i < List->Count; i++)
  2705. {
  2706. delete List->Objects[i];
  2707. }
  2708. delete List;
  2709. }
  2710. }
  2711. //---------------------------------------------------------------------------
  2712. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2713. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2714. {
  2715. // needed for checking filemasks, as there's no directory object
  2716. // associated with the file object
  2717. File->FullFileName = FullFileName;
  2718. TFileMasks::TParams MaskParams;
  2719. MaskParams.Size = File->Size;
  2720. MaskParams.Modification = File->Modification;
  2721. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2722. }
  2723. //---------------------------------------------------------------------------
  2724. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2725. {
  2726. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2727. Side = GetSide(Side);
  2728. TCustomDirView * DView = DirView(Side);
  2729. TListItem * FocusedItem = DView->ItemFocused;
  2730. UnicodeString Name;
  2731. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2732. {
  2733. Name = DView->ItemFileName(FocusedItem);
  2734. }
  2735. else
  2736. {
  2737. Name = LoadStr(NEW_FILE);
  2738. }
  2739. UnicodeString Names = Name;
  2740. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2741. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2742. HELP_EDIT_NEW, History.get(), true))
  2743. {
  2744. while (!Names.IsEmpty())
  2745. {
  2746. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2747. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2748. UnicodeString TargetFileName;
  2749. UnicodeString LocalFileName;
  2750. UnicodeString RootTempDir;
  2751. UnicodeString TempDir;
  2752. UnicodeString RemoteDirectory;
  2753. bool ExistingFile = false;
  2754. if (Side == osRemote)
  2755. {
  2756. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2757. TRemoteFile * File = NULL;
  2758. if (FTerminal->FileExists(Name, &File) &&
  2759. !File->IsDirectory)
  2760. {
  2761. try
  2762. {
  2763. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2764. ExistingFile = true;
  2765. }
  2766. __finally
  2767. {
  2768. delete File;
  2769. }
  2770. }
  2771. if (!ExistingFile)
  2772. {
  2773. RemoteDirectory = UnixExtractFilePath(Name);
  2774. TemporaryDirectoryForRemoteFiles(
  2775. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2776. TargetFileName = UnixExtractFileName(Name);
  2777. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2778. LocalFileName = TempDir +
  2779. // We probably do not want to trim the VMS version here
  2780. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2781. }
  2782. }
  2783. else
  2784. {
  2785. if (ExtractFilePath(Name).IsEmpty())
  2786. {
  2787. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2788. }
  2789. else
  2790. {
  2791. LocalFileName = ExpandFileName(Name);
  2792. }
  2793. TargetFileName = ExtractFileName(Name);
  2794. }
  2795. if (!ExistingFile)
  2796. {
  2797. if (!FileExists(ApiPath(LocalFileName)))
  2798. {
  2799. int File = FileCreate(ApiPath(LocalFileName));
  2800. if (File < 0)
  2801. {
  2802. if (!RootTempDir.IsEmpty())
  2803. {
  2804. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2805. }
  2806. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2807. }
  2808. else
  2809. {
  2810. FileClose(File);
  2811. }
  2812. }
  2813. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2814. const TEditorData * ExternalEditor = NULL;
  2815. TFileMasks::TParams MaskParams; // size not known
  2816. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2817. false, MaskParams);
  2818. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2819. ExternalEditor, RootTempDir, RemoteDirectory);
  2820. }
  2821. }
  2822. }
  2823. }
  2824. //---------------------------------------------------------------------------
  2825. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2826. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2827. {
  2828. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2829. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2830. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2831. return
  2832. ((ExecuteFileBy == efInternalEditor)) ||
  2833. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2834. }
  2835. //---------------------------------------------------------------------------
  2836. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2837. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2838. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2839. UnicodeString RemoteDirectory)
  2840. {
  2841. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2842. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2843. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2844. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2845. Side = GetSide(Side);
  2846. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2847. if (Side == osRemote)
  2848. {
  2849. Data->Terminal = Terminal;
  2850. Data->Queue = Queue;
  2851. Data->SessionData = CloneCurrentSessionData();
  2852. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2853. Data->RemoteDirectory = RemoteDirectory;
  2854. Data->SessionName = Terminal->SessionData->SessionName;
  2855. Data->LocalRootDirectory = LocalRootDirectory;
  2856. Data->OriginalFileName = OriginalFileName;
  2857. Data->Command = L""; // will be changed later for external editor
  2858. }
  2859. if (ExecuteFileBy == efInternalEditor)
  2860. {
  2861. if (Side == osRemote)
  2862. {
  2863. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2864. L" - " + Terminal->SessionData->SessionName;
  2865. TForm * Editor;
  2866. try
  2867. {
  2868. TNotifyEvent OnFileChanged = NULL;
  2869. TNotifyEvent OnSaveAll = NULL;
  2870. TAnyModifiedEvent OnAnyModified = NULL;
  2871. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2872. if (Terminal->IsCapable[fsBackgroundTransfers])
  2873. {
  2874. OnFileChanged = FEditorManager->FileChanged;
  2875. OnSaveAll = SaveAllInternalEditors;
  2876. OnAnyModified = AnyInternalEditorModified;
  2877. }
  2878. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2879. FEditorManager->FileReload, FEditorManager->FileClosed,
  2880. OnSaveAll, OnAnyModified,
  2881. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding);
  2882. }
  2883. catch(...)
  2884. {
  2885. if (!LocalRootDirectory.IsEmpty())
  2886. {
  2887. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2888. }
  2889. throw;
  2890. }
  2891. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2892. }
  2893. else
  2894. {
  2895. DebugAssert(!FStandaloneEditing);
  2896. TForm * Editor =
  2897. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2898. SaveAllInternalEditors, AnyInternalEditorModified,
  2899. L"", false, SessionColor, -1);
  2900. FLocalEditors->Add(Editor);
  2901. }
  2902. }
  2903. else
  2904. {
  2905. HANDLE Process;
  2906. if (ExecuteFileBy == efExternalEditor)
  2907. {
  2908. UnicodeString Program, Params, Dir;
  2909. Data->Command = ExternalEditor->ExternalEditor;
  2910. ReformatFileNameCommand(Data->Command);
  2911. SplitCommand(Data->Command, Program, Params, Dir);
  2912. Params = ExpandFileNameCommand(Params, FileName);
  2913. Program = ExpandEnvironmentVariables(Program);
  2914. if (!ExecuteShell(Program, Params, Process))
  2915. {
  2916. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2917. }
  2918. }
  2919. else
  2920. {
  2921. DebugAssert(Side == osRemote);
  2922. if (!ExecuteShell(FileName, L"", Process))
  2923. {
  2924. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2925. }
  2926. }
  2927. if ((Side == osLocal) ||
  2928. ((ExecuteFileBy == efShell) &&
  2929. !WinConfiguration->Editor.SDIShellEditor) ||
  2930. ((ExecuteFileBy == efExternalEditor) &&
  2931. !ExternalEditor->SDIExternalEditor))
  2932. {
  2933. // no need for handle
  2934. if (Process != NULL)
  2935. {
  2936. DebugCheck(CloseHandle(Process));
  2937. }
  2938. Process = INVALID_HANDLE_VALUE;
  2939. }
  2940. else
  2941. {
  2942. if (Process == NULL)
  2943. {
  2944. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2945. }
  2946. }
  2947. if (Side == osRemote)
  2948. {
  2949. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2950. }
  2951. }
  2952. }
  2953. //---------------------------------------------------------------------------
  2954. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2955. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2956. void * /*Arg*/)
  2957. {
  2958. if (Token != NULL)
  2959. {
  2960. EditorFormFileSave(static_cast<TForm *>(Token));
  2961. }
  2962. }
  2963. //---------------------------------------------------------------------------
  2964. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  2965. {
  2966. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  2967. {
  2968. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  2969. }
  2970. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  2971. }
  2972. //---------------------------------------------------------------------------
  2973. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  2974. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2975. void * Arg)
  2976. {
  2977. if ((Token != NULL) &&
  2978. IsEditorFormModified(static_cast<TForm *>(Token)))
  2979. {
  2980. *static_cast<bool *>(Arg) = true;
  2981. }
  2982. }
  2983. //---------------------------------------------------------------------------
  2984. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  2985. TObject * /*Sender*/, bool & Modified)
  2986. {
  2987. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  2988. {
  2989. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  2990. {
  2991. Modified = true;
  2992. }
  2993. }
  2994. if (!Modified)
  2995. {
  2996. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  2997. }
  2998. }
  2999. //---------------------------------------------------------------------------
  3000. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3001. {
  3002. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3003. }
  3004. //---------------------------------------------------------------------------
  3005. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3006. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3007. UnicodeString & Result, UnicodeString & RootDirectory)
  3008. {
  3009. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3010. {
  3011. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3012. Result = RootDirectory;
  3013. }
  3014. else
  3015. {
  3016. RootDirectory = L"";
  3017. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3018. Result = IncludeTrailingBackslash(Result);
  3019. }
  3020. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3021. {
  3022. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3023. }
  3024. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3025. {
  3026. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3027. {
  3028. RemoteDirectory.Delete(1, 1);
  3029. }
  3030. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3031. }
  3032. if (!ForceDirectories(ApiPath(Result)))
  3033. {
  3034. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3035. }
  3036. }
  3037. //---------------------------------------------------------------------------
  3038. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3039. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3040. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3041. {
  3042. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3043. if (ForceText)
  3044. {
  3045. CopyParam.TransferMode = tmAscii;
  3046. }
  3047. TemporaryFileCopyParam(CopyParam);
  3048. if (AllFiles)
  3049. {
  3050. CopyParam.IncludeFileMask = TFileMasks();
  3051. }
  3052. if (TempDir.IsEmpty())
  3053. {
  3054. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3055. }
  3056. DebugAssert(!FAutoOperation);
  3057. FAutoOperation = AutoOperation;
  3058. Terminal->ExceptionOnFail = true;
  3059. try
  3060. {
  3061. try
  3062. {
  3063. // turn off confirmations, as for MDI editors we may possibly download
  3064. // the same file over
  3065. Terminal->CopyToLocal(
  3066. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3067. if (GetTargetNames)
  3068. {
  3069. for (int i = 0; i < FileList->Count; i++)
  3070. {
  3071. FileList->Strings[i] =
  3072. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3073. }
  3074. }
  3075. }
  3076. catch(...)
  3077. {
  3078. if (!RootTempDir.IsEmpty())
  3079. {
  3080. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3081. }
  3082. throw;
  3083. }
  3084. }
  3085. __finally
  3086. {
  3087. FAutoOperation = false;
  3088. Terminal->ExceptionOnFail = false;
  3089. }
  3090. }
  3091. //---------------------------------------------------------------------------
  3092. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3093. {
  3094. // Do not waste time checking for default editor list the next time,
  3095. // or testing if default editor is not notepad.
  3096. bool TryNextTime = false;
  3097. UnicodeString UsageState;
  3098. if (!WinConfiguration->EditorList->IsDefaultList())
  3099. {
  3100. UsageState = "H";
  3101. }
  3102. else
  3103. {
  3104. UnicodeString Executable;
  3105. UnicodeString ExecutableDescription;
  3106. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3107. {
  3108. UnicodeString Message =
  3109. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3110. unsigned int Answer =
  3111. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3112. if (Answer != qaOK)
  3113. {
  3114. UsageState = "R";
  3115. }
  3116. else
  3117. {
  3118. UsageState = "A";
  3119. TEditorData EditorData;
  3120. EditorData.Editor = edExternal;
  3121. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3122. EditorData.DecideExternalEditorText();
  3123. TEditorList EditorList;
  3124. EditorList = *WinConfiguration->EditorList;
  3125. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3126. WinConfiguration->EditorList = &EditorList;
  3127. }
  3128. }
  3129. }
  3130. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3131. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3132. }
  3133. //---------------------------------------------------------------------------
  3134. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3135. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3136. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3137. {
  3138. if (ExecuteFileBy == efDefaultEditor)
  3139. {
  3140. if (!WinConfiguration->OfferedEditorAutoConfig)
  3141. {
  3142. EditorAutoConfig();
  3143. }
  3144. const TEditorPreferences * Editor =
  3145. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3146. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3147. {
  3148. ExecuteFileBy = efInternalEditor;
  3149. ExternalEditor = NULL;
  3150. }
  3151. else if (Editor->Data->Editor == edOpen)
  3152. {
  3153. ExecuteFileBy = efShell;
  3154. ExternalEditor = NULL;
  3155. }
  3156. else
  3157. {
  3158. ExecuteFileBy = efExternalEditor;
  3159. ExternalEditor = Editor->Data;
  3160. }
  3161. }
  3162. }
  3163. //---------------------------------------------------------------------------
  3164. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3165. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3166. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3167. {
  3168. UnicodeString OriginalFileName;
  3169. UnicodeString LocalRootDirectory;
  3170. UnicodeString RemoteDirectory;
  3171. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3172. (Side == osLocal), MaskParams);
  3173. UnicodeString Counter;
  3174. UnicodeString LocalFileName;
  3175. bool Handled = false;
  3176. if (Side == osRemote)
  3177. {
  3178. // We need to trim VMS version here, so that we use name without version
  3179. // when uploading back to create a new version of the file
  3180. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3181. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3182. TObject * Token = NULL;
  3183. UnicodeString LocalDirectory;
  3184. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3185. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3186. LocalDirectory))
  3187. {
  3188. if (Token != NULL)
  3189. {
  3190. TForm * Form = dynamic_cast<TForm *>(Token);
  3191. if (Form->WindowState == wsMinimized)
  3192. {
  3193. ShowWindow(Form->Handle, SW_RESTORE);
  3194. }
  3195. else
  3196. {
  3197. Form->SetFocus();
  3198. }
  3199. Handled = true;
  3200. }
  3201. else
  3202. {
  3203. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3204. }
  3205. }
  3206. if (!Handled)
  3207. {
  3208. TStringList * FileList1 = new TStringList();
  3209. try
  3210. {
  3211. FileList1->AddObject(FullFileName, Object);
  3212. TemporarilyDownloadFiles(FileList1,
  3213. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3214. LocalRootDirectory, LocalDirectory, true, true, true);
  3215. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3216. }
  3217. __finally
  3218. {
  3219. delete FileList1;
  3220. }
  3221. switch (ExecuteFileBy)
  3222. {
  3223. case efShell:
  3224. Counter = "RemoteFilesExecuted";
  3225. break;
  3226. case efInternalEditor:
  3227. Counter = "RemoteFilesOpenedInInternalEditor";
  3228. break;
  3229. case efExternalEditor:
  3230. Counter = "RemoteFilesOpenedInExternalEditor";
  3231. break;
  3232. default:
  3233. DebugFail();
  3234. }
  3235. }
  3236. }
  3237. else
  3238. {
  3239. LocalFileName = FullFileName;
  3240. OriginalFileName = ExtractFileName(FullFileName);
  3241. switch (ExecuteFileBy)
  3242. {
  3243. case efShell:
  3244. Counter = "LocalFilesExecuted";
  3245. break;
  3246. case efInternalEditor:
  3247. Counter = "LocalFilesOpenedInInternalEditor";
  3248. break;
  3249. case efExternalEditor:
  3250. Counter = "LocalFilesOpenedInExternalEditor";
  3251. break;
  3252. default:
  3253. DebugFail();
  3254. }
  3255. }
  3256. if (!Handled)
  3257. {
  3258. Configuration->Usage->Inc(Counter);
  3259. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3260. ExternalEditor, LocalRootDirectory, RemoteDirectory);
  3261. }
  3262. }
  3263. //---------------------------------------------------------------------------
  3264. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3265. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3266. bool AllSelected, bool OnFocused)
  3267. {
  3268. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3269. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3270. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3271. Side = GetSide(Side);
  3272. TCustomDirView * DView = DirView(Side);
  3273. TStrings * FileList = AllSelected ?
  3274. DView->CreateFileList(OnFocused, Side == osLocal) :
  3275. DView->CreateFocusedFileList(Side == osLocal);
  3276. try
  3277. {
  3278. DebugAssert(AllSelected || (FileList->Count == 1));
  3279. for (int i = 0; i < FileList->Count; i++)
  3280. {
  3281. UnicodeString ListFileName = FileList->Strings[i];
  3282. UnicodeString FileNameOnly = (Side == osRemote) ?
  3283. UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3284. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3285. if (!DView->ItemIsDirectory(Item))
  3286. {
  3287. UnicodeString FullFileName;
  3288. if (Side == osRemote)
  3289. {
  3290. FullFileName = RemoteDirView->Path + ListFileName;
  3291. }
  3292. else
  3293. {
  3294. FullFileName = ListFileName;
  3295. }
  3296. TFileMasks::TParams MaskParams;
  3297. MaskParams.Size = DView->ItemFileSize(Item);
  3298. TDateTimePrecision Precision;
  3299. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3300. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3301. FileList->Objects[i], MaskParams);
  3302. }
  3303. }
  3304. }
  3305. __finally
  3306. {
  3307. delete FileList;
  3308. }
  3309. }
  3310. //---------------------------------------------------------------------------
  3311. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3312. {
  3313. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3314. CopyParam.FileNameCase = ncNoChange;
  3315. CopyParam.PreserveRights = false;
  3316. CopyParam.PreserveReadOnly = false;
  3317. CopyParam.ReplaceInvalidChars = true;
  3318. CopyParam.IncludeFileMask = TFileMasks();
  3319. CopyParam.NewerOnly = false;
  3320. CopyParam.FileMask = L"";
  3321. }
  3322. //---------------------------------------------------------------------------
  3323. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3324. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3325. {
  3326. TTerminalManager * Manager = TTerminalManager::Instance();
  3327. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3328. {
  3329. if (!NonVisualDataModule->Busy)
  3330. {
  3331. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3332. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3333. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3334. if (Data->Terminal == NULL)
  3335. {
  3336. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3337. if (SameSiteTerminal != NULL)
  3338. {
  3339. UnicodeString Message =
  3340. FMTLOAD(EDIT_SESSION_REATTACH,
  3341. (FileNameOnly, Data->SessionName, FileNameOnly));
  3342. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3343. {
  3344. Data->Terminal = Terminal;
  3345. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3346. Data->SessionName = Terminal->SessionData->SessionName;
  3347. // We might also overwrite session data
  3348. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3349. }
  3350. else
  3351. {
  3352. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3353. Abort();
  3354. }
  3355. }
  3356. }
  3357. // foreground session should reconnect itself
  3358. else if (Terminal != Data->Terminal)
  3359. {
  3360. UnicodeString Message =
  3361. MainInstructions(
  3362. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3363. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3364. {
  3365. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3366. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3367. }
  3368. else
  3369. {
  3370. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3371. Abort();
  3372. }
  3373. }
  3374. }
  3375. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3376. {
  3377. Configuration->Usage->Inc(L"EditInactiveSession");
  3378. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3379. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3380. (ExtractFileName(FileName), Data->SessionName)));
  3381. }
  3382. }
  3383. TStrings * FileList = new TStringList();
  3384. try
  3385. {
  3386. FileList->Add(FileName);
  3387. // consider using the same settings (preset) as when the file was downloaded
  3388. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3389. TemporaryFileCopyParam(CopyParam);
  3390. if (Data->ForceText)
  3391. {
  3392. CopyParam.TransferMode = tmAscii;
  3393. }
  3394. // so i do not need to worry if masking algorithm works in all cases
  3395. // ("" means "copy file name", no masking is actually done)
  3396. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3397. {
  3398. CopyParam.FileMask = L"";
  3399. }
  3400. else
  3401. {
  3402. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3403. }
  3404. DebugAssert(Data->Queue != NULL);
  3405. int Params = cpNoConfirmation | cpTemporary;
  3406. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3407. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3408. QueueItem->CompleteEvent = UploadCompleteEvent;
  3409. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3410. }
  3411. __finally
  3412. {
  3413. delete FileList;
  3414. }
  3415. }
  3416. //---------------------------------------------------------------------------
  3417. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3418. const UnicodeString FileName, const TEditedFileData * Data)
  3419. {
  3420. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3421. DebugAssert(!NonVisualDataModule->Busy);
  3422. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3423. {
  3424. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3425. (ExtractFileName(FileName), Data->SessionName)));
  3426. }
  3427. TTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3428. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3429. NonVisualDataModule->StartBusy();
  3430. try
  3431. {
  3432. std::unique_ptr<TRemoteFile> File;
  3433. UnicodeString RemoteFileName =
  3434. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3435. FTerminal->ExceptionOnFail = true;
  3436. try
  3437. {
  3438. TRemoteFile * AFile = NULL;
  3439. FTerminal->ReadFile(RemoteFileName, AFile);
  3440. File.reset(AFile);
  3441. if (!File->HaveFullFileName)
  3442. {
  3443. File->FullFileName = RemoteFileName;
  3444. }
  3445. }
  3446. __finally
  3447. {
  3448. FTerminal->ExceptionOnFail = false;
  3449. }
  3450. std::unique_ptr<TStrings> FileList(new TStringList());
  3451. FileList->AddObject(RemoteFileName, File.get());
  3452. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3453. UnicodeString TempDir = ExtractFilePath(FileName);
  3454. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3455. TempDir, true, true, true);
  3456. // sanity check, the target file name should be still the same
  3457. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3458. }
  3459. __finally
  3460. {
  3461. NonVisualDataModule->EndBusy();
  3462. // it actually may not exist anymore...
  3463. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3464. }
  3465. }
  3466. //---------------------------------------------------------------------------
  3467. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3468. const TEditedFileData * Data, bool & KeepOpen)
  3469. {
  3470. // Command is set for external editors only (not for "shell" open).
  3471. if (!Data->Command.IsEmpty())
  3472. {
  3473. bool AnyFound = false;
  3474. bool AnyMDI = false;
  3475. bool AnyNonMDI = false;
  3476. bool AnyDetect = false;
  3477. TEditorList * EditorList = new TEditorList();
  3478. try
  3479. {
  3480. *EditorList = *WinConfiguration->EditorList;
  3481. for (int i = 0; i < EditorList->Count; i++)
  3482. {
  3483. const TEditorPreferences * Editor = EditorList->Editors[i];
  3484. if ((Editor->Data->Editor == edExternal) &&
  3485. (Editor->Data->ExternalEditor == Data->Command))
  3486. {
  3487. AnyFound = true;
  3488. if (Editor->Data->SDIExternalEditor)
  3489. {
  3490. AnyNonMDI = true;
  3491. if (Editor->Data->DetectMDIExternalEditor)
  3492. {
  3493. AnyDetect = true;
  3494. }
  3495. }
  3496. else
  3497. {
  3498. AnyMDI = true;
  3499. }
  3500. }
  3501. }
  3502. bool EnableMDI = false;
  3503. bool DisableDetect = false;
  3504. if (AnyMDI)
  3505. {
  3506. KeepOpen = true;
  3507. if (AnyNonMDI)
  3508. {
  3509. // there is at least one instance of the editor with MDI support enabled,
  3510. // and one with disabled, enable it for all instances
  3511. EnableMDI = true;
  3512. }
  3513. }
  3514. else if (AnyFound && !AnyDetect)
  3515. {
  3516. // at least once instance found but all have MDI autodetection disabled
  3517. // => close the file (default action)
  3518. }
  3519. else
  3520. {
  3521. // no instance of the editor has MDI support enabled
  3522. TMessageParams Params;
  3523. if (AnyFound)
  3524. {
  3525. // there is at least one instance of the editor
  3526. Params.Params |= mpNeverAskAgainCheck;
  3527. }
  3528. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3529. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3530. switch (Answer)
  3531. {
  3532. case qaNeverAskAgain:
  3533. DisableDetect = true;
  3534. break;
  3535. case qaNo:
  3536. EnableMDI = true;
  3537. KeepOpen = true;
  3538. break;
  3539. }
  3540. }
  3541. if (AnyFound && (EnableMDI || DisableDetect))
  3542. {
  3543. bool Changed = false;
  3544. for (int i = 0; i < EditorList->Count; i++)
  3545. {
  3546. const TEditorPreferences * Editor = EditorList->Editors[i];
  3547. if ((Editor->Data->Editor == edExternal) &&
  3548. (Editor->Data->ExternalEditor == Data->Command) &&
  3549. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3550. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3551. {
  3552. Changed = true;
  3553. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3554. if (EnableMDI)
  3555. {
  3556. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3557. }
  3558. if (DisableDetect)
  3559. {
  3560. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3561. }
  3562. EditorList->Change(i, UpdatedEditor);
  3563. }
  3564. }
  3565. if (Changed)
  3566. {
  3567. WinConfiguration->EditorList = EditorList;
  3568. }
  3569. }
  3570. }
  3571. __finally
  3572. {
  3573. delete EditorList;
  3574. }
  3575. }
  3576. else
  3577. {
  3578. // "open" case
  3579. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3580. qaOK, HELP_APP_EARLY_CLOSED);
  3581. }
  3582. }
  3583. //---------------------------------------------------------------------------
  3584. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3585. {
  3586. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3587. }
  3588. //---------------------------------------------------------------------------
  3589. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3590. {
  3591. SideEnter(osRemote);
  3592. }
  3593. //---------------------------------------------------------------------------
  3594. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3595. {
  3596. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3597. SideEnter(osRemote);
  3598. }
  3599. //---------------------------------------------------------------------------
  3600. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3601. {
  3602. FCurrentSide = Side;
  3603. if (Visible)
  3604. {
  3605. UpdateControls();
  3606. }
  3607. }
  3608. //---------------------------------------------------------------------------
  3609. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3610. TStrings * FileList, bool Alternative)
  3611. {
  3612. TCustomDirView * DView = DirView(Side);
  3613. DView->SaveSelection();
  3614. DView->SaveSelectedNames();
  3615. DebugAssert(!FAlternativeDelete);
  3616. FAlternativeDelete = Alternative;
  3617. try
  3618. {
  3619. if (Side == osRemote)
  3620. {
  3621. DebugAssert(Terminal != NULL);
  3622. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3623. }
  3624. else
  3625. {
  3626. try
  3627. {
  3628. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3629. }
  3630. __finally
  3631. {
  3632. ReloadLocalDirectory();
  3633. }
  3634. }
  3635. FAlternativeDelete = false;
  3636. }
  3637. catch(...)
  3638. {
  3639. FAlternativeDelete = false;
  3640. DView->DiscardSavedSelection();
  3641. throw;
  3642. }
  3643. DView->RestoreSelection();
  3644. }
  3645. //---------------------------------------------------------------------------
  3646. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3647. {
  3648. DebugAssert(Terminal);
  3649. RemoteDirView->SaveSelection();
  3650. RemoteDirView->SaveSelectedNames();
  3651. try
  3652. {
  3653. if (Lock)
  3654. {
  3655. Terminal->LockFiles(FileList);
  3656. }
  3657. else
  3658. {
  3659. Terminal->UnlockFiles(FileList);
  3660. }
  3661. }
  3662. catch(...)
  3663. {
  3664. RemoteDirView->DiscardSavedSelection();
  3665. throw;
  3666. }
  3667. RemoteDirView->RestoreSelection();
  3668. }
  3669. //---------------------------------------------------------------------------
  3670. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TTerminal *& Session,
  3671. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3672. bool NoConfirmation, bool Move)
  3673. {
  3674. DebugAssert(Terminal != NULL);
  3675. // update Terminal->StateData->RemoteDirectory
  3676. UpdateTerminal(Terminal);
  3677. if (Session == NULL)
  3678. {
  3679. Session = TTerminalManager::Instance()->ActiveTerminal;
  3680. }
  3681. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3682. {
  3683. if (RemoteDriveView->DropTarget != NULL)
  3684. {
  3685. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3686. }
  3687. else if (RemoteDirView->DropTarget != NULL)
  3688. {
  3689. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3690. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3691. }
  3692. else
  3693. {
  3694. Target = RemoteDirView->Path;
  3695. }
  3696. }
  3697. else
  3698. {
  3699. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Session));
  3700. Target = ManagedTerminal->StateData->RemoteDirectory;
  3701. }
  3702. Target = UnixIncludeTrailingBackslash(Target);
  3703. if (FileList->Count == 1)
  3704. {
  3705. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3706. }
  3707. else
  3708. {
  3709. FileMask = AnyMask;
  3710. }
  3711. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3712. bool Result = true;
  3713. if (!NoConfirmation)
  3714. {
  3715. bool Multi = (FileList->Count > 1);
  3716. if (Move)
  3717. {
  3718. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3719. }
  3720. else
  3721. {
  3722. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3723. TStrings * Directories = new TStringList;
  3724. try
  3725. {
  3726. for (int Index = 0; Index < Sessions->Count; Index++)
  3727. {
  3728. TManagedTerminal * Terminal =
  3729. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3730. Directories->Add(Terminal->StateData->RemoteDirectory);
  3731. }
  3732. TDirectRemoteCopy AllowDirectCopy;
  3733. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3734. {
  3735. DebugAssert(DirectCopy);
  3736. AllowDirectCopy = drcAllow;
  3737. }
  3738. else if (FTerminal->IsCapable[fcSecondaryShell])
  3739. {
  3740. DebugAssert(DirectCopy);
  3741. AllowDirectCopy = drcConfirmCommandSession;
  3742. }
  3743. else
  3744. {
  3745. DebugAssert(!DirectCopy);
  3746. AllowDirectCopy = drcDisallow;
  3747. }
  3748. void * ASession = Session;
  3749. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3750. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3751. Session = static_cast<TTerminal *>(ASession);
  3752. }
  3753. __finally
  3754. {
  3755. delete Directories;
  3756. }
  3757. }
  3758. }
  3759. return Result;
  3760. }
  3761. //---------------------------------------------------------------------------
  3762. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3763. TStrings * FileList, bool NoConfirmation, bool Move, TTerminal * Session)
  3764. {
  3765. bool DirectCopy;
  3766. UnicodeString Target, FileMask;
  3767. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3768. if (Result)
  3769. {
  3770. if (!Move && !DirectCopy)
  3771. {
  3772. Configuration->Usage->Inc("RemoteCopyTemp");
  3773. UnicodeString RootTempDir;
  3774. UnicodeString TempDir;
  3775. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3776. TStrings * TemporaryFilesList = new TStringList();
  3777. try
  3778. {
  3779. TMakeLocalFileListParams MakeFileListParam;
  3780. MakeFileListParam.FileList = TemporaryFilesList;
  3781. MakeFileListParam.FileTimes = NULL;
  3782. MakeFileListParam.IncludeDirs = true;
  3783. MakeFileListParam.Recursive = false;
  3784. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3785. TTerminalManager::Instance()->ActiveTerminal = Session;
  3786. if (TemporaryFilesList->Count > 0)
  3787. {
  3788. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3789. CopyParam.FileMask = FileMask;
  3790. DebugAssert(!FAutoOperation);
  3791. FAutoOperation = true;
  3792. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3793. }
  3794. }
  3795. __finally
  3796. {
  3797. delete TemporaryFilesList;
  3798. FAutoOperation = false;
  3799. if (!RootTempDir.IsEmpty())
  3800. {
  3801. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3802. }
  3803. }
  3804. }
  3805. else
  3806. {
  3807. RemoteDirView->SaveSelection();
  3808. RemoteDirView->SaveSelectedNames();
  3809. try
  3810. {
  3811. if (Move)
  3812. {
  3813. Configuration->Usage->Inc("RemoteMove");
  3814. Terminal->MoveFiles(FileList, Target, FileMask);
  3815. }
  3816. else
  3817. {
  3818. Configuration->Usage->Inc("RemoteCopyDirect");
  3819. DebugAssert(DirectCopy);
  3820. DebugAssert(Session == FTerminal);
  3821. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3822. FTerminal->CommandSessionOpened ||
  3823. CommandSessionFallback())
  3824. {
  3825. Terminal->CopyFiles(FileList, Target, FileMask);
  3826. }
  3827. }
  3828. }
  3829. catch(...)
  3830. {
  3831. RemoteDirView->DiscardSavedSelection();
  3832. throw;
  3833. }
  3834. RemoteDirView->RestoreSelection();
  3835. }
  3836. }
  3837. return Result;
  3838. }
  3839. //---------------------------------------------------------------------------
  3840. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3841. const UnicodeString & Path, TRemoteProperties & Properties)
  3842. {
  3843. Properties.Valid = Properties.Valid << vpEncrypt;
  3844. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3845. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3846. }
  3847. //---------------------------------------------------------------------------
  3848. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3849. {
  3850. Side = GetSide(Side);
  3851. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3852. TRemoteProperties * AProperties = (Side == osRemote ? &Properties : NULL);
  3853. UnicodeString Name = LoadStr(NEW_FOLDER);
  3854. int AllowedChanges =
  3855. FLAGMASK((Side == osRemote) && Terminal->IsCapable[fcModeChanging], cpMode);
  3856. bool SaveSettings = false;
  3857. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3858. {
  3859. TWindowLock Lock(this);
  3860. if (Side == osRemote)
  3861. {
  3862. if (SaveSettings)
  3863. {
  3864. GUIConfiguration->NewDirectoryProperties = Properties;
  3865. }
  3866. CreateRemoteDirectory(Name, Properties);
  3867. }
  3868. else
  3869. {
  3870. DirView(Side)->CreateDirectory(Name);
  3871. }
  3872. }
  3873. }
  3874. //---------------------------------------------------------------------------
  3875. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3876. {
  3877. TWindowLock Lock(this);
  3878. DirView(Side)->ExecuteHomeDirectory();
  3879. }
  3880. //---------------------------------------------------------------------------
  3881. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3882. {
  3883. DoOpenDirectoryDialog(odBrowse, Side);
  3884. }
  3885. //---------------------------------------------------------------------------
  3886. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3887. {
  3888. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3889. bool Result = !Path.IsEmpty();
  3890. if (Result)
  3891. {
  3892. // While we might get here when the session is closed (from location profiles),
  3893. // it's not a problem as the Path setter is noop then
  3894. DirView(Side)->Path = Path;
  3895. }
  3896. return Result;
  3897. }
  3898. //---------------------------------------------------------------------------
  3899. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3900. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3901. bool & Close)
  3902. {
  3903. // terminal can be already closed (e.g. dropped connection)
  3904. if (Terminal != NULL)
  3905. {
  3906. try
  3907. {
  3908. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3909. }
  3910. catch(...)
  3911. {
  3912. if (!Terminal->Active)
  3913. {
  3914. Close = true;
  3915. }
  3916. throw;
  3917. }
  3918. }
  3919. }
  3920. //---------------------------------------------------------------------------
  3921. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3922. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3923. bool & Close)
  3924. {
  3925. // terminal can be already closed (e.g. dropped connection)
  3926. if (Terminal != NULL)
  3927. {
  3928. Configuration->Usage->Inc(L"ChecksumCalculated");
  3929. try
  3930. {
  3931. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3932. }
  3933. catch(...)
  3934. {
  3935. if (!Terminal->Active)
  3936. {
  3937. Close = true;
  3938. }
  3939. throw;
  3940. }
  3941. }
  3942. }
  3943. //---------------------------------------------------------------------------
  3944. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3945. {
  3946. bool Result;
  3947. if (Side == osRemote)
  3948. {
  3949. TRemoteTokenList * GroupList = NULL;
  3950. TRemoteTokenList * UserList = NULL;
  3951. try
  3952. {
  3953. TRemoteProperties CurrentProperties;
  3954. if (Terminal->LoadFilesProperties(FileList))
  3955. {
  3956. RemoteDirView->Invalidate();
  3957. }
  3958. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  3959. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  3960. if (CapableGroupChanging || CapableOwnerChanging)
  3961. {
  3962. if (CapableGroupChanging)
  3963. {
  3964. GroupList = Terminal->Groups->Duplicate();
  3965. }
  3966. if (CapableOwnerChanging)
  3967. {
  3968. UserList = Terminal->Users->Duplicate();
  3969. }
  3970. TRemoteDirectory * Files = Terminal->Files;
  3971. int Count = Files->Count;
  3972. if (Count > 100)
  3973. {
  3974. Count = 100;
  3975. }
  3976. for (int Index = 0; Index < Count; Index++)
  3977. {
  3978. TRemoteFile * File = Files->Files[Index];
  3979. if (CapableGroupChanging)
  3980. {
  3981. GroupList->AddUnique(File->Group);
  3982. }
  3983. if (CapableOwnerChanging)
  3984. {
  3985. UserList->AddUnique(File->Owner);
  3986. }
  3987. }
  3988. // if we haven't collected tokens for all files in current directory,
  3989. // make sure we collect them at least for all selected files.
  3990. // (note that so far the files in FileList has to be from current direcotry)
  3991. if (Count < Files->Count)
  3992. {
  3993. for (int Index = 0; Index < FileList->Count; Index++)
  3994. {
  3995. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  3996. if (CapableGroupChanging)
  3997. {
  3998. GroupList->AddUnique(File->Group);
  3999. }
  4000. if (CapableOwnerChanging)
  4001. {
  4002. UserList->AddUnique(File->Owner);
  4003. }
  4004. }
  4005. }
  4006. }
  4007. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4008. int Flags = 0;
  4009. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4010. if (CapableOwnerChanging) Flags |= cpOwner;
  4011. if (CapableGroupChanging) Flags |= cpGroup;
  4012. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4013. if (Terminal->IsCapable[fcCalculatingChecksum])
  4014. {
  4015. CalculateChecksumEvent = CalculateChecksum;
  4016. }
  4017. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4018. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4019. TRemoteProperties NewProperties = CurrentProperties;
  4020. Result =
  4021. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4022. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4023. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4024. CalculateSize, CalculateChecksumEvent);
  4025. if (Result)
  4026. {
  4027. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4028. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4029. }
  4030. }
  4031. __finally
  4032. {
  4033. delete GroupList;
  4034. delete UserList;
  4035. }
  4036. }
  4037. else
  4038. {
  4039. DirView(Side)->DisplayPropertiesMenu();
  4040. Result = true;
  4041. }
  4042. return Result;
  4043. }
  4044. //---------------------------------------------------------------------------
  4045. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4046. {
  4047. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4048. {
  4049. FIgnoreNextDialogChar = Key;
  4050. }
  4051. Key = 0;
  4052. }
  4053. //---------------------------------------------------------------------------
  4054. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4055. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4056. {
  4057. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4058. if (Command != NULL)
  4059. {
  4060. KeyProcessed(Key, Shift);
  4061. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4062. {
  4063. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4064. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4065. false, false, const_cast<TCustomCommandType *>(Command));
  4066. }
  4067. }
  4068. }
  4069. //---------------------------------------------------------------------------
  4070. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4071. {
  4072. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4073. {
  4074. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4075. }
  4076. if (!DirView(osCurrent)->IsEditing())
  4077. {
  4078. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4079. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4080. {
  4081. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4082. if (((Action->ShortCut == KeyShortCut) ||
  4083. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4084. AllowedAction(Action, aaShortCut))
  4085. {
  4086. // Has to be called before the action as the dialog char is already in queue.
  4087. // So when the action consumes message queue, we already need to have the
  4088. // FIgnoreNextDialogChar set
  4089. KeyProcessed(Key, Shift);
  4090. // Reset reference to previous component (when menu/toolbar was clicked).
  4091. // Needed to detect that action was invoked by keyboard shortcut
  4092. // in TNonVisualDataModule::ExplorerActionsExecute
  4093. Action->ActionComponent = NULL;
  4094. Action->Execute();
  4095. return;
  4096. }
  4097. }
  4098. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4099. {
  4100. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4101. {
  4102. KeyProcessed(Key, Shift);
  4103. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4104. return;
  4105. }
  4106. }
  4107. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4108. {
  4109. KeyProcessed(Key, Shift);
  4110. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4111. }
  4112. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4113. if (IsCustomShortCut(CustomShortCut))
  4114. {
  4115. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4116. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4117. if (WinConfiguration->SharedBookmarks != NULL)
  4118. {
  4119. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4120. if ((Bookmark != NULL) &&
  4121. OpenBookmark(FCurrentSide, Bookmark))
  4122. {
  4123. KeyProcessed(Key, Shift);
  4124. }
  4125. }
  4126. }
  4127. }
  4128. TForm::KeyDown(Key, Shift);
  4129. }
  4130. //---------------------------------------------------------------------------
  4131. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4132. {
  4133. DebugAssert(Terminal != NULL);
  4134. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4135. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4136. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4137. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4138. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4139. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4140. // expanded from ?: to avoid memory leaks
  4141. if (SecurityEnabled)
  4142. {
  4143. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4144. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4145. }
  4146. else
  4147. {
  4148. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4149. }
  4150. if (FileSystemInfo.ProtocolName.IsEmpty())
  4151. {
  4152. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4153. }
  4154. else
  4155. {
  4156. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4157. }
  4158. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4159. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4160. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4161. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4162. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4163. UnicodeString CompressionStr = CSCompressionStr;
  4164. if (CSCompressionStr != SCCompressionStr)
  4165. {
  4166. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4167. }
  4168. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4169. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4170. UpdateStatusBar();
  4171. }
  4172. //---------------------------------------------------------------------------
  4173. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4174. {
  4175. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4176. DebugAssert(SessionStatusBar != NULL);
  4177. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4178. {
  4179. // note: (Terminal->Status < sshReady) currently never happens here,
  4180. // so STATUS_CONNECTING is never used
  4181. SessionStatusBar->SimplePanel = true;
  4182. SessionStatusBar->SimpleText = LoadStr(
  4183. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4184. }
  4185. else
  4186. {
  4187. DebugAssert(Terminal);
  4188. SessionStatusBar->SimplePanel = false;
  4189. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4190. if (!FNote.IsEmpty())
  4191. {
  4192. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4193. }
  4194. else
  4195. {
  4196. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4197. }
  4198. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4199. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4200. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4201. }
  4202. }
  4203. //---------------------------------------------------------------------------
  4204. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4205. {
  4206. Panel->Caption = L"";
  4207. }
  4208. //---------------------------------------------------------------------------
  4209. void __fastcall TCustomScpExplorerForm::Idle()
  4210. {
  4211. if (FShowing || FStandaloneEditing)
  4212. {
  4213. FEditorManager->Check();
  4214. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4215. // that needs to know if queue view is visible (and it may be closed after queue update)
  4216. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4217. }
  4218. if (FShowing)
  4219. {
  4220. if (!NonVisualDataModule->Busy &&
  4221. // Menu is opened or drag&drop is going on
  4222. (Mouse->Capture == NULL))
  4223. {
  4224. if (FRefreshRemoteDirectory)
  4225. {
  4226. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4227. {
  4228. Terminal->RefreshDirectory();
  4229. }
  4230. FRefreshRemoteDirectory = false;
  4231. }
  4232. if (FRefreshLocalDirectory)
  4233. {
  4234. ReloadLocalDirectory();
  4235. FRefreshLocalDirectory = false;
  4236. }
  4237. if (WinConfiguration->RefreshRemotePanel)
  4238. {
  4239. TManagedTerminal * ManagedTerminal =
  4240. dynamic_cast<TManagedTerminal *>(Terminal);
  4241. if ((ManagedTerminal != NULL) && (Terminal->Status == ssOpened) &&
  4242. (Now() - ManagedTerminal->DirectoryLoaded >
  4243. WinConfiguration->RefreshRemotePanelInterval))
  4244. {
  4245. RemoteDirView->ReloadDirectory();
  4246. }
  4247. }
  4248. }
  4249. }
  4250. if (FShowing || FStandaloneEditing)
  4251. {
  4252. if (FQueueStatusInvalidated)
  4253. {
  4254. UpdateQueueStatus(false);
  4255. }
  4256. RefreshQueueItems();
  4257. }
  4258. if (FShowing)
  4259. {
  4260. UpdateStatusBar();
  4261. }
  4262. FIgnoreNextDialogChar = 0;
  4263. }
  4264. //---------------------------------------------------------------------------
  4265. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4266. {
  4267. try
  4268. {
  4269. FUserActionTimer->Enabled = false;
  4270. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4271. {
  4272. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4273. {
  4274. FPendingQueueActionItem->ProcessUserAction();
  4275. }
  4276. }
  4277. }
  4278. __finally
  4279. {
  4280. FPendingQueueActionItem = NULL;
  4281. }
  4282. }
  4283. //---------------------------------------------------------------------------
  4284. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4285. {
  4286. if (WinConfiguration->MinimizeToTray)
  4287. {
  4288. UpdateTrayIcon();
  4289. FTrayIcon->Visible = true;
  4290. if (Visible)
  4291. {
  4292. ShowWindow(Handle, SW_HIDE);
  4293. }
  4294. }
  4295. }
  4296. //---------------------------------------------------------------------------
  4297. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4298. {
  4299. // WORKAROUND
  4300. // When restoring maximized window from minimization,
  4301. // rarely some controls do not align properly.
  4302. // Two instances seen (both for Commander):
  4303. // - When restoring, window is temporarily narrower (not maximized),
  4304. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4305. // Once maximized already, top dock shinks back, but the session PageControl,
  4306. // do not align up, leaving space between TopDock and PageControl.
  4307. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4308. for (int Index = 0; Index < ControlCount; Index++)
  4309. {
  4310. RealignControl(Controls[Index]);
  4311. }
  4312. if (FTrayIcon->Visible)
  4313. {
  4314. FTrayIcon->Visible = false;
  4315. if (Visible)
  4316. {
  4317. ShowWindow(Handle, SW_SHOW);
  4318. }
  4319. }
  4320. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4321. {
  4322. FNeedSession = false;
  4323. NonVisualDataModule->StartBusy();
  4324. try
  4325. {
  4326. NeedSession(false);
  4327. }
  4328. __finally
  4329. {
  4330. NonVisualDataModule->EndBusy();
  4331. }
  4332. }
  4333. }
  4334. //---------------------------------------------------------------------------
  4335. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4336. {
  4337. FTrayIcon->Hint = GetMainForm()->Caption;
  4338. }
  4339. //---------------------------------------------------------------------------
  4340. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4341. {
  4342. UpdateTrayIcon();
  4343. }
  4344. //---------------------------------------------------------------------------
  4345. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4346. {
  4347. // workaround
  4348. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4349. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4350. // But WM_ACTIVATEAPP message can be received even when
  4351. // the main window is minimized to the tray and internal editor window is focused
  4352. // (after another application was previously active)
  4353. if (::IsIconic(Handle))
  4354. {
  4355. if (!IsAppIconic())
  4356. {
  4357. SetAppIconic(true);
  4358. }
  4359. }
  4360. ::ApplicationRestore();
  4361. Application->BringToFront();
  4362. }
  4363. //---------------------------------------------------------------------------
  4364. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4365. {
  4366. RestoreApp();
  4367. }
  4368. //---------------------------------------------------------------------------
  4369. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4370. {
  4371. if (OpenInNewWindow())
  4372. {
  4373. // todo: Pass FromSite
  4374. ExecuteNewInstance(SessionUrl);
  4375. }
  4376. else
  4377. {
  4378. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4379. }
  4380. }
  4381. //---------------------------------------------------------------------------
  4382. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4383. {
  4384. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4385. // current working directories become defaults here, what is not right
  4386. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4387. StoredSessions->NewSession(SessionName, SessionData.get());
  4388. // modified only, explicit
  4389. StoredSessions->Save(false, true);
  4390. // encode session name because of slashes in hierarchical sessions
  4391. return EncodeUrlString(SessionName);
  4392. }
  4393. //---------------------------------------------------------------------------
  4394. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4395. {
  4396. if (OpenInNewWindow())
  4397. {
  4398. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4399. }
  4400. else
  4401. {
  4402. // current working directories become defaults here, what is not right
  4403. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4404. TTerminalManager * Manager = TTerminalManager::Instance();
  4405. TTerminal * Terminal = Manager->NewTerminal(SessionData.get());
  4406. Manager->ActiveTerminal = Terminal;
  4407. }
  4408. }
  4409. //---------------------------------------------------------------------------
  4410. void __fastcall TCustomScpExplorerForm::RenameSession()
  4411. {
  4412. UnicodeString Name = Terminal->SessionData->Name;
  4413. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4414. {
  4415. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4416. TSessionData::ValidateName(Name);
  4417. // This is inconsistent with how color (for example) is handled.
  4418. Terminal->SessionData->Name = Name;
  4419. UpdateControls();
  4420. // Add/Remove distinguishing paths from sessions of the same name.
  4421. DoTerminalListChanged();
  4422. }
  4423. }
  4424. //---------------------------------------------------------------------------
  4425. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4426. {
  4427. DebugAssert(Queue != NULL);
  4428. bool Result = Queue->IsEmpty;
  4429. if (!Result)
  4430. {
  4431. SetFocus();
  4432. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4433. }
  4434. return Result;
  4435. }
  4436. //---------------------------------------------------------------------------
  4437. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4438. {
  4439. return CanCloseQueue(FQueue);
  4440. }
  4441. //---------------------------------------------------------------------------
  4442. void __fastcall TCustomScpExplorerForm::CloseSession()
  4443. {
  4444. if (CanCloseQueue())
  4445. {
  4446. TTerminalManager::Instance()->FreeActiveTerminal();
  4447. }
  4448. }
  4449. //---------------------------------------------------------------------------
  4450. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4451. {
  4452. if (OpenInNewWindow())
  4453. {
  4454. // encode session name because of slashes in hierarchical sessions
  4455. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4456. }
  4457. else
  4458. {
  4459. TTerminalManager * Manager = TTerminalManager::Instance();
  4460. TTerminal * Terminal = Manager->NewTerminal(Data);
  4461. Manager->ActiveTerminal = Terminal;
  4462. }
  4463. }
  4464. //---------------------------------------------------------------------------
  4465. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4466. {
  4467. if (OpenInNewWindow())
  4468. {
  4469. ExecuteNewInstance(Name);
  4470. }
  4471. else
  4472. {
  4473. TTerminalManager * Manager = TTerminalManager::Instance();
  4474. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4475. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4476. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  4477. }
  4478. }
  4479. //---------------------------------------------------------------------------
  4480. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4481. bool &CanClose)
  4482. {
  4483. if (Terminal != NULL)
  4484. {
  4485. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4486. {
  4487. unsigned int Result;
  4488. TMessageParams Params(mpNeverAskAgainCheck);
  4489. UnicodeString Message;
  4490. int Answers = qaOK | qaCancel;
  4491. if (TTerminalManager::Instance()->Count > 1)
  4492. {
  4493. if (!WinConfiguration->AutoSaveWorkspace)
  4494. {
  4495. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE2);
  4496. Answers = qaYes | qaNo | qaCancel;
  4497. }
  4498. else
  4499. {
  4500. Message = MainInstructions(LoadStr(CLOSE_SESSIONS));
  4501. }
  4502. }
  4503. else
  4504. {
  4505. Message = MainInstructions(FMTLOAD(CLOSE_SESSION, (Terminal->SessionData->SessionName)));
  4506. }
  4507. if (WinConfiguration->AutoSaveWorkspace)
  4508. {
  4509. Message =
  4510. FORMAT("%s\n\n%s", (Message,
  4511. FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()))));
  4512. }
  4513. SetFocus();
  4514. Result = MessageDialog(Message, qtConfirmation,
  4515. Answers, HELP_NONE, &Params);
  4516. if (Result == qaNeverAskAgain)
  4517. {
  4518. WinConfiguration->ConfirmClosingSession = false;
  4519. }
  4520. if (Result == qaNo)
  4521. {
  4522. CanClose = SaveWorkspace(true);
  4523. // note that the workspace will be saved redundatly again from FormClose
  4524. }
  4525. else
  4526. {
  4527. CanClose =
  4528. (Result == qaOK) ||
  4529. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4530. (Result == qaNeverAskAgain);
  4531. }
  4532. }
  4533. if (CanClose)
  4534. {
  4535. CanClose = CanCloseQueue();
  4536. }
  4537. }
  4538. if (CanClose)
  4539. {
  4540. CanClose =
  4541. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4542. FEditorManager->CloseExternalFilesWithoutProcess();
  4543. if (CanClose)
  4544. {
  4545. while (CanClose && (FLocalEditors->Count > 0))
  4546. {
  4547. int PrevCount = FLocalEditors->Count;
  4548. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4549. CanClose = (FLocalEditors->Count < PrevCount);
  4550. }
  4551. if (CanClose)
  4552. {
  4553. CanClose = FEditorManager->Empty(true);
  4554. if (!CanClose)
  4555. {
  4556. SetFocus();
  4557. CanClose =
  4558. (MessageDialog(
  4559. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4560. }
  4561. }
  4562. }
  4563. }
  4564. }
  4565. //---------------------------------------------------------------------------
  4566. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4567. {
  4568. TForm * Form = dynamic_cast<TForm *>(Sender);
  4569. DebugAssert(Form != NULL);
  4570. Form->Close();
  4571. }
  4572. //---------------------------------------------------------------------------
  4573. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4574. {
  4575. TForm * Form = dynamic_cast<TForm *>(Sender);
  4576. delete Form;
  4577. }
  4578. //---------------------------------------------------------------------------
  4579. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4580. {
  4581. while (FLocalEditors->Count > 0)
  4582. {
  4583. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4584. }
  4585. }
  4586. //---------------------------------------------------------------------------
  4587. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4588. TObject *Sender)
  4589. {
  4590. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4591. try
  4592. {
  4593. SetProperties(osRemote, FileList);
  4594. }
  4595. __finally
  4596. {
  4597. delete FileList;
  4598. }
  4599. }
  4600. //---------------------------------------------------------------------------
  4601. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4602. {
  4603. if (value)
  4604. {
  4605. if (Component == fcCustomCommandsBand)
  4606. {
  4607. UpdateCustomCommandsToolbar();
  4608. }
  4609. }
  4610. }
  4611. //---------------------------------------------------------------------------
  4612. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4613. {
  4614. TControl * Control = GetComponent(Component);
  4615. DebugAssert(Control);
  4616. bool Changed = (Control->Visible != value);
  4617. if (Changed)
  4618. {
  4619. ComponentShowing(Component, value);
  4620. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4621. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4622. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4623. if (value)
  4624. {
  4625. int RemainingHeight = Control->Parent->ClientHeight;
  4626. int RemainingWidth = Control->Parent->ClientWidth;
  4627. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4628. {
  4629. TControl * ChildControl = Control->Parent->Controls[i];
  4630. if (ChildControl->Visible)
  4631. {
  4632. switch (ChildControl->Align)
  4633. {
  4634. case alTop:
  4635. case alBottom:
  4636. RemainingHeight -= ChildControl->Height;
  4637. break;
  4638. case alLeft:
  4639. case alRight:
  4640. RemainingWidth -= ChildControl->Width;
  4641. break;
  4642. }
  4643. }
  4644. }
  4645. int Reserve = ScaleByTextHeight(this, 32);
  4646. // queue in explorer, trees in commander
  4647. if (Control->Height > RemainingHeight - Reserve)
  4648. {
  4649. Control->Height = RemainingHeight / 2;
  4650. }
  4651. if (Control->Width > RemainingWidth - Reserve)
  4652. {
  4653. Control->Width = RemainingWidth / 2;
  4654. }
  4655. }
  4656. Control->Visible = value;
  4657. if (WasFocused && Visible)
  4658. {
  4659. DirView(osCurrent)->SetFocus();
  4660. }
  4661. FixControlsPlacement();
  4662. }
  4663. }
  4664. //---------------------------------------------------------------------------
  4665. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4666. {
  4667. TControl * Control = GetComponent(Component);
  4668. if (Control == NULL)
  4669. {
  4670. return false;
  4671. }
  4672. else
  4673. {
  4674. return Control->Visible;
  4675. }
  4676. }
  4677. //---------------------------------------------------------------------------
  4678. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4679. {
  4680. bool Result;
  4681. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4682. {
  4683. Result = !IsUWP();
  4684. }
  4685. else
  4686. {
  4687. Result = true;
  4688. }
  4689. return Result;
  4690. }
  4691. //---------------------------------------------------------------------------
  4692. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4693. {
  4694. if (RemoteDirView->ItemFocused != NULL)
  4695. {
  4696. RemoteDirView->ItemFocused->MakeVisible(false);
  4697. }
  4698. QueueSplitter->Visible = QueuePanel->Visible;
  4699. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4700. }
  4701. //---------------------------------------------------------------------------
  4702. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4703. {
  4704. switch (Component) {
  4705. case fcStatusBar: return RemoteStatusBar;
  4706. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4707. case fcQueueView: return QueuePanel;
  4708. case fcQueueToolbar: return QueueDock;
  4709. case fcRemoteTree: return RemoteDrivePanel;
  4710. case fcSessionsTabs: return SessionsPageControl;
  4711. default: return NULL;
  4712. }
  4713. }
  4714. //---------------------------------------------------------------------------
  4715. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4716. TObject *Sender, TListColumn *Column, TPoint &Point)
  4717. {
  4718. DebugAssert(NonVisualDataModule && Column && Sender);
  4719. NonVisualDataModule->ListColumn = Column;
  4720. TPopupMenu * DirViewColumnMenu;
  4721. if (Sender == RemoteDirView)
  4722. {
  4723. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4724. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4725. (Column->Index == uvName);
  4726. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4727. (Column->Index == uvSize);
  4728. }
  4729. else
  4730. {
  4731. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4732. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4733. (Column->Index == dvName);
  4734. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4735. (Column->Index == dvSize);
  4736. }
  4737. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4738. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4739. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4740. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4741. }
  4742. //---------------------------------------------------------------------------
  4743. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4744. TObject *Sender, TListItem *Item, bool &AllowExec)
  4745. {
  4746. DoDirViewExecFile(Sender, Item, AllowExec);
  4747. }
  4748. //---------------------------------------------------------------------------
  4749. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4750. TListItem * Item, bool & AllowExec)
  4751. {
  4752. DebugAssert(Sender && Item && Configuration);
  4753. DebugAssert(AllowExec);
  4754. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4755. bool Remote = (ADirView == DirView(osRemote));
  4756. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4757. TOperationSide Side = (Remote ? osRemote : osLocal);
  4758. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4759. // on files only (not directories)
  4760. // and only when symlinks are resolved (apply to remote panel only)
  4761. if (!ADirView->ItemIsDirectory(Item) &&
  4762. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4763. {
  4764. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4765. !FForceExecution &&
  4766. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4767. {
  4768. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4769. {
  4770. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4771. UnlockWindow();
  4772. try
  4773. {
  4774. ExecuteFileOperation(
  4775. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4776. }
  4777. __finally
  4778. {
  4779. LockWindow();
  4780. }
  4781. AllowExec = false;
  4782. }
  4783. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4784. {
  4785. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4786. {
  4787. ExecuteFile(Side, efDefaultEditor);
  4788. AllowExec = false;
  4789. }
  4790. }
  4791. else
  4792. {
  4793. DebugFail();
  4794. }
  4795. }
  4796. // if we have not done anything special, fall back to default behavior
  4797. if (AllowExec)
  4798. {
  4799. if (Remote && !WinConfiguration->DisableOpenEdit)
  4800. {
  4801. ExecuteFile(osRemote, efShell);
  4802. AllowExec = false;
  4803. }
  4804. }
  4805. }
  4806. }
  4807. //---------------------------------------------------------------------------
  4808. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4809. {
  4810. return ((Side == osRemote) || (Side == osCurrent));
  4811. }
  4812. //---------------------------------------------------------------------------
  4813. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4814. {
  4815. DebugFail();
  4816. }
  4817. //---------------------------------------------------------------------------
  4818. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4819. {
  4820. DebugFail();
  4821. }
  4822. //---------------------------------------------------------------------------
  4823. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4824. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, bool UseDefaults)
  4825. {
  4826. TSynchronizeParamType Params;
  4827. Params.LocalDirectory = LocalDirectory;
  4828. Params.RemoteDirectory = RemoteDirectory;
  4829. int UnusedParams =
  4830. (GUIConfiguration->SynchronizeParams &
  4831. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4832. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4833. Params.Options = GUIConfiguration->SynchronizeOptions;
  4834. bool SaveSettings = false;
  4835. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4836. &DoSynchronizeTooManyDirectories);
  4837. DebugAssert(FSynchronizeController == NULL);
  4838. FSynchronizeController = &Controller;
  4839. bool Result;
  4840. try
  4841. {
  4842. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4843. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4844. int Options =
  4845. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4846. DebugAssert(FOnFeedSynchronizeError == NULL);
  4847. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4848. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4849. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4850. if (Result)
  4851. {
  4852. if (SaveSettings)
  4853. {
  4854. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4855. GUIConfiguration->SynchronizeOptions = Params.Options;
  4856. }
  4857. else
  4858. {
  4859. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4860. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4861. FLAGSET(Params.Options, soSynchronize))
  4862. {
  4863. GUIConfiguration->SynchronizeOptions =
  4864. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4865. soSynchronize;
  4866. }
  4867. }
  4868. LocalDirectory = Params.LocalDirectory;
  4869. RemoteDirectory = Params.RemoteDirectory;
  4870. }
  4871. }
  4872. __finally
  4873. {
  4874. FSynchronizeController = NULL;
  4875. DebugAssert(FOnFeedSynchronizeError == NULL);
  4876. FOnFeedSynchronizeError = NULL;
  4877. }
  4878. return Result;
  4879. }
  4880. //---------------------------------------------------------------------------
  4881. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4882. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4883. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4884. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4885. TSynchronizeOptions * Options, bool Full)
  4886. {
  4887. // Keep-up-to-date mode
  4888. if (Terminal->Status == ssOpened)
  4889. {
  4890. try
  4891. {
  4892. int PParams = Params.Params;
  4893. if (!Full)
  4894. {
  4895. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4896. if (FLAGSET(Params.Options, soRecurse))
  4897. {
  4898. PParams |= TTerminal::spSubDirs;
  4899. }
  4900. }
  4901. else
  4902. {
  4903. // if keepuptodate is non-recursive,
  4904. // full sync before has to be non-recursive as well
  4905. if (FLAGCLEAR(Params.Options, soRecurse))
  4906. {
  4907. PParams |= TTerminal::spNoRecurse;
  4908. }
  4909. }
  4910. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  4911. PParams, Checklist, Options);
  4912. }
  4913. catch (Exception & E)
  4914. {
  4915. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  4916. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  4917. {
  4918. // noop, already logged in MoreMessageDialog
  4919. }
  4920. else
  4921. {
  4922. ShowExtendedExceptionEx(Terminal, &E);
  4923. throw;
  4924. }
  4925. }
  4926. }
  4927. }
  4928. //---------------------------------------------------------------------------
  4929. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  4930. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  4931. const UnicodeString ErrorStr)
  4932. {
  4933. if (!Directory.IsEmpty())
  4934. {
  4935. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  4936. }
  4937. else
  4938. {
  4939. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  4940. }
  4941. }
  4942. //---------------------------------------------------------------------------
  4943. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  4944. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  4945. {
  4946. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  4947. {
  4948. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  4949. }
  4950. else
  4951. {
  4952. TMessageParams Params(mpNeverAskAgainCheck);
  4953. unsigned int Result = MessageDialog(
  4954. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  4955. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  4956. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  4957. {
  4958. MaxDirectories *= 2;
  4959. if (Result == qaNeverAskAgain)
  4960. {
  4961. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  4962. }
  4963. }
  4964. else
  4965. {
  4966. Abort();
  4967. }
  4968. }
  4969. }
  4970. //---------------------------------------------------------------------------
  4971. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  4972. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  4973. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  4974. TSynchronizeOptions * Options)
  4975. {
  4976. // Keep-up-to-date mode
  4977. DebugAssert(!FAutoOperation);
  4978. void * BatchStorage;
  4979. BatchStart(BatchStorage);
  4980. FAutoOperation = true;
  4981. bool AnyOperation = false;
  4982. TDateTime StartTime = Now();
  4983. TSynchronizeChecklist * AChecklist = NULL;
  4984. try
  4985. {
  4986. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  4987. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  4988. {
  4989. FSynchronizeProgressForm->Start();
  4990. }
  4991. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  4992. static_cast<TTerminal::TSynchronizeMode>(Mode),
  4993. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4994. Options);
  4995. SAFE_DESTROY(FSynchronizeProgressForm);
  4996. AnyOperation = (AChecklist->CheckedCount > 0);
  4997. if (AnyOperation)
  4998. {
  4999. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5000. CreateProgressForm(&SynchronizeProgress);
  5001. try
  5002. {
  5003. Terminal->SynchronizeApply(
  5004. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5005. SynchronizeProcessedItem, NULL, NULL, NULL);
  5006. }
  5007. __finally
  5008. {
  5009. DestroyProgressForm();
  5010. }
  5011. }
  5012. }
  5013. __finally
  5014. {
  5015. if (Checklist == NULL)
  5016. {
  5017. delete AChecklist;
  5018. }
  5019. else
  5020. {
  5021. *Checklist = AChecklist;
  5022. }
  5023. FAutoOperation = false;
  5024. SAFE_DESTROY(FSynchronizeProgressForm);
  5025. BatchEnd(BatchStorage);
  5026. ReloadLocalDirectory();
  5027. if (AnyOperation)
  5028. {
  5029. OperationComplete(StartTime);
  5030. }
  5031. }
  5032. }
  5033. //---------------------------------------------------------------------------
  5034. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5035. {
  5036. // can be called from command line
  5037. return Visible &&
  5038. ((DirView(osRemote)->SelCount > 0) ||
  5039. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5040. }
  5041. //---------------------------------------------------------------------------
  5042. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5043. {
  5044. LogSynchronizeEvent(Terminal, Message);
  5045. }
  5046. //---------------------------------------------------------------------------
  5047. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5048. int Params, TSynchronizeOptions & Options)
  5049. {
  5050. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5051. {
  5052. Options.Filter = new TStringList();
  5053. Options.Filter->CaseSensitive = false;
  5054. Options.Filter->Duplicates = Types::dupAccept;
  5055. if (DirView(osRemote)->SelCount > 0)
  5056. {
  5057. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5058. }
  5059. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5060. {
  5061. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5062. }
  5063. Options.Filter->Sort();
  5064. }
  5065. }
  5066. //---------------------------------------------------------------------------
  5067. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5068. {
  5069. TCopyParamType Defaults;
  5070. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5071. ConfigStorage->AccessMode = smRead;
  5072. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false, false))
  5073. {
  5074. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5075. }
  5076. ConfigStorage.reset(NULL);
  5077. std::unique_ptr<TStringList> Options(new TStringList());
  5078. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5079. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5080. UnicodeString Result;
  5081. if (Options->Count > 0)
  5082. {
  5083. Result =
  5084. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5085. }
  5086. return Result;
  5087. }
  5088. //---------------------------------------------------------------------------
  5089. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5090. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5091. {
  5092. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5093. UnicodeString AdditionalParams =
  5094. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5095. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5096. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  5097. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5098. SerializeCopyParamForCommandLine(CopyParams)));
  5099. ExecuteNewInstance(SessionName, AdditionalParams);
  5100. }
  5101. //---------------------------------------------------------------------------
  5102. struct TSynchronizeParams
  5103. {
  5104. UnicodeString LocalDirectory;
  5105. UnicodeString RemoteDirectory;
  5106. TSynchronizeMode Mode;
  5107. TSynchronizeChecklist * Checklist;
  5108. int Params;
  5109. TCopyParamType * CopyParam;
  5110. TDateTime CollectElapsed;
  5111. TDateTime * StartTime;
  5112. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5113. };
  5114. //---------------------------------------------------------------------------
  5115. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5116. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5117. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5118. {
  5119. DebugAssert(!FAutoOperation);
  5120. void * BatchStorage;
  5121. BatchStart(BatchStorage);
  5122. FAutoOperation = true;
  5123. TFileOperationStatistics Statistics;
  5124. TDateTime Start = Now();
  5125. try
  5126. {
  5127. Params.OnProcessedItem = OnProcessedItem;
  5128. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5129. CreateProgressForm(&SynchronizeProgress);
  5130. Terminal->SynchronizeApply(
  5131. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5132. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5133. &Statistics);
  5134. }
  5135. __finally
  5136. {
  5137. Params.OnProcessedItem = NULL;
  5138. FAutoOperation = false;
  5139. DestroyProgressForm();
  5140. BatchEnd(BatchStorage);
  5141. ReloadLocalDirectory();
  5142. }
  5143. if (WinConfiguration->SynchronizeSummary)
  5144. {
  5145. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5146. // The statistics should be 0 anyway in this case
  5147. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5148. {
  5149. Message += L"\n";
  5150. if (Statistics.FilesUploaded > 0)
  5151. {
  5152. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5153. }
  5154. if (Statistics.FilesDownloaded > 0)
  5155. {
  5156. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5157. }
  5158. if (Statistics.FilesDeletedLocal > 0)
  5159. {
  5160. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5161. }
  5162. if (Statistics.FilesDeletedRemote > 0)
  5163. {
  5164. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5165. }
  5166. }
  5167. TDateTime Elapsed = (Now() - Start);
  5168. Message +=
  5169. L"\n" +
  5170. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5171. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5172. TMessageParams Params(mpNeverAskAgainCheck);
  5173. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5174. if (Result == qaNeverAskAgain)
  5175. {
  5176. WinConfiguration->SynchronizeSummary = false;
  5177. }
  5178. }
  5179. }
  5180. //---------------------------------------------------------------------------
  5181. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5182. {
  5183. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5184. {
  5185. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5186. }
  5187. // Not set in keep-up-to-date mode - Synchronize() method
  5188. if (Token != NULL)
  5189. {
  5190. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5191. if (Params.OnProcessedItem != NULL)
  5192. {
  5193. Params.OnProcessedItem(NULL, ChecklistItem);
  5194. }
  5195. }
  5196. }
  5197. //---------------------------------------------------------------------------
  5198. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5199. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5200. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5201. {
  5202. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5203. *Params.StartTime = Now();
  5204. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5205. }
  5206. //---------------------------------------------------------------------------
  5207. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5208. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5209. {
  5210. // terminal can be already closed (e.g. dropped connection)
  5211. if (Terminal != NULL)
  5212. {
  5213. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5214. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5215. }
  5216. }
  5217. //---------------------------------------------------------------------------
  5218. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5219. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5220. {
  5221. TAutoBatch AutoBatch(this);
  5222. TAutoFlag AutoOperationFlag(FAutoOperation);
  5223. if (Side == osRemote)
  5224. {
  5225. std::unique_ptr<TStrings> FileList(new TStringList());
  5226. FileList->AddObject(FileName, RemoteFile);
  5227. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5228. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5229. RemoteDirView->SaveSelection();
  5230. RemoteDirView->SaveSelectedNames();
  5231. try
  5232. {
  5233. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5234. }
  5235. catch(...)
  5236. {
  5237. RemoteDirView->DiscardSavedSelection();
  5238. throw;
  5239. }
  5240. RemoteDirView->RestoreSelection();
  5241. }
  5242. else if (DebugAlwaysTrue(Side == osLocal))
  5243. {
  5244. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5245. {
  5246. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5247. }
  5248. UnicodeString Directory = ExtractFileDir(FileName);
  5249. ReloadLocalDirectory(Directory);
  5250. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5251. if (!SamePaths(Directory, NewDirectory))
  5252. {
  5253. ReloadLocalDirectory(NewDirectory);
  5254. }
  5255. }
  5256. }
  5257. //---------------------------------------------------------------------------
  5258. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5259. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5260. const TCopyParamType * CopyParams)
  5261. {
  5262. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5263. UnicodeString AdditionalParams =
  5264. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5265. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5266. LocalDirectory, RemoteDirectory, Mode, Params,
  5267. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5268. SerializeCopyParamForCommandLine(CopyParams)));
  5269. ExecuteNewInstance(SessionName, AdditionalParams);
  5270. }
  5271. //---------------------------------------------------------------------------
  5272. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5273. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5274. TSynchronizeMode & Mode, int Params, bool & SaveMode, bool UseDefaults)
  5275. {
  5276. int Result;
  5277. bool SaveSettings = false;
  5278. int Options =
  5279. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5280. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5281. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5282. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5283. bool Continue =
  5284. UseDefaults ||
  5285. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5286. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow);
  5287. if (Continue)
  5288. {
  5289. Configuration->Usage->Inc(L"Synchronizations");
  5290. UpdateCopyParamCounters(CopyParam);
  5291. TSynchronizeOptions SynchronizeOptions;
  5292. GetSynchronizeOptions(Params, SynchronizeOptions);
  5293. if (SaveSettings)
  5294. {
  5295. GUIConfiguration->SynchronizeParams = Params;
  5296. }
  5297. else
  5298. {
  5299. SaveMode = false;
  5300. }
  5301. TDateTime StartTime = Now();
  5302. TSynchronizeChecklist * Checklist = NULL;
  5303. try
  5304. {
  5305. DebugAssert(!FAutoOperation);
  5306. FAutoOperation = true;
  5307. try
  5308. {
  5309. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  5310. FSynchronizeProgressForm->Start();
  5311. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5312. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5313. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5314. &SynchronizeOptions);
  5315. }
  5316. __finally
  5317. {
  5318. FAutoOperation = false;
  5319. SAFE_DESTROY(FSynchronizeProgressForm);
  5320. }
  5321. TSynchronizeParams SynchronizeParams;
  5322. SynchronizeParams.LocalDirectory = LocalDirectory;
  5323. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5324. SynchronizeParams.Mode = Mode;
  5325. SynchronizeParams.CopyParam = &CopyParam;
  5326. SynchronizeParams.Params = Params;
  5327. SynchronizeParams.Checklist = Checklist;
  5328. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5329. SynchronizeParams.StartTime = &StartTime;
  5330. SynchronizeParams.OnProcessedItem = NULL;
  5331. Result = Checklist->Count;
  5332. if (Checklist->Count > 0)
  5333. {
  5334. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5335. {
  5336. if (!DoSynchronizeChecklistDialog(
  5337. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5338. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, &SynchronizeParams))
  5339. {
  5340. Result = -1;
  5341. }
  5342. }
  5343. else
  5344. {
  5345. FullSynchronize(SynchronizeParams, NULL, NULL);
  5346. }
  5347. }
  5348. else
  5349. {
  5350. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5351. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5352. }
  5353. }
  5354. __finally
  5355. {
  5356. delete Checklist;
  5357. }
  5358. OperationComplete(StartTime);
  5359. }
  5360. else
  5361. {
  5362. Result = -1;
  5363. }
  5364. return Result;
  5365. }
  5366. //---------------------------------------------------------------------------
  5367. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5368. const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
  5369. bool & Continue, bool Collect)
  5370. {
  5371. if (Collect)
  5372. {
  5373. DebugAssert(FSynchronizeProgressForm != NULL);
  5374. if (!FSynchronizeProgressForm->Started)
  5375. {
  5376. FSynchronizeProgressForm->Start();
  5377. }
  5378. FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Continue);
  5379. }
  5380. else
  5381. {
  5382. DebugAssert(FSynchronizeProgressForm == NULL);
  5383. DebugAssert(FProgressForm != NULL);
  5384. }
  5385. }
  5386. //---------------------------------------------------------------------------
  5387. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5388. {
  5389. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5390. TRemoteFile * File = NULL;
  5391. Terminal->ReadFile(FullFileName, File);
  5392. if (File != NULL)
  5393. {
  5394. std::unique_ptr<TRemoteFile> FileOwner(File);
  5395. TAutoFlag Flag(FStandaloneEditing);
  5396. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5397. Application->ShowMainForm = false;
  5398. Application->Run();
  5399. }
  5400. }
  5401. //---------------------------------------------------------------------------
  5402. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5403. {
  5404. DebugFail();
  5405. }
  5406. //---------------------------------------------------------------------------
  5407. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5408. {
  5409. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5410. SessionData->Assign(Terminal->SessionData);
  5411. UpdateSessionData(SessionData.get());
  5412. Terminal->UpdateSessionCredentials(SessionData.get());
  5413. if (!Terminal->SessionData->HasSessionName())
  5414. {
  5415. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5416. // once IsWorkspace is cleared
  5417. SessionData->Name = UnicodeString();
  5418. SessionData->IsWorkspace = false;
  5419. }
  5420. return SessionData.release();
  5421. }
  5422. //---------------------------------------------------------------------------
  5423. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5424. {
  5425. TSessionData * SessionData = CloneCurrentSessionData();
  5426. try
  5427. {
  5428. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5429. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5430. }
  5431. __finally
  5432. {
  5433. delete SessionData;
  5434. }
  5435. }
  5436. //---------------------------------------------------------------------------
  5437. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5438. {
  5439. TTerminalManager * Manager = TTerminalManager::Instance();
  5440. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5441. if (DebugAlwaysTrue(Terminal != NULL))
  5442. {
  5443. // Update (Managed)Terminal->StateData
  5444. UpdateTerminal(Terminal);
  5445. }
  5446. Manager->SaveWorkspace(DataList.get());
  5447. return DataList.release();
  5448. }
  5449. //---------------------------------------------------------------------------
  5450. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5451. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5452. {
  5453. WinConfiguration->LastWorkspace = Name;
  5454. if (!SavePasswords)
  5455. {
  5456. for (int Index = 0; Index < DataList->Count; Index++)
  5457. {
  5458. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5459. if (SessionData->Link.IsEmpty())
  5460. {
  5461. SessionData->ClearSessionPasswords();
  5462. }
  5463. }
  5464. }
  5465. StoredSessions->NewWorkspace(Name, DataList);
  5466. // modified only
  5467. StoredSessions->Save(false, Explicit);
  5468. }
  5469. //---------------------------------------------------------------------------
  5470. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5471. {
  5472. return
  5473. DefaultStr(WinConfiguration->LastWorkspace,
  5474. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5475. }
  5476. //---------------------------------------------------------------------------
  5477. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5478. {
  5479. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5480. bool AnyNonStoredSessionWithPassword = false;
  5481. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5482. bool AllNonStoredSessionsAnonymous = true;
  5483. TTerminalManager * Manager = TTerminalManager::Instance();
  5484. for (int Index = 0; Index < DataList->Count; Index++)
  5485. {
  5486. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5487. if (SessionData->Link.IsEmpty())
  5488. {
  5489. if (SessionData->HasAnySessionPassword())
  5490. {
  5491. AnyNonStoredSessionWithPassword = true;
  5492. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5493. {
  5494. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5495. }
  5496. }
  5497. if (!SameText(SessionData->UserName, AnonymousUserName))
  5498. {
  5499. AllNonStoredSessionsAnonymous = false;
  5500. }
  5501. }
  5502. }
  5503. bool SavePasswords;
  5504. bool * PSavePasswords;
  5505. bool NotRecommendedSavingPasswords =
  5506. !CustomWinConfiguration->UseMasterPassword &&
  5507. !AllNonStoredSessionsAnonymous;
  5508. if (Configuration->DisablePasswordStoring ||
  5509. !AnyNonStoredSessionWithPassword)
  5510. {
  5511. PSavePasswords = NULL;
  5512. SavePasswords = false;
  5513. }
  5514. else
  5515. {
  5516. PSavePasswords = &SavePasswords;
  5517. SavePasswords =
  5518. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5519. !NotRecommendedSavingPasswords;
  5520. }
  5521. UnicodeString Name = WorkspaceName();
  5522. bool CreateShortcut = false;
  5523. bool Result =
  5524. DoSaveWorkspaceDialog(
  5525. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5526. EnableAutoSave);
  5527. if (Result)
  5528. {
  5529. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5530. if (CreateShortcut)
  5531. {
  5532. TOperationVisualizer Visualizer;
  5533. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5534. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5535. }
  5536. if (EnableAutoSave)
  5537. {
  5538. WinConfiguration->AutoSaveWorkspace = true;
  5539. WinConfiguration->AutoWorkspace = Name;
  5540. if (PSavePasswords != NULL)
  5541. {
  5542. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5543. }
  5544. }
  5545. }
  5546. return Result;
  5547. }
  5548. //---------------------------------------------------------------------------
  5549. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TTerminal * Terminal)
  5550. {
  5551. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  5552. DebugAssert(ManagedTerminal != NULL);
  5553. SAFE_DESTROY(ManagedTerminal->RemoteExplorerState);
  5554. if (WinConfiguration->PreservePanelState)
  5555. {
  5556. ManagedTerminal->RemoteExplorerState = RemoteDirView->SaveState();
  5557. }
  5558. UpdateSessionData(ManagedTerminal->StateData);
  5559. }
  5560. //---------------------------------------------------------------------------
  5561. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5562. {
  5563. // Keep in sync with TSessionData::CopyStateData
  5564. DebugAssert(Data != NULL);
  5565. // This is inconsistent with how color (for example) is handled.
  5566. Data->Name = Terminal->SessionData->Name;
  5567. // cannot use RemoteDirView->Path, because it is empty if connection
  5568. // was already closed
  5569. // also only peek, we may not be connected at all atm
  5570. // so make sure we do not try retrieving current directory from the server
  5571. // (particularly with FTP)
  5572. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5573. if (!ACurrentDirectory.IsEmpty())
  5574. {
  5575. Data->RemoteDirectory = ACurrentDirectory;
  5576. }
  5577. Data->Color = SessionColor;
  5578. }
  5579. //---------------------------------------------------------------------------
  5580. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5581. {
  5582. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5583. DebugAssert(Toolbar != NULL);
  5584. for (int i = 0; i < Toolbar->Items->Count; i++)
  5585. {
  5586. TTBXCustomDropDownItem * DropDownItem;
  5587. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5588. if (DropDownItem != NULL)
  5589. {
  5590. ToolbarItemResize(DropDownItem,
  5591. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5592. Toolbar->NonClientWidth);
  5593. break;
  5594. }
  5595. }
  5596. }
  5597. //---------------------------------------------------------------------------
  5598. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5599. {
  5600. Item->EditWidth = Width;
  5601. }
  5602. //---------------------------------------------------------------------------
  5603. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5604. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5605. {
  5606. for (int i = 0; i < Toolbar->Items->Count; i++)
  5607. {
  5608. TTBXCustomDropDownItem * DropDownItem;
  5609. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5610. if (DropDownItem != NULL)
  5611. {
  5612. ASize.x -= DropDownItem->EditWidth;
  5613. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5614. }
  5615. }
  5616. }
  5617. //---------------------------------------------------------------------------
  5618. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5619. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5620. {
  5621. if (WinConfiguration->DDWarnLackOfTempSpace)
  5622. {
  5623. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5624. if (!ADrive.IsEmpty())
  5625. {
  5626. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5627. DebugAssert(RequiredSpace >= 0);
  5628. __int64 RequiredWithReserve;
  5629. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5630. if (FreeSpace < RequiredWithReserve)
  5631. {
  5632. unsigned int Result;
  5633. TMessageParams Params(mpNeverAskAgainCheck);
  5634. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5635. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5636. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5637. if (Result == qaNeverAskAgain)
  5638. {
  5639. WinConfiguration->DDWarnLackOfTempSpace = false;
  5640. }
  5641. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5642. }
  5643. }
  5644. }
  5645. }
  5646. //---------------------------------------------------------------------------
  5647. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5648. {
  5649. DoOpenDirectoryDialog(odAddBookmark, Side);
  5650. }
  5651. //---------------------------------------------------------------------------
  5652. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5653. {
  5654. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5655. TStringList * VisitedDirectories = CreateSortedStringList((Side == osRemote));
  5656. try
  5657. {
  5658. TCustomDirView * DView = DirView(Side);
  5659. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5660. {
  5661. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5662. }
  5663. }
  5664. catch (...)
  5665. {
  5666. delete VisitedDirectories;
  5667. throw;
  5668. }
  5669. return VisitedDirectories;
  5670. }
  5671. //---------------------------------------------------------------------------
  5672. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5673. TOpenDirectoryMode Mode, TOperationSide Side)
  5674. {
  5675. bool Continue = true;
  5676. if (Mode == odAddBookmark)
  5677. {
  5678. TMessageParams Params(mpNeverAskAgainCheck);
  5679. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5680. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5681. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5682. unsigned int Answer =
  5683. MessageDialog(Message,
  5684. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5685. if (Answer == qaNeverAskAgain)
  5686. {
  5687. Continue = true;
  5688. WinConfiguration->UseSharedBookmarks = true;
  5689. }
  5690. else if (Answer == qaOK)
  5691. {
  5692. Continue = true;
  5693. WinConfiguration->UseSharedBookmarks = false;
  5694. }
  5695. else
  5696. {
  5697. Continue = false;
  5698. }
  5699. }
  5700. if (Continue)
  5701. {
  5702. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5703. try
  5704. {
  5705. UnicodeString Name = DirView(Side)->PathName;
  5706. bool Repeat;
  5707. do
  5708. {
  5709. Repeat = false;
  5710. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5711. // do not allow switching to location profiles,
  5712. // if we are not connected
  5713. HasDirView[osLocal] && (Terminal != NULL)))
  5714. {
  5715. TWindowLock Lock(this);
  5716. if (!TryOpenDirectory(Side, Name))
  5717. {
  5718. Repeat = true;
  5719. Mode = odBrowse;
  5720. }
  5721. }
  5722. }
  5723. while (Repeat);
  5724. }
  5725. __finally
  5726. {
  5727. delete VisitedDirectories;
  5728. }
  5729. }
  5730. }
  5731. //---------------------------------------------------------------------------
  5732. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5733. {
  5734. DebugAssert(!FTerminal->CommandSessionOpened);
  5735. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5736. }
  5737. //---------------------------------------------------------------------------
  5738. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5739. {
  5740. bool Result = FTerminal->IsCapable[Capability] ||
  5741. FTerminal->CommandSessionOpened;
  5742. if (!Result)
  5743. {
  5744. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5745. if (!GUIConfiguration->ConfirmCommandSession)
  5746. {
  5747. Result = true;
  5748. }
  5749. else
  5750. {
  5751. TMessageParams Params(mpNeverAskAgainCheck);
  5752. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5753. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5754. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5755. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5756. if (Answer == qaNeverAskAgain)
  5757. {
  5758. GUIConfiguration->ConfirmCommandSession = false;
  5759. Result = true;
  5760. }
  5761. else if (Answer == qaOK)
  5762. {
  5763. Result = true;
  5764. }
  5765. }
  5766. if (Result)
  5767. {
  5768. Result = CommandSessionFallback();
  5769. }
  5770. }
  5771. return Result;
  5772. }
  5773. //---------------------------------------------------------------------------
  5774. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5775. {
  5776. if (EnsureCommandSessionFallback(fcAnyCommand))
  5777. {
  5778. DoConsoleDialog(Terminal, Command);
  5779. }
  5780. }
  5781. //---------------------------------------------------------------------------
  5782. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5783. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5784. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5785. {
  5786. if (IsFileControl(DropSourceControl, osRemote) &&
  5787. (FDDExtMapFile != NULL))
  5788. {
  5789. Accept = true;
  5790. }
  5791. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5792. DebugAssert(FDDTargetControl != NULL);
  5793. }
  5794. //---------------------------------------------------------------------------
  5795. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5796. _di_IDataObject DataObj, int KeyState,
  5797. const TPoint & Point, int & Effect, bool & Accept)
  5798. {
  5799. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5800. }
  5801. //---------------------------------------------------------------------------
  5802. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5803. _di_IDataObject DataObj, int KeyState,
  5804. const TPoint & Point, int & Effect, bool & Accept)
  5805. {
  5806. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5807. }
  5808. //---------------------------------------------------------------------------
  5809. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5810. TObject * Sender)
  5811. {
  5812. DebugUsedParam(Sender);
  5813. DebugAssert(FDDTargetControl == Sender);
  5814. FDDTargetControl = NULL;
  5815. }
  5816. //---------------------------------------------------------------------------
  5817. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5818. {
  5819. FileControlDDDragLeave(SessionsPageControl);
  5820. }
  5821. //---------------------------------------------------------------------------
  5822. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5823. {
  5824. FileControlDDDragLeave(QueueView3);
  5825. }
  5826. //---------------------------------------------------------------------------
  5827. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5828. {
  5829. DebugAssert(GetSide(Side) == osRemote);
  5830. DebugUsedParam(Side);
  5831. bool Edit = false;
  5832. TRemoteFile * File = NULL;
  5833. UnicodeString FileName;
  5834. UnicodeString PointTo;
  5835. bool SymbolicLink = true;
  5836. if (RemoteDirView->ItemFocused)
  5837. {
  5838. DebugAssert(RemoteDirView->ItemFocused->Data);
  5839. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5840. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5841. if (Edit)
  5842. {
  5843. FileName = File->FileName;
  5844. PointTo = File->LinkTo;
  5845. }
  5846. else
  5847. {
  5848. PointTo = File->FileName;
  5849. }
  5850. }
  5851. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5852. Terminal->IsCapable[fcHardLink]))
  5853. {
  5854. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5855. if (Edit)
  5856. {
  5857. DebugAssert(File->FileName == FileName);
  5858. int Params = dfNoRecursive;
  5859. Terminal->ExceptionOnFail = true;
  5860. try
  5861. {
  5862. Terminal->DeleteFile(L"", File, &Params);
  5863. }
  5864. __finally
  5865. {
  5866. Terminal->ExceptionOnFail = false;
  5867. }
  5868. }
  5869. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  5870. }
  5871. }
  5872. //---------------------------------------------------------------------------
  5873. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  5874. {
  5875. return
  5876. ((GetSide(Side) != osRemote) ||
  5877. ((Terminal != NULL) &&
  5878. Terminal->ResolvingSymlinks &&
  5879. Terminal->IsCapable[fcSymbolicLink]));
  5880. }
  5881. //---------------------------------------------------------------------------
  5882. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  5883. {
  5884. return
  5885. (FCurrentSide == osRemote) &&
  5886. (RemoteDirView->ItemFocused != NULL) &&
  5887. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  5888. Terminal->SessionData->ResolveSymlinks;
  5889. }
  5890. //---------------------------------------------------------------------------
  5891. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  5892. {
  5893. DebugAssert(!WinConfiguration->DisableOpenEdit);
  5894. FForceExecution = true;
  5895. try
  5896. {
  5897. DirView(osCurrent)->ExecuteCurrentFile();
  5898. }
  5899. __finally
  5900. {
  5901. FForceExecution = false;
  5902. }
  5903. }
  5904. //---------------------------------------------------------------------------
  5905. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  5906. {
  5907. TEditorData ExternalEditor;
  5908. ExternalEditor.Editor = edExternal;
  5909. bool Remember = false;
  5910. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc,
  5911. (GetSide(osCurrent) == osRemote)))
  5912. {
  5913. if (Remember)
  5914. {
  5915. TEditorList * EditorList = new TEditorList;
  5916. try
  5917. {
  5918. *EditorList = *WinConfiguration->EditorList;
  5919. bool Found = false;
  5920. int i = 0;
  5921. while (!Found && (i < EditorList->Count))
  5922. {
  5923. const TEditorPreferences * Editor = EditorList->Editors[i];
  5924. if ((Editor->Data->Editor == edExternal) &&
  5925. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  5926. {
  5927. Found = true;
  5928. }
  5929. i++;
  5930. }
  5931. if (!Found)
  5932. {
  5933. EditorList->Add(new TEditorPreferences(ExternalEditor));
  5934. WinConfiguration->EditorList = EditorList;
  5935. }
  5936. }
  5937. __finally
  5938. {
  5939. delete EditorList;
  5940. }
  5941. }
  5942. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  5943. }
  5944. }
  5945. //---------------------------------------------------------------------------
  5946. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  5947. {
  5948. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  5949. if (FFileFindTerminal == Sender)
  5950. {
  5951. FFileFindTerminal = NULL;
  5952. HideFileFindDialog();
  5953. }
  5954. if (FClipboardTerminal == Sender)
  5955. {
  5956. ClipboardClear(); // implies ClipboardStop
  5957. }
  5958. }
  5959. //---------------------------------------------------------------------------
  5960. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  5961. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  5962. {
  5963. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  5964. DebugAssert(Terminal != NULL);
  5965. if (Data->Terminal == Terminal)
  5966. {
  5967. Data->Terminal = NULL;
  5968. }
  5969. }
  5970. //---------------------------------------------------------------------------
  5971. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  5972. {
  5973. UpdateControls();
  5974. SessionColor = TColor(0);
  5975. UpdateRemotePathComboBox(false);
  5976. NeedSession(true);
  5977. }
  5978. //---------------------------------------------------------------------------
  5979. void __fastcall TCustomScpExplorerForm::NeedSession(bool ReloadSessions)
  5980. {
  5981. try
  5982. {
  5983. try
  5984. {
  5985. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  5986. }
  5987. __finally
  5988. {
  5989. if (!WinConfiguration->KeepOpenWhenNoSession &&
  5990. (!Terminal || !Terminal->Active))
  5991. {
  5992. TerminateApplication();
  5993. }
  5994. }
  5995. }
  5996. catch (EAbort &)
  5997. {
  5998. // swallow
  5999. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6000. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6001. }
  6002. }
  6003. //---------------------------------------------------------------------------
  6004. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  6005. {
  6006. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  6007. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6008. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  6009. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6010. try
  6011. {
  6012. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  6013. // Clear the design time unthemed tab
  6014. ((SessionsPageControl->PageCount > 0) &&
  6015. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6016. {
  6017. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6018. }
  6019. for (int Index = 0; Index <= TerminalList->Count; Index++)
  6020. {
  6021. TThemeTabSheet * TabSheet;
  6022. if (Index >= SessionsPageControl->PageCount)
  6023. {
  6024. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6025. TabSheet->PageControl = SessionsPageControl;
  6026. }
  6027. else
  6028. {
  6029. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6030. }
  6031. bool IsSessionTab = (Index < TerminalList->Count);
  6032. if (IsSessionTab)
  6033. {
  6034. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  6035. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6036. UpdateSessionTab(TabSheet);
  6037. }
  6038. else
  6039. {
  6040. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6041. TabSheet->Tag = 0; // not really needed
  6042. // We know that we are at the last page, sotherwise we could not call this (it assumes that new session tab is the last one)
  6043. UpdateNewSessionTab();
  6044. }
  6045. TabSheet->ShowCloseButton = IsSessionTab;
  6046. }
  6047. }
  6048. __finally
  6049. {
  6050. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6051. }
  6052. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6053. }
  6054. //---------------------------------------------------------------------------
  6055. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6056. {
  6057. DoTerminalListChanged();
  6058. }
  6059. //---------------------------------------------------------------------------
  6060. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6061. {
  6062. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6063. DebugAssert(TabSheet->ImageIndex == 0);
  6064. TabSheet->Caption =
  6065. WinConfiguration->SelectiveToolbarText ?
  6066. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6067. UnicodeString();
  6068. }
  6069. //---------------------------------------------------------------------------
  6070. TTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6071. {
  6072. return reinterpret_cast<TTerminal *>(TabSheet->Tag);
  6073. }
  6074. //---------------------------------------------------------------------------
  6075. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6076. {
  6077. if (DebugAlwaysTrue(TabSheet != NULL))
  6078. {
  6079. TManagedTerminal * ManagedTerminal =
  6080. dynamic_cast<TManagedTerminal *>(GetSessionTabTerminal(TabSheet));
  6081. if (DebugAlwaysTrue(ManagedTerminal != NULL))
  6082. {
  6083. TColor Color = (ManagedTerminal == FTerminal) ? FSessionColor : ManagedTerminal->StateData->Color;
  6084. TabSheet->ImageIndex = AddSessionColor(Color);
  6085. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ManagedTerminal, true);
  6086. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6087. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6088. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6089. {
  6090. ThemeTabSheet->Shadowed = !ManagedTerminal->Active;
  6091. }
  6092. }
  6093. }
  6094. }
  6095. //---------------------------------------------------------------------------
  6096. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6097. {
  6098. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6099. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6100. bool Result = (Terminal != NULL);
  6101. if (Result)
  6102. {
  6103. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6104. }
  6105. else
  6106. {
  6107. try
  6108. {
  6109. NewSession(false);
  6110. }
  6111. __finally
  6112. {
  6113. DoTerminalListChanged();
  6114. }
  6115. FSessionsPageControlNewSessionTime = Now();
  6116. }
  6117. return Result;
  6118. }
  6119. //---------------------------------------------------------------------------
  6120. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6121. {
  6122. SessionTabSwitched();
  6123. }
  6124. //---------------------------------------------------------------------------
  6125. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6126. {
  6127. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6128. DebugAssert(TransferList != NULL);
  6129. UnicodeString Name;
  6130. if (TransferList->ItemIndex <= 0)
  6131. {
  6132. Name = L"";
  6133. }
  6134. else
  6135. {
  6136. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6137. }
  6138. if (FCopyParamAutoSelected.IsEmpty())
  6139. {
  6140. // if previous preset was not autoselected, make new preset the "default"
  6141. FCopyParamDefault = Name;
  6142. }
  6143. GUIConfiguration->CopyParamCurrent = Name;
  6144. }
  6145. //---------------------------------------------------------------------------
  6146. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6147. {
  6148. // sanity check
  6149. bool ExistingPreset =
  6150. (FTransferListHoverIndex >= 0) &&
  6151. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6152. DebugAssert(ExistingPreset);
  6153. if (ExistingPreset)
  6154. {
  6155. HDC DC = GetDC(0);
  6156. TCanvas * Canvas = new TCanvas();
  6157. try
  6158. {
  6159. Canvas->Handle = DC;
  6160. Canvas->Font = GetToolbarFont(this);
  6161. UnicodeString Name;
  6162. if (FTransferListHoverIndex == 0)
  6163. {
  6164. Name = L"";
  6165. }
  6166. else
  6167. {
  6168. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6169. }
  6170. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6171. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6172. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6173. static_cast<TObject*>(GetComponent(fcTransferList)));
  6174. UnicodeString InfoStr =
  6175. GUIConfiguration->CopyParamPreset[Name].
  6176. GetInfoStr(L"; ",
  6177. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General));
  6178. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6179. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6180. {
  6181. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6182. {
  6183. InfoStr.SetLength(InfoStr.Length() - 1);
  6184. }
  6185. InfoStr += Ellipsis;
  6186. }
  6187. // UpdateCaption does not cause invalidation of whole submenu, while
  6188. // setting Caption property does.
  6189. // also it probably does not resize the label, even if necessary
  6190. // (we do not want that anyway)
  6191. TransferLabel->UpdateCaption(InfoStr);
  6192. }
  6193. __finally
  6194. {
  6195. Canvas->Handle = NULL;
  6196. ReleaseDC(0, DC);
  6197. delete Canvas;
  6198. }
  6199. }
  6200. }
  6201. //---------------------------------------------------------------------------
  6202. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6203. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6204. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6205. {
  6206. if (FTransferListHoverIndex != AHoverIndex)
  6207. {
  6208. FTransferListHoverIndex = AHoverIndex;
  6209. UpdateTransferLabel();
  6210. }
  6211. }
  6212. //---------------------------------------------------------------------------
  6213. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6214. {
  6215. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6216. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6217. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6218. {
  6219. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6220. {
  6221. OpenDirectory(GetSide(osCurrent));
  6222. Message.Result = 1;
  6223. }
  6224. else
  6225. {
  6226. TForm::Dispatch(&Message);
  6227. }
  6228. }
  6229. else
  6230. {
  6231. TForm::Dispatch(&Message);
  6232. }
  6233. }
  6234. //---------------------------------------------------------------------------
  6235. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6236. {
  6237. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6238. if ((FIgnoreNextDialogChar != 0) &&
  6239. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6240. {
  6241. Message.Result = 1;
  6242. }
  6243. else
  6244. {
  6245. TForm::Dispatch(&Message);
  6246. }
  6247. FIgnoreNextDialogChar = 0;
  6248. }
  6249. //---------------------------------------------------------------------------
  6250. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6251. {
  6252. // The four low-order bits are used internally by Windows
  6253. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6254. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6255. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6256. // 61730 - restore thru double click - undocumented
  6257. // 61490 - maximize thru double click - undocumented
  6258. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6259. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6260. {
  6261. SysResizing(Cmd);
  6262. }
  6263. TForm::Dispatch(&Message);
  6264. }
  6265. //---------------------------------------------------------------------------
  6266. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6267. {
  6268. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6269. // Also there no point returning TRUE as we are not able to
  6270. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6271. // Hence the process termination might be safer :)
  6272. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6273. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6274. {
  6275. Message.Result = FALSE;
  6276. }
  6277. else
  6278. {
  6279. Message.Result = TRUE;
  6280. }
  6281. // Do not call default handling as that triggers OnCloseQuery,
  6282. // where our implementation will popup confirmation dialogs, what we do not want,
  6283. // as per Vista guidelines:
  6284. // https://docs.microsoft.com/en-us/windows/desktop/shutdown/shutting-down
  6285. }
  6286. //---------------------------------------------------------------------------
  6287. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6288. {
  6289. if (Message.EndSession && IsApplicationMinimized())
  6290. {
  6291. // WORKAROUND
  6292. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6293. // when it receives WM_ENDSESSION,
  6294. // but that sometimes (particularly when application is minimized) cause crashes.
  6295. // Crash popups message that blocks log off.
  6296. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6297. // so we call ExitProcess() immediately, not even trying to cleanup.
  6298. // It still causes beep, so there's likely some popup, but it does not block
  6299. // log off.
  6300. ExitProcess(0);
  6301. }
  6302. TForm::Dispatch(&Message);
  6303. }
  6304. #ifdef _DEBUG
  6305. //---------------------------------------------------------------------------
  6306. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6307. {
  6308. TForm::Dispatch(&Message);
  6309. }
  6310. #endif
  6311. //---------------------------------------------------------------------------
  6312. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6313. {
  6314. }
  6315. //---------------------------------------------------------------------------
  6316. void __fastcall TCustomScpExplorerForm::DoShow()
  6317. {
  6318. // only now are the controls resized finally, so the size constraints
  6319. // will not conflict with possibly very small window size
  6320. RestoreFormParams();
  6321. FixControlsPlacement();
  6322. if (Position == poDefaultPosOnly)
  6323. {
  6324. CutFormToDesktop(this);
  6325. }
  6326. TForm::DoShow();
  6327. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6328. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6329. if (Terminal == NULL)
  6330. {
  6331. StartingDisconnected();
  6332. }
  6333. UpdatePixelsPerInchMainWindowCounter();
  6334. FShowing = true;
  6335. }
  6336. //---------------------------------------------------------------------------
  6337. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6338. {
  6339. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6340. }
  6341. //---------------------------------------------------------------------------
  6342. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6343. {
  6344. DoTerminalListChanged();
  6345. }
  6346. //---------------------------------------------------------------------------
  6347. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6348. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6349. TNotifyEvent OnBalloonClick, TObject * UserData)
  6350. {
  6351. bool Do;
  6352. UnicodeString Message;
  6353. if (E == NULL)
  6354. {
  6355. Message = Str;
  6356. Do = true;
  6357. }
  6358. else
  6359. {
  6360. Do = ExceptionMessage(E, Message);
  6361. }
  6362. if (Do && WinConfiguration->BalloonNotifications)
  6363. {
  6364. UnicodeString Title;
  6365. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6366. {
  6367. Message = TrimLeft(Message);
  6368. }
  6369. else
  6370. {
  6371. Message = UnformatMessage(Message);
  6372. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6373. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6374. Title = LoadResourceString(Captions[Type]);
  6375. if (Terminal != NULL)
  6376. {
  6377. Title = FORMAT(L"%s - %s",
  6378. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6379. }
  6380. }
  6381. if (Seconds == 0)
  6382. {
  6383. Seconds = WinConfiguration->NotificationsTimeout;
  6384. }
  6385. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6386. }
  6387. }
  6388. //---------------------------------------------------------------------------
  6389. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6390. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6391. UnicodeString HelpKeyword, const TMessageParams * Params,
  6392. TTerminal * Terminal)
  6393. {
  6394. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6395. (FOnFeedSynchronizeError != NULL)) &&
  6396. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6397. {
  6398. if (FOnFeedSynchronizeError != NULL)
  6399. {
  6400. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6401. }
  6402. else
  6403. {
  6404. DebugAssert(FErrorList != NULL);
  6405. TStringList * MoreMessagesCopy = NULL;
  6406. if (MoreMessages)
  6407. {
  6408. MoreMessagesCopy = new TStringList();
  6409. MoreMessagesCopy->Assign(MoreMessages);
  6410. }
  6411. FErrorList->AddObject(Message, MoreMessagesCopy);
  6412. }
  6413. PopupTrayBalloon(Terminal, Message, Type);
  6414. return ContinueAnswer(Answers);
  6415. }
  6416. else
  6417. {
  6418. bool UseBalloon = FTrayIcon->Visible;
  6419. if (UseBalloon)
  6420. {
  6421. PopupTrayBalloon(Terminal, Message, Type);
  6422. }
  6423. unsigned int Result;
  6424. try
  6425. {
  6426. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6427. }
  6428. __finally
  6429. {
  6430. // cancel only balloon we popped up, otherwise we may cancel notification
  6431. // balloon that was there before the message dialog
  6432. // (such as "dd confirmation opt in balloon)
  6433. if (UseBalloon)
  6434. {
  6435. FTrayIcon->CancelBalloon();
  6436. }
  6437. }
  6438. return Result;
  6439. }
  6440. }
  6441. //---------------------------------------------------------------------------
  6442. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6443. TTerminal * Terminal, Exception * E)
  6444. {
  6445. if (FTrayIcon->Visible)
  6446. {
  6447. PopupTrayBalloon(Terminal, L"", qtError, E);
  6448. }
  6449. // particularly prevent opening new session from jump list,
  6450. // while exception is shown
  6451. NonVisualDataModule->StartBusy();
  6452. try
  6453. {
  6454. ShowExtendedExceptionEx(Terminal, E);
  6455. }
  6456. __finally
  6457. {
  6458. NonVisualDataModule->EndBusy();
  6459. FTrayIcon->CancelBalloon();
  6460. }
  6461. }
  6462. //---------------------------------------------------------------------------
  6463. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6464. {
  6465. // cannot rely on active page being page for active terminal,
  6466. // as it can happen that active page is the "new session" page
  6467. // (e.g. when reconnecting active terminal, while login dialog
  6468. // invoked from "new session" page is modal)
  6469. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6470. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6471. }
  6472. //---------------------------------------------------------------------------
  6473. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6474. TTerminal * Terminal, Exception * E)
  6475. {
  6476. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6477. if (!Terminal->Active)
  6478. {
  6479. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6480. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6481. {
  6482. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6483. }
  6484. }
  6485. }
  6486. //---------------------------------------------------------------------------
  6487. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6488. UnicodeString Message, TQueryType Type,
  6489. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6490. {
  6491. if ((E == NULL) ||
  6492. ExceptionMessage(E, Message))
  6493. {
  6494. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6495. if (Important)
  6496. {
  6497. Seconds *= 5;
  6498. }
  6499. UnicodeString NoteMessage(UnformatMessage(Message));
  6500. if (Terminal != NULL)
  6501. {
  6502. NoteMessage = FORMAT(L"%s: %s",
  6503. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6504. }
  6505. if (WinConfiguration->BalloonNotifications)
  6506. {
  6507. AddNote(NoteMessage);
  6508. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6509. }
  6510. else
  6511. {
  6512. FlashOnBackground();
  6513. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6514. }
  6515. }
  6516. }
  6517. //---------------------------------------------------------------------------
  6518. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6519. {
  6520. RestoreApp();
  6521. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6522. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6523. !NonVisualDataModule->Busy)
  6524. {
  6525. TTerminal * Terminal = TerminalNoteData->Terminal;
  6526. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6527. if (!ComponentVisible[fcQueueView])
  6528. {
  6529. ToggleQueueVisibility();
  6530. GoToQueue();
  6531. }
  6532. }
  6533. }
  6534. //---------------------------------------------------------------------------
  6535. void __fastcall TCustomScpExplorerForm::QueueEvent(TTerminal * ATerminal,
  6536. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6537. {
  6538. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  6539. UnicodeString Message;
  6540. TNotifyEvent OnClick = NULL;
  6541. TObject * UserData = NULL;
  6542. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6543. switch (Event)
  6544. {
  6545. case qeEmptyButMonitored:
  6546. if ((ATerminal != Terminal) || QueueInvisible)
  6547. {
  6548. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6549. OnClick = QueueEmptyNoteClicked;
  6550. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6551. TerminalNoteData->Terminal = ATerminal;
  6552. UserData = TerminalNoteData;
  6553. }
  6554. break;
  6555. case qeEmpty:
  6556. OperationComplete(ManagedTerminal->QueueOperationStart);
  6557. break;
  6558. case qePendingUserAction:
  6559. if ((ATerminal != Terminal) ||
  6560. (QueueInvisible && !IsQueueAutoPopup()))
  6561. {
  6562. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6563. }
  6564. break;
  6565. default:
  6566. DebugFail();
  6567. }
  6568. if (!Message.IsEmpty())
  6569. {
  6570. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6571. }
  6572. }
  6573. //---------------------------------------------------------------------------
  6574. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6575. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6576. {
  6577. if (FDDExtMapFile != NULL)
  6578. {
  6579. CloseHandle(FDDExtMapFile);
  6580. FDDExtMapFile = NULL;
  6581. }
  6582. if (WinConfiguration->DDFakeFile)
  6583. {
  6584. DDFakeFileInitDrag(FileList, Created);
  6585. }
  6586. }
  6587. //---------------------------------------------------------------------------
  6588. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6589. {
  6590. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6591. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6592. {
  6593. FFakeFileDropTarget = FileName;
  6594. }
  6595. }
  6596. //---------------------------------------------------------------------------
  6597. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6598. {
  6599. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6600. if (!ForceDirectories(ApiPath(Result)))
  6601. {
  6602. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6603. }
  6604. FileSetAttr(ApiPath(Result), faHidden);
  6605. return Result;
  6606. }
  6607. //---------------------------------------------------------------------------
  6608. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6609. bool & Created)
  6610. {
  6611. FFakeFileDropTarget = UnicodeString();
  6612. FDragFakeDirectory = CreateFakeTransferDirectory();
  6613. FileList->AddItem(NULL, FDragFakeDirectory);
  6614. Created = true;
  6615. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6616. {
  6617. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6618. }
  6619. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6620. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6621. {
  6622. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6623. TDragExtCommStruct* CommStruct;
  6624. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6625. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6626. DebugAssert(CommStruct != NULL);
  6627. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6628. CommStruct->Dragging = true;
  6629. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6630. LENOF(CommStruct->DropDest));
  6631. NULL_TERMINATE(CommStruct->DropDest);
  6632. UnmapViewOfFile(CommStruct);
  6633. }
  6634. }
  6635. //---------------------------------------------------------------------------
  6636. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6637. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6638. {
  6639. bool Result;
  6640. if (Sender == RemoteDirView)
  6641. {
  6642. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6643. }
  6644. else
  6645. {
  6646. DebugAssert(Sender == RemoteDriveView);
  6647. TStrings * FileList = RemoteDriveView->DragFileList();
  6648. try
  6649. {
  6650. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6651. }
  6652. __finally
  6653. {
  6654. delete FileList;
  6655. }
  6656. }
  6657. if (FDDTargetControl == RemoteDriveView)
  6658. {
  6659. RemoteDriveView->UpdateDropTarget();
  6660. }
  6661. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6662. // or from tree view to dir view.
  6663. // foMove happens when file/dir is dragged from remote tree view outside of
  6664. // application via dragex
  6665. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6666. (DropSourceControl == RemoteDriveView))
  6667. {
  6668. RemoteDriveView->UpdateDropSource();
  6669. }
  6670. return Result;
  6671. }
  6672. //---------------------------------------------------------------------------
  6673. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6674. {
  6675. // This also handles drops of remote files to queue.
  6676. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6677. SAFE_DESTROY(FDDFileList);
  6678. if (!FFakeFileDropTarget.IsEmpty())
  6679. {
  6680. RemoveDir(ApiPath(FFakeFileDropTarget));
  6681. }
  6682. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6683. {
  6684. try
  6685. {
  6686. TDragResult DDResult = (Sender == RemoteDirView) ?
  6687. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6688. // Focus is moved to the target application,
  6689. // but as we are going to present the UI, we need to steal the focus back.
  6690. // This most likely won't work though (windows does not allow application
  6691. // to steal focus most of the time)
  6692. Application->BringToFront();
  6693. // On older version of Windows we never got drMove here, see also comment below.
  6694. // On Windows 10, we get the "move".
  6695. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6696. {
  6697. UnicodeString TargetDirectory;
  6698. TFileOperation Operation;
  6699. // drInvalid may mean drMove, see comment below
  6700. switch (DDResult)
  6701. {
  6702. case drCopy:
  6703. Operation = foCopy;
  6704. break;
  6705. case drMove:
  6706. Operation = foMove;
  6707. break;
  6708. default:
  6709. DebugFail();
  6710. case drInvalid:
  6711. // prefer "copy" for safety
  6712. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6713. break;
  6714. }
  6715. TTransferOperationParam Param;
  6716. UnicodeString CounterName;
  6717. bool ForceQueue;
  6718. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6719. {
  6720. // we get drInvalid both if d&d was intercepted by ddext,
  6721. // and when users drops on no-drop location.
  6722. // we tell the difference by existence of response from ddext,
  6723. // so we ignore absence of response in this case
  6724. if (DDResult != drInvalid)
  6725. {
  6726. // here we know that the extension is installed,
  6727. // as it is checked as soon as drag&drop starts from
  6728. // RemoteFileControlDDCreateDragFileList
  6729. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6730. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  6731. {
  6732. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  6733. }
  6734. else
  6735. {
  6736. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6737. }
  6738. }
  6739. }
  6740. else
  6741. {
  6742. // download using fake file
  6743. Param.Temp = false;
  6744. Param.DragDrop = true;
  6745. if (ForceQueue)
  6746. {
  6747. Param.Queue = asOn;
  6748. }
  6749. if (Sender == RemoteDirView)
  6750. {
  6751. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6752. }
  6753. if (RemoteFileControlFileOperation(Sender, Operation,
  6754. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6755. {
  6756. Configuration->Usage->Inc(CounterName);
  6757. }
  6758. }
  6759. }
  6760. }
  6761. __finally
  6762. {
  6763. CloseHandle(FDDExtMapFile);
  6764. FDDExtMapFile = NULL;
  6765. delete FDragFakeMonitors;
  6766. FDragFakeMonitors = NULL;
  6767. RemoveDir(ApiPath(FDragFakeDirectory));
  6768. FDragFakeDirectory = L"";
  6769. FFakeFileDropTarget = UnicodeString();
  6770. }
  6771. }
  6772. if (FDragDropSshTerminate.get() != NULL)
  6773. {
  6774. FDragDropSshTerminate->Rethrow();
  6775. }
  6776. }
  6777. //---------------------------------------------------------------------------
  6778. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6779. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  6780. {
  6781. // Remember drop effect so we know (when user drops files), if we copy or move
  6782. FLastDropEffect = dwEffect;
  6783. }
  6784. //---------------------------------------------------------------------------
  6785. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6786. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6787. {
  6788. bool Result;
  6789. if (FDDTargetControl == QueueView3)
  6790. {
  6791. Directory = DefaultDownloadTargetDirectory();
  6792. Result = true;
  6793. CounterName = L"DownloadsDragDropQueue";
  6794. ForceQueue = true;
  6795. }
  6796. else if (!FFakeFileDropTarget.IsEmpty())
  6797. {
  6798. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6799. Result = true;
  6800. ForceQueue = false;
  6801. CounterName = L"DownloadsDragDropFakeFile";
  6802. }
  6803. else
  6804. {
  6805. ForceQueue = false;
  6806. Enabled = false;
  6807. try
  6808. {
  6809. int Timer = 0;
  6810. Result = false;
  6811. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6812. {
  6813. {
  6814. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6815. TDragExtCommStruct* CommStruct;
  6816. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6817. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6818. DebugAssert(CommStruct != NULL);
  6819. Result = !CommStruct->Dragging;
  6820. if (Result)
  6821. {
  6822. Directory = ExtractFilePath(CommStruct->DropDest);
  6823. CounterName = L"DownloadsDragDropExternalExt";
  6824. }
  6825. UnmapViewOfFile(CommStruct);
  6826. }
  6827. if (!Result)
  6828. {
  6829. Sleep(50);
  6830. Timer += 50;
  6831. Application->ProcessMessages();
  6832. }
  6833. }
  6834. }
  6835. __finally
  6836. {
  6837. Enabled = true;
  6838. }
  6839. }
  6840. return Result;
  6841. }
  6842. //---------------------------------------------------------------------------
  6843. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  6844. const UnicodeString TempDir, int SecDelay)
  6845. {
  6846. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  6847. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  6848. if (FDelayedDeletionTimer == NULL)
  6849. {
  6850. DebugAssert(HandleAllocated());
  6851. FDelayedDeletionTimer = new TTimer(this);
  6852. FDelayedDeletionTimer->Interval = 10000;
  6853. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  6854. }
  6855. else
  6856. {
  6857. FDelayedDeletionTimer->Enabled = true;
  6858. }
  6859. }
  6860. //---------------------------------------------------------------------------
  6861. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  6862. {
  6863. DebugAssert(FDelayedDeletionList != NULL);
  6864. TDateTime N = Now();
  6865. TDateTime Alert;
  6866. UnicodeString Directory;
  6867. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  6868. {
  6869. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  6870. if ((N >= Alert) || (Sender == NULL))
  6871. {
  6872. Directory = FDelayedDeletionList->Strings[Index];
  6873. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  6874. {
  6875. FDelayedDeletionList->Delete(Index);
  6876. }
  6877. }
  6878. }
  6879. if (FDelayedDeletionList->Count == 0)
  6880. {
  6881. FDelayedDeletionTimer->Enabled = false;
  6882. }
  6883. }
  6884. //---------------------------------------------------------------------------
  6885. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  6886. {
  6887. if (IsFileControl(FDDTargetControl, osRemote) ||
  6888. (FDDTargetControl == SessionsPageControl))
  6889. {
  6890. TTerminal * TargetTerminal = NULL;
  6891. TFileOperation Operation = foNone;
  6892. if (FDDTargetControl == SessionsPageControl)
  6893. {
  6894. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  6895. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  6896. // do not allow dropping on the "+" tab
  6897. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  6898. if (TargetTerminal != NULL)
  6899. {
  6900. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  6901. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  6902. {
  6903. Operation = foRemoteMove;
  6904. }
  6905. else
  6906. {
  6907. Operation = foRemoteCopy;
  6908. }
  6909. }
  6910. }
  6911. else
  6912. {
  6913. if (FLastDropEffect == DROPEFFECT_MOVE)
  6914. {
  6915. Operation = foRemoteMove;
  6916. }
  6917. else if (FLastDropEffect == DROPEFFECT_COPY)
  6918. {
  6919. Operation = foRemoteCopy;
  6920. }
  6921. }
  6922. if (Operation != foNone)
  6923. {
  6924. RemoteFileControlFileOperation(DropSourceControl,
  6925. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  6926. }
  6927. // abort drag&drop
  6928. Abort();
  6929. }
  6930. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  6931. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  6932. (FDDTargetControl != QueueView3))
  6933. {
  6934. DebugAssert(!FDragTempDir.IsEmpty());
  6935. TTransferType Type;
  6936. UnicodeString TempDir = FDragTempDir;
  6937. // We clear FDragTempDir before calling
  6938. // just in case it fail (raises exception)
  6939. FDragTempDir = L"";
  6940. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  6941. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  6942. // empty directory parameter means temp directory -> don't display it!
  6943. UnicodeString TargetDir = L"";
  6944. int Options = 0;
  6945. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  6946. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  6947. {
  6948. Abort();
  6949. }
  6950. // TargetDir is set when dropped on local file control
  6951. // (this was workaround for legacy dirview event handling, now it should be
  6952. // made prettier)
  6953. if (TargetDir.IsEmpty())
  6954. {
  6955. TargetDir = TempDir;
  6956. if (ForceDirectories(ApiPath(TargetDir)))
  6957. {
  6958. DebugAssert(Terminal && !TargetDir.IsEmpty());
  6959. FPendingTempSpaceWarn = true;
  6960. try
  6961. {
  6962. FDragDropOperation = true;
  6963. int Params = cpTemporary |
  6964. (Type == ttMove ? cpDelete : 0);
  6965. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  6966. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  6967. }
  6968. __finally
  6969. {
  6970. FDragDropOperation = false;
  6971. FPendingTempSpaceWarn = false;
  6972. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  6973. }
  6974. }
  6975. else
  6976. {
  6977. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  6978. }
  6979. }
  6980. }
  6981. }
  6982. //---------------------------------------------------------------------------
  6983. void __fastcall TCustomScpExplorerForm::DDDownload(
  6984. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  6985. {
  6986. TAutoBatch AutoBatch(this);
  6987. UpdateCopyParamCounters(*CopyParam);
  6988. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  6989. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  6990. {
  6991. RemoteDriveView->UpdateDropSource();
  6992. }
  6993. }
  6994. //---------------------------------------------------------------------------
  6995. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  6996. {
  6997. public:
  6998. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  6999. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7000. {
  7001. }
  7002. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7003. {
  7004. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7005. TDataObjectFilesEx::AllowData(FormatEtc);
  7006. }
  7007. };
  7008. //---------------------------------------------------------------------------
  7009. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7010. TObject * Sender, TDataObject *& DataObject)
  7011. {
  7012. if (FDDExtMapFile != NULL)
  7013. {
  7014. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7015. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7016. {
  7017. Abort();
  7018. }
  7019. if (FileList->Count > 0)
  7020. {
  7021. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7022. if (!FilesObject->IsValid(true, true))
  7023. {
  7024. FilesObject->_Release();
  7025. }
  7026. else
  7027. {
  7028. DataObject = FilesObject;
  7029. }
  7030. }
  7031. }
  7032. }
  7033. //---------------------------------------------------------------------------
  7034. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7035. {
  7036. DebugFail();
  7037. }
  7038. //---------------------------------------------------------------------------
  7039. void __fastcall TCustomScpExplorerForm::GoToTree()
  7040. {
  7041. ComponentVisible[fcRemoteTree] = true;
  7042. RemoteDriveView->SetFocus();
  7043. }
  7044. //---------------------------------------------------------------------------
  7045. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7046. TPanelExport Export)
  7047. {
  7048. TCustomDirView * DirView = this->DirView(Side);
  7049. std::unique_ptr<TStrings> ExportData(new TStringList());
  7050. switch (Export)
  7051. {
  7052. case pePath:
  7053. ExportData->Add(DirView->PathName);
  7054. break;
  7055. case peFileList:
  7056. case peFullFileList:
  7057. {
  7058. bool FullPath = (Export == peFullFileList);
  7059. DirView->CreateFileList(false, FullPath, ExportData.get());
  7060. for (int Index = 0; Index < ExportData->Count; Index++)
  7061. {
  7062. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7063. {
  7064. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7065. }
  7066. }
  7067. }
  7068. break;
  7069. default:
  7070. DebugFail();
  7071. }
  7072. return ExportData.release();
  7073. }
  7074. //---------------------------------------------------------------------------
  7075. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7076. TPanelExport Export, TPanelExportDestination Destination)
  7077. {
  7078. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7079. PanelExportStore(Side, Export, Destination, ExportData.get());
  7080. }
  7081. //---------------------------------------------------------------------------
  7082. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7083. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7084. TStrings * ExportData)
  7085. {
  7086. if (Destination == pedClipboard)
  7087. {
  7088. TInstantOperationVisualizer Visualizer;
  7089. CopyToClipboard(ExportData);
  7090. }
  7091. else
  7092. {
  7093. DebugFail();
  7094. }
  7095. }
  7096. //---------------------------------------------------------------------------
  7097. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7098. {
  7099. TCustomDirView * DirView = this->DirView(Side);
  7100. UnicodeString Mask = DirView->Mask;
  7101. if (DoFilterMaskDialog(DirView, Mask))
  7102. {
  7103. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7104. Configuration->Usage->Inc(L"Filters");
  7105. }
  7106. }
  7107. //---------------------------------------------------------------------------
  7108. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7109. {
  7110. return FQueueController->DefaultOperation();
  7111. }
  7112. //---------------------------------------------------------------------------
  7113. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7114. TQueueOperation Operation, void ** Param)
  7115. {
  7116. switch (Operation)
  7117. {
  7118. case qoPreferences:
  7119. return true;
  7120. case qoGoTo:
  7121. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7122. case qoOnceEmpty:
  7123. return !FQueueController->Empty;
  7124. default:
  7125. return FQueueController->AllowOperation(Operation, Param);
  7126. }
  7127. }
  7128. //---------------------------------------------------------------------------
  7129. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7130. {
  7131. if (DebugAlwaysTrue(QueueView3->Visible))
  7132. {
  7133. QueueView3->SetFocus();
  7134. }
  7135. }
  7136. //---------------------------------------------------------------------------
  7137. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7138. TQueueOperation Operation, void * Param)
  7139. {
  7140. if (Operation == qoGoTo)
  7141. {
  7142. GoToQueue();
  7143. }
  7144. else if (Operation == qoPreferences)
  7145. {
  7146. PreferencesDialog(pmQueue);
  7147. }
  7148. else
  7149. {
  7150. FQueueController->ExecuteOperation(Operation, Param);
  7151. }
  7152. }
  7153. //---------------------------------------------------------------------------
  7154. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7155. {
  7156. return (Queue != NULL) && Queue->Enabled;
  7157. }
  7158. //---------------------------------------------------------------------------
  7159. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7160. {
  7161. DebugAssert(Queue != NULL);
  7162. if (Queue != NULL)
  7163. {
  7164. Queue->Enabled = !Queue->Enabled;
  7165. }
  7166. }
  7167. //---------------------------------------------------------------------------
  7168. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7169. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7170. {
  7171. FQueueActedItem = QueueView3->ItemFocused;
  7172. }
  7173. //---------------------------------------------------------------------------
  7174. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7175. {
  7176. return TopDock->Height + QueueSplitter->Height;
  7177. }
  7178. //---------------------------------------------------------------------------
  7179. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7180. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7181. {
  7182. // when queue is hidden by double-clicking splitter, stray attempt to
  7183. // resize the panel with strange value arrives, make sure it is ignored
  7184. if (ComponentVisible[fcQueueView])
  7185. {
  7186. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7187. RemotePanel->Constraints->MinHeight;
  7188. if (NewSize > HeightLimit)
  7189. {
  7190. NewSize = HeightLimit;
  7191. }
  7192. }
  7193. else
  7194. {
  7195. Accept = false;
  7196. }
  7197. }
  7198. //---------------------------------------------------------------------------
  7199. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7200. TDragObject *& /*DragObject*/)
  7201. {
  7202. FQueueActedItem = QueueView3->ItemFocused;
  7203. }
  7204. //---------------------------------------------------------------------------
  7205. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7206. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7207. {
  7208. Accept = true;
  7209. if (Source == QueueView3)
  7210. {
  7211. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7212. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7213. if (Accept)
  7214. {
  7215. TQueueItemProxy * QueueItem;
  7216. TQueueItemProxy * DestQueueItem;
  7217. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7218. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7219. Accept = (QueueItem != DestQueueItem) &&
  7220. (QueueItem->Status == TQueueItem::qsPending) &&
  7221. (DestQueueItem->Status == TQueueItem::qsPending);
  7222. }
  7223. }
  7224. }
  7225. //---------------------------------------------------------------------------
  7226. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7227. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7228. {
  7229. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7230. {
  7231. TQueueItemProxy * QueueItem;
  7232. TQueueItemProxy * DestQueueItem;
  7233. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7234. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7235. QueueItem->Move(DestQueueItem);
  7236. }
  7237. }
  7238. //---------------------------------------------------------------------------
  7239. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7240. {
  7241. if ((QueueView3->ItemFocused == NULL) &&
  7242. (QueueView3->Items->Count > 0))
  7243. {
  7244. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7245. }
  7246. QueueLabelUpdateStatus();
  7247. }
  7248. //---------------------------------------------------------------------------
  7249. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7250. {
  7251. QueueLabelUpdateStatus();
  7252. }
  7253. //---------------------------------------------------------------------------
  7254. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7255. {
  7256. QueueLabel->UpdateStatus();
  7257. }
  7258. //---------------------------------------------------------------------------
  7259. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7260. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7261. {
  7262. if (Selected)
  7263. {
  7264. NonVisualDataModule->UpdateNonVisibleActions();
  7265. }
  7266. }
  7267. //---------------------------------------------------------------------------
  7268. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7269. {
  7270. TDragDropFilesEx * Result = NULL;
  7271. if (Sender == SessionsPageControl)
  7272. {
  7273. Result = FSessionsDragDropFilesEx;
  7274. }
  7275. else if (Sender == QueueView3)
  7276. {
  7277. Result = FQueueDragDropFilesEx;
  7278. }
  7279. else
  7280. {
  7281. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7282. if (DirView != NULL)
  7283. {
  7284. Result = DirView->DragDropFilesEx;
  7285. }
  7286. else
  7287. {
  7288. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7289. if (DriveView != NULL)
  7290. {
  7291. Result = DriveView->DragDropFilesEx;
  7292. }
  7293. }
  7294. }
  7295. DebugAssert(Result != NULL);
  7296. return Result;
  7297. }
  7298. //---------------------------------------------------------------------------
  7299. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7300. {
  7301. return (DView->SelCount == DView->FilesCount);
  7302. }
  7303. //---------------------------------------------------------------------------
  7304. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7305. {
  7306. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7307. }
  7308. //---------------------------------------------------------------------------
  7309. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7310. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7311. {
  7312. TFileOperation Operation;
  7313. switch (Effect)
  7314. {
  7315. case DROPEFFECT_MOVE:
  7316. Operation = foMove;
  7317. break;
  7318. case DROPEFFECT_COPY:
  7319. // occures on WinXP (reported by user)
  7320. default:
  7321. Operation = foCopy;
  7322. break;
  7323. };
  7324. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7325. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7326. if (DragDropFilesEx->FileList->Count > 0)
  7327. {
  7328. TStrings * FileList = new TStringList();
  7329. try
  7330. {
  7331. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7332. {
  7333. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7334. }
  7335. FDragDropOperation = true;
  7336. TTransferOperationParam Param;
  7337. Param.TargetDirectory = TargetPath;
  7338. // upload, no temp dirs
  7339. Param.Temp = false;
  7340. Param.DragDrop = DragDrop;
  7341. Param.Options =
  7342. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7343. if (ForceQueue)
  7344. {
  7345. Param.Queue = asOn;
  7346. }
  7347. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7348. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7349. {
  7350. if (IsFileControl(DropSourceControl, osLocal))
  7351. {
  7352. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7353. }
  7354. else
  7355. {
  7356. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7357. }
  7358. }
  7359. }
  7360. __finally
  7361. {
  7362. FDragDropOperation = false;
  7363. delete FileList;
  7364. }
  7365. }
  7366. }
  7367. //---------------------------------------------------------------------------
  7368. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7369. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7370. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7371. {
  7372. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7373. }
  7374. //---------------------------------------------------------------------------
  7375. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7376. TObject * Sender, int grfKeyState, int & dwEffect)
  7377. {
  7378. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7379. if ((dwEffect != DROPEFFECT_NONE) &&
  7380. IsFileControl(DropSourceControl, osRemote))
  7381. {
  7382. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7383. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7384. (RemoteDirView->DropTarget == NULL))
  7385. {
  7386. dwEffect = DROPEFFECT_NONE;
  7387. }
  7388. else
  7389. {
  7390. if (dwEffect == DROPEFFECT_COPY)
  7391. {
  7392. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7393. // currently we support copying always (at least via temporary directory);
  7394. // remove associated checks once this all proves stable and working
  7395. bool CopyCapable = true;
  7396. // if we do not support neither of operations, there's no discussion
  7397. if (!MoveCapable && !CopyCapable)
  7398. {
  7399. dwEffect = DROPEFFECT_NONE;
  7400. }
  7401. else
  7402. {
  7403. // The default effect inside remote panel is move,
  7404. // unless we do not support it, but support copy
  7405. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7406. {
  7407. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7408. }
  7409. else
  7410. {
  7411. // with ctrl-down, we want copy unless it is not supported
  7412. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7413. }
  7414. }
  7415. }
  7416. }
  7417. }
  7418. }
  7419. //---------------------------------------------------------------------------
  7420. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7421. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7422. {
  7423. if (FDDTotalSize >= 0)
  7424. {
  7425. if (File->IsDirectory)
  7426. {
  7427. FDDTotalSize = -1;
  7428. }
  7429. else
  7430. {
  7431. FDDTotalSize += File->Size;
  7432. }
  7433. }
  7434. DebugAssert(!FDragTempDir.IsEmpty());
  7435. // TODO: this is quite ineffective
  7436. // TODO: what if invalid character replacement is disabled?
  7437. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7438. UnicodeString TransferFileName;
  7439. if (Sender == RemoteDriveView)
  7440. {
  7441. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7442. }
  7443. else
  7444. {
  7445. TransferFileName = File->FileName;
  7446. }
  7447. FDDFileList->AddObject(TransferFileName, File);
  7448. }
  7449. //---------------------------------------------------------------------------
  7450. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7451. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7452. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7453. {
  7454. // sometimes we do not get DDEnd so the list is not released
  7455. SAFE_DESTROY(FDDFileList);
  7456. FDDFileList = new TStringList();
  7457. FDragTempDir = WinConfiguration->TemporaryDir();
  7458. FDDTotalSize = 0;
  7459. FDragDropSshTerminate.reset(NULL);
  7460. }
  7461. //---------------------------------------------------------------------------
  7462. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7463. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7464. HRESULT & Result)
  7465. {
  7466. if (Result == DRAGDROP_S_DROP)
  7467. {
  7468. try
  7469. {
  7470. GlobalDragImageList->HideDragImage();
  7471. try
  7472. {
  7473. RemoteFileControlDDTargetDrop();
  7474. }
  7475. catch(ESshTerminate & E)
  7476. {
  7477. FDragDropSshTerminate.reset(E.Clone());
  7478. }
  7479. }
  7480. catch (Exception &E)
  7481. {
  7482. // If downloading fails we need to cancel drag&drop, otherwise
  7483. // Explorer shows error
  7484. // But by the way exception probably never reach this point as
  7485. // it's catched on way.
  7486. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7487. Result = DRAGDROP_S_CANCEL;
  7488. ShowExtendedException(Terminal, &E);
  7489. }
  7490. }
  7491. }
  7492. //---------------------------------------------------------------------------
  7493. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7494. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7495. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7496. {
  7497. TFileMasks::TParams MaskParams;
  7498. MaskParams.Size = Size;
  7499. MaskParams.Modification = Modification;
  7500. // this does not re-parse the mask if it is the same as the last time
  7501. FDirViewMatchMask = Masks;
  7502. bool ImplicitMatch;
  7503. Matches =
  7504. // RecurseInclude parameter has no effect as the Path is empty
  7505. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7506. (AllowImplicitMatches || !ImplicitMatch);
  7507. }
  7508. //---------------------------------------------------------------------------
  7509. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7510. TObject * Sender, TListItem * Item, WORD & Indexes)
  7511. {
  7512. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7513. UnicodeString Ext;
  7514. if (DirView == RemoteDirView)
  7515. {
  7516. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7517. }
  7518. else
  7519. {
  7520. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7521. }
  7522. if (SameText(Ext, Configuration->PartialExt))
  7523. {
  7524. Indexes |= oiPartial;
  7525. }
  7526. }
  7527. //---------------------------------------------------------------------------
  7528. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7529. {
  7530. return
  7531. DirViewEnabled(osCurrent) &&
  7532. DirView(osCurrent)->CanPasteFromClipBoard();
  7533. }
  7534. //---------------------------------------------------------------------------
  7535. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7536. {
  7537. bool Result = false;
  7538. if (CanPasteToDirViewFromClipBoard())
  7539. {
  7540. Result = true;
  7541. }
  7542. else
  7543. {
  7544. UnicodeString ClipboardText;
  7545. if (NonEmptyTextFromClipboard(ClipboardText))
  7546. {
  7547. if (StoredSessions->IsUrl(ClipboardText))
  7548. {
  7549. Result = true;
  7550. }
  7551. else
  7552. {
  7553. Result = DirViewEnabled(osCurrent);
  7554. }
  7555. }
  7556. }
  7557. return Result;
  7558. }
  7559. //---------------------------------------------------------------------------
  7560. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7561. {
  7562. if (DoesClipboardContainOurFiles())
  7563. {
  7564. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7565. {
  7566. TTerminalManager * Manager = TTerminalManager::Instance();
  7567. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7568. Manager->ActiveTerminal = FClipboardTerminal;
  7569. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7570. }
  7571. }
  7572. else if (CanPasteToDirViewFromClipBoard())
  7573. {
  7574. DirView(osCurrent)->PasteFromClipBoard();
  7575. }
  7576. else
  7577. {
  7578. UnicodeString ClipboardText;
  7579. if (NonEmptyTextFromClipboard(ClipboardText))
  7580. {
  7581. if (StoredSessions->IsUrl(ClipboardText))
  7582. {
  7583. NewSession(false, ClipboardText);
  7584. }
  7585. else
  7586. {
  7587. DirView(osCurrent)->Path = ClipboardText;
  7588. }
  7589. }
  7590. }
  7591. }
  7592. //---------------------------------------------------------------------------
  7593. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7594. {
  7595. // TBD
  7596. }
  7597. //---------------------------------------------------------------------------
  7598. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7599. {
  7600. TCustomDirView * ADirView = DirView(Side);
  7601. ADirView->SelectAll(Mode);
  7602. ADirView->SetFocus();
  7603. }
  7604. //---------------------------------------------------------------------------
  7605. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7606. {
  7607. TCustomDirView * ADirView = DirView(Side);
  7608. TFileFilter Filter;
  7609. DefaultFileFilter(Filter);
  7610. if (DoSelectMaskDialog(ADirView, Select, Filter))
  7611. {
  7612. Configuration->Usage->Inc(L"MaskSelections");
  7613. ADirView->SelectFiles(Filter, Select);
  7614. ADirView->SetFocus();
  7615. }
  7616. }
  7617. //---------------------------------------------------------------------------
  7618. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7619. {
  7620. TCustomDirView * ADirView = DirView(Side);
  7621. ADirView->RestoreSelectedNames();
  7622. ADirView->SetFocus();
  7623. }
  7624. //---------------------------------------------------------------------------
  7625. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7626. {
  7627. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7628. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7629. {
  7630. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7631. UnicodeString Ext;
  7632. if (GetSide(osCurrent) == osRemote)
  7633. {
  7634. Ext = UnixExtractFileExt(FileName);
  7635. }
  7636. else
  7637. {
  7638. Ext = ExtractFileExt(FileName);
  7639. }
  7640. if (Ext.IsEmpty())
  7641. {
  7642. Ext = L".";
  7643. }
  7644. TFileFilter Filter;
  7645. Filter.Masks = FORMAT(L"*%s", (Ext));
  7646. Filter.Directories = false;
  7647. CurrentDirView->SelectFiles(Filter, Select);
  7648. }
  7649. }
  7650. //---------------------------------------------------------------------------
  7651. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7652. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7653. {
  7654. UnicodeString Result;
  7655. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  7656. {
  7657. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  7658. }
  7659. else if ((Side == osRemote) && (Terminal == NULL))
  7660. {
  7661. // noop
  7662. }
  7663. else
  7664. {
  7665. Result =
  7666. FMTLOAD(FILE_INFO_FORMAT,
  7667. (FormatBytes(FileInfo.SelectedSize),
  7668. FormatBytes(FileInfo.FilesSize),
  7669. FormatNumber(FileInfo.SelectedCount),
  7670. FormatNumber(FileInfo.FilesCount)));
  7671. }
  7672. return Result;
  7673. }
  7674. //---------------------------------------------------------------------------
  7675. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7676. TTBXStatusPanel * Panel, TOperationSide Side)
  7677. {
  7678. if (Panel->Index == 1)
  7679. {
  7680. ToggleShowHiddenFiles();
  7681. }
  7682. else if (Panel->Index == 2)
  7683. {
  7684. Filter(Side);
  7685. }
  7686. }
  7687. //---------------------------------------------------------------------------
  7688. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7689. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7690. {
  7691. DebugAssert(!StatusBar->SimplePanel);
  7692. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7693. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7694. }
  7695. //---------------------------------------------------------------------------
  7696. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7697. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7698. {
  7699. DebugAssert(StatusBar->Panels->Count >= 3);
  7700. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7701. if (FileInfo.HiddenCount > 0)
  7702. {
  7703. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7704. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7705. }
  7706. else
  7707. {
  7708. HiddenFilesPanel->ViewPriority = 0;
  7709. // not really necessary, just to cleanup no-longer-valid data
  7710. HiddenFilesPanel->Caption = L"";
  7711. }
  7712. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7713. if (FileInfo.FilteredCount > 0)
  7714. {
  7715. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7716. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7717. }
  7718. else
  7719. {
  7720. FilteredFilesPanel->ViewPriority = 0;
  7721. // not really necessary, just to cleanup no-longer-valid data
  7722. FilteredFilesPanel->Caption = L"";
  7723. }
  7724. }
  7725. //---------------------------------------------------------------------------
  7726. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7727. TObject * /*Sender*/)
  7728. {
  7729. if (RemoteDirView->Enabled)
  7730. {
  7731. RemoteDirView->SetFocus();
  7732. }
  7733. }
  7734. //---------------------------------------------------------------------------
  7735. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7736. {
  7737. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7738. switch (Config.Show)
  7739. {
  7740. case qvShow:
  7741. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7742. {
  7743. Config.Show = qvHide;
  7744. }
  7745. else
  7746. {
  7747. Config.Show = Config.LastHideShow;
  7748. }
  7749. break;
  7750. case qvHideWhenEmpty:
  7751. if (ComponentVisible[fcQueueView])
  7752. {
  7753. Config.Show = qvHide;
  7754. }
  7755. else
  7756. {
  7757. Config.LastHideShow = Config.Show;
  7758. Config.Show = qvShow;
  7759. }
  7760. break;
  7761. case qvHide:
  7762. Config.LastHideShow = Config.Show;
  7763. Config.Show = qvShow;
  7764. break;
  7765. }
  7766. WinConfiguration->QueueView = Config;
  7767. }
  7768. //---------------------------------------------------------------------------
  7769. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7770. {
  7771. UnicodeString Result;
  7772. if (FTerminal != NULL)
  7773. {
  7774. switch (WinConfiguration->PathInCaption)
  7775. {
  7776. case picShort:
  7777. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7778. break;
  7779. case picFull:
  7780. Result = FTerminal->CurrentDirectory;
  7781. break;
  7782. }
  7783. }
  7784. return Result;
  7785. }
  7786. //---------------------------------------------------------------------------
  7787. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  7788. {
  7789. TColor Result = GetWindowColor(FSessionColor);
  7790. return Result;
  7791. }
  7792. //---------------------------------------------------------------------------
  7793. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  7794. {
  7795. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  7796. return Result;
  7797. }
  7798. //---------------------------------------------------------------------------
  7799. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7800. {
  7801. TTerminalManager::Instance()->UpdateAppTitle();
  7802. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7803. // background to gray on Windows 7 (works on Windows 10).
  7804. // See also EnableControl
  7805. if (Showing)
  7806. {
  7807. bool HasTerminal = (Terminal != NULL) && Terminal->Active;
  7808. if (HasTerminal)
  7809. {
  7810. // Update path when it changes
  7811. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7812. {
  7813. UpdateSessionTab(SessionsPageControl->ActivePage);
  7814. }
  7815. if (!RemoteDirView->Enabled)
  7816. {
  7817. RemoteDirView->Enabled = true;
  7818. if (FRemoteDirViewWasFocused)
  7819. {
  7820. ActiveControl = RemoteDirView;
  7821. }
  7822. }
  7823. RemoteDirView->Color = PanelColor();
  7824. }
  7825. else
  7826. {
  7827. if (RemoteDirView->Enabled)
  7828. {
  7829. // This is first called when the form is being constructed
  7830. // (not anymore due to Showing test above)
  7831. // but the false is overriden in the constructor later.
  7832. // An even later in TScpCommanderForm::DoShow()
  7833. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  7834. RemoteDirView->Enabled = false;
  7835. RemoteDirView->Color = DisabledPanelColor();
  7836. }
  7837. }
  7838. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  7839. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  7840. RemoteDriveView->Color = RemoteDirView->Color;
  7841. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  7842. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fsBackgroundTransfers];
  7843. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  7844. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  7845. QueueLabelUpdateStatus();
  7846. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  7847. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  7848. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  7849. }
  7850. }
  7851. //---------------------------------------------------------------------------
  7852. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  7853. {
  7854. UpdateControls();
  7855. }
  7856. //---------------------------------------------------------------------------
  7857. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  7858. TObject * Sender)
  7859. {
  7860. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7861. DebugAssert(DirView != NULL);
  7862. DoDirViewLoaded(DirView);
  7863. TransferPresetAutoSelect();
  7864. }
  7865. //---------------------------------------------------------------------------
  7866. void __fastcall TCustomScpExplorerForm::StartUpdates()
  7867. {
  7868. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  7869. // first run after installation
  7870. if (double(Updates.LastCheck) == 0)
  7871. {
  7872. // make sure next time there will be an update (if enabled)
  7873. Updates.LastCheck = TDateTime(1);
  7874. WinConfiguration->Updates = Updates;
  7875. }
  7876. else if ((double(Updates.Period) > 0) &&
  7877. (Now() - Updates.LastCheck >= Updates.Period))
  7878. {
  7879. TThreadMethod OnUpdatesChecked = NULL;
  7880. if (!IsUWP())
  7881. {
  7882. OnUpdatesChecked = UpdatesChecked;
  7883. }
  7884. StartUpdateThread(OnUpdatesChecked);
  7885. }
  7886. }
  7887. //---------------------------------------------------------------------------
  7888. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  7889. {
  7890. UnicodeString Message;
  7891. bool New;
  7892. TQueryType Type;
  7893. GetUpdatesMessage(Message, New, Type, false);
  7894. if (!Message.IsEmpty())
  7895. {
  7896. if (New)
  7897. {
  7898. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  7899. }
  7900. if (!New && (Type != qtWarning))
  7901. {
  7902. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  7903. }
  7904. else
  7905. {
  7906. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  7907. }
  7908. if (New)
  7909. {
  7910. Configuration->Usage->Inc(L"UpdateNotifications");
  7911. }
  7912. }
  7913. }
  7914. //---------------------------------------------------------------------------
  7915. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  7916. {
  7917. RestoreApp();
  7918. bool CanDisplay = !NonVisualDataModule->Busy;
  7919. if (!CanDisplay && (Screen->ActiveForm != NULL))
  7920. {
  7921. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  7922. }
  7923. if (CanDisplay)
  7924. {
  7925. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  7926. CheckForUpdates(true);
  7927. }
  7928. else
  7929. {
  7930. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  7931. }
  7932. }
  7933. //---------------------------------------------------------------------------
  7934. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  7935. TCopyParamRuleData & Data)
  7936. {
  7937. DebugAssert(Terminal != NULL);
  7938. Data.HostName = Terminal->SessionData->HostNameExpanded;
  7939. Data.UserName = Terminal->SessionData->UserNameExpanded;
  7940. Data.RemoteDirectory = RemoteDirView->PathName;
  7941. }
  7942. //---------------------------------------------------------------------------
  7943. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  7944. {
  7945. // Terminal can be null when we are changing local directory implicitly,
  7946. // because it has been deleted, while no session is connected
  7947. // (Login dialog is open)
  7948. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  7949. {
  7950. TCopyParamRuleData Data;
  7951. GetTransferPresetAutoSelectData(Data);
  7952. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  7953. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  7954. if (CopyParamIndex < 0)
  7955. {
  7956. // there is no preset that matches autoselection
  7957. // set preset that we consider "default"
  7958. FCopyParamAutoSelected = L""; // forget last autoselected preset
  7959. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  7960. }
  7961. else
  7962. {
  7963. // there is preset matching autoselection
  7964. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  7965. if (CopyParamName == FCopyParamAutoSelected)
  7966. {
  7967. // autoselected the same preset as the last time
  7968. // make no change (i.e. preserve custom user preset, if any)
  7969. }
  7970. else
  7971. {
  7972. // autoselected the different preset then the last time (or there
  7973. // was default preset set)
  7974. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  7975. GUIConfiguration->CopyParamCurrent = CopyParamName;
  7976. }
  7977. }
  7978. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  7979. {
  7980. if (CopyParamIndex >= 0)
  7981. {
  7982. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  7983. }
  7984. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  7985. try
  7986. {
  7987. int Fmt =
  7988. (CopyParamIndex < 0) ?
  7989. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  7990. COPY_PARAM_AUTOSELECTED;
  7991. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  7992. Data->Message = MainInstructions(Message);
  7993. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  7994. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  7995. CopyParamAttrs);
  7996. if (CopyParamIndex >= 0)
  7997. {
  7998. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  7999. Info = FORMAT(L"%s\n \n%s", (Info,
  8000. FMTLOAD(COPY_PARAM_RULE,
  8001. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8002. }
  8003. Data->Message += L"\n\n" + Info;
  8004. if (WinConfiguration->CopyParamAutoSelectNotice)
  8005. {
  8006. TransferPresetNoteMessage(Data, true);
  8007. }
  8008. else
  8009. {
  8010. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8011. Data = NULL; // ownership passed
  8012. }
  8013. }
  8014. __finally
  8015. {
  8016. delete Data;
  8017. }
  8018. }
  8019. }
  8020. }
  8021. //---------------------------------------------------------------------------
  8022. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8023. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8024. {
  8025. DebugAssert(NoteData != NULL);
  8026. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8027. TQueryButtonAlias Aliases[1];
  8028. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8029. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8030. Params.Aliases = Aliases;
  8031. Params.AliasesCount = LENOF(Aliases);
  8032. unsigned int Result =
  8033. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8034. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8035. switch (Result)
  8036. {
  8037. case qaNeverAskAgain:
  8038. DebugAssert(AllowNeverAskAgain);
  8039. WinConfiguration->CopyParamAutoSelectNotice = false;
  8040. break;
  8041. case qaIgnore:
  8042. PreferencesDialog(pmPresets);
  8043. break;
  8044. }
  8045. }
  8046. //---------------------------------------------------------------------------
  8047. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8048. {
  8049. // as of now useless, as this is used for notes only, never for balloons, ...
  8050. RestoreApp();
  8051. // .. and we should never be busy here
  8052. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8053. {
  8054. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8055. }
  8056. }
  8057. //---------------------------------------------------------------------------
  8058. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8059. TPreferencesMode APreferencesMode)
  8060. {
  8061. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8062. TCopyParamRuleData Data;
  8063. if (Terminal != NULL)
  8064. {
  8065. PreferencesData.reset(new TPreferencesDialogData());
  8066. GetTransferPresetAutoSelectData(Data);
  8067. PreferencesData->CopyParamRuleData = &Data;
  8068. }
  8069. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8070. }
  8071. //---------------------------------------------------------------------------
  8072. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8073. const TCustomCommandType & Command)
  8074. {
  8075. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8076. {
  8077. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  8078. }
  8079. }
  8080. //---------------------------------------------------------------------------
  8081. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8082. {
  8083. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8084. TCustomCommandType Command;
  8085. // make sure we use local custom command when remote are not supported
  8086. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8087. {
  8088. Command = FLastCustomCommand;
  8089. }
  8090. else
  8091. {
  8092. Command.Params = Command.Params | ccLocal;
  8093. }
  8094. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8095. FEditingFocusedAdHocCommand = OnFocused;
  8096. bool LocalSide = (FCurrentSide == osLocal);
  8097. int Options =
  8098. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8099. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8100. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8101. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8102. {
  8103. FLastCustomCommand = Command;
  8104. UpdateCustomCommandsToolbar();
  8105. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8106. }
  8107. }
  8108. //---------------------------------------------------------------------------
  8109. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8110. {
  8111. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8112. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8113. DebugAssert(State > 0);
  8114. if (State <= 0)
  8115. {
  8116. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8117. }
  8118. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8119. }
  8120. //---------------------------------------------------------------------------
  8121. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8122. TCustomCommandType & Command, int & State)
  8123. {
  8124. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8125. if (Result)
  8126. {
  8127. Command = FLastCustomCommand;
  8128. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8129. }
  8130. return Result;
  8131. }
  8132. //---------------------------------------------------------------------------
  8133. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8134. {
  8135. if (RemoteDirView->ItemFocused != NULL)
  8136. {
  8137. RemoteDirView->ItemFocused->CancelEdit();
  8138. }
  8139. ResetIncrementalSearch();
  8140. }
  8141. //---------------------------------------------------------------------------
  8142. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8143. {
  8144. DebugAssert(ComponentVisible[Component]);
  8145. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8146. }
  8147. //---------------------------------------------------------------------------
  8148. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8149. {
  8150. // when queue panel is resized here directly, the status bar is stretched
  8151. // over whole space the panel occupied
  8152. PostComponentHide(fcQueueView);
  8153. }
  8154. //---------------------------------------------------------------------------
  8155. void __fastcall TCustomScpExplorerForm::WMWinIniChange(TMessage & Message)
  8156. {
  8157. WinConfiguration->ResetSysDarkTheme();
  8158. ConfigurationChanged();
  8159. ConfigureInterface();
  8160. TForm::Dispatch(&Message);
  8161. }
  8162. //---------------------------------------------------------------------------
  8163. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8164. {
  8165. TMessage * M = static_cast<TMessage*>(Message);
  8166. switch (M->Msg)
  8167. {
  8168. case CM_DIALOGCHAR:
  8169. CMDialogChar(*M);
  8170. break;
  8171. case WM_APPCOMMAND:
  8172. WMAppCommand(*M);
  8173. break;
  8174. case WM_SYSCOMMAND:
  8175. WMSysCommand(*M);
  8176. break;
  8177. case WM_QUERYENDSESSION:
  8178. WMQueryEndSession(*M);
  8179. break;
  8180. case WM_ENDSESSION:
  8181. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8182. break;
  8183. #ifdef _DEBUG
  8184. case WM_WINDOWPOSCHANGED:
  8185. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8186. break;
  8187. case CM_CUSTOMSTYLECHANGED:
  8188. TForm::Dispatch(Message);
  8189. break;
  8190. #endif
  8191. case WM_COMPONENT_HIDE:
  8192. {
  8193. Byte Component = static_cast<Byte>(M->WParam);
  8194. // sanity check
  8195. if (ComponentVisible[Component])
  8196. {
  8197. // special treatment
  8198. if (Component == fcQueueView)
  8199. {
  8200. ToggleQueueVisibility();
  8201. DebugAssert(!ComponentVisible[fcQueueView]);
  8202. }
  8203. else
  8204. {
  8205. ComponentVisible[Component] = false;
  8206. }
  8207. }
  8208. }
  8209. break;
  8210. case WM_COPYDATA:
  8211. WMCopyData(*M);
  8212. break;
  8213. case WM_MANAGES_CAPTION:
  8214. // caption managed in TTerminalManager::UpdateAppTitle()
  8215. M->Result = 1;
  8216. break;
  8217. case WM_WANTS_MOUSEWHEEL:
  8218. M->Result = 1;
  8219. break;
  8220. case CM_SHOWINGCHANGED:
  8221. CMShowingChanged(*M);
  8222. break;
  8223. case WM_CLOSE:
  8224. WMClose(*M);
  8225. break;
  8226. case WM_DPICHANGED:
  8227. WMDpiChanged(*M);
  8228. break;
  8229. case CM_DPICHANGED:
  8230. CMDpiChanged(*M);
  8231. break;
  8232. case WM_WININICHANGE:
  8233. WMWinIniChange(*M);
  8234. break;
  8235. case CM_DIALOGKEY:
  8236. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8237. break;
  8238. default:
  8239. TForm::Dispatch(Message);
  8240. break;
  8241. }
  8242. }
  8243. //---------------------------------------------------------------------------
  8244. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8245. {
  8246. // noop
  8247. }
  8248. //---------------------------------------------------------------------------
  8249. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8250. {
  8251. TForm::Dispatch(&Message);
  8252. FSessionColors->Clear();
  8253. AddFixedSessionImages();
  8254. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8255. UpdateImages();
  8256. }
  8257. //---------------------------------------------------------------------------
  8258. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8259. {
  8260. TForm::Dispatch(&Message);
  8261. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8262. UpdatePixelsPerInchMainWindowCounter();
  8263. }
  8264. //---------------------------------------------------------------------------
  8265. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8266. {
  8267. // Cannot close window while we are busy.
  8268. // We cannot test this in FormCloseQuery as that is called also from
  8269. // Close(), which is called by CloseApplicationAction. So we can be busy
  8270. // there already even, when it is legitimate to close the application.
  8271. // Possibly a better place to handle this would be WMSysCommand.
  8272. if (NonVisualDataModule->Busy)
  8273. {
  8274. Message.Result = 1;
  8275. }
  8276. else
  8277. {
  8278. TForm::Dispatch(&Message);
  8279. }
  8280. }
  8281. //---------------------------------------------------------------------------
  8282. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8283. {
  8284. TForm::Dispatch(&Message);
  8285. if (Showing && (Terminal == NULL))
  8286. {
  8287. // When we are starting minimized (i.e. from an installer),
  8288. // postpone showing Login dialog until we get restored.
  8289. // Otherwise the Login dialog (and Authentication window) show restored
  8290. // over invidible (minimized) main window.
  8291. if (WindowState == wsMinimized)
  8292. {
  8293. FNeedSession = true;
  8294. }
  8295. else
  8296. {
  8297. // This happens before application ever goes idle, so the toolbars would
  8298. // stay enabled (initial state) until the Login dialog is dismissed.
  8299. UpdateActions();
  8300. NonVisualDataModule->StartBusy();
  8301. try
  8302. {
  8303. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8304. // otherwise the dialog does not receive focus.
  8305. // With Commander interface the ProcessMessages is called already
  8306. // by TDriveView, but with Explorer interface, we need to call it explicily
  8307. Application->ProcessMessages();
  8308. // do not reload sessions, they have been loaded just now (optimization)
  8309. NeedSession(false);
  8310. }
  8311. __finally
  8312. {
  8313. NonVisualDataModule->EndBusy();
  8314. }
  8315. }
  8316. }
  8317. }
  8318. //---------------------------------------------------------------------------
  8319. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8320. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8321. int & MaxHeight)
  8322. {
  8323. // workaround for bug in TWinControl.CalcConstraints
  8324. // Check for empty rect (restore from iconic state) is done there only after
  8325. // call to AdjustClientRect, which enlarges the rect (for forms).
  8326. TRect R = GetClientRect();
  8327. // when restoring from iconic state, place no restrictions
  8328. if (IsRectEmpty(R))
  8329. {
  8330. MinWidth = 0;
  8331. MinHeight = 0;
  8332. MaxWidth = 32000;
  8333. MaxHeight = 32000;
  8334. }
  8335. }
  8336. //---------------------------------------------------------------------------
  8337. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8338. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8339. {
  8340. // terminal can be already closed (e.g. dropped connection)
  8341. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8342. {
  8343. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8344. try
  8345. {
  8346. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8347. }
  8348. catch(...)
  8349. {
  8350. if (!Terminal->Active)
  8351. {
  8352. Close = true;
  8353. }
  8354. throw;
  8355. }
  8356. }
  8357. }
  8358. //---------------------------------------------------------------------------
  8359. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8360. {
  8361. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8362. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8363. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8364. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8365. {
  8366. OnGetSpaceAvailable = GetSpaceAvailable;
  8367. }
  8368. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8369. OnGetSpaceAvailable);
  8370. }
  8371. //---------------------------------------------------------------------------
  8372. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8373. {
  8374. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8375. Terminal->FillSessionDataForCode(Data.get());
  8376. return Data.release();
  8377. }
  8378. //---------------------------------------------------------------------------
  8379. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8380. {
  8381. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8382. DoGenerateUrlDialog(Data.get(), Paths);
  8383. }
  8384. //---------------------------------------------------------------------------
  8385. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8386. {
  8387. GenerateUrl(NULL);
  8388. }
  8389. //---------------------------------------------------------------------------
  8390. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8391. {
  8392. std::unique_ptr<TStrings> Paths(new TStringList());
  8393. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8394. GenerateUrl(Paths.get());
  8395. }
  8396. //---------------------------------------------------------------------------
  8397. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8398. {
  8399. FSessionColor = value;
  8400. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8401. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8402. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8403. DebugAssert(ColorItem != NULL);
  8404. ColorItem->Color = C;
  8405. NonVisualDataModule->ColorMenuItem->Color = C;
  8406. UpdateControls();
  8407. }
  8408. //---------------------------------------------------------------------------
  8409. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8410. {
  8411. if (value != FSessionColor)
  8412. {
  8413. UpdateSessionColor(value);
  8414. // Is null when called from LastTerminalClosed
  8415. if (Terminal != NULL)
  8416. {
  8417. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8418. }
  8419. }
  8420. }
  8421. //---------------------------------------------------------------------------
  8422. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8423. {
  8424. bool Result = FNoteTimer->Enabled;
  8425. if (Result)
  8426. {
  8427. // cannot cancel note too early
  8428. bool NotEarly =
  8429. (Now() - FNoteShown >
  8430. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8431. if (Force || NotEarly)
  8432. {
  8433. FNoteTimer->Enabled = false;
  8434. FNote = L"";
  8435. SAFE_DESTROY(FNoteData);
  8436. FOnNoteClick = NULL;
  8437. FNoteHints = FNotes->Text;
  8438. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8439. UpdateStatusBar();
  8440. }
  8441. }
  8442. return Result;
  8443. }
  8444. //---------------------------------------------------------------------------
  8445. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8446. {
  8447. DebugAssert(FNoteTimer->Enabled);
  8448. CancelNote(true);
  8449. }
  8450. //---------------------------------------------------------------------------
  8451. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8452. {
  8453. int P = Note.Pos(L"\n");
  8454. if (P > 0)
  8455. {
  8456. Note.SetLength(P - 1);
  8457. }
  8458. FNotes->Add(FORMAT(L"[%s] %s",
  8459. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8460. while (FNotes->Count > 10)
  8461. {
  8462. FNotes->Delete(0);
  8463. }
  8464. if (UpdateNow)
  8465. {
  8466. FNoteHints = FNotes->Text;
  8467. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8468. UpdateStatusBar();
  8469. }
  8470. }
  8471. //---------------------------------------------------------------------------
  8472. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8473. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8474. {
  8475. int P = Note.Pos(L"\n");
  8476. if (P > 0)
  8477. {
  8478. Note.SetLength(P - 1);
  8479. }
  8480. FNoteHints = FNotes->Text;
  8481. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8482. FNote = Note;
  8483. SAFE_DESTROY(FNoteData);
  8484. FNoteData = NoteData;
  8485. FOnNoteClick = OnNoteClick;
  8486. AddNote(Note, false);
  8487. UpdateStatusBar();
  8488. FNoteShown = Now();
  8489. FNoteTimer->Enabled = false;
  8490. if (Seconds == 0)
  8491. {
  8492. Seconds = WinConfiguration->NotificationsTimeout;
  8493. }
  8494. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8495. FNoteTimer->Enabled = true;
  8496. }
  8497. //---------------------------------------------------------------------------
  8498. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8499. {
  8500. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8501. }
  8502. //---------------------------------------------------------------------------
  8503. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8504. {
  8505. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8506. {
  8507. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8508. }
  8509. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8510. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8511. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8512. 0, NULL, NULL);
  8513. }
  8514. //---------------------------------------------------------------------------
  8515. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8516. {
  8517. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8518. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8519. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8520. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8521. }
  8522. //---------------------------------------------------------------------------
  8523. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8524. {
  8525. WinConfiguration->FormatSizeBytes = Style;
  8526. }
  8527. //---------------------------------------------------------------------------
  8528. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8529. {
  8530. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8531. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8532. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8533. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8534. }
  8535. //---------------------------------------------------------------------------
  8536. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8537. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8538. {
  8539. if (Panel->Index == 0)
  8540. {
  8541. if (FOnNoteClick != NULL)
  8542. {
  8543. // prevent the user data from being freed by possible call
  8544. // to CancelNote or PostNote during call to OnNoteClick
  8545. std::unique_ptr<TObject> NoteData(FNoteData);
  8546. TNotifyEvent OnNoteClick = FOnNoteClick;
  8547. FNoteData = NULL;
  8548. // need to cancel the note as we are going to delete its user data
  8549. CancelNote(true);
  8550. OnNoteClick(NoteData.get());
  8551. }
  8552. }
  8553. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8554. {
  8555. FileSystemInfo();
  8556. }
  8557. }
  8558. //---------------------------------------------------------------------------
  8559. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8560. {
  8561. // workaround:
  8562. // 1) for unknown reason, disabling window, while minimized,
  8563. // prevents it from restoring, even if it was enabled again meanwhile
  8564. // 2) when disabling the main window, while another has focus
  8565. // minimize is no longer possible
  8566. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8567. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8568. // Shouldn't we use IsApplicationMinimized() here?
  8569. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8570. {
  8571. Enabled = false;
  8572. }
  8573. FLockLevel++;
  8574. }
  8575. //---------------------------------------------------------------------------
  8576. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8577. {
  8578. DebugAssert(FLockLevel > 0);
  8579. FLockLevel--;
  8580. if (FLockLevel == 0)
  8581. {
  8582. DebugAssert(FLockSuspendLevel == 0);
  8583. Enabled = true;
  8584. }
  8585. }
  8586. //---------------------------------------------------------------------------
  8587. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8588. {
  8589. // We need to make sure that window is enabled when the last modal window closes
  8590. // otherwise focus is not restored correctly.
  8591. // So we re-enable the window when modal window opens and
  8592. // disable it back after is closes
  8593. if (FLockLevel > 0)
  8594. {
  8595. // while we have nesting counter, we know that we never be called
  8596. // recursivelly as Application->OnModalBegin is called only
  8597. // for the top-level modal window
  8598. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8599. {
  8600. // won't be disabled when conditions in LockWindow() were not satisfied
  8601. FDisabledOnLockSuspend = !Enabled;
  8602. // When minimized to tray (or actually when set to SW_HIDE),
  8603. // setting Enabled makes the window focusable even when there's
  8604. // modal window over it
  8605. if (!FTrayIcon->Visible)
  8606. {
  8607. Enabled = true;
  8608. }
  8609. }
  8610. FLockSuspendLevel++;
  8611. }
  8612. }
  8613. //---------------------------------------------------------------------------
  8614. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8615. {
  8616. if (FLockSuspendLevel > 0)
  8617. {
  8618. DebugAssert(FLockLevel > 0);
  8619. FLockSuspendLevel--;
  8620. // see comment in SuspendWindowLock
  8621. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8622. {
  8623. // Note that window can be enabled here, when we were minized to tray when
  8624. // was SuspendWindowLock() called.
  8625. // We should possibly do the same check as in LockWindow(),
  8626. // if it is ever possible that the consitions change between
  8627. // SuspendWindowLock() and ResumeWindowLock()
  8628. Enabled = !FDisabledOnLockSuspend;
  8629. }
  8630. }
  8631. }
  8632. //---------------------------------------------------------------------------
  8633. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8634. {
  8635. if (!TextOnly)
  8636. {
  8637. TTBXComboBoxItem * RemotePathComboBox =
  8638. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8639. TStrings * Items = RemotePathComboBox->Strings;
  8640. Items->BeginUpdate();
  8641. try
  8642. {
  8643. Items->Clear();
  8644. if (Terminal != NULL)
  8645. {
  8646. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8647. while (!IsUnixRootPath(APath))
  8648. {
  8649. int P = APath.LastDelimiter(L'/');
  8650. DebugAssert(P >= 0);
  8651. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8652. APath.SetLength(P - 1);
  8653. }
  8654. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8655. }
  8656. }
  8657. __finally
  8658. {
  8659. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8660. // Setting ItemIndex to -1 does not reset its text
  8661. if (Items->Count == 0)
  8662. {
  8663. RemotePathComboBox->Text = L"";
  8664. }
  8665. Items->EndUpdate();
  8666. }
  8667. }
  8668. }
  8669. //---------------------------------------------------------------------------
  8670. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8671. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8672. int & ImageIndex)
  8673. {
  8674. if (AIndex < 0)
  8675. {
  8676. AIndex = Sender->ItemIndex;
  8677. }
  8678. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8679. }
  8680. //---------------------------------------------------------------------------
  8681. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8682. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8683. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8684. {
  8685. ARect.Left += (10 * AIndex);
  8686. }
  8687. //---------------------------------------------------------------------------
  8688. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8689. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8690. {
  8691. AWidth += (10 * AIndex);
  8692. }
  8693. //---------------------------------------------------------------------------
  8694. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8695. TObject * Sender)
  8696. {
  8697. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8698. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8699. int Index = RemotePathComboBox->ItemIndex;
  8700. while (Index < RemotePathComboBox->Strings->Count - 1)
  8701. {
  8702. APath = UnixExtractFileDir(APath);
  8703. Index++;
  8704. }
  8705. // VanDyke style paths
  8706. if (APath.IsEmpty())
  8707. {
  8708. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8709. APath = ROOTDIRECTORY;
  8710. }
  8711. if (RemoteDirView->Path != APath)
  8712. {
  8713. RemoteDirView->Path = APath;
  8714. }
  8715. }
  8716. //---------------------------------------------------------------------------
  8717. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8718. {
  8719. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8720. DebugUsedParam(Sender);
  8721. UpdateRemotePathComboBox(true);
  8722. }
  8723. //---------------------------------------------------------------------------
  8724. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8725. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8726. {
  8727. ResetIncrementalSearch();
  8728. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8729. DebugAssert(DirView != NULL);
  8730. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8731. {
  8732. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8733. // OnEditing is called also from TCustomListView::CanEdit
  8734. if (Edit != NULL)
  8735. {
  8736. EditSelectBaseName(Edit);
  8737. }
  8738. }
  8739. }
  8740. //---------------------------------------------------------------------------
  8741. //---------------------------------------------------------------------------
  8742. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8743. {
  8744. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8745. Result->AutoDetectDnD = false;
  8746. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8747. Result->RenderDataOn = rdoEnterAndDropSync;
  8748. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8749. return Result;
  8750. }
  8751. //---------------------------------------------------------------------------
  8752. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8753. {
  8754. TForm::CreateWnd();
  8755. if (FSessionsDragDropFilesEx == NULL)
  8756. {
  8757. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8758. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8759. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8760. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8761. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8762. }
  8763. if (FQueueDragDropFilesEx == NULL)
  8764. {
  8765. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8766. // No need to set OnDragOver as we do not have any restrictions
  8767. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8768. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8769. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8770. }
  8771. }
  8772. //---------------------------------------------------------------------------
  8773. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8774. {
  8775. TForm::DestroyWnd();
  8776. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8777. FQueueDragDropFilesEx->DragDropControl = NULL;
  8778. }
  8779. //---------------------------------------------------------------------------
  8780. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8781. {
  8782. SideEnter(FCurrentSide);
  8783. FEverShown = true;
  8784. }
  8785. //---------------------------------------------------------------------------
  8786. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8787. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8788. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8789. {
  8790. if (!NonVisualDataModule->Busy)
  8791. {
  8792. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8793. Configuration->Usage->Inc(L"FileFinds");
  8794. LockWindow(true);
  8795. NonVisualDataModule->StartBusy();
  8796. WinConfiguration->LockedInterface = true;
  8797. try
  8798. {
  8799. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8800. }
  8801. __finally
  8802. {
  8803. WinConfiguration->LockedInterface = false;
  8804. NonVisualDataModule->EndBusy();
  8805. UnlockWindow();
  8806. }
  8807. }
  8808. }
  8809. //---------------------------------------------------------------------------
  8810. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8811. {
  8812. if (!NonVisualDataModule->Busy)
  8813. {
  8814. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8815. SetFocus();
  8816. RemoteDirView->Path = UnixExtractFilePath(Path);
  8817. UnicodeString FileName = UnixExtractFileName(Path);
  8818. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  8819. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  8820. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  8821. if (Item == NULL)
  8822. {
  8823. ReloadDirectory(osRemote);
  8824. Item = RemoteDirView->FindFileItem(FileName);
  8825. }
  8826. if (Item != NULL)
  8827. {
  8828. RemoteDirView->ItemFocused = Item;
  8829. RemoteDirView->ItemFocused->MakeVisible(false);
  8830. RemoteDirView->SetFocus();
  8831. }
  8832. }
  8833. }
  8834. //---------------------------------------------------------------------------
  8835. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  8836. {
  8837. bool Result = !NonVisualDataModule->Busy;
  8838. if (Result)
  8839. {
  8840. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8841. }
  8842. return Result;
  8843. }
  8844. //---------------------------------------------------------------------------
  8845. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  8846. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8847. {
  8848. if (CanOperateOnFoundFiles(ATerminal))
  8849. {
  8850. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  8851. FOnFileOperationFinished = OnFileOperationFinished;
  8852. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  8853. }
  8854. }
  8855. //---------------------------------------------------------------------------
  8856. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  8857. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8858. {
  8859. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  8860. }
  8861. //---------------------------------------------------------------------------
  8862. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  8863. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8864. {
  8865. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  8866. }
  8867. //---------------------------------------------------------------------------
  8868. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  8869. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8870. {
  8871. if (CanOperateOnFoundFiles(ATerminal))
  8872. {
  8873. for (int Index = 0; Index < FileList->Count; Index++)
  8874. {
  8875. UnicodeString FileName = FileList->Strings[Index];
  8876. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  8877. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  8878. OnFileOperationFinished(FileName, true);
  8879. }
  8880. }
  8881. }
  8882. //---------------------------------------------------------------------------
  8883. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  8884. {
  8885. FFileFindTerminal = Terminal;
  8886. ShowFileFindDialog(
  8887. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  8888. DoDownloadFoundFiles, DoEditFoundFiles);
  8889. }
  8890. //---------------------------------------------------------------------------
  8891. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  8892. {
  8893. FTaskbarList = TaskbarList;
  8894. }
  8895. //---------------------------------------------------------------------------
  8896. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  8897. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  8898. {
  8899. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8900. if (Index >= 0)
  8901. {
  8902. if (Button == mbLeft)
  8903. {
  8904. // "Mouse down" is raised only after tab is switched.
  8905. // If switching tab (switching session) takes long enough for user
  8906. // to actually release the button, "mouse down" is still raised,
  8907. // but we do not get "mouse up" event, so dragging is not cancelled,
  8908. // prevent that by not beginning dragging in the first place.
  8909. if (IsKeyPressed(VK_LBUTTON))
  8910. {
  8911. // when user clicks the "+", we get mouse down only after the session
  8912. // is closed, when new session tab is already on X:Y, so dragging
  8913. // starts, prevent that
  8914. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  8915. {
  8916. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8917. if (Terminal != NULL)
  8918. {
  8919. SessionsPageControl->BeginDrag(false);
  8920. }
  8921. }
  8922. }
  8923. }
  8924. else if (Button == mbMiddle)
  8925. {
  8926. // ignore middle-click for "New session tab"
  8927. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8928. if (Terminal != NULL)
  8929. {
  8930. CloseSessionTab(Index);
  8931. }
  8932. }
  8933. }
  8934. }
  8935. //---------------------------------------------------------------------------
  8936. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  8937. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  8938. {
  8939. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8940. // do not allow dropping on the "+" tab
  8941. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8942. if ((TargetTerminal != NULL) &&
  8943. (SessionsPageControl->ActivePage->PageIndex != Index))
  8944. {
  8945. Configuration->Usage->Inc(L"SessionTabMoves");
  8946. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  8947. // but we want to actually prevent that to avoid flicker
  8948. SessionsPageControl->ActivePage->PageIndex = Index;
  8949. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  8950. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  8951. }
  8952. }
  8953. //---------------------------------------------------------------------------
  8954. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  8955. TObject * Sender, TObject * Source, int X, int Y,
  8956. TDragState /*State*/, bool & Accept)
  8957. {
  8958. Accept = (Sender == Source);
  8959. if (Accept)
  8960. {
  8961. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8962. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8963. // do not allow dragging to the "+" tab
  8964. Accept = (Terminal != NULL);
  8965. }
  8966. }
  8967. //---------------------------------------------------------------------------
  8968. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  8969. const TPoint & Point, int & Effect)
  8970. {
  8971. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8972. if (Index < 0)
  8973. {
  8974. Effect = DROPEFFECT_NONE;
  8975. }
  8976. else
  8977. {
  8978. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8979. // do not allow dropping on the "+" tab
  8980. if (TargetTerminal == NULL)
  8981. {
  8982. Effect = DROPEFFECT_NONE;
  8983. }
  8984. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_MOVE))
  8985. {
  8986. Effect = DROPEFFECT_COPY;
  8987. }
  8988. }
  8989. }
  8990. //---------------------------------------------------------------------------
  8991. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  8992. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  8993. {
  8994. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8995. // do not allow dropping on the "+" tab
  8996. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8997. if (TargetTerminal != NULL)
  8998. {
  8999. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9000. if (!IsFileControl(DropSourceControl, osRemote))
  9001. {
  9002. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  9003. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9004. // Why don't we use Terminal->CurrentDirectory directly?
  9005. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9006. // do not force queue, drag drop
  9007. false, true);
  9008. }
  9009. }
  9010. }
  9011. //---------------------------------------------------------------------------
  9012. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9013. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9014. {
  9015. // Downloads are handled in RemoteFileControlDDEnd
  9016. if (!IsFileControl(DropSourceControl, osRemote))
  9017. {
  9018. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9019. Terminal->CurrentDirectory,
  9020. // force queue, drag drop
  9021. true, true);
  9022. }
  9023. }
  9024. //---------------------------------------------------------------------------
  9025. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9026. {
  9027. FShowing = false;
  9028. // Do not save empty workspace
  9029. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  9030. {
  9031. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9032. UnicodeString Name = WorkspaceName();
  9033. DoSaveWorkspace(
  9034. Name, DataList.get(),
  9035. !Configuration->DisablePasswordStoring &&
  9036. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9037. WinConfiguration->LastStoredSession = Name;
  9038. }
  9039. }
  9040. //---------------------------------------------------------------------------
  9041. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9042. {
  9043. TManagedTerminal * ManagedTerminal =
  9044. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9045. if (ManagedTerminal != NULL)
  9046. {
  9047. ManagedTerminal->DirectoryLoaded = Now();
  9048. }
  9049. }
  9050. //---------------------------------------------------------------------------
  9051. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9052. TListItem * /*Item*/, bool /*Selected*/)
  9053. {
  9054. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9055. switch (DirView->LastSelectMethod)
  9056. {
  9057. case smKeyboard:
  9058. Configuration->Usage->Inc(L"KeyboardSelections");
  9059. break;
  9060. case smMouse:
  9061. Configuration->Usage->Inc(L"MouseSelections");
  9062. break;
  9063. }
  9064. }
  9065. //---------------------------------------------------------------------------
  9066. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9067. {
  9068. if (Color != 0)
  9069. {
  9070. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9071. }
  9072. else
  9073. {
  9074. return FSessionTabImageIndex;
  9075. }
  9076. }
  9077. //---------------------------------------------------------------------------
  9078. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9079. {
  9080. TPngImageCollectionItem * Item =
  9081. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9082. FSessionColors->AddPng(Item->PngImage);
  9083. return FSessionColors->Count - 1;
  9084. }
  9085. //---------------------------------------------------------------------------
  9086. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9087. {
  9088. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9089. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9090. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9091. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9092. }
  9093. //---------------------------------------------------------------------------
  9094. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9095. {
  9096. for (int Index = 0; Index < Control->ControlCount; Index++)
  9097. {
  9098. TControl * AControl = Control->Controls[Index];
  9099. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9100. if (WinControl != NULL)
  9101. {
  9102. CollectItemsWithTextDisplayMode(WinControl);
  9103. }
  9104. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9105. if (Toolbar != NULL)
  9106. {
  9107. // we care for top-level items only
  9108. TTBCustomItem * Items = Toolbar->Items;
  9109. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9110. {
  9111. TTBCustomItem * Item = Items->Items[ItemIndex];
  9112. if (((Item->DisplayMode == nbdmImageAndText) ||
  9113. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9114. EligibleForImageDisplayMode(Item))
  9115. {
  9116. FItemsWithTextDisplayMode.insert(Item);
  9117. }
  9118. }
  9119. }
  9120. }
  9121. }
  9122. //---------------------------------------------------------------------------
  9123. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9124. {
  9125. return true;
  9126. }
  9127. //---------------------------------------------------------------------------
  9128. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9129. {
  9130. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9131. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9132. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9133. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9134. bool Result = true;
  9135. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9136. {
  9137. TTBCustomItem * Item = *i;
  9138. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9139. if (Label != NULL)
  9140. {
  9141. // optimization
  9142. if (Label->Visible == SelectiveToolbarText)
  9143. {
  9144. Result = false;
  9145. }
  9146. else
  9147. {
  9148. Label->Visible = SelectiveToolbarText;
  9149. }
  9150. }
  9151. else
  9152. {
  9153. // optimization
  9154. if (Item->DisplayMode == DisplayMode)
  9155. {
  9156. Result = false;
  9157. }
  9158. else
  9159. {
  9160. Item->DisplayMode = DisplayMode;
  9161. }
  9162. }
  9163. ++i;
  9164. }
  9165. if (Result)
  9166. {
  9167. UpdateNewSessionTab();
  9168. }
  9169. return Result;
  9170. }
  9171. //---------------------------------------------------------------------------
  9172. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9173. {
  9174. DebugFail();
  9175. }
  9176. //---------------------------------------------------------------------------
  9177. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9178. {
  9179. // Among other, a lock level is non zero, while directory is being loaded,
  9180. // even when it happens as a result of dirview navigation,
  9181. // i.e. when TNonVisualDataModule is NOT FBusy.
  9182. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9183. // Among other this prevents a panel auto update to occur while
  9184. // directory is changing.
  9185. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9186. }
  9187. //---------------------------------------------------------------------------
  9188. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9189. {
  9190. // While the window is disabled, we still seem to process menu shortcuts at least,
  9191. // so stop it at least here.
  9192. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9193. return
  9194. (Allowed == aaUpdate) ||
  9195. !NonVisualDataModule->Busy;
  9196. }
  9197. //---------------------------------------------------------------------------
  9198. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9199. {
  9200. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9201. }
  9202. //---------------------------------------------------------------------------
  9203. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9204. {
  9205. // This is somewhat redundant to LockWindow() call from
  9206. // TTerminalManager::TerminalReadDirectoryProgress.
  9207. // But disabling window is known not to block keyboard shorcuts
  9208. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9209. if (Busy > 0)
  9210. {
  9211. if (NonVisualDataModule->Busy)
  9212. {
  9213. State = false;
  9214. }
  9215. else
  9216. {
  9217. NonVisualDataModule->StartBusy();
  9218. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9219. LockWindow();
  9220. }
  9221. }
  9222. else if (Busy < 0)
  9223. {
  9224. UnlockWindow();
  9225. NonVisualDataModule->EndBusy();
  9226. }
  9227. else
  9228. {
  9229. State = NonVisualDataModule->Busy;
  9230. }
  9231. }
  9232. //---------------------------------------------------------------------------
  9233. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9234. {
  9235. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9236. // no context menu for "New session tab"
  9237. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9238. {
  9239. SessionsPageControl->ActivePageIndex = Index;
  9240. if (DebugAlwaysTrue(SessionTabSwitched()))
  9241. {
  9242. // copied from TControl.WMContextMenu
  9243. SendCancelMode(SessionsPageControl);
  9244. // explicit popup instead of using PopupMenu property
  9245. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9246. // while connecting yet not-connected session and hence
  9247. // allowing an access to commands over not-completelly connected session
  9248. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9249. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9250. PopupMenu->PopupComponent = SessionsPageControl;
  9251. PopupMenu->Popup(Point.x, Point.y);
  9252. }
  9253. }
  9254. Handled = true;
  9255. }
  9256. //---------------------------------------------------------------------------
  9257. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9258. {
  9259. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9260. }
  9261. //---------------------------------------------------------------------------
  9262. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9263. {
  9264. Configuration->Usage->Inc(L"PasswordChanges");
  9265. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9266. ChangePasswordData->DisableAuthentationsExceptPassword();
  9267. ChangePasswordData->ChangePassword = true;
  9268. std::unique_ptr<TTerminal> ChangePasswordSession(
  9269. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9270. ChangePasswordSession->AutoReadDirectory = false;
  9271. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9272. {
  9273. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9274. }
  9275. else
  9276. {
  9277. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9278. }
  9279. }
  9280. //---------------------------------------------------------------------------
  9281. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9282. {
  9283. // No nice way to assert SSH2
  9284. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9285. }
  9286. //---------------------------------------------------------------------------
  9287. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9288. {
  9289. TOperationVisualizer OperationVisualizer;
  9290. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9291. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9292. }
  9293. //---------------------------------------------------------------------------
  9294. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9295. {
  9296. TForm::ChangeScale(M, D);
  9297. int APixelsPerInch = GetControlPixelsPerInch(this);
  9298. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9299. try
  9300. {
  9301. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9302. }
  9303. __finally
  9304. {
  9305. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9306. }
  9307. }
  9308. //---------------------------------------------------------------------------
  9309. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9310. {
  9311. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9312. }
  9313. //---------------------------------------------------------------------
  9314. class TPublicControl : public TControl
  9315. {
  9316. friend class TCustomScpExplorerForm;
  9317. };
  9318. //---------------------------------------------------------------------------
  9319. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9320. {
  9321. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9322. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9323. }
  9324. //---------------------------------------------------------------------------
  9325. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9326. {
  9327. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9328. OpenBookmark(Side, Bookmark);
  9329. }
  9330. //---------------------------------------------------------------------------
  9331. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9332. {
  9333. DoBookmarkClick(osLocal, Sender);
  9334. }
  9335. //---------------------------------------------------------------------------
  9336. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9337. {
  9338. DoBookmarkClick(osRemote, Sender);
  9339. }
  9340. //---------------------------------------------------------------------------
  9341. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9342. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9343. {
  9344. if (BookmarkList != NULL)
  9345. {
  9346. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9347. if (!WinConfiguration->UseLocationProfiles)
  9348. {
  9349. std::unique_ptr<TStringList> Directories(new TStringList());
  9350. Directories->CaseSensitive = (Side == osRemote);
  9351. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9352. {
  9353. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9354. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9355. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9356. {
  9357. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9358. Item->Caption = EscapeHotkey(Directory);
  9359. Item->ShortCut = Bookmark->ShortCut;
  9360. Item->OnClick = OnBookmarkClick;
  9361. Item->Tag = reinterpret_cast<int>(Bookmark);
  9362. Directories->Add(Directory);
  9363. Menu->Add(Item.release());
  9364. }
  9365. }
  9366. }
  9367. else
  9368. {
  9369. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9370. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9371. {
  9372. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9373. if (!Bookmark->Node.IsEmpty())
  9374. {
  9375. Folders->Add(Bookmark->Node);
  9376. }
  9377. }
  9378. for (int Index = 0; Index < Folders->Count; Index++)
  9379. {
  9380. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9381. Item->Caption = Folders->Strings[Index];
  9382. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9383. Folders->Objects[Index] = Item.get();
  9384. Menu->Add(Item.release());
  9385. }
  9386. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9387. {
  9388. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9389. TTBCustomItem * Parent;
  9390. if (!Bookmark->Node.IsEmpty())
  9391. {
  9392. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9393. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9394. }
  9395. else
  9396. {
  9397. Parent = Menu;
  9398. }
  9399. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9400. Item->Caption = Bookmark->Name;
  9401. Item->ShortCut = Bookmark->ShortCut;
  9402. Item->OnClick = OnBookmarkClick;
  9403. Item->Tag = reinterpret_cast<int>(Bookmark);
  9404. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9405. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9406. {
  9407. // No hint for location profiles that are actually mere "bookmarks"
  9408. }
  9409. else
  9410. {
  9411. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9412. UnicodeString LongHint;
  9413. if (!Bookmark->Local.IsEmpty())
  9414. {
  9415. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9416. }
  9417. if (!Bookmark->Remote.IsEmpty())
  9418. {
  9419. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9420. }
  9421. AddToList(Hint, LongHint, L"|");
  9422. Item->Hint = Hint;
  9423. }
  9424. Parent->Add(Item.release());
  9425. }
  9426. }
  9427. AddMenuSeparator(Menu);
  9428. }
  9429. }
  9430. //---------------------------------------------------------------------------
  9431. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9432. {
  9433. Menu->Clear();
  9434. std::unique_ptr<TTBCustomItem> Item;
  9435. Item.reset(new TTBXItem(Owner));
  9436. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9437. Item->Options = Item->Options << tboDefault;
  9438. Menu->Add(Item.release());
  9439. AddMenuSeparator(Menu);
  9440. if (Terminal != NULL)
  9441. {
  9442. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9443. }
  9444. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9445. Item.reset(new TTBXItem(Owner));
  9446. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9447. Menu->Add(Item.release());
  9448. }
  9449. //---------------------------------------------------------------------------
  9450. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9451. {
  9452. bool Result = true;
  9453. try
  9454. {
  9455. if (Side == osRemote)
  9456. {
  9457. Terminal->ExceptionOnFail = true;
  9458. }
  9459. try
  9460. {
  9461. DirView(Side)->Path = Path;
  9462. }
  9463. __finally
  9464. {
  9465. if (Side == osRemote)
  9466. {
  9467. Terminal->ExceptionOnFail = false;
  9468. }
  9469. }
  9470. }
  9471. catch (Exception & E)
  9472. {
  9473. if ((Side != osRemote) || Terminal->Active)
  9474. {
  9475. ShowExtendedExceptionEx(Terminal, &E);
  9476. Result = false;
  9477. }
  9478. else
  9479. {
  9480. throw;
  9481. }
  9482. }
  9483. return Result;
  9484. }
  9485. //---------------------------------------------------------------------------
  9486. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9487. {
  9488. // Make sure there some feedback even when loading tine local folders
  9489. TInstantOperationVisualizer Visualizer;
  9490. DirView(Side)->ReloadDirectory();
  9491. }
  9492. //---------------------------------------------------------------------------
  9493. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9494. {
  9495. if (Index == SessionsPageControl->TabIndex)
  9496. {
  9497. CloseSession();
  9498. }
  9499. else
  9500. {
  9501. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9502. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9503. TTerminalManager * Manager = TTerminalManager::Instance();
  9504. if (!Terminal->Active)
  9505. {
  9506. Manager->FreeTerminal(Terminal);
  9507. }
  9508. else
  9509. {
  9510. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9511. if (CanCloseQueue(Queue))
  9512. {
  9513. Manager->FreeTerminal(Terminal);
  9514. }
  9515. }
  9516. }
  9517. }
  9518. //---------------------------------------------------------------------------
  9519. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9520. {
  9521. CloseSessionTab(Index);
  9522. }
  9523. //---------------------------------------------------------------------------
  9524. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side)
  9525. {
  9526. if (DebugAlwaysTrue(GetSide(Side) == osRemote))
  9527. {
  9528. TInstantOperationVisualizer Visualizer;
  9529. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9530. // before we create a new data object
  9531. ClipboardClear();
  9532. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9533. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9534. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9535. FClipboardFakeMonitors.reset(StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated));
  9536. if (FClipboardDragDropFilesEx.get() == NULL)
  9537. {
  9538. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9539. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9540. }
  9541. FClipboardDragDropFilesEx->FileList->Clear();
  9542. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9543. FClipboardDragDropFilesEx->CopyToClipboard();
  9544. FClipboardTerminal = FTerminal;
  9545. // Need full paths, as cwd can be different once files are pasted
  9546. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(false, true)));
  9547. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9548. {
  9549. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9550. }
  9551. }
  9552. }
  9553. //---------------------------------------------------------------------------
  9554. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9555. {
  9556. if (OpenClipboard(0))
  9557. {
  9558. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9559. CloseClipboard();
  9560. }
  9561. }
  9562. //---------------------------------------------------------------------------
  9563. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9564. {
  9565. return !FClipboardFakeDirectory.IsEmpty();
  9566. }
  9567. //---------------------------------------------------------------------------
  9568. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9569. {
  9570. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9571. FClipboardFakeDirectory = UnicodeString();
  9572. FClipboardTerminal = NULL;
  9573. // Also called in Idle()
  9574. FClipboardFakeMonitors.reset(NULL);
  9575. }
  9576. //---------------------------------------------------------------------------
  9577. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9578. {
  9579. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9580. {
  9581. ClipboardStop();
  9582. }
  9583. }
  9584. //---------------------------------------------------------------------------
  9585. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9586. {
  9587. TTerminalManager * Manager = TTerminalManager::Instance();
  9588. Manager->ActiveTerminal = FClipboardTerminal;
  9589. TTransferOperationParam Params;
  9590. Params.TargetDirectory = TargetDirectory;
  9591. Params.DragDrop = DragDrop;
  9592. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9593. }
  9594. //---------------------------------------------------------------------------
  9595. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9596. {
  9597. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9598. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9599. {
  9600. RemoveDir(ApiPath(FileName));
  9601. if (!NonVisualDataModule->Busy)
  9602. {
  9603. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9604. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9605. }
  9606. }
  9607. }
  9608. //---------------------------------------------------------------------------
  9609. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  9610. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  9611. {
  9612. TFileMasks::TParams MaskParams;
  9613. MaskParams.Size = Size;
  9614. MaskParams.Modification = Modification;
  9615. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  9616. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  9617. {
  9618. bool ImplicitMatch;
  9619. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  9620. !ImplicitMatch)
  9621. {
  9622. Color = Iter->Color;
  9623. break;
  9624. }
  9625. }
  9626. }
  9627. //---------------------------------------------------------------------------
  9628. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  9629. {
  9630. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9631. if (!DirView->IsEditing())
  9632. {
  9633. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  9634. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  9635. // while searching
  9636. if (Key == VK_BACK)
  9637. {
  9638. if (!FIncrementalSearch.IsEmpty())
  9639. {
  9640. if (FIncrementalSearch.Length() == 1)
  9641. {
  9642. ResetIncrementalSearch();
  9643. }
  9644. else
  9645. {
  9646. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  9647. IncrementalSearch(NewText, false, false);
  9648. }
  9649. Key = 0;
  9650. }
  9651. }
  9652. }
  9653. }
  9654. //---------------------------------------------------------------------------
  9655. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  9656. {
  9657. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9658. if (!DirView->IsEditing())
  9659. {
  9660. // Filter control sequences.
  9661. // When not searching yet, prefer use of the space for toggling file selection
  9662. // (so we cannot incrementally search for a string starting with a space).
  9663. if ((Key > VK_SPACE) ||
  9664. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  9665. {
  9666. IncrementalSearch(FIncrementalSearch + Key, false, false);
  9667. Key = 0;
  9668. }
  9669. }
  9670. }
  9671. //---------------------------------------------------------------------------
  9672. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  9673. {
  9674. if (!FIncrementalSearch.IsEmpty())
  9675. {
  9676. FIncrementalSearch = L"";
  9677. DirView(osCurrent)->UpdateStatusBar();
  9678. }
  9679. }
  9680. //---------------------------------------------------------------------------
  9681. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9682. {
  9683. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  9684. if (Item == NULL)
  9685. {
  9686. MessageBeep(MB_ICONHAND);
  9687. }
  9688. else
  9689. {
  9690. TCustomDirView * ADirView = DirView(osCurrent);
  9691. {
  9692. TAutoNestingCounter Guard(FIncrementalSearching);
  9693. ADirView->FocusItem(Item);
  9694. if (ADirView->NortonLike == nlOff)
  9695. {
  9696. if ((ADirView->Selected != Item) ||
  9697. (ADirView->SelCount > 1))
  9698. {
  9699. ADirView->ClearSelection();
  9700. ADirView->Selected = Item;
  9701. }
  9702. }
  9703. }
  9704. FIncrementalSearch = Text;
  9705. Item->MakeVisible(false);
  9706. TListItem * NextItem = SearchFile(Text, true, Reverse);
  9707. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  9708. ADirView->UpdateStatusBar();
  9709. }
  9710. }
  9711. //---------------------------------------------------------------------------
  9712. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  9713. {
  9714. int Index = Item->Index;
  9715. if (!Reverse)
  9716. {
  9717. Index++;
  9718. if (Index >= Item->Owner->Count)
  9719. {
  9720. Index = 0;
  9721. }
  9722. }
  9723. else
  9724. {
  9725. Index--;
  9726. if (Index < 0)
  9727. {
  9728. Index = Item->Owner->Count - 1;
  9729. }
  9730. }
  9731. return Item->Owner->Item[Index];
  9732. }
  9733. //---------------------------------------------------------------------------
  9734. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9735. {
  9736. TCustomDirView * ADirView = DirView(osCurrent);
  9737. if (ADirView->Items->Count == 0)
  9738. {
  9739. return NULL;
  9740. }
  9741. else
  9742. {
  9743. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  9744. TListItem * Item = CurrentItem;
  9745. if (SkipCurrent)
  9746. {
  9747. Item = DebugNotNull(GetNextFile(Item, Reverse));
  9748. }
  9749. while (true)
  9750. {
  9751. bool Matches = false;
  9752. switch (WinConfiguration->PanelSearch)
  9753. {
  9754. case isNameStartOnly:
  9755. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  9756. break;
  9757. case isName:
  9758. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  9759. break;
  9760. case isAll:
  9761. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  9762. int Index = 0;
  9763. while ((Index < ColCount) && !Matches)
  9764. {
  9765. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  9766. if (Visible)
  9767. {
  9768. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  9769. }
  9770. Index++;
  9771. }
  9772. break;
  9773. }
  9774. if (Matches)
  9775. {
  9776. return Item;
  9777. }
  9778. Item = GetNextFile(Item, Reverse);
  9779. if (Item == CurrentItem)
  9780. {
  9781. return NULL;
  9782. }
  9783. }
  9784. }
  9785. }
  9786. //---------------------------------------------------------------------------
  9787. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  9788. {
  9789. ResetIncrementalSearch();
  9790. }
  9791. //---------------------------------------------------------------------------
  9792. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  9793. {
  9794. if (Message.CharCode == VK_TAB)
  9795. {
  9796. if (!FIncrementalSearch.IsEmpty())
  9797. {
  9798. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  9799. bool Reverse = Shift.Contains(ssShift);
  9800. IncrementalSearch(FIncrementalSearch, true, Reverse);
  9801. Message.Result = 1;
  9802. return;
  9803. }
  9804. }
  9805. else if (Message.CharCode == VK_ESCAPE)
  9806. {
  9807. if (!FIncrementalSearch.IsEmpty())
  9808. {
  9809. ResetIncrementalSearch();
  9810. Message.Result = 1;
  9811. return;
  9812. }
  9813. }
  9814. TForm::Dispatch(&Message);
  9815. }
  9816. //---------------------------------------------------------------------------
  9817. void __fastcall TCustomScpExplorerForm::Deactivate()
  9818. {
  9819. TForm::Deactivate();
  9820. // E.g. when switching to an internal editor window
  9821. ResetIncrementalSearch();
  9822. }
  9823. //---------------------------------------------------------------------------
  9824. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  9825. {
  9826. // When switching to another application
  9827. ResetIncrementalSearch();
  9828. }
  9829. //---------------------------------------------------------------------------
  9830. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  9831. {
  9832. ResetIncrementalSearch();
  9833. }
  9834. //---------------------------------------------------------------------------
  9835. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  9836. {
  9837. if (FIncrementalSearching <= 0)
  9838. {
  9839. ResetIncrementalSearch();
  9840. }
  9841. }
  9842. //---------------------------------------------------------------------------
  9843. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  9844. {
  9845. CountClicksForWindowPrint(this);
  9846. }
  9847. //---------------------------------------------------------------------------