CustomScpExplorer.cpp 374 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <Bookmarks.h>
  8. #include <Interface.h>
  9. #include <Exceptions.h>
  10. #include <CoreMain.h>
  11. #include <FileSystems.h>
  12. #include <TextsCore.h>
  13. #include <TextsWin.h>
  14. #include <HelpWin.h>
  15. #include <VCLCommon.h>
  16. #include <Progress.h>
  17. #include <SynchronizeProgress.h>
  18. #include <DragExt.h>
  19. #include <WinApi.h>
  20. #include "GUITools.h"
  21. #include "NonVisual.h"
  22. #include "Glyphs.h"
  23. #include "Tools.h"
  24. #include "WinConfiguration.h"
  25. #include "TerminalManager.h"
  26. #include "EditorManager.h"
  27. #include "ProgParams.h"
  28. #include "Setup.h"
  29. #include <Consts.hpp>
  30. #include <DateUtils.hpp>
  31. #include <TB2Common.hpp>
  32. #include <DirectoryMonitor.hpp>
  33. #include <System.IOUtils.hpp>
  34. //---------------------------------------------------------------------------
  35. #pragma package(smart_init)
  36. #pragma link "CustomDirView"
  37. #pragma link "CustomUnixDirView"
  38. #pragma link "IEListView"
  39. #pragma link "NortonLikeListView"
  40. #pragma link "UnixDirView"
  41. #pragma link "CustomDriveView"
  42. #pragma link "UnixDriveView"
  43. #pragma link "CustomDriveView"
  44. #pragma link "TB2Dock"
  45. #pragma link "TBXStatusBars"
  46. #pragma link "TB2Item"
  47. #pragma link "TB2Toolbar"
  48. #pragma link "ThemePageControl"
  49. #pragma link "PathLabel"
  50. #pragma resource "*.dfm"
  51. //---------------------------------------------------------------------------
  52. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  53. static const int SessionPanelCount = 3;
  54. //---------------------------------------------------------------------------
  55. class TMutexGuard
  56. {
  57. public:
  58. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  59. int Timeout = 5000)
  60. {
  61. FMutex = NULL;
  62. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  63. if (WaitResult == WAIT_TIMEOUT)
  64. {
  65. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  66. }
  67. else
  68. {
  69. FMutex = AMutex;
  70. }
  71. }
  72. ~TMutexGuard()
  73. {
  74. if (FMutex != NULL)
  75. {
  76. ReleaseMutex(FMutex);
  77. }
  78. }
  79. private:
  80. HANDLE FMutex;
  81. };
  82. //---------------------------------------------------------------------------
  83. class TWindowLock
  84. {
  85. public:
  86. TWindowLock(TCustomScpExplorerForm * Form) :
  87. FForm(Form)
  88. {
  89. FForm->LockWindow();
  90. }
  91. ~TWindowLock()
  92. {
  93. FForm->UnlockWindow();
  94. }
  95. private:
  96. TCustomScpExplorerForm * FForm;
  97. };
  98. //---------------------------------------------------------------------------
  99. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  100. FForm(Form)
  101. {
  102. FForm->BatchStart(FBatchStorage);
  103. }
  104. //---------------------------------------------------------------------------
  105. TAutoBatch::~TAutoBatch()
  106. {
  107. FForm->BatchEnd(FBatchStorage);
  108. }
  109. //---------------------------------------------------------------------------
  110. struct TTransferOperationParam
  111. {
  112. TTransferOperationParam();
  113. UnicodeString TargetDirectory;
  114. bool Temp;
  115. bool DragDrop;
  116. int Options;
  117. TAutoSwitch Queue;
  118. };
  119. //---------------------------------------------------------------------------
  120. TTransferOperationParam::TTransferOperationParam()
  121. {
  122. Temp = false;
  123. DragDrop = false;
  124. Options = 0;
  125. Queue = asAuto;
  126. }
  127. //---------------------------------------------------------------------------
  128. class TTransferPresetNoteData : public TObject
  129. {
  130. public:
  131. UnicodeString Message;
  132. };
  133. //---------------------------------------------------------------------------
  134. class TTerminalNoteData : public TObject
  135. {
  136. public:
  137. TManagedTerminal * Terminal;
  138. };
  139. //---------------------------------------------------------------------------
  140. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  141. FFormRestored(false),
  142. TForm(Owner)
  143. {
  144. FInvalid = false;
  145. FCurrentSide = osRemote;
  146. FEverShown = false;
  147. FDocks = new TList();
  148. InitControls();
  149. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  150. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  151. NonVisualDataModule->ScpExplorer = this;
  152. RestoreParams();
  153. ConfigurationChanged();
  154. RemoteDirView->Invalidate();
  155. FAutoOperation = false;
  156. FOnFileOperationFinished = NULL;
  157. FForceExecution = false;
  158. FIgnoreNextDialogChar = 0;
  159. FErrorList = NULL;
  160. FSynchronizeProgressForm = NULL;
  161. FProgressForm = NULL;
  162. FRefreshLocalDirectory = false;
  163. FRefreshRemoteDirectory = false;
  164. FDDExtMapFile = NULL;
  165. // CreateMutexW keeps failing with ERROR_NOACCESS
  166. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  167. DebugAssert(FDDExtMutex != NULL);
  168. FDDTargetControl = NULL;
  169. FDelayedDeletionTimer = NULL;
  170. FDelayedDeletionList = new TStringList();
  171. FDDFileList = NULL;
  172. FPendingTempSpaceWarn = false;
  173. FCapturedLog = NULL;
  174. FDragDropOperation = false;
  175. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  176. FAllowTransferPresetAutoSelect = true;
  177. FSessionChanging = false;
  178. FCopyParamDefault = L"";
  179. FSynchronizeController = NULL;
  180. FPendingQueueActionItem = NULL;
  181. FLockLevel = 0;
  182. FLockSuspendLevel = 0;
  183. FDisabledOnLockSuspend = false;
  184. FAlternativeDelete = false;
  185. FTrayIcon = new ::TTrayIcon(0);
  186. FTrayIcon->OnClick = TrayIconClick;
  187. FMaxQueueLength = 0;
  188. FLastContextPopupScreenPoint = TPoint(-1, -1);
  189. FTransferResumeList = NULL;
  190. FMoveToQueue = false;
  191. FStandaloneEditing = false;
  192. FOnFeedSynchronizeError = NULL;
  193. FNeedSession = false;
  194. FDoNotIdleCurrentTerminal = 0;
  195. FIncrementalSearching = 0;
  196. FIncrementalSearchHaveNext = false;
  197. FQueueFileList.reset(new TQueueFileList());
  198. FProgressSide = osCurrent;
  199. FEditorManager = new TEditorManager();
  200. FEditorManager->OnFileChange = ExecutedFileChanged;
  201. FEditorManager->OnFileReload = ExecutedFileReload;
  202. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  203. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  204. FLocalEditors = new TList();
  205. FQueueStatus = NULL;
  206. FQueueStatusSection = new TCriticalSection();
  207. FQueueStatusInvalidated = false;
  208. FQueueItemInvalidated = false;
  209. FQueueStatusUpdating = false;
  210. FQueueActedItem = NULL;
  211. FQueueController = new TQueueController(QueueView3);
  212. FUserActionTimer = new TTimer(this);
  213. FUserActionTimer->Enabled = false;
  214. FUserActionTimer->Interval = 10;
  215. FUserActionTimer->OnTimer = UserActionTimer;
  216. FNotes = new TStringList();
  217. FNoteTimer = new TTimer(this);
  218. FNoteTimer->Enabled = false;
  219. FNoteTimer->OnTimer = NoteTimer;
  220. FOnNoteClick = NULL;
  221. FOle32Library = LoadLibrary(L"Ole32.dll");
  222. FDragMoveCursor = FOle32Library != NULL ?
  223. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  224. UseSystemSettings(this);
  225. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  226. static_cast<TObject*>(GetComponent(fcTransferList)));
  227. DebugAssert(TransferList != NULL);
  228. FTransferListHoverIndex = -1;
  229. TransferList->OnChange = TransferListChange;
  230. TransferList->OnDrawItem = TransferListDrawItem;
  231. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  232. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  233. UseDesktopFont(SessionsPageControl);
  234. UpdateSessionsPageControlHeight();
  235. UseDesktopFont(RemoteDirView);
  236. UseDesktopFont(RemoteDriveView);
  237. UseDesktopFont(QueueView3);
  238. UseDesktopFont(QueueFileList);
  239. UseDesktopFont(QueueLabel);
  240. UseDesktopFont(RemoteStatusBar);
  241. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  242. QueueSplitter->ShowHint = true;
  243. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  244. QueueFileListSplitter->ShowHint = true;
  245. RemotePanelSplitter->ShowHint = true;
  246. UpdateImages();
  247. FCustomCommandMenu = CreateTBXPopupMenu(this);
  248. FCustomCommandLocalFileList = NULL;
  249. FCustomCommandRemoteFileList = NULL;
  250. FSessionColors = new TPngImageList(this);
  251. FSessionColors->ColorDepth = cd32Bit;
  252. AddFixedSessionImages();
  253. SessionsPageControl->Images = FSessionColors;
  254. UpdateQueueLabel();
  255. CreateHiddenWindow();
  256. StartUpdates();
  257. }
  258. //---------------------------------------------------------------------------
  259. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  260. {
  261. FInvalid = true;
  262. // this has to be one of the very first things to do
  263. StopUpdateThread();
  264. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  265. TTerminalManager::Instance()->ScpExplorer = NULL;
  266. delete FCustomCommandLocalFileList;
  267. delete FCustomCommandRemoteFileList;
  268. delete FCustomCommandMenu;
  269. delete FTrayIcon;
  270. FTrayIcon = NULL;
  271. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  272. delete FEditorManager;
  273. ForceCloseLocalEditors();
  274. delete FLocalEditors;
  275. HideFileFindDialog();
  276. if (FDelayedDeletionTimer)
  277. {
  278. DoDelayedDeletion(NULL);
  279. SAFE_DESTROY(FDelayedDeletionTimer);
  280. }
  281. SAFE_DESTROY(FDelayedDeletionList);
  282. // sometimes we do not get DDEnd so the list is not released
  283. SAFE_DESTROY(FDDFileList);
  284. DebugAssert(FSynchronizeController == NULL);
  285. CloseHandle(FDDExtMutex);
  286. FDDExtMutex = NULL;
  287. FreeLibrary(FOle32Library);
  288. FOle32Library = NULL;
  289. FDragMoveCursor = NULL;
  290. DebugAssert(!FErrorList);
  291. if (FEverShown)
  292. {
  293. // when window is never shown (like when running command-line operation),
  294. // particularly window site is not restored correctly (BoundsRect value set
  295. // in RestoreForm gets lost during handle allocation), so we do not want
  296. // it to be stored
  297. StoreParams();
  298. }
  299. ManagedSession = NULL;
  300. Queue = NULL;
  301. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  302. NonVisualDataModule->ScpExplorer = NULL;
  303. delete FQueueController;
  304. FQueueController = NULL;
  305. delete FQueueStatusSection;
  306. FQueueStatusSection = NULL;
  307. delete FQueueStatus;
  308. FQueueStatus = NULL;
  309. delete FUserActionTimer;
  310. FUserActionTimer = NULL;
  311. delete FNoteTimer;
  312. delete FNotes;
  313. delete FNoteData;
  314. SAFE_DESTROY(FDocks);
  315. SessionsPageControl->Images = NULL;
  316. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  317. SAFE_DESTROY(FSessionColors);
  318. SAFE_DESTROY(FSessionsDragDropFilesEx);
  319. SAFE_DESTROY(FHistoryMenu[0][0]);
  320. SAFE_DESTROY(FHistoryMenu[0][1]);
  321. SAFE_DESTROY(FHistoryMenu[1][0]);
  322. SAFE_DESTROY(FHistoryMenu[1][1]);
  323. if (FHiddenWindow != NULL)
  324. {
  325. DestroyWindow(FHiddenWindow);
  326. FHiddenWindow = NULL;
  327. }
  328. }
  329. //---------------------------------------------------------------------------
  330. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  331. {
  332. std::unique_ptr<TSessionData> Data;
  333. if (!Session.IsEmpty())
  334. {
  335. bool DefaultsOnly;
  336. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  337. }
  338. TTerminalManager * Manager = TTerminalManager::Instance();
  339. for (int Index = 0; Index < Manager->Count; Index++)
  340. {
  341. TTerminal * Terminal = Manager->Sessions[Index];
  342. if (Session.IsEmpty() ||
  343. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  344. {
  345. if (Path.IsEmpty())
  346. {
  347. Terminal->ClearCaches();
  348. }
  349. else
  350. {
  351. Terminal->DirectoryModified(Path, true);
  352. }
  353. }
  354. }
  355. // We should flag a pending refresh for the background terminals or busy foreground terminals
  356. if (HasActiveTerminal() &&
  357. CanCommandLineFromAnotherInstance() &&
  358. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  359. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  360. {
  361. Terminal->ReloadDirectory();
  362. }
  363. }
  364. //---------------------------------------------------------------------------
  365. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  366. {
  367. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  368. size_t MessageSize = sizeof(TCopyDataMessage);
  369. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  370. if (Result)
  371. {
  372. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  373. Result = (Message.Version == TCopyDataMessage::Version1);
  374. if (Result)
  375. {
  376. switch (Message.Command)
  377. {
  378. case TCopyDataMessage::CommandCanCommandLine:
  379. Result = CanCommandLineFromAnotherInstance();
  380. break;
  381. case TCopyDataMessage::CommandCommandLine:
  382. {
  383. UnicodeString CommandLine(Message.CommandLine);
  384. Result = CommandLineFromAnotherInstance(CommandLine);
  385. }
  386. break;
  387. case TCopyDataMessage::RefreshPanel:
  388. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  389. break;
  390. case TCopyDataMessage::MainWindowCheck:
  391. Result = true;
  392. break;
  393. default:
  394. Result = false;
  395. break;
  396. }
  397. }
  398. }
  399. Message.Result = Result ? 1 : 0;
  400. }
  401. //---------------------------------------------------------------------------
  402. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  403. {
  404. WNDCLASS WindowClass = {0};
  405. WindowClass.lpfnWndProc = DefWindowProc;
  406. WindowClass.hInstance = HInstance;
  407. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  408. FHiddenWindow = NULL;
  409. if (RegisterClass(&WindowClass))
  410. {
  411. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  412. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  413. }
  414. }
  415. //---------------------------------------------------------------------------
  416. bool __fastcall TCustomScpExplorerForm::CanConsole()
  417. {
  418. return
  419. HasActiveTerminal() &&
  420. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  421. }
  422. //---------------------------------------------------------------------------
  423. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  424. {
  425. bool Result = Visible && !NonVisualDataModule->Busy;
  426. return Result;
  427. }
  428. //---------------------------------------------------------------------------
  429. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  430. const UnicodeString & CommandLine)
  431. {
  432. TProgramParams Params(CommandLine);
  433. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  434. if (Result)
  435. {
  436. NonVisualDataModule->StartBusy();
  437. try
  438. {
  439. // this action is initiated from another process,
  440. // so it's likely that our window is not visible,
  441. // and user won't see what is going on
  442. Application->BringToFront();
  443. // reload sessions as we may be asked to open a session
  444. // just stored by another instance
  445. StoredSessions->Reload();
  446. UnicodeString SessionName = Params.Param[1];
  447. std::unique_ptr<TObjectList> DataList(new TObjectList());
  448. UnicodeString DownloadFile; // unused
  449. try
  450. {
  451. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  452. }
  453. catch (EAbort &)
  454. {
  455. // Happens when opening session in PuTTY and in other situations,
  456. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  457. // to process the command-line on its own (or to pass it to yet another instance)
  458. }
  459. if (DataList->Count > 0)
  460. {
  461. TTerminalManager * Manager = TTerminalManager::Instance();
  462. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  463. }
  464. }
  465. __finally
  466. {
  467. NonVisualDataModule->EndBusy();
  468. }
  469. }
  470. return Result;
  471. }
  472. //---------------------------------------------------------------------------
  473. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  474. {
  475. TManagedTerminal * Result;
  476. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  477. {
  478. Result = FManagedSession;
  479. }
  480. else
  481. {
  482. Result = NULL;
  483. }
  484. return Result;
  485. }
  486. //---------------------------------------------------------------------------
  487. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  488. {
  489. if (FManagedSession != value)
  490. {
  491. SessionChanging();
  492. DoSetManagedSession(value, false);
  493. }
  494. }
  495. //---------------------------------------------------------------------------
  496. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  497. {
  498. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  499. FManagedSession = value;
  500. {
  501. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  502. FAllowTransferPresetAutoSelect = false;
  503. TAutoFlag SessionChangingFlag(FSessionChanging);
  504. SessionChanged(Replace);
  505. }
  506. if (Terminal != NULL)
  507. {
  508. TransferPresetAutoSelect();
  509. }
  510. // Update app and tab titles, prevented above by FSessionChanging.
  511. UpdateControls();
  512. }
  513. //---------------------------------------------------------------------------
  514. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  515. {
  516. DoSetManagedSession(value, true);
  517. }
  518. //---------------------------------------------------------------------------
  519. void __fastcall TCustomScpExplorerForm::SessionChanging()
  520. {
  521. if (ManagedSession != NULL)
  522. {
  523. UpdateSession(ManagedSession);
  524. }
  525. }
  526. //---------------------------------------------------------------------------
  527. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  528. {
  529. if (Terminal != NULL)
  530. {
  531. UpdateSessionColor((TColor)Terminal->StateData->Color);
  532. }
  533. if (!TTerminalManager::Instance()->Updating)
  534. {
  535. SessionListChanged();
  536. }
  537. if (Terminal != NULL)
  538. {
  539. if (Replaced)
  540. {
  541. RemoteDirView->ReplaceTerminal(Terminal);
  542. }
  543. else
  544. {
  545. RemoteDirView->Terminal = Terminal;
  546. }
  547. if (Terminal->Active)
  548. {
  549. Terminal->RefreshDirectory();
  550. }
  551. if (WinConfiguration->PreservePanelState)
  552. {
  553. DirView(osRemote)->RestoreState(Terminal->RemoteExplorerState);
  554. }
  555. }
  556. else
  557. {
  558. RemoteDirView->Terminal = NULL;
  559. }
  560. InitStatusBar();
  561. UpdateTransferList();
  562. // Update panels Enable state before refreshing the labels
  563. UpdateControls();
  564. }
  565. //---------------------------------------------------------------------------
  566. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  567. {
  568. if (Queue != value)
  569. {
  570. if (FQueue != NULL)
  571. {
  572. FQueue->OnListUpdate = NULL;
  573. FQueue->OnQueueItemUpdate = NULL;
  574. }
  575. FQueue = value;
  576. if (FQueue != NULL)
  577. {
  578. DebugAssert(FQueue->OnListUpdate == NULL);
  579. FQueue->OnListUpdate = QueueListUpdate;
  580. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  581. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  582. }
  583. QueueChanged();
  584. }
  585. }
  586. //---------------------------------------------------------------------------
  587. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  588. TListItem * Item)
  589. {
  590. if (FQueueActedItem == Item)
  591. {
  592. FQueueActedItem = NULL;
  593. if ((QueueView3->PopupMenu != NULL) &&
  594. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  595. {
  596. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  597. ReleaseCapture();
  598. }
  599. }
  600. if (Item->Data == FPendingQueueActionItem)
  601. {
  602. FPendingQueueActionItem = NULL;
  603. }
  604. }
  605. //---------------------------------------------------------------------------
  606. bool TCustomScpExplorerForm::IsAnythingQueued()
  607. {
  608. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  609. }
  610. //---------------------------------------------------------------------------
  611. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  612. {
  613. {
  614. TGuard Guard(FQueueStatusSection);
  615. FQueueStatusInvalidated = false;
  616. if (FQueue != NULL)
  617. {
  618. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  619. }
  620. }
  621. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  622. {
  623. FMaxQueueLength = FQueueStatus->Count;
  624. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  625. }
  626. {
  627. TAutoFlag Flag(FQueueStatusUpdating);
  628. FQueueController->UpdateQueueStatus(FQueueStatus);
  629. }
  630. UpdateQueueFileList();
  631. SetQueueProgress();
  632. UpdateQueueView();
  633. bool IsEmpty = !IsAnythingQueued();
  634. if (IsEmpty && (Terminal != NULL))
  635. {
  636. TOnceDoneOperation OnceDoneOperation =
  637. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  638. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  639. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  640. {
  641. FQueue->Enabled = false;
  642. }
  643. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  644. {
  645. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  646. }
  647. }
  648. }
  649. //---------------------------------------------------------------------------
  650. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  651. {
  652. UnicodeString Result;
  653. if (FQueueStatus != NULL)
  654. {
  655. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  656. {
  657. TFileOperationProgressType * ProgressData =
  658. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  659. if ((ProgressData != NULL) && ProgressData->InProgress)
  660. {
  661. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  662. }
  663. }
  664. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  665. {
  666. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  667. }
  668. }
  669. return Result;
  670. }
  671. //---------------------------------------------------------------------------
  672. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  673. {
  674. ComponentVisible[fcQueueView] =
  675. !IsLocalBrowserMode() &&
  676. ((WinConfiguration->QueueView.Show == qvShow) ||
  677. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  678. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  679. }
  680. //---------------------------------------------------------------------------
  681. void __fastcall TCustomScpExplorerForm::QueueChanged()
  682. {
  683. if (FQueueStatus != NULL)
  684. {
  685. // UpdateFileList implementation relies on the status being recreated when session changes
  686. delete FQueueStatus;
  687. FQueueStatus = NULL;
  688. }
  689. UpdateQueueStatus(true);
  690. }
  691. //---------------------------------------------------------------------------
  692. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  693. {
  694. if (FQueue == Queue)
  695. {
  696. FQueueStatusInvalidated = true;
  697. }
  698. }
  699. //---------------------------------------------------------------------------
  700. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  701. TQueueItem * Item)
  702. {
  703. if (FQueue == Queue)
  704. {
  705. TGuard Guard(FQueueStatusSection);
  706. // this may be running in parallel with QueueChanged
  707. if (FQueueStatus != NULL)
  708. {
  709. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  710. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  711. {
  712. FRefreshLocalDirectory = (QueueItem == NULL) ||
  713. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  714. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  715. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  716. }
  717. if (QueueItem != NULL)
  718. {
  719. QueueItem->UserData = (void*)true;
  720. FQueueItemInvalidated = true;
  721. }
  722. }
  723. }
  724. }
  725. //---------------------------------------------------------------------------
  726. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  727. {
  728. // during standalone editing, we have no way to see/control queue,
  729. // so we have to always popup prompts automatically
  730. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  731. }
  732. //---------------------------------------------------------------------------
  733. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  734. {
  735. if (FQueueStatus != NULL)
  736. {
  737. bool QueueAutoPopup = IsQueueAutoPopup();
  738. bool NeedRefresh = FQueueController->NeedRefresh();
  739. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  740. FQueueItemInvalidated = false;
  741. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  742. bool Updated = false;
  743. TQueueItemProxy * QueueItem;
  744. bool WasUserAction;
  745. for (int Index = 0; Index < Limit; Index++)
  746. {
  747. bool Update = false;
  748. QueueItem = FQueueStatus->Items[Index];
  749. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  750. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  751. (FPendingQueueActionItem == NULL))
  752. {
  753. FPendingQueueActionItem = QueueItem;
  754. FUserActionTimer->Enabled = true;
  755. }
  756. if ((bool)QueueItem->UserData)
  757. {
  758. QueueItem->UserData = (void*)false;
  759. QueueItem->Update();
  760. Updated = true;
  761. Update = true;
  762. }
  763. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  764. {
  765. Update = true;
  766. }
  767. if (Update)
  768. {
  769. FQueueController->RefreshQueueItem(QueueItem);
  770. }
  771. }
  772. if (Updated)
  773. {
  774. NonVisualDataModule->UpdateNonVisibleActions();
  775. SetQueueProgress();
  776. UpdateQueueFileList();
  777. }
  778. }
  779. }
  780. //---------------------------------------------------------------------------
  781. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  782. {
  783. if (FTaskbarList != NULL)
  784. {
  785. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  786. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  787. }
  788. }
  789. //---------------------------------------------------------------------------
  790. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  791. {
  792. if (Progress >= 0)
  793. {
  794. if (FTaskbarList != NULL)
  795. {
  796. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  797. }
  798. }
  799. else
  800. {
  801. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  802. }
  803. }
  804. //---------------------------------------------------------------------------
  805. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  806. {
  807. int OverallProgress;
  808. // FProgressForm is null when this is called from SetQueueProgress
  809. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  810. {
  811. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  812. }
  813. else if (!TFileOperationProgressType::IsIndeterminateOperation(ProgressData->Operation))
  814. {
  815. OverallProgress = ProgressData->OverallProgress();
  816. }
  817. else
  818. {
  819. OverallProgress = -1;
  820. }
  821. SetTaskbarListProgressValue(OverallProgress);
  822. }
  823. //---------------------------------------------------------------------------
  824. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  825. {
  826. TTerminalManager::Instance()->QueueStatusUpdated();
  827. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  828. {
  829. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  830. {
  831. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  832. {
  833. TFileOperationProgressType * ProgressData = NULL;
  834. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  835. {
  836. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  837. }
  838. if ((ProgressData != NULL) &&
  839. ProgressData->InProgress)
  840. {
  841. SetTaskbarListProgressValue(ProgressData);
  842. }
  843. else
  844. {
  845. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  846. }
  847. }
  848. else
  849. {
  850. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  851. }
  852. }
  853. else
  854. {
  855. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  856. }
  857. }
  858. UpdateQueueLabel();
  859. }
  860. //---------------------------------------------------------------------------
  861. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  862. {
  863. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  864. if (FQueueStatus != NULL)
  865. {
  866. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  867. if (ActiveAndPendingPrimaryCount > 0)
  868. {
  869. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  870. }
  871. }
  872. QueueLabel->Caption = Caption;
  873. }
  874. //---------------------------------------------------------------------------
  875. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  876. {
  877. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  878. static_cast<TComponent*>(GetComponent(fcTransferList)));
  879. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  880. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  881. TransferList->Strings->BeginUpdate();
  882. try
  883. {
  884. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  885. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  886. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  887. if (FTransferDropDownHint.IsEmpty())
  888. {
  889. FTransferDropDownHint = TransferDropDown->Hint;
  890. }
  891. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  892. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  893. TransferDropDown->Text = StripHotkey(Name);
  894. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  895. (FTransferDropDownHint, StripHotkey(Name),
  896. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  897. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  898. // update the label, otherwise when it is updated only on the first draw
  899. // of the list, it is drawn "bold" for some reason
  900. FTransferListHoverIndex = TransferList->ItemIndex;
  901. UpdateTransferLabel();
  902. }
  903. __finally
  904. {
  905. TransferList->Strings->EndUpdate();
  906. }
  907. }
  908. //---------------------------------------------------------------------------
  909. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  910. {
  911. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  912. DebugAssert(Toolbar != NULL);
  913. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  914. }
  915. //---------------------------------------------------------------------------
  916. void __fastcall TCustomScpExplorerForm::UpdateActions()
  917. {
  918. TForm::UpdateActions();
  919. if (ComponentVisible[fcCustomCommandsBand])
  920. {
  921. UpdateCustomCommandsToolbar();
  922. }
  923. }
  924. //---------------------------------------------------------------------------
  925. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  926. {
  927. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  928. }
  929. //---------------------------------------------------------------------------
  930. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  931. {
  932. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  933. {
  934. DirView->RowSelect = WinConfiguration->FullRowSelect;
  935. // selection is not redrawn automatically when RowSelect changes
  936. DirView->Invalidate();
  937. }
  938. }
  939. //---------------------------------------------------------------------------
  940. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  941. {
  942. Color = GetBtnFaceColor();
  943. DebugAssert(Configuration && RemoteDirView);
  944. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  945. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  946. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  947. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  948. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  949. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  950. UpdateRowSelect(RemoteDirView);
  951. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  952. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  953. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  954. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  955. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  956. UpdateSessionsPageControlHeight();
  957. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  958. UpdateToolbarDisplayMode();
  959. UpdateTransferList();
  960. if (Terminal != NULL)
  961. {
  962. TransferPresetAutoSelect();
  963. }
  964. UpdateQueueView();
  965. UpdateControls();
  966. // this can be called even before constuctor finishes.
  967. if (FEditorManager != NULL)
  968. {
  969. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  970. }
  971. // this can be called even before constuctor finishes.
  972. if (FLocalEditors != NULL)
  973. {
  974. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  975. {
  976. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  977. }
  978. }
  979. if (ComponentVisible[fcCustomCommandsBand])
  980. {
  981. UpdateCustomCommandsToolbar();
  982. }
  983. // show only when keeping queue items forever,
  984. // otherwise, its enough to have in in the context menu
  985. QueueDeleteAllDoneQueueToolbarItem->Visible =
  986. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  987. if (FFileColorsCurrent != WinConfiguration->FileColors)
  988. {
  989. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  990. FFileColorsCurrent = WinConfiguration->FileColors;
  991. FileColorsChanged();
  992. }
  993. }
  994. //---------------------------------------------------------------------------
  995. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  996. {
  997. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  998. DirView->Invalidate();
  999. }
  1000. //---------------------------------------------------------------------------
  1001. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1002. {
  1003. DoFileColorsChanged(RemoteDirView);
  1004. }
  1005. //---------------------------------------------------------------------------
  1006. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1007. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1008. void * /*Arg*/)
  1009. {
  1010. if (Token != NULL)
  1011. {
  1012. TForm * Editor = dynamic_cast<TForm*>(Token);
  1013. DebugAssert(Editor != NULL);
  1014. ReconfigureEditorForm(Editor);
  1015. }
  1016. }
  1017. //---------------------------------------------------------------------------
  1018. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1019. {
  1020. WinConfiguration->DDTransferConfirmation = asOn;
  1021. }
  1022. //---------------------------------------------------------------------------
  1023. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1024. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1025. {
  1026. // noop
  1027. }
  1028. //---------------------------------------------------------------------------
  1029. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1030. {
  1031. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1032. }
  1033. //---------------------------------------------------------------------------
  1034. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1035. {
  1036. if (DoNotShowAgain)
  1037. {
  1038. if (DragDrop)
  1039. {
  1040. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1041. {
  1042. PopupTrayBalloon(
  1043. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1044. }
  1045. WinConfiguration->DDTransferConfirmation = asOff;
  1046. }
  1047. else
  1048. {
  1049. WinConfiguration->ConfirmTransferring = false;
  1050. }
  1051. }
  1052. else
  1053. {
  1054. // User explicitly unchecked "do not show again",
  1055. // so show him the dialog the next time
  1056. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1057. {
  1058. WinConfiguration->DDTransferConfirmation = asOn;
  1059. }
  1060. }
  1061. }
  1062. //---------------------------------------------------------------------------
  1063. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1064. TTransferDirection Direction, TTransferType Type, bool Temp,
  1065. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1066. bool Confirm, bool DragDrop, int Options)
  1067. {
  1068. bool Result = true;
  1069. DebugAssert(HasActiveTerminal());
  1070. // these parameters are known in advance
  1071. int Params =
  1072. FLAGMASK(Type == ttMove, cpDelete);
  1073. bool ToTemp = (Temp && (Direction == tdToLocal));
  1074. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1075. {
  1076. bool DisableNewerOnly =
  1077. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1078. ToTemp;
  1079. Options |=
  1080. FLAGMASK(ToTemp, coTemp) |
  1081. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1082. coDoNotShowAgain;
  1083. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1084. int CopyParamAttrs = (Direction == tdToRemote ?
  1085. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1086. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1087. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1088. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1089. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1090. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1091. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1092. if (Result)
  1093. {
  1094. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1095. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1096. }
  1097. }
  1098. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1099. {
  1100. Configuration->Usage->Inc(L"TransfersOnBackground");
  1101. // these parameter are known only after transfer dialog
  1102. Params |=
  1103. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1104. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1105. Result = false;
  1106. ClearTransferSourceSelection(Direction);
  1107. }
  1108. return Result;
  1109. }
  1110. //---------------------------------------------------------------------------
  1111. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1112. {
  1113. if (FOnFileOperationFinished != NULL)
  1114. {
  1115. FOnFileOperationFinished(UnicodeString(), true);
  1116. }
  1117. else
  1118. {
  1119. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1120. if (HasDirView[Side])
  1121. {
  1122. DirView(Side)->SelectAll(smNone);
  1123. }
  1124. }
  1125. }
  1126. //---------------------------------------------------------------------------
  1127. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1128. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1129. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1130. int Params)
  1131. {
  1132. DebugAssert(Queue != NULL);
  1133. bool SingleFile = false;
  1134. if (FileList->Count == 1)
  1135. {
  1136. if (Direction == tdToRemote)
  1137. {
  1138. UnicodeString FileName = FileList->Strings[0];
  1139. SingleFile = FileExists(ApiPath(FileName));
  1140. }
  1141. else
  1142. {
  1143. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1144. SingleFile = !File->IsDirectory;
  1145. }
  1146. }
  1147. TQueueItem * QueueItem;
  1148. if (Direction == tdToRemote)
  1149. {
  1150. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1151. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1152. }
  1153. else
  1154. {
  1155. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1156. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1157. }
  1158. AddQueueItem(Queue, QueueItem, Terminal);
  1159. }
  1160. //---------------------------------------------------------------------------
  1161. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1162. {
  1163. if (Queue->IsEmpty)
  1164. {
  1165. Terminal->QueueOperationStart = Now();
  1166. }
  1167. Queue->AddItem(QueueItem);
  1168. }
  1169. //---------------------------------------------------------------------------
  1170. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1171. {
  1172. }
  1173. //---------------------------------------------------------------------------
  1174. void __fastcall TCustomScpExplorerForm::InitControls()
  1175. {
  1176. DebugAssert(FDocks != NULL);
  1177. for (int Index = 0; Index < ComponentCount; Index++)
  1178. {
  1179. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1180. if ((Dock != NULL) && (Dock->Tag == 0))
  1181. {
  1182. FDocks->Add(Dock);
  1183. }
  1184. }
  1185. CollectItemsWithTextDisplayMode(this);
  1186. }
  1187. //---------------------------------------------------------------------------
  1188. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1189. {
  1190. ConfigurationChanged();
  1191. QueuePanel->Height =
  1192. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1193. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1194. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1195. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1196. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1197. QueueFileList->Height =
  1198. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1199. if (QueueFileList->Visible)
  1200. {
  1201. AdjustQueueLayout();
  1202. }
  1203. UpdateCustomCommandsToolbar();
  1204. }
  1205. //---------------------------------------------------------------------------
  1206. void __fastcall TCustomScpExplorerForm::StoreParams()
  1207. {
  1208. int APixelsPerInch = GetControlPixelsPerInch(this);
  1209. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1210. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1211. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1212. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1213. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1214. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1215. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1216. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1217. }
  1218. //---------------------------------------------------------------------------
  1219. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1220. {
  1221. if (!FFormRestored)
  1222. {
  1223. FFormRestored = true;
  1224. RestoreFormParams();
  1225. }
  1226. TForm::CreateParams(Params);
  1227. }
  1228. //---------------------------------------------------------------------------
  1229. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1230. {
  1231. DebugAssert(FDocks != NULL);
  1232. for (int Index = 0; Index < FDocks->Count; Index++)
  1233. {
  1234. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1235. }
  1236. }
  1237. //---------------------------------------------------------------------------
  1238. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1239. {
  1240. SetDockAllowDrag(true);
  1241. while (!ButtonsStr.IsEmpty())
  1242. {
  1243. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1244. UnicodeString Name = CutToChar(S, L'=', true);
  1245. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1246. {
  1247. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1248. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1249. {
  1250. while (!S.IsEmpty())
  1251. {
  1252. UnicodeString S2 = CutToChar(S, L',', true);
  1253. UnicodeString Name = CutToChar(S2, L':', true);
  1254. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1255. {
  1256. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1257. if (SameText(GetToolbarItemName(Item), Name))
  1258. {
  1259. // forward compatibility
  1260. UnicodeString S3 = CutToChar(S2, L':', true);
  1261. bool Visible = StrToIntDef(S3, 0);
  1262. Item->Visible = Visible;
  1263. }
  1264. }
  1265. }
  1266. }
  1267. }
  1268. }
  1269. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1270. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1271. }
  1272. //---------------------------------------------------------------------------
  1273. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1274. {
  1275. UnicodeString Result;
  1276. SetDockAllowDrag(true);
  1277. Result = ::GetToolbarsLayoutStr(this);
  1278. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1279. return Result;
  1280. }
  1281. //---------------------------------------------------------------------------
  1282. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1283. {
  1284. UnicodeString Result;
  1285. if (Item->Action != NULL)
  1286. {
  1287. Result = Item->Action->Name;
  1288. Result = RemoveSuffix(Result, L"Action", true);
  1289. }
  1290. else
  1291. {
  1292. Result = Item->Name;
  1293. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1294. Result = RemoveSuffix(Result, L"Item", true);
  1295. }
  1296. return Result;
  1297. }
  1298. //---------------------------------------------------------------------------
  1299. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1300. {
  1301. UnicodeString Result;
  1302. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1303. {
  1304. UnicodeString ToolbarStr;
  1305. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1306. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1307. {
  1308. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1309. {
  1310. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1311. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1312. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1313. // and save the changes here only.
  1314. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1315. {
  1316. UnicodeString Name = GetToolbarItemName(Item);
  1317. DebugAssert(Name.Pos(L"TBX") == 0);
  1318. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1319. }
  1320. }
  1321. }
  1322. if (!ToolbarStr.IsEmpty())
  1323. {
  1324. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1325. }
  1326. }
  1327. return Result;
  1328. }
  1329. //---------------------------------------------------------------------------
  1330. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1331. {
  1332. DebugAssert(FProgressForm == NULL);
  1333. bool AllowSkip = (Terminal != NULL) ? Terminal->IsCapable[fcSkipTransfer] : false;
  1334. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1335. FProgressForm->DeleteLocalToRecycleBin =
  1336. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1337. FProgressForm->DeleteRemoteToRecycleBin =
  1338. (Terminal != NULL) &&
  1339. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1340. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1341. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1342. LockWindow();
  1343. // Actually, do not know what hides the progress once the operation finishes
  1344. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1345. SetTaskbarListProgressState(TBPF_NORMAL);
  1346. }
  1347. //---------------------------------------------------------------------------
  1348. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1349. {
  1350. UnlockWindow();
  1351. SAFE_DESTROY(FProgressForm);
  1352. SetQueueProgress();
  1353. }
  1354. //---------------------------------------------------------------------------
  1355. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1356. TFileOperationProgressType & ProgressData)
  1357. {
  1358. // Download to temporary local directory
  1359. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1360. {
  1361. bool Continue = true;
  1362. FPendingTempSpaceWarn = false;
  1363. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1364. if (!Continue)
  1365. {
  1366. Abort();
  1367. }
  1368. }
  1369. // operation is being executed and we still didn't show up progress form
  1370. if (ProgressData.InProgress && !FProgressForm)
  1371. {
  1372. CreateProgressForm(NULL);
  1373. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1374. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1375. }
  1376. // operation is finished (or terminated), so we hide progress form
  1377. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1378. {
  1379. DestroyProgressForm();
  1380. if (ProgressData.Operation == foCalculateSize)
  1381. {
  1382. // When calculating size before transfer, the abort caused by
  1383. // cancel flag set due to "MoveToQueue" below,
  1384. // is not propagated back to us in ExecuteFileOperation,
  1385. // as it is silently swallowed.
  1386. // So we explicitly (re)throw it here.
  1387. if (FMoveToQueue)
  1388. {
  1389. Abort();
  1390. }
  1391. }
  1392. else
  1393. {
  1394. if ((ProgressData.Cancel == csContinue) &&
  1395. !FAutoOperation)
  1396. {
  1397. OperationComplete(ProgressData.StartTime);
  1398. }
  1399. }
  1400. }
  1401. if (FProgressForm &&
  1402. // Can happen only during synchronization, when ending one of the operations
  1403. ProgressData.InProgress)
  1404. {
  1405. FProgressForm->SetProgressData(ProgressData);
  1406. DebugAssert(ProgressData.InProgress);
  1407. SetTaskbarListProgressValue(&ProgressData);
  1408. if (FProgressForm->Cancel > csContinue)
  1409. {
  1410. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1411. if (FProgressForm->Cancel == csCancelFile)
  1412. {
  1413. FProgressForm->ClearCancel();
  1414. }
  1415. // cancel cancels even the move
  1416. FMoveToQueue = false;
  1417. }
  1418. else if (FProgressForm->MoveToQueue)
  1419. {
  1420. FMoveToQueue = true;
  1421. ProgressData.SetCancelAtLeast(csCancel);
  1422. }
  1423. if ((FTransferResumeList != NULL) &&
  1424. ProgressData.InProgress &&
  1425. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1426. !ProgressData.FullFileName.IsEmpty())
  1427. {
  1428. if ((FTransferResumeList->Count == 0) ||
  1429. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1430. {
  1431. // note that we do not recognize directories from files here
  1432. FTransferResumeList->Add(ProgressData.FullFileName);
  1433. }
  1434. }
  1435. }
  1436. }
  1437. //---------------------------------------------------------------------------
  1438. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1439. const TDateTime & StartTime)
  1440. {
  1441. if (GUIConfiguration->BeepOnFinish &&
  1442. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1443. {
  1444. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1445. DWORD Sound;
  1446. if (!ExtractFileExt(BeepSound).IsEmpty())
  1447. {
  1448. Sound = SND_FILENAME;
  1449. }
  1450. else
  1451. {
  1452. Sound = SND_ALIAS;
  1453. }
  1454. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1455. }
  1456. }
  1457. //---------------------------------------------------------------------------
  1458. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1459. TFileOperationProgressType & ProgressData)
  1460. {
  1461. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1462. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1463. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1464. FileOperationProgress(ProgressData);
  1465. }
  1466. //---------------------------------------------------------------------------
  1467. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1468. {
  1469. UnicodeString Result;
  1470. if (FProgressForm != NULL)
  1471. {
  1472. Result = FProgressForm->ProgressStr();
  1473. }
  1474. return Result;
  1475. }
  1476. //---------------------------------------------------------------------------
  1477. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1478. bool DragDrop)
  1479. {
  1480. return (!DragDrop && (DropSourceControl == NULL)) ||
  1481. (DropSourceControl == DirView(osOther));
  1482. }
  1483. //---------------------------------------------------------------------------
  1484. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1485. TFileOperation Operation, TOperationSide Side,
  1486. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1487. TOnceDoneOperation & OnceDoneOperation)
  1488. {
  1489. if (!FAutoOperation)
  1490. {
  1491. // no selection on "/upload", form serves only as event handler
  1492. // (it is not displayed)
  1493. if (PanelOperation(Side, FDragDropOperation) &&
  1494. Visible && (Operation != foCalculateSize) &&
  1495. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1496. {
  1497. if (FOnFileOperationFinished != NULL)
  1498. {
  1499. FOnFileOperationFinished(FileName, Success);
  1500. }
  1501. else
  1502. {
  1503. TOperationSide DViewSide;
  1504. if ((FProgressSide != osCurrent) &&
  1505. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1506. {
  1507. // assume the operation is over the focused panel
  1508. DViewSide = FProgressSide;
  1509. }
  1510. else
  1511. {
  1512. DViewSide = Side;
  1513. }
  1514. TCustomDirView * DView = DirView(DViewSide);
  1515. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(DViewSide));
  1516. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1517. // this can happen when local drive is unplugged in the middle of the operation
  1518. if (Item != NULL)
  1519. {
  1520. if (Success) Item->Selected = false;
  1521. DView->MakeProgressVisible(Item);
  1522. }
  1523. }
  1524. }
  1525. if ((Operation == foCopy) || (Operation == foMove))
  1526. {
  1527. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1528. {
  1529. if (Side == osLocal)
  1530. {
  1531. Configuration->Usage->Inc(L"UploadedFiles");
  1532. }
  1533. else
  1534. {
  1535. Configuration->Usage->Inc(L"DownloadedFiles");
  1536. }
  1537. }
  1538. }
  1539. }
  1540. if (Success && (FSynchronizeController != NULL))
  1541. {
  1542. DebugAssert(!IsSideLocalBrowser(osRemote));
  1543. if (Operation == foCopy)
  1544. {
  1545. DebugAssert(Side == osLocal);
  1546. FSynchronizeController->LogOperation(soUpload, FileName);
  1547. }
  1548. else if (Operation == foDelete)
  1549. {
  1550. DebugAssert(Side == osRemote);
  1551. FSynchronizeController->LogOperation(soDelete, FileName);
  1552. }
  1553. }
  1554. if (FProgressForm)
  1555. {
  1556. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1557. }
  1558. }
  1559. //---------------------------------------------------------------------------
  1560. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1561. TFileOperation Operation, TOperationSide Side,
  1562. bool Temp, const UnicodeString & FileName, Boolean Success,
  1563. TOnceDoneOperation & OnceDoneOperation)
  1564. {
  1565. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1566. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1567. OnceDoneOperation);
  1568. }
  1569. //---------------------------------------------------------------------------
  1570. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1571. {
  1572. return false;
  1573. }
  1574. //---------------------------------------------------------------------------
  1575. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1576. {
  1577. return false;
  1578. }
  1579. //---------------------------------------------------------------------------
  1580. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1581. {
  1582. return false;
  1583. }
  1584. //---------------------------------------------------------------------------
  1585. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1586. {
  1587. return NULL;
  1588. }
  1589. //---------------------------------------------------------------------------
  1590. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1591. {
  1592. DebugAssert(GetSide(Side) == osRemote);
  1593. DebugUsedParam(Side);
  1594. return RemoteDirView;
  1595. }
  1596. //---------------------------------------------------------------------------
  1597. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1598. {
  1599. DebugAssert(GetSide(Side) == osRemote);
  1600. DebugUsedParam(Side);
  1601. return RemoteDriveView;
  1602. }
  1603. //---------------------------------------------------------------------------
  1604. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1605. {
  1606. DebugAssert(GetSide(Side) == osRemote);
  1607. DebugUsedParam(Side);
  1608. return HasActiveTerminal();
  1609. }
  1610. //---------------------------------------------------------------------------
  1611. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1612. TOperationSide Side, int FilesOnly)
  1613. {
  1614. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1615. }
  1616. //---------------------------------------------------------------------------
  1617. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1618. TOperationSide Side, int FilesOnly)
  1619. {
  1620. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1621. }
  1622. //---------------------------------------------------------------------------
  1623. struct THistoryItemData
  1624. {
  1625. short int Side;
  1626. short int Index;
  1627. };
  1628. //---------------------------------------------------------------------------
  1629. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1630. {
  1631. DirView(Side)->HistoryGo(Index);
  1632. }
  1633. //---------------------------------------------------------------------------
  1634. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1635. {
  1636. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1637. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1638. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1639. }
  1640. //---------------------------------------------------------------------------
  1641. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1642. bool Back)
  1643. {
  1644. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1645. {
  1646. TCustomDirView * DView = DirView(Side);
  1647. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1648. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1649. if (ICount > 10)
  1650. {
  1651. ICount = 10;
  1652. }
  1653. Menu->Items->Clear();
  1654. THistoryItemData Data;
  1655. Data.Side = Side;
  1656. for (short int i = 1; i <= ICount; i++)
  1657. {
  1658. TTBCustomItem * Item = new TTBXItem(Menu);
  1659. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1660. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1661. Item->Hint = DView->HistoryPath[Data.Index];
  1662. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1663. Item->Tag = *reinterpret_cast<int*>(&Data);
  1664. Item->OnClick = HistoryItemClick;
  1665. Menu->Items->Add(Item);
  1666. }
  1667. }
  1668. }
  1669. //---------------------------------------------------------------------------
  1670. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1671. TOperationSide Side, bool Back)
  1672. {
  1673. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1674. {
  1675. // workaround
  1676. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1677. // operator new allocates memory in Pascal code, but calls inline
  1678. // contructor in C++, leading in problems, the function does
  1679. // both in Pascal code
  1680. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1681. UpdateHistoryMenu(Side, Back);
  1682. }
  1683. return FHistoryMenu[Side == osLocal][Back];
  1684. }
  1685. //---------------------------------------------------------------------------
  1686. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1687. TCustomDirView *Sender)
  1688. {
  1689. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1690. UpdateHistoryMenu(Side, true);
  1691. UpdateHistoryMenu(Side, false);
  1692. }
  1693. //---------------------------------------------------------------------------
  1694. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1695. {
  1696. // remote custom commands can be executed only if the server supports shell commands
  1697. // or have secondary shell
  1698. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1699. }
  1700. //---------------------------------------------------------------------------
  1701. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1702. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1703. {
  1704. // -1 = hidden, 0 = disabled, 1 = enabled
  1705. int Result;
  1706. TFileCustomCommand RemoteCustomCommand;
  1707. TLocalCustomCommand LocalCustomCommand;
  1708. TFileCustomCommand * NonInteractiveCustomCommand =
  1709. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1710. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1711. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1712. if (FLAGCLEAR(Command.Params, ccLocal))
  1713. {
  1714. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1715. // custom command that does not operate with files can be executed anytime ...
  1716. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1717. {
  1718. // ... but do not show such command in remote file menu
  1719. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1720. {
  1721. Result = AllowedState;
  1722. }
  1723. else
  1724. {
  1725. Result = -1;
  1726. }
  1727. }
  1728. else
  1729. {
  1730. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1731. {
  1732. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1733. }
  1734. else
  1735. {
  1736. Result = -1;
  1737. }
  1738. }
  1739. }
  1740. else
  1741. {
  1742. // custom command that does not operate with files can be executed anytime
  1743. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1744. {
  1745. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1746. }
  1747. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1748. {
  1749. if ((ListType == ccltAll) || (ListType == ccltFile))
  1750. {
  1751. // special case is "diff"-style command that can be executed over any side,
  1752. // if we have both sides
  1753. Result =
  1754. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1755. // directly (instead of EnableSelectedOperation) to pass
  1756. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1757. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1758. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1759. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1760. }
  1761. else if (ListType == ccltBoth)
  1762. {
  1763. DebugAssert(!IsLocalBrowserMode());
  1764. Result = 1;
  1765. }
  1766. else
  1767. {
  1768. Result = -1;
  1769. }
  1770. }
  1771. else
  1772. {
  1773. if ((ListType == ccltAll) ||
  1774. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1775. {
  1776. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1777. }
  1778. else
  1779. {
  1780. Result = -1;
  1781. }
  1782. }
  1783. if ((Result > 0) &&
  1784. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1785. !HasActiveTerminal())
  1786. {
  1787. Result = 0;
  1788. }
  1789. }
  1790. return Result;
  1791. }
  1792. //---------------------------------------------------------------------------
  1793. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1794. TStrings * FileList, const TCustomCommandType & ACommand,
  1795. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1796. {
  1797. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1798. {
  1799. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1800. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1801. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1802. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1803. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1804. bool Capture =
  1805. FLAGSET(ACommand.Params, ccShowResults) ||
  1806. FLAGSET(ACommand.Params, ccCopyResults) ||
  1807. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1808. TCaptureOutputEvent OutputEvent = NULL;
  1809. DebugAssert(FCapturedLog == NULL);
  1810. if (Capture)
  1811. {
  1812. FCapturedLog = new TStringList();
  1813. OutputEvent = TerminalCaptureLog;
  1814. }
  1815. try
  1816. {
  1817. if (!RemoteCustomCommand.IsFileCommand(Command))
  1818. {
  1819. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1820. OutputEvent);
  1821. }
  1822. else
  1823. {
  1824. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1825. }
  1826. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1827. {
  1828. if (FLAGSET(ACommand.Params, ccCopyResults))
  1829. {
  1830. CopyToClipboard(FCapturedLog);
  1831. }
  1832. if (FLAGSET(ACommand.Params, ccShowResults))
  1833. {
  1834. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1835. }
  1836. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1837. {
  1838. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1839. }
  1840. }
  1841. }
  1842. __finally
  1843. {
  1844. SAFE_DESTROY(FCapturedLog);
  1845. }
  1846. }
  1847. }
  1848. //---------------------------------------------------------------------------
  1849. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1850. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1851. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1852. {
  1853. DebugAssert(!IsLocalBrowserMode());
  1854. TStrings * LocalFileList = NULL;
  1855. TStrings * RemoteFileList = NULL;
  1856. TStrings * RemoteFileListFull = NULL;
  1857. try
  1858. {
  1859. if (LocalFileCommand)
  1860. {
  1861. if (ALocalFileList == NULL)
  1862. {
  1863. DebugAssert(HasDirView[osLocal]);
  1864. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1865. // directly (instead of EnableSelectedOperation) to pass
  1866. // false to FocusedFileOnlyWhenFocused
  1867. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1868. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1869. }
  1870. else
  1871. {
  1872. LocalFileList = ALocalFileList;
  1873. }
  1874. if (FileListCommand)
  1875. {
  1876. if (LocalFileList->Count != 1)
  1877. {
  1878. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1879. }
  1880. }
  1881. else
  1882. {
  1883. if ((LocalFileList->Count != 1) &&
  1884. (FileList->Count != 1) &&
  1885. (LocalFileList->Count != FileList->Count))
  1886. {
  1887. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1888. }
  1889. }
  1890. }
  1891. UnicodeString RootTempDir;
  1892. UnicodeString TempDir;
  1893. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1894. if (!RemoteFiles)
  1895. {
  1896. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1897. }
  1898. try
  1899. {
  1900. TDateTimes RemoteFileTimes;
  1901. if (RemoteFiles)
  1902. {
  1903. RemoteFileList = FileList;
  1904. }
  1905. else
  1906. {
  1907. RemoteFileList = new TStringList();
  1908. TMakeLocalFileListParams MakeFileListParam;
  1909. MakeFileListParam.FileList = RemoteFileList;
  1910. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1911. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1912. MakeFileListParam.Recursive =
  1913. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1914. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1915. if (!MakeFileListParam.Recursive)
  1916. {
  1917. RemoteFileTimes.clear();
  1918. RemoteFileListFull = new TStringList();
  1919. MakeFileListParam.FileList = RemoteFileListFull;
  1920. MakeFileListParam.Recursive = true;
  1921. MakeFileListParam.IncludeDirs = false;
  1922. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1923. }
  1924. else
  1925. {
  1926. RemoteFileListFull = RemoteFileList;
  1927. }
  1928. }
  1929. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1930. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1931. if (!NonBlocking)
  1932. {
  1933. int Count;
  1934. if (FileListCommand)
  1935. {
  1936. Count = 1;
  1937. }
  1938. else if (LocalFileCommand)
  1939. {
  1940. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1941. }
  1942. else
  1943. {
  1944. Count = RemoteFileList->Count;
  1945. }
  1946. Progress.Start(foCustomCommand, osRemote, Count);
  1947. DebugAssert(FProgressForm != NULL);
  1948. FProgressForm->ReadOnly = true;
  1949. }
  1950. try
  1951. {
  1952. if (FileListCommand)
  1953. {
  1954. UnicodeString LocalFile;
  1955. // MakeFileList does not delimit filenames
  1956. UnicodeString FileList = MakeFileList(RemoteFileList);
  1957. if (LocalFileCommand)
  1958. {
  1959. DebugAssert(LocalFileList->Count == 1);
  1960. LocalFile = LocalFileList->Strings[0];
  1961. }
  1962. TLocalCustomCommand CustomCommand(Data,
  1963. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1964. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1965. if (NonBlocking)
  1966. {
  1967. DebugAssert(!POutput);
  1968. ExecuteShellChecked(ShellCommand);
  1969. }
  1970. else
  1971. {
  1972. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1973. }
  1974. }
  1975. else if (LocalFileCommand)
  1976. {
  1977. if (LocalFileList->Count == 1)
  1978. {
  1979. UnicodeString LocalFile = LocalFileList->Strings[0];
  1980. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1981. {
  1982. UnicodeString FileName = RemoteFileList->Strings[Index];
  1983. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1984. Progress.SetFile(FileNameForProgress);
  1985. TLocalCustomCommand CustomCommand(Data,
  1986. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1987. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1988. TOnceDoneOperation OnceDoneOperation;
  1989. // Do not unselect anything, as it won't work with two panels anyway
  1990. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1991. }
  1992. }
  1993. else if (RemoteFileList->Count == 1)
  1994. {
  1995. UnicodeString FileName = RemoteFileList->Strings[0];
  1996. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1997. {
  1998. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1999. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2000. Progress.SetFile(LocalFileNameOnly);
  2001. TLocalCustomCommand CustomCommand(
  2002. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2003. FileName, LocalFileName, L"");
  2004. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2005. TOnceDoneOperation OnceDoneOperation;
  2006. // Do not unselect anything, as it won't work with two panels anyway
  2007. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2008. }
  2009. }
  2010. else
  2011. {
  2012. if (LocalFileList->Count != RemoteFileList->Count)
  2013. {
  2014. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2015. }
  2016. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2017. {
  2018. UnicodeString FileName = RemoteFileList->Strings[Index];
  2019. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2020. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2021. Progress.SetFile(LocalFileNameOnly); // sic
  2022. TLocalCustomCommand CustomCommand(
  2023. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2024. FileName, LocalFileName, L"");
  2025. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2026. TOnceDoneOperation OnceDoneOperation;
  2027. // Do not unselect anything, as it won't work with two panels anyway
  2028. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2029. }
  2030. }
  2031. }
  2032. else
  2033. {
  2034. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2035. {
  2036. UnicodeString FileName = RemoteFileList->Strings[Index];
  2037. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2038. Progress.SetFile(FileNameForProgress);
  2039. TLocalCustomCommand CustomCommand(Data,
  2040. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2041. FileName, L"", L"");
  2042. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2043. TOnceDoneOperation OnceDoneOperation;
  2044. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2045. }
  2046. }
  2047. }
  2048. __finally
  2049. {
  2050. if (!NonBlocking)
  2051. {
  2052. Progress.Stop();
  2053. }
  2054. }
  2055. DebugAssert(!FAutoOperation);
  2056. if (!RemoteFiles)
  2057. {
  2058. TempDir = IncludeTrailingBackslash(TempDir);
  2059. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2060. {
  2061. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2062. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2063. // Skip directories, process only nested files.
  2064. // The check is redundant as FileAge fails for directories anyway.
  2065. !DirectoryExists(FileName))
  2066. {
  2067. UnicodeString RemoteDir =
  2068. UnixExtractFileDir(
  2069. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2070. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2071. TDateTime NewTime;
  2072. if (FileAge(FileName, NewTime) &&
  2073. (NewTime != RemoteFileTimes[Index]))
  2074. {
  2075. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2076. TemporaryFileCopyParam(CopyParam);
  2077. CopyParam.FileMask = L"";
  2078. FAutoOperation = true;
  2079. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2080. TemporaryFilesList->Add(FileName);
  2081. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2082. }
  2083. }
  2084. }
  2085. }
  2086. }
  2087. __finally
  2088. {
  2089. FAutoOperation = false;
  2090. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2091. {
  2092. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2093. }
  2094. }
  2095. }
  2096. __finally
  2097. {
  2098. if (RemoteFileList != FileList)
  2099. {
  2100. delete RemoteFileList;
  2101. }
  2102. if (LocalFileList != ALocalFileList)
  2103. {
  2104. delete LocalFileList;
  2105. }
  2106. if (RemoteFileListFull != RemoteFileList)
  2107. {
  2108. delete RemoteFileListFull;
  2109. }
  2110. }
  2111. }
  2112. //---------------------------------------------------------------------------
  2113. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2114. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2115. bool FileListCommand, UnicodeString * POutput)
  2116. {
  2117. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2118. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2119. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2120. {
  2121. UnicodeString FileName = SelectedFileList->Strings[Index];
  2122. if (DirectoryExists(FileName))
  2123. {
  2124. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2125. {
  2126. LocalFileList->Add(FileName);
  2127. }
  2128. if (FLAGSET(ACommand.Params, ccRecursive))
  2129. {
  2130. TMakeLocalFileListParams MakeFileListParam;
  2131. MakeFileListParam.FileList = LocalFileList.get();
  2132. MakeFileListParam.FileTimes = NULL;
  2133. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2134. MakeFileListParam.Recursive = true;
  2135. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2136. }
  2137. }
  2138. else
  2139. {
  2140. LocalFileList->Add(FileName);
  2141. }
  2142. }
  2143. UnicodeString RemotePath;
  2144. if (Terminal != NULL)
  2145. {
  2146. RemotePath = Terminal->CurrentDirectory;
  2147. }
  2148. if (FileListCommand)
  2149. {
  2150. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2151. TLocalCustomCommand CustomCommand(
  2152. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2153. L"", L"", FileList);
  2154. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2155. }
  2156. else
  2157. {
  2158. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2159. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2160. DebugAssert(FProgressForm != NULL);
  2161. FProgressForm->ReadOnly = true;
  2162. try
  2163. {
  2164. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2165. {
  2166. UnicodeString FileName = LocalFileList->Strings[Index];
  2167. Progress.SetFile(FileName);
  2168. TLocalCustomCommand CustomCommand(
  2169. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2170. FileName, L"", L"");
  2171. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2172. TOnceDoneOperation OnceDoneOperation;
  2173. Progress.Finish(FileName, true, OnceDoneOperation);
  2174. }
  2175. }
  2176. __finally
  2177. {
  2178. Progress.Stop();
  2179. }
  2180. }
  2181. }
  2182. //---------------------------------------------------------------------------
  2183. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2184. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2185. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2186. {
  2187. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2188. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2189. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2190. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2191. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2192. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2193. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2194. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2195. std::unique_ptr<UnicodeString> POutput;
  2196. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2197. FLAGSET(ACommand.Params, ccCopyResults))
  2198. {
  2199. POutput.reset(new UnicodeString());
  2200. }
  2201. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2202. FProgressSide = FCurrentSide;
  2203. if (!LocalCustomCommand.IsFileCommand(Command))
  2204. {
  2205. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2206. }
  2207. // remote files?
  2208. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2209. {
  2210. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2211. }
  2212. // local files
  2213. else
  2214. {
  2215. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2216. }
  2217. if (POutput.get() != NULL)
  2218. {
  2219. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2220. int P = POutput->Pos(sLineBreak);
  2221. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2222. {
  2223. POutput->SetLength(P - 1);
  2224. }
  2225. // Copy even empty output to clipboard
  2226. if (FLAGSET(ACommand.Params, ccCopyResults))
  2227. {
  2228. CopyToClipboard(*POutput);
  2229. }
  2230. // But do not show an empty message box.
  2231. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2232. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2233. !POutput->IsEmpty())
  2234. {
  2235. TClipboardHandler ClipboardHandler;
  2236. ClipboardHandler.Text = *POutput;
  2237. TMessageParams Params;
  2238. TQueryButtonAlias Aliases[1];
  2239. Aliases[0].Button = qaRetry;
  2240. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2241. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2242. Params.Aliases = Aliases;
  2243. Params.AliasesCount = LENOF(Aliases);
  2244. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2245. }
  2246. }
  2247. }
  2248. //---------------------------------------------------------------------------
  2249. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2250. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2251. {
  2252. TCustomCommandData Data;
  2253. UnicodeString Site;
  2254. UnicodeString RemotePath;
  2255. if (HasActiveTerminal())
  2256. {
  2257. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2258. Data = TCustomCommandData(SessionData.get());
  2259. Site = Terminal->SessionData->SessionKey;
  2260. RemotePath = Terminal->CurrentDirectory;
  2261. }
  2262. UnicodeString HelpKeyword = ACommand.HomePage;
  2263. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2264. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2265. {
  2266. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2267. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2268. {
  2269. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2270. }
  2271. else
  2272. {
  2273. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2274. }
  2275. if (!DoCustomCommandOptionsDialog(
  2276. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2277. {
  2278. Abort();
  2279. }
  2280. }
  2281. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2282. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2283. {
  2284. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2285. }
  2286. else
  2287. {
  2288. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2289. }
  2290. }
  2291. //---------------------------------------------------------------------------
  2292. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2293. const TCustomCommandType & Command)
  2294. {
  2295. DebugAssert(FCustomCommandLocalFileList != NULL);
  2296. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2297. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2298. TStrings * LocalFileList = new TStringList();
  2299. TStrings * RemoteFileList = new TStringList();
  2300. try
  2301. {
  2302. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2303. {
  2304. LocalFileList->Clear();
  2305. LocalFileList->AddObject(
  2306. FCustomCommandLocalFileList->Strings[Index],
  2307. FCustomCommandLocalFileList->Objects[Index]);
  2308. RemoteFileList->Clear();
  2309. RemoteFileList->AddObject(
  2310. FCustomCommandRemoteFileList->Strings[Index],
  2311. FCustomCommandRemoteFileList->Objects[Index]);
  2312. CustomCommand(RemoteFileList, Command, LocalFileList);
  2313. }
  2314. }
  2315. __finally
  2316. {
  2317. delete LocalFileList;
  2318. delete RemoteFileList;
  2319. }
  2320. }
  2321. //---------------------------------------------------------------------------
  2322. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2323. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2324. {
  2325. delete FCustomCommandLocalFileList;
  2326. delete FCustomCommandRemoteFileList;
  2327. // takeover ownership,
  2328. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2329. FCustomCommandLocalFileList = LocalFileList;
  2330. FCustomCommandRemoteFileList = RemoteFileList;
  2331. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2332. if (Button != NULL)
  2333. {
  2334. FCustomCommandMenu->Items->Clear();
  2335. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2336. MenuPopup(FCustomCommandMenu, Button);
  2337. }
  2338. else
  2339. {
  2340. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2341. }
  2342. }
  2343. //---------------------------------------------------------------------------
  2344. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2345. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2346. {
  2347. DebugAssert(FCapturedLog != NULL);
  2348. if ((OutputType == cotOutput) || (OutputType == cotError))
  2349. {
  2350. FCapturedLog->Add(AddedLine);
  2351. }
  2352. }
  2353. //---------------------------------------------------------------------------
  2354. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2355. TOperationSide Side)
  2356. {
  2357. return
  2358. (Side == osOther) &&
  2359. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2360. }
  2361. //---------------------------------------------------------------------------
  2362. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2363. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2364. {
  2365. TTBItemOptions O;
  2366. O = Item->Options;
  2367. TCustomDirView * DView = DirView(Side);
  2368. if ((DView->ItemFocused != NULL) &&
  2369. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2370. // when resolving links is disabled, default action is to enter the directory,
  2371. // no matter what DoubleClickAction is configured to
  2372. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2373. // Can only Edit files, but can Open/Copy even directories
  2374. ((DoubleClickAction != dcaEdit) ||
  2375. !DView->ItemIsDirectory(DView->ItemFocused)))
  2376. {
  2377. Item->Options = O << tboDefault;
  2378. }
  2379. else
  2380. {
  2381. Item->Options = O >> tboDefault;
  2382. }
  2383. }
  2384. //---------------------------------------------------------------------------
  2385. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2386. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2387. {
  2388. TCustomDirView * DView = DirView(Side);
  2389. TListItem * Item = DView->ItemFocused;
  2390. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2391. EnableFocusedOperation[Side])
  2392. {
  2393. TPoint ClientPoint;
  2394. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2395. TPoint(0, 0) : MousePos;
  2396. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2397. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2398. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2399. }
  2400. }
  2401. //---------------------------------------------------------------------------
  2402. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2403. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2404. {
  2405. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2406. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2407. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2408. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2409. Handled = true;
  2410. }
  2411. //---------------------------------------------------------------------------
  2412. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2413. {
  2414. }
  2415. //---------------------------------------------------------------------------
  2416. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2417. {
  2418. DebugAssert(FErrorList == NULL);
  2419. if (WinConfiguration->ContinueOnError)
  2420. {
  2421. FErrorList = new TStringList();
  2422. Configuration->Usage->Inc(L"ContinuationsOnError");
  2423. }
  2424. NonVisualDataModule->StartBusy();
  2425. }
  2426. //---------------------------------------------------------------------------
  2427. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2428. {
  2429. NonVisualDataModule->EndBusy();
  2430. if (FErrorList)
  2431. {
  2432. HandleErrorList(FErrorList);
  2433. }
  2434. }
  2435. //---------------------------------------------------------------------------
  2436. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2437. const TCopyParamType & CopyParam)
  2438. {
  2439. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2440. {
  2441. Configuration->Usage->Inc(L"FileMaskUses");
  2442. }
  2443. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2444. {
  2445. Configuration->Usage->Inc(L"OperationMaskUses");
  2446. }
  2447. }
  2448. //---------------------------------------------------------------------------
  2449. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2450. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2451. {
  2452. DebugAssert(!IsLocalBrowserMode());
  2453. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2454. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2455. TTransferOperationParam DefaultParam;
  2456. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2457. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2458. switch (Param.Queue)
  2459. {
  2460. case asOn:
  2461. CopyParam.Queue = true;
  2462. break;
  2463. case asOff:
  2464. CopyParam.Queue = false;
  2465. break;
  2466. case asAuto:
  2467. default:
  2468. // keep default
  2469. break;
  2470. }
  2471. bool Result =
  2472. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2473. if (Result)
  2474. {
  2475. DebugAssert(Terminal);
  2476. bool SelectionRestored = false;
  2477. TCustomDirView * DView = NULL;
  2478. if (HasDirView[Side])
  2479. {
  2480. DView = DirView(Side);
  2481. DView->SaveSelection();
  2482. DView->SaveSelectedNames();
  2483. }
  2484. UpdateCopyParamCounters(CopyParam);
  2485. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2486. DebugAssert(FTransferResumeList == NULL);
  2487. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2488. FMoveToQueue = false;
  2489. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2490. try
  2491. {
  2492. TStrings * PermanentFileList;
  2493. std::unique_ptr<TStrings> PermanentFileListOwner;
  2494. try
  2495. {
  2496. if (Side == osLocal)
  2497. {
  2498. PermanentFileList = FileList;
  2499. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2500. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2501. if (Operation == foMove)
  2502. {
  2503. ReloadLocalDirectory();
  2504. if (DView != NULL)
  2505. {
  2506. DView->RestoreSelection();
  2507. }
  2508. SelectionRestored = true;
  2509. }
  2510. }
  2511. else
  2512. {
  2513. // Clone the file list as it may refer to current directory files,
  2514. // which get destroyed, when the source directory is reloaded after foMove operation.
  2515. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2516. // But for a hotfix, we are not going to do such a big change.
  2517. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2518. PermanentFileList = PermanentFileListOwner.get();
  2519. try
  2520. {
  2521. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2522. }
  2523. __finally
  2524. {
  2525. if (Operation == foMove)
  2526. {
  2527. if (DView != NULL)
  2528. {
  2529. DView->RestoreSelection();
  2530. }
  2531. SelectionRestored = true;
  2532. }
  2533. ReloadLocalDirectory(Param.TargetDirectory);
  2534. }
  2535. }
  2536. }
  2537. catch (EAbort &)
  2538. {
  2539. if (FMoveToQueue)
  2540. {
  2541. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2542. DebugAssert(CopyParam.TransferSkipList == NULL);
  2543. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2544. if (TransferResumeList->Count > 0)
  2545. {
  2546. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2547. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2548. }
  2549. CopyParam.TransferSkipList = TransferResumeList.release();
  2550. // not really needed, just to keep it consistent with TransferResumeList
  2551. FTransferResumeList = NULL;
  2552. FMoveToQueue = false;
  2553. Configuration->Usage->Inc("MovesToBackground");
  2554. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2555. ClearTransferSourceSelection(Direction);
  2556. }
  2557. throw;
  2558. }
  2559. }
  2560. __finally
  2561. {
  2562. if (!SelectionRestored && (DView != NULL))
  2563. {
  2564. DView->DiscardSavedSelection();
  2565. }
  2566. FTransferResumeList = NULL;
  2567. }
  2568. }
  2569. return Result;
  2570. }
  2571. //---------------------------------------------------------------------------
  2572. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2573. TOperationSide Side, TStrings * FileList, void * Param)
  2574. {
  2575. DebugAssert(FileList->Count);
  2576. // We deliberately do not toggle alternative flag (Param), but use OR,
  2577. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2578. // shortcut of CurrentDeleteAlternativeAction
  2579. bool Alternative =
  2580. bool(Param) || UseAlternativeFunction();
  2581. bool Recycle;
  2582. if (IsSideLocalBrowser(Side))
  2583. {
  2584. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2585. }
  2586. else
  2587. {
  2588. Recycle =
  2589. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2590. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2591. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2592. }
  2593. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2594. if (!Result)
  2595. {
  2596. UnicodeString Query;
  2597. if (FileList->Count == 1)
  2598. {
  2599. if (IsSideLocalBrowser(Side))
  2600. {
  2601. Query = ExtractFileName(FileList->Strings[0]);
  2602. }
  2603. else
  2604. {
  2605. Query = UnixExtractFileName(FileList->Strings[0]);
  2606. }
  2607. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2608. }
  2609. else
  2610. {
  2611. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2612. }
  2613. TMessageParams Params(mpNeverAskAgainCheck);
  2614. Params.ImageName = L"Delete file";
  2615. unsigned int Answer =
  2616. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2617. if (Answer == qaNeverAskAgain)
  2618. {
  2619. Result = true;
  2620. if (Recycle)
  2621. {
  2622. WinConfiguration->ConfirmRecycling = false;
  2623. }
  2624. else
  2625. {
  2626. WinConfiguration->ConfirmDeleting = false;
  2627. }
  2628. }
  2629. else
  2630. {
  2631. Result = (Answer == qaOK);
  2632. }
  2633. }
  2634. if (Result)
  2635. {
  2636. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2637. }
  2638. return Result;
  2639. }
  2640. //---------------------------------------------------------------------------
  2641. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2642. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2643. {
  2644. TAutoBatch AutoBatch(this);
  2645. bool Result;
  2646. if ((Operation == foCopy) || (Operation == foMove))
  2647. {
  2648. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2649. }
  2650. else if (Operation == foRename)
  2651. {
  2652. DebugAssert(DirView(Side)->ItemFocused);
  2653. DirView(Side)->ItemFocused->EditCaption();
  2654. Result = true;
  2655. }
  2656. else if (Operation == foDelete)
  2657. {
  2658. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2659. }
  2660. else if (Operation == foSetProperties)
  2661. {
  2662. DirView(osRemote)->SaveSelectedNames();
  2663. Result = SetProperties(Side, FileList);
  2664. }
  2665. else if (Operation == foCustomCommand)
  2666. {
  2667. DebugAssert(Param);
  2668. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2669. DirView(osRemote)->SaveSelectedNames();
  2670. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2671. CustomCommand(FileList, *Command, NULL);
  2672. Result = true;
  2673. }
  2674. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2675. {
  2676. DebugAssert(!IsSideLocalBrowser(Side));
  2677. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2678. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2679. }
  2680. else if (Operation == foLock)
  2681. {
  2682. DebugAssert(!IsSideLocalBrowser(Side));
  2683. LockFiles(FileList, true);
  2684. Result = true;
  2685. }
  2686. else if (Operation == foUnlock)
  2687. {
  2688. DebugAssert(!IsSideLocalBrowser(Side));
  2689. LockFiles(FileList, false);
  2690. Result = true;
  2691. }
  2692. else
  2693. {
  2694. DebugFail();
  2695. }
  2696. return Result;
  2697. }
  2698. //---------------------------------------------------------------------------
  2699. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2700. {
  2701. if (Side == osCurrent)
  2702. {
  2703. Side = FCurrentSide;
  2704. }
  2705. return Side;
  2706. }
  2707. //---------------------------------------------------------------------------
  2708. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2709. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2710. {
  2711. Side = GetSide(Side);
  2712. bool Result;
  2713. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, IsSideLocalBrowser(Side), NULL);
  2714. try
  2715. {
  2716. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2717. }
  2718. __finally
  2719. {
  2720. delete FileList;
  2721. }
  2722. return Result;
  2723. }
  2724. //---------------------------------------------------------------------------
  2725. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2726. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2727. bool NoConfirmation, void * Param)
  2728. {
  2729. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2730. {
  2731. if ((Operation == foCopy) || (Operation == foMove))
  2732. {
  2733. DebugAssert(!IsLocalBrowserMode());
  2734. if (GetSide(Side) == osLocal)
  2735. {
  2736. Configuration->Usage->Inc(L"UploadsCommand");
  2737. }
  2738. else
  2739. {
  2740. Configuration->Usage->Inc(L"DownloadsCommand");
  2741. }
  2742. }
  2743. }
  2744. }
  2745. //---------------------------------------------------------------------------
  2746. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2747. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2748. {
  2749. TTransferOperationParam Param;
  2750. if ((WinConfiguration->Interface != ifCommander) ||
  2751. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2752. {
  2753. Flags &= ~cocShortCutHint;
  2754. }
  2755. TCustomDirView * DView = DirView(Side);
  2756. Param.Options =
  2757. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2758. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2759. if (FLAGSET(Flags, cocQueue))
  2760. {
  2761. Param.Queue = asOn;
  2762. }
  2763. else if (FLAGSET(Flags, cocNonQueue))
  2764. {
  2765. Param.Queue = asOff;
  2766. }
  2767. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2768. }
  2769. //---------------------------------------------------------------------------
  2770. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2771. {
  2772. int Count = ErrorList->Count;
  2773. try
  2774. {
  2775. if (ErrorList->Count)
  2776. {
  2777. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2778. if (MessageDialog(Message, qtError,
  2779. qaOK | qaCancel, HELP_NONE) == qaOK)
  2780. {
  2781. unsigned int Answer;
  2782. int Index = 0;
  2783. do
  2784. {
  2785. DebugAssert(Index >= 0 && Index < Count);
  2786. TQueryButtonAlias Aliases[2];
  2787. Aliases[0].Button = qaYes;
  2788. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2789. Aliases[1].Button = qaNo;
  2790. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2791. TMessageParams Params;
  2792. Params.Aliases = Aliases;
  2793. Params.AliasesCount = LENOF(Aliases);
  2794. int No = Index + 1;
  2795. UnicodeString Message = ErrorList->Strings[Index];
  2796. UnicodeString AMainInstructions;
  2797. UnicodeString Details;
  2798. if (ExtractMainInstructions(Message, AMainInstructions))
  2799. {
  2800. Details = Message;
  2801. Message = AMainInstructions;
  2802. }
  2803. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2804. Answer = MoreMessageDialog(
  2805. Message,
  2806. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2807. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2808. qaOK, HELP_NONE, &Params);
  2809. if (Answer == qaNo)
  2810. {
  2811. Index++;
  2812. }
  2813. if (Answer == qaYes)
  2814. {
  2815. Index--;
  2816. }
  2817. }
  2818. while (Answer != qaOK);
  2819. }
  2820. }
  2821. }
  2822. __finally
  2823. {
  2824. TStrings * List = ErrorList;
  2825. ErrorList = NULL;
  2826. for (int i = 0; i < Count; i++)
  2827. {
  2828. delete List->Objects[i];
  2829. }
  2830. delete List;
  2831. }
  2832. }
  2833. //---------------------------------------------------------------------------
  2834. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2835. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2836. {
  2837. // needed for checking filemasks, as there's no directory object
  2838. // associated with the file object
  2839. File->FullFileName = FullFileName;
  2840. TFileMasks::TParams MaskParams;
  2841. MaskParams.Size = File->Size;
  2842. MaskParams.Modification = File->Modification;
  2843. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2844. }
  2845. //---------------------------------------------------------------------------
  2846. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2847. {
  2848. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2849. Side = GetSide(Side);
  2850. TCustomDirView * DView = DirView(Side);
  2851. TListItem * FocusedItem = DView->ItemFocused;
  2852. UnicodeString Name;
  2853. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2854. {
  2855. Name = DView->ItemFileName(FocusedItem);
  2856. }
  2857. else
  2858. {
  2859. Name = LoadStr(NEW_FILE);
  2860. }
  2861. UnicodeString Names = Name;
  2862. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2863. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2864. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  2865. {
  2866. while (!Names.IsEmpty())
  2867. {
  2868. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2869. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2870. UnicodeString TargetFileName;
  2871. UnicodeString LocalFileName;
  2872. UnicodeString RootTempDir;
  2873. UnicodeString TempDir;
  2874. UnicodeString RemoteDirectory;
  2875. bool ExistingFile = false;
  2876. if (!IsSideLocalBrowser(Side))
  2877. {
  2878. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  2879. TRemoteFile * File = NULL;
  2880. if (Terminal->FileExists(Name, &File) &&
  2881. !File->IsDirectory)
  2882. {
  2883. try
  2884. {
  2885. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2886. ExistingFile = true;
  2887. }
  2888. __finally
  2889. {
  2890. delete File;
  2891. }
  2892. }
  2893. if (!ExistingFile)
  2894. {
  2895. RemoteDirectory = UnixExtractFilePath(Name);
  2896. TemporaryDirectoryForRemoteFiles(
  2897. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2898. TargetFileName = UnixExtractFileName(Name);
  2899. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2900. LocalFileName = TempDir +
  2901. // We probably do not want to trim the VMS version here
  2902. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2903. }
  2904. }
  2905. else
  2906. {
  2907. if (ExtractFilePath(Name).IsEmpty())
  2908. {
  2909. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2910. }
  2911. else
  2912. {
  2913. LocalFileName = ExpandFileName(Name);
  2914. }
  2915. TargetFileName = ExtractFileName(Name);
  2916. }
  2917. if (!ExistingFile)
  2918. {
  2919. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2920. if (NewFile)
  2921. {
  2922. int File = FileCreate(ApiPath(LocalFileName));
  2923. if (File < 0)
  2924. {
  2925. if (!RootTempDir.IsEmpty())
  2926. {
  2927. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2928. }
  2929. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2930. }
  2931. else
  2932. {
  2933. FileClose(File);
  2934. }
  2935. }
  2936. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2937. const TEditorData * ExternalEditor = NULL;
  2938. TFileMasks::TParams MaskParams; // size not known
  2939. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2940. false, MaskParams);
  2941. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2942. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  2943. }
  2944. }
  2945. }
  2946. }
  2947. //---------------------------------------------------------------------------
  2948. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2949. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2950. {
  2951. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2952. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2953. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2954. return
  2955. ((ExecuteFileBy == efInternalEditor)) ||
  2956. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2957. }
  2958. //---------------------------------------------------------------------------
  2959. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2960. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2961. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2962. UnicodeString RemoteDirectory, bool NewFile)
  2963. {
  2964. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2965. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2966. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2967. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2968. Side = GetSide(Side);
  2969. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2970. if (!IsSideLocalBrowser(Side))
  2971. {
  2972. Data->Terminal = Terminal;
  2973. Data->Queue = Queue;
  2974. Data->SessionData = CloneCurrentSessionData();
  2975. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2976. Data->RemoteDirectory = RemoteDirectory;
  2977. Data->SessionName = Terminal->SessionData->SessionName;
  2978. Data->LocalRootDirectory = LocalRootDirectory;
  2979. Data->OriginalFileName = OriginalFileName;
  2980. Data->Command = L""; // will be changed later for external editor
  2981. }
  2982. if (ExecuteFileBy == efInternalEditor)
  2983. {
  2984. if (!IsSideLocalBrowser(Side))
  2985. {
  2986. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  2987. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  2988. TForm * Editor;
  2989. try
  2990. {
  2991. TNotifyEvent OnFileChanged = NULL;
  2992. TNotifyEvent OnSaveAll = NULL;
  2993. TAnyModifiedEvent OnAnyModified = NULL;
  2994. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2995. if (Terminal->IsCapable[fcBackgroundTransfers])
  2996. {
  2997. OnFileChanged = FEditorManager->FileChanged;
  2998. OnSaveAll = SaveAllInternalEditors;
  2999. OnAnyModified = AnyInternalEditorModified;
  3000. }
  3001. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  3002. FEditorManager->FileReload, FEditorManager->FileClosed,
  3003. OnSaveAll, OnAnyModified,
  3004. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3005. }
  3006. catch(...)
  3007. {
  3008. if (!LocalRootDirectory.IsEmpty())
  3009. {
  3010. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  3011. }
  3012. throw;
  3013. }
  3014. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3015. }
  3016. else
  3017. {
  3018. DebugAssert(!FStandaloneEditing);
  3019. TForm * Editor =
  3020. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3021. SaveAllInternalEditors, AnyInternalEditorModified,
  3022. L"", false, SessionColor, -1, NewFile);
  3023. FLocalEditors->Add(Editor);
  3024. }
  3025. }
  3026. else
  3027. {
  3028. HANDLE Process;
  3029. if (ExecuteFileBy == efExternalEditor)
  3030. {
  3031. UnicodeString Program, Params, Dir;
  3032. Data->Command = ExternalEditor->ExternalEditor;
  3033. ReformatFileNameCommand(Data->Command);
  3034. SplitCommand(Data->Command, Program, Params, Dir);
  3035. Params = ExpandFileNameCommand(Params, FileName);
  3036. Program = ExpandEnvironmentVariables(Program);
  3037. if (!ExecuteShell(Program, Params, Process))
  3038. {
  3039. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3040. }
  3041. }
  3042. else
  3043. {
  3044. DebugAssert(!IsSideLocalBrowser(Side));
  3045. if (!ExecuteShell(FileName, L"", Process))
  3046. {
  3047. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3048. }
  3049. }
  3050. if (IsSideLocalBrowser(Side) ||
  3051. ((ExecuteFileBy == efShell) &&
  3052. !WinConfiguration->Editor.SDIShellEditor) ||
  3053. ((ExecuteFileBy == efExternalEditor) &&
  3054. !ExternalEditor->SDIExternalEditor))
  3055. {
  3056. // no need for handle
  3057. if (Process != NULL)
  3058. {
  3059. DebugCheck(CloseHandle(Process));
  3060. }
  3061. Process = INVALID_HANDLE_VALUE;
  3062. }
  3063. else
  3064. {
  3065. if (Process == NULL)
  3066. {
  3067. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3068. }
  3069. }
  3070. if (!IsSideLocalBrowser(Side))
  3071. {
  3072. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3073. }
  3074. }
  3075. }
  3076. //---------------------------------------------------------------------------
  3077. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3078. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3079. void * /*Arg*/)
  3080. {
  3081. if (Token != NULL)
  3082. {
  3083. EditorFormFileSave(static_cast<TForm *>(Token));
  3084. }
  3085. }
  3086. //---------------------------------------------------------------------------
  3087. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3088. {
  3089. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3090. {
  3091. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3092. }
  3093. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3094. }
  3095. //---------------------------------------------------------------------------
  3096. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3097. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3098. void * Arg)
  3099. {
  3100. if ((Token != NULL) &&
  3101. IsEditorFormModified(static_cast<TForm *>(Token)))
  3102. {
  3103. *static_cast<bool *>(Arg) = true;
  3104. }
  3105. }
  3106. //---------------------------------------------------------------------------
  3107. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3108. TObject * /*Sender*/, bool & Modified)
  3109. {
  3110. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3111. {
  3112. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3113. {
  3114. Modified = true;
  3115. }
  3116. }
  3117. if (!Modified)
  3118. {
  3119. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3120. }
  3121. }
  3122. //---------------------------------------------------------------------------
  3123. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3124. {
  3125. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3126. }
  3127. //---------------------------------------------------------------------------
  3128. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3129. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3130. UnicodeString & Result, UnicodeString & RootDirectory)
  3131. {
  3132. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3133. {
  3134. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3135. Result = RootDirectory;
  3136. }
  3137. else
  3138. {
  3139. RootDirectory = L"";
  3140. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3141. Result = IncludeTrailingBackslash(Result);
  3142. }
  3143. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3144. {
  3145. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3146. }
  3147. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3148. {
  3149. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3150. {
  3151. RemoteDirectory.Delete(1, 1);
  3152. }
  3153. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3154. }
  3155. if (!ForceDirectories(ApiPath(Result)))
  3156. {
  3157. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3158. }
  3159. }
  3160. //---------------------------------------------------------------------------
  3161. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3162. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3163. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3164. {
  3165. DebugAssert(!IsLocalBrowserMode());
  3166. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3167. if (ForceText)
  3168. {
  3169. CopyParam.TransferMode = tmAscii;
  3170. }
  3171. TemporaryFileCopyParam(CopyParam);
  3172. if (AllFiles)
  3173. {
  3174. CopyParam.IncludeFileMask = TFileMasks();
  3175. }
  3176. if (TempDir.IsEmpty())
  3177. {
  3178. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3179. }
  3180. DebugAssert(!FAutoOperation);
  3181. FAutoOperation = AutoOperation;
  3182. Terminal->ExceptionOnFail = true;
  3183. try
  3184. {
  3185. try
  3186. {
  3187. // turn off confirmations, as for MDI editors we may possibly download
  3188. // the same file over
  3189. Terminal->CopyToLocal(
  3190. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3191. if (GetTargetNames)
  3192. {
  3193. for (int i = 0; i < FileList->Count; i++)
  3194. {
  3195. FileList->Strings[i] =
  3196. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3197. }
  3198. }
  3199. }
  3200. catch(...)
  3201. {
  3202. if (!RootTempDir.IsEmpty())
  3203. {
  3204. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3205. }
  3206. throw;
  3207. }
  3208. }
  3209. __finally
  3210. {
  3211. FAutoOperation = false;
  3212. Terminal->ExceptionOnFail = false;
  3213. }
  3214. }
  3215. //---------------------------------------------------------------------------
  3216. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3217. {
  3218. // Do not waste time checking for default editor list the next time,
  3219. // or testing if default editor is not notepad.
  3220. bool TryNextTime = false;
  3221. UnicodeString UsageState;
  3222. if (!WinConfiguration->EditorList->IsDefaultList())
  3223. {
  3224. UsageState = "H";
  3225. }
  3226. else
  3227. {
  3228. UnicodeString Executable;
  3229. UnicodeString ExecutableDescription;
  3230. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3231. {
  3232. UnicodeString Message =
  3233. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3234. unsigned int Answer =
  3235. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3236. if (Answer != qaOK)
  3237. {
  3238. UsageState = "R";
  3239. }
  3240. else
  3241. {
  3242. UsageState = "A";
  3243. TEditorData EditorData;
  3244. EditorData.Editor = edExternal;
  3245. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3246. EditorData.ExternalEditorOptionsAutodetect();
  3247. TEditorList EditorList;
  3248. EditorList = *WinConfiguration->EditorList;
  3249. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3250. WinConfiguration->EditorList = &EditorList;
  3251. }
  3252. }
  3253. }
  3254. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3255. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3256. }
  3257. //---------------------------------------------------------------------------
  3258. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3259. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3260. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3261. {
  3262. if (ExecuteFileBy == efDefaultEditor)
  3263. {
  3264. if (!WinConfiguration->OfferedEditorAutoConfig)
  3265. {
  3266. EditorAutoConfig();
  3267. }
  3268. const TEditorPreferences * Editor =
  3269. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3270. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3271. {
  3272. ExecuteFileBy = efInternalEditor;
  3273. ExternalEditor = NULL;
  3274. }
  3275. else if (Editor->Data->Editor == edOpen)
  3276. {
  3277. ExecuteFileBy = efShell;
  3278. ExternalEditor = NULL;
  3279. }
  3280. else
  3281. {
  3282. ExecuteFileBy = efExternalEditor;
  3283. ExternalEditor = Editor->Data;
  3284. }
  3285. }
  3286. }
  3287. //---------------------------------------------------------------------------
  3288. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3289. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3290. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3291. {
  3292. UnicodeString OriginalFileName;
  3293. UnicodeString LocalRootDirectory;
  3294. UnicodeString RemoteDirectory;
  3295. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3296. IsSideLocalBrowser(Side), MaskParams);
  3297. UnicodeString Counter;
  3298. UnicodeString LocalFileName;
  3299. bool Handled = false;
  3300. if (!IsSideLocalBrowser(Side))
  3301. {
  3302. // We need to trim VMS version here, so that we use name without version
  3303. // when uploading back to create a new version of the file
  3304. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3305. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3306. TObject * Token = NULL;
  3307. UnicodeString LocalDirectory;
  3308. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3309. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3310. LocalDirectory))
  3311. {
  3312. if (Token != NULL)
  3313. {
  3314. TForm * Form = dynamic_cast<TForm *>(Token);
  3315. if (Form->WindowState == wsMinimized)
  3316. {
  3317. ShowWindow(Form->Handle, SW_RESTORE);
  3318. }
  3319. else
  3320. {
  3321. Form->SetFocus();
  3322. }
  3323. Handled = true;
  3324. }
  3325. else
  3326. {
  3327. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3328. }
  3329. }
  3330. if (!Handled)
  3331. {
  3332. TStringList * FileList1 = new TStringList();
  3333. try
  3334. {
  3335. FileList1->AddObject(FullFileName, Object);
  3336. TemporarilyDownloadFiles(FileList1,
  3337. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3338. LocalRootDirectory, LocalDirectory, true, true, true);
  3339. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3340. }
  3341. __finally
  3342. {
  3343. delete FileList1;
  3344. }
  3345. switch (ExecuteFileBy)
  3346. {
  3347. case efShell:
  3348. Counter = "RemoteFilesExecuted";
  3349. break;
  3350. case efInternalEditor:
  3351. Counter = "RemoteFilesOpenedInInternalEditor";
  3352. break;
  3353. case efExternalEditor:
  3354. Counter = "RemoteFilesOpenedInExternalEditor";
  3355. break;
  3356. default:
  3357. DebugFail();
  3358. }
  3359. }
  3360. }
  3361. else
  3362. {
  3363. LocalFileName = FullFileName;
  3364. OriginalFileName = ExtractFileName(FullFileName);
  3365. switch (ExecuteFileBy)
  3366. {
  3367. case efShell:
  3368. Counter = "LocalFilesExecuted";
  3369. break;
  3370. case efInternalEditor:
  3371. Counter = "LocalFilesOpenedInInternalEditor";
  3372. break;
  3373. case efExternalEditor:
  3374. Counter = "LocalFilesOpenedInExternalEditor";
  3375. break;
  3376. default:
  3377. DebugFail();
  3378. }
  3379. }
  3380. if (!Handled)
  3381. {
  3382. Configuration->Usage->Inc(Counter);
  3383. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3384. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3385. }
  3386. }
  3387. //---------------------------------------------------------------------------
  3388. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3389. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3390. bool AllSelected, bool OnFocused)
  3391. {
  3392. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3393. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3394. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3395. Side = GetSide(Side);
  3396. TCustomDirView * DView = DirView(Side);
  3397. TStrings * FileList =
  3398. AllSelected ?
  3399. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3400. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3401. try
  3402. {
  3403. DebugAssert(AllSelected || (FileList->Count == 1));
  3404. for (int i = 0; i < FileList->Count; i++)
  3405. {
  3406. UnicodeString ListFileName = FileList->Strings[i];
  3407. UnicodeString FileNameOnly =
  3408. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3409. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3410. if (!DView->ItemIsDirectory(Item))
  3411. {
  3412. UnicodeString FullFileName;
  3413. if (!IsSideLocalBrowser(Side))
  3414. {
  3415. FullFileName = DirView(Side)->Path + ListFileName;
  3416. }
  3417. else
  3418. {
  3419. FullFileName = ListFileName;
  3420. }
  3421. TFileMasks::TParams MaskParams;
  3422. MaskParams.Size = DView->ItemFileSize(Item);
  3423. TDateTimePrecision Precision;
  3424. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3425. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3426. FileList->Objects[i], MaskParams);
  3427. }
  3428. }
  3429. }
  3430. __finally
  3431. {
  3432. delete FileList;
  3433. }
  3434. }
  3435. //---------------------------------------------------------------------------
  3436. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3437. {
  3438. CopyParam.FileNameCase = ncNoChange;
  3439. CopyParam.PreserveRights = false;
  3440. CopyParam.PreserveReadOnly = false;
  3441. CopyParam.ReplaceInvalidChars = true;
  3442. CopyParam.IncludeFileMask = TFileMasks();
  3443. CopyParam.NewerOnly = false;
  3444. CopyParam.FileMask = L"";
  3445. }
  3446. //---------------------------------------------------------------------------
  3447. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3448. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3449. {
  3450. TTerminalManager * Manager = TTerminalManager::Instance();
  3451. if (!IsActiveTerminal(Data->Terminal))
  3452. {
  3453. if (!NonVisualDataModule->Busy)
  3454. {
  3455. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3456. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3457. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3458. if (Data->Terminal == NULL)
  3459. {
  3460. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3461. if (SameSiteTerminal != NULL)
  3462. {
  3463. UnicodeString Message =
  3464. FMTLOAD(EDIT_SESSION_REATTACH,
  3465. (FileNameOnly, Data->SessionName, FileNameOnly));
  3466. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3467. {
  3468. Data->Terminal = Terminal;
  3469. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3470. Data->SessionName = Terminal->SessionData->SessionName;
  3471. // We might also overwrite session data
  3472. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3473. }
  3474. else
  3475. {
  3476. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3477. Abort();
  3478. }
  3479. }
  3480. }
  3481. // foreground session should reconnect itself
  3482. else if (Terminal != Data->Terminal)
  3483. {
  3484. UnicodeString Message =
  3485. MainInstructions(
  3486. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3487. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3488. {
  3489. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3490. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3491. }
  3492. else
  3493. {
  3494. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3495. Abort();
  3496. }
  3497. }
  3498. }
  3499. if (!IsActiveTerminal(Data->Terminal))
  3500. {
  3501. Configuration->Usage->Inc(L"EditInactiveSession");
  3502. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3503. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3504. (ExtractFileName(FileName), Data->SessionName)));
  3505. }
  3506. }
  3507. TStrings * FileList = new TStringList();
  3508. try
  3509. {
  3510. FileList->Add(FileName);
  3511. // consider using the same settings (preset) as when the file was downloaded
  3512. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3513. TemporaryFileCopyParam(CopyParam);
  3514. if (Data->ForceText)
  3515. {
  3516. CopyParam.TransferMode = tmAscii;
  3517. }
  3518. // so i do not need to worry if masking algorithm works in all cases
  3519. // ("" means "copy file name", no masking is actually done)
  3520. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3521. {
  3522. CopyParam.FileMask = L"";
  3523. }
  3524. else
  3525. {
  3526. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3527. }
  3528. DebugAssert(Data->Queue != NULL);
  3529. int Params = cpNoConfirmation | cpTemporary;
  3530. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3531. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3532. QueueItem->CompleteEvent = UploadCompleteEvent;
  3533. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3534. }
  3535. __finally
  3536. {
  3537. delete FileList;
  3538. }
  3539. }
  3540. //---------------------------------------------------------------------------
  3541. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3542. const UnicodeString FileName, const TEditedFileData * Data)
  3543. {
  3544. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3545. DebugAssert(!NonVisualDataModule->Busy);
  3546. if (!IsActiveTerminal(Data->Terminal))
  3547. {
  3548. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3549. (ExtractFileName(FileName), Data->SessionName)));
  3550. }
  3551. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3552. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3553. NonVisualDataModule->StartBusy();
  3554. try
  3555. {
  3556. std::unique_ptr<TRemoteFile> File;
  3557. UnicodeString RemoteFileName =
  3558. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3559. Terminal->ExceptionOnFail = true;
  3560. try
  3561. {
  3562. TRemoteFile * AFile = NULL;
  3563. Terminal->ReadFile(RemoteFileName, AFile);
  3564. File.reset(AFile);
  3565. if (!File->HaveFullFileName)
  3566. {
  3567. File->FullFileName = RemoteFileName;
  3568. }
  3569. }
  3570. __finally
  3571. {
  3572. Terminal->ExceptionOnFail = false;
  3573. }
  3574. std::unique_ptr<TStrings> FileList(new TStringList());
  3575. FileList->AddObject(RemoteFileName, File.get());
  3576. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3577. UnicodeString TempDir = ExtractFilePath(FileName);
  3578. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3579. TempDir, true, true, true);
  3580. // sanity check, the target file name should be still the same
  3581. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3582. }
  3583. __finally
  3584. {
  3585. NonVisualDataModule->EndBusy();
  3586. // it actually may not exist anymore...
  3587. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3588. }
  3589. }
  3590. //---------------------------------------------------------------------------
  3591. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3592. const TEditedFileData * Data, bool & KeepOpen)
  3593. {
  3594. // Command is set for external editors only (not for "shell" open).
  3595. if (!Data->Command.IsEmpty())
  3596. {
  3597. bool AnyFound = false;
  3598. bool AnyMDI = false;
  3599. bool AnyNonMDI = false;
  3600. bool AnyDetect = false;
  3601. TEditorList * EditorList = new TEditorList();
  3602. try
  3603. {
  3604. *EditorList = *WinConfiguration->EditorList;
  3605. for (int i = 0; i < EditorList->Count; i++)
  3606. {
  3607. const TEditorPreferences * Editor = EditorList->Editors[i];
  3608. if ((Editor->Data->Editor == edExternal) &&
  3609. (Editor->Data->ExternalEditor == Data->Command))
  3610. {
  3611. AnyFound = true;
  3612. if (Editor->Data->SDIExternalEditor)
  3613. {
  3614. AnyNonMDI = true;
  3615. if (Editor->Data->DetectMDIExternalEditor)
  3616. {
  3617. AnyDetect = true;
  3618. }
  3619. }
  3620. else
  3621. {
  3622. AnyMDI = true;
  3623. }
  3624. }
  3625. }
  3626. bool EnableMDI = false;
  3627. bool DisableDetect = false;
  3628. if (AnyMDI)
  3629. {
  3630. KeepOpen = true;
  3631. if (AnyNonMDI)
  3632. {
  3633. // there is at least one instance of the editor with MDI support enabled,
  3634. // and one with disabled, enable it for all instances
  3635. EnableMDI = true;
  3636. }
  3637. }
  3638. else if (AnyFound && !AnyDetect)
  3639. {
  3640. // at least once instance found but all have MDI autodetection disabled
  3641. // => close the file (default action)
  3642. }
  3643. else
  3644. {
  3645. // no instance of the editor has MDI support enabled
  3646. TMessageParams Params;
  3647. if (AnyFound)
  3648. {
  3649. // there is at least one instance of the editor
  3650. Params.Params |= mpNeverAskAgainCheck;
  3651. }
  3652. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3653. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3654. switch (Answer)
  3655. {
  3656. case qaNeverAskAgain:
  3657. DisableDetect = true;
  3658. break;
  3659. case qaNo:
  3660. EnableMDI = true;
  3661. KeepOpen = true;
  3662. break;
  3663. }
  3664. }
  3665. if (AnyFound && (EnableMDI || DisableDetect))
  3666. {
  3667. bool Changed = false;
  3668. for (int i = 0; i < EditorList->Count; i++)
  3669. {
  3670. const TEditorPreferences * Editor = EditorList->Editors[i];
  3671. if ((Editor->Data->Editor == edExternal) &&
  3672. (Editor->Data->ExternalEditor == Data->Command) &&
  3673. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3674. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3675. {
  3676. Changed = true;
  3677. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3678. if (EnableMDI)
  3679. {
  3680. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3681. }
  3682. if (DisableDetect)
  3683. {
  3684. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3685. }
  3686. EditorList->Change(i, UpdatedEditor);
  3687. }
  3688. }
  3689. if (Changed)
  3690. {
  3691. WinConfiguration->EditorList = EditorList;
  3692. }
  3693. }
  3694. }
  3695. __finally
  3696. {
  3697. delete EditorList;
  3698. }
  3699. }
  3700. else
  3701. {
  3702. // "open" case
  3703. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3704. qaOK, HELP_APP_EARLY_CLOSED);
  3705. }
  3706. }
  3707. //---------------------------------------------------------------------------
  3708. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3709. {
  3710. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3711. }
  3712. //---------------------------------------------------------------------------
  3713. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3714. {
  3715. SideEnter(osRemote);
  3716. }
  3717. //---------------------------------------------------------------------------
  3718. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3719. {
  3720. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3721. SideEnter(osRemote);
  3722. }
  3723. //---------------------------------------------------------------------------
  3724. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3725. {
  3726. FCurrentSide = Side;
  3727. if (Visible)
  3728. {
  3729. UpdateControls();
  3730. }
  3731. }
  3732. //---------------------------------------------------------------------------
  3733. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3734. TStrings * FileList, bool Alternative)
  3735. {
  3736. TCustomDirView * DView = DirView(Side);
  3737. DView->SaveSelection();
  3738. DView->SaveSelectedNames();
  3739. DebugAssert(!FAlternativeDelete);
  3740. FAlternativeDelete = Alternative;
  3741. try
  3742. {
  3743. if (!IsSideLocalBrowser(Side))
  3744. {
  3745. DebugAssert(Terminal != NULL);
  3746. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3747. }
  3748. else
  3749. {
  3750. if (IsLocalBrowserMode())
  3751. {
  3752. Configuration->Usage->Inc(L"LocalLocalDeletes");
  3753. }
  3754. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  3755. FProgressSide = FCurrentSide;
  3756. try
  3757. {
  3758. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3759. }
  3760. __finally
  3761. {
  3762. ReloadLocalDirectory();
  3763. }
  3764. }
  3765. FAlternativeDelete = false;
  3766. }
  3767. catch(...)
  3768. {
  3769. FAlternativeDelete = false;
  3770. DView->DiscardSavedSelection();
  3771. throw;
  3772. }
  3773. DView->RestoreSelection();
  3774. }
  3775. //---------------------------------------------------------------------------
  3776. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3777. {
  3778. DebugAssert(Terminal);
  3779. DebugAssert(!IsLocalBrowserMode());
  3780. RemoteDirView->SaveSelection();
  3781. RemoteDirView->SaveSelectedNames();
  3782. try
  3783. {
  3784. if (Lock)
  3785. {
  3786. Terminal->LockFiles(FileList);
  3787. }
  3788. else
  3789. {
  3790. Terminal->UnlockFiles(FileList);
  3791. }
  3792. }
  3793. catch(...)
  3794. {
  3795. RemoteDirView->DiscardSavedSelection();
  3796. throw;
  3797. }
  3798. RemoteDirView->RestoreSelection();
  3799. }
  3800. //---------------------------------------------------------------------------
  3801. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3802. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3803. bool NoConfirmation, bool Move)
  3804. {
  3805. DebugAssert(Terminal != NULL);
  3806. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  3807. DebugAssert(!IsLocalBrowserMode());
  3808. if (Session == NULL)
  3809. {
  3810. Session = Terminal;
  3811. }
  3812. if (Session == Terminal)
  3813. {
  3814. if (RemoteDriveView->DropTarget != NULL)
  3815. {
  3816. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3817. }
  3818. else if (RemoteDirView->DropTarget != NULL)
  3819. {
  3820. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3821. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3822. }
  3823. else
  3824. {
  3825. Target = RemoteDirView->Path;
  3826. }
  3827. }
  3828. else
  3829. {
  3830. Target = GetSessionPath(Session, osRemote);
  3831. }
  3832. Target = UnixIncludeTrailingBackslash(Target);
  3833. if (FileList->Count == 1)
  3834. {
  3835. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3836. }
  3837. else
  3838. {
  3839. FileMask = AnyMask;
  3840. }
  3841. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  3842. bool Result = true;
  3843. if (!NoConfirmation)
  3844. {
  3845. bool Multi = (FileList->Count > 1);
  3846. if (Move)
  3847. {
  3848. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3849. }
  3850. else
  3851. {
  3852. std::unique_ptr<TStrings> Sessions(new TStringList);
  3853. std::unique_ptr<TStrings> Directories(new TStringList);
  3854. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  3855. for (int Index = 0; Index < SessionList->Count; Index++)
  3856. {
  3857. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  3858. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  3859. {
  3860. Sessions->AddObject(SessionList->Strings[Index], ASession);
  3861. Directories->Add(GetSessionPath(ASession, osRemote));
  3862. }
  3863. }
  3864. TDirectRemoteCopy AllowDirectCopy;
  3865. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  3866. {
  3867. DebugAssert(DirectCopy);
  3868. AllowDirectCopy = drcAllow;
  3869. }
  3870. else if (Terminal->IsCapable[fcSecondaryShell])
  3871. {
  3872. DebugAssert(DirectCopy);
  3873. AllowDirectCopy = drcConfirmCommandSession;
  3874. }
  3875. else
  3876. {
  3877. DebugAssert(!DirectCopy);
  3878. AllowDirectCopy = drcDisallow;
  3879. }
  3880. void * ASession = Session;
  3881. Result = DoRemoteCopyDialog(
  3882. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal);
  3883. Session = static_cast<TManagedTerminal *>(ASession);
  3884. }
  3885. }
  3886. return Result;
  3887. }
  3888. //---------------------------------------------------------------------------
  3889. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3890. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3891. {
  3892. DebugAssert(!IsLocalBrowserMode());
  3893. bool DirectCopy;
  3894. UnicodeString Target, FileMask;
  3895. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3896. if (Result)
  3897. {
  3898. if (!Move && !DirectCopy)
  3899. {
  3900. Configuration->Usage->Inc("RemoteCopyTemp");
  3901. UnicodeString RootTempDir;
  3902. UnicodeString TempDir;
  3903. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3904. TStrings * TemporaryFilesList = new TStringList();
  3905. try
  3906. {
  3907. TMakeLocalFileListParams MakeFileListParam;
  3908. MakeFileListParam.FileList = TemporaryFilesList;
  3909. MakeFileListParam.FileTimes = NULL;
  3910. MakeFileListParam.IncludeDirs = true;
  3911. MakeFileListParam.Recursive = false;
  3912. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  3913. TTerminalManager::Instance()->ActiveSession = Session;
  3914. if (TemporaryFilesList->Count > 0)
  3915. {
  3916. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3917. CopyParam.FileMask = FileMask;
  3918. DebugAssert(!FAutoOperation);
  3919. FAutoOperation = true;
  3920. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3921. }
  3922. }
  3923. __finally
  3924. {
  3925. delete TemporaryFilesList;
  3926. FAutoOperation = false;
  3927. if (!RootTempDir.IsEmpty())
  3928. {
  3929. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3930. }
  3931. }
  3932. }
  3933. else
  3934. {
  3935. RemoteDirView->SaveSelection();
  3936. RemoteDirView->SaveSelectedNames();
  3937. try
  3938. {
  3939. if (Move)
  3940. {
  3941. Configuration->Usage->Inc("RemoteMove");
  3942. Terminal->MoveFiles(FileList, Target, FileMask);
  3943. }
  3944. else
  3945. {
  3946. Configuration->Usage->Inc("RemoteCopyDirect");
  3947. DebugAssert(DirectCopy);
  3948. DebugAssert(Session == Terminal);
  3949. if (Terminal->IsCapable[fcRemoteCopy] ||
  3950. Terminal->CommandSessionOpened ||
  3951. CommandSessionFallback())
  3952. {
  3953. Terminal->CopyFiles(FileList, Target, FileMask);
  3954. }
  3955. }
  3956. }
  3957. catch(...)
  3958. {
  3959. RemoteDirView->DiscardSavedSelection();
  3960. throw;
  3961. }
  3962. RemoteDirView->RestoreSelection();
  3963. }
  3964. }
  3965. return Result;
  3966. }
  3967. //---------------------------------------------------------------------------
  3968. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3969. const UnicodeString & Path, TRemoteProperties & Properties)
  3970. {
  3971. DebugAssert(!IsLocalBrowserMode());
  3972. Properties.Valid = Properties.Valid << vpEncrypt;
  3973. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3974. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3975. }
  3976. //---------------------------------------------------------------------------
  3977. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3978. {
  3979. Side = GetSide(Side);
  3980. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3981. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  3982. UnicodeString Name = LoadStr(NEW_FOLDER);
  3983. int AllowedChanges =
  3984. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  3985. bool SaveSettings = false;
  3986. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3987. {
  3988. TWindowLock Lock(this);
  3989. if (!IsSideLocalBrowser(Side))
  3990. {
  3991. if (SaveSettings)
  3992. {
  3993. GUIConfiguration->NewDirectoryProperties = Properties;
  3994. }
  3995. CreateRemoteDirectory(Name, Properties);
  3996. }
  3997. else
  3998. {
  3999. DirView(Side)->CreateDirectory(Name);
  4000. }
  4001. }
  4002. }
  4003. //---------------------------------------------------------------------------
  4004. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4005. {
  4006. TWindowLock Lock(this);
  4007. DirView(Side)->ExecuteHomeDirectory();
  4008. }
  4009. //---------------------------------------------------------------------------
  4010. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4011. {
  4012. DoOpenDirectoryDialog(odBrowse, Side);
  4013. }
  4014. //---------------------------------------------------------------------------
  4015. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4016. {
  4017. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4018. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4019. bool Result = !Path.IsEmpty();
  4020. if (Result)
  4021. {
  4022. // While we might get here when the session is closed (from location profiles),
  4023. // it's not a problem as the Path setter is noop then
  4024. DirView(Side)->Path = Path;
  4025. }
  4026. return Result;
  4027. }
  4028. //---------------------------------------------------------------------------
  4029. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4030. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4031. bool & Close)
  4032. {
  4033. // terminal can be already closed (e.g. dropped connection)
  4034. if (Terminal != NULL)
  4035. {
  4036. try
  4037. {
  4038. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  4039. }
  4040. catch(...)
  4041. {
  4042. if (!Terminal->Active)
  4043. {
  4044. Close = true;
  4045. }
  4046. throw;
  4047. }
  4048. }
  4049. }
  4050. //---------------------------------------------------------------------------
  4051. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4052. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4053. bool & Close)
  4054. {
  4055. // terminal can be already closed (e.g. dropped connection)
  4056. if (Terminal != NULL)
  4057. {
  4058. Configuration->Usage->Inc(L"ChecksumCalculated");
  4059. try
  4060. {
  4061. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  4062. }
  4063. catch(...)
  4064. {
  4065. if (!Terminal->Active)
  4066. {
  4067. Close = true;
  4068. }
  4069. throw;
  4070. }
  4071. }
  4072. }
  4073. //---------------------------------------------------------------------------
  4074. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4075. {
  4076. bool Result;
  4077. if (!IsSideLocalBrowser(Side))
  4078. {
  4079. TRemoteTokenList * GroupList = NULL;
  4080. TRemoteTokenList * UserList = NULL;
  4081. try
  4082. {
  4083. TRemoteProperties CurrentProperties;
  4084. if (Terminal->LoadFilesProperties(FileList))
  4085. {
  4086. RemoteDirView->Invalidate();
  4087. }
  4088. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4089. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4090. if (CapableGroupChanging || CapableOwnerChanging)
  4091. {
  4092. if (CapableGroupChanging)
  4093. {
  4094. GroupList = Terminal->Groups->Duplicate();
  4095. }
  4096. if (CapableOwnerChanging)
  4097. {
  4098. UserList = Terminal->Users->Duplicate();
  4099. }
  4100. TRemoteDirectory * Files = Terminal->Files;
  4101. int Count = Files->Count;
  4102. if (Count > 100)
  4103. {
  4104. Count = 100;
  4105. }
  4106. for (int Index = 0; Index < Count; Index++)
  4107. {
  4108. TRemoteFile * File = Files->Files[Index];
  4109. if (CapableGroupChanging)
  4110. {
  4111. GroupList->AddUnique(File->Group);
  4112. }
  4113. if (CapableOwnerChanging)
  4114. {
  4115. UserList->AddUnique(File->Owner);
  4116. }
  4117. }
  4118. // if we haven't collected tokens for all files in current directory,
  4119. // make sure we collect them at least for all selected files.
  4120. // (note that so far the files in FileList has to be from current direcotry)
  4121. if (Count < Files->Count)
  4122. {
  4123. for (int Index = 0; Index < FileList->Count; Index++)
  4124. {
  4125. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4126. if (CapableGroupChanging)
  4127. {
  4128. GroupList->AddUnique(File->Group);
  4129. }
  4130. if (CapableOwnerChanging)
  4131. {
  4132. UserList->AddUnique(File->Owner);
  4133. }
  4134. }
  4135. }
  4136. }
  4137. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4138. int Flags = 0;
  4139. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4140. if (CapableOwnerChanging) Flags |= cpOwner;
  4141. if (CapableGroupChanging) Flags |= cpGroup;
  4142. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4143. if (Terminal->IsCapable[fcCalculatingChecksum])
  4144. {
  4145. CalculateChecksumEvent = CalculateChecksum;
  4146. }
  4147. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4148. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4149. TRemoteProperties NewProperties = CurrentProperties;
  4150. Result =
  4151. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4152. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4153. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4154. CalculateSize, CalculateChecksumEvent);
  4155. if (Result)
  4156. {
  4157. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4158. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4159. }
  4160. }
  4161. __finally
  4162. {
  4163. delete GroupList;
  4164. delete UserList;
  4165. }
  4166. }
  4167. else
  4168. {
  4169. DirView(Side)->DisplayPropertiesMenu();
  4170. Result = true;
  4171. }
  4172. return Result;
  4173. }
  4174. //---------------------------------------------------------------------------
  4175. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4176. {
  4177. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4178. {
  4179. FIgnoreNextDialogChar = Key;
  4180. }
  4181. Key = 0;
  4182. }
  4183. //---------------------------------------------------------------------------
  4184. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4185. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4186. {
  4187. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4188. if (Command != NULL)
  4189. {
  4190. KeyProcessed(Key, Shift);
  4191. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4192. {
  4193. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4194. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4195. false, false, const_cast<TCustomCommandType *>(Command));
  4196. }
  4197. }
  4198. }
  4199. //---------------------------------------------------------------------------
  4200. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4201. {
  4202. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4203. {
  4204. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4205. }
  4206. if (!DirView(osCurrent)->IsEditing())
  4207. {
  4208. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4209. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4210. {
  4211. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4212. // But so far it's not a problem, as it does what we want.
  4213. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4214. if (((Action->ShortCut == KeyShortCut) ||
  4215. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4216. AllowedAction(Action, aaShortCut))
  4217. {
  4218. // Has to be called before the action as the dialog char is already in queue.
  4219. // So when the action consumes message queue, we already need to have the
  4220. // FIgnoreNextDialogChar set
  4221. KeyProcessed(Key, Shift);
  4222. // Reset reference to previous component (when menu/toolbar was clicked).
  4223. // Needed to detect that action was invoked by keyboard shortcut
  4224. // in TNonVisualDataModule::ExplorerActionsExecute
  4225. Action->ActionComponent = NULL;
  4226. Action->Execute();
  4227. return;
  4228. }
  4229. }
  4230. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4231. {
  4232. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4233. {
  4234. KeyProcessed(Key, Shift);
  4235. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4236. return;
  4237. }
  4238. }
  4239. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4240. {
  4241. KeyProcessed(Key, Shift);
  4242. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4243. }
  4244. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4245. if (IsCustomShortCut(CustomShortCut))
  4246. {
  4247. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4248. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4249. if (WinConfiguration->SharedBookmarks != NULL)
  4250. {
  4251. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4252. if ((Bookmark != NULL) &&
  4253. OpenBookmark(FCurrentSide, Bookmark))
  4254. {
  4255. KeyProcessed(Key, Shift);
  4256. }
  4257. }
  4258. }
  4259. }
  4260. TForm::KeyDown(Key, Shift);
  4261. }
  4262. //---------------------------------------------------------------------------
  4263. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4264. {
  4265. bool OpenedTerminal = false;
  4266. UnicodeString ProtocolName;
  4267. UnicodeString ProtocolNameHint;
  4268. UnicodeString SecurityProtocolName;
  4269. UnicodeString DurationHint;
  4270. // See the comment in TScpCommanderForm::UpdateControls()
  4271. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4272. if (ActiveTerminal != NULL)
  4273. {
  4274. // Active is not enough here
  4275. if (ActiveTerminal->Status == ssOpened)
  4276. {
  4277. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4278. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4279. if (FileSystemInfo.ProtocolName.IsEmpty())
  4280. {
  4281. ProtocolName = SessionInfo.ProtocolName;
  4282. }
  4283. else
  4284. {
  4285. ProtocolName = FileSystemInfo.ProtocolName;
  4286. }
  4287. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4288. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4289. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4290. OpenedTerminal = true;
  4291. }
  4292. else if (ActiveTerminal->Status > ssClosed)
  4293. {
  4294. ProtocolName = LoadStr(STATUS_CONNECTING);
  4295. }
  4296. else
  4297. {
  4298. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4299. }
  4300. }
  4301. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4302. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4303. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4304. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4305. if (ProtocolStatusPanel->Caption != ProtocolName)
  4306. {
  4307. ProtocolStatusPanel->Caption = ProtocolName;
  4308. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4309. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4310. }
  4311. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4312. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4313. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4314. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4315. SecurityStatusPanel->Enabled = SecurityEnabled;
  4316. UnicodeString SecurityProtocolHint;
  4317. if (SecurityEnabled)
  4318. {
  4319. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4320. }
  4321. else
  4322. {
  4323. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4324. }
  4325. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4326. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4327. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4328. DurationStatusPanel->Hint = DurationHint;
  4329. UpdateStatusBar();
  4330. }
  4331. //---------------------------------------------------------------------------
  4332. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4333. {
  4334. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4335. DebugAssert(SessionStatusBar != NULL);
  4336. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4337. if (!FNote.IsEmpty())
  4338. {
  4339. NoteStatusPanel->Caption = FNote;
  4340. }
  4341. else
  4342. {
  4343. UpdateStatusPanelText(NoteStatusPanel);
  4344. }
  4345. NoteStatusPanel->Hint = FNoteHints;
  4346. UnicodeString LoginTime;
  4347. // See the comment in TScpCommanderForm::UpdateControls()
  4348. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4349. // Active is not enough here
  4350. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4351. {
  4352. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4353. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4354. }
  4355. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4356. DurationStatusPanel->Caption = LoginTime;
  4357. }
  4358. //---------------------------------------------------------------------------
  4359. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4360. {
  4361. Panel->Caption = L"";
  4362. }
  4363. //---------------------------------------------------------------------------
  4364. void __fastcall TCustomScpExplorerForm::Idle()
  4365. {
  4366. if (FShowing || FStandaloneEditing)
  4367. {
  4368. FEditorManager->Check();
  4369. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4370. // that needs to know if queue view is visible (and it may be closed after queue update)
  4371. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4372. }
  4373. if (FShowing)
  4374. {
  4375. if (!NonVisualDataModule->Busy &&
  4376. // Menu is opened or drag&drop is going on
  4377. (Mouse->Capture == NULL))
  4378. {
  4379. if (FRefreshRemoteDirectory)
  4380. {
  4381. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4382. {
  4383. Terminal->RefreshDirectory();
  4384. }
  4385. FRefreshRemoteDirectory = false;
  4386. }
  4387. if (FRefreshLocalDirectory)
  4388. {
  4389. ReloadLocalDirectory();
  4390. FRefreshLocalDirectory = false;
  4391. }
  4392. if (WinConfiguration->RefreshRemotePanel)
  4393. {
  4394. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4395. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4396. {
  4397. RemoteDirView->ReloadDirectory();
  4398. }
  4399. }
  4400. }
  4401. }
  4402. if (FShowing || FStandaloneEditing)
  4403. {
  4404. if (FQueueStatusInvalidated)
  4405. {
  4406. UpdateQueueStatus(false);
  4407. }
  4408. RefreshQueueItems();
  4409. }
  4410. if (FShowing)
  4411. {
  4412. UpdateStatusBar();
  4413. }
  4414. FIgnoreNextDialogChar = 0;
  4415. }
  4416. //---------------------------------------------------------------------------
  4417. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4418. {
  4419. try
  4420. {
  4421. FUserActionTimer->Enabled = false;
  4422. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4423. {
  4424. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4425. {
  4426. FPendingQueueActionItem->ProcessUserAction();
  4427. }
  4428. }
  4429. }
  4430. __finally
  4431. {
  4432. FPendingQueueActionItem = NULL;
  4433. }
  4434. }
  4435. //---------------------------------------------------------------------------
  4436. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4437. {
  4438. if (WinConfiguration->MinimizeToTray)
  4439. {
  4440. UpdateTrayIcon();
  4441. FTrayIcon->Visible = true;
  4442. if (Visible)
  4443. {
  4444. ShowWindow(Handle, SW_HIDE);
  4445. }
  4446. }
  4447. }
  4448. //---------------------------------------------------------------------------
  4449. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4450. {
  4451. // WORKAROUND
  4452. // When restoring maximized window from minimization,
  4453. // rarely some controls do not align properly.
  4454. // Two instances seen (both for Commander):
  4455. // - When restoring, window is temporarily narrower (not maximized),
  4456. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4457. // Once maximized already, top dock shinks back, but the session PageControl,
  4458. // do not align up, leaving space between TopDock and PageControl.
  4459. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4460. for (int Index = 0; Index < ControlCount; Index++)
  4461. {
  4462. RealignControl(Controls[Index]);
  4463. }
  4464. if (FTrayIcon->Visible)
  4465. {
  4466. FTrayIcon->Visible = false;
  4467. if (Visible)
  4468. {
  4469. ShowWindow(Handle, SW_SHOW);
  4470. }
  4471. }
  4472. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4473. {
  4474. FNeedSession = false;
  4475. NonVisualDataModule->StartBusy();
  4476. try
  4477. {
  4478. NeedSession(true);
  4479. }
  4480. __finally
  4481. {
  4482. NonVisualDataModule->EndBusy();
  4483. }
  4484. }
  4485. }
  4486. //---------------------------------------------------------------------------
  4487. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4488. {
  4489. FTrayIcon->Hint = GetMainForm()->Caption;
  4490. }
  4491. //---------------------------------------------------------------------------
  4492. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4493. {
  4494. UpdateTrayIcon();
  4495. }
  4496. //---------------------------------------------------------------------------
  4497. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4498. {
  4499. // workaround
  4500. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4501. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4502. // But WM_ACTIVATEAPP message can be received even when
  4503. // the main window is minimized to the tray and internal editor window is focused
  4504. // (after another application was previously active)
  4505. if (::IsIconic(Handle))
  4506. {
  4507. if (!IsAppIconic())
  4508. {
  4509. SetAppIconic(true);
  4510. }
  4511. }
  4512. ::ApplicationRestore();
  4513. Application->BringToFront();
  4514. }
  4515. //---------------------------------------------------------------------------
  4516. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4517. {
  4518. RestoreApp();
  4519. }
  4520. //---------------------------------------------------------------------------
  4521. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4522. {
  4523. if (OpenInNewWindow())
  4524. {
  4525. ExecuteNewInstance(SessionUrl);
  4526. }
  4527. else
  4528. {
  4529. TTerminalManager::Instance()->NewSession(SessionUrl);
  4530. }
  4531. }
  4532. //---------------------------------------------------------------------------
  4533. void TCustomScpExplorerForm::NewTab(TOperationSide)
  4534. {
  4535. NewSession();
  4536. }
  4537. //---------------------------------------------------------------------------
  4538. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4539. {
  4540. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4541. StoredSessions->NewSession(SessionName, SessionData);
  4542. // modified only, explicit
  4543. StoredSessions->Save(false, true);
  4544. // encode session name because of slashes in hierarchical sessions
  4545. return EncodeUrlString(SessionName);
  4546. }
  4547. //---------------------------------------------------------------------------
  4548. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4549. {
  4550. // current working directories become defaults here, what is not right
  4551. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4552. return SaveHiddenDuplicateSession(SessionData.get());
  4553. }
  4554. //---------------------------------------------------------------------------
  4555. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4556. {
  4557. if (OpenInNewWindow())
  4558. {
  4559. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4560. }
  4561. else
  4562. {
  4563. // current working directories become defaults here, what is not right
  4564. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4565. TTerminalManager * Manager = TTerminalManager::Instance();
  4566. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4567. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4568. // We definitelly want these
  4569. ASession->Disconnected = ManagedSession->Disconnected;
  4570. // Not sure about these two
  4571. ASession->Permanent = ManagedSession->Permanent;
  4572. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4573. Manager->ActiveSession = ASession;
  4574. }
  4575. }
  4576. //---------------------------------------------------------------------------
  4577. void __fastcall TCustomScpExplorerForm::RenameSession()
  4578. {
  4579. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4580. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME) &&
  4581. // When submitting the default name, do not fix it
  4582. (Name != ManagedSession->SessionData->SessionName))
  4583. {
  4584. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4585. TSessionData::ValidateName(Name);
  4586. // This is inconsistent with how color (for example) is handled.
  4587. ManagedSession->SessionData->Name = Name;
  4588. UpdateControls();
  4589. // Add/Remove distinguishing paths from sessions of the same name.
  4590. SessionListChanged();
  4591. }
  4592. }
  4593. //---------------------------------------------------------------------------
  4594. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4595. {
  4596. DebugAssert(Queue != NULL);
  4597. bool Result = Queue->IsEmpty;
  4598. if (!Result)
  4599. {
  4600. SetFocus();
  4601. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4602. }
  4603. return Result;
  4604. }
  4605. //---------------------------------------------------------------------------
  4606. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4607. {
  4608. return CanCloseQueue(FQueue);
  4609. }
  4610. //---------------------------------------------------------------------------
  4611. void __fastcall TCustomScpExplorerForm::CloseSession()
  4612. {
  4613. if (CanCloseQueue())
  4614. {
  4615. TTerminalManager::Instance()->FreeActiveTerminal();
  4616. }
  4617. }
  4618. //---------------------------------------------------------------------------
  4619. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4620. {
  4621. if (CanCloseQueue())
  4622. {
  4623. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4624. }
  4625. }
  4626. //---------------------------------------------------------------------------
  4627. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4628. {
  4629. DetachTerminal(Terminal);
  4630. RemoteDirView->Terminal = Terminal;
  4631. UpdateRemotePathComboBox(false);
  4632. InitStatusBar();
  4633. UpdateControls();
  4634. }
  4635. //---------------------------------------------------------------------------
  4636. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4637. {
  4638. InitStatusBar();
  4639. }
  4640. //---------------------------------------------------------------------------
  4641. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4642. {
  4643. if (DebugAlwaysTrue(!Terminal->Active))
  4644. {
  4645. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4646. }
  4647. }
  4648. //---------------------------------------------------------------------------
  4649. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4650. {
  4651. if (OpenInNewWindow())
  4652. {
  4653. // encode session name because of slashes in hierarchical sessions
  4654. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4655. }
  4656. else
  4657. {
  4658. TTerminalManager * Manager = TTerminalManager::Instance();
  4659. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4660. Manager->ActiveSession = Terminal;
  4661. }
  4662. }
  4663. //---------------------------------------------------------------------------
  4664. void __fastcall TCustomScpExplorerForm::DoOpenFolderOrWorkspace(const UnicodeString & Name, bool ConnectFirstTerminal)
  4665. {
  4666. TTerminalManager * Manager = TTerminalManager::Instance();
  4667. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4668. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4669. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  4670. // FirstSession can be null, if some of the
  4671. if (!ConnectFirstTerminal && (FirstSession != NULL))
  4672. {
  4673. FirstSession->Disconnected = true;
  4674. FirstSession->DisconnectedTemporarily = true;
  4675. }
  4676. Manager->ActiveSession = FirstSession;
  4677. }
  4678. //---------------------------------------------------------------------------
  4679. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4680. {
  4681. if (OpenInNewWindow())
  4682. {
  4683. ExecuteNewInstance(Name);
  4684. }
  4685. else
  4686. {
  4687. DoOpenFolderOrWorkspace(Name, true);
  4688. }
  4689. }
  4690. //---------------------------------------------------------------------------
  4691. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4692. bool &CanClose)
  4693. {
  4694. // See the comment in CloseApp()
  4695. if (NonVisualDataModule->Busy)
  4696. {
  4697. CanClose = false;
  4698. }
  4699. else
  4700. {
  4701. TTerminalManager * Manager = TTerminalManager::Instance();
  4702. int ActiveSessions = 0;
  4703. for (int Index = 0; Index < Manager->Count; Index++)
  4704. {
  4705. if (Manager->Sessions[Index]->Active)
  4706. {
  4707. ActiveSessions++;
  4708. }
  4709. }
  4710. // Confirm, when there's at least one active remote session or
  4711. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4712. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4713. WinConfiguration->ConfirmClosingSession)
  4714. {
  4715. unsigned int Result;
  4716. TMessageParams Params(mpNeverAskAgainCheck);
  4717. UnicodeString Message;
  4718. int Answers = qaOK | qaCancel;
  4719. // The current session is the only active session
  4720. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4721. {
  4722. if (!WinConfiguration->AutoSaveWorkspace)
  4723. {
  4724. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4725. }
  4726. else
  4727. {
  4728. Message = LoadStr(CLOSE_SESSION);
  4729. }
  4730. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4731. }
  4732. // Multiple active sessions or one active session, but it's not the current one
  4733. else if (ActiveSessions > 0)
  4734. {
  4735. if (!WinConfiguration->AutoSaveWorkspace)
  4736. {
  4737. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4738. }
  4739. else
  4740. {
  4741. Message = LoadStr(CLOSE_SESSIONS);
  4742. }
  4743. }
  4744. // Multiple tabs, but all are intactive or local, and saving of worspace is not enabled
  4745. else
  4746. {
  4747. DebugAlwaysTrue(Manager->Count > 1);
  4748. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  4749. Message = LoadStr(CLOSE_WORKSPACE);
  4750. }
  4751. UnicodeString Note;
  4752. if (WinConfiguration->AutoSaveWorkspace)
  4753. {
  4754. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4755. }
  4756. else
  4757. {
  4758. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4759. Answers = qaYes | qaNo | qaCancel;
  4760. }
  4761. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4762. SetFocus();
  4763. Result = MessageDialog(Message, qtConfirmation,
  4764. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4765. if (Result == qaNeverAskAgain)
  4766. {
  4767. WinConfiguration->ConfirmClosingSession = false;
  4768. }
  4769. if (Result == qaNo)
  4770. {
  4771. CanClose = SaveWorkspace(true);
  4772. // note that the workspace will be saved redundatly again from FormClose
  4773. }
  4774. else
  4775. {
  4776. CanClose =
  4777. (Result == qaOK) ||
  4778. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4779. (Result == qaNeverAskAgain);
  4780. }
  4781. }
  4782. if (CanClose && HasActiveTerminal())
  4783. {
  4784. CanClose = CanCloseQueue();
  4785. }
  4786. }
  4787. if (CanClose)
  4788. {
  4789. CanClose =
  4790. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4791. FEditorManager->CloseExternalFilesWithoutProcess();
  4792. if (CanClose)
  4793. {
  4794. while (CanClose && (FLocalEditors->Count > 0))
  4795. {
  4796. int PrevCount = FLocalEditors->Count;
  4797. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4798. CanClose = (FLocalEditors->Count < PrevCount);
  4799. }
  4800. if (CanClose)
  4801. {
  4802. CanClose = FEditorManager->Empty(true);
  4803. if (!CanClose)
  4804. {
  4805. SetFocus();
  4806. CanClose =
  4807. (MessageDialog(
  4808. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4809. }
  4810. }
  4811. }
  4812. }
  4813. }
  4814. //---------------------------------------------------------------------------
  4815. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4816. {
  4817. TForm * Form = dynamic_cast<TForm *>(Sender);
  4818. DebugAssert(Form != NULL);
  4819. Form->Close();
  4820. }
  4821. //---------------------------------------------------------------------------
  4822. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4823. {
  4824. TForm * Form = dynamic_cast<TForm *>(Sender);
  4825. delete Form;
  4826. }
  4827. //---------------------------------------------------------------------------
  4828. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4829. {
  4830. while (FLocalEditors->Count > 0)
  4831. {
  4832. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4833. }
  4834. }
  4835. //---------------------------------------------------------------------------
  4836. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4837. TObject *Sender)
  4838. {
  4839. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4840. try
  4841. {
  4842. SetProperties(osRemote, FileList);
  4843. }
  4844. __finally
  4845. {
  4846. delete FileList;
  4847. }
  4848. }
  4849. //---------------------------------------------------------------------------
  4850. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4851. {
  4852. if (value)
  4853. {
  4854. if (Component == fcCustomCommandsBand)
  4855. {
  4856. UpdateCustomCommandsToolbar();
  4857. }
  4858. if (Component == fcQueueFileList)
  4859. {
  4860. DebugAssert(!QueueFileListSplitter->Visible);
  4861. AdjustQueueLayout();
  4862. UpdateQueueFileList();
  4863. }
  4864. }
  4865. }
  4866. //---------------------------------------------------------------------------
  4867. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  4868. {
  4869. int MinHeightLimit =
  4870. GetStaticQueuePanelComponentsHeight() +
  4871. QueueFileListSplitter->Height +
  4872. QueueFileList->Height +
  4873. GetMinQueueViewHeight();
  4874. if (QueuePanel->ClientHeight < MinHeightLimit)
  4875. {
  4876. QueuePanel->ClientHeight = MinHeightLimit;
  4877. }
  4878. }
  4879. //---------------------------------------------------------------------------
  4880. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4881. {
  4882. TControl * Control = GetComponent(Component);
  4883. DebugAssert(Control);
  4884. bool Changed = (Control->Visible != value);
  4885. if (Changed)
  4886. {
  4887. ComponentShowing(Component, value);
  4888. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4889. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4890. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4891. if (value)
  4892. {
  4893. int RemainingHeight = Control->Parent->ClientHeight;
  4894. int RemainingWidth = Control->Parent->ClientWidth;
  4895. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4896. {
  4897. TControl * ChildControl = Control->Parent->Controls[i];
  4898. if (ChildControl->Visible)
  4899. {
  4900. switch (ChildControl->Align)
  4901. {
  4902. case alTop:
  4903. case alBottom:
  4904. RemainingHeight -= ChildControl->Height;
  4905. break;
  4906. case alLeft:
  4907. case alRight:
  4908. RemainingWidth -= ChildControl->Width;
  4909. break;
  4910. }
  4911. }
  4912. }
  4913. int Reserve = ScaleByTextHeight(this, 32);
  4914. // queue in explorer, trees in commander
  4915. if (Control->Height > RemainingHeight - Reserve)
  4916. {
  4917. Control->Height = RemainingHeight / 2;
  4918. }
  4919. if (Control->Width > RemainingWidth - Reserve)
  4920. {
  4921. Control->Width = RemainingWidth / 2;
  4922. }
  4923. }
  4924. Control->Visible = value;
  4925. if (WasFocused && Visible)
  4926. {
  4927. DirView(osCurrent)->SetFocus();
  4928. }
  4929. FixControlsPlacement();
  4930. }
  4931. }
  4932. //---------------------------------------------------------------------------
  4933. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4934. {
  4935. TControl * Control = GetComponent(Component);
  4936. if (Control == NULL)
  4937. {
  4938. return false;
  4939. }
  4940. else
  4941. {
  4942. return Control->Visible;
  4943. }
  4944. }
  4945. //---------------------------------------------------------------------------
  4946. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4947. {
  4948. bool Result;
  4949. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4950. {
  4951. Result = !IsUWP();
  4952. }
  4953. else
  4954. {
  4955. Result = true;
  4956. }
  4957. return Result;
  4958. }
  4959. //---------------------------------------------------------------------------
  4960. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  4961. {
  4962. if (DirView->ItemFocused != NULL)
  4963. {
  4964. DirView->ItemFocused->MakeVisible(false);
  4965. }
  4966. }
  4967. //---------------------------------------------------------------------------
  4968. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4969. {
  4970. MakeFocusedItemVisible(RemoteDirView);
  4971. QueueSplitter->Visible = QueuePanel->Visible;
  4972. QueueFileListSplitter->Visible = QueueFileList->Visible;
  4973. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4974. TControl * QueueControlsOrder[] =
  4975. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  4976. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  4977. }
  4978. //---------------------------------------------------------------------------
  4979. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4980. {
  4981. switch (Component) {
  4982. case fcStatusBar: return RemoteStatusBar;
  4983. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4984. case fcQueueView: return QueuePanel;
  4985. case fcQueueToolbar: return QueueDock;
  4986. case fcRemoteTree: return RemoteDrivePanel;
  4987. case fcSessionsTabs: return SessionsPageControl;
  4988. case fcQueueFileList: return QueueFileList;
  4989. default: return NULL;
  4990. }
  4991. }
  4992. //---------------------------------------------------------------------------
  4993. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4994. TObject *Sender, TListColumn *Column, TPoint &Point)
  4995. {
  4996. DebugAssert(NonVisualDataModule && Column && Sender);
  4997. NonVisualDataModule->ListColumn = Column;
  4998. TPopupMenu * DirViewColumnMenu;
  4999. if (Sender == DirView(osOther))
  5000. {
  5001. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5002. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  5003. (Column->Index == uvName);
  5004. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  5005. (Column->Index == uvSize);
  5006. }
  5007. else
  5008. {
  5009. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5010. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  5011. (Column->Index == dvName);
  5012. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  5013. (Column->Index == dvSize);
  5014. }
  5015. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5016. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5017. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5018. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5019. }
  5020. //---------------------------------------------------------------------------
  5021. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5022. TObject *Sender, TListItem *Item, bool &AllowExec)
  5023. {
  5024. DoDirViewExecFile(Sender, Item, AllowExec);
  5025. }
  5026. //---------------------------------------------------------------------------
  5027. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5028. TListItem * Item, bool & AllowExec)
  5029. {
  5030. DebugAssert(Sender && Item && Configuration);
  5031. DebugAssert(AllowExec);
  5032. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5033. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5034. bool Remote = !IsSideLocalBrowser(Side);
  5035. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles();
  5036. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  5037. // on files only (not directories)
  5038. // and only when symlinks are resolved (apply to remote panel only)
  5039. if (!ADirView->ItemIsDirectory(Item) &&
  5040. (ResolvedSymlinks || FForceExecution))
  5041. {
  5042. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  5043. !FForceExecution && ResolvedSymlinks)
  5044. {
  5045. if (WinConfiguration->DoubleClickAction == dcaCopy)
  5046. {
  5047. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5048. UnlockWindow();
  5049. try
  5050. {
  5051. if (IsLocalBrowserMode())
  5052. {
  5053. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5054. }
  5055. else
  5056. {
  5057. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5058. }
  5059. }
  5060. __finally
  5061. {
  5062. LockWindow();
  5063. }
  5064. AllowExec = false;
  5065. }
  5066. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  5067. {
  5068. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5069. {
  5070. ExecuteFile(Side, efDefaultEditor);
  5071. AllowExec = false;
  5072. }
  5073. }
  5074. else
  5075. {
  5076. DebugFail();
  5077. }
  5078. }
  5079. // if we have not done anything special, fall back to default behavior
  5080. if (AllowExec)
  5081. {
  5082. if (Remote && !WinConfiguration->DisableOpenEdit)
  5083. {
  5084. ExecuteFile(osRemote, efShell);
  5085. AllowExec = false;
  5086. }
  5087. }
  5088. }
  5089. }
  5090. //---------------------------------------------------------------------------
  5091. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5092. {
  5093. return ((Side == osRemote) || (Side == osCurrent));
  5094. }
  5095. //---------------------------------------------------------------------------
  5096. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5097. {
  5098. DebugFail();
  5099. }
  5100. //---------------------------------------------------------------------------
  5101. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5102. {
  5103. DebugFail();
  5104. }
  5105. //---------------------------------------------------------------------------
  5106. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5107. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5108. {
  5109. TSynchronizeParamType Params;
  5110. Params.LocalDirectory = LocalDirectory;
  5111. Params.RemoteDirectory = RemoteDirectory;
  5112. int UnusedParams =
  5113. (GUIConfiguration->SynchronizeParams &
  5114. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5115. TTerminal::spCaseSensitive));
  5116. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5117. Params.Options = GUIConfiguration->SynchronizeOptions;
  5118. bool SaveSettings = false;
  5119. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5120. &DoSynchronizeTooManyDirectories);
  5121. DebugAssert(FSynchronizeController == NULL);
  5122. FSynchronizeController = &Controller;
  5123. bool Result;
  5124. try
  5125. {
  5126. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5127. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5128. int Options =
  5129. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5130. DebugAssert(FOnFeedSynchronizeError == NULL);
  5131. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5132. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5133. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  5134. if (Result)
  5135. {
  5136. if (SaveSettings)
  5137. {
  5138. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5139. GUIConfiguration->SynchronizeOptions = Params.Options;
  5140. }
  5141. else
  5142. {
  5143. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5144. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5145. FLAGSET(Params.Options, soSynchronize))
  5146. {
  5147. GUIConfiguration->SynchronizeOptions =
  5148. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5149. soSynchronize;
  5150. }
  5151. }
  5152. LocalDirectory = Params.LocalDirectory;
  5153. RemoteDirectory = Params.RemoteDirectory;
  5154. }
  5155. }
  5156. __finally
  5157. {
  5158. FSynchronizeController = NULL;
  5159. DebugAssert(FOnFeedSynchronizeError == NULL);
  5160. FOnFeedSynchronizeError = NULL;
  5161. }
  5162. return Result;
  5163. }
  5164. //---------------------------------------------------------------------------
  5165. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5166. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5167. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5168. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5169. TSynchronizeOptions * Options, bool Full)
  5170. {
  5171. // Keep-up-to-date mode
  5172. if (Terminal->Status == ssOpened)
  5173. {
  5174. try
  5175. {
  5176. int PParams = Params.Params;
  5177. if (!Full)
  5178. {
  5179. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5180. if (FLAGSET(Params.Options, soRecurse))
  5181. {
  5182. PParams |= TTerminal::spSubDirs;
  5183. }
  5184. }
  5185. else
  5186. {
  5187. // if keepuptodate is non-recursive,
  5188. // full sync before has to be non-recursive as well
  5189. if (FLAGCLEAR(Params.Options, soRecurse))
  5190. {
  5191. PParams |= TTerminal::spNoRecurse;
  5192. }
  5193. }
  5194. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5195. PParams, Checklist, Options);
  5196. }
  5197. catch (Exception & E)
  5198. {
  5199. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  5200. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5201. {
  5202. // noop, already logged in MoreMessageDialog
  5203. }
  5204. else
  5205. {
  5206. ShowExtendedExceptionEx(Terminal, &E);
  5207. throw;
  5208. }
  5209. }
  5210. }
  5211. }
  5212. //---------------------------------------------------------------------------
  5213. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5214. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5215. const UnicodeString ErrorStr)
  5216. {
  5217. if (!Directory.IsEmpty())
  5218. {
  5219. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5220. }
  5221. else
  5222. {
  5223. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5224. }
  5225. }
  5226. //---------------------------------------------------------------------------
  5227. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5228. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5229. {
  5230. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5231. {
  5232. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5233. }
  5234. else
  5235. {
  5236. TMessageParams Params(mpNeverAskAgainCheck);
  5237. unsigned int Result = MessageDialog(
  5238. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5239. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5240. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5241. {
  5242. MaxDirectories *= 2;
  5243. if (Result == qaNeverAskAgain)
  5244. {
  5245. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5246. }
  5247. }
  5248. else
  5249. {
  5250. Abort();
  5251. }
  5252. }
  5253. }
  5254. //---------------------------------------------------------------------------
  5255. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5256. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5257. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5258. TSynchronizeOptions * Options)
  5259. {
  5260. // Keep-up-to-date mode
  5261. DebugAssert(!FAutoOperation);
  5262. void * BatchStorage;
  5263. BatchStart(BatchStorage);
  5264. FAutoOperation = true;
  5265. bool AnyOperation = false;
  5266. TDateTime StartTime = Now();
  5267. TSynchronizeChecklist * AChecklist = NULL;
  5268. try
  5269. {
  5270. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5271. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5272. {
  5273. FSynchronizeProgressForm->Start();
  5274. }
  5275. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5276. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5277. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5278. Options);
  5279. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5280. SAFE_DESTROY(FSynchronizeProgressForm);
  5281. AnyOperation = (AChecklist->CheckedCount > 0);
  5282. if (AnyOperation)
  5283. {
  5284. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5285. CreateProgressForm(&SynchronizeProgress);
  5286. try
  5287. {
  5288. Terminal->SynchronizeApply(
  5289. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5290. SynchronizeProcessedItem, NULL, NULL, NULL);
  5291. }
  5292. __finally
  5293. {
  5294. DestroyProgressForm();
  5295. }
  5296. }
  5297. }
  5298. __finally
  5299. {
  5300. if (Checklist == NULL)
  5301. {
  5302. delete AChecklist;
  5303. }
  5304. else
  5305. {
  5306. *Checklist = AChecklist;
  5307. }
  5308. FAutoOperation = false;
  5309. SAFE_DESTROY(FSynchronizeProgressForm);
  5310. BatchEnd(BatchStorage);
  5311. ReloadLocalDirectory();
  5312. if (AnyOperation)
  5313. {
  5314. OperationComplete(StartTime);
  5315. }
  5316. }
  5317. }
  5318. //---------------------------------------------------------------------------
  5319. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5320. {
  5321. // can be called from command line
  5322. return Visible &&
  5323. ((DirView(osRemote)->SelCount > 0) ||
  5324. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5325. }
  5326. //---------------------------------------------------------------------------
  5327. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5328. {
  5329. LogSynchronizeEvent(Terminal, Message);
  5330. }
  5331. //---------------------------------------------------------------------------
  5332. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5333. int Params, TSynchronizeOptions & Options)
  5334. {
  5335. DebugAssert(!IsLocalBrowserMode());
  5336. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5337. {
  5338. Options.Filter = new TStringList();
  5339. Options.Filter->CaseSensitive = false;
  5340. Options.Filter->Duplicates = Types::dupAccept;
  5341. if (DirView(osRemote)->SelCount > 0)
  5342. {
  5343. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5344. }
  5345. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5346. {
  5347. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5348. }
  5349. Options.Filter->Sort();
  5350. }
  5351. }
  5352. //---------------------------------------------------------------------------
  5353. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5354. {
  5355. TCopyParamType Defaults;
  5356. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5357. ConfigStorage->AccessMode = smRead;
  5358. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5359. {
  5360. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5361. }
  5362. ConfigStorage.reset(NULL);
  5363. std::unique_ptr<TStringList> Options(new TStringList());
  5364. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5365. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5366. UnicodeString Result;
  5367. if (Options->Count > 0)
  5368. {
  5369. Result =
  5370. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5371. }
  5372. return Result;
  5373. }
  5374. //---------------------------------------------------------------------------
  5375. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5376. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5377. {
  5378. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5379. UnicodeString AdditionalParams =
  5380. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5381. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5382. SerializeCopyParamForCommandLine(CopyParams),
  5383. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5384. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5385. ExecuteNewInstance(SessionName, AdditionalParams);
  5386. }
  5387. //---------------------------------------------------------------------------
  5388. struct TSynchronizeParams
  5389. {
  5390. UnicodeString LocalDirectory;
  5391. UnicodeString RemoteDirectory;
  5392. TSynchronizeMode Mode;
  5393. TSynchronizeChecklist * Checklist;
  5394. int Params;
  5395. TCopyParamType * CopyParam;
  5396. TDateTime CollectElapsed;
  5397. TDateTime * StartTime;
  5398. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5399. };
  5400. //---------------------------------------------------------------------------
  5401. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5402. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5403. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5404. {
  5405. DebugAssert(!FAutoOperation);
  5406. void * BatchStorage;
  5407. BatchStart(BatchStorage);
  5408. FAutoOperation = true;
  5409. TFileOperationStatistics Statistics;
  5410. TDateTime Start = Now();
  5411. try
  5412. {
  5413. Params.OnProcessedItem = OnProcessedItem;
  5414. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5415. CreateProgressForm(&SynchronizeProgress);
  5416. Terminal->SynchronizeApply(
  5417. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5418. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5419. &Statistics);
  5420. }
  5421. __finally
  5422. {
  5423. Params.OnProcessedItem = NULL;
  5424. FAutoOperation = false;
  5425. DestroyProgressForm();
  5426. BatchEnd(BatchStorage);
  5427. ReloadLocalDirectory();
  5428. }
  5429. if (WinConfiguration->SynchronizeSummary)
  5430. {
  5431. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5432. // The statistics should be 0 anyway in this case
  5433. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5434. {
  5435. Message += L"\n";
  5436. if (Statistics.FilesUploaded > 0)
  5437. {
  5438. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5439. }
  5440. if (Statistics.FilesDownloaded > 0)
  5441. {
  5442. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5443. }
  5444. if (Statistics.FilesDeletedLocal > 0)
  5445. {
  5446. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5447. }
  5448. if (Statistics.FilesDeletedRemote > 0)
  5449. {
  5450. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5451. }
  5452. }
  5453. TDateTime Elapsed = (Now() - Start);
  5454. Message +=
  5455. L"\n" +
  5456. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5457. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5458. TMessageParams Params(mpNeverAskAgainCheck);
  5459. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5460. if (Result == qaNeverAskAgain)
  5461. {
  5462. WinConfiguration->SynchronizeSummary = false;
  5463. }
  5464. }
  5465. }
  5466. //---------------------------------------------------------------------------
  5467. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5468. {
  5469. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5470. {
  5471. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5472. }
  5473. // Not set in keep-up-to-date mode - Synchronize() method
  5474. if (Token != NULL)
  5475. {
  5476. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5477. if (Params.OnProcessedItem != NULL)
  5478. {
  5479. Params.OnProcessedItem(NULL, ChecklistItem);
  5480. }
  5481. }
  5482. }
  5483. //---------------------------------------------------------------------------
  5484. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5485. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5486. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5487. {
  5488. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5489. *Params.StartTime = Now();
  5490. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5491. }
  5492. //---------------------------------------------------------------------------
  5493. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5494. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5495. {
  5496. // terminal can be already closed (e.g. dropped connection)
  5497. if (Terminal != NULL)
  5498. {
  5499. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5500. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5501. }
  5502. }
  5503. //---------------------------------------------------------------------------
  5504. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5505. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5506. {
  5507. DebugAssert(!IsLocalBrowserMode());
  5508. TAutoBatch AutoBatch(this);
  5509. TAutoFlag AutoOperationFlag(FAutoOperation);
  5510. if (Side == osRemote)
  5511. {
  5512. std::unique_ptr<TStrings> FileList(new TStringList());
  5513. FileList->AddObject(FileName, RemoteFile);
  5514. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5515. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5516. RemoteDirView->SaveSelection();
  5517. RemoteDirView->SaveSelectedNames();
  5518. try
  5519. {
  5520. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5521. }
  5522. catch(...)
  5523. {
  5524. RemoteDirView->DiscardSavedSelection();
  5525. throw;
  5526. }
  5527. RemoteDirView->RestoreSelection();
  5528. }
  5529. else if (DebugAlwaysTrue(Side == osLocal))
  5530. {
  5531. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5532. {
  5533. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5534. }
  5535. UnicodeString Directory = ExtractFileDir(FileName);
  5536. ReloadLocalDirectory(Directory);
  5537. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5538. if (!SamePaths(Directory, NewDirectory))
  5539. {
  5540. ReloadLocalDirectory(NewDirectory);
  5541. }
  5542. }
  5543. }
  5544. //---------------------------------------------------------------------------
  5545. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5546. {
  5547. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5548. if (Side == osLocal)
  5549. {
  5550. if (Action == TSynchronizeChecklist::saDownloadNew)
  5551. {
  5552. OpenFolderInExplorer(LocalPath);
  5553. }
  5554. else
  5555. {
  5556. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5557. }
  5558. }
  5559. else if (DebugAlwaysTrue(Side == osRemote))
  5560. {
  5561. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5562. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5563. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5564. if (!LocalPath.IsEmpty())
  5565. {
  5566. SessionData->LocalDirectory = LocalPath;
  5567. }
  5568. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5569. ExecuteNewInstance(SessionName, FORMAT(L"%s=%s", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5570. }
  5571. }
  5572. //---------------------------------------------------------------------------
  5573. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5574. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5575. const TCopyParamType * CopyParams)
  5576. {
  5577. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5578. UnicodeString AdditionalParams =
  5579. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5580. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5581. SerializeCopyParamForCommandLine(CopyParams),
  5582. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5583. LocalDirectory, RemoteDirectory, Mode, Params));
  5584. ExecuteNewInstance(SessionName, AdditionalParams);
  5585. }
  5586. //---------------------------------------------------------------------------
  5587. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5588. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5589. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5590. {
  5591. int Result;
  5592. bool SaveSettings = false;
  5593. int Options =
  5594. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5595. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5596. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5597. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5598. bool Continue =
  5599. (UseDefaults == 0) ||
  5600. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5601. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5602. if (Continue)
  5603. {
  5604. Configuration->Usage->Inc(L"Synchronizations");
  5605. UpdateCopyParamCounters(CopyParam);
  5606. TSynchronizeOptions SynchronizeOptions;
  5607. GetSynchronizeOptions(Params, SynchronizeOptions);
  5608. if (SaveSettings)
  5609. {
  5610. GUIConfiguration->SynchronizeParams = Params;
  5611. }
  5612. else
  5613. {
  5614. SaveMode = false;
  5615. }
  5616. TDateTime StartTime = Now();
  5617. TSynchronizeChecklist * Checklist = NULL;
  5618. try
  5619. {
  5620. DebugAssert(!FAutoOperation);
  5621. FAutoOperation = true;
  5622. try
  5623. {
  5624. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5625. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5626. int Files = -1;
  5627. if (DataList->Count >= 1)
  5628. {
  5629. Files = StrToIntDef(DataList->Strings[0], Files);
  5630. }
  5631. else
  5632. {
  5633. DataList->Add(UnicodeString());
  5634. }
  5635. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5636. FSynchronizeProgressForm->Start();
  5637. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5638. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5639. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5640. &SynchronizeOptions);
  5641. if (Terminal->SessionData->CacheDirectories)
  5642. {
  5643. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5644. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5645. }
  5646. }
  5647. __finally
  5648. {
  5649. FAutoOperation = false;
  5650. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5651. SAFE_DESTROY(FSynchronizeProgressForm);
  5652. }
  5653. TSynchronizeParams SynchronizeParams;
  5654. SynchronizeParams.LocalDirectory = LocalDirectory;
  5655. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5656. SynchronizeParams.Mode = Mode;
  5657. SynchronizeParams.CopyParam = &CopyParam;
  5658. SynchronizeParams.Params = Params;
  5659. SynchronizeParams.Checklist = Checklist;
  5660. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5661. SynchronizeParams.StartTime = &StartTime;
  5662. SynchronizeParams.OnProcessedItem = NULL;
  5663. Result = Checklist->Count;
  5664. if (Checklist->Count > 0)
  5665. {
  5666. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5667. {
  5668. if (!DoSynchronizeChecklistDialog(
  5669. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5670. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5671. {
  5672. Result = -1;
  5673. }
  5674. }
  5675. else
  5676. {
  5677. FullSynchronize(SynchronizeParams, NULL, NULL);
  5678. }
  5679. }
  5680. else
  5681. {
  5682. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5683. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5684. }
  5685. }
  5686. __finally
  5687. {
  5688. delete Checklist;
  5689. }
  5690. OperationComplete(StartTime);
  5691. }
  5692. else
  5693. {
  5694. Result = -1;
  5695. }
  5696. return Result;
  5697. }
  5698. //---------------------------------------------------------------------------
  5699. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5700. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5701. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5702. {
  5703. if (Collect)
  5704. {
  5705. DebugAssert(FSynchronizeProgressForm != NULL);
  5706. if (!FSynchronizeProgressForm->Started)
  5707. {
  5708. FSynchronizeProgressForm->Start();
  5709. }
  5710. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5711. SetTaskbarListProgressValue(CompareProgress);
  5712. }
  5713. else
  5714. {
  5715. DebugAssert(FSynchronizeProgressForm == NULL);
  5716. DebugAssert(FProgressForm != NULL);
  5717. }
  5718. }
  5719. //---------------------------------------------------------------------------
  5720. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5721. {
  5722. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  5723. TRemoteFile * File = NULL;
  5724. Terminal->ReadFile(FullFileName, File);
  5725. if (File != NULL)
  5726. {
  5727. std::unique_ptr<TRemoteFile> FileOwner(File);
  5728. TAutoFlag Flag(FStandaloneEditing);
  5729. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5730. Application->ShowMainForm = false;
  5731. Application->Run();
  5732. }
  5733. }
  5734. //---------------------------------------------------------------------------
  5735. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  5736. {
  5737. DebugFail();
  5738. }
  5739. //---------------------------------------------------------------------------
  5740. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5741. {
  5742. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5743. SessionData->Assign(ManagedSession->SessionData);
  5744. UpdateSessionData(SessionData.get());
  5745. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  5746. if (IsActiveTerminal(ManagedSession))
  5747. {
  5748. ManagedSession->UpdateSessionCredentials(SessionData.get());
  5749. }
  5750. if (!ManagedSession->SessionData->HasSessionName())
  5751. {
  5752. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5753. // once IsWorkspace is cleared
  5754. SessionData->Name = UnicodeString();
  5755. SessionData->IsWorkspace = false;
  5756. }
  5757. return SessionData.release();
  5758. }
  5759. //---------------------------------------------------------------------------
  5760. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5761. {
  5762. TSessionData * SessionData = CloneCurrentSessionData();
  5763. try
  5764. {
  5765. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5766. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5767. }
  5768. __finally
  5769. {
  5770. delete SessionData;
  5771. }
  5772. }
  5773. //---------------------------------------------------------------------------
  5774. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5775. {
  5776. TTerminalManager * Manager = TTerminalManager::Instance();
  5777. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5778. if (DebugAlwaysTrue(ManagedSession != NULL))
  5779. {
  5780. // Update ManagedSession->StateData
  5781. UpdateSession(ManagedSession);
  5782. }
  5783. Manager->SaveWorkspace(DataList.get());
  5784. return DataList.release();
  5785. }
  5786. //---------------------------------------------------------------------------
  5787. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5788. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5789. {
  5790. WinConfiguration->LastWorkspace = Name;
  5791. if (!SavePasswords)
  5792. {
  5793. for (int Index = 0; Index < DataList->Count; Index++)
  5794. {
  5795. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5796. if (SessionData->Link.IsEmpty())
  5797. {
  5798. SessionData->ClearSessionPasswords();
  5799. }
  5800. }
  5801. }
  5802. StoredSessions->NewWorkspace(Name, DataList);
  5803. // modified only
  5804. StoredSessions->Save(false, Explicit);
  5805. }
  5806. //---------------------------------------------------------------------------
  5807. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5808. {
  5809. return
  5810. DefaultStr(WinConfiguration->LastWorkspace,
  5811. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5812. }
  5813. //---------------------------------------------------------------------------
  5814. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5815. {
  5816. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5817. bool AnyNonStoredSessionWithPassword = false;
  5818. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5819. bool AllNonStoredSessionsAnonymous = true;
  5820. TTerminalManager * Manager = TTerminalManager::Instance();
  5821. for (int Index = 0; Index < DataList->Count; Index++)
  5822. {
  5823. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5824. if (SessionData->Link.IsEmpty())
  5825. {
  5826. if (SessionData->HasAnySessionPassword())
  5827. {
  5828. AnyNonStoredSessionWithPassword = true;
  5829. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  5830. {
  5831. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5832. }
  5833. }
  5834. if (!SameText(SessionData->UserName, AnonymousUserName))
  5835. {
  5836. AllNonStoredSessionsAnonymous = false;
  5837. }
  5838. }
  5839. }
  5840. bool SavePasswords;
  5841. bool * PSavePasswords;
  5842. bool NotRecommendedSavingPasswords =
  5843. !CustomWinConfiguration->UseMasterPassword &&
  5844. !AllNonStoredSessionsAnonymous;
  5845. if (Configuration->DisablePasswordStoring ||
  5846. !AnyNonStoredSessionWithPassword)
  5847. {
  5848. PSavePasswords = NULL;
  5849. SavePasswords = false;
  5850. }
  5851. else
  5852. {
  5853. PSavePasswords = &SavePasswords;
  5854. SavePasswords =
  5855. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5856. !NotRecommendedSavingPasswords;
  5857. }
  5858. UnicodeString Name = WorkspaceName();
  5859. bool CreateShortcut = false;
  5860. bool Result =
  5861. DoSaveWorkspaceDialog(
  5862. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5863. EnableAutoSave);
  5864. if (Result)
  5865. {
  5866. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5867. if (CreateShortcut)
  5868. {
  5869. TOperationVisualizer Visualizer;
  5870. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5871. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5872. }
  5873. if (EnableAutoSave)
  5874. {
  5875. WinConfiguration->AutoSaveWorkspace = true;
  5876. WinConfiguration->AutoWorkspace = Name;
  5877. if (PSavePasswords != NULL)
  5878. {
  5879. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5880. }
  5881. }
  5882. }
  5883. return Result;
  5884. }
  5885. //---------------------------------------------------------------------------
  5886. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  5887. {
  5888. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  5889. DebugAssert(Session == ManagedSession);
  5890. if (!IsLocalBrowserMode())
  5891. {
  5892. SAFE_DESTROY(Session->RemoteExplorerState);
  5893. if (WinConfiguration->PreservePanelState)
  5894. {
  5895. Session->RemoteExplorerState = RemoteDirView->SaveState();
  5896. }
  5897. }
  5898. UpdateSessionData(Session->StateData);
  5899. }
  5900. //---------------------------------------------------------------------------
  5901. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5902. {
  5903. // Keep in sync with TSessionData::CopyStateData
  5904. DebugAssert(Data != NULL);
  5905. // This is inconsistent with how color (for example) is handled.
  5906. Data->Name = ManagedSession->SessionData->Name;
  5907. if (!IsLocalBrowserMode())
  5908. {
  5909. // cannot use RemoteDirView->Path, because it is empty if connection
  5910. // was already closed
  5911. // also only peek, we may not be connected at all atm
  5912. // so make sure we do not try retrieving current directory from the server
  5913. // (particularly with FTP)
  5914. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5915. if (!ACurrentDirectory.IsEmpty())
  5916. {
  5917. Data->RemoteDirectory = ACurrentDirectory;
  5918. }
  5919. }
  5920. Data->Color = SessionColor;
  5921. }
  5922. //---------------------------------------------------------------------------
  5923. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5924. {
  5925. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5926. DebugAssert(Toolbar != NULL);
  5927. for (int i = 0; i < Toolbar->Items->Count; i++)
  5928. {
  5929. TTBXCustomDropDownItem * DropDownItem;
  5930. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5931. if (DropDownItem != NULL)
  5932. {
  5933. ToolbarItemResize(DropDownItem,
  5934. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5935. Toolbar->NonClientWidth);
  5936. break;
  5937. }
  5938. }
  5939. }
  5940. //---------------------------------------------------------------------------
  5941. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5942. {
  5943. Item->EditWidth = Width;
  5944. }
  5945. //---------------------------------------------------------------------------
  5946. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5947. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5948. {
  5949. for (int i = 0; i < Toolbar->Items->Count; i++)
  5950. {
  5951. TTBXCustomDropDownItem * DropDownItem;
  5952. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5953. if (DropDownItem != NULL)
  5954. {
  5955. ASize.x -= DropDownItem->EditWidth;
  5956. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5957. }
  5958. }
  5959. }
  5960. //---------------------------------------------------------------------------
  5961. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5962. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5963. {
  5964. if (WinConfiguration->DDWarnLackOfTempSpace)
  5965. {
  5966. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5967. if (!ADrive.IsEmpty())
  5968. {
  5969. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5970. DebugAssert(RequiredSpace >= 0);
  5971. __int64 RequiredWithReserve;
  5972. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5973. if (FreeSpace < RequiredWithReserve)
  5974. {
  5975. unsigned int Result;
  5976. TMessageParams Params(mpNeverAskAgainCheck);
  5977. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5978. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5979. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5980. if (Result == qaNeverAskAgain)
  5981. {
  5982. WinConfiguration->DDWarnLackOfTempSpace = false;
  5983. }
  5984. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5985. }
  5986. }
  5987. }
  5988. }
  5989. //---------------------------------------------------------------------------
  5990. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5991. {
  5992. DoOpenDirectoryDialog(odAddBookmark, Side);
  5993. }
  5994. //---------------------------------------------------------------------------
  5995. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5996. {
  5997. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5998. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  5999. try
  6000. {
  6001. TCustomDirView * DView = DirView(Side);
  6002. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6003. {
  6004. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6005. }
  6006. }
  6007. catch (...)
  6008. {
  6009. delete VisitedDirectories;
  6010. throw;
  6011. }
  6012. return VisitedDirectories;
  6013. }
  6014. //---------------------------------------------------------------------------
  6015. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6016. TOpenDirectoryMode Mode, TOperationSide Side)
  6017. {
  6018. bool Continue = true;
  6019. if (Mode == odAddBookmark)
  6020. {
  6021. TMessageParams Params(mpNeverAskAgainCheck);
  6022. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6023. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6024. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6025. unsigned int Answer =
  6026. MessageDialog(Message,
  6027. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6028. if (Answer == qaNeverAskAgain)
  6029. {
  6030. Continue = true;
  6031. WinConfiguration->UseSharedBookmarks = true;
  6032. }
  6033. else if (Answer == qaOK)
  6034. {
  6035. Continue = true;
  6036. WinConfiguration->UseSharedBookmarks = false;
  6037. }
  6038. else
  6039. {
  6040. Continue = false;
  6041. }
  6042. }
  6043. if (Continue)
  6044. {
  6045. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6046. try
  6047. {
  6048. UnicodeString Name = DirView(Side)->PathName;
  6049. bool Repeat;
  6050. do
  6051. {
  6052. Repeat = false;
  6053. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6054. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6055. // do not allow switching to location profiles,
  6056. // if we are not connected
  6057. HasDirView[osLocal] && (Terminal != NULL)))
  6058. {
  6059. TWindowLock Lock(this);
  6060. if (!TryOpenDirectory(Side, Name))
  6061. {
  6062. Repeat = true;
  6063. Mode = odBrowse;
  6064. }
  6065. }
  6066. }
  6067. while (Repeat);
  6068. }
  6069. __finally
  6070. {
  6071. delete VisitedDirectories;
  6072. }
  6073. }
  6074. }
  6075. //---------------------------------------------------------------------------
  6076. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6077. {
  6078. DebugAssert(!Terminal->CommandSessionOpened);
  6079. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6080. }
  6081. //---------------------------------------------------------------------------
  6082. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6083. {
  6084. bool Result = Terminal->IsCapable[Capability] ||
  6085. Terminal->CommandSessionOpened;
  6086. if (!Result)
  6087. {
  6088. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6089. if (!GUIConfiguration->ConfirmCommandSession)
  6090. {
  6091. Result = true;
  6092. }
  6093. else
  6094. {
  6095. TMessageParams Params(mpNeverAskAgainCheck);
  6096. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6097. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6098. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6099. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6100. if (Answer == qaNeverAskAgain)
  6101. {
  6102. GUIConfiguration->ConfirmCommandSession = false;
  6103. Result = true;
  6104. }
  6105. else if (Answer == qaOK)
  6106. {
  6107. Result = true;
  6108. }
  6109. }
  6110. if (Result)
  6111. {
  6112. Result = CommandSessionFallback();
  6113. }
  6114. }
  6115. return Result;
  6116. }
  6117. //---------------------------------------------------------------------------
  6118. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6119. {
  6120. if (EnsureCommandSessionFallback(fcAnyCommand))
  6121. {
  6122. DoConsoleDialog(Terminal, Command);
  6123. }
  6124. }
  6125. //---------------------------------------------------------------------------
  6126. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6127. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6128. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6129. {
  6130. if (!IsSideLocalBrowser(osRemote) &&
  6131. IsFileControl(DropSourceControl, osRemote) &&
  6132. (FDDExtMapFile != NULL))
  6133. {
  6134. Accept = true;
  6135. }
  6136. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6137. DebugAssert(FDDTargetControl != NULL);
  6138. }
  6139. //---------------------------------------------------------------------------
  6140. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6141. _di_IDataObject DataObj, int KeyState,
  6142. const TPoint & Point, int & Effect, bool & Accept)
  6143. {
  6144. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6145. }
  6146. //---------------------------------------------------------------------------
  6147. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6148. _di_IDataObject DataObj, int KeyState,
  6149. const TPoint & Point, int & Effect, bool & Accept)
  6150. {
  6151. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6152. }
  6153. //---------------------------------------------------------------------------
  6154. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6155. TObject * Sender)
  6156. {
  6157. DebugUsedParam(Sender);
  6158. DebugAssert(FDDTargetControl == Sender);
  6159. FDDTargetControl = NULL;
  6160. }
  6161. //---------------------------------------------------------------------------
  6162. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6163. {
  6164. FileControlDDDragLeave(SessionsPageControl);
  6165. }
  6166. //---------------------------------------------------------------------------
  6167. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6168. {
  6169. FileControlDDDragLeave(QueueView3);
  6170. }
  6171. //---------------------------------------------------------------------------
  6172. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6173. {
  6174. DebugAssert(!IsSideLocalBrowser(Side));
  6175. DebugUsedParam(Side);
  6176. bool Edit = false;
  6177. TRemoteFile * File = NULL;
  6178. UnicodeString FileName;
  6179. UnicodeString PointTo;
  6180. bool SymbolicLink = true;
  6181. if (RemoteDirView->ItemFocused)
  6182. {
  6183. DebugAssert(RemoteDirView->ItemFocused->Data);
  6184. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6185. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6186. if (Edit)
  6187. {
  6188. FileName = File->FileName;
  6189. PointTo = File->LinkTo;
  6190. }
  6191. else
  6192. {
  6193. PointTo = File->FileName;
  6194. }
  6195. }
  6196. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6197. Terminal->IsCapable[fcHardLink]))
  6198. {
  6199. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6200. if (Edit)
  6201. {
  6202. DebugAssert(File->FileName == FileName);
  6203. int Params = dfNoRecursive;
  6204. Terminal->ExceptionOnFail = true;
  6205. try
  6206. {
  6207. Terminal->DeleteFile(L"", File, &Params);
  6208. }
  6209. __finally
  6210. {
  6211. Terminal->ExceptionOnFail = false;
  6212. }
  6213. }
  6214. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6215. }
  6216. }
  6217. //---------------------------------------------------------------------------
  6218. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6219. {
  6220. return
  6221. (IsSideLocalBrowser(Side) ||
  6222. (HasActiveTerminal() &&
  6223. Terminal->ResolvingSymlinks &&
  6224. Terminal->IsCapable[fcSymbolicLink]));
  6225. }
  6226. //---------------------------------------------------------------------------
  6227. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6228. {
  6229. return
  6230. !IsSideLocalBrowser(FCurrentSide) &&
  6231. (RemoteDirView->ItemFocused != NULL) &&
  6232. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6233. Terminal->SessionData->ResolveSymlinks;
  6234. }
  6235. //---------------------------------------------------------------------------
  6236. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6237. {
  6238. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6239. FForceExecution = true;
  6240. try
  6241. {
  6242. DirView(osCurrent)->ExecuteCurrentFile();
  6243. }
  6244. __finally
  6245. {
  6246. FForceExecution = false;
  6247. }
  6248. }
  6249. //---------------------------------------------------------------------------
  6250. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6251. {
  6252. TEditorData ExternalEditor;
  6253. ExternalEditor.Editor = edExternal;
  6254. bool Remember = false;
  6255. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6256. {
  6257. if (Remember)
  6258. {
  6259. TEditorList * EditorList = new TEditorList;
  6260. try
  6261. {
  6262. *EditorList = *WinConfiguration->EditorList;
  6263. bool Found = false;
  6264. int i = 0;
  6265. while (!Found && (i < EditorList->Count))
  6266. {
  6267. const TEditorPreferences * Editor = EditorList->Editors[i];
  6268. if ((Editor->Data->Editor == edExternal) &&
  6269. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6270. {
  6271. Found = true;
  6272. }
  6273. i++;
  6274. }
  6275. if (!Found)
  6276. {
  6277. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6278. WinConfiguration->EditorList = EditorList;
  6279. }
  6280. }
  6281. __finally
  6282. {
  6283. delete EditorList;
  6284. }
  6285. }
  6286. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6287. }
  6288. }
  6289. //---------------------------------------------------------------------------
  6290. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6291. {
  6292. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6293. if (FFileFindTerminal == ATerminal)
  6294. {
  6295. FFileFindTerminal = NULL;
  6296. HideFileFindDialog();
  6297. }
  6298. if (FClipboardTerminal == ATerminal)
  6299. {
  6300. ClipboardClear(); // implies ClipboardStop
  6301. }
  6302. }
  6303. //---------------------------------------------------------------------------
  6304. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6305. {
  6306. DetachTerminal(Sender);
  6307. }
  6308. //---------------------------------------------------------------------------
  6309. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6310. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6311. {
  6312. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6313. DebugAssert(Terminal != NULL);
  6314. if (Data->Terminal == Terminal)
  6315. {
  6316. Data->Terminal = NULL;
  6317. }
  6318. }
  6319. //---------------------------------------------------------------------------
  6320. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6321. {
  6322. return NULL;
  6323. }
  6324. //---------------------------------------------------------------------------
  6325. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6326. {
  6327. UpdateControls();
  6328. SessionColor = TColor(0);
  6329. UpdateRemotePathComboBox(false);
  6330. NeedSession(false);
  6331. }
  6332. //---------------------------------------------------------------------------
  6333. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6334. {
  6335. try
  6336. {
  6337. // Cache, as the login dialog can change its value.
  6338. // See also DoShow. Also see TTerminalManager::NewSession.
  6339. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6340. try
  6341. {
  6342. if (ShowLogin)
  6343. {
  6344. bool ReloadSessions = !Startup;
  6345. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6346. }
  6347. }
  6348. __finally
  6349. {
  6350. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6351. // (so there was no chance for the user to open any session yet)
  6352. if ((ShowLogin || !Startup) &&
  6353. !WinConfiguration->KeepOpenWhenNoSession &&
  6354. // this rules out the implicit Commander's local-local browser
  6355. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6356. {
  6357. TerminateApplication();
  6358. }
  6359. }
  6360. }
  6361. catch (EAbort &)
  6362. {
  6363. // swallow
  6364. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6365. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6366. }
  6367. }
  6368. //---------------------------------------------------------------------------
  6369. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6370. {
  6371. TTerminalManager * Manager = TTerminalManager::Instance();
  6372. DebugAssert(!Manager->Updating);
  6373. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6374. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6375. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6376. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6377. try
  6378. {
  6379. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6380. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6381. {
  6382. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6383. }
  6384. for (int Index = 0; Index <= Manager->Count; Index++)
  6385. {
  6386. TThemeTabSheet * TabSheet;
  6387. if (Index >= SessionsPageControl->PageCount)
  6388. {
  6389. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6390. TabSheet->PageControl = SessionsPageControl;
  6391. }
  6392. else
  6393. {
  6394. TabSheet = SessionsPageControl->Pages[Index];
  6395. }
  6396. bool IsSessionTab = (Index < Manager->Count);
  6397. if (IsSessionTab)
  6398. {
  6399. TTerminal * Terminal = Manager->Sessions[Index];
  6400. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6401. TabSheet->CaptionTruncation = tttEllipsis;
  6402. UpdateSessionTab(TabSheet);
  6403. }
  6404. else
  6405. {
  6406. TabSheet->Tag = 0; // not really needed
  6407. TabSheet->Shadowed = false;
  6408. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6409. TabSheet->CaptionTruncation = tttNoText;
  6410. // We know that we are at the last page, otherwise we could not call this (it assumes that new session tab is the last one)
  6411. UpdateNewTabTab();
  6412. }
  6413. }
  6414. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6415. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6416. {
  6417. SessionsPageControl->UpdateTabsCaptionTruncation();
  6418. }
  6419. }
  6420. __finally
  6421. {
  6422. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6423. }
  6424. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6425. }
  6426. //---------------------------------------------------------------------------
  6427. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6428. {
  6429. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6430. }
  6431. //---------------------------------------------------------------------------
  6432. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6433. {
  6434. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6435. UnicodeString TabCaption;
  6436. if (WinConfiguration->SelectiveToolbarText)
  6437. {
  6438. TabCaption = GetNewTabTabCaption();
  6439. }
  6440. TabSheet->BaseCaption = TabCaption;
  6441. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6442. }
  6443. //---------------------------------------------------------------------------
  6444. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6445. {
  6446. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6447. }
  6448. //---------------------------------------------------------------------------
  6449. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6450. {
  6451. DebugFail();
  6452. return UnicodeString();
  6453. }
  6454. //---------------------------------------------------------------------------
  6455. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6456. {
  6457. if (DebugAlwaysTrue(TabSheet != NULL))
  6458. {
  6459. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6460. if (DebugAlwaysTrue(ASession != NULL))
  6461. {
  6462. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6463. if (ASession->LocalBrowser)
  6464. {
  6465. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6466. }
  6467. else
  6468. {
  6469. TabSheet->ImageIndex = AddSessionColor(Color);
  6470. }
  6471. TTerminalManager * Manager = TTerminalManager::Instance();
  6472. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6473. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6474. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6475. }
  6476. }
  6477. }
  6478. //---------------------------------------------------------------------------
  6479. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6480. {
  6481. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6482. }
  6483. //---------------------------------------------------------------------------
  6484. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6485. {
  6486. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6487. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6488. bool Result = (Session != NULL);
  6489. if (Result)
  6490. {
  6491. TTerminalManager::Instance()->ActiveSession = Session;
  6492. }
  6493. else
  6494. {
  6495. try
  6496. {
  6497. NewTab();
  6498. }
  6499. __finally
  6500. {
  6501. SessionListChanged();
  6502. }
  6503. FSessionsPageControlNewTabTime = Now();
  6504. }
  6505. return Result;
  6506. }
  6507. //---------------------------------------------------------------------------
  6508. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6509. {
  6510. SessionTabSwitched();
  6511. }
  6512. //---------------------------------------------------------------------------
  6513. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6514. {
  6515. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6516. DebugAssert(TransferList != NULL);
  6517. UnicodeString Name;
  6518. if (TransferList->ItemIndex <= 0)
  6519. {
  6520. Name = L"";
  6521. }
  6522. else
  6523. {
  6524. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6525. }
  6526. if (FCopyParamAutoSelected.IsEmpty())
  6527. {
  6528. // if previous preset was not autoselected, make new preset the "default"
  6529. FCopyParamDefault = Name;
  6530. }
  6531. GUIConfiguration->CopyParamCurrent = Name;
  6532. }
  6533. //---------------------------------------------------------------------------
  6534. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6535. {
  6536. // sanity check
  6537. bool ExistingPreset =
  6538. (FTransferListHoverIndex >= 0) &&
  6539. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6540. DebugAssert(ExistingPreset);
  6541. if (ExistingPreset)
  6542. {
  6543. HDC DC = GetDC(0);
  6544. TCanvas * Canvas = new TCanvas();
  6545. try
  6546. {
  6547. Canvas->Handle = DC;
  6548. Canvas->Font = GetToolbarFont(this);
  6549. UnicodeString Name;
  6550. if (FTransferListHoverIndex == 0)
  6551. {
  6552. Name = L"";
  6553. }
  6554. else
  6555. {
  6556. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6557. }
  6558. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6559. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6560. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6561. static_cast<TObject*>(GetComponent(fcTransferList)));
  6562. UnicodeString InfoStr =
  6563. GUIConfiguration->CopyParamPreset[Name].
  6564. GetInfoStr(L"; ",
  6565. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6566. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6567. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6568. {
  6569. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6570. {
  6571. InfoStr.SetLength(InfoStr.Length() - 1);
  6572. }
  6573. InfoStr += Ellipsis;
  6574. }
  6575. // UpdateCaption does not cause invalidation of whole submenu, while
  6576. // setting Caption property does.
  6577. // also it probably does not resize the label, even if necessary
  6578. // (we do not want that anyway)
  6579. TransferLabel->UpdateCaption(InfoStr);
  6580. }
  6581. __finally
  6582. {
  6583. Canvas->Handle = NULL;
  6584. ReleaseDC(0, DC);
  6585. delete Canvas;
  6586. }
  6587. }
  6588. }
  6589. //---------------------------------------------------------------------------
  6590. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6591. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6592. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6593. {
  6594. if (FTransferListHoverIndex != AHoverIndex)
  6595. {
  6596. FTransferListHoverIndex = AHoverIndex;
  6597. UpdateTransferLabel();
  6598. }
  6599. }
  6600. //---------------------------------------------------------------------------
  6601. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6602. {
  6603. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6604. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6605. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6606. {
  6607. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6608. {
  6609. if (!NonVisualDataModule->Busy)
  6610. {
  6611. OpenDirectory(GetSide(osCurrent));
  6612. }
  6613. Message.Result = 1;
  6614. }
  6615. else
  6616. {
  6617. TForm::Dispatch(&Message);
  6618. }
  6619. }
  6620. else
  6621. {
  6622. TForm::Dispatch(&Message);
  6623. }
  6624. }
  6625. //---------------------------------------------------------------------------
  6626. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6627. {
  6628. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6629. if ((FIgnoreNextDialogChar != 0) &&
  6630. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6631. {
  6632. Message.Result = 1;
  6633. }
  6634. else
  6635. {
  6636. TForm::Dispatch(&Message);
  6637. }
  6638. FIgnoreNextDialogChar = 0;
  6639. }
  6640. //---------------------------------------------------------------------------
  6641. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6642. {
  6643. // The four low-order bits are used internally by Windows
  6644. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6645. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6646. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6647. // 61730 - restore thru double click - undocumented
  6648. // 61490 - maximize thru double click - undocumented
  6649. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6650. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6651. {
  6652. SysResizing(Cmd);
  6653. }
  6654. TForm::Dispatch(&Message);
  6655. }
  6656. //---------------------------------------------------------------------------
  6657. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6658. {
  6659. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6660. // Also there no point returning TRUE as we are not able to
  6661. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6662. // Hence the process termination might be safer :)
  6663. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6664. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6665. {
  6666. Message.Result = FALSE;
  6667. }
  6668. else
  6669. {
  6670. Message.Result = TRUE;
  6671. }
  6672. // Do not call default handling as that triggers OnCloseQuery,
  6673. // where our implementation will popup confirmation dialogs, what we do not want,
  6674. // as per Vista guidelines:
  6675. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6676. }
  6677. //---------------------------------------------------------------------------
  6678. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6679. {
  6680. if (Message.EndSession && IsApplicationMinimized())
  6681. {
  6682. // WORKAROUND
  6683. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6684. // when it receives WM_ENDSESSION,
  6685. // but that sometimes (particularly when application is minimized) cause crashes.
  6686. // Crash popups message that blocks log off.
  6687. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6688. // so we call ExitProcess() immediately, not even trying to cleanup.
  6689. // It still causes beep, so there's likely some popup, but it does not block
  6690. // log off.
  6691. ExitProcess(0);
  6692. }
  6693. TForm::Dispatch(&Message);
  6694. }
  6695. #ifdef _DEBUG
  6696. //---------------------------------------------------------------------------
  6697. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6698. {
  6699. TForm::Dispatch(&Message);
  6700. }
  6701. #endif
  6702. //---------------------------------------------------------------------------
  6703. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6704. {
  6705. }
  6706. //---------------------------------------------------------------------------
  6707. void __fastcall TCustomScpExplorerForm::DoShow()
  6708. {
  6709. // Called from TCustomScpExplorerForm::CMShowingChanged
  6710. // only now are the controls resized finally, so the size constraints
  6711. // will not conflict with possibly very small window size
  6712. RestoreFormParams();
  6713. // This indicates that the form was rescaled after parameters were loaded and
  6714. // as most probably the parameters were actually saved with the current form scaling
  6715. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6716. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6717. // and no rounding problems will occur.
  6718. // See also RestoreForm.
  6719. // (we should be safe to call RestoreParams unconditionally)
  6720. if (PixelsPerInch != Screen->PixelsPerInch)
  6721. {
  6722. RestoreParams();
  6723. }
  6724. FixControlsPlacement();
  6725. if (Position == poDefaultPosOnly)
  6726. {
  6727. CutFormToDesktop(this);
  6728. }
  6729. TForm::DoShow();
  6730. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6731. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6732. // This was previously in NeedSession, where other related code is.
  6733. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  6734. // unpleasanly shows briefly.
  6735. if ((ManagedSession == NULL) &&
  6736. !WinConfiguration->ShowLoginWhenNoSession &&
  6737. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6738. // This detects if workspace was saved the last time the main widow was closed
  6739. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6740. {
  6741. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false);
  6742. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6743. }
  6744. // Before main window displays
  6745. if (ManagedSession == NULL)
  6746. {
  6747. StartingWithoutSession();
  6748. }
  6749. UpdatePixelsPerInchMainWindowCounter();
  6750. FShowing = true;
  6751. }
  6752. //---------------------------------------------------------------------------
  6753. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6754. {
  6755. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6756. }
  6757. //---------------------------------------------------------------------------
  6758. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  6759. {
  6760. SessionListChanged();
  6761. InitStatusBar();
  6762. UpdateControls();
  6763. }
  6764. //---------------------------------------------------------------------------
  6765. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6766. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6767. TNotifyEvent OnBalloonClick, TObject * UserData)
  6768. {
  6769. bool Do;
  6770. UnicodeString Message;
  6771. if (E == NULL)
  6772. {
  6773. Message = Str;
  6774. Do = true;
  6775. }
  6776. else
  6777. {
  6778. Do = ExceptionMessage(E, Message);
  6779. }
  6780. if (Do && WinConfiguration->BalloonNotifications)
  6781. {
  6782. UnicodeString Title;
  6783. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6784. {
  6785. Message = TrimLeft(Message);
  6786. }
  6787. else
  6788. {
  6789. Message = UnformatMessage(Message);
  6790. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6791. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6792. Title = LoadResourceString(Captions[Type]);
  6793. if (Terminal != NULL)
  6794. {
  6795. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  6796. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  6797. }
  6798. }
  6799. if (Seconds == 0)
  6800. {
  6801. Seconds = WinConfiguration->NotificationsTimeout;
  6802. }
  6803. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6804. }
  6805. }
  6806. //---------------------------------------------------------------------------
  6807. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6808. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6809. UnicodeString HelpKeyword, const TMessageParams * Params,
  6810. TTerminal * Terminal)
  6811. {
  6812. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6813. (FOnFeedSynchronizeError != NULL)) &&
  6814. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6815. {
  6816. if (FOnFeedSynchronizeError != NULL)
  6817. {
  6818. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6819. }
  6820. else
  6821. {
  6822. DebugAssert(FErrorList != NULL);
  6823. TStringList * MoreMessagesCopy = NULL;
  6824. if (MoreMessages)
  6825. {
  6826. MoreMessagesCopy = new TStringList();
  6827. MoreMessagesCopy->Assign(MoreMessages);
  6828. }
  6829. FErrorList->AddObject(Message, MoreMessagesCopy);
  6830. }
  6831. PopupTrayBalloon(Terminal, Message, Type);
  6832. return ContinueAnswer(Answers);
  6833. }
  6834. else
  6835. {
  6836. bool UseBalloon = FTrayIcon->Visible;
  6837. if (UseBalloon)
  6838. {
  6839. PopupTrayBalloon(Terminal, Message, Type);
  6840. }
  6841. unsigned int Result;
  6842. try
  6843. {
  6844. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6845. }
  6846. __finally
  6847. {
  6848. // cancel only balloon we popped up, otherwise we may cancel notification
  6849. // balloon that was there before the message dialog
  6850. // (such as "dd confirmation opt in balloon)
  6851. if (UseBalloon)
  6852. {
  6853. FTrayIcon->CancelBalloon();
  6854. }
  6855. }
  6856. return Result;
  6857. }
  6858. }
  6859. //---------------------------------------------------------------------------
  6860. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6861. TTerminal * Terminal, Exception * E)
  6862. {
  6863. if (FTrayIcon->Visible)
  6864. {
  6865. PopupTrayBalloon(Terminal, L"", qtError, E);
  6866. }
  6867. // particularly prevent opening new session from jump list,
  6868. // while exception is shown
  6869. NonVisualDataModule->StartBusy();
  6870. try
  6871. {
  6872. ShowExtendedExceptionEx(Terminal, E);
  6873. }
  6874. __finally
  6875. {
  6876. NonVisualDataModule->EndBusy();
  6877. FTrayIcon->CancelBalloon();
  6878. }
  6879. }
  6880. //---------------------------------------------------------------------------
  6881. void __fastcall TCustomScpExplorerForm::SessionReady()
  6882. {
  6883. InitStatusBar();
  6884. // cannot rely on active page being page for active terminal,
  6885. // as it can happen that active page is the "new session" page
  6886. // (e.g. when reconnecting active terminal, while login dialog
  6887. // invoked from "new session" page is modal)
  6888. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  6889. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  6890. }
  6891. //---------------------------------------------------------------------------
  6892. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6893. TTerminal * Terminal, Exception * E)
  6894. {
  6895. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6896. if (!Terminal->Active)
  6897. {
  6898. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6899. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6900. {
  6901. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6902. }
  6903. }
  6904. }
  6905. //---------------------------------------------------------------------------
  6906. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6907. UnicodeString Message, TQueryType Type,
  6908. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6909. {
  6910. if ((E == NULL) ||
  6911. ExceptionMessage(E, Message))
  6912. {
  6913. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6914. if (Important)
  6915. {
  6916. Seconds *= 5;
  6917. }
  6918. UnicodeString NoteMessage(UnformatMessage(Message));
  6919. if (Terminal != NULL)
  6920. {
  6921. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  6922. NoteMessage = FORMAT(L"%s: %s",
  6923. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  6924. }
  6925. if (WinConfiguration->BalloonNotifications)
  6926. {
  6927. AddNote(NoteMessage);
  6928. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6929. }
  6930. else
  6931. {
  6932. FlashOnBackground();
  6933. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6934. }
  6935. }
  6936. }
  6937. //---------------------------------------------------------------------------
  6938. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6939. {
  6940. RestoreApp();
  6941. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6942. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6943. !NonVisualDataModule->Busy)
  6944. {
  6945. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6946. TTerminalManager::Instance()->ActiveSession = Terminal;
  6947. if (!ComponentVisible[fcQueueView])
  6948. {
  6949. ToggleQueueVisibility();
  6950. GoToQueue();
  6951. }
  6952. }
  6953. }
  6954. //---------------------------------------------------------------------------
  6955. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6956. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6957. {
  6958. UnicodeString Message;
  6959. TNotifyEvent OnClick = NULL;
  6960. TObject * UserData = NULL;
  6961. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6962. switch (Event)
  6963. {
  6964. case qeEmptyButMonitored:
  6965. if ((ATerminal != Terminal) || QueueInvisible)
  6966. {
  6967. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6968. OnClick = QueueEmptyNoteClicked;
  6969. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6970. TerminalNoteData->Terminal = ATerminal;
  6971. UserData = TerminalNoteData;
  6972. }
  6973. break;
  6974. case qeEmpty:
  6975. OperationComplete(ATerminal->QueueOperationStart);
  6976. break;
  6977. case qePendingUserAction:
  6978. if ((ATerminal != Terminal) ||
  6979. (QueueInvisible && !IsQueueAutoPopup()))
  6980. {
  6981. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6982. }
  6983. break;
  6984. default:
  6985. DebugFail();
  6986. }
  6987. if (!Message.IsEmpty())
  6988. {
  6989. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6990. }
  6991. }
  6992. //---------------------------------------------------------------------------
  6993. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6994. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6995. {
  6996. if (FDDExtMapFile != NULL)
  6997. {
  6998. CloseHandle(FDDExtMapFile);
  6999. FDDExtMapFile = NULL;
  7000. }
  7001. if (WinConfiguration->DDFakeFile)
  7002. {
  7003. DDFakeFileInitDrag(FileList, Created);
  7004. }
  7005. }
  7006. //---------------------------------------------------------------------------
  7007. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7008. {
  7009. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7010. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7011. {
  7012. FFakeFileDropTarget = FileName;
  7013. }
  7014. }
  7015. //---------------------------------------------------------------------------
  7016. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7017. {
  7018. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7019. if (!ForceDirectories(ApiPath(Result)))
  7020. {
  7021. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7022. }
  7023. FileSetAttr(ApiPath(Result), faHidden);
  7024. return Result;
  7025. }
  7026. //---------------------------------------------------------------------------
  7027. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7028. bool & Created)
  7029. {
  7030. FFakeFileDropTarget = UnicodeString();
  7031. FDragFakeDirectory = CreateFakeTransferDirectory();
  7032. FileList->AddItem(NULL, FDragFakeDirectory);
  7033. Created = true;
  7034. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7035. {
  7036. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7037. }
  7038. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7039. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7040. {
  7041. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7042. TDragExtCommStruct* CommStruct;
  7043. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7044. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7045. DebugAssert(CommStruct != NULL);
  7046. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7047. CommStruct->Dragging = true;
  7048. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7049. LENOF(CommStruct->DropDest));
  7050. NULL_TERMINATE(CommStruct->DropDest);
  7051. UnmapViewOfFile(CommStruct);
  7052. }
  7053. }
  7054. //---------------------------------------------------------------------------
  7055. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7056. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7057. {
  7058. bool Result;
  7059. DebugAssert(!IsLocalBrowserMode());
  7060. if (Sender == RemoteDirView)
  7061. {
  7062. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7063. }
  7064. else
  7065. {
  7066. DebugAssert(Sender == RemoteDriveView);
  7067. TStrings * FileList = RemoteDriveView->DragFileList();
  7068. try
  7069. {
  7070. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7071. }
  7072. __finally
  7073. {
  7074. delete FileList;
  7075. }
  7076. }
  7077. if (FDDTargetControl == RemoteDriveView)
  7078. {
  7079. RemoteDriveView->UpdateDropTarget();
  7080. }
  7081. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7082. // or from tree view to dir view.
  7083. // foMove happens when file/dir is dragged from remote tree view outside of
  7084. // application via dragex
  7085. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7086. (DropSourceControl == RemoteDriveView))
  7087. {
  7088. RemoteDriveView->UpdateDropSource();
  7089. }
  7090. return Result;
  7091. }
  7092. //---------------------------------------------------------------------------
  7093. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7094. {
  7095. DebugAssert(!IsLocalBrowserMode());
  7096. // This also handles drops of remote files to queue.
  7097. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7098. SAFE_DESTROY(FDDFileList);
  7099. if (!FFakeFileDropTarget.IsEmpty())
  7100. {
  7101. RemoveDir(ApiPath(FFakeFileDropTarget));
  7102. }
  7103. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7104. {
  7105. try
  7106. {
  7107. TDragResult DDResult = (Sender == RemoteDirView) ?
  7108. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7109. // Focus is moved to the target application,
  7110. // but as we are going to present the UI, we need to steal the focus back.
  7111. // This most likely won't work though (windows does not allow application
  7112. // to steal focus most of the time)
  7113. Application->BringToFront();
  7114. // On older version of Windows we never got drMove here, see also comment below.
  7115. // On Windows 10, we get the "move".
  7116. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7117. {
  7118. UnicodeString TargetDirectory;
  7119. TFileOperation Operation;
  7120. // drInvalid may mean drMove, see comment below
  7121. switch (DDResult)
  7122. {
  7123. case drCopy:
  7124. Operation = foCopy;
  7125. break;
  7126. case drMove:
  7127. Operation = foMove;
  7128. break;
  7129. default:
  7130. DebugFail();
  7131. case drInvalid:
  7132. // prefer "copy" for safety
  7133. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7134. break;
  7135. }
  7136. TTransferOperationParam Param;
  7137. UnicodeString CounterName;
  7138. bool ForceQueue;
  7139. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7140. {
  7141. // we get drInvalid both if d&d was intercepted by ddext,
  7142. // and when users drops on no-drop location.
  7143. // we tell the difference by existence of response from ddext,
  7144. // so we ignore absence of response in this case
  7145. if (DDResult != drInvalid)
  7146. {
  7147. // here we know that the extension is installed,
  7148. // as it is checked as soon as drag&drop starts from
  7149. // RemoteFileControlDDCreateDragFileList
  7150. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7151. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7152. {
  7153. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7154. }
  7155. else
  7156. {
  7157. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7158. }
  7159. }
  7160. }
  7161. else
  7162. {
  7163. // download using fake file
  7164. Param.Temp = false;
  7165. Param.DragDrop = true;
  7166. if (ForceQueue)
  7167. {
  7168. Param.Queue = asOn;
  7169. }
  7170. if (Sender == RemoteDirView)
  7171. {
  7172. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7173. }
  7174. if (RemoteFileControlFileOperation(Sender, Operation,
  7175. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7176. {
  7177. Configuration->Usage->Inc(CounterName);
  7178. }
  7179. }
  7180. }
  7181. }
  7182. __finally
  7183. {
  7184. CloseHandle(FDDExtMapFile);
  7185. FDDExtMapFile = NULL;
  7186. delete FDragFakeMonitors;
  7187. FDragFakeMonitors = NULL;
  7188. RemoveDir(ApiPath(FDragFakeDirectory));
  7189. FDragFakeDirectory = L"";
  7190. FFakeFileDropTarget = UnicodeString();
  7191. }
  7192. }
  7193. if (FDragDropSshTerminate.get() != NULL)
  7194. {
  7195. FDragDropSshTerminate->Rethrow();
  7196. }
  7197. }
  7198. //---------------------------------------------------------------------------
  7199. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7200. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7201. {
  7202. // Remember drop effect so we know (when user drops files), if we copy or move
  7203. FLastDropEffect = dwEffect;
  7204. }
  7205. //---------------------------------------------------------------------------
  7206. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7207. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7208. {
  7209. bool Result;
  7210. if (FDDTargetControl == QueueView3)
  7211. {
  7212. Directory = DefaultDownloadTargetDirectory();
  7213. Result = true;
  7214. CounterName = L"DownloadsDragDropQueue";
  7215. ForceQueue = true;
  7216. }
  7217. else if (!FFakeFileDropTarget.IsEmpty())
  7218. {
  7219. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7220. Result = true;
  7221. ForceQueue = false;
  7222. CounterName = L"DownloadsDragDropFakeFile";
  7223. }
  7224. else
  7225. {
  7226. ForceQueue = false;
  7227. Enabled = false;
  7228. try
  7229. {
  7230. int Timer = 0;
  7231. Result = false;
  7232. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7233. {
  7234. {
  7235. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7236. TDragExtCommStruct* CommStruct;
  7237. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7238. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7239. DebugAssert(CommStruct != NULL);
  7240. Result = !CommStruct->Dragging;
  7241. if (Result)
  7242. {
  7243. Directory = ExtractFilePath(CommStruct->DropDest);
  7244. CounterName = L"DownloadsDragDropExternalExt";
  7245. }
  7246. UnmapViewOfFile(CommStruct);
  7247. }
  7248. if (!Result)
  7249. {
  7250. Sleep(50);
  7251. Timer += 50;
  7252. Application->ProcessMessages();
  7253. }
  7254. }
  7255. }
  7256. __finally
  7257. {
  7258. Enabled = true;
  7259. }
  7260. }
  7261. return Result;
  7262. }
  7263. //---------------------------------------------------------------------------
  7264. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7265. const UnicodeString TempDir, int SecDelay)
  7266. {
  7267. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7268. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7269. if (FDelayedDeletionTimer == NULL)
  7270. {
  7271. DebugAssert(HandleAllocated());
  7272. FDelayedDeletionTimer = new TTimer(this);
  7273. FDelayedDeletionTimer->Interval = 10000;
  7274. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7275. }
  7276. else
  7277. {
  7278. FDelayedDeletionTimer->Enabled = true;
  7279. }
  7280. }
  7281. //---------------------------------------------------------------------------
  7282. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7283. {
  7284. DebugAssert(FDelayedDeletionList != NULL);
  7285. TDateTime N = Now();
  7286. TDateTime Alert;
  7287. UnicodeString Directory;
  7288. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7289. {
  7290. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7291. if ((N >= Alert) || (Sender == NULL))
  7292. {
  7293. Directory = FDelayedDeletionList->Strings[Index];
  7294. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7295. {
  7296. FDelayedDeletionList->Delete(Index);
  7297. }
  7298. }
  7299. }
  7300. if (FDelayedDeletionList->Count == 0)
  7301. {
  7302. FDelayedDeletionTimer->Enabled = false;
  7303. }
  7304. }
  7305. //---------------------------------------------------------------------------
  7306. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7307. {
  7308. DebugAssert(!IsLocalBrowserMode());
  7309. if (IsFileControl(FDDTargetControl, osRemote) ||
  7310. (FDDTargetControl == SessionsPageControl))
  7311. {
  7312. TManagedTerminal * TargetTerminal = NULL;
  7313. TFileOperation Operation = foNone;
  7314. if (FDDTargetControl == SessionsPageControl)
  7315. {
  7316. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7317. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7318. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7319. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7320. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7321. {
  7322. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7323. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7324. {
  7325. Operation = foRemoteMove;
  7326. }
  7327. else
  7328. {
  7329. Operation = foRemoteCopy;
  7330. }
  7331. }
  7332. }
  7333. else
  7334. {
  7335. if (FLastDropEffect == DROPEFFECT_MOVE)
  7336. {
  7337. Operation = foRemoteMove;
  7338. }
  7339. else if (FLastDropEffect == DROPEFFECT_COPY)
  7340. {
  7341. Operation = foRemoteCopy;
  7342. }
  7343. }
  7344. if (Operation != foNone)
  7345. {
  7346. RemoteFileControlFileOperation(DropSourceControl,
  7347. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7348. }
  7349. // abort drag&drop
  7350. Abort();
  7351. }
  7352. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7353. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7354. (FDDTargetControl != QueueView3))
  7355. {
  7356. DebugAssert(!FDragTempDir.IsEmpty());
  7357. TTransferType Type;
  7358. UnicodeString TempDir = FDragTempDir;
  7359. // We clear FDragTempDir before calling
  7360. // just in case it fail (raises exception)
  7361. FDragTempDir = L"";
  7362. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7363. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7364. // empty directory parameter means temp directory -> don't display it!
  7365. UnicodeString TargetDir = L"";
  7366. int Options = 0;
  7367. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7368. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7369. {
  7370. Abort();
  7371. }
  7372. // TargetDir is set when dropped on local file control
  7373. // (this was workaround for legacy dirview event handling, now it should be
  7374. // made prettier)
  7375. if (TargetDir.IsEmpty())
  7376. {
  7377. TargetDir = TempDir;
  7378. if (ForceDirectories(ApiPath(TargetDir)))
  7379. {
  7380. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7381. FPendingTempSpaceWarn = true;
  7382. try
  7383. {
  7384. FDragDropOperation = true;
  7385. int Params = cpTemporary |
  7386. (Type == ttMove ? cpDelete : 0);
  7387. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7388. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7389. }
  7390. __finally
  7391. {
  7392. FDragDropOperation = false;
  7393. FPendingTempSpaceWarn = false;
  7394. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7395. }
  7396. }
  7397. else
  7398. {
  7399. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7400. }
  7401. }
  7402. }
  7403. }
  7404. //---------------------------------------------------------------------------
  7405. void __fastcall TCustomScpExplorerForm::DDDownload(
  7406. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7407. {
  7408. DebugAssert(!IsLocalBrowserMode());
  7409. TAutoBatch AutoBatch(this);
  7410. UpdateCopyParamCounters(*CopyParam);
  7411. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7412. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7413. {
  7414. RemoteDriveView->UpdateDropSource();
  7415. }
  7416. }
  7417. //---------------------------------------------------------------------------
  7418. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7419. {
  7420. public:
  7421. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7422. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7423. {
  7424. }
  7425. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7426. {
  7427. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7428. TDataObjectFilesEx::AllowData(FormatEtc);
  7429. }
  7430. };
  7431. //---------------------------------------------------------------------------
  7432. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7433. TObject * Sender, TDataObject *& DataObject)
  7434. {
  7435. if (FDDExtMapFile != NULL)
  7436. {
  7437. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7438. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7439. {
  7440. Abort();
  7441. }
  7442. if (FileList->Count > 0)
  7443. {
  7444. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7445. if (!FilesObject->IsValid(true, true))
  7446. {
  7447. FilesObject->_Release();
  7448. }
  7449. else
  7450. {
  7451. DataObject = FilesObject;
  7452. }
  7453. }
  7454. }
  7455. }
  7456. //---------------------------------------------------------------------------
  7457. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7458. {
  7459. DebugFail();
  7460. }
  7461. //---------------------------------------------------------------------------
  7462. void __fastcall TCustomScpExplorerForm::GoToTree()
  7463. {
  7464. ComponentVisible[fcRemoteTree] = true;
  7465. DriveView(osRemote)->SetFocus();
  7466. }
  7467. //---------------------------------------------------------------------------
  7468. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7469. TPanelExport Export)
  7470. {
  7471. TCustomDirView * DirView = this->DirView(Side);
  7472. std::unique_ptr<TStrings> ExportData(new TStringList());
  7473. switch (Export)
  7474. {
  7475. case pePath:
  7476. ExportData->Add(DirView->PathName);
  7477. break;
  7478. case peFileList:
  7479. case peFullFileList:
  7480. {
  7481. bool FullPath = (Export == peFullFileList);
  7482. DirView->CreateFileList(false, FullPath, ExportData.get());
  7483. for (int Index = 0; Index < ExportData->Count; Index++)
  7484. {
  7485. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7486. {
  7487. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7488. }
  7489. }
  7490. }
  7491. break;
  7492. default:
  7493. DebugFail();
  7494. }
  7495. return ExportData.release();
  7496. }
  7497. //---------------------------------------------------------------------------
  7498. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7499. TPanelExport Export, TPanelExportDestination Destination)
  7500. {
  7501. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7502. PanelExportStore(Side, Export, Destination, ExportData.get());
  7503. }
  7504. //---------------------------------------------------------------------------
  7505. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7506. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7507. TStrings * ExportData)
  7508. {
  7509. if (Destination == pedClipboard)
  7510. {
  7511. TInstantOperationVisualizer Visualizer;
  7512. CopyToClipboard(ExportData);
  7513. }
  7514. else
  7515. {
  7516. DebugFail();
  7517. }
  7518. }
  7519. //---------------------------------------------------------------------------
  7520. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7521. {
  7522. TCustomDirView * DirView = this->DirView(Side);
  7523. UnicodeString Mask = DirView->Mask;
  7524. if (DoFilterMaskDialog(DirView, Mask))
  7525. {
  7526. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7527. Configuration->Usage->Inc(L"Filters");
  7528. }
  7529. }
  7530. //---------------------------------------------------------------------------
  7531. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7532. {
  7533. return FQueueController->DefaultOperation();
  7534. }
  7535. //---------------------------------------------------------------------------
  7536. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7537. TQueueOperation Operation, void ** Param)
  7538. {
  7539. switch (Operation)
  7540. {
  7541. case qoPreferences:
  7542. return true;
  7543. case qoGoTo:
  7544. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7545. case qoOnceEmpty:
  7546. return IsAnythingQueued();
  7547. default:
  7548. return FQueueController->AllowOperation(Operation, Param);
  7549. }
  7550. }
  7551. //---------------------------------------------------------------------------
  7552. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7553. {
  7554. if (DebugAlwaysTrue(QueueView3->Visible))
  7555. {
  7556. QueueView3->SetFocus();
  7557. }
  7558. }
  7559. //---------------------------------------------------------------------------
  7560. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7561. TQueueOperation Operation, void * Param)
  7562. {
  7563. if (Operation == qoGoTo)
  7564. {
  7565. GoToQueue();
  7566. }
  7567. else if (Operation == qoPreferences)
  7568. {
  7569. PreferencesDialog(pmQueue);
  7570. }
  7571. else
  7572. {
  7573. FQueueController->ExecuteOperation(Operation, Param);
  7574. }
  7575. }
  7576. //---------------------------------------------------------------------------
  7577. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7578. {
  7579. return (Queue != NULL) && Queue->Enabled;
  7580. }
  7581. //---------------------------------------------------------------------------
  7582. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7583. {
  7584. DebugAssert(Queue != NULL);
  7585. if (Queue != NULL)
  7586. {
  7587. Queue->Enabled = !Queue->Enabled;
  7588. }
  7589. }
  7590. //---------------------------------------------------------------------------
  7591. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7592. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7593. {
  7594. FQueueActedItem = QueueView3->ItemFocused;
  7595. }
  7596. //---------------------------------------------------------------------------
  7597. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7598. {
  7599. return TopDock->Height + QueueSplitter->Height;
  7600. }
  7601. //---------------------------------------------------------------------------
  7602. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7603. {
  7604. return
  7605. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7606. (QueueDock->Visible ? QueueDock->Height : 0) +
  7607. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7608. }
  7609. //---------------------------------------------------------------------------
  7610. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7611. {
  7612. return ScaleByTextHeight(this, 48);
  7613. }
  7614. //---------------------------------------------------------------------------
  7615. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7616. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7617. {
  7618. // when queue is hidden by double-clicking splitter, stray attempt to
  7619. // resize the panel with strange value arrives, make sure it is ignored
  7620. if (ComponentVisible[fcQueueView])
  7621. {
  7622. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7623. if (NewSize > MaxHeightLimit)
  7624. {
  7625. NewSize = MaxHeightLimit;
  7626. }
  7627. int MinHeightLimit =
  7628. GetStaticQueuePanelComponentsHeight() +
  7629. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7630. GetMinQueueViewHeight();
  7631. if (NewSize < MinHeightLimit)
  7632. {
  7633. NewSize = MinHeightLimit;
  7634. }
  7635. }
  7636. else
  7637. {
  7638. Accept = false;
  7639. }
  7640. }
  7641. //---------------------------------------------------------------------------
  7642. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7643. {
  7644. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7645. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7646. if (QueueViewHeight < GetMinQueueViewHeight())
  7647. {
  7648. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7649. }
  7650. }
  7651. //---------------------------------------------------------------------------
  7652. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7653. TDragObject *& /*DragObject*/)
  7654. {
  7655. FQueueActedItem = QueueView3->ItemFocused;
  7656. }
  7657. //---------------------------------------------------------------------------
  7658. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7659. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7660. {
  7661. Accept = true;
  7662. if (Source == QueueView3)
  7663. {
  7664. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7665. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7666. if (Accept)
  7667. {
  7668. TQueueItemProxy * QueueItem;
  7669. TQueueItemProxy * DestQueueItem;
  7670. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7671. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7672. Accept = (QueueItem != DestQueueItem) &&
  7673. (QueueItem->Status == TQueueItem::qsPending) &&
  7674. (DestQueueItem->Status == TQueueItem::qsPending);
  7675. }
  7676. }
  7677. }
  7678. //---------------------------------------------------------------------------
  7679. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7680. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7681. {
  7682. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7683. {
  7684. TQueueItemProxy * QueueItem;
  7685. TQueueItemProxy * DestQueueItem;
  7686. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7687. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7688. QueueItem->Move(DestQueueItem);
  7689. }
  7690. }
  7691. //---------------------------------------------------------------------------
  7692. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7693. {
  7694. if ((QueueView3->ItemFocused == NULL) &&
  7695. (QueueView3->Items->Count > 0))
  7696. {
  7697. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7698. }
  7699. QueueLabelUpdateStatus();
  7700. }
  7701. //---------------------------------------------------------------------------
  7702. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7703. {
  7704. QueueLabelUpdateStatus();
  7705. }
  7706. //---------------------------------------------------------------------------
  7707. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7708. {
  7709. QueueLabel->UpdateStatus();
  7710. }
  7711. //---------------------------------------------------------------------------
  7712. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7713. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7714. {
  7715. if (Selected)
  7716. {
  7717. NonVisualDataModule->UpdateNonVisibleActions();
  7718. }
  7719. }
  7720. //---------------------------------------------------------------------------
  7721. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7722. {
  7723. TDragDropFilesEx * Result = NULL;
  7724. if (Sender == SessionsPageControl)
  7725. {
  7726. Result = FSessionsDragDropFilesEx;
  7727. }
  7728. else if (Sender == QueueView3)
  7729. {
  7730. Result = FQueueDragDropFilesEx;
  7731. }
  7732. else
  7733. {
  7734. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7735. if (DirView != NULL)
  7736. {
  7737. Result = DirView->DragDropFilesEx;
  7738. }
  7739. else
  7740. {
  7741. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7742. if (DriveView != NULL)
  7743. {
  7744. Result = DriveView->DragDropFilesEx;
  7745. }
  7746. }
  7747. }
  7748. DebugAssert(Result != NULL);
  7749. return Result;
  7750. }
  7751. //---------------------------------------------------------------------------
  7752. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7753. {
  7754. return (DView->SelCount == DView->FilesCount);
  7755. }
  7756. //---------------------------------------------------------------------------
  7757. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7758. {
  7759. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7760. }
  7761. //---------------------------------------------------------------------------
  7762. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7763. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7764. {
  7765. DebugAssert(!IsLocalBrowserMode());
  7766. TFileOperation Operation;
  7767. switch (Effect)
  7768. {
  7769. case DROPEFFECT_MOVE:
  7770. Operation = foMove;
  7771. break;
  7772. case DROPEFFECT_COPY:
  7773. // occures on WinXP (reported by user)
  7774. default:
  7775. Operation = foCopy;
  7776. break;
  7777. };
  7778. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7779. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7780. if (DragDropFilesEx->FileList->Count > 0)
  7781. {
  7782. TStrings * FileList = new TStringList();
  7783. try
  7784. {
  7785. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7786. {
  7787. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7788. }
  7789. FDragDropOperation = true;
  7790. TTransferOperationParam Param;
  7791. Param.TargetDirectory = TargetPath;
  7792. // upload, no temp dirs
  7793. Param.Temp = false;
  7794. Param.DragDrop = DragDrop;
  7795. Param.Options =
  7796. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7797. if (ForceQueue)
  7798. {
  7799. Param.Queue = asOn;
  7800. }
  7801. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7802. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7803. {
  7804. if (IsFileControl(DropSourceControl, osLocal))
  7805. {
  7806. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7807. }
  7808. else
  7809. {
  7810. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7811. }
  7812. }
  7813. }
  7814. __finally
  7815. {
  7816. FDragDropOperation = false;
  7817. delete FileList;
  7818. }
  7819. }
  7820. }
  7821. //---------------------------------------------------------------------------
  7822. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7823. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7824. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7825. {
  7826. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7827. }
  7828. //---------------------------------------------------------------------------
  7829. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7830. TObject * Sender, int grfKeyState, int & dwEffect)
  7831. {
  7832. DebugAssert(!IsLocalBrowserMode());
  7833. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7834. if ((dwEffect != DROPEFFECT_NONE) &&
  7835. IsFileControl(DropSourceControl, osRemote))
  7836. {
  7837. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7838. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7839. (RemoteDirView->DropTarget == NULL))
  7840. {
  7841. dwEffect = DROPEFFECT_NONE;
  7842. }
  7843. else
  7844. {
  7845. if (dwEffect == DROPEFFECT_COPY)
  7846. {
  7847. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  7848. // currently we support copying always (at least via temporary directory);
  7849. // remove associated checks once this all proves stable and working
  7850. bool CopyCapable = true;
  7851. // if we do not support neither of operations, there's no discussion
  7852. if (!MoveCapable && !CopyCapable)
  7853. {
  7854. dwEffect = DROPEFFECT_NONE;
  7855. }
  7856. else
  7857. {
  7858. // The default effect inside remote panel is move,
  7859. // unless we do not support it, but support copy
  7860. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7861. {
  7862. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7863. }
  7864. else
  7865. {
  7866. // with ctrl-down, we want copy unless it is not supported
  7867. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7868. }
  7869. }
  7870. }
  7871. }
  7872. }
  7873. }
  7874. //---------------------------------------------------------------------------
  7875. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7876. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7877. {
  7878. DebugAssert(!IsLocalBrowserMode());
  7879. if (FDDTotalSize >= 0)
  7880. {
  7881. if (File->IsDirectory)
  7882. {
  7883. FDDTotalSize = -1;
  7884. }
  7885. else
  7886. {
  7887. FDDTotalSize += File->Size;
  7888. }
  7889. }
  7890. DebugAssert(!FDragTempDir.IsEmpty());
  7891. // TODO: this is quite ineffective
  7892. // TODO: what if invalid character replacement is disabled?
  7893. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7894. UnicodeString TransferFileName;
  7895. if (Sender == RemoteDriveView)
  7896. {
  7897. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7898. }
  7899. else
  7900. {
  7901. TransferFileName = File->FileName;
  7902. }
  7903. FDDFileList->AddObject(TransferFileName, File);
  7904. }
  7905. //---------------------------------------------------------------------------
  7906. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7907. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7908. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7909. {
  7910. // sometimes we do not get DDEnd so the list is not released
  7911. SAFE_DESTROY(FDDFileList);
  7912. FDDFileList = new TStringList();
  7913. FDragTempDir = WinConfiguration->TemporaryDir();
  7914. FDDTotalSize = 0;
  7915. FDragDropSshTerminate.reset(NULL);
  7916. }
  7917. //---------------------------------------------------------------------------
  7918. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7919. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7920. HRESULT & Result)
  7921. {
  7922. if (Result == DRAGDROP_S_DROP)
  7923. {
  7924. try
  7925. {
  7926. GlobalDragImageList->HideDragImage();
  7927. try
  7928. {
  7929. RemoteFileControlDDTargetDrop();
  7930. }
  7931. catch(ESshTerminate & E)
  7932. {
  7933. FDragDropSshTerminate.reset(E.Clone());
  7934. }
  7935. }
  7936. catch (Exception &E)
  7937. {
  7938. // If downloading fails we need to cancel drag&drop, otherwise
  7939. // Explorer shows error
  7940. // But by the way exception probably never reach this point as
  7941. // it's catched on way.
  7942. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7943. Result = DRAGDROP_S_CANCEL;
  7944. ShowExtendedException(Terminal, &E);
  7945. }
  7946. }
  7947. }
  7948. //---------------------------------------------------------------------------
  7949. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7950. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7951. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7952. {
  7953. TFileMasks::TParams MaskParams;
  7954. MaskParams.Size = Size;
  7955. MaskParams.Modification = Modification;
  7956. // this does not re-parse the mask if it is the same as the last time
  7957. FDirViewMatchMask = Masks;
  7958. bool ImplicitMatch;
  7959. Matches =
  7960. // RecurseInclude parameter has no effect as the Path is empty
  7961. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7962. (AllowImplicitMatches || !ImplicitMatch);
  7963. }
  7964. //---------------------------------------------------------------------------
  7965. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7966. TObject * Sender, TListItem * Item, WORD & Indexes)
  7967. {
  7968. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7969. UnicodeString Ext;
  7970. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7971. {
  7972. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7973. }
  7974. else
  7975. {
  7976. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7977. }
  7978. if (SameText(Ext, Configuration->PartialExt))
  7979. {
  7980. Indexes |= oiPartial;
  7981. }
  7982. }
  7983. //---------------------------------------------------------------------------
  7984. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7985. {
  7986. return
  7987. DirViewEnabled(osCurrent) &&
  7988. DirView(osCurrent)->CanPasteFromClipBoard();
  7989. }
  7990. //---------------------------------------------------------------------------
  7991. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7992. {
  7993. bool Result = false;
  7994. if (CanPasteToDirViewFromClipBoard())
  7995. {
  7996. Result = true;
  7997. }
  7998. else
  7999. {
  8000. UnicodeString ClipboardText;
  8001. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8002. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8003. {
  8004. if (StoredSessions->IsUrl(ClipboardText))
  8005. {
  8006. Result = true;
  8007. }
  8008. else
  8009. {
  8010. Result = DirViewEnabled(osCurrent);
  8011. }
  8012. }
  8013. }
  8014. return Result;
  8015. }
  8016. //---------------------------------------------------------------------------
  8017. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8018. {
  8019. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8020. if (DoesClipboardContainOurFiles())
  8021. {
  8022. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8023. {
  8024. DebugAssert(!IsLocalBrowserMode());
  8025. TTerminalManager * Manager = TTerminalManager::Instance();
  8026. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8027. Manager->ActiveSession = FClipboardTerminal;
  8028. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8029. }
  8030. }
  8031. else if (CanPasteToDirViewFromClipBoard())
  8032. {
  8033. DirView(osCurrent)->PasteFromClipBoard();
  8034. }
  8035. else
  8036. {
  8037. UnicodeString ClipboardText;
  8038. if (NonEmptyTextFromClipboard(ClipboardText))
  8039. {
  8040. if (StoredSessions->IsUrl(ClipboardText))
  8041. {
  8042. NewSession(ClipboardText);
  8043. }
  8044. else
  8045. {
  8046. DirView(osCurrent)->Path = ClipboardText;
  8047. }
  8048. }
  8049. }
  8050. }
  8051. //---------------------------------------------------------------------------
  8052. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8053. {
  8054. // TBD
  8055. }
  8056. //---------------------------------------------------------------------------
  8057. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8058. {
  8059. TCustomDirView * ADirView = DirView(Side);
  8060. ADirView->SelectAll(Mode);
  8061. ADirView->SetFocus();
  8062. }
  8063. //---------------------------------------------------------------------------
  8064. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8065. {
  8066. TCustomDirView * ADirView = DirView(Side);
  8067. TFileFilter Filter;
  8068. DefaultFileFilter(Filter);
  8069. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8070. {
  8071. Configuration->Usage->Inc(L"MaskSelections");
  8072. ADirView->SelectFiles(Filter, Select);
  8073. ADirView->SetFocus();
  8074. }
  8075. }
  8076. //---------------------------------------------------------------------------
  8077. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8078. {
  8079. TCustomDirView * ADirView = DirView(Side);
  8080. ADirView->RestoreSelectedNames();
  8081. ADirView->SetFocus();
  8082. }
  8083. //---------------------------------------------------------------------------
  8084. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8085. {
  8086. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8087. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8088. {
  8089. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8090. UnicodeString Ext;
  8091. if (!IsSideLocalBrowser(osCurrent))
  8092. {
  8093. Ext = UnixExtractFileExt(FileName);
  8094. }
  8095. else
  8096. {
  8097. Ext = ExtractFileExt(FileName);
  8098. }
  8099. if (Ext.IsEmpty())
  8100. {
  8101. Ext = L".";
  8102. }
  8103. TFileFilter Filter;
  8104. Filter.Masks = FORMAT(L"*%s", (Ext));
  8105. Filter.Directories = false;
  8106. CurrentDirView->SelectFiles(Filter, Select);
  8107. }
  8108. }
  8109. //---------------------------------------------------------------------------
  8110. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8111. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8112. {
  8113. UnicodeString Result;
  8114. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8115. {
  8116. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8117. }
  8118. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8119. {
  8120. // noop
  8121. }
  8122. else
  8123. {
  8124. Result =
  8125. FMTLOAD(FILE_INFO_FORMAT,
  8126. (FormatBytes(FileInfo.SelectedSize),
  8127. FormatBytes(FileInfo.FilesSize),
  8128. FormatNumber(FileInfo.SelectedCount),
  8129. FormatNumber(FileInfo.FilesCount)));
  8130. }
  8131. return Result;
  8132. }
  8133. //---------------------------------------------------------------------------
  8134. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8135. TTBXStatusPanel * Panel, TOperationSide Side)
  8136. {
  8137. if (Panel->Index == 1)
  8138. {
  8139. ToggleShowHiddenFiles();
  8140. }
  8141. else if (Panel->Index == 2)
  8142. {
  8143. Filter(Side);
  8144. }
  8145. }
  8146. //---------------------------------------------------------------------------
  8147. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8148. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8149. {
  8150. DebugAssert(!StatusBar->SimplePanel);
  8151. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8152. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8153. }
  8154. //---------------------------------------------------------------------------
  8155. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8156. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8157. {
  8158. DebugAssert(StatusBar->Panels->Count >= 3);
  8159. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8160. if (FileInfo.HiddenCount > 0)
  8161. {
  8162. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8163. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8164. }
  8165. else
  8166. {
  8167. HiddenFilesPanel->ViewPriority = 0;
  8168. // not really necessary, just to cleanup no-longer-valid data
  8169. HiddenFilesPanel->Caption = L"";
  8170. }
  8171. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8172. if (FileInfo.FilteredCount > 0)
  8173. {
  8174. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8175. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8176. }
  8177. else
  8178. {
  8179. FilteredFilesPanel->ViewPriority = 0;
  8180. // not really necessary, just to cleanup no-longer-valid data
  8181. FilteredFilesPanel->Caption = L"";
  8182. }
  8183. }
  8184. //---------------------------------------------------------------------------
  8185. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8186. TObject * /*Sender*/)
  8187. {
  8188. if (DirView(osOther)->Enabled)
  8189. {
  8190. DirView(osOther)->SetFocus();
  8191. }
  8192. }
  8193. //---------------------------------------------------------------------------
  8194. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8195. {
  8196. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8197. switch (Config.Show)
  8198. {
  8199. case qvShow:
  8200. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8201. {
  8202. Config.Show = qvHide;
  8203. }
  8204. else
  8205. {
  8206. Config.Show = Config.LastHideShow;
  8207. }
  8208. break;
  8209. case qvHideWhenEmpty:
  8210. if (ComponentVisible[fcQueueView])
  8211. {
  8212. Config.Show = qvHide;
  8213. }
  8214. else
  8215. {
  8216. Config.LastHideShow = Config.Show;
  8217. Config.Show = qvShow;
  8218. }
  8219. break;
  8220. case qvHide:
  8221. Config.LastHideShow = Config.Show;
  8222. Config.Show = qvShow;
  8223. break;
  8224. }
  8225. WinConfiguration->QueueView = Config;
  8226. }
  8227. //---------------------------------------------------------------------------
  8228. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8229. {
  8230. UnicodeString Result;
  8231. if (Terminal != NULL)
  8232. {
  8233. switch (WinConfiguration->PathInCaption)
  8234. {
  8235. case picShort:
  8236. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8237. break;
  8238. case picFull:
  8239. Result = Terminal->CurrentDirectory;
  8240. break;
  8241. }
  8242. }
  8243. return Result;
  8244. }
  8245. //---------------------------------------------------------------------------
  8246. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8247. {
  8248. TColor Result = GetWindowColor(FSessionColor);
  8249. return Result;
  8250. }
  8251. //---------------------------------------------------------------------------
  8252. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8253. {
  8254. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8255. return Result;
  8256. }
  8257. //---------------------------------------------------------------------------
  8258. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8259. {
  8260. if (!FSessionChanging)
  8261. {
  8262. TTerminalManager::Instance()->UpdateAppTitle();
  8263. }
  8264. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8265. // background to gray on Windows 7 (works on Windows 10).
  8266. // See also EnableControl
  8267. if (Showing)
  8268. {
  8269. if (ManagedSession != NULL)
  8270. {
  8271. // Update path when it changes
  8272. if ((SessionsPageControl->ActivePage != NULL) &&
  8273. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8274. {
  8275. UpdateSessionTab(SessionsPageControl->ActivePage);
  8276. }
  8277. }
  8278. NonVisualDataModule->ReconnectSessionAction->Update();
  8279. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8280. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8281. CenterReconnectToolbar();
  8282. bool HasTerminal = HasActiveTerminal();
  8283. RemoteDirView->Enabled = HasTerminal;
  8284. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8285. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8286. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8287. RemoteDriveView->Color = RemoteDirView->Color;
  8288. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8289. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8290. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8291. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8292. QueueFileList->Enabled = QueueView3->Enabled;
  8293. QueueFileList->Color = QueueView3->Color;
  8294. QueueFileList->Font->Color = QueueView3->Font->Color;
  8295. QueueLabelUpdateStatus();
  8296. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8297. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8298. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8299. {
  8300. UpdateDarkMode();
  8301. }
  8302. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8303. }
  8304. }
  8305. //---------------------------------------------------------------------------
  8306. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8307. {
  8308. UpdateControls();
  8309. }
  8310. //---------------------------------------------------------------------------
  8311. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8312. TObject * Sender)
  8313. {
  8314. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8315. DebugAssert(DirView != NULL);
  8316. DoDirViewLoaded(DirView);
  8317. TransferPresetAutoSelect();
  8318. }
  8319. //---------------------------------------------------------------------------
  8320. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8321. {
  8322. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8323. // first run after installation
  8324. if (double(Updates.LastCheck) == 0)
  8325. {
  8326. // make sure next time there will be an update (if enabled)
  8327. Updates.LastCheck = TDateTime(1);
  8328. WinConfiguration->Updates = Updates;
  8329. }
  8330. else if ((double(Updates.Period) > 0) &&
  8331. (Now() - Updates.LastCheck >= Updates.Period))
  8332. {
  8333. TThreadMethod OnUpdatesChecked = NULL;
  8334. if (!IsUWP())
  8335. {
  8336. OnUpdatesChecked = UpdatesChecked;
  8337. }
  8338. StartUpdateThread(OnUpdatesChecked);
  8339. }
  8340. }
  8341. //---------------------------------------------------------------------------
  8342. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8343. {
  8344. UnicodeString Message;
  8345. bool New;
  8346. TQueryType Type;
  8347. GetUpdatesMessage(Message, New, Type, false);
  8348. if (!Message.IsEmpty())
  8349. {
  8350. if (New)
  8351. {
  8352. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8353. }
  8354. if (!New && (Type != qtWarning))
  8355. {
  8356. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8357. }
  8358. else
  8359. {
  8360. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8361. }
  8362. if (New)
  8363. {
  8364. Configuration->Usage->Inc(L"UpdateNotifications");
  8365. }
  8366. }
  8367. }
  8368. //---------------------------------------------------------------------------
  8369. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8370. {
  8371. RestoreApp();
  8372. bool CanDisplay = !NonVisualDataModule->Busy;
  8373. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8374. {
  8375. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8376. }
  8377. if (CanDisplay)
  8378. {
  8379. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8380. CheckForUpdates(true);
  8381. }
  8382. else
  8383. {
  8384. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8385. }
  8386. }
  8387. //---------------------------------------------------------------------------
  8388. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8389. TCopyParamRuleData & Data)
  8390. {
  8391. DebugAssert(Terminal != NULL);
  8392. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8393. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8394. Data.RemoteDirectory = RemoteDirView->PathName;
  8395. }
  8396. //---------------------------------------------------------------------------
  8397. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8398. {
  8399. // Terminal can be null when we are changing local directory implicitly,
  8400. // because it has been deleted, while no session is connected
  8401. // (Login dialog is open)
  8402. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8403. {
  8404. DebugAssert(!IsLocalBrowserMode());
  8405. TCopyParamRuleData Data;
  8406. GetTransferPresetAutoSelectData(Data);
  8407. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8408. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8409. if (CopyParamIndex < 0)
  8410. {
  8411. // there is no preset that matches autoselection
  8412. // set preset that we consider "default"
  8413. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8414. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8415. }
  8416. else
  8417. {
  8418. // there is preset matching autoselection
  8419. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8420. if (CopyParamName == FCopyParamAutoSelected)
  8421. {
  8422. // autoselected the same preset as the last time
  8423. // make no change (i.e. preserve custom user preset, if any)
  8424. }
  8425. else
  8426. {
  8427. // autoselected the different preset then the last time (or there
  8428. // was default preset set)
  8429. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8430. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8431. }
  8432. }
  8433. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8434. {
  8435. if (CopyParamIndex >= 0)
  8436. {
  8437. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8438. }
  8439. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8440. try
  8441. {
  8442. int Fmt =
  8443. (CopyParamIndex < 0) ?
  8444. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8445. COPY_PARAM_AUTOSELECTED;
  8446. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8447. Data->Message = MainInstructions(Message);
  8448. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8449. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8450. CopyParamAttrs);
  8451. if (CopyParamIndex >= 0)
  8452. {
  8453. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8454. Info = FORMAT(L"%s\n \n%s", (Info,
  8455. FMTLOAD(COPY_PARAM_RULE,
  8456. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8457. }
  8458. Data->Message += L"\n\n" + Info;
  8459. if (WinConfiguration->CopyParamAutoSelectNotice)
  8460. {
  8461. TransferPresetNoteMessage(Data, true);
  8462. }
  8463. else
  8464. {
  8465. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8466. Data = NULL; // ownership passed
  8467. }
  8468. }
  8469. __finally
  8470. {
  8471. delete Data;
  8472. }
  8473. }
  8474. }
  8475. }
  8476. //---------------------------------------------------------------------------
  8477. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8478. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8479. {
  8480. DebugAssert(NoteData != NULL);
  8481. DebugAssert(!IsLocalBrowserMode());
  8482. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8483. TQueryButtonAlias Aliases[1];
  8484. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8485. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8486. Params.Aliases = Aliases;
  8487. Params.AliasesCount = LENOF(Aliases);
  8488. unsigned int Result =
  8489. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8490. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8491. switch (Result)
  8492. {
  8493. case qaNeverAskAgain:
  8494. DebugAssert(AllowNeverAskAgain);
  8495. WinConfiguration->CopyParamAutoSelectNotice = false;
  8496. break;
  8497. case qaIgnore:
  8498. PreferencesDialog(pmPresets);
  8499. break;
  8500. }
  8501. }
  8502. //---------------------------------------------------------------------------
  8503. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8504. {
  8505. // as of now useless, as this is used for notes only, never for balloons, ...
  8506. RestoreApp();
  8507. // .. and we should never be busy here
  8508. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8509. {
  8510. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8511. }
  8512. }
  8513. //---------------------------------------------------------------------------
  8514. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8515. TPreferencesMode APreferencesMode)
  8516. {
  8517. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8518. TCopyParamRuleData Data;
  8519. if (Terminal != NULL)
  8520. {
  8521. PreferencesData.reset(new TPreferencesDialogData());
  8522. GetTransferPresetAutoSelectData(Data);
  8523. PreferencesData->CopyParamRuleData = &Data;
  8524. }
  8525. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8526. }
  8527. //---------------------------------------------------------------------------
  8528. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8529. const TCustomCommandType & Command)
  8530. {
  8531. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8532. {
  8533. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8534. }
  8535. }
  8536. //---------------------------------------------------------------------------
  8537. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8538. {
  8539. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8540. TCustomCommandType Command;
  8541. // make sure we use local custom command when remote are not supported
  8542. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8543. {
  8544. Command = FLastCustomCommand;
  8545. }
  8546. else
  8547. {
  8548. Command.Params = Command.Params | ccLocal;
  8549. }
  8550. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8551. FEditingFocusedAdHocCommand = OnFocused;
  8552. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8553. int Options =
  8554. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8555. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8556. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8557. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8558. {
  8559. FLastCustomCommand = Command;
  8560. UpdateCustomCommandsToolbar();
  8561. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8562. }
  8563. }
  8564. //---------------------------------------------------------------------------
  8565. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8566. {
  8567. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8568. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8569. DebugAssert(State > 0);
  8570. if (State <= 0)
  8571. {
  8572. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8573. }
  8574. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8575. }
  8576. //---------------------------------------------------------------------------
  8577. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8578. TCustomCommandType & Command, int & State)
  8579. {
  8580. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8581. if (Result)
  8582. {
  8583. Command = FLastCustomCommand;
  8584. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8585. }
  8586. return Result;
  8587. }
  8588. //---------------------------------------------------------------------------
  8589. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8590. {
  8591. ResetIncrementalSearch();
  8592. }
  8593. //---------------------------------------------------------------------------
  8594. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8595. {
  8596. DebugAssert(ComponentVisible[Component]);
  8597. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8598. }
  8599. //---------------------------------------------------------------------------
  8600. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8601. {
  8602. // when queue panel is resized here directly, the status bar is stretched
  8603. // over whole space the panel occupied
  8604. PostComponentHide(fcQueueView);
  8605. }
  8606. //---------------------------------------------------------------------------
  8607. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8608. {
  8609. ComponentVisible[fcQueueFileList] = false;
  8610. }
  8611. //---------------------------------------------------------------------------
  8612. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8613. {
  8614. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8615. RefreshColorMode();
  8616. ResetSysDarkTheme();
  8617. ConfigurationChanged();
  8618. ConfigureInterface();
  8619. // Should be called for all controls
  8620. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8621. }
  8622. //---------------------------------------------------------------------------
  8623. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8624. {
  8625. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8626. if (!FInvalid &&
  8627. (Message.LParam != 0) &&
  8628. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8629. {
  8630. ThemeChanged();
  8631. }
  8632. TForm::Dispatch(&Message);
  8633. }
  8634. //---------------------------------------------------------------------------
  8635. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8636. {
  8637. TMessage * M = static_cast<TMessage*>(Message);
  8638. switch (M->Msg)
  8639. {
  8640. case CM_DIALOGCHAR:
  8641. CMDialogChar(*M);
  8642. break;
  8643. case WM_APPCOMMAND:
  8644. WMAppCommand(*M);
  8645. break;
  8646. case WM_SYSCOMMAND:
  8647. WMSysCommand(*M);
  8648. break;
  8649. case WM_QUERYENDSESSION:
  8650. WMQueryEndSession(*M);
  8651. break;
  8652. case WM_ENDSESSION:
  8653. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8654. break;
  8655. #ifdef _DEBUG
  8656. case WM_WINDOWPOSCHANGED:
  8657. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8658. break;
  8659. case CM_CUSTOMSTYLECHANGED:
  8660. TForm::Dispatch(Message);
  8661. break;
  8662. #endif
  8663. case WM_COMPONENT_HIDE:
  8664. {
  8665. Byte Component = static_cast<Byte>(M->WParam);
  8666. // sanity check
  8667. if (ComponentVisible[Component])
  8668. {
  8669. // special treatment
  8670. if (Component == fcQueueView)
  8671. {
  8672. ToggleQueueVisibility();
  8673. DebugAssert(!ComponentVisible[fcQueueView]);
  8674. }
  8675. else
  8676. {
  8677. ComponentVisible[Component] = false;
  8678. }
  8679. }
  8680. }
  8681. break;
  8682. case WM_COPYDATA:
  8683. WMCopyData(*M);
  8684. break;
  8685. case WM_MANAGES_CAPTION:
  8686. // caption managed in TTerminalManager::UpdateAppTitle()
  8687. M->Result = 1;
  8688. break;
  8689. case WM_WANTS_MOUSEWHEEL:
  8690. M->Result = 1;
  8691. break;
  8692. case CM_SHOWINGCHANGED:
  8693. CMShowingChanged(*M);
  8694. break;
  8695. case WM_CLOSE:
  8696. WMClose(*M);
  8697. break;
  8698. case WM_DPICHANGED:
  8699. WMDpiChanged(*M);
  8700. break;
  8701. case CM_DPICHANGED:
  8702. CMDpiChanged(*M);
  8703. break;
  8704. case WM_SETTINGCHANGE:
  8705. WMSettingChange(*M);
  8706. break;
  8707. case CM_DIALOGKEY:
  8708. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8709. break;
  8710. default:
  8711. TForm::Dispatch(Message);
  8712. break;
  8713. }
  8714. }
  8715. //---------------------------------------------------------------------------
  8716. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8717. {
  8718. // noop
  8719. }
  8720. //---------------------------------------------------------------------------
  8721. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8722. {
  8723. TForm::Dispatch(&Message);
  8724. FSessionColors->Clear();
  8725. AddFixedSessionImages();
  8726. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8727. UpdateImages();
  8728. }
  8729. //---------------------------------------------------------------------------
  8730. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8731. {
  8732. TForm::Dispatch(&Message);
  8733. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8734. UpdatePixelsPerInchMainWindowCounter();
  8735. }
  8736. //---------------------------------------------------------------------------
  8737. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8738. {
  8739. // Cannot close window while we are busy.
  8740. // We cannot test this in FormCloseQuery as that is called also from
  8741. // Close(), which is called by CloseApplicationAction. So we can be busy
  8742. // there already even, when it is legitimate to close the application.
  8743. // Possibly a better place to handle this would be WMSysCommand.
  8744. if (NonVisualDataModule->Busy)
  8745. {
  8746. Message.Result = 1;
  8747. }
  8748. else
  8749. {
  8750. TForm::Dispatch(&Message);
  8751. }
  8752. }
  8753. //---------------------------------------------------------------------------
  8754. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8755. {
  8756. // Ignoring implicit local-local browser of Commander, opened from DoShow
  8757. bool NoSession = (ManagedSession == NULL);
  8758. // This calls TCustomScpExplorerForm::DoShow
  8759. TForm::Dispatch(&Message);
  8760. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  8761. if (Showing && !FStarted)
  8762. {
  8763. FStarted = true;
  8764. InterfaceStarted();
  8765. }
  8766. if (Showing && NoSession)
  8767. {
  8768. // When we are starting minimized (i.e. from an installer),
  8769. // postpone showing Login dialog until we get restored.
  8770. // Otherwise the Login dialog (and Authentication window) show restored
  8771. // over invisible (minimized) main window.
  8772. if (WindowState == wsMinimized)
  8773. {
  8774. FNeedSession = true;
  8775. }
  8776. else
  8777. {
  8778. // This happens before application ever goes idle, so the toolbars would
  8779. // stay enabled (initial state) until the Login dialog is dismissed.
  8780. UpdateActions();
  8781. NonVisualDataModule->StartBusy();
  8782. try
  8783. {
  8784. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8785. // otherwise the dialog does not receive focus.
  8786. // With Commander interface the ProcessMessages is called already
  8787. // by TDriveView, but with Explorer interface, we need to call it explicily
  8788. Application->ProcessMessages();
  8789. // do not reload sessions, they have been loaded just now (optimization)
  8790. NeedSession(true);
  8791. }
  8792. __finally
  8793. {
  8794. NonVisualDataModule->EndBusy();
  8795. }
  8796. }
  8797. }
  8798. }
  8799. //---------------------------------------------------------------------------
  8800. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8801. {
  8802. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8803. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8804. }
  8805. //---------------------------------------------------------------------------
  8806. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8807. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8808. int & MaxHeight)
  8809. {
  8810. // workaround for bug in TWinControl.CalcConstraints
  8811. // Check for empty rect (restore from iconic state) is done there only after
  8812. // call to AdjustClientRect, which enlarges the rect (for forms).
  8813. TRect R = GetClientRect();
  8814. // when restoring from iconic state, place no restrictions
  8815. if (IsRectEmpty(R))
  8816. {
  8817. MinWidth = 0;
  8818. MinHeight = 0;
  8819. MaxWidth = 32000;
  8820. MaxHeight = 32000;
  8821. }
  8822. }
  8823. //---------------------------------------------------------------------------
  8824. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8825. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8826. {
  8827. // terminal can be already closed (e.g. dropped connection)
  8828. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8829. {
  8830. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8831. try
  8832. {
  8833. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8834. }
  8835. catch(...)
  8836. {
  8837. if (!Terminal->Active)
  8838. {
  8839. Close = true;
  8840. }
  8841. throw;
  8842. }
  8843. }
  8844. }
  8845. //---------------------------------------------------------------------------
  8846. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8847. {
  8848. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8849. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8850. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8851. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8852. {
  8853. OnGetSpaceAvailable = GetSpaceAvailable;
  8854. }
  8855. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8856. OnGetSpaceAvailable);
  8857. }
  8858. //---------------------------------------------------------------------------
  8859. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8860. {
  8861. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8862. Terminal->FillSessionDataForCode(Data.get());
  8863. return Data.release();
  8864. }
  8865. //---------------------------------------------------------------------------
  8866. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8867. {
  8868. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8869. DoGenerateUrlDialog(Data.get(), Paths);
  8870. }
  8871. //---------------------------------------------------------------------------
  8872. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8873. {
  8874. GenerateUrl(NULL);
  8875. }
  8876. //---------------------------------------------------------------------------
  8877. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8878. {
  8879. std::unique_ptr<TStrings> Paths(new TStringList());
  8880. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8881. GenerateUrl(Paths.get());
  8882. }
  8883. //---------------------------------------------------------------------------
  8884. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8885. {
  8886. FSessionColor = value;
  8887. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8888. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8889. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8890. DebugAssert(ColorItem != NULL);
  8891. ColorItem->Color = C;
  8892. NonVisualDataModule->ColorMenuItem->Color = C;
  8893. UpdateControls();
  8894. }
  8895. //---------------------------------------------------------------------------
  8896. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8897. {
  8898. if (value != FSessionColor)
  8899. {
  8900. UpdateSessionColor(value);
  8901. // Is null when called from LastTerminalClosed
  8902. if (Terminal != NULL)
  8903. {
  8904. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8905. }
  8906. }
  8907. }
  8908. //---------------------------------------------------------------------------
  8909. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8910. {
  8911. bool Result = FNoteTimer->Enabled;
  8912. if (Result)
  8913. {
  8914. // cannot cancel note too early
  8915. bool NotEarly =
  8916. (Now() - FNoteShown >
  8917. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8918. if (Force || NotEarly)
  8919. {
  8920. FNoteTimer->Enabled = false;
  8921. FNote = L"";
  8922. SAFE_DESTROY(FNoteData);
  8923. FOnNoteClick = NULL;
  8924. FNoteHints = FNotes->Text;
  8925. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8926. UpdateStatusBar();
  8927. }
  8928. }
  8929. return Result;
  8930. }
  8931. //---------------------------------------------------------------------------
  8932. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8933. {
  8934. DebugAssert(FNoteTimer->Enabled);
  8935. CancelNote(true);
  8936. }
  8937. //---------------------------------------------------------------------------
  8938. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  8939. {
  8940. FNoteHints = FNotes->Text.TrimRight();
  8941. }
  8942. //---------------------------------------------------------------------------
  8943. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8944. {
  8945. int P = Note.Pos(L"\n");
  8946. if (P > 0)
  8947. {
  8948. Note.SetLength(P - 1);
  8949. }
  8950. FNotes->Add(FORMAT(L"[%s] %s",
  8951. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8952. while (FNotes->Count > 10)
  8953. {
  8954. FNotes->Delete(0);
  8955. }
  8956. if (UpdateNow)
  8957. {
  8958. UpdateNoteHints();
  8959. UpdateStatusBar();
  8960. }
  8961. }
  8962. //---------------------------------------------------------------------------
  8963. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8964. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8965. {
  8966. int P = Note.Pos(L"\n");
  8967. if (P > 0)
  8968. {
  8969. Note.SetLength(P - 1);
  8970. }
  8971. UpdateNoteHints();
  8972. FNote = Note;
  8973. SAFE_DESTROY(FNoteData);
  8974. FNoteData = NoteData;
  8975. FOnNoteClick = OnNoteClick;
  8976. AddNote(Note, false);
  8977. UpdateStatusBar();
  8978. FNoteShown = Now();
  8979. FNoteTimer->Enabled = false;
  8980. if (Seconds == 0)
  8981. {
  8982. Seconds = WinConfiguration->NotificationsTimeout;
  8983. }
  8984. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8985. FNoteTimer->Enabled = true;
  8986. }
  8987. //---------------------------------------------------------------------------
  8988. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8989. {
  8990. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8991. }
  8992. //---------------------------------------------------------------------------
  8993. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8994. {
  8995. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  8996. {
  8997. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8998. }
  8999. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9000. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9001. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9002. 0, NULL, NULL);
  9003. }
  9004. //---------------------------------------------------------------------------
  9005. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9006. {
  9007. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  9008. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  9009. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  9010. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9011. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9012. }
  9013. //---------------------------------------------------------------------------
  9014. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9015. {
  9016. WinConfiguration->FormatSizeBytes = Style;
  9017. }
  9018. //---------------------------------------------------------------------------
  9019. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9020. {
  9021. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9022. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9023. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9024. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9025. }
  9026. //---------------------------------------------------------------------------
  9027. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9028. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9029. {
  9030. if (Panel->Index == 0)
  9031. {
  9032. if (FOnNoteClick != NULL)
  9033. {
  9034. // prevent the user data from being freed by possible call
  9035. // to CancelNote or PostNote during call to OnNoteClick
  9036. std::unique_ptr<TObject> NoteData(FNoteData);
  9037. TNotifyEvent OnNoteClick = FOnNoteClick;
  9038. FNoteData = NULL;
  9039. // need to cancel the note as we are going to delete its user data
  9040. CancelNote(true);
  9041. OnNoteClick(NoteData.get());
  9042. }
  9043. }
  9044. if (HasActiveTerminal() &&
  9045. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9046. {
  9047. FileSystemInfo();
  9048. }
  9049. }
  9050. //---------------------------------------------------------------------------
  9051. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9052. {
  9053. // workaround:
  9054. // 1) for unknown reason, disabling window, while minimized,
  9055. // prevents it from restoring, even if it was enabled again meanwhile
  9056. // 2) when disabling the main window, while another has focus
  9057. // minimize is no longer possible
  9058. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9059. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9060. // Shouldn't we use IsApplicationMinimized() here?
  9061. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9062. {
  9063. Enabled = false;
  9064. }
  9065. FLockLevel++;
  9066. }
  9067. //---------------------------------------------------------------------------
  9068. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9069. {
  9070. DebugAssert(FLockLevel > 0);
  9071. FLockLevel--;
  9072. if (FLockLevel == 0)
  9073. {
  9074. DebugAssert(FLockSuspendLevel == 0);
  9075. Enabled = true;
  9076. }
  9077. }
  9078. //---------------------------------------------------------------------------
  9079. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9080. {
  9081. // We need to make sure that window is enabled when the last modal window closes
  9082. // otherwise focus is not restored correctly.
  9083. // So we re-enable the window when modal window opens and
  9084. // disable it back after is closes
  9085. if (FLockLevel > 0)
  9086. {
  9087. // while we have nesting counter, we know that we never be called
  9088. // recursivelly as Application->OnModalBegin is called only
  9089. // for the top-level modal window
  9090. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9091. {
  9092. // won't be disabled when conditions in LockWindow() were not satisfied
  9093. FDisabledOnLockSuspend = !Enabled;
  9094. // When minimized to tray (or actually when set to SW_HIDE),
  9095. // setting Enabled makes the window focusable even when there's
  9096. // modal window over it
  9097. if (!FTrayIcon->Visible)
  9098. {
  9099. Enabled = true;
  9100. }
  9101. }
  9102. FLockSuspendLevel++;
  9103. }
  9104. }
  9105. //---------------------------------------------------------------------------
  9106. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9107. {
  9108. if (FLockSuspendLevel > 0)
  9109. {
  9110. DebugAssert(FLockLevel > 0);
  9111. FLockSuspendLevel--;
  9112. // see comment in SuspendWindowLock
  9113. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9114. {
  9115. // Note that window can be enabled here, when we were minized to tray when
  9116. // was SuspendWindowLock() called.
  9117. // We should possibly do the same check as in LockWindow(),
  9118. // if it is ever possible that the consitions change between
  9119. // SuspendWindowLock() and ResumeWindowLock()
  9120. Enabled = !FDisabledOnLockSuspend;
  9121. }
  9122. }
  9123. }
  9124. //---------------------------------------------------------------------------
  9125. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9126. {
  9127. TTBXComboBoxItem * RemotePathComboBox =
  9128. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9129. if (!TextOnly)
  9130. {
  9131. TStrings * Items = RemotePathComboBox->Strings;
  9132. Items->BeginUpdate();
  9133. try
  9134. {
  9135. Items->Clear();
  9136. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9137. {
  9138. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9139. while (!IsUnixRootPath(APath))
  9140. {
  9141. int P = APath.LastDelimiter(L'/');
  9142. DebugAssert(P >= 0);
  9143. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9144. APath.SetLength(P - 1);
  9145. }
  9146. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9147. }
  9148. }
  9149. __finally
  9150. {
  9151. Items->EndUpdate();
  9152. }
  9153. }
  9154. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9155. // Setting ItemIndex to -1 does not reset its text
  9156. if (RemotePathComboBox->Strings->Count == 0)
  9157. {
  9158. RemotePathComboBox->Text = L"";
  9159. }
  9160. }
  9161. //---------------------------------------------------------------------------
  9162. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9163. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9164. {
  9165. if (AIndex < 0)
  9166. {
  9167. AIndex = Sender->ItemIndex;
  9168. }
  9169. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9170. }
  9171. //---------------------------------------------------------------------------
  9172. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9173. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9174. int & ImageIndex)
  9175. {
  9176. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9177. }
  9178. //---------------------------------------------------------------------------
  9179. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9180. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9181. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9182. {
  9183. if (!IsLocalBrowserMode())
  9184. {
  9185. ARect.Left += (10 * AIndex);
  9186. }
  9187. }
  9188. //---------------------------------------------------------------------------
  9189. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9190. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9191. {
  9192. AWidth += (10 * AIndex);
  9193. }
  9194. //---------------------------------------------------------------------------
  9195. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9196. {
  9197. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9198. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9199. int Index = RemotePathComboBox->ItemIndex;
  9200. while (Index < RemotePathComboBox->Strings->Count - 1)
  9201. {
  9202. APath = UnixExtractFileDir(APath);
  9203. Index++;
  9204. }
  9205. // VanDyke style paths
  9206. if (APath.IsEmpty())
  9207. {
  9208. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9209. APath = ROOTDIRECTORY;
  9210. }
  9211. if (RemoteDirView->Path != APath)
  9212. {
  9213. RemoteDirView->Path = APath;
  9214. }
  9215. }
  9216. //---------------------------------------------------------------------------
  9217. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9218. {
  9219. DoRemotePathComboBoxItemClick(Sender);
  9220. }
  9221. //---------------------------------------------------------------------------
  9222. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9223. {
  9224. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9225. DebugUsedParam(Sender);
  9226. UpdateRemotePathComboBox(true);
  9227. }
  9228. //---------------------------------------------------------------------------
  9229. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9230. {
  9231. DoRemotePathComboBoxCancel(Sender);
  9232. }
  9233. //---------------------------------------------------------------------------
  9234. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9235. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9236. {
  9237. ResetIncrementalSearch();
  9238. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9239. DebugAssert(DirView != NULL);
  9240. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9241. {
  9242. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9243. // OnEditing is called also from TCustomListView::CanEdit
  9244. if (Edit != NULL)
  9245. {
  9246. EditSelectBaseName(Edit);
  9247. }
  9248. }
  9249. }
  9250. //---------------------------------------------------------------------------
  9251. //---------------------------------------------------------------------------
  9252. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9253. {
  9254. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9255. Result->AutoDetectDnD = false;
  9256. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9257. Result->RenderDataOn = rdoEnterAndDropSync;
  9258. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9259. return Result;
  9260. }
  9261. //---------------------------------------------------------------------------
  9262. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9263. {
  9264. if (IsWin10Build(2004))
  9265. {
  9266. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9267. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9268. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9269. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9270. }
  9271. }
  9272. //---------------------------------------------------------------------------
  9273. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9274. {
  9275. TForm::CreateWnd();
  9276. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9277. UpdateDarkMode();
  9278. if (FSessionsDragDropFilesEx == NULL)
  9279. {
  9280. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9281. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9282. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9283. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9284. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9285. }
  9286. if (FQueueDragDropFilesEx == NULL)
  9287. {
  9288. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9289. // No need to set OnDragOver as we do not have any restrictions
  9290. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9291. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9292. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9293. }
  9294. }
  9295. //---------------------------------------------------------------------------
  9296. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9297. {
  9298. TForm::DestroyWnd();
  9299. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9300. FQueueDragDropFilesEx->DragDropControl = NULL;
  9301. }
  9302. //---------------------------------------------------------------------------
  9303. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9304. {
  9305. SideEnter(FCurrentSide);
  9306. FEverShown = true;
  9307. }
  9308. //---------------------------------------------------------------------------
  9309. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9310. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9311. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9312. {
  9313. if (!NonVisualDataModule->Busy)
  9314. {
  9315. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9316. Configuration->Usage->Inc(L"FileFinds");
  9317. LockWindow(true);
  9318. NonVisualDataModule->StartBusy();
  9319. WinConfiguration->LockedInterface = true;
  9320. try
  9321. {
  9322. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9323. }
  9324. __finally
  9325. {
  9326. WinConfiguration->LockedInterface = false;
  9327. NonVisualDataModule->EndBusy();
  9328. UnlockWindow();
  9329. }
  9330. }
  9331. }
  9332. //---------------------------------------------------------------------------
  9333. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9334. {
  9335. if (!NonVisualDataModule->Busy)
  9336. {
  9337. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9338. SetFocus();
  9339. RemoteDirView->Path = UnixExtractFilePath(Path);
  9340. UnicodeString FileName = UnixExtractFileName(Path);
  9341. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9342. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9343. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9344. if (Item == NULL)
  9345. {
  9346. ReloadDirectory(osRemote);
  9347. Item = RemoteDirView->FindFileItem(FileName);
  9348. }
  9349. if (Item != NULL)
  9350. {
  9351. RemoteDirView->ItemFocused = Item;
  9352. RemoteDirView->ItemFocused->MakeVisible(false);
  9353. RemoteDirView->SetFocus();
  9354. }
  9355. }
  9356. }
  9357. //---------------------------------------------------------------------------
  9358. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9359. {
  9360. bool Result = !NonVisualDataModule->Busy;
  9361. if (Result)
  9362. {
  9363. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9364. }
  9365. return Result;
  9366. }
  9367. //---------------------------------------------------------------------------
  9368. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9369. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9370. {
  9371. if (CanOperateOnFoundFiles(ATerminal))
  9372. {
  9373. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9374. FOnFileOperationFinished = OnFileOperationFinished;
  9375. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9376. }
  9377. }
  9378. //---------------------------------------------------------------------------
  9379. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9380. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9381. {
  9382. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9383. }
  9384. //---------------------------------------------------------------------------
  9385. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9386. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9387. {
  9388. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9389. }
  9390. //---------------------------------------------------------------------------
  9391. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9392. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9393. {
  9394. if (CanOperateOnFoundFiles(ATerminal))
  9395. {
  9396. for (int Index = 0; Index < FileList->Count; Index++)
  9397. {
  9398. UnicodeString FileName = FileList->Strings[Index];
  9399. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9400. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9401. OnFileOperationFinished(FileName, true);
  9402. }
  9403. }
  9404. }
  9405. //---------------------------------------------------------------------------
  9406. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9407. {
  9408. FFileFindTerminal = Terminal;
  9409. ShowFileFindDialog(
  9410. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9411. DoDownloadFoundFiles, DoEditFoundFiles);
  9412. }
  9413. //---------------------------------------------------------------------------
  9414. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9415. {
  9416. FTaskbarList = TaskbarList;
  9417. }
  9418. //---------------------------------------------------------------------------
  9419. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9420. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9421. {
  9422. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9423. if (Index >= 0)
  9424. {
  9425. if (Button == mbLeft)
  9426. {
  9427. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9428. // If switching tab (switching session) takes long enough for user
  9429. // to actually release the button, "mouse down" is still raised,
  9430. // but we do not get "mouse up" event, so dragging is not cancelled,
  9431. // prevent that by not beginning dragging in the first place.
  9432. if (IsKeyPressed(VK_LBUTTON))
  9433. {
  9434. // when user clicks the "+", we get mouse down only after the session
  9435. // is closed, when new session tab is already on X:Y, so dragging
  9436. // starts, prevent that
  9437. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9438. {
  9439. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9440. if (Terminal != NULL)
  9441. {
  9442. SessionsPageControl->BeginDrag(false);
  9443. }
  9444. }
  9445. }
  9446. }
  9447. else if (Button == mbMiddle)
  9448. {
  9449. // ignore middle-click for "New session tab" and the last local-local tab
  9450. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9451. if ((Session != NULL) && CanCloseSession(Session))
  9452. {
  9453. CloseSessionTab(Index);
  9454. }
  9455. }
  9456. }
  9457. }
  9458. //---------------------------------------------------------------------------
  9459. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9460. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9461. {
  9462. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9463. // do not allow dropping on the "+" tab
  9464. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9465. if ((TargetTerminal != NULL) &&
  9466. (SessionsPageControl->ActivePage->PageIndex != Index))
  9467. {
  9468. Configuration->Usage->Inc(L"SessionTabMoves");
  9469. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9470. // but we want to actually prevent that to avoid flicker
  9471. SessionsPageControl->ActivePage->PageIndex = Index;
  9472. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9473. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9474. }
  9475. }
  9476. //---------------------------------------------------------------------------
  9477. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9478. TObject * Sender, TObject * Source, int X, int Y,
  9479. TDragState /*State*/, bool & Accept)
  9480. {
  9481. Accept = (Sender == Source);
  9482. if (Accept)
  9483. {
  9484. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9485. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9486. // do not allow dragging to the "+" tab
  9487. Accept = (Terminal != NULL);
  9488. }
  9489. }
  9490. //---------------------------------------------------------------------------
  9491. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9492. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9493. {
  9494. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9495. if (Index < 0)
  9496. {
  9497. Effect = DROPEFFECT_NONE;
  9498. }
  9499. else
  9500. {
  9501. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9502. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9503. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9504. {
  9505. Effect = DROPEFFECT_NONE;
  9506. }
  9507. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9508. {
  9509. Effect = DROPEFFECT_COPY;
  9510. }
  9511. }
  9512. }
  9513. //---------------------------------------------------------------------------
  9514. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9515. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9516. {
  9517. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9518. // do not allow dropping on local-local, disconnected and the "+" tab
  9519. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9520. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9521. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9522. {
  9523. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9524. if (!IsFileControl(DropSourceControl, osRemote))
  9525. {
  9526. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9527. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9528. // Why don't we use Terminal->CurrentDirectory directly?
  9529. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9530. // do not force queue, drag drop
  9531. false, true);
  9532. }
  9533. }
  9534. }
  9535. //---------------------------------------------------------------------------
  9536. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9537. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9538. {
  9539. // Downloads are handled in RemoteFileControlDDEnd
  9540. if (!IsFileControl(DropSourceControl, osRemote))
  9541. {
  9542. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9543. Terminal->CurrentDirectory,
  9544. // force queue, drag drop
  9545. true, true);
  9546. }
  9547. }
  9548. //---------------------------------------------------------------------------
  9549. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9550. {
  9551. FShowing = false;
  9552. // Do not save empty workspace
  9553. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9554. {
  9555. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9556. UnicodeString Name = WorkspaceName();
  9557. DoSaveWorkspace(
  9558. Name, DataList.get(),
  9559. !Configuration->DisablePasswordStoring &&
  9560. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9561. WinConfiguration->LastStoredSession = Name;
  9562. }
  9563. }
  9564. //---------------------------------------------------------------------------
  9565. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9566. {
  9567. TManagedTerminal * ManagedTerminal =
  9568. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9569. if (ManagedTerminal != NULL)
  9570. {
  9571. ManagedTerminal->DirectoryLoaded = Now();
  9572. }
  9573. }
  9574. //---------------------------------------------------------------------------
  9575. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9576. TListItem * /*Item*/, bool /*Selected*/)
  9577. {
  9578. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9579. switch (DirView->LastSelectMethod)
  9580. {
  9581. case smKeyboard:
  9582. Configuration->Usage->Inc(L"KeyboardSelections");
  9583. break;
  9584. case smMouse:
  9585. Configuration->Usage->Inc(L"MouseSelections");
  9586. break;
  9587. }
  9588. }
  9589. //---------------------------------------------------------------------------
  9590. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9591. {
  9592. if (Color != 0)
  9593. {
  9594. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9595. }
  9596. else
  9597. {
  9598. return FSessionTabImageIndex;
  9599. }
  9600. }
  9601. //---------------------------------------------------------------------------
  9602. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9603. {
  9604. TPngImageCollectionItem * Item =
  9605. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9606. FSessionColors->AddPng(Item->PngImage);
  9607. return FSessionColors->Count - 1;
  9608. }
  9609. //---------------------------------------------------------------------------
  9610. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9611. {
  9612. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9613. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  9614. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  9615. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9616. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9617. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  9618. }
  9619. //---------------------------------------------------------------------------
  9620. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  9621. {
  9622. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  9623. }
  9624. //---------------------------------------------------------------------------
  9625. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  9626. {
  9627. switch (Side)
  9628. {
  9629. case osLocal:
  9630. return FNewLocalTabTabImageIndex;
  9631. case osRemote:
  9632. case osCurrent:
  9633. return FNewRemoteTabTabImageIndex;
  9634. default:
  9635. DebugFail();
  9636. return -1;
  9637. }
  9638. }
  9639. //---------------------------------------------------------------------------
  9640. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9641. {
  9642. for (int Index = 0; Index < Control->ControlCount; Index++)
  9643. {
  9644. TControl * AControl = Control->Controls[Index];
  9645. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9646. if (WinControl != NULL)
  9647. {
  9648. CollectItemsWithTextDisplayMode(WinControl);
  9649. }
  9650. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9651. if (Toolbar != NULL)
  9652. {
  9653. // we care for top-level items only
  9654. TTBCustomItem * Items = Toolbar->Items;
  9655. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9656. {
  9657. TTBCustomItem * Item = Items->Items[ItemIndex];
  9658. if (((Item->DisplayMode == nbdmImageAndText) ||
  9659. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9660. EligibleForImageDisplayMode(Item))
  9661. {
  9662. FItemsWithTextDisplayMode.insert(Item);
  9663. }
  9664. }
  9665. }
  9666. }
  9667. }
  9668. //---------------------------------------------------------------------------
  9669. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9670. {
  9671. return true;
  9672. }
  9673. //---------------------------------------------------------------------------
  9674. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9675. {
  9676. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9677. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9678. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9679. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9680. bool Result = true;
  9681. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9682. {
  9683. TTBCustomItem * Item = *i;
  9684. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9685. if (Label != NULL)
  9686. {
  9687. // optimization
  9688. if (Label->Visible == SelectiveToolbarText)
  9689. {
  9690. Result = false;
  9691. }
  9692. else
  9693. {
  9694. Label->Visible = SelectiveToolbarText;
  9695. }
  9696. }
  9697. else
  9698. {
  9699. // optimization
  9700. if (Item->DisplayMode == DisplayMode)
  9701. {
  9702. Result = false;
  9703. }
  9704. else
  9705. {
  9706. Item->DisplayMode = DisplayMode;
  9707. }
  9708. }
  9709. ++i;
  9710. }
  9711. UpdateNewTabTab();
  9712. return Result;
  9713. }
  9714. //---------------------------------------------------------------------------
  9715. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9716. {
  9717. DebugFail();
  9718. }
  9719. //---------------------------------------------------------------------------
  9720. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9721. {
  9722. // Among other, a lock level is non zero, while directory is being loaded,
  9723. // even when it happens as a result of dirview navigation,
  9724. // i.e. when TNonVisualDataModule is NOT FBusy.
  9725. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9726. // Among other this prevents a panel auto update to occur while
  9727. // directory is changing.
  9728. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9729. }
  9730. //---------------------------------------------------------------------------
  9731. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  9732. {
  9733. // While the window is disabled, we still seem to process menu shortcuts at least,
  9734. // so stop it at least here.
  9735. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9736. bool Result;
  9737. if (Allowed == aaUpdate)
  9738. {
  9739. Result = true;
  9740. }
  9741. else
  9742. {
  9743. Result = !NonVisualDataModule->Busy;
  9744. if (Result && (Allowed == aaShortCut))
  9745. {
  9746. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  9747. Action->Update();
  9748. Result = Action->Enabled;
  9749. }
  9750. }
  9751. return Result;
  9752. }
  9753. //---------------------------------------------------------------------------
  9754. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9755. {
  9756. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9757. }
  9758. //---------------------------------------------------------------------------
  9759. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9760. {
  9761. // This is somewhat redundant to LockWindow() call from
  9762. // TTerminalManager::TerminalReadDirectoryProgress.
  9763. // But disabling window is known not to block keyboard shorcuts
  9764. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9765. if (Busy > 0)
  9766. {
  9767. if (NonVisualDataModule->Busy)
  9768. {
  9769. State = false;
  9770. }
  9771. else
  9772. {
  9773. NonVisualDataModule->StartBusy();
  9774. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9775. LockWindow();
  9776. }
  9777. }
  9778. else if (Busy < 0)
  9779. {
  9780. UnlockWindow();
  9781. NonVisualDataModule->EndBusy();
  9782. }
  9783. else
  9784. {
  9785. State = NonVisualDataModule->Busy;
  9786. }
  9787. }
  9788. //---------------------------------------------------------------------------
  9789. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9790. {
  9791. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9792. if (Index >= 0)
  9793. {
  9794. TPopupMenu * PopupMenu = NULL;
  9795. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9796. if (Session != NULL)
  9797. {
  9798. SessionsPageControl->ActivePageIndex = Index;
  9799. if (DebugAlwaysTrue(SessionTabSwitched()))
  9800. {
  9801. // copied from TControl.WMContextMenu
  9802. SendCancelMode(SessionsPageControl);
  9803. // explicit popup instead of using PopupMenu property
  9804. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9805. // while connecting yet not-connected session and hence
  9806. // allowing an access to commands over not-completelly connected session
  9807. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  9808. }
  9809. }
  9810. else
  9811. {
  9812. if (SupportsLocalBrowser())
  9813. {
  9814. PopupMenu = NonVisualDataModule->NewTabPopup;
  9815. }
  9816. }
  9817. if (PopupMenu != NULL)
  9818. {
  9819. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9820. PopupMenu->PopupComponent = SessionsPageControl;
  9821. PopupMenu->Popup(Point.x, Point.y);
  9822. }
  9823. }
  9824. Handled = true;
  9825. }
  9826. //---------------------------------------------------------------------------
  9827. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9828. {
  9829. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  9830. }
  9831. //---------------------------------------------------------------------------
  9832. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9833. {
  9834. Configuration->Usage->Inc(L"PasswordChanges");
  9835. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9836. ChangePasswordData->DisableAuthentationsExceptPassword();
  9837. ChangePasswordData->ChangePassword = true;
  9838. std::unique_ptr<TTerminal> ChangePasswordSession(
  9839. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9840. ChangePasswordSession->AutoReadDirectory = false;
  9841. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9842. {
  9843. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9844. }
  9845. else
  9846. {
  9847. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9848. }
  9849. }
  9850. //---------------------------------------------------------------------------
  9851. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9852. {
  9853. // No nice way to assert SSH2
  9854. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  9855. }
  9856. //---------------------------------------------------------------------------
  9857. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9858. {
  9859. TOperationVisualizer OperationVisualizer;
  9860. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9861. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9862. }
  9863. //---------------------------------------------------------------------------
  9864. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9865. {
  9866. TForm::ChangeScale(M, D);
  9867. int APixelsPerInch = GetControlPixelsPerInch(this);
  9868. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9869. try
  9870. {
  9871. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9872. }
  9873. __finally
  9874. {
  9875. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9876. }
  9877. }
  9878. //---------------------------------------------------------------------------
  9879. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9880. {
  9881. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9882. }
  9883. //---------------------------------------------------------------------
  9884. class TPublicControl : public TControl
  9885. {
  9886. friend class TCustomScpExplorerForm;
  9887. };
  9888. //---------------------------------------------------------------------------
  9889. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9890. {
  9891. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9892. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9893. }
  9894. //---------------------------------------------------------------------------
  9895. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9896. {
  9897. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9898. OpenBookmark(Side, Bookmark);
  9899. }
  9900. //---------------------------------------------------------------------------
  9901. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9902. {
  9903. DoBookmarkClick(osLocal, Sender);
  9904. }
  9905. //---------------------------------------------------------------------------
  9906. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9907. {
  9908. DoBookmarkClick(osRemote, Sender);
  9909. }
  9910. //---------------------------------------------------------------------------
  9911. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9912. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9913. {
  9914. if (BookmarkList != NULL)
  9915. {
  9916. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9917. if (IsLocalBrowserMode())
  9918. {
  9919. Side = osLocal;
  9920. }
  9921. if (!WinConfiguration->UseLocationProfiles)
  9922. {
  9923. std::unique_ptr<TStringList> Directories(new TStringList());
  9924. Directories->CaseSensitive = (Side == osRemote);
  9925. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9926. {
  9927. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9928. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9929. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9930. {
  9931. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9932. Item->Caption = EscapeHotkey(Directory);
  9933. Item->ShortCut = Bookmark->ShortCut;
  9934. Item->OnClick = OnBookmarkClick;
  9935. Item->Tag = reinterpret_cast<int>(Bookmark);
  9936. Directories->Add(Directory);
  9937. Menu->Add(Item.release());
  9938. }
  9939. }
  9940. }
  9941. else
  9942. {
  9943. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9944. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9945. {
  9946. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9947. if (!Bookmark->Node.IsEmpty())
  9948. {
  9949. Folders->Add(Bookmark->Node);
  9950. }
  9951. }
  9952. for (int Index = 0; Index < Folders->Count; Index++)
  9953. {
  9954. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9955. Item->Caption = Folders->Strings[Index];
  9956. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  9957. Folders->Objects[Index] = Item.get();
  9958. Menu->Add(Item.release());
  9959. }
  9960. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9961. {
  9962. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9963. TTBCustomItem * Parent;
  9964. if (!Bookmark->Node.IsEmpty())
  9965. {
  9966. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9967. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9968. }
  9969. else
  9970. {
  9971. Parent = Menu;
  9972. }
  9973. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9974. Item->Caption = Bookmark->Name;
  9975. Item->ShortCut = Bookmark->ShortCut;
  9976. Item->OnClick = OnBookmarkClick;
  9977. Item->Tag = reinterpret_cast<int>(Bookmark);
  9978. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9979. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9980. {
  9981. // No hint for location profiles that are actually mere "bookmarks"
  9982. }
  9983. else
  9984. {
  9985. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9986. UnicodeString LongHint;
  9987. if (!Bookmark->Local.IsEmpty())
  9988. {
  9989. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9990. }
  9991. if (!Bookmark->Remote.IsEmpty())
  9992. {
  9993. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9994. }
  9995. AddToList(Hint, LongHint, L"|");
  9996. Item->Hint = Hint;
  9997. }
  9998. Parent->Add(Item.release());
  9999. }
  10000. }
  10001. AddMenuSeparator(Menu);
  10002. }
  10003. }
  10004. //---------------------------------------------------------------------------
  10005. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10006. {
  10007. Menu->Clear();
  10008. std::unique_ptr<TTBCustomItem> Item;
  10009. Item.reset(new TTBXItem(Owner));
  10010. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10011. Item->Options = Item->Options << tboDefault;
  10012. Menu->Add(Item.release());
  10013. AddMenuSeparator(Menu);
  10014. if (Terminal != NULL)
  10015. {
  10016. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10017. }
  10018. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10019. Item.reset(new TTBXItem(Owner));
  10020. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10021. Menu->Add(Item.release());
  10022. }
  10023. //---------------------------------------------------------------------------
  10024. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10025. {
  10026. bool Result = true;
  10027. bool Remote = !IsSideLocalBrowser(Side);
  10028. try
  10029. {
  10030. if (Remote)
  10031. {
  10032. Terminal->ExceptionOnFail = true;
  10033. }
  10034. try
  10035. {
  10036. DirView(Side)->Path = Path;
  10037. }
  10038. __finally
  10039. {
  10040. if (Remote)
  10041. {
  10042. Terminal->ExceptionOnFail = false;
  10043. }
  10044. }
  10045. }
  10046. catch (Exception & E)
  10047. {
  10048. if (!Remote || Terminal->Active)
  10049. {
  10050. ShowExtendedExceptionEx(Terminal, &E);
  10051. Result = false;
  10052. }
  10053. else
  10054. {
  10055. throw;
  10056. }
  10057. }
  10058. return Result;
  10059. }
  10060. //---------------------------------------------------------------------------
  10061. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10062. {
  10063. // Make sure there some feedback even when loading tiny local folders
  10064. TInstantOperationVisualizer Visualizer;
  10065. DirView(Side)->ReloadDirectory();
  10066. }
  10067. //---------------------------------------------------------------------------
  10068. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10069. {
  10070. if (Index == SessionsPageControl->TabIndex)
  10071. {
  10072. CloseSession();
  10073. }
  10074. else
  10075. {
  10076. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10077. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10078. TTerminalManager * Manager = TTerminalManager::Instance();
  10079. if (!Terminal->Active)
  10080. {
  10081. Manager->FreeTerminal(Terminal);
  10082. }
  10083. else
  10084. {
  10085. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10086. if (CanCloseQueue(Queue))
  10087. {
  10088. Manager->FreeTerminal(Terminal);
  10089. }
  10090. }
  10091. }
  10092. }
  10093. //---------------------------------------------------------------------------
  10094. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10095. {
  10096. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10097. if (Session == NULL)
  10098. {
  10099. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10100. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10101. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10102. //P = SessionsPageControl->ClientToScreen(P);
  10103. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10104. }
  10105. else
  10106. {
  10107. CloseSessionTab(Index);
  10108. }
  10109. }
  10110. //---------------------------------------------------------------------------
  10111. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10112. {
  10113. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10114. {
  10115. TInstantOperationVisualizer Visualizer;
  10116. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10117. // before we create a new data object
  10118. ClipboardClear();
  10119. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10120. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10121. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10122. // Intermediate variable to WORKAROUND a crash in compiler
  10123. TObjectList * ClipboardFakeMonitors =
  10124. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10125. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10126. if (FClipboardDragDropFilesEx.get() == NULL)
  10127. {
  10128. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10129. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10130. }
  10131. FClipboardDragDropFilesEx->FileList->Clear();
  10132. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10133. FClipboardDragDropFilesEx->CopyToClipboard();
  10134. FClipboardTerminal = Terminal;
  10135. // Need full paths, as cwd can be different once files are pasted
  10136. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10137. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10138. {
  10139. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10140. }
  10141. }
  10142. }
  10143. //---------------------------------------------------------------------------
  10144. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10145. {
  10146. if (OpenClipboard(0))
  10147. {
  10148. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10149. CloseClipboard();
  10150. }
  10151. }
  10152. //---------------------------------------------------------------------------
  10153. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10154. {
  10155. return !FClipboardFakeDirectory.IsEmpty();
  10156. }
  10157. //---------------------------------------------------------------------------
  10158. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10159. {
  10160. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10161. FClipboardFakeDirectory = UnicodeString();
  10162. FClipboardTerminal = NULL;
  10163. // Also called in Idle()
  10164. FClipboardFakeMonitors.reset(NULL);
  10165. }
  10166. //---------------------------------------------------------------------------
  10167. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10168. {
  10169. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10170. {
  10171. ClipboardStop();
  10172. }
  10173. }
  10174. //---------------------------------------------------------------------------
  10175. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10176. {
  10177. TTerminalManager * Manager = TTerminalManager::Instance();
  10178. Manager->ActiveSession = FClipboardTerminal;
  10179. TTransferOperationParam Params;
  10180. Params.TargetDirectory = TargetDirectory;
  10181. Params.DragDrop = DragDrop;
  10182. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10183. }
  10184. //---------------------------------------------------------------------------
  10185. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10186. {
  10187. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  10188. // (as it pumps a Windows message queue while being shutted down)
  10189. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10190. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10191. {
  10192. // Can fail as it can be e.g. locked by AV, so we retry that later
  10193. bool Removed = RemoveDir(ApiPath(FileName));
  10194. try
  10195. {
  10196. if (!NonVisualDataModule->Busy)
  10197. {
  10198. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10199. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10200. }
  10201. }
  10202. __finally
  10203. {
  10204. if (!Removed)
  10205. {
  10206. RemoveDir(ApiPath(FileName));
  10207. }
  10208. }
  10209. }
  10210. }
  10211. //---------------------------------------------------------------------------
  10212. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10213. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10214. {
  10215. TFileMasks::TParams MaskParams;
  10216. MaskParams.Size = Size;
  10217. MaskParams.Modification = Modification;
  10218. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10219. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10220. {
  10221. bool ImplicitMatch;
  10222. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  10223. !ImplicitMatch)
  10224. {
  10225. Color = Iter->Color;
  10226. break;
  10227. }
  10228. }
  10229. }
  10230. //---------------------------------------------------------------------------
  10231. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10232. {
  10233. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10234. if (!DirView->IsEditing())
  10235. {
  10236. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10237. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10238. // while searching
  10239. if (Key == VK_BACK)
  10240. {
  10241. if (!FIncrementalSearch.IsEmpty())
  10242. {
  10243. if (FIncrementalSearch.Length() == 1)
  10244. {
  10245. ResetIncrementalSearch();
  10246. }
  10247. else
  10248. {
  10249. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10250. IncrementalSearch(NewText, false, false);
  10251. }
  10252. Key = 0;
  10253. }
  10254. }
  10255. }
  10256. }
  10257. //---------------------------------------------------------------------------
  10258. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10259. {
  10260. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10261. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10262. {
  10263. // Filter control sequences.
  10264. // When not searching yet, prefer use of the space for toggling file selection
  10265. // (so we cannot incrementally search for a string starting with a space).
  10266. if ((Key > VK_SPACE) ||
  10267. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10268. {
  10269. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10270. Key = 0;
  10271. }
  10272. }
  10273. }
  10274. //---------------------------------------------------------------------------
  10275. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10276. {
  10277. if (!FIncrementalSearch.IsEmpty())
  10278. {
  10279. FIncrementalSearch = L"";
  10280. DirView(osCurrent)->UpdateStatusBar();
  10281. }
  10282. }
  10283. //---------------------------------------------------------------------------
  10284. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10285. {
  10286. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10287. if (Item == NULL)
  10288. {
  10289. MessageBeep(MB_ICONHAND);
  10290. }
  10291. else
  10292. {
  10293. TCustomDirView * ADirView = DirView(osCurrent);
  10294. {
  10295. TAutoNestingCounter Guard(FIncrementalSearching);
  10296. ADirView->FocusItem(Item);
  10297. if (ADirView->NortonLike == nlOff)
  10298. {
  10299. if ((ADirView->Selected != Item) ||
  10300. (ADirView->SelCount > 1))
  10301. {
  10302. ADirView->ClearSelection();
  10303. ADirView->Selected = Item;
  10304. }
  10305. }
  10306. }
  10307. FIncrementalSearch = Text;
  10308. Item->MakeVisible(false);
  10309. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10310. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10311. ADirView->UpdateStatusBar();
  10312. }
  10313. }
  10314. //---------------------------------------------------------------------------
  10315. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10316. {
  10317. int Index = Item->Index;
  10318. if (!Reverse)
  10319. {
  10320. Index++;
  10321. if (Index >= Item->Owner->Count)
  10322. {
  10323. Index = 0;
  10324. }
  10325. }
  10326. else
  10327. {
  10328. Index--;
  10329. if (Index < 0)
  10330. {
  10331. Index = Item->Owner->Count - 1;
  10332. }
  10333. }
  10334. return Item->Owner->Item[Index];
  10335. }
  10336. //---------------------------------------------------------------------------
  10337. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10338. {
  10339. TCustomDirView * ADirView = DirView(osCurrent);
  10340. if (ADirView->Items->Count == 0)
  10341. {
  10342. return NULL;
  10343. }
  10344. else
  10345. {
  10346. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10347. TListItem * Item = CurrentItem;
  10348. if (SkipCurrent)
  10349. {
  10350. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10351. }
  10352. while (true)
  10353. {
  10354. bool Matches = false;
  10355. switch (WinConfiguration->PanelSearch)
  10356. {
  10357. case isNameStartOnly:
  10358. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  10359. break;
  10360. case isName:
  10361. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  10362. break;
  10363. case isAll:
  10364. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10365. int Index = 0;
  10366. while ((Index < ColCount) && !Matches)
  10367. {
  10368. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10369. if (Visible)
  10370. {
  10371. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10372. }
  10373. Index++;
  10374. }
  10375. break;
  10376. }
  10377. if (Matches)
  10378. {
  10379. return Item;
  10380. }
  10381. Item = GetNextFile(Item, Reverse);
  10382. if (Item == CurrentItem)
  10383. {
  10384. return NULL;
  10385. }
  10386. }
  10387. }
  10388. }
  10389. //---------------------------------------------------------------------------
  10390. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10391. {
  10392. ResetIncrementalSearch();
  10393. }
  10394. //---------------------------------------------------------------------------
  10395. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10396. {
  10397. if (Message.CharCode == VK_TAB)
  10398. {
  10399. if (!FIncrementalSearch.IsEmpty())
  10400. {
  10401. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10402. bool Reverse = Shift.Contains(ssShift);
  10403. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10404. Message.Result = 1;
  10405. return;
  10406. }
  10407. }
  10408. else if (Message.CharCode == VK_ESCAPE)
  10409. {
  10410. if (!FIncrementalSearch.IsEmpty())
  10411. {
  10412. ResetIncrementalSearch();
  10413. Message.Result = 1;
  10414. return;
  10415. }
  10416. }
  10417. TForm::Dispatch(&Message);
  10418. }
  10419. //---------------------------------------------------------------------------
  10420. void __fastcall TCustomScpExplorerForm::Deactivate()
  10421. {
  10422. TForm::Deactivate();
  10423. // E.g. when switching to an internal editor window
  10424. ResetIncrementalSearch();
  10425. }
  10426. //---------------------------------------------------------------------------
  10427. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10428. {
  10429. // When switching to another application
  10430. ResetIncrementalSearch();
  10431. }
  10432. //---------------------------------------------------------------------------
  10433. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10434. {
  10435. ResetIncrementalSearch();
  10436. }
  10437. //---------------------------------------------------------------------------
  10438. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10439. {
  10440. if (FIncrementalSearching <= 0)
  10441. {
  10442. ResetIncrementalSearch();
  10443. }
  10444. }
  10445. //---------------------------------------------------------------------------
  10446. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10447. {
  10448. CountClicksForWindowPrint(this);
  10449. }
  10450. //---------------------------------------------------------------------------
  10451. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10452. {
  10453. CenterReconnectToolbar();
  10454. }
  10455. //---------------------------------------------------------------------------
  10456. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10457. {
  10458. if (RemoteDirView->ItemFocused != NULL)
  10459. {
  10460. RemoteDirView->ItemFocused->Selected = true;
  10461. }
  10462. }
  10463. //---------------------------------------------------------------------------
  10464. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10465. {
  10466. if (!FQueueStatusUpdating)
  10467. {
  10468. bool Refresh;
  10469. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10470. {
  10471. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10472. }
  10473. else
  10474. {
  10475. if (FQueueFileList->GetCount() > 0)
  10476. {
  10477. FQueueFileList->Clear();
  10478. Refresh = true;
  10479. }
  10480. }
  10481. int Count = FQueueFileList->GetCount();
  10482. if (QueueFileList->Items->Count != Count)
  10483. {
  10484. DebugAssert(Refresh);
  10485. QueueFileList->Items->Count = Count;
  10486. }
  10487. if (Refresh)
  10488. {
  10489. QueueFileList->Invalidate();
  10490. }
  10491. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10492. }
  10493. }
  10494. //---------------------------------------------------------------------------
  10495. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10496. {
  10497. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10498. }
  10499. //---------------------------------------------------------------------------
  10500. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10501. {
  10502. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10503. // See TSynchronizeChecklistDialog::UpdateTimer
  10504. if (QueueFileList->Items->Count > 0)
  10505. {
  10506. QueueFileList->Items->Item[0]->MakeVisible(false);
  10507. }
  10508. UpdateQueueFileList();
  10509. }
  10510. //---------------------------------------------------------------------------
  10511. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10512. {
  10513. Active = QueueView3->Focused() || QueueFileList->Focused();
  10514. }
  10515. //---------------------------------------------------------------------------
  10516. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10517. {
  10518. QueueLabelUpdateStatus();
  10519. }
  10520. //---------------------------------------------------------------------------
  10521. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10522. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10523. {
  10524. int State = FQueueFileList->GetState(Item->Index);
  10525. if (State == qfsProcessed)
  10526. {
  10527. Sender->Canvas->Font->Color = clGrayText;
  10528. }
  10529. }
  10530. //---------------------------------------------------------------------------
  10531. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10532. {
  10533. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10534. QueueFileList->Column[0]->Width =
  10535. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10536. }
  10537. //---------------------------------------------------------------------------
  10538. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10539. {
  10540. QueueFileListColumnAutoSize();
  10541. }
  10542. //---------------------------------------------------------------------------
  10543. void __fastcall TCustomScpExplorerForm::CloseApp()
  10544. {
  10545. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10546. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10547. // So we clear the flag here.
  10548. DebugAssert(NonVisualDataModule->Busy);
  10549. NonVisualDataModule->EndBusy();
  10550. try
  10551. {
  10552. Close();
  10553. }
  10554. __finally
  10555. {
  10556. NonVisualDataModule->StartBusy();
  10557. }
  10558. }
  10559. //---------------------------------------------------------------------------
  10560. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10561. {
  10562. return (Terminal != NULL) && Terminal->Active;
  10563. }
  10564. //---------------------------------------------------------------------------
  10565. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  10566. {
  10567. return (ManagedSession != NULL);
  10568. }
  10569. //---------------------------------------------------------------------------
  10570. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10571. {
  10572. return IsActiveTerminal(Terminal);
  10573. }
  10574. //---------------------------------------------------------------------------
  10575. void TCustomScpExplorerForm::LocalLocalCopy(
  10576. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  10577. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  10578. {
  10579. DebugFail();
  10580. }
  10581. //---------------------------------------------------------------------------
  10582. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  10583. {
  10584. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  10585. }
  10586. //---------------------------------------------------------------------------
  10587. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  10588. {
  10589. // Changing tab list triggers the OnResize.
  10590. if (!FUpdatingSessionTabs)
  10591. {
  10592. // UpdateTabsCaptionTruncation should be enough
  10593. SessionListChanged(true);
  10594. }
  10595. }
  10596. //---------------------------------------------------------------------------
  10597. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  10598. {
  10599. if (ASession == ManagedSession)
  10600. {
  10601. UpdateSession(ASession);
  10602. }
  10603. TSessionData * Data = ASession->StateData;
  10604. UnicodeString Result;
  10605. if (Side == osLocal)
  10606. {
  10607. Result = Data->LocalDirectory;
  10608. }
  10609. else if (DebugAlwaysTrue(Side == osOther))
  10610. {
  10611. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  10612. }
  10613. return Result;
  10614. }
  10615. //---------------------------------------------------------------------------
  10616. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  10617. {
  10618. UnicodeString Result;
  10619. if (!ASession->Active)
  10620. {
  10621. Result = LoadStr(STATUS_NOT_CONNECTED2);
  10622. }
  10623. else
  10624. {
  10625. Result = GetSessionPath(ASession, osRemote);
  10626. }
  10627. return Result;
  10628. }
  10629. //---------------------------------------------------------------------------
  10630. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  10631. {
  10632. return LoadStr(NEW_REMOTE_TAB_HINT);
  10633. }
  10634. //---------------------------------------------------------------------------
  10635. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  10636. {
  10637. if (Index >= 0)
  10638. {
  10639. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10640. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  10641. if (TabSession == NULL)
  10642. {
  10643. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  10644. if (NewTabTabCaption != TabSheet->Caption)
  10645. {
  10646. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  10647. }
  10648. }
  10649. else
  10650. {
  10651. TTerminalManager * Manager = TTerminalManager::Instance();
  10652. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  10653. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  10654. }
  10655. }
  10656. }
  10657. //---------------------------------------------------------------------------