CustomScpExplorer.cpp 347 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <Bookmarks.h>
  8. #include <Interface.h>
  9. #include <Exceptions.h>
  10. #include <CoreMain.h>
  11. #include <FileSystems.h>
  12. #include <TextsCore.h>
  13. #include <TextsWin.h>
  14. #include <HelpWin.h>
  15. #include <VCLCommon.h>
  16. #include <Progress.h>
  17. #include <SynchronizeProgress.h>
  18. #include <DragExt.h>
  19. #include <WinApi.h>
  20. #include "GUITools.h"
  21. #include "NonVisual.h"
  22. #include "Glyphs.h"
  23. #include "Tools.h"
  24. #include "WinConfiguration.h"
  25. #include "TerminalManager.h"
  26. #include "EditorManager.h"
  27. #include "ProgParams.h"
  28. #include "Setup.h"
  29. #include <Consts.hpp>
  30. #include <DateUtils.hpp>
  31. #include <TB2Common.hpp>
  32. #include <DirectoryMonitor.hpp>
  33. //---------------------------------------------------------------------------
  34. #pragma package(smart_init)
  35. #pragma link "CustomDirView"
  36. #pragma link "CustomUnixDirView"
  37. #pragma link "IEListView"
  38. #pragma link "NortonLikeListView"
  39. #pragma link "UnixDirView"
  40. #pragma link "CustomDriveView"
  41. #pragma link "UnixDriveView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "TB2Dock"
  44. #pragma link "TBXStatusBars"
  45. #pragma link "TB2Item"
  46. #pragma link "TB2Toolbar"
  47. #pragma link "ThemePageControl"
  48. #pragma link "PathLabel"
  49. #ifndef NO_RESOURCES
  50. #pragma resource "*.dfm"
  51. #endif
  52. //---------------------------------------------------------------------------
  53. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  54. static const int SessionPanelCount = 4;
  55. //---------------------------------------------------------------------------
  56. class TMutexGuard
  57. {
  58. public:
  59. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  60. int Timeout = 5000)
  61. {
  62. FMutex = NULL;
  63. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  64. if (WaitResult == WAIT_TIMEOUT)
  65. {
  66. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  67. }
  68. else
  69. {
  70. FMutex = AMutex;
  71. }
  72. }
  73. ~TMutexGuard()
  74. {
  75. if (FMutex != NULL)
  76. {
  77. ReleaseMutex(FMutex);
  78. }
  79. }
  80. private:
  81. HANDLE FMutex;
  82. };
  83. //---------------------------------------------------------------------------
  84. class TWindowLock
  85. {
  86. public:
  87. TWindowLock(TCustomScpExplorerForm * Form) :
  88. FForm(Form)
  89. {
  90. FForm->LockWindow();
  91. }
  92. ~TWindowLock()
  93. {
  94. FForm->UnlockWindow();
  95. }
  96. private:
  97. TCustomScpExplorerForm * FForm;
  98. };
  99. //---------------------------------------------------------------------------
  100. class TAutoBatch
  101. {
  102. public:
  103. TAutoBatch(TCustomScpExplorerForm * Form) :
  104. FForm(Form)
  105. {
  106. FForm->BatchStart(FBatchStorage);
  107. }
  108. ~TAutoBatch()
  109. {
  110. FForm->BatchEnd(FBatchStorage);
  111. }
  112. private:
  113. TCustomScpExplorerForm * FForm;
  114. void * FBatchStorage;
  115. };
  116. //---------------------------------------------------------------------------
  117. struct TTransferOperationParam
  118. {
  119. TTransferOperationParam();
  120. UnicodeString TargetDirectory;
  121. bool Temp;
  122. bool DragDrop;
  123. int Options;
  124. TAutoSwitch Queue;
  125. };
  126. //---------------------------------------------------------------------------
  127. TTransferOperationParam::TTransferOperationParam()
  128. {
  129. Temp = false;
  130. DragDrop = false;
  131. Options = 0;
  132. Queue = asAuto;
  133. }
  134. //---------------------------------------------------------------------------
  135. class TTransferPresetNoteData : public TObject
  136. {
  137. public:
  138. UnicodeString Message;
  139. };
  140. //---------------------------------------------------------------------------
  141. class TTerminalNoteData : public TObject
  142. {
  143. public:
  144. TManagedTerminal * Terminal;
  145. };
  146. //---------------------------------------------------------------------------
  147. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  148. FFormRestored(false),
  149. TForm(Owner)
  150. {
  151. FCurrentSide = osRemote;
  152. FEverShown = false;
  153. FDocks = new TList();
  154. InitControls();
  155. RestoreParams();
  156. ConfigurationChanged();
  157. RemoteDirView->Invalidate();
  158. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  159. NonVisualDataModule->ScpExplorer = this;
  160. FAutoOperation = false;
  161. FOnFileOperationFinished = NULL;
  162. FForceExecution = false;
  163. FIgnoreNextDialogChar = 0;
  164. FErrorList = NULL;
  165. FSynchronizeProgressForm = NULL;
  166. FProgressForm = NULL;
  167. FRefreshLocalDirectory = false;
  168. FRefreshRemoteDirectory = false;
  169. FDDExtMapFile = NULL;
  170. // CreateMutexW keeps failing with ERROR_NOACCESS
  171. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  172. DebugAssert(FDDExtMutex != NULL);
  173. FDDTargetControl = NULL;
  174. FDelayedDeletionTimer = NULL;
  175. FDelayedDeletionList = new TStringList();
  176. FDDFileList = NULL;
  177. FPendingTempSpaceWarn = false;
  178. FCapturedLog = NULL;
  179. FDragDropOperation = false;
  180. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  181. FAllowTransferPresetAutoSelect = true;
  182. FCopyParamDefault = L"";
  183. FSynchronizeController = NULL;
  184. FPendingQueueActionItem = NULL;
  185. FLockLevel = 0;
  186. FLockSuspendLevel = 0;
  187. FDisabledOnLockSuspend = false;
  188. FAlternativeDelete = false;
  189. FTrayIcon = new ::TTrayIcon(0);
  190. FTrayIcon->OnClick = TrayIconClick;
  191. FMaxQueueLength = 0;
  192. FLastContextPopupScreenPoint = TPoint(-1, -1);
  193. FTransferResumeList = NULL;
  194. FMoveToQueue = false;
  195. FStandaloneEditing = false;
  196. FOnFeedSynchronizeError = NULL;
  197. FNeedSession = false;
  198. FDoNotIdleCurrentTerminal = 0;
  199. FIncrementalSearching = 0;
  200. FIncrementalSearchHaveNext = false;
  201. FEditorManager = new TEditorManager();
  202. FEditorManager->OnFileChange = ExecutedFileChanged;
  203. FEditorManager->OnFileReload = ExecutedFileReload;
  204. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  205. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  206. FLocalEditors = new TList();
  207. FQueueStatus = NULL;
  208. FQueueStatusSection = new TCriticalSection();
  209. FQueueStatusInvalidated = false;
  210. FQueueItemInvalidated = false;
  211. FQueueActedItem = NULL;
  212. FQueueController = new TQueueController(QueueView3);
  213. FUserActionTimer = new TTimer(this);
  214. FUserActionTimer->Enabled = false;
  215. FUserActionTimer->Interval = 10;
  216. FUserActionTimer->OnTimer = UserActionTimer;
  217. FNotes = new TStringList();
  218. FNoteTimer = new TTimer(this);
  219. FNoteTimer->Enabled = false;
  220. FNoteTimer->OnTimer = NoteTimer;
  221. FOnNoteClick = NULL;
  222. FOle32Library = LoadLibrary(L"Ole32.dll");
  223. FDragMoveCursor = FOle32Library != NULL ?
  224. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  225. UseSystemSettings(this);
  226. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  227. static_cast<TObject*>(GetComponent(fcTransferList)));
  228. DebugAssert(TransferList != NULL);
  229. FTransferListHoverIndex = -1;
  230. TransferList->OnChange = TransferListChange;
  231. TransferList->OnDrawItem = TransferListDrawItem;
  232. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  233. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  234. UseDesktopFont(SessionsPageControl);
  235. UpdateSessionsPageControlHeight();
  236. UseDesktopFont(RemoteDirView);
  237. UseDesktopFont(RemoteDriveView);
  238. UseDesktopFont(QueueView3);
  239. UseDesktopFont(QueueLabel);
  240. UseDesktopFont(RemoteStatusBar);
  241. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  242. QueueSplitter->ShowHint = true;
  243. RemotePanelSplitter->ShowHint = true;
  244. UpdateImages();
  245. FCustomCommandMenu = CreateTBXPopupMenu(this);
  246. FCustomCommandLocalFileList = NULL;
  247. FCustomCommandRemoteFileList = NULL;
  248. FSessionColors = new TPngImageList(this);
  249. FSessionColors->ColorDepth = cd32Bit;
  250. AddFixedSessionImages();
  251. SessionsPageControl->Images = FSessionColors;
  252. QueueLabel->FocusControl = QueueView3;
  253. UpdateQueueLabel();
  254. FRemoteDirViewWasFocused = true;
  255. CreateHiddenWindow();
  256. StartUpdates();
  257. }
  258. //---------------------------------------------------------------------------
  259. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  260. {
  261. // this has to be one of the very first things to do
  262. StopUpdateThread();
  263. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  264. TTerminalManager::Instance()->ScpExplorer = NULL;
  265. delete FCustomCommandLocalFileList;
  266. delete FCustomCommandRemoteFileList;
  267. delete FCustomCommandMenu;
  268. delete FTrayIcon;
  269. FTrayIcon = NULL;
  270. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  271. delete FEditorManager;
  272. ForceCloseLocalEditors();
  273. delete FLocalEditors;
  274. HideFileFindDialog();
  275. if (FDelayedDeletionTimer)
  276. {
  277. DoDelayedDeletion(NULL);
  278. SAFE_DESTROY(FDelayedDeletionTimer);
  279. }
  280. SAFE_DESTROY(FDelayedDeletionList);
  281. // sometimes we do not get DDEnd so the list is not released
  282. SAFE_DESTROY(FDDFileList);
  283. DebugAssert(FSynchronizeController == NULL);
  284. CloseHandle(FDDExtMutex);
  285. FDDExtMutex = NULL;
  286. FreeLibrary(FOle32Library);
  287. FOle32Library = NULL;
  288. FDragMoveCursor = NULL;
  289. DebugAssert(!FErrorList);
  290. if (FEverShown)
  291. {
  292. // when window is never shown (like when running command-line operation),
  293. // particularly window site is not restored correctly (BoundsRect value set
  294. // in RestoreForm gets lost during handle allocation), so we do not want
  295. // it to be stored
  296. StoreParams();
  297. }
  298. Terminal = NULL;
  299. Queue = NULL;
  300. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  301. NonVisualDataModule->ScpExplorer = NULL;
  302. delete FQueueController;
  303. FQueueController = NULL;
  304. delete FQueueStatusSection;
  305. FQueueStatusSection = NULL;
  306. delete FQueueStatus;
  307. FQueueStatus = NULL;
  308. delete FUserActionTimer;
  309. FUserActionTimer = NULL;
  310. delete FNoteTimer;
  311. delete FNotes;
  312. delete FNoteData;
  313. SAFE_DESTROY(FDocks);
  314. SessionsPageControl->Images = NULL;
  315. // only after clearing Terminal (after DoTerminalListChanged => ... => AddSessionColor is called)
  316. SAFE_DESTROY(FSessionColors);
  317. SAFE_DESTROY(FSessionsDragDropFilesEx);
  318. SAFE_DESTROY(FHistoryMenu[0][0]);
  319. SAFE_DESTROY(FHistoryMenu[0][1]);
  320. SAFE_DESTROY(FHistoryMenu[1][0]);
  321. SAFE_DESTROY(FHistoryMenu[1][1]);
  322. if (FHiddenWindow != NULL)
  323. {
  324. DestroyWindow(FHiddenWindow);
  325. FHiddenWindow = NULL;
  326. }
  327. }
  328. //---------------------------------------------------------------------------
  329. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  330. {
  331. std::unique_ptr<TSessionData> Data;
  332. if (!Session.IsEmpty())
  333. {
  334. bool DefaultsOnly;
  335. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  336. }
  337. TTerminalManager * Manager = TTerminalManager::Instance();
  338. for (int Index = 0; Index < Manager->Count; Index++)
  339. {
  340. TTerminal * Terminal = Manager->Terminals[Index];
  341. if (Session.IsEmpty() ||
  342. Terminal->SessionData->IsSameSite(Data.get()))
  343. {
  344. if (Path.IsEmpty())
  345. {
  346. Terminal->ClearCaches();
  347. }
  348. else
  349. {
  350. Terminal->DirectoryModified(Path, true);
  351. }
  352. }
  353. }
  354. // We should flag a pending refresh for the background terminals or busy foreground terminals
  355. if ((Terminal != NULL) && Terminal->Active &&
  356. CanCommandLineFromAnotherInstance() &&
  357. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  358. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  359. {
  360. Terminal->ReloadDirectory();
  361. }
  362. }
  363. //---------------------------------------------------------------------------
  364. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  365. {
  366. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  367. size_t MessageSize = sizeof(TCopyDataMessage);
  368. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  369. if (Result)
  370. {
  371. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  372. Result = (Message.Version == TCopyDataMessage::Version1);
  373. if (Result)
  374. {
  375. switch (Message.Command)
  376. {
  377. case TCopyDataMessage::CommandCanCommandLine:
  378. Result = CanCommandLineFromAnotherInstance();
  379. break;
  380. case TCopyDataMessage::CommandCommandLine:
  381. {
  382. UnicodeString CommandLine(Message.CommandLine);
  383. Result = CommandLineFromAnotherInstance(CommandLine);
  384. }
  385. break;
  386. case TCopyDataMessage::RefreshPanel:
  387. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  388. break;
  389. case TCopyDataMessage::MainWindowCheck:
  390. Result = true;
  391. break;
  392. default:
  393. Result = false;
  394. break;
  395. }
  396. }
  397. }
  398. Message.Result = Result ? 1 : 0;
  399. }
  400. //---------------------------------------------------------------------------
  401. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  402. {
  403. WNDCLASS WindowClass = {0};
  404. WindowClass.lpfnWndProc = DefWindowProc;
  405. WindowClass.hInstance = HInstance;
  406. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  407. FHiddenWindow = NULL;
  408. if (RegisterClass(&WindowClass))
  409. {
  410. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  411. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  412. }
  413. }
  414. //---------------------------------------------------------------------------
  415. bool __fastcall TCustomScpExplorerForm::CanConsole()
  416. {
  417. return
  418. (Terminal != NULL) &&
  419. Terminal->Active &&
  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->ActiveTerminal = Manager->NewTerminals(DataList.get());
  463. }
  464. }
  465. __finally
  466. {
  467. NonVisualDataModule->EndBusy();
  468. }
  469. }
  470. return Result;
  471. }
  472. //---------------------------------------------------------------------------
  473. void __fastcall TCustomScpExplorerForm::SetTerminal(TManagedTerminal * value)
  474. {
  475. if (FTerminal != value)
  476. {
  477. TerminalChanging();
  478. DoSetTerminal(value, false);
  479. }
  480. }
  481. //---------------------------------------------------------------------------
  482. void __fastcall TCustomScpExplorerForm::DoSetTerminal(TManagedTerminal * value, bool Replace)
  483. {
  484. FTerminal = value;
  485. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  486. FAllowTransferPresetAutoSelect = false;
  487. try
  488. {
  489. TerminalChanged(Replace);
  490. }
  491. __finally
  492. {
  493. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  494. }
  495. if (Terminal != NULL)
  496. {
  497. TransferPresetAutoSelect();
  498. }
  499. }
  500. //---------------------------------------------------------------------------
  501. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  502. {
  503. DoSetTerminal(value, true);
  504. }
  505. //---------------------------------------------------------------------------
  506. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  507. {
  508. if (FTerminal != NULL)
  509. {
  510. UpdateTerminal(Terminal);
  511. }
  512. }
  513. //---------------------------------------------------------------------------
  514. void __fastcall TCustomScpExplorerForm::TerminalChanged(bool Replaced)
  515. {
  516. if (Terminal != NULL)
  517. {
  518. UpdateSessionColor((TColor)Terminal->StateData->Color);
  519. }
  520. DoTerminalListChanged();
  521. DebugAssert(!IsLocalBrowserMode()); // TODO
  522. if (Replaced)
  523. {
  524. RemoteDirView->ReplaceTerminal(Terminal);
  525. }
  526. else
  527. {
  528. RemoteDirView->Terminal = Terminal;
  529. }
  530. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  531. if (Terminal != NULL)
  532. {
  533. if (Terminal->Active)
  534. {
  535. Terminal->RefreshDirectory();
  536. }
  537. if (WinConfiguration->PreservePanelState)
  538. {
  539. if (Terminal->RemoteExplorerState != NULL)
  540. {
  541. DirView(osRemote)->RestoreState(Terminal->RemoteExplorerState);
  542. }
  543. else
  544. {
  545. DirView(osRemote)->ClearState();
  546. }
  547. }
  548. if (!Replaced && Terminal->Active)
  549. {
  550. InitStatusBar();
  551. }
  552. }
  553. UpdateTransferList();
  554. // Update panels Enable state before refreshing the labels
  555. UpdateControls();
  556. }
  557. //---------------------------------------------------------------------------
  558. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  559. {
  560. if (Queue != value)
  561. {
  562. if (FQueue != NULL)
  563. {
  564. FQueue->OnListUpdate = NULL;
  565. FQueue->OnQueueItemUpdate = NULL;
  566. }
  567. FQueue = value;
  568. if (FQueue != NULL)
  569. {
  570. DebugAssert(FQueue->OnListUpdate == NULL);
  571. FQueue->OnListUpdate = QueueListUpdate;
  572. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  573. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  574. }
  575. QueueChanged();
  576. }
  577. }
  578. //---------------------------------------------------------------------------
  579. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  580. TListItem * Item)
  581. {
  582. if (FQueueActedItem == Item)
  583. {
  584. FQueueActedItem = NULL;
  585. if ((QueueView3->PopupMenu != NULL) &&
  586. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  587. {
  588. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  589. ReleaseCapture();
  590. }
  591. }
  592. if (Item->Data == FPendingQueueActionItem)
  593. {
  594. FPendingQueueActionItem = NULL;
  595. }
  596. }
  597. //---------------------------------------------------------------------------
  598. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  599. {
  600. {
  601. TGuard Guard(FQueueStatusSection);
  602. FQueueStatusInvalidated = false;
  603. if (FQueue != NULL)
  604. {
  605. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  606. }
  607. }
  608. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  609. {
  610. FMaxQueueLength = FQueueStatus->Count;
  611. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  612. }
  613. FQueueController->UpdateQueueStatus(FQueueStatus);
  614. SetQueueProgress();
  615. UpdateQueueView();
  616. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  617. if (IsEmpty && (Terminal != NULL))
  618. {
  619. TOnceDoneOperation OnceDoneOperation =
  620. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  621. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  622. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  623. {
  624. FQueue->Enabled = false;
  625. }
  626. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  627. {
  628. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  629. }
  630. }
  631. }
  632. //---------------------------------------------------------------------------
  633. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  634. {
  635. UnicodeString Result;
  636. if (FQueueStatus != NULL)
  637. {
  638. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  639. {
  640. TFileOperationProgressType * ProgressData =
  641. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  642. if ((ProgressData != NULL) && ProgressData->InProgress)
  643. {
  644. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  645. }
  646. }
  647. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  648. {
  649. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  650. }
  651. }
  652. return Result;
  653. }
  654. //---------------------------------------------------------------------------
  655. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  656. {
  657. ComponentVisible[fcQueueView] =
  658. (WinConfiguration->QueueView.Show == qvShow) ||
  659. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  660. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  661. }
  662. //---------------------------------------------------------------------------
  663. void __fastcall TCustomScpExplorerForm::QueueChanged()
  664. {
  665. if (FQueueStatus != NULL)
  666. {
  667. delete FQueueStatus;
  668. FQueueStatus = NULL;
  669. }
  670. UpdateQueueStatus(true);
  671. }
  672. //---------------------------------------------------------------------------
  673. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  674. {
  675. if (FQueue == Queue)
  676. {
  677. FQueueStatusInvalidated = true;
  678. }
  679. }
  680. //---------------------------------------------------------------------------
  681. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  682. TQueueItem * Item)
  683. {
  684. if (FQueue == Queue)
  685. {
  686. TGuard Guard(FQueueStatusSection);
  687. // this may be running in parallel with QueueChanged
  688. if (FQueueStatus != NULL)
  689. {
  690. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  691. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  692. {
  693. FRefreshLocalDirectory = (QueueItem == NULL) ||
  694. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  695. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  696. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  697. }
  698. if (QueueItem != NULL)
  699. {
  700. QueueItem->UserData = (void*)true;
  701. FQueueItemInvalidated = true;
  702. }
  703. }
  704. }
  705. }
  706. //---------------------------------------------------------------------------
  707. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  708. {
  709. // during standalone editing, we have no way to see/control queue,
  710. // so we have to always popup prompts automatically
  711. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  712. }
  713. //---------------------------------------------------------------------------
  714. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  715. {
  716. if (FQueueStatus != NULL)
  717. {
  718. bool QueueAutoPopup = IsQueueAutoPopup();
  719. bool NeedRefresh = FQueueController->NeedRefresh();
  720. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  721. FQueueItemInvalidated = false;
  722. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  723. bool Updated = false;
  724. TQueueItemProxy * QueueItem;
  725. bool WasUserAction;
  726. for (int Index = 0; Index < Limit; Index++)
  727. {
  728. bool Update = false;
  729. QueueItem = FQueueStatus->Items[Index];
  730. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  731. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  732. (FPendingQueueActionItem == NULL))
  733. {
  734. FPendingQueueActionItem = QueueItem;
  735. FUserActionTimer->Enabled = true;
  736. }
  737. if ((bool)QueueItem->UserData)
  738. {
  739. QueueItem->UserData = (void*)false;
  740. QueueItem->Update();
  741. Updated = true;
  742. Update = true;
  743. }
  744. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  745. {
  746. Update = true;
  747. }
  748. if (Update)
  749. {
  750. FQueueController->RefreshQueueItem(QueueItem);
  751. }
  752. }
  753. if (Updated)
  754. {
  755. NonVisualDataModule->UpdateNonVisibleActions();
  756. SetQueueProgress();
  757. }
  758. }
  759. }
  760. //---------------------------------------------------------------------------
  761. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  762. {
  763. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  764. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  765. }
  766. //---------------------------------------------------------------------------
  767. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  768. {
  769. if (Progress >= 0)
  770. {
  771. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  772. }
  773. else
  774. {
  775. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  776. }
  777. }
  778. //---------------------------------------------------------------------------
  779. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  780. {
  781. int OverallProgress;
  782. // FProgressForm is null when this is called from SetQueueProgress
  783. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  784. {
  785. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  786. }
  787. else if (!TFileOperationProgressType::IsIndeterminateOperation(ProgressData->Operation))
  788. {
  789. OverallProgress = ProgressData->OverallProgress();
  790. }
  791. else
  792. {
  793. OverallProgress = -1;
  794. }
  795. SetTaskbarListProgressValue(OverallProgress);
  796. }
  797. //---------------------------------------------------------------------------
  798. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  799. {
  800. TTerminalManager::Instance()->QueueStatusUpdated();
  801. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  802. {
  803. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  804. {
  805. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  806. {
  807. TFileOperationProgressType * ProgressData = NULL;
  808. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  809. {
  810. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  811. }
  812. if ((ProgressData != NULL) &&
  813. ProgressData->InProgress)
  814. {
  815. SetTaskbarListProgressValue(ProgressData);
  816. }
  817. else
  818. {
  819. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  820. }
  821. }
  822. else
  823. {
  824. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  825. }
  826. }
  827. else
  828. {
  829. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  830. }
  831. }
  832. UpdateQueueLabel();
  833. }
  834. //---------------------------------------------------------------------------
  835. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  836. {
  837. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  838. if (FQueueStatus != NULL)
  839. {
  840. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  841. if (ActiveAndPendingPrimaryCount > 0)
  842. {
  843. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  844. }
  845. }
  846. QueueLabel->Caption = Caption;
  847. }
  848. //---------------------------------------------------------------------------
  849. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  850. {
  851. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  852. static_cast<TComponent*>(GetComponent(fcTransferList)));
  853. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  854. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  855. TransferList->Strings->BeginUpdate();
  856. try
  857. {
  858. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  859. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  860. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  861. if (FTransferDropDownHint.IsEmpty())
  862. {
  863. FTransferDropDownHint = TransferDropDown->Hint;
  864. }
  865. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  866. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  867. TransferDropDown->Text = StripHotkey(Name);
  868. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  869. (FTransferDropDownHint, StripHotkey(Name),
  870. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  871. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General))));
  872. // update the label, otherwise when it is updated only on the first draw
  873. // of the list, it is drawn "bold" for some reason
  874. FTransferListHoverIndex = TransferList->ItemIndex;
  875. UpdateTransferLabel();
  876. }
  877. __finally
  878. {
  879. TransferList->Strings->EndUpdate();
  880. }
  881. }
  882. //---------------------------------------------------------------------------
  883. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  884. {
  885. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  886. DebugAssert(Toolbar != NULL);
  887. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  888. }
  889. //---------------------------------------------------------------------------
  890. void __fastcall TCustomScpExplorerForm::UpdateActions()
  891. {
  892. TForm::UpdateActions();
  893. if (ComponentVisible[fcCustomCommandsBand])
  894. {
  895. UpdateCustomCommandsToolbar();
  896. }
  897. }
  898. //---------------------------------------------------------------------------
  899. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  900. {
  901. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  902. }
  903. //---------------------------------------------------------------------------
  904. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  905. {
  906. Color = GetBtnFaceColor();
  907. DebugAssert(Configuration && RemoteDirView);
  908. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  909. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  910. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  911. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  912. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  913. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  914. {
  915. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  916. // selection is not redrawn automatically when RowSelect changes
  917. RemoteDirView->Invalidate();
  918. }
  919. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  920. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  921. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  922. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  923. UpdateSessionsPageControlHeight();
  924. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  925. UpdateToolbarDisplayMode();
  926. UpdateTransferList();
  927. if (Terminal != NULL)
  928. {
  929. TransferPresetAutoSelect();
  930. }
  931. UpdateQueueView();
  932. UpdateControls();
  933. // this can be called even before constuctor finishes.
  934. if (FEditorManager != NULL)
  935. {
  936. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  937. }
  938. // this can be called even before constuctor finishes.
  939. if (FLocalEditors != NULL)
  940. {
  941. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  942. {
  943. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  944. }
  945. }
  946. if (ComponentVisible[fcCustomCommandsBand])
  947. {
  948. UpdateCustomCommandsToolbar();
  949. }
  950. // show only when keeping queue items forever,
  951. // otherwise, its enough to have in in the context menu
  952. QueueDeleteAllDoneQueueToolbarItem->Visible =
  953. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  954. if (FFileColorsCurrent != WinConfiguration->FileColors)
  955. {
  956. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  957. FFileColorsCurrent = WinConfiguration->FileColors;
  958. FileColorsChanged();
  959. }
  960. }
  961. //---------------------------------------------------------------------------
  962. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  963. {
  964. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  965. DirView->Invalidate();
  966. }
  967. //---------------------------------------------------------------------------
  968. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  969. {
  970. DoFileColorsChanged(RemoteDirView);
  971. }
  972. //---------------------------------------------------------------------------
  973. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  974. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  975. void * /*Arg*/)
  976. {
  977. if (Token != NULL)
  978. {
  979. TForm * Editor = dynamic_cast<TForm*>(Token);
  980. DebugAssert(Editor != NULL);
  981. ReconfigureEditorForm(Editor);
  982. }
  983. }
  984. //---------------------------------------------------------------------------
  985. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  986. {
  987. WinConfiguration->DDTransferConfirmation = asOn;
  988. }
  989. //---------------------------------------------------------------------------
  990. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  991. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  992. {
  993. // noop
  994. }
  995. //---------------------------------------------------------------------------
  996. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  997. TTransferDirection Direction, TTransferType Type, bool Temp,
  998. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  999. bool Confirm, bool DragDrop, int Options)
  1000. {
  1001. bool Result = true;
  1002. DebugAssert(Terminal && Terminal->Active);
  1003. // these parameters are known in advance
  1004. int Params =
  1005. FLAGMASK(Type == ttMove, cpDelete);
  1006. bool ToTemp = (Temp && (Direction == tdToLocal));
  1007. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1008. {
  1009. bool DisableNewerOnly =
  1010. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1011. ToTemp;
  1012. Options |=
  1013. FLAGMASK(ToTemp, coTemp) |
  1014. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  1015. coDoNotShowAgain;
  1016. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1017. int CopyParamAttrs = (Direction == tdToRemote ?
  1018. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1019. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1020. int OutputOptions =
  1021. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1022. cooDoNotShowAgain);
  1023. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1024. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1025. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1026. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions);
  1027. if (Result)
  1028. {
  1029. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1030. {
  1031. if (DragDrop)
  1032. {
  1033. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1034. {
  1035. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1036. NULL, 0, EnableDDTransferConfirmation, NULL);
  1037. }
  1038. WinConfiguration->DDTransferConfirmation = asOff;
  1039. }
  1040. else
  1041. {
  1042. WinConfiguration->ConfirmTransferring = false;
  1043. }
  1044. }
  1045. else
  1046. {
  1047. // User explicitly unchecked "do not show again",
  1048. // so show him the dialog the next time
  1049. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1050. {
  1051. WinConfiguration->DDTransferConfirmation = asOn;
  1052. }
  1053. }
  1054. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1055. }
  1056. }
  1057. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1058. {
  1059. Configuration->Usage->Inc(L"TransfersOnBackground");
  1060. // these parameter are known only after transfer dialog
  1061. Params |=
  1062. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1063. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1064. Result = false;
  1065. ClearTransferSourceSelection(Direction);
  1066. }
  1067. return Result;
  1068. }
  1069. //---------------------------------------------------------------------------
  1070. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1071. {
  1072. if (FOnFileOperationFinished != NULL)
  1073. {
  1074. FOnFileOperationFinished(UnicodeString(), true);
  1075. }
  1076. else
  1077. {
  1078. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1079. if (HasDirView[Side])
  1080. {
  1081. DirView(Side)->SelectAll(smNone);
  1082. }
  1083. }
  1084. }
  1085. //---------------------------------------------------------------------------
  1086. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1087. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1088. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1089. int Params)
  1090. {
  1091. DebugAssert(Queue != NULL);
  1092. bool SingleFile = false;
  1093. if (FileList->Count == 1)
  1094. {
  1095. if (Direction == tdToRemote)
  1096. {
  1097. UnicodeString FileName = FileList->Strings[0];
  1098. SingleFile = FileExists(ApiPath(FileName));
  1099. }
  1100. else
  1101. {
  1102. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1103. SingleFile = !File->IsDirectory;
  1104. }
  1105. }
  1106. TQueueItem * QueueItem;
  1107. if (Direction == tdToRemote)
  1108. {
  1109. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1110. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1111. }
  1112. else
  1113. {
  1114. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1115. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1116. }
  1117. AddQueueItem(Queue, QueueItem, Terminal);
  1118. }
  1119. //---------------------------------------------------------------------------
  1120. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1121. {
  1122. if (Queue->IsEmpty)
  1123. {
  1124. Terminal->QueueOperationStart = Now();
  1125. }
  1126. Queue->AddItem(QueueItem);
  1127. }
  1128. //---------------------------------------------------------------------------
  1129. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1130. {
  1131. }
  1132. //---------------------------------------------------------------------------
  1133. void __fastcall TCustomScpExplorerForm::InitControls()
  1134. {
  1135. DebugAssert(FDocks != NULL);
  1136. for (int Index = 0; Index < ComponentCount; Index++)
  1137. {
  1138. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1139. if ((Dock != NULL) && (Dock->Tag == 0))
  1140. {
  1141. FDocks->Add(Dock);
  1142. }
  1143. }
  1144. CollectItemsWithTextDisplayMode(this);
  1145. }
  1146. //---------------------------------------------------------------------------
  1147. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1148. {
  1149. ConfigurationChanged();
  1150. QueuePanel->Height =
  1151. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1152. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1153. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1154. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1155. }
  1156. //---------------------------------------------------------------------------
  1157. void __fastcall TCustomScpExplorerForm::StoreParams()
  1158. {
  1159. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1160. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1161. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1162. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1163. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1164. }
  1165. //---------------------------------------------------------------------------
  1166. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1167. {
  1168. if (!FFormRestored)
  1169. {
  1170. FFormRestored = true;
  1171. RestoreFormParams();
  1172. }
  1173. TForm::CreateParams(Params);
  1174. }
  1175. //---------------------------------------------------------------------------
  1176. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1177. {
  1178. DebugAssert(FDocks != NULL);
  1179. for (int Index = 0; Index < FDocks->Count; Index++)
  1180. {
  1181. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1182. }
  1183. }
  1184. //---------------------------------------------------------------------------
  1185. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1186. {
  1187. SetDockAllowDrag(true);
  1188. while (!ButtonsStr.IsEmpty())
  1189. {
  1190. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1191. UnicodeString Name = CutToChar(S, L'=', true);
  1192. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1193. {
  1194. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1195. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1196. {
  1197. while (!S.IsEmpty())
  1198. {
  1199. UnicodeString S2 = CutToChar(S, L',', true);
  1200. UnicodeString Name = CutToChar(S2, L':', true);
  1201. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1202. {
  1203. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1204. if (SameText(GetToolbarItemName(Item), Name))
  1205. {
  1206. // forward compatibility
  1207. UnicodeString S3 = CutToChar(S2, L':', true);
  1208. bool Visible = StrToIntDef(S3, 0);
  1209. Item->Visible = Visible;
  1210. }
  1211. }
  1212. }
  1213. }
  1214. }
  1215. }
  1216. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1217. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1218. }
  1219. //---------------------------------------------------------------------------
  1220. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1221. {
  1222. UnicodeString Result;
  1223. SetDockAllowDrag(true);
  1224. Result = ::GetToolbarsLayoutStr(this);
  1225. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1226. return Result;
  1227. }
  1228. //---------------------------------------------------------------------------
  1229. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1230. {
  1231. UnicodeString Result;
  1232. if (Item->Action != NULL)
  1233. {
  1234. Result = Item->Action->Name;
  1235. Result = RemoveSuffix(Result, L"Action");
  1236. }
  1237. else
  1238. {
  1239. Result = Item->Name;
  1240. Result = RemoveSuffix(Result, L"SubmenuItem");
  1241. Result = RemoveSuffix(Result, L"Item");
  1242. }
  1243. return Result;
  1244. }
  1245. //---------------------------------------------------------------------------
  1246. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1247. {
  1248. UnicodeString Result;
  1249. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1250. {
  1251. UnicodeString ToolbarStr;
  1252. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1253. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1254. {
  1255. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1256. {
  1257. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1258. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1259. // Once we introduce any buttons that aer hidden by default, we would have to remember their initial state
  1260. // and save the changes here only.
  1261. if (!Item->Visible)
  1262. {
  1263. UnicodeString Name = GetToolbarItemName(Item);
  1264. DebugAssert(Name.Pos(L"TBX") == 0);
  1265. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1266. }
  1267. }
  1268. }
  1269. if (!ToolbarStr.IsEmpty())
  1270. {
  1271. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1272. }
  1273. }
  1274. return Result;
  1275. }
  1276. //---------------------------------------------------------------------------
  1277. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1278. {
  1279. DebugAssert(FProgressForm == NULL);
  1280. bool AllowSkip = (Terminal != NULL) ? Terminal->IsCapable[fsSkipTransfer] : false;
  1281. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1282. FProgressForm->DeleteLocalToRecycleBin =
  1283. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1284. FProgressForm->DeleteRemoteToRecycleBin =
  1285. (Terminal != NULL) &&
  1286. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1287. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1288. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1289. LockWindow();
  1290. if (FTaskbarList != NULL)
  1291. {
  1292. // Actually, do not know what hides the progress once the operation finishes
  1293. SetTaskbarListProgressState(TBPF_NORMAL);
  1294. }
  1295. }
  1296. //---------------------------------------------------------------------------
  1297. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1298. {
  1299. UnlockWindow();
  1300. SAFE_DESTROY(FProgressForm);
  1301. SetQueueProgress();
  1302. }
  1303. //---------------------------------------------------------------------------
  1304. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1305. TFileOperationProgressType & ProgressData)
  1306. {
  1307. // Download to temporary local directory
  1308. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1309. {
  1310. bool Continue = true;
  1311. FPendingTempSpaceWarn = false;
  1312. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1313. if (!Continue)
  1314. {
  1315. Abort();
  1316. }
  1317. }
  1318. // operation is being executed and we still didn't show up progress form
  1319. if (ProgressData.InProgress && !FProgressForm)
  1320. {
  1321. CreateProgressForm(NULL);
  1322. // When main window is hidden, synchronisation form does not exist (we do not get here during synchronization - only comparing),
  1323. // we suppose "/upload" or URL download mode
  1324. if (!Visible && (ProgressData.Operation != foCalculateSize) &&
  1325. (ProgressData.Operation != foCalculateChecksum) &&
  1326. // Hack to prevent renames from DoSynchronizeMove (synchronize checklist) closing the connection
  1327. (ProgressData.Operation != foRemoteMove) &&
  1328. (FSynchronizeProgressForm == NULL) &&
  1329. !FStandaloneEditing)
  1330. {
  1331. FProgressForm->OnceDoneOperation = odoDisconnect;
  1332. }
  1333. }
  1334. // operation is finished (or terminated), so we hide progress form
  1335. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1336. {
  1337. DestroyProgressForm();
  1338. if (ProgressData.Operation == foCalculateSize)
  1339. {
  1340. // When calculating size before transfer, the abort caused by
  1341. // cancel flag set due to "MoveToQueue" below,
  1342. // is not propagated back to us in ExecuteFileOperation,
  1343. // as it is silently swallowed.
  1344. // So we explicitly (re)throw it here.
  1345. if (FMoveToQueue)
  1346. {
  1347. Abort();
  1348. }
  1349. }
  1350. else
  1351. {
  1352. if ((ProgressData.Cancel == csContinue) &&
  1353. !FAutoOperation)
  1354. {
  1355. OperationComplete(ProgressData.StartTime);
  1356. }
  1357. }
  1358. }
  1359. if (FProgressForm &&
  1360. // Can happen only during synchronization, when ending one of the operations
  1361. ProgressData.InProgress)
  1362. {
  1363. FProgressForm->SetProgressData(ProgressData);
  1364. if (FTaskbarList != NULL)
  1365. {
  1366. DebugAssert(ProgressData.InProgress);
  1367. SetTaskbarListProgressValue(&ProgressData);
  1368. }
  1369. if (FProgressForm->Cancel > csContinue)
  1370. {
  1371. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1372. if (FProgressForm->Cancel == csCancelFile)
  1373. {
  1374. FProgressForm->ClearCancel();
  1375. }
  1376. // cancel cancels even the move
  1377. FMoveToQueue = false;
  1378. }
  1379. else if (FProgressForm->MoveToQueue)
  1380. {
  1381. FMoveToQueue = true;
  1382. ProgressData.SetCancelAtLeast(csCancel);
  1383. }
  1384. if ((FTransferResumeList != NULL) &&
  1385. ProgressData.InProgress &&
  1386. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1387. !ProgressData.FullFileName.IsEmpty())
  1388. {
  1389. if ((FTransferResumeList->Count == 0) ||
  1390. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1391. {
  1392. // note that we do not recognize directories from files here
  1393. FTransferResumeList->Add(ProgressData.FullFileName);
  1394. }
  1395. }
  1396. }
  1397. }
  1398. //---------------------------------------------------------------------------
  1399. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1400. const TDateTime & StartTime)
  1401. {
  1402. if (GUIConfiguration->BeepOnFinish &&
  1403. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1404. {
  1405. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1406. DWORD Sound;
  1407. if (!ExtractFileExt(BeepSound).IsEmpty())
  1408. {
  1409. Sound = SND_FILENAME;
  1410. }
  1411. else
  1412. {
  1413. Sound = SND_ALIAS;
  1414. }
  1415. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1416. }
  1417. }
  1418. //---------------------------------------------------------------------------
  1419. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1420. TFileOperationProgressType & ProgressData)
  1421. {
  1422. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1423. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1424. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1425. FileOperationProgress(ProgressData);
  1426. }
  1427. //---------------------------------------------------------------------------
  1428. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1429. {
  1430. UnicodeString Result;
  1431. if (FProgressForm != NULL)
  1432. {
  1433. Result = FProgressForm->ProgressStr();
  1434. }
  1435. return Result;
  1436. }
  1437. //---------------------------------------------------------------------------
  1438. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1439. bool DragDrop)
  1440. {
  1441. return (!DragDrop && (DropSourceControl == NULL)) ||
  1442. (DropSourceControl == DirView(osOther));
  1443. }
  1444. //---------------------------------------------------------------------------
  1445. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1446. TFileOperation Operation, TOperationSide Side,
  1447. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1448. TOnceDoneOperation & OnceDoneOperation)
  1449. {
  1450. if (!FAutoOperation)
  1451. {
  1452. // no selection on "/upload", form servers only as event handler
  1453. // (it is not displayed)
  1454. if (PanelOperation(Side, FDragDropOperation) &&
  1455. Visible && (Operation != foCalculateSize) &&
  1456. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1457. {
  1458. if (FOnFileOperationFinished != NULL)
  1459. {
  1460. FOnFileOperationFinished(FileName, Success);
  1461. }
  1462. else
  1463. {
  1464. TCustomDirView * DView = DirView(Side);
  1465. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1466. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1467. // this can happen when local drive is unplugged in the middle of the operation
  1468. if (Item != NULL)
  1469. {
  1470. if (Success) Item->Selected = false;
  1471. DView->MakeProgressVisible(Item);
  1472. }
  1473. }
  1474. }
  1475. if ((Operation == foCopy) || (Operation == foMove))
  1476. {
  1477. DebugAssert(!IsLocalBrowserMode()); // TODO
  1478. if (Side == osLocal)
  1479. {
  1480. Configuration->Usage->Inc(L"UploadedFiles");
  1481. }
  1482. else
  1483. {
  1484. Configuration->Usage->Inc(L"DownloadedFiles");
  1485. }
  1486. }
  1487. }
  1488. if (Success && (FSynchronizeController != NULL))
  1489. {
  1490. DebugAssert(!IsSideLocalBrowser(Side));
  1491. if (Operation == foCopy)
  1492. {
  1493. DebugAssert(Side == osLocal);
  1494. FSynchronizeController->LogOperation(soUpload, FileName);
  1495. }
  1496. else if (Operation == foDelete)
  1497. {
  1498. DebugAssert(Side == osRemote);
  1499. FSynchronizeController->LogOperation(soDelete, FileName);
  1500. }
  1501. }
  1502. if (FProgressForm)
  1503. {
  1504. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1505. }
  1506. }
  1507. //---------------------------------------------------------------------------
  1508. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1509. TFileOperation Operation, TOperationSide Side,
  1510. bool Temp, const UnicodeString & FileName, Boolean Success,
  1511. TOnceDoneOperation & OnceDoneOperation)
  1512. {
  1513. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1514. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1515. OnceDoneOperation);
  1516. }
  1517. //---------------------------------------------------------------------------
  1518. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1519. {
  1520. return false;
  1521. }
  1522. //---------------------------------------------------------------------------
  1523. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1524. {
  1525. return false;
  1526. }
  1527. //---------------------------------------------------------------------------
  1528. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1529. {
  1530. return NULL;
  1531. }
  1532. //---------------------------------------------------------------------------
  1533. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1534. {
  1535. DebugAssert(GetSide(Side) == osRemote);
  1536. DebugUsedParam(Side);
  1537. return RemoteDirView;
  1538. }
  1539. //---------------------------------------------------------------------------
  1540. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1541. {
  1542. DebugAssert(GetSide(Side) == osRemote);
  1543. DebugUsedParam(Side);
  1544. return RemoteDriveView;
  1545. }
  1546. //---------------------------------------------------------------------------
  1547. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1548. {
  1549. DebugAssert(GetSide(Side) == osRemote);
  1550. DebugUsedParam(Side);
  1551. return (Terminal != NULL) && Terminal->Active;
  1552. }
  1553. //---------------------------------------------------------------------------
  1554. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1555. TOperationSide Side, int FilesOnly)
  1556. {
  1557. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1558. }
  1559. //---------------------------------------------------------------------------
  1560. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1561. TOperationSide Side, int FilesOnly)
  1562. {
  1563. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1564. }
  1565. //---------------------------------------------------------------------------
  1566. struct THistoryItemData
  1567. {
  1568. short int Side;
  1569. short int Index;
  1570. };
  1571. //---------------------------------------------------------------------------
  1572. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1573. {
  1574. DirView(Side)->HistoryGo(Index);
  1575. }
  1576. //---------------------------------------------------------------------------
  1577. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1578. {
  1579. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1580. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1581. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1582. }
  1583. //---------------------------------------------------------------------------
  1584. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1585. bool Back)
  1586. {
  1587. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1588. {
  1589. TCustomDirView * DView = DirView(Side);
  1590. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1591. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1592. if (ICount > 10)
  1593. {
  1594. ICount = 10;
  1595. }
  1596. Menu->Items->Clear();
  1597. THistoryItemData Data;
  1598. Data.Side = Side;
  1599. for (short int i = 1; i <= ICount; i++)
  1600. {
  1601. TTBCustomItem * Item = new TTBXItem(Menu);
  1602. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1603. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1604. Item->Hint = DView->HistoryPath[Data.Index];
  1605. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1606. Item->Tag = *reinterpret_cast<int*>(&Data);
  1607. Item->OnClick = HistoryItemClick;
  1608. Menu->Items->Add(Item);
  1609. }
  1610. }
  1611. }
  1612. //---------------------------------------------------------------------------
  1613. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1614. TOperationSide Side, bool Back)
  1615. {
  1616. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1617. {
  1618. // workaround
  1619. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1620. // operator new allocates memory in Pascal code, but calls inline
  1621. // contructor in C++, leading in problems, the function does
  1622. // both in Pascal code
  1623. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1624. UpdateHistoryMenu(Side, Back);
  1625. }
  1626. return FHistoryMenu[Side == osLocal][Back];
  1627. }
  1628. //---------------------------------------------------------------------------
  1629. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1630. TCustomDirView *Sender)
  1631. {
  1632. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1633. UpdateHistoryMenu(Side, true);
  1634. UpdateHistoryMenu(Side, false);
  1635. }
  1636. //---------------------------------------------------------------------------
  1637. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1638. {
  1639. // remote custom commands can be executed only if the server supports shell commands
  1640. // or have secondary shell
  1641. return (FTerminal != NULL) && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1642. }
  1643. //---------------------------------------------------------------------------
  1644. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1645. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1646. {
  1647. int Result;
  1648. TFileCustomCommand RemoteCustomCommand;
  1649. TLocalCustomCommand LocalCustomCommand;
  1650. TFileCustomCommand * NonInteractiveCustomCommand =
  1651. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1652. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1653. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1654. if (FLAGCLEAR(Command.Params, ccLocal))
  1655. {
  1656. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1657. // custom command that does not operate with files can be executed anytime ...
  1658. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1659. {
  1660. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1661. {
  1662. // ... but do not show such command in remote file menu (TODO)
  1663. Result = AllowedState;
  1664. }
  1665. else
  1666. {
  1667. Result = -1;
  1668. }
  1669. }
  1670. else
  1671. {
  1672. if ((ListType == ccltAll) || (ListType == ccltFile))
  1673. {
  1674. Result = (!IsSideLocalBrowser(FCurrentSide) && DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1675. }
  1676. else
  1677. {
  1678. Result = -1;
  1679. }
  1680. }
  1681. }
  1682. else
  1683. {
  1684. // custom command that does not operate with files can be executed anytime
  1685. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1686. {
  1687. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1688. }
  1689. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1690. {
  1691. if ((ListType == ccltAll) || (ListType == ccltFile))
  1692. {
  1693. // special case is "diff"-style command that can be executed over any side,
  1694. // if we have both sides
  1695. Result =
  1696. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1697. // directly (instead of EnableSelectedOperation) to pass
  1698. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1699. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1700. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1701. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1702. }
  1703. else if (ListType == ccltBoth)
  1704. {
  1705. DebugAssert(!IsLocalBrowserMode());
  1706. Result = 1;
  1707. }
  1708. else
  1709. {
  1710. Result = -1;
  1711. }
  1712. }
  1713. else
  1714. {
  1715. if ((ListType == ccltAll) || (ListType == ccltFile))
  1716. {
  1717. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1718. }
  1719. else
  1720. {
  1721. Result = -1;
  1722. }
  1723. }
  1724. }
  1725. return Result;
  1726. }
  1727. //---------------------------------------------------------------------------
  1728. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1729. TStrings * FileList, const TCustomCommandType & ACommand,
  1730. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1731. {
  1732. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1733. {
  1734. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1735. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1736. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1737. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1738. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1739. bool Capture =
  1740. FLAGSET(ACommand.Params, ccShowResults) ||
  1741. FLAGSET(ACommand.Params, ccCopyResults) ||
  1742. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1743. TCaptureOutputEvent OutputEvent = NULL;
  1744. DebugAssert(FCapturedLog == NULL);
  1745. if (Capture)
  1746. {
  1747. FCapturedLog = new TStringList();
  1748. OutputEvent = TerminalCaptureLog;
  1749. }
  1750. try
  1751. {
  1752. if (!RemoteCustomCommand.IsFileCommand(Command))
  1753. {
  1754. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1755. OutputEvent);
  1756. }
  1757. else
  1758. {
  1759. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1760. }
  1761. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1762. {
  1763. if (FLAGSET(ACommand.Params, ccCopyResults))
  1764. {
  1765. CopyToClipboard(FCapturedLog);
  1766. }
  1767. if (FLAGSET(ACommand.Params, ccShowResults))
  1768. {
  1769. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1770. }
  1771. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1772. {
  1773. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1774. }
  1775. }
  1776. }
  1777. __finally
  1778. {
  1779. SAFE_DESTROY(FCapturedLog);
  1780. }
  1781. }
  1782. }
  1783. //---------------------------------------------------------------------------
  1784. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1785. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1786. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1787. {
  1788. DebugAssert(!IsLocalBrowserMode());
  1789. TStrings * LocalFileList = NULL;
  1790. TStrings * RemoteFileList = NULL;
  1791. TStrings * RemoteFileListFull = NULL;
  1792. try
  1793. {
  1794. if (LocalFileCommand)
  1795. {
  1796. if (ALocalFileList == NULL)
  1797. {
  1798. DebugAssert(HasDirView[osLocal]);
  1799. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1800. // directly (instead of EnableSelectedOperation) to pass
  1801. // false to FocusedFileOnlyWhenFocused
  1802. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1803. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1804. }
  1805. else
  1806. {
  1807. LocalFileList = ALocalFileList;
  1808. }
  1809. if (FileListCommand)
  1810. {
  1811. if (LocalFileList->Count != 1)
  1812. {
  1813. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1814. }
  1815. }
  1816. else
  1817. {
  1818. if ((LocalFileList->Count != 1) &&
  1819. (FileList->Count != 1) &&
  1820. (LocalFileList->Count != FileList->Count))
  1821. {
  1822. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1823. }
  1824. }
  1825. }
  1826. UnicodeString RootTempDir;
  1827. UnicodeString TempDir;
  1828. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1829. if (!RemoteFiles)
  1830. {
  1831. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1832. }
  1833. try
  1834. {
  1835. TDateTimes RemoteFileTimes;
  1836. if (RemoteFiles)
  1837. {
  1838. RemoteFileList = FileList;
  1839. }
  1840. else
  1841. {
  1842. RemoteFileList = new TStringList();
  1843. TMakeLocalFileListParams MakeFileListParam;
  1844. MakeFileListParam.FileList = RemoteFileList;
  1845. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1846. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1847. MakeFileListParam.Recursive =
  1848. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1849. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1850. if (!MakeFileListParam.Recursive)
  1851. {
  1852. RemoteFileTimes.clear();
  1853. RemoteFileListFull = new TStringList();
  1854. MakeFileListParam.FileList = RemoteFileListFull;
  1855. MakeFileListParam.Recursive = true;
  1856. MakeFileListParam.IncludeDirs = false;
  1857. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1858. }
  1859. else
  1860. {
  1861. RemoteFileListFull = RemoteFileList;
  1862. }
  1863. }
  1864. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1865. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1866. if (!NonBlocking)
  1867. {
  1868. int Count;
  1869. if (FileListCommand)
  1870. {
  1871. Count = 1;
  1872. }
  1873. else if (LocalFileCommand)
  1874. {
  1875. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1876. }
  1877. else
  1878. {
  1879. Count = RemoteFileList->Count;
  1880. }
  1881. Progress.Start(foCustomCommand, osRemote, Count);
  1882. DebugAssert(FProgressForm != NULL);
  1883. FProgressForm->ReadOnly = true;
  1884. }
  1885. try
  1886. {
  1887. if (FileListCommand)
  1888. {
  1889. UnicodeString LocalFile;
  1890. // MakeFileList does not delimit filenames
  1891. UnicodeString FileList = MakeFileList(RemoteFileList);
  1892. if (LocalFileCommand)
  1893. {
  1894. DebugAssert(LocalFileList->Count == 1);
  1895. LocalFile = LocalFileList->Strings[0];
  1896. }
  1897. TLocalCustomCommand CustomCommand(Data,
  1898. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1899. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1900. if (NonBlocking)
  1901. {
  1902. DebugAssert(!POutput);
  1903. ExecuteShellChecked(ShellCommand);
  1904. }
  1905. else
  1906. {
  1907. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1908. }
  1909. }
  1910. else if (LocalFileCommand)
  1911. {
  1912. if (LocalFileList->Count == 1)
  1913. {
  1914. UnicodeString LocalFile = LocalFileList->Strings[0];
  1915. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1916. {
  1917. UnicodeString FileName = RemoteFileList->Strings[Index];
  1918. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1919. Progress.SetFile(FileNameForProgress);
  1920. TLocalCustomCommand CustomCommand(Data,
  1921. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1922. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1923. TOnceDoneOperation OnceDoneOperation;
  1924. // Do not unselect anything, as it won't work with two panels anyway
  1925. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1926. }
  1927. }
  1928. else if (RemoteFileList->Count == 1)
  1929. {
  1930. UnicodeString FileName = RemoteFileList->Strings[0];
  1931. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1932. {
  1933. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1934. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1935. Progress.SetFile(LocalFileNameOnly);
  1936. TLocalCustomCommand CustomCommand(
  1937. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1938. FileName, LocalFileName, L"");
  1939. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1940. TOnceDoneOperation OnceDoneOperation;
  1941. // Do not unselect anything, as it won't work with two panels anyway
  1942. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1943. }
  1944. }
  1945. else
  1946. {
  1947. if (LocalFileList->Count != RemoteFileList->Count)
  1948. {
  1949. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1950. }
  1951. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1952. {
  1953. UnicodeString FileName = RemoteFileList->Strings[Index];
  1954. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1955. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1956. Progress.SetFile(LocalFileNameOnly); // sic
  1957. TLocalCustomCommand CustomCommand(
  1958. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1959. FileName, LocalFileName, L"");
  1960. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1961. TOnceDoneOperation OnceDoneOperation;
  1962. // Do not unselect anything, as it won't work with two panels anyway
  1963. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1964. }
  1965. }
  1966. }
  1967. else
  1968. {
  1969. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1970. {
  1971. UnicodeString FileName = RemoteFileList->Strings[Index];
  1972. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1973. Progress.SetFile(FileNameForProgress);
  1974. TLocalCustomCommand CustomCommand(Data,
  1975. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1976. FileName, L"", L"");
  1977. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1978. TOnceDoneOperation OnceDoneOperation;
  1979. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  1980. }
  1981. }
  1982. }
  1983. __finally
  1984. {
  1985. if (!NonBlocking)
  1986. {
  1987. Progress.Stop();
  1988. }
  1989. }
  1990. DebugAssert(!FAutoOperation);
  1991. if (!RemoteFiles)
  1992. {
  1993. TempDir = IncludeTrailingBackslash(TempDir);
  1994. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1995. {
  1996. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1997. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1998. // Skip directories, process only nested files.
  1999. // The check is redundant as FileAge fails for directories anyway.
  2000. !DirectoryExists(FileName))
  2001. {
  2002. UnicodeString RemoteDir =
  2003. UnixExtractFileDir(
  2004. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  2005. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2006. TDateTime NewTime;
  2007. if (FileAge(FileName, NewTime) &&
  2008. (NewTime != RemoteFileTimes[Index]))
  2009. {
  2010. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2011. TemporaryFileCopyParam(CopyParam);
  2012. CopyParam.FileMask = L"";
  2013. FAutoOperation = true;
  2014. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2015. TemporaryFilesList->Add(FileName);
  2016. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2017. }
  2018. }
  2019. }
  2020. }
  2021. }
  2022. __finally
  2023. {
  2024. FAutoOperation = false;
  2025. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2026. {
  2027. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2028. }
  2029. }
  2030. }
  2031. __finally
  2032. {
  2033. if (RemoteFileList != FileList)
  2034. {
  2035. delete RemoteFileList;
  2036. }
  2037. if (LocalFileList != ALocalFileList)
  2038. {
  2039. delete LocalFileList;
  2040. }
  2041. if (RemoteFileListFull != RemoteFileList)
  2042. {
  2043. delete RemoteFileListFull;
  2044. }
  2045. }
  2046. }
  2047. //---------------------------------------------------------------------------
  2048. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2049. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2050. bool FileListCommand, UnicodeString * POutput)
  2051. {
  2052. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  2053. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2054. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2055. {
  2056. UnicodeString FileName = SelectedFileList->Strings[Index];
  2057. if (DirectoryExists(FileName))
  2058. {
  2059. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2060. {
  2061. LocalFileList->Add(FileName);
  2062. }
  2063. if (FLAGSET(ACommand.Params, ccRecursive))
  2064. {
  2065. TMakeLocalFileListParams MakeFileListParam;
  2066. MakeFileListParam.FileList = LocalFileList.get();
  2067. MakeFileListParam.FileTimes = NULL;
  2068. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2069. MakeFileListParam.Recursive = true;
  2070. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2071. }
  2072. }
  2073. else
  2074. {
  2075. LocalFileList->Add(FileName);
  2076. }
  2077. }
  2078. UnicodeString RemotePath;
  2079. if (Terminal != NULL)
  2080. {
  2081. RemotePath = Terminal->CurrentDirectory;
  2082. }
  2083. if (FileListCommand)
  2084. {
  2085. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2086. TLocalCustomCommand CustomCommand(
  2087. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2088. L"", L"", FileList);
  2089. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2090. }
  2091. else
  2092. {
  2093. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2094. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2095. DebugAssert(FProgressForm != NULL);
  2096. FProgressForm->ReadOnly = true;
  2097. try
  2098. {
  2099. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2100. {
  2101. UnicodeString FileName = LocalFileList->Strings[Index];
  2102. Progress.SetFile(FileName);
  2103. TLocalCustomCommand CustomCommand(
  2104. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2105. FileName, L"", L"");
  2106. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2107. TOnceDoneOperation OnceDoneOperation;
  2108. Progress.Finish(FileName, true, OnceDoneOperation);
  2109. }
  2110. }
  2111. __finally
  2112. {
  2113. Progress.Stop();
  2114. }
  2115. }
  2116. }
  2117. //---------------------------------------------------------------------------
  2118. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2119. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2120. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2121. {
  2122. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2123. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2124. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2125. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2126. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2127. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2128. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2129. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2130. std::unique_ptr<UnicodeString> POutput;
  2131. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2132. FLAGSET(ACommand.Params, ccCopyResults))
  2133. {
  2134. POutput.reset(new UnicodeString());
  2135. }
  2136. if (!LocalCustomCommand.IsFileCommand(Command))
  2137. {
  2138. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2139. }
  2140. // remote files?
  2141. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2142. {
  2143. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2144. }
  2145. // local files
  2146. else
  2147. {
  2148. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2149. }
  2150. if (POutput.get() != NULL)
  2151. {
  2152. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2153. int P = POutput->Pos(sLineBreak);
  2154. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2155. {
  2156. POutput->SetLength(P - 1);
  2157. }
  2158. // Copy even empty output to clipboard
  2159. if (FLAGSET(ACommand.Params, ccCopyResults))
  2160. {
  2161. CopyToClipboard(*POutput);
  2162. }
  2163. // But do not show an empty message box.
  2164. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2165. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2166. !POutput->IsEmpty())
  2167. {
  2168. TClipboardHandler ClipboardHandler;
  2169. ClipboardHandler.Text = *POutput;
  2170. TMessageParams Params;
  2171. TQueryButtonAlias Aliases[1];
  2172. Aliases[0].Button = qaRetry;
  2173. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2174. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2175. Params.Aliases = Aliases;
  2176. Params.AliasesCount = LENOF(Aliases);
  2177. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2178. }
  2179. }
  2180. }
  2181. //---------------------------------------------------------------------------
  2182. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2183. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2184. {
  2185. TCustomCommandData Data;
  2186. UnicodeString Site;
  2187. UnicodeString RemotePath;
  2188. if (Terminal != NULL)
  2189. {
  2190. Data = TCustomCommandData(Terminal);
  2191. Site = Terminal->SessionData->SessionKey;
  2192. RemotePath = Terminal->CurrentDirectory;
  2193. }
  2194. UnicodeString HelpKeyword = ACommand.HomePage;
  2195. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2196. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2197. {
  2198. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2199. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2200. {
  2201. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2202. }
  2203. else
  2204. {
  2205. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2206. }
  2207. if (!DoCustomCommandOptionsDialog(
  2208. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2209. {
  2210. Abort();
  2211. }
  2212. }
  2213. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2214. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2215. {
  2216. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2217. }
  2218. else
  2219. {
  2220. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2221. }
  2222. }
  2223. //---------------------------------------------------------------------------
  2224. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2225. const TCustomCommandType & Command)
  2226. {
  2227. DebugAssert(FCustomCommandLocalFileList != NULL);
  2228. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2229. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2230. TStrings * LocalFileList = new TStringList();
  2231. TStrings * RemoteFileList = new TStringList();
  2232. try
  2233. {
  2234. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2235. {
  2236. LocalFileList->Clear();
  2237. LocalFileList->AddObject(
  2238. FCustomCommandLocalFileList->Strings[Index],
  2239. FCustomCommandLocalFileList->Objects[Index]);
  2240. RemoteFileList->Clear();
  2241. RemoteFileList->AddObject(
  2242. FCustomCommandRemoteFileList->Strings[Index],
  2243. FCustomCommandRemoteFileList->Objects[Index]);
  2244. CustomCommand(RemoteFileList, Command, LocalFileList);
  2245. }
  2246. }
  2247. __finally
  2248. {
  2249. delete LocalFileList;
  2250. delete RemoteFileList;
  2251. }
  2252. }
  2253. //---------------------------------------------------------------------------
  2254. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2255. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2256. {
  2257. delete FCustomCommandLocalFileList;
  2258. delete FCustomCommandRemoteFileList;
  2259. // takeover ownership,
  2260. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2261. FCustomCommandLocalFileList = LocalFileList;
  2262. FCustomCommandRemoteFileList = RemoteFileList;
  2263. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2264. if (Button != NULL)
  2265. {
  2266. FCustomCommandMenu->Items->Clear();
  2267. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
  2268. MenuPopup(FCustomCommandMenu, Button);
  2269. }
  2270. else
  2271. {
  2272. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2273. }
  2274. }
  2275. //---------------------------------------------------------------------------
  2276. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2277. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2278. {
  2279. DebugAssert(FCapturedLog != NULL);
  2280. if ((OutputType == cotOutput) || (OutputType == cotError))
  2281. {
  2282. FCapturedLog->Add(AddedLine);
  2283. }
  2284. }
  2285. //---------------------------------------------------------------------------
  2286. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2287. TOperationSide Side)
  2288. {
  2289. return
  2290. (Side == osOther) &&
  2291. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2292. }
  2293. //---------------------------------------------------------------------------
  2294. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2295. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2296. {
  2297. TTBItemOptions O;
  2298. O = Item->Options;
  2299. TCustomDirView * DView = DirView(Side);
  2300. if ((DView->ItemFocused != NULL) &&
  2301. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2302. // when resolving links is disabled, default action is to enter the directory,
  2303. // no matter what DoubleClickAction is configured to
  2304. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2305. // Can only Edit files, but can Open/Copy even directories
  2306. ((DoubleClickAction != dcaEdit) ||
  2307. !DView->ItemIsDirectory(DView->ItemFocused)))
  2308. {
  2309. Item->Options = O << tboDefault;
  2310. }
  2311. else
  2312. {
  2313. Item->Options = O >> tboDefault;
  2314. }
  2315. }
  2316. //---------------------------------------------------------------------------
  2317. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2318. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2319. {
  2320. TCustomDirView * DView = DirView(Side);
  2321. TListItem * Item = DView->ItemFocused;
  2322. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2323. EnableFocusedOperation[Side])
  2324. {
  2325. TPoint ClientPoint;
  2326. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2327. TPoint(0, 0) : MousePos;
  2328. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2329. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2330. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2331. }
  2332. }
  2333. //---------------------------------------------------------------------------
  2334. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2335. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2336. {
  2337. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2338. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2339. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2340. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2341. Handled = true;
  2342. }
  2343. //---------------------------------------------------------------------------
  2344. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2345. {
  2346. }
  2347. //---------------------------------------------------------------------------
  2348. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2349. {
  2350. DebugAssert(FErrorList == NULL);
  2351. if (WinConfiguration->ContinueOnError)
  2352. {
  2353. FErrorList = new TStringList();
  2354. Configuration->Usage->Inc(L"ContinuationsOnError");
  2355. }
  2356. NonVisualDataModule->StartBusy();
  2357. }
  2358. //---------------------------------------------------------------------------
  2359. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2360. {
  2361. NonVisualDataModule->EndBusy();
  2362. if (FErrorList)
  2363. {
  2364. HandleErrorList(FErrorList);
  2365. }
  2366. }
  2367. //---------------------------------------------------------------------------
  2368. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2369. const TCopyParamType & CopyParam)
  2370. {
  2371. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2372. {
  2373. Configuration->Usage->Inc(L"FileMaskUses");
  2374. }
  2375. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2376. {
  2377. Configuration->Usage->Inc(L"OperationMaskUses");
  2378. }
  2379. }
  2380. //---------------------------------------------------------------------------
  2381. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2382. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2383. {
  2384. DebugAssert(!IsLocalBrowserMode());
  2385. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2386. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2387. TTransferOperationParam DefaultParam;
  2388. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2389. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2390. switch (Param.Queue)
  2391. {
  2392. case asOn:
  2393. CopyParam.Queue = true;
  2394. break;
  2395. case asOff:
  2396. CopyParam.Queue = false;
  2397. break;
  2398. case asAuto:
  2399. default:
  2400. // keep default
  2401. break;
  2402. }
  2403. bool Result =
  2404. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2405. if (Result)
  2406. {
  2407. DebugAssert(Terminal);
  2408. bool SelectionRestored = false;
  2409. TCustomDirView * DView = NULL;
  2410. if (HasDirView[Side])
  2411. {
  2412. DView = DirView(Side);
  2413. DView->SaveSelection();
  2414. DView->SaveSelectedNames();
  2415. }
  2416. UpdateCopyParamCounters(CopyParam);
  2417. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2418. DebugAssert(FTransferResumeList == NULL);
  2419. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2420. FMoveToQueue = false;
  2421. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2422. try
  2423. {
  2424. TStrings * PermanentFileList;
  2425. std::unique_ptr<TStrings> PermanentFileListOwner;
  2426. try
  2427. {
  2428. if (Side == osLocal)
  2429. {
  2430. PermanentFileList = FileList;
  2431. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2432. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2433. if (Operation == foMove)
  2434. {
  2435. ReloadLocalDirectory();
  2436. if (DView != NULL)
  2437. {
  2438. DView->RestoreSelection();
  2439. }
  2440. SelectionRestored = true;
  2441. }
  2442. }
  2443. else
  2444. {
  2445. // Clone the file list as it may refer to current directory files,
  2446. // which get destroyed, when the source directory is reloaded after foMove operation.
  2447. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2448. // But for a hotfix, we are not going to do such a big change.
  2449. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2450. PermanentFileList = PermanentFileListOwner.get();
  2451. try
  2452. {
  2453. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2454. }
  2455. __finally
  2456. {
  2457. if (Operation == foMove)
  2458. {
  2459. if (DView != NULL)
  2460. {
  2461. DView->RestoreSelection();
  2462. }
  2463. SelectionRestored = true;
  2464. }
  2465. ReloadLocalDirectory(Param.TargetDirectory);
  2466. }
  2467. }
  2468. }
  2469. catch (EAbort &)
  2470. {
  2471. if (FMoveToQueue)
  2472. {
  2473. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2474. DebugAssert(CopyParam.TransferSkipList == NULL);
  2475. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2476. if (TransferResumeList->Count > 0)
  2477. {
  2478. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2479. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2480. }
  2481. CopyParam.TransferSkipList = TransferResumeList.release();
  2482. // not really needed, just to keep it consistent with TransferResumeList
  2483. FTransferResumeList = NULL;
  2484. FMoveToQueue = false;
  2485. Configuration->Usage->Inc("MovesToBackground");
  2486. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2487. ClearTransferSourceSelection(Direction);
  2488. }
  2489. throw;
  2490. }
  2491. }
  2492. __finally
  2493. {
  2494. if (!SelectionRestored && (DView != NULL))
  2495. {
  2496. DView->DiscardSavedSelection();
  2497. }
  2498. FTransferResumeList = NULL;
  2499. }
  2500. }
  2501. return Result;
  2502. }
  2503. //---------------------------------------------------------------------------
  2504. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2505. TOperationSide Side, TStrings * FileList, void * Param)
  2506. {
  2507. DebugAssert(FileList->Count);
  2508. // We deliberately do not toggle alternative flag (Param), but use OR,
  2509. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2510. // shortcut of CurrentDeleteAlternativeAction
  2511. bool Alternative =
  2512. bool(Param) || UseAlternativeFunction();
  2513. bool Recycle;
  2514. if (IsSideLocalBrowser(Side))
  2515. {
  2516. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2517. }
  2518. else
  2519. {
  2520. Recycle =
  2521. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2522. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2523. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2524. }
  2525. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2526. if (!Result)
  2527. {
  2528. UnicodeString Query;
  2529. if (FileList->Count == 1)
  2530. {
  2531. if (IsSideLocalBrowser(Side))
  2532. {
  2533. Query = ExtractFileName(FileList->Strings[0]);
  2534. }
  2535. else
  2536. {
  2537. Query = UnixExtractFileName(FileList->Strings[0]);
  2538. }
  2539. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2540. }
  2541. else
  2542. {
  2543. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2544. }
  2545. TMessageParams Params(mpNeverAskAgainCheck);
  2546. Params.ImageName = L"Delete file";
  2547. unsigned int Answer =
  2548. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2549. if (Answer == qaNeverAskAgain)
  2550. {
  2551. Result = true;
  2552. if (Recycle)
  2553. {
  2554. WinConfiguration->ConfirmRecycling = false;
  2555. }
  2556. else
  2557. {
  2558. WinConfiguration->ConfirmDeleting = false;
  2559. }
  2560. }
  2561. else
  2562. {
  2563. Result = (Answer == qaOK);
  2564. }
  2565. }
  2566. if (Result)
  2567. {
  2568. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2569. }
  2570. return Result;
  2571. }
  2572. //---------------------------------------------------------------------------
  2573. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2574. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2575. {
  2576. TAutoBatch AutoBatch(this);
  2577. bool Result;
  2578. if ((Operation == foCopy) || (Operation == foMove))
  2579. {
  2580. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2581. }
  2582. else if (Operation == foRename)
  2583. {
  2584. DebugAssert(DirView(Side)->ItemFocused);
  2585. DirView(Side)->ItemFocused->EditCaption();
  2586. Result = true;
  2587. }
  2588. else if (Operation == foDelete)
  2589. {
  2590. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2591. }
  2592. else if (Operation == foSetProperties)
  2593. {
  2594. DirView(osRemote)->SaveSelectedNames();
  2595. Result = SetProperties(Side, FileList);
  2596. }
  2597. else if (Operation == foCustomCommand)
  2598. {
  2599. DebugAssert(Param);
  2600. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2601. DirView(osRemote)->SaveSelectedNames();
  2602. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2603. CustomCommand(FileList, *Command, NULL);
  2604. Result = true;
  2605. }
  2606. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2607. {
  2608. DebugAssert(!IsSideLocalBrowser(Side));
  2609. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2610. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2611. }
  2612. else if (Operation == foLock)
  2613. {
  2614. DebugAssert(!IsSideLocalBrowser(Side));
  2615. LockFiles(FileList, true);
  2616. Result = true;
  2617. }
  2618. else if (Operation == foUnlock)
  2619. {
  2620. DebugAssert(!IsSideLocalBrowser(Side));
  2621. LockFiles(FileList, false);
  2622. Result = true;
  2623. }
  2624. else
  2625. {
  2626. DebugFail();
  2627. }
  2628. return Result;
  2629. }
  2630. //---------------------------------------------------------------------------
  2631. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2632. {
  2633. if (Side == osCurrent)
  2634. {
  2635. Side = FCurrentSide;
  2636. }
  2637. return Side;
  2638. }
  2639. //---------------------------------------------------------------------------
  2640. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2641. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2642. {
  2643. Side = GetSide(Side);
  2644. bool Result;
  2645. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, IsSideLocalBrowser(Side), NULL);
  2646. try
  2647. {
  2648. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2649. }
  2650. __finally
  2651. {
  2652. delete FileList;
  2653. }
  2654. return Result;
  2655. }
  2656. //---------------------------------------------------------------------------
  2657. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2658. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2659. bool NoConfirmation, void * Param)
  2660. {
  2661. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2662. {
  2663. if ((Operation == foCopy) || (Operation == foMove))
  2664. {
  2665. DebugAssert(!IsLocalBrowserMode());
  2666. if (GetSide(Side) == osLocal)
  2667. {
  2668. Configuration->Usage->Inc(L"UploadsCommand");
  2669. }
  2670. else
  2671. {
  2672. Configuration->Usage->Inc(L"DownloadsCommand");
  2673. }
  2674. }
  2675. }
  2676. }
  2677. //---------------------------------------------------------------------------
  2678. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2679. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2680. {
  2681. TTransferOperationParam Param;
  2682. if ((WinConfiguration->Interface != ifCommander) ||
  2683. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2684. {
  2685. Flags &= ~cocShortCutHint;
  2686. }
  2687. TCustomDirView * DView = DirView(Side);
  2688. Param.Options =
  2689. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2690. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2691. if (FLAGSET(Flags, cocQueue))
  2692. {
  2693. Param.Queue = asOn;
  2694. }
  2695. else if (FLAGSET(Flags, cocNonQueue))
  2696. {
  2697. Param.Queue = asOff;
  2698. }
  2699. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2700. }
  2701. //---------------------------------------------------------------------------
  2702. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2703. {
  2704. try
  2705. {
  2706. if (ErrorList->Count)
  2707. {
  2708. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2709. if (MessageDialog(Message, qtError,
  2710. qaOK | qaCancel, HELP_NONE) == qaOK)
  2711. {
  2712. unsigned int Answer;
  2713. int Index = 0;
  2714. do
  2715. {
  2716. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2717. TQueryButtonAlias Aliases[2];
  2718. Aliases[0].Button = qaYes;
  2719. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2720. Aliases[1].Button = qaNo;
  2721. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2722. TMessageParams Params;
  2723. Params.Aliases = Aliases;
  2724. Params.AliasesCount = LENOF(Aliases);
  2725. Answer = MoreMessageDialog(
  2726. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2727. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2728. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2729. qaOK, HELP_NONE, &Params);
  2730. if (Answer == qaNo)
  2731. {
  2732. Index++;
  2733. }
  2734. if (Answer == qaYes)
  2735. {
  2736. Index--;
  2737. }
  2738. }
  2739. while (Answer != qaOK);
  2740. }
  2741. }
  2742. }
  2743. __finally
  2744. {
  2745. TStrings * List = ErrorList;
  2746. ErrorList = NULL;
  2747. for (int i = 0; i < List->Count; i++)
  2748. {
  2749. delete List->Objects[i];
  2750. }
  2751. delete List;
  2752. }
  2753. }
  2754. //---------------------------------------------------------------------------
  2755. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2756. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2757. {
  2758. // needed for checking filemasks, as there's no directory object
  2759. // associated with the file object
  2760. File->FullFileName = FullFileName;
  2761. TFileMasks::TParams MaskParams;
  2762. MaskParams.Size = File->Size;
  2763. MaskParams.Modification = File->Modification;
  2764. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2765. }
  2766. //---------------------------------------------------------------------------
  2767. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2768. {
  2769. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2770. Side = GetSide(Side);
  2771. TCustomDirView * DView = DirView(Side);
  2772. TListItem * FocusedItem = DView->ItemFocused;
  2773. UnicodeString Name;
  2774. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2775. {
  2776. Name = DView->ItemFileName(FocusedItem);
  2777. }
  2778. else
  2779. {
  2780. Name = LoadStr(NEW_FILE);
  2781. }
  2782. UnicodeString Names = Name;
  2783. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2784. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2785. HELP_EDIT_NEW, History.get(), true))
  2786. {
  2787. while (!Names.IsEmpty())
  2788. {
  2789. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2790. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2791. UnicodeString TargetFileName;
  2792. UnicodeString LocalFileName;
  2793. UnicodeString RootTempDir;
  2794. UnicodeString TempDir;
  2795. UnicodeString RemoteDirectory;
  2796. bool ExistingFile = false;
  2797. if (!IsSideLocalBrowser(Side))
  2798. {
  2799. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2800. TRemoteFile * File = NULL;
  2801. if (FTerminal->FileExists(Name, &File) &&
  2802. !File->IsDirectory)
  2803. {
  2804. try
  2805. {
  2806. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2807. ExistingFile = true;
  2808. }
  2809. __finally
  2810. {
  2811. delete File;
  2812. }
  2813. }
  2814. if (!ExistingFile)
  2815. {
  2816. RemoteDirectory = UnixExtractFilePath(Name);
  2817. TemporaryDirectoryForRemoteFiles(
  2818. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2819. TargetFileName = UnixExtractFileName(Name);
  2820. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2821. LocalFileName = TempDir +
  2822. // We probably do not want to trim the VMS version here
  2823. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2824. }
  2825. }
  2826. else
  2827. {
  2828. if (ExtractFilePath(Name).IsEmpty())
  2829. {
  2830. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2831. }
  2832. else
  2833. {
  2834. LocalFileName = ExpandFileName(Name);
  2835. }
  2836. TargetFileName = ExtractFileName(Name);
  2837. }
  2838. if (!ExistingFile)
  2839. {
  2840. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2841. if (NewFile)
  2842. {
  2843. int File = FileCreate(ApiPath(LocalFileName));
  2844. if (File < 0)
  2845. {
  2846. if (!RootTempDir.IsEmpty())
  2847. {
  2848. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2849. }
  2850. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2851. }
  2852. else
  2853. {
  2854. FileClose(File);
  2855. }
  2856. }
  2857. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2858. const TEditorData * ExternalEditor = NULL;
  2859. TFileMasks::TParams MaskParams; // size not known
  2860. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2861. false, MaskParams);
  2862. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2863. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  2864. }
  2865. }
  2866. }
  2867. }
  2868. //---------------------------------------------------------------------------
  2869. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2870. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2871. {
  2872. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2873. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2874. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2875. return
  2876. ((ExecuteFileBy == efInternalEditor)) ||
  2877. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2878. }
  2879. //---------------------------------------------------------------------------
  2880. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2881. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2882. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2883. UnicodeString RemoteDirectory, bool NewFile)
  2884. {
  2885. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2886. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2887. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2888. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2889. Side = GetSide(Side);
  2890. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2891. if (!IsSideLocalBrowser(Side))
  2892. {
  2893. Data->Terminal = Terminal;
  2894. Data->Queue = Queue;
  2895. Data->SessionData = CloneCurrentSessionData();
  2896. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2897. Data->RemoteDirectory = RemoteDirectory;
  2898. Data->SessionName = Terminal->SessionData->SessionName;
  2899. Data->LocalRootDirectory = LocalRootDirectory;
  2900. Data->OriginalFileName = OriginalFileName;
  2901. Data->Command = L""; // will be changed later for external editor
  2902. }
  2903. if (ExecuteFileBy == efInternalEditor)
  2904. {
  2905. if (!IsSideLocalBrowser(Side))
  2906. {
  2907. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2908. L" - " + Terminal->SessionData->SessionName;
  2909. TForm * Editor;
  2910. try
  2911. {
  2912. TNotifyEvent OnFileChanged = NULL;
  2913. TNotifyEvent OnSaveAll = NULL;
  2914. TAnyModifiedEvent OnAnyModified = NULL;
  2915. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2916. if (Terminal->IsCapable[fsBackgroundTransfers])
  2917. {
  2918. OnFileChanged = FEditorManager->FileChanged;
  2919. OnSaveAll = SaveAllInternalEditors;
  2920. OnAnyModified = AnyInternalEditorModified;
  2921. }
  2922. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2923. FEditorManager->FileReload, FEditorManager->FileClosed,
  2924. OnSaveAll, OnAnyModified,
  2925. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  2926. }
  2927. catch(...)
  2928. {
  2929. if (!LocalRootDirectory.IsEmpty())
  2930. {
  2931. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2932. }
  2933. throw;
  2934. }
  2935. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2936. }
  2937. else
  2938. {
  2939. DebugAssert(!FStandaloneEditing);
  2940. TForm * Editor =
  2941. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2942. SaveAllInternalEditors, AnyInternalEditorModified,
  2943. L"", false, SessionColor, -1, NewFile);
  2944. FLocalEditors->Add(Editor);
  2945. }
  2946. }
  2947. else
  2948. {
  2949. HANDLE Process;
  2950. if (ExecuteFileBy == efExternalEditor)
  2951. {
  2952. UnicodeString Program, Params, Dir;
  2953. Data->Command = ExternalEditor->ExternalEditor;
  2954. ReformatFileNameCommand(Data->Command);
  2955. SplitCommand(Data->Command, Program, Params, Dir);
  2956. Params = ExpandFileNameCommand(Params, FileName);
  2957. Program = ExpandEnvironmentVariables(Program);
  2958. if (!ExecuteShell(Program, Params, Process))
  2959. {
  2960. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2961. }
  2962. }
  2963. else
  2964. {
  2965. DebugAssert(IsSideLocalBrowser(Side));
  2966. if (!ExecuteShell(FileName, L"", Process))
  2967. {
  2968. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2969. }
  2970. }
  2971. if (IsSideLocalBrowser(Side) ||
  2972. ((ExecuteFileBy == efShell) &&
  2973. !WinConfiguration->Editor.SDIShellEditor) ||
  2974. ((ExecuteFileBy == efExternalEditor) &&
  2975. !ExternalEditor->SDIExternalEditor))
  2976. {
  2977. // no need for handle
  2978. if (Process != NULL)
  2979. {
  2980. DebugCheck(CloseHandle(Process));
  2981. }
  2982. Process = INVALID_HANDLE_VALUE;
  2983. }
  2984. else
  2985. {
  2986. if (Process == NULL)
  2987. {
  2988. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2989. }
  2990. }
  2991. if (!IsSideLocalBrowser(Side))
  2992. {
  2993. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2994. }
  2995. }
  2996. }
  2997. //---------------------------------------------------------------------------
  2998. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2999. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3000. void * /*Arg*/)
  3001. {
  3002. if (Token != NULL)
  3003. {
  3004. EditorFormFileSave(static_cast<TForm *>(Token));
  3005. }
  3006. }
  3007. //---------------------------------------------------------------------------
  3008. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3009. {
  3010. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3011. {
  3012. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3013. }
  3014. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3015. }
  3016. //---------------------------------------------------------------------------
  3017. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3018. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3019. void * Arg)
  3020. {
  3021. if ((Token != NULL) &&
  3022. IsEditorFormModified(static_cast<TForm *>(Token)))
  3023. {
  3024. *static_cast<bool *>(Arg) = true;
  3025. }
  3026. }
  3027. //---------------------------------------------------------------------------
  3028. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3029. TObject * /*Sender*/, bool & Modified)
  3030. {
  3031. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3032. {
  3033. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3034. {
  3035. Modified = true;
  3036. }
  3037. }
  3038. if (!Modified)
  3039. {
  3040. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3041. }
  3042. }
  3043. //---------------------------------------------------------------------------
  3044. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3045. {
  3046. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3047. }
  3048. //---------------------------------------------------------------------------
  3049. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3050. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3051. UnicodeString & Result, UnicodeString & RootDirectory)
  3052. {
  3053. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3054. {
  3055. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3056. Result = RootDirectory;
  3057. }
  3058. else
  3059. {
  3060. RootDirectory = L"";
  3061. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3062. Result = IncludeTrailingBackslash(Result);
  3063. }
  3064. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3065. {
  3066. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3067. }
  3068. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3069. {
  3070. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3071. {
  3072. RemoteDirectory.Delete(1, 1);
  3073. }
  3074. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3075. }
  3076. if (!ForceDirectories(ApiPath(Result)))
  3077. {
  3078. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3079. }
  3080. }
  3081. //---------------------------------------------------------------------------
  3082. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3083. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3084. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3085. {
  3086. DebugAssert(!IsLocalBrowserMode());
  3087. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3088. if (ForceText)
  3089. {
  3090. CopyParam.TransferMode = tmAscii;
  3091. }
  3092. TemporaryFileCopyParam(CopyParam);
  3093. if (AllFiles)
  3094. {
  3095. CopyParam.IncludeFileMask = TFileMasks();
  3096. }
  3097. if (TempDir.IsEmpty())
  3098. {
  3099. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3100. }
  3101. DebugAssert(!FAutoOperation);
  3102. FAutoOperation = AutoOperation;
  3103. Terminal->ExceptionOnFail = true;
  3104. try
  3105. {
  3106. try
  3107. {
  3108. // turn off confirmations, as for MDI editors we may possibly download
  3109. // the same file over
  3110. Terminal->CopyToLocal(
  3111. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3112. if (GetTargetNames)
  3113. {
  3114. for (int i = 0; i < FileList->Count; i++)
  3115. {
  3116. FileList->Strings[i] =
  3117. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3118. }
  3119. }
  3120. }
  3121. catch(...)
  3122. {
  3123. if (!RootTempDir.IsEmpty())
  3124. {
  3125. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3126. }
  3127. throw;
  3128. }
  3129. }
  3130. __finally
  3131. {
  3132. FAutoOperation = false;
  3133. Terminal->ExceptionOnFail = false;
  3134. }
  3135. }
  3136. //---------------------------------------------------------------------------
  3137. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3138. {
  3139. // Do not waste time checking for default editor list the next time,
  3140. // or testing if default editor is not notepad.
  3141. bool TryNextTime = false;
  3142. UnicodeString UsageState;
  3143. if (!WinConfiguration->EditorList->IsDefaultList())
  3144. {
  3145. UsageState = "H";
  3146. }
  3147. else
  3148. {
  3149. UnicodeString Executable;
  3150. UnicodeString ExecutableDescription;
  3151. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3152. {
  3153. UnicodeString Message =
  3154. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3155. unsigned int Answer =
  3156. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3157. if (Answer != qaOK)
  3158. {
  3159. UsageState = "R";
  3160. }
  3161. else
  3162. {
  3163. UsageState = "A";
  3164. TEditorData EditorData;
  3165. EditorData.Editor = edExternal;
  3166. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3167. EditorData.DecideExternalEditorText();
  3168. TEditorList EditorList;
  3169. EditorList = *WinConfiguration->EditorList;
  3170. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3171. WinConfiguration->EditorList = &EditorList;
  3172. }
  3173. }
  3174. }
  3175. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3176. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3177. }
  3178. //---------------------------------------------------------------------------
  3179. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3180. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3181. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3182. {
  3183. if (ExecuteFileBy == efDefaultEditor)
  3184. {
  3185. if (!WinConfiguration->OfferedEditorAutoConfig)
  3186. {
  3187. EditorAutoConfig();
  3188. }
  3189. const TEditorPreferences * Editor =
  3190. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3191. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3192. {
  3193. ExecuteFileBy = efInternalEditor;
  3194. ExternalEditor = NULL;
  3195. }
  3196. else if (Editor->Data->Editor == edOpen)
  3197. {
  3198. ExecuteFileBy = efShell;
  3199. ExternalEditor = NULL;
  3200. }
  3201. else
  3202. {
  3203. ExecuteFileBy = efExternalEditor;
  3204. ExternalEditor = Editor->Data;
  3205. }
  3206. }
  3207. }
  3208. //---------------------------------------------------------------------------
  3209. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3210. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3211. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3212. {
  3213. UnicodeString OriginalFileName;
  3214. UnicodeString LocalRootDirectory;
  3215. UnicodeString RemoteDirectory;
  3216. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3217. IsSideLocalBrowser(Side), MaskParams);
  3218. UnicodeString Counter;
  3219. UnicodeString LocalFileName;
  3220. bool Handled = false;
  3221. if (!IsSideLocalBrowser(Side))
  3222. {
  3223. // We need to trim VMS version here, so that we use name without version
  3224. // when uploading back to create a new version of the file
  3225. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3226. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3227. TObject * Token = NULL;
  3228. UnicodeString LocalDirectory;
  3229. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3230. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3231. LocalDirectory))
  3232. {
  3233. if (Token != NULL)
  3234. {
  3235. TForm * Form = dynamic_cast<TForm *>(Token);
  3236. if (Form->WindowState == wsMinimized)
  3237. {
  3238. ShowWindow(Form->Handle, SW_RESTORE);
  3239. }
  3240. else
  3241. {
  3242. Form->SetFocus();
  3243. }
  3244. Handled = true;
  3245. }
  3246. else
  3247. {
  3248. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3249. }
  3250. }
  3251. if (!Handled)
  3252. {
  3253. TStringList * FileList1 = new TStringList();
  3254. try
  3255. {
  3256. FileList1->AddObject(FullFileName, Object);
  3257. TemporarilyDownloadFiles(FileList1,
  3258. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3259. LocalRootDirectory, LocalDirectory, true, true, true);
  3260. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3261. }
  3262. __finally
  3263. {
  3264. delete FileList1;
  3265. }
  3266. switch (ExecuteFileBy)
  3267. {
  3268. case efShell:
  3269. Counter = "RemoteFilesExecuted";
  3270. break;
  3271. case efInternalEditor:
  3272. Counter = "RemoteFilesOpenedInInternalEditor";
  3273. break;
  3274. case efExternalEditor:
  3275. Counter = "RemoteFilesOpenedInExternalEditor";
  3276. break;
  3277. default:
  3278. DebugFail();
  3279. }
  3280. }
  3281. }
  3282. else
  3283. {
  3284. LocalFileName = FullFileName;
  3285. OriginalFileName = ExtractFileName(FullFileName);
  3286. switch (ExecuteFileBy)
  3287. {
  3288. case efShell:
  3289. Counter = "LocalFilesExecuted";
  3290. break;
  3291. case efInternalEditor:
  3292. Counter = "LocalFilesOpenedInInternalEditor";
  3293. break;
  3294. case efExternalEditor:
  3295. Counter = "LocalFilesOpenedInExternalEditor";
  3296. break;
  3297. default:
  3298. DebugFail();
  3299. }
  3300. }
  3301. if (!Handled)
  3302. {
  3303. Configuration->Usage->Inc(Counter);
  3304. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3305. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3306. }
  3307. }
  3308. //---------------------------------------------------------------------------
  3309. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3310. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3311. bool AllSelected, bool OnFocused)
  3312. {
  3313. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3314. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3315. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3316. Side = GetSide(Side);
  3317. TCustomDirView * DView = DirView(Side);
  3318. TStrings * FileList =
  3319. AllSelected ?
  3320. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3321. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3322. try
  3323. {
  3324. DebugAssert(AllSelected || (FileList->Count == 1));
  3325. for (int i = 0; i < FileList->Count; i++)
  3326. {
  3327. UnicodeString ListFileName = FileList->Strings[i];
  3328. UnicodeString FileNameOnly =
  3329. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3330. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3331. if (!DView->ItemIsDirectory(Item))
  3332. {
  3333. UnicodeString FullFileName;
  3334. if (!IsSideLocalBrowser(Side))
  3335. {
  3336. FullFileName = DirView(Side)->Path + ListFileName;
  3337. }
  3338. else
  3339. {
  3340. FullFileName = ListFileName;
  3341. }
  3342. TFileMasks::TParams MaskParams;
  3343. MaskParams.Size = DView->ItemFileSize(Item);
  3344. TDateTimePrecision Precision;
  3345. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3346. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3347. FileList->Objects[i], MaskParams);
  3348. }
  3349. }
  3350. }
  3351. __finally
  3352. {
  3353. delete FileList;
  3354. }
  3355. }
  3356. //---------------------------------------------------------------------------
  3357. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3358. {
  3359. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3360. CopyParam.FileNameCase = ncNoChange;
  3361. CopyParam.PreserveRights = false;
  3362. CopyParam.PreserveReadOnly = false;
  3363. CopyParam.ReplaceInvalidChars = true;
  3364. CopyParam.IncludeFileMask = TFileMasks();
  3365. CopyParam.NewerOnly = false;
  3366. CopyParam.FileMask = L"";
  3367. }
  3368. //---------------------------------------------------------------------------
  3369. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3370. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3371. {
  3372. TTerminalManager * Manager = TTerminalManager::Instance();
  3373. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3374. {
  3375. if (!NonVisualDataModule->Busy)
  3376. {
  3377. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3378. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3379. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3380. if (Data->Terminal == NULL)
  3381. {
  3382. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3383. if (SameSiteTerminal != NULL)
  3384. {
  3385. UnicodeString Message =
  3386. FMTLOAD(EDIT_SESSION_REATTACH,
  3387. (FileNameOnly, Data->SessionName, FileNameOnly));
  3388. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3389. {
  3390. Data->Terminal = Terminal;
  3391. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3392. Data->SessionName = Terminal->SessionData->SessionName;
  3393. // We might also overwrite session data
  3394. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3395. }
  3396. else
  3397. {
  3398. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3399. Abort();
  3400. }
  3401. }
  3402. }
  3403. // foreground session should reconnect itself
  3404. else if (Terminal != Data->Terminal)
  3405. {
  3406. UnicodeString Message =
  3407. MainInstructions(
  3408. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3409. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3410. {
  3411. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3412. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3413. }
  3414. else
  3415. {
  3416. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3417. Abort();
  3418. }
  3419. }
  3420. }
  3421. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3422. {
  3423. Configuration->Usage->Inc(L"EditInactiveSession");
  3424. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3425. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3426. (ExtractFileName(FileName), Data->SessionName)));
  3427. }
  3428. }
  3429. TStrings * FileList = new TStringList();
  3430. try
  3431. {
  3432. FileList->Add(FileName);
  3433. // consider using the same settings (preset) as when the file was downloaded
  3434. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3435. TemporaryFileCopyParam(CopyParam);
  3436. if (Data->ForceText)
  3437. {
  3438. CopyParam.TransferMode = tmAscii;
  3439. }
  3440. // so i do not need to worry if masking algorithm works in all cases
  3441. // ("" means "copy file name", no masking is actually done)
  3442. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3443. {
  3444. CopyParam.FileMask = L"";
  3445. }
  3446. else
  3447. {
  3448. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3449. }
  3450. DebugAssert(Data->Queue != NULL);
  3451. int Params = cpNoConfirmation | cpTemporary;
  3452. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3453. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3454. QueueItem->CompleteEvent = UploadCompleteEvent;
  3455. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3456. }
  3457. __finally
  3458. {
  3459. delete FileList;
  3460. }
  3461. }
  3462. //---------------------------------------------------------------------------
  3463. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3464. const UnicodeString FileName, const TEditedFileData * Data)
  3465. {
  3466. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3467. DebugAssert(!NonVisualDataModule->Busy);
  3468. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3469. {
  3470. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3471. (ExtractFileName(FileName), Data->SessionName)));
  3472. }
  3473. TManagedTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3474. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3475. NonVisualDataModule->StartBusy();
  3476. try
  3477. {
  3478. std::unique_ptr<TRemoteFile> File;
  3479. UnicodeString RemoteFileName =
  3480. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3481. FTerminal->ExceptionOnFail = true;
  3482. try
  3483. {
  3484. TRemoteFile * AFile = NULL;
  3485. FTerminal->ReadFile(RemoteFileName, AFile);
  3486. File.reset(AFile);
  3487. if (!File->HaveFullFileName)
  3488. {
  3489. File->FullFileName = RemoteFileName;
  3490. }
  3491. }
  3492. __finally
  3493. {
  3494. FTerminal->ExceptionOnFail = false;
  3495. }
  3496. std::unique_ptr<TStrings> FileList(new TStringList());
  3497. FileList->AddObject(RemoteFileName, File.get());
  3498. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3499. UnicodeString TempDir = ExtractFilePath(FileName);
  3500. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3501. TempDir, true, true, true);
  3502. // sanity check, the target file name should be still the same
  3503. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3504. }
  3505. __finally
  3506. {
  3507. NonVisualDataModule->EndBusy();
  3508. // it actually may not exist anymore...
  3509. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3510. }
  3511. }
  3512. //---------------------------------------------------------------------------
  3513. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3514. const TEditedFileData * Data, bool & KeepOpen)
  3515. {
  3516. // Command is set for external editors only (not for "shell" open).
  3517. if (!Data->Command.IsEmpty())
  3518. {
  3519. bool AnyFound = false;
  3520. bool AnyMDI = false;
  3521. bool AnyNonMDI = false;
  3522. bool AnyDetect = false;
  3523. TEditorList * EditorList = new TEditorList();
  3524. try
  3525. {
  3526. *EditorList = *WinConfiguration->EditorList;
  3527. for (int i = 0; i < EditorList->Count; i++)
  3528. {
  3529. const TEditorPreferences * Editor = EditorList->Editors[i];
  3530. if ((Editor->Data->Editor == edExternal) &&
  3531. (Editor->Data->ExternalEditor == Data->Command))
  3532. {
  3533. AnyFound = true;
  3534. if (Editor->Data->SDIExternalEditor)
  3535. {
  3536. AnyNonMDI = true;
  3537. if (Editor->Data->DetectMDIExternalEditor)
  3538. {
  3539. AnyDetect = true;
  3540. }
  3541. }
  3542. else
  3543. {
  3544. AnyMDI = true;
  3545. }
  3546. }
  3547. }
  3548. bool EnableMDI = false;
  3549. bool DisableDetect = false;
  3550. if (AnyMDI)
  3551. {
  3552. KeepOpen = true;
  3553. if (AnyNonMDI)
  3554. {
  3555. // there is at least one instance of the editor with MDI support enabled,
  3556. // and one with disabled, enable it for all instances
  3557. EnableMDI = true;
  3558. }
  3559. }
  3560. else if (AnyFound && !AnyDetect)
  3561. {
  3562. // at least once instance found but all have MDI autodetection disabled
  3563. // => close the file (default action)
  3564. }
  3565. else
  3566. {
  3567. // no instance of the editor has MDI support enabled
  3568. TMessageParams Params;
  3569. if (AnyFound)
  3570. {
  3571. // there is at least one instance of the editor
  3572. Params.Params |= mpNeverAskAgainCheck;
  3573. }
  3574. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3575. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3576. switch (Answer)
  3577. {
  3578. case qaNeverAskAgain:
  3579. DisableDetect = true;
  3580. break;
  3581. case qaNo:
  3582. EnableMDI = true;
  3583. KeepOpen = true;
  3584. break;
  3585. }
  3586. }
  3587. if (AnyFound && (EnableMDI || DisableDetect))
  3588. {
  3589. bool Changed = false;
  3590. for (int i = 0; i < EditorList->Count; i++)
  3591. {
  3592. const TEditorPreferences * Editor = EditorList->Editors[i];
  3593. if ((Editor->Data->Editor == edExternal) &&
  3594. (Editor->Data->ExternalEditor == Data->Command) &&
  3595. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3596. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3597. {
  3598. Changed = true;
  3599. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3600. if (EnableMDI)
  3601. {
  3602. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3603. }
  3604. if (DisableDetect)
  3605. {
  3606. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3607. }
  3608. EditorList->Change(i, UpdatedEditor);
  3609. }
  3610. }
  3611. if (Changed)
  3612. {
  3613. WinConfiguration->EditorList = EditorList;
  3614. }
  3615. }
  3616. }
  3617. __finally
  3618. {
  3619. delete EditorList;
  3620. }
  3621. }
  3622. else
  3623. {
  3624. // "open" case
  3625. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3626. qaOK, HELP_APP_EARLY_CLOSED);
  3627. }
  3628. }
  3629. //---------------------------------------------------------------------------
  3630. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3631. {
  3632. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3633. }
  3634. //---------------------------------------------------------------------------
  3635. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3636. {
  3637. SideEnter(osRemote);
  3638. }
  3639. //---------------------------------------------------------------------------
  3640. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3641. {
  3642. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3643. SideEnter(osRemote);
  3644. }
  3645. //---------------------------------------------------------------------------
  3646. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3647. {
  3648. FCurrentSide = Side;
  3649. if (Visible)
  3650. {
  3651. UpdateControls();
  3652. }
  3653. }
  3654. //---------------------------------------------------------------------------
  3655. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3656. TStrings * FileList, bool Alternative)
  3657. {
  3658. TCustomDirView * DView = DirView(Side);
  3659. DView->SaveSelection();
  3660. DView->SaveSelectedNames();
  3661. DebugAssert(!FAlternativeDelete);
  3662. FAlternativeDelete = Alternative;
  3663. try
  3664. {
  3665. if (!IsSideLocalBrowser(Side))
  3666. {
  3667. DebugAssert(Terminal != NULL);
  3668. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3669. }
  3670. else
  3671. {
  3672. try
  3673. {
  3674. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3675. }
  3676. __finally
  3677. {
  3678. ReloadLocalDirectory();
  3679. }
  3680. }
  3681. FAlternativeDelete = false;
  3682. }
  3683. catch(...)
  3684. {
  3685. FAlternativeDelete = false;
  3686. DView->DiscardSavedSelection();
  3687. throw;
  3688. }
  3689. DView->RestoreSelection();
  3690. }
  3691. //---------------------------------------------------------------------------
  3692. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3693. {
  3694. DebugAssert(Terminal);
  3695. DebugAssert(!IsLocalBrowserMode());
  3696. RemoteDirView->SaveSelection();
  3697. RemoteDirView->SaveSelectedNames();
  3698. try
  3699. {
  3700. if (Lock)
  3701. {
  3702. Terminal->LockFiles(FileList);
  3703. }
  3704. else
  3705. {
  3706. Terminal->UnlockFiles(FileList);
  3707. }
  3708. }
  3709. catch(...)
  3710. {
  3711. RemoteDirView->DiscardSavedSelection();
  3712. throw;
  3713. }
  3714. RemoteDirView->RestoreSelection();
  3715. }
  3716. //---------------------------------------------------------------------------
  3717. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3718. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3719. bool NoConfirmation, bool Move)
  3720. {
  3721. DebugAssert(Terminal != NULL);
  3722. DebugAssert(!IsLocalBrowserMode());
  3723. // update Terminal->StateData->RemoteDirectory
  3724. UpdateTerminal(Terminal);
  3725. if (Session == NULL)
  3726. {
  3727. Session = TTerminalManager::Instance()->ActiveTerminal;
  3728. }
  3729. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3730. {
  3731. if (RemoteDriveView->DropTarget != NULL)
  3732. {
  3733. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3734. }
  3735. else if (RemoteDirView->DropTarget != NULL)
  3736. {
  3737. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3738. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3739. }
  3740. else
  3741. {
  3742. Target = RemoteDirView->Path;
  3743. }
  3744. }
  3745. else
  3746. {
  3747. Target = Session->StateData->RemoteDirectory;
  3748. }
  3749. Target = UnixIncludeTrailingBackslash(Target);
  3750. if (FileList->Count == 1)
  3751. {
  3752. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3753. }
  3754. else
  3755. {
  3756. FileMask = AnyMask;
  3757. }
  3758. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3759. bool Result = true;
  3760. if (!NoConfirmation)
  3761. {
  3762. bool Multi = (FileList->Count > 1);
  3763. if (Move)
  3764. {
  3765. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3766. }
  3767. else
  3768. {
  3769. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3770. TStrings * Directories = new TStringList;
  3771. try
  3772. {
  3773. for (int Index = 0; Index < Sessions->Count; Index++)
  3774. {
  3775. TManagedTerminal * Terminal =
  3776. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3777. Directories->Add(Terminal->StateData->RemoteDirectory);
  3778. }
  3779. TDirectRemoteCopy AllowDirectCopy;
  3780. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3781. {
  3782. DebugAssert(DirectCopy);
  3783. AllowDirectCopy = drcAllow;
  3784. }
  3785. else if (FTerminal->IsCapable[fcSecondaryShell])
  3786. {
  3787. DebugAssert(DirectCopy);
  3788. AllowDirectCopy = drcConfirmCommandSession;
  3789. }
  3790. else
  3791. {
  3792. DebugAssert(!DirectCopy);
  3793. AllowDirectCopy = drcDisallow;
  3794. }
  3795. void * ASession = Session;
  3796. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3797. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3798. Session = static_cast<TManagedTerminal *>(ASession);
  3799. }
  3800. __finally
  3801. {
  3802. delete Directories;
  3803. }
  3804. }
  3805. }
  3806. return Result;
  3807. }
  3808. //---------------------------------------------------------------------------
  3809. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3810. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3811. {
  3812. DebugAssert(!IsLocalBrowserMode());
  3813. bool DirectCopy;
  3814. UnicodeString Target, FileMask;
  3815. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3816. if (Result)
  3817. {
  3818. if (!Move && !DirectCopy)
  3819. {
  3820. Configuration->Usage->Inc("RemoteCopyTemp");
  3821. UnicodeString RootTempDir;
  3822. UnicodeString TempDir;
  3823. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3824. TStrings * TemporaryFilesList = new TStringList();
  3825. try
  3826. {
  3827. TMakeLocalFileListParams MakeFileListParam;
  3828. MakeFileListParam.FileList = TemporaryFilesList;
  3829. MakeFileListParam.FileTimes = NULL;
  3830. MakeFileListParam.IncludeDirs = true;
  3831. MakeFileListParam.Recursive = false;
  3832. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3833. TTerminalManager::Instance()->ActiveTerminal = Session;
  3834. if (TemporaryFilesList->Count > 0)
  3835. {
  3836. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3837. CopyParam.FileMask = FileMask;
  3838. DebugAssert(!FAutoOperation);
  3839. FAutoOperation = true;
  3840. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3841. }
  3842. }
  3843. __finally
  3844. {
  3845. delete TemporaryFilesList;
  3846. FAutoOperation = false;
  3847. if (!RootTempDir.IsEmpty())
  3848. {
  3849. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3850. }
  3851. }
  3852. }
  3853. else
  3854. {
  3855. RemoteDirView->SaveSelection();
  3856. RemoteDirView->SaveSelectedNames();
  3857. try
  3858. {
  3859. if (Move)
  3860. {
  3861. Configuration->Usage->Inc("RemoteMove");
  3862. Terminal->MoveFiles(FileList, Target, FileMask);
  3863. }
  3864. else
  3865. {
  3866. Configuration->Usage->Inc("RemoteCopyDirect");
  3867. DebugAssert(DirectCopy);
  3868. DebugAssert(Session == FTerminal);
  3869. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3870. FTerminal->CommandSessionOpened ||
  3871. CommandSessionFallback())
  3872. {
  3873. Terminal->CopyFiles(FileList, Target, FileMask);
  3874. }
  3875. }
  3876. }
  3877. catch(...)
  3878. {
  3879. RemoteDirView->DiscardSavedSelection();
  3880. throw;
  3881. }
  3882. RemoteDirView->RestoreSelection();
  3883. }
  3884. }
  3885. return Result;
  3886. }
  3887. //---------------------------------------------------------------------------
  3888. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3889. const UnicodeString & Path, TRemoteProperties & Properties)
  3890. {
  3891. DebugAssert(!IsLocalBrowserMode());
  3892. Properties.Valid = Properties.Valid << vpEncrypt;
  3893. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3894. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3895. }
  3896. //---------------------------------------------------------------------------
  3897. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3898. {
  3899. Side = GetSide(Side);
  3900. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3901. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  3902. UnicodeString Name = LoadStr(NEW_FOLDER);
  3903. int AllowedChanges =
  3904. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  3905. bool SaveSettings = false;
  3906. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3907. {
  3908. TWindowLock Lock(this);
  3909. if (!IsSideLocalBrowser(Side))
  3910. {
  3911. if (SaveSettings)
  3912. {
  3913. GUIConfiguration->NewDirectoryProperties = Properties;
  3914. }
  3915. CreateRemoteDirectory(Name, Properties);
  3916. }
  3917. else
  3918. {
  3919. DirView(Side)->CreateDirectory(Name);
  3920. }
  3921. }
  3922. }
  3923. //---------------------------------------------------------------------------
  3924. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3925. {
  3926. TWindowLock Lock(this);
  3927. DirView(Side)->ExecuteHomeDirectory();
  3928. }
  3929. //---------------------------------------------------------------------------
  3930. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3931. {
  3932. DoOpenDirectoryDialog(odBrowse, Side);
  3933. }
  3934. //---------------------------------------------------------------------------
  3935. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3936. {
  3937. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3938. bool Result = !Path.IsEmpty();
  3939. if (Result)
  3940. {
  3941. // While we might get here when the session is closed (from location profiles),
  3942. // it's not a problem as the Path setter is noop then
  3943. DirView(Side)->Path = Path;
  3944. }
  3945. return Result;
  3946. }
  3947. //---------------------------------------------------------------------------
  3948. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3949. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3950. bool & Close)
  3951. {
  3952. // terminal can be already closed (e.g. dropped connection)
  3953. if (Terminal != NULL)
  3954. {
  3955. try
  3956. {
  3957. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3958. }
  3959. catch(...)
  3960. {
  3961. if (!Terminal->Active)
  3962. {
  3963. Close = true;
  3964. }
  3965. throw;
  3966. }
  3967. }
  3968. }
  3969. //---------------------------------------------------------------------------
  3970. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3971. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3972. bool & Close)
  3973. {
  3974. // terminal can be already closed (e.g. dropped connection)
  3975. if (Terminal != NULL)
  3976. {
  3977. Configuration->Usage->Inc(L"ChecksumCalculated");
  3978. try
  3979. {
  3980. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3981. }
  3982. catch(...)
  3983. {
  3984. if (!Terminal->Active)
  3985. {
  3986. Close = true;
  3987. }
  3988. throw;
  3989. }
  3990. }
  3991. }
  3992. //---------------------------------------------------------------------------
  3993. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3994. {
  3995. bool Result;
  3996. if (!IsSideLocalBrowser(Side))
  3997. {
  3998. TRemoteTokenList * GroupList = NULL;
  3999. TRemoteTokenList * UserList = NULL;
  4000. try
  4001. {
  4002. TRemoteProperties CurrentProperties;
  4003. if (Terminal->LoadFilesProperties(FileList))
  4004. {
  4005. RemoteDirView->Invalidate();
  4006. }
  4007. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4008. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4009. if (CapableGroupChanging || CapableOwnerChanging)
  4010. {
  4011. if (CapableGroupChanging)
  4012. {
  4013. GroupList = Terminal->Groups->Duplicate();
  4014. }
  4015. if (CapableOwnerChanging)
  4016. {
  4017. UserList = Terminal->Users->Duplicate();
  4018. }
  4019. TRemoteDirectory * Files = Terminal->Files;
  4020. int Count = Files->Count;
  4021. if (Count > 100)
  4022. {
  4023. Count = 100;
  4024. }
  4025. for (int Index = 0; Index < Count; Index++)
  4026. {
  4027. TRemoteFile * File = Files->Files[Index];
  4028. if (CapableGroupChanging)
  4029. {
  4030. GroupList->AddUnique(File->Group);
  4031. }
  4032. if (CapableOwnerChanging)
  4033. {
  4034. UserList->AddUnique(File->Owner);
  4035. }
  4036. }
  4037. // if we haven't collected tokens for all files in current directory,
  4038. // make sure we collect them at least for all selected files.
  4039. // (note that so far the files in FileList has to be from current direcotry)
  4040. if (Count < Files->Count)
  4041. {
  4042. for (int Index = 0; Index < FileList->Count; Index++)
  4043. {
  4044. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4045. if (CapableGroupChanging)
  4046. {
  4047. GroupList->AddUnique(File->Group);
  4048. }
  4049. if (CapableOwnerChanging)
  4050. {
  4051. UserList->AddUnique(File->Owner);
  4052. }
  4053. }
  4054. }
  4055. }
  4056. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4057. int Flags = 0;
  4058. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4059. if (CapableOwnerChanging) Flags |= cpOwner;
  4060. if (CapableGroupChanging) Flags |= cpGroup;
  4061. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4062. if (Terminal->IsCapable[fcCalculatingChecksum])
  4063. {
  4064. CalculateChecksumEvent = CalculateChecksum;
  4065. }
  4066. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4067. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4068. TRemoteProperties NewProperties = CurrentProperties;
  4069. Result =
  4070. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4071. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4072. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4073. CalculateSize, CalculateChecksumEvent);
  4074. if (Result)
  4075. {
  4076. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4077. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4078. }
  4079. }
  4080. __finally
  4081. {
  4082. delete GroupList;
  4083. delete UserList;
  4084. }
  4085. }
  4086. else
  4087. {
  4088. DirView(Side)->DisplayPropertiesMenu();
  4089. Result = true;
  4090. }
  4091. return Result;
  4092. }
  4093. //---------------------------------------------------------------------------
  4094. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4095. {
  4096. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4097. {
  4098. FIgnoreNextDialogChar = Key;
  4099. }
  4100. Key = 0;
  4101. }
  4102. //---------------------------------------------------------------------------
  4103. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4104. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4105. {
  4106. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4107. if (Command != NULL)
  4108. {
  4109. KeyProcessed(Key, Shift);
  4110. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4111. {
  4112. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4113. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4114. false, false, const_cast<TCustomCommandType *>(Command));
  4115. }
  4116. }
  4117. }
  4118. //---------------------------------------------------------------------------
  4119. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4120. {
  4121. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4122. {
  4123. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4124. }
  4125. if (!DirView(osCurrent)->IsEditing())
  4126. {
  4127. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4128. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4129. {
  4130. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4131. if (((Action->ShortCut == KeyShortCut) ||
  4132. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4133. AllowedAction(Action, aaShortCut))
  4134. {
  4135. // Has to be called before the action as the dialog char is already in queue.
  4136. // So when the action consumes message queue, we already need to have the
  4137. // FIgnoreNextDialogChar set
  4138. KeyProcessed(Key, Shift);
  4139. // Reset reference to previous component (when menu/toolbar was clicked).
  4140. // Needed to detect that action was invoked by keyboard shortcut
  4141. // in TNonVisualDataModule::ExplorerActionsExecute
  4142. Action->ActionComponent = NULL;
  4143. Action->Execute();
  4144. return;
  4145. }
  4146. }
  4147. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4148. {
  4149. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4150. {
  4151. KeyProcessed(Key, Shift);
  4152. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4153. return;
  4154. }
  4155. }
  4156. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4157. {
  4158. KeyProcessed(Key, Shift);
  4159. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4160. }
  4161. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4162. if (IsCustomShortCut(CustomShortCut))
  4163. {
  4164. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4165. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4166. if (WinConfiguration->SharedBookmarks != NULL)
  4167. {
  4168. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4169. if ((Bookmark != NULL) &&
  4170. OpenBookmark(FCurrentSide, Bookmark))
  4171. {
  4172. KeyProcessed(Key, Shift);
  4173. }
  4174. }
  4175. }
  4176. }
  4177. TForm::KeyDown(Key, Shift);
  4178. }
  4179. //---------------------------------------------------------------------------
  4180. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4181. {
  4182. DebugAssert(Terminal != NULL);
  4183. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4184. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4185. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4186. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4187. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4188. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4189. // expanded from ?: to avoid memory leaks
  4190. if (SecurityEnabled)
  4191. {
  4192. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4193. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4194. }
  4195. else
  4196. {
  4197. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4198. }
  4199. if (FileSystemInfo.ProtocolName.IsEmpty())
  4200. {
  4201. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4202. }
  4203. else
  4204. {
  4205. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4206. }
  4207. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4208. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4209. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4210. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4211. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4212. UnicodeString CompressionStr = CSCompressionStr;
  4213. if (CSCompressionStr != SCCompressionStr)
  4214. {
  4215. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4216. }
  4217. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4218. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4219. UpdateStatusBar();
  4220. }
  4221. //---------------------------------------------------------------------------
  4222. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4223. {
  4224. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4225. DebugAssert(SessionStatusBar != NULL);
  4226. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4227. {
  4228. // note: (Terminal->Status < sshReady) currently never happens here,
  4229. // so STATUS_CONNECTING is never used
  4230. SessionStatusBar->SimplePanel = true;
  4231. SessionStatusBar->SimpleText = LoadStr(
  4232. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4233. }
  4234. else
  4235. {
  4236. DebugAssert(Terminal);
  4237. SessionStatusBar->SimplePanel = false;
  4238. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4239. if (!FNote.IsEmpty())
  4240. {
  4241. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4242. }
  4243. else
  4244. {
  4245. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4246. }
  4247. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4248. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4249. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4250. }
  4251. }
  4252. //---------------------------------------------------------------------------
  4253. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4254. {
  4255. Panel->Caption = L"";
  4256. }
  4257. //---------------------------------------------------------------------------
  4258. void __fastcall TCustomScpExplorerForm::Idle()
  4259. {
  4260. if (FShowing || FStandaloneEditing)
  4261. {
  4262. FEditorManager->Check();
  4263. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4264. // that needs to know if queue view is visible (and it may be closed after queue update)
  4265. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4266. }
  4267. if (FShowing)
  4268. {
  4269. if (!NonVisualDataModule->Busy &&
  4270. // Menu is opened or drag&drop is going on
  4271. (Mouse->Capture == NULL))
  4272. {
  4273. if (FRefreshRemoteDirectory)
  4274. {
  4275. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4276. {
  4277. Terminal->RefreshDirectory();
  4278. }
  4279. FRefreshRemoteDirectory = false;
  4280. }
  4281. if (FRefreshLocalDirectory)
  4282. {
  4283. ReloadLocalDirectory();
  4284. FRefreshLocalDirectory = false;
  4285. }
  4286. if (WinConfiguration->RefreshRemotePanel)
  4287. {
  4288. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4289. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4290. {
  4291. RemoteDirView->ReloadDirectory();
  4292. }
  4293. }
  4294. }
  4295. }
  4296. if (FShowing || FStandaloneEditing)
  4297. {
  4298. if (FQueueStatusInvalidated)
  4299. {
  4300. UpdateQueueStatus(false);
  4301. }
  4302. RefreshQueueItems();
  4303. }
  4304. if (FShowing)
  4305. {
  4306. UpdateStatusBar();
  4307. }
  4308. FIgnoreNextDialogChar = 0;
  4309. }
  4310. //---------------------------------------------------------------------------
  4311. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4312. {
  4313. try
  4314. {
  4315. FUserActionTimer->Enabled = false;
  4316. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4317. {
  4318. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4319. {
  4320. FPendingQueueActionItem->ProcessUserAction();
  4321. }
  4322. }
  4323. }
  4324. __finally
  4325. {
  4326. FPendingQueueActionItem = NULL;
  4327. }
  4328. }
  4329. //---------------------------------------------------------------------------
  4330. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4331. {
  4332. if (WinConfiguration->MinimizeToTray)
  4333. {
  4334. UpdateTrayIcon();
  4335. FTrayIcon->Visible = true;
  4336. if (Visible)
  4337. {
  4338. ShowWindow(Handle, SW_HIDE);
  4339. }
  4340. }
  4341. }
  4342. //---------------------------------------------------------------------------
  4343. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4344. {
  4345. // WORKAROUND
  4346. // When restoring maximized window from minimization,
  4347. // rarely some controls do not align properly.
  4348. // Two instances seen (both for Commander):
  4349. // - When restoring, window is temporarily narrower (not maximized),
  4350. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4351. // Once maximized already, top dock shinks back, but the session PageControl,
  4352. // do not align up, leaving space between TopDock and PageControl.
  4353. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4354. for (int Index = 0; Index < ControlCount; Index++)
  4355. {
  4356. RealignControl(Controls[Index]);
  4357. }
  4358. if (FTrayIcon->Visible)
  4359. {
  4360. FTrayIcon->Visible = false;
  4361. if (Visible)
  4362. {
  4363. ShowWindow(Handle, SW_SHOW);
  4364. }
  4365. }
  4366. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4367. {
  4368. FNeedSession = false;
  4369. NonVisualDataModule->StartBusy();
  4370. try
  4371. {
  4372. NeedSession(false);
  4373. }
  4374. __finally
  4375. {
  4376. NonVisualDataModule->EndBusy();
  4377. }
  4378. }
  4379. }
  4380. //---------------------------------------------------------------------------
  4381. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4382. {
  4383. FTrayIcon->Hint = GetMainForm()->Caption;
  4384. }
  4385. //---------------------------------------------------------------------------
  4386. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4387. {
  4388. UpdateTrayIcon();
  4389. }
  4390. //---------------------------------------------------------------------------
  4391. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4392. {
  4393. // workaround
  4394. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4395. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4396. // But WM_ACTIVATEAPP message can be received even when
  4397. // the main window is minimized to the tray and internal editor window is focused
  4398. // (after another application was previously active)
  4399. if (::IsIconic(Handle))
  4400. {
  4401. if (!IsAppIconic())
  4402. {
  4403. SetAppIconic(true);
  4404. }
  4405. }
  4406. ::ApplicationRestore();
  4407. Application->BringToFront();
  4408. }
  4409. //---------------------------------------------------------------------------
  4410. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4411. {
  4412. RestoreApp();
  4413. }
  4414. //---------------------------------------------------------------------------
  4415. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4416. {
  4417. if (OpenInNewWindow())
  4418. {
  4419. // todo: Pass FromSite
  4420. ExecuteNewInstance(SessionUrl);
  4421. }
  4422. else
  4423. {
  4424. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4425. }
  4426. }
  4427. //---------------------------------------------------------------------------
  4428. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4429. {
  4430. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4431. // current working directories become defaults here, what is not right
  4432. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4433. StoredSessions->NewSession(SessionName, SessionData.get());
  4434. // modified only, explicit
  4435. StoredSessions->Save(false, true);
  4436. // encode session name because of slashes in hierarchical sessions
  4437. return EncodeUrlString(SessionName);
  4438. }
  4439. //---------------------------------------------------------------------------
  4440. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4441. {
  4442. if (OpenInNewWindow())
  4443. {
  4444. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4445. }
  4446. else
  4447. {
  4448. // current working directories become defaults here, what is not right
  4449. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4450. TTerminalManager * Manager = TTerminalManager::Instance();
  4451. TManagedTerminal * Terminal = Manager->NewManagedTerminal(SessionData.get());
  4452. Manager->ActiveTerminal = Terminal;
  4453. }
  4454. }
  4455. //---------------------------------------------------------------------------
  4456. void __fastcall TCustomScpExplorerForm::RenameSession()
  4457. {
  4458. UnicodeString Name = Terminal->SessionData->SessionName;
  4459. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4460. {
  4461. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4462. TSessionData::ValidateName(Name);
  4463. // This is inconsistent with how color (for example) is handled.
  4464. Terminal->SessionData->Name = Name;
  4465. UpdateControls();
  4466. // Add/Remove distinguishing paths from sessions of the same name.
  4467. DoTerminalListChanged();
  4468. }
  4469. }
  4470. //---------------------------------------------------------------------------
  4471. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4472. {
  4473. DebugAssert(Queue != NULL);
  4474. bool Result = Queue->IsEmpty;
  4475. if (!Result)
  4476. {
  4477. SetFocus();
  4478. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4479. }
  4480. return Result;
  4481. }
  4482. //---------------------------------------------------------------------------
  4483. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4484. {
  4485. return CanCloseQueue(FQueue);
  4486. }
  4487. //---------------------------------------------------------------------------
  4488. void __fastcall TCustomScpExplorerForm::CloseSession()
  4489. {
  4490. if (CanCloseQueue())
  4491. {
  4492. TTerminalManager::Instance()->FreeActiveTerminal();
  4493. }
  4494. }
  4495. //---------------------------------------------------------------------------
  4496. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4497. {
  4498. if (CanCloseQueue())
  4499. {
  4500. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4501. }
  4502. }
  4503. //---------------------------------------------------------------------------
  4504. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4505. {
  4506. RemoteDirView->Terminal = Terminal;
  4507. UpdateRemotePathComboBox(false);
  4508. UpdateControls();
  4509. }
  4510. //---------------------------------------------------------------------------
  4511. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4512. {
  4513. if (DebugAlwaysTrue(!Terminal->Active))
  4514. {
  4515. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4516. }
  4517. }
  4518. //---------------------------------------------------------------------------
  4519. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4520. {
  4521. if (OpenInNewWindow())
  4522. {
  4523. // encode session name because of slashes in hierarchical sessions
  4524. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4525. }
  4526. else
  4527. {
  4528. TTerminalManager * Manager = TTerminalManager::Instance();
  4529. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4530. Manager->ActiveTerminal = Terminal;
  4531. }
  4532. }
  4533. //---------------------------------------------------------------------------
  4534. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4535. {
  4536. if (OpenInNewWindow())
  4537. {
  4538. ExecuteNewInstance(Name);
  4539. }
  4540. else
  4541. {
  4542. TTerminalManager * Manager = TTerminalManager::Instance();
  4543. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4544. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4545. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  4546. }
  4547. }
  4548. //---------------------------------------------------------------------------
  4549. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4550. bool &CanClose)
  4551. {
  4552. if (Terminal != NULL)
  4553. {
  4554. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4555. {
  4556. unsigned int Result;
  4557. TMessageParams Params(mpNeverAskAgainCheck);
  4558. UnicodeString Message;
  4559. int Answers = qaOK | qaCancel;
  4560. if (TTerminalManager::Instance()->Count > 1)
  4561. {
  4562. if (!WinConfiguration->AutoSaveWorkspace)
  4563. {
  4564. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE2);
  4565. Answers = qaYes | qaNo | qaCancel;
  4566. }
  4567. else
  4568. {
  4569. Message = MainInstructions(LoadStr(CLOSE_SESSIONS));
  4570. }
  4571. }
  4572. else
  4573. {
  4574. Message = MainInstructions(FMTLOAD(CLOSE_SESSION, (Terminal->SessionData->SessionName)));
  4575. }
  4576. if (WinConfiguration->AutoSaveWorkspace)
  4577. {
  4578. Message =
  4579. FORMAT("%s\n\n%s", (Message,
  4580. FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()))));
  4581. }
  4582. SetFocus();
  4583. Result = MessageDialog(Message, qtConfirmation,
  4584. Answers, HELP_NONE, &Params);
  4585. if (Result == qaNeverAskAgain)
  4586. {
  4587. WinConfiguration->ConfirmClosingSession = false;
  4588. }
  4589. if (Result == qaNo)
  4590. {
  4591. CanClose = SaveWorkspace(true);
  4592. // note that the workspace will be saved redundatly again from FormClose
  4593. }
  4594. else
  4595. {
  4596. CanClose =
  4597. (Result == qaOK) ||
  4598. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4599. (Result == qaNeverAskAgain);
  4600. }
  4601. }
  4602. if (CanClose)
  4603. {
  4604. CanClose = CanCloseQueue();
  4605. }
  4606. }
  4607. if (CanClose)
  4608. {
  4609. CanClose =
  4610. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4611. FEditorManager->CloseExternalFilesWithoutProcess();
  4612. if (CanClose)
  4613. {
  4614. while (CanClose && (FLocalEditors->Count > 0))
  4615. {
  4616. int PrevCount = FLocalEditors->Count;
  4617. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4618. CanClose = (FLocalEditors->Count < PrevCount);
  4619. }
  4620. if (CanClose)
  4621. {
  4622. CanClose = FEditorManager->Empty(true);
  4623. if (!CanClose)
  4624. {
  4625. SetFocus();
  4626. CanClose =
  4627. (MessageDialog(
  4628. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4629. }
  4630. }
  4631. }
  4632. }
  4633. }
  4634. //---------------------------------------------------------------------------
  4635. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4636. {
  4637. TForm * Form = dynamic_cast<TForm *>(Sender);
  4638. DebugAssert(Form != NULL);
  4639. Form->Close();
  4640. }
  4641. //---------------------------------------------------------------------------
  4642. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4643. {
  4644. TForm * Form = dynamic_cast<TForm *>(Sender);
  4645. delete Form;
  4646. }
  4647. //---------------------------------------------------------------------------
  4648. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4649. {
  4650. while (FLocalEditors->Count > 0)
  4651. {
  4652. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4653. }
  4654. }
  4655. //---------------------------------------------------------------------------
  4656. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4657. TObject *Sender)
  4658. {
  4659. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4660. try
  4661. {
  4662. SetProperties(osRemote, FileList);
  4663. }
  4664. __finally
  4665. {
  4666. delete FileList;
  4667. }
  4668. }
  4669. //---------------------------------------------------------------------------
  4670. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4671. {
  4672. if (value)
  4673. {
  4674. if (Component == fcCustomCommandsBand)
  4675. {
  4676. UpdateCustomCommandsToolbar();
  4677. }
  4678. }
  4679. }
  4680. //---------------------------------------------------------------------------
  4681. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4682. {
  4683. TControl * Control = GetComponent(Component);
  4684. DebugAssert(Control);
  4685. bool Changed = (Control->Visible != value);
  4686. if (Changed)
  4687. {
  4688. ComponentShowing(Component, value);
  4689. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4690. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4691. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4692. if (value)
  4693. {
  4694. int RemainingHeight = Control->Parent->ClientHeight;
  4695. int RemainingWidth = Control->Parent->ClientWidth;
  4696. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4697. {
  4698. TControl * ChildControl = Control->Parent->Controls[i];
  4699. if (ChildControl->Visible)
  4700. {
  4701. switch (ChildControl->Align)
  4702. {
  4703. case alTop:
  4704. case alBottom:
  4705. RemainingHeight -= ChildControl->Height;
  4706. break;
  4707. case alLeft:
  4708. case alRight:
  4709. RemainingWidth -= ChildControl->Width;
  4710. break;
  4711. }
  4712. }
  4713. }
  4714. int Reserve = ScaleByTextHeight(this, 32);
  4715. // queue in explorer, trees in commander
  4716. if (Control->Height > RemainingHeight - Reserve)
  4717. {
  4718. Control->Height = RemainingHeight / 2;
  4719. }
  4720. if (Control->Width > RemainingWidth - Reserve)
  4721. {
  4722. Control->Width = RemainingWidth / 2;
  4723. }
  4724. }
  4725. Control->Visible = value;
  4726. if (WasFocused && Visible)
  4727. {
  4728. DirView(osCurrent)->SetFocus();
  4729. }
  4730. FixControlsPlacement();
  4731. }
  4732. }
  4733. //---------------------------------------------------------------------------
  4734. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4735. {
  4736. TControl * Control = GetComponent(Component);
  4737. if (Control == NULL)
  4738. {
  4739. return false;
  4740. }
  4741. else
  4742. {
  4743. return Control->Visible;
  4744. }
  4745. }
  4746. //---------------------------------------------------------------------------
  4747. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4748. {
  4749. bool Result;
  4750. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4751. {
  4752. Result = !IsUWP();
  4753. }
  4754. else
  4755. {
  4756. Result = true;
  4757. }
  4758. return Result;
  4759. }
  4760. //---------------------------------------------------------------------------
  4761. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4762. {
  4763. if (DirView(osOther)->ItemFocused != NULL)
  4764. {
  4765. DirView(osOther)->ItemFocused->MakeVisible(false);
  4766. }
  4767. QueueSplitter->Visible = QueuePanel->Visible;
  4768. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4769. }
  4770. //---------------------------------------------------------------------------
  4771. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4772. {
  4773. switch (Component) {
  4774. case fcStatusBar: return RemoteStatusBar;
  4775. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4776. case fcQueueView: return QueuePanel;
  4777. case fcQueueToolbar: return QueueDock;
  4778. case fcRemoteTree: return RemoteDrivePanel;
  4779. case fcSessionsTabs: return SessionsPageControl;
  4780. default: return NULL;
  4781. }
  4782. }
  4783. //---------------------------------------------------------------------------
  4784. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4785. TObject *Sender, TListColumn *Column, TPoint &Point)
  4786. {
  4787. DebugAssert(NonVisualDataModule && Column && Sender);
  4788. NonVisualDataModule->ListColumn = Column;
  4789. TPopupMenu * DirViewColumnMenu;
  4790. if (dynamic_cast<TUnixDirView *>(Sender) != NULL)
  4791. {
  4792. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4793. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4794. (Column->Index == uvName);
  4795. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4796. (Column->Index == uvSize);
  4797. }
  4798. else
  4799. {
  4800. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4801. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4802. (Column->Index == dvName);
  4803. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4804. (Column->Index == dvSize);
  4805. }
  4806. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4807. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4808. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4809. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4810. }
  4811. //---------------------------------------------------------------------------
  4812. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4813. TObject *Sender, TListItem *Item, bool &AllowExec)
  4814. {
  4815. DoDirViewExecFile(Sender, Item, AllowExec);
  4816. }
  4817. //---------------------------------------------------------------------------
  4818. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4819. TListItem * Item, bool & AllowExec)
  4820. {
  4821. DebugAssert(Sender && Item && Configuration);
  4822. DebugAssert(AllowExec);
  4823. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4824. bool Remote = (ADirView == DirView(osRemote)) && !IsSideLocalBrowser(osRemote);
  4825. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4826. TOperationSide Side = (Remote ? osRemote : osLocal); // TODO
  4827. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4828. // on files only (not directories)
  4829. // and only when symlinks are resolved (apply to remote panel only)
  4830. if (!ADirView->ItemIsDirectory(Item) &&
  4831. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4832. {
  4833. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4834. !FForceExecution &&
  4835. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4836. {
  4837. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4838. {
  4839. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4840. UnlockWindow();
  4841. try
  4842. {
  4843. ExecuteFileOperation(
  4844. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4845. }
  4846. __finally
  4847. {
  4848. LockWindow();
  4849. }
  4850. AllowExec = false;
  4851. }
  4852. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4853. {
  4854. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4855. {
  4856. ExecuteFile(Side, efDefaultEditor);
  4857. AllowExec = false;
  4858. }
  4859. }
  4860. else
  4861. {
  4862. DebugFail();
  4863. }
  4864. }
  4865. // if we have not done anything special, fall back to default behavior
  4866. if (AllowExec)
  4867. {
  4868. if (Remote && !WinConfiguration->DisableOpenEdit)
  4869. {
  4870. ExecuteFile(osRemote, efShell);
  4871. AllowExec = false;
  4872. }
  4873. }
  4874. }
  4875. }
  4876. //---------------------------------------------------------------------------
  4877. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4878. {
  4879. return ((Side == osRemote) || (Side == osCurrent));
  4880. }
  4881. //---------------------------------------------------------------------------
  4882. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4883. {
  4884. DebugFail();
  4885. }
  4886. //---------------------------------------------------------------------------
  4887. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4888. {
  4889. DebugFail();
  4890. }
  4891. //---------------------------------------------------------------------------
  4892. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4893. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, bool UseDefaults)
  4894. {
  4895. TSynchronizeParamType Params;
  4896. Params.LocalDirectory = LocalDirectory;
  4897. Params.RemoteDirectory = RemoteDirectory;
  4898. int UnusedParams =
  4899. (GUIConfiguration->SynchronizeParams &
  4900. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4901. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4902. Params.Options = GUIConfiguration->SynchronizeOptions;
  4903. bool SaveSettings = false;
  4904. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4905. &DoSynchronizeTooManyDirectories);
  4906. DebugAssert(FSynchronizeController == NULL);
  4907. FSynchronizeController = &Controller;
  4908. bool Result;
  4909. try
  4910. {
  4911. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4912. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4913. int Options =
  4914. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4915. DebugAssert(FOnFeedSynchronizeError == NULL);
  4916. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4917. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4918. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4919. if (Result)
  4920. {
  4921. if (SaveSettings)
  4922. {
  4923. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4924. GUIConfiguration->SynchronizeOptions = Params.Options;
  4925. }
  4926. else
  4927. {
  4928. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4929. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4930. FLAGSET(Params.Options, soSynchronize))
  4931. {
  4932. GUIConfiguration->SynchronizeOptions =
  4933. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4934. soSynchronize;
  4935. }
  4936. }
  4937. LocalDirectory = Params.LocalDirectory;
  4938. RemoteDirectory = Params.RemoteDirectory;
  4939. }
  4940. }
  4941. __finally
  4942. {
  4943. FSynchronizeController = NULL;
  4944. DebugAssert(FOnFeedSynchronizeError == NULL);
  4945. FOnFeedSynchronizeError = NULL;
  4946. }
  4947. return Result;
  4948. }
  4949. //---------------------------------------------------------------------------
  4950. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4951. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4952. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4953. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4954. TSynchronizeOptions * Options, bool Full)
  4955. {
  4956. // Keep-up-to-date mode
  4957. if (Terminal->Status == ssOpened)
  4958. {
  4959. try
  4960. {
  4961. int PParams = Params.Params;
  4962. if (!Full)
  4963. {
  4964. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4965. if (FLAGSET(Params.Options, soRecurse))
  4966. {
  4967. PParams |= TTerminal::spSubDirs;
  4968. }
  4969. }
  4970. else
  4971. {
  4972. // if keepuptodate is non-recursive,
  4973. // full sync before has to be non-recursive as well
  4974. if (FLAGCLEAR(Params.Options, soRecurse))
  4975. {
  4976. PParams |= TTerminal::spNoRecurse;
  4977. }
  4978. }
  4979. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  4980. PParams, Checklist, Options);
  4981. }
  4982. catch (Exception & E)
  4983. {
  4984. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  4985. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  4986. {
  4987. // noop, already logged in MoreMessageDialog
  4988. }
  4989. else
  4990. {
  4991. ShowExtendedExceptionEx(Terminal, &E);
  4992. throw;
  4993. }
  4994. }
  4995. }
  4996. }
  4997. //---------------------------------------------------------------------------
  4998. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  4999. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5000. const UnicodeString ErrorStr)
  5001. {
  5002. if (!Directory.IsEmpty())
  5003. {
  5004. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5005. }
  5006. else
  5007. {
  5008. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5009. }
  5010. }
  5011. //---------------------------------------------------------------------------
  5012. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5013. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5014. {
  5015. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5016. {
  5017. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5018. }
  5019. else
  5020. {
  5021. TMessageParams Params(mpNeverAskAgainCheck);
  5022. unsigned int Result = MessageDialog(
  5023. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5024. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5025. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5026. {
  5027. MaxDirectories *= 2;
  5028. if (Result == qaNeverAskAgain)
  5029. {
  5030. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5031. }
  5032. }
  5033. else
  5034. {
  5035. Abort();
  5036. }
  5037. }
  5038. }
  5039. //---------------------------------------------------------------------------
  5040. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5041. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5042. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5043. TSynchronizeOptions * Options)
  5044. {
  5045. // Keep-up-to-date mode
  5046. DebugAssert(!FAutoOperation);
  5047. void * BatchStorage;
  5048. BatchStart(BatchStorage);
  5049. FAutoOperation = true;
  5050. bool AnyOperation = false;
  5051. TDateTime StartTime = Now();
  5052. TSynchronizeChecklist * AChecklist = NULL;
  5053. try
  5054. {
  5055. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5056. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5057. {
  5058. FSynchronizeProgressForm->Start();
  5059. }
  5060. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5061. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5062. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5063. Options);
  5064. SAFE_DESTROY(FSynchronizeProgressForm);
  5065. AnyOperation = (AChecklist->CheckedCount > 0);
  5066. if (AnyOperation)
  5067. {
  5068. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5069. CreateProgressForm(&SynchronizeProgress);
  5070. try
  5071. {
  5072. Terminal->SynchronizeApply(
  5073. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5074. SynchronizeProcessedItem, NULL, NULL, NULL);
  5075. }
  5076. __finally
  5077. {
  5078. DestroyProgressForm();
  5079. }
  5080. }
  5081. }
  5082. __finally
  5083. {
  5084. if (Checklist == NULL)
  5085. {
  5086. delete AChecklist;
  5087. }
  5088. else
  5089. {
  5090. *Checklist = AChecklist;
  5091. }
  5092. FAutoOperation = false;
  5093. SAFE_DESTROY(FSynchronizeProgressForm);
  5094. BatchEnd(BatchStorage);
  5095. ReloadLocalDirectory();
  5096. if (AnyOperation)
  5097. {
  5098. OperationComplete(StartTime);
  5099. }
  5100. }
  5101. }
  5102. //---------------------------------------------------------------------------
  5103. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5104. {
  5105. // can be called from command line
  5106. return Visible &&
  5107. ((DirView(osOther)->SelCount > 0) ||
  5108. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5109. }
  5110. //---------------------------------------------------------------------------
  5111. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5112. {
  5113. LogSynchronizeEvent(Terminal, Message);
  5114. }
  5115. //---------------------------------------------------------------------------
  5116. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5117. int Params, TSynchronizeOptions & Options)
  5118. {
  5119. DebugAssert(!IsLocalBrowserMode());
  5120. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5121. {
  5122. Options.Filter = new TStringList();
  5123. Options.Filter->CaseSensitive = false;
  5124. Options.Filter->Duplicates = Types::dupAccept;
  5125. if (DirView(osRemote)->SelCount > 0)
  5126. {
  5127. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5128. }
  5129. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5130. {
  5131. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5132. }
  5133. Options.Filter->Sort();
  5134. }
  5135. }
  5136. //---------------------------------------------------------------------------
  5137. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5138. {
  5139. TCopyParamType Defaults;
  5140. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5141. ConfigStorage->AccessMode = smRead;
  5142. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5143. {
  5144. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5145. }
  5146. ConfigStorage.reset(NULL);
  5147. std::unique_ptr<TStringList> Options(new TStringList());
  5148. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5149. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5150. UnicodeString Result;
  5151. if (Options->Count > 0)
  5152. {
  5153. Result =
  5154. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5155. }
  5156. return Result;
  5157. }
  5158. //---------------------------------------------------------------------------
  5159. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5160. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5161. {
  5162. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5163. UnicodeString AdditionalParams =
  5164. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5165. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5166. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  5167. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5168. SerializeCopyParamForCommandLine(CopyParams)));
  5169. ExecuteNewInstance(SessionName, AdditionalParams);
  5170. }
  5171. //---------------------------------------------------------------------------
  5172. struct TSynchronizeParams
  5173. {
  5174. UnicodeString LocalDirectory;
  5175. UnicodeString RemoteDirectory;
  5176. TSynchronizeMode Mode;
  5177. TSynchronizeChecklist * Checklist;
  5178. int Params;
  5179. TCopyParamType * CopyParam;
  5180. TDateTime CollectElapsed;
  5181. TDateTime * StartTime;
  5182. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5183. };
  5184. //---------------------------------------------------------------------------
  5185. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5186. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5187. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5188. {
  5189. DebugAssert(!FAutoOperation);
  5190. void * BatchStorage;
  5191. BatchStart(BatchStorage);
  5192. FAutoOperation = true;
  5193. TFileOperationStatistics Statistics;
  5194. TDateTime Start = Now();
  5195. try
  5196. {
  5197. Params.OnProcessedItem = OnProcessedItem;
  5198. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5199. CreateProgressForm(&SynchronizeProgress);
  5200. Terminal->SynchronizeApply(
  5201. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5202. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5203. &Statistics);
  5204. }
  5205. __finally
  5206. {
  5207. Params.OnProcessedItem = NULL;
  5208. FAutoOperation = false;
  5209. DestroyProgressForm();
  5210. BatchEnd(BatchStorage);
  5211. ReloadLocalDirectory();
  5212. }
  5213. if (WinConfiguration->SynchronizeSummary)
  5214. {
  5215. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5216. // The statistics should be 0 anyway in this case
  5217. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5218. {
  5219. Message += L"\n";
  5220. if (Statistics.FilesUploaded > 0)
  5221. {
  5222. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5223. }
  5224. if (Statistics.FilesDownloaded > 0)
  5225. {
  5226. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5227. }
  5228. if (Statistics.FilesDeletedLocal > 0)
  5229. {
  5230. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5231. }
  5232. if (Statistics.FilesDeletedRemote > 0)
  5233. {
  5234. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5235. }
  5236. }
  5237. TDateTime Elapsed = (Now() - Start);
  5238. Message +=
  5239. L"\n" +
  5240. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5241. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5242. TMessageParams Params(mpNeverAskAgainCheck);
  5243. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5244. if (Result == qaNeverAskAgain)
  5245. {
  5246. WinConfiguration->SynchronizeSummary = false;
  5247. }
  5248. }
  5249. }
  5250. //---------------------------------------------------------------------------
  5251. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5252. {
  5253. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5254. {
  5255. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5256. }
  5257. // Not set in keep-up-to-date mode - Synchronize() method
  5258. if (Token != NULL)
  5259. {
  5260. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5261. if (Params.OnProcessedItem != NULL)
  5262. {
  5263. Params.OnProcessedItem(NULL, ChecklistItem);
  5264. }
  5265. }
  5266. }
  5267. //---------------------------------------------------------------------------
  5268. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5269. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5270. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5271. {
  5272. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5273. *Params.StartTime = Now();
  5274. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5275. }
  5276. //---------------------------------------------------------------------------
  5277. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5278. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5279. {
  5280. // terminal can be already closed (e.g. dropped connection)
  5281. if (Terminal != NULL)
  5282. {
  5283. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5284. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5285. }
  5286. }
  5287. //---------------------------------------------------------------------------
  5288. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5289. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5290. {
  5291. DebugAssert(!IsLocalBrowserMode());
  5292. TAutoBatch AutoBatch(this);
  5293. TAutoFlag AutoOperationFlag(FAutoOperation);
  5294. if (Side == osRemote)
  5295. {
  5296. std::unique_ptr<TStrings> FileList(new TStringList());
  5297. FileList->AddObject(FileName, RemoteFile);
  5298. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5299. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5300. RemoteDirView->SaveSelection();
  5301. RemoteDirView->SaveSelectedNames();
  5302. try
  5303. {
  5304. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5305. }
  5306. catch(...)
  5307. {
  5308. RemoteDirView->DiscardSavedSelection();
  5309. throw;
  5310. }
  5311. RemoteDirView->RestoreSelection();
  5312. }
  5313. else if (DebugAlwaysTrue(Side == osLocal))
  5314. {
  5315. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5316. {
  5317. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5318. }
  5319. UnicodeString Directory = ExtractFileDir(FileName);
  5320. ReloadLocalDirectory(Directory);
  5321. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5322. if (!SamePaths(Directory, NewDirectory))
  5323. {
  5324. ReloadLocalDirectory(NewDirectory);
  5325. }
  5326. }
  5327. }
  5328. //---------------------------------------------------------------------------
  5329. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5330. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5331. const TCopyParamType * CopyParams)
  5332. {
  5333. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5334. UnicodeString AdditionalParams =
  5335. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5336. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5337. LocalDirectory, RemoteDirectory, Mode, Params,
  5338. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5339. SerializeCopyParamForCommandLine(CopyParams)));
  5340. ExecuteNewInstance(SessionName, AdditionalParams);
  5341. }
  5342. //---------------------------------------------------------------------------
  5343. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5344. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5345. TSynchronizeMode & Mode, int Params, bool & SaveMode, bool UseDefaults)
  5346. {
  5347. int Result;
  5348. bool SaveSettings = false;
  5349. int Options =
  5350. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5351. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5352. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5353. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5354. bool Continue =
  5355. UseDefaults ||
  5356. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5357. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow);
  5358. if (Continue)
  5359. {
  5360. Configuration->Usage->Inc(L"Synchronizations");
  5361. UpdateCopyParamCounters(CopyParam);
  5362. TSynchronizeOptions SynchronizeOptions;
  5363. GetSynchronizeOptions(Params, SynchronizeOptions);
  5364. if (SaveSettings)
  5365. {
  5366. GUIConfiguration->SynchronizeParams = Params;
  5367. }
  5368. else
  5369. {
  5370. SaveMode = false;
  5371. }
  5372. TDateTime StartTime = Now();
  5373. TSynchronizeChecklist * Checklist = NULL;
  5374. try
  5375. {
  5376. DebugAssert(!FAutoOperation);
  5377. FAutoOperation = true;
  5378. try
  5379. {
  5380. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5381. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5382. int Files = -1;
  5383. if (DataList->Count >= 1)
  5384. {
  5385. Files = StrToIntDef(DataList->Strings[0], Files);
  5386. }
  5387. else
  5388. {
  5389. DataList->Add(UnicodeString());
  5390. }
  5391. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5392. FSynchronizeProgressForm->Start();
  5393. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5394. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5395. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5396. &SynchronizeOptions);
  5397. if (Terminal->SessionData->CacheDirectories)
  5398. {
  5399. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5400. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5401. }
  5402. }
  5403. __finally
  5404. {
  5405. FAutoOperation = false;
  5406. SAFE_DESTROY(FSynchronizeProgressForm);
  5407. }
  5408. TSynchronizeParams SynchronizeParams;
  5409. SynchronizeParams.LocalDirectory = LocalDirectory;
  5410. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5411. SynchronizeParams.Mode = Mode;
  5412. SynchronizeParams.CopyParam = &CopyParam;
  5413. SynchronizeParams.Params = Params;
  5414. SynchronizeParams.Checklist = Checklist;
  5415. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5416. SynchronizeParams.StartTime = &StartTime;
  5417. SynchronizeParams.OnProcessedItem = NULL;
  5418. Result = Checklist->Count;
  5419. if (Checklist->Count > 0)
  5420. {
  5421. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5422. {
  5423. if (!DoSynchronizeChecklistDialog(
  5424. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5425. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, &SynchronizeParams))
  5426. {
  5427. Result = -1;
  5428. }
  5429. }
  5430. else
  5431. {
  5432. FullSynchronize(SynchronizeParams, NULL, NULL);
  5433. }
  5434. }
  5435. else
  5436. {
  5437. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5438. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5439. }
  5440. }
  5441. __finally
  5442. {
  5443. delete Checklist;
  5444. }
  5445. OperationComplete(StartTime);
  5446. }
  5447. else
  5448. {
  5449. Result = -1;
  5450. }
  5451. return Result;
  5452. }
  5453. //---------------------------------------------------------------------------
  5454. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5455. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5456. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5457. {
  5458. if (Collect)
  5459. {
  5460. DebugAssert(FSynchronizeProgressForm != NULL);
  5461. if (!FSynchronizeProgressForm->Started)
  5462. {
  5463. FSynchronizeProgressForm->Start();
  5464. }
  5465. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5466. SetTaskbarListProgressValue(CompareProgress);
  5467. }
  5468. else
  5469. {
  5470. DebugAssert(FSynchronizeProgressForm == NULL);
  5471. DebugAssert(FProgressForm != NULL);
  5472. }
  5473. }
  5474. //---------------------------------------------------------------------------
  5475. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5476. {
  5477. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5478. TRemoteFile * File = NULL;
  5479. Terminal->ReadFile(FullFileName, File);
  5480. if (File != NULL)
  5481. {
  5482. std::unique_ptr<TRemoteFile> FileOwner(File);
  5483. TAutoFlag Flag(FStandaloneEditing);
  5484. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5485. Application->ShowMainForm = false;
  5486. Application->Run();
  5487. }
  5488. }
  5489. //---------------------------------------------------------------------------
  5490. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5491. {
  5492. DebugFail();
  5493. }
  5494. //---------------------------------------------------------------------------
  5495. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5496. {
  5497. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5498. SessionData->Assign(Terminal->SessionData);
  5499. UpdateSessionData(SessionData.get());
  5500. Terminal->UpdateSessionCredentials(SessionData.get());
  5501. if (!Terminal->SessionData->HasSessionName())
  5502. {
  5503. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5504. // once IsWorkspace is cleared
  5505. SessionData->Name = UnicodeString();
  5506. SessionData->IsWorkspace = false;
  5507. }
  5508. return SessionData.release();
  5509. }
  5510. //---------------------------------------------------------------------------
  5511. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5512. {
  5513. TSessionData * SessionData = CloneCurrentSessionData();
  5514. try
  5515. {
  5516. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5517. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5518. }
  5519. __finally
  5520. {
  5521. delete SessionData;
  5522. }
  5523. }
  5524. //---------------------------------------------------------------------------
  5525. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5526. {
  5527. TTerminalManager * Manager = TTerminalManager::Instance();
  5528. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5529. if (DebugAlwaysTrue(Terminal != NULL))
  5530. {
  5531. // Update (Managed)Terminal->StateData
  5532. UpdateTerminal(Terminal);
  5533. }
  5534. Manager->SaveWorkspace(DataList.get());
  5535. return DataList.release();
  5536. }
  5537. //---------------------------------------------------------------------------
  5538. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5539. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5540. {
  5541. WinConfiguration->LastWorkspace = Name;
  5542. if (!SavePasswords)
  5543. {
  5544. for (int Index = 0; Index < DataList->Count; Index++)
  5545. {
  5546. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5547. if (SessionData->Link.IsEmpty())
  5548. {
  5549. SessionData->ClearSessionPasswords();
  5550. }
  5551. }
  5552. }
  5553. StoredSessions->NewWorkspace(Name, DataList);
  5554. // modified only
  5555. StoredSessions->Save(false, Explicit);
  5556. }
  5557. //---------------------------------------------------------------------------
  5558. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5559. {
  5560. return
  5561. DefaultStr(WinConfiguration->LastWorkspace,
  5562. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5563. }
  5564. //---------------------------------------------------------------------------
  5565. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5566. {
  5567. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5568. bool AnyNonStoredSessionWithPassword = false;
  5569. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5570. bool AllNonStoredSessionsAnonymous = true;
  5571. TTerminalManager * Manager = TTerminalManager::Instance();
  5572. for (int Index = 0; Index < DataList->Count; Index++)
  5573. {
  5574. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5575. if (SessionData->Link.IsEmpty())
  5576. {
  5577. if (SessionData->HasAnySessionPassword())
  5578. {
  5579. AnyNonStoredSessionWithPassword = true;
  5580. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5581. {
  5582. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5583. }
  5584. }
  5585. if (!SameText(SessionData->UserName, AnonymousUserName))
  5586. {
  5587. AllNonStoredSessionsAnonymous = false;
  5588. }
  5589. }
  5590. }
  5591. bool SavePasswords;
  5592. bool * PSavePasswords;
  5593. bool NotRecommendedSavingPasswords =
  5594. !CustomWinConfiguration->UseMasterPassword &&
  5595. !AllNonStoredSessionsAnonymous;
  5596. if (Configuration->DisablePasswordStoring ||
  5597. !AnyNonStoredSessionWithPassword)
  5598. {
  5599. PSavePasswords = NULL;
  5600. SavePasswords = false;
  5601. }
  5602. else
  5603. {
  5604. PSavePasswords = &SavePasswords;
  5605. SavePasswords =
  5606. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5607. !NotRecommendedSavingPasswords;
  5608. }
  5609. UnicodeString Name = WorkspaceName();
  5610. bool CreateShortcut = false;
  5611. bool Result =
  5612. DoSaveWorkspaceDialog(
  5613. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5614. EnableAutoSave);
  5615. if (Result)
  5616. {
  5617. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5618. if (CreateShortcut)
  5619. {
  5620. TOperationVisualizer Visualizer;
  5621. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5622. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5623. }
  5624. if (EnableAutoSave)
  5625. {
  5626. WinConfiguration->AutoSaveWorkspace = true;
  5627. WinConfiguration->AutoWorkspace = Name;
  5628. if (PSavePasswords != NULL)
  5629. {
  5630. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5631. }
  5632. }
  5633. }
  5634. return Result;
  5635. }
  5636. //---------------------------------------------------------------------------
  5637. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TManagedTerminal * Terminal)
  5638. {
  5639. DebugAssert(!IsLocalBrowserMode());
  5640. SAFE_DESTROY(Terminal->RemoteExplorerState);
  5641. if (WinConfiguration->PreservePanelState)
  5642. {
  5643. Terminal->RemoteExplorerState = RemoteDirView->SaveState();
  5644. }
  5645. UpdateSessionData(Terminal->StateData);
  5646. }
  5647. //---------------------------------------------------------------------------
  5648. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5649. {
  5650. // Keep in sync with TSessionData::CopyStateData
  5651. DebugAssert(!IsLocalBrowserMode());
  5652. DebugAssert(Data != NULL);
  5653. // This is inconsistent with how color (for example) is handled.
  5654. Data->Name = Terminal->SessionData->Name;
  5655. // cannot use RemoteDirView->Path, because it is empty if connection
  5656. // was already closed
  5657. // also only peek, we may not be connected at all atm
  5658. // so make sure we do not try retrieving current directory from the server
  5659. // (particularly with FTP)
  5660. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5661. if (!ACurrentDirectory.IsEmpty())
  5662. {
  5663. Data->RemoteDirectory = ACurrentDirectory;
  5664. }
  5665. Data->Color = SessionColor;
  5666. }
  5667. //---------------------------------------------------------------------------
  5668. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5669. {
  5670. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5671. DebugAssert(Toolbar != NULL);
  5672. for (int i = 0; i < Toolbar->Items->Count; i++)
  5673. {
  5674. TTBXCustomDropDownItem * DropDownItem;
  5675. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5676. if (DropDownItem != NULL)
  5677. {
  5678. ToolbarItemResize(DropDownItem,
  5679. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5680. Toolbar->NonClientWidth);
  5681. break;
  5682. }
  5683. }
  5684. }
  5685. //---------------------------------------------------------------------------
  5686. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5687. {
  5688. Item->EditWidth = Width;
  5689. }
  5690. //---------------------------------------------------------------------------
  5691. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5692. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5693. {
  5694. for (int i = 0; i < Toolbar->Items->Count; i++)
  5695. {
  5696. TTBXCustomDropDownItem * DropDownItem;
  5697. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5698. if (DropDownItem != NULL)
  5699. {
  5700. ASize.x -= DropDownItem->EditWidth;
  5701. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5702. }
  5703. }
  5704. }
  5705. //---------------------------------------------------------------------------
  5706. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5707. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5708. {
  5709. if (WinConfiguration->DDWarnLackOfTempSpace)
  5710. {
  5711. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5712. if (!ADrive.IsEmpty())
  5713. {
  5714. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5715. DebugAssert(RequiredSpace >= 0);
  5716. __int64 RequiredWithReserve;
  5717. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5718. if (FreeSpace < RequiredWithReserve)
  5719. {
  5720. unsigned int Result;
  5721. TMessageParams Params(mpNeverAskAgainCheck);
  5722. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5723. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5724. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5725. if (Result == qaNeverAskAgain)
  5726. {
  5727. WinConfiguration->DDWarnLackOfTempSpace = false;
  5728. }
  5729. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5730. }
  5731. }
  5732. }
  5733. }
  5734. //---------------------------------------------------------------------------
  5735. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5736. {
  5737. DoOpenDirectoryDialog(odAddBookmark, Side);
  5738. }
  5739. //---------------------------------------------------------------------------
  5740. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5741. {
  5742. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5743. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  5744. try
  5745. {
  5746. TCustomDirView * DView = DirView(Side);
  5747. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5748. {
  5749. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5750. }
  5751. }
  5752. catch (...)
  5753. {
  5754. delete VisitedDirectories;
  5755. throw;
  5756. }
  5757. return VisitedDirectories;
  5758. }
  5759. //---------------------------------------------------------------------------
  5760. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5761. TOpenDirectoryMode Mode, TOperationSide Side)
  5762. {
  5763. bool Continue = true;
  5764. if (Mode == odAddBookmark)
  5765. {
  5766. TMessageParams Params(mpNeverAskAgainCheck);
  5767. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5768. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5769. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5770. unsigned int Answer =
  5771. MessageDialog(Message,
  5772. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5773. if (Answer == qaNeverAskAgain)
  5774. {
  5775. Continue = true;
  5776. WinConfiguration->UseSharedBookmarks = true;
  5777. }
  5778. else if (Answer == qaOK)
  5779. {
  5780. Continue = true;
  5781. WinConfiguration->UseSharedBookmarks = false;
  5782. }
  5783. else
  5784. {
  5785. Continue = false;
  5786. }
  5787. }
  5788. if (Continue)
  5789. {
  5790. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5791. try
  5792. {
  5793. UnicodeString Name = DirView(Side)->PathName;
  5794. bool Repeat;
  5795. do
  5796. {
  5797. Repeat = false;
  5798. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5799. // do not allow switching to location profiles,
  5800. // if we are not connected
  5801. HasDirView[osLocal] && (Terminal != NULL)))
  5802. {
  5803. TWindowLock Lock(this);
  5804. if (!TryOpenDirectory(Side, Name))
  5805. {
  5806. Repeat = true;
  5807. Mode = odBrowse;
  5808. }
  5809. }
  5810. }
  5811. while (Repeat);
  5812. }
  5813. __finally
  5814. {
  5815. delete VisitedDirectories;
  5816. }
  5817. }
  5818. }
  5819. //---------------------------------------------------------------------------
  5820. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5821. {
  5822. DebugAssert(!FTerminal->CommandSessionOpened);
  5823. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5824. }
  5825. //---------------------------------------------------------------------------
  5826. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5827. {
  5828. bool Result = FTerminal->IsCapable[Capability] ||
  5829. FTerminal->CommandSessionOpened;
  5830. if (!Result)
  5831. {
  5832. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5833. if (!GUIConfiguration->ConfirmCommandSession)
  5834. {
  5835. Result = true;
  5836. }
  5837. else
  5838. {
  5839. TMessageParams Params(mpNeverAskAgainCheck);
  5840. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5841. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5842. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5843. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5844. if (Answer == qaNeverAskAgain)
  5845. {
  5846. GUIConfiguration->ConfirmCommandSession = false;
  5847. Result = true;
  5848. }
  5849. else if (Answer == qaOK)
  5850. {
  5851. Result = true;
  5852. }
  5853. }
  5854. if (Result)
  5855. {
  5856. Result = CommandSessionFallback();
  5857. }
  5858. }
  5859. return Result;
  5860. }
  5861. //---------------------------------------------------------------------------
  5862. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5863. {
  5864. if (EnsureCommandSessionFallback(fcAnyCommand))
  5865. {
  5866. DoConsoleDialog(Terminal, Command);
  5867. }
  5868. }
  5869. //---------------------------------------------------------------------------
  5870. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5871. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5872. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5873. {
  5874. if (!IsSideLocalBrowser(osRemote) &&
  5875. IsFileControl(DropSourceControl, osRemote) &&
  5876. (FDDExtMapFile != NULL))
  5877. {
  5878. Accept = true;
  5879. }
  5880. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5881. DebugAssert(FDDTargetControl != NULL);
  5882. }
  5883. //---------------------------------------------------------------------------
  5884. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5885. _di_IDataObject DataObj, int KeyState,
  5886. const TPoint & Point, int & Effect, bool & Accept)
  5887. {
  5888. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5889. }
  5890. //---------------------------------------------------------------------------
  5891. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5892. _di_IDataObject DataObj, int KeyState,
  5893. const TPoint & Point, int & Effect, bool & Accept)
  5894. {
  5895. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5896. }
  5897. //---------------------------------------------------------------------------
  5898. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5899. TObject * Sender)
  5900. {
  5901. DebugUsedParam(Sender);
  5902. DebugAssert(FDDTargetControl == Sender);
  5903. FDDTargetControl = NULL;
  5904. }
  5905. //---------------------------------------------------------------------------
  5906. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5907. {
  5908. FileControlDDDragLeave(SessionsPageControl);
  5909. }
  5910. //---------------------------------------------------------------------------
  5911. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5912. {
  5913. FileControlDDDragLeave(QueueView3);
  5914. }
  5915. //---------------------------------------------------------------------------
  5916. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5917. {
  5918. DebugAssert(!IsSideLocalBrowser(Side));
  5919. DebugUsedParam(Side);
  5920. bool Edit = false;
  5921. TRemoteFile * File = NULL;
  5922. UnicodeString FileName;
  5923. UnicodeString PointTo;
  5924. bool SymbolicLink = true;
  5925. if (RemoteDirView->ItemFocused)
  5926. {
  5927. DebugAssert(RemoteDirView->ItemFocused->Data);
  5928. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5929. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5930. if (Edit)
  5931. {
  5932. FileName = File->FileName;
  5933. PointTo = File->LinkTo;
  5934. }
  5935. else
  5936. {
  5937. PointTo = File->FileName;
  5938. }
  5939. }
  5940. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5941. Terminal->IsCapable[fcHardLink]))
  5942. {
  5943. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5944. if (Edit)
  5945. {
  5946. DebugAssert(File->FileName == FileName);
  5947. int Params = dfNoRecursive;
  5948. Terminal->ExceptionOnFail = true;
  5949. try
  5950. {
  5951. Terminal->DeleteFile(L"", File, &Params);
  5952. }
  5953. __finally
  5954. {
  5955. Terminal->ExceptionOnFail = false;
  5956. }
  5957. }
  5958. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  5959. }
  5960. }
  5961. //---------------------------------------------------------------------------
  5962. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  5963. {
  5964. return
  5965. (IsSideLocalBrowser(Side) ||
  5966. ((Terminal != NULL) &&
  5967. Terminal->ResolvingSymlinks &&
  5968. Terminal->IsCapable[fcSymbolicLink]));
  5969. }
  5970. //---------------------------------------------------------------------------
  5971. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  5972. {
  5973. return
  5974. !IsSideLocalBrowser(FCurrentSide) &&
  5975. (RemoteDirView->ItemFocused != NULL) &&
  5976. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  5977. Terminal->SessionData->ResolveSymlinks;
  5978. }
  5979. //---------------------------------------------------------------------------
  5980. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  5981. {
  5982. DebugAssert(!WinConfiguration->DisableOpenEdit);
  5983. FForceExecution = true;
  5984. try
  5985. {
  5986. DirView(osCurrent)->ExecuteCurrentFile();
  5987. }
  5988. __finally
  5989. {
  5990. FForceExecution = false;
  5991. }
  5992. }
  5993. //---------------------------------------------------------------------------
  5994. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  5995. {
  5996. TEditorData ExternalEditor;
  5997. ExternalEditor.Editor = edExternal;
  5998. bool Remember = false;
  5999. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6000. {
  6001. if (Remember)
  6002. {
  6003. TEditorList * EditorList = new TEditorList;
  6004. try
  6005. {
  6006. *EditorList = *WinConfiguration->EditorList;
  6007. bool Found = false;
  6008. int i = 0;
  6009. while (!Found && (i < EditorList->Count))
  6010. {
  6011. const TEditorPreferences * Editor = EditorList->Editors[i];
  6012. if ((Editor->Data->Editor == edExternal) &&
  6013. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6014. {
  6015. Found = true;
  6016. }
  6017. i++;
  6018. }
  6019. if (!Found)
  6020. {
  6021. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6022. WinConfiguration->EditorList = EditorList;
  6023. }
  6024. }
  6025. __finally
  6026. {
  6027. delete EditorList;
  6028. }
  6029. }
  6030. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6031. }
  6032. }
  6033. //---------------------------------------------------------------------------
  6034. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6035. {
  6036. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  6037. if (FFileFindTerminal == Sender)
  6038. {
  6039. FFileFindTerminal = NULL;
  6040. HideFileFindDialog();
  6041. }
  6042. if (FClipboardTerminal == Sender)
  6043. {
  6044. ClipboardClear(); // implies ClipboardStop
  6045. }
  6046. }
  6047. //---------------------------------------------------------------------------
  6048. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6049. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6050. {
  6051. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6052. DebugAssert(Terminal != NULL);
  6053. if (Data->Terminal == Terminal)
  6054. {
  6055. Data->Terminal = NULL;
  6056. }
  6057. }
  6058. //---------------------------------------------------------------------------
  6059. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  6060. {
  6061. UpdateControls();
  6062. SessionColor = TColor(0);
  6063. UpdateRemotePathComboBox(false);
  6064. NeedSession(true);
  6065. }
  6066. //---------------------------------------------------------------------------
  6067. void __fastcall TCustomScpExplorerForm::NeedSession(bool ReloadSessions)
  6068. {
  6069. try
  6070. {
  6071. try
  6072. {
  6073. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  6074. }
  6075. __finally
  6076. {
  6077. if (!WinConfiguration->KeepOpenWhenNoSession &&
  6078. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6079. {
  6080. TerminateApplication();
  6081. }
  6082. }
  6083. }
  6084. catch (EAbort &)
  6085. {
  6086. // swallow
  6087. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6088. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6089. }
  6090. }
  6091. //---------------------------------------------------------------------------
  6092. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  6093. {
  6094. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  6095. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6096. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  6097. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6098. try
  6099. {
  6100. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  6101. // Clear the design time unthemed tab
  6102. ((SessionsPageControl->PageCount > 0) &&
  6103. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6104. {
  6105. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6106. }
  6107. for (int Index = 0; Index <= TerminalList->Count; Index++)
  6108. {
  6109. TThemeTabSheet * TabSheet;
  6110. if (Index >= SessionsPageControl->PageCount)
  6111. {
  6112. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6113. TabSheet->PageControl = SessionsPageControl;
  6114. }
  6115. else
  6116. {
  6117. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6118. }
  6119. bool IsSessionTab = (Index < TerminalList->Count);
  6120. if (IsSessionTab)
  6121. {
  6122. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  6123. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6124. UpdateSessionTab(TabSheet);
  6125. }
  6126. else
  6127. {
  6128. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6129. TabSheet->Tag = 0; // not really needed
  6130. TabSheet->Shadowed = false;
  6131. // 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)
  6132. UpdateNewSessionTab();
  6133. }
  6134. TabSheet->ShowCloseButton = IsSessionTab;
  6135. }
  6136. }
  6137. __finally
  6138. {
  6139. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6140. }
  6141. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6142. }
  6143. //---------------------------------------------------------------------------
  6144. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6145. {
  6146. DoTerminalListChanged();
  6147. }
  6148. //---------------------------------------------------------------------------
  6149. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6150. {
  6151. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6152. DebugAssert(TabSheet->ImageIndex == 0);
  6153. TabSheet->Caption =
  6154. WinConfiguration->SelectiveToolbarText ?
  6155. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6156. UnicodeString();
  6157. }
  6158. //---------------------------------------------------------------------------
  6159. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6160. {
  6161. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6162. }
  6163. //---------------------------------------------------------------------------
  6164. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6165. {
  6166. if (DebugAlwaysTrue(TabSheet != NULL))
  6167. {
  6168. TManagedTerminal * ATerminal = GetSessionTabTerminal(TabSheet);
  6169. if (DebugAlwaysTrue(ATerminal != NULL))
  6170. {
  6171. TColor Color = (ATerminal == FTerminal) ? FSessionColor : ATerminal->StateData->Color;
  6172. TabSheet->ImageIndex = AddSessionColor(Color);
  6173. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ATerminal, true);
  6174. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6175. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6176. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6177. {
  6178. ThemeTabSheet->Shadowed = !ATerminal->Active;
  6179. }
  6180. }
  6181. }
  6182. }
  6183. //---------------------------------------------------------------------------
  6184. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6185. {
  6186. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6187. TManagedTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6188. bool Result = (Terminal != NULL);
  6189. if (Result)
  6190. {
  6191. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6192. }
  6193. else
  6194. {
  6195. try
  6196. {
  6197. NewSession(false);
  6198. }
  6199. __finally
  6200. {
  6201. DoTerminalListChanged();
  6202. }
  6203. FSessionsPageControlNewSessionTime = Now();
  6204. }
  6205. return Result;
  6206. }
  6207. //---------------------------------------------------------------------------
  6208. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6209. {
  6210. SessionTabSwitched();
  6211. }
  6212. //---------------------------------------------------------------------------
  6213. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6214. {
  6215. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6216. DebugAssert(TransferList != NULL);
  6217. UnicodeString Name;
  6218. if (TransferList->ItemIndex <= 0)
  6219. {
  6220. Name = L"";
  6221. }
  6222. else
  6223. {
  6224. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6225. }
  6226. if (FCopyParamAutoSelected.IsEmpty())
  6227. {
  6228. // if previous preset was not autoselected, make new preset the "default"
  6229. FCopyParamDefault = Name;
  6230. }
  6231. GUIConfiguration->CopyParamCurrent = Name;
  6232. }
  6233. //---------------------------------------------------------------------------
  6234. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6235. {
  6236. // sanity check
  6237. bool ExistingPreset =
  6238. (FTransferListHoverIndex >= 0) &&
  6239. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6240. DebugAssert(ExistingPreset);
  6241. if (ExistingPreset)
  6242. {
  6243. HDC DC = GetDC(0);
  6244. TCanvas * Canvas = new TCanvas();
  6245. try
  6246. {
  6247. Canvas->Handle = DC;
  6248. Canvas->Font = GetToolbarFont(this);
  6249. UnicodeString Name;
  6250. if (FTransferListHoverIndex == 0)
  6251. {
  6252. Name = L"";
  6253. }
  6254. else
  6255. {
  6256. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6257. }
  6258. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6259. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6260. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6261. static_cast<TObject*>(GetComponent(fcTransferList)));
  6262. UnicodeString InfoStr =
  6263. GUIConfiguration->CopyParamPreset[Name].
  6264. GetInfoStr(L"; ",
  6265. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General));
  6266. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6267. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6268. {
  6269. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6270. {
  6271. InfoStr.SetLength(InfoStr.Length() - 1);
  6272. }
  6273. InfoStr += Ellipsis;
  6274. }
  6275. // UpdateCaption does not cause invalidation of whole submenu, while
  6276. // setting Caption property does.
  6277. // also it probably does not resize the label, even if necessary
  6278. // (we do not want that anyway)
  6279. TransferLabel->UpdateCaption(InfoStr);
  6280. }
  6281. __finally
  6282. {
  6283. Canvas->Handle = NULL;
  6284. ReleaseDC(0, DC);
  6285. delete Canvas;
  6286. }
  6287. }
  6288. }
  6289. //---------------------------------------------------------------------------
  6290. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6291. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6292. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6293. {
  6294. if (FTransferListHoverIndex != AHoverIndex)
  6295. {
  6296. FTransferListHoverIndex = AHoverIndex;
  6297. UpdateTransferLabel();
  6298. }
  6299. }
  6300. //---------------------------------------------------------------------------
  6301. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6302. {
  6303. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6304. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6305. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6306. {
  6307. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6308. {
  6309. OpenDirectory(GetSide(osCurrent));
  6310. Message.Result = 1;
  6311. }
  6312. else
  6313. {
  6314. TForm::Dispatch(&Message);
  6315. }
  6316. }
  6317. else
  6318. {
  6319. TForm::Dispatch(&Message);
  6320. }
  6321. }
  6322. //---------------------------------------------------------------------------
  6323. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6324. {
  6325. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6326. if ((FIgnoreNextDialogChar != 0) &&
  6327. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6328. {
  6329. Message.Result = 1;
  6330. }
  6331. else
  6332. {
  6333. TForm::Dispatch(&Message);
  6334. }
  6335. FIgnoreNextDialogChar = 0;
  6336. }
  6337. //---------------------------------------------------------------------------
  6338. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6339. {
  6340. // The four low-order bits are used internally by Windows
  6341. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6342. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6343. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6344. // 61730 - restore thru double click - undocumented
  6345. // 61490 - maximize thru double click - undocumented
  6346. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6347. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6348. {
  6349. SysResizing(Cmd);
  6350. }
  6351. TForm::Dispatch(&Message);
  6352. }
  6353. //---------------------------------------------------------------------------
  6354. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6355. {
  6356. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6357. // Also there no point returning TRUE as we are not able to
  6358. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6359. // Hence the process termination might be safer :)
  6360. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6361. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6362. {
  6363. Message.Result = FALSE;
  6364. }
  6365. else
  6366. {
  6367. Message.Result = TRUE;
  6368. }
  6369. // Do not call default handling as that triggers OnCloseQuery,
  6370. // where our implementation will popup confirmation dialogs, what we do not want,
  6371. // as per Vista guidelines:
  6372. // https://docs.microsoft.com/en-us/windows/desktop/shutdown/shutting-down
  6373. }
  6374. //---------------------------------------------------------------------------
  6375. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6376. {
  6377. if (Message.EndSession && IsApplicationMinimized())
  6378. {
  6379. // WORKAROUND
  6380. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6381. // when it receives WM_ENDSESSION,
  6382. // but that sometimes (particularly when application is minimized) cause crashes.
  6383. // Crash popups message that blocks log off.
  6384. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6385. // so we call ExitProcess() immediately, not even trying to cleanup.
  6386. // It still causes beep, so there's likely some popup, but it does not block
  6387. // log off.
  6388. ExitProcess(0);
  6389. }
  6390. TForm::Dispatch(&Message);
  6391. }
  6392. #ifdef _DEBUG
  6393. //---------------------------------------------------------------------------
  6394. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6395. {
  6396. TForm::Dispatch(&Message);
  6397. }
  6398. #endif
  6399. //---------------------------------------------------------------------------
  6400. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6401. {
  6402. }
  6403. //---------------------------------------------------------------------------
  6404. void __fastcall TCustomScpExplorerForm::DoShow()
  6405. {
  6406. // only now are the controls resized finally, so the size constraints
  6407. // will not conflict with possibly very small window size
  6408. RestoreFormParams();
  6409. // This indicates that the form was rescaled after parameters were loaded and
  6410. // as most probably the parameters were actually saved with the current form scaling
  6411. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6412. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6413. // and no rounding problems will occur.
  6414. // See also RestoreForm.
  6415. // (we should be safe to call RestoreParams unconditionally)
  6416. if (PixelsPerInch != Screen->PixelsPerInch)
  6417. {
  6418. RestoreParams();
  6419. }
  6420. FixControlsPlacement();
  6421. if (Position == poDefaultPosOnly)
  6422. {
  6423. CutFormToDesktop(this);
  6424. }
  6425. TForm::DoShow();
  6426. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6427. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6428. if (Terminal == NULL)
  6429. {
  6430. StartingDisconnected();
  6431. }
  6432. UpdatePixelsPerInchMainWindowCounter();
  6433. FShowing = true;
  6434. }
  6435. //---------------------------------------------------------------------------
  6436. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6437. {
  6438. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6439. }
  6440. //---------------------------------------------------------------------------
  6441. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6442. {
  6443. DoTerminalListChanged();
  6444. }
  6445. //---------------------------------------------------------------------------
  6446. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6447. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6448. TNotifyEvent OnBalloonClick, TObject * UserData)
  6449. {
  6450. bool Do;
  6451. UnicodeString Message;
  6452. if (E == NULL)
  6453. {
  6454. Message = Str;
  6455. Do = true;
  6456. }
  6457. else
  6458. {
  6459. Do = ExceptionMessage(E, Message);
  6460. }
  6461. if (Do && WinConfiguration->BalloonNotifications)
  6462. {
  6463. UnicodeString Title;
  6464. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6465. {
  6466. Message = TrimLeft(Message);
  6467. }
  6468. else
  6469. {
  6470. Message = UnformatMessage(Message);
  6471. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6472. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6473. Title = LoadResourceString(Captions[Type]);
  6474. if (Terminal != NULL)
  6475. {
  6476. Title = FORMAT(L"%s - %s",
  6477. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6478. }
  6479. }
  6480. if (Seconds == 0)
  6481. {
  6482. Seconds = WinConfiguration->NotificationsTimeout;
  6483. }
  6484. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6485. }
  6486. }
  6487. //---------------------------------------------------------------------------
  6488. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6489. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6490. UnicodeString HelpKeyword, const TMessageParams * Params,
  6491. TTerminal * Terminal)
  6492. {
  6493. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6494. (FOnFeedSynchronizeError != NULL)) &&
  6495. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6496. {
  6497. if (FOnFeedSynchronizeError != NULL)
  6498. {
  6499. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6500. }
  6501. else
  6502. {
  6503. DebugAssert(FErrorList != NULL);
  6504. TStringList * MoreMessagesCopy = NULL;
  6505. if (MoreMessages)
  6506. {
  6507. MoreMessagesCopy = new TStringList();
  6508. MoreMessagesCopy->Assign(MoreMessages);
  6509. }
  6510. FErrorList->AddObject(Message, MoreMessagesCopy);
  6511. }
  6512. PopupTrayBalloon(Terminal, Message, Type);
  6513. return ContinueAnswer(Answers);
  6514. }
  6515. else
  6516. {
  6517. bool UseBalloon = FTrayIcon->Visible;
  6518. if (UseBalloon)
  6519. {
  6520. PopupTrayBalloon(Terminal, Message, Type);
  6521. }
  6522. unsigned int Result;
  6523. try
  6524. {
  6525. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6526. }
  6527. __finally
  6528. {
  6529. // cancel only balloon we popped up, otherwise we may cancel notification
  6530. // balloon that was there before the message dialog
  6531. // (such as "dd confirmation opt in balloon)
  6532. if (UseBalloon)
  6533. {
  6534. FTrayIcon->CancelBalloon();
  6535. }
  6536. }
  6537. return Result;
  6538. }
  6539. }
  6540. //---------------------------------------------------------------------------
  6541. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6542. TTerminal * Terminal, Exception * E)
  6543. {
  6544. if (FTrayIcon->Visible)
  6545. {
  6546. PopupTrayBalloon(Terminal, L"", qtError, E);
  6547. }
  6548. // particularly prevent opening new session from jump list,
  6549. // while exception is shown
  6550. NonVisualDataModule->StartBusy();
  6551. try
  6552. {
  6553. ShowExtendedExceptionEx(Terminal, E);
  6554. }
  6555. __finally
  6556. {
  6557. NonVisualDataModule->EndBusy();
  6558. FTrayIcon->CancelBalloon();
  6559. }
  6560. }
  6561. //---------------------------------------------------------------------------
  6562. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6563. {
  6564. if (Terminal->Active)
  6565. {
  6566. InitStatusBar();
  6567. }
  6568. // cannot rely on active page being page for active terminal,
  6569. // as it can happen that active page is the "new session" page
  6570. // (e.g. when reconnecting active terminal, while login dialog
  6571. // invoked from "new session" page is modal)
  6572. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6573. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6574. }
  6575. //---------------------------------------------------------------------------
  6576. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6577. TTerminal * Terminal, Exception * E)
  6578. {
  6579. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6580. if (!Terminal->Active)
  6581. {
  6582. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6583. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6584. {
  6585. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6586. }
  6587. }
  6588. }
  6589. //---------------------------------------------------------------------------
  6590. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6591. UnicodeString Message, TQueryType Type,
  6592. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6593. {
  6594. if ((E == NULL) ||
  6595. ExceptionMessage(E, Message))
  6596. {
  6597. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6598. if (Important)
  6599. {
  6600. Seconds *= 5;
  6601. }
  6602. UnicodeString NoteMessage(UnformatMessage(Message));
  6603. if (Terminal != NULL)
  6604. {
  6605. NoteMessage = FORMAT(L"%s: %s",
  6606. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6607. }
  6608. if (WinConfiguration->BalloonNotifications)
  6609. {
  6610. AddNote(NoteMessage);
  6611. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6612. }
  6613. else
  6614. {
  6615. FlashOnBackground();
  6616. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6617. }
  6618. }
  6619. }
  6620. //---------------------------------------------------------------------------
  6621. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6622. {
  6623. RestoreApp();
  6624. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6625. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6626. !NonVisualDataModule->Busy)
  6627. {
  6628. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6629. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6630. if (!ComponentVisible[fcQueueView])
  6631. {
  6632. ToggleQueueVisibility();
  6633. GoToQueue();
  6634. }
  6635. }
  6636. }
  6637. //---------------------------------------------------------------------------
  6638. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6639. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6640. {
  6641. UnicodeString Message;
  6642. TNotifyEvent OnClick = NULL;
  6643. TObject * UserData = NULL;
  6644. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6645. switch (Event)
  6646. {
  6647. case qeEmptyButMonitored:
  6648. if ((ATerminal != Terminal) || QueueInvisible)
  6649. {
  6650. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6651. OnClick = QueueEmptyNoteClicked;
  6652. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6653. TerminalNoteData->Terminal = ATerminal;
  6654. UserData = TerminalNoteData;
  6655. }
  6656. break;
  6657. case qeEmpty:
  6658. OperationComplete(ATerminal->QueueOperationStart);
  6659. break;
  6660. case qePendingUserAction:
  6661. if ((ATerminal != Terminal) ||
  6662. (QueueInvisible && !IsQueueAutoPopup()))
  6663. {
  6664. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6665. }
  6666. break;
  6667. default:
  6668. DebugFail();
  6669. }
  6670. if (!Message.IsEmpty())
  6671. {
  6672. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6673. }
  6674. }
  6675. //---------------------------------------------------------------------------
  6676. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6677. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6678. {
  6679. if (FDDExtMapFile != NULL)
  6680. {
  6681. CloseHandle(FDDExtMapFile);
  6682. FDDExtMapFile = NULL;
  6683. }
  6684. if (WinConfiguration->DDFakeFile)
  6685. {
  6686. DDFakeFileInitDrag(FileList, Created);
  6687. }
  6688. }
  6689. //---------------------------------------------------------------------------
  6690. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6691. {
  6692. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6693. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6694. {
  6695. FFakeFileDropTarget = FileName;
  6696. }
  6697. }
  6698. //---------------------------------------------------------------------------
  6699. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6700. {
  6701. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6702. if (!ForceDirectories(ApiPath(Result)))
  6703. {
  6704. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6705. }
  6706. FileSetAttr(ApiPath(Result), faHidden);
  6707. return Result;
  6708. }
  6709. //---------------------------------------------------------------------------
  6710. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6711. bool & Created)
  6712. {
  6713. FFakeFileDropTarget = UnicodeString();
  6714. FDragFakeDirectory = CreateFakeTransferDirectory();
  6715. FileList->AddItem(NULL, FDragFakeDirectory);
  6716. Created = true;
  6717. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6718. {
  6719. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6720. }
  6721. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6722. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6723. {
  6724. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6725. TDragExtCommStruct* CommStruct;
  6726. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6727. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6728. DebugAssert(CommStruct != NULL);
  6729. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6730. CommStruct->Dragging = true;
  6731. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6732. LENOF(CommStruct->DropDest));
  6733. NULL_TERMINATE(CommStruct->DropDest);
  6734. UnmapViewOfFile(CommStruct);
  6735. }
  6736. }
  6737. //---------------------------------------------------------------------------
  6738. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6739. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6740. {
  6741. bool Result;
  6742. DebugAssert(!IsLocalBrowserMode());
  6743. if (Sender == RemoteDirView)
  6744. {
  6745. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6746. }
  6747. else
  6748. {
  6749. DebugAssert(Sender == RemoteDriveView);
  6750. TStrings * FileList = RemoteDriveView->DragFileList();
  6751. try
  6752. {
  6753. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6754. }
  6755. __finally
  6756. {
  6757. delete FileList;
  6758. }
  6759. }
  6760. if (FDDTargetControl == RemoteDriveView)
  6761. {
  6762. RemoteDriveView->UpdateDropTarget();
  6763. }
  6764. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6765. // or from tree view to dir view.
  6766. // foMove happens when file/dir is dragged from remote tree view outside of
  6767. // application via dragex
  6768. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6769. (DropSourceControl == RemoteDriveView))
  6770. {
  6771. RemoteDriveView->UpdateDropSource();
  6772. }
  6773. return Result;
  6774. }
  6775. //---------------------------------------------------------------------------
  6776. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6777. {
  6778. DebugAssert(!IsLocalBrowserMode());
  6779. // This also handles drops of remote files to queue.
  6780. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6781. SAFE_DESTROY(FDDFileList);
  6782. if (!FFakeFileDropTarget.IsEmpty())
  6783. {
  6784. RemoveDir(ApiPath(FFakeFileDropTarget));
  6785. }
  6786. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6787. {
  6788. try
  6789. {
  6790. TDragResult DDResult = (Sender == RemoteDirView) ?
  6791. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6792. // Focus is moved to the target application,
  6793. // but as we are going to present the UI, we need to steal the focus back.
  6794. // This most likely won't work though (windows does not allow application
  6795. // to steal focus most of the time)
  6796. Application->BringToFront();
  6797. // On older version of Windows we never got drMove here, see also comment below.
  6798. // On Windows 10, we get the "move".
  6799. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6800. {
  6801. UnicodeString TargetDirectory;
  6802. TFileOperation Operation;
  6803. // drInvalid may mean drMove, see comment below
  6804. switch (DDResult)
  6805. {
  6806. case drCopy:
  6807. Operation = foCopy;
  6808. break;
  6809. case drMove:
  6810. Operation = foMove;
  6811. break;
  6812. default:
  6813. DebugFail();
  6814. case drInvalid:
  6815. // prefer "copy" for safety
  6816. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6817. break;
  6818. }
  6819. TTransferOperationParam Param;
  6820. UnicodeString CounterName;
  6821. bool ForceQueue;
  6822. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6823. {
  6824. // we get drInvalid both if d&d was intercepted by ddext,
  6825. // and when users drops on no-drop location.
  6826. // we tell the difference by existence of response from ddext,
  6827. // so we ignore absence of response in this case
  6828. if (DDResult != drInvalid)
  6829. {
  6830. // here we know that the extension is installed,
  6831. // as it is checked as soon as drag&drop starts from
  6832. // RemoteFileControlDDCreateDragFileList
  6833. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6834. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  6835. {
  6836. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  6837. }
  6838. else
  6839. {
  6840. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6841. }
  6842. }
  6843. }
  6844. else
  6845. {
  6846. // download using fake file
  6847. Param.Temp = false;
  6848. Param.DragDrop = true;
  6849. if (ForceQueue)
  6850. {
  6851. Param.Queue = asOn;
  6852. }
  6853. if (Sender == RemoteDirView)
  6854. {
  6855. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6856. }
  6857. if (RemoteFileControlFileOperation(Sender, Operation,
  6858. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6859. {
  6860. Configuration->Usage->Inc(CounterName);
  6861. }
  6862. }
  6863. }
  6864. }
  6865. __finally
  6866. {
  6867. CloseHandle(FDDExtMapFile);
  6868. FDDExtMapFile = NULL;
  6869. delete FDragFakeMonitors;
  6870. FDragFakeMonitors = NULL;
  6871. RemoveDir(ApiPath(FDragFakeDirectory));
  6872. FDragFakeDirectory = L"";
  6873. FFakeFileDropTarget = UnicodeString();
  6874. }
  6875. }
  6876. if (FDragDropSshTerminate.get() != NULL)
  6877. {
  6878. FDragDropSshTerminate->Rethrow();
  6879. }
  6880. }
  6881. //---------------------------------------------------------------------------
  6882. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6883. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  6884. {
  6885. // Remember drop effect so we know (when user drops files), if we copy or move
  6886. FLastDropEffect = dwEffect;
  6887. }
  6888. //---------------------------------------------------------------------------
  6889. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6890. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6891. {
  6892. bool Result;
  6893. if (FDDTargetControl == QueueView3)
  6894. {
  6895. Directory = DefaultDownloadTargetDirectory();
  6896. Result = true;
  6897. CounterName = L"DownloadsDragDropQueue";
  6898. ForceQueue = true;
  6899. }
  6900. else if (!FFakeFileDropTarget.IsEmpty())
  6901. {
  6902. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6903. Result = true;
  6904. ForceQueue = false;
  6905. CounterName = L"DownloadsDragDropFakeFile";
  6906. }
  6907. else
  6908. {
  6909. ForceQueue = false;
  6910. Enabled = false;
  6911. try
  6912. {
  6913. int Timer = 0;
  6914. Result = false;
  6915. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6916. {
  6917. {
  6918. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6919. TDragExtCommStruct* CommStruct;
  6920. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6921. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6922. DebugAssert(CommStruct != NULL);
  6923. Result = !CommStruct->Dragging;
  6924. if (Result)
  6925. {
  6926. Directory = ExtractFilePath(CommStruct->DropDest);
  6927. CounterName = L"DownloadsDragDropExternalExt";
  6928. }
  6929. UnmapViewOfFile(CommStruct);
  6930. }
  6931. if (!Result)
  6932. {
  6933. Sleep(50);
  6934. Timer += 50;
  6935. Application->ProcessMessages();
  6936. }
  6937. }
  6938. }
  6939. __finally
  6940. {
  6941. Enabled = true;
  6942. }
  6943. }
  6944. return Result;
  6945. }
  6946. //---------------------------------------------------------------------------
  6947. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  6948. const UnicodeString TempDir, int SecDelay)
  6949. {
  6950. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  6951. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  6952. if (FDelayedDeletionTimer == NULL)
  6953. {
  6954. DebugAssert(HandleAllocated());
  6955. FDelayedDeletionTimer = new TTimer(this);
  6956. FDelayedDeletionTimer->Interval = 10000;
  6957. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  6958. }
  6959. else
  6960. {
  6961. FDelayedDeletionTimer->Enabled = true;
  6962. }
  6963. }
  6964. //---------------------------------------------------------------------------
  6965. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  6966. {
  6967. DebugAssert(FDelayedDeletionList != NULL);
  6968. TDateTime N = Now();
  6969. TDateTime Alert;
  6970. UnicodeString Directory;
  6971. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  6972. {
  6973. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  6974. if ((N >= Alert) || (Sender == NULL))
  6975. {
  6976. Directory = FDelayedDeletionList->Strings[Index];
  6977. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  6978. {
  6979. FDelayedDeletionList->Delete(Index);
  6980. }
  6981. }
  6982. }
  6983. if (FDelayedDeletionList->Count == 0)
  6984. {
  6985. FDelayedDeletionTimer->Enabled = false;
  6986. }
  6987. }
  6988. //---------------------------------------------------------------------------
  6989. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  6990. {
  6991. DebugAssert(!IsLocalBrowserMode());
  6992. if (IsFileControl(FDDTargetControl, osRemote) ||
  6993. (FDDTargetControl == SessionsPageControl))
  6994. {
  6995. TTerminal * TargetTerminal = NULL;
  6996. TFileOperation Operation = foNone;
  6997. if (FDDTargetControl == SessionsPageControl)
  6998. {
  6999. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7000. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7001. // do not allow dropping on the "+" tab
  7002. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  7003. if (TargetTerminal != NULL)
  7004. {
  7005. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7006. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7007. {
  7008. Operation = foRemoteMove;
  7009. }
  7010. else
  7011. {
  7012. Operation = foRemoteCopy;
  7013. }
  7014. }
  7015. }
  7016. else
  7017. {
  7018. if (FLastDropEffect == DROPEFFECT_MOVE)
  7019. {
  7020. Operation = foRemoteMove;
  7021. }
  7022. else if (FLastDropEffect == DROPEFFECT_COPY)
  7023. {
  7024. Operation = foRemoteCopy;
  7025. }
  7026. }
  7027. if (Operation != foNone)
  7028. {
  7029. RemoteFileControlFileOperation(DropSourceControl,
  7030. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7031. }
  7032. // abort drag&drop
  7033. Abort();
  7034. }
  7035. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7036. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7037. (FDDTargetControl != QueueView3))
  7038. {
  7039. DebugAssert(!FDragTempDir.IsEmpty());
  7040. TTransferType Type;
  7041. UnicodeString TempDir = FDragTempDir;
  7042. // We clear FDragTempDir before calling
  7043. // just in case it fail (raises exception)
  7044. FDragTempDir = L"";
  7045. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7046. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7047. // empty directory parameter means temp directory -> don't display it!
  7048. UnicodeString TargetDir = L"";
  7049. int Options = 0;
  7050. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7051. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7052. {
  7053. Abort();
  7054. }
  7055. // TargetDir is set when dropped on local file control
  7056. // (this was workaround for legacy dirview event handling, now it should be
  7057. // made prettier)
  7058. if (TargetDir.IsEmpty())
  7059. {
  7060. TargetDir = TempDir;
  7061. if (ForceDirectories(ApiPath(TargetDir)))
  7062. {
  7063. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7064. FPendingTempSpaceWarn = true;
  7065. try
  7066. {
  7067. FDragDropOperation = true;
  7068. int Params = cpTemporary |
  7069. (Type == ttMove ? cpDelete : 0);
  7070. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7071. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7072. }
  7073. __finally
  7074. {
  7075. FDragDropOperation = false;
  7076. FPendingTempSpaceWarn = false;
  7077. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7078. }
  7079. }
  7080. else
  7081. {
  7082. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7083. }
  7084. }
  7085. }
  7086. }
  7087. //---------------------------------------------------------------------------
  7088. void __fastcall TCustomScpExplorerForm::DDDownload(
  7089. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7090. {
  7091. DebugAssert(!IsLocalBrowserMode());
  7092. TAutoBatch AutoBatch(this);
  7093. UpdateCopyParamCounters(*CopyParam);
  7094. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7095. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7096. {
  7097. RemoteDriveView->UpdateDropSource();
  7098. }
  7099. }
  7100. //---------------------------------------------------------------------------
  7101. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7102. {
  7103. public:
  7104. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7105. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7106. {
  7107. }
  7108. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7109. {
  7110. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7111. TDataObjectFilesEx::AllowData(FormatEtc);
  7112. }
  7113. };
  7114. //---------------------------------------------------------------------------
  7115. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7116. TObject * Sender, TDataObject *& DataObject)
  7117. {
  7118. if (FDDExtMapFile != NULL)
  7119. {
  7120. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7121. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7122. {
  7123. Abort();
  7124. }
  7125. if (FileList->Count > 0)
  7126. {
  7127. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7128. if (!FilesObject->IsValid(true, true))
  7129. {
  7130. FilesObject->_Release();
  7131. }
  7132. else
  7133. {
  7134. DataObject = FilesObject;
  7135. }
  7136. }
  7137. }
  7138. }
  7139. //---------------------------------------------------------------------------
  7140. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7141. {
  7142. DebugFail();
  7143. }
  7144. //---------------------------------------------------------------------------
  7145. void __fastcall TCustomScpExplorerForm::GoToTree()
  7146. {
  7147. ComponentVisible[fcRemoteTree] = true;
  7148. DriveView(osRemote)->SetFocus();
  7149. }
  7150. //---------------------------------------------------------------------------
  7151. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7152. TPanelExport Export)
  7153. {
  7154. TCustomDirView * DirView = this->DirView(Side);
  7155. std::unique_ptr<TStrings> ExportData(new TStringList());
  7156. switch (Export)
  7157. {
  7158. case pePath:
  7159. ExportData->Add(DirView->PathName);
  7160. break;
  7161. case peFileList:
  7162. case peFullFileList:
  7163. {
  7164. bool FullPath = (Export == peFullFileList);
  7165. DirView->CreateFileList(false, FullPath, ExportData.get());
  7166. for (int Index = 0; Index < ExportData->Count; Index++)
  7167. {
  7168. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7169. {
  7170. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7171. }
  7172. }
  7173. }
  7174. break;
  7175. default:
  7176. DebugFail();
  7177. }
  7178. return ExportData.release();
  7179. }
  7180. //---------------------------------------------------------------------------
  7181. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7182. TPanelExport Export, TPanelExportDestination Destination)
  7183. {
  7184. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7185. PanelExportStore(Side, Export, Destination, ExportData.get());
  7186. }
  7187. //---------------------------------------------------------------------------
  7188. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7189. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7190. TStrings * ExportData)
  7191. {
  7192. if (Destination == pedClipboard)
  7193. {
  7194. TInstantOperationVisualizer Visualizer;
  7195. CopyToClipboard(ExportData);
  7196. }
  7197. else
  7198. {
  7199. DebugFail();
  7200. }
  7201. }
  7202. //---------------------------------------------------------------------------
  7203. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7204. {
  7205. TCustomDirView * DirView = this->DirView(Side);
  7206. UnicodeString Mask = DirView->Mask;
  7207. if (DoFilterMaskDialog(DirView, Mask))
  7208. {
  7209. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7210. Configuration->Usage->Inc(L"Filters");
  7211. }
  7212. }
  7213. //---------------------------------------------------------------------------
  7214. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7215. {
  7216. return FQueueController->DefaultOperation();
  7217. }
  7218. //---------------------------------------------------------------------------
  7219. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7220. TQueueOperation Operation, void ** Param)
  7221. {
  7222. switch (Operation)
  7223. {
  7224. case qoPreferences:
  7225. return true;
  7226. case qoGoTo:
  7227. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7228. case qoOnceEmpty:
  7229. return !FQueueController->Empty;
  7230. default:
  7231. return FQueueController->AllowOperation(Operation, Param);
  7232. }
  7233. }
  7234. //---------------------------------------------------------------------------
  7235. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7236. {
  7237. if (DebugAlwaysTrue(QueueView3->Visible))
  7238. {
  7239. QueueView3->SetFocus();
  7240. }
  7241. }
  7242. //---------------------------------------------------------------------------
  7243. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7244. TQueueOperation Operation, void * Param)
  7245. {
  7246. if (Operation == qoGoTo)
  7247. {
  7248. GoToQueue();
  7249. }
  7250. else if (Operation == qoPreferences)
  7251. {
  7252. PreferencesDialog(pmQueue);
  7253. }
  7254. else
  7255. {
  7256. FQueueController->ExecuteOperation(Operation, Param);
  7257. }
  7258. }
  7259. //---------------------------------------------------------------------------
  7260. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7261. {
  7262. return (Queue != NULL) && Queue->Enabled;
  7263. }
  7264. //---------------------------------------------------------------------------
  7265. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7266. {
  7267. DebugAssert(Queue != NULL);
  7268. if (Queue != NULL)
  7269. {
  7270. Queue->Enabled = !Queue->Enabled;
  7271. }
  7272. }
  7273. //---------------------------------------------------------------------------
  7274. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7275. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7276. {
  7277. FQueueActedItem = QueueView3->ItemFocused;
  7278. }
  7279. //---------------------------------------------------------------------------
  7280. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7281. {
  7282. return TopDock->Height + QueueSplitter->Height;
  7283. }
  7284. //---------------------------------------------------------------------------
  7285. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7286. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7287. {
  7288. // when queue is hidden by double-clicking splitter, stray attempt to
  7289. // resize the panel with strange value arrives, make sure it is ignored
  7290. if (ComponentVisible[fcQueueView])
  7291. {
  7292. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7293. RemotePanel->Constraints->MinHeight;
  7294. if (NewSize > HeightLimit)
  7295. {
  7296. NewSize = HeightLimit;
  7297. }
  7298. }
  7299. else
  7300. {
  7301. Accept = false;
  7302. }
  7303. }
  7304. //---------------------------------------------------------------------------
  7305. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7306. TDragObject *& /*DragObject*/)
  7307. {
  7308. FQueueActedItem = QueueView3->ItemFocused;
  7309. }
  7310. //---------------------------------------------------------------------------
  7311. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7312. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7313. {
  7314. Accept = true;
  7315. if (Source == QueueView3)
  7316. {
  7317. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7318. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7319. if (Accept)
  7320. {
  7321. TQueueItemProxy * QueueItem;
  7322. TQueueItemProxy * DestQueueItem;
  7323. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7324. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7325. Accept = (QueueItem != DestQueueItem) &&
  7326. (QueueItem->Status == TQueueItem::qsPending) &&
  7327. (DestQueueItem->Status == TQueueItem::qsPending);
  7328. }
  7329. }
  7330. }
  7331. //---------------------------------------------------------------------------
  7332. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7333. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7334. {
  7335. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7336. {
  7337. TQueueItemProxy * QueueItem;
  7338. TQueueItemProxy * DestQueueItem;
  7339. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7340. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7341. QueueItem->Move(DestQueueItem);
  7342. }
  7343. }
  7344. //---------------------------------------------------------------------------
  7345. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7346. {
  7347. if ((QueueView3->ItemFocused == NULL) &&
  7348. (QueueView3->Items->Count > 0))
  7349. {
  7350. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7351. }
  7352. QueueLabelUpdateStatus();
  7353. }
  7354. //---------------------------------------------------------------------------
  7355. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7356. {
  7357. QueueLabelUpdateStatus();
  7358. }
  7359. //---------------------------------------------------------------------------
  7360. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7361. {
  7362. QueueLabel->UpdateStatus();
  7363. }
  7364. //---------------------------------------------------------------------------
  7365. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7366. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7367. {
  7368. if (Selected)
  7369. {
  7370. NonVisualDataModule->UpdateNonVisibleActions();
  7371. }
  7372. }
  7373. //---------------------------------------------------------------------------
  7374. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7375. {
  7376. TDragDropFilesEx * Result = NULL;
  7377. if (Sender == SessionsPageControl)
  7378. {
  7379. Result = FSessionsDragDropFilesEx;
  7380. }
  7381. else if (Sender == QueueView3)
  7382. {
  7383. Result = FQueueDragDropFilesEx;
  7384. }
  7385. else
  7386. {
  7387. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7388. if (DirView != NULL)
  7389. {
  7390. Result = DirView->DragDropFilesEx;
  7391. }
  7392. else
  7393. {
  7394. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7395. if (DriveView != NULL)
  7396. {
  7397. Result = DriveView->DragDropFilesEx;
  7398. }
  7399. }
  7400. }
  7401. DebugAssert(Result != NULL);
  7402. return Result;
  7403. }
  7404. //---------------------------------------------------------------------------
  7405. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7406. {
  7407. return (DView->SelCount == DView->FilesCount);
  7408. }
  7409. //---------------------------------------------------------------------------
  7410. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7411. {
  7412. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7413. }
  7414. //---------------------------------------------------------------------------
  7415. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7416. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7417. {
  7418. DebugAssert(!IsLocalBrowserMode());
  7419. TFileOperation Operation;
  7420. switch (Effect)
  7421. {
  7422. case DROPEFFECT_MOVE:
  7423. Operation = foMove;
  7424. break;
  7425. case DROPEFFECT_COPY:
  7426. // occures on WinXP (reported by user)
  7427. default:
  7428. Operation = foCopy;
  7429. break;
  7430. };
  7431. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7432. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7433. if (DragDropFilesEx->FileList->Count > 0)
  7434. {
  7435. TStrings * FileList = new TStringList();
  7436. try
  7437. {
  7438. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7439. {
  7440. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7441. }
  7442. FDragDropOperation = true;
  7443. TTransferOperationParam Param;
  7444. Param.TargetDirectory = TargetPath;
  7445. // upload, no temp dirs
  7446. Param.Temp = false;
  7447. Param.DragDrop = DragDrop;
  7448. Param.Options =
  7449. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7450. if (ForceQueue)
  7451. {
  7452. Param.Queue = asOn;
  7453. }
  7454. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7455. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7456. {
  7457. if (IsFileControl(DropSourceControl, osLocal))
  7458. {
  7459. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7460. }
  7461. else
  7462. {
  7463. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7464. }
  7465. }
  7466. }
  7467. __finally
  7468. {
  7469. FDragDropOperation = false;
  7470. delete FileList;
  7471. }
  7472. }
  7473. }
  7474. //---------------------------------------------------------------------------
  7475. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7476. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7477. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7478. {
  7479. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7480. }
  7481. //---------------------------------------------------------------------------
  7482. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7483. TObject * Sender, int grfKeyState, int & dwEffect)
  7484. {
  7485. DebugAssert(!IsLocalBrowserMode());
  7486. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7487. if ((dwEffect != DROPEFFECT_NONE) &&
  7488. IsFileControl(DropSourceControl, osRemote))
  7489. {
  7490. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7491. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7492. (RemoteDirView->DropTarget == NULL))
  7493. {
  7494. dwEffect = DROPEFFECT_NONE;
  7495. }
  7496. else
  7497. {
  7498. if (dwEffect == DROPEFFECT_COPY)
  7499. {
  7500. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7501. // currently we support copying always (at least via temporary directory);
  7502. // remove associated checks once this all proves stable and working
  7503. bool CopyCapable = true;
  7504. // if we do not support neither of operations, there's no discussion
  7505. if (!MoveCapable && !CopyCapable)
  7506. {
  7507. dwEffect = DROPEFFECT_NONE;
  7508. }
  7509. else
  7510. {
  7511. // The default effect inside remote panel is move,
  7512. // unless we do not support it, but support copy
  7513. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7514. {
  7515. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7516. }
  7517. else
  7518. {
  7519. // with ctrl-down, we want copy unless it is not supported
  7520. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7521. }
  7522. }
  7523. }
  7524. }
  7525. }
  7526. }
  7527. //---------------------------------------------------------------------------
  7528. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7529. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7530. {
  7531. DebugAssert(!IsLocalBrowserMode());
  7532. if (FDDTotalSize >= 0)
  7533. {
  7534. if (File->IsDirectory)
  7535. {
  7536. FDDTotalSize = -1;
  7537. }
  7538. else
  7539. {
  7540. FDDTotalSize += File->Size;
  7541. }
  7542. }
  7543. DebugAssert(!FDragTempDir.IsEmpty());
  7544. // TODO: this is quite ineffective
  7545. // TODO: what if invalid character replacement is disabled?
  7546. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7547. UnicodeString TransferFileName;
  7548. if (Sender == RemoteDriveView)
  7549. {
  7550. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7551. }
  7552. else
  7553. {
  7554. TransferFileName = File->FileName;
  7555. }
  7556. FDDFileList->AddObject(TransferFileName, File);
  7557. }
  7558. //---------------------------------------------------------------------------
  7559. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7560. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7561. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7562. {
  7563. // sometimes we do not get DDEnd so the list is not released
  7564. SAFE_DESTROY(FDDFileList);
  7565. FDDFileList = new TStringList();
  7566. FDragTempDir = WinConfiguration->TemporaryDir();
  7567. FDDTotalSize = 0;
  7568. FDragDropSshTerminate.reset(NULL);
  7569. }
  7570. //---------------------------------------------------------------------------
  7571. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7572. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7573. HRESULT & Result)
  7574. {
  7575. if (Result == DRAGDROP_S_DROP)
  7576. {
  7577. try
  7578. {
  7579. GlobalDragImageList->HideDragImage();
  7580. try
  7581. {
  7582. RemoteFileControlDDTargetDrop();
  7583. }
  7584. catch(ESshTerminate & E)
  7585. {
  7586. FDragDropSshTerminate.reset(E.Clone());
  7587. }
  7588. }
  7589. catch (Exception &E)
  7590. {
  7591. // If downloading fails we need to cancel drag&drop, otherwise
  7592. // Explorer shows error
  7593. // But by the way exception probably never reach this point as
  7594. // it's catched on way.
  7595. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7596. Result = DRAGDROP_S_CANCEL;
  7597. ShowExtendedException(Terminal, &E);
  7598. }
  7599. }
  7600. }
  7601. //---------------------------------------------------------------------------
  7602. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7603. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7604. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7605. {
  7606. TFileMasks::TParams MaskParams;
  7607. MaskParams.Size = Size;
  7608. MaskParams.Modification = Modification;
  7609. // this does not re-parse the mask if it is the same as the last time
  7610. FDirViewMatchMask = Masks;
  7611. bool ImplicitMatch;
  7612. Matches =
  7613. // RecurseInclude parameter has no effect as the Path is empty
  7614. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7615. (AllowImplicitMatches || !ImplicitMatch);
  7616. }
  7617. //---------------------------------------------------------------------------
  7618. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7619. TObject * Sender, TListItem * Item, WORD & Indexes)
  7620. {
  7621. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7622. UnicodeString Ext;
  7623. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7624. {
  7625. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7626. }
  7627. else
  7628. {
  7629. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7630. }
  7631. if (SameText(Ext, Configuration->PartialExt))
  7632. {
  7633. Indexes |= oiPartial;
  7634. }
  7635. }
  7636. //---------------------------------------------------------------------------
  7637. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7638. {
  7639. return
  7640. DirViewEnabled(osCurrent) &&
  7641. DirView(osCurrent)->CanPasteFromClipBoard();
  7642. }
  7643. //---------------------------------------------------------------------------
  7644. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7645. {
  7646. bool Result = false;
  7647. if (CanPasteToDirViewFromClipBoard())
  7648. {
  7649. Result = true;
  7650. }
  7651. else
  7652. {
  7653. UnicodeString ClipboardText;
  7654. if (NonEmptyTextFromClipboard(ClipboardText))
  7655. {
  7656. if (StoredSessions->IsUrl(ClipboardText))
  7657. {
  7658. Result = true;
  7659. }
  7660. else
  7661. {
  7662. Result = DirViewEnabled(osCurrent);
  7663. }
  7664. }
  7665. }
  7666. return Result;
  7667. }
  7668. //---------------------------------------------------------------------------
  7669. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7670. {
  7671. if (DoesClipboardContainOurFiles())
  7672. {
  7673. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7674. {
  7675. DebugAssert(!IsLocalBrowserMode());
  7676. TTerminalManager * Manager = TTerminalManager::Instance();
  7677. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7678. Manager->ActiveTerminal = FClipboardTerminal;
  7679. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7680. }
  7681. }
  7682. else if (CanPasteToDirViewFromClipBoard())
  7683. {
  7684. DirView(osCurrent)->PasteFromClipBoard();
  7685. }
  7686. else
  7687. {
  7688. UnicodeString ClipboardText;
  7689. if (NonEmptyTextFromClipboard(ClipboardText))
  7690. {
  7691. if (StoredSessions->IsUrl(ClipboardText))
  7692. {
  7693. NewSession(false, ClipboardText);
  7694. }
  7695. else
  7696. {
  7697. DirView(osCurrent)->Path = ClipboardText;
  7698. }
  7699. }
  7700. }
  7701. }
  7702. //---------------------------------------------------------------------------
  7703. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7704. {
  7705. // TBD
  7706. }
  7707. //---------------------------------------------------------------------------
  7708. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7709. {
  7710. TCustomDirView * ADirView = DirView(Side);
  7711. ADirView->SelectAll(Mode);
  7712. ADirView->SetFocus();
  7713. }
  7714. //---------------------------------------------------------------------------
  7715. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7716. {
  7717. TCustomDirView * ADirView = DirView(Side);
  7718. TFileFilter Filter;
  7719. DefaultFileFilter(Filter);
  7720. if (DoSelectMaskDialog(ADirView, Select, Filter))
  7721. {
  7722. Configuration->Usage->Inc(L"MaskSelections");
  7723. ADirView->SelectFiles(Filter, Select);
  7724. ADirView->SetFocus();
  7725. }
  7726. }
  7727. //---------------------------------------------------------------------------
  7728. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7729. {
  7730. TCustomDirView * ADirView = DirView(Side);
  7731. ADirView->RestoreSelectedNames();
  7732. ADirView->SetFocus();
  7733. }
  7734. //---------------------------------------------------------------------------
  7735. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7736. {
  7737. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7738. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7739. {
  7740. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7741. UnicodeString Ext;
  7742. if (!IsSideLocalBrowser(osCurrent))
  7743. {
  7744. Ext = UnixExtractFileExt(FileName);
  7745. }
  7746. else
  7747. {
  7748. Ext = ExtractFileExt(FileName);
  7749. }
  7750. if (Ext.IsEmpty())
  7751. {
  7752. Ext = L".";
  7753. }
  7754. TFileFilter Filter;
  7755. Filter.Masks = FORMAT(L"*%s", (Ext));
  7756. Filter.Directories = false;
  7757. CurrentDirView->SelectFiles(Filter, Select);
  7758. }
  7759. }
  7760. //---------------------------------------------------------------------------
  7761. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7762. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7763. {
  7764. UnicodeString Result;
  7765. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  7766. {
  7767. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  7768. }
  7769. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  7770. {
  7771. // noop
  7772. }
  7773. else
  7774. {
  7775. Result =
  7776. FMTLOAD(FILE_INFO_FORMAT,
  7777. (FormatBytes(FileInfo.SelectedSize),
  7778. FormatBytes(FileInfo.FilesSize),
  7779. FormatNumber(FileInfo.SelectedCount),
  7780. FormatNumber(FileInfo.FilesCount)));
  7781. }
  7782. return Result;
  7783. }
  7784. //---------------------------------------------------------------------------
  7785. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7786. TTBXStatusPanel * Panel, TOperationSide Side)
  7787. {
  7788. if (Panel->Index == 1)
  7789. {
  7790. ToggleShowHiddenFiles();
  7791. }
  7792. else if (Panel->Index == 2)
  7793. {
  7794. Filter(Side);
  7795. }
  7796. }
  7797. //---------------------------------------------------------------------------
  7798. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7799. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7800. {
  7801. DebugAssert(!StatusBar->SimplePanel);
  7802. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7803. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7804. }
  7805. //---------------------------------------------------------------------------
  7806. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7807. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7808. {
  7809. DebugAssert(StatusBar->Panels->Count >= 3);
  7810. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7811. if (FileInfo.HiddenCount > 0)
  7812. {
  7813. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7814. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7815. }
  7816. else
  7817. {
  7818. HiddenFilesPanel->ViewPriority = 0;
  7819. // not really necessary, just to cleanup no-longer-valid data
  7820. HiddenFilesPanel->Caption = L"";
  7821. }
  7822. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7823. if (FileInfo.FilteredCount > 0)
  7824. {
  7825. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7826. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7827. }
  7828. else
  7829. {
  7830. FilteredFilesPanel->ViewPriority = 0;
  7831. // not really necessary, just to cleanup no-longer-valid data
  7832. FilteredFilesPanel->Caption = L"";
  7833. }
  7834. }
  7835. //---------------------------------------------------------------------------
  7836. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7837. TObject * /*Sender*/)
  7838. {
  7839. if (DirView(osRemote)->Enabled)
  7840. {
  7841. DirView(osRemote)->SetFocus();
  7842. }
  7843. }
  7844. //---------------------------------------------------------------------------
  7845. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7846. {
  7847. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7848. switch (Config.Show)
  7849. {
  7850. case qvShow:
  7851. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7852. {
  7853. Config.Show = qvHide;
  7854. }
  7855. else
  7856. {
  7857. Config.Show = Config.LastHideShow;
  7858. }
  7859. break;
  7860. case qvHideWhenEmpty:
  7861. if (ComponentVisible[fcQueueView])
  7862. {
  7863. Config.Show = qvHide;
  7864. }
  7865. else
  7866. {
  7867. Config.LastHideShow = Config.Show;
  7868. Config.Show = qvShow;
  7869. }
  7870. break;
  7871. case qvHide:
  7872. Config.LastHideShow = Config.Show;
  7873. Config.Show = qvShow;
  7874. break;
  7875. }
  7876. WinConfiguration->QueueView = Config;
  7877. }
  7878. //---------------------------------------------------------------------------
  7879. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7880. {
  7881. UnicodeString Result;
  7882. if (FTerminal != NULL)
  7883. {
  7884. switch (WinConfiguration->PathInCaption)
  7885. {
  7886. case picShort:
  7887. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7888. break;
  7889. case picFull:
  7890. Result = FTerminal->CurrentDirectory;
  7891. break;
  7892. }
  7893. }
  7894. return Result;
  7895. }
  7896. //---------------------------------------------------------------------------
  7897. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  7898. {
  7899. TColor Result = GetWindowColor(FSessionColor);
  7900. return Result;
  7901. }
  7902. //---------------------------------------------------------------------------
  7903. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  7904. {
  7905. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  7906. return Result;
  7907. }
  7908. //---------------------------------------------------------------------------
  7909. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7910. {
  7911. TTerminalManager::Instance()->UpdateAppTitle();
  7912. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7913. // background to gray on Windows 7 (works on Windows 10).
  7914. // See also EnableControl
  7915. if (Showing)
  7916. {
  7917. if (Terminal != NULL)
  7918. {
  7919. // Update path when it changes
  7920. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7921. {
  7922. UpdateSessionTab(SessionsPageControl->ActivePage);
  7923. }
  7924. }
  7925. NonVisualDataModule->ReconnectSessionAction->Update();
  7926. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  7927. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  7928. CenterReconnectToolbar();
  7929. bool HasTerminal = (Terminal != NULL) && Terminal->Active;
  7930. if (HasTerminal)
  7931. {
  7932. // TODO needed yet with second local browser?
  7933. if (!RemoteDirView->Enabled)
  7934. {
  7935. RemoteDirView->Enabled = true;
  7936. if (FRemoteDirViewWasFocused)
  7937. {
  7938. ActiveControl = RemoteDirView;
  7939. }
  7940. }
  7941. RemoteDirView->Color = PanelColor();
  7942. }
  7943. else
  7944. {
  7945. if (RemoteDirView->Enabled)
  7946. {
  7947. // This is first called when the form is being constructed
  7948. // (not anymore due to Showing test above)
  7949. // but the false is overriden in the constructor later.
  7950. // An even later in TScpCommanderForm::DoShow()
  7951. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  7952. RemoteDirView->Enabled = false;
  7953. RemoteDirView->Color = DisabledPanelColor();
  7954. }
  7955. }
  7956. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  7957. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  7958. RemoteDriveView->Color = RemoteDirView->Color;
  7959. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  7960. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fsBackgroundTransfers];
  7961. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  7962. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  7963. QueueLabelUpdateStatus();
  7964. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  7965. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  7966. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  7967. }
  7968. }
  7969. //---------------------------------------------------------------------------
  7970. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  7971. {
  7972. UpdateControls();
  7973. }
  7974. //---------------------------------------------------------------------------
  7975. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  7976. TObject * Sender)
  7977. {
  7978. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7979. DebugAssert(DirView != NULL);
  7980. DoDirViewLoaded(DirView);
  7981. TransferPresetAutoSelect();
  7982. }
  7983. //---------------------------------------------------------------------------
  7984. void __fastcall TCustomScpExplorerForm::StartUpdates()
  7985. {
  7986. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  7987. // first run after installation
  7988. if (double(Updates.LastCheck) == 0)
  7989. {
  7990. // make sure next time there will be an update (if enabled)
  7991. Updates.LastCheck = TDateTime(1);
  7992. WinConfiguration->Updates = Updates;
  7993. }
  7994. else if ((double(Updates.Period) > 0) &&
  7995. (Now() - Updates.LastCheck >= Updates.Period))
  7996. {
  7997. TThreadMethod OnUpdatesChecked = NULL;
  7998. if (!IsUWP())
  7999. {
  8000. OnUpdatesChecked = UpdatesChecked;
  8001. }
  8002. StartUpdateThread(OnUpdatesChecked);
  8003. }
  8004. }
  8005. //---------------------------------------------------------------------------
  8006. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8007. {
  8008. UnicodeString Message;
  8009. bool New;
  8010. TQueryType Type;
  8011. GetUpdatesMessage(Message, New, Type, false);
  8012. if (!Message.IsEmpty())
  8013. {
  8014. if (New)
  8015. {
  8016. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8017. }
  8018. if (!New && (Type != qtWarning))
  8019. {
  8020. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8021. }
  8022. else
  8023. {
  8024. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8025. }
  8026. if (New)
  8027. {
  8028. Configuration->Usage->Inc(L"UpdateNotifications");
  8029. }
  8030. }
  8031. }
  8032. //---------------------------------------------------------------------------
  8033. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8034. {
  8035. RestoreApp();
  8036. bool CanDisplay = !NonVisualDataModule->Busy;
  8037. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8038. {
  8039. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8040. }
  8041. if (CanDisplay)
  8042. {
  8043. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8044. CheckForUpdates(true);
  8045. }
  8046. else
  8047. {
  8048. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8049. }
  8050. }
  8051. //---------------------------------------------------------------------------
  8052. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8053. TCopyParamRuleData & Data)
  8054. {
  8055. DebugAssert(Terminal != NULL);
  8056. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8057. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8058. Data.RemoteDirectory = RemoteDirView->PathName;
  8059. }
  8060. //---------------------------------------------------------------------------
  8061. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8062. {
  8063. // Terminal can be null when we are changing local directory implicitly,
  8064. // because it has been deleted, while no session is connected
  8065. // (Login dialog is open)
  8066. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8067. {
  8068. DebugAssert(!IsLocalBrowserMode());
  8069. TCopyParamRuleData Data;
  8070. GetTransferPresetAutoSelectData(Data);
  8071. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8072. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8073. if (CopyParamIndex < 0)
  8074. {
  8075. // there is no preset that matches autoselection
  8076. // set preset that we consider "default"
  8077. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8078. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8079. }
  8080. else
  8081. {
  8082. // there is preset matching autoselection
  8083. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8084. if (CopyParamName == FCopyParamAutoSelected)
  8085. {
  8086. // autoselected the same preset as the last time
  8087. // make no change (i.e. preserve custom user preset, if any)
  8088. }
  8089. else
  8090. {
  8091. // autoselected the different preset then the last time (or there
  8092. // was default preset set)
  8093. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8094. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8095. }
  8096. }
  8097. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8098. {
  8099. if (CopyParamIndex >= 0)
  8100. {
  8101. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8102. }
  8103. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8104. try
  8105. {
  8106. int Fmt =
  8107. (CopyParamIndex < 0) ?
  8108. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8109. COPY_PARAM_AUTOSELECTED;
  8110. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8111. Data->Message = MainInstructions(Message);
  8112. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8113. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8114. CopyParamAttrs);
  8115. if (CopyParamIndex >= 0)
  8116. {
  8117. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8118. Info = FORMAT(L"%s\n \n%s", (Info,
  8119. FMTLOAD(COPY_PARAM_RULE,
  8120. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8121. }
  8122. Data->Message += L"\n\n" + Info;
  8123. if (WinConfiguration->CopyParamAutoSelectNotice)
  8124. {
  8125. TransferPresetNoteMessage(Data, true);
  8126. }
  8127. else
  8128. {
  8129. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8130. Data = NULL; // ownership passed
  8131. }
  8132. }
  8133. __finally
  8134. {
  8135. delete Data;
  8136. }
  8137. }
  8138. }
  8139. }
  8140. //---------------------------------------------------------------------------
  8141. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8142. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8143. {
  8144. DebugAssert(NoteData != NULL);
  8145. DebugAssert(!IsLocalBrowserMode());
  8146. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8147. TQueryButtonAlias Aliases[1];
  8148. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8149. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8150. Params.Aliases = Aliases;
  8151. Params.AliasesCount = LENOF(Aliases);
  8152. unsigned int Result =
  8153. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8154. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8155. switch (Result)
  8156. {
  8157. case qaNeverAskAgain:
  8158. DebugAssert(AllowNeverAskAgain);
  8159. WinConfiguration->CopyParamAutoSelectNotice = false;
  8160. break;
  8161. case qaIgnore:
  8162. PreferencesDialog(pmPresets);
  8163. break;
  8164. }
  8165. }
  8166. //---------------------------------------------------------------------------
  8167. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8168. {
  8169. // as of now useless, as this is used for notes only, never for balloons, ...
  8170. RestoreApp();
  8171. // .. and we should never be busy here
  8172. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8173. {
  8174. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8175. }
  8176. }
  8177. //---------------------------------------------------------------------------
  8178. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8179. TPreferencesMode APreferencesMode)
  8180. {
  8181. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8182. TCopyParamRuleData Data;
  8183. if (Terminal != NULL)
  8184. {
  8185. PreferencesData.reset(new TPreferencesDialogData());
  8186. GetTransferPresetAutoSelectData(Data);
  8187. PreferencesData->CopyParamRuleData = &Data;
  8188. }
  8189. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8190. }
  8191. //---------------------------------------------------------------------------
  8192. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8193. const TCustomCommandType & Command)
  8194. {
  8195. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8196. {
  8197. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  8198. }
  8199. }
  8200. //---------------------------------------------------------------------------
  8201. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8202. {
  8203. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8204. TCustomCommandType Command;
  8205. // make sure we use local custom command when remote are not supported
  8206. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8207. {
  8208. Command = FLastCustomCommand;
  8209. }
  8210. else
  8211. {
  8212. Command.Params = Command.Params | ccLocal;
  8213. }
  8214. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8215. FEditingFocusedAdHocCommand = OnFocused;
  8216. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8217. int Options =
  8218. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8219. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8220. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8221. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8222. {
  8223. FLastCustomCommand = Command;
  8224. UpdateCustomCommandsToolbar();
  8225. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8226. }
  8227. }
  8228. //---------------------------------------------------------------------------
  8229. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8230. {
  8231. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8232. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8233. DebugAssert(State > 0);
  8234. if (State <= 0)
  8235. {
  8236. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8237. }
  8238. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8239. }
  8240. //---------------------------------------------------------------------------
  8241. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8242. TCustomCommandType & Command, int & State)
  8243. {
  8244. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8245. if (Result)
  8246. {
  8247. Command = FLastCustomCommand;
  8248. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8249. }
  8250. return Result;
  8251. }
  8252. //---------------------------------------------------------------------------
  8253. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8254. {
  8255. ResetIncrementalSearch();
  8256. }
  8257. //---------------------------------------------------------------------------
  8258. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8259. {
  8260. DebugAssert(ComponentVisible[Component]);
  8261. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8262. }
  8263. //---------------------------------------------------------------------------
  8264. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8265. {
  8266. // when queue panel is resized here directly, the status bar is stretched
  8267. // over whole space the panel occupied
  8268. PostComponentHide(fcQueueView);
  8269. }
  8270. //---------------------------------------------------------------------------
  8271. void __fastcall TCustomScpExplorerForm::WMWinIniChange(TMessage & Message)
  8272. {
  8273. WinConfiguration->ResetSysDarkTheme();
  8274. ConfigurationChanged();
  8275. ConfigureInterface();
  8276. TForm::Dispatch(&Message);
  8277. }
  8278. //---------------------------------------------------------------------------
  8279. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8280. {
  8281. TMessage * M = static_cast<TMessage*>(Message);
  8282. switch (M->Msg)
  8283. {
  8284. case CM_DIALOGCHAR:
  8285. CMDialogChar(*M);
  8286. break;
  8287. case WM_APPCOMMAND:
  8288. WMAppCommand(*M);
  8289. break;
  8290. case WM_SYSCOMMAND:
  8291. WMSysCommand(*M);
  8292. break;
  8293. case WM_QUERYENDSESSION:
  8294. WMQueryEndSession(*M);
  8295. break;
  8296. case WM_ENDSESSION:
  8297. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8298. break;
  8299. #ifdef _DEBUG
  8300. case WM_WINDOWPOSCHANGED:
  8301. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8302. break;
  8303. case CM_CUSTOMSTYLECHANGED:
  8304. TForm::Dispatch(Message);
  8305. break;
  8306. #endif
  8307. case WM_COMPONENT_HIDE:
  8308. {
  8309. Byte Component = static_cast<Byte>(M->WParam);
  8310. // sanity check
  8311. if (ComponentVisible[Component])
  8312. {
  8313. // special treatment
  8314. if (Component == fcQueueView)
  8315. {
  8316. ToggleQueueVisibility();
  8317. DebugAssert(!ComponentVisible[fcQueueView]);
  8318. }
  8319. else
  8320. {
  8321. ComponentVisible[Component] = false;
  8322. }
  8323. }
  8324. }
  8325. break;
  8326. case WM_COPYDATA:
  8327. WMCopyData(*M);
  8328. break;
  8329. case WM_MANAGES_CAPTION:
  8330. // caption managed in TTerminalManager::UpdateAppTitle()
  8331. M->Result = 1;
  8332. break;
  8333. case WM_WANTS_MOUSEWHEEL:
  8334. M->Result = 1;
  8335. break;
  8336. case CM_SHOWINGCHANGED:
  8337. CMShowingChanged(*M);
  8338. break;
  8339. case WM_CLOSE:
  8340. WMClose(*M);
  8341. break;
  8342. case WM_DPICHANGED:
  8343. WMDpiChanged(*M);
  8344. break;
  8345. case CM_DPICHANGED:
  8346. CMDpiChanged(*M);
  8347. break;
  8348. case WM_WININICHANGE:
  8349. WMWinIniChange(*M);
  8350. break;
  8351. case CM_DIALOGKEY:
  8352. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8353. break;
  8354. default:
  8355. TForm::Dispatch(Message);
  8356. break;
  8357. }
  8358. }
  8359. //---------------------------------------------------------------------------
  8360. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8361. {
  8362. // noop
  8363. }
  8364. //---------------------------------------------------------------------------
  8365. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8366. {
  8367. TForm::Dispatch(&Message);
  8368. FSessionColors->Clear();
  8369. AddFixedSessionImages();
  8370. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8371. UpdateImages();
  8372. }
  8373. //---------------------------------------------------------------------------
  8374. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8375. {
  8376. TForm::Dispatch(&Message);
  8377. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8378. UpdatePixelsPerInchMainWindowCounter();
  8379. }
  8380. //---------------------------------------------------------------------------
  8381. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8382. {
  8383. // Cannot close window while we are busy.
  8384. // We cannot test this in FormCloseQuery as that is called also from
  8385. // Close(), which is called by CloseApplicationAction. So we can be busy
  8386. // there already even, when it is legitimate to close the application.
  8387. // Possibly a better place to handle this would be WMSysCommand.
  8388. if (NonVisualDataModule->Busy)
  8389. {
  8390. Message.Result = 1;
  8391. }
  8392. else
  8393. {
  8394. TForm::Dispatch(&Message);
  8395. }
  8396. }
  8397. //---------------------------------------------------------------------------
  8398. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8399. {
  8400. TForm::Dispatch(&Message);
  8401. if (Showing && (Terminal == NULL))
  8402. {
  8403. // When we are starting minimized (i.e. from an installer),
  8404. // postpone showing Login dialog until we get restored.
  8405. // Otherwise the Login dialog (and Authentication window) show restored
  8406. // over invidible (minimized) main window.
  8407. if (WindowState == wsMinimized)
  8408. {
  8409. FNeedSession = true;
  8410. }
  8411. else
  8412. {
  8413. // This happens before application ever goes idle, so the toolbars would
  8414. // stay enabled (initial state) until the Login dialog is dismissed.
  8415. UpdateActions();
  8416. NonVisualDataModule->StartBusy();
  8417. try
  8418. {
  8419. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8420. // otherwise the dialog does not receive focus.
  8421. // With Commander interface the ProcessMessages is called already
  8422. // by TDriveView, but with Explorer interface, we need to call it explicily
  8423. Application->ProcessMessages();
  8424. // do not reload sessions, they have been loaded just now (optimization)
  8425. NeedSession(false);
  8426. }
  8427. __finally
  8428. {
  8429. NonVisualDataModule->EndBusy();
  8430. }
  8431. }
  8432. }
  8433. }
  8434. //---------------------------------------------------------------------------
  8435. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8436. {
  8437. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8438. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8439. }
  8440. //---------------------------------------------------------------------------
  8441. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8442. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8443. int & MaxHeight)
  8444. {
  8445. // workaround for bug in TWinControl.CalcConstraints
  8446. // Check for empty rect (restore from iconic state) is done there only after
  8447. // call to AdjustClientRect, which enlarges the rect (for forms).
  8448. TRect R = GetClientRect();
  8449. // when restoring from iconic state, place no restrictions
  8450. if (IsRectEmpty(R))
  8451. {
  8452. MinWidth = 0;
  8453. MinHeight = 0;
  8454. MaxWidth = 32000;
  8455. MaxHeight = 32000;
  8456. }
  8457. }
  8458. //---------------------------------------------------------------------------
  8459. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8460. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8461. {
  8462. // terminal can be already closed (e.g. dropped connection)
  8463. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8464. {
  8465. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8466. try
  8467. {
  8468. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8469. }
  8470. catch(...)
  8471. {
  8472. if (!Terminal->Active)
  8473. {
  8474. Close = true;
  8475. }
  8476. throw;
  8477. }
  8478. }
  8479. }
  8480. //---------------------------------------------------------------------------
  8481. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8482. {
  8483. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8484. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8485. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8486. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8487. {
  8488. OnGetSpaceAvailable = GetSpaceAvailable;
  8489. }
  8490. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8491. OnGetSpaceAvailable);
  8492. }
  8493. //---------------------------------------------------------------------------
  8494. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8495. {
  8496. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8497. Terminal->FillSessionDataForCode(Data.get());
  8498. return Data.release();
  8499. }
  8500. //---------------------------------------------------------------------------
  8501. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8502. {
  8503. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8504. DoGenerateUrlDialog(Data.get(), Paths);
  8505. }
  8506. //---------------------------------------------------------------------------
  8507. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8508. {
  8509. GenerateUrl(NULL);
  8510. }
  8511. //---------------------------------------------------------------------------
  8512. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8513. {
  8514. std::unique_ptr<TStrings> Paths(new TStringList());
  8515. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8516. GenerateUrl(Paths.get());
  8517. }
  8518. //---------------------------------------------------------------------------
  8519. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8520. {
  8521. FSessionColor = value;
  8522. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8523. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8524. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8525. DebugAssert(ColorItem != NULL);
  8526. ColorItem->Color = C;
  8527. NonVisualDataModule->ColorMenuItem->Color = C;
  8528. UpdateControls();
  8529. }
  8530. //---------------------------------------------------------------------------
  8531. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8532. {
  8533. if (value != FSessionColor)
  8534. {
  8535. UpdateSessionColor(value);
  8536. // Is null when called from LastTerminalClosed
  8537. if (Terminal != NULL)
  8538. {
  8539. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8540. }
  8541. }
  8542. }
  8543. //---------------------------------------------------------------------------
  8544. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8545. {
  8546. bool Result = FNoteTimer->Enabled;
  8547. if (Result)
  8548. {
  8549. // cannot cancel note too early
  8550. bool NotEarly =
  8551. (Now() - FNoteShown >
  8552. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8553. if (Force || NotEarly)
  8554. {
  8555. FNoteTimer->Enabled = false;
  8556. FNote = L"";
  8557. SAFE_DESTROY(FNoteData);
  8558. FOnNoteClick = NULL;
  8559. FNoteHints = FNotes->Text;
  8560. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8561. UpdateStatusBar();
  8562. }
  8563. }
  8564. return Result;
  8565. }
  8566. //---------------------------------------------------------------------------
  8567. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8568. {
  8569. DebugAssert(FNoteTimer->Enabled);
  8570. CancelNote(true);
  8571. }
  8572. //---------------------------------------------------------------------------
  8573. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8574. {
  8575. int P = Note.Pos(L"\n");
  8576. if (P > 0)
  8577. {
  8578. Note.SetLength(P - 1);
  8579. }
  8580. FNotes->Add(FORMAT(L"[%s] %s",
  8581. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8582. while (FNotes->Count > 10)
  8583. {
  8584. FNotes->Delete(0);
  8585. }
  8586. if (UpdateNow)
  8587. {
  8588. FNoteHints = FNotes->Text;
  8589. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8590. UpdateStatusBar();
  8591. }
  8592. }
  8593. //---------------------------------------------------------------------------
  8594. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8595. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8596. {
  8597. int P = Note.Pos(L"\n");
  8598. if (P > 0)
  8599. {
  8600. Note.SetLength(P - 1);
  8601. }
  8602. FNoteHints = FNotes->Text;
  8603. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8604. FNote = Note;
  8605. SAFE_DESTROY(FNoteData);
  8606. FNoteData = NoteData;
  8607. FOnNoteClick = OnNoteClick;
  8608. AddNote(Note, false);
  8609. UpdateStatusBar();
  8610. FNoteShown = Now();
  8611. FNoteTimer->Enabled = false;
  8612. if (Seconds == 0)
  8613. {
  8614. Seconds = WinConfiguration->NotificationsTimeout;
  8615. }
  8616. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8617. FNoteTimer->Enabled = true;
  8618. }
  8619. //---------------------------------------------------------------------------
  8620. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8621. {
  8622. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8623. }
  8624. //---------------------------------------------------------------------------
  8625. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8626. {
  8627. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8628. {
  8629. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8630. }
  8631. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8632. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8633. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8634. 0, NULL, NULL);
  8635. }
  8636. //---------------------------------------------------------------------------
  8637. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8638. {
  8639. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8640. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8641. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8642. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8643. }
  8644. //---------------------------------------------------------------------------
  8645. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8646. {
  8647. WinConfiguration->FormatSizeBytes = Style;
  8648. }
  8649. //---------------------------------------------------------------------------
  8650. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8651. {
  8652. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8653. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8654. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8655. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8656. }
  8657. //---------------------------------------------------------------------------
  8658. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8659. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8660. {
  8661. if (Panel->Index == 0)
  8662. {
  8663. if (FOnNoteClick != NULL)
  8664. {
  8665. // prevent the user data from being freed by possible call
  8666. // to CancelNote or PostNote during call to OnNoteClick
  8667. std::unique_ptr<TObject> NoteData(FNoteData);
  8668. TNotifyEvent OnNoteClick = FOnNoteClick;
  8669. FNoteData = NULL;
  8670. // need to cancel the note as we are going to delete its user data
  8671. CancelNote(true);
  8672. OnNoteClick(NoteData.get());
  8673. }
  8674. }
  8675. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8676. {
  8677. FileSystemInfo();
  8678. }
  8679. }
  8680. //---------------------------------------------------------------------------
  8681. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8682. {
  8683. // workaround:
  8684. // 1) for unknown reason, disabling window, while minimized,
  8685. // prevents it from restoring, even if it was enabled again meanwhile
  8686. // 2) when disabling the main window, while another has focus
  8687. // minimize is no longer possible
  8688. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8689. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8690. // Shouldn't we use IsApplicationMinimized() here?
  8691. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8692. {
  8693. Enabled = false;
  8694. }
  8695. FLockLevel++;
  8696. }
  8697. //---------------------------------------------------------------------------
  8698. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8699. {
  8700. DebugAssert(FLockLevel > 0);
  8701. FLockLevel--;
  8702. if (FLockLevel == 0)
  8703. {
  8704. DebugAssert(FLockSuspendLevel == 0);
  8705. Enabled = true;
  8706. }
  8707. }
  8708. //---------------------------------------------------------------------------
  8709. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8710. {
  8711. // We need to make sure that window is enabled when the last modal window closes
  8712. // otherwise focus is not restored correctly.
  8713. // So we re-enable the window when modal window opens and
  8714. // disable it back after is closes
  8715. if (FLockLevel > 0)
  8716. {
  8717. // while we have nesting counter, we know that we never be called
  8718. // recursivelly as Application->OnModalBegin is called only
  8719. // for the top-level modal window
  8720. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8721. {
  8722. // won't be disabled when conditions in LockWindow() were not satisfied
  8723. FDisabledOnLockSuspend = !Enabled;
  8724. // When minimized to tray (or actually when set to SW_HIDE),
  8725. // setting Enabled makes the window focusable even when there's
  8726. // modal window over it
  8727. if (!FTrayIcon->Visible)
  8728. {
  8729. Enabled = true;
  8730. }
  8731. }
  8732. FLockSuspendLevel++;
  8733. }
  8734. }
  8735. //---------------------------------------------------------------------------
  8736. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8737. {
  8738. if (FLockSuspendLevel > 0)
  8739. {
  8740. DebugAssert(FLockLevel > 0);
  8741. FLockSuspendLevel--;
  8742. // see comment in SuspendWindowLock
  8743. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8744. {
  8745. // Note that window can be enabled here, when we were minized to tray when
  8746. // was SuspendWindowLock() called.
  8747. // We should possibly do the same check as in LockWindow(),
  8748. // if it is ever possible that the consitions change between
  8749. // SuspendWindowLock() and ResumeWindowLock()
  8750. Enabled = !FDisabledOnLockSuspend;
  8751. }
  8752. }
  8753. }
  8754. //---------------------------------------------------------------------------
  8755. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8756. {
  8757. if (!TextOnly)
  8758. {
  8759. TTBXComboBoxItem * RemotePathComboBox =
  8760. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8761. TStrings * Items = RemotePathComboBox->Strings;
  8762. Items->BeginUpdate();
  8763. try
  8764. {
  8765. Items->Clear();
  8766. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  8767. {
  8768. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8769. while (!IsUnixRootPath(APath))
  8770. {
  8771. int P = APath.LastDelimiter(L'/');
  8772. DebugAssert(P >= 0);
  8773. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8774. APath.SetLength(P - 1);
  8775. }
  8776. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8777. }
  8778. }
  8779. __finally
  8780. {
  8781. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8782. // Setting ItemIndex to -1 does not reset its text
  8783. if (Items->Count == 0)
  8784. {
  8785. RemotePathComboBox->Text = L"";
  8786. }
  8787. Items->EndUpdate();
  8788. }
  8789. }
  8790. }
  8791. //---------------------------------------------------------------------------
  8792. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8793. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8794. int & ImageIndex)
  8795. {
  8796. if (AIndex < 0)
  8797. {
  8798. AIndex = Sender->ItemIndex;
  8799. }
  8800. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8801. }
  8802. //---------------------------------------------------------------------------
  8803. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8804. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8805. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8806. {
  8807. ARect.Left += (10 * AIndex);
  8808. }
  8809. //---------------------------------------------------------------------------
  8810. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8811. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8812. {
  8813. AWidth += (10 * AIndex);
  8814. }
  8815. //---------------------------------------------------------------------------
  8816. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8817. TObject * Sender)
  8818. {
  8819. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8820. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8821. int Index = RemotePathComboBox->ItemIndex;
  8822. while (Index < RemotePathComboBox->Strings->Count - 1)
  8823. {
  8824. APath = UnixExtractFileDir(APath);
  8825. Index++;
  8826. }
  8827. // VanDyke style paths
  8828. if (APath.IsEmpty())
  8829. {
  8830. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8831. APath = ROOTDIRECTORY;
  8832. }
  8833. if (RemoteDirView->Path != APath)
  8834. {
  8835. RemoteDirView->Path = APath;
  8836. }
  8837. }
  8838. //---------------------------------------------------------------------------
  8839. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8840. {
  8841. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8842. DebugUsedParam(Sender);
  8843. UpdateRemotePathComboBox(true);
  8844. }
  8845. //---------------------------------------------------------------------------
  8846. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8847. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8848. {
  8849. ResetIncrementalSearch();
  8850. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8851. DebugAssert(DirView != NULL);
  8852. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8853. {
  8854. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8855. // OnEditing is called also from TCustomListView::CanEdit
  8856. if (Edit != NULL)
  8857. {
  8858. EditSelectBaseName(Edit);
  8859. }
  8860. }
  8861. }
  8862. //---------------------------------------------------------------------------
  8863. //---------------------------------------------------------------------------
  8864. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8865. {
  8866. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8867. Result->AutoDetectDnD = false;
  8868. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8869. Result->RenderDataOn = rdoEnterAndDropSync;
  8870. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8871. return Result;
  8872. }
  8873. //---------------------------------------------------------------------------
  8874. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8875. {
  8876. TForm::CreateWnd();
  8877. if (FSessionsDragDropFilesEx == NULL)
  8878. {
  8879. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8880. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8881. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8882. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8883. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8884. }
  8885. if (FQueueDragDropFilesEx == NULL)
  8886. {
  8887. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8888. // No need to set OnDragOver as we do not have any restrictions
  8889. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8890. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8891. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8892. }
  8893. }
  8894. //---------------------------------------------------------------------------
  8895. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8896. {
  8897. TForm::DestroyWnd();
  8898. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8899. FQueueDragDropFilesEx->DragDropControl = NULL;
  8900. }
  8901. //---------------------------------------------------------------------------
  8902. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8903. {
  8904. SideEnter(FCurrentSide);
  8905. FEverShown = true;
  8906. }
  8907. //---------------------------------------------------------------------------
  8908. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8909. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8910. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8911. {
  8912. if (!NonVisualDataModule->Busy)
  8913. {
  8914. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  8915. Configuration->Usage->Inc(L"FileFinds");
  8916. LockWindow(true);
  8917. NonVisualDataModule->StartBusy();
  8918. WinConfiguration->LockedInterface = true;
  8919. try
  8920. {
  8921. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8922. }
  8923. __finally
  8924. {
  8925. WinConfiguration->LockedInterface = false;
  8926. NonVisualDataModule->EndBusy();
  8927. UnlockWindow();
  8928. }
  8929. }
  8930. }
  8931. //---------------------------------------------------------------------------
  8932. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8933. {
  8934. if (!NonVisualDataModule->Busy)
  8935. {
  8936. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  8937. SetFocus();
  8938. RemoteDirView->Path = UnixExtractFilePath(Path);
  8939. UnicodeString FileName = UnixExtractFileName(Path);
  8940. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  8941. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  8942. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  8943. if (Item == NULL)
  8944. {
  8945. ReloadDirectory(osRemote);
  8946. Item = RemoteDirView->FindFileItem(FileName);
  8947. }
  8948. if (Item != NULL)
  8949. {
  8950. RemoteDirView->ItemFocused = Item;
  8951. RemoteDirView->ItemFocused->MakeVisible(false);
  8952. RemoteDirView->SetFocus();
  8953. }
  8954. }
  8955. }
  8956. //---------------------------------------------------------------------------
  8957. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  8958. {
  8959. bool Result = !NonVisualDataModule->Busy;
  8960. if (Result)
  8961. {
  8962. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  8963. }
  8964. return Result;
  8965. }
  8966. //---------------------------------------------------------------------------
  8967. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  8968. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8969. {
  8970. if (CanOperateOnFoundFiles(ATerminal))
  8971. {
  8972. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  8973. FOnFileOperationFinished = OnFileOperationFinished;
  8974. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  8975. }
  8976. }
  8977. //---------------------------------------------------------------------------
  8978. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  8979. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8980. {
  8981. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  8982. }
  8983. //---------------------------------------------------------------------------
  8984. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  8985. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8986. {
  8987. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  8988. }
  8989. //---------------------------------------------------------------------------
  8990. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  8991. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8992. {
  8993. if (CanOperateOnFoundFiles(ATerminal))
  8994. {
  8995. for (int Index = 0; Index < FileList->Count; Index++)
  8996. {
  8997. UnicodeString FileName = FileList->Strings[Index];
  8998. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  8999. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9000. OnFileOperationFinished(FileName, true);
  9001. }
  9002. }
  9003. }
  9004. //---------------------------------------------------------------------------
  9005. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9006. {
  9007. FFileFindTerminal = Terminal;
  9008. ShowFileFindDialog(
  9009. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9010. DoDownloadFoundFiles, DoEditFoundFiles);
  9011. }
  9012. //---------------------------------------------------------------------------
  9013. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9014. {
  9015. FTaskbarList = TaskbarList;
  9016. }
  9017. //---------------------------------------------------------------------------
  9018. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9019. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9020. {
  9021. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9022. if (Index >= 0)
  9023. {
  9024. if (Button == mbLeft)
  9025. {
  9026. // "Mouse down" is raised only after tab is switched.
  9027. // If switching tab (switching session) takes long enough for user
  9028. // to actually release the button, "mouse down" is still raised,
  9029. // but we do not get "mouse up" event, so dragging is not cancelled,
  9030. // prevent that by not beginning dragging in the first place.
  9031. if (IsKeyPressed(VK_LBUTTON))
  9032. {
  9033. // when user clicks the "+", we get mouse down only after the session
  9034. // is closed, when new session tab is already on X:Y, so dragging
  9035. // starts, prevent that
  9036. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  9037. {
  9038. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9039. if (Terminal != NULL)
  9040. {
  9041. SessionsPageControl->BeginDrag(false);
  9042. }
  9043. }
  9044. }
  9045. }
  9046. else if (Button == mbMiddle)
  9047. {
  9048. // ignore middle-click for "New session tab"
  9049. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9050. if (Terminal != NULL)
  9051. {
  9052. CloseSessionTab(Index);
  9053. }
  9054. }
  9055. }
  9056. }
  9057. //---------------------------------------------------------------------------
  9058. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9059. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9060. {
  9061. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9062. // do not allow dropping on the "+" tab
  9063. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9064. if ((TargetTerminal != NULL) &&
  9065. (SessionsPageControl->ActivePage->PageIndex != Index))
  9066. {
  9067. Configuration->Usage->Inc(L"SessionTabMoves");
  9068. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  9069. // but we want to actually prevent that to avoid flicker
  9070. SessionsPageControl->ActivePage->PageIndex = Index;
  9071. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  9072. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9073. }
  9074. }
  9075. //---------------------------------------------------------------------------
  9076. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9077. TObject * Sender, TObject * Source, int X, int Y,
  9078. TDragState /*State*/, bool & Accept)
  9079. {
  9080. Accept = (Sender == Source);
  9081. if (Accept)
  9082. {
  9083. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9084. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9085. // do not allow dragging to the "+" tab
  9086. Accept = (Terminal != NULL);
  9087. }
  9088. }
  9089. //---------------------------------------------------------------------------
  9090. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  9091. const TPoint & Point, int & Effect)
  9092. {
  9093. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9094. if (Index < 0)
  9095. {
  9096. Effect = DROPEFFECT_NONE;
  9097. }
  9098. else
  9099. {
  9100. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9101. // do not allow dropping on the "+" tab
  9102. if (TargetTerminal == NULL)
  9103. {
  9104. Effect = DROPEFFECT_NONE;
  9105. }
  9106. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_MOVE))
  9107. {
  9108. Effect = DROPEFFECT_COPY;
  9109. }
  9110. }
  9111. }
  9112. //---------------------------------------------------------------------------
  9113. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9114. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9115. {
  9116. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9117. // do not allow dropping on the "+" tab
  9118. TManagedTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9119. if (TargetTerminal != NULL)
  9120. {
  9121. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9122. if (!IsFileControl(DropSourceControl, osRemote))
  9123. {
  9124. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  9125. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9126. // Why don't we use Terminal->CurrentDirectory directly?
  9127. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9128. // do not force queue, drag drop
  9129. false, true);
  9130. }
  9131. }
  9132. }
  9133. //---------------------------------------------------------------------------
  9134. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9135. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9136. {
  9137. // Downloads are handled in RemoteFileControlDDEnd
  9138. if (!IsFileControl(DropSourceControl, osRemote))
  9139. {
  9140. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9141. Terminal->CurrentDirectory,
  9142. // force queue, drag drop
  9143. true, true);
  9144. }
  9145. }
  9146. //---------------------------------------------------------------------------
  9147. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9148. {
  9149. FShowing = false;
  9150. // Do not save empty workspace
  9151. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  9152. {
  9153. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9154. UnicodeString Name = WorkspaceName();
  9155. DoSaveWorkspace(
  9156. Name, DataList.get(),
  9157. !Configuration->DisablePasswordStoring &&
  9158. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9159. WinConfiguration->LastStoredSession = Name;
  9160. }
  9161. }
  9162. //---------------------------------------------------------------------------
  9163. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9164. {
  9165. TManagedTerminal * ManagedTerminal =
  9166. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9167. if (ManagedTerminal != NULL)
  9168. {
  9169. ManagedTerminal->DirectoryLoaded = Now();
  9170. }
  9171. }
  9172. //---------------------------------------------------------------------------
  9173. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9174. TListItem * /*Item*/, bool /*Selected*/)
  9175. {
  9176. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9177. switch (DirView->LastSelectMethod)
  9178. {
  9179. case smKeyboard:
  9180. Configuration->Usage->Inc(L"KeyboardSelections");
  9181. break;
  9182. case smMouse:
  9183. Configuration->Usage->Inc(L"MouseSelections");
  9184. break;
  9185. }
  9186. }
  9187. //---------------------------------------------------------------------------
  9188. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9189. {
  9190. if (Color != 0)
  9191. {
  9192. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9193. }
  9194. else
  9195. {
  9196. return FSessionTabImageIndex;
  9197. }
  9198. }
  9199. //---------------------------------------------------------------------------
  9200. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9201. {
  9202. TPngImageCollectionItem * Item =
  9203. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9204. FSessionColors->AddPng(Item->PngImage);
  9205. return FSessionColors->Count - 1;
  9206. }
  9207. //---------------------------------------------------------------------------
  9208. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9209. {
  9210. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9211. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9212. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9213. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9214. }
  9215. //---------------------------------------------------------------------------
  9216. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9217. {
  9218. for (int Index = 0; Index < Control->ControlCount; Index++)
  9219. {
  9220. TControl * AControl = Control->Controls[Index];
  9221. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9222. if (WinControl != NULL)
  9223. {
  9224. CollectItemsWithTextDisplayMode(WinControl);
  9225. }
  9226. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9227. if (Toolbar != NULL)
  9228. {
  9229. // we care for top-level items only
  9230. TTBCustomItem * Items = Toolbar->Items;
  9231. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9232. {
  9233. TTBCustomItem * Item = Items->Items[ItemIndex];
  9234. if (((Item->DisplayMode == nbdmImageAndText) ||
  9235. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9236. EligibleForImageDisplayMode(Item))
  9237. {
  9238. FItemsWithTextDisplayMode.insert(Item);
  9239. }
  9240. }
  9241. }
  9242. }
  9243. }
  9244. //---------------------------------------------------------------------------
  9245. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9246. {
  9247. return true;
  9248. }
  9249. //---------------------------------------------------------------------------
  9250. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9251. {
  9252. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9253. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9254. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9255. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9256. bool Result = true;
  9257. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9258. {
  9259. TTBCustomItem * Item = *i;
  9260. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9261. if (Label != NULL)
  9262. {
  9263. // optimization
  9264. if (Label->Visible == SelectiveToolbarText)
  9265. {
  9266. Result = false;
  9267. }
  9268. else
  9269. {
  9270. Label->Visible = SelectiveToolbarText;
  9271. }
  9272. }
  9273. else
  9274. {
  9275. // optimization
  9276. if (Item->DisplayMode == DisplayMode)
  9277. {
  9278. Result = false;
  9279. }
  9280. else
  9281. {
  9282. Item->DisplayMode = DisplayMode;
  9283. }
  9284. }
  9285. ++i;
  9286. }
  9287. if (Result)
  9288. {
  9289. UpdateNewSessionTab();
  9290. }
  9291. return Result;
  9292. }
  9293. //---------------------------------------------------------------------------
  9294. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9295. {
  9296. DebugFail();
  9297. }
  9298. //---------------------------------------------------------------------------
  9299. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9300. {
  9301. // Among other, a lock level is non zero, while directory is being loaded,
  9302. // even when it happens as a result of dirview navigation,
  9303. // i.e. when TNonVisualDataModule is NOT FBusy.
  9304. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9305. // Among other this prevents a panel auto update to occur while
  9306. // directory is changing.
  9307. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9308. }
  9309. //---------------------------------------------------------------------------
  9310. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9311. {
  9312. // While the window is disabled, we still seem to process menu shortcuts at least,
  9313. // so stop it at least here.
  9314. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9315. return
  9316. (Allowed == aaUpdate) ||
  9317. !NonVisualDataModule->Busy;
  9318. }
  9319. //---------------------------------------------------------------------------
  9320. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9321. {
  9322. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9323. }
  9324. //---------------------------------------------------------------------------
  9325. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9326. {
  9327. // This is somewhat redundant to LockWindow() call from
  9328. // TTerminalManager::TerminalReadDirectoryProgress.
  9329. // But disabling window is known not to block keyboard shorcuts
  9330. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9331. if (Busy > 0)
  9332. {
  9333. if (NonVisualDataModule->Busy)
  9334. {
  9335. State = false;
  9336. }
  9337. else
  9338. {
  9339. NonVisualDataModule->StartBusy();
  9340. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9341. LockWindow();
  9342. }
  9343. }
  9344. else if (Busy < 0)
  9345. {
  9346. UnlockWindow();
  9347. NonVisualDataModule->EndBusy();
  9348. }
  9349. else
  9350. {
  9351. State = NonVisualDataModule->Busy;
  9352. }
  9353. }
  9354. //---------------------------------------------------------------------------
  9355. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9356. {
  9357. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9358. // no context menu for "New session tab"
  9359. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9360. {
  9361. SessionsPageControl->ActivePageIndex = Index;
  9362. if (DebugAlwaysTrue(SessionTabSwitched()))
  9363. {
  9364. // copied from TControl.WMContextMenu
  9365. SendCancelMode(SessionsPageControl);
  9366. // explicit popup instead of using PopupMenu property
  9367. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9368. // while connecting yet not-connected session and hence
  9369. // allowing an access to commands over not-completelly connected session
  9370. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9371. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9372. PopupMenu->PopupComponent = SessionsPageControl;
  9373. PopupMenu->Popup(Point.x, Point.y);
  9374. }
  9375. }
  9376. Handled = true;
  9377. }
  9378. //---------------------------------------------------------------------------
  9379. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9380. {
  9381. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9382. }
  9383. //---------------------------------------------------------------------------
  9384. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9385. {
  9386. Configuration->Usage->Inc(L"PasswordChanges");
  9387. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9388. ChangePasswordData->DisableAuthentationsExceptPassword();
  9389. ChangePasswordData->ChangePassword = true;
  9390. std::unique_ptr<TTerminal> ChangePasswordSession(
  9391. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9392. ChangePasswordSession->AutoReadDirectory = false;
  9393. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9394. {
  9395. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9396. }
  9397. else
  9398. {
  9399. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9400. }
  9401. }
  9402. //---------------------------------------------------------------------------
  9403. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9404. {
  9405. // No nice way to assert SSH2
  9406. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9407. }
  9408. //---------------------------------------------------------------------------
  9409. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9410. {
  9411. TOperationVisualizer OperationVisualizer;
  9412. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9413. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9414. }
  9415. //---------------------------------------------------------------------------
  9416. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9417. {
  9418. TForm::ChangeScale(M, D);
  9419. int APixelsPerInch = GetControlPixelsPerInch(this);
  9420. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9421. try
  9422. {
  9423. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9424. }
  9425. __finally
  9426. {
  9427. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9428. }
  9429. }
  9430. //---------------------------------------------------------------------------
  9431. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9432. {
  9433. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9434. }
  9435. //---------------------------------------------------------------------
  9436. class TPublicControl : public TControl
  9437. {
  9438. friend class TCustomScpExplorerForm;
  9439. };
  9440. //---------------------------------------------------------------------------
  9441. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9442. {
  9443. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9444. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9445. }
  9446. //---------------------------------------------------------------------------
  9447. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9448. {
  9449. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9450. OpenBookmark(Side, Bookmark);
  9451. }
  9452. //---------------------------------------------------------------------------
  9453. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9454. {
  9455. DoBookmarkClick(osLocal, Sender);
  9456. }
  9457. //---------------------------------------------------------------------------
  9458. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9459. {
  9460. DoBookmarkClick(osRemote, Sender);
  9461. }
  9462. //---------------------------------------------------------------------------
  9463. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9464. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9465. {
  9466. // TODO
  9467. if (BookmarkList != NULL)
  9468. {
  9469. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9470. if (!WinConfiguration->UseLocationProfiles)
  9471. {
  9472. std::unique_ptr<TStringList> Directories(new TStringList());
  9473. Directories->CaseSensitive = (Side == osRemote);
  9474. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9475. {
  9476. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9477. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9478. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9479. {
  9480. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9481. Item->Caption = EscapeHotkey(Directory);
  9482. Item->ShortCut = Bookmark->ShortCut;
  9483. Item->OnClick = OnBookmarkClick;
  9484. Item->Tag = reinterpret_cast<int>(Bookmark);
  9485. Directories->Add(Directory);
  9486. Menu->Add(Item.release());
  9487. }
  9488. }
  9489. }
  9490. else
  9491. {
  9492. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9493. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9494. {
  9495. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9496. if (!Bookmark->Node.IsEmpty())
  9497. {
  9498. Folders->Add(Bookmark->Node);
  9499. }
  9500. }
  9501. for (int Index = 0; Index < Folders->Count; Index++)
  9502. {
  9503. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9504. Item->Caption = Folders->Strings[Index];
  9505. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9506. Folders->Objects[Index] = Item.get();
  9507. Menu->Add(Item.release());
  9508. }
  9509. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9510. {
  9511. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9512. TTBCustomItem * Parent;
  9513. if (!Bookmark->Node.IsEmpty())
  9514. {
  9515. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9516. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9517. }
  9518. else
  9519. {
  9520. Parent = Menu;
  9521. }
  9522. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9523. Item->Caption = Bookmark->Name;
  9524. Item->ShortCut = Bookmark->ShortCut;
  9525. Item->OnClick = OnBookmarkClick;
  9526. Item->Tag = reinterpret_cast<int>(Bookmark);
  9527. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9528. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9529. {
  9530. // No hint for location profiles that are actually mere "bookmarks"
  9531. }
  9532. else
  9533. {
  9534. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9535. UnicodeString LongHint;
  9536. if (!Bookmark->Local.IsEmpty())
  9537. {
  9538. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9539. }
  9540. if (!Bookmark->Remote.IsEmpty())
  9541. {
  9542. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9543. }
  9544. AddToList(Hint, LongHint, L"|");
  9545. Item->Hint = Hint;
  9546. }
  9547. Parent->Add(Item.release());
  9548. }
  9549. }
  9550. AddMenuSeparator(Menu);
  9551. }
  9552. }
  9553. //---------------------------------------------------------------------------
  9554. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9555. {
  9556. Menu->Clear();
  9557. std::unique_ptr<TTBCustomItem> Item;
  9558. Item.reset(new TTBXItem(Owner));
  9559. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9560. Item->Options = Item->Options << tboDefault;
  9561. Menu->Add(Item.release());
  9562. AddMenuSeparator(Menu);
  9563. if (Terminal != NULL)
  9564. {
  9565. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9566. }
  9567. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9568. Item.reset(new TTBXItem(Owner));
  9569. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9570. Menu->Add(Item.release());
  9571. }
  9572. //---------------------------------------------------------------------------
  9573. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9574. {
  9575. bool Result = true;
  9576. try
  9577. {
  9578. if (Side == osRemote)
  9579. {
  9580. Terminal->ExceptionOnFail = true;
  9581. }
  9582. try
  9583. {
  9584. DirView(Side)->Path = Path;
  9585. }
  9586. __finally
  9587. {
  9588. if (Side == osRemote)
  9589. {
  9590. Terminal->ExceptionOnFail = false;
  9591. }
  9592. }
  9593. }
  9594. catch (Exception & E)
  9595. {
  9596. if ((Side != osRemote) || Terminal->Active)
  9597. {
  9598. ShowExtendedExceptionEx(Terminal, &E);
  9599. Result = false;
  9600. }
  9601. else
  9602. {
  9603. throw;
  9604. }
  9605. }
  9606. return Result;
  9607. }
  9608. //---------------------------------------------------------------------------
  9609. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9610. {
  9611. // Make sure there some feedback even when loading tiny local folders
  9612. TInstantOperationVisualizer Visualizer;
  9613. DirView(Side)->ReloadDirectory();
  9614. }
  9615. //---------------------------------------------------------------------------
  9616. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9617. {
  9618. if (Index == SessionsPageControl->TabIndex)
  9619. {
  9620. CloseSession();
  9621. }
  9622. else
  9623. {
  9624. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9625. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9626. TTerminalManager * Manager = TTerminalManager::Instance();
  9627. if (!Terminal->Active)
  9628. {
  9629. Manager->FreeTerminal(Terminal);
  9630. }
  9631. else
  9632. {
  9633. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9634. if (CanCloseQueue(Queue))
  9635. {
  9636. Manager->FreeTerminal(Terminal);
  9637. }
  9638. }
  9639. }
  9640. }
  9641. //---------------------------------------------------------------------------
  9642. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9643. {
  9644. CloseSessionTab(Index);
  9645. }
  9646. //---------------------------------------------------------------------------
  9647. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  9648. {
  9649. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  9650. {
  9651. TInstantOperationVisualizer Visualizer;
  9652. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9653. // before we create a new data object
  9654. ClipboardClear();
  9655. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9656. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9657. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9658. FClipboardFakeMonitors.reset(StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated));
  9659. if (FClipboardDragDropFilesEx.get() == NULL)
  9660. {
  9661. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9662. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9663. }
  9664. FClipboardDragDropFilesEx->FileList->Clear();
  9665. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9666. FClipboardDragDropFilesEx->CopyToClipboard();
  9667. FClipboardTerminal = FTerminal;
  9668. // Need full paths, as cwd can be different once files are pasted
  9669. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  9670. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9671. {
  9672. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9673. }
  9674. }
  9675. }
  9676. //---------------------------------------------------------------------------
  9677. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9678. {
  9679. if (OpenClipboard(0))
  9680. {
  9681. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9682. CloseClipboard();
  9683. }
  9684. }
  9685. //---------------------------------------------------------------------------
  9686. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9687. {
  9688. return !FClipboardFakeDirectory.IsEmpty();
  9689. }
  9690. //---------------------------------------------------------------------------
  9691. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9692. {
  9693. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9694. FClipboardFakeDirectory = UnicodeString();
  9695. FClipboardTerminal = NULL;
  9696. // Also called in Idle()
  9697. FClipboardFakeMonitors.reset(NULL);
  9698. }
  9699. //---------------------------------------------------------------------------
  9700. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9701. {
  9702. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9703. {
  9704. ClipboardStop();
  9705. }
  9706. }
  9707. //---------------------------------------------------------------------------
  9708. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9709. {
  9710. TTerminalManager * Manager = TTerminalManager::Instance();
  9711. Manager->ActiveTerminal = FClipboardTerminal;
  9712. TTransferOperationParam Params;
  9713. Params.TargetDirectory = TargetDirectory;
  9714. Params.DragDrop = DragDrop;
  9715. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9716. }
  9717. //---------------------------------------------------------------------------
  9718. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9719. {
  9720. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9721. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9722. {
  9723. RemoveDir(ApiPath(FileName));
  9724. if (!NonVisualDataModule->Busy)
  9725. {
  9726. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9727. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9728. }
  9729. }
  9730. }
  9731. //---------------------------------------------------------------------------
  9732. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  9733. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  9734. {
  9735. TFileMasks::TParams MaskParams;
  9736. MaskParams.Size = Size;
  9737. MaskParams.Modification = Modification;
  9738. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  9739. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  9740. {
  9741. bool ImplicitMatch;
  9742. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  9743. !ImplicitMatch)
  9744. {
  9745. Color = Iter->Color;
  9746. break;
  9747. }
  9748. }
  9749. }
  9750. //---------------------------------------------------------------------------
  9751. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  9752. {
  9753. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9754. if (!DirView->IsEditing())
  9755. {
  9756. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  9757. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  9758. // while searching
  9759. if (Key == VK_BACK)
  9760. {
  9761. if (!FIncrementalSearch.IsEmpty())
  9762. {
  9763. if (FIncrementalSearch.Length() == 1)
  9764. {
  9765. ResetIncrementalSearch();
  9766. }
  9767. else
  9768. {
  9769. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  9770. IncrementalSearch(NewText, false, false);
  9771. }
  9772. Key = 0;
  9773. }
  9774. }
  9775. }
  9776. }
  9777. //---------------------------------------------------------------------------
  9778. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  9779. {
  9780. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9781. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  9782. {
  9783. // Filter control sequences.
  9784. // When not searching yet, prefer use of the space for toggling file selection
  9785. // (so we cannot incrementally search for a string starting with a space).
  9786. if ((Key > VK_SPACE) ||
  9787. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  9788. {
  9789. IncrementalSearch(FIncrementalSearch + Key, false, false);
  9790. Key = 0;
  9791. }
  9792. }
  9793. }
  9794. //---------------------------------------------------------------------------
  9795. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  9796. {
  9797. if (!FIncrementalSearch.IsEmpty())
  9798. {
  9799. FIncrementalSearch = L"";
  9800. DirView(osCurrent)->UpdateStatusBar();
  9801. }
  9802. }
  9803. //---------------------------------------------------------------------------
  9804. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9805. {
  9806. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  9807. if (Item == NULL)
  9808. {
  9809. MessageBeep(MB_ICONHAND);
  9810. }
  9811. else
  9812. {
  9813. TCustomDirView * ADirView = DirView(osCurrent);
  9814. {
  9815. TAutoNestingCounter Guard(FIncrementalSearching);
  9816. ADirView->FocusItem(Item);
  9817. if (ADirView->NortonLike == nlOff)
  9818. {
  9819. if ((ADirView->Selected != Item) ||
  9820. (ADirView->SelCount > 1))
  9821. {
  9822. ADirView->ClearSelection();
  9823. ADirView->Selected = Item;
  9824. }
  9825. }
  9826. }
  9827. FIncrementalSearch = Text;
  9828. Item->MakeVisible(false);
  9829. TListItem * NextItem = SearchFile(Text, true, Reverse);
  9830. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  9831. ADirView->UpdateStatusBar();
  9832. }
  9833. }
  9834. //---------------------------------------------------------------------------
  9835. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  9836. {
  9837. int Index = Item->Index;
  9838. if (!Reverse)
  9839. {
  9840. Index++;
  9841. if (Index >= Item->Owner->Count)
  9842. {
  9843. Index = 0;
  9844. }
  9845. }
  9846. else
  9847. {
  9848. Index--;
  9849. if (Index < 0)
  9850. {
  9851. Index = Item->Owner->Count - 1;
  9852. }
  9853. }
  9854. return Item->Owner->Item[Index];
  9855. }
  9856. //---------------------------------------------------------------------------
  9857. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9858. {
  9859. TCustomDirView * ADirView = DirView(osCurrent);
  9860. if (ADirView->Items->Count == 0)
  9861. {
  9862. return NULL;
  9863. }
  9864. else
  9865. {
  9866. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  9867. TListItem * Item = CurrentItem;
  9868. if (SkipCurrent)
  9869. {
  9870. Item = DebugNotNull(GetNextFile(Item, Reverse));
  9871. }
  9872. while (true)
  9873. {
  9874. bool Matches = false;
  9875. switch (WinConfiguration->PanelSearch)
  9876. {
  9877. case isNameStartOnly:
  9878. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  9879. break;
  9880. case isName:
  9881. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  9882. break;
  9883. case isAll:
  9884. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  9885. int Index = 0;
  9886. while ((Index < ColCount) && !Matches)
  9887. {
  9888. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  9889. if (Visible)
  9890. {
  9891. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  9892. }
  9893. Index++;
  9894. }
  9895. break;
  9896. }
  9897. if (Matches)
  9898. {
  9899. return Item;
  9900. }
  9901. Item = GetNextFile(Item, Reverse);
  9902. if (Item == CurrentItem)
  9903. {
  9904. return NULL;
  9905. }
  9906. }
  9907. }
  9908. }
  9909. //---------------------------------------------------------------------------
  9910. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  9911. {
  9912. ResetIncrementalSearch();
  9913. }
  9914. //---------------------------------------------------------------------------
  9915. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  9916. {
  9917. if (Message.CharCode == VK_TAB)
  9918. {
  9919. if (!FIncrementalSearch.IsEmpty())
  9920. {
  9921. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  9922. bool Reverse = Shift.Contains(ssShift);
  9923. IncrementalSearch(FIncrementalSearch, true, Reverse);
  9924. Message.Result = 1;
  9925. return;
  9926. }
  9927. }
  9928. else if (Message.CharCode == VK_ESCAPE)
  9929. {
  9930. if (!FIncrementalSearch.IsEmpty())
  9931. {
  9932. ResetIncrementalSearch();
  9933. Message.Result = 1;
  9934. return;
  9935. }
  9936. }
  9937. TForm::Dispatch(&Message);
  9938. }
  9939. //---------------------------------------------------------------------------
  9940. void __fastcall TCustomScpExplorerForm::Deactivate()
  9941. {
  9942. TForm::Deactivate();
  9943. // E.g. when switching to an internal editor window
  9944. ResetIncrementalSearch();
  9945. }
  9946. //---------------------------------------------------------------------------
  9947. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  9948. {
  9949. // When switching to another application
  9950. ResetIncrementalSearch();
  9951. }
  9952. //---------------------------------------------------------------------------
  9953. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  9954. {
  9955. ResetIncrementalSearch();
  9956. }
  9957. //---------------------------------------------------------------------------
  9958. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  9959. {
  9960. if (FIncrementalSearching <= 0)
  9961. {
  9962. ResetIncrementalSearch();
  9963. }
  9964. }
  9965. //---------------------------------------------------------------------------
  9966. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  9967. {
  9968. CountClicksForWindowPrint(this);
  9969. }
  9970. //---------------------------------------------------------------------------
  9971. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  9972. {
  9973. CenterReconnectToolbar();
  9974. }
  9975. //---------------------------------------------------------------------------