CustomScpExplorer.cpp 297 KB

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