CustomScpExplorer.cpp 299 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299
  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. //---------------------------------------------------------------------------
  33. #pragma package(smart_init)
  34. #pragma link "CustomDirView"
  35. #pragma link "CustomUnixDirView"
  36. #pragma link "IEListView"
  37. #pragma link "NortonLikeListView"
  38. #pragma link "UnixDirView"
  39. #pragma link "CustomDriveView"
  40. #pragma link "UnixDriveView"
  41. #pragma link "CustomDriveView"
  42. #pragma link "TB2Dock"
  43. #pragma link "TBXStatusBars"
  44. #pragma link "TB2Item"
  45. #pragma link "TB2Toolbar"
  46. #pragma link "ThemePageControl"
  47. #pragma link "PathLabel"
  48. #ifndef NO_RESOURCES
  49. #pragma resource "*.dfm"
  50. #endif
  51. //---------------------------------------------------------------------------
  52. #define SAVE_SELECTION(DIRVIEW) \
  53. UnicodeString FocusFile = L""; \
  54. UnicodeString LastFocusedFile = L""; \
  55. if (DIRVIEW->ItemFocused) LastFocusedFile = DIRVIEW->ItemFocused->Caption; \
  56. { TListItem * ClosestUnselected = DIRVIEW->ClosestUnselected(DIRVIEW->ItemFocused); \
  57. if (ClosestUnselected) FocusFile = ClosestUnselected->Caption; }
  58. #define RESTORE_SELECTION(DIRVIEW) \
  59. if (!LastFocusedFile.IsEmpty() && \
  60. (!DIRVIEW->ItemFocused || (DIRVIEW->ItemFocused->Caption != LastFocusedFile))) \
  61. { \
  62. TListItem *ItemToSelect = DIRVIEW->FindFileItem(FocusFile); \
  63. if (ItemToSelect) \
  64. { \
  65. DIRVIEW->ItemFocused = ItemToSelect; \
  66. DIRVIEW->ItemFocused->MakeVisible(False); \
  67. } \
  68. }
  69. //---------------------------------------------------------------------------
  70. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  71. static const int SessionPanelCount = 4;
  72. //---------------------------------------------------------------------------
  73. class TMutexGuard
  74. {
  75. public:
  76. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  77. int Timeout = 5000)
  78. {
  79. FMutex = NULL;
  80. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  81. if (WaitResult == WAIT_TIMEOUT)
  82. {
  83. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  84. }
  85. else
  86. {
  87. FMutex = AMutex;
  88. }
  89. }
  90. ~TMutexGuard()
  91. {
  92. if (FMutex != NULL)
  93. {
  94. ReleaseMutex(FMutex);
  95. }
  96. }
  97. private:
  98. HANDLE FMutex;
  99. };
  100. //---------------------------------------------------------------------------
  101. class TWindowLock
  102. {
  103. public:
  104. TWindowLock(TCustomScpExplorerForm * Form) :
  105. FForm(Form)
  106. {
  107. FForm->LockWindow();
  108. }
  109. ~TWindowLock()
  110. {
  111. FForm->UnlockWindow();
  112. }
  113. private:
  114. TCustomScpExplorerForm * FForm;
  115. };
  116. //---------------------------------------------------------------------------
  117. struct TTransferOperationParam
  118. {
  119. TTransferOperationParam();
  120. UnicodeString TargetDirectory;
  121. bool Temp;
  122. bool DragDrop;
  123. int Options;
  124. TAutoSwitch Queue;
  125. };
  126. //---------------------------------------------------------------------------
  127. TTransferOperationParam::TTransferOperationParam()
  128. {
  129. Temp = false;
  130. DragDrop = false;
  131. Options = 0;
  132. Queue = asAuto;
  133. }
  134. //---------------------------------------------------------------------------
  135. class TTransferPresetNoteData : public TObject
  136. {
  137. public:
  138. UnicodeString Message;
  139. };
  140. //---------------------------------------------------------------------------
  141. class TTerminalNoteData : public TObject
  142. {
  143. public:
  144. TTerminal * Terminal;
  145. };
  146. //---------------------------------------------------------------------------
  147. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  148. FFormRestored(false),
  149. TForm(Owner)
  150. {
  151. FCurrentSide = osRemote;
  152. FEverShown = false;
  153. FDocks = new TList();
  154. RestoreParams();
  155. ConfigurationChanged();
  156. RemoteDirView->Invalidate();
  157. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  158. NonVisualDataModule->ScpExplorer = this;
  159. FAutoOperation = false;
  160. FOnFileOperationFinished = NULL;
  161. FForceExecution = false;
  162. FIgnoreNextDialogChar = 0;
  163. FErrorList = NULL;
  164. FSynchronizeProgressForm = NULL;
  165. FProgressForm = NULL;
  166. FRefreshLocalDirectory = false;
  167. FRefreshRemoteDirectory = false;
  168. FDDMoveSlipped = false;
  169. FDDExtMapFile = NULL;
  170. // CreateMutexW keeps failing with ERROR_NOACCESS
  171. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  172. DebugAssert(FDDExtMutex != NULL);
  173. FDDTargetControl = NULL;
  174. FDelayedDeletionTimer = NULL;
  175. FDelayedDeletionList = new TStringList();
  176. FDDFileList = NULL;
  177. FPendingTempSpaceWarn = false;
  178. FCapturedLog = NULL;
  179. FDragDropOperation = false;
  180. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  181. FAllowTransferPresetAutoSelect = true;
  182. FCopyParamDefault = L"";
  183. FSynchronizeController = NULL;
  184. FPendingQueueActionItem = NULL;
  185. FLockLevel = 0;
  186. FLockSuspendLevel = 0;
  187. FDisabledOnLockSuspend = false;
  188. FAlternativeDelete = false;
  189. FTrayIcon = new ::TTrayIcon(0);
  190. FTrayIcon->OnClick = TrayIconClick;
  191. FMaxQueueLength = 0;
  192. FLastContextPopupScreenPoint = TPoint(-1, -1);
  193. FTransferResumeList = NULL;
  194. FMoveToQueue = false;
  195. FStandaloneEditing = false;
  196. FOnFeedSynchronizeError = NULL;
  197. FNeedSession = false;
  198. FEditorManager = new TEditorManager();
  199. FEditorManager->OnFileChange = ExecutedFileChanged;
  200. FEditorManager->OnFileReload = ExecutedFileReload;
  201. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  202. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  203. FLocalEditors = new TList();
  204. FQueueStatus = NULL;
  205. FQueueStatusSection = new TCriticalSection();
  206. FQueueStatusInvalidated = false;
  207. FQueueItemInvalidated = false;
  208. FQueueActedItem = NULL;
  209. FQueueController = new TQueueController(QueueView3);
  210. FUserActionTimer = new TTimer(this);
  211. FUserActionTimer->Enabled = false;
  212. FUserActionTimer->Interval = 10;
  213. FUserActionTimer->OnTimer = UserActionTimer;
  214. FNotes = new TStringList();
  215. FNoteTimer = new TTimer(this);
  216. FNoteTimer->Enabled = false;
  217. FNoteTimer->OnTimer = NoteTimer;
  218. FOnNoteClick = NULL;
  219. FOle32Library = LoadLibrary(L"Ole32.dll");
  220. FDragMoveCursor = FOle32Library != NULL ?
  221. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  222. UseSystemSettings(this);
  223. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  224. static_cast<TObject*>(GetComponent(fcTransferList)));
  225. DebugAssert(TransferList != NULL);
  226. FTransferListHoverIndex = -1;
  227. TransferList->OnChange = TransferListChange;
  228. TransferList->OnDrawItem = TransferListDrawItem;
  229. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  230. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  231. UseDesktopFont(SessionsPageControl);
  232. UpdateSessionsPageControlHeight();
  233. UseDesktopFont(RemoteDirView);
  234. UseDesktopFont(RemoteDriveView);
  235. UseDesktopFont(QueueView3);
  236. UseDesktopFont(QueueLabel);
  237. UseDesktopFont(RemoteStatusBar);
  238. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  239. QueueSplitter->ShowHint = true;
  240. RemotePanelSplitter->ShowHint = true;
  241. UpdateImages();
  242. FCustomCommandMenu = CreateTBXPopupMenu(this);
  243. FCustomCommandLocalFileList = NULL;
  244. FCustomCommandRemoteFileList = NULL;
  245. FSessionColors = new TPngImageList(this);
  246. FSessionColors->ColorDepth = cd32Bit;
  247. AddFixedSessionImages();
  248. SessionsPageControl->Images = FSessionColors;
  249. QueueLabel->FocusControl = QueueView3;
  250. UpdateQueueLabel();
  251. FRemoteDirViewWasFocused = true;
  252. CreateHiddenWindow();
  253. StartUpdates();
  254. }
  255. //---------------------------------------------------------------------------
  256. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  257. {
  258. // this has to be one of the very first things to do
  259. StopUpdateThread();
  260. SessionsPageControl->Images = NULL;
  261. SAFE_DESTROY(FSessionColors);
  262. SAFE_DESTROY(FSessionsDragDropFilesEx);
  263. delete FCustomCommandLocalFileList;
  264. delete FCustomCommandRemoteFileList;
  265. delete FCustomCommandMenu;
  266. delete FTrayIcon;
  267. FTrayIcon = NULL;
  268. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  269. delete FEditorManager;
  270. ForceCloseLocalEditors();
  271. delete FLocalEditors;
  272. HideFileFindDialog();
  273. if (FDelayedDeletionTimer)
  274. {
  275. DoDelayedDeletion(NULL);
  276. SAFE_DESTROY(FDelayedDeletionTimer);
  277. }
  278. SAFE_DESTROY(FDelayedDeletionList);
  279. // sometimes we do not get DDEnd so the list is not released
  280. SAFE_DESTROY(FDDFileList);
  281. DebugAssert(FSynchronizeController == NULL);
  282. CloseHandle(FDDExtMutex);
  283. FDDExtMutex = NULL;
  284. FreeLibrary(FOle32Library);
  285. FOle32Library = NULL;
  286. FDragMoveCursor = NULL;
  287. DebugAssert(!FErrorList);
  288. if (FEverShown)
  289. {
  290. // when window is never shown (like when running command-line operation),
  291. // particularly window site is not restored correctly (BoundsRect value set
  292. // in RestoreForm gets lost during handle allocation), so we do not want
  293. // it to be stored
  294. StoreParams();
  295. }
  296. Terminal = NULL;
  297. Queue = NULL;
  298. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  299. NonVisualDataModule->ScpExplorer = NULL;
  300. delete FQueueController;
  301. FQueueController = NULL;
  302. delete FQueueStatusSection;
  303. FQueueStatusSection = NULL;
  304. delete FQueueStatus;
  305. FQueueStatus = NULL;
  306. delete FUserActionTimer;
  307. FUserActionTimer = NULL;
  308. delete FNoteTimer;
  309. delete FNotes;
  310. delete FNoteData;
  311. SAFE_DESTROY(FDocks);
  312. SAFE_DESTROY(FHistoryMenu[0][0]);
  313. SAFE_DESTROY(FHistoryMenu[0][1]);
  314. SAFE_DESTROY(FHistoryMenu[1][0]);
  315. SAFE_DESTROY(FHistoryMenu[1][1]);
  316. if (FHiddenWindow != NULL)
  317. {
  318. DestroyWindow(FHiddenWindow);
  319. FHiddenWindow = NULL;
  320. }
  321. }
  322. //---------------------------------------------------------------------------
  323. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  324. {
  325. std::unique_ptr<TSessionData> Data;
  326. if (!Session.IsEmpty())
  327. {
  328. bool DefaultsOnly;
  329. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  330. }
  331. TTerminalManager * Manager = TTerminalManager::Instance();
  332. for (int Index = 0; Index < Manager->Count; Index++)
  333. {
  334. TTerminal * Terminal = Manager->Terminals[Index];
  335. if (Session.IsEmpty() ||
  336. Terminal->SessionData->IsSameSite(Data.get()))
  337. {
  338. if (Path.IsEmpty())
  339. {
  340. Terminal->ClearCaches();
  341. }
  342. else
  343. {
  344. Terminal->DirectoryModified(Path, true);
  345. }
  346. }
  347. }
  348. // We should flag a pending refresh for the background terminals or busy foreground terminals
  349. if ((Terminal != NULL) && Terminal->Active &&
  350. CanCommandLineFromAnotherInstance() &&
  351. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  352. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  353. {
  354. Terminal->ReloadDirectory();
  355. }
  356. }
  357. //---------------------------------------------------------------------------
  358. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  359. {
  360. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  361. size_t MessageSize = sizeof(TCopyDataMessage);
  362. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  363. if (Result)
  364. {
  365. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  366. Result = (Message.Version == TCopyDataMessage::Version1);
  367. if (Result)
  368. {
  369. switch (Message.Command)
  370. {
  371. case TCopyDataMessage::CommandCanCommandLine:
  372. Result = CanCommandLineFromAnotherInstance();
  373. break;
  374. case TCopyDataMessage::CommandCommandLine:
  375. {
  376. UnicodeString CommandLine(Message.CommandLine);
  377. Result = CommandLineFromAnotherInstance(CommandLine);
  378. }
  379. break;
  380. case TCopyDataMessage::RefreshPanel:
  381. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  382. break;
  383. case TCopyDataMessage::MainWindowCheck:
  384. Result = true;
  385. break;
  386. default:
  387. Result = false;
  388. break;
  389. }
  390. }
  391. }
  392. Message.Result = Result ? 1 : 0;
  393. }
  394. //---------------------------------------------------------------------------
  395. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  396. {
  397. WNDCLASS WindowClass = {0};
  398. WindowClass.lpfnWndProc = DefWindowProc;
  399. WindowClass.hInstance = HInstance;
  400. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  401. FHiddenWindow = NULL;
  402. if (RegisterClass(&WindowClass))
  403. {
  404. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  405. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  406. }
  407. }
  408. //---------------------------------------------------------------------------
  409. bool __fastcall TCustomScpExplorerForm::CanConsole()
  410. {
  411. return (Terminal != NULL) && (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  412. }
  413. //---------------------------------------------------------------------------
  414. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  415. {
  416. bool Result = !NonVisualDataModule->Busy;
  417. return Result;
  418. }
  419. //---------------------------------------------------------------------------
  420. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  421. const UnicodeString & CommandLine)
  422. {
  423. TProgramParams Params(CommandLine);
  424. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  425. if (Result)
  426. {
  427. NonVisualDataModule->StartBusy();
  428. try
  429. {
  430. // this action is initiated from another process,
  431. // so it's likely that our window is not visible,
  432. // and user won't see what is going on
  433. Application->BringToFront();
  434. // reload sessions as we may be asked to open a session
  435. // just stored by another instance
  436. StoredSessions->Load();
  437. UnicodeString SessionName = Params.Param[1];
  438. std::unique_ptr<TObjectList> DataList(new TObjectList());
  439. UnicodeString DownloadFile; // unused
  440. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  441. if (DataList->Count > 0)
  442. {
  443. TTerminalManager * Manager = TTerminalManager::Instance();
  444. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  445. }
  446. }
  447. __finally
  448. {
  449. NonVisualDataModule->EndBusy();
  450. }
  451. }
  452. return Result;
  453. }
  454. //---------------------------------------------------------------------------
  455. void __fastcall TCustomScpExplorerForm::SetTerminal(TTerminal * value)
  456. {
  457. if (FTerminal != value)
  458. {
  459. TerminalChanging();
  460. FTerminal = value;
  461. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  462. FAllowTransferPresetAutoSelect = false;
  463. try
  464. {
  465. TerminalChanged();
  466. }
  467. __finally
  468. {
  469. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  470. }
  471. if (Terminal != NULL)
  472. {
  473. TransferPresetAutoSelect();
  474. }
  475. }
  476. }
  477. //---------------------------------------------------------------------------
  478. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  479. {
  480. if (FTerminal != NULL)
  481. {
  482. UpdateTerminal(Terminal);
  483. }
  484. }
  485. //---------------------------------------------------------------------------
  486. void __fastcall TCustomScpExplorerForm::TerminalChanged()
  487. {
  488. RemoteDirView->Terminal = Terminal;
  489. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  490. TManagedTerminal * ManagedTerminal = NULL;
  491. if (Terminal != NULL)
  492. {
  493. if (Terminal->Active)
  494. {
  495. Terminal->RefreshDirectory();
  496. }
  497. ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  498. DebugAssert(ManagedTerminal != NULL);
  499. if (WinConfiguration->PreservePanelState)
  500. {
  501. if (ManagedTerminal->RemoteExplorerState != NULL)
  502. {
  503. RemoteDirView->RestoreState(ManagedTerminal->RemoteExplorerState);
  504. }
  505. else
  506. {
  507. RemoteDirView->ClearState();
  508. }
  509. }
  510. InitStatusBar();
  511. }
  512. DoTerminalListChanged(false);
  513. if (ManagedTerminal != NULL)
  514. {
  515. // this has to be set only after the tab is switched from DoTerminalListChanged,
  516. // otherwise we are changing color of wrong tab
  517. SessionColor = (TColor)ManagedTerminal->StateData->Color;
  518. }
  519. UpdateTransferList();
  520. // Update panels Enable state before refreshing the labels
  521. UpdateControls();
  522. }
  523. //---------------------------------------------------------------------------
  524. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  525. {
  526. if (Queue != value)
  527. {
  528. if (FQueue != NULL)
  529. {
  530. FQueue->OnListUpdate = NULL;
  531. FQueue->OnQueueItemUpdate = NULL;
  532. }
  533. FQueue = value;
  534. if (FQueue != NULL)
  535. {
  536. DebugAssert(FQueue->OnListUpdate == NULL);
  537. FQueue->OnListUpdate = QueueListUpdate;
  538. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  539. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  540. }
  541. QueueChanged();
  542. }
  543. }
  544. //---------------------------------------------------------------------------
  545. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  546. TListItem * Item)
  547. {
  548. if (FQueueActedItem == Item)
  549. {
  550. FQueueActedItem = NULL;
  551. if ((QueueView3->PopupMenu != NULL) &&
  552. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  553. {
  554. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  555. ReleaseCapture();
  556. }
  557. }
  558. if (Item->Data == FPendingQueueActionItem)
  559. {
  560. FPendingQueueActionItem = NULL;
  561. }
  562. }
  563. //---------------------------------------------------------------------------
  564. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  565. {
  566. {
  567. TGuard Guard(FQueueStatusSection);
  568. FQueueStatusInvalidated = false;
  569. if (FQueue != NULL)
  570. {
  571. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  572. }
  573. }
  574. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  575. {
  576. FMaxQueueLength = FQueueStatus->Count;
  577. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  578. }
  579. FQueueController->UpdateQueueStatus(FQueueStatus);
  580. SetQueueProgress();
  581. UpdateQueueView();
  582. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  583. if (IsEmpty && (Terminal != NULL))
  584. {
  585. TOnceDoneOperation OnceDoneOperation =
  586. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  587. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  588. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  589. {
  590. FQueue->Enabled = false;
  591. }
  592. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  593. {
  594. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  595. }
  596. }
  597. }
  598. //---------------------------------------------------------------------------
  599. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  600. {
  601. UnicodeString Result;
  602. if (FQueueStatus != NULL)
  603. {
  604. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  605. {
  606. TFileOperationProgressType * ProgressData =
  607. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  608. if ((ProgressData != NULL) && ProgressData->InProgress)
  609. {
  610. Result = TProgressForm::ProgressStr(ProgressData);
  611. }
  612. }
  613. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  614. {
  615. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  616. }
  617. }
  618. return Result;
  619. }
  620. //---------------------------------------------------------------------------
  621. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  622. {
  623. ComponentVisible[fcQueueView] =
  624. (WinConfiguration->QueueView.Show == qvShow) ||
  625. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  626. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  627. }
  628. //---------------------------------------------------------------------------
  629. void __fastcall TCustomScpExplorerForm::QueueChanged()
  630. {
  631. if (FQueueStatus != NULL)
  632. {
  633. delete FQueueStatus;
  634. FQueueStatus = NULL;
  635. }
  636. UpdateQueueStatus(true);
  637. }
  638. //---------------------------------------------------------------------------
  639. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  640. {
  641. if (FQueue == Queue)
  642. {
  643. FQueueStatusInvalidated = true;
  644. }
  645. }
  646. //---------------------------------------------------------------------------
  647. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  648. TQueueItem * Item)
  649. {
  650. if (FQueue == Queue)
  651. {
  652. TGuard Guard(FQueueStatusSection);
  653. // this may be running in parallel with QueueChanged
  654. if (FQueueStatus != NULL)
  655. {
  656. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  657. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  658. {
  659. FRefreshLocalDirectory = (QueueItem == NULL) ||
  660. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  661. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  662. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  663. }
  664. if (QueueItem != NULL)
  665. {
  666. QueueItem->UserData = (void*)true;
  667. FQueueItemInvalidated = true;
  668. }
  669. }
  670. }
  671. }
  672. //---------------------------------------------------------------------------
  673. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  674. {
  675. // during standalone editing, we have no way to see/control queue,
  676. // so we have to always popup prompts automatically
  677. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  678. }
  679. //---------------------------------------------------------------------------
  680. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  681. {
  682. if (FQueueStatus != NULL)
  683. {
  684. bool QueueAutoPopup = IsQueueAutoPopup();
  685. bool NeedRefresh = FQueueController->NeedRefresh();
  686. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  687. FQueueItemInvalidated = false;
  688. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  689. bool Updated = false;
  690. TQueueItemProxy * QueueItem;
  691. bool WasUserAction;
  692. for (int Index = 0; Index < Limit; Index++)
  693. {
  694. bool Update = false;
  695. QueueItem = FQueueStatus->Items[Index];
  696. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  697. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  698. (FPendingQueueActionItem == NULL))
  699. {
  700. FPendingQueueActionItem = QueueItem;
  701. FUserActionTimer->Enabled = true;
  702. }
  703. if ((bool)QueueItem->UserData)
  704. {
  705. QueueItem->UserData = (void*)false;
  706. QueueItem->Update();
  707. Updated = true;
  708. Update = true;
  709. }
  710. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  711. {
  712. Update = true;
  713. }
  714. if (Update)
  715. {
  716. FQueueController->RefreshQueueItem(QueueItem);
  717. }
  718. }
  719. if (Updated)
  720. {
  721. NonVisualDataModule->UpdateNonVisibleActions();
  722. SetQueueProgress();
  723. }
  724. }
  725. }
  726. //---------------------------------------------------------------------------
  727. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  728. {
  729. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  730. }
  731. //---------------------------------------------------------------------------
  732. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  733. {
  734. if (ProgressData->Operation != foCalculateSize)
  735. {
  736. // implies TBPF_NORMAL
  737. FTaskbarList->SetProgressValue(GetMainForm()->Handle, ProgressData->OverallProgress(), 100);
  738. }
  739. else
  740. {
  741. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  742. }
  743. }
  744. //---------------------------------------------------------------------------
  745. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  746. {
  747. TTerminalManager::Instance()->QueueStatusUpdated();
  748. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  749. {
  750. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  751. {
  752. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  753. {
  754. TFileOperationProgressType * ProgressData = NULL;
  755. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  756. {
  757. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  758. }
  759. if ((ProgressData != NULL) &&
  760. ProgressData->InProgress)
  761. {
  762. SetTaskbarListProgressValue(ProgressData);
  763. }
  764. else
  765. {
  766. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  767. }
  768. }
  769. else
  770. {
  771. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  772. }
  773. }
  774. else
  775. {
  776. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  777. }
  778. }
  779. UpdateQueueLabel();
  780. }
  781. //---------------------------------------------------------------------------
  782. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  783. {
  784. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  785. if (FQueueStatus != NULL)
  786. {
  787. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  788. if (ActiveAndPendingPrimaryCount > 0)
  789. {
  790. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  791. }
  792. }
  793. QueueLabel->Caption = Caption;
  794. }
  795. //---------------------------------------------------------------------------
  796. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  797. {
  798. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  799. static_cast<TComponent*>(GetComponent(fcTransferList)));
  800. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  801. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  802. TransferList->Strings->BeginUpdate();
  803. try
  804. {
  805. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  806. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  807. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  808. if (FTransferDropDownHint.IsEmpty())
  809. {
  810. FTransferDropDownHint = TransferDropDown->Hint;
  811. }
  812. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  813. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  814. TransferDropDown->Text = StripHotkey(Name);
  815. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  816. (FTransferDropDownHint, StripHotkey(Name),
  817. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  818. FLAGMASK(Terminal != NULL, Terminal->UsableCopyParamAttrs(0).General))));
  819. // update the label, otherwise when it is updated only on the first draw
  820. // of the list, it is drawn "bold" for some reason
  821. FTransferListHoverIndex = TransferList->ItemIndex;
  822. UpdateTransferLabel();
  823. }
  824. __finally
  825. {
  826. TransferList->Strings->EndUpdate();
  827. }
  828. }
  829. //---------------------------------------------------------------------------
  830. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  831. {
  832. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  833. DebugAssert(Toolbar != NULL);
  834. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  835. }
  836. //---------------------------------------------------------------------------
  837. void __fastcall TCustomScpExplorerForm::UpdateActions()
  838. {
  839. TForm::UpdateActions();
  840. if (ComponentVisible[fcCustomCommandsBand])
  841. {
  842. UpdateCustomCommandsToolbar();
  843. }
  844. }
  845. //---------------------------------------------------------------------------
  846. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  847. {
  848. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  849. }
  850. //---------------------------------------------------------------------------
  851. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  852. {
  853. DebugAssert(Configuration && RemoteDirView);
  854. RemoteDirView->DDAllowMove = WinConfiguration->DDAllowMoveInit;
  855. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  856. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  857. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  858. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  859. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  860. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  861. {
  862. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  863. // selection is not redrawn automatically when RowSelect changes
  864. RemoteDirView->Invalidate();
  865. }
  866. RemoteDriveView->DDAllowMove = WinConfiguration->DDAllowMoveInit;
  867. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  868. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  869. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  870. UpdateSessionsPageControlHeight();
  871. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  872. UpdateToolbarDisplayMode();
  873. UpdateTransferList();
  874. if (Terminal != NULL)
  875. {
  876. TransferPresetAutoSelect();
  877. }
  878. UpdateQueueView();
  879. UpdateControls();
  880. // this can be called even before constuctor finishes.
  881. if (FEditorManager != NULL)
  882. {
  883. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  884. }
  885. // this can be called even before constuctor finishes.
  886. if (FLocalEditors != NULL)
  887. {
  888. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  889. {
  890. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  891. }
  892. }
  893. if (ComponentVisible[fcCustomCommandsBand])
  894. {
  895. UpdateCustomCommandsToolbar();
  896. }
  897. // show only when keeping queue items forever,
  898. // otherwise, its enough to have in in the context menu
  899. QueueDeleteAllDoneQueueToolbarItem->Visible =
  900. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  901. }
  902. //---------------------------------------------------------------------------
  903. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  904. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  905. void * /*Arg*/)
  906. {
  907. if (Token != NULL)
  908. {
  909. TForm * Editor = dynamic_cast<TForm*>(Token);
  910. DebugAssert(Editor != NULL);
  911. ReconfigureEditorForm(Editor);
  912. }
  913. }
  914. //---------------------------------------------------------------------------
  915. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  916. {
  917. WinConfiguration->DDTransferConfirmation = asOn;
  918. }
  919. //---------------------------------------------------------------------------
  920. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  921. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  922. {
  923. // noop
  924. }
  925. //---------------------------------------------------------------------------
  926. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  927. TTransferDirection Direction, TTransferType Type, bool Temp,
  928. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  929. bool Confirm, bool DragDrop, int Options)
  930. {
  931. bool Result = true;
  932. DebugAssert(Terminal && Terminal->Active);
  933. // Temp means d&d here so far, may change in future!
  934. if (Temp && (Direction == tdToLocal) && (Type == ttMove) &&
  935. !WinConfiguration->DDAllowMove)
  936. {
  937. TMessageParams Params(mpNeverAskAgainCheck);
  938. unsigned int Answer = MessageDialog(LoadStr(DND_DOWNLOAD_MOVE_WARNING), qtWarning,
  939. qaOK | qaCancel, HELP_DND_DOWNLOAD_MOVE_WARNING, &Params);
  940. if (Answer == qaNeverAskAgain)
  941. {
  942. WinConfiguration->DDAllowMove = true;
  943. }
  944. else if (Answer == qaCancel)
  945. {
  946. Result = false;
  947. }
  948. }
  949. // these parameters are known in advance
  950. int Params =
  951. FLAGMASK(Type == ttMove, cpDelete);
  952. bool ToTemp = (Temp && (Direction == tdToLocal));
  953. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  954. {
  955. bool DisableNewerOnly =
  956. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  957. ToTemp;
  958. Options |=
  959. FLAGMASK(ToTemp, coTemp) |
  960. coDoNotShowAgain;
  961. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  962. int CopyParamAttrs = (Direction == tdToRemote ?
  963. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  964. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  965. int OutputOptions =
  966. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  967. cooDoNotShowAgain);
  968. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  969. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  970. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions);
  971. if (Result)
  972. {
  973. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  974. {
  975. if (DragDrop)
  976. {
  977. if (WinConfiguration->DDTransferConfirmation == asAuto)
  978. {
  979. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  980. NULL, 0, EnableDDTransferConfirmation, NULL);
  981. }
  982. WinConfiguration->DDTransferConfirmation = asOff;
  983. }
  984. else
  985. {
  986. WinConfiguration->ConfirmTransferring = false;
  987. }
  988. }
  989. else
  990. {
  991. // User exclicitly unchecked "do not show again",
  992. // so show him the dialog the next time
  993. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  994. {
  995. WinConfiguration->DDTransferConfirmation = asOn;
  996. }
  997. }
  998. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  999. }
  1000. }
  1001. if (Result && CopyParam.Queue && !ToTemp)
  1002. {
  1003. Configuration->Usage->Inc(L"TransfersOnBackground");
  1004. // these parameter are known only after transfer dialog
  1005. Params |=
  1006. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1007. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1008. Result = false;
  1009. ClearTransferSourceSelection(Direction);
  1010. }
  1011. return Result;
  1012. }
  1013. //---------------------------------------------------------------------------
  1014. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1015. {
  1016. if (FOnFileOperationFinished != NULL)
  1017. {
  1018. FOnFileOperationFinished(UnicodeString(), true);
  1019. }
  1020. else
  1021. {
  1022. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1023. if (HasDirView[Side])
  1024. {
  1025. DirView(Side)->SelectAll(smNone);
  1026. }
  1027. }
  1028. }
  1029. //---------------------------------------------------------------------------
  1030. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1031. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1032. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1033. int Params)
  1034. {
  1035. DebugAssert(Queue != NULL);
  1036. bool SingleFile = false;
  1037. if (FileList->Count == 1)
  1038. {
  1039. if (Direction == tdToRemote)
  1040. {
  1041. UnicodeString FileName = FileList->Strings[0];
  1042. SingleFile = FileExists(ApiPath(FileName));
  1043. }
  1044. else
  1045. {
  1046. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1047. SingleFile = !File->IsDirectory;
  1048. }
  1049. }
  1050. TQueueItem * QueueItem;
  1051. if (Direction == tdToRemote)
  1052. {
  1053. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1054. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1055. }
  1056. else
  1057. {
  1058. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1059. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1060. }
  1061. AddQueueItem(Queue, QueueItem, Terminal);
  1062. }
  1063. //---------------------------------------------------------------------------
  1064. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TTerminal * Terminal)
  1065. {
  1066. if (Queue->IsEmpty)
  1067. {
  1068. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  1069. ManagedTerminal->QueueOperationStart = Now();
  1070. }
  1071. Queue->AddItem(QueueItem);
  1072. }
  1073. //---------------------------------------------------------------------------
  1074. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1075. {
  1076. }
  1077. //---------------------------------------------------------------------------
  1078. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1079. {
  1080. DebugAssert(FDocks != NULL);
  1081. for (int Index = 0; Index < ComponentCount; Index++)
  1082. {
  1083. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1084. if ((Dock != NULL) && (Dock->Tag == 0))
  1085. {
  1086. FDocks->Add(Dock);
  1087. }
  1088. }
  1089. CollectItemsWithTextDisplayMode(this);
  1090. ConfigurationChanged();
  1091. QueuePanel->Height =
  1092. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1093. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1094. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1095. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1096. }
  1097. //---------------------------------------------------------------------------
  1098. void __fastcall TCustomScpExplorerForm::StoreParams()
  1099. {
  1100. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1101. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1102. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1103. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1104. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1105. }
  1106. //---------------------------------------------------------------------------
  1107. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1108. {
  1109. if (!FFormRestored)
  1110. {
  1111. FFormRestored = true;
  1112. RestoreFormParams();
  1113. }
  1114. TForm::CreateParams(Params);
  1115. }
  1116. //---------------------------------------------------------------------------
  1117. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1118. {
  1119. DebugAssert(FDocks != NULL);
  1120. for (int Index = 0; Index < FDocks->Count; Index++)
  1121. {
  1122. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1123. }
  1124. }
  1125. //---------------------------------------------------------------------------
  1126. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr)
  1127. {
  1128. SetDockAllowDrag(true);
  1129. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1130. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1131. }
  1132. //---------------------------------------------------------------------------
  1133. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1134. {
  1135. UnicodeString Result;
  1136. SetDockAllowDrag(true);
  1137. Result = ::GetToolbarsLayoutStr(this);
  1138. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1139. return Result;
  1140. }
  1141. //---------------------------------------------------------------------------
  1142. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1143. TFileOperationProgressType & ProgressData)
  1144. {
  1145. // Download to temporary local directory
  1146. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1147. {
  1148. bool Continue = true;
  1149. FPendingTempSpaceWarn = false;
  1150. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1151. if (!Continue)
  1152. {
  1153. Abort();
  1154. }
  1155. }
  1156. // operation is being executed and we still didn't show up progress form
  1157. if (ProgressData.InProgress && !FProgressForm)
  1158. {
  1159. bool AllowSkip =
  1160. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1161. Terminal->IsCapable[fsSkipTransfer];
  1162. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip);
  1163. // As progress window has delayed show now, we need to lock ourselves,
  1164. // (at least) until then
  1165. LockWindow();
  1166. FProgressForm->DeleteToRecycleBin = (ProgressData.Side == osLocal ?
  1167. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete) :
  1168. ((Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1169. !Terminal->SessionData->RecycleBinPath.IsEmpty()));
  1170. // When main window is hidden, synchronisation form does not exist,
  1171. // we suppose "/upload" or URL download mode
  1172. if (!Visible && (ProgressData.Operation != foCalculateSize) &&
  1173. (ProgressData.Operation != foCalculateChecksum) &&
  1174. (FSynchronizeProgressForm == NULL) &&
  1175. !FStandaloneEditing)
  1176. {
  1177. FProgressForm->OnceDoneOperation = odoDisconnect;
  1178. }
  1179. if (FTaskbarList != NULL)
  1180. {
  1181. // Actually, do not know what hides the progress once the operation finishes
  1182. SetTaskbarListProgressState(TBPF_NORMAL);
  1183. }
  1184. }
  1185. // operation is finished (or terminated), so we hide progress form
  1186. else if (!ProgressData.InProgress && FProgressForm)
  1187. {
  1188. UnlockWindow();
  1189. SAFE_DESTROY(FProgressForm);
  1190. SetQueueProgress();
  1191. if (ProgressData.Operation == foCalculateSize)
  1192. {
  1193. // When calculating size before transfer, the abort caused by
  1194. // cancel flag set due to "MoveToQueue" below,
  1195. // is not propagated back to us in ExecuteFileOperation,
  1196. // as it is silently swallowed.
  1197. // So we explicitly (re)throw it here.
  1198. if (FMoveToQueue)
  1199. {
  1200. Abort();
  1201. }
  1202. }
  1203. else
  1204. {
  1205. if ((ProgressData.Cancel == csContinue) &&
  1206. !FAutoOperation)
  1207. {
  1208. OperationComplete(ProgressData.StartTime);
  1209. }
  1210. }
  1211. }
  1212. if (FProgressForm)
  1213. {
  1214. FProgressForm->SetProgressData(ProgressData);
  1215. if (FTaskbarList != NULL)
  1216. {
  1217. DebugAssert(ProgressData.InProgress);
  1218. SetTaskbarListProgressValue(&ProgressData);
  1219. }
  1220. if (FProgressForm->Cancel > csContinue)
  1221. {
  1222. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1223. if (FProgressForm->Cancel == csCancelFile)
  1224. {
  1225. FProgressForm->ClearCancel();
  1226. }
  1227. // cancel cancels even the move
  1228. FMoveToQueue = false;
  1229. }
  1230. else if (FProgressForm->MoveToQueue)
  1231. {
  1232. FMoveToQueue = true;
  1233. ProgressData.SetCancelAtLeast(csCancel);
  1234. }
  1235. if ((FTransferResumeList != NULL) &&
  1236. ProgressData.InProgress &&
  1237. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1238. !ProgressData.FullFileName.IsEmpty())
  1239. {
  1240. if ((FTransferResumeList->Count == 0) ||
  1241. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1242. {
  1243. // note that we do not recognize directories from files here
  1244. FTransferResumeList->Add(ProgressData.FullFileName);
  1245. }
  1246. }
  1247. }
  1248. }
  1249. //---------------------------------------------------------------------------
  1250. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1251. const TDateTime & StartTime)
  1252. {
  1253. if (GUIConfiguration->BeepOnFinish &&
  1254. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1255. {
  1256. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1257. DWORD Sound;
  1258. if (!ExtractFileExt(BeepSound).IsEmpty())
  1259. {
  1260. Sound = SND_FILENAME;
  1261. }
  1262. else
  1263. {
  1264. Sound = SND_ALIAS;
  1265. }
  1266. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1267. }
  1268. }
  1269. //---------------------------------------------------------------------------
  1270. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1271. TFileOperationProgressType & ProgressData)
  1272. {
  1273. FileOperationProgress(ProgressData);
  1274. }
  1275. //---------------------------------------------------------------------------
  1276. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1277. bool DragDrop)
  1278. {
  1279. return (!DragDrop && (DropSourceControl == NULL)) ||
  1280. (DropSourceControl == RemoteDirView);
  1281. }
  1282. //---------------------------------------------------------------------------
  1283. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1284. TFileOperation Operation, TOperationSide Side,
  1285. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1286. TOnceDoneOperation & OnceDoneOperation)
  1287. {
  1288. if (!FAutoOperation)
  1289. {
  1290. // no selection on "/upload", form servers only as event handler
  1291. // (it is not displayed)
  1292. if (PanelOperation(Side, FDragDropOperation) &&
  1293. Visible && (Operation != foCalculateSize) &&
  1294. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1295. {
  1296. if (FOnFileOperationFinished != NULL)
  1297. {
  1298. FOnFileOperationFinished(FileName, Success);
  1299. }
  1300. else
  1301. {
  1302. TCustomDirView * DView = DirView(Side);
  1303. UnicodeString FileNameOnly = ExtractFileName(FileName, (Side == osRemote));
  1304. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1305. // this can happen when local drive is unplugged in the middle of the operation
  1306. if (Item != NULL)
  1307. {
  1308. if (Success) Item->Selected = false;
  1309. DView->MakeProgressVisible(Item);
  1310. }
  1311. }
  1312. }
  1313. if ((Operation == foCopy) || (Operation == foMove))
  1314. {
  1315. if (Side == osLocal)
  1316. {
  1317. Configuration->Usage->Inc(L"UploadedFiles");
  1318. }
  1319. else
  1320. {
  1321. Configuration->Usage->Inc(L"DownloadedFiles");
  1322. }
  1323. }
  1324. }
  1325. if (Success && (FSynchronizeController != NULL))
  1326. {
  1327. if (Operation == foCopy)
  1328. {
  1329. DebugAssert(Side == osLocal);
  1330. FSynchronizeController->LogOperation(soUpload, FileName);
  1331. }
  1332. else if (Operation == foDelete)
  1333. {
  1334. DebugAssert(Side == osRemote);
  1335. FSynchronizeController->LogOperation(soDelete, FileName);
  1336. }
  1337. }
  1338. if (FProgressForm)
  1339. {
  1340. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1341. }
  1342. }
  1343. //---------------------------------------------------------------------------
  1344. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1345. TFileOperation Operation, TOperationSide Side,
  1346. bool Temp, const UnicodeString & FileName, Boolean Success,
  1347. TOnceDoneOperation & OnceDoneOperation)
  1348. {
  1349. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1350. OnceDoneOperation);
  1351. }
  1352. //---------------------------------------------------------------------------
  1353. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1354. {
  1355. DebugAssert(GetSide(Side) == osRemote);
  1356. DebugUsedParam(Side);
  1357. return RemoteDirView;
  1358. }
  1359. //---------------------------------------------------------------------------
  1360. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1361. {
  1362. DebugAssert(GetSide(Side) == osRemote);
  1363. DebugUsedParam(Side);
  1364. return (Terminal != NULL);
  1365. }
  1366. //---------------------------------------------------------------------------
  1367. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1368. TOperationSide Side, int FilesOnly)
  1369. {
  1370. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1371. }
  1372. //---------------------------------------------------------------------------
  1373. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1374. TOperationSide Side, int FilesOnly)
  1375. {
  1376. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1377. }
  1378. //---------------------------------------------------------------------------
  1379. struct THistoryItemData
  1380. {
  1381. short int Side;
  1382. short int Index;
  1383. };
  1384. //---------------------------------------------------------------------------
  1385. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1386. {
  1387. DirView(Side)->HistoryGo(Index);
  1388. }
  1389. //---------------------------------------------------------------------------
  1390. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1391. {
  1392. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1393. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1394. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1395. }
  1396. //---------------------------------------------------------------------------
  1397. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1398. bool Back)
  1399. {
  1400. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1401. {
  1402. TCustomDirView * DView = DirView(Side);
  1403. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1404. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1405. if (ICount > 10)
  1406. {
  1407. ICount = 10;
  1408. }
  1409. Menu->Items->Clear();
  1410. THistoryItemData Data;
  1411. Data.Side = Side;
  1412. for (short int i = 1; i <= ICount; i++)
  1413. {
  1414. TTBCustomItem * Item = new TTBXItem(Menu);
  1415. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1416. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, (Side == osRemote));
  1417. Item->Hint = DView->HistoryPath[Data.Index];
  1418. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1419. Item->Tag = *reinterpret_cast<int*>(&Data);
  1420. Item->OnClick = HistoryItemClick;
  1421. Menu->Items->Add(Item);
  1422. }
  1423. }
  1424. }
  1425. //---------------------------------------------------------------------------
  1426. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1427. TOperationSide Side, bool Back)
  1428. {
  1429. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1430. {
  1431. // workaround
  1432. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1433. // operator new allocates memory in Pascal code, but calls inline
  1434. // contructor in C++, leading in problems, the function does
  1435. // both in Pascal code
  1436. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1437. UpdateHistoryMenu(Side, Back);
  1438. }
  1439. return FHistoryMenu[Side == osLocal][Back];
  1440. }
  1441. //---------------------------------------------------------------------------
  1442. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1443. TCustomDirView *Sender)
  1444. {
  1445. TOperationSide Side = (Sender == DirView(osRemote) ? osRemote : osLocal);
  1446. UpdateHistoryMenu(Side, true);
  1447. UpdateHistoryMenu(Side, false);
  1448. }
  1449. //---------------------------------------------------------------------------
  1450. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1451. {
  1452. // remote custom commands can be executed only if the server supports shell commands
  1453. // or have secondary shell
  1454. return (FTerminal != NULL) && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1455. }
  1456. //---------------------------------------------------------------------------
  1457. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1458. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1459. {
  1460. int Result;
  1461. TFileCustomCommand RemoteCustomCommand;
  1462. TLocalCustomCommand LocalCustomCommand;
  1463. TFileCustomCommand * NonInteractiveCustomCommand =
  1464. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1465. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1466. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1467. if (FLAGCLEAR(Command.Params, ccLocal))
  1468. {
  1469. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1470. // custom command that does not operate with files can be executed anytime ...
  1471. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1472. {
  1473. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1474. {
  1475. // ... but do not show such command in remote file menu (TODO)
  1476. Result = AllowedState;
  1477. }
  1478. else
  1479. {
  1480. Result = -1;
  1481. }
  1482. }
  1483. else
  1484. {
  1485. if ((ListType == ccltAll) || (ListType == ccltFile))
  1486. {
  1487. Result = ((FCurrentSide == osRemote) && DirView(osRemote)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1488. }
  1489. else
  1490. {
  1491. Result = -1;
  1492. }
  1493. }
  1494. }
  1495. else
  1496. {
  1497. // custom command that does not operate with files can be executed anytime
  1498. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1499. {
  1500. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1501. }
  1502. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1503. {
  1504. if ((ListType == ccltAll) || (ListType == ccltFile))
  1505. {
  1506. // special case is "diff"-style command that can be executed over any side,
  1507. // if we have both sides
  1508. Result =
  1509. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1510. // directly (instead of EnableSelectedOperation) to pass
  1511. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1512. ((HasDirView[osLocal] && DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1513. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1514. }
  1515. else if (ListType == ccltBoth)
  1516. {
  1517. Result = 1;
  1518. }
  1519. else
  1520. {
  1521. Result = -1;
  1522. }
  1523. }
  1524. else
  1525. {
  1526. if ((ListType == ccltAll) || (ListType == ccltFile))
  1527. {
  1528. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1529. }
  1530. else
  1531. {
  1532. Result = -1;
  1533. }
  1534. }
  1535. }
  1536. return Result;
  1537. }
  1538. //---------------------------------------------------------------------------
  1539. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1540. TStrings * FileList, const TCustomCommandType & ACommand,
  1541. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1542. {
  1543. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1544. {
  1545. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1546. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1547. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1548. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1549. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1550. bool Capture =
  1551. FLAGSET(ACommand.Params, ccShowResults) ||
  1552. FLAGSET(ACommand.Params, ccCopyResults) ||
  1553. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1554. TCaptureOutputEvent OutputEvent = NULL;
  1555. DebugAssert(FCapturedLog == NULL);
  1556. if (Capture)
  1557. {
  1558. FCapturedLog = new TStringList();
  1559. OutputEvent = TerminalCaptureLog;
  1560. }
  1561. try
  1562. {
  1563. if (!RemoteCustomCommand.IsFileCommand(Command))
  1564. {
  1565. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1566. OutputEvent);
  1567. }
  1568. else
  1569. {
  1570. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1571. }
  1572. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1573. {
  1574. if (FLAGSET(ACommand.Params, ccCopyResults))
  1575. {
  1576. CopyToClipboard(FCapturedLog);
  1577. }
  1578. if (FLAGSET(ACommand.Params, ccShowResults))
  1579. {
  1580. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1581. }
  1582. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1583. {
  1584. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1585. }
  1586. }
  1587. }
  1588. __finally
  1589. {
  1590. SAFE_DESTROY(FCapturedLog);
  1591. }
  1592. }
  1593. }
  1594. //---------------------------------------------------------------------------
  1595. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1596. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1597. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1598. {
  1599. TStrings * LocalFileList = NULL;
  1600. TStrings * RemoteFileList = NULL;
  1601. TStrings * RemoteFileListFull = NULL;
  1602. try
  1603. {
  1604. if (LocalFileCommand)
  1605. {
  1606. if (ALocalFileList == NULL)
  1607. {
  1608. DebugAssert(HasDirView[osLocal]);
  1609. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1610. // directly (instead of EnableSelectedOperation) to pass
  1611. // false to FocusedFileOnlyWhenFocused
  1612. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1613. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1614. }
  1615. else
  1616. {
  1617. LocalFileList = ALocalFileList;
  1618. }
  1619. if (FileListCommand)
  1620. {
  1621. if (LocalFileList->Count != 1)
  1622. {
  1623. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1624. }
  1625. }
  1626. else
  1627. {
  1628. if ((LocalFileList->Count != 1) &&
  1629. (FileList->Count != 1) &&
  1630. (LocalFileList->Count != FileList->Count))
  1631. {
  1632. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1633. }
  1634. }
  1635. }
  1636. UnicodeString RootTempDir;
  1637. UnicodeString TempDir;
  1638. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1639. if (!RemoteFiles)
  1640. {
  1641. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1642. }
  1643. try
  1644. {
  1645. TDateTimes RemoteFileTimes;
  1646. if (RemoteFiles)
  1647. {
  1648. RemoteFileList = FileList;
  1649. }
  1650. else
  1651. {
  1652. RemoteFileList = new TStringList();
  1653. TMakeLocalFileListParams MakeFileListParam;
  1654. MakeFileListParam.FileList = RemoteFileList;
  1655. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1656. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1657. MakeFileListParam.Recursive =
  1658. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1659. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1660. if (!MakeFileListParam.Recursive)
  1661. {
  1662. RemoteFileTimes.clear();
  1663. RemoteFileListFull = new TStringList();
  1664. MakeFileListParam.FileList = RemoteFileListFull;
  1665. MakeFileListParam.Recursive = true;
  1666. MakeFileListParam.IncludeDirs = false;
  1667. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1668. }
  1669. else
  1670. {
  1671. RemoteFileListFull = RemoteFileList;
  1672. }
  1673. }
  1674. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1675. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1676. if (!NonBlocking)
  1677. {
  1678. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : FileList->Count);
  1679. DebugAssert(FProgressForm != NULL);
  1680. FProgressForm->ReadOnly = true;
  1681. }
  1682. try
  1683. {
  1684. if (FileListCommand)
  1685. {
  1686. UnicodeString LocalFile;
  1687. // MakeFileList does not delimit filenames
  1688. UnicodeString FileList = MakeFileList(RemoteFileList);
  1689. if (LocalFileCommand)
  1690. {
  1691. DebugAssert(LocalFileList->Count == 1);
  1692. LocalFile = LocalFileList->Strings[0];
  1693. }
  1694. TLocalCustomCommand CustomCommand(Data,
  1695. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1696. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1697. if (NonBlocking)
  1698. {
  1699. DebugAssert(!POutput);
  1700. ExecuteShellChecked(ShellCommand);
  1701. }
  1702. else
  1703. {
  1704. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1705. }
  1706. }
  1707. else if (LocalFileCommand)
  1708. {
  1709. if (LocalFileList->Count == 1)
  1710. {
  1711. UnicodeString LocalFile = LocalFileList->Strings[0];
  1712. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1713. {
  1714. UnicodeString FileName = RemoteFileList->Strings[Index];
  1715. TLocalCustomCommand CustomCommand(Data,
  1716. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1717. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1718. }
  1719. }
  1720. else if (RemoteFileList->Count == 1)
  1721. {
  1722. UnicodeString FileName = RemoteFileList->Strings[0];
  1723. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1724. {
  1725. TLocalCustomCommand CustomCommand(
  1726. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1727. FileName, LocalFileList->Strings[Index], L"");
  1728. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1729. }
  1730. }
  1731. else
  1732. {
  1733. if (LocalFileList->Count != RemoteFileList->Count)
  1734. {
  1735. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1736. }
  1737. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1738. {
  1739. UnicodeString FileName = RemoteFileList->Strings[Index];
  1740. TLocalCustomCommand CustomCommand(
  1741. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1742. FileName, LocalFileList->Strings[Index], L"");
  1743. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1744. }
  1745. }
  1746. }
  1747. else
  1748. {
  1749. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1750. {
  1751. TLocalCustomCommand CustomCommand(Data,
  1752. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1753. RemoteFileList->Strings[Index], L"", L"");
  1754. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1755. }
  1756. }
  1757. }
  1758. __finally
  1759. {
  1760. if (!NonBlocking)
  1761. {
  1762. Progress.Stop();
  1763. }
  1764. }
  1765. DebugAssert(!FAutoOperation);
  1766. if (!RemoteFiles)
  1767. {
  1768. TempDir = IncludeTrailingBackslash(TempDir);
  1769. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1770. {
  1771. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1772. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1773. // Skip directories, process only nested files.
  1774. // The check is redundant as FileAge fails for directories anyway.
  1775. !DirectoryExists(FileName))
  1776. {
  1777. UnicodeString RemoteDir =
  1778. UnixExtractFileDir(
  1779. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  1780. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  1781. TDateTime NewTime;
  1782. if (FileAge(FileName, NewTime) &&
  1783. (NewTime != RemoteFileTimes[Index]))
  1784. {
  1785. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  1786. TemporaryFileCopyParam(CopyParam);
  1787. CopyParam.FileMask = L"";
  1788. FAutoOperation = true;
  1789. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  1790. TemporaryFilesList->Add(FileName);
  1791. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  1792. }
  1793. }
  1794. }
  1795. }
  1796. }
  1797. __finally
  1798. {
  1799. FAutoOperation = false;
  1800. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  1801. {
  1802. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  1803. }
  1804. }
  1805. }
  1806. __finally
  1807. {
  1808. if (RemoteFileList != FileList)
  1809. {
  1810. delete RemoteFileList;
  1811. }
  1812. if (LocalFileList != ALocalFileList)
  1813. {
  1814. delete LocalFileList;
  1815. }
  1816. if (RemoteFileListFull != RemoteFileList)
  1817. {
  1818. delete RemoteFileListFull;
  1819. }
  1820. }
  1821. }
  1822. //---------------------------------------------------------------------------
  1823. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithRemoteFiles(
  1824. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  1825. bool FileListCommand, UnicodeString * POutput)
  1826. {
  1827. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  1828. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  1829. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  1830. {
  1831. UnicodeString FileName = SelectedFileList->Strings[Index];
  1832. if (DirectoryExists(FileName))
  1833. {
  1834. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  1835. {
  1836. LocalFileList->Add(FileName);
  1837. }
  1838. if (FLAGSET(ACommand.Params, ccRecursive))
  1839. {
  1840. TMakeLocalFileListParams MakeFileListParam;
  1841. MakeFileListParam.FileList = LocalFileList.get();
  1842. MakeFileListParam.FileTimes = NULL;
  1843. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1844. MakeFileListParam.Recursive = true;
  1845. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  1846. }
  1847. }
  1848. else
  1849. {
  1850. LocalFileList->Add(FileName);
  1851. }
  1852. }
  1853. if (FileListCommand)
  1854. {
  1855. UnicodeString FileList = MakeFileList(LocalFileList.get());
  1856. TLocalCustomCommand CustomCommand(
  1857. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1858. L"", L"", FileList);
  1859. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1860. }
  1861. else
  1862. {
  1863. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1864. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : LocalFileList->Count);
  1865. DebugAssert(FProgressForm != NULL);
  1866. FProgressForm->ReadOnly = true;
  1867. try
  1868. {
  1869. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1870. {
  1871. UnicodeString FileName = LocalFileList->Strings[Index];
  1872. TLocalCustomCommand CustomCommand(
  1873. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1874. FileName, L"", L"");
  1875. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1876. }
  1877. }
  1878. __finally
  1879. {
  1880. Progress.Stop();
  1881. }
  1882. }
  1883. }
  1884. //---------------------------------------------------------------------------
  1885. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  1886. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  1887. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1888. {
  1889. TLocalCustomCommand LocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory());
  1890. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1891. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  1892. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1893. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  1894. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  1895. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  1896. std::unique_ptr<UnicodeString> POutput;
  1897. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  1898. FLAGSET(ACommand.Params, ccCopyResults))
  1899. {
  1900. POutput.reset(new UnicodeString());
  1901. }
  1902. if (!LocalCustomCommand.IsFileCommand(Command))
  1903. {
  1904. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  1905. }
  1906. // remote files?
  1907. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  1908. {
  1909. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  1910. }
  1911. // local files
  1912. else
  1913. {
  1914. LocalCustomCommandWithRemoteFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  1915. }
  1916. if (POutput.get() != NULL)
  1917. {
  1918. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  1919. int P = POutput->Pos(sLineBreak);
  1920. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  1921. {
  1922. POutput->SetLength(P - 1);
  1923. }
  1924. // Copy even empty output to clipboard
  1925. if (FLAGSET(ACommand.Params, ccCopyResults))
  1926. {
  1927. CopyToClipboard(*POutput);
  1928. }
  1929. // But do not show an empty message box.
  1930. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  1931. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  1932. !POutput->IsEmpty())
  1933. {
  1934. TClipboardHandler ClipboardHandler;
  1935. ClipboardHandler.Text = *POutput;
  1936. TMessageParams Params;
  1937. TQueryButtonAlias Aliases[1];
  1938. Aliases[0].Button = qaRetry;
  1939. Aliases[0].Alias = LoadStr(URL_LINK_COPY); // misuse
  1940. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  1941. Params.Aliases = Aliases;
  1942. Params.AliasesCount = LENOF(Aliases);
  1943. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  1944. }
  1945. }
  1946. }
  1947. //---------------------------------------------------------------------------
  1948. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  1949. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  1950. {
  1951. TCustomCommandData Data(Terminal);
  1952. UnicodeString Site = Terminal->SessionData->SessionKey;
  1953. UnicodeString HelpKeyword = ACommand.HomePage;
  1954. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  1955. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  1956. {
  1957. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  1958. if (FLAGCLEAR(ACommand.Params, ccLocal))
  1959. {
  1960. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, Terminal->CurrentDirectory));
  1961. }
  1962. else
  1963. {
  1964. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory()));
  1965. }
  1966. if (!DoCustomCommandOptionsDialog(
  1967. &ACommand, CustomCommandOptions.get(), TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site))
  1968. {
  1969. Abort();
  1970. }
  1971. }
  1972. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  1973. if (FLAGCLEAR(ACommand.Params, ccLocal))
  1974. {
  1975. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  1976. }
  1977. else
  1978. {
  1979. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  1980. }
  1981. }
  1982. //---------------------------------------------------------------------------
  1983. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  1984. const TCustomCommandType & Command)
  1985. {
  1986. DebugAssert(FCustomCommandLocalFileList != NULL);
  1987. DebugAssert(FCustomCommandRemoteFileList != NULL);
  1988. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  1989. TStrings * LocalFileList = new TStringList();
  1990. TStrings * RemoteFileList = new TStringList();
  1991. try
  1992. {
  1993. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  1994. {
  1995. LocalFileList->Clear();
  1996. LocalFileList->AddObject(
  1997. FCustomCommandLocalFileList->Strings[Index],
  1998. FCustomCommandLocalFileList->Objects[Index]);
  1999. RemoteFileList->Clear();
  2000. RemoteFileList->AddObject(
  2001. FCustomCommandRemoteFileList->Strings[Index],
  2002. FCustomCommandRemoteFileList->Objects[Index]);
  2003. CustomCommand(RemoteFileList, Command, LocalFileList);
  2004. }
  2005. }
  2006. __finally
  2007. {
  2008. delete LocalFileList;
  2009. delete RemoteFileList;
  2010. }
  2011. }
  2012. //---------------------------------------------------------------------------
  2013. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2014. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2015. {
  2016. delete FCustomCommandLocalFileList;
  2017. delete FCustomCommandRemoteFileList;
  2018. // takeover ownership,
  2019. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2020. FCustomCommandLocalFileList = LocalFileList;
  2021. FCustomCommandRemoteFileList = RemoteFileList;
  2022. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2023. if (Button != NULL)
  2024. {
  2025. FCustomCommandMenu->Items->Clear();
  2026. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
  2027. MenuPopup(FCustomCommandMenu, Button);
  2028. }
  2029. else
  2030. {
  2031. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2032. }
  2033. }
  2034. //---------------------------------------------------------------------------
  2035. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2036. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2037. {
  2038. DebugAssert(FCapturedLog != NULL);
  2039. if ((OutputType == cotOutput) || (OutputType == cotError))
  2040. {
  2041. FCapturedLog->Add(AddedLine);
  2042. }
  2043. }
  2044. //---------------------------------------------------------------------------
  2045. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2046. TOperationSide Side)
  2047. {
  2048. return (Side == osRemote) &&
  2049. ((Control == RemoteDirView) || (Control == RemoteDriveView));
  2050. }
  2051. //---------------------------------------------------------------------------
  2052. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2053. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2054. {
  2055. TTBItemOptions O;
  2056. O = Item->Options;
  2057. TCustomDirView * DView = DirView(Side);
  2058. if ((DView->ItemFocused != NULL) &&
  2059. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2060. // when resolving links is disabled, default action is to enter the directory,
  2061. // no matter what DoubleClickAction is configured to
  2062. ((Side != osRemote) || Terminal->ResolvingSymlinks) &&
  2063. // Can only Edit files, but can Open/Copy even directories
  2064. ((DoubleClickAction != dcaEdit) ||
  2065. !DView->ItemIsDirectory(DView->ItemFocused)))
  2066. {
  2067. Item->Options = O << tboDefault;
  2068. }
  2069. else
  2070. {
  2071. Item->Options = O >> tboDefault;
  2072. }
  2073. }
  2074. //---------------------------------------------------------------------------
  2075. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2076. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2077. {
  2078. TCustomDirView * DView = DirView(Side);
  2079. TListItem * Item = DView->ItemFocused;
  2080. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2081. EnableFocusedOperation[Side])
  2082. {
  2083. TPoint ClientPoint;
  2084. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2085. TPoint(0, 0) : MousePos;
  2086. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2087. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2088. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2089. }
  2090. }
  2091. //---------------------------------------------------------------------------
  2092. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2093. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2094. {
  2095. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2096. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2097. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2098. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2099. Handled = true;
  2100. }
  2101. //---------------------------------------------------------------------------
  2102. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2103. {
  2104. }
  2105. //---------------------------------------------------------------------------
  2106. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2107. {
  2108. DebugAssert(FErrorList == NULL);
  2109. if (WinConfiguration->ContinueOnError)
  2110. {
  2111. FErrorList = new TStringList();
  2112. Configuration->Usage->Inc(L"ContinuationsOnError");
  2113. }
  2114. NonVisualDataModule->StartBusy();
  2115. }
  2116. //---------------------------------------------------------------------------
  2117. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2118. {
  2119. NonVisualDataModule->EndBusy();
  2120. if (FErrorList)
  2121. {
  2122. HandleErrorList(FErrorList);
  2123. }
  2124. }
  2125. //---------------------------------------------------------------------------
  2126. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2127. const TCopyParamType & CopyParam)
  2128. {
  2129. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2130. {
  2131. Configuration->Usage->Inc(L"FileMaskUses");
  2132. }
  2133. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2134. {
  2135. Configuration->Usage->Inc(L"OperationMaskUses");
  2136. }
  2137. }
  2138. //---------------------------------------------------------------------------
  2139. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2140. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2141. {
  2142. void * BatchStorage;
  2143. BatchStart(BatchStorage);
  2144. bool Result;
  2145. try
  2146. {
  2147. if ((Operation == foCopy) || (Operation == foMove))
  2148. {
  2149. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2150. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2151. UnicodeString TargetDirectory;
  2152. bool Temp = false;
  2153. bool DragDrop = false;
  2154. int Options = 0;
  2155. TAutoSwitch UseQueue = asAuto;
  2156. if (Param != NULL)
  2157. {
  2158. TTransferOperationParam& TParam =
  2159. *static_cast<TTransferOperationParam*>(Param);
  2160. TargetDirectory = TParam.TargetDirectory;
  2161. Temp = TParam.Temp;
  2162. DragDrop = TParam.DragDrop;
  2163. Options = TParam.Options;
  2164. UseQueue = TParam.Queue;
  2165. }
  2166. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2167. switch (UseQueue)
  2168. {
  2169. case asOn:
  2170. CopyParam.Queue = true;
  2171. break;
  2172. case asOff:
  2173. CopyParam.Queue = false;
  2174. break;
  2175. case asAuto:
  2176. default:
  2177. // keep default
  2178. break;
  2179. }
  2180. Result =
  2181. CopyParamDialog(Direction, Type, Temp, FileList, TargetDirectory,
  2182. CopyParam, !NoConfirmation, DragDrop, Options);
  2183. if (Result)
  2184. {
  2185. DebugAssert(Terminal);
  2186. bool SelectionRestored = false;
  2187. TCustomDirView * DView = NULL;
  2188. if (HasDirView[Side])
  2189. {
  2190. DView = DirView(Side);
  2191. DView->SaveSelection();
  2192. DView->SaveSelectedNames();
  2193. }
  2194. UpdateCopyParamCounters(CopyParam);
  2195. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2196. DebugAssert(FTransferResumeList == NULL);
  2197. FTransferResumeList =
  2198. Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2199. FMoveToQueue = false;
  2200. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2201. try
  2202. {
  2203. TStrings * PermanentFileList;
  2204. std::unique_ptr<TStrings> PermanentFileListOwner;
  2205. try
  2206. {
  2207. if (Side == osLocal)
  2208. {
  2209. PermanentFileList = FileList;
  2210. Params |= FLAGMASK(Temp, cpTemporary);
  2211. Terminal->CopyToRemote(FileList, TargetDirectory, &CopyParam, Params, NULL);
  2212. if (Operation == foMove)
  2213. {
  2214. ReloadLocalDirectory();
  2215. if (DView != NULL)
  2216. {
  2217. DView->RestoreSelection();
  2218. }
  2219. SelectionRestored = true;
  2220. }
  2221. }
  2222. else
  2223. {
  2224. // Clone the file list as it may refer to current directory files,
  2225. // which get destroyed, when the source directory is reloaded after foMove operation.
  2226. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2227. // But for a hotfix, we are not going to do such a big change.
  2228. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2229. PermanentFileList = PermanentFileListOwner.get();
  2230. try
  2231. {
  2232. Terminal->CopyToLocal(
  2233. FileList, TargetDirectory, &CopyParam, Params, NULL);
  2234. }
  2235. __finally
  2236. {
  2237. if (Operation == foMove)
  2238. {
  2239. if (DView != NULL)
  2240. {
  2241. DView->RestoreSelection();
  2242. }
  2243. SelectionRestored = true;
  2244. }
  2245. ReloadLocalDirectory(TargetDirectory);
  2246. }
  2247. }
  2248. }
  2249. catch (EAbort &)
  2250. {
  2251. if (FMoveToQueue)
  2252. {
  2253. Params |=
  2254. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2255. DebugAssert(CopyParam.TransferSkipList == NULL);
  2256. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2257. if (TransferResumeList->Count > 0)
  2258. {
  2259. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2260. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2261. }
  2262. CopyParam.TransferSkipList = TransferResumeList.release();
  2263. // not really needed, just to keep it consistent with TransferResumeList
  2264. FTransferResumeList = NULL;
  2265. FMoveToQueue = false;
  2266. Configuration->Usage->Inc("MovesToBackground");
  2267. AddQueueItem(Queue, Direction, PermanentFileList, TargetDirectory, CopyParam, Params);
  2268. ClearTransferSourceSelection(Direction);
  2269. }
  2270. throw;
  2271. }
  2272. }
  2273. __finally
  2274. {
  2275. if (!SelectionRestored && (DView != NULL))
  2276. {
  2277. DView->DiscardSavedSelection();
  2278. }
  2279. FTransferResumeList = NULL;
  2280. }
  2281. }
  2282. }
  2283. else if (Operation == foRename)
  2284. {
  2285. DebugAssert(DirView(Side)->ItemFocused);
  2286. DirView(Side)->ItemFocused->EditCaption();
  2287. Result = true;
  2288. }
  2289. else if (Operation == foDelete)
  2290. {
  2291. DebugAssert(FileList->Count);
  2292. // We deliberately do not toggle alternative flag (Param), but use OR,
  2293. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2294. // shortcut of CurrentDeleteAlternativeAction
  2295. bool Alternative =
  2296. bool(Param) || UseAlternativeFunction();
  2297. bool Recycle;
  2298. if (Side == osLocal)
  2299. {
  2300. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2301. }
  2302. else
  2303. {
  2304. Recycle = (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2305. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2306. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2307. }
  2308. Result =
  2309. !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2310. if (!Result)
  2311. {
  2312. UnicodeString Query;
  2313. if (FileList->Count == 1)
  2314. {
  2315. if (Side == osLocal)
  2316. {
  2317. Query = ExtractFileName(FileList->Strings[0]);
  2318. }
  2319. else
  2320. {
  2321. Query = UnixExtractFileName(FileList->Strings[0]);
  2322. }
  2323. Query = FMTLOAD(
  2324. (Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2325. }
  2326. else
  2327. {
  2328. Query = FMTLOAD(
  2329. (Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2330. }
  2331. TMessageParams Params(mpNeverAskAgainCheck);
  2332. Params.ImageName = L"Delete file";
  2333. unsigned int Answer = MessageDialog(MainInstructions(Query), qtConfirmation,
  2334. qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2335. if (Answer == qaNeverAskAgain)
  2336. {
  2337. Result = true;
  2338. if (Recycle)
  2339. {
  2340. WinConfiguration->ConfirmRecycling = false;
  2341. }
  2342. else
  2343. {
  2344. WinConfiguration->ConfirmDeleting = false;
  2345. }
  2346. }
  2347. else
  2348. {
  2349. Result = (Answer == qaOK);
  2350. }
  2351. }
  2352. if (Result)
  2353. {
  2354. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2355. }
  2356. }
  2357. else if (Operation == foSetProperties)
  2358. {
  2359. RemoteDirView->SaveSelectedNames();
  2360. Result = SetProperties(Side, FileList);
  2361. }
  2362. else if (Operation == foCustomCommand)
  2363. {
  2364. DebugAssert(Param);
  2365. DebugAssert(Side == osRemote);
  2366. RemoteDirView->SaveSelectedNames();
  2367. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2368. CustomCommand(FileList, *Command, NULL);
  2369. Result = true;
  2370. }
  2371. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2372. {
  2373. DebugAssert(Side == osRemote);
  2374. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2375. (Operation == foRemoteMove), reinterpret_cast<TTerminal *>(Param));
  2376. }
  2377. else if (Operation == foLock)
  2378. {
  2379. DebugAssert(Side == osRemote);
  2380. LockFiles(FileList, true);
  2381. Result = true;
  2382. }
  2383. else if (Operation == foUnlock)
  2384. {
  2385. DebugAssert(Side == osRemote);
  2386. LockFiles(FileList, false);
  2387. Result = true;
  2388. }
  2389. else
  2390. {
  2391. DebugFail();
  2392. }
  2393. }
  2394. __finally
  2395. {
  2396. BatchEnd(BatchStorage);
  2397. }
  2398. return Result;
  2399. }
  2400. //---------------------------------------------------------------------------
  2401. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2402. {
  2403. if (Side == osCurrent)
  2404. {
  2405. Side = FCurrentSide;
  2406. }
  2407. return Side;
  2408. }
  2409. //---------------------------------------------------------------------------
  2410. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2411. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2412. {
  2413. Side = GetSide(Side);
  2414. bool Result;
  2415. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, (Side == osLocal), NULL);
  2416. try
  2417. {
  2418. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2419. }
  2420. __finally
  2421. {
  2422. delete FileList;
  2423. }
  2424. return Result;
  2425. }
  2426. //---------------------------------------------------------------------------
  2427. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2428. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2429. bool NoConfirmation, void * Param)
  2430. {
  2431. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2432. {
  2433. if ((Operation == foCopy) || (Operation == foMove))
  2434. {
  2435. if (GetSide(Side) == osLocal)
  2436. {
  2437. Configuration->Usage->Inc(L"UploadsCommand");
  2438. }
  2439. else
  2440. {
  2441. Configuration->Usage->Inc(L"DownloadsCommand");
  2442. }
  2443. }
  2444. }
  2445. }
  2446. //---------------------------------------------------------------------------
  2447. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2448. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2449. {
  2450. TTransferOperationParam Param;
  2451. if ((WinConfiguration->Interface != ifCommander) ||
  2452. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2453. {
  2454. Flags &= ~cocShortCutHint;
  2455. }
  2456. TCustomDirView * DView = DirView(Side);
  2457. Param.Options =
  2458. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2459. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2460. if (FLAGSET(Flags, cocQueue))
  2461. {
  2462. Param.Queue = asOn;
  2463. }
  2464. else if (FLAGSET(Flags, cocNonQueue))
  2465. {
  2466. Param.Queue = asOff;
  2467. }
  2468. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2469. }
  2470. //---------------------------------------------------------------------------
  2471. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2472. {
  2473. try
  2474. {
  2475. if (ErrorList->Count)
  2476. {
  2477. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2478. if (MessageDialog(Message, qtError,
  2479. qaOK | qaCancel, HELP_NONE) == qaOK)
  2480. {
  2481. unsigned int Answer;
  2482. int Index = 0;
  2483. do
  2484. {
  2485. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2486. TQueryButtonAlias Aliases[2];
  2487. Aliases[0].Button = qaYes;
  2488. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2489. Aliases[1].Button = qaNo;
  2490. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2491. TMessageParams Params;
  2492. Params.Aliases = Aliases;
  2493. Params.AliasesCount = LENOF(Aliases);
  2494. Answer = MoreMessageDialog(
  2495. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2496. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2497. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2498. qaOK, HELP_NONE, &Params);
  2499. if (Answer == qaNo)
  2500. {
  2501. Index++;
  2502. }
  2503. if (Answer == qaYes)
  2504. {
  2505. Index--;
  2506. }
  2507. }
  2508. while (Answer != qaOK);
  2509. }
  2510. }
  2511. }
  2512. __finally
  2513. {
  2514. TStrings * List = ErrorList;
  2515. ErrorList = NULL;
  2516. for (int i = 0; i < List->Count; i++)
  2517. {
  2518. delete List->Objects[i];
  2519. }
  2520. delete List;
  2521. }
  2522. }
  2523. //---------------------------------------------------------------------------
  2524. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2525. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2526. {
  2527. // needed for checking filemasks, as there's no directory object
  2528. // associated with the file object
  2529. File->FullFileName = FullFileName;
  2530. TFileMasks::TParams MaskParams;
  2531. MaskParams.Size = File->Size;
  2532. MaskParams.Modification = File->Modification;
  2533. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2534. }
  2535. //---------------------------------------------------------------------------
  2536. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2537. {
  2538. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2539. Side = GetSide(Side);
  2540. TCustomDirView * DView = DirView(Side);
  2541. TListItem * FocusedItem = DView->ItemFocused;
  2542. UnicodeString Name;
  2543. if ((FocusedItem != NULL) && !DView->ItemIsParentDirectory(FocusedItem))
  2544. {
  2545. Name = DView->ItemFileName(FocusedItem);
  2546. }
  2547. else
  2548. {
  2549. Name = LoadStr(NEW_FILE);
  2550. }
  2551. UnicodeString Names = Name;
  2552. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2553. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2554. HELP_EDIT_NEW, History.get(), true))
  2555. {
  2556. while (!Names.IsEmpty())
  2557. {
  2558. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2559. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2560. UnicodeString TargetFileName;
  2561. UnicodeString LocalFileName;
  2562. UnicodeString RootTempDir;
  2563. UnicodeString TempDir;
  2564. UnicodeString RemoteDirectory;
  2565. bool ExistingFile = false;
  2566. if (Side == osRemote)
  2567. {
  2568. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2569. TRemoteFile * File = NULL;
  2570. if (FTerminal->FileExists(Name, &File))
  2571. {
  2572. try
  2573. {
  2574. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2575. ExistingFile = true;
  2576. }
  2577. __finally
  2578. {
  2579. delete File;
  2580. }
  2581. }
  2582. if (!ExistingFile)
  2583. {
  2584. RemoteDirectory = UnixExtractFilePath(Name);
  2585. TemporaryDirectoryForRemoteFiles(
  2586. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2587. TargetFileName = UnixExtractFileName(Name);
  2588. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2589. LocalFileName = TempDir +
  2590. // We probably do not want to trim the VMS version here
  2591. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2592. }
  2593. }
  2594. else
  2595. {
  2596. if (ExtractFilePath(Name).IsEmpty())
  2597. {
  2598. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2599. }
  2600. else
  2601. {
  2602. LocalFileName = ExpandFileName(Name);
  2603. }
  2604. TargetFileName = ExtractFileName(Name);
  2605. }
  2606. if (!ExistingFile)
  2607. {
  2608. if (!FileExists(ApiPath(LocalFileName)))
  2609. {
  2610. int File = FileCreate(ApiPath(LocalFileName));
  2611. if (File < 0)
  2612. {
  2613. if (!RootTempDir.IsEmpty())
  2614. {
  2615. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2616. }
  2617. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2618. }
  2619. else
  2620. {
  2621. FileClose(File);
  2622. }
  2623. }
  2624. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2625. const TEditorData * ExternalEditor = NULL;
  2626. TFileMasks::TParams MaskParams; // size not known
  2627. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2628. false, MaskParams);
  2629. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2630. ExternalEditor, RootTempDir, RemoteDirectory);
  2631. }
  2632. }
  2633. }
  2634. }
  2635. //---------------------------------------------------------------------------
  2636. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2637. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2638. {
  2639. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2640. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2641. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2642. return
  2643. ((ExecuteFileBy == efInternalEditor)) ||
  2644. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2645. }
  2646. //---------------------------------------------------------------------------
  2647. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2648. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2649. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2650. UnicodeString RemoteDirectory)
  2651. {
  2652. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2653. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2654. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2655. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2656. Side = GetSide(Side);
  2657. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2658. if (Side == osRemote)
  2659. {
  2660. Data->Terminal = Terminal;
  2661. Data->Queue = Queue;
  2662. Data->SessionData = CloneCurrentSessionData();
  2663. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2664. Data->RemoteDirectory = RemoteDirectory;
  2665. Data->SessionName = Terminal->SessionData->SessionName;
  2666. Data->LocalRootDirectory = LocalRootDirectory;
  2667. Data->OriginalFileName = OriginalFileName;
  2668. Data->Command = L""; // will be changed later for external editor
  2669. }
  2670. if (ExecuteFileBy == efInternalEditor)
  2671. {
  2672. if (Side == osRemote)
  2673. {
  2674. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2675. L" - " + Terminal->SessionData->SessionName;
  2676. TForm * Editor;
  2677. try
  2678. {
  2679. Editor = ShowEditorForm(FileName, this, FEditorManager->FileChanged,
  2680. FEditorManager->FileReload, FEditorManager->FileClosed,
  2681. SaveAllInternalEditors, AnyInternalEditorModified,
  2682. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding);
  2683. }
  2684. catch(...)
  2685. {
  2686. if (!LocalRootDirectory.IsEmpty())
  2687. {
  2688. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2689. }
  2690. throw;
  2691. }
  2692. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2693. }
  2694. else
  2695. {
  2696. DebugAssert(!FStandaloneEditing);
  2697. TForm * Editor =
  2698. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2699. SaveAllInternalEditors, AnyInternalEditorModified,
  2700. L"", false, SessionColor, -1);
  2701. FLocalEditors->Add(Editor);
  2702. }
  2703. }
  2704. else
  2705. {
  2706. HANDLE Process;
  2707. if (ExecuteFileBy == efExternalEditor)
  2708. {
  2709. UnicodeString Program, Params, Dir;
  2710. Data->Command = ExternalEditor->ExternalEditor;
  2711. ReformatFileNameCommand(Data->Command);
  2712. SplitCommand(Data->Command, Program, Params, Dir);
  2713. Params = ExpandFileNameCommand(Params, FileName);
  2714. Program = ExpandEnvironmentVariables(Program);
  2715. if (!ExecuteShell(Program, Params, Process))
  2716. {
  2717. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2718. }
  2719. }
  2720. else
  2721. {
  2722. DebugAssert(Side == osRemote);
  2723. if (!ExecuteShell(FileName, L"", Process))
  2724. {
  2725. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2726. }
  2727. }
  2728. if ((Side == osLocal) ||
  2729. ((ExecuteFileBy == efShell) &&
  2730. !WinConfiguration->Editor.SDIShellEditor) ||
  2731. ((ExecuteFileBy == efExternalEditor) &&
  2732. !ExternalEditor->SDIExternalEditor))
  2733. {
  2734. // no need for handle
  2735. if (Process != NULL)
  2736. {
  2737. DebugCheck(CloseHandle(Process));
  2738. }
  2739. Process = INVALID_HANDLE_VALUE;
  2740. }
  2741. else
  2742. {
  2743. if (Process == NULL)
  2744. {
  2745. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2746. }
  2747. }
  2748. if (Side == osRemote)
  2749. {
  2750. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2751. }
  2752. }
  2753. }
  2754. //---------------------------------------------------------------------------
  2755. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2756. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2757. void * /*Arg*/)
  2758. {
  2759. if (Token != NULL)
  2760. {
  2761. EditorFormFileSave(static_cast<TForm *>(Token));
  2762. }
  2763. }
  2764. //---------------------------------------------------------------------------
  2765. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  2766. {
  2767. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  2768. {
  2769. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  2770. }
  2771. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  2772. }
  2773. //---------------------------------------------------------------------------
  2774. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  2775. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2776. void * Arg)
  2777. {
  2778. if ((Token != NULL) &&
  2779. IsEditorFormModified(static_cast<TForm *>(Token)))
  2780. {
  2781. *static_cast<bool *>(Arg) = true;
  2782. }
  2783. }
  2784. //---------------------------------------------------------------------------
  2785. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  2786. TObject * /*Sender*/, bool & Modified)
  2787. {
  2788. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  2789. {
  2790. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  2791. {
  2792. Modified = true;
  2793. }
  2794. }
  2795. if (!Modified)
  2796. {
  2797. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  2798. }
  2799. }
  2800. //---------------------------------------------------------------------------
  2801. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  2802. {
  2803. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  2804. }
  2805. //---------------------------------------------------------------------------
  2806. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  2807. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  2808. UnicodeString & Result, UnicodeString & RootDirectory)
  2809. {
  2810. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  2811. {
  2812. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  2813. Result = RootDirectory;
  2814. }
  2815. else
  2816. {
  2817. RootDirectory = L"";
  2818. Result = WinConfiguration->ExpandedTemporaryDirectory();
  2819. Result = IncludeTrailingBackslash(Result);
  2820. }
  2821. if (WinConfiguration->TemporaryDirectoryAppendSession)
  2822. {
  2823. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(Terminal->SessionData->SessionName));
  2824. }
  2825. if (WinConfiguration->TemporaryDirectoryAppendPath)
  2826. {
  2827. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  2828. {
  2829. RemoteDirectory.Delete(1, 1);
  2830. }
  2831. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  2832. }
  2833. if (!ForceDirectories(ApiPath(Result)))
  2834. {
  2835. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  2836. }
  2837. }
  2838. //---------------------------------------------------------------------------
  2839. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  2840. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  2841. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  2842. {
  2843. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2844. if (ForceText)
  2845. {
  2846. CopyParam.TransferMode = tmAscii;
  2847. }
  2848. TemporaryFileCopyParam(CopyParam);
  2849. if (AllFiles)
  2850. {
  2851. CopyParam.IncludeFileMask = TFileMasks();
  2852. }
  2853. if (TempDir.IsEmpty())
  2854. {
  2855. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  2856. }
  2857. DebugAssert(!FAutoOperation);
  2858. FAutoOperation = AutoOperation;
  2859. Terminal->ExceptionOnFail = true;
  2860. try
  2861. {
  2862. try
  2863. {
  2864. // turn off confirmations, as for MDI editors we may possibly download
  2865. // the same file over
  2866. Terminal->CopyToLocal(
  2867. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  2868. if (GetTargetNames)
  2869. {
  2870. for (int i = 0; i < FileList->Count; i++)
  2871. {
  2872. FileList->Strings[i] =
  2873. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  2874. }
  2875. }
  2876. }
  2877. catch(...)
  2878. {
  2879. if (!RootTempDir.IsEmpty())
  2880. {
  2881. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2882. }
  2883. throw;
  2884. }
  2885. }
  2886. __finally
  2887. {
  2888. FAutoOperation = false;
  2889. Terminal->ExceptionOnFail = false;
  2890. }
  2891. }
  2892. //---------------------------------------------------------------------------
  2893. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  2894. {
  2895. // Do not waste time checking for default editor list the next time,
  2896. // or testing if default editor is not notepad.
  2897. bool TryNextTime = false;
  2898. UnicodeString UsageState;
  2899. if (!WinConfiguration->EditorList->IsDefaultList())
  2900. {
  2901. UsageState = "H";
  2902. }
  2903. else
  2904. {
  2905. UnicodeString Executable;
  2906. UnicodeString ExecutableDescription;
  2907. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  2908. {
  2909. UnicodeString Message =
  2910. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  2911. unsigned int Answer =
  2912. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  2913. if (Answer != qaOK)
  2914. {
  2915. UsageState = "R";
  2916. }
  2917. else
  2918. {
  2919. UsageState = "A";
  2920. TEditorData EditorData;
  2921. EditorData.Editor = edExternal;
  2922. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  2923. EditorData.DecideExternalEditorText();
  2924. TEditorList EditorList;
  2925. EditorList = *WinConfiguration->EditorList;
  2926. EditorList.Insert(0, new TEditorPreferences(EditorData));
  2927. WinConfiguration->EditorList = &EditorList;
  2928. }
  2929. }
  2930. }
  2931. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  2932. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  2933. }
  2934. //---------------------------------------------------------------------------
  2935. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  2936. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  2937. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  2938. {
  2939. if (ExecuteFileBy == efDefaultEditor)
  2940. {
  2941. if (!WinConfiguration->OfferedEditorAutoConfig)
  2942. {
  2943. EditorAutoConfig();
  2944. }
  2945. const TEditorPreferences * Editor =
  2946. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  2947. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  2948. {
  2949. ExecuteFileBy = efInternalEditor;
  2950. ExternalEditor = NULL;
  2951. }
  2952. else if (Editor->Data->Editor == edOpen)
  2953. {
  2954. ExecuteFileBy = efShell;
  2955. ExternalEditor = NULL;
  2956. }
  2957. else
  2958. {
  2959. ExecuteFileBy = efExternalEditor;
  2960. ExternalEditor = Editor->Data;
  2961. }
  2962. }
  2963. }
  2964. //---------------------------------------------------------------------------
  2965. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  2966. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  2967. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  2968. {
  2969. UnicodeString OriginalFileName;
  2970. UnicodeString LocalRootDirectory;
  2971. UnicodeString RemoteDirectory;
  2972. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  2973. (Side == osLocal), MaskParams);
  2974. UnicodeString Counter;
  2975. UnicodeString LocalFileName;
  2976. if (Side == osRemote)
  2977. {
  2978. // We need to trim VMS version here, so that we use name without version
  2979. // when uploading back to create a new version of the file
  2980. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  2981. RemoteDirectory = UnixExtractFilePath(FullFileName);
  2982. TObject * Token = NULL;
  2983. UnicodeString LocalDirectory;
  2984. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  2985. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  2986. LocalDirectory))
  2987. {
  2988. if (Token != NULL)
  2989. {
  2990. TForm * Form = dynamic_cast<TForm *>(Token);
  2991. if (Form->WindowState == wsMinimized)
  2992. {
  2993. ShowWindow(Form->Handle, SW_RESTORE);
  2994. }
  2995. else
  2996. {
  2997. Form->SetFocus();
  2998. }
  2999. Abort();
  3000. }
  3001. else
  3002. {
  3003. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3004. }
  3005. }
  3006. TStringList * FileList1 = new TStringList();
  3007. try
  3008. {
  3009. FileList1->AddObject(FullFileName, Object);
  3010. TemporarilyDownloadFiles(FileList1,
  3011. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3012. LocalRootDirectory, LocalDirectory, true, true, true);
  3013. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3014. }
  3015. __finally
  3016. {
  3017. delete FileList1;
  3018. }
  3019. switch (ExecuteFileBy)
  3020. {
  3021. case efShell:
  3022. Counter = "RemoteFilesExecuted";
  3023. break;
  3024. case efInternalEditor:
  3025. Counter = "RemoteFilesOpenedInInternalEditor";
  3026. break;
  3027. case efExternalEditor:
  3028. Counter = "RemoteFilesOpenedInExternalEditor";
  3029. break;
  3030. default:
  3031. DebugFail();
  3032. }
  3033. }
  3034. else
  3035. {
  3036. LocalFileName = FullFileName;
  3037. OriginalFileName = ExtractFileName(FullFileName);
  3038. switch (ExecuteFileBy)
  3039. {
  3040. case efShell:
  3041. Counter = "LocalFilesExecuted";
  3042. break;
  3043. case efInternalEditor:
  3044. Counter = "LocalFilesOpenedInInternalEditor";
  3045. break;
  3046. case efExternalEditor:
  3047. Counter = "LocalFilesOpenedInExternalEditor";
  3048. break;
  3049. default:
  3050. DebugFail();
  3051. }
  3052. }
  3053. Configuration->Usage->Inc(Counter);
  3054. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3055. ExternalEditor, LocalRootDirectory, RemoteDirectory);
  3056. }
  3057. //---------------------------------------------------------------------------
  3058. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3059. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3060. bool AllSelected, bool OnFocused)
  3061. {
  3062. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3063. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3064. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3065. Side = GetSide(Side);
  3066. TCustomDirView * DView = DirView(Side);
  3067. TStrings * FileList = AllSelected ?
  3068. DView->CreateFileList(OnFocused, Side == osLocal) :
  3069. DView->CreateFocusedFileList(Side == osLocal);
  3070. try
  3071. {
  3072. DebugAssert(AllSelected || (FileList->Count == 1));
  3073. for (int i = 0; i < FileList->Count; i++)
  3074. {
  3075. UnicodeString ListFileName = FileList->Strings[i];
  3076. UnicodeString FileNameOnly = (Side == osRemote) ?
  3077. UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3078. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3079. if (!DView->ItemIsDirectory(Item))
  3080. {
  3081. UnicodeString FullFileName;
  3082. if (Side == osRemote)
  3083. {
  3084. FullFileName = RemoteDirView->Path + ListFileName;
  3085. }
  3086. else
  3087. {
  3088. FullFileName = ListFileName;
  3089. }
  3090. TFileMasks::TParams MaskParams;
  3091. MaskParams.Size = DView->ItemFileSize(Item);
  3092. TDateTimePrecision Precision;
  3093. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3094. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3095. FileList->Objects[i], MaskParams);
  3096. }
  3097. }
  3098. }
  3099. __finally
  3100. {
  3101. delete FileList;
  3102. }
  3103. }
  3104. //---------------------------------------------------------------------------
  3105. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3106. {
  3107. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3108. CopyParam.FileNameCase = ncNoChange;
  3109. CopyParam.PreserveRights = false;
  3110. CopyParam.PreserveReadOnly = false;
  3111. CopyParam.ReplaceInvalidChars = true;
  3112. CopyParam.IncludeFileMask = TFileMasks();
  3113. CopyParam.NewerOnly = false;
  3114. CopyParam.FileMask = L"";
  3115. }
  3116. //---------------------------------------------------------------------------
  3117. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3118. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3119. {
  3120. TTerminalManager * Manager = TTerminalManager::Instance();
  3121. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3122. {
  3123. if (!NonVisualDataModule->Busy)
  3124. {
  3125. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3126. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3127. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3128. if (Data->Terminal == NULL)
  3129. {
  3130. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3131. if (SameSiteTerminal != NULL)
  3132. {
  3133. UnicodeString Message =
  3134. FMTLOAD(EDIT_SESSION_REATTACH,
  3135. (FileNameOnly, Data->SessionName, FileNameOnly));
  3136. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3137. {
  3138. Data->Terminal = Terminal;
  3139. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3140. Data->SessionName = Terminal->SessionData->SessionName;
  3141. // We might also overwrite session data
  3142. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3143. }
  3144. else
  3145. {
  3146. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3147. Abort();
  3148. }
  3149. }
  3150. }
  3151. // foreground session should reconnect itself
  3152. else if (Terminal != Data->Terminal)
  3153. {
  3154. UnicodeString Message =
  3155. MainInstructions(
  3156. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3157. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3158. {
  3159. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3160. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3161. }
  3162. else
  3163. {
  3164. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3165. Abort();
  3166. }
  3167. }
  3168. }
  3169. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3170. {
  3171. Configuration->Usage->Inc(L"EditInactiveSession");
  3172. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3173. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3174. (ExtractFileName(FileName), Data->SessionName)));
  3175. }
  3176. }
  3177. TStrings * FileList = new TStringList();
  3178. try
  3179. {
  3180. FileList->Add(FileName);
  3181. // consider using the same settings (preset) as when the file was downloaded
  3182. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3183. TemporaryFileCopyParam(CopyParam);
  3184. if (Data->ForceText)
  3185. {
  3186. CopyParam.TransferMode = tmAscii;
  3187. }
  3188. // so i do not need to worry if masking algorithm works in all cases
  3189. // ("" means "copy file name", no masking is actually done)
  3190. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3191. {
  3192. CopyParam.FileMask = L"";
  3193. }
  3194. else
  3195. {
  3196. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3197. }
  3198. DebugAssert(Data->Queue != NULL);
  3199. int Params = cpNoConfirmation | cpTemporary;
  3200. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3201. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3202. QueueItem->CompleteEvent = UploadCompleteEvent;
  3203. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3204. }
  3205. __finally
  3206. {
  3207. delete FileList;
  3208. }
  3209. }
  3210. //---------------------------------------------------------------------------
  3211. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3212. const UnicodeString FileName, const TEditedFileData * Data)
  3213. {
  3214. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3215. DebugAssert(!NonVisualDataModule->Busy);
  3216. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3217. {
  3218. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3219. (ExtractFileName(FileName), Data->SessionName)));
  3220. }
  3221. TTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3222. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3223. NonVisualDataModule->StartBusy();
  3224. try
  3225. {
  3226. std::unique_ptr<TRemoteFile> File;
  3227. UnicodeString RemoteFileName =
  3228. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3229. FTerminal->ExceptionOnFail = true;
  3230. try
  3231. {
  3232. TRemoteFile * AFile = NULL;
  3233. FTerminal->ReadFile(RemoteFileName, AFile);
  3234. File.reset(AFile);
  3235. if (!File->HaveFullFileName)
  3236. {
  3237. File->FullFileName = RemoteFileName;
  3238. }
  3239. }
  3240. __finally
  3241. {
  3242. FTerminal->ExceptionOnFail = false;
  3243. }
  3244. std::unique_ptr<TStrings> FileList(new TStringList());
  3245. FileList->AddObject(RemoteFileName, File.get());
  3246. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3247. UnicodeString TempDir = ExtractFilePath(FileName);
  3248. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3249. TempDir, true, true, true);
  3250. // sanity check, the target file name should be still the same
  3251. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3252. }
  3253. __finally
  3254. {
  3255. NonVisualDataModule->EndBusy();
  3256. // it actually may not exist anymore...
  3257. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3258. }
  3259. }
  3260. //---------------------------------------------------------------------------
  3261. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3262. const TEditedFileData * Data, bool & KeepOpen)
  3263. {
  3264. // Command is set for external editors only (not for "shell" open).
  3265. if (!Data->Command.IsEmpty())
  3266. {
  3267. bool AnyFound = false;
  3268. bool AnyMDI = false;
  3269. bool AnyNonMDI = false;
  3270. bool AnyDetect = false;
  3271. TEditorList * EditorList = new TEditorList();
  3272. try
  3273. {
  3274. *EditorList = *WinConfiguration->EditorList;
  3275. for (int i = 0; i < EditorList->Count; i++)
  3276. {
  3277. const TEditorPreferences * Editor = EditorList->Editors[i];
  3278. if ((Editor->Data->Editor == edExternal) &&
  3279. (Editor->Data->ExternalEditor == Data->Command))
  3280. {
  3281. AnyFound = true;
  3282. if (Editor->Data->SDIExternalEditor)
  3283. {
  3284. AnyNonMDI = true;
  3285. if (Editor->Data->DetectMDIExternalEditor)
  3286. {
  3287. AnyDetect = true;
  3288. }
  3289. }
  3290. else
  3291. {
  3292. AnyMDI = true;
  3293. }
  3294. }
  3295. }
  3296. bool EnableMDI = false;
  3297. bool DisableDetect = false;
  3298. if (AnyMDI)
  3299. {
  3300. KeepOpen = true;
  3301. if (AnyNonMDI)
  3302. {
  3303. // there is at least one instance of the editor with MDI support enabled,
  3304. // and one with disabled, enable it for all instances
  3305. EnableMDI = true;
  3306. }
  3307. }
  3308. else if (AnyFound && !AnyDetect)
  3309. {
  3310. // at least once instance found but all have MDI autodetection disabled
  3311. // => close the file (default action)
  3312. }
  3313. else
  3314. {
  3315. // no instance of the editor has MDI support enabled
  3316. TMessageParams Params;
  3317. if (AnyFound)
  3318. {
  3319. // there is at least one instance of the editor
  3320. Params.Params |= mpNeverAskAgainCheck;
  3321. }
  3322. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3323. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3324. switch (Answer)
  3325. {
  3326. case qaNeverAskAgain:
  3327. DisableDetect = true;
  3328. break;
  3329. case qaNo:
  3330. EnableMDI = true;
  3331. KeepOpen = true;
  3332. break;
  3333. }
  3334. }
  3335. if (AnyFound && (EnableMDI || DisableDetect))
  3336. {
  3337. bool Changed = false;
  3338. for (int i = 0; i < EditorList->Count; i++)
  3339. {
  3340. const TEditorPreferences * Editor = EditorList->Editors[i];
  3341. if ((Editor->Data->Editor == edExternal) &&
  3342. (Editor->Data->ExternalEditor == Data->Command) &&
  3343. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3344. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3345. {
  3346. Changed = true;
  3347. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3348. if (EnableMDI)
  3349. {
  3350. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3351. }
  3352. if (DisableDetect)
  3353. {
  3354. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3355. }
  3356. EditorList->Change(i, UpdatedEditor);
  3357. }
  3358. }
  3359. if (Changed)
  3360. {
  3361. WinConfiguration->EditorList = EditorList;
  3362. }
  3363. }
  3364. }
  3365. __finally
  3366. {
  3367. delete EditorList;
  3368. }
  3369. }
  3370. else
  3371. {
  3372. // "open" case
  3373. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3374. qaOK, HELP_APP_EARLY_CLOSED);
  3375. }
  3376. }
  3377. //---------------------------------------------------------------------------
  3378. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3379. {
  3380. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3381. }
  3382. //---------------------------------------------------------------------------
  3383. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3384. {
  3385. SideEnter(osRemote);
  3386. }
  3387. //---------------------------------------------------------------------------
  3388. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3389. {
  3390. MakeNextInTabOrder(RemoteDirView, RemoteDriveView);
  3391. SideEnter(osRemote);
  3392. }
  3393. //---------------------------------------------------------------------------
  3394. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3395. {
  3396. FCurrentSide = Side;
  3397. if (Visible)
  3398. {
  3399. UpdateControls();
  3400. }
  3401. }
  3402. //---------------------------------------------------------------------------
  3403. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3404. TStrings * FileList, bool Alternative)
  3405. {
  3406. TCustomDirView * DView = DirView(Side);
  3407. DView->SaveSelection();
  3408. DView->SaveSelectedNames();
  3409. DebugAssert(!FAlternativeDelete);
  3410. FAlternativeDelete = Alternative;
  3411. try
  3412. {
  3413. if (Side == osRemote)
  3414. {
  3415. DebugAssert(Terminal != NULL);
  3416. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3417. }
  3418. else
  3419. {
  3420. try
  3421. {
  3422. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3423. }
  3424. __finally
  3425. {
  3426. ReloadLocalDirectory();
  3427. }
  3428. }
  3429. FAlternativeDelete = false;
  3430. }
  3431. catch(...)
  3432. {
  3433. FAlternativeDelete = false;
  3434. DView->DiscardSavedSelection();
  3435. throw;
  3436. }
  3437. DView->RestoreSelection();
  3438. }
  3439. //---------------------------------------------------------------------------
  3440. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3441. {
  3442. DebugAssert(Terminal);
  3443. RemoteDirView->SaveSelection();
  3444. RemoteDirView->SaveSelectedNames();
  3445. try
  3446. {
  3447. if (Lock)
  3448. {
  3449. Terminal->LockFiles(FileList);
  3450. }
  3451. else
  3452. {
  3453. Terminal->UnlockFiles(FileList);
  3454. }
  3455. }
  3456. catch(...)
  3457. {
  3458. RemoteDirView->DiscardSavedSelection();
  3459. throw;
  3460. }
  3461. RemoteDirView->RestoreSelection();
  3462. }
  3463. //---------------------------------------------------------------------------
  3464. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TTerminal *& Session,
  3465. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3466. bool NoConfirmation, bool Move)
  3467. {
  3468. if (RemoteDriveView->DropTarget != NULL)
  3469. {
  3470. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3471. }
  3472. else if (RemoteDirView->DropTarget != NULL)
  3473. {
  3474. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3475. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3476. }
  3477. else
  3478. {
  3479. Target = RemoteDirView->Path;
  3480. }
  3481. if (Session == NULL)
  3482. {
  3483. Session = TTerminalManager::Instance()->ActiveTerminal;
  3484. }
  3485. Target = UnixIncludeTrailingBackslash(Target);
  3486. if (FileList->Count == 1)
  3487. {
  3488. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3489. }
  3490. else
  3491. {
  3492. FileMask = AnyMask;
  3493. }
  3494. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3495. bool Result = true;
  3496. if (!NoConfirmation)
  3497. {
  3498. bool Multi = (FileList->Count > 1);
  3499. if (Move)
  3500. {
  3501. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3502. }
  3503. else
  3504. {
  3505. DebugAssert(Terminal != NULL);
  3506. // update Terminal->StateData->RemoteDirectory
  3507. UpdateTerminal(Terminal);
  3508. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3509. TStrings * Directories = new TStringList;
  3510. try
  3511. {
  3512. for (int Index = 0; Index < Sessions->Count; Index++)
  3513. {
  3514. TManagedTerminal * Terminal =
  3515. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3516. Directories->Add(Terminal->StateData->RemoteDirectory);
  3517. }
  3518. TDirectRemoteCopy AllowDirectCopy;
  3519. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3520. {
  3521. DebugAssert(DirectCopy);
  3522. AllowDirectCopy = drcAllow;
  3523. }
  3524. else if (FTerminal->IsCapable[fcSecondaryShell])
  3525. {
  3526. DebugAssert(DirectCopy);
  3527. AllowDirectCopy = drcConfirmCommandSession;
  3528. }
  3529. else
  3530. {
  3531. DebugAssert(!DirectCopy);
  3532. AllowDirectCopy = drcDisallow;
  3533. }
  3534. void * ASession = Session;
  3535. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3536. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3537. Session = static_cast<TTerminal *>(ASession);
  3538. }
  3539. __finally
  3540. {
  3541. delete Directories;
  3542. }
  3543. }
  3544. }
  3545. return Result;
  3546. }
  3547. //---------------------------------------------------------------------------
  3548. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3549. TStrings * FileList, bool NoConfirmation, bool Move, TTerminal * Session)
  3550. {
  3551. bool DirectCopy;
  3552. UnicodeString Target, FileMask;
  3553. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3554. if (Result)
  3555. {
  3556. if (!Move && !DirectCopy)
  3557. {
  3558. Configuration->Usage->Inc("RemoteCopyTemp");
  3559. UnicodeString RootTempDir;
  3560. UnicodeString TempDir;
  3561. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3562. TStrings * TemporaryFilesList = new TStringList();
  3563. try
  3564. {
  3565. TMakeLocalFileListParams MakeFileListParam;
  3566. MakeFileListParam.FileList = TemporaryFilesList;
  3567. MakeFileListParam.FileTimes = NULL;
  3568. MakeFileListParam.IncludeDirs = true;
  3569. MakeFileListParam.Recursive = false;
  3570. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3571. TTerminalManager::Instance()->ActiveTerminal = Session;
  3572. if (TemporaryFilesList->Count > 0)
  3573. {
  3574. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3575. CopyParam.FileMask = FileMask;
  3576. DebugAssert(!FAutoOperation);
  3577. FAutoOperation = true;
  3578. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3579. }
  3580. }
  3581. __finally
  3582. {
  3583. delete TemporaryFilesList;
  3584. FAutoOperation = false;
  3585. if (!RootTempDir.IsEmpty())
  3586. {
  3587. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3588. }
  3589. }
  3590. }
  3591. else
  3592. {
  3593. RemoteDirView->SaveSelection();
  3594. RemoteDirView->SaveSelectedNames();
  3595. try
  3596. {
  3597. if (Move)
  3598. {
  3599. Configuration->Usage->Inc("RemoteMove");
  3600. Terminal->MoveFiles(FileList, Target, FileMask);
  3601. }
  3602. else
  3603. {
  3604. Configuration->Usage->Inc("RemoteCopyDirect");
  3605. DebugAssert(DirectCopy);
  3606. DebugAssert(Session == FTerminal);
  3607. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3608. FTerminal->CommandSessionOpened ||
  3609. CommandSessionFallback())
  3610. {
  3611. Terminal->CopyFiles(FileList, Target, FileMask);
  3612. }
  3613. }
  3614. }
  3615. catch(...)
  3616. {
  3617. RemoteDirView->DiscardSavedSelection();
  3618. throw;
  3619. }
  3620. RemoteDirView->RestoreSelection();
  3621. }
  3622. }
  3623. return Result;
  3624. }
  3625. //---------------------------------------------------------------------------
  3626. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3627. {
  3628. Side = GetSide(Side);
  3629. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3630. TRemoteProperties * AProperties = (Side == osRemote ? &Properties : NULL);
  3631. UnicodeString Name = LoadStr(NEW_FOLDER);
  3632. int AllowedChanges =
  3633. FLAGMASK((Side == osRemote) && Terminal->IsCapable[fcModeChanging], cpMode);
  3634. bool SaveSettings = false;
  3635. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3636. {
  3637. TWindowLock Lock(this);
  3638. if (Side == osRemote)
  3639. {
  3640. if (SaveSettings)
  3641. {
  3642. GUIConfiguration->NewDirectoryProperties = Properties;
  3643. }
  3644. RemoteDirView->CreateDirectoryEx(Name, &Properties);
  3645. }
  3646. else
  3647. {
  3648. DirView(Side)->CreateDirectory(Name);
  3649. }
  3650. }
  3651. }
  3652. //---------------------------------------------------------------------------
  3653. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3654. {
  3655. TWindowLock Lock(this);
  3656. DirView(Side)->ExecuteHomeDirectory();
  3657. }
  3658. //---------------------------------------------------------------------------
  3659. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3660. {
  3661. DoOpenDirectoryDialog(odBrowse, Side);
  3662. }
  3663. //---------------------------------------------------------------------------
  3664. bool __fastcall TCustomScpExplorerForm::OpenBookmark(UnicodeString Local, UnicodeString Remote)
  3665. {
  3666. UnicodeString Path;
  3667. if (FCurrentSide == osRemote)
  3668. {
  3669. Path = Remote;
  3670. }
  3671. else
  3672. {
  3673. Path = Local;
  3674. }
  3675. bool Result = !Path.IsEmpty();
  3676. if (Result)
  3677. {
  3678. // While we might get here when the session is closed (from location profiles),
  3679. // it's not a problem as the Path setter is noop then
  3680. DirView(FCurrentSide)->Path = Path;
  3681. }
  3682. return Result;
  3683. }
  3684. //---------------------------------------------------------------------------
  3685. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3686. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3687. bool & Close)
  3688. {
  3689. // terminal can be already closed (e.g. dropped connection)
  3690. if (Terminal != NULL)
  3691. {
  3692. try
  3693. {
  3694. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3695. }
  3696. catch(...)
  3697. {
  3698. if (!Terminal->Active)
  3699. {
  3700. Close = true;
  3701. }
  3702. throw;
  3703. }
  3704. }
  3705. }
  3706. //---------------------------------------------------------------------------
  3707. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3708. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3709. bool & Close)
  3710. {
  3711. // terminal can be already closed (e.g. dropped connection)
  3712. if (Terminal != NULL)
  3713. {
  3714. Configuration->Usage->Inc(L"ChecksumCalculated");
  3715. try
  3716. {
  3717. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3718. }
  3719. catch(...)
  3720. {
  3721. if (!Terminal->Active)
  3722. {
  3723. Close = true;
  3724. }
  3725. throw;
  3726. }
  3727. }
  3728. }
  3729. //---------------------------------------------------------------------------
  3730. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3731. {
  3732. bool Result;
  3733. if (Side == osRemote)
  3734. {
  3735. TRemoteTokenList * GroupList = NULL;
  3736. TRemoteTokenList * UserList = NULL;
  3737. try
  3738. {
  3739. TRemoteProperties CurrentProperties;
  3740. if (Terminal->LoadFilesProperties(FileList))
  3741. {
  3742. RemoteDirView->Invalidate();
  3743. }
  3744. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  3745. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  3746. if (CapableGroupChanging || CapableOwnerChanging)
  3747. {
  3748. if (CapableGroupChanging)
  3749. {
  3750. GroupList = Terminal->Groups->Duplicate();
  3751. }
  3752. if (CapableOwnerChanging)
  3753. {
  3754. UserList = Terminal->Users->Duplicate();
  3755. }
  3756. TRemoteDirectory * Files = Terminal->Files;
  3757. int Count = Files->Count;
  3758. if (Count > 100)
  3759. {
  3760. Count = 100;
  3761. }
  3762. for (int Index = 0; Index < Count; Index++)
  3763. {
  3764. TRemoteFile * File = Files->Files[Index];
  3765. if (CapableGroupChanging)
  3766. {
  3767. GroupList->AddUnique(File->Group);
  3768. }
  3769. if (CapableOwnerChanging)
  3770. {
  3771. UserList->AddUnique(File->Owner);
  3772. }
  3773. }
  3774. // if we haven't collected tokens for all files in current directory,
  3775. // make sure we collect them at least for all selected files.
  3776. // (note that so far the files in FileList has to be from current direcotry)
  3777. if (Count < Files->Count)
  3778. {
  3779. for (int Index = 0; Index < FileList->Count; Index++)
  3780. {
  3781. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  3782. if (CapableGroupChanging)
  3783. {
  3784. GroupList->AddUnique(File->Group);
  3785. }
  3786. if (CapableOwnerChanging)
  3787. {
  3788. UserList->AddUnique(File->Owner);
  3789. }
  3790. }
  3791. }
  3792. }
  3793. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  3794. int Flags = 0;
  3795. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  3796. if (CapableOwnerChanging) Flags |= cpOwner;
  3797. if (CapableGroupChanging) Flags |= cpGroup;
  3798. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  3799. if (Terminal->IsCapable[fcCalculatingChecksum])
  3800. {
  3801. CalculateChecksumEvent = CalculateChecksum;
  3802. }
  3803. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  3804. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  3805. TRemoteProperties NewProperties = CurrentProperties;
  3806. Result =
  3807. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  3808. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  3809. Terminal->IsCapable[fcGroupOwnerChangingByID],
  3810. CalculateSize, CalculateChecksumEvent);
  3811. if (Result)
  3812. {
  3813. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  3814. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  3815. }
  3816. }
  3817. __finally
  3818. {
  3819. delete GroupList;
  3820. delete UserList;
  3821. }
  3822. }
  3823. else
  3824. {
  3825. DirView(Side)->DisplayPropertiesMenu();
  3826. Result = true;
  3827. }
  3828. return Result;
  3829. }
  3830. //---------------------------------------------------------------------------
  3831. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  3832. {
  3833. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  3834. {
  3835. FIgnoreNextDialogChar = Key;
  3836. }
  3837. Key = 0;
  3838. }
  3839. //---------------------------------------------------------------------------
  3840. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  3841. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  3842. {
  3843. const TCustomCommandType * Command = List->Find(KeyShortCut);
  3844. if (Command != NULL)
  3845. {
  3846. KeyProcessed(Key, Shift);
  3847. if (CustomCommandState(*Command, false, ccltAll) > 0)
  3848. {
  3849. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  3850. false, false, const_cast<TCustomCommandType *>(Command));
  3851. }
  3852. }
  3853. }
  3854. //---------------------------------------------------------------------------
  3855. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  3856. {
  3857. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  3858. {
  3859. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  3860. }
  3861. if (!DirView(osCurrent)->IsEditing())
  3862. {
  3863. TShortCut KeyShortCut = ShortCut(Key, Shift);
  3864. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  3865. {
  3866. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  3867. if (((Action->ShortCut == KeyShortCut) ||
  3868. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  3869. AllowedAction(Action, aaShortCut))
  3870. {
  3871. // Has to be called before the action as the dialog char is already in queue.
  3872. // So when the action consumes message queue, we already need to have the
  3873. // FIgnoreNextDialogChar set
  3874. KeyProcessed(Key, Shift);
  3875. // Reset reference to previous component (when menu/toolbar was clicked).
  3876. // Needed to detect that action was invoked by keyboard shortcut
  3877. // in TNonVisualDataModule::ExplorerActionsExecute
  3878. Action->ActionComponent = NULL;
  3879. Action->Execute();
  3880. return;
  3881. }
  3882. }
  3883. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  3884. {
  3885. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  3886. {
  3887. KeyProcessed(Key, Shift);
  3888. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  3889. return;
  3890. }
  3891. }
  3892. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  3893. {
  3894. KeyProcessed(Key, Shift);
  3895. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  3896. }
  3897. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  3898. if (IsCustomShortCut(CustomShortCut))
  3899. {
  3900. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  3901. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  3902. if (WinConfiguration->SharedBookmarks != NULL)
  3903. {
  3904. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  3905. if ((Bookmark != NULL) &&
  3906. OpenBookmark(Bookmark->Local, Bookmark->Remote))
  3907. {
  3908. KeyProcessed(Key, Shift);
  3909. }
  3910. }
  3911. }
  3912. }
  3913. TForm::KeyDown(Key, Shift);
  3914. }
  3915. //---------------------------------------------------------------------------
  3916. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  3917. {
  3918. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  3919. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  3920. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  3921. DebugAssert(Terminal);
  3922. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  3923. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  3924. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  3925. // expanded from ?: to avoid memory leaks
  3926. if (SecurityEnabled)
  3927. {
  3928. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  3929. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  3930. }
  3931. else
  3932. {
  3933. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  3934. }
  3935. if (FileSystemInfo.ProtocolName.IsEmpty())
  3936. {
  3937. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  3938. }
  3939. else
  3940. {
  3941. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  3942. }
  3943. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  3944. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  3945. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  3946. if (SessionInfo.CSCompression == SessionInfo.SCCompression)
  3947. {
  3948. SessionStatusBar->Panels->Items[Offset + 2]->Hint =
  3949. FMTLOAD(STATUS_COMPRESSION_HINT, (DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR))));
  3950. }
  3951. else
  3952. {
  3953. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION2_HINT,
  3954. (DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR)),
  3955. DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR))));
  3956. }
  3957. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  3958. UpdateStatusBar();
  3959. }
  3960. //---------------------------------------------------------------------------
  3961. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  3962. {
  3963. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  3964. DebugAssert(SessionStatusBar != NULL);
  3965. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  3966. {
  3967. // note: (Terminal->Status < sshReady) currently never happens here,
  3968. // so STATUS_CONNECTING is never used
  3969. SessionStatusBar->SimplePanel = true;
  3970. SessionStatusBar->SimpleText = LoadStr(
  3971. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  3972. }
  3973. else
  3974. {
  3975. DebugAssert(Terminal);
  3976. SessionStatusBar->SimplePanel = false;
  3977. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  3978. if (!FNote.IsEmpty())
  3979. {
  3980. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  3981. }
  3982. else
  3983. {
  3984. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  3985. }
  3986. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  3987. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  3988. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  3989. }
  3990. }
  3991. //---------------------------------------------------------------------------
  3992. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  3993. {
  3994. Panel->Caption = L"";
  3995. }
  3996. //---------------------------------------------------------------------------
  3997. void __fastcall TCustomScpExplorerForm::Idle()
  3998. {
  3999. if (FShowing || FStandaloneEditing)
  4000. {
  4001. FEditorManager->Check();
  4002. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4003. // that needs to know if queue view is visible (and it may be closed after queue update)
  4004. TTerminalManager::Instance()->Idle();
  4005. }
  4006. if (FShowing)
  4007. {
  4008. if (!NonVisualDataModule->Busy &&
  4009. // Menu is opened or drag&drop is going on
  4010. (Mouse->Capture == NULL))
  4011. {
  4012. if (FRefreshRemoteDirectory)
  4013. {
  4014. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4015. {
  4016. Terminal->RefreshDirectory();
  4017. }
  4018. FRefreshRemoteDirectory = false;
  4019. }
  4020. if (FRefreshLocalDirectory)
  4021. {
  4022. ReloadLocalDirectory();
  4023. FRefreshLocalDirectory = false;
  4024. }
  4025. if (WinConfiguration->RefreshRemotePanel)
  4026. {
  4027. TManagedTerminal * ManagedTerminal =
  4028. dynamic_cast<TManagedTerminal *>(Terminal);
  4029. if ((ManagedTerminal != NULL) && (Terminal->Status == ssOpened) &&
  4030. (Now() - ManagedTerminal->DirectoryLoaded >
  4031. WinConfiguration->RefreshRemotePanelInterval))
  4032. {
  4033. RemoteDirView->ReloadDirectory();
  4034. }
  4035. }
  4036. }
  4037. }
  4038. if (FShowing || FStandaloneEditing)
  4039. {
  4040. if (FQueueStatusInvalidated)
  4041. {
  4042. UpdateQueueStatus(false);
  4043. }
  4044. RefreshQueueItems();
  4045. }
  4046. if (FShowing)
  4047. {
  4048. UpdateStatusBar();
  4049. }
  4050. FIgnoreNextDialogChar = 0;
  4051. }
  4052. //---------------------------------------------------------------------------
  4053. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4054. {
  4055. try
  4056. {
  4057. FUserActionTimer->Enabled = false;
  4058. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4059. {
  4060. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4061. {
  4062. FPendingQueueActionItem->ProcessUserAction();
  4063. }
  4064. }
  4065. }
  4066. __finally
  4067. {
  4068. FPendingQueueActionItem = NULL;
  4069. }
  4070. }
  4071. //---------------------------------------------------------------------------
  4072. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4073. {
  4074. if (WinConfiguration->MinimizeToTray)
  4075. {
  4076. UpdateTrayIcon();
  4077. FTrayIcon->Visible = true;
  4078. if (Visible)
  4079. {
  4080. ShowWindow(Handle, SW_HIDE);
  4081. }
  4082. }
  4083. }
  4084. //---------------------------------------------------------------------------
  4085. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4086. {
  4087. // WORKAROUND
  4088. // When restoring maximized window from minimization,
  4089. // rarely some controls do not align properly.
  4090. // Two instances seen (both for Commander):
  4091. // - When restoring, window is temporarily narrower (not maximized),
  4092. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4093. // Once maximized already, top dock shinks back, but the session PageControl,
  4094. // do not align up, leaving space between TopDock and PageControl.
  4095. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4096. for (int Index = 0; Index < ControlCount; Index++)
  4097. {
  4098. RealignControl(Controls[Index]);
  4099. }
  4100. if (FTrayIcon->Visible)
  4101. {
  4102. FTrayIcon->Visible = false;
  4103. if (Visible)
  4104. {
  4105. ShowWindow(Handle, SW_SHOW);
  4106. }
  4107. }
  4108. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4109. {
  4110. FNeedSession = false;
  4111. NonVisualDataModule->StartBusy();
  4112. try
  4113. {
  4114. NeedSession(false);
  4115. }
  4116. __finally
  4117. {
  4118. NonVisualDataModule->EndBusy();
  4119. }
  4120. }
  4121. }
  4122. //---------------------------------------------------------------------------
  4123. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4124. {
  4125. FTrayIcon->Hint = Caption;
  4126. }
  4127. //---------------------------------------------------------------------------
  4128. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4129. {
  4130. UpdateTrayIcon();
  4131. }
  4132. //---------------------------------------------------------------------------
  4133. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4134. {
  4135. // workaround
  4136. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4137. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4138. // But WM_ACTIVATEAPP message can be received even when
  4139. // the main window is minimized to the tray and internal editor window is focused
  4140. // (after another application was previously active)
  4141. if (::IsIconic(Handle))
  4142. {
  4143. if (!IsAppIconic())
  4144. {
  4145. SetAppIconic(true);
  4146. }
  4147. }
  4148. ::ApplicationRestore();
  4149. Application->BringToFront();
  4150. }
  4151. //---------------------------------------------------------------------------
  4152. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4153. {
  4154. RestoreApp();
  4155. }
  4156. //---------------------------------------------------------------------------
  4157. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4158. {
  4159. if (OpenInNewWindow())
  4160. {
  4161. // todo: Pass FromSite
  4162. ExecuteNewInstance(SessionUrl);
  4163. }
  4164. else
  4165. {
  4166. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4167. }
  4168. }
  4169. //---------------------------------------------------------------------------
  4170. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4171. {
  4172. // current working directories become defaults here, what is not right
  4173. TSessionData * SessionData = CloneCurrentSessionData();
  4174. try
  4175. {
  4176. if (OpenInNewWindow())
  4177. {
  4178. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->Name;
  4179. StoredSessions->NewSession(SessionName, SessionData);
  4180. // modified only, explicit
  4181. StoredSessions->Save(false, true);
  4182. // encode session name because of slashes in hierarchical sessions
  4183. ExecuteNewInstance(EncodeUrlString(SessionName));
  4184. }
  4185. else
  4186. {
  4187. TTerminalManager * Manager = TTerminalManager::Instance();
  4188. TTerminal * Terminal = Manager->NewTerminal(SessionData);
  4189. Manager->ActiveTerminal = Terminal;
  4190. }
  4191. }
  4192. __finally
  4193. {
  4194. delete SessionData;
  4195. }
  4196. }
  4197. //---------------------------------------------------------------------------
  4198. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4199. {
  4200. DebugAssert(FQueue != NULL);
  4201. bool Result = FQueue->IsEmpty;
  4202. if (!Result)
  4203. {
  4204. SetFocus();
  4205. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4206. }
  4207. return Result;
  4208. }
  4209. //---------------------------------------------------------------------------
  4210. void __fastcall TCustomScpExplorerForm::CloseSession()
  4211. {
  4212. if (CanCloseQueue())
  4213. {
  4214. TTerminalManager::Instance()->FreeActiveTerminal();
  4215. }
  4216. }
  4217. //---------------------------------------------------------------------------
  4218. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4219. {
  4220. if (OpenInNewWindow())
  4221. {
  4222. // encode session name because of slashes in hierarchical sessions
  4223. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4224. }
  4225. else
  4226. {
  4227. TTerminalManager * Manager = TTerminalManager::Instance();
  4228. TTerminal * Terminal = Manager->NewTerminal(Data);
  4229. Manager->ActiveTerminal = Terminal;
  4230. }
  4231. }
  4232. //---------------------------------------------------------------------------
  4233. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4234. {
  4235. if (OpenInNewWindow())
  4236. {
  4237. ExecuteNewInstance(Name);
  4238. }
  4239. else
  4240. {
  4241. TTerminalManager * Manager = TTerminalManager::Instance();
  4242. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4243. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4244. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  4245. }
  4246. }
  4247. //---------------------------------------------------------------------------
  4248. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4249. bool &CanClose)
  4250. {
  4251. if (Terminal != NULL)
  4252. {
  4253. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4254. {
  4255. unsigned int Result;
  4256. TMessageParams Params(mpNeverAskAgainCheck);
  4257. UnicodeString Message;
  4258. int Answers = qaOK | qaCancel;
  4259. if (TTerminalManager::Instance()->Count > 1)
  4260. {
  4261. if (!WinConfiguration->AutoSaveWorkspace)
  4262. {
  4263. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE2);
  4264. Answers = qaYes | qaNo | qaCancel;
  4265. }
  4266. else
  4267. {
  4268. Message = MainInstructions(LoadStr(CLOSE_SESSIONS));
  4269. }
  4270. }
  4271. else
  4272. {
  4273. Message = MainInstructions(FMTLOAD(CLOSE_SESSION, (Terminal->SessionData->SessionName)));
  4274. }
  4275. if (WinConfiguration->AutoSaveWorkspace)
  4276. {
  4277. Message =
  4278. FORMAT("%s\n\n%s", (Message,
  4279. FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()))));
  4280. }
  4281. SetFocus();
  4282. Result = MessageDialog(Message, qtConfirmation,
  4283. Answers, HELP_NONE, &Params);
  4284. if (Result == qaNeverAskAgain)
  4285. {
  4286. WinConfiguration->ConfirmClosingSession = false;
  4287. }
  4288. if (Result == qaNo)
  4289. {
  4290. CanClose = SaveWorkspace(true);
  4291. // note that the workspace will be saved redundatly again from FormClose
  4292. }
  4293. else
  4294. {
  4295. CanClose =
  4296. (Result == qaOK) ||
  4297. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4298. (Result == qaNeverAskAgain);
  4299. }
  4300. }
  4301. if (CanClose)
  4302. {
  4303. CanClose = CanCloseQueue();
  4304. }
  4305. }
  4306. if (CanClose)
  4307. {
  4308. CanClose =
  4309. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4310. FEditorManager->CloseExternalFilesWithoutProcess();
  4311. if (CanClose)
  4312. {
  4313. while (CanClose && (FLocalEditors->Count > 0))
  4314. {
  4315. int PrevCount = FLocalEditors->Count;
  4316. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4317. CanClose = (FLocalEditors->Count < PrevCount);
  4318. }
  4319. if (CanClose)
  4320. {
  4321. CanClose = FEditorManager->Empty(true);
  4322. if (!CanClose)
  4323. {
  4324. SetFocus();
  4325. CanClose =
  4326. (MessageDialog(
  4327. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4328. }
  4329. }
  4330. }
  4331. }
  4332. }
  4333. //---------------------------------------------------------------------------
  4334. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4335. {
  4336. TForm * Form = dynamic_cast<TForm *>(Sender);
  4337. DebugAssert(Form != NULL);
  4338. Form->Close();
  4339. }
  4340. //---------------------------------------------------------------------------
  4341. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4342. {
  4343. TForm * Form = dynamic_cast<TForm *>(Sender);
  4344. delete Form;
  4345. }
  4346. //---------------------------------------------------------------------------
  4347. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4348. {
  4349. while (FLocalEditors->Count > 0)
  4350. {
  4351. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4352. }
  4353. }
  4354. //---------------------------------------------------------------------------
  4355. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4356. TObject *Sender)
  4357. {
  4358. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4359. try
  4360. {
  4361. SetProperties(osRemote, FileList);
  4362. }
  4363. __finally
  4364. {
  4365. delete FileList;
  4366. }
  4367. }
  4368. //---------------------------------------------------------------------------
  4369. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4370. {
  4371. if (value)
  4372. {
  4373. if (Component == fcCustomCommandsBand)
  4374. {
  4375. UpdateCustomCommandsToolbar();
  4376. }
  4377. }
  4378. }
  4379. //---------------------------------------------------------------------------
  4380. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4381. {
  4382. TControl * Control = GetComponent(Component);
  4383. DebugAssert(Control);
  4384. bool Changed = (Control->Visible != value);
  4385. if (Changed)
  4386. {
  4387. ComponentShowing(Component, value);
  4388. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4389. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4390. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4391. if (value)
  4392. {
  4393. int RemainingHeight = Control->Parent->ClientHeight;
  4394. int RemainingWidth = Control->Parent->ClientWidth;
  4395. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4396. {
  4397. TControl * ChildControl = Control->Parent->Controls[i];
  4398. if (ChildControl->Visible)
  4399. {
  4400. switch (ChildControl->Align)
  4401. {
  4402. case alTop:
  4403. case alBottom:
  4404. RemainingHeight -= ChildControl->Height;
  4405. break;
  4406. case alLeft:
  4407. case alRight:
  4408. RemainingWidth -= ChildControl->Width;
  4409. break;
  4410. }
  4411. }
  4412. }
  4413. int Reserve = ScaleByTextHeight(this, 32);
  4414. // queue in explorer, trees in commander
  4415. if (Control->Height > RemainingHeight - Reserve)
  4416. {
  4417. Control->Height = RemainingHeight / 2;
  4418. }
  4419. if (Control->Width > RemainingWidth - Reserve)
  4420. {
  4421. Control->Width = RemainingWidth / 2;
  4422. }
  4423. }
  4424. Control->Visible = value;
  4425. if (WasFocused && Visible)
  4426. {
  4427. DirView(osCurrent)->SetFocus();
  4428. }
  4429. FixControlsPlacement();
  4430. }
  4431. }
  4432. //---------------------------------------------------------------------------
  4433. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4434. {
  4435. TControl * Control = GetComponent(Component);
  4436. if (Control == NULL)
  4437. {
  4438. return false;
  4439. }
  4440. else
  4441. {
  4442. return Control->Visible;
  4443. }
  4444. }
  4445. //---------------------------------------------------------------------------
  4446. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4447. {
  4448. if (RemoteDirView->ItemFocused != NULL)
  4449. {
  4450. RemoteDirView->ItemFocused->MakeVisible(false);
  4451. }
  4452. QueueSplitter->Visible = QueuePanel->Visible;
  4453. RemotePanelSplitter->Visible = RemoteDriveView->Visible;
  4454. }
  4455. //---------------------------------------------------------------------------
  4456. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4457. {
  4458. switch (Component) {
  4459. case fcStatusBar: return RemoteStatusBar;
  4460. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4461. case fcQueueView: return QueuePanel;
  4462. case fcQueueToolbar: return QueueDock;
  4463. case fcRemoteTree: return RemoteDriveView;
  4464. case fcSessionsTabs: return SessionsPageControl;
  4465. default: return NULL;
  4466. }
  4467. }
  4468. //---------------------------------------------------------------------------
  4469. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4470. TObject *Sender, TListColumn *Column, TPoint &Point)
  4471. {
  4472. DebugAssert(NonVisualDataModule && Column && Sender);
  4473. NonVisualDataModule->ListColumn = Column;
  4474. TPoint ScreenPoint = ((TControl*)Sender)->ClientToScreen(Point);
  4475. TPopupMenu * DirViewColumnMenu;
  4476. if (Sender == RemoteDirView)
  4477. {
  4478. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4479. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4480. (Column->Index == uvName);
  4481. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4482. (Column->Index == uvSize);
  4483. }
  4484. else
  4485. {
  4486. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4487. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4488. (Column->Index == dvName);
  4489. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4490. (Column->Index == dvSize);
  4491. }
  4492. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4493. }
  4494. //---------------------------------------------------------------------------
  4495. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4496. TObject *Sender, TListItem *Item, bool &AllowExec)
  4497. {
  4498. DoDirViewExecFile(Sender, Item, AllowExec);
  4499. }
  4500. //---------------------------------------------------------------------------
  4501. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4502. TListItem * Item, bool & AllowExec)
  4503. {
  4504. DebugAssert(Sender && Item && Configuration);
  4505. DebugAssert(AllowExec);
  4506. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4507. bool Remote = (ADirView == DirView(osRemote));
  4508. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4509. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4510. // on files only (not directories)
  4511. // and only when symlinks are resolved (apply to remote panel only)
  4512. if (!ADirView->ItemIsDirectory(Item) &&
  4513. (ResolvedSymlinks || FForceExecution))
  4514. {
  4515. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4516. !FForceExecution &&
  4517. ResolvedSymlinks)
  4518. {
  4519. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4520. {
  4521. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4522. UnlockWindow();
  4523. try
  4524. {
  4525. ExecuteFileOperation(foCopy,
  4526. (ADirView == DirView(osRemote) ? osRemote : osLocal),
  4527. true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4528. }
  4529. __finally
  4530. {
  4531. LockWindow();
  4532. }
  4533. AllowExec = false;
  4534. }
  4535. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4536. {
  4537. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4538. {
  4539. ExecuteFile(osCurrent, efDefaultEditor);
  4540. AllowExec = false;
  4541. }
  4542. }
  4543. else
  4544. {
  4545. DebugFail();
  4546. }
  4547. }
  4548. // if we have not done anything special, fall back to default behavior
  4549. if (AllowExec)
  4550. {
  4551. if (Remote && !WinConfiguration->DisableOpenEdit)
  4552. {
  4553. ExecuteFile(osRemote, efShell);
  4554. AllowExec = false;
  4555. }
  4556. }
  4557. }
  4558. }
  4559. //---------------------------------------------------------------------------
  4560. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4561. {
  4562. return ((Side == osRemote) || (Side == osCurrent));
  4563. }
  4564. //---------------------------------------------------------------------------
  4565. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4566. {
  4567. DebugFail();
  4568. }
  4569. //---------------------------------------------------------------------------
  4570. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4571. {
  4572. DebugFail();
  4573. }
  4574. //---------------------------------------------------------------------------
  4575. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4576. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, bool UseDefaults)
  4577. {
  4578. TSynchronizeParamType Params;
  4579. Params.LocalDirectory = LocalDirectory;
  4580. Params.RemoteDirectory = RemoteDirectory;
  4581. int UnusedParams =
  4582. (GUIConfiguration->SynchronizeParams &
  4583. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4584. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4585. Params.Options = GUIConfiguration->SynchronizeOptions;
  4586. bool SaveSettings = false;
  4587. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4588. &DoSynchronizeTooManyDirectories);
  4589. DebugAssert(FSynchronizeController == NULL);
  4590. FSynchronizeController = &Controller;
  4591. bool Result;
  4592. try
  4593. {
  4594. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4595. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4596. int Options =
  4597. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4598. DebugAssert(FOnFeedSynchronizeError == NULL);
  4599. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4600. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4601. FOnFeedSynchronizeError, UseDefaults);
  4602. if (Result)
  4603. {
  4604. if (SaveSettings)
  4605. {
  4606. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4607. GUIConfiguration->SynchronizeOptions = Params.Options;
  4608. }
  4609. else
  4610. {
  4611. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4612. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4613. FLAGSET(Params.Options, soSynchronize))
  4614. {
  4615. GUIConfiguration->SynchronizeOptions =
  4616. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4617. soSynchronize;
  4618. }
  4619. }
  4620. LocalDirectory = Params.LocalDirectory;
  4621. RemoteDirectory = Params.RemoteDirectory;
  4622. }
  4623. }
  4624. __finally
  4625. {
  4626. FSynchronizeController = NULL;
  4627. DebugAssert(FOnFeedSynchronizeError == NULL);
  4628. FOnFeedSynchronizeError = NULL;
  4629. }
  4630. return Result;
  4631. }
  4632. //---------------------------------------------------------------------------
  4633. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4634. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4635. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4636. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4637. TSynchronizeOptions * Options, bool Full)
  4638. {
  4639. if (Terminal->Status == ssOpened)
  4640. {
  4641. try
  4642. {
  4643. int PParams = Params.Params;
  4644. if (!Full)
  4645. {
  4646. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4647. if (FLAGSET(Params.Options, soRecurse))
  4648. {
  4649. PParams |= TTerminal::spSubDirs;
  4650. }
  4651. }
  4652. else
  4653. {
  4654. // if keepuptodate is non-recursive,
  4655. // full sync before has to be non-recursive as well
  4656. if (FLAGCLEAR(Params.Options, soRecurse))
  4657. {
  4658. PParams |= TTerminal::spNoRecurse;
  4659. }
  4660. }
  4661. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  4662. PParams, Checklist, Options);
  4663. }
  4664. catch (Exception & E)
  4665. {
  4666. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  4667. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  4668. {
  4669. // noop, already logged in MoreMessageDialog
  4670. }
  4671. else
  4672. {
  4673. ShowExtendedExceptionEx(Terminal, &E);
  4674. throw;
  4675. }
  4676. }
  4677. }
  4678. }
  4679. //---------------------------------------------------------------------------
  4680. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  4681. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  4682. const UnicodeString ErrorStr)
  4683. {
  4684. if (!Directory.IsEmpty())
  4685. {
  4686. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  4687. }
  4688. else
  4689. {
  4690. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  4691. }
  4692. }
  4693. //---------------------------------------------------------------------------
  4694. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  4695. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  4696. {
  4697. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  4698. {
  4699. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  4700. }
  4701. else
  4702. {
  4703. TMessageParams Params(mpNeverAskAgainCheck);
  4704. unsigned int Result = MessageDialog(
  4705. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  4706. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  4707. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  4708. {
  4709. MaxDirectories *= 2;
  4710. if (Result == qaNeverAskAgain)
  4711. {
  4712. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  4713. }
  4714. }
  4715. else
  4716. {
  4717. Abort();
  4718. }
  4719. }
  4720. }
  4721. //---------------------------------------------------------------------------
  4722. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  4723. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  4724. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  4725. TSynchronizeOptions * Options)
  4726. {
  4727. DebugAssert(!FAutoOperation);
  4728. void * BatchStorage;
  4729. BatchStart(BatchStorage);
  4730. FAutoOperation = true;
  4731. bool AnyOperation = false;
  4732. TDateTime StartTime = Now();
  4733. TSynchronizeChecklist * AChecklist = NULL;
  4734. try
  4735. {
  4736. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  4737. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  4738. {
  4739. FSynchronizeProgressForm->Start();
  4740. }
  4741. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  4742. static_cast<TTerminal::TSynchronizeMode>(Mode),
  4743. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4744. Options);
  4745. SAFE_DESTROY(FSynchronizeProgressForm);
  4746. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, false);
  4747. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  4748. {
  4749. FSynchronizeProgressForm->Start();
  4750. }
  4751. for (int Index = 0; !AnyOperation && (Index < AChecklist->Count); Index++)
  4752. {
  4753. AnyOperation = AChecklist->Item[Index]->Checked;
  4754. }
  4755. // No need to call if !AnyOperation
  4756. Terminal->SynchronizeApply(AChecklist, LocalDirectory, RemoteDirectory,
  4757. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory);
  4758. }
  4759. __finally
  4760. {
  4761. if (Checklist == NULL)
  4762. {
  4763. delete AChecklist;
  4764. }
  4765. else
  4766. {
  4767. *Checklist = AChecklist;
  4768. }
  4769. FAutoOperation = false;
  4770. SAFE_DESTROY(FSynchronizeProgressForm);
  4771. BatchEnd(BatchStorage);
  4772. ReloadLocalDirectory();
  4773. if (AnyOperation)
  4774. {
  4775. OperationComplete(StartTime);
  4776. }
  4777. }
  4778. }
  4779. //---------------------------------------------------------------------------
  4780. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  4781. {
  4782. // can be called from command line
  4783. return Visible &&
  4784. ((DirView(osRemote)->SelCount > 0) ||
  4785. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  4786. }
  4787. //---------------------------------------------------------------------------
  4788. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  4789. {
  4790. LogSynchronizeEvent(Terminal, Message);
  4791. }
  4792. //---------------------------------------------------------------------------
  4793. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  4794. int Params, TSynchronizeOptions & Options)
  4795. {
  4796. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  4797. {
  4798. Options.Filter = new TStringList();
  4799. Options.Filter->CaseSensitive = false;
  4800. Options.Filter->Duplicates = Types::dupAccept;
  4801. if (DirView(osRemote)->SelCount > 0)
  4802. {
  4803. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  4804. }
  4805. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  4806. {
  4807. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  4808. }
  4809. Options.Filter->Sort();
  4810. }
  4811. }
  4812. //---------------------------------------------------------------------------
  4813. bool __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  4814. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  4815. TSynchronizeMode & Mode, bool & SaveMode, bool UseDefaults)
  4816. {
  4817. bool Result;
  4818. int Params = GUIConfiguration->SynchronizeParams;
  4819. bool SaveSettings = false;
  4820. int Options =
  4821. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  4822. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  4823. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4824. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  4825. Result = UseDefaults ||
  4826. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  4827. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs);
  4828. if (Result)
  4829. {
  4830. Configuration->Usage->Inc(L"Synchronizations");
  4831. UpdateCopyParamCounters(CopyParam);
  4832. TSynchronizeOptions SynchronizeOptions;
  4833. GetSynchronizeOptions(Params, SynchronizeOptions);
  4834. if (SaveSettings)
  4835. {
  4836. GUIConfiguration->SynchronizeParams = Params;
  4837. }
  4838. else
  4839. {
  4840. SaveMode = false;
  4841. }
  4842. TDateTime StartTime = Now();
  4843. TSynchronizeChecklist * Checklist = NULL;
  4844. try
  4845. {
  4846. DebugAssert(!FAutoOperation);
  4847. FAutoOperation = true;
  4848. try
  4849. {
  4850. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  4851. FSynchronizeProgressForm->Start();
  4852. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  4853. static_cast<TTerminal::TSynchronizeMode>(Mode),
  4854. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4855. &SynchronizeOptions);
  4856. }
  4857. __finally
  4858. {
  4859. FAutoOperation = false;
  4860. SAFE_DESTROY(FSynchronizeProgressForm);
  4861. }
  4862. if (Checklist->Count == 0)
  4863. {
  4864. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  4865. MessageDialog(Message, qtInformation, qaOK,
  4866. HELP_SYNCHRONIZE_NO_DIFFERENCES);
  4867. }
  4868. else if (FLAGCLEAR(Params, TTerminal::spPreviewChanges) ||
  4869. DoSynchronizeChecklistDialog(Checklist, Mode, Params,
  4870. LocalDirectory, RemoteDirectory, CustomCommandMenu))
  4871. {
  4872. DebugAssert(!FAutoOperation);
  4873. void * BatchStorage;
  4874. BatchStart(BatchStorage);
  4875. FAutoOperation = true;
  4876. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  4877. {
  4878. StartTime = Now();
  4879. }
  4880. try
  4881. {
  4882. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, false);
  4883. FSynchronizeProgressForm->Start();
  4884. Terminal->SynchronizeApply(Checklist, LocalDirectory, RemoteDirectory,
  4885. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory);
  4886. }
  4887. __finally
  4888. {
  4889. FAutoOperation = false;
  4890. SAFE_DESTROY(FSynchronizeProgressForm);
  4891. BatchEnd(BatchStorage);
  4892. ReloadLocalDirectory();
  4893. }
  4894. }
  4895. }
  4896. __finally
  4897. {
  4898. delete Checklist;
  4899. }
  4900. OperationComplete(StartTime);
  4901. }
  4902. return Result;
  4903. }
  4904. //---------------------------------------------------------------------------
  4905. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  4906. const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
  4907. bool & Continue, bool /*Collect*/)
  4908. {
  4909. DebugAssert(FSynchronizeProgressForm != NULL);
  4910. if (!FSynchronizeProgressForm->Started)
  4911. {
  4912. FSynchronizeProgressForm->Start();
  4913. }
  4914. FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Continue);
  4915. }
  4916. //---------------------------------------------------------------------------
  4917. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  4918. {
  4919. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  4920. TRemoteFile * File = NULL;
  4921. Terminal->ReadFile(FullFileName, File);
  4922. if (File != NULL)
  4923. {
  4924. std::unique_ptr<TRemoteFile> FileOwner(File);
  4925. TAutoFlag Flag(FStandaloneEditing);
  4926. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  4927. Application->ShowMainForm = false;
  4928. Application->Run();
  4929. }
  4930. }
  4931. //---------------------------------------------------------------------------
  4932. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  4933. {
  4934. DebugFail();
  4935. }
  4936. //---------------------------------------------------------------------------
  4937. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  4938. {
  4939. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  4940. SessionData->Assign(Terminal->SessionData);
  4941. UpdateSessionData(SessionData.get());
  4942. TTerminalManager::Instance()->UpdateSessionCredentials(SessionData.get());
  4943. if (Terminal->SessionData->IsWorkspace)
  4944. {
  4945. // Have to reset the "Workspace/XXX" name which would become user-visible
  4946. // once IsWorkspace is cleared
  4947. SessionData->Name = UnicodeString();
  4948. SessionData->IsWorkspace = false;
  4949. }
  4950. return SessionData.release();
  4951. }
  4952. //---------------------------------------------------------------------------
  4953. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  4954. {
  4955. TSessionData * SessionData = CloneCurrentSessionData();
  4956. try
  4957. {
  4958. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  4959. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  4960. }
  4961. __finally
  4962. {
  4963. delete SessionData;
  4964. }
  4965. }
  4966. //---------------------------------------------------------------------------
  4967. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  4968. {
  4969. TTerminalManager * Manager = TTerminalManager::Instance();
  4970. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4971. if (DebugAlwaysTrue(Terminal != NULL))
  4972. {
  4973. // Update (Managed)Terminal->StateData
  4974. UpdateTerminal(Terminal);
  4975. }
  4976. Manager->SaveWorkspace(DataList.get());
  4977. return DataList.release();
  4978. }
  4979. //---------------------------------------------------------------------------
  4980. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  4981. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords)
  4982. {
  4983. WinConfiguration->LastWorkspace = Name;
  4984. if (!SavePasswords)
  4985. {
  4986. for (int Index = 0; Index < DataList->Count; Index++)
  4987. {
  4988. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  4989. if (SessionData->Link.IsEmpty())
  4990. {
  4991. SessionData->ClearSessionPasswords();
  4992. }
  4993. }
  4994. }
  4995. StoredSessions->NewWorkspace(Name, DataList);
  4996. // modified only, explicit
  4997. StoredSessions->Save(false, true);
  4998. }
  4999. //---------------------------------------------------------------------------
  5000. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5001. {
  5002. return
  5003. DefaultStr(WinConfiguration->LastWorkspace,
  5004. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5005. }
  5006. //---------------------------------------------------------------------------
  5007. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5008. {
  5009. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5010. bool AnyNonStoredSessionWithPassword = false;
  5011. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5012. bool AllNonStoredSessionsAnonymous = true;
  5013. TTerminalManager * Manager = TTerminalManager::Instance();
  5014. for (int Index = 0; Index < DataList->Count; Index++)
  5015. {
  5016. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5017. if (SessionData->Link.IsEmpty())
  5018. {
  5019. if (SessionData->HasAnySessionPassword())
  5020. {
  5021. AnyNonStoredSessionWithPassword = true;
  5022. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5023. {
  5024. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5025. }
  5026. }
  5027. if (!SameText(SessionData->UserName, AnonymousUserName))
  5028. {
  5029. AllNonStoredSessionsAnonymous = false;
  5030. }
  5031. }
  5032. }
  5033. bool SavePasswords;
  5034. bool * PSavePasswords;
  5035. bool NotRecommendedSavingPasswords =
  5036. !CustomWinConfiguration->UseMasterPassword &&
  5037. !AllNonStoredSessionsAnonymous;
  5038. if (Configuration->DisablePasswordStoring ||
  5039. !AnyNonStoredSessionWithPassword)
  5040. {
  5041. PSavePasswords = NULL;
  5042. SavePasswords = false;
  5043. }
  5044. else
  5045. {
  5046. PSavePasswords = &SavePasswords;
  5047. SavePasswords =
  5048. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5049. !NotRecommendedSavingPasswords;
  5050. }
  5051. UnicodeString Name = WorkspaceName();
  5052. bool CreateShortcut = false;
  5053. bool Result =
  5054. DoSaveWorkspaceDialog(
  5055. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5056. EnableAutoSave);
  5057. if (Result)
  5058. {
  5059. DoSaveWorkspace(Name, DataList.get(), SavePasswords);
  5060. if (CreateShortcut)
  5061. {
  5062. TOperationVisualizer Visualizer;
  5063. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5064. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5065. }
  5066. if (EnableAutoSave)
  5067. {
  5068. WinConfiguration->AutoSaveWorkspace = true;
  5069. WinConfiguration->AutoWorkspace = Name;
  5070. if (PSavePasswords != NULL)
  5071. {
  5072. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5073. }
  5074. }
  5075. }
  5076. return Result;
  5077. }
  5078. //---------------------------------------------------------------------------
  5079. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TTerminal * Terminal)
  5080. {
  5081. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  5082. DebugAssert(ManagedTerminal != NULL);
  5083. SAFE_DESTROY(ManagedTerminal->RemoteExplorerState);
  5084. if (WinConfiguration->PreservePanelState)
  5085. {
  5086. ManagedTerminal->RemoteExplorerState = RemoteDirView->SaveState();
  5087. }
  5088. UpdateSessionData(ManagedTerminal->StateData);
  5089. }
  5090. //---------------------------------------------------------------------------
  5091. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5092. {
  5093. // Keep in sync with TSessionData::CopyStateData
  5094. DebugAssert(Data != NULL);
  5095. // cannot use RemoteDirView->Path, because it is empty if connection
  5096. // was already closed
  5097. // also only peek, we may not be connected at all atm
  5098. // so make sure we do not try retrieving current directory from the server
  5099. // (particularly with FTP)
  5100. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5101. if (!ACurrentDirectory.IsEmpty())
  5102. {
  5103. Data->RemoteDirectory = ACurrentDirectory;
  5104. }
  5105. Data->Color = SessionColor;
  5106. }
  5107. //---------------------------------------------------------------------------
  5108. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5109. {
  5110. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5111. DebugAssert(Toolbar != NULL);
  5112. for (int i = 0; i < Toolbar->Items->Count; i++)
  5113. {
  5114. TTBXCustomDropDownItem * DropDownItem;
  5115. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5116. if (DropDownItem != NULL)
  5117. {
  5118. ToolbarItemResize(DropDownItem,
  5119. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5120. Toolbar->NonClientWidth);
  5121. break;
  5122. }
  5123. }
  5124. }
  5125. //---------------------------------------------------------------------------
  5126. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5127. {
  5128. Item->EditWidth = Width;
  5129. }
  5130. //---------------------------------------------------------------------------
  5131. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5132. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5133. {
  5134. for (int i = 0; i < Toolbar->Items->Count; i++)
  5135. {
  5136. TTBXCustomDropDownItem * DropDownItem;
  5137. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5138. if (DropDownItem != NULL)
  5139. {
  5140. ASize.x -= DropDownItem->EditWidth;
  5141. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5142. }
  5143. }
  5144. }
  5145. //---------------------------------------------------------------------------
  5146. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5147. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5148. {
  5149. if (WinConfiguration->DDWarnLackOfTempSpace)
  5150. {
  5151. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5152. if (!ADrive.IsEmpty())
  5153. {
  5154. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5155. DebugAssert(RequiredSpace >= 0);
  5156. __int64 RequiredWithReserve;
  5157. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5158. if (FreeSpace < RequiredWithReserve)
  5159. {
  5160. unsigned int Result;
  5161. TMessageParams Params(mpNeverAskAgainCheck);
  5162. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5163. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5164. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5165. if (Result == qaNeverAskAgain)
  5166. {
  5167. WinConfiguration->DDWarnLackOfTempSpace = false;
  5168. }
  5169. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5170. }
  5171. }
  5172. }
  5173. }
  5174. //---------------------------------------------------------------------------
  5175. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5176. {
  5177. DoOpenDirectoryDialog(odAddBookmark, Side);
  5178. }
  5179. //---------------------------------------------------------------------------
  5180. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5181. {
  5182. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5183. TStringList * VisitedDirectories = CreateSortedStringList((Side == osRemote));
  5184. try
  5185. {
  5186. TCustomDirView * DView = DirView(Side);
  5187. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5188. {
  5189. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5190. }
  5191. }
  5192. catch (...)
  5193. {
  5194. delete VisitedDirectories;
  5195. throw;
  5196. }
  5197. return VisitedDirectories;
  5198. }
  5199. //---------------------------------------------------------------------------
  5200. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5201. TOpenDirectoryMode Mode, TOperationSide Side)
  5202. {
  5203. bool Continue = true;
  5204. if (Mode == odAddBookmark)
  5205. {
  5206. TMessageParams Params(mpNeverAskAgainCheck);
  5207. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5208. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5209. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5210. unsigned int Answer =
  5211. MessageDialog(Message,
  5212. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5213. if (Answer == qaNeverAskAgain)
  5214. {
  5215. Continue = true;
  5216. WinConfiguration->UseSharedBookmarks = true;
  5217. }
  5218. else if (Answer == qaOK)
  5219. {
  5220. Continue = true;
  5221. WinConfiguration->UseSharedBookmarks = false;
  5222. }
  5223. else
  5224. {
  5225. Continue = false;
  5226. }
  5227. }
  5228. if (Continue)
  5229. {
  5230. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5231. try
  5232. {
  5233. UnicodeString Name = DirView(Side)->PathName;
  5234. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5235. // do not allow switching to location profiles,
  5236. // if we are not connected
  5237. HasDirView[osLocal] && (Terminal != NULL)))
  5238. {
  5239. TWindowLock Lock(this);
  5240. DirView(Side)->Path = Name;
  5241. }
  5242. }
  5243. __finally
  5244. {
  5245. delete VisitedDirectories;
  5246. }
  5247. }
  5248. }
  5249. //---------------------------------------------------------------------------
  5250. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5251. {
  5252. DebugAssert(!FTerminal->CommandSessionOpened);
  5253. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5254. }
  5255. //---------------------------------------------------------------------------
  5256. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5257. {
  5258. bool Result = FTerminal->IsCapable[Capability] ||
  5259. FTerminal->CommandSessionOpened;
  5260. if (!Result)
  5261. {
  5262. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5263. if (!GUIConfiguration->ConfirmCommandSession)
  5264. {
  5265. Result = true;
  5266. }
  5267. else
  5268. {
  5269. TMessageParams Params(mpNeverAskAgainCheck);
  5270. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5271. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5272. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5273. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5274. if (Answer == qaNeverAskAgain)
  5275. {
  5276. GUIConfiguration->ConfirmCommandSession = false;
  5277. Result = true;
  5278. }
  5279. else if (Answer == qaOK)
  5280. {
  5281. Result = true;
  5282. }
  5283. }
  5284. if (Result)
  5285. {
  5286. Result = CommandSessionFallback();
  5287. }
  5288. }
  5289. return Result;
  5290. }
  5291. //---------------------------------------------------------------------------
  5292. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5293. {
  5294. if (EnsureCommandSessionFallback(fcAnyCommand))
  5295. {
  5296. DoConsoleDialog(Terminal, Command);
  5297. }
  5298. }
  5299. //---------------------------------------------------------------------------
  5300. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5301. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5302. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5303. {
  5304. if (IsFileControl(DropSourceControl, osRemote) &&
  5305. (FDDExtMapFile != NULL))
  5306. {
  5307. Accept = true;
  5308. }
  5309. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5310. DebugAssert(FDDTargetControl != NULL);
  5311. }
  5312. //---------------------------------------------------------------------------
  5313. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5314. _di_IDataObject DataObj, int KeyState,
  5315. const TPoint & Point, int & Effect, bool & Accept)
  5316. {
  5317. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5318. }
  5319. //---------------------------------------------------------------------------
  5320. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5321. _di_IDataObject DataObj, int KeyState,
  5322. const TPoint & Point, int & Effect, bool & Accept)
  5323. {
  5324. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5325. }
  5326. //---------------------------------------------------------------------------
  5327. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5328. TObject * Sender)
  5329. {
  5330. DebugUsedParam(Sender);
  5331. DebugAssert(FDDTargetControl == Sender);
  5332. FDDTargetControl = NULL;
  5333. }
  5334. //---------------------------------------------------------------------------
  5335. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5336. {
  5337. FileControlDDDragLeave(SessionsPageControl);
  5338. }
  5339. //---------------------------------------------------------------------------
  5340. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5341. {
  5342. FileControlDDDragLeave(QueueView3);
  5343. }
  5344. //---------------------------------------------------------------------------
  5345. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5346. {
  5347. DebugAssert(GetSide(Side) == osRemote);
  5348. DebugUsedParam(Side);
  5349. bool Edit = false;
  5350. TRemoteFile * File = NULL;
  5351. UnicodeString FileName;
  5352. UnicodeString PointTo;
  5353. bool SymbolicLink = true;
  5354. if (RemoteDirView->ItemFocused)
  5355. {
  5356. DebugAssert(RemoteDirView->ItemFocused->Data);
  5357. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5358. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5359. if (Edit)
  5360. {
  5361. FileName = File->FileName;
  5362. PointTo = File->LinkTo;
  5363. }
  5364. else
  5365. {
  5366. PointTo = File->FileName;
  5367. }
  5368. }
  5369. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5370. Terminal->IsCapable[fcHardLink]))
  5371. {
  5372. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5373. if (Edit)
  5374. {
  5375. DebugAssert(File->FileName == FileName);
  5376. int Params = dfNoRecursive;
  5377. Terminal->ExceptionOnFail = true;
  5378. try
  5379. {
  5380. Terminal->DeleteFile(L"", File, &Params);
  5381. }
  5382. __finally
  5383. {
  5384. Terminal->ExceptionOnFail = false;
  5385. }
  5386. }
  5387. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  5388. }
  5389. }
  5390. //---------------------------------------------------------------------------
  5391. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  5392. {
  5393. return
  5394. (Terminal != NULL) &&
  5395. ((GetSide(Side) != osRemote) ||
  5396. (Terminal->ResolvingSymlinks &&
  5397. Terminal->IsCapable[fcSymbolicLink]));
  5398. }
  5399. //---------------------------------------------------------------------------
  5400. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  5401. {
  5402. return
  5403. (FCurrentSide == osRemote) &&
  5404. (RemoteDirView->ItemFocused != NULL) &&
  5405. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  5406. Terminal->SessionData->ResolveSymlinks;
  5407. }
  5408. //---------------------------------------------------------------------------
  5409. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  5410. {
  5411. DebugAssert(!WinConfiguration->DisableOpenEdit);
  5412. FForceExecution = true;
  5413. try
  5414. {
  5415. DirView(osCurrent)->ExecuteCurrentFile();
  5416. }
  5417. __finally
  5418. {
  5419. FForceExecution = false;
  5420. }
  5421. }
  5422. //---------------------------------------------------------------------------
  5423. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  5424. {
  5425. TEditorData ExternalEditor;
  5426. ExternalEditor.Editor = edExternal;
  5427. bool Remember = false;
  5428. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc,
  5429. (GetSide(osCurrent) == osRemote)))
  5430. {
  5431. if (Remember)
  5432. {
  5433. TEditorList * EditorList = new TEditorList;
  5434. try
  5435. {
  5436. *EditorList = *WinConfiguration->EditorList;
  5437. bool Found = false;
  5438. int i = 0;
  5439. while (!Found && (i < EditorList->Count))
  5440. {
  5441. const TEditorPreferences * Editor = EditorList->Editors[i];
  5442. if ((Editor->Data->Editor == edExternal) &&
  5443. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  5444. {
  5445. Found = true;
  5446. }
  5447. i++;
  5448. }
  5449. if (!Found)
  5450. {
  5451. EditorList->Add(new TEditorPreferences(ExternalEditor));
  5452. WinConfiguration->EditorList = EditorList;
  5453. }
  5454. }
  5455. __finally
  5456. {
  5457. delete EditorList;
  5458. }
  5459. }
  5460. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  5461. }
  5462. }
  5463. //---------------------------------------------------------------------------
  5464. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  5465. {
  5466. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  5467. if (FFileFindTerminal == Sender)
  5468. {
  5469. FFileFindTerminal = NULL;
  5470. HideFileFindDialog();
  5471. }
  5472. }
  5473. //---------------------------------------------------------------------------
  5474. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  5475. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  5476. {
  5477. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  5478. DebugAssert(Terminal != NULL);
  5479. if (Data->Terminal == Terminal)
  5480. {
  5481. Data->Terminal = NULL;
  5482. }
  5483. }
  5484. //---------------------------------------------------------------------------
  5485. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  5486. {
  5487. UpdateControls();
  5488. SessionColor = TColor(0);
  5489. UpdateRemotePathComboBox(false);
  5490. NeedSession(true);
  5491. }
  5492. //---------------------------------------------------------------------------
  5493. void __fastcall TCustomScpExplorerForm::NeedSession(bool ReloadSessions)
  5494. {
  5495. try
  5496. {
  5497. try
  5498. {
  5499. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  5500. }
  5501. __finally
  5502. {
  5503. if (!WinConfiguration->KeepOpenWhenNoSession &&
  5504. (!Terminal || !Terminal->Active))
  5505. {
  5506. TerminateApplication();
  5507. }
  5508. }
  5509. }
  5510. catch (EAbort &)
  5511. {
  5512. // swallow
  5513. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  5514. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  5515. }
  5516. }
  5517. //---------------------------------------------------------------------------
  5518. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged(bool Force)
  5519. {
  5520. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  5521. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  5522. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  5523. bool ListChanged = Force || (TerminalList->Count + 1 != SessionsPageControl->PageCount);
  5524. if (!ListChanged)
  5525. {
  5526. int Index = 0;
  5527. while (!ListChanged && (Index < TerminalList->Count))
  5528. {
  5529. ListChanged =
  5530. (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != TerminalList->Objects[Index]) ||
  5531. (SessionsPageControl->Pages[Index]->Caption != TerminalList->Strings[Index]);
  5532. Index++;
  5533. }
  5534. }
  5535. if (ListChanged)
  5536. {
  5537. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  5538. try
  5539. {
  5540. FSessionColors->Clear();
  5541. AddFixedSessionImages();
  5542. while (SessionsPageControl->PageCount > 0)
  5543. {
  5544. delete SessionsPageControl->Pages[0];
  5545. }
  5546. for (int Index = 0; Index < TerminalList->Count; Index++)
  5547. {
  5548. TThemeTabSheet * TabSheet = new TThemeTabSheet(SessionsPageControl);
  5549. TabSheet->Caption = TerminalList->Strings[Index];
  5550. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  5551. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  5552. TabSheet->PageControl = SessionsPageControl;
  5553. UpdateSessionTab(TabSheet);
  5554. }
  5555. TTabSheet * TabSheet = new TTabSheet(SessionsPageControl);
  5556. TabSheet->PageControl = SessionsPageControl;
  5557. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  5558. UpdateNewSessionTab();
  5559. }
  5560. __finally
  5561. {
  5562. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  5563. }
  5564. }
  5565. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  5566. }
  5567. //---------------------------------------------------------------------------
  5568. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  5569. {
  5570. DoTerminalListChanged(false);
  5571. }
  5572. //---------------------------------------------------------------------------
  5573. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  5574. {
  5575. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  5576. DebugAssert(TabSheet->ImageIndex == 0);
  5577. TabSheet->Caption =
  5578. WinConfiguration->SelectiveToolbarText ?
  5579. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  5580. UnicodeString();
  5581. }
  5582. //---------------------------------------------------------------------------
  5583. TTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  5584. {
  5585. return reinterpret_cast<TTerminal *>(TabSheet->Tag);
  5586. }
  5587. //---------------------------------------------------------------------------
  5588. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  5589. {
  5590. if (DebugAlwaysTrue(TabSheet != NULL))
  5591. {
  5592. TManagedTerminal * ManagedTerminal =
  5593. dynamic_cast<TManagedTerminal *>(GetSessionTabTerminal(TabSheet));
  5594. if (DebugAlwaysTrue(ManagedTerminal != NULL))
  5595. {
  5596. TColor Color =
  5597. (ManagedTerminal == FTerminal) ? FSessionColor : ManagedTerminal->StateData->Color;
  5598. TabSheet->ImageIndex = AddSessionColor(Color);
  5599. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  5600. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  5601. {
  5602. ThemeTabSheet->Shadowed = !ManagedTerminal->Active;
  5603. }
  5604. }
  5605. }
  5606. }
  5607. //---------------------------------------------------------------------------
  5608. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  5609. {
  5610. DebugAssert(SessionsPageControl->ActivePage != NULL);
  5611. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  5612. bool Result = (Terminal != NULL);
  5613. if (Result)
  5614. {
  5615. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  5616. }
  5617. else
  5618. {
  5619. try
  5620. {
  5621. NewSession(false);
  5622. }
  5623. __finally
  5624. {
  5625. DoTerminalListChanged(false);
  5626. }
  5627. FSessionsPageControlNewSessionTime = Now();
  5628. }
  5629. return Result;
  5630. }
  5631. //---------------------------------------------------------------------------
  5632. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  5633. {
  5634. SessionTabSwitched();
  5635. }
  5636. //---------------------------------------------------------------------------
  5637. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  5638. {
  5639. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  5640. DebugAssert(TransferList != NULL);
  5641. UnicodeString Name;
  5642. if (TransferList->ItemIndex <= 0)
  5643. {
  5644. Name = L"";
  5645. }
  5646. else
  5647. {
  5648. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  5649. }
  5650. if (FCopyParamAutoSelected.IsEmpty())
  5651. {
  5652. // if previous preset was not autoselected, make new preset the "default"
  5653. FCopyParamDefault = Name;
  5654. }
  5655. GUIConfiguration->CopyParamCurrent = Name;
  5656. }
  5657. //---------------------------------------------------------------------------
  5658. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  5659. {
  5660. // sanity check
  5661. bool ExistingPreset =
  5662. (FTransferListHoverIndex >= 0) &&
  5663. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  5664. DebugAssert(ExistingPreset);
  5665. if (ExistingPreset)
  5666. {
  5667. HDC DC = GetDC(0);
  5668. TCanvas * Canvas = new TCanvas();
  5669. try
  5670. {
  5671. Canvas->Handle = DC;
  5672. Canvas->Font = GetToolbarFont(this);
  5673. UnicodeString Name;
  5674. if (FTransferListHoverIndex == 0)
  5675. {
  5676. Name = L"";
  5677. }
  5678. else
  5679. {
  5680. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  5681. }
  5682. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  5683. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  5684. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  5685. static_cast<TObject*>(GetComponent(fcTransferList)));
  5686. UnicodeString InfoStr =
  5687. GUIConfiguration->CopyParamPreset[Name].
  5688. GetInfoStr(L"; ",
  5689. FLAGMASK(Terminal != NULL, Terminal->UsableCopyParamAttrs(0).General));
  5690. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  5691. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  5692. {
  5693. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  5694. {
  5695. InfoStr.SetLength(InfoStr.Length() - 1);
  5696. }
  5697. InfoStr += Ellipsis;
  5698. }
  5699. // UpdateCaption does not cause invalidation of whole submenu, while
  5700. // setting Caption property does.
  5701. // also it probably does not resize the label, even if necessary
  5702. // (we do not want that anyway)
  5703. TransferLabel->UpdateCaption(InfoStr);
  5704. }
  5705. __finally
  5706. {
  5707. Canvas->Handle = NULL;
  5708. ReleaseDC(0, DC);
  5709. delete Canvas;
  5710. }
  5711. }
  5712. }
  5713. //---------------------------------------------------------------------------
  5714. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  5715. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  5716. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  5717. {
  5718. if (FTransferListHoverIndex != AHoverIndex)
  5719. {
  5720. FTransferListHoverIndex = AHoverIndex;
  5721. UpdateTransferLabel();
  5722. }
  5723. }
  5724. //---------------------------------------------------------------------------
  5725. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  5726. {
  5727. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  5728. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  5729. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  5730. {
  5731. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  5732. {
  5733. OpenDirectory(GetSide(osCurrent));
  5734. Message.Result = 1;
  5735. }
  5736. else
  5737. {
  5738. TForm::Dispatch(&Message);
  5739. }
  5740. }
  5741. else
  5742. {
  5743. TForm::Dispatch(&Message);
  5744. }
  5745. }
  5746. //---------------------------------------------------------------------------
  5747. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  5748. {
  5749. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  5750. if ((FIgnoreNextDialogChar != 0) &&
  5751. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  5752. {
  5753. Message.Result = 1;
  5754. }
  5755. else
  5756. {
  5757. TForm::Dispatch(&Message);
  5758. }
  5759. FIgnoreNextDialogChar = 0;
  5760. }
  5761. //---------------------------------------------------------------------------
  5762. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  5763. {
  5764. // The four low-order bits are used internally by Windows
  5765. unsigned int Cmd = (Message.WParam & 0xFFF0);
  5766. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  5767. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  5768. // 61730 - restore thru double click - undocumented
  5769. // 61490 - maximize thru double click - undocumented
  5770. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  5771. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  5772. {
  5773. SysResizing(Cmd);
  5774. }
  5775. TForm::Dispatch(&Message);
  5776. }
  5777. //---------------------------------------------------------------------------
  5778. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  5779. {
  5780. // We were actually never able to make ENDSESSION_CRITICAL happen.
  5781. // Also there no point returning TRUE as we are not able to
  5782. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  5783. // Hence the process termination might be safer :)
  5784. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  5785. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  5786. {
  5787. Message.Result = FALSE;
  5788. }
  5789. else
  5790. {
  5791. Message.Result = TRUE;
  5792. }
  5793. // Do not call default handling as that triggers OnCloseQuery,
  5794. // where our implementation will popup confirmation dialogs, what we do not want,
  5795. // as per Vista guidelines:
  5796. // https://msdn.microsoft.com/en-us/library/ms700677.aspx
  5797. }
  5798. //---------------------------------------------------------------------------
  5799. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  5800. {
  5801. if (Message.EndSession && IsApplicationMinimized())
  5802. {
  5803. // WORKAROUND
  5804. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  5805. // when it receives WM_ENDSESSION,
  5806. // but that sometimes (particularly when application is minimized) cause crashes.
  5807. // Crash popups message that blocks log off.
  5808. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  5809. // so we call ExitProcess() immediately, not even trying to cleanup.
  5810. // It still causes beep, so there's likely some popup, but it does not block
  5811. // log off.
  5812. ExitProcess(0);
  5813. }
  5814. TForm::Dispatch(&Message);
  5815. }
  5816. #ifdef _DEBUG
  5817. //---------------------------------------------------------------------------
  5818. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  5819. {
  5820. TForm::Dispatch(&Message);
  5821. }
  5822. #endif
  5823. //---------------------------------------------------------------------------
  5824. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  5825. {
  5826. }
  5827. //---------------------------------------------------------------------------
  5828. void __fastcall TCustomScpExplorerForm::DoShow()
  5829. {
  5830. // only now are the controls resized finally, so the size constraints
  5831. // will not conflict with possibly very small window size
  5832. RestoreFormParams();
  5833. FixControlsPlacement();
  5834. if (Position == poDefaultPosOnly)
  5835. {
  5836. CutFormToDesktop(this);
  5837. }
  5838. TForm::DoShow();
  5839. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  5840. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  5841. if (Terminal == NULL)
  5842. {
  5843. StartingDisconnected();
  5844. }
  5845. UpdatePixelsPerInchMainWindowCounter();
  5846. FShowing = true;
  5847. }
  5848. //---------------------------------------------------------------------------
  5849. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  5850. {
  5851. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  5852. }
  5853. //---------------------------------------------------------------------------
  5854. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  5855. {
  5856. DoTerminalListChanged(true);
  5857. }
  5858. //---------------------------------------------------------------------------
  5859. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  5860. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  5861. TNotifyEvent OnBalloonClick, TObject * UserData)
  5862. {
  5863. bool Do;
  5864. UnicodeString Message;
  5865. if (E == NULL)
  5866. {
  5867. Message = Str;
  5868. Do = true;
  5869. }
  5870. else
  5871. {
  5872. Do = ExceptionMessage(E, Message);
  5873. }
  5874. if (Do && WinConfiguration->BalloonNotifications)
  5875. {
  5876. UnicodeString Title;
  5877. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  5878. {
  5879. Message = TrimLeft(Message);
  5880. }
  5881. else
  5882. {
  5883. Message = UnformatMessage(Message);
  5884. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  5885. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  5886. Title = LoadResourceString(Captions[Type]);
  5887. if (Terminal != NULL)
  5888. {
  5889. Title = FORMAT(L"%s - %s",
  5890. (TTerminalManager::Instance()->TerminalTitle(Terminal), Title));
  5891. }
  5892. }
  5893. if (Seconds == 0)
  5894. {
  5895. Seconds = WinConfiguration->NotificationsTimeout;
  5896. }
  5897. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  5898. }
  5899. }
  5900. //---------------------------------------------------------------------------
  5901. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  5902. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  5903. UnicodeString HelpKeyword, const TMessageParams * Params,
  5904. TTerminal * Terminal)
  5905. {
  5906. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  5907. (FOnFeedSynchronizeError != NULL)) &&
  5908. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  5909. {
  5910. if (FOnFeedSynchronizeError != NULL)
  5911. {
  5912. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  5913. }
  5914. else
  5915. {
  5916. DebugAssert(FErrorList != NULL);
  5917. TStringList * MoreMessagesCopy = NULL;
  5918. if (MoreMessages)
  5919. {
  5920. MoreMessagesCopy = new TStringList();
  5921. MoreMessagesCopy->Assign(MoreMessages);
  5922. }
  5923. FErrorList->AddObject(Message, MoreMessagesCopy);
  5924. }
  5925. PopupTrayBalloon(Terminal, Message, Type);
  5926. return ContinueAnswer(Answers);
  5927. }
  5928. else
  5929. {
  5930. bool UseBalloon = FTrayIcon->Visible;
  5931. if (UseBalloon)
  5932. {
  5933. PopupTrayBalloon(Terminal, Message, Type);
  5934. }
  5935. unsigned int Result;
  5936. try
  5937. {
  5938. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  5939. }
  5940. __finally
  5941. {
  5942. // cancel only balloon we popped up, otherwise we may cancel notification
  5943. // balloon that was there before the message dialog
  5944. // (such as "dd confirmation opt in balloon)
  5945. if (UseBalloon)
  5946. {
  5947. FTrayIcon->CancelBalloon();
  5948. }
  5949. }
  5950. return Result;
  5951. }
  5952. }
  5953. //---------------------------------------------------------------------------
  5954. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  5955. TTerminal * Terminal, Exception * E)
  5956. {
  5957. if (FTrayIcon->Visible)
  5958. {
  5959. PopupTrayBalloon(Terminal, L"", qtError, E);
  5960. }
  5961. // particularly prevent opening new session from jump list,
  5962. // while exception is shown
  5963. NonVisualDataModule->StartBusy();
  5964. try
  5965. {
  5966. ShowExtendedExceptionEx(Terminal, E);
  5967. }
  5968. __finally
  5969. {
  5970. NonVisualDataModule->EndBusy();
  5971. FTrayIcon->CancelBalloon();
  5972. }
  5973. }
  5974. //---------------------------------------------------------------------------
  5975. void __fastcall TCustomScpExplorerForm::TerminalReady()
  5976. {
  5977. // cannot rely on active page being page for active terminal,
  5978. // as it can happen that active page is the "new session" page
  5979. // (e.g. when reconnecting active terminal, while login dialog
  5980. // invoked from "new session" page is modal)
  5981. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  5982. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  5983. }
  5984. //---------------------------------------------------------------------------
  5985. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  5986. TTerminal * Terminal, Exception * E)
  5987. {
  5988. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  5989. if (!Terminal->Active)
  5990. {
  5991. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  5992. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  5993. {
  5994. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  5995. }
  5996. }
  5997. }
  5998. //---------------------------------------------------------------------------
  5999. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6000. UnicodeString Message, TQueryType Type,
  6001. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6002. {
  6003. if ((E == NULL) ||
  6004. ExceptionMessage(E, Message))
  6005. {
  6006. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6007. if (Important)
  6008. {
  6009. Seconds *= 5;
  6010. }
  6011. UnicodeString NoteMessage(UnformatMessage(Message));
  6012. if (Terminal != NULL)
  6013. {
  6014. NoteMessage = FORMAT(L"%s: %s",
  6015. (TTerminalManager::Instance()->TerminalTitle(Terminal), NoteMessage));
  6016. }
  6017. if (WinConfiguration->BalloonNotifications)
  6018. {
  6019. AddNote(NoteMessage);
  6020. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6021. }
  6022. else
  6023. {
  6024. FlashOnBackground();
  6025. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6026. }
  6027. }
  6028. }
  6029. //---------------------------------------------------------------------------
  6030. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6031. {
  6032. RestoreApp();
  6033. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6034. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6035. !NonVisualDataModule->Busy)
  6036. {
  6037. TTerminal * Terminal = TerminalNoteData->Terminal;
  6038. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6039. if (!ComponentVisible[fcQueueView])
  6040. {
  6041. ToggleQueueVisibility();
  6042. GoToQueue();
  6043. }
  6044. }
  6045. }
  6046. //---------------------------------------------------------------------------
  6047. void __fastcall TCustomScpExplorerForm::QueueEvent(TTerminal * ATerminal,
  6048. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6049. {
  6050. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  6051. UnicodeString Message;
  6052. TNotifyEvent OnClick = NULL;
  6053. TObject * UserData = NULL;
  6054. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6055. switch (Event)
  6056. {
  6057. case qeEmptyButMonitored:
  6058. if ((ATerminal != Terminal) || QueueInvisible)
  6059. {
  6060. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6061. OnClick = QueueEmptyNoteClicked;
  6062. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6063. TerminalNoteData->Terminal = ATerminal;
  6064. UserData = TerminalNoteData;
  6065. }
  6066. break;
  6067. case qeEmpty:
  6068. OperationComplete(ManagedTerminal->QueueOperationStart);
  6069. break;
  6070. case qePendingUserAction:
  6071. if ((ATerminal != Terminal) ||
  6072. (QueueInvisible && !IsQueueAutoPopup()))
  6073. {
  6074. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6075. }
  6076. break;
  6077. default:
  6078. DebugFail();
  6079. }
  6080. if (!Message.IsEmpty())
  6081. {
  6082. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6083. }
  6084. }
  6085. //---------------------------------------------------------------------------
  6086. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6087. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6088. {
  6089. if (FDDExtMapFile != NULL)
  6090. {
  6091. CloseHandle(FDDExtMapFile);
  6092. FDDExtMapFile = NULL;
  6093. }
  6094. if (WinConfiguration->DDExtEnabled)
  6095. {
  6096. if (!WinConfiguration->DDExtInstalled)
  6097. {
  6098. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtNotInstalled");
  6099. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_NOT_INSTALLED2), HELP_DRAGEXT_TARGET_NOT_INSTALLED);
  6100. }
  6101. DDExtInitDrag(FileList, Created);
  6102. }
  6103. }
  6104. //---------------------------------------------------------------------------
  6105. void __fastcall TCustomScpExplorerForm::DDExtInitDrag(TFileList * FileList,
  6106. bool & Created)
  6107. {
  6108. FDragExtFakeDirectory =
  6109. ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6110. if (!ForceDirectories(ApiPath(FDragExtFakeDirectory)))
  6111. {
  6112. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (FDragExtFakeDirectory)));
  6113. }
  6114. FileList->AddItem(NULL, FDragExtFakeDirectory);
  6115. Created = true;
  6116. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6117. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6118. {
  6119. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6120. TDragExtCommStruct* CommStruct;
  6121. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6122. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6123. DebugAssert(CommStruct != NULL);
  6124. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6125. CommStruct->Dragging = true;
  6126. wcsncpy(CommStruct->DropDest, FDragExtFakeDirectory.c_str(),
  6127. LENOF(CommStruct->DropDest));
  6128. NULL_TERMINATE(CommStruct->DropDest);
  6129. UnmapViewOfFile(CommStruct);
  6130. }
  6131. FDDMoveSlipped = false;
  6132. }
  6133. //---------------------------------------------------------------------------
  6134. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6135. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6136. {
  6137. bool Result;
  6138. if (Sender == RemoteDirView)
  6139. {
  6140. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6141. }
  6142. else
  6143. {
  6144. DebugAssert(Sender == RemoteDriveView);
  6145. TStrings * FileList = RemoteDriveView->DragFileList();
  6146. try
  6147. {
  6148. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6149. }
  6150. __finally
  6151. {
  6152. delete FileList;
  6153. }
  6154. }
  6155. if (FDDTargetControl == RemoteDriveView)
  6156. {
  6157. RemoteDriveView->UpdateDropTarget();
  6158. }
  6159. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6160. // or from tree view to dir view.
  6161. // foMove happens when file/dir is dragged from remote tree view outside of
  6162. // application via dragex
  6163. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6164. (DropSourceControl == RemoteDriveView))
  6165. {
  6166. RemoteDriveView->UpdateDropSource();
  6167. }
  6168. return Result;
  6169. }
  6170. //---------------------------------------------------------------------------
  6171. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6172. {
  6173. // This also handles drops of remote files to queue.
  6174. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6175. SAFE_DESTROY(FDDFileList);
  6176. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6177. {
  6178. try
  6179. {
  6180. TDragResult DDResult = (Sender == RemoteDirView) ?
  6181. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6182. // Focus is moved to the target application,
  6183. // but as we are going to present the UI, we need to steal the focus back.
  6184. // This most likely won't work though (windows does not allow application
  6185. // to steal focus most of the time)
  6186. Application->BringToFront();
  6187. // On older version of Windows we never got drMove here, see also comment below.
  6188. // On Windows 10, we get the "move".
  6189. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6190. {
  6191. UnicodeString TargetDirectory;
  6192. TFileOperation Operation;
  6193. // drInvalid may mean drMove, see comment below
  6194. switch (DDResult)
  6195. {
  6196. case drCopy:
  6197. Operation = foCopy;
  6198. break;
  6199. case drMove:
  6200. Operation = foMove;
  6201. break;
  6202. default:
  6203. DebugFail();
  6204. case drInvalid:
  6205. // prefer "copy" for safety
  6206. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6207. break;
  6208. }
  6209. if (FDDMoveSlipped)
  6210. {
  6211. Operation = foMove;
  6212. }
  6213. TTransferOperationParam Param;
  6214. bool Internal;
  6215. bool ForceQueue;
  6216. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, Internal))
  6217. {
  6218. // we get drInvalid both if d&d was intercepted by ddext,
  6219. // and when users drops on no-drop location.
  6220. // we tell the difference by existence of response from ddext,
  6221. // so we ignore absence of response in this case
  6222. if (DDResult != drInvalid)
  6223. {
  6224. // here we know that the extension is installed,
  6225. // as it is checked as soon as drag&drop starts from
  6226. // RemoteFileControlDDCreateDragFileList
  6227. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6228. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6229. }
  6230. }
  6231. else
  6232. {
  6233. // download using ddext
  6234. Param.Temp = false;
  6235. Param.DragDrop = true;
  6236. if (ForceQueue)
  6237. {
  6238. Param.Queue = asOn;
  6239. }
  6240. if (Sender == RemoteDirView)
  6241. {
  6242. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6243. }
  6244. if (RemoteFileControlFileOperation(Sender, Operation,
  6245. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6246. {
  6247. Configuration->Usage->Inc(
  6248. Internal ? L"DownloadsDragDropInternal" : L"DownloadsDragDropExternalExt");
  6249. }
  6250. }
  6251. }
  6252. }
  6253. __finally
  6254. {
  6255. CloseHandle(FDDExtMapFile);
  6256. FDDExtMapFile = NULL;
  6257. RemoveDir(ApiPath(FDragExtFakeDirectory));
  6258. FDragExtFakeDirectory = L"";
  6259. }
  6260. }
  6261. if (FDragDropSshTerminate.get() != NULL)
  6262. {
  6263. FDragDropSshTerminate->Rethrow();
  6264. }
  6265. }
  6266. //---------------------------------------------------------------------------
  6267. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6268. TObject * Sender, int dwEffect, HRESULT & /*Result*/)
  6269. {
  6270. HCURSOR SlippedCopyCursor;
  6271. FDDMoveSlipped =
  6272. (FDragMoveCursor != NULL) &&
  6273. (!WinConfiguration->DDAllowMoveInit) && (dwEffect == DROPEFFECT_Copy) &&
  6274. ((IsFileControl(FDDTargetControl, osRemote) && (GetKeyState(VK_CONTROL) >= 0) &&
  6275. FTerminal->IsCapable[fcRemoteMove]) ||
  6276. (IsFileControl(FDDTargetControl, osLocal) && (GetKeyState(VK_SHIFT) < 0)));
  6277. SlippedCopyCursor = FDDMoveSlipped ? FDragMoveCursor : Dragdrop::DefaultCursor;
  6278. DragDropFiles(Sender)->CHCopy = SlippedCopyCursor;
  6279. DragDropFiles(Sender)->CHScrollCopy = SlippedCopyCursor;
  6280. // Remember drop effect so we know (when user drops files), if we copy or move
  6281. FLastDropEffect = dwEffect;
  6282. }
  6283. //---------------------------------------------------------------------------
  6284. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6285. UnicodeString & Directory, bool & ForceQueue, bool & Internal)
  6286. {
  6287. bool Result;
  6288. if (FDDTargetControl == QueueView3)
  6289. {
  6290. Directory = DefaultDownloadTargetDirectory();
  6291. Result = true;
  6292. Internal = true;
  6293. ForceQueue = true;
  6294. }
  6295. else
  6296. {
  6297. ForceQueue = false;
  6298. Enabled = false;
  6299. try
  6300. {
  6301. int Timer = 0;
  6302. Result = false;
  6303. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6304. {
  6305. {
  6306. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6307. TDragExtCommStruct* CommStruct;
  6308. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6309. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6310. DebugAssert(CommStruct != NULL);
  6311. Result = !CommStruct->Dragging;
  6312. if (Result)
  6313. {
  6314. Directory = ExtractFilePath(CommStruct->DropDest);
  6315. Internal = false;
  6316. }
  6317. UnmapViewOfFile(CommStruct);
  6318. }
  6319. if (!Result)
  6320. {
  6321. Sleep(50);
  6322. Timer += 50;
  6323. Application->ProcessMessages();
  6324. }
  6325. }
  6326. }
  6327. __finally
  6328. {
  6329. Enabled = true;
  6330. }
  6331. }
  6332. return Result;
  6333. }
  6334. //---------------------------------------------------------------------------
  6335. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  6336. const UnicodeString TempDir, int SecDelay)
  6337. {
  6338. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  6339. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  6340. if (FDelayedDeletionTimer == NULL)
  6341. {
  6342. DebugAssert(HandleAllocated());
  6343. FDelayedDeletionTimer = new TTimer(this);
  6344. FDelayedDeletionTimer->Interval = 10000;
  6345. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  6346. }
  6347. else
  6348. {
  6349. FDelayedDeletionTimer->Enabled = true;
  6350. }
  6351. }
  6352. //---------------------------------------------------------------------------
  6353. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  6354. {
  6355. DebugAssert(FDelayedDeletionList != NULL);
  6356. TDateTime N = Now();
  6357. TDateTime Alert;
  6358. UnicodeString Directory;
  6359. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  6360. {
  6361. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  6362. if ((N >= Alert) || (Sender == NULL))
  6363. {
  6364. Directory = FDelayedDeletionList->Strings[Index];
  6365. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  6366. {
  6367. FDelayedDeletionList->Delete(Index);
  6368. }
  6369. }
  6370. }
  6371. if (FDelayedDeletionList->Count == 0)
  6372. {
  6373. FDelayedDeletionTimer->Enabled = false;
  6374. }
  6375. }
  6376. //---------------------------------------------------------------------------
  6377. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  6378. {
  6379. if (IsFileControl(FDDTargetControl, osRemote) ||
  6380. (FDDTargetControl == SessionsPageControl))
  6381. {
  6382. TTerminal * TargetTerminal = NULL;
  6383. TFileOperation Operation = foNone;
  6384. if (FDDTargetControl == SessionsPageControl)
  6385. {
  6386. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  6387. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  6388. // do not allow dropping on the "+" tab
  6389. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  6390. if (TargetTerminal != NULL)
  6391. {
  6392. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  6393. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  6394. {
  6395. Operation = foRemoteMove;
  6396. }
  6397. else
  6398. {
  6399. Operation = foRemoteCopy;
  6400. }
  6401. }
  6402. }
  6403. else
  6404. {
  6405. // when move from remote side is disabled, we allow copying inside the remote
  6406. // panel, but we interpret is as moving (we also slip in the move cursor)
  6407. if ((FLastDropEffect == DROPEFFECT_MOVE) ||
  6408. (!WinConfiguration->DDAllowMoveInit && (FLastDropEffect == DROPEFFECT_COPY) &&
  6409. FDDMoveSlipped))
  6410. {
  6411. Operation = foRemoteMove;
  6412. }
  6413. else if (FLastDropEffect == DROPEFFECT_COPY)
  6414. {
  6415. Operation = foRemoteCopy;
  6416. }
  6417. }
  6418. if (Operation != foNone)
  6419. {
  6420. RemoteFileControlFileOperation(DropSourceControl,
  6421. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  6422. }
  6423. // abort drag&drop
  6424. Abort();
  6425. }
  6426. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  6427. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  6428. (FDDTargetControl != QueueView3))
  6429. {
  6430. DebugAssert(!FDragTempDir.IsEmpty());
  6431. TTransferType Type;
  6432. UnicodeString TempDir = FDragTempDir;
  6433. // We clear FDragTempDir before calling
  6434. // just in case it fail (raises exception)
  6435. FDragTempDir = L"";
  6436. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  6437. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  6438. // empty directory parameter means temp directory -> don't display it!
  6439. UnicodeString TargetDir = L"";
  6440. int Options = 0;
  6441. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  6442. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  6443. {
  6444. Abort();
  6445. }
  6446. // TargetDir is set when dropped on local file control
  6447. // (this was workaround for legacy dirview event handling, now it should be
  6448. // made prettier)
  6449. if (TargetDir.IsEmpty())
  6450. {
  6451. TargetDir = TempDir;
  6452. if (ForceDirectories(ApiPath(TargetDir)))
  6453. {
  6454. DebugAssert(Terminal && !TargetDir.IsEmpty());
  6455. FPendingTempSpaceWarn = true;
  6456. try
  6457. {
  6458. FDragDropOperation = true;
  6459. int Params = cpTemporary |
  6460. (Type == ttMove ? cpDelete : 0);
  6461. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  6462. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  6463. }
  6464. __finally
  6465. {
  6466. FDragDropOperation = false;
  6467. FPendingTempSpaceWarn = false;
  6468. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  6469. }
  6470. }
  6471. else
  6472. {
  6473. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  6474. }
  6475. }
  6476. }
  6477. }
  6478. //---------------------------------------------------------------------------
  6479. void __fastcall TCustomScpExplorerForm::DDDownload(TStrings * FilesToCopy,
  6480. const UnicodeString TargetDir, const TCopyParamType * CopyParam, int Params)
  6481. {
  6482. void * BatchStorage;
  6483. BatchStart(BatchStorage);
  6484. try
  6485. {
  6486. UpdateCopyParamCounters(*CopyParam);
  6487. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  6488. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  6489. {
  6490. RemoteDriveView->UpdateDropSource();
  6491. }
  6492. }
  6493. __finally
  6494. {
  6495. BatchEnd(BatchStorage);
  6496. }
  6497. }
  6498. //---------------------------------------------------------------------------
  6499. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  6500. {
  6501. public:
  6502. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  6503. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename)
  6504. {
  6505. }
  6506. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  6507. {
  6508. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  6509. TDataObjectFilesEx::AllowData(FormatEtc);
  6510. }
  6511. };
  6512. //---------------------------------------------------------------------------
  6513. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  6514. TObject * Sender, TDataObject *& DataObject)
  6515. {
  6516. if (FDDExtMapFile != NULL)
  6517. {
  6518. TFileList * FileList = DragDropFiles(Sender)->FileList;
  6519. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  6520. {
  6521. Abort();
  6522. }
  6523. if (FileList->Count > 0)
  6524. {
  6525. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  6526. if (!FilesObject->IsValid(true, true))
  6527. {
  6528. FilesObject->_Release();
  6529. }
  6530. else
  6531. {
  6532. DataObject = FilesObject;
  6533. }
  6534. }
  6535. }
  6536. }
  6537. //---------------------------------------------------------------------------
  6538. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  6539. {
  6540. DebugFail();
  6541. }
  6542. //---------------------------------------------------------------------------
  6543. void __fastcall TCustomScpExplorerForm::GoToTree()
  6544. {
  6545. ComponentVisible[fcRemoteTree] = true;
  6546. RemoteDriveView->SetFocus();
  6547. }
  6548. //---------------------------------------------------------------------------
  6549. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  6550. TPanelExport Export)
  6551. {
  6552. TCustomDirView * DirView = this->DirView(Side);
  6553. std::unique_ptr<TStrings> ExportData(new TStringList());
  6554. switch (Export)
  6555. {
  6556. case pePath:
  6557. ExportData->Add(DirView->PathName);
  6558. break;
  6559. case peFileList:
  6560. case peFullFileList:
  6561. {
  6562. bool FullPath = (Export == peFullFileList);
  6563. DirView->CreateFileList(false, FullPath, ExportData.get());
  6564. for (int Index = 0; Index < ExportData->Count; Index++)
  6565. {
  6566. if (ExportData->Strings[Index].Pos(L" ") > 0)
  6567. {
  6568. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  6569. }
  6570. }
  6571. }
  6572. break;
  6573. default:
  6574. DebugFail();
  6575. }
  6576. return ExportData.release();
  6577. }
  6578. //---------------------------------------------------------------------------
  6579. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  6580. TPanelExport Export, TPanelExportDestination Destination)
  6581. {
  6582. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  6583. PanelExportStore(Side, Export, Destination, ExportData.get());
  6584. }
  6585. //---------------------------------------------------------------------------
  6586. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  6587. TPanelExport /*Export*/, TPanelExportDestination Destination,
  6588. TStrings * ExportData)
  6589. {
  6590. if (Destination == pedClipboard)
  6591. {
  6592. TInstantOperationVisualizer Visualizer;
  6593. CopyToClipboard(ExportData);
  6594. }
  6595. else
  6596. {
  6597. DebugFail();
  6598. }
  6599. }
  6600. //---------------------------------------------------------------------------
  6601. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  6602. {
  6603. TCustomDirView * DirView = this->DirView(Side);
  6604. TFileFilter Filter;
  6605. DefaultFileFilter(Filter);
  6606. Filter.Masks = DirView->Mask;
  6607. if (DoFilterMaskDialog(DirView, &Filter))
  6608. {
  6609. DirView->Mask = TFileMasks::NormalizeMask(Filter.Masks);
  6610. Configuration->Usage->Inc(L"Filters");
  6611. }
  6612. }
  6613. //---------------------------------------------------------------------------
  6614. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  6615. {
  6616. return FQueueController->DefaultOperation();
  6617. }
  6618. //---------------------------------------------------------------------------
  6619. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  6620. TQueueOperation Operation, void ** Param)
  6621. {
  6622. switch (Operation)
  6623. {
  6624. case qoPreferences:
  6625. return true;
  6626. case qoGoTo:
  6627. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  6628. case qoOnceEmpty:
  6629. return !FQueueController->Empty;
  6630. default:
  6631. return FQueueController->AllowOperation(Operation, Param);
  6632. }
  6633. }
  6634. //---------------------------------------------------------------------------
  6635. void __fastcall TCustomScpExplorerForm::GoToQueue()
  6636. {
  6637. if (DebugAlwaysTrue(QueueView3->Visible))
  6638. {
  6639. QueueView3->SetFocus();
  6640. }
  6641. }
  6642. //---------------------------------------------------------------------------
  6643. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  6644. TQueueOperation Operation, void * Param)
  6645. {
  6646. if (Operation == qoGoTo)
  6647. {
  6648. GoToQueue();
  6649. }
  6650. else if (Operation == qoPreferences)
  6651. {
  6652. PreferencesDialog(pmQueue);
  6653. }
  6654. else
  6655. {
  6656. FQueueController->ExecuteOperation(Operation, Param);
  6657. }
  6658. }
  6659. //---------------------------------------------------------------------------
  6660. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  6661. {
  6662. return (Queue != NULL) && Queue->Enabled;
  6663. }
  6664. //---------------------------------------------------------------------------
  6665. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  6666. {
  6667. DebugAssert(Queue != NULL);
  6668. if (Queue != NULL)
  6669. {
  6670. Queue->Enabled = !Queue->Enabled;
  6671. }
  6672. }
  6673. //---------------------------------------------------------------------------
  6674. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  6675. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  6676. {
  6677. FQueueActedItem = QueueView3->ItemFocused;
  6678. }
  6679. //---------------------------------------------------------------------------
  6680. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  6681. {
  6682. return TopDock->Height + QueueSplitter->Height;
  6683. }
  6684. //---------------------------------------------------------------------------
  6685. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  6686. TObject * /*Sender*/, int & NewSize, bool & Accept)
  6687. {
  6688. // when queue is hidden by double-clicking splitter, stray attempt to
  6689. // resize the panel with strange value arrives, make sure it is ignored
  6690. if (ComponentVisible[fcQueueView])
  6691. {
  6692. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  6693. RemotePanel->Constraints->MinHeight;
  6694. if (NewSize > HeightLimit)
  6695. {
  6696. NewSize = HeightLimit;
  6697. }
  6698. }
  6699. else
  6700. {
  6701. Accept = false;
  6702. }
  6703. }
  6704. //---------------------------------------------------------------------------
  6705. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  6706. TDragObject *& /*DragObject*/)
  6707. {
  6708. FQueueActedItem = QueueView3->ItemFocused;
  6709. }
  6710. //---------------------------------------------------------------------------
  6711. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  6712. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  6713. {
  6714. Accept = true;
  6715. if (Source == QueueView3)
  6716. {
  6717. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  6718. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  6719. if (Accept)
  6720. {
  6721. TQueueItemProxy * QueueItem;
  6722. TQueueItemProxy * DestQueueItem;
  6723. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  6724. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  6725. Accept = (QueueItem != DestQueueItem) &&
  6726. (QueueItem->Status == TQueueItem::qsPending) &&
  6727. (DestQueueItem->Status == TQueueItem::qsPending);
  6728. }
  6729. }
  6730. }
  6731. //---------------------------------------------------------------------------
  6732. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  6733. TObject * /*Source*/, int /*X*/, int /*Y*/)
  6734. {
  6735. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  6736. {
  6737. TQueueItemProxy * QueueItem;
  6738. TQueueItemProxy * DestQueueItem;
  6739. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  6740. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  6741. QueueItem->Move(DestQueueItem);
  6742. }
  6743. }
  6744. //---------------------------------------------------------------------------
  6745. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  6746. {
  6747. if ((QueueView3->ItemFocused == NULL) &&
  6748. (QueueView3->Items->Count > 0))
  6749. {
  6750. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  6751. }
  6752. QueueLabelUpdateStatus();
  6753. }
  6754. //---------------------------------------------------------------------------
  6755. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  6756. {
  6757. QueueLabelUpdateStatus();
  6758. }
  6759. //---------------------------------------------------------------------------
  6760. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  6761. {
  6762. QueueLabel->UpdateStatus();
  6763. }
  6764. //---------------------------------------------------------------------------
  6765. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  6766. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  6767. {
  6768. if (Selected)
  6769. {
  6770. NonVisualDataModule->UpdateNonVisibleActions();
  6771. }
  6772. }
  6773. //---------------------------------------------------------------------------
  6774. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  6775. {
  6776. TDragDropFilesEx * Result = NULL;
  6777. if (Sender == SessionsPageControl)
  6778. {
  6779. Result = FSessionsDragDropFilesEx;
  6780. }
  6781. else if (Sender == QueueView3)
  6782. {
  6783. Result = FQueueDragDropFilesEx;
  6784. }
  6785. else
  6786. {
  6787. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  6788. if (DirView != NULL)
  6789. {
  6790. Result = DirView->DragDropFilesEx;
  6791. }
  6792. else
  6793. {
  6794. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  6795. if (DriveView != NULL)
  6796. {
  6797. Result = DriveView->DragDropFilesEx;
  6798. }
  6799. }
  6800. }
  6801. DebugAssert(Result != NULL);
  6802. return Result;
  6803. }
  6804. //---------------------------------------------------------------------------
  6805. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  6806. {
  6807. return (DView->SelCount == DView->FilesCount);
  6808. }
  6809. //---------------------------------------------------------------------------
  6810. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  6811. {
  6812. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  6813. }
  6814. //---------------------------------------------------------------------------
  6815. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  6816. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue)
  6817. {
  6818. TFileOperation Operation;
  6819. switch (Effect)
  6820. {
  6821. case DROPEFFECT_MOVE:
  6822. Operation = foMove;
  6823. break;
  6824. case DROPEFFECT_COPY:
  6825. // occures on WinXP (reported by user)
  6826. default:
  6827. Operation = foCopy;
  6828. break;
  6829. };
  6830. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  6831. // see a comment in TUnixDirView::PerformItemDragDropOperation
  6832. if (DragDropFilesEx->FileList->Count > 0)
  6833. {
  6834. TStrings * FileList = new TStringList();
  6835. try
  6836. {
  6837. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  6838. {
  6839. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  6840. }
  6841. FDragDropOperation = true;
  6842. TTransferOperationParam Param;
  6843. Param.TargetDirectory = TargetPath;
  6844. // upload, no temp dirs
  6845. Param.Temp = false;
  6846. Param.DragDrop = true;
  6847. Param.Options =
  6848. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  6849. if (ForceQueue)
  6850. {
  6851. Param.Queue = asOn;
  6852. }
  6853. if (ExecuteFileOperation(Operation, osLocal, FileList,
  6854. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6855. {
  6856. if (IsFileControl(DropSourceControl, osLocal))
  6857. {
  6858. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  6859. }
  6860. else
  6861. {
  6862. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  6863. }
  6864. }
  6865. }
  6866. __finally
  6867. {
  6868. FDragDropOperation = false;
  6869. delete FileList;
  6870. }
  6871. }
  6872. }
  6873. //---------------------------------------------------------------------------
  6874. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  6875. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  6876. UnicodeString TargetPath, bool & /*DoOperation*/)
  6877. {
  6878. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false);
  6879. }
  6880. //---------------------------------------------------------------------------
  6881. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  6882. TObject * Sender, int grfKeyState, int & dwEffect)
  6883. {
  6884. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  6885. if ((dwEffect != DROPEFFECT_None) &&
  6886. IsFileControl(DropSourceControl, osRemote))
  6887. {
  6888. // do not allow drop on remote panel (on free space, still allow drop on directories)
  6889. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  6890. (RemoteDirView->DropTarget == NULL))
  6891. {
  6892. dwEffect = DROPEFFECT_None;
  6893. }
  6894. else
  6895. {
  6896. if (dwEffect == DROPEFFECT_Copy)
  6897. {
  6898. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  6899. // currently we support copying always (at least via temporary directory);
  6900. // remove associated checks once this all proves stable and working
  6901. bool CopyCapable = true;
  6902. // if we do not support neither of operations, there's no discussion
  6903. if (!MoveCapable && !CopyCapable)
  6904. {
  6905. dwEffect = DROPEFFECT_None;
  6906. }
  6907. // when moving is disabled, we need to keep effect to "copy",
  6908. // which will be later interpretted as move (with slipped-in cursor)
  6909. else if (!WinConfiguration->DDAllowMoveInit && FLAGCLEAR(grfKeyState, MK_CONTROL))
  6910. {
  6911. // no-op, keep copy
  6912. }
  6913. else
  6914. {
  6915. // The default effect inside remote panel is move,
  6916. // unless we do not support it, but support copy
  6917. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  6918. {
  6919. dwEffect = MoveCapable ? DROPEFFECT_Move : DROPEFFECT_Copy;
  6920. }
  6921. else
  6922. {
  6923. // with ctrl-down, we want copy unless it is not supported
  6924. dwEffect = CopyCapable ? DROPEFFECT_Copy : DROPEFFECT_None;
  6925. }
  6926. }
  6927. }
  6928. }
  6929. }
  6930. }
  6931. //---------------------------------------------------------------------------
  6932. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  6933. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  6934. {
  6935. if (FDDTotalSize >= 0)
  6936. {
  6937. if (File->IsDirectory)
  6938. {
  6939. FDDTotalSize = -1;
  6940. }
  6941. else
  6942. {
  6943. FDDTotalSize += File->Size;
  6944. }
  6945. }
  6946. DebugAssert(!FDragTempDir.IsEmpty());
  6947. // TODO: this is quite ineffective
  6948. // TODO: what if invalid character replacement is disabled?
  6949. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  6950. UnicodeString TransferFileName;
  6951. if (Sender == RemoteDriveView)
  6952. {
  6953. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  6954. }
  6955. else
  6956. {
  6957. TransferFileName = File->FileName;
  6958. }
  6959. FDDFileList->AddObject(TransferFileName, File);
  6960. }
  6961. //---------------------------------------------------------------------------
  6962. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  6963. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  6964. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  6965. {
  6966. // sometimes we do not get DDEnd so the list is not released
  6967. SAFE_DESTROY(FDDFileList);
  6968. FDDFileList = new TStringList();
  6969. FDragTempDir = WinConfiguration->TemporaryDir();
  6970. FDDTotalSize = 0;
  6971. FDragDropSshTerminate.reset(NULL);
  6972. }
  6973. //---------------------------------------------------------------------------
  6974. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  6975. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  6976. HRESULT & Result)
  6977. {
  6978. if (Result == DRAGDROP_S_DROP)
  6979. {
  6980. try
  6981. {
  6982. GlobalDragImageList->HideDragImage();
  6983. try
  6984. {
  6985. RemoteFileControlDDTargetDrop();
  6986. }
  6987. catch(ESshTerminate & E)
  6988. {
  6989. FDragDropSshTerminate.reset(E.Clone());
  6990. }
  6991. }
  6992. catch (Exception &E)
  6993. {
  6994. // If downloading fails we need to cancel drag&drop, otherwise
  6995. // Explorer shows error
  6996. // But by the way exception probably never reach this point as
  6997. // it's catched on way.
  6998. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  6999. Result = DRAGDROP_S_CANCEL;
  7000. ShowExtendedException(Terminal, &E);
  7001. }
  7002. }
  7003. }
  7004. //---------------------------------------------------------------------------
  7005. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7006. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7007. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7008. {
  7009. TFileMasks::TParams MaskParams;
  7010. MaskParams.Size = Size;
  7011. MaskParams.Modification = Modification;
  7012. // this does not re-parse the mask if it is the same as the last time
  7013. FDirViewMatchMask = Masks;
  7014. bool ImplicitMatch;
  7015. Matches =
  7016. // RecurseInclude parameter has no effect as the Path is empty
  7017. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7018. (AllowImplicitMatches || !ImplicitMatch);
  7019. }
  7020. //---------------------------------------------------------------------------
  7021. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7022. TObject * Sender, TListItem * Item, WORD & Indexes)
  7023. {
  7024. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7025. UnicodeString Ext;
  7026. if (DirView == RemoteDirView)
  7027. {
  7028. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7029. }
  7030. else
  7031. {
  7032. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7033. }
  7034. if (SameText(Ext, Configuration->PartialExt))
  7035. {
  7036. Indexes |= oiPartial;
  7037. }
  7038. }
  7039. //---------------------------------------------------------------------------
  7040. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7041. {
  7042. return
  7043. DirViewEnabled(osCurrent) &&
  7044. DirView(osCurrent)->CanPasteFromClipBoard();
  7045. }
  7046. //---------------------------------------------------------------------------
  7047. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7048. {
  7049. bool Result = false;
  7050. if (CanPasteToDirViewFromClipBoard())
  7051. {
  7052. Result = true;
  7053. }
  7054. else
  7055. {
  7056. UnicodeString ClipboardText;
  7057. if (NonEmptyTextFromClipboard(ClipboardText))
  7058. {
  7059. if (StoredSessions->IsUrl(ClipboardText))
  7060. {
  7061. Result = true;
  7062. }
  7063. else
  7064. {
  7065. Result = DirViewEnabled(osCurrent);
  7066. }
  7067. }
  7068. }
  7069. return Result;
  7070. }
  7071. //---------------------------------------------------------------------------
  7072. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7073. {
  7074. if (CanPasteToDirViewFromClipBoard())
  7075. {
  7076. DirView(osCurrent)->PasteFromClipBoard();
  7077. }
  7078. else
  7079. {
  7080. UnicodeString ClipboardText;
  7081. if (NonEmptyTextFromClipboard(ClipboardText))
  7082. {
  7083. if (StoredSessions->IsUrl(ClipboardText))
  7084. {
  7085. NewSession(false, ClipboardText);
  7086. }
  7087. else
  7088. {
  7089. DirView(osCurrent)->Path = ClipboardText;
  7090. }
  7091. }
  7092. }
  7093. }
  7094. //---------------------------------------------------------------------------
  7095. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7096. {
  7097. // TBD
  7098. }
  7099. //---------------------------------------------------------------------------
  7100. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7101. {
  7102. TCustomDirView * ADirView = DirView(Side);
  7103. ADirView->SelectAll(Mode);
  7104. ADirView->SetFocus();
  7105. }
  7106. //---------------------------------------------------------------------------
  7107. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7108. {
  7109. TCustomDirView * ADirView = DirView(Side);
  7110. TFileFilter Filter;
  7111. DefaultFileFilter(Filter);
  7112. if (DoSelectMaskDialog(ADirView, Select, &Filter, Configuration))
  7113. {
  7114. Configuration->Usage->Inc(L"MaskSelections");
  7115. ADirView->SelectFiles(Filter, Select);
  7116. ADirView->SetFocus();
  7117. }
  7118. }
  7119. //---------------------------------------------------------------------------
  7120. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7121. {
  7122. TCustomDirView * ADirView = DirView(Side);
  7123. ADirView->RestoreSelectedNames();
  7124. ADirView->SetFocus();
  7125. }
  7126. //---------------------------------------------------------------------------
  7127. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7128. {
  7129. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7130. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7131. {
  7132. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7133. UnicodeString Ext;
  7134. if (GetSide(osCurrent) == osRemote)
  7135. {
  7136. Ext = UnixExtractFileExt(FileName);
  7137. }
  7138. else
  7139. {
  7140. Ext = ExtractFileExt(FileName);
  7141. }
  7142. if (Ext.IsEmpty())
  7143. {
  7144. Ext = L".";
  7145. }
  7146. TFileFilter Filter;
  7147. Filter.Masks = FORMAT(L"*%s", (Ext));
  7148. Filter.Directories = false;
  7149. CurrentDirView->SelectFiles(Filter, Select);
  7150. }
  7151. }
  7152. //---------------------------------------------------------------------------
  7153. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7154. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7155. {
  7156. UnicodeString Result;
  7157. if ((Side == osRemote) && (Terminal == NULL))
  7158. {
  7159. // noop
  7160. }
  7161. else
  7162. {
  7163. Result =
  7164. FMTLOAD(FILE_INFO_FORMAT,
  7165. (FormatBytes(FileInfo.SelectedSize),
  7166. FormatBytes(FileInfo.FilesSize),
  7167. FormatNumber(FileInfo.SelectedCount),
  7168. FormatNumber(FileInfo.FilesCount)));
  7169. }
  7170. return Result;
  7171. }
  7172. //---------------------------------------------------------------------------
  7173. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7174. TTBXStatusPanel * Panel, TOperationSide Side)
  7175. {
  7176. if (Panel->Index == 1)
  7177. {
  7178. ToggleShowHiddenFiles();
  7179. }
  7180. else if (Panel->Index == 2)
  7181. {
  7182. Filter(Side);
  7183. }
  7184. }
  7185. //---------------------------------------------------------------------------
  7186. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7187. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7188. {
  7189. DebugAssert(!StatusBar->SimplePanel);
  7190. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7191. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7192. }
  7193. //---------------------------------------------------------------------------
  7194. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7195. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7196. {
  7197. DebugAssert(StatusBar->Panels->Count >= 3);
  7198. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7199. if (FileInfo.HiddenCount > 0)
  7200. {
  7201. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7202. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7203. }
  7204. else
  7205. {
  7206. HiddenFilesPanel->ViewPriority = 0;
  7207. // not really necessary, just to cleanup no-longer-valid data
  7208. HiddenFilesPanel->Caption = L"";
  7209. }
  7210. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7211. if (FileInfo.FilteredCount > 0)
  7212. {
  7213. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7214. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7215. }
  7216. else
  7217. {
  7218. FilteredFilesPanel->ViewPriority = 0;
  7219. // not really necessary, just to cleanup no-longer-valid data
  7220. FilteredFilesPanel->Caption = L"";
  7221. }
  7222. }
  7223. //---------------------------------------------------------------------------
  7224. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7225. TObject * /*Sender*/)
  7226. {
  7227. if (RemoteDirView->Enabled)
  7228. {
  7229. RemoteDirView->SetFocus();
  7230. }
  7231. }
  7232. //---------------------------------------------------------------------------
  7233. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7234. {
  7235. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7236. switch (Config.Show)
  7237. {
  7238. case qvShow:
  7239. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7240. {
  7241. Config.Show = qvHide;
  7242. }
  7243. else
  7244. {
  7245. Config.Show = Config.LastHideShow;
  7246. }
  7247. break;
  7248. case qvHideWhenEmpty:
  7249. if (ComponentVisible[fcQueueView])
  7250. {
  7251. Config.Show = qvHide;
  7252. }
  7253. else
  7254. {
  7255. Config.LastHideShow = Config.Show;
  7256. Config.Show = qvShow;
  7257. }
  7258. break;
  7259. case qvHide:
  7260. Config.LastHideShow = Config.Show;
  7261. Config.Show = qvShow;
  7262. break;
  7263. }
  7264. WinConfiguration->QueueView = Config;
  7265. }
  7266. //---------------------------------------------------------------------------
  7267. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7268. {
  7269. UnicodeString Result;
  7270. if (FTerminal != NULL)
  7271. {
  7272. switch (WinConfiguration->PathInCaption)
  7273. {
  7274. case picShort:
  7275. {
  7276. Result = UnixExtractFileName(FTerminal->CurrentDirectory);
  7277. if (Result.IsEmpty())
  7278. {
  7279. Result = FTerminal->CurrentDirectory;
  7280. }
  7281. }
  7282. break;
  7283. case picFull:
  7284. Result = FTerminal->CurrentDirectory;
  7285. break;
  7286. }
  7287. }
  7288. return Result;
  7289. }
  7290. //---------------------------------------------------------------------------
  7291. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7292. {
  7293. TTerminalManager::Instance()->UpdateAppTitle();
  7294. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7295. // background to gray on Windows 7 (works on Windows 10).
  7296. // See also EnableControl
  7297. if (Showing)
  7298. {
  7299. bool HasTerminal = (Terminal != NULL);
  7300. if (HasTerminal)
  7301. {
  7302. if (!RemoteDirView->Enabled)
  7303. {
  7304. RemoteDirView->Enabled = true;
  7305. if (FRemoteDirViewWasFocused)
  7306. {
  7307. ActiveControl = RemoteDirView;
  7308. }
  7309. }
  7310. RemoteDriveView->Enabled = true;
  7311. RemoteDirView->Color = (FSessionColor != 0 ? FSessionColor : clWindow);
  7312. RemoteDriveView->Color = RemoteDirView->Color;
  7313. }
  7314. else
  7315. {
  7316. if (RemoteDirView->Enabled)
  7317. {
  7318. // This is first called when the form is being constructed
  7319. // (not anymore due to Showing test above)
  7320. // but the false is overriden in the constructor later.
  7321. // An even later in TScpCommanderForm::DoShow()
  7322. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  7323. EnableControl(RemoteDirView, false);
  7324. }
  7325. EnableControl(RemoteDriveView, false);
  7326. }
  7327. EnableControl(QueueView3, HasTerminal);
  7328. QueueLabelUpdateStatus();
  7329. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  7330. }
  7331. }
  7332. //---------------------------------------------------------------------------
  7333. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  7334. {
  7335. UpdateControls();
  7336. }
  7337. //---------------------------------------------------------------------------
  7338. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  7339. TObject * Sender)
  7340. {
  7341. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7342. DebugAssert(DirView != NULL);
  7343. DoDirViewLoaded(DirView);
  7344. TransferPresetAutoSelect();
  7345. }
  7346. //---------------------------------------------------------------------------
  7347. void __fastcall TCustomScpExplorerForm::StartUpdates()
  7348. {
  7349. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  7350. // first run after installation
  7351. if (double(Updates.LastCheck) == 0)
  7352. {
  7353. // make sure next time there will be an update (if enabled)
  7354. Updates.LastCheck = TDateTime(1);
  7355. WinConfiguration->Updates = Updates;
  7356. }
  7357. else if ((double(Updates.Period) > 0) &&
  7358. (Now() - Updates.LastCheck >= Updates.Period))
  7359. {
  7360. StartUpdateThread(UpdatesChecked);
  7361. }
  7362. }
  7363. //---------------------------------------------------------------------------
  7364. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  7365. {
  7366. UnicodeString Message;
  7367. bool New;
  7368. TQueryType Type;
  7369. GetUpdatesMessage(Message, New, Type, false);
  7370. if (!Message.IsEmpty())
  7371. {
  7372. if (New)
  7373. {
  7374. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  7375. }
  7376. if (!New && (Type != qtWarning))
  7377. {
  7378. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  7379. }
  7380. else
  7381. {
  7382. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  7383. }
  7384. if (New)
  7385. {
  7386. Configuration->Usage->Inc(L"UpdateNotifications");
  7387. }
  7388. }
  7389. }
  7390. //---------------------------------------------------------------------------
  7391. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  7392. {
  7393. RestoreApp();
  7394. bool CanDisplay = !NonVisualDataModule->Busy;
  7395. if (!CanDisplay && (Screen->ActiveForm != NULL))
  7396. {
  7397. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  7398. }
  7399. if (CanDisplay)
  7400. {
  7401. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  7402. CheckForUpdates(true);
  7403. }
  7404. else
  7405. {
  7406. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  7407. }
  7408. }
  7409. //---------------------------------------------------------------------------
  7410. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  7411. TCopyParamRuleData & Data)
  7412. {
  7413. DebugAssert(Terminal != NULL);
  7414. Data.HostName = Terminal->SessionData->HostNameExpanded;
  7415. Data.UserName = Terminal->SessionData->UserNameExpanded;
  7416. Data.RemoteDirectory = RemoteDirView->PathName;
  7417. }
  7418. //---------------------------------------------------------------------------
  7419. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  7420. {
  7421. // Terminal can be null when we are changing local directory implicitly,
  7422. // because it has been deleted, while no session is connected
  7423. // (Login dialog is open)
  7424. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  7425. {
  7426. TCopyParamRuleData Data;
  7427. GetTransferPresetAutoSelectData(Data);
  7428. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  7429. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  7430. if (CopyParamIndex < 0)
  7431. {
  7432. // there is no preset that matches autoselection
  7433. // set preset that we consider "default"
  7434. FCopyParamAutoSelected = L""; // forget last autoselected preset
  7435. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  7436. }
  7437. else
  7438. {
  7439. // there is preset matching autoselection
  7440. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  7441. if (CopyParamName == FCopyParamAutoSelected)
  7442. {
  7443. // autoselected the same preset as the last time
  7444. // make no change (i.e. preserve custom user preset, if any)
  7445. }
  7446. else
  7447. {
  7448. // autoselected the different preset then the last time (or there
  7449. // was default preset set)
  7450. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  7451. GUIConfiguration->CopyParamCurrent = CopyParamName;
  7452. }
  7453. }
  7454. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  7455. {
  7456. if (CopyParamIndex >= 0)
  7457. {
  7458. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  7459. }
  7460. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  7461. try
  7462. {
  7463. int Fmt =
  7464. (CopyParamIndex < 0) ?
  7465. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  7466. COPY_PARAM_AUTOSELECTED;
  7467. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  7468. Data->Message = MainInstructions(Message);
  7469. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  7470. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  7471. CopyParamAttrs);
  7472. if (CopyParamIndex >= 0)
  7473. {
  7474. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  7475. Info = FORMAT(L"%s\n \n%s", (Info,
  7476. FMTLOAD(COPY_PARAM_RULE,
  7477. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  7478. }
  7479. Data->Message += L"\n\n" + Info;
  7480. if (WinConfiguration->CopyParamAutoSelectNotice)
  7481. {
  7482. TransferPresetNoteMessage(Data, true);
  7483. }
  7484. else
  7485. {
  7486. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  7487. Data = NULL; // ownership passed
  7488. }
  7489. }
  7490. __finally
  7491. {
  7492. delete Data;
  7493. }
  7494. }
  7495. }
  7496. }
  7497. //---------------------------------------------------------------------------
  7498. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  7499. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  7500. {
  7501. DebugAssert(NoteData != NULL);
  7502. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  7503. TQueryButtonAlias Aliases[1];
  7504. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  7505. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  7506. Params.Aliases = Aliases;
  7507. Params.AliasesCount = LENOF(Aliases);
  7508. unsigned int Result =
  7509. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  7510. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  7511. switch (Result)
  7512. {
  7513. case qaNeverAskAgain:
  7514. DebugAssert(AllowNeverAskAgain);
  7515. WinConfiguration->CopyParamAutoSelectNotice = false;
  7516. break;
  7517. case qaIgnore:
  7518. PreferencesDialog(pmPresets);
  7519. break;
  7520. }
  7521. }
  7522. //---------------------------------------------------------------------------
  7523. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  7524. {
  7525. // as of now useless, as this is used for notes only, never for balloons, ...
  7526. RestoreApp();
  7527. // .. and we should never be busy here
  7528. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  7529. {
  7530. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  7531. }
  7532. }
  7533. //---------------------------------------------------------------------------
  7534. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  7535. TPreferencesMode APreferencesMode)
  7536. {
  7537. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  7538. TCopyParamRuleData Data;
  7539. if (Terminal != NULL)
  7540. {
  7541. PreferencesData.reset(new TPreferencesDialogData());
  7542. GetTransferPresetAutoSelectData(Data);
  7543. PreferencesData->CopyParamRuleData = &Data;
  7544. }
  7545. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  7546. }
  7547. //---------------------------------------------------------------------------
  7548. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  7549. const TCustomCommandType & Command)
  7550. {
  7551. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  7552. {
  7553. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  7554. }
  7555. }
  7556. //---------------------------------------------------------------------------
  7557. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  7558. {
  7559. bool RemoteAllowed = CustomCommandRemoteAllowed();
  7560. TCustomCommandType Command;
  7561. // make sure we use local custom command when remote are not supported
  7562. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  7563. {
  7564. Command = FLastCustomCommand;
  7565. }
  7566. else
  7567. {
  7568. Command.Params = Command.Params | ccLocal;
  7569. }
  7570. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  7571. FEditingFocusedAdHocCommand = OnFocused;
  7572. bool LocalSide = (FCurrentSide == osLocal);
  7573. int Options =
  7574. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  7575. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  7576. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  7577. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  7578. {
  7579. FLastCustomCommand = Command;
  7580. UpdateCustomCommandsToolbar();
  7581. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  7582. }
  7583. }
  7584. //---------------------------------------------------------------------------
  7585. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  7586. {
  7587. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  7588. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  7589. DebugAssert(State > 0);
  7590. if (State <= 0)
  7591. {
  7592. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  7593. }
  7594. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  7595. }
  7596. //---------------------------------------------------------------------------
  7597. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  7598. TCustomCommandType & Command, int & State)
  7599. {
  7600. bool Result = !FLastCustomCommand.Command.IsEmpty();
  7601. if (Result)
  7602. {
  7603. Command = FLastCustomCommand;
  7604. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  7605. }
  7606. return Result;
  7607. }
  7608. //---------------------------------------------------------------------------
  7609. void __fastcall TCustomScpExplorerForm::BeforeAction()
  7610. {
  7611. if (RemoteDirView->ItemFocused != NULL)
  7612. {
  7613. RemoteDirView->ItemFocused->CancelEdit();
  7614. }
  7615. }
  7616. //---------------------------------------------------------------------------
  7617. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  7618. {
  7619. DebugAssert(ComponentVisible[Component]);
  7620. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  7621. }
  7622. //---------------------------------------------------------------------------
  7623. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  7624. {
  7625. // when queue panel is resized here directly, the status bar is stretched
  7626. // over whole space the panel occupied
  7627. PostComponentHide(fcQueueView);
  7628. }
  7629. //---------------------------------------------------------------------------
  7630. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  7631. {
  7632. TMessage * M = static_cast<TMessage*>(Message);
  7633. switch (M->Msg)
  7634. {
  7635. case CM_DIALOGCHAR:
  7636. CMDialogChar(*M);
  7637. break;
  7638. case WM_APPCOMMAND:
  7639. WMAppCommand(*M);
  7640. break;
  7641. case WM_SYSCOMMAND:
  7642. WMSysCommand(*M);
  7643. break;
  7644. case WM_QUERYENDSESSION:
  7645. WMQueryEndSession(*M);
  7646. break;
  7647. case WM_ENDSESSION:
  7648. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  7649. break;
  7650. #ifdef _DEBUG
  7651. case WM_WINDOWPOSCHANGED:
  7652. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  7653. break;
  7654. case CM_CUSTOMSTYLECHANGED:
  7655. TForm::Dispatch(Message);
  7656. break;
  7657. #endif
  7658. case WM_COMPONENT_HIDE:
  7659. {
  7660. Byte Component = static_cast<Byte>(M->WParam);
  7661. // sanity check
  7662. if (ComponentVisible[Component])
  7663. {
  7664. // special treatment
  7665. if (Component == fcQueueView)
  7666. {
  7667. ToggleQueueVisibility();
  7668. DebugAssert(!ComponentVisible[fcQueueView]);
  7669. }
  7670. else
  7671. {
  7672. ComponentVisible[Component] = false;
  7673. }
  7674. }
  7675. }
  7676. break;
  7677. case WM_COPYDATA:
  7678. WMCopyData(*M);
  7679. break;
  7680. case WM_MANAGES_CAPTION:
  7681. // caption managed in TTerminalManager::UpdateAppTitle()
  7682. M->Result = 1;
  7683. break;
  7684. case WM_WANTS_MOUSEWHEEL:
  7685. M->Result = 1;
  7686. break;
  7687. case CM_SHOWINGCHANGED:
  7688. CMShowingChanged(*M);
  7689. break;
  7690. case WM_CLOSE:
  7691. WMClose(*M);
  7692. break;
  7693. case WM_DPICHANGED:
  7694. WMDpiChanged(*M);
  7695. break;
  7696. case CM_DPICHANGED:
  7697. CMDpiChanged(*M);
  7698. break;
  7699. default:
  7700. TForm::Dispatch(Message);
  7701. break;
  7702. }
  7703. }
  7704. //---------------------------------------------------------------------------
  7705. void __fastcall TCustomScpExplorerForm::UpdateImages()
  7706. {
  7707. // noop
  7708. }
  7709. //---------------------------------------------------------------------------
  7710. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  7711. {
  7712. TForm::Dispatch(&Message);
  7713. // regenerate session images
  7714. DoTerminalListChanged(true);
  7715. UpdateImages();
  7716. }
  7717. //---------------------------------------------------------------------------
  7718. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  7719. {
  7720. TForm::Dispatch(&Message);
  7721. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  7722. UpdatePixelsPerInchMainWindowCounter();
  7723. }
  7724. //---------------------------------------------------------------------------
  7725. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  7726. {
  7727. // Cannot close window while we are busy.
  7728. // We cannot test this in FormCloseQuery as that is called also from
  7729. // Close(), which is called by CloseApplicationAction. So we can be busy
  7730. // there already even, when it is legitimate to close the application.
  7731. // Possibly a better place to handle this would be WMSysCommand.
  7732. if (NonVisualDataModule->Busy)
  7733. {
  7734. Message.Result = 1;
  7735. }
  7736. else
  7737. {
  7738. TForm::Dispatch(&Message);
  7739. }
  7740. }
  7741. //---------------------------------------------------------------------------
  7742. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  7743. {
  7744. TForm::Dispatch(&Message);
  7745. if (Showing && (Terminal == NULL))
  7746. {
  7747. // When we are starting minimized (i.e. from an installer),
  7748. // postpone showing Login dialog until we get restored.
  7749. // Otherwise the Login dialog (and Authentication window) show restored
  7750. // over invidible (minimized) main window.
  7751. if (WindowState == wsMinimized)
  7752. {
  7753. FNeedSession = true;
  7754. }
  7755. else
  7756. {
  7757. // This happens before application ever goes idle, so the toolbars would
  7758. // stay enabled (initial state) until the Login dialog is dismissed.
  7759. UpdateActions();
  7760. NonVisualDataModule->StartBusy();
  7761. try
  7762. {
  7763. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  7764. // otherwise the dialog does not receive focus.
  7765. // With Commander interface the ProcessMessages is called already
  7766. // by TDriveView, but with Explorer interface, we need to call it explicily
  7767. Application->ProcessMessages();
  7768. // do not reload sessions, they have been loaded just now (optimization)
  7769. NeedSession(false);
  7770. }
  7771. __finally
  7772. {
  7773. NonVisualDataModule->EndBusy();
  7774. }
  7775. }
  7776. }
  7777. }
  7778. //---------------------------------------------------------------------------
  7779. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  7780. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  7781. int & MaxHeight)
  7782. {
  7783. // workaround for bug in TWinControl.CalcConstraints
  7784. // Check for empty rect (restore from iconic state) is done there only after
  7785. // call to AdjustClientRect, which enlarges the rect (for forms).
  7786. TRect R = GetClientRect();
  7787. // when restoring from iconic state, place no restrictions
  7788. if (IsRectEmpty(R))
  7789. {
  7790. MinWidth = 0;
  7791. MinHeight = 0;
  7792. MaxWidth = 32000;
  7793. MaxHeight = 32000;
  7794. }
  7795. }
  7796. //---------------------------------------------------------------------------
  7797. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  7798. TSpaceAvailable & ASpaceAvailable, bool & Close)
  7799. {
  7800. // terminal can be already closed (e.g. dropped connection)
  7801. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  7802. {
  7803. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  7804. try
  7805. {
  7806. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  7807. }
  7808. catch(...)
  7809. {
  7810. if (!Terminal->Active)
  7811. {
  7812. Close = true;
  7813. }
  7814. throw;
  7815. }
  7816. }
  7817. }
  7818. //---------------------------------------------------------------------------
  7819. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  7820. {
  7821. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  7822. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  7823. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  7824. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  7825. {
  7826. OnGetSpaceAvailable = GetSpaceAvailable;
  7827. }
  7828. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  7829. OnGetSpaceAvailable);
  7830. }
  7831. //---------------------------------------------------------------------------
  7832. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  7833. {
  7834. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  7835. Terminal->FillSessionDataForCode(Data.get());
  7836. return Data.release();
  7837. }
  7838. //---------------------------------------------------------------------------
  7839. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  7840. {
  7841. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  7842. DoGenerateUrlDialog(Data.get(), Paths);
  7843. }
  7844. //---------------------------------------------------------------------------
  7845. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  7846. {
  7847. GenerateUrl(NULL);
  7848. }
  7849. //---------------------------------------------------------------------------
  7850. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  7851. {
  7852. std::unique_ptr<TStrings> Paths(new TStringList());
  7853. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  7854. GenerateUrl(Paths.get());
  7855. }
  7856. //---------------------------------------------------------------------------
  7857. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  7858. {
  7859. if (value != FSessionColor)
  7860. {
  7861. FSessionColor = value;
  7862. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  7863. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  7864. static_cast<TObject *>(GetComponent(fcColorMenu)));
  7865. DebugAssert(ColorItem != NULL);
  7866. ColorItem->Color = C;
  7867. NonVisualDataModule->ColorMenuItem->Color = C;
  7868. // Is null when called from LastTerminalClosed
  7869. if (Terminal != NULL)
  7870. {
  7871. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  7872. }
  7873. UpdateControls();
  7874. }
  7875. }
  7876. //---------------------------------------------------------------------------
  7877. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  7878. {
  7879. bool Result = FNoteTimer->Enabled;
  7880. if (Result)
  7881. {
  7882. // cannot cancel note too early
  7883. bool NotEarly =
  7884. (Now() - FNoteShown >
  7885. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  7886. if (Force || NotEarly)
  7887. {
  7888. FNoteTimer->Enabled = false;
  7889. FNote = L"";
  7890. SAFE_DESTROY(FNoteData);
  7891. FOnNoteClick = NULL;
  7892. FNoteHints = FNotes->Text;
  7893. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  7894. UpdateStatusBar();
  7895. }
  7896. }
  7897. return Result;
  7898. }
  7899. //---------------------------------------------------------------------------
  7900. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  7901. {
  7902. DebugAssert(FNoteTimer->Enabled);
  7903. CancelNote(true);
  7904. }
  7905. //---------------------------------------------------------------------------
  7906. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  7907. {
  7908. int P = Note.Pos(L"\n");
  7909. if (P > 0)
  7910. {
  7911. Note.SetLength(P - 1);
  7912. }
  7913. FNotes->Add(FORMAT(L"[%s] %s",
  7914. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  7915. while (FNotes->Count > 10)
  7916. {
  7917. FNotes->Delete(0);
  7918. }
  7919. if (UpdateNow)
  7920. {
  7921. FNoteHints = FNotes->Text;
  7922. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  7923. UpdateStatusBar();
  7924. }
  7925. }
  7926. //---------------------------------------------------------------------------
  7927. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  7928. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  7929. {
  7930. int P = Note.Pos(L"\n");
  7931. if (P > 0)
  7932. {
  7933. Note.SetLength(P - 1);
  7934. }
  7935. FNoteHints = FNotes->Text;
  7936. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  7937. FNote = Note;
  7938. SAFE_DESTROY(FNoteData);
  7939. FNoteData = NoteData;
  7940. FOnNoteClick = OnNoteClick;
  7941. AddNote(Note, false);
  7942. UpdateStatusBar();
  7943. FNoteShown = Now();
  7944. FNoteTimer->Enabled = false;
  7945. if (Seconds == 0)
  7946. {
  7947. Seconds = WinConfiguration->NotificationsTimeout;
  7948. }
  7949. FNoteTimer->Interval = Seconds * MSecsPerSec;
  7950. FNoteTimer->Enabled = true;
  7951. }
  7952. //---------------------------------------------------------------------------
  7953. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  7954. {
  7955. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  7956. }
  7957. //---------------------------------------------------------------------------
  7958. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  7959. {
  7960. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  7961. {
  7962. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  7963. }
  7964. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  7965. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  7966. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  7967. 0, NULL, NULL);
  7968. }
  7969. //---------------------------------------------------------------------------
  7970. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  7971. {
  7972. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  7973. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  7974. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  7975. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  7976. }
  7977. //---------------------------------------------------------------------------
  7978. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  7979. {
  7980. WinConfiguration->FormatSizeBytes = Style;
  7981. }
  7982. //---------------------------------------------------------------------------
  7983. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  7984. {
  7985. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  7986. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  7987. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  7988. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  7989. }
  7990. //---------------------------------------------------------------------------
  7991. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  7992. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  7993. {
  7994. if (Panel->Index == 0)
  7995. {
  7996. if (FOnNoteClick != NULL)
  7997. {
  7998. // prevent the user data from being freed by possible call
  7999. // to CancelNote or PostNote during call to OnNoteClick
  8000. std::unique_ptr<TObject> NoteData(FNoteData);
  8001. TNotifyEvent OnNoteClick = FOnNoteClick;
  8002. FNoteData = NULL;
  8003. // need to cancel the note as we are going to delete its user data
  8004. CancelNote(true);
  8005. OnNoteClick(NoteData.get());
  8006. }
  8007. }
  8008. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8009. {
  8010. FileSystemInfo();
  8011. }
  8012. }
  8013. //---------------------------------------------------------------------------
  8014. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8015. {
  8016. // workaround:
  8017. // 1) for unknown reason, disabling window, while minimized,
  8018. // prevents it from restoring, even if it was enabled again meanwhile
  8019. // 2) when disabling the main window, while another has focus
  8020. // minimize is no longer possible
  8021. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8022. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8023. // Shouldn't we use IsApplicationMinimized() here?
  8024. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8025. {
  8026. Enabled = false;
  8027. }
  8028. FLockLevel++;
  8029. }
  8030. //---------------------------------------------------------------------------
  8031. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8032. {
  8033. DebugAssert(FLockLevel > 0);
  8034. FLockLevel--;
  8035. if (FLockLevel == 0)
  8036. {
  8037. DebugAssert(FLockSuspendLevel == 0);
  8038. Enabled = true;
  8039. }
  8040. }
  8041. //---------------------------------------------------------------------------
  8042. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8043. {
  8044. // We need to make sure that window is enabled when the last modal window closes
  8045. // otherwise focus is not restored correctly.
  8046. // So we re-enable the window when modal window opens and
  8047. // disable it back after is closes
  8048. if (FLockLevel > 0)
  8049. {
  8050. // while we have nesting counter, we know that we never be called
  8051. // recursivelly as Application->OnModalBegin is called only
  8052. // for the top-level modal window
  8053. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8054. {
  8055. // won't be disabled when conditions in LockWindow() were not satisfied
  8056. FDisabledOnLockSuspend = !Enabled;
  8057. // When minimized to tray (or actually when set to SW_HIDE),
  8058. // setting Enabled makes the window focusable even when there's
  8059. // modal window over it
  8060. if (!FTrayIcon->Visible)
  8061. {
  8062. Enabled = true;
  8063. }
  8064. }
  8065. FLockSuspendLevel++;
  8066. }
  8067. }
  8068. //---------------------------------------------------------------------------
  8069. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8070. {
  8071. if (FLockSuspendLevel > 0)
  8072. {
  8073. DebugAssert(FLockLevel > 0);
  8074. FLockSuspendLevel--;
  8075. // see comment in SuspendWindowLock
  8076. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8077. {
  8078. // Note that window can be enabled here, when we were minized to tray when
  8079. // was SuspendWindowLock() called.
  8080. // We should possibly do the same check as in LockWindow(),
  8081. // if it is ever possible that the consitions change between
  8082. // SuspendWindowLock() and ResumeWindowLock()
  8083. Enabled = !FDisabledOnLockSuspend;
  8084. }
  8085. }
  8086. }
  8087. //---------------------------------------------------------------------------
  8088. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8089. {
  8090. if (!TextOnly)
  8091. {
  8092. TTBXComboBoxItem * RemotePathComboBox =
  8093. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8094. TStrings * Items = RemotePathComboBox->Strings;
  8095. Items->BeginUpdate();
  8096. try
  8097. {
  8098. Items->Clear();
  8099. if (Terminal != NULL)
  8100. {
  8101. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8102. while (!IsUnixRootPath(APath))
  8103. {
  8104. int P = APath.LastDelimiter(L'/');
  8105. DebugAssert(P >= 0);
  8106. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8107. APath.SetLength(P - 1);
  8108. }
  8109. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8110. }
  8111. }
  8112. __finally
  8113. {
  8114. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8115. // Setting ItemIndex to -1 does not reset its text
  8116. if (Items->Count == 0)
  8117. {
  8118. RemotePathComboBox->Text = L"";
  8119. }
  8120. Items->EndUpdate();
  8121. }
  8122. }
  8123. }
  8124. //---------------------------------------------------------------------------
  8125. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8126. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8127. int & ImageIndex)
  8128. {
  8129. if (AIndex < 0)
  8130. {
  8131. AIndex = Sender->ItemIndex;
  8132. }
  8133. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8134. }
  8135. //---------------------------------------------------------------------------
  8136. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8137. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8138. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8139. {
  8140. ARect.Left += (10 * AIndex);
  8141. }
  8142. //---------------------------------------------------------------------------
  8143. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8144. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8145. {
  8146. AWidth += (10 * AIndex);
  8147. }
  8148. //---------------------------------------------------------------------------
  8149. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8150. TObject * Sender)
  8151. {
  8152. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8153. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8154. int Index = RemotePathComboBox->ItemIndex;
  8155. while (Index < RemotePathComboBox->Strings->Count - 1)
  8156. {
  8157. APath = UnixExtractFileDir(APath);
  8158. Index++;
  8159. }
  8160. // VanDyke style paths
  8161. if (APath.IsEmpty())
  8162. {
  8163. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8164. APath = ROOTDIRECTORY;
  8165. }
  8166. if (RemoteDirView->Path != APath)
  8167. {
  8168. RemoteDirView->Path = APath;
  8169. }
  8170. }
  8171. //---------------------------------------------------------------------------
  8172. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8173. {
  8174. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8175. DebugUsedParam(Sender);
  8176. UpdateRemotePathComboBox(true);
  8177. }
  8178. //---------------------------------------------------------------------------
  8179. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8180. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8181. {
  8182. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8183. DebugAssert(DirView != NULL);
  8184. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8185. {
  8186. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8187. // OnEditing is called also from TCustomListView::CanEdit
  8188. if (Edit != NULL)
  8189. {
  8190. EditSelectBaseName(Edit);
  8191. }
  8192. }
  8193. }
  8194. //---------------------------------------------------------------------------
  8195. //---------------------------------------------------------------------------
  8196. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8197. {
  8198. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8199. Result->AutoDetectDnD = false;
  8200. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8201. Result->RenderDataOn = rdoEnterAndDropSync;
  8202. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8203. return Result;
  8204. }
  8205. //---------------------------------------------------------------------------
  8206. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8207. {
  8208. TForm::CreateWnd();
  8209. if (FSessionsDragDropFilesEx == NULL)
  8210. {
  8211. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8212. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8213. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8214. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8215. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8216. }
  8217. if (FQueueDragDropFilesEx == NULL)
  8218. {
  8219. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8220. // No need to set OnDragOver as we do not have any restrictions
  8221. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8222. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8223. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8224. }
  8225. }
  8226. //---------------------------------------------------------------------------
  8227. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8228. {
  8229. TForm::DestroyWnd();
  8230. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8231. FQueueDragDropFilesEx->DragDropControl = NULL;
  8232. }
  8233. //---------------------------------------------------------------------------
  8234. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8235. {
  8236. SideEnter(FCurrentSide);
  8237. FEverShown = true;
  8238. }
  8239. //---------------------------------------------------------------------------
  8240. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8241. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8242. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8243. {
  8244. if (!NonVisualDataModule->Busy)
  8245. {
  8246. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8247. Configuration->Usage->Inc(L"FileFinds");
  8248. LockWindow(true);
  8249. NonVisualDataModule->StartBusy();
  8250. WinConfiguration->LockedInterface = true;
  8251. try
  8252. {
  8253. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8254. }
  8255. __finally
  8256. {
  8257. WinConfiguration->LockedInterface = false;
  8258. NonVisualDataModule->EndBusy();
  8259. UnlockWindow();
  8260. }
  8261. }
  8262. }
  8263. //---------------------------------------------------------------------------
  8264. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8265. {
  8266. if (!NonVisualDataModule->Busy)
  8267. {
  8268. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8269. SetFocus();
  8270. RemoteDirView->Path = UnixExtractFilePath(Path);
  8271. TListItem * Item = RemoteDirView->FindFileItem(UnixExtractFileName(Path));
  8272. if (Item != NULL)
  8273. {
  8274. RemoteDirView->ItemFocused = Item;
  8275. RemoteDirView->ItemFocused->MakeVisible(false);
  8276. RemoteDirView->SetFocus();
  8277. }
  8278. }
  8279. }
  8280. //---------------------------------------------------------------------------
  8281. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  8282. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8283. {
  8284. if (!NonVisualDataModule->Busy)
  8285. {
  8286. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8287. DebugAssert(FOnFileOperationFinished == NULL);
  8288. FOnFileOperationFinished = OnFileOperationFinished;
  8289. try
  8290. {
  8291. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  8292. }
  8293. __finally
  8294. {
  8295. FOnFileOperationFinished = NULL;
  8296. }
  8297. }
  8298. }
  8299. //---------------------------------------------------------------------------
  8300. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  8301. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8302. {
  8303. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  8304. }
  8305. //---------------------------------------------------------------------------
  8306. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  8307. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8308. {
  8309. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  8310. }
  8311. //---------------------------------------------------------------------------
  8312. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  8313. {
  8314. FFileFindTerminal = Terminal;
  8315. ShowFileFindDialog(Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles, DoDownloadFoundFiles);
  8316. }
  8317. //---------------------------------------------------------------------------
  8318. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  8319. {
  8320. FTaskbarList = TaskbarList;
  8321. }
  8322. //---------------------------------------------------------------------------
  8323. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  8324. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  8325. {
  8326. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8327. if (Index >= 0)
  8328. {
  8329. if (Button == mbLeft)
  8330. {
  8331. // "Mouse down" is raised only after tab is switched.
  8332. // If switching tab (switching session) takes long enough for user
  8333. // to actually release the button, "mouse down" is still raised,
  8334. // but we do not get "mouse up" event, so dragging is not cancelled,
  8335. // prevent that by not beginning dragging in the first place.
  8336. if (IsKeyPressed(VK_LBUTTON))
  8337. {
  8338. // when user clicks the "+", we get mouse down only after the session
  8339. // is closed, when new session tab is already on X:Y, so dragging
  8340. // starts, prevent that
  8341. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  8342. {
  8343. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8344. if (Terminal != NULL)
  8345. {
  8346. SessionsPageControl->BeginDrag(false);
  8347. }
  8348. }
  8349. }
  8350. }
  8351. else if (Button == mbMiddle)
  8352. {
  8353. // ignore middle-click for "New session tab"
  8354. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8355. if (Terminal != NULL)
  8356. {
  8357. SessionsPageControl->ActivePageIndex = Index;
  8358. // Switch to session tab (has to be session tab, due to previous check)
  8359. if (DebugAlwaysTrue(SessionTabSwitched()))
  8360. {
  8361. CloseSession();
  8362. }
  8363. }
  8364. }
  8365. }
  8366. }
  8367. //---------------------------------------------------------------------------
  8368. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  8369. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  8370. {
  8371. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8372. // do not allow dropping on the "+" tab
  8373. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8374. if ((TargetTerminal != NULL) &&
  8375. (SessionsPageControl->ActivePage->PageIndex != Index))
  8376. {
  8377. Configuration->Usage->Inc(L"SessionTabMoves");
  8378. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  8379. // but we want to actually prevent that to avoid flicker
  8380. SessionsPageControl->ActivePage->PageIndex = Index;
  8381. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  8382. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  8383. }
  8384. }
  8385. //---------------------------------------------------------------------------
  8386. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  8387. TObject * Sender, TObject * Source, int X, int Y,
  8388. TDragState /*State*/, bool & Accept)
  8389. {
  8390. Accept = (Sender == Source);
  8391. if (Accept)
  8392. {
  8393. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8394. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8395. // do not allow dragging to the "+" tab
  8396. Accept = (Terminal != NULL);
  8397. }
  8398. }
  8399. //---------------------------------------------------------------------------
  8400. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  8401. const TPoint & Point, int & Effect)
  8402. {
  8403. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8404. if (Index < 0)
  8405. {
  8406. Effect = DROPEFFECT_None;
  8407. }
  8408. else
  8409. {
  8410. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8411. // do not allow dropping on the "+" tab
  8412. if (TargetTerminal == NULL)
  8413. {
  8414. Effect = DROPEFFECT_None;
  8415. }
  8416. }
  8417. }
  8418. //---------------------------------------------------------------------------
  8419. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  8420. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  8421. {
  8422. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8423. // do not allow dropping on the "+" tab
  8424. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8425. if (TargetTerminal != NULL)
  8426. {
  8427. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  8428. if (!IsFileControl(DropSourceControl, osRemote))
  8429. {
  8430. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  8431. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  8432. // Why don't we use Terminal->CurrentDirectory directly?
  8433. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory, false);
  8434. }
  8435. }
  8436. }
  8437. //---------------------------------------------------------------------------
  8438. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  8439. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  8440. {
  8441. // Downloads are handled in RemoteFileControlDDEnd
  8442. if (!IsFileControl(DropSourceControl, osRemote))
  8443. {
  8444. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  8445. Terminal->CurrentDirectory,
  8446. // force queue
  8447. true);
  8448. }
  8449. }
  8450. //---------------------------------------------------------------------------
  8451. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  8452. {
  8453. FShowing = false;
  8454. // Do not save empty workspace
  8455. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  8456. {
  8457. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  8458. UnicodeString Name = WorkspaceName();
  8459. DoSaveWorkspace(
  8460. Name, DataList.get(),
  8461. !Configuration->DisablePasswordStoring &&
  8462. WinConfiguration->AutoSaveWorkspacePasswords);
  8463. WinConfiguration->LastStoredSession = Name;
  8464. }
  8465. }
  8466. //---------------------------------------------------------------------------
  8467. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  8468. {
  8469. TManagedTerminal * ManagedTerminal =
  8470. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  8471. if (ManagedTerminal != NULL)
  8472. {
  8473. ManagedTerminal->DirectoryLoaded = Now();
  8474. }
  8475. }
  8476. //---------------------------------------------------------------------------
  8477. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  8478. TListItem * /*Item*/, bool /*Selected*/)
  8479. {
  8480. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8481. switch (DirView->LastSelectMethod)
  8482. {
  8483. case smKeyboard:
  8484. Configuration->Usage->Inc(L"KeyboardSelections");
  8485. break;
  8486. case smMouse:
  8487. Configuration->Usage->Inc(L"MouseSelections");
  8488. break;
  8489. }
  8490. }
  8491. //---------------------------------------------------------------------------
  8492. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  8493. {
  8494. if (Color != 0)
  8495. {
  8496. AddSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  8497. return FSessionColors->Count - 1;
  8498. }
  8499. else
  8500. {
  8501. return FSessionTabImageIndex;
  8502. }
  8503. }
  8504. //---------------------------------------------------------------------------
  8505. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  8506. {
  8507. TPngImageCollectionItem * Item =
  8508. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  8509. FSessionColors->AddPng(Item->PngImage);
  8510. return FSessionColors->Count - 1;
  8511. }
  8512. //---------------------------------------------------------------------------
  8513. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  8514. {
  8515. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  8516. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  8517. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  8518. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  8519. }
  8520. //---------------------------------------------------------------------------
  8521. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  8522. {
  8523. for (int Index = 0; Index < Control->ControlCount; Index++)
  8524. {
  8525. TControl * AControl = Control->Controls[Index];
  8526. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  8527. if (WinControl != NULL)
  8528. {
  8529. CollectItemsWithTextDisplayMode(WinControl);
  8530. }
  8531. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  8532. if (Toolbar != NULL)
  8533. {
  8534. // we care for top-level items only
  8535. TTBCustomItem * Items = Toolbar->Items;
  8536. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  8537. {
  8538. TTBCustomItem * Item = Items->Items[ItemIndex];
  8539. if (((Item->DisplayMode == nbdmImageAndText) ||
  8540. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  8541. EligibleForImageDisplayMode(Item))
  8542. {
  8543. FItemsWithTextDisplayMode.insert(Item);
  8544. }
  8545. }
  8546. }
  8547. }
  8548. }
  8549. //---------------------------------------------------------------------------
  8550. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  8551. {
  8552. return true;
  8553. }
  8554. //---------------------------------------------------------------------------
  8555. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  8556. {
  8557. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  8558. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  8559. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  8560. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  8561. bool Result = true;
  8562. while (Result && (i != FItemsWithTextDisplayMode.end()))
  8563. {
  8564. TTBCustomItem * Item = *i;
  8565. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  8566. if (Label != NULL)
  8567. {
  8568. // optimization
  8569. if (Label->Visible == SelectiveToolbarText)
  8570. {
  8571. Result = false;
  8572. }
  8573. else
  8574. {
  8575. Label->Visible = SelectiveToolbarText;
  8576. }
  8577. }
  8578. else
  8579. {
  8580. // optimization
  8581. if (Item->DisplayMode == DisplayMode)
  8582. {
  8583. Result = false;
  8584. }
  8585. else
  8586. {
  8587. Item->DisplayMode = DisplayMode;
  8588. }
  8589. }
  8590. ++i;
  8591. }
  8592. if (Result)
  8593. {
  8594. UpdateNewSessionTab();
  8595. }
  8596. return Result;
  8597. }
  8598. //---------------------------------------------------------------------------
  8599. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  8600. {
  8601. DebugFail();
  8602. }
  8603. //---------------------------------------------------------------------------
  8604. bool __fastcall TCustomScpExplorerForm::IsBusy()
  8605. {
  8606. // Among other, a lock level is non zero, while directory is being loaded,
  8607. // even when it happens as a result of dirview navigation,
  8608. // i.e. when TNonVisualDataModule is NOT FBusy.
  8609. // That's why the TNonVisualDataModule::GetBusy calls this method.
  8610. // Among other this prevents a panel auto update to occur while
  8611. // directory is changing.
  8612. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  8613. }
  8614. //---------------------------------------------------------------------------
  8615. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  8616. {
  8617. // While the window is disabled, we still seem to process menu shortcuts at least,
  8618. // so stop it at least here.
  8619. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  8620. return
  8621. (Allowed == aaUpdate) ||
  8622. !NonVisualDataModule->Busy;
  8623. }
  8624. //---------------------------------------------------------------------------
  8625. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  8626. {
  8627. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  8628. }
  8629. //---------------------------------------------------------------------------
  8630. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  8631. {
  8632. // This is somewhat redundant to LockWindow() call from
  8633. // TTerminalManager::TerminalReadDirectoryProgress.
  8634. // But disabling window is known not to block keyboard shorcuts
  8635. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  8636. if (Busy > 0)
  8637. {
  8638. if (NonVisualDataModule->Busy)
  8639. {
  8640. State = false;
  8641. }
  8642. else
  8643. {
  8644. NonVisualDataModule->StartBusy();
  8645. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  8646. LockWindow();
  8647. }
  8648. }
  8649. else if (Busy < 0)
  8650. {
  8651. UnlockWindow();
  8652. NonVisualDataModule->EndBusy();
  8653. }
  8654. else
  8655. {
  8656. State = NonVisualDataModule->Busy;
  8657. }
  8658. }
  8659. //---------------------------------------------------------------------------
  8660. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  8661. {
  8662. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  8663. // no context menu for "New session tab"
  8664. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  8665. {
  8666. SessionsPageControl->ActivePageIndex = Index;
  8667. if (DebugAlwaysTrue(SessionTabSwitched()))
  8668. {
  8669. // copied from TControl.WMContextMenu
  8670. SendCancelMode(SessionsPageControl);
  8671. // explicit popup instead of using PopupMenu property
  8672. // to avoid menu to popup somewhere within SessionTabSwitched above,
  8673. // while connecting yet not-connected session and hence
  8674. // allowing an access to commands over not-completelly connected session
  8675. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  8676. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  8677. PopupMenu->PopupComponent = SessionsPageControl;
  8678. PopupMenu->Popup(Point.x, Point.y);
  8679. }
  8680. }
  8681. Handled = true;
  8682. }
  8683. //---------------------------------------------------------------------------
  8684. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  8685. {
  8686. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  8687. }
  8688. //---------------------------------------------------------------------------
  8689. void __fastcall TCustomScpExplorerForm::ChangePassword()
  8690. {
  8691. Configuration->Usage->Inc(L"PasswordChanges");
  8692. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  8693. ChangePasswordData->DisableAuthentationsExceptPassword();
  8694. ChangePasswordData->ChangePassword = true;
  8695. std::unique_ptr<TTerminal> ChangePasswordSession(
  8696. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  8697. ChangePasswordSession->AutoReadDirectory = false;
  8698. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  8699. {
  8700. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  8701. }
  8702. else
  8703. {
  8704. Configuration->Usage->Inc(L"PasswordChangesFailed");
  8705. }
  8706. }
  8707. //---------------------------------------------------------------------------
  8708. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  8709. {
  8710. TForm::ChangeScale(M, D);
  8711. int APixelsPerInch = GetControlPixelsPerInch(this);
  8712. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  8713. try
  8714. {
  8715. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  8716. }
  8717. __finally
  8718. {
  8719. GUIConfiguration->ChangeResourceModule(ResourceModule);
  8720. }
  8721. }