CustomScpExplorer.cpp 319 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857
  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 SAVE_SELECTION(DIRVIEW) \
  54. UnicodeString FocusFile = L""; \
  55. UnicodeString LastFocusedFile = L""; \
  56. if (DIRVIEW->ItemFocused) LastFocusedFile = DIRVIEW->ItemFocused->Caption; \
  57. { TListItem * ClosestUnselected = DIRVIEW->ClosestUnselected(DIRVIEW->ItemFocused); \
  58. if (ClosestUnselected) FocusFile = ClosestUnselected->Caption; }
  59. #define RESTORE_SELECTION(DIRVIEW) \
  60. if (!LastFocusedFile.IsEmpty() && \
  61. (!DIRVIEW->ItemFocused || (DIRVIEW->ItemFocused->Caption != LastFocusedFile))) \
  62. { \
  63. TListItem *ItemToSelect = DIRVIEW->FindFileItem(FocusFile); \
  64. if (ItemToSelect) \
  65. { \
  66. DIRVIEW->ItemFocused = ItemToSelect; \
  67. DIRVIEW->ItemFocused->MakeVisible(False); \
  68. } \
  69. }
  70. //---------------------------------------------------------------------------
  71. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  72. static const int SessionPanelCount = 4;
  73. //---------------------------------------------------------------------------
  74. class TMutexGuard
  75. {
  76. public:
  77. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  78. int Timeout = 5000)
  79. {
  80. FMutex = NULL;
  81. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  82. if (WaitResult == WAIT_TIMEOUT)
  83. {
  84. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  85. }
  86. else
  87. {
  88. FMutex = AMutex;
  89. }
  90. }
  91. ~TMutexGuard()
  92. {
  93. if (FMutex != NULL)
  94. {
  95. ReleaseMutex(FMutex);
  96. }
  97. }
  98. private:
  99. HANDLE FMutex;
  100. };
  101. //---------------------------------------------------------------------------
  102. class TWindowLock
  103. {
  104. public:
  105. TWindowLock(TCustomScpExplorerForm * Form) :
  106. FForm(Form)
  107. {
  108. FForm->LockWindow();
  109. }
  110. ~TWindowLock()
  111. {
  112. FForm->UnlockWindow();
  113. }
  114. private:
  115. TCustomScpExplorerForm * FForm;
  116. };
  117. //---------------------------------------------------------------------------
  118. struct TTransferOperationParam
  119. {
  120. TTransferOperationParam();
  121. UnicodeString TargetDirectory;
  122. bool Temp;
  123. bool DragDrop;
  124. int Options;
  125. TAutoSwitch Queue;
  126. };
  127. //---------------------------------------------------------------------------
  128. TTransferOperationParam::TTransferOperationParam()
  129. {
  130. Temp = false;
  131. DragDrop = false;
  132. Options = 0;
  133. Queue = asAuto;
  134. }
  135. //---------------------------------------------------------------------------
  136. class TTransferPresetNoteData : public TObject
  137. {
  138. public:
  139. UnicodeString Message;
  140. };
  141. //---------------------------------------------------------------------------
  142. class TTerminalNoteData : public TObject
  143. {
  144. public:
  145. TTerminal * Terminal;
  146. };
  147. //---------------------------------------------------------------------------
  148. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  149. FFormRestored(false),
  150. TForm(Owner)
  151. {
  152. FCurrentSide = osRemote;
  153. FEverShown = false;
  154. FDocks = new TList();
  155. RestoreParams();
  156. ConfigurationChanged();
  157. RemoteDirView->Invalidate();
  158. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  159. NonVisualDataModule->ScpExplorer = this;
  160. FAutoOperation = false;
  161. FOnFileOperationFinished = NULL;
  162. FForceExecution = false;
  163. FIgnoreNextDialogChar = 0;
  164. FErrorList = NULL;
  165. FSynchronizeProgressForm = NULL;
  166. FProgressForm = NULL;
  167. FRefreshLocalDirectory = false;
  168. FRefreshRemoteDirectory = false;
  169. FDDMoveSlipped = false;
  170. FDDExtMapFile = NULL;
  171. // CreateMutexW keeps failing with ERROR_NOACCESS
  172. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  173. DebugAssert(FDDExtMutex != NULL);
  174. FDDTargetControl = NULL;
  175. FDelayedDeletionTimer = NULL;
  176. FDelayedDeletionList = new TStringList();
  177. FDDFileList = NULL;
  178. FPendingTempSpaceWarn = false;
  179. FCapturedLog = NULL;
  180. FDragDropOperation = false;
  181. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  182. FAllowTransferPresetAutoSelect = true;
  183. FCopyParamDefault = L"";
  184. FSynchronizeController = NULL;
  185. FPendingQueueActionItem = NULL;
  186. FLockLevel = 0;
  187. FLockSuspendLevel = 0;
  188. FDisabledOnLockSuspend = false;
  189. FAlternativeDelete = false;
  190. FTrayIcon = new ::TTrayIcon(0);
  191. FTrayIcon->OnClick = TrayIconClick;
  192. FMaxQueueLength = 0;
  193. FLastContextPopupScreenPoint = TPoint(-1, -1);
  194. FTransferResumeList = NULL;
  195. FMoveToQueue = false;
  196. FStandaloneEditing = false;
  197. FOnFeedSynchronizeError = NULL;
  198. FNeedSession = false;
  199. FDoNotIdleCurrentTerminal = 0;
  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. SessionsPageControl->Images = NULL;
  263. SAFE_DESTROY(FSessionColors);
  264. SAFE_DESTROY(FSessionsDragDropFilesEx);
  265. delete FCustomCommandLocalFileList;
  266. delete FCustomCommandRemoteFileList;
  267. delete FCustomCommandMenu;
  268. delete FTrayIcon;
  269. FTrayIcon = NULL;
  270. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  271. delete FEditorManager;
  272. ForceCloseLocalEditors();
  273. delete FLocalEditors;
  274. HideFileFindDialog();
  275. if (FDelayedDeletionTimer)
  276. {
  277. DoDelayedDeletion(NULL);
  278. SAFE_DESTROY(FDelayedDeletionTimer);
  279. }
  280. SAFE_DESTROY(FDelayedDeletionList);
  281. // sometimes we do not get DDEnd so the list is not released
  282. SAFE_DESTROY(FDDFileList);
  283. DebugAssert(FSynchronizeController == NULL);
  284. CloseHandle(FDDExtMutex);
  285. FDDExtMutex = NULL;
  286. FreeLibrary(FOle32Library);
  287. FOle32Library = NULL;
  288. FDragMoveCursor = NULL;
  289. DebugAssert(!FErrorList);
  290. if (FEverShown)
  291. {
  292. // when window is never shown (like when running command-line operation),
  293. // particularly window site is not restored correctly (BoundsRect value set
  294. // in RestoreForm gets lost during handle allocation), so we do not want
  295. // it to be stored
  296. StoreParams();
  297. }
  298. Terminal = NULL;
  299. Queue = NULL;
  300. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  301. NonVisualDataModule->ScpExplorer = NULL;
  302. delete FQueueController;
  303. FQueueController = NULL;
  304. delete FQueueStatusSection;
  305. FQueueStatusSection = NULL;
  306. delete FQueueStatus;
  307. FQueueStatus = NULL;
  308. delete FUserActionTimer;
  309. FUserActionTimer = NULL;
  310. delete FNoteTimer;
  311. delete FNotes;
  312. delete FNoteData;
  313. SAFE_DESTROY(FDocks);
  314. SAFE_DESTROY(FHistoryMenu[0][0]);
  315. SAFE_DESTROY(FHistoryMenu[0][1]);
  316. SAFE_DESTROY(FHistoryMenu[1][0]);
  317. SAFE_DESTROY(FHistoryMenu[1][1]);
  318. if (FHiddenWindow != NULL)
  319. {
  320. DestroyWindow(FHiddenWindow);
  321. FHiddenWindow = NULL;
  322. }
  323. }
  324. //---------------------------------------------------------------------------
  325. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  326. {
  327. std::unique_ptr<TSessionData> Data;
  328. if (!Session.IsEmpty())
  329. {
  330. bool DefaultsOnly;
  331. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  332. }
  333. TTerminalManager * Manager = TTerminalManager::Instance();
  334. for (int Index = 0; Index < Manager->Count; Index++)
  335. {
  336. TTerminal * Terminal = Manager->Terminals[Index];
  337. if (Session.IsEmpty() ||
  338. Terminal->SessionData->IsSameSite(Data.get()))
  339. {
  340. if (Path.IsEmpty())
  341. {
  342. Terminal->ClearCaches();
  343. }
  344. else
  345. {
  346. Terminal->DirectoryModified(Path, true);
  347. }
  348. }
  349. }
  350. // We should flag a pending refresh for the background terminals or busy foreground terminals
  351. if ((Terminal != NULL) && Terminal->Active &&
  352. CanCommandLineFromAnotherInstance() &&
  353. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  354. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  355. {
  356. Terminal->ReloadDirectory();
  357. }
  358. }
  359. //---------------------------------------------------------------------------
  360. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  361. {
  362. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  363. size_t MessageSize = sizeof(TCopyDataMessage);
  364. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  365. if (Result)
  366. {
  367. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  368. Result = (Message.Version == TCopyDataMessage::Version1);
  369. if (Result)
  370. {
  371. switch (Message.Command)
  372. {
  373. case TCopyDataMessage::CommandCanCommandLine:
  374. Result = CanCommandLineFromAnotherInstance();
  375. break;
  376. case TCopyDataMessage::CommandCommandLine:
  377. {
  378. UnicodeString CommandLine(Message.CommandLine);
  379. Result = CommandLineFromAnotherInstance(CommandLine);
  380. }
  381. break;
  382. case TCopyDataMessage::RefreshPanel:
  383. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  384. break;
  385. case TCopyDataMessage::MainWindowCheck:
  386. Result = true;
  387. break;
  388. default:
  389. Result = false;
  390. break;
  391. }
  392. }
  393. }
  394. Message.Result = Result ? 1 : 0;
  395. }
  396. //---------------------------------------------------------------------------
  397. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  398. {
  399. WNDCLASS WindowClass = {0};
  400. WindowClass.lpfnWndProc = DefWindowProc;
  401. WindowClass.hInstance = HInstance;
  402. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  403. FHiddenWindow = NULL;
  404. if (RegisterClass(&WindowClass))
  405. {
  406. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  407. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  408. }
  409. }
  410. //---------------------------------------------------------------------------
  411. bool __fastcall TCustomScpExplorerForm::CanConsole()
  412. {
  413. return (Terminal != NULL) && (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  414. }
  415. //---------------------------------------------------------------------------
  416. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  417. {
  418. bool Result = Visible && !NonVisualDataModule->Busy;
  419. return Result;
  420. }
  421. //---------------------------------------------------------------------------
  422. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  423. const UnicodeString & CommandLine)
  424. {
  425. TProgramParams Params(CommandLine);
  426. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  427. if (Result)
  428. {
  429. NonVisualDataModule->StartBusy();
  430. try
  431. {
  432. // this action is initiated from another process,
  433. // so it's likely that our window is not visible,
  434. // and user won't see what is going on
  435. Application->BringToFront();
  436. // reload sessions as we may be asked to open a session
  437. // just stored by another instance
  438. StoredSessions->Reload();
  439. UnicodeString SessionName = Params.Param[1];
  440. std::unique_ptr<TObjectList> DataList(new TObjectList());
  441. UnicodeString DownloadFile; // unused
  442. try
  443. {
  444. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  445. }
  446. catch (EAbort &)
  447. {
  448. // Happens when opening session in PuTTY and in other situations,
  449. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  450. // to process the command-line on its own (or to pass it to yet another instance)
  451. }
  452. if (DataList->Count > 0)
  453. {
  454. TTerminalManager * Manager = TTerminalManager::Instance();
  455. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  456. }
  457. }
  458. __finally
  459. {
  460. NonVisualDataModule->EndBusy();
  461. }
  462. }
  463. return Result;
  464. }
  465. //---------------------------------------------------------------------------
  466. void __fastcall TCustomScpExplorerForm::SetTerminal(TTerminal * value)
  467. {
  468. if (FTerminal != value)
  469. {
  470. TerminalChanging();
  471. FTerminal = value;
  472. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  473. FAllowTransferPresetAutoSelect = false;
  474. try
  475. {
  476. TerminalChanged();
  477. }
  478. __finally
  479. {
  480. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  481. }
  482. if (Terminal != NULL)
  483. {
  484. TransferPresetAutoSelect();
  485. }
  486. }
  487. }
  488. //---------------------------------------------------------------------------
  489. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  490. {
  491. if (FTerminal != NULL)
  492. {
  493. UpdateTerminal(Terminal);
  494. }
  495. }
  496. //---------------------------------------------------------------------------
  497. void __fastcall TCustomScpExplorerForm::TerminalChanged()
  498. {
  499. RemoteDirView->Terminal = Terminal;
  500. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  501. TManagedTerminal * ManagedTerminal = NULL;
  502. if (Terminal != NULL)
  503. {
  504. if (Terminal->Active)
  505. {
  506. Terminal->RefreshDirectory();
  507. }
  508. ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  509. DebugAssert(ManagedTerminal != NULL);
  510. if (WinConfiguration->PreservePanelState)
  511. {
  512. if (ManagedTerminal->RemoteExplorerState != NULL)
  513. {
  514. RemoteDirView->RestoreState(ManagedTerminal->RemoteExplorerState);
  515. }
  516. else
  517. {
  518. RemoteDirView->ClearState();
  519. }
  520. }
  521. InitStatusBar();
  522. }
  523. DoTerminalListChanged(false);
  524. if (ManagedTerminal != NULL)
  525. {
  526. // this has to be set only after the tab is switched from DoTerminalListChanged,
  527. // otherwise we are changing color of wrong tab
  528. SessionColor = (TColor)ManagedTerminal->StateData->Color;
  529. }
  530. UpdateTransferList();
  531. // Update panels Enable state before refreshing the labels
  532. UpdateControls();
  533. }
  534. //---------------------------------------------------------------------------
  535. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  536. {
  537. if (Queue != value)
  538. {
  539. if (FQueue != NULL)
  540. {
  541. FQueue->OnListUpdate = NULL;
  542. FQueue->OnQueueItemUpdate = NULL;
  543. }
  544. FQueue = value;
  545. if (FQueue != NULL)
  546. {
  547. DebugAssert(FQueue->OnListUpdate == NULL);
  548. FQueue->OnListUpdate = QueueListUpdate;
  549. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  550. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  551. }
  552. QueueChanged();
  553. }
  554. }
  555. //---------------------------------------------------------------------------
  556. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  557. TListItem * Item)
  558. {
  559. if (FQueueActedItem == Item)
  560. {
  561. FQueueActedItem = NULL;
  562. if ((QueueView3->PopupMenu != NULL) &&
  563. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  564. {
  565. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  566. ReleaseCapture();
  567. }
  568. }
  569. if (Item->Data == FPendingQueueActionItem)
  570. {
  571. FPendingQueueActionItem = NULL;
  572. }
  573. }
  574. //---------------------------------------------------------------------------
  575. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  576. {
  577. {
  578. TGuard Guard(FQueueStatusSection);
  579. FQueueStatusInvalidated = false;
  580. if (FQueue != NULL)
  581. {
  582. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  583. }
  584. }
  585. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  586. {
  587. FMaxQueueLength = FQueueStatus->Count;
  588. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  589. }
  590. FQueueController->UpdateQueueStatus(FQueueStatus);
  591. SetQueueProgress();
  592. UpdateQueueView();
  593. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  594. if (IsEmpty && (Terminal != NULL))
  595. {
  596. TOnceDoneOperation OnceDoneOperation =
  597. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  598. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  599. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  600. {
  601. FQueue->Enabled = false;
  602. }
  603. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  604. {
  605. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  606. }
  607. }
  608. }
  609. //---------------------------------------------------------------------------
  610. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  611. {
  612. UnicodeString Result;
  613. if (FQueueStatus != NULL)
  614. {
  615. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  616. {
  617. TFileOperationProgressType * ProgressData =
  618. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  619. if ((ProgressData != NULL) && ProgressData->InProgress)
  620. {
  621. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  622. }
  623. }
  624. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  625. {
  626. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  627. }
  628. }
  629. return Result;
  630. }
  631. //---------------------------------------------------------------------------
  632. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  633. {
  634. ComponentVisible[fcQueueView] =
  635. (WinConfiguration->QueueView.Show == qvShow) ||
  636. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  637. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  638. }
  639. //---------------------------------------------------------------------------
  640. void __fastcall TCustomScpExplorerForm::QueueChanged()
  641. {
  642. if (FQueueStatus != NULL)
  643. {
  644. delete FQueueStatus;
  645. FQueueStatus = NULL;
  646. }
  647. UpdateQueueStatus(true);
  648. }
  649. //---------------------------------------------------------------------------
  650. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  651. {
  652. if (FQueue == Queue)
  653. {
  654. FQueueStatusInvalidated = true;
  655. }
  656. }
  657. //---------------------------------------------------------------------------
  658. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  659. TQueueItem * Item)
  660. {
  661. if (FQueue == Queue)
  662. {
  663. TGuard Guard(FQueueStatusSection);
  664. // this may be running in parallel with QueueChanged
  665. if (FQueueStatus != NULL)
  666. {
  667. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  668. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  669. {
  670. FRefreshLocalDirectory = (QueueItem == NULL) ||
  671. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  672. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  673. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  674. }
  675. if (QueueItem != NULL)
  676. {
  677. QueueItem->UserData = (void*)true;
  678. FQueueItemInvalidated = true;
  679. }
  680. }
  681. }
  682. }
  683. //---------------------------------------------------------------------------
  684. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  685. {
  686. // during standalone editing, we have no way to see/control queue,
  687. // so we have to always popup prompts automatically
  688. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  689. }
  690. //---------------------------------------------------------------------------
  691. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  692. {
  693. if (FQueueStatus != NULL)
  694. {
  695. bool QueueAutoPopup = IsQueueAutoPopup();
  696. bool NeedRefresh = FQueueController->NeedRefresh();
  697. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  698. FQueueItemInvalidated = false;
  699. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  700. bool Updated = false;
  701. TQueueItemProxy * QueueItem;
  702. bool WasUserAction;
  703. for (int Index = 0; Index < Limit; Index++)
  704. {
  705. bool Update = false;
  706. QueueItem = FQueueStatus->Items[Index];
  707. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  708. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  709. (FPendingQueueActionItem == NULL))
  710. {
  711. FPendingQueueActionItem = QueueItem;
  712. FUserActionTimer->Enabled = true;
  713. }
  714. if ((bool)QueueItem->UserData)
  715. {
  716. QueueItem->UserData = (void*)false;
  717. QueueItem->Update();
  718. Updated = true;
  719. Update = true;
  720. }
  721. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  722. {
  723. Update = true;
  724. }
  725. if (Update)
  726. {
  727. FQueueController->RefreshQueueItem(QueueItem);
  728. }
  729. }
  730. if (Updated)
  731. {
  732. NonVisualDataModule->UpdateNonVisibleActions();
  733. SetQueueProgress();
  734. }
  735. }
  736. }
  737. //---------------------------------------------------------------------------
  738. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  739. {
  740. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  741. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  742. }
  743. //---------------------------------------------------------------------------
  744. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  745. {
  746. if (ProgressData->Operation != foCalculateSize)
  747. {
  748. int OverallProgress;
  749. if (DebugAlwaysTrue(FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  750. {
  751. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  752. }
  753. else
  754. {
  755. OverallProgress = ProgressData->OverallProgress();
  756. }
  757. // implies TBPF_NORMAL
  758. FTaskbarList->SetProgressValue(GetMainForm()->Handle, OverallProgress, 100);
  759. }
  760. else
  761. {
  762. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  763. }
  764. }
  765. //---------------------------------------------------------------------------
  766. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  767. {
  768. TTerminalManager::Instance()->QueueStatusUpdated();
  769. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  770. {
  771. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  772. {
  773. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  774. {
  775. TFileOperationProgressType * ProgressData = NULL;
  776. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  777. {
  778. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  779. }
  780. if ((ProgressData != NULL) &&
  781. ProgressData->InProgress)
  782. {
  783. SetTaskbarListProgressValue(ProgressData);
  784. }
  785. else
  786. {
  787. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  788. }
  789. }
  790. else
  791. {
  792. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  793. }
  794. }
  795. else
  796. {
  797. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  798. }
  799. }
  800. UpdateQueueLabel();
  801. }
  802. //---------------------------------------------------------------------------
  803. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  804. {
  805. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  806. if (FQueueStatus != NULL)
  807. {
  808. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  809. if (ActiveAndPendingPrimaryCount > 0)
  810. {
  811. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  812. }
  813. }
  814. QueueLabel->Caption = Caption;
  815. }
  816. //---------------------------------------------------------------------------
  817. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  818. {
  819. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  820. static_cast<TComponent*>(GetComponent(fcTransferList)));
  821. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  822. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  823. TransferList->Strings->BeginUpdate();
  824. try
  825. {
  826. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  827. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  828. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  829. if (FTransferDropDownHint.IsEmpty())
  830. {
  831. FTransferDropDownHint = TransferDropDown->Hint;
  832. }
  833. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  834. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  835. TransferDropDown->Text = StripHotkey(Name);
  836. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  837. (FTransferDropDownHint, StripHotkey(Name),
  838. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  839. FLAGMASK(Terminal != NULL, Terminal->UsableCopyParamAttrs(0).General))));
  840. // update the label, otherwise when it is updated only on the first draw
  841. // of the list, it is drawn "bold" for some reason
  842. FTransferListHoverIndex = TransferList->ItemIndex;
  843. UpdateTransferLabel();
  844. }
  845. __finally
  846. {
  847. TransferList->Strings->EndUpdate();
  848. }
  849. }
  850. //---------------------------------------------------------------------------
  851. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  852. {
  853. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  854. DebugAssert(Toolbar != NULL);
  855. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  856. }
  857. //---------------------------------------------------------------------------
  858. void __fastcall TCustomScpExplorerForm::UpdateActions()
  859. {
  860. TForm::UpdateActions();
  861. if (ComponentVisible[fcCustomCommandsBand])
  862. {
  863. UpdateCustomCommandsToolbar();
  864. }
  865. }
  866. //---------------------------------------------------------------------------
  867. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  868. {
  869. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  870. }
  871. //---------------------------------------------------------------------------
  872. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  873. {
  874. DebugAssert(Configuration && RemoteDirView);
  875. RemoteDirView->DDAllowMove = WinConfiguration->DDAllowMoveInit;
  876. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  877. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  878. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  879. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  880. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  881. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  882. {
  883. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  884. // selection is not redrawn automatically when RowSelect changes
  885. RemoteDirView->Invalidate();
  886. }
  887. RemoteDriveView->DDAllowMove = WinConfiguration->DDAllowMoveInit;
  888. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  889. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  890. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  891. UpdateSessionsPageControlHeight();
  892. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  893. UpdateToolbarDisplayMode();
  894. UpdateTransferList();
  895. if (Terminal != NULL)
  896. {
  897. TransferPresetAutoSelect();
  898. }
  899. UpdateQueueView();
  900. UpdateControls();
  901. // this can be called even before constuctor finishes.
  902. if (FEditorManager != NULL)
  903. {
  904. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  905. }
  906. // this can be called even before constuctor finishes.
  907. if (FLocalEditors != NULL)
  908. {
  909. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  910. {
  911. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  912. }
  913. }
  914. if (ComponentVisible[fcCustomCommandsBand])
  915. {
  916. UpdateCustomCommandsToolbar();
  917. }
  918. // show only when keeping queue items forever,
  919. // otherwise, its enough to have in in the context menu
  920. QueueDeleteAllDoneQueueToolbarItem->Visible =
  921. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  922. }
  923. //---------------------------------------------------------------------------
  924. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  925. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  926. void * /*Arg*/)
  927. {
  928. if (Token != NULL)
  929. {
  930. TForm * Editor = dynamic_cast<TForm*>(Token);
  931. DebugAssert(Editor != NULL);
  932. ReconfigureEditorForm(Editor);
  933. }
  934. }
  935. //---------------------------------------------------------------------------
  936. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  937. {
  938. WinConfiguration->DDTransferConfirmation = asOn;
  939. }
  940. //---------------------------------------------------------------------------
  941. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  942. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  943. {
  944. // noop
  945. }
  946. //---------------------------------------------------------------------------
  947. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  948. TTransferDirection Direction, TTransferType Type, bool Temp,
  949. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  950. bool Confirm, bool DragDrop, int Options)
  951. {
  952. bool Result = true;
  953. DebugAssert(Terminal && Terminal->Active);
  954. // Temp means d&d here so far, may change in future!
  955. if (Temp && (Direction == tdToLocal) && (Type == ttMove) &&
  956. !WinConfiguration->DDAllowMove)
  957. {
  958. TMessageParams Params(mpNeverAskAgainCheck);
  959. unsigned int Answer = MessageDialog(LoadStr(DND_DOWNLOAD_MOVE_WARNING), qtWarning,
  960. qaOK | qaCancel, HELP_DND_DOWNLOAD_MOVE_WARNING, &Params);
  961. if (Answer == qaNeverAskAgain)
  962. {
  963. WinConfiguration->DDAllowMove = true;
  964. }
  965. else if (Answer == qaCancel)
  966. {
  967. Result = false;
  968. }
  969. }
  970. // these parameters are known in advance
  971. int Params =
  972. FLAGMASK(Type == ttMove, cpDelete);
  973. bool ToTemp = (Temp && (Direction == tdToLocal));
  974. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  975. {
  976. bool DisableNewerOnly =
  977. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  978. ToTemp;
  979. Options |=
  980. FLAGMASK(ToTemp, coTemp) |
  981. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  982. coDoNotShowAgain;
  983. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  984. int CopyParamAttrs = (Direction == tdToRemote ?
  985. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  986. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  987. int OutputOptions =
  988. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  989. cooDoNotShowAgain);
  990. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  991. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  992. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions);
  993. if (Result)
  994. {
  995. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  996. {
  997. if (DragDrop)
  998. {
  999. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1000. {
  1001. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1002. NULL, 0, EnableDDTransferConfirmation, NULL);
  1003. }
  1004. WinConfiguration->DDTransferConfirmation = asOff;
  1005. }
  1006. else
  1007. {
  1008. WinConfiguration->ConfirmTransferring = false;
  1009. }
  1010. }
  1011. else
  1012. {
  1013. // User exclicitly unchecked "do not show again",
  1014. // so show him the dialog the next time
  1015. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1016. {
  1017. WinConfiguration->DDTransferConfirmation = asOn;
  1018. }
  1019. }
  1020. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1021. }
  1022. }
  1023. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1024. {
  1025. Configuration->Usage->Inc(L"TransfersOnBackground");
  1026. // these parameter are known only after transfer dialog
  1027. Params |=
  1028. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1029. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1030. Result = false;
  1031. ClearTransferSourceSelection(Direction);
  1032. }
  1033. return Result;
  1034. }
  1035. //---------------------------------------------------------------------------
  1036. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1037. {
  1038. if (FOnFileOperationFinished != NULL)
  1039. {
  1040. FOnFileOperationFinished(UnicodeString(), true);
  1041. }
  1042. else
  1043. {
  1044. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1045. if (HasDirView[Side])
  1046. {
  1047. DirView(Side)->SelectAll(smNone);
  1048. }
  1049. }
  1050. }
  1051. //---------------------------------------------------------------------------
  1052. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1053. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1054. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1055. int Params)
  1056. {
  1057. DebugAssert(Queue != NULL);
  1058. bool SingleFile = false;
  1059. if (FileList->Count == 1)
  1060. {
  1061. if (Direction == tdToRemote)
  1062. {
  1063. UnicodeString FileName = FileList->Strings[0];
  1064. SingleFile = FileExists(ApiPath(FileName));
  1065. }
  1066. else
  1067. {
  1068. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1069. SingleFile = !File->IsDirectory;
  1070. }
  1071. }
  1072. TQueueItem * QueueItem;
  1073. if (Direction == tdToRemote)
  1074. {
  1075. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1076. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1077. }
  1078. else
  1079. {
  1080. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1081. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1082. }
  1083. AddQueueItem(Queue, QueueItem, Terminal);
  1084. }
  1085. //---------------------------------------------------------------------------
  1086. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TTerminal * Terminal)
  1087. {
  1088. if (Queue->IsEmpty)
  1089. {
  1090. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  1091. ManagedTerminal->QueueOperationStart = Now();
  1092. }
  1093. Queue->AddItem(QueueItem);
  1094. }
  1095. //---------------------------------------------------------------------------
  1096. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1097. {
  1098. }
  1099. //---------------------------------------------------------------------------
  1100. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1101. {
  1102. DebugAssert(FDocks != NULL);
  1103. for (int Index = 0; Index < ComponentCount; Index++)
  1104. {
  1105. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1106. if ((Dock != NULL) && (Dock->Tag == 0))
  1107. {
  1108. FDocks->Add(Dock);
  1109. }
  1110. }
  1111. CollectItemsWithTextDisplayMode(this);
  1112. ConfigurationChanged();
  1113. QueuePanel->Height =
  1114. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1115. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1116. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1117. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1118. }
  1119. //---------------------------------------------------------------------------
  1120. void __fastcall TCustomScpExplorerForm::StoreParams()
  1121. {
  1122. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1123. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1124. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1125. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1126. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1127. }
  1128. //---------------------------------------------------------------------------
  1129. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1130. {
  1131. if (!FFormRestored)
  1132. {
  1133. FFormRestored = true;
  1134. RestoreFormParams();
  1135. }
  1136. TForm::CreateParams(Params);
  1137. }
  1138. //---------------------------------------------------------------------------
  1139. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1140. {
  1141. DebugAssert(FDocks != NULL);
  1142. for (int Index = 0; Index < FDocks->Count; Index++)
  1143. {
  1144. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1145. }
  1146. }
  1147. //---------------------------------------------------------------------------
  1148. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1149. {
  1150. SetDockAllowDrag(true);
  1151. while (!ButtonsStr.IsEmpty())
  1152. {
  1153. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1154. UnicodeString Name = CutToChar(S, L'=', true);
  1155. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1156. {
  1157. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1158. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1159. {
  1160. while (!S.IsEmpty())
  1161. {
  1162. UnicodeString S2 = CutToChar(S, L',', true);
  1163. UnicodeString Name = CutToChar(S2, L':', true);
  1164. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1165. {
  1166. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1167. if (SameText(GetToolbarItemName(Item), Name))
  1168. {
  1169. // forward compatibility
  1170. UnicodeString S3 = CutToChar(S2, L':', true);
  1171. bool Visible = StrToIntDef(S3, 0);
  1172. Item->Visible = Visible;
  1173. }
  1174. }
  1175. }
  1176. }
  1177. }
  1178. }
  1179. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1180. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1181. }
  1182. //---------------------------------------------------------------------------
  1183. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1184. {
  1185. UnicodeString Result;
  1186. SetDockAllowDrag(true);
  1187. Result = ::GetToolbarsLayoutStr(this);
  1188. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1189. return Result;
  1190. }
  1191. //---------------------------------------------------------------------------
  1192. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1193. {
  1194. UnicodeString Result;
  1195. if (Item->Action != NULL)
  1196. {
  1197. Result = Item->Action->Name;
  1198. Result = RemoveSuffix(Result, L"Action");
  1199. }
  1200. else
  1201. {
  1202. Result = Item->Name;
  1203. Result = RemoveSuffix(Result, L"SubmenuItem");
  1204. Result = RemoveSuffix(Result, L"Item");
  1205. }
  1206. return Result;
  1207. }
  1208. //---------------------------------------------------------------------------
  1209. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1210. {
  1211. UnicodeString Result;
  1212. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1213. {
  1214. UnicodeString ToolbarStr;
  1215. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1216. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1217. {
  1218. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1219. {
  1220. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1221. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1222. // Once we introduce any buttons that aer hidden by default, we would have to remember their initial state
  1223. // and save the changes here only.
  1224. if (!Item->Visible)
  1225. {
  1226. UnicodeString Name = GetToolbarItemName(Item);
  1227. DebugAssert(Name.Pos(L"TBX") == 0);
  1228. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1229. }
  1230. }
  1231. }
  1232. if (!ToolbarStr.IsEmpty())
  1233. {
  1234. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1235. }
  1236. }
  1237. return Result;
  1238. }
  1239. //---------------------------------------------------------------------------
  1240. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1241. {
  1242. DebugAssert(FProgressForm == NULL);
  1243. FProgressForm =
  1244. new TProgressForm(Application, (FTransferResumeList != NULL), Terminal->IsCapable[fsSkipTransfer], SynchronizeProgress);
  1245. FProgressForm->DeleteLocalToRecycleBin =
  1246. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1247. FProgressForm->DeleteRemoteToRecycleBin =
  1248. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1249. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1250. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1251. LockWindow();
  1252. if (FTaskbarList != NULL)
  1253. {
  1254. // Actually, do not know what hides the progress once the operation finishes
  1255. SetTaskbarListProgressState(TBPF_NORMAL);
  1256. }
  1257. }
  1258. //---------------------------------------------------------------------------
  1259. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1260. {
  1261. UnlockWindow();
  1262. SAFE_DESTROY(FProgressForm);
  1263. SetQueueProgress();
  1264. }
  1265. //---------------------------------------------------------------------------
  1266. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1267. TFileOperationProgressType & ProgressData)
  1268. {
  1269. // Download to temporary local directory
  1270. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1271. {
  1272. bool Continue = true;
  1273. FPendingTempSpaceWarn = false;
  1274. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1275. if (!Continue)
  1276. {
  1277. Abort();
  1278. }
  1279. }
  1280. // operation is being executed and we still didn't show up progress form
  1281. if (ProgressData.InProgress && !FProgressForm)
  1282. {
  1283. CreateProgressForm(NULL);
  1284. // When main window is hidden, synchronisation form does not exist (we do not get here during synchronization - only comparing),
  1285. // we suppose "/upload" or URL download mode
  1286. if (!Visible && (ProgressData.Operation != foCalculateSize) &&
  1287. (ProgressData.Operation != foCalculateChecksum) &&
  1288. (FSynchronizeProgressForm == NULL) &&
  1289. !FStandaloneEditing)
  1290. {
  1291. FProgressForm->OnceDoneOperation = odoDisconnect;
  1292. }
  1293. }
  1294. // operation is finished (or terminated), so we hide progress form
  1295. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1296. {
  1297. DestroyProgressForm();
  1298. if (ProgressData.Operation == foCalculateSize)
  1299. {
  1300. // When calculating size before transfer, the abort caused by
  1301. // cancel flag set due to "MoveToQueue" below,
  1302. // is not propagated back to us in ExecuteFileOperation,
  1303. // as it is silently swallowed.
  1304. // So we explicitly (re)throw it here.
  1305. if (FMoveToQueue)
  1306. {
  1307. Abort();
  1308. }
  1309. }
  1310. else
  1311. {
  1312. if ((ProgressData.Cancel == csContinue) &&
  1313. !FAutoOperation)
  1314. {
  1315. OperationComplete(ProgressData.StartTime);
  1316. }
  1317. }
  1318. }
  1319. if (FProgressForm &&
  1320. // Can happen only during synchronization, when ending one of the operations
  1321. ProgressData.InProgress)
  1322. {
  1323. FProgressForm->SetProgressData(ProgressData);
  1324. if (FTaskbarList != NULL)
  1325. {
  1326. DebugAssert(ProgressData.InProgress);
  1327. SetTaskbarListProgressValue(&ProgressData);
  1328. }
  1329. if (FProgressForm->Cancel > csContinue)
  1330. {
  1331. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1332. if (FProgressForm->Cancel == csCancelFile)
  1333. {
  1334. FProgressForm->ClearCancel();
  1335. }
  1336. // cancel cancels even the move
  1337. FMoveToQueue = false;
  1338. }
  1339. else if (FProgressForm->MoveToQueue)
  1340. {
  1341. FMoveToQueue = true;
  1342. ProgressData.SetCancelAtLeast(csCancel);
  1343. }
  1344. if ((FTransferResumeList != NULL) &&
  1345. ProgressData.InProgress &&
  1346. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1347. !ProgressData.FullFileName.IsEmpty())
  1348. {
  1349. if ((FTransferResumeList->Count == 0) ||
  1350. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1351. {
  1352. // note that we do not recognize directories from files here
  1353. FTransferResumeList->Add(ProgressData.FullFileName);
  1354. }
  1355. }
  1356. }
  1357. }
  1358. //---------------------------------------------------------------------------
  1359. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1360. const TDateTime & StartTime)
  1361. {
  1362. if (GUIConfiguration->BeepOnFinish &&
  1363. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1364. {
  1365. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1366. DWORD Sound;
  1367. if (!ExtractFileExt(BeepSound).IsEmpty())
  1368. {
  1369. Sound = SND_FILENAME;
  1370. }
  1371. else
  1372. {
  1373. Sound = SND_ALIAS;
  1374. }
  1375. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1376. }
  1377. }
  1378. //---------------------------------------------------------------------------
  1379. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1380. TFileOperationProgressType & ProgressData)
  1381. {
  1382. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1383. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1384. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1385. FileOperationProgress(ProgressData);
  1386. }
  1387. //---------------------------------------------------------------------------
  1388. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1389. {
  1390. UnicodeString Result;
  1391. if (FProgressForm != NULL)
  1392. {
  1393. Result = FProgressForm->ProgressStr();
  1394. }
  1395. return Result;
  1396. }
  1397. //---------------------------------------------------------------------------
  1398. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1399. bool DragDrop)
  1400. {
  1401. return (!DragDrop && (DropSourceControl == NULL)) ||
  1402. (DropSourceControl == RemoteDirView);
  1403. }
  1404. //---------------------------------------------------------------------------
  1405. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1406. TFileOperation Operation, TOperationSide Side,
  1407. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1408. TOnceDoneOperation & OnceDoneOperation)
  1409. {
  1410. if (!FAutoOperation)
  1411. {
  1412. // no selection on "/upload", form servers only as event handler
  1413. // (it is not displayed)
  1414. if (PanelOperation(Side, FDragDropOperation) &&
  1415. Visible && (Operation != foCalculateSize) &&
  1416. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1417. {
  1418. if (FOnFileOperationFinished != NULL)
  1419. {
  1420. FOnFileOperationFinished(FileName, Success);
  1421. }
  1422. else
  1423. {
  1424. TCustomDirView * DView = DirView(Side);
  1425. UnicodeString FileNameOnly = ExtractFileName(FileName, (Side == osRemote));
  1426. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1427. // this can happen when local drive is unplugged in the middle of the operation
  1428. if (Item != NULL)
  1429. {
  1430. if (Success) Item->Selected = false;
  1431. DView->MakeProgressVisible(Item);
  1432. }
  1433. }
  1434. }
  1435. if ((Operation == foCopy) || (Operation == foMove))
  1436. {
  1437. if (Side == osLocal)
  1438. {
  1439. Configuration->Usage->Inc(L"UploadedFiles");
  1440. }
  1441. else
  1442. {
  1443. Configuration->Usage->Inc(L"DownloadedFiles");
  1444. }
  1445. }
  1446. }
  1447. if (Success && (FSynchronizeController != NULL))
  1448. {
  1449. if (Operation == foCopy)
  1450. {
  1451. DebugAssert(Side == osLocal);
  1452. FSynchronizeController->LogOperation(soUpload, FileName);
  1453. }
  1454. else if (Operation == foDelete)
  1455. {
  1456. DebugAssert(Side == osRemote);
  1457. FSynchronizeController->LogOperation(soDelete, FileName);
  1458. }
  1459. }
  1460. if (FProgressForm)
  1461. {
  1462. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1463. }
  1464. }
  1465. //---------------------------------------------------------------------------
  1466. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1467. TFileOperation Operation, TOperationSide Side,
  1468. bool Temp, const UnicodeString & FileName, Boolean Success,
  1469. TOnceDoneOperation & OnceDoneOperation)
  1470. {
  1471. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1472. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1473. OnceDoneOperation);
  1474. }
  1475. //---------------------------------------------------------------------------
  1476. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1477. {
  1478. DebugAssert(GetSide(Side) == osRemote);
  1479. DebugUsedParam(Side);
  1480. return RemoteDirView;
  1481. }
  1482. //---------------------------------------------------------------------------
  1483. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1484. {
  1485. DebugAssert(GetSide(Side) == osRemote);
  1486. DebugUsedParam(Side);
  1487. return (Terminal != NULL);
  1488. }
  1489. //---------------------------------------------------------------------------
  1490. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1491. TOperationSide Side, int FilesOnly)
  1492. {
  1493. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1494. }
  1495. //---------------------------------------------------------------------------
  1496. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1497. TOperationSide Side, int FilesOnly)
  1498. {
  1499. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1500. }
  1501. //---------------------------------------------------------------------------
  1502. struct THistoryItemData
  1503. {
  1504. short int Side;
  1505. short int Index;
  1506. };
  1507. //---------------------------------------------------------------------------
  1508. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1509. {
  1510. DirView(Side)->HistoryGo(Index);
  1511. }
  1512. //---------------------------------------------------------------------------
  1513. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1514. {
  1515. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1516. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1517. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1518. }
  1519. //---------------------------------------------------------------------------
  1520. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1521. bool Back)
  1522. {
  1523. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1524. {
  1525. TCustomDirView * DView = DirView(Side);
  1526. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1527. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1528. if (ICount > 10)
  1529. {
  1530. ICount = 10;
  1531. }
  1532. Menu->Items->Clear();
  1533. THistoryItemData Data;
  1534. Data.Side = Side;
  1535. for (short int i = 1; i <= ICount; i++)
  1536. {
  1537. TTBCustomItem * Item = new TTBXItem(Menu);
  1538. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1539. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, (Side == osRemote));
  1540. Item->Hint = DView->HistoryPath[Data.Index];
  1541. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1542. Item->Tag = *reinterpret_cast<int*>(&Data);
  1543. Item->OnClick = HistoryItemClick;
  1544. Menu->Items->Add(Item);
  1545. }
  1546. }
  1547. }
  1548. //---------------------------------------------------------------------------
  1549. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1550. TOperationSide Side, bool Back)
  1551. {
  1552. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1553. {
  1554. // workaround
  1555. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1556. // operator new allocates memory in Pascal code, but calls inline
  1557. // contructor in C++, leading in problems, the function does
  1558. // both in Pascal code
  1559. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1560. UpdateHistoryMenu(Side, Back);
  1561. }
  1562. return FHistoryMenu[Side == osLocal][Back];
  1563. }
  1564. //---------------------------------------------------------------------------
  1565. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1566. TCustomDirView *Sender)
  1567. {
  1568. TOperationSide Side = (Sender == DirView(osRemote) ? osRemote : osLocal);
  1569. UpdateHistoryMenu(Side, true);
  1570. UpdateHistoryMenu(Side, false);
  1571. }
  1572. //---------------------------------------------------------------------------
  1573. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1574. {
  1575. // remote custom commands can be executed only if the server supports shell commands
  1576. // or have secondary shell
  1577. return (FTerminal != NULL) && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1578. }
  1579. //---------------------------------------------------------------------------
  1580. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1581. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1582. {
  1583. int Result;
  1584. TFileCustomCommand RemoteCustomCommand;
  1585. TLocalCustomCommand LocalCustomCommand;
  1586. TFileCustomCommand * NonInteractiveCustomCommand =
  1587. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1588. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1589. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1590. if (FLAGCLEAR(Command.Params, ccLocal))
  1591. {
  1592. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1593. // custom command that does not operate with files can be executed anytime ...
  1594. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1595. {
  1596. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1597. {
  1598. // ... but do not show such command in remote file menu (TODO)
  1599. Result = AllowedState;
  1600. }
  1601. else
  1602. {
  1603. Result = -1;
  1604. }
  1605. }
  1606. else
  1607. {
  1608. if ((ListType == ccltAll) || (ListType == ccltFile))
  1609. {
  1610. Result = ((FCurrentSide == osRemote) && DirView(osRemote)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1611. }
  1612. else
  1613. {
  1614. Result = -1;
  1615. }
  1616. }
  1617. }
  1618. else
  1619. {
  1620. // custom command that does not operate with files can be executed anytime
  1621. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1622. {
  1623. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1624. }
  1625. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1626. {
  1627. if ((ListType == ccltAll) || (ListType == ccltFile))
  1628. {
  1629. // special case is "diff"-style command that can be executed over any side,
  1630. // if we have both sides
  1631. Result =
  1632. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1633. // directly (instead of EnableSelectedOperation) to pass
  1634. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1635. ((HasDirView[osLocal] && DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1636. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1637. }
  1638. else if (ListType == ccltBoth)
  1639. {
  1640. Result = 1;
  1641. }
  1642. else
  1643. {
  1644. Result = -1;
  1645. }
  1646. }
  1647. else
  1648. {
  1649. if ((ListType == ccltAll) || (ListType == ccltFile))
  1650. {
  1651. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1652. }
  1653. else
  1654. {
  1655. Result = -1;
  1656. }
  1657. }
  1658. }
  1659. return Result;
  1660. }
  1661. //---------------------------------------------------------------------------
  1662. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1663. TStrings * FileList, const TCustomCommandType & ACommand,
  1664. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1665. {
  1666. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1667. {
  1668. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1669. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1670. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1671. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1672. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1673. bool Capture =
  1674. FLAGSET(ACommand.Params, ccShowResults) ||
  1675. FLAGSET(ACommand.Params, ccCopyResults) ||
  1676. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1677. TCaptureOutputEvent OutputEvent = NULL;
  1678. DebugAssert(FCapturedLog == NULL);
  1679. if (Capture)
  1680. {
  1681. FCapturedLog = new TStringList();
  1682. OutputEvent = TerminalCaptureLog;
  1683. }
  1684. try
  1685. {
  1686. if (!RemoteCustomCommand.IsFileCommand(Command))
  1687. {
  1688. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1689. OutputEvent);
  1690. }
  1691. else
  1692. {
  1693. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1694. }
  1695. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1696. {
  1697. if (FLAGSET(ACommand.Params, ccCopyResults))
  1698. {
  1699. CopyToClipboard(FCapturedLog);
  1700. }
  1701. if (FLAGSET(ACommand.Params, ccShowResults))
  1702. {
  1703. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1704. }
  1705. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1706. {
  1707. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1708. }
  1709. }
  1710. }
  1711. __finally
  1712. {
  1713. SAFE_DESTROY(FCapturedLog);
  1714. }
  1715. }
  1716. }
  1717. //---------------------------------------------------------------------------
  1718. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1719. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1720. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1721. {
  1722. TStrings * LocalFileList = NULL;
  1723. TStrings * RemoteFileList = NULL;
  1724. TStrings * RemoteFileListFull = NULL;
  1725. try
  1726. {
  1727. if (LocalFileCommand)
  1728. {
  1729. if (ALocalFileList == NULL)
  1730. {
  1731. DebugAssert(HasDirView[osLocal]);
  1732. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1733. // directly (instead of EnableSelectedOperation) to pass
  1734. // false to FocusedFileOnlyWhenFocused
  1735. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1736. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1737. }
  1738. else
  1739. {
  1740. LocalFileList = ALocalFileList;
  1741. }
  1742. if (FileListCommand)
  1743. {
  1744. if (LocalFileList->Count != 1)
  1745. {
  1746. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1747. }
  1748. }
  1749. else
  1750. {
  1751. if ((LocalFileList->Count != 1) &&
  1752. (FileList->Count != 1) &&
  1753. (LocalFileList->Count != FileList->Count))
  1754. {
  1755. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1756. }
  1757. }
  1758. }
  1759. UnicodeString RootTempDir;
  1760. UnicodeString TempDir;
  1761. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1762. if (!RemoteFiles)
  1763. {
  1764. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1765. }
  1766. try
  1767. {
  1768. TDateTimes RemoteFileTimes;
  1769. if (RemoteFiles)
  1770. {
  1771. RemoteFileList = FileList;
  1772. }
  1773. else
  1774. {
  1775. RemoteFileList = new TStringList();
  1776. TMakeLocalFileListParams MakeFileListParam;
  1777. MakeFileListParam.FileList = RemoteFileList;
  1778. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1779. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1780. MakeFileListParam.Recursive =
  1781. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1782. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1783. if (!MakeFileListParam.Recursive)
  1784. {
  1785. RemoteFileTimes.clear();
  1786. RemoteFileListFull = new TStringList();
  1787. MakeFileListParam.FileList = RemoteFileListFull;
  1788. MakeFileListParam.Recursive = true;
  1789. MakeFileListParam.IncludeDirs = false;
  1790. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1791. }
  1792. else
  1793. {
  1794. RemoteFileListFull = RemoteFileList;
  1795. }
  1796. }
  1797. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1798. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1799. if (!NonBlocking)
  1800. {
  1801. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : FileList->Count);
  1802. DebugAssert(FProgressForm != NULL);
  1803. FProgressForm->ReadOnly = true;
  1804. }
  1805. try
  1806. {
  1807. if (FileListCommand)
  1808. {
  1809. UnicodeString LocalFile;
  1810. // MakeFileList does not delimit filenames
  1811. UnicodeString FileList = MakeFileList(RemoteFileList);
  1812. if (LocalFileCommand)
  1813. {
  1814. DebugAssert(LocalFileList->Count == 1);
  1815. LocalFile = LocalFileList->Strings[0];
  1816. }
  1817. TLocalCustomCommand CustomCommand(Data,
  1818. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1819. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1820. if (NonBlocking)
  1821. {
  1822. DebugAssert(!POutput);
  1823. ExecuteShellChecked(ShellCommand);
  1824. }
  1825. else
  1826. {
  1827. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1828. }
  1829. }
  1830. else if (LocalFileCommand)
  1831. {
  1832. if (LocalFileList->Count == 1)
  1833. {
  1834. UnicodeString LocalFile = LocalFileList->Strings[0];
  1835. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1836. {
  1837. UnicodeString FileName = RemoteFileList->Strings[Index];
  1838. TLocalCustomCommand CustomCommand(Data,
  1839. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1840. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1841. }
  1842. }
  1843. else if (RemoteFileList->Count == 1)
  1844. {
  1845. UnicodeString FileName = RemoteFileList->Strings[0];
  1846. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1847. {
  1848. TLocalCustomCommand CustomCommand(
  1849. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1850. FileName, LocalFileList->Strings[Index], L"");
  1851. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1852. }
  1853. }
  1854. else
  1855. {
  1856. if (LocalFileList->Count != RemoteFileList->Count)
  1857. {
  1858. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1859. }
  1860. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1861. {
  1862. UnicodeString FileName = RemoteFileList->Strings[Index];
  1863. TLocalCustomCommand CustomCommand(
  1864. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1865. FileName, LocalFileList->Strings[Index], L"");
  1866. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1867. }
  1868. }
  1869. }
  1870. else
  1871. {
  1872. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1873. {
  1874. TLocalCustomCommand CustomCommand(Data,
  1875. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1876. RemoteFileList->Strings[Index], L"", L"");
  1877. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1878. }
  1879. }
  1880. }
  1881. __finally
  1882. {
  1883. if (!NonBlocking)
  1884. {
  1885. Progress.Stop();
  1886. }
  1887. }
  1888. DebugAssert(!FAutoOperation);
  1889. if (!RemoteFiles)
  1890. {
  1891. TempDir = IncludeTrailingBackslash(TempDir);
  1892. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1893. {
  1894. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1895. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1896. // Skip directories, process only nested files.
  1897. // The check is redundant as FileAge fails for directories anyway.
  1898. !DirectoryExists(FileName))
  1899. {
  1900. UnicodeString RemoteDir =
  1901. UnixExtractFileDir(
  1902. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  1903. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  1904. TDateTime NewTime;
  1905. if (FileAge(FileName, NewTime) &&
  1906. (NewTime != RemoteFileTimes[Index]))
  1907. {
  1908. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  1909. TemporaryFileCopyParam(CopyParam);
  1910. CopyParam.FileMask = L"";
  1911. FAutoOperation = true;
  1912. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  1913. TemporaryFilesList->Add(FileName);
  1914. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  1915. }
  1916. }
  1917. }
  1918. }
  1919. }
  1920. __finally
  1921. {
  1922. FAutoOperation = false;
  1923. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  1924. {
  1925. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  1926. }
  1927. }
  1928. }
  1929. __finally
  1930. {
  1931. if (RemoteFileList != FileList)
  1932. {
  1933. delete RemoteFileList;
  1934. }
  1935. if (LocalFileList != ALocalFileList)
  1936. {
  1937. delete LocalFileList;
  1938. }
  1939. if (RemoteFileListFull != RemoteFileList)
  1940. {
  1941. delete RemoteFileListFull;
  1942. }
  1943. }
  1944. }
  1945. //---------------------------------------------------------------------------
  1946. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithRemoteFiles(
  1947. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  1948. bool FileListCommand, UnicodeString * POutput)
  1949. {
  1950. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  1951. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  1952. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  1953. {
  1954. UnicodeString FileName = SelectedFileList->Strings[Index];
  1955. if (DirectoryExists(FileName))
  1956. {
  1957. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  1958. {
  1959. LocalFileList->Add(FileName);
  1960. }
  1961. if (FLAGSET(ACommand.Params, ccRecursive))
  1962. {
  1963. TMakeLocalFileListParams MakeFileListParam;
  1964. MakeFileListParam.FileList = LocalFileList.get();
  1965. MakeFileListParam.FileTimes = NULL;
  1966. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1967. MakeFileListParam.Recursive = true;
  1968. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  1969. }
  1970. }
  1971. else
  1972. {
  1973. LocalFileList->Add(FileName);
  1974. }
  1975. }
  1976. if (FileListCommand)
  1977. {
  1978. UnicodeString FileList = MakeFileList(LocalFileList.get());
  1979. TLocalCustomCommand CustomCommand(
  1980. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1981. L"", L"", FileList);
  1982. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1983. }
  1984. else
  1985. {
  1986. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1987. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : LocalFileList->Count);
  1988. DebugAssert(FProgressForm != NULL);
  1989. FProgressForm->ReadOnly = true;
  1990. try
  1991. {
  1992. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1993. {
  1994. UnicodeString FileName = LocalFileList->Strings[Index];
  1995. TLocalCustomCommand CustomCommand(
  1996. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1997. FileName, L"", L"");
  1998. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1999. }
  2000. }
  2001. __finally
  2002. {
  2003. Progress.Stop();
  2004. }
  2005. }
  2006. }
  2007. //---------------------------------------------------------------------------
  2008. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2009. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2010. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2011. {
  2012. TLocalCustomCommand LocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory());
  2013. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2014. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2015. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2016. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2017. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2018. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2019. std::unique_ptr<UnicodeString> POutput;
  2020. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2021. FLAGSET(ACommand.Params, ccCopyResults))
  2022. {
  2023. POutput.reset(new UnicodeString());
  2024. }
  2025. if (!LocalCustomCommand.IsFileCommand(Command))
  2026. {
  2027. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2028. }
  2029. // remote files?
  2030. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2031. {
  2032. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2033. }
  2034. // local files
  2035. else
  2036. {
  2037. LocalCustomCommandWithRemoteFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2038. }
  2039. if (POutput.get() != NULL)
  2040. {
  2041. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2042. int P = POutput->Pos(sLineBreak);
  2043. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2044. {
  2045. POutput->SetLength(P - 1);
  2046. }
  2047. // Copy even empty output to clipboard
  2048. if (FLAGSET(ACommand.Params, ccCopyResults))
  2049. {
  2050. CopyToClipboard(*POutput);
  2051. }
  2052. // But do not show an empty message box.
  2053. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2054. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2055. !POutput->IsEmpty())
  2056. {
  2057. TClipboardHandler ClipboardHandler;
  2058. ClipboardHandler.Text = *POutput;
  2059. TMessageParams Params;
  2060. TQueryButtonAlias Aliases[1];
  2061. Aliases[0].Button = qaRetry;
  2062. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2063. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2064. Params.Aliases = Aliases;
  2065. Params.AliasesCount = LENOF(Aliases);
  2066. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2067. }
  2068. }
  2069. }
  2070. //---------------------------------------------------------------------------
  2071. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2072. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2073. {
  2074. TCustomCommandData Data(Terminal);
  2075. UnicodeString Site = Terminal->SessionData->SessionKey;
  2076. UnicodeString HelpKeyword = ACommand.HomePage;
  2077. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2078. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2079. {
  2080. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2081. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2082. {
  2083. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, Terminal->CurrentDirectory));
  2084. }
  2085. else
  2086. {
  2087. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory()));
  2088. }
  2089. if (!DoCustomCommandOptionsDialog(
  2090. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2091. {
  2092. Abort();
  2093. }
  2094. }
  2095. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2096. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2097. {
  2098. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2099. }
  2100. else
  2101. {
  2102. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2103. }
  2104. }
  2105. //---------------------------------------------------------------------------
  2106. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2107. const TCustomCommandType & Command)
  2108. {
  2109. DebugAssert(FCustomCommandLocalFileList != NULL);
  2110. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2111. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2112. TStrings * LocalFileList = new TStringList();
  2113. TStrings * RemoteFileList = new TStringList();
  2114. try
  2115. {
  2116. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2117. {
  2118. LocalFileList->Clear();
  2119. LocalFileList->AddObject(
  2120. FCustomCommandLocalFileList->Strings[Index],
  2121. FCustomCommandLocalFileList->Objects[Index]);
  2122. RemoteFileList->Clear();
  2123. RemoteFileList->AddObject(
  2124. FCustomCommandRemoteFileList->Strings[Index],
  2125. FCustomCommandRemoteFileList->Objects[Index]);
  2126. CustomCommand(RemoteFileList, Command, LocalFileList);
  2127. }
  2128. }
  2129. __finally
  2130. {
  2131. delete LocalFileList;
  2132. delete RemoteFileList;
  2133. }
  2134. }
  2135. //---------------------------------------------------------------------------
  2136. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2137. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2138. {
  2139. delete FCustomCommandLocalFileList;
  2140. delete FCustomCommandRemoteFileList;
  2141. // takeover ownership,
  2142. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2143. FCustomCommandLocalFileList = LocalFileList;
  2144. FCustomCommandRemoteFileList = RemoteFileList;
  2145. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2146. if (Button != NULL)
  2147. {
  2148. FCustomCommandMenu->Items->Clear();
  2149. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
  2150. MenuPopup(FCustomCommandMenu, Button);
  2151. }
  2152. else
  2153. {
  2154. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2155. }
  2156. }
  2157. //---------------------------------------------------------------------------
  2158. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2159. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2160. {
  2161. DebugAssert(FCapturedLog != NULL);
  2162. if ((OutputType == cotOutput) || (OutputType == cotError))
  2163. {
  2164. FCapturedLog->Add(AddedLine);
  2165. }
  2166. }
  2167. //---------------------------------------------------------------------------
  2168. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2169. TOperationSide Side)
  2170. {
  2171. return (Side == osRemote) &&
  2172. ((Control == RemoteDirView) || (Control == RemoteDriveView));
  2173. }
  2174. //---------------------------------------------------------------------------
  2175. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2176. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2177. {
  2178. TTBItemOptions O;
  2179. O = Item->Options;
  2180. TCustomDirView * DView = DirView(Side);
  2181. if ((DView->ItemFocused != NULL) &&
  2182. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2183. // when resolving links is disabled, default action is to enter the directory,
  2184. // no matter what DoubleClickAction is configured to
  2185. ((Side != osRemote) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2186. // Can only Edit files, but can Open/Copy even directories
  2187. ((DoubleClickAction != dcaEdit) ||
  2188. !DView->ItemIsDirectory(DView->ItemFocused)))
  2189. {
  2190. Item->Options = O << tboDefault;
  2191. }
  2192. else
  2193. {
  2194. Item->Options = O >> tboDefault;
  2195. }
  2196. }
  2197. //---------------------------------------------------------------------------
  2198. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2199. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2200. {
  2201. TCustomDirView * DView = DirView(Side);
  2202. TListItem * Item = DView->ItemFocused;
  2203. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2204. EnableFocusedOperation[Side])
  2205. {
  2206. TPoint ClientPoint;
  2207. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2208. TPoint(0, 0) : MousePos;
  2209. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2210. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2211. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2212. }
  2213. }
  2214. //---------------------------------------------------------------------------
  2215. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2216. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2217. {
  2218. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2219. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2220. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2221. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2222. Handled = true;
  2223. }
  2224. //---------------------------------------------------------------------------
  2225. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2226. {
  2227. }
  2228. //---------------------------------------------------------------------------
  2229. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2230. {
  2231. DebugAssert(FErrorList == NULL);
  2232. if (WinConfiguration->ContinueOnError)
  2233. {
  2234. FErrorList = new TStringList();
  2235. Configuration->Usage->Inc(L"ContinuationsOnError");
  2236. }
  2237. NonVisualDataModule->StartBusy();
  2238. }
  2239. //---------------------------------------------------------------------------
  2240. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2241. {
  2242. NonVisualDataModule->EndBusy();
  2243. if (FErrorList)
  2244. {
  2245. HandleErrorList(FErrorList);
  2246. }
  2247. }
  2248. //---------------------------------------------------------------------------
  2249. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2250. const TCopyParamType & CopyParam)
  2251. {
  2252. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2253. {
  2254. Configuration->Usage->Inc(L"FileMaskUses");
  2255. }
  2256. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2257. {
  2258. Configuration->Usage->Inc(L"OperationMaskUses");
  2259. }
  2260. }
  2261. //---------------------------------------------------------------------------
  2262. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2263. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2264. {
  2265. void * BatchStorage;
  2266. BatchStart(BatchStorage);
  2267. bool Result;
  2268. try
  2269. {
  2270. if ((Operation == foCopy) || (Operation == foMove))
  2271. {
  2272. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2273. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2274. UnicodeString TargetDirectory;
  2275. bool Temp = false;
  2276. bool DragDrop = false;
  2277. int Options = 0;
  2278. TAutoSwitch UseQueue = asAuto;
  2279. if (Param != NULL)
  2280. {
  2281. TTransferOperationParam& TParam =
  2282. *static_cast<TTransferOperationParam*>(Param);
  2283. TargetDirectory = TParam.TargetDirectory;
  2284. Temp = TParam.Temp;
  2285. DragDrop = TParam.DragDrop;
  2286. Options = TParam.Options;
  2287. UseQueue = TParam.Queue;
  2288. }
  2289. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2290. switch (UseQueue)
  2291. {
  2292. case asOn:
  2293. CopyParam.Queue = true;
  2294. break;
  2295. case asOff:
  2296. CopyParam.Queue = false;
  2297. break;
  2298. case asAuto:
  2299. default:
  2300. // keep default
  2301. break;
  2302. }
  2303. Result =
  2304. CopyParamDialog(Direction, Type, Temp, FileList, TargetDirectory,
  2305. CopyParam, !NoConfirmation, DragDrop, Options);
  2306. if (Result)
  2307. {
  2308. DebugAssert(Terminal);
  2309. bool SelectionRestored = false;
  2310. TCustomDirView * DView = NULL;
  2311. if (HasDirView[Side])
  2312. {
  2313. DView = DirView(Side);
  2314. DView->SaveSelection();
  2315. DView->SaveSelectedNames();
  2316. }
  2317. UpdateCopyParamCounters(CopyParam);
  2318. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2319. DebugAssert(FTransferResumeList == NULL);
  2320. FTransferResumeList =
  2321. Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2322. FMoveToQueue = false;
  2323. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2324. try
  2325. {
  2326. TStrings * PermanentFileList;
  2327. std::unique_ptr<TStrings> PermanentFileListOwner;
  2328. try
  2329. {
  2330. if (Side == osLocal)
  2331. {
  2332. PermanentFileList = FileList;
  2333. Params |= FLAGMASK(Temp, cpTemporary);
  2334. Terminal->CopyToRemote(FileList, TargetDirectory, &CopyParam, Params, NULL);
  2335. if (Operation == foMove)
  2336. {
  2337. ReloadLocalDirectory();
  2338. if (DView != NULL)
  2339. {
  2340. DView->RestoreSelection();
  2341. }
  2342. SelectionRestored = true;
  2343. }
  2344. }
  2345. else
  2346. {
  2347. // Clone the file list as it may refer to current directory files,
  2348. // which get destroyed, when the source directory is reloaded after foMove operation.
  2349. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2350. // But for a hotfix, we are not going to do such a big change.
  2351. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2352. PermanentFileList = PermanentFileListOwner.get();
  2353. try
  2354. {
  2355. Terminal->CopyToLocal(
  2356. FileList, TargetDirectory, &CopyParam, Params, NULL);
  2357. }
  2358. __finally
  2359. {
  2360. if (Operation == foMove)
  2361. {
  2362. if (DView != NULL)
  2363. {
  2364. DView->RestoreSelection();
  2365. }
  2366. SelectionRestored = true;
  2367. }
  2368. ReloadLocalDirectory(TargetDirectory);
  2369. }
  2370. }
  2371. }
  2372. catch (EAbort &)
  2373. {
  2374. if (FMoveToQueue)
  2375. {
  2376. Params |=
  2377. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2378. DebugAssert(CopyParam.TransferSkipList == NULL);
  2379. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2380. if (TransferResumeList->Count > 0)
  2381. {
  2382. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2383. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2384. }
  2385. CopyParam.TransferSkipList = TransferResumeList.release();
  2386. // not really needed, just to keep it consistent with TransferResumeList
  2387. FTransferResumeList = NULL;
  2388. FMoveToQueue = false;
  2389. Configuration->Usage->Inc("MovesToBackground");
  2390. AddQueueItem(Queue, Direction, PermanentFileList, TargetDirectory, CopyParam, Params);
  2391. ClearTransferSourceSelection(Direction);
  2392. }
  2393. throw;
  2394. }
  2395. }
  2396. __finally
  2397. {
  2398. if (!SelectionRestored && (DView != NULL))
  2399. {
  2400. DView->DiscardSavedSelection();
  2401. }
  2402. FTransferResumeList = NULL;
  2403. }
  2404. }
  2405. }
  2406. else if (Operation == foRename)
  2407. {
  2408. DebugAssert(DirView(Side)->ItemFocused);
  2409. DirView(Side)->ItemFocused->EditCaption();
  2410. Result = true;
  2411. }
  2412. else if (Operation == foDelete)
  2413. {
  2414. DebugAssert(FileList->Count);
  2415. // We deliberately do not toggle alternative flag (Param), but use OR,
  2416. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2417. // shortcut of CurrentDeleteAlternativeAction
  2418. bool Alternative =
  2419. bool(Param) || UseAlternativeFunction();
  2420. bool Recycle;
  2421. if (Side == osLocal)
  2422. {
  2423. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2424. }
  2425. else
  2426. {
  2427. Recycle = (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2428. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2429. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2430. }
  2431. Result =
  2432. !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2433. if (!Result)
  2434. {
  2435. UnicodeString Query;
  2436. if (FileList->Count == 1)
  2437. {
  2438. if (Side == osLocal)
  2439. {
  2440. Query = ExtractFileName(FileList->Strings[0]);
  2441. }
  2442. else
  2443. {
  2444. Query = UnixExtractFileName(FileList->Strings[0]);
  2445. }
  2446. Query = FMTLOAD(
  2447. (Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2448. }
  2449. else
  2450. {
  2451. Query = FMTLOAD(
  2452. (Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2453. }
  2454. TMessageParams Params(mpNeverAskAgainCheck);
  2455. Params.ImageName = L"Delete file";
  2456. unsigned int Answer = MessageDialog(MainInstructions(Query), qtConfirmation,
  2457. qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2458. if (Answer == qaNeverAskAgain)
  2459. {
  2460. Result = true;
  2461. if (Recycle)
  2462. {
  2463. WinConfiguration->ConfirmRecycling = false;
  2464. }
  2465. else
  2466. {
  2467. WinConfiguration->ConfirmDeleting = false;
  2468. }
  2469. }
  2470. else
  2471. {
  2472. Result = (Answer == qaOK);
  2473. }
  2474. }
  2475. if (Result)
  2476. {
  2477. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2478. }
  2479. }
  2480. else if (Operation == foSetProperties)
  2481. {
  2482. RemoteDirView->SaveSelectedNames();
  2483. Result = SetProperties(Side, FileList);
  2484. }
  2485. else if (Operation == foCustomCommand)
  2486. {
  2487. DebugAssert(Param);
  2488. DebugAssert(Side == osRemote);
  2489. RemoteDirView->SaveSelectedNames();
  2490. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2491. CustomCommand(FileList, *Command, NULL);
  2492. Result = true;
  2493. }
  2494. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2495. {
  2496. DebugAssert(Side == osRemote);
  2497. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2498. (Operation == foRemoteMove), reinterpret_cast<TTerminal *>(Param));
  2499. }
  2500. else if (Operation == foLock)
  2501. {
  2502. DebugAssert(Side == osRemote);
  2503. LockFiles(FileList, true);
  2504. Result = true;
  2505. }
  2506. else if (Operation == foUnlock)
  2507. {
  2508. DebugAssert(Side == osRemote);
  2509. LockFiles(FileList, false);
  2510. Result = true;
  2511. }
  2512. else
  2513. {
  2514. DebugFail();
  2515. }
  2516. }
  2517. __finally
  2518. {
  2519. BatchEnd(BatchStorage);
  2520. }
  2521. return Result;
  2522. }
  2523. //---------------------------------------------------------------------------
  2524. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2525. {
  2526. if (Side == osCurrent)
  2527. {
  2528. Side = FCurrentSide;
  2529. }
  2530. return Side;
  2531. }
  2532. //---------------------------------------------------------------------------
  2533. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2534. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2535. {
  2536. Side = GetSide(Side);
  2537. bool Result;
  2538. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, (Side == osLocal), NULL);
  2539. try
  2540. {
  2541. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2542. }
  2543. __finally
  2544. {
  2545. delete FileList;
  2546. }
  2547. return Result;
  2548. }
  2549. //---------------------------------------------------------------------------
  2550. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2551. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2552. bool NoConfirmation, void * Param)
  2553. {
  2554. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2555. {
  2556. if ((Operation == foCopy) || (Operation == foMove))
  2557. {
  2558. if (GetSide(Side) == osLocal)
  2559. {
  2560. Configuration->Usage->Inc(L"UploadsCommand");
  2561. }
  2562. else
  2563. {
  2564. Configuration->Usage->Inc(L"DownloadsCommand");
  2565. }
  2566. }
  2567. }
  2568. }
  2569. //---------------------------------------------------------------------------
  2570. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2571. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2572. {
  2573. TTransferOperationParam Param;
  2574. if ((WinConfiguration->Interface != ifCommander) ||
  2575. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2576. {
  2577. Flags &= ~cocShortCutHint;
  2578. }
  2579. TCustomDirView * DView = DirView(Side);
  2580. Param.Options =
  2581. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2582. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2583. if (FLAGSET(Flags, cocQueue))
  2584. {
  2585. Param.Queue = asOn;
  2586. }
  2587. else if (FLAGSET(Flags, cocNonQueue))
  2588. {
  2589. Param.Queue = asOff;
  2590. }
  2591. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2592. }
  2593. //---------------------------------------------------------------------------
  2594. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2595. {
  2596. try
  2597. {
  2598. if (ErrorList->Count)
  2599. {
  2600. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2601. if (MessageDialog(Message, qtError,
  2602. qaOK | qaCancel, HELP_NONE) == qaOK)
  2603. {
  2604. unsigned int Answer;
  2605. int Index = 0;
  2606. do
  2607. {
  2608. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2609. TQueryButtonAlias Aliases[2];
  2610. Aliases[0].Button = qaYes;
  2611. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2612. Aliases[1].Button = qaNo;
  2613. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2614. TMessageParams Params;
  2615. Params.Aliases = Aliases;
  2616. Params.AliasesCount = LENOF(Aliases);
  2617. Answer = MoreMessageDialog(
  2618. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2619. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2620. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2621. qaOK, HELP_NONE, &Params);
  2622. if (Answer == qaNo)
  2623. {
  2624. Index++;
  2625. }
  2626. if (Answer == qaYes)
  2627. {
  2628. Index--;
  2629. }
  2630. }
  2631. while (Answer != qaOK);
  2632. }
  2633. }
  2634. }
  2635. __finally
  2636. {
  2637. TStrings * List = ErrorList;
  2638. ErrorList = NULL;
  2639. for (int i = 0; i < List->Count; i++)
  2640. {
  2641. delete List->Objects[i];
  2642. }
  2643. delete List;
  2644. }
  2645. }
  2646. //---------------------------------------------------------------------------
  2647. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2648. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2649. {
  2650. // needed for checking filemasks, as there's no directory object
  2651. // associated with the file object
  2652. File->FullFileName = FullFileName;
  2653. TFileMasks::TParams MaskParams;
  2654. MaskParams.Size = File->Size;
  2655. MaskParams.Modification = File->Modification;
  2656. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2657. }
  2658. //---------------------------------------------------------------------------
  2659. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2660. {
  2661. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2662. Side = GetSide(Side);
  2663. TCustomDirView * DView = DirView(Side);
  2664. TListItem * FocusedItem = DView->ItemFocused;
  2665. UnicodeString Name;
  2666. if ((FocusedItem != NULL) && !DView->ItemIsParentDirectory(FocusedItem))
  2667. {
  2668. Name = DView->ItemFileName(FocusedItem);
  2669. }
  2670. else
  2671. {
  2672. Name = LoadStr(NEW_FILE);
  2673. }
  2674. UnicodeString Names = Name;
  2675. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2676. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2677. HELP_EDIT_NEW, History.get(), true))
  2678. {
  2679. while (!Names.IsEmpty())
  2680. {
  2681. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2682. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2683. UnicodeString TargetFileName;
  2684. UnicodeString LocalFileName;
  2685. UnicodeString RootTempDir;
  2686. UnicodeString TempDir;
  2687. UnicodeString RemoteDirectory;
  2688. bool ExistingFile = false;
  2689. if (Side == osRemote)
  2690. {
  2691. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2692. TRemoteFile * File = NULL;
  2693. if (FTerminal->FileExists(Name, &File))
  2694. {
  2695. try
  2696. {
  2697. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2698. ExistingFile = true;
  2699. }
  2700. __finally
  2701. {
  2702. delete File;
  2703. }
  2704. }
  2705. if (!ExistingFile)
  2706. {
  2707. RemoteDirectory = UnixExtractFilePath(Name);
  2708. TemporaryDirectoryForRemoteFiles(
  2709. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2710. TargetFileName = UnixExtractFileName(Name);
  2711. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2712. LocalFileName = TempDir +
  2713. // We probably do not want to trim the VMS version here
  2714. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2715. }
  2716. }
  2717. else
  2718. {
  2719. if (ExtractFilePath(Name).IsEmpty())
  2720. {
  2721. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2722. }
  2723. else
  2724. {
  2725. LocalFileName = ExpandFileName(Name);
  2726. }
  2727. TargetFileName = ExtractFileName(Name);
  2728. }
  2729. if (!ExistingFile)
  2730. {
  2731. if (!FileExists(ApiPath(LocalFileName)))
  2732. {
  2733. int File = FileCreate(ApiPath(LocalFileName));
  2734. if (File < 0)
  2735. {
  2736. if (!RootTempDir.IsEmpty())
  2737. {
  2738. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2739. }
  2740. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2741. }
  2742. else
  2743. {
  2744. FileClose(File);
  2745. }
  2746. }
  2747. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2748. const TEditorData * ExternalEditor = NULL;
  2749. TFileMasks::TParams MaskParams; // size not known
  2750. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2751. false, MaskParams);
  2752. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2753. ExternalEditor, RootTempDir, RemoteDirectory);
  2754. }
  2755. }
  2756. }
  2757. }
  2758. //---------------------------------------------------------------------------
  2759. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2760. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2761. {
  2762. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2763. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2764. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2765. return
  2766. ((ExecuteFileBy == efInternalEditor)) ||
  2767. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2768. }
  2769. //---------------------------------------------------------------------------
  2770. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2771. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2772. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2773. UnicodeString RemoteDirectory)
  2774. {
  2775. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2776. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2777. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2778. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2779. Side = GetSide(Side);
  2780. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2781. if (Side == osRemote)
  2782. {
  2783. Data->Terminal = Terminal;
  2784. Data->Queue = Queue;
  2785. Data->SessionData = CloneCurrentSessionData();
  2786. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2787. Data->RemoteDirectory = RemoteDirectory;
  2788. Data->SessionName = Terminal->SessionData->SessionName;
  2789. Data->LocalRootDirectory = LocalRootDirectory;
  2790. Data->OriginalFileName = OriginalFileName;
  2791. Data->Command = L""; // will be changed later for external editor
  2792. }
  2793. if (ExecuteFileBy == efInternalEditor)
  2794. {
  2795. if (Side == osRemote)
  2796. {
  2797. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2798. L" - " + Terminal->SessionData->SessionName;
  2799. TForm * Editor;
  2800. try
  2801. {
  2802. TNotifyEvent OnFileChanged = NULL;
  2803. TNotifyEvent OnSaveAll = NULL;
  2804. TAnyModifiedEvent OnAnyModified = NULL;
  2805. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2806. if (Terminal->IsCapable[fsBackgroundTransfers])
  2807. {
  2808. OnFileChanged = FEditorManager->FileChanged;
  2809. OnSaveAll = SaveAllInternalEditors;
  2810. OnAnyModified = AnyInternalEditorModified;
  2811. }
  2812. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2813. FEditorManager->FileReload, FEditorManager->FileClosed,
  2814. OnSaveAll, OnAnyModified,
  2815. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding);
  2816. }
  2817. catch(...)
  2818. {
  2819. if (!LocalRootDirectory.IsEmpty())
  2820. {
  2821. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2822. }
  2823. throw;
  2824. }
  2825. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2826. }
  2827. else
  2828. {
  2829. DebugAssert(!FStandaloneEditing);
  2830. TForm * Editor =
  2831. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2832. SaveAllInternalEditors, AnyInternalEditorModified,
  2833. L"", false, SessionColor, -1);
  2834. FLocalEditors->Add(Editor);
  2835. }
  2836. }
  2837. else
  2838. {
  2839. HANDLE Process;
  2840. if (ExecuteFileBy == efExternalEditor)
  2841. {
  2842. UnicodeString Program, Params, Dir;
  2843. Data->Command = ExternalEditor->ExternalEditor;
  2844. ReformatFileNameCommand(Data->Command);
  2845. SplitCommand(Data->Command, Program, Params, Dir);
  2846. Params = ExpandFileNameCommand(Params, FileName);
  2847. Program = ExpandEnvironmentVariables(Program);
  2848. if (!ExecuteShell(Program, Params, Process))
  2849. {
  2850. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2851. }
  2852. }
  2853. else
  2854. {
  2855. DebugAssert(Side == osRemote);
  2856. if (!ExecuteShell(FileName, L"", Process))
  2857. {
  2858. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2859. }
  2860. }
  2861. if ((Side == osLocal) ||
  2862. ((ExecuteFileBy == efShell) &&
  2863. !WinConfiguration->Editor.SDIShellEditor) ||
  2864. ((ExecuteFileBy == efExternalEditor) &&
  2865. !ExternalEditor->SDIExternalEditor))
  2866. {
  2867. // no need for handle
  2868. if (Process != NULL)
  2869. {
  2870. DebugCheck(CloseHandle(Process));
  2871. }
  2872. Process = INVALID_HANDLE_VALUE;
  2873. }
  2874. else
  2875. {
  2876. if (Process == NULL)
  2877. {
  2878. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2879. }
  2880. }
  2881. if (Side == osRemote)
  2882. {
  2883. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2884. }
  2885. }
  2886. }
  2887. //---------------------------------------------------------------------------
  2888. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2889. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2890. void * /*Arg*/)
  2891. {
  2892. if (Token != NULL)
  2893. {
  2894. EditorFormFileSave(static_cast<TForm *>(Token));
  2895. }
  2896. }
  2897. //---------------------------------------------------------------------------
  2898. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  2899. {
  2900. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  2901. {
  2902. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  2903. }
  2904. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  2905. }
  2906. //---------------------------------------------------------------------------
  2907. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  2908. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2909. void * Arg)
  2910. {
  2911. if ((Token != NULL) &&
  2912. IsEditorFormModified(static_cast<TForm *>(Token)))
  2913. {
  2914. *static_cast<bool *>(Arg) = true;
  2915. }
  2916. }
  2917. //---------------------------------------------------------------------------
  2918. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  2919. TObject * /*Sender*/, bool & Modified)
  2920. {
  2921. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  2922. {
  2923. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  2924. {
  2925. Modified = true;
  2926. }
  2927. }
  2928. if (!Modified)
  2929. {
  2930. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  2931. }
  2932. }
  2933. //---------------------------------------------------------------------------
  2934. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  2935. {
  2936. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  2937. }
  2938. //---------------------------------------------------------------------------
  2939. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  2940. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  2941. UnicodeString & Result, UnicodeString & RootDirectory)
  2942. {
  2943. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  2944. {
  2945. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  2946. Result = RootDirectory;
  2947. }
  2948. else
  2949. {
  2950. RootDirectory = L"";
  2951. Result = WinConfiguration->ExpandedTemporaryDirectory();
  2952. Result = IncludeTrailingBackslash(Result);
  2953. }
  2954. if (WinConfiguration->TemporaryDirectoryAppendSession)
  2955. {
  2956. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  2957. }
  2958. if (WinConfiguration->TemporaryDirectoryAppendPath)
  2959. {
  2960. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  2961. {
  2962. RemoteDirectory.Delete(1, 1);
  2963. }
  2964. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  2965. }
  2966. if (!ForceDirectories(ApiPath(Result)))
  2967. {
  2968. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  2969. }
  2970. }
  2971. //---------------------------------------------------------------------------
  2972. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  2973. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  2974. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  2975. {
  2976. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2977. if (ForceText)
  2978. {
  2979. CopyParam.TransferMode = tmAscii;
  2980. }
  2981. TemporaryFileCopyParam(CopyParam);
  2982. if (AllFiles)
  2983. {
  2984. CopyParam.IncludeFileMask = TFileMasks();
  2985. }
  2986. if (TempDir.IsEmpty())
  2987. {
  2988. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  2989. }
  2990. DebugAssert(!FAutoOperation);
  2991. FAutoOperation = AutoOperation;
  2992. Terminal->ExceptionOnFail = true;
  2993. try
  2994. {
  2995. try
  2996. {
  2997. // turn off confirmations, as for MDI editors we may possibly download
  2998. // the same file over
  2999. Terminal->CopyToLocal(
  3000. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3001. if (GetTargetNames)
  3002. {
  3003. for (int i = 0; i < FileList->Count; i++)
  3004. {
  3005. FileList->Strings[i] =
  3006. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3007. }
  3008. }
  3009. }
  3010. catch(...)
  3011. {
  3012. if (!RootTempDir.IsEmpty())
  3013. {
  3014. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3015. }
  3016. throw;
  3017. }
  3018. }
  3019. __finally
  3020. {
  3021. FAutoOperation = false;
  3022. Terminal->ExceptionOnFail = false;
  3023. }
  3024. }
  3025. //---------------------------------------------------------------------------
  3026. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3027. {
  3028. // Do not waste time checking for default editor list the next time,
  3029. // or testing if default editor is not notepad.
  3030. bool TryNextTime = false;
  3031. UnicodeString UsageState;
  3032. if (!WinConfiguration->EditorList->IsDefaultList())
  3033. {
  3034. UsageState = "H";
  3035. }
  3036. else
  3037. {
  3038. UnicodeString Executable;
  3039. UnicodeString ExecutableDescription;
  3040. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3041. {
  3042. UnicodeString Message =
  3043. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3044. unsigned int Answer =
  3045. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3046. if (Answer != qaOK)
  3047. {
  3048. UsageState = "R";
  3049. }
  3050. else
  3051. {
  3052. UsageState = "A";
  3053. TEditorData EditorData;
  3054. EditorData.Editor = edExternal;
  3055. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3056. EditorData.DecideExternalEditorText();
  3057. TEditorList EditorList;
  3058. EditorList = *WinConfiguration->EditorList;
  3059. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3060. WinConfiguration->EditorList = &EditorList;
  3061. }
  3062. }
  3063. }
  3064. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3065. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3066. }
  3067. //---------------------------------------------------------------------------
  3068. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3069. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3070. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3071. {
  3072. if (ExecuteFileBy == efDefaultEditor)
  3073. {
  3074. if (!WinConfiguration->OfferedEditorAutoConfig)
  3075. {
  3076. EditorAutoConfig();
  3077. }
  3078. const TEditorPreferences * Editor =
  3079. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3080. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3081. {
  3082. ExecuteFileBy = efInternalEditor;
  3083. ExternalEditor = NULL;
  3084. }
  3085. else if (Editor->Data->Editor == edOpen)
  3086. {
  3087. ExecuteFileBy = efShell;
  3088. ExternalEditor = NULL;
  3089. }
  3090. else
  3091. {
  3092. ExecuteFileBy = efExternalEditor;
  3093. ExternalEditor = Editor->Data;
  3094. }
  3095. }
  3096. }
  3097. //---------------------------------------------------------------------------
  3098. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3099. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3100. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3101. {
  3102. UnicodeString OriginalFileName;
  3103. UnicodeString LocalRootDirectory;
  3104. UnicodeString RemoteDirectory;
  3105. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3106. (Side == osLocal), MaskParams);
  3107. UnicodeString Counter;
  3108. UnicodeString LocalFileName;
  3109. bool Handled = false;
  3110. if (Side == osRemote)
  3111. {
  3112. // We need to trim VMS version here, so that we use name without version
  3113. // when uploading back to create a new version of the file
  3114. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3115. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3116. TObject * Token = NULL;
  3117. UnicodeString LocalDirectory;
  3118. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3119. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3120. LocalDirectory))
  3121. {
  3122. if (Token != NULL)
  3123. {
  3124. TForm * Form = dynamic_cast<TForm *>(Token);
  3125. if (Form->WindowState == wsMinimized)
  3126. {
  3127. ShowWindow(Form->Handle, SW_RESTORE);
  3128. }
  3129. else
  3130. {
  3131. Form->SetFocus();
  3132. }
  3133. Handled = true;
  3134. }
  3135. else
  3136. {
  3137. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3138. }
  3139. }
  3140. if (!Handled)
  3141. {
  3142. TStringList * FileList1 = new TStringList();
  3143. try
  3144. {
  3145. FileList1->AddObject(FullFileName, Object);
  3146. TemporarilyDownloadFiles(FileList1,
  3147. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3148. LocalRootDirectory, LocalDirectory, true, true, true);
  3149. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3150. }
  3151. __finally
  3152. {
  3153. delete FileList1;
  3154. }
  3155. switch (ExecuteFileBy)
  3156. {
  3157. case efShell:
  3158. Counter = "RemoteFilesExecuted";
  3159. break;
  3160. case efInternalEditor:
  3161. Counter = "RemoteFilesOpenedInInternalEditor";
  3162. break;
  3163. case efExternalEditor:
  3164. Counter = "RemoteFilesOpenedInExternalEditor";
  3165. break;
  3166. default:
  3167. DebugFail();
  3168. }
  3169. }
  3170. }
  3171. else
  3172. {
  3173. LocalFileName = FullFileName;
  3174. OriginalFileName = ExtractFileName(FullFileName);
  3175. switch (ExecuteFileBy)
  3176. {
  3177. case efShell:
  3178. Counter = "LocalFilesExecuted";
  3179. break;
  3180. case efInternalEditor:
  3181. Counter = "LocalFilesOpenedInInternalEditor";
  3182. break;
  3183. case efExternalEditor:
  3184. Counter = "LocalFilesOpenedInExternalEditor";
  3185. break;
  3186. default:
  3187. DebugFail();
  3188. }
  3189. }
  3190. if (!Handled)
  3191. {
  3192. Configuration->Usage->Inc(Counter);
  3193. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3194. ExternalEditor, LocalRootDirectory, RemoteDirectory);
  3195. }
  3196. }
  3197. //---------------------------------------------------------------------------
  3198. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3199. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3200. bool AllSelected, bool OnFocused)
  3201. {
  3202. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3203. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3204. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3205. Side = GetSide(Side);
  3206. TCustomDirView * DView = DirView(Side);
  3207. TStrings * FileList = AllSelected ?
  3208. DView->CreateFileList(OnFocused, Side == osLocal) :
  3209. DView->CreateFocusedFileList(Side == osLocal);
  3210. try
  3211. {
  3212. DebugAssert(AllSelected || (FileList->Count == 1));
  3213. for (int i = 0; i < FileList->Count; i++)
  3214. {
  3215. UnicodeString ListFileName = FileList->Strings[i];
  3216. UnicodeString FileNameOnly = (Side == osRemote) ?
  3217. UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3218. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3219. if (!DView->ItemIsDirectory(Item))
  3220. {
  3221. UnicodeString FullFileName;
  3222. if (Side == osRemote)
  3223. {
  3224. FullFileName = RemoteDirView->Path + ListFileName;
  3225. }
  3226. else
  3227. {
  3228. FullFileName = ListFileName;
  3229. }
  3230. TFileMasks::TParams MaskParams;
  3231. MaskParams.Size = DView->ItemFileSize(Item);
  3232. TDateTimePrecision Precision;
  3233. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3234. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3235. FileList->Objects[i], MaskParams);
  3236. }
  3237. }
  3238. }
  3239. __finally
  3240. {
  3241. delete FileList;
  3242. }
  3243. }
  3244. //---------------------------------------------------------------------------
  3245. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3246. {
  3247. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3248. CopyParam.FileNameCase = ncNoChange;
  3249. CopyParam.PreserveRights = false;
  3250. CopyParam.PreserveReadOnly = false;
  3251. CopyParam.ReplaceInvalidChars = true;
  3252. CopyParam.IncludeFileMask = TFileMasks();
  3253. CopyParam.NewerOnly = false;
  3254. CopyParam.FileMask = L"";
  3255. }
  3256. //---------------------------------------------------------------------------
  3257. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3258. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3259. {
  3260. TTerminalManager * Manager = TTerminalManager::Instance();
  3261. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3262. {
  3263. if (!NonVisualDataModule->Busy)
  3264. {
  3265. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3266. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3267. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3268. if (Data->Terminal == NULL)
  3269. {
  3270. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3271. if (SameSiteTerminal != NULL)
  3272. {
  3273. UnicodeString Message =
  3274. FMTLOAD(EDIT_SESSION_REATTACH,
  3275. (FileNameOnly, Data->SessionName, FileNameOnly));
  3276. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3277. {
  3278. Data->Terminal = Terminal;
  3279. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3280. Data->SessionName = Terminal->SessionData->SessionName;
  3281. // We might also overwrite session data
  3282. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3283. }
  3284. else
  3285. {
  3286. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3287. Abort();
  3288. }
  3289. }
  3290. }
  3291. // foreground session should reconnect itself
  3292. else if (Terminal != Data->Terminal)
  3293. {
  3294. UnicodeString Message =
  3295. MainInstructions(
  3296. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3297. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3298. {
  3299. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3300. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3301. }
  3302. else
  3303. {
  3304. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3305. Abort();
  3306. }
  3307. }
  3308. }
  3309. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3310. {
  3311. Configuration->Usage->Inc(L"EditInactiveSession");
  3312. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3313. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3314. (ExtractFileName(FileName), Data->SessionName)));
  3315. }
  3316. }
  3317. TStrings * FileList = new TStringList();
  3318. try
  3319. {
  3320. FileList->Add(FileName);
  3321. // consider using the same settings (preset) as when the file was downloaded
  3322. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3323. TemporaryFileCopyParam(CopyParam);
  3324. if (Data->ForceText)
  3325. {
  3326. CopyParam.TransferMode = tmAscii;
  3327. }
  3328. // so i do not need to worry if masking algorithm works in all cases
  3329. // ("" means "copy file name", no masking is actually done)
  3330. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3331. {
  3332. CopyParam.FileMask = L"";
  3333. }
  3334. else
  3335. {
  3336. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3337. }
  3338. DebugAssert(Data->Queue != NULL);
  3339. int Params = cpNoConfirmation | cpTemporary;
  3340. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3341. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3342. QueueItem->CompleteEvent = UploadCompleteEvent;
  3343. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3344. }
  3345. __finally
  3346. {
  3347. delete FileList;
  3348. }
  3349. }
  3350. //---------------------------------------------------------------------------
  3351. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3352. const UnicodeString FileName, const TEditedFileData * Data)
  3353. {
  3354. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3355. DebugAssert(!NonVisualDataModule->Busy);
  3356. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3357. {
  3358. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3359. (ExtractFileName(FileName), Data->SessionName)));
  3360. }
  3361. TTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3362. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3363. NonVisualDataModule->StartBusy();
  3364. try
  3365. {
  3366. std::unique_ptr<TRemoteFile> File;
  3367. UnicodeString RemoteFileName =
  3368. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3369. FTerminal->ExceptionOnFail = true;
  3370. try
  3371. {
  3372. TRemoteFile * AFile = NULL;
  3373. FTerminal->ReadFile(RemoteFileName, AFile);
  3374. File.reset(AFile);
  3375. if (!File->HaveFullFileName)
  3376. {
  3377. File->FullFileName = RemoteFileName;
  3378. }
  3379. }
  3380. __finally
  3381. {
  3382. FTerminal->ExceptionOnFail = false;
  3383. }
  3384. std::unique_ptr<TStrings> FileList(new TStringList());
  3385. FileList->AddObject(RemoteFileName, File.get());
  3386. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3387. UnicodeString TempDir = ExtractFilePath(FileName);
  3388. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3389. TempDir, true, true, true);
  3390. // sanity check, the target file name should be still the same
  3391. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3392. }
  3393. __finally
  3394. {
  3395. NonVisualDataModule->EndBusy();
  3396. // it actually may not exist anymore...
  3397. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3398. }
  3399. }
  3400. //---------------------------------------------------------------------------
  3401. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3402. const TEditedFileData * Data, bool & KeepOpen)
  3403. {
  3404. // Command is set for external editors only (not for "shell" open).
  3405. if (!Data->Command.IsEmpty())
  3406. {
  3407. bool AnyFound = false;
  3408. bool AnyMDI = false;
  3409. bool AnyNonMDI = false;
  3410. bool AnyDetect = false;
  3411. TEditorList * EditorList = new TEditorList();
  3412. try
  3413. {
  3414. *EditorList = *WinConfiguration->EditorList;
  3415. for (int i = 0; i < EditorList->Count; i++)
  3416. {
  3417. const TEditorPreferences * Editor = EditorList->Editors[i];
  3418. if ((Editor->Data->Editor == edExternal) &&
  3419. (Editor->Data->ExternalEditor == Data->Command))
  3420. {
  3421. AnyFound = true;
  3422. if (Editor->Data->SDIExternalEditor)
  3423. {
  3424. AnyNonMDI = true;
  3425. if (Editor->Data->DetectMDIExternalEditor)
  3426. {
  3427. AnyDetect = true;
  3428. }
  3429. }
  3430. else
  3431. {
  3432. AnyMDI = true;
  3433. }
  3434. }
  3435. }
  3436. bool EnableMDI = false;
  3437. bool DisableDetect = false;
  3438. if (AnyMDI)
  3439. {
  3440. KeepOpen = true;
  3441. if (AnyNonMDI)
  3442. {
  3443. // there is at least one instance of the editor with MDI support enabled,
  3444. // and one with disabled, enable it for all instances
  3445. EnableMDI = true;
  3446. }
  3447. }
  3448. else if (AnyFound && !AnyDetect)
  3449. {
  3450. // at least once instance found but all have MDI autodetection disabled
  3451. // => close the file (default action)
  3452. }
  3453. else
  3454. {
  3455. // no instance of the editor has MDI support enabled
  3456. TMessageParams Params;
  3457. if (AnyFound)
  3458. {
  3459. // there is at least one instance of the editor
  3460. Params.Params |= mpNeverAskAgainCheck;
  3461. }
  3462. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3463. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3464. switch (Answer)
  3465. {
  3466. case qaNeverAskAgain:
  3467. DisableDetect = true;
  3468. break;
  3469. case qaNo:
  3470. EnableMDI = true;
  3471. KeepOpen = true;
  3472. break;
  3473. }
  3474. }
  3475. if (AnyFound && (EnableMDI || DisableDetect))
  3476. {
  3477. bool Changed = false;
  3478. for (int i = 0; i < EditorList->Count; i++)
  3479. {
  3480. const TEditorPreferences * Editor = EditorList->Editors[i];
  3481. if ((Editor->Data->Editor == edExternal) &&
  3482. (Editor->Data->ExternalEditor == Data->Command) &&
  3483. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3484. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3485. {
  3486. Changed = true;
  3487. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3488. if (EnableMDI)
  3489. {
  3490. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3491. }
  3492. if (DisableDetect)
  3493. {
  3494. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3495. }
  3496. EditorList->Change(i, UpdatedEditor);
  3497. }
  3498. }
  3499. if (Changed)
  3500. {
  3501. WinConfiguration->EditorList = EditorList;
  3502. }
  3503. }
  3504. }
  3505. __finally
  3506. {
  3507. delete EditorList;
  3508. }
  3509. }
  3510. else
  3511. {
  3512. // "open" case
  3513. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3514. qaOK, HELP_APP_EARLY_CLOSED);
  3515. }
  3516. }
  3517. //---------------------------------------------------------------------------
  3518. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3519. {
  3520. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3521. }
  3522. //---------------------------------------------------------------------------
  3523. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3524. {
  3525. SideEnter(osRemote);
  3526. }
  3527. //---------------------------------------------------------------------------
  3528. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3529. {
  3530. MakeNextInTabOrder(RemoteDirView, RemoteDriveView);
  3531. SideEnter(osRemote);
  3532. }
  3533. //---------------------------------------------------------------------------
  3534. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3535. {
  3536. FCurrentSide = Side;
  3537. if (Visible)
  3538. {
  3539. UpdateControls();
  3540. }
  3541. }
  3542. //---------------------------------------------------------------------------
  3543. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3544. TStrings * FileList, bool Alternative)
  3545. {
  3546. TCustomDirView * DView = DirView(Side);
  3547. DView->SaveSelection();
  3548. DView->SaveSelectedNames();
  3549. DebugAssert(!FAlternativeDelete);
  3550. FAlternativeDelete = Alternative;
  3551. try
  3552. {
  3553. if (Side == osRemote)
  3554. {
  3555. DebugAssert(Terminal != NULL);
  3556. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3557. }
  3558. else
  3559. {
  3560. try
  3561. {
  3562. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3563. }
  3564. __finally
  3565. {
  3566. ReloadLocalDirectory();
  3567. }
  3568. }
  3569. FAlternativeDelete = false;
  3570. }
  3571. catch(...)
  3572. {
  3573. FAlternativeDelete = false;
  3574. DView->DiscardSavedSelection();
  3575. throw;
  3576. }
  3577. DView->RestoreSelection();
  3578. }
  3579. //---------------------------------------------------------------------------
  3580. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3581. {
  3582. DebugAssert(Terminal);
  3583. RemoteDirView->SaveSelection();
  3584. RemoteDirView->SaveSelectedNames();
  3585. try
  3586. {
  3587. if (Lock)
  3588. {
  3589. Terminal->LockFiles(FileList);
  3590. }
  3591. else
  3592. {
  3593. Terminal->UnlockFiles(FileList);
  3594. }
  3595. }
  3596. catch(...)
  3597. {
  3598. RemoteDirView->DiscardSavedSelection();
  3599. throw;
  3600. }
  3601. RemoteDirView->RestoreSelection();
  3602. }
  3603. //---------------------------------------------------------------------------
  3604. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TTerminal *& Session,
  3605. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3606. bool NoConfirmation, bool Move)
  3607. {
  3608. if (RemoteDriveView->DropTarget != NULL)
  3609. {
  3610. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3611. }
  3612. else if (RemoteDirView->DropTarget != NULL)
  3613. {
  3614. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3615. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3616. }
  3617. else
  3618. {
  3619. Target = RemoteDirView->Path;
  3620. }
  3621. if (Session == NULL)
  3622. {
  3623. Session = TTerminalManager::Instance()->ActiveTerminal;
  3624. }
  3625. Target = UnixIncludeTrailingBackslash(Target);
  3626. if (FileList->Count == 1)
  3627. {
  3628. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3629. }
  3630. else
  3631. {
  3632. FileMask = AnyMask;
  3633. }
  3634. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3635. bool Result = true;
  3636. if (!NoConfirmation)
  3637. {
  3638. bool Multi = (FileList->Count > 1);
  3639. if (Move)
  3640. {
  3641. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3642. }
  3643. else
  3644. {
  3645. DebugAssert(Terminal != NULL);
  3646. // update Terminal->StateData->RemoteDirectory
  3647. UpdateTerminal(Terminal);
  3648. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3649. TStrings * Directories = new TStringList;
  3650. try
  3651. {
  3652. for (int Index = 0; Index < Sessions->Count; Index++)
  3653. {
  3654. TManagedTerminal * Terminal =
  3655. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3656. Directories->Add(Terminal->StateData->RemoteDirectory);
  3657. }
  3658. TDirectRemoteCopy AllowDirectCopy;
  3659. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3660. {
  3661. DebugAssert(DirectCopy);
  3662. AllowDirectCopy = drcAllow;
  3663. }
  3664. else if (FTerminal->IsCapable[fcSecondaryShell])
  3665. {
  3666. DebugAssert(DirectCopy);
  3667. AllowDirectCopy = drcConfirmCommandSession;
  3668. }
  3669. else
  3670. {
  3671. DebugAssert(!DirectCopy);
  3672. AllowDirectCopy = drcDisallow;
  3673. }
  3674. void * ASession = Session;
  3675. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3676. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3677. Session = static_cast<TTerminal *>(ASession);
  3678. }
  3679. __finally
  3680. {
  3681. delete Directories;
  3682. }
  3683. }
  3684. }
  3685. return Result;
  3686. }
  3687. //---------------------------------------------------------------------------
  3688. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3689. TStrings * FileList, bool NoConfirmation, bool Move, TTerminal * Session)
  3690. {
  3691. bool DirectCopy;
  3692. UnicodeString Target, FileMask;
  3693. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3694. if (Result)
  3695. {
  3696. if (!Move && !DirectCopy)
  3697. {
  3698. Configuration->Usage->Inc("RemoteCopyTemp");
  3699. UnicodeString RootTempDir;
  3700. UnicodeString TempDir;
  3701. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3702. TStrings * TemporaryFilesList = new TStringList();
  3703. try
  3704. {
  3705. TMakeLocalFileListParams MakeFileListParam;
  3706. MakeFileListParam.FileList = TemporaryFilesList;
  3707. MakeFileListParam.FileTimes = NULL;
  3708. MakeFileListParam.IncludeDirs = true;
  3709. MakeFileListParam.Recursive = false;
  3710. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3711. TTerminalManager::Instance()->ActiveTerminal = Session;
  3712. if (TemporaryFilesList->Count > 0)
  3713. {
  3714. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3715. CopyParam.FileMask = FileMask;
  3716. DebugAssert(!FAutoOperation);
  3717. FAutoOperation = true;
  3718. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3719. }
  3720. }
  3721. __finally
  3722. {
  3723. delete TemporaryFilesList;
  3724. FAutoOperation = false;
  3725. if (!RootTempDir.IsEmpty())
  3726. {
  3727. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3728. }
  3729. }
  3730. }
  3731. else
  3732. {
  3733. RemoteDirView->SaveSelection();
  3734. RemoteDirView->SaveSelectedNames();
  3735. try
  3736. {
  3737. if (Move)
  3738. {
  3739. Configuration->Usage->Inc("RemoteMove");
  3740. Terminal->MoveFiles(FileList, Target, FileMask);
  3741. }
  3742. else
  3743. {
  3744. Configuration->Usage->Inc("RemoteCopyDirect");
  3745. DebugAssert(DirectCopy);
  3746. DebugAssert(Session == FTerminal);
  3747. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3748. FTerminal->CommandSessionOpened ||
  3749. CommandSessionFallback())
  3750. {
  3751. Terminal->CopyFiles(FileList, Target, FileMask);
  3752. }
  3753. }
  3754. }
  3755. catch(...)
  3756. {
  3757. RemoteDirView->DiscardSavedSelection();
  3758. throw;
  3759. }
  3760. RemoteDirView->RestoreSelection();
  3761. }
  3762. }
  3763. return Result;
  3764. }
  3765. //---------------------------------------------------------------------------
  3766. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3767. const UnicodeString & Path, TRemoteProperties & Properties)
  3768. {
  3769. Properties.Valid = Properties.Valid << vpEncrypt;
  3770. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3771. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3772. }
  3773. //---------------------------------------------------------------------------
  3774. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3775. {
  3776. Side = GetSide(Side);
  3777. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3778. TRemoteProperties * AProperties = (Side == osRemote ? &Properties : NULL);
  3779. UnicodeString Name = LoadStr(NEW_FOLDER);
  3780. int AllowedChanges =
  3781. FLAGMASK((Side == osRemote) && Terminal->IsCapable[fcModeChanging], cpMode);
  3782. bool SaveSettings = false;
  3783. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3784. {
  3785. TWindowLock Lock(this);
  3786. if (Side == osRemote)
  3787. {
  3788. if (SaveSettings)
  3789. {
  3790. GUIConfiguration->NewDirectoryProperties = Properties;
  3791. }
  3792. CreateRemoteDirectory(Name, Properties);
  3793. }
  3794. else
  3795. {
  3796. DirView(Side)->CreateDirectory(Name);
  3797. }
  3798. }
  3799. }
  3800. //---------------------------------------------------------------------------
  3801. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3802. {
  3803. TWindowLock Lock(this);
  3804. DirView(Side)->ExecuteHomeDirectory();
  3805. }
  3806. //---------------------------------------------------------------------------
  3807. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3808. {
  3809. DoOpenDirectoryDialog(odBrowse, Side);
  3810. }
  3811. //---------------------------------------------------------------------------
  3812. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3813. {
  3814. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3815. bool Result = !Path.IsEmpty();
  3816. if (Result)
  3817. {
  3818. // While we might get here when the session is closed (from location profiles),
  3819. // it's not a problem as the Path setter is noop then
  3820. DirView(Side)->Path = Path;
  3821. }
  3822. return Result;
  3823. }
  3824. //---------------------------------------------------------------------------
  3825. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3826. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3827. bool & Close)
  3828. {
  3829. // terminal can be already closed (e.g. dropped connection)
  3830. if (Terminal != NULL)
  3831. {
  3832. try
  3833. {
  3834. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3835. }
  3836. catch(...)
  3837. {
  3838. if (!Terminal->Active)
  3839. {
  3840. Close = true;
  3841. }
  3842. throw;
  3843. }
  3844. }
  3845. }
  3846. //---------------------------------------------------------------------------
  3847. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3848. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3849. bool & Close)
  3850. {
  3851. // terminal can be already closed (e.g. dropped connection)
  3852. if (Terminal != NULL)
  3853. {
  3854. Configuration->Usage->Inc(L"ChecksumCalculated");
  3855. try
  3856. {
  3857. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3858. }
  3859. catch(...)
  3860. {
  3861. if (!Terminal->Active)
  3862. {
  3863. Close = true;
  3864. }
  3865. throw;
  3866. }
  3867. }
  3868. }
  3869. //---------------------------------------------------------------------------
  3870. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3871. {
  3872. bool Result;
  3873. if (Side == osRemote)
  3874. {
  3875. TRemoteTokenList * GroupList = NULL;
  3876. TRemoteTokenList * UserList = NULL;
  3877. try
  3878. {
  3879. TRemoteProperties CurrentProperties;
  3880. if (Terminal->LoadFilesProperties(FileList))
  3881. {
  3882. RemoteDirView->Invalidate();
  3883. }
  3884. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  3885. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  3886. if (CapableGroupChanging || CapableOwnerChanging)
  3887. {
  3888. if (CapableGroupChanging)
  3889. {
  3890. GroupList = Terminal->Groups->Duplicate();
  3891. }
  3892. if (CapableOwnerChanging)
  3893. {
  3894. UserList = Terminal->Users->Duplicate();
  3895. }
  3896. TRemoteDirectory * Files = Terminal->Files;
  3897. int Count = Files->Count;
  3898. if (Count > 100)
  3899. {
  3900. Count = 100;
  3901. }
  3902. for (int Index = 0; Index < Count; Index++)
  3903. {
  3904. TRemoteFile * File = Files->Files[Index];
  3905. if (CapableGroupChanging)
  3906. {
  3907. GroupList->AddUnique(File->Group);
  3908. }
  3909. if (CapableOwnerChanging)
  3910. {
  3911. UserList->AddUnique(File->Owner);
  3912. }
  3913. }
  3914. // if we haven't collected tokens for all files in current directory,
  3915. // make sure we collect them at least for all selected files.
  3916. // (note that so far the files in FileList has to be from current direcotry)
  3917. if (Count < Files->Count)
  3918. {
  3919. for (int Index = 0; Index < FileList->Count; Index++)
  3920. {
  3921. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  3922. if (CapableGroupChanging)
  3923. {
  3924. GroupList->AddUnique(File->Group);
  3925. }
  3926. if (CapableOwnerChanging)
  3927. {
  3928. UserList->AddUnique(File->Owner);
  3929. }
  3930. }
  3931. }
  3932. }
  3933. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  3934. int Flags = 0;
  3935. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  3936. if (CapableOwnerChanging) Flags |= cpOwner;
  3937. if (CapableGroupChanging) Flags |= cpGroup;
  3938. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  3939. if (Terminal->IsCapable[fcCalculatingChecksum])
  3940. {
  3941. CalculateChecksumEvent = CalculateChecksum;
  3942. }
  3943. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  3944. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  3945. TRemoteProperties NewProperties = CurrentProperties;
  3946. Result =
  3947. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  3948. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  3949. Terminal->IsCapable[fcGroupOwnerChangingByID],
  3950. CalculateSize, CalculateChecksumEvent);
  3951. if (Result)
  3952. {
  3953. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  3954. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  3955. }
  3956. }
  3957. __finally
  3958. {
  3959. delete GroupList;
  3960. delete UserList;
  3961. }
  3962. }
  3963. else
  3964. {
  3965. DirView(Side)->DisplayPropertiesMenu();
  3966. Result = true;
  3967. }
  3968. return Result;
  3969. }
  3970. //---------------------------------------------------------------------------
  3971. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  3972. {
  3973. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  3974. {
  3975. FIgnoreNextDialogChar = Key;
  3976. }
  3977. Key = 0;
  3978. }
  3979. //---------------------------------------------------------------------------
  3980. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  3981. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  3982. {
  3983. const TCustomCommandType * Command = List->Find(KeyShortCut);
  3984. if (Command != NULL)
  3985. {
  3986. KeyProcessed(Key, Shift);
  3987. if (CustomCommandState(*Command, false, ccltAll) > 0)
  3988. {
  3989. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  3990. false, false, const_cast<TCustomCommandType *>(Command));
  3991. }
  3992. }
  3993. }
  3994. //---------------------------------------------------------------------------
  3995. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  3996. {
  3997. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  3998. {
  3999. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4000. }
  4001. if (!DirView(osCurrent)->IsEditing())
  4002. {
  4003. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4004. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4005. {
  4006. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4007. if (((Action->ShortCut == KeyShortCut) ||
  4008. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4009. AllowedAction(Action, aaShortCut))
  4010. {
  4011. // Has to be called before the action as the dialog char is already in queue.
  4012. // So when the action consumes message queue, we already need to have the
  4013. // FIgnoreNextDialogChar set
  4014. KeyProcessed(Key, Shift);
  4015. // Reset reference to previous component (when menu/toolbar was clicked).
  4016. // Needed to detect that action was invoked by keyboard shortcut
  4017. // in TNonVisualDataModule::ExplorerActionsExecute
  4018. Action->ActionComponent = NULL;
  4019. Action->Execute();
  4020. return;
  4021. }
  4022. }
  4023. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4024. {
  4025. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4026. {
  4027. KeyProcessed(Key, Shift);
  4028. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4029. return;
  4030. }
  4031. }
  4032. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4033. {
  4034. KeyProcessed(Key, Shift);
  4035. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4036. }
  4037. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4038. if (IsCustomShortCut(CustomShortCut))
  4039. {
  4040. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4041. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4042. if (WinConfiguration->SharedBookmarks != NULL)
  4043. {
  4044. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4045. if ((Bookmark != NULL) &&
  4046. OpenBookmark(FCurrentSide, Bookmark))
  4047. {
  4048. KeyProcessed(Key, Shift);
  4049. }
  4050. }
  4051. }
  4052. }
  4053. TForm::KeyDown(Key, Shift);
  4054. }
  4055. //---------------------------------------------------------------------------
  4056. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4057. {
  4058. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4059. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4060. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4061. DebugAssert(Terminal);
  4062. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4063. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4064. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4065. // expanded from ?: to avoid memory leaks
  4066. if (SecurityEnabled)
  4067. {
  4068. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4069. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4070. }
  4071. else
  4072. {
  4073. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4074. }
  4075. if (FileSystemInfo.ProtocolName.IsEmpty())
  4076. {
  4077. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4078. }
  4079. else
  4080. {
  4081. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4082. }
  4083. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4084. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4085. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4086. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4087. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4088. UnicodeString CompressionStr = CSCompressionStr;
  4089. if (CSCompressionStr != SCCompressionStr)
  4090. {
  4091. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4092. }
  4093. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4094. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4095. UpdateStatusBar();
  4096. }
  4097. //---------------------------------------------------------------------------
  4098. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4099. {
  4100. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4101. DebugAssert(SessionStatusBar != NULL);
  4102. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4103. {
  4104. // note: (Terminal->Status < sshReady) currently never happens here,
  4105. // so STATUS_CONNECTING is never used
  4106. SessionStatusBar->SimplePanel = true;
  4107. SessionStatusBar->SimpleText = LoadStr(
  4108. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4109. }
  4110. else
  4111. {
  4112. DebugAssert(Terminal);
  4113. SessionStatusBar->SimplePanel = false;
  4114. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4115. if (!FNote.IsEmpty())
  4116. {
  4117. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4118. }
  4119. else
  4120. {
  4121. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4122. }
  4123. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4124. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4125. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4126. }
  4127. }
  4128. //---------------------------------------------------------------------------
  4129. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4130. {
  4131. Panel->Caption = L"";
  4132. }
  4133. //---------------------------------------------------------------------------
  4134. void __fastcall TCustomScpExplorerForm::Idle()
  4135. {
  4136. if (FShowing || FStandaloneEditing)
  4137. {
  4138. FEditorManager->Check();
  4139. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4140. // that needs to know if queue view is visible (and it may be closed after queue update)
  4141. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4142. }
  4143. if (FShowing)
  4144. {
  4145. if (!NonVisualDataModule->Busy &&
  4146. // Menu is opened or drag&drop is going on
  4147. (Mouse->Capture == NULL))
  4148. {
  4149. if (FRefreshRemoteDirectory)
  4150. {
  4151. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4152. {
  4153. Terminal->RefreshDirectory();
  4154. }
  4155. FRefreshRemoteDirectory = false;
  4156. }
  4157. if (FRefreshLocalDirectory)
  4158. {
  4159. ReloadLocalDirectory();
  4160. FRefreshLocalDirectory = false;
  4161. }
  4162. if (WinConfiguration->RefreshRemotePanel)
  4163. {
  4164. TManagedTerminal * ManagedTerminal =
  4165. dynamic_cast<TManagedTerminal *>(Terminal);
  4166. if ((ManagedTerminal != NULL) && (Terminal->Status == ssOpened) &&
  4167. (Now() - ManagedTerminal->DirectoryLoaded >
  4168. WinConfiguration->RefreshRemotePanelInterval))
  4169. {
  4170. RemoteDirView->ReloadDirectory();
  4171. }
  4172. }
  4173. }
  4174. }
  4175. if (FShowing || FStandaloneEditing)
  4176. {
  4177. if (FQueueStatusInvalidated)
  4178. {
  4179. UpdateQueueStatus(false);
  4180. }
  4181. RefreshQueueItems();
  4182. }
  4183. if (FShowing)
  4184. {
  4185. UpdateStatusBar();
  4186. }
  4187. FIgnoreNextDialogChar = 0;
  4188. }
  4189. //---------------------------------------------------------------------------
  4190. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4191. {
  4192. try
  4193. {
  4194. FUserActionTimer->Enabled = false;
  4195. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4196. {
  4197. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4198. {
  4199. FPendingQueueActionItem->ProcessUserAction();
  4200. }
  4201. }
  4202. }
  4203. __finally
  4204. {
  4205. FPendingQueueActionItem = NULL;
  4206. }
  4207. }
  4208. //---------------------------------------------------------------------------
  4209. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4210. {
  4211. if (WinConfiguration->MinimizeToTray)
  4212. {
  4213. UpdateTrayIcon();
  4214. FTrayIcon->Visible = true;
  4215. if (Visible)
  4216. {
  4217. ShowWindow(Handle, SW_HIDE);
  4218. }
  4219. }
  4220. }
  4221. //---------------------------------------------------------------------------
  4222. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4223. {
  4224. // WORKAROUND
  4225. // When restoring maximized window from minimization,
  4226. // rarely some controls do not align properly.
  4227. // Two instances seen (both for Commander):
  4228. // - When restoring, window is temporarily narrower (not maximized),
  4229. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4230. // Once maximized already, top dock shinks back, but the session PageControl,
  4231. // do not align up, leaving space between TopDock and PageControl.
  4232. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4233. for (int Index = 0; Index < ControlCount; Index++)
  4234. {
  4235. RealignControl(Controls[Index]);
  4236. }
  4237. if (FTrayIcon->Visible)
  4238. {
  4239. FTrayIcon->Visible = false;
  4240. if (Visible)
  4241. {
  4242. ShowWindow(Handle, SW_SHOW);
  4243. }
  4244. }
  4245. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4246. {
  4247. FNeedSession = false;
  4248. NonVisualDataModule->StartBusy();
  4249. try
  4250. {
  4251. NeedSession(false);
  4252. }
  4253. __finally
  4254. {
  4255. NonVisualDataModule->EndBusy();
  4256. }
  4257. }
  4258. }
  4259. //---------------------------------------------------------------------------
  4260. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4261. {
  4262. FTrayIcon->Hint = GetMainForm()->Caption;
  4263. }
  4264. //---------------------------------------------------------------------------
  4265. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4266. {
  4267. UpdateTrayIcon();
  4268. }
  4269. //---------------------------------------------------------------------------
  4270. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4271. {
  4272. // workaround
  4273. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4274. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4275. // But WM_ACTIVATEAPP message can be received even when
  4276. // the main window is minimized to the tray and internal editor window is focused
  4277. // (after another application was previously active)
  4278. if (::IsIconic(Handle))
  4279. {
  4280. if (!IsAppIconic())
  4281. {
  4282. SetAppIconic(true);
  4283. }
  4284. }
  4285. ::ApplicationRestore();
  4286. Application->BringToFront();
  4287. }
  4288. //---------------------------------------------------------------------------
  4289. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4290. {
  4291. RestoreApp();
  4292. }
  4293. //---------------------------------------------------------------------------
  4294. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4295. {
  4296. if (OpenInNewWindow())
  4297. {
  4298. // todo: Pass FromSite
  4299. ExecuteNewInstance(SessionUrl);
  4300. }
  4301. else
  4302. {
  4303. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4304. }
  4305. }
  4306. //---------------------------------------------------------------------------
  4307. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4308. {
  4309. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4310. // current working directories become defaults here, what is not right
  4311. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4312. StoredSessions->NewSession(SessionName, SessionData.get());
  4313. // modified only, explicit
  4314. StoredSessions->Save(false, true);
  4315. // encode session name because of slashes in hierarchical sessions
  4316. return EncodeUrlString(SessionName);
  4317. }
  4318. //---------------------------------------------------------------------------
  4319. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4320. {
  4321. if (OpenInNewWindow())
  4322. {
  4323. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4324. }
  4325. else
  4326. {
  4327. // current working directories become defaults here, what is not right
  4328. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4329. TTerminalManager * Manager = TTerminalManager::Instance();
  4330. TTerminal * Terminal = Manager->NewTerminal(SessionData.get());
  4331. Manager->ActiveTerminal = Terminal;
  4332. }
  4333. }
  4334. //---------------------------------------------------------------------------
  4335. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4336. {
  4337. DebugAssert(FQueue != NULL);
  4338. bool Result = FQueue->IsEmpty;
  4339. if (!Result)
  4340. {
  4341. SetFocus();
  4342. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4343. }
  4344. return Result;
  4345. }
  4346. //---------------------------------------------------------------------------
  4347. void __fastcall TCustomScpExplorerForm::CloseSession()
  4348. {
  4349. if (CanCloseQueue())
  4350. {
  4351. TTerminalManager::Instance()->FreeActiveTerminal();
  4352. }
  4353. }
  4354. //---------------------------------------------------------------------------
  4355. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4356. {
  4357. if (OpenInNewWindow())
  4358. {
  4359. // encode session name because of slashes in hierarchical sessions
  4360. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4361. }
  4362. else
  4363. {
  4364. TTerminalManager * Manager = TTerminalManager::Instance();
  4365. TTerminal * Terminal = Manager->NewTerminal(Data);
  4366. Manager->ActiveTerminal = Terminal;
  4367. }
  4368. }
  4369. //---------------------------------------------------------------------------
  4370. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4371. {
  4372. if (OpenInNewWindow())
  4373. {
  4374. ExecuteNewInstance(Name);
  4375. }
  4376. else
  4377. {
  4378. TTerminalManager * Manager = TTerminalManager::Instance();
  4379. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4380. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4381. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  4382. }
  4383. }
  4384. //---------------------------------------------------------------------------
  4385. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4386. bool &CanClose)
  4387. {
  4388. if (Terminal != NULL)
  4389. {
  4390. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4391. {
  4392. unsigned int Result;
  4393. TMessageParams Params(mpNeverAskAgainCheck);
  4394. UnicodeString Message;
  4395. int Answers = qaOK | qaCancel;
  4396. if (TTerminalManager::Instance()->Count > 1)
  4397. {
  4398. if (!WinConfiguration->AutoSaveWorkspace)
  4399. {
  4400. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE2);
  4401. Answers = qaYes | qaNo | qaCancel;
  4402. }
  4403. else
  4404. {
  4405. Message = MainInstructions(LoadStr(CLOSE_SESSIONS));
  4406. }
  4407. }
  4408. else
  4409. {
  4410. Message = MainInstructions(FMTLOAD(CLOSE_SESSION, (Terminal->SessionData->SessionName)));
  4411. }
  4412. if (WinConfiguration->AutoSaveWorkspace)
  4413. {
  4414. Message =
  4415. FORMAT("%s\n\n%s", (Message,
  4416. FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()))));
  4417. }
  4418. SetFocus();
  4419. Result = MessageDialog(Message, qtConfirmation,
  4420. Answers, HELP_NONE, &Params);
  4421. if (Result == qaNeverAskAgain)
  4422. {
  4423. WinConfiguration->ConfirmClosingSession = false;
  4424. }
  4425. if (Result == qaNo)
  4426. {
  4427. CanClose = SaveWorkspace(true);
  4428. // note that the workspace will be saved redundatly again from FormClose
  4429. }
  4430. else
  4431. {
  4432. CanClose =
  4433. (Result == qaOK) ||
  4434. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4435. (Result == qaNeverAskAgain);
  4436. }
  4437. }
  4438. if (CanClose)
  4439. {
  4440. CanClose = CanCloseQueue();
  4441. }
  4442. }
  4443. if (CanClose)
  4444. {
  4445. CanClose =
  4446. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4447. FEditorManager->CloseExternalFilesWithoutProcess();
  4448. if (CanClose)
  4449. {
  4450. while (CanClose && (FLocalEditors->Count > 0))
  4451. {
  4452. int PrevCount = FLocalEditors->Count;
  4453. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4454. CanClose = (FLocalEditors->Count < PrevCount);
  4455. }
  4456. if (CanClose)
  4457. {
  4458. CanClose = FEditorManager->Empty(true);
  4459. if (!CanClose)
  4460. {
  4461. SetFocus();
  4462. CanClose =
  4463. (MessageDialog(
  4464. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4465. }
  4466. }
  4467. }
  4468. }
  4469. }
  4470. //---------------------------------------------------------------------------
  4471. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4472. {
  4473. TForm * Form = dynamic_cast<TForm *>(Sender);
  4474. DebugAssert(Form != NULL);
  4475. Form->Close();
  4476. }
  4477. //---------------------------------------------------------------------------
  4478. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4479. {
  4480. TForm * Form = dynamic_cast<TForm *>(Sender);
  4481. delete Form;
  4482. }
  4483. //---------------------------------------------------------------------------
  4484. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4485. {
  4486. while (FLocalEditors->Count > 0)
  4487. {
  4488. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4489. }
  4490. }
  4491. //---------------------------------------------------------------------------
  4492. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4493. TObject *Sender)
  4494. {
  4495. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4496. try
  4497. {
  4498. SetProperties(osRemote, FileList);
  4499. }
  4500. __finally
  4501. {
  4502. delete FileList;
  4503. }
  4504. }
  4505. //---------------------------------------------------------------------------
  4506. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4507. {
  4508. if (value)
  4509. {
  4510. if (Component == fcCustomCommandsBand)
  4511. {
  4512. UpdateCustomCommandsToolbar();
  4513. }
  4514. }
  4515. }
  4516. //---------------------------------------------------------------------------
  4517. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4518. {
  4519. TControl * Control = GetComponent(Component);
  4520. DebugAssert(Control);
  4521. bool Changed = (Control->Visible != value);
  4522. if (Changed)
  4523. {
  4524. ComponentShowing(Component, value);
  4525. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4526. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4527. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4528. if (value)
  4529. {
  4530. int RemainingHeight = Control->Parent->ClientHeight;
  4531. int RemainingWidth = Control->Parent->ClientWidth;
  4532. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4533. {
  4534. TControl * ChildControl = Control->Parent->Controls[i];
  4535. if (ChildControl->Visible)
  4536. {
  4537. switch (ChildControl->Align)
  4538. {
  4539. case alTop:
  4540. case alBottom:
  4541. RemainingHeight -= ChildControl->Height;
  4542. break;
  4543. case alLeft:
  4544. case alRight:
  4545. RemainingWidth -= ChildControl->Width;
  4546. break;
  4547. }
  4548. }
  4549. }
  4550. int Reserve = ScaleByTextHeight(this, 32);
  4551. // queue in explorer, trees in commander
  4552. if (Control->Height > RemainingHeight - Reserve)
  4553. {
  4554. Control->Height = RemainingHeight / 2;
  4555. }
  4556. if (Control->Width > RemainingWidth - Reserve)
  4557. {
  4558. Control->Width = RemainingWidth / 2;
  4559. }
  4560. }
  4561. Control->Visible = value;
  4562. if (WasFocused && Visible)
  4563. {
  4564. DirView(osCurrent)->SetFocus();
  4565. }
  4566. FixControlsPlacement();
  4567. }
  4568. }
  4569. //---------------------------------------------------------------------------
  4570. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4571. {
  4572. TControl * Control = GetComponent(Component);
  4573. if (Control == NULL)
  4574. {
  4575. return false;
  4576. }
  4577. else
  4578. {
  4579. return Control->Visible;
  4580. }
  4581. }
  4582. //---------------------------------------------------------------------------
  4583. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4584. {
  4585. bool Result;
  4586. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4587. {
  4588. Result = !IsUWP();
  4589. }
  4590. else
  4591. {
  4592. Result = true;
  4593. }
  4594. return Result;
  4595. }
  4596. //---------------------------------------------------------------------------
  4597. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4598. {
  4599. if (RemoteDirView->ItemFocused != NULL)
  4600. {
  4601. RemoteDirView->ItemFocused->MakeVisible(false);
  4602. }
  4603. QueueSplitter->Visible = QueuePanel->Visible;
  4604. RemotePanelSplitter->Visible = RemoteDriveView->Visible;
  4605. }
  4606. //---------------------------------------------------------------------------
  4607. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4608. {
  4609. switch (Component) {
  4610. case fcStatusBar: return RemoteStatusBar;
  4611. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4612. case fcQueueView: return QueuePanel;
  4613. case fcQueueToolbar: return QueueDock;
  4614. case fcRemoteTree: return RemoteDriveView;
  4615. case fcSessionsTabs: return SessionsPageControl;
  4616. default: return NULL;
  4617. }
  4618. }
  4619. //---------------------------------------------------------------------------
  4620. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4621. TObject *Sender, TListColumn *Column, TPoint &Point)
  4622. {
  4623. DebugAssert(NonVisualDataModule && Column && Sender);
  4624. NonVisualDataModule->ListColumn = Column;
  4625. TPopupMenu * DirViewColumnMenu;
  4626. if (Sender == RemoteDirView)
  4627. {
  4628. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4629. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4630. (Column->Index == uvName);
  4631. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4632. (Column->Index == uvSize);
  4633. }
  4634. else
  4635. {
  4636. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4637. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4638. (Column->Index == dvName);
  4639. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4640. (Column->Index == dvSize);
  4641. }
  4642. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4643. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4644. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4645. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4646. }
  4647. //---------------------------------------------------------------------------
  4648. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4649. TObject *Sender, TListItem *Item, bool &AllowExec)
  4650. {
  4651. DoDirViewExecFile(Sender, Item, AllowExec);
  4652. }
  4653. //---------------------------------------------------------------------------
  4654. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4655. TListItem * Item, bool & AllowExec)
  4656. {
  4657. DebugAssert(Sender && Item && Configuration);
  4658. DebugAssert(AllowExec);
  4659. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4660. bool Remote = (ADirView == DirView(osRemote));
  4661. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4662. TOperationSide Side = (Remote ? osRemote : osLocal);
  4663. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4664. // on files only (not directories)
  4665. // and only when symlinks are resolved (apply to remote panel only)
  4666. if (!ADirView->ItemIsDirectory(Item) &&
  4667. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4668. {
  4669. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4670. !FForceExecution &&
  4671. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4672. {
  4673. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4674. {
  4675. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4676. UnlockWindow();
  4677. try
  4678. {
  4679. ExecuteFileOperation(
  4680. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4681. }
  4682. __finally
  4683. {
  4684. LockWindow();
  4685. }
  4686. AllowExec = false;
  4687. }
  4688. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4689. {
  4690. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4691. {
  4692. ExecuteFile(Side, efDefaultEditor);
  4693. AllowExec = false;
  4694. }
  4695. }
  4696. else
  4697. {
  4698. DebugFail();
  4699. }
  4700. }
  4701. // if we have not done anything special, fall back to default behavior
  4702. if (AllowExec)
  4703. {
  4704. if (Remote && !WinConfiguration->DisableOpenEdit)
  4705. {
  4706. ExecuteFile(osRemote, efShell);
  4707. AllowExec = false;
  4708. }
  4709. }
  4710. }
  4711. }
  4712. //---------------------------------------------------------------------------
  4713. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4714. {
  4715. return ((Side == osRemote) || (Side == osCurrent));
  4716. }
  4717. //---------------------------------------------------------------------------
  4718. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4719. {
  4720. DebugFail();
  4721. }
  4722. //---------------------------------------------------------------------------
  4723. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4724. {
  4725. DebugFail();
  4726. }
  4727. //---------------------------------------------------------------------------
  4728. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4729. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, bool UseDefaults)
  4730. {
  4731. TSynchronizeParamType Params;
  4732. Params.LocalDirectory = LocalDirectory;
  4733. Params.RemoteDirectory = RemoteDirectory;
  4734. int UnusedParams =
  4735. (GUIConfiguration->SynchronizeParams &
  4736. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4737. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4738. Params.Options = GUIConfiguration->SynchronizeOptions;
  4739. bool SaveSettings = false;
  4740. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4741. &DoSynchronizeTooManyDirectories);
  4742. DebugAssert(FSynchronizeController == NULL);
  4743. FSynchronizeController = &Controller;
  4744. bool Result;
  4745. try
  4746. {
  4747. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4748. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4749. int Options =
  4750. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4751. DebugAssert(FOnFeedSynchronizeError == NULL);
  4752. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4753. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4754. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4755. if (Result)
  4756. {
  4757. if (SaveSettings)
  4758. {
  4759. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4760. GUIConfiguration->SynchronizeOptions = Params.Options;
  4761. }
  4762. else
  4763. {
  4764. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4765. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4766. FLAGSET(Params.Options, soSynchronize))
  4767. {
  4768. GUIConfiguration->SynchronizeOptions =
  4769. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4770. soSynchronize;
  4771. }
  4772. }
  4773. LocalDirectory = Params.LocalDirectory;
  4774. RemoteDirectory = Params.RemoteDirectory;
  4775. }
  4776. }
  4777. __finally
  4778. {
  4779. FSynchronizeController = NULL;
  4780. DebugAssert(FOnFeedSynchronizeError == NULL);
  4781. FOnFeedSynchronizeError = NULL;
  4782. }
  4783. return Result;
  4784. }
  4785. //---------------------------------------------------------------------------
  4786. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4787. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4788. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4789. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4790. TSynchronizeOptions * Options, bool Full)
  4791. {
  4792. // Keep-up-to-date mode
  4793. if (Terminal->Status == ssOpened)
  4794. {
  4795. try
  4796. {
  4797. int PParams = Params.Params;
  4798. if (!Full)
  4799. {
  4800. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4801. if (FLAGSET(Params.Options, soRecurse))
  4802. {
  4803. PParams |= TTerminal::spSubDirs;
  4804. }
  4805. }
  4806. else
  4807. {
  4808. // if keepuptodate is non-recursive,
  4809. // full sync before has to be non-recursive as well
  4810. if (FLAGCLEAR(Params.Options, soRecurse))
  4811. {
  4812. PParams |= TTerminal::spNoRecurse;
  4813. }
  4814. }
  4815. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  4816. PParams, Checklist, Options);
  4817. }
  4818. catch (Exception & E)
  4819. {
  4820. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  4821. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  4822. {
  4823. // noop, already logged in MoreMessageDialog
  4824. }
  4825. else
  4826. {
  4827. ShowExtendedExceptionEx(Terminal, &E);
  4828. throw;
  4829. }
  4830. }
  4831. }
  4832. }
  4833. //---------------------------------------------------------------------------
  4834. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  4835. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  4836. const UnicodeString ErrorStr)
  4837. {
  4838. if (!Directory.IsEmpty())
  4839. {
  4840. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  4841. }
  4842. else
  4843. {
  4844. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  4845. }
  4846. }
  4847. //---------------------------------------------------------------------------
  4848. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  4849. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  4850. {
  4851. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  4852. {
  4853. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  4854. }
  4855. else
  4856. {
  4857. TMessageParams Params(mpNeverAskAgainCheck);
  4858. unsigned int Result = MessageDialog(
  4859. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  4860. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  4861. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  4862. {
  4863. MaxDirectories *= 2;
  4864. if (Result == qaNeverAskAgain)
  4865. {
  4866. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  4867. }
  4868. }
  4869. else
  4870. {
  4871. Abort();
  4872. }
  4873. }
  4874. }
  4875. //---------------------------------------------------------------------------
  4876. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  4877. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  4878. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  4879. TSynchronizeOptions * Options)
  4880. {
  4881. // Keep-up-to-date mode
  4882. DebugAssert(!FAutoOperation);
  4883. void * BatchStorage;
  4884. BatchStart(BatchStorage);
  4885. FAutoOperation = true;
  4886. bool AnyOperation = false;
  4887. TDateTime StartTime = Now();
  4888. TSynchronizeChecklist * AChecklist = NULL;
  4889. try
  4890. {
  4891. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  4892. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  4893. {
  4894. FSynchronizeProgressForm->Start();
  4895. }
  4896. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  4897. static_cast<TTerminal::TSynchronizeMode>(Mode),
  4898. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4899. Options);
  4900. SAFE_DESTROY(FSynchronizeProgressForm);
  4901. AnyOperation = (AChecklist->CheckedCount > 0);
  4902. if (AnyOperation)
  4903. {
  4904. TSynchronizeProgress SynchronizeProgress(AChecklist);
  4905. CreateProgressForm(&SynchronizeProgress);
  4906. try
  4907. {
  4908. Terminal->SynchronizeApply(
  4909. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4910. SynchronizeProcessedItem, NULL, NULL);
  4911. }
  4912. __finally
  4913. {
  4914. DestroyProgressForm();
  4915. }
  4916. }
  4917. }
  4918. __finally
  4919. {
  4920. if (Checklist == NULL)
  4921. {
  4922. delete AChecklist;
  4923. }
  4924. else
  4925. {
  4926. *Checklist = AChecklist;
  4927. }
  4928. FAutoOperation = false;
  4929. SAFE_DESTROY(FSynchronizeProgressForm);
  4930. BatchEnd(BatchStorage);
  4931. ReloadLocalDirectory();
  4932. if (AnyOperation)
  4933. {
  4934. OperationComplete(StartTime);
  4935. }
  4936. }
  4937. }
  4938. //---------------------------------------------------------------------------
  4939. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  4940. {
  4941. // can be called from command line
  4942. return Visible &&
  4943. ((DirView(osRemote)->SelCount > 0) ||
  4944. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  4945. }
  4946. //---------------------------------------------------------------------------
  4947. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  4948. {
  4949. LogSynchronizeEvent(Terminal, Message);
  4950. }
  4951. //---------------------------------------------------------------------------
  4952. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  4953. int Params, TSynchronizeOptions & Options)
  4954. {
  4955. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  4956. {
  4957. Options.Filter = new TStringList();
  4958. Options.Filter->CaseSensitive = false;
  4959. Options.Filter->Duplicates = Types::dupAccept;
  4960. if (DirView(osRemote)->SelCount > 0)
  4961. {
  4962. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  4963. }
  4964. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  4965. {
  4966. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  4967. }
  4968. Options.Filter->Sort();
  4969. }
  4970. }
  4971. //---------------------------------------------------------------------------
  4972. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  4973. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  4974. {
  4975. UnicodeString SessionName = CreateHiddenDuplicateSession();
  4976. UnicodeString AdditionalParams =
  4977. FORMAT(L"%s \"%s\" \"%s\" %d %d %s", (
  4978. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  4979. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  4980. TProgramParams::FormatSwitch(DEFAULTS_SWITCH)));
  4981. TCopyParamType Defaults;
  4982. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  4983. ConfigStorage->AccessMode = smRead;
  4984. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false, false))
  4985. {
  4986. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  4987. }
  4988. ConfigStorage.reset(NULL);
  4989. std::unique_ptr<TStringList> Options(new TStringList());
  4990. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  4991. GUIConfiguration->SaveCopyParam(OptionsStorage.get(), CopyParams, &Defaults);
  4992. if (Options->Count > 0)
  4993. {
  4994. AdditionalParams +=
  4995. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAW_CONFIG_SWITCH), StringsToParams(Options.get())));
  4996. }
  4997. ExecuteNewInstance(SessionName, AdditionalParams);
  4998. }
  4999. //---------------------------------------------------------------------------
  5000. struct TSynchronizeParams
  5001. {
  5002. UnicodeString LocalDirectory;
  5003. UnicodeString RemoteDirectory;
  5004. TSynchronizeMode Mode;
  5005. TSynchronizeChecklist * Checklist;
  5006. int Params;
  5007. TCopyParamType * CopyParam;
  5008. TDateTime * StartTime;
  5009. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5010. };
  5011. //---------------------------------------------------------------------------
  5012. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5013. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5014. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5015. {
  5016. DebugAssert(!FAutoOperation);
  5017. void * BatchStorage;
  5018. BatchStart(BatchStorage);
  5019. FAutoOperation = true;
  5020. try
  5021. {
  5022. Params.OnProcessedItem = OnProcessedItem;
  5023. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5024. CreateProgressForm(&SynchronizeProgress);
  5025. Terminal->SynchronizeApply(
  5026. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5027. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params);
  5028. }
  5029. __finally
  5030. {
  5031. Params.OnProcessedItem = NULL;
  5032. FAutoOperation = false;
  5033. DestroyProgressForm();
  5034. BatchEnd(BatchStorage);
  5035. ReloadLocalDirectory();
  5036. }
  5037. }
  5038. //---------------------------------------------------------------------------
  5039. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5040. {
  5041. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5042. {
  5043. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5044. }
  5045. // Not set in keep-up-to-date mode - Synchronize() method
  5046. if (Token != NULL)
  5047. {
  5048. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5049. if (Params.OnProcessedItem != NULL)
  5050. {
  5051. Params.OnProcessedItem(NULL, ChecklistItem);
  5052. }
  5053. }
  5054. }
  5055. //---------------------------------------------------------------------------
  5056. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5057. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5058. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5059. {
  5060. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5061. *Params.StartTime = Now();
  5062. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5063. }
  5064. //---------------------------------------------------------------------------
  5065. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5066. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5067. {
  5068. // terminal can be already closed (e.g. dropped connection)
  5069. if (Terminal != NULL)
  5070. {
  5071. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5072. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5073. }
  5074. }
  5075. //---------------------------------------------------------------------------
  5076. void __fastcall TCustomScpExplorerForm::SynchronizeNoDifferences()
  5077. {
  5078. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5079. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5080. }
  5081. //---------------------------------------------------------------------------
  5082. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5083. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5084. TSynchronizeMode & Mode, bool & SaveMode, bool UseDefaults)
  5085. {
  5086. int Result;
  5087. int Params = GUIConfiguration->SynchronizeParams;
  5088. bool SaveSettings = false;
  5089. int Options =
  5090. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5091. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5092. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5093. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5094. bool Continue =
  5095. UseDefaults ||
  5096. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5097. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs);
  5098. if (Continue)
  5099. {
  5100. Configuration->Usage->Inc(L"Synchronizations");
  5101. UpdateCopyParamCounters(CopyParam);
  5102. TSynchronizeOptions SynchronizeOptions;
  5103. GetSynchronizeOptions(Params, SynchronizeOptions);
  5104. if (SaveSettings)
  5105. {
  5106. GUIConfiguration->SynchronizeParams = Params;
  5107. }
  5108. else
  5109. {
  5110. SaveMode = false;
  5111. }
  5112. TDateTime StartTime = Now();
  5113. TSynchronizeChecklist * Checklist = NULL;
  5114. try
  5115. {
  5116. DebugAssert(!FAutoOperation);
  5117. FAutoOperation = true;
  5118. try
  5119. {
  5120. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  5121. FSynchronizeProgressForm->Start();
  5122. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5123. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5124. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5125. &SynchronizeOptions);
  5126. }
  5127. __finally
  5128. {
  5129. FAutoOperation = false;
  5130. SAFE_DESTROY(FSynchronizeProgressForm);
  5131. }
  5132. TSynchronizeParams SynchronizeParams;
  5133. SynchronizeParams.LocalDirectory = LocalDirectory;
  5134. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5135. SynchronizeParams.Mode = Mode;
  5136. SynchronizeParams.CopyParam = &CopyParam;
  5137. SynchronizeParams.Params = Params;
  5138. SynchronizeParams.Checklist = Checklist;
  5139. SynchronizeParams.StartTime = &StartTime;
  5140. SynchronizeParams.OnProcessedItem = NULL;
  5141. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5142. {
  5143. if (Checklist->Count > 0)
  5144. {
  5145. if (DoSynchronizeChecklistDialog(
  5146. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5147. DoSynchronizeChecklistCalculateSize, &SynchronizeParams))
  5148. {
  5149. Result = Checklist->Count;
  5150. }
  5151. else
  5152. {
  5153. Result = -1;
  5154. }
  5155. }
  5156. else
  5157. {
  5158. SynchronizeNoDifferences();
  5159. }
  5160. }
  5161. else
  5162. {
  5163. Result = Checklist->CheckedCount;
  5164. if (Checklist->CheckedCount > 0)
  5165. {
  5166. FullSynchronize(SynchronizeParams, NULL, NULL);
  5167. }
  5168. else
  5169. {
  5170. SynchronizeNoDifferences();
  5171. }
  5172. }
  5173. }
  5174. __finally
  5175. {
  5176. delete Checklist;
  5177. }
  5178. OperationComplete(StartTime);
  5179. }
  5180. else
  5181. {
  5182. Result = -1;
  5183. }
  5184. return Result;
  5185. }
  5186. //---------------------------------------------------------------------------
  5187. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5188. const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
  5189. bool & Continue, bool Collect)
  5190. {
  5191. if (Collect)
  5192. {
  5193. DebugAssert(FSynchronizeProgressForm != NULL);
  5194. if (!FSynchronizeProgressForm->Started)
  5195. {
  5196. FSynchronizeProgressForm->Start();
  5197. }
  5198. FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Continue);
  5199. }
  5200. else
  5201. {
  5202. DebugAssert(FSynchronizeProgressForm == NULL);
  5203. DebugAssert(FProgressForm != NULL);
  5204. }
  5205. }
  5206. //---------------------------------------------------------------------------
  5207. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5208. {
  5209. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5210. TRemoteFile * File = NULL;
  5211. Terminal->ReadFile(FullFileName, File);
  5212. if (File != NULL)
  5213. {
  5214. std::unique_ptr<TRemoteFile> FileOwner(File);
  5215. TAutoFlag Flag(FStandaloneEditing);
  5216. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5217. Application->ShowMainForm = false;
  5218. Application->Run();
  5219. }
  5220. }
  5221. //---------------------------------------------------------------------------
  5222. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5223. {
  5224. DebugFail();
  5225. }
  5226. //---------------------------------------------------------------------------
  5227. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5228. {
  5229. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5230. SessionData->Assign(Terminal->SessionData);
  5231. UpdateSessionData(SessionData.get());
  5232. TTerminalManager::Instance()->UpdateSessionCredentials(SessionData.get());
  5233. if (Terminal->SessionData->IsWorkspace)
  5234. {
  5235. // Have to reset the "Workspace/XXX" name which would become user-visible
  5236. // once IsWorkspace is cleared
  5237. SessionData->Name = UnicodeString();
  5238. SessionData->IsWorkspace = false;
  5239. }
  5240. return SessionData.release();
  5241. }
  5242. //---------------------------------------------------------------------------
  5243. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5244. {
  5245. TSessionData * SessionData = CloneCurrentSessionData();
  5246. try
  5247. {
  5248. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5249. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5250. }
  5251. __finally
  5252. {
  5253. delete SessionData;
  5254. }
  5255. }
  5256. //---------------------------------------------------------------------------
  5257. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5258. {
  5259. TTerminalManager * Manager = TTerminalManager::Instance();
  5260. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5261. if (DebugAlwaysTrue(Terminal != NULL))
  5262. {
  5263. // Update (Managed)Terminal->StateData
  5264. UpdateTerminal(Terminal);
  5265. }
  5266. Manager->SaveWorkspace(DataList.get());
  5267. return DataList.release();
  5268. }
  5269. //---------------------------------------------------------------------------
  5270. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5271. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5272. {
  5273. WinConfiguration->LastWorkspace = Name;
  5274. if (!SavePasswords)
  5275. {
  5276. for (int Index = 0; Index < DataList->Count; Index++)
  5277. {
  5278. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5279. if (SessionData->Link.IsEmpty())
  5280. {
  5281. SessionData->ClearSessionPasswords();
  5282. }
  5283. }
  5284. }
  5285. StoredSessions->NewWorkspace(Name, DataList);
  5286. // modified only
  5287. StoredSessions->Save(false, Explicit);
  5288. }
  5289. //---------------------------------------------------------------------------
  5290. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5291. {
  5292. return
  5293. DefaultStr(WinConfiguration->LastWorkspace,
  5294. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5295. }
  5296. //---------------------------------------------------------------------------
  5297. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5298. {
  5299. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5300. bool AnyNonStoredSessionWithPassword = false;
  5301. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5302. bool AllNonStoredSessionsAnonymous = true;
  5303. TTerminalManager * Manager = TTerminalManager::Instance();
  5304. for (int Index = 0; Index < DataList->Count; Index++)
  5305. {
  5306. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5307. if (SessionData->Link.IsEmpty())
  5308. {
  5309. if (SessionData->HasAnySessionPassword())
  5310. {
  5311. AnyNonStoredSessionWithPassword = true;
  5312. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5313. {
  5314. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5315. }
  5316. }
  5317. if (!SameText(SessionData->UserName, AnonymousUserName))
  5318. {
  5319. AllNonStoredSessionsAnonymous = false;
  5320. }
  5321. }
  5322. }
  5323. bool SavePasswords;
  5324. bool * PSavePasswords;
  5325. bool NotRecommendedSavingPasswords =
  5326. !CustomWinConfiguration->UseMasterPassword &&
  5327. !AllNonStoredSessionsAnonymous;
  5328. if (Configuration->DisablePasswordStoring ||
  5329. !AnyNonStoredSessionWithPassword)
  5330. {
  5331. PSavePasswords = NULL;
  5332. SavePasswords = false;
  5333. }
  5334. else
  5335. {
  5336. PSavePasswords = &SavePasswords;
  5337. SavePasswords =
  5338. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5339. !NotRecommendedSavingPasswords;
  5340. }
  5341. UnicodeString Name = WorkspaceName();
  5342. bool CreateShortcut = false;
  5343. bool Result =
  5344. DoSaveWorkspaceDialog(
  5345. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5346. EnableAutoSave);
  5347. if (Result)
  5348. {
  5349. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5350. if (CreateShortcut)
  5351. {
  5352. TOperationVisualizer Visualizer;
  5353. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5354. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5355. }
  5356. if (EnableAutoSave)
  5357. {
  5358. WinConfiguration->AutoSaveWorkspace = true;
  5359. WinConfiguration->AutoWorkspace = Name;
  5360. if (PSavePasswords != NULL)
  5361. {
  5362. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5363. }
  5364. }
  5365. }
  5366. return Result;
  5367. }
  5368. //---------------------------------------------------------------------------
  5369. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TTerminal * Terminal)
  5370. {
  5371. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  5372. DebugAssert(ManagedTerminal != NULL);
  5373. SAFE_DESTROY(ManagedTerminal->RemoteExplorerState);
  5374. if (WinConfiguration->PreservePanelState)
  5375. {
  5376. ManagedTerminal->RemoteExplorerState = RemoteDirView->SaveState();
  5377. }
  5378. UpdateSessionData(ManagedTerminal->StateData);
  5379. }
  5380. //---------------------------------------------------------------------------
  5381. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5382. {
  5383. // Keep in sync with TSessionData::CopyStateData
  5384. DebugAssert(Data != NULL);
  5385. // cannot use RemoteDirView->Path, because it is empty if connection
  5386. // was already closed
  5387. // also only peek, we may not be connected at all atm
  5388. // so make sure we do not try retrieving current directory from the server
  5389. // (particularly with FTP)
  5390. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5391. if (!ACurrentDirectory.IsEmpty())
  5392. {
  5393. Data->RemoteDirectory = ACurrentDirectory;
  5394. }
  5395. Data->Color = SessionColor;
  5396. }
  5397. //---------------------------------------------------------------------------
  5398. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5399. {
  5400. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5401. DebugAssert(Toolbar != NULL);
  5402. for (int i = 0; i < Toolbar->Items->Count; i++)
  5403. {
  5404. TTBXCustomDropDownItem * DropDownItem;
  5405. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5406. if (DropDownItem != NULL)
  5407. {
  5408. ToolbarItemResize(DropDownItem,
  5409. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5410. Toolbar->NonClientWidth);
  5411. break;
  5412. }
  5413. }
  5414. }
  5415. //---------------------------------------------------------------------------
  5416. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5417. {
  5418. Item->EditWidth = Width;
  5419. }
  5420. //---------------------------------------------------------------------------
  5421. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5422. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5423. {
  5424. for (int i = 0; i < Toolbar->Items->Count; i++)
  5425. {
  5426. TTBXCustomDropDownItem * DropDownItem;
  5427. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5428. if (DropDownItem != NULL)
  5429. {
  5430. ASize.x -= DropDownItem->EditWidth;
  5431. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5432. }
  5433. }
  5434. }
  5435. //---------------------------------------------------------------------------
  5436. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5437. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5438. {
  5439. if (WinConfiguration->DDWarnLackOfTempSpace)
  5440. {
  5441. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5442. if (!ADrive.IsEmpty())
  5443. {
  5444. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5445. DebugAssert(RequiredSpace >= 0);
  5446. __int64 RequiredWithReserve;
  5447. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5448. if (FreeSpace < RequiredWithReserve)
  5449. {
  5450. unsigned int Result;
  5451. TMessageParams Params(mpNeverAskAgainCheck);
  5452. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5453. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5454. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5455. if (Result == qaNeverAskAgain)
  5456. {
  5457. WinConfiguration->DDWarnLackOfTempSpace = false;
  5458. }
  5459. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5460. }
  5461. }
  5462. }
  5463. }
  5464. //---------------------------------------------------------------------------
  5465. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5466. {
  5467. DoOpenDirectoryDialog(odAddBookmark, Side);
  5468. }
  5469. //---------------------------------------------------------------------------
  5470. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5471. {
  5472. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5473. TStringList * VisitedDirectories = CreateSortedStringList((Side == osRemote));
  5474. try
  5475. {
  5476. TCustomDirView * DView = DirView(Side);
  5477. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5478. {
  5479. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5480. }
  5481. }
  5482. catch (...)
  5483. {
  5484. delete VisitedDirectories;
  5485. throw;
  5486. }
  5487. return VisitedDirectories;
  5488. }
  5489. //---------------------------------------------------------------------------
  5490. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5491. TOpenDirectoryMode Mode, TOperationSide Side)
  5492. {
  5493. bool Continue = true;
  5494. if (Mode == odAddBookmark)
  5495. {
  5496. TMessageParams Params(mpNeverAskAgainCheck);
  5497. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5498. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5499. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5500. unsigned int Answer =
  5501. MessageDialog(Message,
  5502. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5503. if (Answer == qaNeverAskAgain)
  5504. {
  5505. Continue = true;
  5506. WinConfiguration->UseSharedBookmarks = true;
  5507. }
  5508. else if (Answer == qaOK)
  5509. {
  5510. Continue = true;
  5511. WinConfiguration->UseSharedBookmarks = false;
  5512. }
  5513. else
  5514. {
  5515. Continue = false;
  5516. }
  5517. }
  5518. if (Continue)
  5519. {
  5520. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5521. try
  5522. {
  5523. UnicodeString Name = DirView(Side)->PathName;
  5524. bool Repeat;
  5525. do
  5526. {
  5527. Repeat = false;
  5528. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5529. // do not allow switching to location profiles,
  5530. // if we are not connected
  5531. HasDirView[osLocal] && (Terminal != NULL)))
  5532. {
  5533. TWindowLock Lock(this);
  5534. if (!TryOpenDirectory(Side, Name))
  5535. {
  5536. Repeat = true;
  5537. Mode = odBrowse;
  5538. }
  5539. }
  5540. }
  5541. while (Repeat);
  5542. }
  5543. __finally
  5544. {
  5545. delete VisitedDirectories;
  5546. }
  5547. }
  5548. }
  5549. //---------------------------------------------------------------------------
  5550. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5551. {
  5552. DebugAssert(!FTerminal->CommandSessionOpened);
  5553. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5554. }
  5555. //---------------------------------------------------------------------------
  5556. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5557. {
  5558. bool Result = FTerminal->IsCapable[Capability] ||
  5559. FTerminal->CommandSessionOpened;
  5560. if (!Result)
  5561. {
  5562. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5563. if (!GUIConfiguration->ConfirmCommandSession)
  5564. {
  5565. Result = true;
  5566. }
  5567. else
  5568. {
  5569. TMessageParams Params(mpNeverAskAgainCheck);
  5570. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5571. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5572. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5573. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5574. if (Answer == qaNeverAskAgain)
  5575. {
  5576. GUIConfiguration->ConfirmCommandSession = false;
  5577. Result = true;
  5578. }
  5579. else if (Answer == qaOK)
  5580. {
  5581. Result = true;
  5582. }
  5583. }
  5584. if (Result)
  5585. {
  5586. Result = CommandSessionFallback();
  5587. }
  5588. }
  5589. return Result;
  5590. }
  5591. //---------------------------------------------------------------------------
  5592. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5593. {
  5594. if (EnsureCommandSessionFallback(fcAnyCommand))
  5595. {
  5596. DoConsoleDialog(Terminal, Command);
  5597. }
  5598. }
  5599. //---------------------------------------------------------------------------
  5600. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5601. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5602. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5603. {
  5604. if (IsFileControl(DropSourceControl, osRemote) &&
  5605. (FDDExtMapFile != NULL))
  5606. {
  5607. Accept = true;
  5608. }
  5609. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5610. DebugAssert(FDDTargetControl != NULL);
  5611. }
  5612. //---------------------------------------------------------------------------
  5613. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5614. _di_IDataObject DataObj, int KeyState,
  5615. const TPoint & Point, int & Effect, bool & Accept)
  5616. {
  5617. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5618. }
  5619. //---------------------------------------------------------------------------
  5620. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5621. _di_IDataObject DataObj, int KeyState,
  5622. const TPoint & Point, int & Effect, bool & Accept)
  5623. {
  5624. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5625. }
  5626. //---------------------------------------------------------------------------
  5627. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5628. TObject * Sender)
  5629. {
  5630. DebugUsedParam(Sender);
  5631. DebugAssert(FDDTargetControl == Sender);
  5632. FDDTargetControl = NULL;
  5633. }
  5634. //---------------------------------------------------------------------------
  5635. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5636. {
  5637. FileControlDDDragLeave(SessionsPageControl);
  5638. }
  5639. //---------------------------------------------------------------------------
  5640. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5641. {
  5642. FileControlDDDragLeave(QueueView3);
  5643. }
  5644. //---------------------------------------------------------------------------
  5645. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5646. {
  5647. DebugAssert(GetSide(Side) == osRemote);
  5648. DebugUsedParam(Side);
  5649. bool Edit = false;
  5650. TRemoteFile * File = NULL;
  5651. UnicodeString FileName;
  5652. UnicodeString PointTo;
  5653. bool SymbolicLink = true;
  5654. if (RemoteDirView->ItemFocused)
  5655. {
  5656. DebugAssert(RemoteDirView->ItemFocused->Data);
  5657. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5658. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5659. if (Edit)
  5660. {
  5661. FileName = File->FileName;
  5662. PointTo = File->LinkTo;
  5663. }
  5664. else
  5665. {
  5666. PointTo = File->FileName;
  5667. }
  5668. }
  5669. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5670. Terminal->IsCapable[fcHardLink]))
  5671. {
  5672. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5673. if (Edit)
  5674. {
  5675. DebugAssert(File->FileName == FileName);
  5676. int Params = dfNoRecursive;
  5677. Terminal->ExceptionOnFail = true;
  5678. try
  5679. {
  5680. Terminal->DeleteFile(L"", File, &Params);
  5681. }
  5682. __finally
  5683. {
  5684. Terminal->ExceptionOnFail = false;
  5685. }
  5686. }
  5687. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  5688. }
  5689. }
  5690. //---------------------------------------------------------------------------
  5691. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  5692. {
  5693. return
  5694. (Terminal != NULL) &&
  5695. ((GetSide(Side) != osRemote) ||
  5696. (Terminal->ResolvingSymlinks &&
  5697. Terminal->IsCapable[fcSymbolicLink]));
  5698. }
  5699. //---------------------------------------------------------------------------
  5700. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  5701. {
  5702. return
  5703. (FCurrentSide == osRemote) &&
  5704. (RemoteDirView->ItemFocused != NULL) &&
  5705. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  5706. Terminal->SessionData->ResolveSymlinks;
  5707. }
  5708. //---------------------------------------------------------------------------
  5709. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  5710. {
  5711. DebugAssert(!WinConfiguration->DisableOpenEdit);
  5712. FForceExecution = true;
  5713. try
  5714. {
  5715. DirView(osCurrent)->ExecuteCurrentFile();
  5716. }
  5717. __finally
  5718. {
  5719. FForceExecution = false;
  5720. }
  5721. }
  5722. //---------------------------------------------------------------------------
  5723. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  5724. {
  5725. TEditorData ExternalEditor;
  5726. ExternalEditor.Editor = edExternal;
  5727. bool Remember = false;
  5728. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc,
  5729. (GetSide(osCurrent) == osRemote)))
  5730. {
  5731. if (Remember)
  5732. {
  5733. TEditorList * EditorList = new TEditorList;
  5734. try
  5735. {
  5736. *EditorList = *WinConfiguration->EditorList;
  5737. bool Found = false;
  5738. int i = 0;
  5739. while (!Found && (i < EditorList->Count))
  5740. {
  5741. const TEditorPreferences * Editor = EditorList->Editors[i];
  5742. if ((Editor->Data->Editor == edExternal) &&
  5743. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  5744. {
  5745. Found = true;
  5746. }
  5747. i++;
  5748. }
  5749. if (!Found)
  5750. {
  5751. EditorList->Add(new TEditorPreferences(ExternalEditor));
  5752. WinConfiguration->EditorList = EditorList;
  5753. }
  5754. }
  5755. __finally
  5756. {
  5757. delete EditorList;
  5758. }
  5759. }
  5760. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  5761. }
  5762. }
  5763. //---------------------------------------------------------------------------
  5764. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  5765. {
  5766. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  5767. if (FFileFindTerminal == Sender)
  5768. {
  5769. FFileFindTerminal = NULL;
  5770. HideFileFindDialog();
  5771. }
  5772. }
  5773. //---------------------------------------------------------------------------
  5774. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  5775. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  5776. {
  5777. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  5778. DebugAssert(Terminal != NULL);
  5779. if (Data->Terminal == Terminal)
  5780. {
  5781. Data->Terminal = NULL;
  5782. }
  5783. }
  5784. //---------------------------------------------------------------------------
  5785. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  5786. {
  5787. UpdateControls();
  5788. SessionColor = TColor(0);
  5789. UpdateRemotePathComboBox(false);
  5790. NeedSession(true);
  5791. }
  5792. //---------------------------------------------------------------------------
  5793. void __fastcall TCustomScpExplorerForm::NeedSession(bool ReloadSessions)
  5794. {
  5795. try
  5796. {
  5797. try
  5798. {
  5799. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  5800. }
  5801. __finally
  5802. {
  5803. if (!WinConfiguration->KeepOpenWhenNoSession &&
  5804. (!Terminal || !Terminal->Active))
  5805. {
  5806. TerminateApplication();
  5807. }
  5808. }
  5809. }
  5810. catch (EAbort &)
  5811. {
  5812. // swallow
  5813. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  5814. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  5815. }
  5816. }
  5817. //---------------------------------------------------------------------------
  5818. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged(bool Force)
  5819. {
  5820. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  5821. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  5822. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  5823. bool ListChanged = Force || (TerminalList->Count + 1 != SessionsPageControl->PageCount);
  5824. if (!ListChanged)
  5825. {
  5826. int Index = 0;
  5827. while (!ListChanged && (Index < TerminalList->Count))
  5828. {
  5829. ListChanged =
  5830. (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != TerminalList->Objects[Index]) ||
  5831. (SessionsPageControl->Pages[Index]->Caption != TerminalList->Strings[Index]);
  5832. Index++;
  5833. }
  5834. }
  5835. if (ListChanged)
  5836. {
  5837. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  5838. try
  5839. {
  5840. FSessionColors->Clear();
  5841. AddFixedSessionImages();
  5842. while (SessionsPageControl->PageCount > 0)
  5843. {
  5844. delete SessionsPageControl->Pages[0];
  5845. }
  5846. for (int Index = 0; Index < TerminalList->Count; Index++)
  5847. {
  5848. TThemeTabSheet * TabSheet = new TThemeTabSheet(SessionsPageControl);
  5849. TabSheet->Caption = TerminalList->Strings[Index];
  5850. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  5851. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  5852. TabSheet->PageControl = SessionsPageControl;
  5853. UpdateSessionTab(TabSheet);
  5854. }
  5855. TTabSheet * TabSheet = new TTabSheet(SessionsPageControl);
  5856. TabSheet->PageControl = SessionsPageControl;
  5857. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  5858. UpdateNewSessionTab();
  5859. }
  5860. __finally
  5861. {
  5862. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  5863. }
  5864. }
  5865. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  5866. }
  5867. //---------------------------------------------------------------------------
  5868. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  5869. {
  5870. DoTerminalListChanged(false);
  5871. }
  5872. //---------------------------------------------------------------------------
  5873. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  5874. {
  5875. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  5876. DebugAssert(TabSheet->ImageIndex == 0);
  5877. TabSheet->Caption =
  5878. WinConfiguration->SelectiveToolbarText ?
  5879. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  5880. UnicodeString();
  5881. }
  5882. //---------------------------------------------------------------------------
  5883. TTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  5884. {
  5885. return reinterpret_cast<TTerminal *>(TabSheet->Tag);
  5886. }
  5887. //---------------------------------------------------------------------------
  5888. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  5889. {
  5890. if (DebugAlwaysTrue(TabSheet != NULL))
  5891. {
  5892. TManagedTerminal * ManagedTerminal =
  5893. dynamic_cast<TManagedTerminal *>(GetSessionTabTerminal(TabSheet));
  5894. if (DebugAlwaysTrue(ManagedTerminal != NULL))
  5895. {
  5896. TColor Color =
  5897. (ManagedTerminal == FTerminal) ? FSessionColor : ManagedTerminal->StateData->Color;
  5898. TabSheet->ImageIndex = AddSessionColor(Color);
  5899. TabSheet->Caption = TTerminalManager::Instance()->GetTerminalTitle(ManagedTerminal, true);
  5900. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  5901. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  5902. {
  5903. ThemeTabSheet->Shadowed = !ManagedTerminal->Active;
  5904. }
  5905. }
  5906. }
  5907. }
  5908. //---------------------------------------------------------------------------
  5909. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  5910. {
  5911. DebugAssert(SessionsPageControl->ActivePage != NULL);
  5912. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  5913. bool Result = (Terminal != NULL);
  5914. if (Result)
  5915. {
  5916. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  5917. }
  5918. else
  5919. {
  5920. try
  5921. {
  5922. NewSession(false);
  5923. }
  5924. __finally
  5925. {
  5926. DoTerminalListChanged(false);
  5927. }
  5928. FSessionsPageControlNewSessionTime = Now();
  5929. }
  5930. return Result;
  5931. }
  5932. //---------------------------------------------------------------------------
  5933. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  5934. {
  5935. SessionTabSwitched();
  5936. }
  5937. //---------------------------------------------------------------------------
  5938. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  5939. {
  5940. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  5941. DebugAssert(TransferList != NULL);
  5942. UnicodeString Name;
  5943. if (TransferList->ItemIndex <= 0)
  5944. {
  5945. Name = L"";
  5946. }
  5947. else
  5948. {
  5949. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  5950. }
  5951. if (FCopyParamAutoSelected.IsEmpty())
  5952. {
  5953. // if previous preset was not autoselected, make new preset the "default"
  5954. FCopyParamDefault = Name;
  5955. }
  5956. GUIConfiguration->CopyParamCurrent = Name;
  5957. }
  5958. //---------------------------------------------------------------------------
  5959. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  5960. {
  5961. // sanity check
  5962. bool ExistingPreset =
  5963. (FTransferListHoverIndex >= 0) &&
  5964. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  5965. DebugAssert(ExistingPreset);
  5966. if (ExistingPreset)
  5967. {
  5968. HDC DC = GetDC(0);
  5969. TCanvas * Canvas = new TCanvas();
  5970. try
  5971. {
  5972. Canvas->Handle = DC;
  5973. Canvas->Font = GetToolbarFont(this);
  5974. UnicodeString Name;
  5975. if (FTransferListHoverIndex == 0)
  5976. {
  5977. Name = L"";
  5978. }
  5979. else
  5980. {
  5981. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  5982. }
  5983. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  5984. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  5985. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  5986. static_cast<TObject*>(GetComponent(fcTransferList)));
  5987. UnicodeString InfoStr =
  5988. GUIConfiguration->CopyParamPreset[Name].
  5989. GetInfoStr(L"; ",
  5990. FLAGMASK(Terminal != NULL, Terminal->UsableCopyParamAttrs(0).General));
  5991. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  5992. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  5993. {
  5994. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  5995. {
  5996. InfoStr.SetLength(InfoStr.Length() - 1);
  5997. }
  5998. InfoStr += Ellipsis;
  5999. }
  6000. // UpdateCaption does not cause invalidation of whole submenu, while
  6001. // setting Caption property does.
  6002. // also it probably does not resize the label, even if necessary
  6003. // (we do not want that anyway)
  6004. TransferLabel->UpdateCaption(InfoStr);
  6005. }
  6006. __finally
  6007. {
  6008. Canvas->Handle = NULL;
  6009. ReleaseDC(0, DC);
  6010. delete Canvas;
  6011. }
  6012. }
  6013. }
  6014. //---------------------------------------------------------------------------
  6015. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6016. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6017. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6018. {
  6019. if (FTransferListHoverIndex != AHoverIndex)
  6020. {
  6021. FTransferListHoverIndex = AHoverIndex;
  6022. UpdateTransferLabel();
  6023. }
  6024. }
  6025. //---------------------------------------------------------------------------
  6026. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6027. {
  6028. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6029. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6030. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6031. {
  6032. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6033. {
  6034. OpenDirectory(GetSide(osCurrent));
  6035. Message.Result = 1;
  6036. }
  6037. else
  6038. {
  6039. TForm::Dispatch(&Message);
  6040. }
  6041. }
  6042. else
  6043. {
  6044. TForm::Dispatch(&Message);
  6045. }
  6046. }
  6047. //---------------------------------------------------------------------------
  6048. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6049. {
  6050. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6051. if ((FIgnoreNextDialogChar != 0) &&
  6052. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6053. {
  6054. Message.Result = 1;
  6055. }
  6056. else
  6057. {
  6058. TForm::Dispatch(&Message);
  6059. }
  6060. FIgnoreNextDialogChar = 0;
  6061. }
  6062. //---------------------------------------------------------------------------
  6063. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6064. {
  6065. // The four low-order bits are used internally by Windows
  6066. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6067. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6068. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6069. // 61730 - restore thru double click - undocumented
  6070. // 61490 - maximize thru double click - undocumented
  6071. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6072. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6073. {
  6074. SysResizing(Cmd);
  6075. }
  6076. TForm::Dispatch(&Message);
  6077. }
  6078. //---------------------------------------------------------------------------
  6079. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6080. {
  6081. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6082. // Also there no point returning TRUE as we are not able to
  6083. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6084. // Hence the process termination might be safer :)
  6085. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6086. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6087. {
  6088. Message.Result = FALSE;
  6089. }
  6090. else
  6091. {
  6092. Message.Result = TRUE;
  6093. }
  6094. // Do not call default handling as that triggers OnCloseQuery,
  6095. // where our implementation will popup confirmation dialogs, what we do not want,
  6096. // as per Vista guidelines:
  6097. // https://docs.microsoft.com/en-us/windows/desktop/shutdown/shutting-down
  6098. }
  6099. //---------------------------------------------------------------------------
  6100. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6101. {
  6102. if (Message.EndSession && IsApplicationMinimized())
  6103. {
  6104. // WORKAROUND
  6105. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6106. // when it receives WM_ENDSESSION,
  6107. // but that sometimes (particularly when application is minimized) cause crashes.
  6108. // Crash popups message that blocks log off.
  6109. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6110. // so we call ExitProcess() immediately, not even trying to cleanup.
  6111. // It still causes beep, so there's likely some popup, but it does not block
  6112. // log off.
  6113. ExitProcess(0);
  6114. }
  6115. TForm::Dispatch(&Message);
  6116. }
  6117. #ifdef _DEBUG
  6118. //---------------------------------------------------------------------------
  6119. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6120. {
  6121. TForm::Dispatch(&Message);
  6122. }
  6123. #endif
  6124. //---------------------------------------------------------------------------
  6125. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6126. {
  6127. }
  6128. //---------------------------------------------------------------------------
  6129. void __fastcall TCustomScpExplorerForm::DoShow()
  6130. {
  6131. // only now are the controls resized finally, so the size constraints
  6132. // will not conflict with possibly very small window size
  6133. RestoreFormParams();
  6134. FixControlsPlacement();
  6135. if (Position == poDefaultPosOnly)
  6136. {
  6137. CutFormToDesktop(this);
  6138. }
  6139. TForm::DoShow();
  6140. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6141. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6142. if (Terminal == NULL)
  6143. {
  6144. StartingDisconnected();
  6145. }
  6146. UpdatePixelsPerInchMainWindowCounter();
  6147. FShowing = true;
  6148. }
  6149. //---------------------------------------------------------------------------
  6150. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6151. {
  6152. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6153. }
  6154. //---------------------------------------------------------------------------
  6155. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6156. {
  6157. DoTerminalListChanged(true);
  6158. }
  6159. //---------------------------------------------------------------------------
  6160. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6161. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6162. TNotifyEvent OnBalloonClick, TObject * UserData)
  6163. {
  6164. bool Do;
  6165. UnicodeString Message;
  6166. if (E == NULL)
  6167. {
  6168. Message = Str;
  6169. Do = true;
  6170. }
  6171. else
  6172. {
  6173. Do = ExceptionMessage(E, Message);
  6174. }
  6175. if (Do && WinConfiguration->BalloonNotifications)
  6176. {
  6177. UnicodeString Title;
  6178. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6179. {
  6180. Message = TrimLeft(Message);
  6181. }
  6182. else
  6183. {
  6184. Message = UnformatMessage(Message);
  6185. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6186. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6187. Title = LoadResourceString(Captions[Type]);
  6188. if (Terminal != NULL)
  6189. {
  6190. Title = FORMAT(L"%s - %s",
  6191. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6192. }
  6193. }
  6194. if (Seconds == 0)
  6195. {
  6196. Seconds = WinConfiguration->NotificationsTimeout;
  6197. }
  6198. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6199. }
  6200. }
  6201. //---------------------------------------------------------------------------
  6202. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6203. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6204. UnicodeString HelpKeyword, const TMessageParams * Params,
  6205. TTerminal * Terminal)
  6206. {
  6207. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6208. (FOnFeedSynchronizeError != NULL)) &&
  6209. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6210. {
  6211. if (FOnFeedSynchronizeError != NULL)
  6212. {
  6213. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6214. }
  6215. else
  6216. {
  6217. DebugAssert(FErrorList != NULL);
  6218. TStringList * MoreMessagesCopy = NULL;
  6219. if (MoreMessages)
  6220. {
  6221. MoreMessagesCopy = new TStringList();
  6222. MoreMessagesCopy->Assign(MoreMessages);
  6223. }
  6224. FErrorList->AddObject(Message, MoreMessagesCopy);
  6225. }
  6226. PopupTrayBalloon(Terminal, Message, Type);
  6227. return ContinueAnswer(Answers);
  6228. }
  6229. else
  6230. {
  6231. bool UseBalloon = FTrayIcon->Visible;
  6232. if (UseBalloon)
  6233. {
  6234. PopupTrayBalloon(Terminal, Message, Type);
  6235. }
  6236. unsigned int Result;
  6237. try
  6238. {
  6239. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6240. }
  6241. __finally
  6242. {
  6243. // cancel only balloon we popped up, otherwise we may cancel notification
  6244. // balloon that was there before the message dialog
  6245. // (such as "dd confirmation opt in balloon)
  6246. if (UseBalloon)
  6247. {
  6248. FTrayIcon->CancelBalloon();
  6249. }
  6250. }
  6251. return Result;
  6252. }
  6253. }
  6254. //---------------------------------------------------------------------------
  6255. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6256. TTerminal * Terminal, Exception * E)
  6257. {
  6258. if (FTrayIcon->Visible)
  6259. {
  6260. PopupTrayBalloon(Terminal, L"", qtError, E);
  6261. }
  6262. // particularly prevent opening new session from jump list,
  6263. // while exception is shown
  6264. NonVisualDataModule->StartBusy();
  6265. try
  6266. {
  6267. ShowExtendedExceptionEx(Terminal, E);
  6268. }
  6269. __finally
  6270. {
  6271. NonVisualDataModule->EndBusy();
  6272. FTrayIcon->CancelBalloon();
  6273. }
  6274. }
  6275. //---------------------------------------------------------------------------
  6276. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6277. {
  6278. // cannot rely on active page being page for active terminal,
  6279. // as it can happen that active page is the "new session" page
  6280. // (e.g. when reconnecting active terminal, while login dialog
  6281. // invoked from "new session" page is modal)
  6282. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6283. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6284. }
  6285. //---------------------------------------------------------------------------
  6286. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6287. TTerminal * Terminal, Exception * E)
  6288. {
  6289. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6290. if (!Terminal->Active)
  6291. {
  6292. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6293. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6294. {
  6295. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6296. }
  6297. }
  6298. }
  6299. //---------------------------------------------------------------------------
  6300. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6301. UnicodeString Message, TQueryType Type,
  6302. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6303. {
  6304. if ((E == NULL) ||
  6305. ExceptionMessage(E, Message))
  6306. {
  6307. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6308. if (Important)
  6309. {
  6310. Seconds *= 5;
  6311. }
  6312. UnicodeString NoteMessage(UnformatMessage(Message));
  6313. if (Terminal != NULL)
  6314. {
  6315. NoteMessage = FORMAT(L"%s: %s",
  6316. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6317. }
  6318. if (WinConfiguration->BalloonNotifications)
  6319. {
  6320. AddNote(NoteMessage);
  6321. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6322. }
  6323. else
  6324. {
  6325. FlashOnBackground();
  6326. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6327. }
  6328. }
  6329. }
  6330. //---------------------------------------------------------------------------
  6331. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6332. {
  6333. RestoreApp();
  6334. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6335. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6336. !NonVisualDataModule->Busy)
  6337. {
  6338. TTerminal * Terminal = TerminalNoteData->Terminal;
  6339. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6340. if (!ComponentVisible[fcQueueView])
  6341. {
  6342. ToggleQueueVisibility();
  6343. GoToQueue();
  6344. }
  6345. }
  6346. }
  6347. //---------------------------------------------------------------------------
  6348. void __fastcall TCustomScpExplorerForm::QueueEvent(TTerminal * ATerminal,
  6349. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6350. {
  6351. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  6352. UnicodeString Message;
  6353. TNotifyEvent OnClick = NULL;
  6354. TObject * UserData = NULL;
  6355. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6356. switch (Event)
  6357. {
  6358. case qeEmptyButMonitored:
  6359. if ((ATerminal != Terminal) || QueueInvisible)
  6360. {
  6361. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6362. OnClick = QueueEmptyNoteClicked;
  6363. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6364. TerminalNoteData->Terminal = ATerminal;
  6365. UserData = TerminalNoteData;
  6366. }
  6367. break;
  6368. case qeEmpty:
  6369. OperationComplete(ManagedTerminal->QueueOperationStart);
  6370. break;
  6371. case qePendingUserAction:
  6372. if ((ATerminal != Terminal) ||
  6373. (QueueInvisible && !IsQueueAutoPopup()))
  6374. {
  6375. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6376. }
  6377. break;
  6378. default:
  6379. DebugFail();
  6380. }
  6381. if (!Message.IsEmpty())
  6382. {
  6383. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6384. }
  6385. }
  6386. //---------------------------------------------------------------------------
  6387. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6388. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6389. {
  6390. if (FDDExtMapFile != NULL)
  6391. {
  6392. CloseHandle(FDDExtMapFile);
  6393. FDDExtMapFile = NULL;
  6394. }
  6395. if (WinConfiguration->DDFakeFile)
  6396. {
  6397. DDFakeFileInitDrag(FileList, Created);
  6398. }
  6399. }
  6400. //---------------------------------------------------------------------------
  6401. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6402. {
  6403. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6404. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6405. {
  6406. FFakeFileDropTarget = FileName;
  6407. }
  6408. }
  6409. //---------------------------------------------------------------------------
  6410. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6411. bool & Created)
  6412. {
  6413. FFakeFileDropTarget = UnicodeString();
  6414. FDragFakeDirectory =
  6415. ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6416. if (!ForceDirectories(ApiPath(FDragFakeDirectory)))
  6417. {
  6418. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (FDragFakeDirectory)));
  6419. }
  6420. FileSetAttr(ApiPath(FDragFakeDirectory), faHidden);
  6421. FileList->AddItem(NULL, FDragFakeDirectory);
  6422. Created = true;
  6423. if (!WinConfiguration->IsDDExtRunning() || (GetWindowsBuild() >= 17134))
  6424. {
  6425. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6426. }
  6427. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6428. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6429. {
  6430. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6431. TDragExtCommStruct* CommStruct;
  6432. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6433. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6434. DebugAssert(CommStruct != NULL);
  6435. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6436. CommStruct->Dragging = true;
  6437. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6438. LENOF(CommStruct->DropDest));
  6439. NULL_TERMINATE(CommStruct->DropDest);
  6440. UnmapViewOfFile(CommStruct);
  6441. }
  6442. FDDMoveSlipped = false;
  6443. }
  6444. //---------------------------------------------------------------------------
  6445. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6446. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6447. {
  6448. bool Result;
  6449. if (Sender == RemoteDirView)
  6450. {
  6451. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6452. }
  6453. else
  6454. {
  6455. DebugAssert(Sender == RemoteDriveView);
  6456. TStrings * FileList = RemoteDriveView->DragFileList();
  6457. try
  6458. {
  6459. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6460. }
  6461. __finally
  6462. {
  6463. delete FileList;
  6464. }
  6465. }
  6466. if (FDDTargetControl == RemoteDriveView)
  6467. {
  6468. RemoteDriveView->UpdateDropTarget();
  6469. }
  6470. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6471. // or from tree view to dir view.
  6472. // foMove happens when file/dir is dragged from remote tree view outside of
  6473. // application via dragex
  6474. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6475. (DropSourceControl == RemoteDriveView))
  6476. {
  6477. RemoteDriveView->UpdateDropSource();
  6478. }
  6479. return Result;
  6480. }
  6481. //---------------------------------------------------------------------------
  6482. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6483. {
  6484. // This also handles drops of remote files to queue.
  6485. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6486. SAFE_DESTROY(FDDFileList);
  6487. if (!FFakeFileDropTarget.IsEmpty())
  6488. {
  6489. RemoveDir(ApiPath(FFakeFileDropTarget));
  6490. }
  6491. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6492. {
  6493. try
  6494. {
  6495. TDragResult DDResult = (Sender == RemoteDirView) ?
  6496. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6497. // Focus is moved to the target application,
  6498. // but as we are going to present the UI, we need to steal the focus back.
  6499. // This most likely won't work though (windows does not allow application
  6500. // to steal focus most of the time)
  6501. Application->BringToFront();
  6502. // On older version of Windows we never got drMove here, see also comment below.
  6503. // On Windows 10, we get the "move".
  6504. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6505. {
  6506. UnicodeString TargetDirectory;
  6507. TFileOperation Operation;
  6508. // drInvalid may mean drMove, see comment below
  6509. switch (DDResult)
  6510. {
  6511. case drCopy:
  6512. Operation = foCopy;
  6513. break;
  6514. case drMove:
  6515. Operation = foMove;
  6516. break;
  6517. default:
  6518. DebugFail();
  6519. case drInvalid:
  6520. // prefer "copy" for safety
  6521. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6522. break;
  6523. }
  6524. if (FDDMoveSlipped)
  6525. {
  6526. Operation = foMove;
  6527. }
  6528. TTransferOperationParam Param;
  6529. UnicodeString CounterName;
  6530. bool ForceQueue;
  6531. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6532. {
  6533. // we get drInvalid both if d&d was intercepted by ddext,
  6534. // and when users drops on no-drop location.
  6535. // we tell the difference by existence of response from ddext,
  6536. // so we ignore absence of response in this case
  6537. if (DDResult != drInvalid)
  6538. {
  6539. // here we know that the extension is installed,
  6540. // as it is checked as soon as drag&drop starts from
  6541. // RemoteFileControlDDCreateDragFileList
  6542. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6543. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6544. }
  6545. }
  6546. else
  6547. {
  6548. // download using fake file
  6549. Param.Temp = false;
  6550. Param.DragDrop = true;
  6551. if (ForceQueue)
  6552. {
  6553. Param.Queue = asOn;
  6554. }
  6555. if (Sender == RemoteDirView)
  6556. {
  6557. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6558. }
  6559. if (RemoteFileControlFileOperation(Sender, Operation,
  6560. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6561. {
  6562. Configuration->Usage->Inc(CounterName);
  6563. }
  6564. }
  6565. }
  6566. }
  6567. __finally
  6568. {
  6569. CloseHandle(FDDExtMapFile);
  6570. FDDExtMapFile = NULL;
  6571. delete FDragFakeMonitors;
  6572. FDragFakeMonitors = NULL;
  6573. RemoveDir(ApiPath(FDragFakeDirectory));
  6574. FDragFakeDirectory = L"";
  6575. FFakeFileDropTarget = UnicodeString();
  6576. }
  6577. }
  6578. if (FDragDropSshTerminate.get() != NULL)
  6579. {
  6580. FDragDropSshTerminate->Rethrow();
  6581. }
  6582. }
  6583. //---------------------------------------------------------------------------
  6584. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6585. TObject * Sender, int dwEffect, HRESULT & /*Result*/)
  6586. {
  6587. HCURSOR SlippedCopyCursor;
  6588. FDDMoveSlipped =
  6589. (FDragMoveCursor != NULL) &&
  6590. (!WinConfiguration->DDAllowMoveInit) && (dwEffect == DROPEFFECT_Copy) &&
  6591. ((IsFileControl(FDDTargetControl, osRemote) && (GetKeyState(VK_CONTROL) >= 0) &&
  6592. FTerminal->IsCapable[fcRemoteMove]) ||
  6593. (IsFileControl(FDDTargetControl, osLocal) && (GetKeyState(VK_SHIFT) < 0)));
  6594. SlippedCopyCursor = FDDMoveSlipped ? FDragMoveCursor : Dragdrop::DefaultCursor;
  6595. DragDropFiles(Sender)->CHCopy = SlippedCopyCursor;
  6596. DragDropFiles(Sender)->CHScrollCopy = SlippedCopyCursor;
  6597. // Remember drop effect so we know (when user drops files), if we copy or move
  6598. FLastDropEffect = dwEffect;
  6599. }
  6600. //---------------------------------------------------------------------------
  6601. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6602. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6603. {
  6604. bool Result;
  6605. if (FDDTargetControl == QueueView3)
  6606. {
  6607. Directory = DefaultDownloadTargetDirectory();
  6608. Result = true;
  6609. CounterName = L"DownloadsDragDropQueue";
  6610. ForceQueue = true;
  6611. }
  6612. else if (!FFakeFileDropTarget.IsEmpty())
  6613. {
  6614. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6615. Result = true;
  6616. ForceQueue = false;
  6617. CounterName = L"DownloadsDragDropFakeFile";
  6618. }
  6619. else
  6620. {
  6621. ForceQueue = false;
  6622. Enabled = false;
  6623. try
  6624. {
  6625. int Timer = 0;
  6626. Result = false;
  6627. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6628. {
  6629. {
  6630. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6631. TDragExtCommStruct* CommStruct;
  6632. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6633. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6634. DebugAssert(CommStruct != NULL);
  6635. Result = !CommStruct->Dragging;
  6636. if (Result)
  6637. {
  6638. Directory = ExtractFilePath(CommStruct->DropDest);
  6639. CounterName = L"DownloadsDragDropExternalExt";
  6640. }
  6641. UnmapViewOfFile(CommStruct);
  6642. }
  6643. if (!Result)
  6644. {
  6645. Sleep(50);
  6646. Timer += 50;
  6647. Application->ProcessMessages();
  6648. }
  6649. }
  6650. }
  6651. __finally
  6652. {
  6653. Enabled = true;
  6654. }
  6655. }
  6656. return Result;
  6657. }
  6658. //---------------------------------------------------------------------------
  6659. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  6660. const UnicodeString TempDir, int SecDelay)
  6661. {
  6662. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  6663. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  6664. if (FDelayedDeletionTimer == NULL)
  6665. {
  6666. DebugAssert(HandleAllocated());
  6667. FDelayedDeletionTimer = new TTimer(this);
  6668. FDelayedDeletionTimer->Interval = 10000;
  6669. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  6670. }
  6671. else
  6672. {
  6673. FDelayedDeletionTimer->Enabled = true;
  6674. }
  6675. }
  6676. //---------------------------------------------------------------------------
  6677. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  6678. {
  6679. DebugAssert(FDelayedDeletionList != NULL);
  6680. TDateTime N = Now();
  6681. TDateTime Alert;
  6682. UnicodeString Directory;
  6683. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  6684. {
  6685. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  6686. if ((N >= Alert) || (Sender == NULL))
  6687. {
  6688. Directory = FDelayedDeletionList->Strings[Index];
  6689. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  6690. {
  6691. FDelayedDeletionList->Delete(Index);
  6692. }
  6693. }
  6694. }
  6695. if (FDelayedDeletionList->Count == 0)
  6696. {
  6697. FDelayedDeletionTimer->Enabled = false;
  6698. }
  6699. }
  6700. //---------------------------------------------------------------------------
  6701. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  6702. {
  6703. if (IsFileControl(FDDTargetControl, osRemote) ||
  6704. (FDDTargetControl == SessionsPageControl))
  6705. {
  6706. TTerminal * TargetTerminal = NULL;
  6707. TFileOperation Operation = foNone;
  6708. if (FDDTargetControl == SessionsPageControl)
  6709. {
  6710. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  6711. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  6712. // do not allow dropping on the "+" tab
  6713. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  6714. if (TargetTerminal != NULL)
  6715. {
  6716. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  6717. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  6718. {
  6719. Operation = foRemoteMove;
  6720. }
  6721. else
  6722. {
  6723. Operation = foRemoteCopy;
  6724. }
  6725. }
  6726. }
  6727. else
  6728. {
  6729. // when move from remote side is disabled, we allow copying inside the remote
  6730. // panel, but we interpret is as moving (we also slip in the move cursor)
  6731. if ((FLastDropEffect == DROPEFFECT_MOVE) ||
  6732. (!WinConfiguration->DDAllowMoveInit && (FLastDropEffect == DROPEFFECT_COPY) &&
  6733. FDDMoveSlipped))
  6734. {
  6735. Operation = foRemoteMove;
  6736. }
  6737. else if (FLastDropEffect == DROPEFFECT_COPY)
  6738. {
  6739. Operation = foRemoteCopy;
  6740. }
  6741. }
  6742. if (Operation != foNone)
  6743. {
  6744. RemoteFileControlFileOperation(DropSourceControl,
  6745. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  6746. }
  6747. // abort drag&drop
  6748. Abort();
  6749. }
  6750. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  6751. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  6752. (FDDTargetControl != QueueView3))
  6753. {
  6754. DebugAssert(!FDragTempDir.IsEmpty());
  6755. TTransferType Type;
  6756. UnicodeString TempDir = FDragTempDir;
  6757. // We clear FDragTempDir before calling
  6758. // just in case it fail (raises exception)
  6759. FDragTempDir = L"";
  6760. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  6761. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  6762. // empty directory parameter means temp directory -> don't display it!
  6763. UnicodeString TargetDir = L"";
  6764. int Options = 0;
  6765. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  6766. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  6767. {
  6768. Abort();
  6769. }
  6770. // TargetDir is set when dropped on local file control
  6771. // (this was workaround for legacy dirview event handling, now it should be
  6772. // made prettier)
  6773. if (TargetDir.IsEmpty())
  6774. {
  6775. TargetDir = TempDir;
  6776. if (ForceDirectories(ApiPath(TargetDir)))
  6777. {
  6778. DebugAssert(Terminal && !TargetDir.IsEmpty());
  6779. FPendingTempSpaceWarn = true;
  6780. try
  6781. {
  6782. FDragDropOperation = true;
  6783. int Params = cpTemporary |
  6784. (Type == ttMove ? cpDelete : 0);
  6785. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  6786. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  6787. }
  6788. __finally
  6789. {
  6790. FDragDropOperation = false;
  6791. FPendingTempSpaceWarn = false;
  6792. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  6793. }
  6794. }
  6795. else
  6796. {
  6797. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  6798. }
  6799. }
  6800. }
  6801. }
  6802. //---------------------------------------------------------------------------
  6803. void __fastcall TCustomScpExplorerForm::DDDownload(
  6804. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  6805. {
  6806. void * BatchStorage;
  6807. BatchStart(BatchStorage);
  6808. try
  6809. {
  6810. UpdateCopyParamCounters(*CopyParam);
  6811. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  6812. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  6813. {
  6814. RemoteDriveView->UpdateDropSource();
  6815. }
  6816. }
  6817. __finally
  6818. {
  6819. BatchEnd(BatchStorage);
  6820. }
  6821. }
  6822. //---------------------------------------------------------------------------
  6823. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  6824. {
  6825. public:
  6826. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  6827. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename)
  6828. {
  6829. }
  6830. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  6831. {
  6832. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  6833. TDataObjectFilesEx::AllowData(FormatEtc);
  6834. }
  6835. };
  6836. //---------------------------------------------------------------------------
  6837. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  6838. TObject * Sender, TDataObject *& DataObject)
  6839. {
  6840. if (FDDExtMapFile != NULL)
  6841. {
  6842. TFileList * FileList = DragDropFiles(Sender)->FileList;
  6843. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  6844. {
  6845. Abort();
  6846. }
  6847. if (FileList->Count > 0)
  6848. {
  6849. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  6850. if (!FilesObject->IsValid(true, true))
  6851. {
  6852. FilesObject->_Release();
  6853. }
  6854. else
  6855. {
  6856. DataObject = FilesObject;
  6857. }
  6858. }
  6859. }
  6860. }
  6861. //---------------------------------------------------------------------------
  6862. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  6863. {
  6864. DebugFail();
  6865. }
  6866. //---------------------------------------------------------------------------
  6867. void __fastcall TCustomScpExplorerForm::GoToTree()
  6868. {
  6869. ComponentVisible[fcRemoteTree] = true;
  6870. RemoteDriveView->SetFocus();
  6871. }
  6872. //---------------------------------------------------------------------------
  6873. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  6874. TPanelExport Export)
  6875. {
  6876. TCustomDirView * DirView = this->DirView(Side);
  6877. std::unique_ptr<TStrings> ExportData(new TStringList());
  6878. switch (Export)
  6879. {
  6880. case pePath:
  6881. ExportData->Add(DirView->PathName);
  6882. break;
  6883. case peFileList:
  6884. case peFullFileList:
  6885. {
  6886. bool FullPath = (Export == peFullFileList);
  6887. DirView->CreateFileList(false, FullPath, ExportData.get());
  6888. for (int Index = 0; Index < ExportData->Count; Index++)
  6889. {
  6890. if (ExportData->Strings[Index].Pos(L" ") > 0)
  6891. {
  6892. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  6893. }
  6894. }
  6895. }
  6896. break;
  6897. default:
  6898. DebugFail();
  6899. }
  6900. return ExportData.release();
  6901. }
  6902. //---------------------------------------------------------------------------
  6903. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  6904. TPanelExport Export, TPanelExportDestination Destination)
  6905. {
  6906. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  6907. PanelExportStore(Side, Export, Destination, ExportData.get());
  6908. }
  6909. //---------------------------------------------------------------------------
  6910. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  6911. TPanelExport /*Export*/, TPanelExportDestination Destination,
  6912. TStrings * ExportData)
  6913. {
  6914. if (Destination == pedClipboard)
  6915. {
  6916. TInstantOperationVisualizer Visualizer;
  6917. CopyToClipboard(ExportData);
  6918. }
  6919. else
  6920. {
  6921. DebugFail();
  6922. }
  6923. }
  6924. //---------------------------------------------------------------------------
  6925. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  6926. {
  6927. TCustomDirView * DirView = this->DirView(Side);
  6928. TFileFilter Filter;
  6929. DefaultFileFilter(Filter);
  6930. Filter.Masks = DirView->Mask;
  6931. if (DoFilterMaskDialog(DirView, &Filter))
  6932. {
  6933. DirView->Mask = TFileMasks::NormalizeMask(Filter.Masks);
  6934. Configuration->Usage->Inc(L"Filters");
  6935. }
  6936. }
  6937. //---------------------------------------------------------------------------
  6938. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  6939. {
  6940. return FQueueController->DefaultOperation();
  6941. }
  6942. //---------------------------------------------------------------------------
  6943. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  6944. TQueueOperation Operation, void ** Param)
  6945. {
  6946. switch (Operation)
  6947. {
  6948. case qoPreferences:
  6949. return true;
  6950. case qoGoTo:
  6951. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  6952. case qoOnceEmpty:
  6953. return !FQueueController->Empty;
  6954. default:
  6955. return FQueueController->AllowOperation(Operation, Param);
  6956. }
  6957. }
  6958. //---------------------------------------------------------------------------
  6959. void __fastcall TCustomScpExplorerForm::GoToQueue()
  6960. {
  6961. if (DebugAlwaysTrue(QueueView3->Visible))
  6962. {
  6963. QueueView3->SetFocus();
  6964. }
  6965. }
  6966. //---------------------------------------------------------------------------
  6967. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  6968. TQueueOperation Operation, void * Param)
  6969. {
  6970. if (Operation == qoGoTo)
  6971. {
  6972. GoToQueue();
  6973. }
  6974. else if (Operation == qoPreferences)
  6975. {
  6976. PreferencesDialog(pmQueue);
  6977. }
  6978. else
  6979. {
  6980. FQueueController->ExecuteOperation(Operation, Param);
  6981. }
  6982. }
  6983. //---------------------------------------------------------------------------
  6984. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  6985. {
  6986. return (Queue != NULL) && Queue->Enabled;
  6987. }
  6988. //---------------------------------------------------------------------------
  6989. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  6990. {
  6991. DebugAssert(Queue != NULL);
  6992. if (Queue != NULL)
  6993. {
  6994. Queue->Enabled = !Queue->Enabled;
  6995. }
  6996. }
  6997. //---------------------------------------------------------------------------
  6998. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  6999. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7000. {
  7001. FQueueActedItem = QueueView3->ItemFocused;
  7002. }
  7003. //---------------------------------------------------------------------------
  7004. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7005. {
  7006. return TopDock->Height + QueueSplitter->Height;
  7007. }
  7008. //---------------------------------------------------------------------------
  7009. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7010. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7011. {
  7012. // when queue is hidden by double-clicking splitter, stray attempt to
  7013. // resize the panel with strange value arrives, make sure it is ignored
  7014. if (ComponentVisible[fcQueueView])
  7015. {
  7016. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7017. RemotePanel->Constraints->MinHeight;
  7018. if (NewSize > HeightLimit)
  7019. {
  7020. NewSize = HeightLimit;
  7021. }
  7022. }
  7023. else
  7024. {
  7025. Accept = false;
  7026. }
  7027. }
  7028. //---------------------------------------------------------------------------
  7029. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7030. TDragObject *& /*DragObject*/)
  7031. {
  7032. FQueueActedItem = QueueView3->ItemFocused;
  7033. }
  7034. //---------------------------------------------------------------------------
  7035. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7036. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7037. {
  7038. Accept = true;
  7039. if (Source == QueueView3)
  7040. {
  7041. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7042. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7043. if (Accept)
  7044. {
  7045. TQueueItemProxy * QueueItem;
  7046. TQueueItemProxy * DestQueueItem;
  7047. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7048. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7049. Accept = (QueueItem != DestQueueItem) &&
  7050. (QueueItem->Status == TQueueItem::qsPending) &&
  7051. (DestQueueItem->Status == TQueueItem::qsPending);
  7052. }
  7053. }
  7054. }
  7055. //---------------------------------------------------------------------------
  7056. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7057. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7058. {
  7059. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7060. {
  7061. TQueueItemProxy * QueueItem;
  7062. TQueueItemProxy * DestQueueItem;
  7063. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7064. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7065. QueueItem->Move(DestQueueItem);
  7066. }
  7067. }
  7068. //---------------------------------------------------------------------------
  7069. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7070. {
  7071. if ((QueueView3->ItemFocused == NULL) &&
  7072. (QueueView3->Items->Count > 0))
  7073. {
  7074. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7075. }
  7076. QueueLabelUpdateStatus();
  7077. }
  7078. //---------------------------------------------------------------------------
  7079. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7080. {
  7081. QueueLabelUpdateStatus();
  7082. }
  7083. //---------------------------------------------------------------------------
  7084. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7085. {
  7086. QueueLabel->UpdateStatus();
  7087. }
  7088. //---------------------------------------------------------------------------
  7089. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7090. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7091. {
  7092. if (Selected)
  7093. {
  7094. NonVisualDataModule->UpdateNonVisibleActions();
  7095. }
  7096. }
  7097. //---------------------------------------------------------------------------
  7098. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7099. {
  7100. TDragDropFilesEx * Result = NULL;
  7101. if (Sender == SessionsPageControl)
  7102. {
  7103. Result = FSessionsDragDropFilesEx;
  7104. }
  7105. else if (Sender == QueueView3)
  7106. {
  7107. Result = FQueueDragDropFilesEx;
  7108. }
  7109. else
  7110. {
  7111. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7112. if (DirView != NULL)
  7113. {
  7114. Result = DirView->DragDropFilesEx;
  7115. }
  7116. else
  7117. {
  7118. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7119. if (DriveView != NULL)
  7120. {
  7121. Result = DriveView->DragDropFilesEx;
  7122. }
  7123. }
  7124. }
  7125. DebugAssert(Result != NULL);
  7126. return Result;
  7127. }
  7128. //---------------------------------------------------------------------------
  7129. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7130. {
  7131. return (DView->SelCount == DView->FilesCount);
  7132. }
  7133. //---------------------------------------------------------------------------
  7134. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7135. {
  7136. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7137. }
  7138. //---------------------------------------------------------------------------
  7139. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7140. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue)
  7141. {
  7142. TFileOperation Operation;
  7143. switch (Effect)
  7144. {
  7145. case DROPEFFECT_MOVE:
  7146. Operation = foMove;
  7147. break;
  7148. case DROPEFFECT_COPY:
  7149. // occures on WinXP (reported by user)
  7150. default:
  7151. Operation = foCopy;
  7152. break;
  7153. };
  7154. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7155. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7156. if (DragDropFilesEx->FileList->Count > 0)
  7157. {
  7158. TStrings * FileList = new TStringList();
  7159. try
  7160. {
  7161. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7162. {
  7163. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7164. }
  7165. FDragDropOperation = true;
  7166. TTransferOperationParam Param;
  7167. Param.TargetDirectory = TargetPath;
  7168. // upload, no temp dirs
  7169. Param.Temp = false;
  7170. Param.DragDrop = true;
  7171. Param.Options =
  7172. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7173. if (ForceQueue)
  7174. {
  7175. Param.Queue = asOn;
  7176. }
  7177. if (ExecuteFileOperation(Operation, osLocal, FileList,
  7178. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7179. {
  7180. if (IsFileControl(DropSourceControl, osLocal))
  7181. {
  7182. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7183. }
  7184. else
  7185. {
  7186. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7187. }
  7188. }
  7189. }
  7190. __finally
  7191. {
  7192. FDragDropOperation = false;
  7193. delete FileList;
  7194. }
  7195. }
  7196. }
  7197. //---------------------------------------------------------------------------
  7198. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7199. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7200. UnicodeString TargetPath, bool & /*DoOperation*/)
  7201. {
  7202. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false);
  7203. }
  7204. //---------------------------------------------------------------------------
  7205. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7206. TObject * Sender, int grfKeyState, int & dwEffect)
  7207. {
  7208. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7209. if ((dwEffect != DROPEFFECT_None) &&
  7210. IsFileControl(DropSourceControl, osRemote))
  7211. {
  7212. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7213. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7214. (RemoteDirView->DropTarget == NULL))
  7215. {
  7216. dwEffect = DROPEFFECT_None;
  7217. }
  7218. else
  7219. {
  7220. if (dwEffect == DROPEFFECT_Copy)
  7221. {
  7222. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7223. // currently we support copying always (at least via temporary directory);
  7224. // remove associated checks once this all proves stable and working
  7225. bool CopyCapable = true;
  7226. // if we do not support neither of operations, there's no discussion
  7227. if (!MoveCapable && !CopyCapable)
  7228. {
  7229. dwEffect = DROPEFFECT_None;
  7230. }
  7231. // when moving is disabled, we need to keep effect to "copy",
  7232. // which will be later interpretted as move (with slipped-in cursor)
  7233. else if (!WinConfiguration->DDAllowMoveInit && FLAGCLEAR(grfKeyState, MK_CONTROL))
  7234. {
  7235. // no-op, keep copy
  7236. }
  7237. else
  7238. {
  7239. // The default effect inside remote panel is move,
  7240. // unless we do not support it, but support copy
  7241. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7242. {
  7243. dwEffect = MoveCapable ? DROPEFFECT_Move : DROPEFFECT_Copy;
  7244. }
  7245. else
  7246. {
  7247. // with ctrl-down, we want copy unless it is not supported
  7248. dwEffect = CopyCapable ? DROPEFFECT_Copy : DROPEFFECT_None;
  7249. }
  7250. }
  7251. }
  7252. }
  7253. }
  7254. }
  7255. //---------------------------------------------------------------------------
  7256. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7257. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7258. {
  7259. if (FDDTotalSize >= 0)
  7260. {
  7261. if (File->IsDirectory)
  7262. {
  7263. FDDTotalSize = -1;
  7264. }
  7265. else
  7266. {
  7267. FDDTotalSize += File->Size;
  7268. }
  7269. }
  7270. DebugAssert(!FDragTempDir.IsEmpty());
  7271. // TODO: this is quite ineffective
  7272. // TODO: what if invalid character replacement is disabled?
  7273. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7274. UnicodeString TransferFileName;
  7275. if (Sender == RemoteDriveView)
  7276. {
  7277. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7278. }
  7279. else
  7280. {
  7281. TransferFileName = File->FileName;
  7282. }
  7283. FDDFileList->AddObject(TransferFileName, File);
  7284. }
  7285. //---------------------------------------------------------------------------
  7286. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7287. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7288. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7289. {
  7290. // sometimes we do not get DDEnd so the list is not released
  7291. SAFE_DESTROY(FDDFileList);
  7292. FDDFileList = new TStringList();
  7293. FDragTempDir = WinConfiguration->TemporaryDir();
  7294. FDDTotalSize = 0;
  7295. FDragDropSshTerminate.reset(NULL);
  7296. }
  7297. //---------------------------------------------------------------------------
  7298. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7299. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7300. HRESULT & Result)
  7301. {
  7302. if (Result == DRAGDROP_S_DROP)
  7303. {
  7304. try
  7305. {
  7306. GlobalDragImageList->HideDragImage();
  7307. try
  7308. {
  7309. RemoteFileControlDDTargetDrop();
  7310. }
  7311. catch(ESshTerminate & E)
  7312. {
  7313. FDragDropSshTerminate.reset(E.Clone());
  7314. }
  7315. }
  7316. catch (Exception &E)
  7317. {
  7318. // If downloading fails we need to cancel drag&drop, otherwise
  7319. // Explorer shows error
  7320. // But by the way exception probably never reach this point as
  7321. // it's catched on way.
  7322. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7323. Result = DRAGDROP_S_CANCEL;
  7324. ShowExtendedException(Terminal, &E);
  7325. }
  7326. }
  7327. }
  7328. //---------------------------------------------------------------------------
  7329. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7330. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7331. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7332. {
  7333. TFileMasks::TParams MaskParams;
  7334. MaskParams.Size = Size;
  7335. MaskParams.Modification = Modification;
  7336. // this does not re-parse the mask if it is the same as the last time
  7337. FDirViewMatchMask = Masks;
  7338. bool ImplicitMatch;
  7339. Matches =
  7340. // RecurseInclude parameter has no effect as the Path is empty
  7341. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7342. (AllowImplicitMatches || !ImplicitMatch);
  7343. }
  7344. //---------------------------------------------------------------------------
  7345. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7346. TObject * Sender, TListItem * Item, WORD & Indexes)
  7347. {
  7348. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7349. UnicodeString Ext;
  7350. if (DirView == RemoteDirView)
  7351. {
  7352. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7353. }
  7354. else
  7355. {
  7356. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7357. }
  7358. if (SameText(Ext, Configuration->PartialExt))
  7359. {
  7360. Indexes |= oiPartial;
  7361. }
  7362. }
  7363. //---------------------------------------------------------------------------
  7364. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7365. {
  7366. return
  7367. DirViewEnabled(osCurrent) &&
  7368. DirView(osCurrent)->CanPasteFromClipBoard();
  7369. }
  7370. //---------------------------------------------------------------------------
  7371. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7372. {
  7373. bool Result = false;
  7374. if (CanPasteToDirViewFromClipBoard())
  7375. {
  7376. Result = true;
  7377. }
  7378. else
  7379. {
  7380. UnicodeString ClipboardText;
  7381. if (NonEmptyTextFromClipboard(ClipboardText))
  7382. {
  7383. if (StoredSessions->IsUrl(ClipboardText))
  7384. {
  7385. Result = true;
  7386. }
  7387. else
  7388. {
  7389. Result = DirViewEnabled(osCurrent);
  7390. }
  7391. }
  7392. }
  7393. return Result;
  7394. }
  7395. //---------------------------------------------------------------------------
  7396. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7397. {
  7398. if (CanPasteToDirViewFromClipBoard())
  7399. {
  7400. DirView(osCurrent)->PasteFromClipBoard();
  7401. }
  7402. else
  7403. {
  7404. UnicodeString ClipboardText;
  7405. if (NonEmptyTextFromClipboard(ClipboardText))
  7406. {
  7407. if (StoredSessions->IsUrl(ClipboardText))
  7408. {
  7409. NewSession(false, ClipboardText);
  7410. }
  7411. else
  7412. {
  7413. DirView(osCurrent)->Path = ClipboardText;
  7414. }
  7415. }
  7416. }
  7417. }
  7418. //---------------------------------------------------------------------------
  7419. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7420. {
  7421. // TBD
  7422. }
  7423. //---------------------------------------------------------------------------
  7424. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7425. {
  7426. TCustomDirView * ADirView = DirView(Side);
  7427. ADirView->SelectAll(Mode);
  7428. ADirView->SetFocus();
  7429. }
  7430. //---------------------------------------------------------------------------
  7431. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7432. {
  7433. TCustomDirView * ADirView = DirView(Side);
  7434. TFileFilter Filter;
  7435. DefaultFileFilter(Filter);
  7436. if (DoSelectMaskDialog(ADirView, Select, &Filter, Configuration))
  7437. {
  7438. Configuration->Usage->Inc(L"MaskSelections");
  7439. ADirView->SelectFiles(Filter, Select);
  7440. ADirView->SetFocus();
  7441. }
  7442. }
  7443. //---------------------------------------------------------------------------
  7444. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7445. {
  7446. TCustomDirView * ADirView = DirView(Side);
  7447. ADirView->RestoreSelectedNames();
  7448. ADirView->SetFocus();
  7449. }
  7450. //---------------------------------------------------------------------------
  7451. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7452. {
  7453. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7454. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7455. {
  7456. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7457. UnicodeString Ext;
  7458. if (GetSide(osCurrent) == osRemote)
  7459. {
  7460. Ext = UnixExtractFileExt(FileName);
  7461. }
  7462. else
  7463. {
  7464. Ext = ExtractFileExt(FileName);
  7465. }
  7466. if (Ext.IsEmpty())
  7467. {
  7468. Ext = L".";
  7469. }
  7470. TFileFilter Filter;
  7471. Filter.Masks = FORMAT(L"*%s", (Ext));
  7472. Filter.Directories = false;
  7473. CurrentDirView->SelectFiles(Filter, Select);
  7474. }
  7475. }
  7476. //---------------------------------------------------------------------------
  7477. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7478. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7479. {
  7480. UnicodeString Result;
  7481. if ((Side == osRemote) && (Terminal == NULL))
  7482. {
  7483. // noop
  7484. }
  7485. else
  7486. {
  7487. Result =
  7488. FMTLOAD(FILE_INFO_FORMAT,
  7489. (FormatBytes(FileInfo.SelectedSize),
  7490. FormatBytes(FileInfo.FilesSize),
  7491. FormatNumber(FileInfo.SelectedCount),
  7492. FormatNumber(FileInfo.FilesCount)));
  7493. }
  7494. return Result;
  7495. }
  7496. //---------------------------------------------------------------------------
  7497. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7498. TTBXStatusPanel * Panel, TOperationSide Side)
  7499. {
  7500. if (Panel->Index == 1)
  7501. {
  7502. ToggleShowHiddenFiles();
  7503. }
  7504. else if (Panel->Index == 2)
  7505. {
  7506. Filter(Side);
  7507. }
  7508. }
  7509. //---------------------------------------------------------------------------
  7510. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7511. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7512. {
  7513. DebugAssert(!StatusBar->SimplePanel);
  7514. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7515. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7516. }
  7517. //---------------------------------------------------------------------------
  7518. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7519. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7520. {
  7521. DebugAssert(StatusBar->Panels->Count >= 3);
  7522. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7523. if (FileInfo.HiddenCount > 0)
  7524. {
  7525. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7526. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7527. }
  7528. else
  7529. {
  7530. HiddenFilesPanel->ViewPriority = 0;
  7531. // not really necessary, just to cleanup no-longer-valid data
  7532. HiddenFilesPanel->Caption = L"";
  7533. }
  7534. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7535. if (FileInfo.FilteredCount > 0)
  7536. {
  7537. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7538. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7539. }
  7540. else
  7541. {
  7542. FilteredFilesPanel->ViewPriority = 0;
  7543. // not really necessary, just to cleanup no-longer-valid data
  7544. FilteredFilesPanel->Caption = L"";
  7545. }
  7546. }
  7547. //---------------------------------------------------------------------------
  7548. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7549. TObject * /*Sender*/)
  7550. {
  7551. if (RemoteDirView->Enabled)
  7552. {
  7553. RemoteDirView->SetFocus();
  7554. }
  7555. }
  7556. //---------------------------------------------------------------------------
  7557. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7558. {
  7559. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7560. switch (Config.Show)
  7561. {
  7562. case qvShow:
  7563. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7564. {
  7565. Config.Show = qvHide;
  7566. }
  7567. else
  7568. {
  7569. Config.Show = Config.LastHideShow;
  7570. }
  7571. break;
  7572. case qvHideWhenEmpty:
  7573. if (ComponentVisible[fcQueueView])
  7574. {
  7575. Config.Show = qvHide;
  7576. }
  7577. else
  7578. {
  7579. Config.LastHideShow = Config.Show;
  7580. Config.Show = qvShow;
  7581. }
  7582. break;
  7583. case qvHide:
  7584. Config.LastHideShow = Config.Show;
  7585. Config.Show = qvShow;
  7586. break;
  7587. }
  7588. WinConfiguration->QueueView = Config;
  7589. }
  7590. //---------------------------------------------------------------------------
  7591. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7592. {
  7593. UnicodeString Result;
  7594. if (FTerminal != NULL)
  7595. {
  7596. switch (WinConfiguration->PathInCaption)
  7597. {
  7598. case picShort:
  7599. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7600. break;
  7601. case picFull:
  7602. Result = FTerminal->CurrentDirectory;
  7603. break;
  7604. }
  7605. }
  7606. return Result;
  7607. }
  7608. //---------------------------------------------------------------------------
  7609. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7610. {
  7611. TTerminalManager::Instance()->UpdateAppTitle();
  7612. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7613. // background to gray on Windows 7 (works on Windows 10).
  7614. // See also EnableControl
  7615. if (Showing)
  7616. {
  7617. bool HasTerminal = (Terminal != NULL);
  7618. if (HasTerminal)
  7619. {
  7620. // Update path when it changes
  7621. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7622. {
  7623. UpdateSessionTab(SessionsPageControl->ActivePage);
  7624. }
  7625. if (!RemoteDirView->Enabled)
  7626. {
  7627. RemoteDirView->Enabled = true;
  7628. if (FRemoteDirViewWasFocused)
  7629. {
  7630. ActiveControl = RemoteDirView;
  7631. }
  7632. }
  7633. RemoteDriveView->Enabled = true;
  7634. RemoteDirView->Color = (FSessionColor != 0 ? FSessionColor : clWindow);
  7635. RemoteDriveView->Color = RemoteDirView->Color;
  7636. }
  7637. else
  7638. {
  7639. if (RemoteDirView->Enabled)
  7640. {
  7641. // This is first called when the form is being constructed
  7642. // (not anymore due to Showing test above)
  7643. // but the false is overriden in the constructor later.
  7644. // An even later in TScpCommanderForm::DoShow()
  7645. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  7646. EnableControl(RemoteDirView, false);
  7647. }
  7648. EnableControl(RemoteDriveView, false);
  7649. }
  7650. EnableControl(QueueView3, HasTerminal && Terminal->IsCapable[fsBackgroundTransfers]);
  7651. QueueLabelUpdateStatus();
  7652. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  7653. }
  7654. }
  7655. //---------------------------------------------------------------------------
  7656. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  7657. {
  7658. UpdateControls();
  7659. }
  7660. //---------------------------------------------------------------------------
  7661. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  7662. TObject * Sender)
  7663. {
  7664. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7665. DebugAssert(DirView != NULL);
  7666. DoDirViewLoaded(DirView);
  7667. TransferPresetAutoSelect();
  7668. }
  7669. //---------------------------------------------------------------------------
  7670. void __fastcall TCustomScpExplorerForm::StartUpdates()
  7671. {
  7672. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  7673. // first run after installation
  7674. if (double(Updates.LastCheck) == 0)
  7675. {
  7676. // make sure next time there will be an update (if enabled)
  7677. Updates.LastCheck = TDateTime(1);
  7678. WinConfiguration->Updates = Updates;
  7679. }
  7680. else if ((double(Updates.Period) > 0) &&
  7681. (Now() - Updates.LastCheck >= Updates.Period))
  7682. {
  7683. TThreadMethod OnUpdatesChecked = NULL;
  7684. if (!IsUWP())
  7685. {
  7686. OnUpdatesChecked = UpdatesChecked;
  7687. }
  7688. StartUpdateThread(OnUpdatesChecked);
  7689. }
  7690. }
  7691. //---------------------------------------------------------------------------
  7692. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  7693. {
  7694. UnicodeString Message;
  7695. bool New;
  7696. TQueryType Type;
  7697. GetUpdatesMessage(Message, New, Type, false);
  7698. if (!Message.IsEmpty())
  7699. {
  7700. if (New)
  7701. {
  7702. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  7703. }
  7704. if (!New && (Type != qtWarning))
  7705. {
  7706. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  7707. }
  7708. else
  7709. {
  7710. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  7711. }
  7712. if (New)
  7713. {
  7714. Configuration->Usage->Inc(L"UpdateNotifications");
  7715. }
  7716. }
  7717. }
  7718. //---------------------------------------------------------------------------
  7719. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  7720. {
  7721. RestoreApp();
  7722. bool CanDisplay = !NonVisualDataModule->Busy;
  7723. if (!CanDisplay && (Screen->ActiveForm != NULL))
  7724. {
  7725. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  7726. }
  7727. if (CanDisplay)
  7728. {
  7729. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  7730. CheckForUpdates(true);
  7731. }
  7732. else
  7733. {
  7734. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  7735. }
  7736. }
  7737. //---------------------------------------------------------------------------
  7738. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  7739. TCopyParamRuleData & Data)
  7740. {
  7741. DebugAssert(Terminal != NULL);
  7742. Data.HostName = Terminal->SessionData->HostNameExpanded;
  7743. Data.UserName = Terminal->SessionData->UserNameExpanded;
  7744. Data.RemoteDirectory = RemoteDirView->PathName;
  7745. }
  7746. //---------------------------------------------------------------------------
  7747. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  7748. {
  7749. // Terminal can be null when we are changing local directory implicitly,
  7750. // because it has been deleted, while no session is connected
  7751. // (Login dialog is open)
  7752. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  7753. {
  7754. TCopyParamRuleData Data;
  7755. GetTransferPresetAutoSelectData(Data);
  7756. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  7757. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  7758. if (CopyParamIndex < 0)
  7759. {
  7760. // there is no preset that matches autoselection
  7761. // set preset that we consider "default"
  7762. FCopyParamAutoSelected = L""; // forget last autoselected preset
  7763. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  7764. }
  7765. else
  7766. {
  7767. // there is preset matching autoselection
  7768. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  7769. if (CopyParamName == FCopyParamAutoSelected)
  7770. {
  7771. // autoselected the same preset as the last time
  7772. // make no change (i.e. preserve custom user preset, if any)
  7773. }
  7774. else
  7775. {
  7776. // autoselected the different preset then the last time (or there
  7777. // was default preset set)
  7778. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  7779. GUIConfiguration->CopyParamCurrent = CopyParamName;
  7780. }
  7781. }
  7782. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  7783. {
  7784. if (CopyParamIndex >= 0)
  7785. {
  7786. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  7787. }
  7788. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  7789. try
  7790. {
  7791. int Fmt =
  7792. (CopyParamIndex < 0) ?
  7793. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  7794. COPY_PARAM_AUTOSELECTED;
  7795. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  7796. Data->Message = MainInstructions(Message);
  7797. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  7798. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  7799. CopyParamAttrs);
  7800. if (CopyParamIndex >= 0)
  7801. {
  7802. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  7803. Info = FORMAT(L"%s\n \n%s", (Info,
  7804. FMTLOAD(COPY_PARAM_RULE,
  7805. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  7806. }
  7807. Data->Message += L"\n\n" + Info;
  7808. if (WinConfiguration->CopyParamAutoSelectNotice)
  7809. {
  7810. TransferPresetNoteMessage(Data, true);
  7811. }
  7812. else
  7813. {
  7814. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  7815. Data = NULL; // ownership passed
  7816. }
  7817. }
  7818. __finally
  7819. {
  7820. delete Data;
  7821. }
  7822. }
  7823. }
  7824. }
  7825. //---------------------------------------------------------------------------
  7826. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  7827. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  7828. {
  7829. DebugAssert(NoteData != NULL);
  7830. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  7831. TQueryButtonAlias Aliases[1];
  7832. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  7833. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  7834. Params.Aliases = Aliases;
  7835. Params.AliasesCount = LENOF(Aliases);
  7836. unsigned int Result =
  7837. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  7838. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  7839. switch (Result)
  7840. {
  7841. case qaNeverAskAgain:
  7842. DebugAssert(AllowNeverAskAgain);
  7843. WinConfiguration->CopyParamAutoSelectNotice = false;
  7844. break;
  7845. case qaIgnore:
  7846. PreferencesDialog(pmPresets);
  7847. break;
  7848. }
  7849. }
  7850. //---------------------------------------------------------------------------
  7851. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  7852. {
  7853. // as of now useless, as this is used for notes only, never for balloons, ...
  7854. RestoreApp();
  7855. // .. and we should never be busy here
  7856. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  7857. {
  7858. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  7859. }
  7860. }
  7861. //---------------------------------------------------------------------------
  7862. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  7863. TPreferencesMode APreferencesMode)
  7864. {
  7865. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  7866. TCopyParamRuleData Data;
  7867. if (Terminal != NULL)
  7868. {
  7869. PreferencesData.reset(new TPreferencesDialogData());
  7870. GetTransferPresetAutoSelectData(Data);
  7871. PreferencesData->CopyParamRuleData = &Data;
  7872. }
  7873. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  7874. }
  7875. //---------------------------------------------------------------------------
  7876. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  7877. const TCustomCommandType & Command)
  7878. {
  7879. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  7880. {
  7881. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  7882. }
  7883. }
  7884. //---------------------------------------------------------------------------
  7885. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  7886. {
  7887. bool RemoteAllowed = CustomCommandRemoteAllowed();
  7888. TCustomCommandType Command;
  7889. // make sure we use local custom command when remote are not supported
  7890. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  7891. {
  7892. Command = FLastCustomCommand;
  7893. }
  7894. else
  7895. {
  7896. Command.Params = Command.Params | ccLocal;
  7897. }
  7898. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  7899. FEditingFocusedAdHocCommand = OnFocused;
  7900. bool LocalSide = (FCurrentSide == osLocal);
  7901. int Options =
  7902. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  7903. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  7904. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  7905. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  7906. {
  7907. FLastCustomCommand = Command;
  7908. UpdateCustomCommandsToolbar();
  7909. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  7910. }
  7911. }
  7912. //---------------------------------------------------------------------------
  7913. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  7914. {
  7915. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  7916. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  7917. DebugAssert(State > 0);
  7918. if (State <= 0)
  7919. {
  7920. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  7921. }
  7922. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  7923. }
  7924. //---------------------------------------------------------------------------
  7925. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  7926. TCustomCommandType & Command, int & State)
  7927. {
  7928. bool Result = !FLastCustomCommand.Command.IsEmpty();
  7929. if (Result)
  7930. {
  7931. Command = FLastCustomCommand;
  7932. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  7933. }
  7934. return Result;
  7935. }
  7936. //---------------------------------------------------------------------------
  7937. void __fastcall TCustomScpExplorerForm::BeforeAction()
  7938. {
  7939. if (RemoteDirView->ItemFocused != NULL)
  7940. {
  7941. RemoteDirView->ItemFocused->CancelEdit();
  7942. }
  7943. }
  7944. //---------------------------------------------------------------------------
  7945. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  7946. {
  7947. DebugAssert(ComponentVisible[Component]);
  7948. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  7949. }
  7950. //---------------------------------------------------------------------------
  7951. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  7952. {
  7953. // when queue panel is resized here directly, the status bar is stretched
  7954. // over whole space the panel occupied
  7955. PostComponentHide(fcQueueView);
  7956. }
  7957. //---------------------------------------------------------------------------
  7958. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  7959. {
  7960. TMessage * M = static_cast<TMessage*>(Message);
  7961. switch (M->Msg)
  7962. {
  7963. case CM_DIALOGCHAR:
  7964. CMDialogChar(*M);
  7965. break;
  7966. case WM_APPCOMMAND:
  7967. WMAppCommand(*M);
  7968. break;
  7969. case WM_SYSCOMMAND:
  7970. WMSysCommand(*M);
  7971. break;
  7972. case WM_QUERYENDSESSION:
  7973. WMQueryEndSession(*M);
  7974. break;
  7975. case WM_ENDSESSION:
  7976. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  7977. break;
  7978. #ifdef _DEBUG
  7979. case WM_WINDOWPOSCHANGED:
  7980. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  7981. break;
  7982. case CM_CUSTOMSTYLECHANGED:
  7983. TForm::Dispatch(Message);
  7984. break;
  7985. #endif
  7986. case WM_COMPONENT_HIDE:
  7987. {
  7988. Byte Component = static_cast<Byte>(M->WParam);
  7989. // sanity check
  7990. if (ComponentVisible[Component])
  7991. {
  7992. // special treatment
  7993. if (Component == fcQueueView)
  7994. {
  7995. ToggleQueueVisibility();
  7996. DebugAssert(!ComponentVisible[fcQueueView]);
  7997. }
  7998. else
  7999. {
  8000. ComponentVisible[Component] = false;
  8001. }
  8002. }
  8003. }
  8004. break;
  8005. case WM_COPYDATA:
  8006. WMCopyData(*M);
  8007. break;
  8008. case WM_MANAGES_CAPTION:
  8009. // caption managed in TTerminalManager::UpdateAppTitle()
  8010. M->Result = 1;
  8011. break;
  8012. case WM_WANTS_MOUSEWHEEL:
  8013. M->Result = 1;
  8014. break;
  8015. case CM_SHOWINGCHANGED:
  8016. CMShowingChanged(*M);
  8017. break;
  8018. case WM_CLOSE:
  8019. WMClose(*M);
  8020. break;
  8021. case WM_DPICHANGED:
  8022. WMDpiChanged(*M);
  8023. break;
  8024. case CM_DPICHANGED:
  8025. CMDpiChanged(*M);
  8026. break;
  8027. default:
  8028. TForm::Dispatch(Message);
  8029. break;
  8030. }
  8031. }
  8032. //---------------------------------------------------------------------------
  8033. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8034. {
  8035. // noop
  8036. }
  8037. //---------------------------------------------------------------------------
  8038. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8039. {
  8040. TForm::Dispatch(&Message);
  8041. // regenerate session images
  8042. DoTerminalListChanged(true);
  8043. UpdateImages();
  8044. }
  8045. //---------------------------------------------------------------------------
  8046. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8047. {
  8048. TForm::Dispatch(&Message);
  8049. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8050. UpdatePixelsPerInchMainWindowCounter();
  8051. }
  8052. //---------------------------------------------------------------------------
  8053. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8054. {
  8055. // Cannot close window while we are busy.
  8056. // We cannot test this in FormCloseQuery as that is called also from
  8057. // Close(), which is called by CloseApplicationAction. So we can be busy
  8058. // there already even, when it is legitimate to close the application.
  8059. // Possibly a better place to handle this would be WMSysCommand.
  8060. if (NonVisualDataModule->Busy)
  8061. {
  8062. Message.Result = 1;
  8063. }
  8064. else
  8065. {
  8066. TForm::Dispatch(&Message);
  8067. }
  8068. }
  8069. //---------------------------------------------------------------------------
  8070. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8071. {
  8072. TForm::Dispatch(&Message);
  8073. if (Showing && (Terminal == NULL))
  8074. {
  8075. // When we are starting minimized (i.e. from an installer),
  8076. // postpone showing Login dialog until we get restored.
  8077. // Otherwise the Login dialog (and Authentication window) show restored
  8078. // over invidible (minimized) main window.
  8079. if (WindowState == wsMinimized)
  8080. {
  8081. FNeedSession = true;
  8082. }
  8083. else
  8084. {
  8085. // This happens before application ever goes idle, so the toolbars would
  8086. // stay enabled (initial state) until the Login dialog is dismissed.
  8087. UpdateActions();
  8088. NonVisualDataModule->StartBusy();
  8089. try
  8090. {
  8091. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8092. // otherwise the dialog does not receive focus.
  8093. // With Commander interface the ProcessMessages is called already
  8094. // by TDriveView, but with Explorer interface, we need to call it explicily
  8095. Application->ProcessMessages();
  8096. // do not reload sessions, they have been loaded just now (optimization)
  8097. NeedSession(false);
  8098. }
  8099. __finally
  8100. {
  8101. NonVisualDataModule->EndBusy();
  8102. }
  8103. }
  8104. }
  8105. }
  8106. //---------------------------------------------------------------------------
  8107. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8108. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8109. int & MaxHeight)
  8110. {
  8111. // workaround for bug in TWinControl.CalcConstraints
  8112. // Check for empty rect (restore from iconic state) is done there only after
  8113. // call to AdjustClientRect, which enlarges the rect (for forms).
  8114. TRect R = GetClientRect();
  8115. // when restoring from iconic state, place no restrictions
  8116. if (IsRectEmpty(R))
  8117. {
  8118. MinWidth = 0;
  8119. MinHeight = 0;
  8120. MaxWidth = 32000;
  8121. MaxHeight = 32000;
  8122. }
  8123. }
  8124. //---------------------------------------------------------------------------
  8125. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8126. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8127. {
  8128. // terminal can be already closed (e.g. dropped connection)
  8129. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8130. {
  8131. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8132. try
  8133. {
  8134. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8135. }
  8136. catch(...)
  8137. {
  8138. if (!Terminal->Active)
  8139. {
  8140. Close = true;
  8141. }
  8142. throw;
  8143. }
  8144. }
  8145. }
  8146. //---------------------------------------------------------------------------
  8147. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8148. {
  8149. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8150. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8151. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8152. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8153. {
  8154. OnGetSpaceAvailable = GetSpaceAvailable;
  8155. }
  8156. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8157. OnGetSpaceAvailable);
  8158. }
  8159. //---------------------------------------------------------------------------
  8160. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8161. {
  8162. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8163. Terminal->FillSessionDataForCode(Data.get());
  8164. return Data.release();
  8165. }
  8166. //---------------------------------------------------------------------------
  8167. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8168. {
  8169. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8170. DoGenerateUrlDialog(Data.get(), Paths);
  8171. }
  8172. //---------------------------------------------------------------------------
  8173. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8174. {
  8175. GenerateUrl(NULL);
  8176. }
  8177. //---------------------------------------------------------------------------
  8178. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8179. {
  8180. std::unique_ptr<TStrings> Paths(new TStringList());
  8181. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8182. GenerateUrl(Paths.get());
  8183. }
  8184. //---------------------------------------------------------------------------
  8185. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8186. {
  8187. if (value != FSessionColor)
  8188. {
  8189. FSessionColor = value;
  8190. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8191. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8192. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8193. DebugAssert(ColorItem != NULL);
  8194. ColorItem->Color = C;
  8195. NonVisualDataModule->ColorMenuItem->Color = C;
  8196. // Is null when called from LastTerminalClosed
  8197. if (Terminal != NULL)
  8198. {
  8199. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8200. }
  8201. UpdateControls();
  8202. }
  8203. }
  8204. //---------------------------------------------------------------------------
  8205. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8206. {
  8207. bool Result = FNoteTimer->Enabled;
  8208. if (Result)
  8209. {
  8210. // cannot cancel note too early
  8211. bool NotEarly =
  8212. (Now() - FNoteShown >
  8213. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8214. if (Force || NotEarly)
  8215. {
  8216. FNoteTimer->Enabled = false;
  8217. FNote = L"";
  8218. SAFE_DESTROY(FNoteData);
  8219. FOnNoteClick = NULL;
  8220. FNoteHints = FNotes->Text;
  8221. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8222. UpdateStatusBar();
  8223. }
  8224. }
  8225. return Result;
  8226. }
  8227. //---------------------------------------------------------------------------
  8228. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8229. {
  8230. DebugAssert(FNoteTimer->Enabled);
  8231. CancelNote(true);
  8232. }
  8233. //---------------------------------------------------------------------------
  8234. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8235. {
  8236. int P = Note.Pos(L"\n");
  8237. if (P > 0)
  8238. {
  8239. Note.SetLength(P - 1);
  8240. }
  8241. FNotes->Add(FORMAT(L"[%s] %s",
  8242. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8243. while (FNotes->Count > 10)
  8244. {
  8245. FNotes->Delete(0);
  8246. }
  8247. if (UpdateNow)
  8248. {
  8249. FNoteHints = FNotes->Text;
  8250. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8251. UpdateStatusBar();
  8252. }
  8253. }
  8254. //---------------------------------------------------------------------------
  8255. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8256. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8257. {
  8258. int P = Note.Pos(L"\n");
  8259. if (P > 0)
  8260. {
  8261. Note.SetLength(P - 1);
  8262. }
  8263. FNoteHints = FNotes->Text;
  8264. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8265. FNote = Note;
  8266. SAFE_DESTROY(FNoteData);
  8267. FNoteData = NoteData;
  8268. FOnNoteClick = OnNoteClick;
  8269. AddNote(Note, false);
  8270. UpdateStatusBar();
  8271. FNoteShown = Now();
  8272. FNoteTimer->Enabled = false;
  8273. if (Seconds == 0)
  8274. {
  8275. Seconds = WinConfiguration->NotificationsTimeout;
  8276. }
  8277. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8278. FNoteTimer->Enabled = true;
  8279. }
  8280. //---------------------------------------------------------------------------
  8281. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8282. {
  8283. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8284. }
  8285. //---------------------------------------------------------------------------
  8286. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8287. {
  8288. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8289. {
  8290. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8291. }
  8292. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8293. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8294. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8295. 0, NULL, NULL);
  8296. }
  8297. //---------------------------------------------------------------------------
  8298. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8299. {
  8300. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8301. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8302. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8303. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8304. }
  8305. //---------------------------------------------------------------------------
  8306. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8307. {
  8308. WinConfiguration->FormatSizeBytes = Style;
  8309. }
  8310. //---------------------------------------------------------------------------
  8311. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8312. {
  8313. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8314. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8315. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8316. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8317. }
  8318. //---------------------------------------------------------------------------
  8319. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8320. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8321. {
  8322. if (Panel->Index == 0)
  8323. {
  8324. if (FOnNoteClick != NULL)
  8325. {
  8326. // prevent the user data from being freed by possible call
  8327. // to CancelNote or PostNote during call to OnNoteClick
  8328. std::unique_ptr<TObject> NoteData(FNoteData);
  8329. TNotifyEvent OnNoteClick = FOnNoteClick;
  8330. FNoteData = NULL;
  8331. // need to cancel the note as we are going to delete its user data
  8332. CancelNote(true);
  8333. OnNoteClick(NoteData.get());
  8334. }
  8335. }
  8336. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8337. {
  8338. FileSystemInfo();
  8339. }
  8340. }
  8341. //---------------------------------------------------------------------------
  8342. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8343. {
  8344. // workaround:
  8345. // 1) for unknown reason, disabling window, while minimized,
  8346. // prevents it from restoring, even if it was enabled again meanwhile
  8347. // 2) when disabling the main window, while another has focus
  8348. // minimize is no longer possible
  8349. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8350. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8351. // Shouldn't we use IsApplicationMinimized() here?
  8352. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8353. {
  8354. Enabled = false;
  8355. }
  8356. FLockLevel++;
  8357. }
  8358. //---------------------------------------------------------------------------
  8359. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8360. {
  8361. DebugAssert(FLockLevel > 0);
  8362. FLockLevel--;
  8363. if (FLockLevel == 0)
  8364. {
  8365. DebugAssert(FLockSuspendLevel == 0);
  8366. Enabled = true;
  8367. }
  8368. }
  8369. //---------------------------------------------------------------------------
  8370. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8371. {
  8372. // We need to make sure that window is enabled when the last modal window closes
  8373. // otherwise focus is not restored correctly.
  8374. // So we re-enable the window when modal window opens and
  8375. // disable it back after is closes
  8376. if (FLockLevel > 0)
  8377. {
  8378. // while we have nesting counter, we know that we never be called
  8379. // recursivelly as Application->OnModalBegin is called only
  8380. // for the top-level modal window
  8381. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8382. {
  8383. // won't be disabled when conditions in LockWindow() were not satisfied
  8384. FDisabledOnLockSuspend = !Enabled;
  8385. // When minimized to tray (or actually when set to SW_HIDE),
  8386. // setting Enabled makes the window focusable even when there's
  8387. // modal window over it
  8388. if (!FTrayIcon->Visible)
  8389. {
  8390. Enabled = true;
  8391. }
  8392. }
  8393. FLockSuspendLevel++;
  8394. }
  8395. }
  8396. //---------------------------------------------------------------------------
  8397. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8398. {
  8399. if (FLockSuspendLevel > 0)
  8400. {
  8401. DebugAssert(FLockLevel > 0);
  8402. FLockSuspendLevel--;
  8403. // see comment in SuspendWindowLock
  8404. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8405. {
  8406. // Note that window can be enabled here, when we were minized to tray when
  8407. // was SuspendWindowLock() called.
  8408. // We should possibly do the same check as in LockWindow(),
  8409. // if it is ever possible that the consitions change between
  8410. // SuspendWindowLock() and ResumeWindowLock()
  8411. Enabled = !FDisabledOnLockSuspend;
  8412. }
  8413. }
  8414. }
  8415. //---------------------------------------------------------------------------
  8416. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8417. {
  8418. if (!TextOnly)
  8419. {
  8420. TTBXComboBoxItem * RemotePathComboBox =
  8421. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8422. TStrings * Items = RemotePathComboBox->Strings;
  8423. Items->BeginUpdate();
  8424. try
  8425. {
  8426. Items->Clear();
  8427. if (Terminal != NULL)
  8428. {
  8429. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8430. while (!IsUnixRootPath(APath))
  8431. {
  8432. int P = APath.LastDelimiter(L'/');
  8433. DebugAssert(P >= 0);
  8434. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8435. APath.SetLength(P - 1);
  8436. }
  8437. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8438. }
  8439. }
  8440. __finally
  8441. {
  8442. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8443. // Setting ItemIndex to -1 does not reset its text
  8444. if (Items->Count == 0)
  8445. {
  8446. RemotePathComboBox->Text = L"";
  8447. }
  8448. Items->EndUpdate();
  8449. }
  8450. }
  8451. }
  8452. //---------------------------------------------------------------------------
  8453. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8454. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8455. int & ImageIndex)
  8456. {
  8457. if (AIndex < 0)
  8458. {
  8459. AIndex = Sender->ItemIndex;
  8460. }
  8461. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8462. }
  8463. //---------------------------------------------------------------------------
  8464. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8465. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8466. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8467. {
  8468. ARect.Left += (10 * AIndex);
  8469. }
  8470. //---------------------------------------------------------------------------
  8471. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8472. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8473. {
  8474. AWidth += (10 * AIndex);
  8475. }
  8476. //---------------------------------------------------------------------------
  8477. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8478. TObject * Sender)
  8479. {
  8480. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8481. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8482. int Index = RemotePathComboBox->ItemIndex;
  8483. while (Index < RemotePathComboBox->Strings->Count - 1)
  8484. {
  8485. APath = UnixExtractFileDir(APath);
  8486. Index++;
  8487. }
  8488. // VanDyke style paths
  8489. if (APath.IsEmpty())
  8490. {
  8491. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8492. APath = ROOTDIRECTORY;
  8493. }
  8494. if (RemoteDirView->Path != APath)
  8495. {
  8496. RemoteDirView->Path = APath;
  8497. }
  8498. }
  8499. //---------------------------------------------------------------------------
  8500. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8501. {
  8502. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8503. DebugUsedParam(Sender);
  8504. UpdateRemotePathComboBox(true);
  8505. }
  8506. //---------------------------------------------------------------------------
  8507. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8508. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8509. {
  8510. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8511. DebugAssert(DirView != NULL);
  8512. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8513. {
  8514. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8515. // OnEditing is called also from TCustomListView::CanEdit
  8516. if (Edit != NULL)
  8517. {
  8518. EditSelectBaseName(Edit);
  8519. }
  8520. }
  8521. }
  8522. //---------------------------------------------------------------------------
  8523. //---------------------------------------------------------------------------
  8524. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8525. {
  8526. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8527. Result->AutoDetectDnD = false;
  8528. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8529. Result->RenderDataOn = rdoEnterAndDropSync;
  8530. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8531. return Result;
  8532. }
  8533. //---------------------------------------------------------------------------
  8534. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8535. {
  8536. TForm::CreateWnd();
  8537. if (FSessionsDragDropFilesEx == NULL)
  8538. {
  8539. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8540. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8541. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8542. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8543. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8544. }
  8545. if (FQueueDragDropFilesEx == NULL)
  8546. {
  8547. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8548. // No need to set OnDragOver as we do not have any restrictions
  8549. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8550. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8551. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8552. }
  8553. }
  8554. //---------------------------------------------------------------------------
  8555. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8556. {
  8557. TForm::DestroyWnd();
  8558. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8559. FQueueDragDropFilesEx->DragDropControl = NULL;
  8560. }
  8561. //---------------------------------------------------------------------------
  8562. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8563. {
  8564. SideEnter(FCurrentSide);
  8565. FEverShown = true;
  8566. }
  8567. //---------------------------------------------------------------------------
  8568. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8569. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8570. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8571. {
  8572. if (!NonVisualDataModule->Busy)
  8573. {
  8574. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8575. Configuration->Usage->Inc(L"FileFinds");
  8576. LockWindow(true);
  8577. NonVisualDataModule->StartBusy();
  8578. WinConfiguration->LockedInterface = true;
  8579. try
  8580. {
  8581. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8582. }
  8583. __finally
  8584. {
  8585. WinConfiguration->LockedInterface = false;
  8586. NonVisualDataModule->EndBusy();
  8587. UnlockWindow();
  8588. }
  8589. }
  8590. }
  8591. //---------------------------------------------------------------------------
  8592. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8593. {
  8594. if (!NonVisualDataModule->Busy)
  8595. {
  8596. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8597. SetFocus();
  8598. RemoteDirView->Path = UnixExtractFilePath(Path);
  8599. TListItem * Item = RemoteDirView->FindFileItem(UnixExtractFileName(Path));
  8600. if (Item != NULL)
  8601. {
  8602. RemoteDirView->ItemFocused = Item;
  8603. RemoteDirView->ItemFocused->MakeVisible(false);
  8604. RemoteDirView->SetFocus();
  8605. }
  8606. }
  8607. }
  8608. //---------------------------------------------------------------------------
  8609. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  8610. {
  8611. bool Result = !NonVisualDataModule->Busy;
  8612. if (Result)
  8613. {
  8614. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8615. }
  8616. return Result;
  8617. }
  8618. //---------------------------------------------------------------------------
  8619. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  8620. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8621. {
  8622. if (CanOperateOnFoundFiles(ATerminal))
  8623. {
  8624. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  8625. FOnFileOperationFinished = OnFileOperationFinished;
  8626. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  8627. }
  8628. }
  8629. //---------------------------------------------------------------------------
  8630. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  8631. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8632. {
  8633. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  8634. }
  8635. //---------------------------------------------------------------------------
  8636. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  8637. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8638. {
  8639. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  8640. }
  8641. //---------------------------------------------------------------------------
  8642. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  8643. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8644. {
  8645. if (CanOperateOnFoundFiles(ATerminal))
  8646. {
  8647. for (int Index = 0; Index < FileList->Count; Index++)
  8648. {
  8649. UnicodeString FileName = FileList->Strings[Index];
  8650. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  8651. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  8652. OnFileOperationFinished(FileName, true);
  8653. }
  8654. }
  8655. }
  8656. //---------------------------------------------------------------------------
  8657. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  8658. {
  8659. FFileFindTerminal = Terminal;
  8660. ShowFileFindDialog(
  8661. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  8662. DoDownloadFoundFiles, DoEditFoundFiles);
  8663. }
  8664. //---------------------------------------------------------------------------
  8665. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  8666. {
  8667. FTaskbarList = TaskbarList;
  8668. }
  8669. //---------------------------------------------------------------------------
  8670. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  8671. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  8672. {
  8673. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8674. if (Index >= 0)
  8675. {
  8676. if (Button == mbLeft)
  8677. {
  8678. // "Mouse down" is raised only after tab is switched.
  8679. // If switching tab (switching session) takes long enough for user
  8680. // to actually release the button, "mouse down" is still raised,
  8681. // but we do not get "mouse up" event, so dragging is not cancelled,
  8682. // prevent that by not beginning dragging in the first place.
  8683. if (IsKeyPressed(VK_LBUTTON))
  8684. {
  8685. // when user clicks the "+", we get mouse down only after the session
  8686. // is closed, when new session tab is already on X:Y, so dragging
  8687. // starts, prevent that
  8688. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  8689. {
  8690. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8691. if (Terminal != NULL)
  8692. {
  8693. SessionsPageControl->BeginDrag(false);
  8694. }
  8695. }
  8696. }
  8697. }
  8698. else if (Button == mbMiddle)
  8699. {
  8700. // ignore middle-click for "New session tab"
  8701. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8702. if (Terminal != NULL)
  8703. {
  8704. SessionsPageControl->ActivePageIndex = Index;
  8705. // Switch to session tab (has to be session tab, due to previous check)
  8706. if (DebugAlwaysTrue(SessionTabSwitched()))
  8707. {
  8708. CloseSession();
  8709. }
  8710. }
  8711. }
  8712. }
  8713. }
  8714. //---------------------------------------------------------------------------
  8715. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  8716. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  8717. {
  8718. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8719. // do not allow dropping on the "+" tab
  8720. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8721. if ((TargetTerminal != NULL) &&
  8722. (SessionsPageControl->ActivePage->PageIndex != Index))
  8723. {
  8724. Configuration->Usage->Inc(L"SessionTabMoves");
  8725. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  8726. // but we want to actually prevent that to avoid flicker
  8727. SessionsPageControl->ActivePage->PageIndex = Index;
  8728. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  8729. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  8730. }
  8731. }
  8732. //---------------------------------------------------------------------------
  8733. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  8734. TObject * Sender, TObject * Source, int X, int Y,
  8735. TDragState /*State*/, bool & Accept)
  8736. {
  8737. Accept = (Sender == Source);
  8738. if (Accept)
  8739. {
  8740. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8741. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8742. // do not allow dragging to the "+" tab
  8743. Accept = (Terminal != NULL);
  8744. }
  8745. }
  8746. //---------------------------------------------------------------------------
  8747. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  8748. const TPoint & Point, int & Effect)
  8749. {
  8750. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8751. if (Index < 0)
  8752. {
  8753. Effect = DROPEFFECT_None;
  8754. }
  8755. else
  8756. {
  8757. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8758. // do not allow dropping on the "+" tab
  8759. if (TargetTerminal == NULL)
  8760. {
  8761. Effect = DROPEFFECT_None;
  8762. }
  8763. }
  8764. }
  8765. //---------------------------------------------------------------------------
  8766. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  8767. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  8768. {
  8769. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8770. // do not allow dropping on the "+" tab
  8771. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8772. if (TargetTerminal != NULL)
  8773. {
  8774. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  8775. if (!IsFileControl(DropSourceControl, osRemote))
  8776. {
  8777. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  8778. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  8779. // Why don't we use Terminal->CurrentDirectory directly?
  8780. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory, false);
  8781. }
  8782. }
  8783. }
  8784. //---------------------------------------------------------------------------
  8785. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  8786. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  8787. {
  8788. // Downloads are handled in RemoteFileControlDDEnd
  8789. if (!IsFileControl(DropSourceControl, osRemote))
  8790. {
  8791. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  8792. Terminal->CurrentDirectory,
  8793. // force queue
  8794. true);
  8795. }
  8796. }
  8797. //---------------------------------------------------------------------------
  8798. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  8799. {
  8800. FShowing = false;
  8801. // Do not save empty workspace
  8802. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  8803. {
  8804. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  8805. UnicodeString Name = WorkspaceName();
  8806. DoSaveWorkspace(
  8807. Name, DataList.get(),
  8808. !Configuration->DisablePasswordStoring &&
  8809. WinConfiguration->AutoSaveWorkspacePasswords, false);
  8810. WinConfiguration->LastStoredSession = Name;
  8811. }
  8812. }
  8813. //---------------------------------------------------------------------------
  8814. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  8815. {
  8816. TManagedTerminal * ManagedTerminal =
  8817. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  8818. if (ManagedTerminal != NULL)
  8819. {
  8820. ManagedTerminal->DirectoryLoaded = Now();
  8821. }
  8822. }
  8823. //---------------------------------------------------------------------------
  8824. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  8825. TListItem * /*Item*/, bool /*Selected*/)
  8826. {
  8827. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8828. switch (DirView->LastSelectMethod)
  8829. {
  8830. case smKeyboard:
  8831. Configuration->Usage->Inc(L"KeyboardSelections");
  8832. break;
  8833. case smMouse:
  8834. Configuration->Usage->Inc(L"MouseSelections");
  8835. break;
  8836. }
  8837. }
  8838. //---------------------------------------------------------------------------
  8839. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  8840. {
  8841. if (Color != 0)
  8842. {
  8843. AddSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  8844. return FSessionColors->Count - 1;
  8845. }
  8846. else
  8847. {
  8848. return FSessionTabImageIndex;
  8849. }
  8850. }
  8851. //---------------------------------------------------------------------------
  8852. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  8853. {
  8854. TPngImageCollectionItem * Item =
  8855. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  8856. FSessionColors->AddPng(Item->PngImage);
  8857. return FSessionColors->Count - 1;
  8858. }
  8859. //---------------------------------------------------------------------------
  8860. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  8861. {
  8862. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  8863. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  8864. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  8865. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  8866. }
  8867. //---------------------------------------------------------------------------
  8868. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  8869. {
  8870. for (int Index = 0; Index < Control->ControlCount; Index++)
  8871. {
  8872. TControl * AControl = Control->Controls[Index];
  8873. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  8874. if (WinControl != NULL)
  8875. {
  8876. CollectItemsWithTextDisplayMode(WinControl);
  8877. }
  8878. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  8879. if (Toolbar != NULL)
  8880. {
  8881. // we care for top-level items only
  8882. TTBCustomItem * Items = Toolbar->Items;
  8883. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  8884. {
  8885. TTBCustomItem * Item = Items->Items[ItemIndex];
  8886. if (((Item->DisplayMode == nbdmImageAndText) ||
  8887. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  8888. EligibleForImageDisplayMode(Item))
  8889. {
  8890. FItemsWithTextDisplayMode.insert(Item);
  8891. }
  8892. }
  8893. }
  8894. }
  8895. }
  8896. //---------------------------------------------------------------------------
  8897. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  8898. {
  8899. return true;
  8900. }
  8901. //---------------------------------------------------------------------------
  8902. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  8903. {
  8904. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  8905. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  8906. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  8907. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  8908. bool Result = true;
  8909. while (Result && (i != FItemsWithTextDisplayMode.end()))
  8910. {
  8911. TTBCustomItem * Item = *i;
  8912. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  8913. if (Label != NULL)
  8914. {
  8915. // optimization
  8916. if (Label->Visible == SelectiveToolbarText)
  8917. {
  8918. Result = false;
  8919. }
  8920. else
  8921. {
  8922. Label->Visible = SelectiveToolbarText;
  8923. }
  8924. }
  8925. else
  8926. {
  8927. // optimization
  8928. if (Item->DisplayMode == DisplayMode)
  8929. {
  8930. Result = false;
  8931. }
  8932. else
  8933. {
  8934. Item->DisplayMode = DisplayMode;
  8935. }
  8936. }
  8937. ++i;
  8938. }
  8939. if (Result)
  8940. {
  8941. UpdateNewSessionTab();
  8942. }
  8943. return Result;
  8944. }
  8945. //---------------------------------------------------------------------------
  8946. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  8947. {
  8948. DebugFail();
  8949. }
  8950. //---------------------------------------------------------------------------
  8951. bool __fastcall TCustomScpExplorerForm::IsBusy()
  8952. {
  8953. // Among other, a lock level is non zero, while directory is being loaded,
  8954. // even when it happens as a result of dirview navigation,
  8955. // i.e. when TNonVisualDataModule is NOT FBusy.
  8956. // That's why the TNonVisualDataModule::GetBusy calls this method.
  8957. // Among other this prevents a panel auto update to occur while
  8958. // directory is changing.
  8959. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  8960. }
  8961. //---------------------------------------------------------------------------
  8962. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  8963. {
  8964. // While the window is disabled, we still seem to process menu shortcuts at least,
  8965. // so stop it at least here.
  8966. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  8967. return
  8968. (Allowed == aaUpdate) ||
  8969. !NonVisualDataModule->Busy;
  8970. }
  8971. //---------------------------------------------------------------------------
  8972. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  8973. {
  8974. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  8975. }
  8976. //---------------------------------------------------------------------------
  8977. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  8978. {
  8979. // This is somewhat redundant to LockWindow() call from
  8980. // TTerminalManager::TerminalReadDirectoryProgress.
  8981. // But disabling window is known not to block keyboard shorcuts
  8982. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  8983. if (Busy > 0)
  8984. {
  8985. if (NonVisualDataModule->Busy)
  8986. {
  8987. State = false;
  8988. }
  8989. else
  8990. {
  8991. NonVisualDataModule->StartBusy();
  8992. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  8993. LockWindow();
  8994. }
  8995. }
  8996. else if (Busy < 0)
  8997. {
  8998. UnlockWindow();
  8999. NonVisualDataModule->EndBusy();
  9000. }
  9001. else
  9002. {
  9003. State = NonVisualDataModule->Busy;
  9004. }
  9005. }
  9006. //---------------------------------------------------------------------------
  9007. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9008. {
  9009. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9010. // no context menu for "New session tab"
  9011. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9012. {
  9013. SessionsPageControl->ActivePageIndex = Index;
  9014. if (DebugAlwaysTrue(SessionTabSwitched()))
  9015. {
  9016. // copied from TControl.WMContextMenu
  9017. SendCancelMode(SessionsPageControl);
  9018. // explicit popup instead of using PopupMenu property
  9019. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9020. // while connecting yet not-connected session and hence
  9021. // allowing an access to commands over not-completelly connected session
  9022. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9023. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9024. PopupMenu->PopupComponent = SessionsPageControl;
  9025. PopupMenu->Popup(Point.x, Point.y);
  9026. }
  9027. }
  9028. Handled = true;
  9029. }
  9030. //---------------------------------------------------------------------------
  9031. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9032. {
  9033. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9034. }
  9035. //---------------------------------------------------------------------------
  9036. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9037. {
  9038. Configuration->Usage->Inc(L"PasswordChanges");
  9039. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9040. ChangePasswordData->DisableAuthentationsExceptPassword();
  9041. ChangePasswordData->ChangePassword = true;
  9042. std::unique_ptr<TTerminal> ChangePasswordSession(
  9043. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9044. ChangePasswordSession->AutoReadDirectory = false;
  9045. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9046. {
  9047. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9048. }
  9049. else
  9050. {
  9051. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9052. }
  9053. }
  9054. //---------------------------------------------------------------------------
  9055. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9056. {
  9057. // No nice way to assert SSH2
  9058. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9059. }
  9060. //---------------------------------------------------------------------------
  9061. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9062. {
  9063. TOperationVisualizer OperationVisualizer;
  9064. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9065. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9066. }
  9067. //---------------------------------------------------------------------------
  9068. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9069. {
  9070. TForm::ChangeScale(M, D);
  9071. int APixelsPerInch = GetControlPixelsPerInch(this);
  9072. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9073. try
  9074. {
  9075. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9076. }
  9077. __finally
  9078. {
  9079. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9080. }
  9081. }
  9082. //---------------------------------------------------------------------------
  9083. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9084. {
  9085. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9086. }
  9087. //---------------------------------------------------------------------
  9088. class TPublicControl : public TControl
  9089. {
  9090. friend class TCustomScpExplorerForm;
  9091. };
  9092. //---------------------------------------------------------------------------
  9093. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9094. {
  9095. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9096. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9097. }
  9098. //---------------------------------------------------------------------------
  9099. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9100. {
  9101. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9102. OpenBookmark(Side, Bookmark);
  9103. }
  9104. //---------------------------------------------------------------------------
  9105. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9106. {
  9107. DoBookmarkClick(osLocal, Sender);
  9108. }
  9109. //---------------------------------------------------------------------------
  9110. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9111. {
  9112. DoBookmarkClick(osRemote, Sender);
  9113. }
  9114. //---------------------------------------------------------------------------
  9115. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9116. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9117. {
  9118. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9119. if (!WinConfiguration->UseLocationProfiles)
  9120. {
  9121. std::unique_ptr<TStringList> Directories(new TStringList());
  9122. Directories->CaseSensitive = (Side == osRemote);
  9123. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9124. {
  9125. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9126. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9127. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9128. {
  9129. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9130. Item->Caption = EscapeHotkey(Directory);
  9131. Item->ShortCut = Bookmark->ShortCut;
  9132. Item->OnClick = OnBookmarkClick;
  9133. Item->Tag = reinterpret_cast<int>(Bookmark);
  9134. Directories->Add(Directory);
  9135. Menu->Add(Item.release());
  9136. }
  9137. }
  9138. }
  9139. else
  9140. {
  9141. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9142. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9143. {
  9144. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9145. if (!Bookmark->Node.IsEmpty())
  9146. {
  9147. Folders->Add(Bookmark->Node);
  9148. }
  9149. }
  9150. for (int Index = 0; Index < Folders->Count; Index++)
  9151. {
  9152. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9153. Item->Caption = Folders->Strings[Index];
  9154. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9155. Folders->Objects[Index] = Item.get();
  9156. Menu->Add(Item.release());
  9157. }
  9158. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9159. {
  9160. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9161. TTBCustomItem * Parent;
  9162. if (!Bookmark->Node.IsEmpty())
  9163. {
  9164. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9165. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9166. }
  9167. else
  9168. {
  9169. Parent = Menu;
  9170. }
  9171. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9172. Item->Caption = Bookmark->Name;
  9173. Item->ShortCut = Bookmark->ShortCut;
  9174. Item->OnClick = OnBookmarkClick;
  9175. Item->Tag = reinterpret_cast<int>(Bookmark);
  9176. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9177. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9178. {
  9179. // No hint for location profiles that are actually mere "bookmarks"
  9180. }
  9181. else
  9182. {
  9183. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9184. UnicodeString LongHint;
  9185. if (!Bookmark->Local.IsEmpty())
  9186. {
  9187. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9188. }
  9189. if (!Bookmark->Remote.IsEmpty())
  9190. {
  9191. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9192. }
  9193. AddToList(Hint, LongHint, L"|");
  9194. Item->Hint = Hint;
  9195. }
  9196. Parent->Add(Item.release());
  9197. }
  9198. }
  9199. }
  9200. //---------------------------------------------------------------------------
  9201. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9202. {
  9203. Menu->Clear();
  9204. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9205. AddMenuSeparator(Menu);
  9206. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9207. AddMenuSeparator(Menu);
  9208. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9209. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9210. Menu->Add(Item.release());
  9211. }
  9212. //---------------------------------------------------------------------------
  9213. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9214. {
  9215. bool Result = true;
  9216. try
  9217. {
  9218. if (Side == osRemote)
  9219. {
  9220. Terminal->ExceptionOnFail = true;
  9221. }
  9222. try
  9223. {
  9224. DirView(Side)->Path = Path;
  9225. }
  9226. __finally
  9227. {
  9228. if (Side == osRemote)
  9229. {
  9230. Terminal->ExceptionOnFail = false;
  9231. }
  9232. }
  9233. }
  9234. catch (Exception & E)
  9235. {
  9236. if ((Side != osRemote) || Terminal->Active)
  9237. {
  9238. ShowExtendedExceptionEx(Terminal, &E);
  9239. Result = false;
  9240. }
  9241. else
  9242. {
  9243. throw;
  9244. }
  9245. }
  9246. return Result;
  9247. }
  9248. //---------------------------------------------------------------------------