CustomScpExplorer.cpp 344 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540
  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. TTerminal * Terminal;
  145. };
  146. //---------------------------------------------------------------------------
  147. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  148. FFormRestored(false),
  149. TForm(Owner)
  150. {
  151. FCurrentSide = osRemote;
  152. FEverShown = false;
  153. FDocks = new TList();
  154. RestoreParams();
  155. ConfigurationChanged();
  156. RemoteDirView->Invalidate();
  157. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  158. NonVisualDataModule->ScpExplorer = this;
  159. FAutoOperation = false;
  160. FOnFileOperationFinished = NULL;
  161. FForceExecution = false;
  162. FIgnoreNextDialogChar = 0;
  163. FErrorList = NULL;
  164. FSynchronizeProgressForm = NULL;
  165. FProgressForm = NULL;
  166. FRefreshLocalDirectory = false;
  167. FRefreshRemoteDirectory = false;
  168. FDDExtMapFile = NULL;
  169. // CreateMutexW keeps failing with ERROR_NOACCESS
  170. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  171. DebugAssert(FDDExtMutex != NULL);
  172. FDDTargetControl = NULL;
  173. FDelayedDeletionTimer = NULL;
  174. FDelayedDeletionList = new TStringList();
  175. FDDFileList = NULL;
  176. FPendingTempSpaceWarn = false;
  177. FCapturedLog = NULL;
  178. FDragDropOperation = false;
  179. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  180. FAllowTransferPresetAutoSelect = true;
  181. FCopyParamDefault = L"";
  182. FSynchronizeController = NULL;
  183. FPendingQueueActionItem = NULL;
  184. FLockLevel = 0;
  185. FLockSuspendLevel = 0;
  186. FDisabledOnLockSuspend = false;
  187. FAlternativeDelete = false;
  188. FTrayIcon = new ::TTrayIcon(0);
  189. FTrayIcon->OnClick = TrayIconClick;
  190. FMaxQueueLength = 0;
  191. FLastContextPopupScreenPoint = TPoint(-1, -1);
  192. FTransferResumeList = NULL;
  193. FMoveToQueue = false;
  194. FStandaloneEditing = false;
  195. FOnFeedSynchronizeError = NULL;
  196. FNeedSession = false;
  197. FDoNotIdleCurrentTerminal = 0;
  198. FIncrementalSearching = 0;
  199. FIncrementalSearchHaveNext = false;
  200. FEditorManager = new TEditorManager();
  201. FEditorManager->OnFileChange = ExecutedFileChanged;
  202. FEditorManager->OnFileReload = ExecutedFileReload;
  203. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  204. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  205. FLocalEditors = new TList();
  206. FQueueStatus = NULL;
  207. FQueueStatusSection = new TCriticalSection();
  208. FQueueStatusInvalidated = false;
  209. FQueueItemInvalidated = false;
  210. FQueueActedItem = NULL;
  211. FQueueController = new TQueueController(QueueView3);
  212. FUserActionTimer = new TTimer(this);
  213. FUserActionTimer->Enabled = false;
  214. FUserActionTimer->Interval = 10;
  215. FUserActionTimer->OnTimer = UserActionTimer;
  216. FNotes = new TStringList();
  217. FNoteTimer = new TTimer(this);
  218. FNoteTimer->Enabled = false;
  219. FNoteTimer->OnTimer = NoteTimer;
  220. FOnNoteClick = NULL;
  221. FOle32Library = LoadLibrary(L"Ole32.dll");
  222. FDragMoveCursor = FOle32Library != NULL ?
  223. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  224. UseSystemSettings(this);
  225. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  226. static_cast<TObject*>(GetComponent(fcTransferList)));
  227. DebugAssert(TransferList != NULL);
  228. FTransferListHoverIndex = -1;
  229. TransferList->OnChange = TransferListChange;
  230. TransferList->OnDrawItem = TransferListDrawItem;
  231. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  232. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  233. UseDesktopFont(SessionsPageControl);
  234. UpdateSessionsPageControlHeight();
  235. UseDesktopFont(RemoteDirView);
  236. UseDesktopFont(RemoteDriveView);
  237. UseDesktopFont(QueueView3);
  238. UseDesktopFont(QueueLabel);
  239. UseDesktopFont(RemoteStatusBar);
  240. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  241. QueueSplitter->ShowHint = true;
  242. RemotePanelSplitter->ShowHint = true;
  243. UpdateImages();
  244. FCustomCommandMenu = CreateTBXPopupMenu(this);
  245. FCustomCommandLocalFileList = NULL;
  246. FCustomCommandRemoteFileList = NULL;
  247. FSessionColors = new TPngImageList(this);
  248. FSessionColors->ColorDepth = cd32Bit;
  249. AddFixedSessionImages();
  250. SessionsPageControl->Images = FSessionColors;
  251. QueueLabel->FocusControl = QueueView3;
  252. UpdateQueueLabel();
  253. FRemoteDirViewWasFocused = true;
  254. CreateHiddenWindow();
  255. StartUpdates();
  256. }
  257. //---------------------------------------------------------------------------
  258. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  259. {
  260. // this has to be one of the very first things to do
  261. StopUpdateThread();
  262. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  263. TTerminalManager::Instance()->ScpExplorer = NULL;
  264. delete FCustomCommandLocalFileList;
  265. delete FCustomCommandRemoteFileList;
  266. delete FCustomCommandMenu;
  267. delete FTrayIcon;
  268. FTrayIcon = NULL;
  269. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  270. delete FEditorManager;
  271. ForceCloseLocalEditors();
  272. delete FLocalEditors;
  273. HideFileFindDialog();
  274. if (FDelayedDeletionTimer)
  275. {
  276. DoDelayedDeletion(NULL);
  277. SAFE_DESTROY(FDelayedDeletionTimer);
  278. }
  279. SAFE_DESTROY(FDelayedDeletionList);
  280. // sometimes we do not get DDEnd so the list is not released
  281. SAFE_DESTROY(FDDFileList);
  282. DebugAssert(FSynchronizeController == NULL);
  283. CloseHandle(FDDExtMutex);
  284. FDDExtMutex = NULL;
  285. FreeLibrary(FOle32Library);
  286. FOle32Library = NULL;
  287. FDragMoveCursor = NULL;
  288. DebugAssert(!FErrorList);
  289. if (FEverShown)
  290. {
  291. // when window is never shown (like when running command-line operation),
  292. // particularly window site is not restored correctly (BoundsRect value set
  293. // in RestoreForm gets lost during handle allocation), so we do not want
  294. // it to be stored
  295. StoreParams();
  296. }
  297. Terminal = NULL;
  298. Queue = NULL;
  299. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  300. NonVisualDataModule->ScpExplorer = NULL;
  301. delete FQueueController;
  302. FQueueController = NULL;
  303. delete FQueueStatusSection;
  304. FQueueStatusSection = NULL;
  305. delete FQueueStatus;
  306. FQueueStatus = NULL;
  307. delete FUserActionTimer;
  308. FUserActionTimer = NULL;
  309. delete FNoteTimer;
  310. delete FNotes;
  311. delete FNoteData;
  312. SAFE_DESTROY(FDocks);
  313. SessionsPageControl->Images = NULL;
  314. // only after clearing Terminal (after DoTerminalListChanged => ... => AddSessionColor is called)
  315. SAFE_DESTROY(FSessionColors);
  316. SAFE_DESTROY(FSessionsDragDropFilesEx);
  317. SAFE_DESTROY(FHistoryMenu[0][0]);
  318. SAFE_DESTROY(FHistoryMenu[0][1]);
  319. SAFE_DESTROY(FHistoryMenu[1][0]);
  320. SAFE_DESTROY(FHistoryMenu[1][1]);
  321. if (FHiddenWindow != NULL)
  322. {
  323. DestroyWindow(FHiddenWindow);
  324. FHiddenWindow = NULL;
  325. }
  326. }
  327. //---------------------------------------------------------------------------
  328. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  329. {
  330. std::unique_ptr<TSessionData> Data;
  331. if (!Session.IsEmpty())
  332. {
  333. bool DefaultsOnly;
  334. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  335. }
  336. TTerminalManager * Manager = TTerminalManager::Instance();
  337. for (int Index = 0; Index < Manager->Count; Index++)
  338. {
  339. TTerminal * Terminal = Manager->Terminals[Index];
  340. if (Session.IsEmpty() ||
  341. Terminal->SessionData->IsSameSite(Data.get()))
  342. {
  343. if (Path.IsEmpty())
  344. {
  345. Terminal->ClearCaches();
  346. }
  347. else
  348. {
  349. Terminal->DirectoryModified(Path, true);
  350. }
  351. }
  352. }
  353. // We should flag a pending refresh for the background terminals or busy foreground terminals
  354. if ((Terminal != NULL) && Terminal->Active &&
  355. CanCommandLineFromAnotherInstance() &&
  356. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  357. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  358. {
  359. Terminal->ReloadDirectory();
  360. }
  361. }
  362. //---------------------------------------------------------------------------
  363. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  364. {
  365. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  366. size_t MessageSize = sizeof(TCopyDataMessage);
  367. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  368. if (Result)
  369. {
  370. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  371. Result = (Message.Version == TCopyDataMessage::Version1);
  372. if (Result)
  373. {
  374. switch (Message.Command)
  375. {
  376. case TCopyDataMessage::CommandCanCommandLine:
  377. Result = CanCommandLineFromAnotherInstance();
  378. break;
  379. case TCopyDataMessage::CommandCommandLine:
  380. {
  381. UnicodeString CommandLine(Message.CommandLine);
  382. Result = CommandLineFromAnotherInstance(CommandLine);
  383. }
  384. break;
  385. case TCopyDataMessage::RefreshPanel:
  386. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  387. break;
  388. case TCopyDataMessage::MainWindowCheck:
  389. Result = true;
  390. break;
  391. default:
  392. Result = false;
  393. break;
  394. }
  395. }
  396. }
  397. Message.Result = Result ? 1 : 0;
  398. }
  399. //---------------------------------------------------------------------------
  400. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  401. {
  402. WNDCLASS WindowClass = {0};
  403. WindowClass.lpfnWndProc = DefWindowProc;
  404. WindowClass.hInstance = HInstance;
  405. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  406. FHiddenWindow = NULL;
  407. if (RegisterClass(&WindowClass))
  408. {
  409. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  410. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  411. }
  412. }
  413. //---------------------------------------------------------------------------
  414. bool __fastcall TCustomScpExplorerForm::CanConsole()
  415. {
  416. return
  417. (Terminal != NULL) &&
  418. Terminal->Active &&
  419. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  420. }
  421. //---------------------------------------------------------------------------
  422. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  423. {
  424. bool Result = Visible && !NonVisualDataModule->Busy;
  425. return Result;
  426. }
  427. //---------------------------------------------------------------------------
  428. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  429. const UnicodeString & CommandLine)
  430. {
  431. TProgramParams Params(CommandLine);
  432. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  433. if (Result)
  434. {
  435. NonVisualDataModule->StartBusy();
  436. try
  437. {
  438. // this action is initiated from another process,
  439. // so it's likely that our window is not visible,
  440. // and user won't see what is going on
  441. Application->BringToFront();
  442. // reload sessions as we may be asked to open a session
  443. // just stored by another instance
  444. StoredSessions->Reload();
  445. UnicodeString SessionName = Params.Param[1];
  446. std::unique_ptr<TObjectList> DataList(new TObjectList());
  447. UnicodeString DownloadFile; // unused
  448. try
  449. {
  450. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  451. }
  452. catch (EAbort &)
  453. {
  454. // Happens when opening session in PuTTY and in other situations,
  455. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  456. // to process the command-line on its own (or to pass it to yet another instance)
  457. }
  458. if (DataList->Count > 0)
  459. {
  460. TTerminalManager * Manager = TTerminalManager::Instance();
  461. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  462. }
  463. }
  464. __finally
  465. {
  466. NonVisualDataModule->EndBusy();
  467. }
  468. }
  469. return Result;
  470. }
  471. //---------------------------------------------------------------------------
  472. void __fastcall TCustomScpExplorerForm::SetTerminal(TTerminal * value)
  473. {
  474. if (FTerminal != value)
  475. {
  476. TerminalChanging();
  477. DoSetTerminal(value, false);
  478. }
  479. }
  480. //---------------------------------------------------------------------------
  481. void __fastcall TCustomScpExplorerForm::DoSetTerminal(TTerminal * value, bool Replace)
  482. {
  483. FTerminal = value;
  484. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  485. FAllowTransferPresetAutoSelect = false;
  486. try
  487. {
  488. TerminalChanged(Replace);
  489. }
  490. __finally
  491. {
  492. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  493. }
  494. if (Terminal != NULL)
  495. {
  496. TransferPresetAutoSelect();
  497. }
  498. }
  499. //---------------------------------------------------------------------------
  500. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TTerminal * value)
  501. {
  502. DoSetTerminal(value, true);
  503. }
  504. //---------------------------------------------------------------------------
  505. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  506. {
  507. if (FTerminal != NULL)
  508. {
  509. UpdateTerminal(Terminal);
  510. }
  511. }
  512. //---------------------------------------------------------------------------
  513. void __fastcall TCustomScpExplorerForm::TerminalChanged(bool Replaced)
  514. {
  515. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  516. if (ManagedTerminal != NULL)
  517. {
  518. UpdateSessionColor((TColor)ManagedTerminal->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. DebugAssert(ManagedTerminal == Terminal);
  538. if (WinConfiguration->PreservePanelState)
  539. {
  540. if (ManagedTerminal->RemoteExplorerState != NULL)
  541. {
  542. DirView(osRemote)->RestoreState(ManagedTerminal->RemoteExplorerState);
  543. }
  544. else
  545. {
  546. DirView(osRemote)->ClearState();
  547. }
  548. }
  549. if (!Replaced)
  550. {
  551. InitStatusBar();
  552. }
  553. }
  554. UpdateTransferList();
  555. // Update panels Enable state before refreshing the labels
  556. UpdateControls();
  557. }
  558. //---------------------------------------------------------------------------
  559. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  560. {
  561. if (Queue != value)
  562. {
  563. if (FQueue != NULL)
  564. {
  565. FQueue->OnListUpdate = NULL;
  566. FQueue->OnQueueItemUpdate = NULL;
  567. }
  568. FQueue = value;
  569. if (FQueue != NULL)
  570. {
  571. DebugAssert(FQueue->OnListUpdate == NULL);
  572. FQueue->OnListUpdate = QueueListUpdate;
  573. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  574. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  575. }
  576. QueueChanged();
  577. }
  578. }
  579. //---------------------------------------------------------------------------
  580. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  581. TListItem * Item)
  582. {
  583. if (FQueueActedItem == Item)
  584. {
  585. FQueueActedItem = NULL;
  586. if ((QueueView3->PopupMenu != NULL) &&
  587. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  588. {
  589. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  590. ReleaseCapture();
  591. }
  592. }
  593. if (Item->Data == FPendingQueueActionItem)
  594. {
  595. FPendingQueueActionItem = NULL;
  596. }
  597. }
  598. //---------------------------------------------------------------------------
  599. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  600. {
  601. {
  602. TGuard Guard(FQueueStatusSection);
  603. FQueueStatusInvalidated = false;
  604. if (FQueue != NULL)
  605. {
  606. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  607. }
  608. }
  609. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  610. {
  611. FMaxQueueLength = FQueueStatus->Count;
  612. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  613. }
  614. FQueueController->UpdateQueueStatus(FQueueStatus);
  615. SetQueueProgress();
  616. UpdateQueueView();
  617. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  618. if (IsEmpty && (Terminal != NULL))
  619. {
  620. TOnceDoneOperation OnceDoneOperation =
  621. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  622. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  623. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  624. {
  625. FQueue->Enabled = false;
  626. }
  627. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  628. {
  629. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  630. }
  631. }
  632. }
  633. //---------------------------------------------------------------------------
  634. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  635. {
  636. UnicodeString Result;
  637. if (FQueueStatus != NULL)
  638. {
  639. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  640. {
  641. TFileOperationProgressType * ProgressData =
  642. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  643. if ((ProgressData != NULL) && ProgressData->InProgress)
  644. {
  645. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  646. }
  647. }
  648. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  649. {
  650. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  651. }
  652. }
  653. return Result;
  654. }
  655. //---------------------------------------------------------------------------
  656. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  657. {
  658. ComponentVisible[fcQueueView] =
  659. (WinConfiguration->QueueView.Show == qvShow) ||
  660. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  661. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  662. }
  663. //---------------------------------------------------------------------------
  664. void __fastcall TCustomScpExplorerForm::QueueChanged()
  665. {
  666. if (FQueueStatus != NULL)
  667. {
  668. delete FQueueStatus;
  669. FQueueStatus = NULL;
  670. }
  671. UpdateQueueStatus(true);
  672. }
  673. //---------------------------------------------------------------------------
  674. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  675. {
  676. if (FQueue == Queue)
  677. {
  678. FQueueStatusInvalidated = true;
  679. }
  680. }
  681. //---------------------------------------------------------------------------
  682. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  683. TQueueItem * Item)
  684. {
  685. if (FQueue == Queue)
  686. {
  687. TGuard Guard(FQueueStatusSection);
  688. // this may be running in parallel with QueueChanged
  689. if (FQueueStatus != NULL)
  690. {
  691. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  692. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  693. {
  694. FRefreshLocalDirectory = (QueueItem == NULL) ||
  695. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  696. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  697. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  698. }
  699. if (QueueItem != NULL)
  700. {
  701. QueueItem->UserData = (void*)true;
  702. FQueueItemInvalidated = true;
  703. }
  704. }
  705. }
  706. }
  707. //---------------------------------------------------------------------------
  708. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  709. {
  710. // during standalone editing, we have no way to see/control queue,
  711. // so we have to always popup prompts automatically
  712. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  713. }
  714. //---------------------------------------------------------------------------
  715. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  716. {
  717. if (FQueueStatus != NULL)
  718. {
  719. bool QueueAutoPopup = IsQueueAutoPopup();
  720. bool NeedRefresh = FQueueController->NeedRefresh();
  721. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  722. FQueueItemInvalidated = false;
  723. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  724. bool Updated = false;
  725. TQueueItemProxy * QueueItem;
  726. bool WasUserAction;
  727. for (int Index = 0; Index < Limit; Index++)
  728. {
  729. bool Update = false;
  730. QueueItem = FQueueStatus->Items[Index];
  731. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  732. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  733. (FPendingQueueActionItem == NULL))
  734. {
  735. FPendingQueueActionItem = QueueItem;
  736. FUserActionTimer->Enabled = true;
  737. }
  738. if ((bool)QueueItem->UserData)
  739. {
  740. QueueItem->UserData = (void*)false;
  741. QueueItem->Update();
  742. Updated = true;
  743. Update = true;
  744. }
  745. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  746. {
  747. Update = true;
  748. }
  749. if (Update)
  750. {
  751. FQueueController->RefreshQueueItem(QueueItem);
  752. }
  753. }
  754. if (Updated)
  755. {
  756. NonVisualDataModule->UpdateNonVisibleActions();
  757. SetQueueProgress();
  758. }
  759. }
  760. }
  761. //---------------------------------------------------------------------------
  762. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  763. {
  764. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  765. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  766. }
  767. //---------------------------------------------------------------------------
  768. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  769. {
  770. if (ProgressData->Operation != foCalculateSize)
  771. {
  772. int OverallProgress;
  773. // FProgressForm is null when this is called from SetQueueProgress
  774. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  775. {
  776. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  777. }
  778. else
  779. {
  780. OverallProgress = ProgressData->OverallProgress();
  781. }
  782. // implies TBPF_NORMAL
  783. FTaskbarList->SetProgressValue(GetMainForm()->Handle, OverallProgress, 100);
  784. }
  785. else
  786. {
  787. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  788. }
  789. }
  790. //---------------------------------------------------------------------------
  791. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  792. {
  793. TTerminalManager::Instance()->QueueStatusUpdated();
  794. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  795. {
  796. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  797. {
  798. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  799. {
  800. TFileOperationProgressType * ProgressData = NULL;
  801. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  802. {
  803. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  804. }
  805. if ((ProgressData != NULL) &&
  806. ProgressData->InProgress)
  807. {
  808. SetTaskbarListProgressValue(ProgressData);
  809. }
  810. else
  811. {
  812. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  813. }
  814. }
  815. else
  816. {
  817. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  818. }
  819. }
  820. else
  821. {
  822. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  823. }
  824. }
  825. UpdateQueueLabel();
  826. }
  827. //---------------------------------------------------------------------------
  828. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  829. {
  830. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  831. if (FQueueStatus != NULL)
  832. {
  833. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  834. if (ActiveAndPendingPrimaryCount > 0)
  835. {
  836. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  837. }
  838. }
  839. QueueLabel->Caption = Caption;
  840. }
  841. //---------------------------------------------------------------------------
  842. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  843. {
  844. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  845. static_cast<TComponent*>(GetComponent(fcTransferList)));
  846. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  847. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  848. TransferList->Strings->BeginUpdate();
  849. try
  850. {
  851. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  852. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  853. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  854. if (FTransferDropDownHint.IsEmpty())
  855. {
  856. FTransferDropDownHint = TransferDropDown->Hint;
  857. }
  858. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  859. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  860. TransferDropDown->Text = StripHotkey(Name);
  861. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  862. (FTransferDropDownHint, StripHotkey(Name),
  863. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  864. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General))));
  865. // update the label, otherwise when it is updated only on the first draw
  866. // of the list, it is drawn "bold" for some reason
  867. FTransferListHoverIndex = TransferList->ItemIndex;
  868. UpdateTransferLabel();
  869. }
  870. __finally
  871. {
  872. TransferList->Strings->EndUpdate();
  873. }
  874. }
  875. //---------------------------------------------------------------------------
  876. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  877. {
  878. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  879. DebugAssert(Toolbar != NULL);
  880. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  881. }
  882. //---------------------------------------------------------------------------
  883. void __fastcall TCustomScpExplorerForm::UpdateActions()
  884. {
  885. TForm::UpdateActions();
  886. if (ComponentVisible[fcCustomCommandsBand])
  887. {
  888. UpdateCustomCommandsToolbar();
  889. }
  890. }
  891. //---------------------------------------------------------------------------
  892. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  893. {
  894. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  895. }
  896. //---------------------------------------------------------------------------
  897. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  898. {
  899. Color = GetBtnFaceColor();
  900. DebugAssert(Configuration && RemoteDirView);
  901. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  902. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  903. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  904. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  905. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  906. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  907. {
  908. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  909. // selection is not redrawn automatically when RowSelect changes
  910. RemoteDirView->Invalidate();
  911. }
  912. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  913. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  914. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  915. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  916. UpdateSessionsPageControlHeight();
  917. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  918. UpdateToolbarDisplayMode();
  919. UpdateTransferList();
  920. if (Terminal != NULL)
  921. {
  922. TransferPresetAutoSelect();
  923. }
  924. UpdateQueueView();
  925. UpdateControls();
  926. // this can be called even before constuctor finishes.
  927. if (FEditorManager != NULL)
  928. {
  929. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  930. }
  931. // this can be called even before constuctor finishes.
  932. if (FLocalEditors != NULL)
  933. {
  934. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  935. {
  936. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  937. }
  938. }
  939. if (ComponentVisible[fcCustomCommandsBand])
  940. {
  941. UpdateCustomCommandsToolbar();
  942. }
  943. // show only when keeping queue items forever,
  944. // otherwise, its enough to have in in the context menu
  945. QueueDeleteAllDoneQueueToolbarItem->Visible =
  946. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  947. if (FFileColorsCurrent != WinConfiguration->FileColors)
  948. {
  949. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  950. FFileColorsCurrent = WinConfiguration->FileColors;
  951. FileColorsChanged();
  952. }
  953. }
  954. //---------------------------------------------------------------------------
  955. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  956. {
  957. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  958. DirView->Invalidate();
  959. }
  960. //---------------------------------------------------------------------------
  961. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  962. {
  963. DoFileColorsChanged(RemoteDirView);
  964. }
  965. //---------------------------------------------------------------------------
  966. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  967. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  968. void * /*Arg*/)
  969. {
  970. if (Token != NULL)
  971. {
  972. TForm * Editor = dynamic_cast<TForm*>(Token);
  973. DebugAssert(Editor != NULL);
  974. ReconfigureEditorForm(Editor);
  975. }
  976. }
  977. //---------------------------------------------------------------------------
  978. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  979. {
  980. WinConfiguration->DDTransferConfirmation = asOn;
  981. }
  982. //---------------------------------------------------------------------------
  983. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  984. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  985. {
  986. // noop
  987. }
  988. //---------------------------------------------------------------------------
  989. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  990. TTransferDirection Direction, TTransferType Type, bool Temp,
  991. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  992. bool Confirm, bool DragDrop, int Options)
  993. {
  994. bool Result = true;
  995. DebugAssert(Terminal && Terminal->Active);
  996. // these parameters are known in advance
  997. int Params =
  998. FLAGMASK(Type == ttMove, cpDelete);
  999. bool ToTemp = (Temp && (Direction == tdToLocal));
  1000. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1001. {
  1002. bool DisableNewerOnly =
  1003. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1004. ToTemp;
  1005. Options |=
  1006. FLAGMASK(ToTemp, coTemp) |
  1007. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  1008. coDoNotShowAgain;
  1009. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1010. int CopyParamAttrs = (Direction == tdToRemote ?
  1011. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1012. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1013. int OutputOptions =
  1014. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1015. cooDoNotShowAgain);
  1016. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1017. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1018. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1019. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions);
  1020. if (Result)
  1021. {
  1022. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1023. {
  1024. if (DragDrop)
  1025. {
  1026. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1027. {
  1028. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1029. NULL, 0, EnableDDTransferConfirmation, NULL);
  1030. }
  1031. WinConfiguration->DDTransferConfirmation = asOff;
  1032. }
  1033. else
  1034. {
  1035. WinConfiguration->ConfirmTransferring = false;
  1036. }
  1037. }
  1038. else
  1039. {
  1040. // User explicitly unchecked "do not show again",
  1041. // so show him the dialog the next time
  1042. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1043. {
  1044. WinConfiguration->DDTransferConfirmation = asOn;
  1045. }
  1046. }
  1047. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1048. }
  1049. }
  1050. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1051. {
  1052. Configuration->Usage->Inc(L"TransfersOnBackground");
  1053. // these parameter are known only after transfer dialog
  1054. Params |=
  1055. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1056. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1057. Result = false;
  1058. ClearTransferSourceSelection(Direction);
  1059. }
  1060. return Result;
  1061. }
  1062. //---------------------------------------------------------------------------
  1063. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1064. {
  1065. if (FOnFileOperationFinished != NULL)
  1066. {
  1067. FOnFileOperationFinished(UnicodeString(), true);
  1068. }
  1069. else
  1070. {
  1071. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1072. if (HasDirView[Side])
  1073. {
  1074. DirView(Side)->SelectAll(smNone);
  1075. }
  1076. }
  1077. }
  1078. //---------------------------------------------------------------------------
  1079. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1080. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1081. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1082. int Params)
  1083. {
  1084. DebugAssert(Queue != NULL);
  1085. bool SingleFile = false;
  1086. if (FileList->Count == 1)
  1087. {
  1088. if (Direction == tdToRemote)
  1089. {
  1090. UnicodeString FileName = FileList->Strings[0];
  1091. SingleFile = FileExists(ApiPath(FileName));
  1092. }
  1093. else
  1094. {
  1095. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1096. SingleFile = !File->IsDirectory;
  1097. }
  1098. }
  1099. TQueueItem * QueueItem;
  1100. if (Direction == tdToRemote)
  1101. {
  1102. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1103. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1104. }
  1105. else
  1106. {
  1107. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1108. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1109. }
  1110. AddQueueItem(Queue, QueueItem, Terminal);
  1111. }
  1112. //---------------------------------------------------------------------------
  1113. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TTerminal * Terminal)
  1114. {
  1115. if (Queue->IsEmpty)
  1116. {
  1117. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  1118. ManagedTerminal->QueueOperationStart = Now();
  1119. }
  1120. Queue->AddItem(QueueItem);
  1121. }
  1122. //---------------------------------------------------------------------------
  1123. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1124. {
  1125. }
  1126. //---------------------------------------------------------------------------
  1127. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1128. {
  1129. DebugAssert(FDocks != NULL);
  1130. for (int Index = 0; Index < ComponentCount; Index++)
  1131. {
  1132. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1133. if ((Dock != NULL) && (Dock->Tag == 0))
  1134. {
  1135. FDocks->Add(Dock);
  1136. }
  1137. }
  1138. CollectItemsWithTextDisplayMode(this);
  1139. ConfigurationChanged();
  1140. QueuePanel->Height =
  1141. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1142. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1143. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1144. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1145. }
  1146. //---------------------------------------------------------------------------
  1147. void __fastcall TCustomScpExplorerForm::StoreParams()
  1148. {
  1149. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1150. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1151. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1152. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1153. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1154. }
  1155. //---------------------------------------------------------------------------
  1156. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1157. {
  1158. if (!FFormRestored)
  1159. {
  1160. FFormRestored = true;
  1161. RestoreFormParams();
  1162. }
  1163. TForm::CreateParams(Params);
  1164. }
  1165. //---------------------------------------------------------------------------
  1166. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1167. {
  1168. DebugAssert(FDocks != NULL);
  1169. for (int Index = 0; Index < FDocks->Count; Index++)
  1170. {
  1171. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1172. }
  1173. }
  1174. //---------------------------------------------------------------------------
  1175. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1176. {
  1177. SetDockAllowDrag(true);
  1178. while (!ButtonsStr.IsEmpty())
  1179. {
  1180. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1181. UnicodeString Name = CutToChar(S, L'=', true);
  1182. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1183. {
  1184. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1185. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1186. {
  1187. while (!S.IsEmpty())
  1188. {
  1189. UnicodeString S2 = CutToChar(S, L',', true);
  1190. UnicodeString Name = CutToChar(S2, L':', true);
  1191. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1192. {
  1193. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1194. if (SameText(GetToolbarItemName(Item), Name))
  1195. {
  1196. // forward compatibility
  1197. UnicodeString S3 = CutToChar(S2, L':', true);
  1198. bool Visible = StrToIntDef(S3, 0);
  1199. Item->Visible = Visible;
  1200. }
  1201. }
  1202. }
  1203. }
  1204. }
  1205. }
  1206. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1207. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1208. }
  1209. //---------------------------------------------------------------------------
  1210. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1211. {
  1212. UnicodeString Result;
  1213. SetDockAllowDrag(true);
  1214. Result = ::GetToolbarsLayoutStr(this);
  1215. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1216. return Result;
  1217. }
  1218. //---------------------------------------------------------------------------
  1219. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1220. {
  1221. UnicodeString Result;
  1222. if (Item->Action != NULL)
  1223. {
  1224. Result = Item->Action->Name;
  1225. Result = RemoveSuffix(Result, L"Action");
  1226. }
  1227. else
  1228. {
  1229. Result = Item->Name;
  1230. Result = RemoveSuffix(Result, L"SubmenuItem");
  1231. Result = RemoveSuffix(Result, L"Item");
  1232. }
  1233. return Result;
  1234. }
  1235. //---------------------------------------------------------------------------
  1236. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1237. {
  1238. UnicodeString Result;
  1239. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1240. {
  1241. UnicodeString ToolbarStr;
  1242. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1243. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1244. {
  1245. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1246. {
  1247. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1248. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1249. // Once we introduce any buttons that aer hidden by default, we would have to remember their initial state
  1250. // and save the changes here only.
  1251. if (!Item->Visible)
  1252. {
  1253. UnicodeString Name = GetToolbarItemName(Item);
  1254. DebugAssert(Name.Pos(L"TBX") == 0);
  1255. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1256. }
  1257. }
  1258. }
  1259. if (!ToolbarStr.IsEmpty())
  1260. {
  1261. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1262. }
  1263. }
  1264. return Result;
  1265. }
  1266. //---------------------------------------------------------------------------
  1267. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1268. {
  1269. DebugAssert(FProgressForm == NULL);
  1270. bool AllowSkip = (Terminal != NULL) ? Terminal->IsCapable[fsSkipTransfer] : false;
  1271. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1272. FProgressForm->DeleteLocalToRecycleBin =
  1273. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1274. FProgressForm->DeleteRemoteToRecycleBin =
  1275. (Terminal != NULL) &&
  1276. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1277. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1278. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1279. LockWindow();
  1280. if (FTaskbarList != NULL)
  1281. {
  1282. // Actually, do not know what hides the progress once the operation finishes
  1283. SetTaskbarListProgressState(TBPF_NORMAL);
  1284. }
  1285. }
  1286. //---------------------------------------------------------------------------
  1287. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1288. {
  1289. UnlockWindow();
  1290. SAFE_DESTROY(FProgressForm);
  1291. SetQueueProgress();
  1292. }
  1293. //---------------------------------------------------------------------------
  1294. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1295. TFileOperationProgressType & ProgressData)
  1296. {
  1297. // Download to temporary local directory
  1298. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1299. {
  1300. bool Continue = true;
  1301. FPendingTempSpaceWarn = false;
  1302. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1303. if (!Continue)
  1304. {
  1305. Abort();
  1306. }
  1307. }
  1308. // operation is being executed and we still didn't show up progress form
  1309. if (ProgressData.InProgress && !FProgressForm)
  1310. {
  1311. CreateProgressForm(NULL);
  1312. // When main window is hidden, synchronisation form does not exist (we do not get here during synchronization - only comparing),
  1313. // we suppose "/upload" or URL download mode
  1314. if (!Visible && (ProgressData.Operation != foCalculateSize) &&
  1315. (ProgressData.Operation != foCalculateChecksum) &&
  1316. // Hack to prevent renames from DoSynchronizeMove (synchronize checklist) closing the connection
  1317. (ProgressData.Operation != foRemoteMove) &&
  1318. (FSynchronizeProgressForm == NULL) &&
  1319. !FStandaloneEditing)
  1320. {
  1321. FProgressForm->OnceDoneOperation = odoDisconnect;
  1322. }
  1323. }
  1324. // operation is finished (or terminated), so we hide progress form
  1325. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1326. {
  1327. DestroyProgressForm();
  1328. if (ProgressData.Operation == foCalculateSize)
  1329. {
  1330. // When calculating size before transfer, the abort caused by
  1331. // cancel flag set due to "MoveToQueue" below,
  1332. // is not propagated back to us in ExecuteFileOperation,
  1333. // as it is silently swallowed.
  1334. // So we explicitly (re)throw it here.
  1335. if (FMoveToQueue)
  1336. {
  1337. Abort();
  1338. }
  1339. }
  1340. else
  1341. {
  1342. if ((ProgressData.Cancel == csContinue) &&
  1343. !FAutoOperation)
  1344. {
  1345. OperationComplete(ProgressData.StartTime);
  1346. }
  1347. }
  1348. }
  1349. if (FProgressForm &&
  1350. // Can happen only during synchronization, when ending one of the operations
  1351. ProgressData.InProgress)
  1352. {
  1353. FProgressForm->SetProgressData(ProgressData);
  1354. if (FTaskbarList != NULL)
  1355. {
  1356. DebugAssert(ProgressData.InProgress);
  1357. SetTaskbarListProgressValue(&ProgressData);
  1358. }
  1359. if (FProgressForm->Cancel > csContinue)
  1360. {
  1361. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1362. if (FProgressForm->Cancel == csCancelFile)
  1363. {
  1364. FProgressForm->ClearCancel();
  1365. }
  1366. // cancel cancels even the move
  1367. FMoveToQueue = false;
  1368. }
  1369. else if (FProgressForm->MoveToQueue)
  1370. {
  1371. FMoveToQueue = true;
  1372. ProgressData.SetCancelAtLeast(csCancel);
  1373. }
  1374. if ((FTransferResumeList != NULL) &&
  1375. ProgressData.InProgress &&
  1376. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1377. !ProgressData.FullFileName.IsEmpty())
  1378. {
  1379. if ((FTransferResumeList->Count == 0) ||
  1380. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1381. {
  1382. // note that we do not recognize directories from files here
  1383. FTransferResumeList->Add(ProgressData.FullFileName);
  1384. }
  1385. }
  1386. }
  1387. }
  1388. //---------------------------------------------------------------------------
  1389. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1390. const TDateTime & StartTime)
  1391. {
  1392. if (GUIConfiguration->BeepOnFinish &&
  1393. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1394. {
  1395. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1396. DWORD Sound;
  1397. if (!ExtractFileExt(BeepSound).IsEmpty())
  1398. {
  1399. Sound = SND_FILENAME;
  1400. }
  1401. else
  1402. {
  1403. Sound = SND_ALIAS;
  1404. }
  1405. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1406. }
  1407. }
  1408. //---------------------------------------------------------------------------
  1409. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1410. TFileOperationProgressType & ProgressData)
  1411. {
  1412. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1413. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1414. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1415. FileOperationProgress(ProgressData);
  1416. }
  1417. //---------------------------------------------------------------------------
  1418. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1419. {
  1420. UnicodeString Result;
  1421. if (FProgressForm != NULL)
  1422. {
  1423. Result = FProgressForm->ProgressStr();
  1424. }
  1425. return Result;
  1426. }
  1427. //---------------------------------------------------------------------------
  1428. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1429. bool DragDrop)
  1430. {
  1431. return (!DragDrop && (DropSourceControl == NULL)) ||
  1432. (DropSourceControl == DirView(osOther));
  1433. }
  1434. //---------------------------------------------------------------------------
  1435. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1436. TFileOperation Operation, TOperationSide Side,
  1437. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1438. TOnceDoneOperation & OnceDoneOperation)
  1439. {
  1440. if (!FAutoOperation)
  1441. {
  1442. // no selection on "/upload", form servers only as event handler
  1443. // (it is not displayed)
  1444. if (PanelOperation(Side, FDragDropOperation) &&
  1445. Visible && (Operation != foCalculateSize) &&
  1446. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1447. {
  1448. if (FOnFileOperationFinished != NULL)
  1449. {
  1450. FOnFileOperationFinished(FileName, Success);
  1451. }
  1452. else
  1453. {
  1454. TCustomDirView * DView = DirView(Side);
  1455. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1456. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1457. // this can happen when local drive is unplugged in the middle of the operation
  1458. if (Item != NULL)
  1459. {
  1460. if (Success) Item->Selected = false;
  1461. DView->MakeProgressVisible(Item);
  1462. }
  1463. }
  1464. }
  1465. if ((Operation == foCopy) || (Operation == foMove))
  1466. {
  1467. DebugAssert(!IsLocalBrowserMode()); // TODO
  1468. if (Side == osLocal)
  1469. {
  1470. Configuration->Usage->Inc(L"UploadedFiles");
  1471. }
  1472. else
  1473. {
  1474. Configuration->Usage->Inc(L"DownloadedFiles");
  1475. }
  1476. }
  1477. }
  1478. if (Success && (FSynchronizeController != NULL))
  1479. {
  1480. DebugAssert(!IsSideLocalBrowser(Side));
  1481. if (Operation == foCopy)
  1482. {
  1483. DebugAssert(Side == osLocal);
  1484. FSynchronizeController->LogOperation(soUpload, FileName);
  1485. }
  1486. else if (Operation == foDelete)
  1487. {
  1488. DebugAssert(Side == osRemote);
  1489. FSynchronizeController->LogOperation(soDelete, FileName);
  1490. }
  1491. }
  1492. if (FProgressForm)
  1493. {
  1494. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1495. }
  1496. }
  1497. //---------------------------------------------------------------------------
  1498. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1499. TFileOperation Operation, TOperationSide Side,
  1500. bool Temp, const UnicodeString & FileName, Boolean Success,
  1501. TOnceDoneOperation & OnceDoneOperation)
  1502. {
  1503. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1504. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1505. OnceDoneOperation);
  1506. }
  1507. //---------------------------------------------------------------------------
  1508. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1509. {
  1510. return false;
  1511. }
  1512. //---------------------------------------------------------------------------
  1513. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1514. {
  1515. return false;
  1516. }
  1517. //---------------------------------------------------------------------------
  1518. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1519. {
  1520. return NULL;
  1521. }
  1522. //---------------------------------------------------------------------------
  1523. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1524. {
  1525. DebugAssert(GetSide(Side) == osRemote);
  1526. DebugUsedParam(Side);
  1527. return RemoteDirView;
  1528. }
  1529. //---------------------------------------------------------------------------
  1530. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1531. {
  1532. DebugAssert(GetSide(Side) == osRemote);
  1533. DebugUsedParam(Side);
  1534. return RemoteDriveView;
  1535. }
  1536. //---------------------------------------------------------------------------
  1537. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1538. {
  1539. DebugAssert(GetSide(Side) == osRemote);
  1540. DebugUsedParam(Side);
  1541. return (Terminal != NULL);
  1542. }
  1543. //---------------------------------------------------------------------------
  1544. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1545. TOperationSide Side, int FilesOnly)
  1546. {
  1547. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1548. }
  1549. //---------------------------------------------------------------------------
  1550. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1551. TOperationSide Side, int FilesOnly)
  1552. {
  1553. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1554. }
  1555. //---------------------------------------------------------------------------
  1556. struct THistoryItemData
  1557. {
  1558. short int Side;
  1559. short int Index;
  1560. };
  1561. //---------------------------------------------------------------------------
  1562. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1563. {
  1564. DirView(Side)->HistoryGo(Index);
  1565. }
  1566. //---------------------------------------------------------------------------
  1567. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1568. {
  1569. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1570. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1571. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1572. }
  1573. //---------------------------------------------------------------------------
  1574. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1575. bool Back)
  1576. {
  1577. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1578. {
  1579. TCustomDirView * DView = DirView(Side);
  1580. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1581. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1582. if (ICount > 10)
  1583. {
  1584. ICount = 10;
  1585. }
  1586. Menu->Items->Clear();
  1587. THistoryItemData Data;
  1588. Data.Side = Side;
  1589. for (short int i = 1; i <= ICount; i++)
  1590. {
  1591. TTBCustomItem * Item = new TTBXItem(Menu);
  1592. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1593. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1594. Item->Hint = DView->HistoryPath[Data.Index];
  1595. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1596. Item->Tag = *reinterpret_cast<int*>(&Data);
  1597. Item->OnClick = HistoryItemClick;
  1598. Menu->Items->Add(Item);
  1599. }
  1600. }
  1601. }
  1602. //---------------------------------------------------------------------------
  1603. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1604. TOperationSide Side, bool Back)
  1605. {
  1606. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1607. {
  1608. // workaround
  1609. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1610. // operator new allocates memory in Pascal code, but calls inline
  1611. // contructor in C++, leading in problems, the function does
  1612. // both in Pascal code
  1613. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1614. UpdateHistoryMenu(Side, Back);
  1615. }
  1616. return FHistoryMenu[Side == osLocal][Back];
  1617. }
  1618. //---------------------------------------------------------------------------
  1619. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1620. TCustomDirView *Sender)
  1621. {
  1622. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1623. UpdateHistoryMenu(Side, true);
  1624. UpdateHistoryMenu(Side, false);
  1625. }
  1626. //---------------------------------------------------------------------------
  1627. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1628. {
  1629. // remote custom commands can be executed only if the server supports shell commands
  1630. // or have secondary shell
  1631. return (FTerminal != NULL) && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1632. }
  1633. //---------------------------------------------------------------------------
  1634. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1635. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1636. {
  1637. int Result;
  1638. TFileCustomCommand RemoteCustomCommand;
  1639. TLocalCustomCommand LocalCustomCommand;
  1640. TFileCustomCommand * NonInteractiveCustomCommand =
  1641. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1642. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1643. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1644. if (FLAGCLEAR(Command.Params, ccLocal))
  1645. {
  1646. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1647. // custom command that does not operate with files can be executed anytime ...
  1648. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1649. {
  1650. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1651. {
  1652. // ... but do not show such command in remote file menu (TODO)
  1653. Result = AllowedState;
  1654. }
  1655. else
  1656. {
  1657. Result = -1;
  1658. }
  1659. }
  1660. else
  1661. {
  1662. if ((ListType == ccltAll) || (ListType == ccltFile))
  1663. {
  1664. Result = (!IsSideLocalBrowser(FCurrentSide) && DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1665. }
  1666. else
  1667. {
  1668. Result = -1;
  1669. }
  1670. }
  1671. }
  1672. else
  1673. {
  1674. // custom command that does not operate with files can be executed anytime
  1675. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1676. {
  1677. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1678. }
  1679. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1680. {
  1681. if ((ListType == ccltAll) || (ListType == ccltFile))
  1682. {
  1683. // special case is "diff"-style command that can be executed over any side,
  1684. // if we have both sides
  1685. Result =
  1686. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1687. // directly (instead of EnableSelectedOperation) to pass
  1688. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1689. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1690. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1691. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1692. }
  1693. else if (ListType == ccltBoth)
  1694. {
  1695. DebugAssert(!IsLocalBrowserMode());
  1696. Result = 1;
  1697. }
  1698. else
  1699. {
  1700. Result = -1;
  1701. }
  1702. }
  1703. else
  1704. {
  1705. if ((ListType == ccltAll) || (ListType == ccltFile))
  1706. {
  1707. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1708. }
  1709. else
  1710. {
  1711. Result = -1;
  1712. }
  1713. }
  1714. }
  1715. return Result;
  1716. }
  1717. //---------------------------------------------------------------------------
  1718. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1719. TStrings * FileList, const TCustomCommandType & ACommand,
  1720. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1721. {
  1722. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1723. {
  1724. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1725. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1726. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1727. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1728. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1729. bool Capture =
  1730. FLAGSET(ACommand.Params, ccShowResults) ||
  1731. FLAGSET(ACommand.Params, ccCopyResults) ||
  1732. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1733. TCaptureOutputEvent OutputEvent = NULL;
  1734. DebugAssert(FCapturedLog == NULL);
  1735. if (Capture)
  1736. {
  1737. FCapturedLog = new TStringList();
  1738. OutputEvent = TerminalCaptureLog;
  1739. }
  1740. try
  1741. {
  1742. if (!RemoteCustomCommand.IsFileCommand(Command))
  1743. {
  1744. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1745. OutputEvent);
  1746. }
  1747. else
  1748. {
  1749. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1750. }
  1751. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1752. {
  1753. if (FLAGSET(ACommand.Params, ccCopyResults))
  1754. {
  1755. CopyToClipboard(FCapturedLog);
  1756. }
  1757. if (FLAGSET(ACommand.Params, ccShowResults))
  1758. {
  1759. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1760. }
  1761. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1762. {
  1763. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1764. }
  1765. }
  1766. }
  1767. __finally
  1768. {
  1769. SAFE_DESTROY(FCapturedLog);
  1770. }
  1771. }
  1772. }
  1773. //---------------------------------------------------------------------------
  1774. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1775. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1776. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1777. {
  1778. DebugAssert(!IsLocalBrowserMode());
  1779. TStrings * LocalFileList = NULL;
  1780. TStrings * RemoteFileList = NULL;
  1781. TStrings * RemoteFileListFull = NULL;
  1782. try
  1783. {
  1784. if (LocalFileCommand)
  1785. {
  1786. if (ALocalFileList == NULL)
  1787. {
  1788. DebugAssert(HasDirView[osLocal]);
  1789. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1790. // directly (instead of EnableSelectedOperation) to pass
  1791. // false to FocusedFileOnlyWhenFocused
  1792. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1793. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1794. }
  1795. else
  1796. {
  1797. LocalFileList = ALocalFileList;
  1798. }
  1799. if (FileListCommand)
  1800. {
  1801. if (LocalFileList->Count != 1)
  1802. {
  1803. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1804. }
  1805. }
  1806. else
  1807. {
  1808. if ((LocalFileList->Count != 1) &&
  1809. (FileList->Count != 1) &&
  1810. (LocalFileList->Count != FileList->Count))
  1811. {
  1812. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1813. }
  1814. }
  1815. }
  1816. UnicodeString RootTempDir;
  1817. UnicodeString TempDir;
  1818. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1819. if (!RemoteFiles)
  1820. {
  1821. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1822. }
  1823. try
  1824. {
  1825. TDateTimes RemoteFileTimes;
  1826. if (RemoteFiles)
  1827. {
  1828. RemoteFileList = FileList;
  1829. }
  1830. else
  1831. {
  1832. RemoteFileList = new TStringList();
  1833. TMakeLocalFileListParams MakeFileListParam;
  1834. MakeFileListParam.FileList = RemoteFileList;
  1835. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1836. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1837. MakeFileListParam.Recursive =
  1838. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1839. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1840. if (!MakeFileListParam.Recursive)
  1841. {
  1842. RemoteFileTimes.clear();
  1843. RemoteFileListFull = new TStringList();
  1844. MakeFileListParam.FileList = RemoteFileListFull;
  1845. MakeFileListParam.Recursive = true;
  1846. MakeFileListParam.IncludeDirs = false;
  1847. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1848. }
  1849. else
  1850. {
  1851. RemoteFileListFull = RemoteFileList;
  1852. }
  1853. }
  1854. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1855. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1856. if (!NonBlocking)
  1857. {
  1858. int Count;
  1859. if (FileListCommand)
  1860. {
  1861. Count = 1;
  1862. }
  1863. else if (LocalFileCommand)
  1864. {
  1865. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1866. }
  1867. else
  1868. {
  1869. Count = RemoteFileList->Count;
  1870. }
  1871. Progress.Start(foCustomCommand, osRemote, Count);
  1872. DebugAssert(FProgressForm != NULL);
  1873. FProgressForm->ReadOnly = true;
  1874. }
  1875. try
  1876. {
  1877. if (FileListCommand)
  1878. {
  1879. UnicodeString LocalFile;
  1880. // MakeFileList does not delimit filenames
  1881. UnicodeString FileList = MakeFileList(RemoteFileList);
  1882. if (LocalFileCommand)
  1883. {
  1884. DebugAssert(LocalFileList->Count == 1);
  1885. LocalFile = LocalFileList->Strings[0];
  1886. }
  1887. TLocalCustomCommand CustomCommand(Data,
  1888. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1889. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1890. if (NonBlocking)
  1891. {
  1892. DebugAssert(!POutput);
  1893. ExecuteShellChecked(ShellCommand);
  1894. }
  1895. else
  1896. {
  1897. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1898. }
  1899. }
  1900. else if (LocalFileCommand)
  1901. {
  1902. if (LocalFileList->Count == 1)
  1903. {
  1904. UnicodeString LocalFile = LocalFileList->Strings[0];
  1905. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1906. {
  1907. UnicodeString FileName = RemoteFileList->Strings[Index];
  1908. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1909. Progress.SetFile(FileNameForProgress);
  1910. TLocalCustomCommand CustomCommand(Data,
  1911. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1912. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1913. TOnceDoneOperation OnceDoneOperation;
  1914. // Do not unselect anything, as it won't work with two panels anyway
  1915. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1916. }
  1917. }
  1918. else if (RemoteFileList->Count == 1)
  1919. {
  1920. UnicodeString FileName = RemoteFileList->Strings[0];
  1921. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1922. {
  1923. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1924. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1925. Progress.SetFile(LocalFileNameOnly);
  1926. TLocalCustomCommand CustomCommand(
  1927. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1928. FileName, LocalFileName, L"");
  1929. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1930. TOnceDoneOperation OnceDoneOperation;
  1931. // Do not unselect anything, as it won't work with two panels anyway
  1932. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1933. }
  1934. }
  1935. else
  1936. {
  1937. if (LocalFileList->Count != RemoteFileList->Count)
  1938. {
  1939. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1940. }
  1941. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1942. {
  1943. UnicodeString FileName = RemoteFileList->Strings[Index];
  1944. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1945. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1946. Progress.SetFile(LocalFileNameOnly); // sic
  1947. TLocalCustomCommand CustomCommand(
  1948. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1949. FileName, LocalFileName, L"");
  1950. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1951. TOnceDoneOperation OnceDoneOperation;
  1952. // Do not unselect anything, as it won't work with two panels anyway
  1953. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1954. }
  1955. }
  1956. }
  1957. else
  1958. {
  1959. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1960. {
  1961. UnicodeString FileName = RemoteFileList->Strings[Index];
  1962. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1963. Progress.SetFile(FileNameForProgress);
  1964. TLocalCustomCommand CustomCommand(Data,
  1965. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1966. FileName, L"", L"");
  1967. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1968. TOnceDoneOperation OnceDoneOperation;
  1969. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  1970. }
  1971. }
  1972. }
  1973. __finally
  1974. {
  1975. if (!NonBlocking)
  1976. {
  1977. Progress.Stop();
  1978. }
  1979. }
  1980. DebugAssert(!FAutoOperation);
  1981. if (!RemoteFiles)
  1982. {
  1983. TempDir = IncludeTrailingBackslash(TempDir);
  1984. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1985. {
  1986. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1987. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1988. // Skip directories, process only nested files.
  1989. // The check is redundant as FileAge fails for directories anyway.
  1990. !DirectoryExists(FileName))
  1991. {
  1992. UnicodeString RemoteDir =
  1993. UnixExtractFileDir(
  1994. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  1995. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  1996. TDateTime NewTime;
  1997. if (FileAge(FileName, NewTime) &&
  1998. (NewTime != RemoteFileTimes[Index]))
  1999. {
  2000. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2001. TemporaryFileCopyParam(CopyParam);
  2002. CopyParam.FileMask = L"";
  2003. FAutoOperation = true;
  2004. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2005. TemporaryFilesList->Add(FileName);
  2006. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2007. }
  2008. }
  2009. }
  2010. }
  2011. }
  2012. __finally
  2013. {
  2014. FAutoOperation = false;
  2015. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2016. {
  2017. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2018. }
  2019. }
  2020. }
  2021. __finally
  2022. {
  2023. if (RemoteFileList != FileList)
  2024. {
  2025. delete RemoteFileList;
  2026. }
  2027. if (LocalFileList != ALocalFileList)
  2028. {
  2029. delete LocalFileList;
  2030. }
  2031. if (RemoteFileListFull != RemoteFileList)
  2032. {
  2033. delete RemoteFileListFull;
  2034. }
  2035. }
  2036. }
  2037. //---------------------------------------------------------------------------
  2038. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2039. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2040. bool FileListCommand, UnicodeString * POutput)
  2041. {
  2042. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  2043. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2044. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2045. {
  2046. UnicodeString FileName = SelectedFileList->Strings[Index];
  2047. if (DirectoryExists(FileName))
  2048. {
  2049. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2050. {
  2051. LocalFileList->Add(FileName);
  2052. }
  2053. if (FLAGSET(ACommand.Params, ccRecursive))
  2054. {
  2055. TMakeLocalFileListParams MakeFileListParam;
  2056. MakeFileListParam.FileList = LocalFileList.get();
  2057. MakeFileListParam.FileTimes = NULL;
  2058. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2059. MakeFileListParam.Recursive = true;
  2060. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2061. }
  2062. }
  2063. else
  2064. {
  2065. LocalFileList->Add(FileName);
  2066. }
  2067. }
  2068. UnicodeString RemotePath;
  2069. if (Terminal != NULL)
  2070. {
  2071. RemotePath = Terminal->CurrentDirectory;
  2072. }
  2073. if (FileListCommand)
  2074. {
  2075. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2076. TLocalCustomCommand CustomCommand(
  2077. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2078. L"", L"", FileList);
  2079. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2080. }
  2081. else
  2082. {
  2083. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2084. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2085. DebugAssert(FProgressForm != NULL);
  2086. FProgressForm->ReadOnly = true;
  2087. try
  2088. {
  2089. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2090. {
  2091. UnicodeString FileName = LocalFileList->Strings[Index];
  2092. Progress.SetFile(FileName);
  2093. TLocalCustomCommand CustomCommand(
  2094. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2095. FileName, L"", L"");
  2096. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2097. TOnceDoneOperation OnceDoneOperation;
  2098. Progress.Finish(FileName, true, OnceDoneOperation);
  2099. }
  2100. }
  2101. __finally
  2102. {
  2103. Progress.Stop();
  2104. }
  2105. }
  2106. }
  2107. //---------------------------------------------------------------------------
  2108. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2109. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2110. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2111. {
  2112. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2113. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2114. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2115. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2116. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2117. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2118. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2119. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2120. std::unique_ptr<UnicodeString> POutput;
  2121. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2122. FLAGSET(ACommand.Params, ccCopyResults))
  2123. {
  2124. POutput.reset(new UnicodeString());
  2125. }
  2126. if (!LocalCustomCommand.IsFileCommand(Command))
  2127. {
  2128. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2129. }
  2130. // remote files?
  2131. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2132. {
  2133. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2134. }
  2135. // local files
  2136. else
  2137. {
  2138. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2139. }
  2140. if (POutput.get() != NULL)
  2141. {
  2142. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2143. int P = POutput->Pos(sLineBreak);
  2144. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2145. {
  2146. POutput->SetLength(P - 1);
  2147. }
  2148. // Copy even empty output to clipboard
  2149. if (FLAGSET(ACommand.Params, ccCopyResults))
  2150. {
  2151. CopyToClipboard(*POutput);
  2152. }
  2153. // But do not show an empty message box.
  2154. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2155. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2156. !POutput->IsEmpty())
  2157. {
  2158. TClipboardHandler ClipboardHandler;
  2159. ClipboardHandler.Text = *POutput;
  2160. TMessageParams Params;
  2161. TQueryButtonAlias Aliases[1];
  2162. Aliases[0].Button = qaRetry;
  2163. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2164. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2165. Params.Aliases = Aliases;
  2166. Params.AliasesCount = LENOF(Aliases);
  2167. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2168. }
  2169. }
  2170. }
  2171. //---------------------------------------------------------------------------
  2172. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2173. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2174. {
  2175. TCustomCommandData Data;
  2176. UnicodeString Site;
  2177. UnicodeString RemotePath;
  2178. if (Terminal != NULL)
  2179. {
  2180. Data = TCustomCommandData(Terminal);
  2181. Site = Terminal->SessionData->SessionKey;
  2182. RemotePath = Terminal->CurrentDirectory;
  2183. }
  2184. UnicodeString HelpKeyword = ACommand.HomePage;
  2185. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2186. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2187. {
  2188. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2189. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2190. {
  2191. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2192. }
  2193. else
  2194. {
  2195. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2196. }
  2197. if (!DoCustomCommandOptionsDialog(
  2198. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2199. {
  2200. Abort();
  2201. }
  2202. }
  2203. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2204. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2205. {
  2206. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2207. }
  2208. else
  2209. {
  2210. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2211. }
  2212. }
  2213. //---------------------------------------------------------------------------
  2214. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2215. const TCustomCommandType & Command)
  2216. {
  2217. DebugAssert(FCustomCommandLocalFileList != NULL);
  2218. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2219. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2220. TStrings * LocalFileList = new TStringList();
  2221. TStrings * RemoteFileList = new TStringList();
  2222. try
  2223. {
  2224. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2225. {
  2226. LocalFileList->Clear();
  2227. LocalFileList->AddObject(
  2228. FCustomCommandLocalFileList->Strings[Index],
  2229. FCustomCommandLocalFileList->Objects[Index]);
  2230. RemoteFileList->Clear();
  2231. RemoteFileList->AddObject(
  2232. FCustomCommandRemoteFileList->Strings[Index],
  2233. FCustomCommandRemoteFileList->Objects[Index]);
  2234. CustomCommand(RemoteFileList, Command, LocalFileList);
  2235. }
  2236. }
  2237. __finally
  2238. {
  2239. delete LocalFileList;
  2240. delete RemoteFileList;
  2241. }
  2242. }
  2243. //---------------------------------------------------------------------------
  2244. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2245. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2246. {
  2247. delete FCustomCommandLocalFileList;
  2248. delete FCustomCommandRemoteFileList;
  2249. // takeover ownership,
  2250. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2251. FCustomCommandLocalFileList = LocalFileList;
  2252. FCustomCommandRemoteFileList = RemoteFileList;
  2253. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2254. if (Button != NULL)
  2255. {
  2256. FCustomCommandMenu->Items->Clear();
  2257. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
  2258. MenuPopup(FCustomCommandMenu, Button);
  2259. }
  2260. else
  2261. {
  2262. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2263. }
  2264. }
  2265. //---------------------------------------------------------------------------
  2266. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2267. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2268. {
  2269. DebugAssert(FCapturedLog != NULL);
  2270. if ((OutputType == cotOutput) || (OutputType == cotError))
  2271. {
  2272. FCapturedLog->Add(AddedLine);
  2273. }
  2274. }
  2275. //---------------------------------------------------------------------------
  2276. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2277. TOperationSide Side)
  2278. {
  2279. return
  2280. (Side == osOther) &&
  2281. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2282. }
  2283. //---------------------------------------------------------------------------
  2284. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2285. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2286. {
  2287. TTBItemOptions O;
  2288. O = Item->Options;
  2289. TCustomDirView * DView = DirView(Side);
  2290. if ((DView->ItemFocused != NULL) &&
  2291. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2292. // when resolving links is disabled, default action is to enter the directory,
  2293. // no matter what DoubleClickAction is configured to
  2294. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2295. // Can only Edit files, but can Open/Copy even directories
  2296. ((DoubleClickAction != dcaEdit) ||
  2297. !DView->ItemIsDirectory(DView->ItemFocused)))
  2298. {
  2299. Item->Options = O << tboDefault;
  2300. }
  2301. else
  2302. {
  2303. Item->Options = O >> tboDefault;
  2304. }
  2305. }
  2306. //---------------------------------------------------------------------------
  2307. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2308. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2309. {
  2310. TCustomDirView * DView = DirView(Side);
  2311. TListItem * Item = DView->ItemFocused;
  2312. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2313. EnableFocusedOperation[Side])
  2314. {
  2315. TPoint ClientPoint;
  2316. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2317. TPoint(0, 0) : MousePos;
  2318. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2319. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2320. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2321. }
  2322. }
  2323. //---------------------------------------------------------------------------
  2324. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2325. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2326. {
  2327. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2328. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2329. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2330. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2331. Handled = true;
  2332. }
  2333. //---------------------------------------------------------------------------
  2334. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2335. {
  2336. }
  2337. //---------------------------------------------------------------------------
  2338. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2339. {
  2340. DebugAssert(FErrorList == NULL);
  2341. if (WinConfiguration->ContinueOnError)
  2342. {
  2343. FErrorList = new TStringList();
  2344. Configuration->Usage->Inc(L"ContinuationsOnError");
  2345. }
  2346. NonVisualDataModule->StartBusy();
  2347. }
  2348. //---------------------------------------------------------------------------
  2349. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2350. {
  2351. NonVisualDataModule->EndBusy();
  2352. if (FErrorList)
  2353. {
  2354. HandleErrorList(FErrorList);
  2355. }
  2356. }
  2357. //---------------------------------------------------------------------------
  2358. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2359. const TCopyParamType & CopyParam)
  2360. {
  2361. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2362. {
  2363. Configuration->Usage->Inc(L"FileMaskUses");
  2364. }
  2365. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2366. {
  2367. Configuration->Usage->Inc(L"OperationMaskUses");
  2368. }
  2369. }
  2370. //---------------------------------------------------------------------------
  2371. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2372. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2373. {
  2374. DebugAssert(!IsLocalBrowserMode());
  2375. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2376. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2377. TTransferOperationParam DefaultParam;
  2378. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2379. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2380. switch (Param.Queue)
  2381. {
  2382. case asOn:
  2383. CopyParam.Queue = true;
  2384. break;
  2385. case asOff:
  2386. CopyParam.Queue = false;
  2387. break;
  2388. case asAuto:
  2389. default:
  2390. // keep default
  2391. break;
  2392. }
  2393. bool Result =
  2394. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2395. if (Result)
  2396. {
  2397. DebugAssert(Terminal);
  2398. bool SelectionRestored = false;
  2399. TCustomDirView * DView = NULL;
  2400. if (HasDirView[Side])
  2401. {
  2402. DView = DirView(Side);
  2403. DView->SaveSelection();
  2404. DView->SaveSelectedNames();
  2405. }
  2406. UpdateCopyParamCounters(CopyParam);
  2407. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2408. DebugAssert(FTransferResumeList == NULL);
  2409. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2410. FMoveToQueue = false;
  2411. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2412. try
  2413. {
  2414. TStrings * PermanentFileList;
  2415. std::unique_ptr<TStrings> PermanentFileListOwner;
  2416. try
  2417. {
  2418. if (Side == osLocal)
  2419. {
  2420. PermanentFileList = FileList;
  2421. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2422. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2423. if (Operation == foMove)
  2424. {
  2425. ReloadLocalDirectory();
  2426. if (DView != NULL)
  2427. {
  2428. DView->RestoreSelection();
  2429. }
  2430. SelectionRestored = true;
  2431. }
  2432. }
  2433. else
  2434. {
  2435. // Clone the file list as it may refer to current directory files,
  2436. // which get destroyed, when the source directory is reloaded after foMove operation.
  2437. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2438. // But for a hotfix, we are not going to do such a big change.
  2439. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2440. PermanentFileList = PermanentFileListOwner.get();
  2441. try
  2442. {
  2443. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2444. }
  2445. __finally
  2446. {
  2447. if (Operation == foMove)
  2448. {
  2449. if (DView != NULL)
  2450. {
  2451. DView->RestoreSelection();
  2452. }
  2453. SelectionRestored = true;
  2454. }
  2455. ReloadLocalDirectory(Param.TargetDirectory);
  2456. }
  2457. }
  2458. }
  2459. catch (EAbort &)
  2460. {
  2461. if (FMoveToQueue)
  2462. {
  2463. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2464. DebugAssert(CopyParam.TransferSkipList == NULL);
  2465. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2466. if (TransferResumeList->Count > 0)
  2467. {
  2468. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2469. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2470. }
  2471. CopyParam.TransferSkipList = TransferResumeList.release();
  2472. // not really needed, just to keep it consistent with TransferResumeList
  2473. FTransferResumeList = NULL;
  2474. FMoveToQueue = false;
  2475. Configuration->Usage->Inc("MovesToBackground");
  2476. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2477. ClearTransferSourceSelection(Direction);
  2478. }
  2479. throw;
  2480. }
  2481. }
  2482. __finally
  2483. {
  2484. if (!SelectionRestored && (DView != NULL))
  2485. {
  2486. DView->DiscardSavedSelection();
  2487. }
  2488. FTransferResumeList = NULL;
  2489. }
  2490. }
  2491. return Result;
  2492. }
  2493. //---------------------------------------------------------------------------
  2494. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2495. TOperationSide Side, TStrings * FileList, void * Param)
  2496. {
  2497. DebugAssert(FileList->Count);
  2498. // We deliberately do not toggle alternative flag (Param), but use OR,
  2499. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2500. // shortcut of CurrentDeleteAlternativeAction
  2501. bool Alternative =
  2502. bool(Param) || UseAlternativeFunction();
  2503. bool Recycle;
  2504. if (IsSideLocalBrowser(Side))
  2505. {
  2506. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2507. }
  2508. else
  2509. {
  2510. Recycle =
  2511. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2512. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2513. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2514. }
  2515. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2516. if (!Result)
  2517. {
  2518. UnicodeString Query;
  2519. if (FileList->Count == 1)
  2520. {
  2521. if (IsSideLocalBrowser(Side))
  2522. {
  2523. Query = ExtractFileName(FileList->Strings[0]);
  2524. }
  2525. else
  2526. {
  2527. Query = UnixExtractFileName(FileList->Strings[0]);
  2528. }
  2529. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2530. }
  2531. else
  2532. {
  2533. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2534. }
  2535. TMessageParams Params(mpNeverAskAgainCheck);
  2536. Params.ImageName = L"Delete file";
  2537. unsigned int Answer =
  2538. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2539. if (Answer == qaNeverAskAgain)
  2540. {
  2541. Result = true;
  2542. if (Recycle)
  2543. {
  2544. WinConfiguration->ConfirmRecycling = false;
  2545. }
  2546. else
  2547. {
  2548. WinConfiguration->ConfirmDeleting = false;
  2549. }
  2550. }
  2551. else
  2552. {
  2553. Result = (Answer == qaOK);
  2554. }
  2555. }
  2556. if (Result)
  2557. {
  2558. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2559. }
  2560. return Result;
  2561. }
  2562. //---------------------------------------------------------------------------
  2563. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2564. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2565. {
  2566. TAutoBatch AutoBatch(this);
  2567. bool Result;
  2568. if ((Operation == foCopy) || (Operation == foMove))
  2569. {
  2570. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2571. }
  2572. else if (Operation == foRename)
  2573. {
  2574. DebugAssert(DirView(Side)->ItemFocused);
  2575. DirView(Side)->ItemFocused->EditCaption();
  2576. Result = true;
  2577. }
  2578. else if (Operation == foDelete)
  2579. {
  2580. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2581. }
  2582. else if (Operation == foSetProperties)
  2583. {
  2584. DirView(osRemote)->SaveSelectedNames();
  2585. Result = SetProperties(Side, FileList);
  2586. }
  2587. else if (Operation == foCustomCommand)
  2588. {
  2589. DebugAssert(Param);
  2590. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2591. DirView(osRemote)->SaveSelectedNames();
  2592. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2593. CustomCommand(FileList, *Command, NULL);
  2594. Result = true;
  2595. }
  2596. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2597. {
  2598. DebugAssert(!IsSideLocalBrowser(Side));
  2599. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2600. (Operation == foRemoteMove), reinterpret_cast<TTerminal *>(Param));
  2601. }
  2602. else if (Operation == foLock)
  2603. {
  2604. DebugAssert(!IsSideLocalBrowser(Side));
  2605. LockFiles(FileList, true);
  2606. Result = true;
  2607. }
  2608. else if (Operation == foUnlock)
  2609. {
  2610. DebugAssert(!IsSideLocalBrowser(Side));
  2611. LockFiles(FileList, false);
  2612. Result = true;
  2613. }
  2614. else
  2615. {
  2616. DebugFail();
  2617. }
  2618. return Result;
  2619. }
  2620. //---------------------------------------------------------------------------
  2621. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2622. {
  2623. if (Side == osCurrent)
  2624. {
  2625. Side = FCurrentSide;
  2626. }
  2627. return Side;
  2628. }
  2629. //---------------------------------------------------------------------------
  2630. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2631. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2632. {
  2633. Side = GetSide(Side);
  2634. bool Result;
  2635. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, IsSideLocalBrowser(Side), NULL);
  2636. try
  2637. {
  2638. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2639. }
  2640. __finally
  2641. {
  2642. delete FileList;
  2643. }
  2644. return Result;
  2645. }
  2646. //---------------------------------------------------------------------------
  2647. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2648. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2649. bool NoConfirmation, void * Param)
  2650. {
  2651. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2652. {
  2653. if ((Operation == foCopy) || (Operation == foMove))
  2654. {
  2655. DebugAssert(!IsLocalBrowserMode());
  2656. if (GetSide(Side) == osLocal)
  2657. {
  2658. Configuration->Usage->Inc(L"UploadsCommand");
  2659. }
  2660. else
  2661. {
  2662. Configuration->Usage->Inc(L"DownloadsCommand");
  2663. }
  2664. }
  2665. }
  2666. }
  2667. //---------------------------------------------------------------------------
  2668. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2669. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2670. {
  2671. TTransferOperationParam Param;
  2672. if ((WinConfiguration->Interface != ifCommander) ||
  2673. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2674. {
  2675. Flags &= ~cocShortCutHint;
  2676. }
  2677. TCustomDirView * DView = DirView(Side);
  2678. Param.Options =
  2679. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2680. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2681. if (FLAGSET(Flags, cocQueue))
  2682. {
  2683. Param.Queue = asOn;
  2684. }
  2685. else if (FLAGSET(Flags, cocNonQueue))
  2686. {
  2687. Param.Queue = asOff;
  2688. }
  2689. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2690. }
  2691. //---------------------------------------------------------------------------
  2692. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2693. {
  2694. try
  2695. {
  2696. if (ErrorList->Count)
  2697. {
  2698. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2699. if (MessageDialog(Message, qtError,
  2700. qaOK | qaCancel, HELP_NONE) == qaOK)
  2701. {
  2702. unsigned int Answer;
  2703. int Index = 0;
  2704. do
  2705. {
  2706. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2707. TQueryButtonAlias Aliases[2];
  2708. Aliases[0].Button = qaYes;
  2709. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2710. Aliases[1].Button = qaNo;
  2711. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2712. TMessageParams Params;
  2713. Params.Aliases = Aliases;
  2714. Params.AliasesCount = LENOF(Aliases);
  2715. Answer = MoreMessageDialog(
  2716. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2717. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2718. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2719. qaOK, HELP_NONE, &Params);
  2720. if (Answer == qaNo)
  2721. {
  2722. Index++;
  2723. }
  2724. if (Answer == qaYes)
  2725. {
  2726. Index--;
  2727. }
  2728. }
  2729. while (Answer != qaOK);
  2730. }
  2731. }
  2732. }
  2733. __finally
  2734. {
  2735. TStrings * List = ErrorList;
  2736. ErrorList = NULL;
  2737. for (int i = 0; i < List->Count; i++)
  2738. {
  2739. delete List->Objects[i];
  2740. }
  2741. delete List;
  2742. }
  2743. }
  2744. //---------------------------------------------------------------------------
  2745. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2746. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2747. {
  2748. // needed for checking filemasks, as there's no directory object
  2749. // associated with the file object
  2750. File->FullFileName = FullFileName;
  2751. TFileMasks::TParams MaskParams;
  2752. MaskParams.Size = File->Size;
  2753. MaskParams.Modification = File->Modification;
  2754. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2755. }
  2756. //---------------------------------------------------------------------------
  2757. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2758. {
  2759. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2760. Side = GetSide(Side);
  2761. TCustomDirView * DView = DirView(Side);
  2762. TListItem * FocusedItem = DView->ItemFocused;
  2763. UnicodeString Name;
  2764. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2765. {
  2766. Name = DView->ItemFileName(FocusedItem);
  2767. }
  2768. else
  2769. {
  2770. Name = LoadStr(NEW_FILE);
  2771. }
  2772. UnicodeString Names = Name;
  2773. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2774. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2775. HELP_EDIT_NEW, History.get(), true))
  2776. {
  2777. while (!Names.IsEmpty())
  2778. {
  2779. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2780. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2781. UnicodeString TargetFileName;
  2782. UnicodeString LocalFileName;
  2783. UnicodeString RootTempDir;
  2784. UnicodeString TempDir;
  2785. UnicodeString RemoteDirectory;
  2786. bool ExistingFile = false;
  2787. if (!IsSideLocalBrowser(Side))
  2788. {
  2789. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2790. TRemoteFile * File = NULL;
  2791. if (FTerminal->FileExists(Name, &File) &&
  2792. !File->IsDirectory)
  2793. {
  2794. try
  2795. {
  2796. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2797. ExistingFile = true;
  2798. }
  2799. __finally
  2800. {
  2801. delete File;
  2802. }
  2803. }
  2804. if (!ExistingFile)
  2805. {
  2806. RemoteDirectory = UnixExtractFilePath(Name);
  2807. TemporaryDirectoryForRemoteFiles(
  2808. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2809. TargetFileName = UnixExtractFileName(Name);
  2810. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2811. LocalFileName = TempDir +
  2812. // We probably do not want to trim the VMS version here
  2813. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2814. }
  2815. }
  2816. else
  2817. {
  2818. if (ExtractFilePath(Name).IsEmpty())
  2819. {
  2820. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2821. }
  2822. else
  2823. {
  2824. LocalFileName = ExpandFileName(Name);
  2825. }
  2826. TargetFileName = ExtractFileName(Name);
  2827. }
  2828. if (!ExistingFile)
  2829. {
  2830. if (!FileExists(ApiPath(LocalFileName)))
  2831. {
  2832. int File = FileCreate(ApiPath(LocalFileName));
  2833. if (File < 0)
  2834. {
  2835. if (!RootTempDir.IsEmpty())
  2836. {
  2837. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2838. }
  2839. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2840. }
  2841. else
  2842. {
  2843. FileClose(File);
  2844. }
  2845. }
  2846. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2847. const TEditorData * ExternalEditor = NULL;
  2848. TFileMasks::TParams MaskParams; // size not known
  2849. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2850. false, MaskParams);
  2851. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2852. ExternalEditor, RootTempDir, RemoteDirectory);
  2853. }
  2854. }
  2855. }
  2856. }
  2857. //---------------------------------------------------------------------------
  2858. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2859. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2860. {
  2861. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2862. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2863. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2864. return
  2865. ((ExecuteFileBy == efInternalEditor)) ||
  2866. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2867. }
  2868. //---------------------------------------------------------------------------
  2869. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2870. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2871. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2872. UnicodeString RemoteDirectory)
  2873. {
  2874. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2875. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2876. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2877. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2878. Side = GetSide(Side);
  2879. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2880. if (!IsSideLocalBrowser(Side))
  2881. {
  2882. Data->Terminal = Terminal;
  2883. Data->Queue = Queue;
  2884. Data->SessionData = CloneCurrentSessionData();
  2885. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2886. Data->RemoteDirectory = RemoteDirectory;
  2887. Data->SessionName = Terminal->SessionData->SessionName;
  2888. Data->LocalRootDirectory = LocalRootDirectory;
  2889. Data->OriginalFileName = OriginalFileName;
  2890. Data->Command = L""; // will be changed later for external editor
  2891. }
  2892. if (ExecuteFileBy == efInternalEditor)
  2893. {
  2894. if (!IsSideLocalBrowser(Side))
  2895. {
  2896. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2897. L" - " + Terminal->SessionData->SessionName;
  2898. TForm * Editor;
  2899. try
  2900. {
  2901. TNotifyEvent OnFileChanged = NULL;
  2902. TNotifyEvent OnSaveAll = NULL;
  2903. TAnyModifiedEvent OnAnyModified = NULL;
  2904. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2905. if (Terminal->IsCapable[fsBackgroundTransfers])
  2906. {
  2907. OnFileChanged = FEditorManager->FileChanged;
  2908. OnSaveAll = SaveAllInternalEditors;
  2909. OnAnyModified = AnyInternalEditorModified;
  2910. }
  2911. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2912. FEditorManager->FileReload, FEditorManager->FileClosed,
  2913. OnSaveAll, OnAnyModified,
  2914. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding);
  2915. }
  2916. catch(...)
  2917. {
  2918. if (!LocalRootDirectory.IsEmpty())
  2919. {
  2920. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2921. }
  2922. throw;
  2923. }
  2924. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2925. }
  2926. else
  2927. {
  2928. DebugAssert(!FStandaloneEditing);
  2929. TForm * Editor =
  2930. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2931. SaveAllInternalEditors, AnyInternalEditorModified,
  2932. L"", false, SessionColor, -1);
  2933. FLocalEditors->Add(Editor);
  2934. }
  2935. }
  2936. else
  2937. {
  2938. HANDLE Process;
  2939. if (ExecuteFileBy == efExternalEditor)
  2940. {
  2941. UnicodeString Program, Params, Dir;
  2942. Data->Command = ExternalEditor->ExternalEditor;
  2943. ReformatFileNameCommand(Data->Command);
  2944. SplitCommand(Data->Command, Program, Params, Dir);
  2945. Params = ExpandFileNameCommand(Params, FileName);
  2946. Program = ExpandEnvironmentVariables(Program);
  2947. if (!ExecuteShell(Program, Params, Process))
  2948. {
  2949. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2950. }
  2951. }
  2952. else
  2953. {
  2954. DebugAssert(IsSideLocalBrowser(Side));
  2955. if (!ExecuteShell(FileName, L"", Process))
  2956. {
  2957. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2958. }
  2959. }
  2960. if (IsSideLocalBrowser(Side) ||
  2961. ((ExecuteFileBy == efShell) &&
  2962. !WinConfiguration->Editor.SDIShellEditor) ||
  2963. ((ExecuteFileBy == efExternalEditor) &&
  2964. !ExternalEditor->SDIExternalEditor))
  2965. {
  2966. // no need for handle
  2967. if (Process != NULL)
  2968. {
  2969. DebugCheck(CloseHandle(Process));
  2970. }
  2971. Process = INVALID_HANDLE_VALUE;
  2972. }
  2973. else
  2974. {
  2975. if (Process == NULL)
  2976. {
  2977. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2978. }
  2979. }
  2980. if (!IsSideLocalBrowser(Side))
  2981. {
  2982. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2983. }
  2984. }
  2985. }
  2986. //---------------------------------------------------------------------------
  2987. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2988. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2989. void * /*Arg*/)
  2990. {
  2991. if (Token != NULL)
  2992. {
  2993. EditorFormFileSave(static_cast<TForm *>(Token));
  2994. }
  2995. }
  2996. //---------------------------------------------------------------------------
  2997. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  2998. {
  2999. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3000. {
  3001. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3002. }
  3003. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3004. }
  3005. //---------------------------------------------------------------------------
  3006. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3007. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3008. void * Arg)
  3009. {
  3010. if ((Token != NULL) &&
  3011. IsEditorFormModified(static_cast<TForm *>(Token)))
  3012. {
  3013. *static_cast<bool *>(Arg) = true;
  3014. }
  3015. }
  3016. //---------------------------------------------------------------------------
  3017. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3018. TObject * /*Sender*/, bool & Modified)
  3019. {
  3020. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3021. {
  3022. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3023. {
  3024. Modified = true;
  3025. }
  3026. }
  3027. if (!Modified)
  3028. {
  3029. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3030. }
  3031. }
  3032. //---------------------------------------------------------------------------
  3033. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3034. {
  3035. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3036. }
  3037. //---------------------------------------------------------------------------
  3038. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3039. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3040. UnicodeString & Result, UnicodeString & RootDirectory)
  3041. {
  3042. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3043. {
  3044. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3045. Result = RootDirectory;
  3046. }
  3047. else
  3048. {
  3049. RootDirectory = L"";
  3050. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3051. Result = IncludeTrailingBackslash(Result);
  3052. }
  3053. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3054. {
  3055. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3056. }
  3057. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3058. {
  3059. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3060. {
  3061. RemoteDirectory.Delete(1, 1);
  3062. }
  3063. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3064. }
  3065. if (!ForceDirectories(ApiPath(Result)))
  3066. {
  3067. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3068. }
  3069. }
  3070. //---------------------------------------------------------------------------
  3071. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3072. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3073. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3074. {
  3075. DebugAssert(!IsLocalBrowserMode());
  3076. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3077. if (ForceText)
  3078. {
  3079. CopyParam.TransferMode = tmAscii;
  3080. }
  3081. TemporaryFileCopyParam(CopyParam);
  3082. if (AllFiles)
  3083. {
  3084. CopyParam.IncludeFileMask = TFileMasks();
  3085. }
  3086. if (TempDir.IsEmpty())
  3087. {
  3088. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3089. }
  3090. DebugAssert(!FAutoOperation);
  3091. FAutoOperation = AutoOperation;
  3092. Terminal->ExceptionOnFail = true;
  3093. try
  3094. {
  3095. try
  3096. {
  3097. // turn off confirmations, as for MDI editors we may possibly download
  3098. // the same file over
  3099. Terminal->CopyToLocal(
  3100. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3101. if (GetTargetNames)
  3102. {
  3103. for (int i = 0; i < FileList->Count; i++)
  3104. {
  3105. FileList->Strings[i] =
  3106. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3107. }
  3108. }
  3109. }
  3110. catch(...)
  3111. {
  3112. if (!RootTempDir.IsEmpty())
  3113. {
  3114. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3115. }
  3116. throw;
  3117. }
  3118. }
  3119. __finally
  3120. {
  3121. FAutoOperation = false;
  3122. Terminal->ExceptionOnFail = false;
  3123. }
  3124. }
  3125. //---------------------------------------------------------------------------
  3126. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3127. {
  3128. // Do not waste time checking for default editor list the next time,
  3129. // or testing if default editor is not notepad.
  3130. bool TryNextTime = false;
  3131. UnicodeString UsageState;
  3132. if (!WinConfiguration->EditorList->IsDefaultList())
  3133. {
  3134. UsageState = "H";
  3135. }
  3136. else
  3137. {
  3138. UnicodeString Executable;
  3139. UnicodeString ExecutableDescription;
  3140. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3141. {
  3142. UnicodeString Message =
  3143. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3144. unsigned int Answer =
  3145. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3146. if (Answer != qaOK)
  3147. {
  3148. UsageState = "R";
  3149. }
  3150. else
  3151. {
  3152. UsageState = "A";
  3153. TEditorData EditorData;
  3154. EditorData.Editor = edExternal;
  3155. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3156. EditorData.DecideExternalEditorText();
  3157. TEditorList EditorList;
  3158. EditorList = *WinConfiguration->EditorList;
  3159. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3160. WinConfiguration->EditorList = &EditorList;
  3161. }
  3162. }
  3163. }
  3164. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3165. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3166. }
  3167. //---------------------------------------------------------------------------
  3168. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3169. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3170. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3171. {
  3172. if (ExecuteFileBy == efDefaultEditor)
  3173. {
  3174. if (!WinConfiguration->OfferedEditorAutoConfig)
  3175. {
  3176. EditorAutoConfig();
  3177. }
  3178. const TEditorPreferences * Editor =
  3179. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3180. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3181. {
  3182. ExecuteFileBy = efInternalEditor;
  3183. ExternalEditor = NULL;
  3184. }
  3185. else if (Editor->Data->Editor == edOpen)
  3186. {
  3187. ExecuteFileBy = efShell;
  3188. ExternalEditor = NULL;
  3189. }
  3190. else
  3191. {
  3192. ExecuteFileBy = efExternalEditor;
  3193. ExternalEditor = Editor->Data;
  3194. }
  3195. }
  3196. }
  3197. //---------------------------------------------------------------------------
  3198. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3199. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3200. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3201. {
  3202. UnicodeString OriginalFileName;
  3203. UnicodeString LocalRootDirectory;
  3204. UnicodeString RemoteDirectory;
  3205. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3206. IsSideLocalBrowser(Side), MaskParams);
  3207. UnicodeString Counter;
  3208. UnicodeString LocalFileName;
  3209. bool Handled = false;
  3210. if (!IsSideLocalBrowser(Side))
  3211. {
  3212. // We need to trim VMS version here, so that we use name without version
  3213. // when uploading back to create a new version of the file
  3214. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3215. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3216. TObject * Token = NULL;
  3217. UnicodeString LocalDirectory;
  3218. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3219. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3220. LocalDirectory))
  3221. {
  3222. if (Token != NULL)
  3223. {
  3224. TForm * Form = dynamic_cast<TForm *>(Token);
  3225. if (Form->WindowState == wsMinimized)
  3226. {
  3227. ShowWindow(Form->Handle, SW_RESTORE);
  3228. }
  3229. else
  3230. {
  3231. Form->SetFocus();
  3232. }
  3233. Handled = true;
  3234. }
  3235. else
  3236. {
  3237. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3238. }
  3239. }
  3240. if (!Handled)
  3241. {
  3242. TStringList * FileList1 = new TStringList();
  3243. try
  3244. {
  3245. FileList1->AddObject(FullFileName, Object);
  3246. TemporarilyDownloadFiles(FileList1,
  3247. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3248. LocalRootDirectory, LocalDirectory, true, true, true);
  3249. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3250. }
  3251. __finally
  3252. {
  3253. delete FileList1;
  3254. }
  3255. switch (ExecuteFileBy)
  3256. {
  3257. case efShell:
  3258. Counter = "RemoteFilesExecuted";
  3259. break;
  3260. case efInternalEditor:
  3261. Counter = "RemoteFilesOpenedInInternalEditor";
  3262. break;
  3263. case efExternalEditor:
  3264. Counter = "RemoteFilesOpenedInExternalEditor";
  3265. break;
  3266. default:
  3267. DebugFail();
  3268. }
  3269. }
  3270. }
  3271. else
  3272. {
  3273. LocalFileName = FullFileName;
  3274. OriginalFileName = ExtractFileName(FullFileName);
  3275. switch (ExecuteFileBy)
  3276. {
  3277. case efShell:
  3278. Counter = "LocalFilesExecuted";
  3279. break;
  3280. case efInternalEditor:
  3281. Counter = "LocalFilesOpenedInInternalEditor";
  3282. break;
  3283. case efExternalEditor:
  3284. Counter = "LocalFilesOpenedInExternalEditor";
  3285. break;
  3286. default:
  3287. DebugFail();
  3288. }
  3289. }
  3290. if (!Handled)
  3291. {
  3292. Configuration->Usage->Inc(Counter);
  3293. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3294. ExternalEditor, LocalRootDirectory, RemoteDirectory);
  3295. }
  3296. }
  3297. //---------------------------------------------------------------------------
  3298. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3299. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3300. bool AllSelected, bool OnFocused)
  3301. {
  3302. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3303. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3304. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3305. Side = GetSide(Side);
  3306. TCustomDirView * DView = DirView(Side);
  3307. TStrings * FileList =
  3308. AllSelected ?
  3309. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3310. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3311. try
  3312. {
  3313. DebugAssert(AllSelected || (FileList->Count == 1));
  3314. for (int i = 0; i < FileList->Count; i++)
  3315. {
  3316. UnicodeString ListFileName = FileList->Strings[i];
  3317. UnicodeString FileNameOnly =
  3318. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3319. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3320. if (!DView->ItemIsDirectory(Item))
  3321. {
  3322. UnicodeString FullFileName;
  3323. if (!IsSideLocalBrowser(Side))
  3324. {
  3325. FullFileName = DirView(Side)->Path + ListFileName;
  3326. }
  3327. else
  3328. {
  3329. FullFileName = ListFileName;
  3330. }
  3331. TFileMasks::TParams MaskParams;
  3332. MaskParams.Size = DView->ItemFileSize(Item);
  3333. TDateTimePrecision Precision;
  3334. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3335. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3336. FileList->Objects[i], MaskParams);
  3337. }
  3338. }
  3339. }
  3340. __finally
  3341. {
  3342. delete FileList;
  3343. }
  3344. }
  3345. //---------------------------------------------------------------------------
  3346. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3347. {
  3348. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3349. CopyParam.FileNameCase = ncNoChange;
  3350. CopyParam.PreserveRights = false;
  3351. CopyParam.PreserveReadOnly = false;
  3352. CopyParam.ReplaceInvalidChars = true;
  3353. CopyParam.IncludeFileMask = TFileMasks();
  3354. CopyParam.NewerOnly = false;
  3355. CopyParam.FileMask = L"";
  3356. }
  3357. //---------------------------------------------------------------------------
  3358. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3359. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3360. {
  3361. TTerminalManager * Manager = TTerminalManager::Instance();
  3362. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3363. {
  3364. if (!NonVisualDataModule->Busy)
  3365. {
  3366. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3367. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3368. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3369. if (Data->Terminal == NULL)
  3370. {
  3371. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3372. if (SameSiteTerminal != NULL)
  3373. {
  3374. UnicodeString Message =
  3375. FMTLOAD(EDIT_SESSION_REATTACH,
  3376. (FileNameOnly, Data->SessionName, FileNameOnly));
  3377. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3378. {
  3379. Data->Terminal = Terminal;
  3380. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3381. Data->SessionName = Terminal->SessionData->SessionName;
  3382. // We might also overwrite session data
  3383. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3384. }
  3385. else
  3386. {
  3387. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3388. Abort();
  3389. }
  3390. }
  3391. }
  3392. // foreground session should reconnect itself
  3393. else if (Terminal != Data->Terminal)
  3394. {
  3395. UnicodeString Message =
  3396. MainInstructions(
  3397. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3398. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3399. {
  3400. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3401. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3402. }
  3403. else
  3404. {
  3405. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3406. Abort();
  3407. }
  3408. }
  3409. }
  3410. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3411. {
  3412. Configuration->Usage->Inc(L"EditInactiveSession");
  3413. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3414. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3415. (ExtractFileName(FileName), Data->SessionName)));
  3416. }
  3417. }
  3418. TStrings * FileList = new TStringList();
  3419. try
  3420. {
  3421. FileList->Add(FileName);
  3422. // consider using the same settings (preset) as when the file was downloaded
  3423. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3424. TemporaryFileCopyParam(CopyParam);
  3425. if (Data->ForceText)
  3426. {
  3427. CopyParam.TransferMode = tmAscii;
  3428. }
  3429. // so i do not need to worry if masking algorithm works in all cases
  3430. // ("" means "copy file name", no masking is actually done)
  3431. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3432. {
  3433. CopyParam.FileMask = L"";
  3434. }
  3435. else
  3436. {
  3437. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3438. }
  3439. DebugAssert(Data->Queue != NULL);
  3440. int Params = cpNoConfirmation | cpTemporary;
  3441. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3442. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3443. QueueItem->CompleteEvent = UploadCompleteEvent;
  3444. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3445. }
  3446. __finally
  3447. {
  3448. delete FileList;
  3449. }
  3450. }
  3451. //---------------------------------------------------------------------------
  3452. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3453. const UnicodeString FileName, const TEditedFileData * Data)
  3454. {
  3455. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3456. DebugAssert(!NonVisualDataModule->Busy);
  3457. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3458. {
  3459. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3460. (ExtractFileName(FileName), Data->SessionName)));
  3461. }
  3462. TTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3463. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3464. NonVisualDataModule->StartBusy();
  3465. try
  3466. {
  3467. std::unique_ptr<TRemoteFile> File;
  3468. UnicodeString RemoteFileName =
  3469. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3470. FTerminal->ExceptionOnFail = true;
  3471. try
  3472. {
  3473. TRemoteFile * AFile = NULL;
  3474. FTerminal->ReadFile(RemoteFileName, AFile);
  3475. File.reset(AFile);
  3476. if (!File->HaveFullFileName)
  3477. {
  3478. File->FullFileName = RemoteFileName;
  3479. }
  3480. }
  3481. __finally
  3482. {
  3483. FTerminal->ExceptionOnFail = false;
  3484. }
  3485. std::unique_ptr<TStrings> FileList(new TStringList());
  3486. FileList->AddObject(RemoteFileName, File.get());
  3487. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3488. UnicodeString TempDir = ExtractFilePath(FileName);
  3489. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3490. TempDir, true, true, true);
  3491. // sanity check, the target file name should be still the same
  3492. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3493. }
  3494. __finally
  3495. {
  3496. NonVisualDataModule->EndBusy();
  3497. // it actually may not exist anymore...
  3498. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3499. }
  3500. }
  3501. //---------------------------------------------------------------------------
  3502. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3503. const TEditedFileData * Data, bool & KeepOpen)
  3504. {
  3505. // Command is set for external editors only (not for "shell" open).
  3506. if (!Data->Command.IsEmpty())
  3507. {
  3508. bool AnyFound = false;
  3509. bool AnyMDI = false;
  3510. bool AnyNonMDI = false;
  3511. bool AnyDetect = false;
  3512. TEditorList * EditorList = new TEditorList();
  3513. try
  3514. {
  3515. *EditorList = *WinConfiguration->EditorList;
  3516. for (int i = 0; i < EditorList->Count; i++)
  3517. {
  3518. const TEditorPreferences * Editor = EditorList->Editors[i];
  3519. if ((Editor->Data->Editor == edExternal) &&
  3520. (Editor->Data->ExternalEditor == Data->Command))
  3521. {
  3522. AnyFound = true;
  3523. if (Editor->Data->SDIExternalEditor)
  3524. {
  3525. AnyNonMDI = true;
  3526. if (Editor->Data->DetectMDIExternalEditor)
  3527. {
  3528. AnyDetect = true;
  3529. }
  3530. }
  3531. else
  3532. {
  3533. AnyMDI = true;
  3534. }
  3535. }
  3536. }
  3537. bool EnableMDI = false;
  3538. bool DisableDetect = false;
  3539. if (AnyMDI)
  3540. {
  3541. KeepOpen = true;
  3542. if (AnyNonMDI)
  3543. {
  3544. // there is at least one instance of the editor with MDI support enabled,
  3545. // and one with disabled, enable it for all instances
  3546. EnableMDI = true;
  3547. }
  3548. }
  3549. else if (AnyFound && !AnyDetect)
  3550. {
  3551. // at least once instance found but all have MDI autodetection disabled
  3552. // => close the file (default action)
  3553. }
  3554. else
  3555. {
  3556. // no instance of the editor has MDI support enabled
  3557. TMessageParams Params;
  3558. if (AnyFound)
  3559. {
  3560. // there is at least one instance of the editor
  3561. Params.Params |= mpNeverAskAgainCheck;
  3562. }
  3563. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3564. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3565. switch (Answer)
  3566. {
  3567. case qaNeverAskAgain:
  3568. DisableDetect = true;
  3569. break;
  3570. case qaNo:
  3571. EnableMDI = true;
  3572. KeepOpen = true;
  3573. break;
  3574. }
  3575. }
  3576. if (AnyFound && (EnableMDI || DisableDetect))
  3577. {
  3578. bool Changed = false;
  3579. for (int i = 0; i < EditorList->Count; i++)
  3580. {
  3581. const TEditorPreferences * Editor = EditorList->Editors[i];
  3582. if ((Editor->Data->Editor == edExternal) &&
  3583. (Editor->Data->ExternalEditor == Data->Command) &&
  3584. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3585. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3586. {
  3587. Changed = true;
  3588. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3589. if (EnableMDI)
  3590. {
  3591. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3592. }
  3593. if (DisableDetect)
  3594. {
  3595. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3596. }
  3597. EditorList->Change(i, UpdatedEditor);
  3598. }
  3599. }
  3600. if (Changed)
  3601. {
  3602. WinConfiguration->EditorList = EditorList;
  3603. }
  3604. }
  3605. }
  3606. __finally
  3607. {
  3608. delete EditorList;
  3609. }
  3610. }
  3611. else
  3612. {
  3613. // "open" case
  3614. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3615. qaOK, HELP_APP_EARLY_CLOSED);
  3616. }
  3617. }
  3618. //---------------------------------------------------------------------------
  3619. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3620. {
  3621. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3622. }
  3623. //---------------------------------------------------------------------------
  3624. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3625. {
  3626. SideEnter(osRemote);
  3627. }
  3628. //---------------------------------------------------------------------------
  3629. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3630. {
  3631. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3632. SideEnter(osRemote);
  3633. }
  3634. //---------------------------------------------------------------------------
  3635. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3636. {
  3637. FCurrentSide = Side;
  3638. if (Visible)
  3639. {
  3640. UpdateControls();
  3641. }
  3642. }
  3643. //---------------------------------------------------------------------------
  3644. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3645. TStrings * FileList, bool Alternative)
  3646. {
  3647. TCustomDirView * DView = DirView(Side);
  3648. DView->SaveSelection();
  3649. DView->SaveSelectedNames();
  3650. DebugAssert(!FAlternativeDelete);
  3651. FAlternativeDelete = Alternative;
  3652. try
  3653. {
  3654. if (!IsSideLocalBrowser(Side))
  3655. {
  3656. DebugAssert(Terminal != NULL);
  3657. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3658. }
  3659. else
  3660. {
  3661. try
  3662. {
  3663. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3664. }
  3665. __finally
  3666. {
  3667. ReloadLocalDirectory();
  3668. }
  3669. }
  3670. FAlternativeDelete = false;
  3671. }
  3672. catch(...)
  3673. {
  3674. FAlternativeDelete = false;
  3675. DView->DiscardSavedSelection();
  3676. throw;
  3677. }
  3678. DView->RestoreSelection();
  3679. }
  3680. //---------------------------------------------------------------------------
  3681. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3682. {
  3683. DebugAssert(Terminal);
  3684. DebugAssert(!IsLocalBrowserMode());
  3685. RemoteDirView->SaveSelection();
  3686. RemoteDirView->SaveSelectedNames();
  3687. try
  3688. {
  3689. if (Lock)
  3690. {
  3691. Terminal->LockFiles(FileList);
  3692. }
  3693. else
  3694. {
  3695. Terminal->UnlockFiles(FileList);
  3696. }
  3697. }
  3698. catch(...)
  3699. {
  3700. RemoteDirView->DiscardSavedSelection();
  3701. throw;
  3702. }
  3703. RemoteDirView->RestoreSelection();
  3704. }
  3705. //---------------------------------------------------------------------------
  3706. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TTerminal *& Session,
  3707. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3708. bool NoConfirmation, bool Move)
  3709. {
  3710. DebugAssert(Terminal != NULL);
  3711. DebugAssert(!IsLocalBrowserMode());
  3712. // update Terminal->StateData->RemoteDirectory
  3713. UpdateTerminal(Terminal);
  3714. if (Session == NULL)
  3715. {
  3716. Session = TTerminalManager::Instance()->ActiveTerminal;
  3717. }
  3718. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3719. {
  3720. if (RemoteDriveView->DropTarget != NULL)
  3721. {
  3722. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3723. }
  3724. else if (RemoteDirView->DropTarget != NULL)
  3725. {
  3726. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3727. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3728. }
  3729. else
  3730. {
  3731. Target = RemoteDirView->Path;
  3732. }
  3733. }
  3734. else
  3735. {
  3736. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Session));
  3737. Target = ManagedTerminal->StateData->RemoteDirectory;
  3738. }
  3739. Target = UnixIncludeTrailingBackslash(Target);
  3740. if (FileList->Count == 1)
  3741. {
  3742. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3743. }
  3744. else
  3745. {
  3746. FileMask = AnyMask;
  3747. }
  3748. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3749. bool Result = true;
  3750. if (!NoConfirmation)
  3751. {
  3752. bool Multi = (FileList->Count > 1);
  3753. if (Move)
  3754. {
  3755. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3756. }
  3757. else
  3758. {
  3759. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3760. TStrings * Directories = new TStringList;
  3761. try
  3762. {
  3763. for (int Index = 0; Index < Sessions->Count; Index++)
  3764. {
  3765. TManagedTerminal * Terminal =
  3766. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3767. Directories->Add(Terminal->StateData->RemoteDirectory);
  3768. }
  3769. TDirectRemoteCopy AllowDirectCopy;
  3770. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3771. {
  3772. DebugAssert(DirectCopy);
  3773. AllowDirectCopy = drcAllow;
  3774. }
  3775. else if (FTerminal->IsCapable[fcSecondaryShell])
  3776. {
  3777. DebugAssert(DirectCopy);
  3778. AllowDirectCopy = drcConfirmCommandSession;
  3779. }
  3780. else
  3781. {
  3782. DebugAssert(!DirectCopy);
  3783. AllowDirectCopy = drcDisallow;
  3784. }
  3785. void * ASession = Session;
  3786. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3787. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3788. Session = static_cast<TTerminal *>(ASession);
  3789. }
  3790. __finally
  3791. {
  3792. delete Directories;
  3793. }
  3794. }
  3795. }
  3796. return Result;
  3797. }
  3798. //---------------------------------------------------------------------------
  3799. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3800. TStrings * FileList, bool NoConfirmation, bool Move, TTerminal * Session)
  3801. {
  3802. DebugAssert(!IsLocalBrowserMode());
  3803. bool DirectCopy;
  3804. UnicodeString Target, FileMask;
  3805. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3806. if (Result)
  3807. {
  3808. if (!Move && !DirectCopy)
  3809. {
  3810. Configuration->Usage->Inc("RemoteCopyTemp");
  3811. UnicodeString RootTempDir;
  3812. UnicodeString TempDir;
  3813. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3814. TStrings * TemporaryFilesList = new TStringList();
  3815. try
  3816. {
  3817. TMakeLocalFileListParams MakeFileListParam;
  3818. MakeFileListParam.FileList = TemporaryFilesList;
  3819. MakeFileListParam.FileTimes = NULL;
  3820. MakeFileListParam.IncludeDirs = true;
  3821. MakeFileListParam.Recursive = false;
  3822. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3823. TTerminalManager::Instance()->ActiveTerminal = Session;
  3824. if (TemporaryFilesList->Count > 0)
  3825. {
  3826. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3827. CopyParam.FileMask = FileMask;
  3828. DebugAssert(!FAutoOperation);
  3829. FAutoOperation = true;
  3830. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3831. }
  3832. }
  3833. __finally
  3834. {
  3835. delete TemporaryFilesList;
  3836. FAutoOperation = false;
  3837. if (!RootTempDir.IsEmpty())
  3838. {
  3839. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3840. }
  3841. }
  3842. }
  3843. else
  3844. {
  3845. RemoteDirView->SaveSelection();
  3846. RemoteDirView->SaveSelectedNames();
  3847. try
  3848. {
  3849. if (Move)
  3850. {
  3851. Configuration->Usage->Inc("RemoteMove");
  3852. Terminal->MoveFiles(FileList, Target, FileMask);
  3853. }
  3854. else
  3855. {
  3856. Configuration->Usage->Inc("RemoteCopyDirect");
  3857. DebugAssert(DirectCopy);
  3858. DebugAssert(Session == FTerminal);
  3859. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3860. FTerminal->CommandSessionOpened ||
  3861. CommandSessionFallback())
  3862. {
  3863. Terminal->CopyFiles(FileList, Target, FileMask);
  3864. }
  3865. }
  3866. }
  3867. catch(...)
  3868. {
  3869. RemoteDirView->DiscardSavedSelection();
  3870. throw;
  3871. }
  3872. RemoteDirView->RestoreSelection();
  3873. }
  3874. }
  3875. return Result;
  3876. }
  3877. //---------------------------------------------------------------------------
  3878. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3879. const UnicodeString & Path, TRemoteProperties & Properties)
  3880. {
  3881. DebugAssert(!IsLocalBrowserMode());
  3882. Properties.Valid = Properties.Valid << vpEncrypt;
  3883. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3884. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3885. }
  3886. //---------------------------------------------------------------------------
  3887. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3888. {
  3889. Side = GetSide(Side);
  3890. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3891. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  3892. UnicodeString Name = LoadStr(NEW_FOLDER);
  3893. int AllowedChanges =
  3894. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  3895. bool SaveSettings = false;
  3896. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3897. {
  3898. TWindowLock Lock(this);
  3899. if (!IsSideLocalBrowser(Side))
  3900. {
  3901. if (SaveSettings)
  3902. {
  3903. GUIConfiguration->NewDirectoryProperties = Properties;
  3904. }
  3905. CreateRemoteDirectory(Name, Properties);
  3906. }
  3907. else
  3908. {
  3909. DirView(Side)->CreateDirectory(Name);
  3910. }
  3911. }
  3912. }
  3913. //---------------------------------------------------------------------------
  3914. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3915. {
  3916. TWindowLock Lock(this);
  3917. DirView(Side)->ExecuteHomeDirectory();
  3918. }
  3919. //---------------------------------------------------------------------------
  3920. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3921. {
  3922. DoOpenDirectoryDialog(odBrowse, Side);
  3923. }
  3924. //---------------------------------------------------------------------------
  3925. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3926. {
  3927. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3928. bool Result = !Path.IsEmpty();
  3929. if (Result)
  3930. {
  3931. // While we might get here when the session is closed (from location profiles),
  3932. // it's not a problem as the Path setter is noop then
  3933. DirView(Side)->Path = Path;
  3934. }
  3935. return Result;
  3936. }
  3937. //---------------------------------------------------------------------------
  3938. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3939. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3940. bool & Close)
  3941. {
  3942. // terminal can be already closed (e.g. dropped connection)
  3943. if (Terminal != NULL)
  3944. {
  3945. try
  3946. {
  3947. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3948. }
  3949. catch(...)
  3950. {
  3951. if (!Terminal->Active)
  3952. {
  3953. Close = true;
  3954. }
  3955. throw;
  3956. }
  3957. }
  3958. }
  3959. //---------------------------------------------------------------------------
  3960. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3961. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3962. bool & Close)
  3963. {
  3964. // terminal can be already closed (e.g. dropped connection)
  3965. if (Terminal != NULL)
  3966. {
  3967. Configuration->Usage->Inc(L"ChecksumCalculated");
  3968. try
  3969. {
  3970. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3971. }
  3972. catch(...)
  3973. {
  3974. if (!Terminal->Active)
  3975. {
  3976. Close = true;
  3977. }
  3978. throw;
  3979. }
  3980. }
  3981. }
  3982. //---------------------------------------------------------------------------
  3983. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3984. {
  3985. bool Result;
  3986. if (!IsSideLocalBrowser(Side))
  3987. {
  3988. TRemoteTokenList * GroupList = NULL;
  3989. TRemoteTokenList * UserList = NULL;
  3990. try
  3991. {
  3992. TRemoteProperties CurrentProperties;
  3993. if (Terminal->LoadFilesProperties(FileList))
  3994. {
  3995. RemoteDirView->Invalidate();
  3996. }
  3997. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  3998. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  3999. if (CapableGroupChanging || CapableOwnerChanging)
  4000. {
  4001. if (CapableGroupChanging)
  4002. {
  4003. GroupList = Terminal->Groups->Duplicate();
  4004. }
  4005. if (CapableOwnerChanging)
  4006. {
  4007. UserList = Terminal->Users->Duplicate();
  4008. }
  4009. TRemoteDirectory * Files = Terminal->Files;
  4010. int Count = Files->Count;
  4011. if (Count > 100)
  4012. {
  4013. Count = 100;
  4014. }
  4015. for (int Index = 0; Index < Count; Index++)
  4016. {
  4017. TRemoteFile * File = Files->Files[Index];
  4018. if (CapableGroupChanging)
  4019. {
  4020. GroupList->AddUnique(File->Group);
  4021. }
  4022. if (CapableOwnerChanging)
  4023. {
  4024. UserList->AddUnique(File->Owner);
  4025. }
  4026. }
  4027. // if we haven't collected tokens for all files in current directory,
  4028. // make sure we collect them at least for all selected files.
  4029. // (note that so far the files in FileList has to be from current direcotry)
  4030. if (Count < Files->Count)
  4031. {
  4032. for (int Index = 0; Index < FileList->Count; Index++)
  4033. {
  4034. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4035. if (CapableGroupChanging)
  4036. {
  4037. GroupList->AddUnique(File->Group);
  4038. }
  4039. if (CapableOwnerChanging)
  4040. {
  4041. UserList->AddUnique(File->Owner);
  4042. }
  4043. }
  4044. }
  4045. }
  4046. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4047. int Flags = 0;
  4048. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4049. if (CapableOwnerChanging) Flags |= cpOwner;
  4050. if (CapableGroupChanging) Flags |= cpGroup;
  4051. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4052. if (Terminal->IsCapable[fcCalculatingChecksum])
  4053. {
  4054. CalculateChecksumEvent = CalculateChecksum;
  4055. }
  4056. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4057. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4058. TRemoteProperties NewProperties = CurrentProperties;
  4059. Result =
  4060. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4061. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4062. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4063. CalculateSize, CalculateChecksumEvent);
  4064. if (Result)
  4065. {
  4066. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4067. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4068. }
  4069. }
  4070. __finally
  4071. {
  4072. delete GroupList;
  4073. delete UserList;
  4074. }
  4075. }
  4076. else
  4077. {
  4078. DirView(Side)->DisplayPropertiesMenu();
  4079. Result = true;
  4080. }
  4081. return Result;
  4082. }
  4083. //---------------------------------------------------------------------------
  4084. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4085. {
  4086. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4087. {
  4088. FIgnoreNextDialogChar = Key;
  4089. }
  4090. Key = 0;
  4091. }
  4092. //---------------------------------------------------------------------------
  4093. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4094. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4095. {
  4096. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4097. if (Command != NULL)
  4098. {
  4099. KeyProcessed(Key, Shift);
  4100. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4101. {
  4102. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4103. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4104. false, false, const_cast<TCustomCommandType *>(Command));
  4105. }
  4106. }
  4107. }
  4108. //---------------------------------------------------------------------------
  4109. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4110. {
  4111. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4112. {
  4113. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4114. }
  4115. if (!DirView(osCurrent)->IsEditing())
  4116. {
  4117. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4118. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4119. {
  4120. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4121. if (((Action->ShortCut == KeyShortCut) ||
  4122. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4123. AllowedAction(Action, aaShortCut))
  4124. {
  4125. // Has to be called before the action as the dialog char is already in queue.
  4126. // So when the action consumes message queue, we already need to have the
  4127. // FIgnoreNextDialogChar set
  4128. KeyProcessed(Key, Shift);
  4129. // Reset reference to previous component (when menu/toolbar was clicked).
  4130. // Needed to detect that action was invoked by keyboard shortcut
  4131. // in TNonVisualDataModule::ExplorerActionsExecute
  4132. Action->ActionComponent = NULL;
  4133. Action->Execute();
  4134. return;
  4135. }
  4136. }
  4137. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4138. {
  4139. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4140. {
  4141. KeyProcessed(Key, Shift);
  4142. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4143. return;
  4144. }
  4145. }
  4146. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4147. {
  4148. KeyProcessed(Key, Shift);
  4149. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4150. }
  4151. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4152. if (IsCustomShortCut(CustomShortCut))
  4153. {
  4154. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4155. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4156. if (WinConfiguration->SharedBookmarks != NULL)
  4157. {
  4158. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4159. if ((Bookmark != NULL) &&
  4160. OpenBookmark(FCurrentSide, Bookmark))
  4161. {
  4162. KeyProcessed(Key, Shift);
  4163. }
  4164. }
  4165. }
  4166. }
  4167. TForm::KeyDown(Key, Shift);
  4168. }
  4169. //---------------------------------------------------------------------------
  4170. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4171. {
  4172. DebugAssert(Terminal != NULL);
  4173. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4174. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4175. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4176. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4177. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4178. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4179. // expanded from ?: to avoid memory leaks
  4180. if (SecurityEnabled)
  4181. {
  4182. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4183. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4184. }
  4185. else
  4186. {
  4187. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4188. }
  4189. if (FileSystemInfo.ProtocolName.IsEmpty())
  4190. {
  4191. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4192. }
  4193. else
  4194. {
  4195. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4196. }
  4197. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4198. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4199. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4200. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4201. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4202. UnicodeString CompressionStr = CSCompressionStr;
  4203. if (CSCompressionStr != SCCompressionStr)
  4204. {
  4205. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4206. }
  4207. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4208. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4209. UpdateStatusBar();
  4210. }
  4211. //---------------------------------------------------------------------------
  4212. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4213. {
  4214. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4215. DebugAssert(SessionStatusBar != NULL);
  4216. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4217. {
  4218. // note: (Terminal->Status < sshReady) currently never happens here,
  4219. // so STATUS_CONNECTING is never used
  4220. SessionStatusBar->SimplePanel = true;
  4221. SessionStatusBar->SimpleText = LoadStr(
  4222. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4223. }
  4224. else
  4225. {
  4226. DebugAssert(Terminal);
  4227. SessionStatusBar->SimplePanel = false;
  4228. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4229. if (!FNote.IsEmpty())
  4230. {
  4231. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4232. }
  4233. else
  4234. {
  4235. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4236. }
  4237. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4238. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4239. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4240. }
  4241. }
  4242. //---------------------------------------------------------------------------
  4243. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4244. {
  4245. Panel->Caption = L"";
  4246. }
  4247. //---------------------------------------------------------------------------
  4248. void __fastcall TCustomScpExplorerForm::Idle()
  4249. {
  4250. if (FShowing || FStandaloneEditing)
  4251. {
  4252. FEditorManager->Check();
  4253. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4254. // that needs to know if queue view is visible (and it may be closed after queue update)
  4255. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4256. }
  4257. if (FShowing)
  4258. {
  4259. if (!NonVisualDataModule->Busy &&
  4260. // Menu is opened or drag&drop is going on
  4261. (Mouse->Capture == NULL))
  4262. {
  4263. if (FRefreshRemoteDirectory)
  4264. {
  4265. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4266. {
  4267. Terminal->RefreshDirectory();
  4268. }
  4269. FRefreshRemoteDirectory = false;
  4270. }
  4271. if (FRefreshLocalDirectory)
  4272. {
  4273. ReloadLocalDirectory();
  4274. FRefreshLocalDirectory = false;
  4275. }
  4276. if (WinConfiguration->RefreshRemotePanel)
  4277. {
  4278. TManagedTerminal * ManagedTerminal =
  4279. dynamic_cast<TManagedTerminal *>(Terminal);
  4280. if ((ManagedTerminal != NULL) && (Terminal->Status == ssOpened) &&
  4281. (Now() - ManagedTerminal->DirectoryLoaded >
  4282. WinConfiguration->RefreshRemotePanelInterval))
  4283. {
  4284. RemoteDirView->ReloadDirectory();
  4285. }
  4286. }
  4287. }
  4288. }
  4289. if (FShowing || FStandaloneEditing)
  4290. {
  4291. if (FQueueStatusInvalidated)
  4292. {
  4293. UpdateQueueStatus(false);
  4294. }
  4295. RefreshQueueItems();
  4296. }
  4297. if (FShowing)
  4298. {
  4299. UpdateStatusBar();
  4300. }
  4301. FIgnoreNextDialogChar = 0;
  4302. }
  4303. //---------------------------------------------------------------------------
  4304. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4305. {
  4306. try
  4307. {
  4308. FUserActionTimer->Enabled = false;
  4309. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4310. {
  4311. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4312. {
  4313. FPendingQueueActionItem->ProcessUserAction();
  4314. }
  4315. }
  4316. }
  4317. __finally
  4318. {
  4319. FPendingQueueActionItem = NULL;
  4320. }
  4321. }
  4322. //---------------------------------------------------------------------------
  4323. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4324. {
  4325. if (WinConfiguration->MinimizeToTray)
  4326. {
  4327. UpdateTrayIcon();
  4328. FTrayIcon->Visible = true;
  4329. if (Visible)
  4330. {
  4331. ShowWindow(Handle, SW_HIDE);
  4332. }
  4333. }
  4334. }
  4335. //---------------------------------------------------------------------------
  4336. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4337. {
  4338. // WORKAROUND
  4339. // When restoring maximized window from minimization,
  4340. // rarely some controls do not align properly.
  4341. // Two instances seen (both for Commander):
  4342. // - When restoring, window is temporarily narrower (not maximized),
  4343. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4344. // Once maximized already, top dock shinks back, but the session PageControl,
  4345. // do not align up, leaving space between TopDock and PageControl.
  4346. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4347. for (int Index = 0; Index < ControlCount; Index++)
  4348. {
  4349. RealignControl(Controls[Index]);
  4350. }
  4351. if (FTrayIcon->Visible)
  4352. {
  4353. FTrayIcon->Visible = false;
  4354. if (Visible)
  4355. {
  4356. ShowWindow(Handle, SW_SHOW);
  4357. }
  4358. }
  4359. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4360. {
  4361. FNeedSession = false;
  4362. NonVisualDataModule->StartBusy();
  4363. try
  4364. {
  4365. NeedSession(false);
  4366. }
  4367. __finally
  4368. {
  4369. NonVisualDataModule->EndBusy();
  4370. }
  4371. }
  4372. }
  4373. //---------------------------------------------------------------------------
  4374. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4375. {
  4376. FTrayIcon->Hint = GetMainForm()->Caption;
  4377. }
  4378. //---------------------------------------------------------------------------
  4379. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4380. {
  4381. UpdateTrayIcon();
  4382. }
  4383. //---------------------------------------------------------------------------
  4384. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4385. {
  4386. // workaround
  4387. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4388. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4389. // But WM_ACTIVATEAPP message can be received even when
  4390. // the main window is minimized to the tray and internal editor window is focused
  4391. // (after another application was previously active)
  4392. if (::IsIconic(Handle))
  4393. {
  4394. if (!IsAppIconic())
  4395. {
  4396. SetAppIconic(true);
  4397. }
  4398. }
  4399. ::ApplicationRestore();
  4400. Application->BringToFront();
  4401. }
  4402. //---------------------------------------------------------------------------
  4403. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4404. {
  4405. RestoreApp();
  4406. }
  4407. //---------------------------------------------------------------------------
  4408. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4409. {
  4410. if (OpenInNewWindow())
  4411. {
  4412. // todo: Pass FromSite
  4413. ExecuteNewInstance(SessionUrl);
  4414. }
  4415. else
  4416. {
  4417. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4418. }
  4419. }
  4420. //---------------------------------------------------------------------------
  4421. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4422. {
  4423. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4424. // current working directories become defaults here, what is not right
  4425. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4426. StoredSessions->NewSession(SessionName, SessionData.get());
  4427. // modified only, explicit
  4428. StoredSessions->Save(false, true);
  4429. // encode session name because of slashes in hierarchical sessions
  4430. return EncodeUrlString(SessionName);
  4431. }
  4432. //---------------------------------------------------------------------------
  4433. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4434. {
  4435. if (OpenInNewWindow())
  4436. {
  4437. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4438. }
  4439. else
  4440. {
  4441. // current working directories become defaults here, what is not right
  4442. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4443. TTerminalManager * Manager = TTerminalManager::Instance();
  4444. TTerminal * Terminal = Manager->NewTerminal(SessionData.get());
  4445. Manager->ActiveTerminal = Terminal;
  4446. }
  4447. }
  4448. //---------------------------------------------------------------------------
  4449. void __fastcall TCustomScpExplorerForm::RenameSession()
  4450. {
  4451. UnicodeString Name = Terminal->SessionData->SessionName;
  4452. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4453. {
  4454. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4455. TSessionData::ValidateName(Name);
  4456. // This is inconsistent with how color (for example) is handled.
  4457. Terminal->SessionData->Name = Name;
  4458. UpdateControls();
  4459. // Add/Remove distinguishing paths from sessions of the same name.
  4460. DoTerminalListChanged();
  4461. }
  4462. }
  4463. //---------------------------------------------------------------------------
  4464. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4465. {
  4466. DebugAssert(Queue != NULL);
  4467. bool Result = Queue->IsEmpty;
  4468. if (!Result)
  4469. {
  4470. SetFocus();
  4471. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4472. }
  4473. return Result;
  4474. }
  4475. //---------------------------------------------------------------------------
  4476. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4477. {
  4478. return CanCloseQueue(FQueue);
  4479. }
  4480. //---------------------------------------------------------------------------
  4481. void __fastcall TCustomScpExplorerForm::CloseSession()
  4482. {
  4483. if (CanCloseQueue())
  4484. {
  4485. TTerminalManager::Instance()->FreeActiveTerminal();
  4486. }
  4487. }
  4488. //---------------------------------------------------------------------------
  4489. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4490. {
  4491. if (OpenInNewWindow())
  4492. {
  4493. // encode session name because of slashes in hierarchical sessions
  4494. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4495. }
  4496. else
  4497. {
  4498. TTerminalManager * Manager = TTerminalManager::Instance();
  4499. TTerminal * Terminal = Manager->NewTerminal(Data);
  4500. Manager->ActiveTerminal = Terminal;
  4501. }
  4502. }
  4503. //---------------------------------------------------------------------------
  4504. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4505. {
  4506. if (OpenInNewWindow())
  4507. {
  4508. ExecuteNewInstance(Name);
  4509. }
  4510. else
  4511. {
  4512. TTerminalManager * Manager = TTerminalManager::Instance();
  4513. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4514. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4515. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  4516. }
  4517. }
  4518. //---------------------------------------------------------------------------
  4519. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4520. bool &CanClose)
  4521. {
  4522. if (Terminal != NULL)
  4523. {
  4524. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4525. {
  4526. unsigned int Result;
  4527. TMessageParams Params(mpNeverAskAgainCheck);
  4528. UnicodeString Message;
  4529. int Answers = qaOK | qaCancel;
  4530. if (TTerminalManager::Instance()->Count > 1)
  4531. {
  4532. if (!WinConfiguration->AutoSaveWorkspace)
  4533. {
  4534. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE2);
  4535. Answers = qaYes | qaNo | qaCancel;
  4536. }
  4537. else
  4538. {
  4539. Message = MainInstructions(LoadStr(CLOSE_SESSIONS));
  4540. }
  4541. }
  4542. else
  4543. {
  4544. Message = MainInstructions(FMTLOAD(CLOSE_SESSION, (Terminal->SessionData->SessionName)));
  4545. }
  4546. if (WinConfiguration->AutoSaveWorkspace)
  4547. {
  4548. Message =
  4549. FORMAT("%s\n\n%s", (Message,
  4550. FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()))));
  4551. }
  4552. SetFocus();
  4553. Result = MessageDialog(Message, qtConfirmation,
  4554. Answers, HELP_NONE, &Params);
  4555. if (Result == qaNeverAskAgain)
  4556. {
  4557. WinConfiguration->ConfirmClosingSession = false;
  4558. }
  4559. if (Result == qaNo)
  4560. {
  4561. CanClose = SaveWorkspace(true);
  4562. // note that the workspace will be saved redundatly again from FormClose
  4563. }
  4564. else
  4565. {
  4566. CanClose =
  4567. (Result == qaOK) ||
  4568. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4569. (Result == qaNeverAskAgain);
  4570. }
  4571. }
  4572. if (CanClose)
  4573. {
  4574. CanClose = CanCloseQueue();
  4575. }
  4576. }
  4577. if (CanClose)
  4578. {
  4579. CanClose =
  4580. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4581. FEditorManager->CloseExternalFilesWithoutProcess();
  4582. if (CanClose)
  4583. {
  4584. while (CanClose && (FLocalEditors->Count > 0))
  4585. {
  4586. int PrevCount = FLocalEditors->Count;
  4587. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4588. CanClose = (FLocalEditors->Count < PrevCount);
  4589. }
  4590. if (CanClose)
  4591. {
  4592. CanClose = FEditorManager->Empty(true);
  4593. if (!CanClose)
  4594. {
  4595. SetFocus();
  4596. CanClose =
  4597. (MessageDialog(
  4598. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4599. }
  4600. }
  4601. }
  4602. }
  4603. }
  4604. //---------------------------------------------------------------------------
  4605. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4606. {
  4607. TForm * Form = dynamic_cast<TForm *>(Sender);
  4608. DebugAssert(Form != NULL);
  4609. Form->Close();
  4610. }
  4611. //---------------------------------------------------------------------------
  4612. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4613. {
  4614. TForm * Form = dynamic_cast<TForm *>(Sender);
  4615. delete Form;
  4616. }
  4617. //---------------------------------------------------------------------------
  4618. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4619. {
  4620. while (FLocalEditors->Count > 0)
  4621. {
  4622. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4623. }
  4624. }
  4625. //---------------------------------------------------------------------------
  4626. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4627. TObject *Sender)
  4628. {
  4629. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4630. try
  4631. {
  4632. SetProperties(osRemote, FileList);
  4633. }
  4634. __finally
  4635. {
  4636. delete FileList;
  4637. }
  4638. }
  4639. //---------------------------------------------------------------------------
  4640. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4641. {
  4642. if (value)
  4643. {
  4644. if (Component == fcCustomCommandsBand)
  4645. {
  4646. UpdateCustomCommandsToolbar();
  4647. }
  4648. }
  4649. }
  4650. //---------------------------------------------------------------------------
  4651. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4652. {
  4653. TControl * Control = GetComponent(Component);
  4654. DebugAssert(Control);
  4655. bool Changed = (Control->Visible != value);
  4656. if (Changed)
  4657. {
  4658. ComponentShowing(Component, value);
  4659. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4660. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4661. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4662. if (value)
  4663. {
  4664. int RemainingHeight = Control->Parent->ClientHeight;
  4665. int RemainingWidth = Control->Parent->ClientWidth;
  4666. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4667. {
  4668. TControl * ChildControl = Control->Parent->Controls[i];
  4669. if (ChildControl->Visible)
  4670. {
  4671. switch (ChildControl->Align)
  4672. {
  4673. case alTop:
  4674. case alBottom:
  4675. RemainingHeight -= ChildControl->Height;
  4676. break;
  4677. case alLeft:
  4678. case alRight:
  4679. RemainingWidth -= ChildControl->Width;
  4680. break;
  4681. }
  4682. }
  4683. }
  4684. int Reserve = ScaleByTextHeight(this, 32);
  4685. // queue in explorer, trees in commander
  4686. if (Control->Height > RemainingHeight - Reserve)
  4687. {
  4688. Control->Height = RemainingHeight / 2;
  4689. }
  4690. if (Control->Width > RemainingWidth - Reserve)
  4691. {
  4692. Control->Width = RemainingWidth / 2;
  4693. }
  4694. }
  4695. Control->Visible = value;
  4696. if (WasFocused && Visible)
  4697. {
  4698. DirView(osCurrent)->SetFocus();
  4699. }
  4700. FixControlsPlacement();
  4701. }
  4702. }
  4703. //---------------------------------------------------------------------------
  4704. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4705. {
  4706. TControl * Control = GetComponent(Component);
  4707. if (Control == NULL)
  4708. {
  4709. return false;
  4710. }
  4711. else
  4712. {
  4713. return Control->Visible;
  4714. }
  4715. }
  4716. //---------------------------------------------------------------------------
  4717. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4718. {
  4719. bool Result;
  4720. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4721. {
  4722. Result = !IsUWP();
  4723. }
  4724. else
  4725. {
  4726. Result = true;
  4727. }
  4728. return Result;
  4729. }
  4730. //---------------------------------------------------------------------------
  4731. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4732. {
  4733. if (DirView(osOther)->ItemFocused != NULL)
  4734. {
  4735. DirView(osOther)->ItemFocused->MakeVisible(false);
  4736. }
  4737. QueueSplitter->Visible = QueuePanel->Visible;
  4738. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4739. }
  4740. //---------------------------------------------------------------------------
  4741. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4742. {
  4743. switch (Component) {
  4744. case fcStatusBar: return RemoteStatusBar;
  4745. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4746. case fcQueueView: return QueuePanel;
  4747. case fcQueueToolbar: return QueueDock;
  4748. case fcRemoteTree: return RemoteDrivePanel;
  4749. case fcSessionsTabs: return SessionsPageControl;
  4750. default: return NULL;
  4751. }
  4752. }
  4753. //---------------------------------------------------------------------------
  4754. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4755. TObject *Sender, TListColumn *Column, TPoint &Point)
  4756. {
  4757. DebugAssert(NonVisualDataModule && Column && Sender);
  4758. NonVisualDataModule->ListColumn = Column;
  4759. TPopupMenu * DirViewColumnMenu;
  4760. if (dynamic_cast<TUnixDirView *>(Sender) != NULL)
  4761. {
  4762. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4763. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4764. (Column->Index == uvName);
  4765. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4766. (Column->Index == uvSize);
  4767. }
  4768. else
  4769. {
  4770. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4771. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4772. (Column->Index == dvName);
  4773. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4774. (Column->Index == dvSize);
  4775. }
  4776. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4777. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4778. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4779. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4780. }
  4781. //---------------------------------------------------------------------------
  4782. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4783. TObject *Sender, TListItem *Item, bool &AllowExec)
  4784. {
  4785. DoDirViewExecFile(Sender, Item, AllowExec);
  4786. }
  4787. //---------------------------------------------------------------------------
  4788. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4789. TListItem * Item, bool & AllowExec)
  4790. {
  4791. DebugAssert(Sender && Item && Configuration);
  4792. DebugAssert(AllowExec);
  4793. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4794. bool Remote = (ADirView == DirView(osRemote)) && !IsSideLocalBrowser(osRemote);
  4795. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4796. TOperationSide Side = (Remote ? osRemote : osLocal); // TODO
  4797. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4798. // on files only (not directories)
  4799. // and only when symlinks are resolved (apply to remote panel only)
  4800. if (!ADirView->ItemIsDirectory(Item) &&
  4801. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4802. {
  4803. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4804. !FForceExecution &&
  4805. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4806. {
  4807. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4808. {
  4809. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4810. UnlockWindow();
  4811. try
  4812. {
  4813. ExecuteFileOperation(
  4814. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4815. }
  4816. __finally
  4817. {
  4818. LockWindow();
  4819. }
  4820. AllowExec = false;
  4821. }
  4822. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4823. {
  4824. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4825. {
  4826. ExecuteFile(Side, efDefaultEditor);
  4827. AllowExec = false;
  4828. }
  4829. }
  4830. else
  4831. {
  4832. DebugFail();
  4833. }
  4834. }
  4835. // if we have not done anything special, fall back to default behavior
  4836. if (AllowExec)
  4837. {
  4838. if (Remote && !WinConfiguration->DisableOpenEdit)
  4839. {
  4840. ExecuteFile(osRemote, efShell);
  4841. AllowExec = false;
  4842. }
  4843. }
  4844. }
  4845. }
  4846. //---------------------------------------------------------------------------
  4847. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4848. {
  4849. return ((Side == osRemote) || (Side == osCurrent));
  4850. }
  4851. //---------------------------------------------------------------------------
  4852. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4853. {
  4854. DebugFail();
  4855. }
  4856. //---------------------------------------------------------------------------
  4857. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4858. {
  4859. DebugFail();
  4860. }
  4861. //---------------------------------------------------------------------------
  4862. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4863. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, bool UseDefaults)
  4864. {
  4865. TSynchronizeParamType Params;
  4866. Params.LocalDirectory = LocalDirectory;
  4867. Params.RemoteDirectory = RemoteDirectory;
  4868. int UnusedParams =
  4869. (GUIConfiguration->SynchronizeParams &
  4870. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4871. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4872. Params.Options = GUIConfiguration->SynchronizeOptions;
  4873. bool SaveSettings = false;
  4874. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4875. &DoSynchronizeTooManyDirectories);
  4876. DebugAssert(FSynchronizeController == NULL);
  4877. FSynchronizeController = &Controller;
  4878. bool Result;
  4879. try
  4880. {
  4881. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4882. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4883. int Options =
  4884. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4885. DebugAssert(FOnFeedSynchronizeError == NULL);
  4886. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4887. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4888. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4889. if (Result)
  4890. {
  4891. if (SaveSettings)
  4892. {
  4893. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4894. GUIConfiguration->SynchronizeOptions = Params.Options;
  4895. }
  4896. else
  4897. {
  4898. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4899. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4900. FLAGSET(Params.Options, soSynchronize))
  4901. {
  4902. GUIConfiguration->SynchronizeOptions =
  4903. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4904. soSynchronize;
  4905. }
  4906. }
  4907. LocalDirectory = Params.LocalDirectory;
  4908. RemoteDirectory = Params.RemoteDirectory;
  4909. }
  4910. }
  4911. __finally
  4912. {
  4913. FSynchronizeController = NULL;
  4914. DebugAssert(FOnFeedSynchronizeError == NULL);
  4915. FOnFeedSynchronizeError = NULL;
  4916. }
  4917. return Result;
  4918. }
  4919. //---------------------------------------------------------------------------
  4920. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4921. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4922. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4923. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4924. TSynchronizeOptions * Options, bool Full)
  4925. {
  4926. // Keep-up-to-date mode
  4927. if (Terminal->Status == ssOpened)
  4928. {
  4929. try
  4930. {
  4931. int PParams = Params.Params;
  4932. if (!Full)
  4933. {
  4934. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4935. if (FLAGSET(Params.Options, soRecurse))
  4936. {
  4937. PParams |= TTerminal::spSubDirs;
  4938. }
  4939. }
  4940. else
  4941. {
  4942. // if keepuptodate is non-recursive,
  4943. // full sync before has to be non-recursive as well
  4944. if (FLAGCLEAR(Params.Options, soRecurse))
  4945. {
  4946. PParams |= TTerminal::spNoRecurse;
  4947. }
  4948. }
  4949. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  4950. PParams, Checklist, Options);
  4951. }
  4952. catch (Exception & E)
  4953. {
  4954. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  4955. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  4956. {
  4957. // noop, already logged in MoreMessageDialog
  4958. }
  4959. else
  4960. {
  4961. ShowExtendedExceptionEx(Terminal, &E);
  4962. throw;
  4963. }
  4964. }
  4965. }
  4966. }
  4967. //---------------------------------------------------------------------------
  4968. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  4969. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  4970. const UnicodeString ErrorStr)
  4971. {
  4972. if (!Directory.IsEmpty())
  4973. {
  4974. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  4975. }
  4976. else
  4977. {
  4978. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  4979. }
  4980. }
  4981. //---------------------------------------------------------------------------
  4982. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  4983. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  4984. {
  4985. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  4986. {
  4987. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  4988. }
  4989. else
  4990. {
  4991. TMessageParams Params(mpNeverAskAgainCheck);
  4992. unsigned int Result = MessageDialog(
  4993. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  4994. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  4995. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  4996. {
  4997. MaxDirectories *= 2;
  4998. if (Result == qaNeverAskAgain)
  4999. {
  5000. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5001. }
  5002. }
  5003. else
  5004. {
  5005. Abort();
  5006. }
  5007. }
  5008. }
  5009. //---------------------------------------------------------------------------
  5010. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5011. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5012. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5013. TSynchronizeOptions * Options)
  5014. {
  5015. // Keep-up-to-date mode
  5016. DebugAssert(!FAutoOperation);
  5017. void * BatchStorage;
  5018. BatchStart(BatchStorage);
  5019. FAutoOperation = true;
  5020. bool AnyOperation = false;
  5021. TDateTime StartTime = Now();
  5022. TSynchronizeChecklist * AChecklist = NULL;
  5023. try
  5024. {
  5025. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  5026. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5027. {
  5028. FSynchronizeProgressForm->Start();
  5029. }
  5030. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5031. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5032. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5033. Options);
  5034. SAFE_DESTROY(FSynchronizeProgressForm);
  5035. AnyOperation = (AChecklist->CheckedCount > 0);
  5036. if (AnyOperation)
  5037. {
  5038. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5039. CreateProgressForm(&SynchronizeProgress);
  5040. try
  5041. {
  5042. Terminal->SynchronizeApply(
  5043. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5044. SynchronizeProcessedItem, NULL, NULL, NULL);
  5045. }
  5046. __finally
  5047. {
  5048. DestroyProgressForm();
  5049. }
  5050. }
  5051. }
  5052. __finally
  5053. {
  5054. if (Checklist == NULL)
  5055. {
  5056. delete AChecklist;
  5057. }
  5058. else
  5059. {
  5060. *Checklist = AChecklist;
  5061. }
  5062. FAutoOperation = false;
  5063. SAFE_DESTROY(FSynchronizeProgressForm);
  5064. BatchEnd(BatchStorage);
  5065. ReloadLocalDirectory();
  5066. if (AnyOperation)
  5067. {
  5068. OperationComplete(StartTime);
  5069. }
  5070. }
  5071. }
  5072. //---------------------------------------------------------------------------
  5073. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5074. {
  5075. // can be called from command line
  5076. return Visible &&
  5077. ((DirView(osOther)->SelCount > 0) ||
  5078. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5079. }
  5080. //---------------------------------------------------------------------------
  5081. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5082. {
  5083. LogSynchronizeEvent(Terminal, Message);
  5084. }
  5085. //---------------------------------------------------------------------------
  5086. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5087. int Params, TSynchronizeOptions & Options)
  5088. {
  5089. DebugAssert(!IsLocalBrowserMode());
  5090. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5091. {
  5092. Options.Filter = new TStringList();
  5093. Options.Filter->CaseSensitive = false;
  5094. Options.Filter->Duplicates = Types::dupAccept;
  5095. if (DirView(osRemote)->SelCount > 0)
  5096. {
  5097. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5098. }
  5099. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5100. {
  5101. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5102. }
  5103. Options.Filter->Sort();
  5104. }
  5105. }
  5106. //---------------------------------------------------------------------------
  5107. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5108. {
  5109. TCopyParamType Defaults;
  5110. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5111. ConfigStorage->AccessMode = smRead;
  5112. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false, false))
  5113. {
  5114. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5115. }
  5116. ConfigStorage.reset(NULL);
  5117. std::unique_ptr<TStringList> Options(new TStringList());
  5118. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5119. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5120. UnicodeString Result;
  5121. if (Options->Count > 0)
  5122. {
  5123. Result =
  5124. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5125. }
  5126. return Result;
  5127. }
  5128. //---------------------------------------------------------------------------
  5129. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5130. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5131. {
  5132. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5133. UnicodeString AdditionalParams =
  5134. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5135. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5136. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  5137. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5138. SerializeCopyParamForCommandLine(CopyParams)));
  5139. ExecuteNewInstance(SessionName, AdditionalParams);
  5140. }
  5141. //---------------------------------------------------------------------------
  5142. struct TSynchronizeParams
  5143. {
  5144. UnicodeString LocalDirectory;
  5145. UnicodeString RemoteDirectory;
  5146. TSynchronizeMode Mode;
  5147. TSynchronizeChecklist * Checklist;
  5148. int Params;
  5149. TCopyParamType * CopyParam;
  5150. TDateTime CollectElapsed;
  5151. TDateTime * StartTime;
  5152. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5153. };
  5154. //---------------------------------------------------------------------------
  5155. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5156. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5157. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5158. {
  5159. DebugAssert(!FAutoOperation);
  5160. void * BatchStorage;
  5161. BatchStart(BatchStorage);
  5162. FAutoOperation = true;
  5163. TFileOperationStatistics Statistics;
  5164. TDateTime Start = Now();
  5165. try
  5166. {
  5167. Params.OnProcessedItem = OnProcessedItem;
  5168. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5169. CreateProgressForm(&SynchronizeProgress);
  5170. Terminal->SynchronizeApply(
  5171. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5172. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5173. &Statistics);
  5174. }
  5175. __finally
  5176. {
  5177. Params.OnProcessedItem = NULL;
  5178. FAutoOperation = false;
  5179. DestroyProgressForm();
  5180. BatchEnd(BatchStorage);
  5181. ReloadLocalDirectory();
  5182. }
  5183. if (WinConfiguration->SynchronizeSummary)
  5184. {
  5185. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5186. // The statistics should be 0 anyway in this case
  5187. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5188. {
  5189. Message += L"\n";
  5190. if (Statistics.FilesUploaded > 0)
  5191. {
  5192. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5193. }
  5194. if (Statistics.FilesDownloaded > 0)
  5195. {
  5196. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5197. }
  5198. if (Statistics.FilesDeletedLocal > 0)
  5199. {
  5200. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5201. }
  5202. if (Statistics.FilesDeletedRemote > 0)
  5203. {
  5204. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5205. }
  5206. }
  5207. TDateTime Elapsed = (Now() - Start);
  5208. Message +=
  5209. L"\n" +
  5210. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5211. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5212. TMessageParams Params(mpNeverAskAgainCheck);
  5213. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5214. if (Result == qaNeverAskAgain)
  5215. {
  5216. WinConfiguration->SynchronizeSummary = false;
  5217. }
  5218. }
  5219. }
  5220. //---------------------------------------------------------------------------
  5221. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5222. {
  5223. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5224. {
  5225. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5226. }
  5227. // Not set in keep-up-to-date mode - Synchronize() method
  5228. if (Token != NULL)
  5229. {
  5230. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5231. if (Params.OnProcessedItem != NULL)
  5232. {
  5233. Params.OnProcessedItem(NULL, ChecklistItem);
  5234. }
  5235. }
  5236. }
  5237. //---------------------------------------------------------------------------
  5238. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5239. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5240. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5241. {
  5242. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5243. *Params.StartTime = Now();
  5244. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5245. }
  5246. //---------------------------------------------------------------------------
  5247. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5248. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5249. {
  5250. // terminal can be already closed (e.g. dropped connection)
  5251. if (Terminal != NULL)
  5252. {
  5253. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5254. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5255. }
  5256. }
  5257. //---------------------------------------------------------------------------
  5258. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5259. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5260. {
  5261. DebugAssert(!IsLocalBrowserMode());
  5262. TAutoBatch AutoBatch(this);
  5263. TAutoFlag AutoOperationFlag(FAutoOperation);
  5264. if (Side == osRemote)
  5265. {
  5266. std::unique_ptr<TStrings> FileList(new TStringList());
  5267. FileList->AddObject(FileName, RemoteFile);
  5268. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5269. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5270. RemoteDirView->SaveSelection();
  5271. RemoteDirView->SaveSelectedNames();
  5272. try
  5273. {
  5274. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5275. }
  5276. catch(...)
  5277. {
  5278. RemoteDirView->DiscardSavedSelection();
  5279. throw;
  5280. }
  5281. RemoteDirView->RestoreSelection();
  5282. }
  5283. else if (DebugAlwaysTrue(Side == osLocal))
  5284. {
  5285. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5286. {
  5287. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5288. }
  5289. UnicodeString Directory = ExtractFileDir(FileName);
  5290. ReloadLocalDirectory(Directory);
  5291. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5292. if (!SamePaths(Directory, NewDirectory))
  5293. {
  5294. ReloadLocalDirectory(NewDirectory);
  5295. }
  5296. }
  5297. }
  5298. //---------------------------------------------------------------------------
  5299. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5300. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5301. const TCopyParamType * CopyParams)
  5302. {
  5303. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5304. UnicodeString AdditionalParams =
  5305. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5306. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5307. LocalDirectory, RemoteDirectory, Mode, Params,
  5308. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5309. SerializeCopyParamForCommandLine(CopyParams)));
  5310. ExecuteNewInstance(SessionName, AdditionalParams);
  5311. }
  5312. //---------------------------------------------------------------------------
  5313. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5314. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5315. TSynchronizeMode & Mode, int Params, bool & SaveMode, bool UseDefaults)
  5316. {
  5317. int Result;
  5318. bool SaveSettings = false;
  5319. int Options =
  5320. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5321. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5322. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5323. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5324. bool Continue =
  5325. UseDefaults ||
  5326. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5327. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow);
  5328. if (Continue)
  5329. {
  5330. Configuration->Usage->Inc(L"Synchronizations");
  5331. UpdateCopyParamCounters(CopyParam);
  5332. TSynchronizeOptions SynchronizeOptions;
  5333. GetSynchronizeOptions(Params, SynchronizeOptions);
  5334. if (SaveSettings)
  5335. {
  5336. GUIConfiguration->SynchronizeParams = Params;
  5337. }
  5338. else
  5339. {
  5340. SaveMode = false;
  5341. }
  5342. TDateTime StartTime = Now();
  5343. TSynchronizeChecklist * Checklist = NULL;
  5344. try
  5345. {
  5346. DebugAssert(!FAutoOperation);
  5347. FAutoOperation = true;
  5348. try
  5349. {
  5350. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  5351. FSynchronizeProgressForm->Start();
  5352. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5353. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5354. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5355. &SynchronizeOptions);
  5356. }
  5357. __finally
  5358. {
  5359. FAutoOperation = false;
  5360. SAFE_DESTROY(FSynchronizeProgressForm);
  5361. }
  5362. TSynchronizeParams SynchronizeParams;
  5363. SynchronizeParams.LocalDirectory = LocalDirectory;
  5364. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5365. SynchronizeParams.Mode = Mode;
  5366. SynchronizeParams.CopyParam = &CopyParam;
  5367. SynchronizeParams.Params = Params;
  5368. SynchronizeParams.Checklist = Checklist;
  5369. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5370. SynchronizeParams.StartTime = &StartTime;
  5371. SynchronizeParams.OnProcessedItem = NULL;
  5372. Result = Checklist->Count;
  5373. if (Checklist->Count > 0)
  5374. {
  5375. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5376. {
  5377. if (!DoSynchronizeChecklistDialog(
  5378. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5379. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, &SynchronizeParams))
  5380. {
  5381. Result = -1;
  5382. }
  5383. }
  5384. else
  5385. {
  5386. FullSynchronize(SynchronizeParams, NULL, NULL);
  5387. }
  5388. }
  5389. else
  5390. {
  5391. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5392. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5393. }
  5394. }
  5395. __finally
  5396. {
  5397. delete Checklist;
  5398. }
  5399. OperationComplete(StartTime);
  5400. }
  5401. else
  5402. {
  5403. Result = -1;
  5404. }
  5405. return Result;
  5406. }
  5407. //---------------------------------------------------------------------------
  5408. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5409. const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
  5410. bool & Continue, bool Collect)
  5411. {
  5412. if (Collect)
  5413. {
  5414. DebugAssert(FSynchronizeProgressForm != NULL);
  5415. if (!FSynchronizeProgressForm->Started)
  5416. {
  5417. FSynchronizeProgressForm->Start();
  5418. }
  5419. FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Continue);
  5420. }
  5421. else
  5422. {
  5423. DebugAssert(FSynchronizeProgressForm == NULL);
  5424. DebugAssert(FProgressForm != NULL);
  5425. }
  5426. }
  5427. //---------------------------------------------------------------------------
  5428. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5429. {
  5430. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5431. TRemoteFile * File = NULL;
  5432. Terminal->ReadFile(FullFileName, File);
  5433. if (File != NULL)
  5434. {
  5435. std::unique_ptr<TRemoteFile> FileOwner(File);
  5436. TAutoFlag Flag(FStandaloneEditing);
  5437. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5438. Application->ShowMainForm = false;
  5439. Application->Run();
  5440. }
  5441. }
  5442. //---------------------------------------------------------------------------
  5443. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5444. {
  5445. DebugFail();
  5446. }
  5447. //---------------------------------------------------------------------------
  5448. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5449. {
  5450. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5451. SessionData->Assign(Terminal->SessionData);
  5452. UpdateSessionData(SessionData.get());
  5453. Terminal->UpdateSessionCredentials(SessionData.get());
  5454. if (!Terminal->SessionData->HasSessionName())
  5455. {
  5456. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5457. // once IsWorkspace is cleared
  5458. SessionData->Name = UnicodeString();
  5459. SessionData->IsWorkspace = false;
  5460. }
  5461. return SessionData.release();
  5462. }
  5463. //---------------------------------------------------------------------------
  5464. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5465. {
  5466. TSessionData * SessionData = CloneCurrentSessionData();
  5467. try
  5468. {
  5469. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5470. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5471. }
  5472. __finally
  5473. {
  5474. delete SessionData;
  5475. }
  5476. }
  5477. //---------------------------------------------------------------------------
  5478. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5479. {
  5480. TTerminalManager * Manager = TTerminalManager::Instance();
  5481. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5482. if (DebugAlwaysTrue(Terminal != NULL))
  5483. {
  5484. // Update (Managed)Terminal->StateData
  5485. UpdateTerminal(Terminal);
  5486. }
  5487. Manager->SaveWorkspace(DataList.get());
  5488. return DataList.release();
  5489. }
  5490. //---------------------------------------------------------------------------
  5491. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5492. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5493. {
  5494. WinConfiguration->LastWorkspace = Name;
  5495. if (!SavePasswords)
  5496. {
  5497. for (int Index = 0; Index < DataList->Count; Index++)
  5498. {
  5499. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5500. if (SessionData->Link.IsEmpty())
  5501. {
  5502. SessionData->ClearSessionPasswords();
  5503. }
  5504. }
  5505. }
  5506. StoredSessions->NewWorkspace(Name, DataList);
  5507. // modified only
  5508. StoredSessions->Save(false, Explicit);
  5509. }
  5510. //---------------------------------------------------------------------------
  5511. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5512. {
  5513. return
  5514. DefaultStr(WinConfiguration->LastWorkspace,
  5515. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5516. }
  5517. //---------------------------------------------------------------------------
  5518. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5519. {
  5520. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5521. bool AnyNonStoredSessionWithPassword = false;
  5522. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5523. bool AllNonStoredSessionsAnonymous = true;
  5524. TTerminalManager * Manager = TTerminalManager::Instance();
  5525. for (int Index = 0; Index < DataList->Count; Index++)
  5526. {
  5527. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5528. if (SessionData->Link.IsEmpty())
  5529. {
  5530. if (SessionData->HasAnySessionPassword())
  5531. {
  5532. AnyNonStoredSessionWithPassword = true;
  5533. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5534. {
  5535. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5536. }
  5537. }
  5538. if (!SameText(SessionData->UserName, AnonymousUserName))
  5539. {
  5540. AllNonStoredSessionsAnonymous = false;
  5541. }
  5542. }
  5543. }
  5544. bool SavePasswords;
  5545. bool * PSavePasswords;
  5546. bool NotRecommendedSavingPasswords =
  5547. !CustomWinConfiguration->UseMasterPassword &&
  5548. !AllNonStoredSessionsAnonymous;
  5549. if (Configuration->DisablePasswordStoring ||
  5550. !AnyNonStoredSessionWithPassword)
  5551. {
  5552. PSavePasswords = NULL;
  5553. SavePasswords = false;
  5554. }
  5555. else
  5556. {
  5557. PSavePasswords = &SavePasswords;
  5558. SavePasswords =
  5559. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5560. !NotRecommendedSavingPasswords;
  5561. }
  5562. UnicodeString Name = WorkspaceName();
  5563. bool CreateShortcut = false;
  5564. bool Result =
  5565. DoSaveWorkspaceDialog(
  5566. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5567. EnableAutoSave);
  5568. if (Result)
  5569. {
  5570. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5571. if (CreateShortcut)
  5572. {
  5573. TOperationVisualizer Visualizer;
  5574. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5575. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5576. }
  5577. if (EnableAutoSave)
  5578. {
  5579. WinConfiguration->AutoSaveWorkspace = true;
  5580. WinConfiguration->AutoWorkspace = Name;
  5581. if (PSavePasswords != NULL)
  5582. {
  5583. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5584. }
  5585. }
  5586. }
  5587. return Result;
  5588. }
  5589. //---------------------------------------------------------------------------
  5590. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TTerminal * Terminal)
  5591. {
  5592. DebugAssert(!IsLocalBrowserMode());
  5593. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  5594. DebugAssert(ManagedTerminal != NULL);
  5595. SAFE_DESTROY(ManagedTerminal->RemoteExplorerState);
  5596. if (WinConfiguration->PreservePanelState)
  5597. {
  5598. ManagedTerminal->RemoteExplorerState = RemoteDirView->SaveState();
  5599. }
  5600. UpdateSessionData(ManagedTerminal->StateData);
  5601. }
  5602. //---------------------------------------------------------------------------
  5603. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5604. {
  5605. // Keep in sync with TSessionData::CopyStateData
  5606. DebugAssert(!IsLocalBrowserMode());
  5607. DebugAssert(Data != NULL);
  5608. // This is inconsistent with how color (for example) is handled.
  5609. Data->Name = Terminal->SessionData->Name;
  5610. // cannot use RemoteDirView->Path, because it is empty if connection
  5611. // was already closed
  5612. // also only peek, we may not be connected at all atm
  5613. // so make sure we do not try retrieving current directory from the server
  5614. // (particularly with FTP)
  5615. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5616. if (!ACurrentDirectory.IsEmpty())
  5617. {
  5618. Data->RemoteDirectory = ACurrentDirectory;
  5619. }
  5620. Data->Color = SessionColor;
  5621. }
  5622. //---------------------------------------------------------------------------
  5623. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5624. {
  5625. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5626. DebugAssert(Toolbar != NULL);
  5627. for (int i = 0; i < Toolbar->Items->Count; i++)
  5628. {
  5629. TTBXCustomDropDownItem * DropDownItem;
  5630. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5631. if (DropDownItem != NULL)
  5632. {
  5633. ToolbarItemResize(DropDownItem,
  5634. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5635. Toolbar->NonClientWidth);
  5636. break;
  5637. }
  5638. }
  5639. }
  5640. //---------------------------------------------------------------------------
  5641. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5642. {
  5643. Item->EditWidth = Width;
  5644. }
  5645. //---------------------------------------------------------------------------
  5646. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5647. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5648. {
  5649. for (int i = 0; i < Toolbar->Items->Count; i++)
  5650. {
  5651. TTBXCustomDropDownItem * DropDownItem;
  5652. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5653. if (DropDownItem != NULL)
  5654. {
  5655. ASize.x -= DropDownItem->EditWidth;
  5656. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5657. }
  5658. }
  5659. }
  5660. //---------------------------------------------------------------------------
  5661. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5662. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5663. {
  5664. if (WinConfiguration->DDWarnLackOfTempSpace)
  5665. {
  5666. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5667. if (!ADrive.IsEmpty())
  5668. {
  5669. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5670. DebugAssert(RequiredSpace >= 0);
  5671. __int64 RequiredWithReserve;
  5672. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5673. if (FreeSpace < RequiredWithReserve)
  5674. {
  5675. unsigned int Result;
  5676. TMessageParams Params(mpNeverAskAgainCheck);
  5677. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5678. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5679. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5680. if (Result == qaNeverAskAgain)
  5681. {
  5682. WinConfiguration->DDWarnLackOfTempSpace = false;
  5683. }
  5684. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5685. }
  5686. }
  5687. }
  5688. }
  5689. //---------------------------------------------------------------------------
  5690. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5691. {
  5692. DoOpenDirectoryDialog(odAddBookmark, Side);
  5693. }
  5694. //---------------------------------------------------------------------------
  5695. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5696. {
  5697. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5698. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  5699. try
  5700. {
  5701. TCustomDirView * DView = DirView(Side);
  5702. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5703. {
  5704. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5705. }
  5706. }
  5707. catch (...)
  5708. {
  5709. delete VisitedDirectories;
  5710. throw;
  5711. }
  5712. return VisitedDirectories;
  5713. }
  5714. //---------------------------------------------------------------------------
  5715. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5716. TOpenDirectoryMode Mode, TOperationSide Side)
  5717. {
  5718. bool Continue = true;
  5719. if (Mode == odAddBookmark)
  5720. {
  5721. TMessageParams Params(mpNeverAskAgainCheck);
  5722. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5723. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5724. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5725. unsigned int Answer =
  5726. MessageDialog(Message,
  5727. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5728. if (Answer == qaNeverAskAgain)
  5729. {
  5730. Continue = true;
  5731. WinConfiguration->UseSharedBookmarks = true;
  5732. }
  5733. else if (Answer == qaOK)
  5734. {
  5735. Continue = true;
  5736. WinConfiguration->UseSharedBookmarks = false;
  5737. }
  5738. else
  5739. {
  5740. Continue = false;
  5741. }
  5742. }
  5743. if (Continue)
  5744. {
  5745. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5746. try
  5747. {
  5748. UnicodeString Name = DirView(Side)->PathName;
  5749. bool Repeat;
  5750. do
  5751. {
  5752. Repeat = false;
  5753. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5754. // do not allow switching to location profiles,
  5755. // if we are not connected
  5756. HasDirView[osLocal] && (Terminal != NULL)))
  5757. {
  5758. TWindowLock Lock(this);
  5759. if (!TryOpenDirectory(Side, Name))
  5760. {
  5761. Repeat = true;
  5762. Mode = odBrowse;
  5763. }
  5764. }
  5765. }
  5766. while (Repeat);
  5767. }
  5768. __finally
  5769. {
  5770. delete VisitedDirectories;
  5771. }
  5772. }
  5773. }
  5774. //---------------------------------------------------------------------------
  5775. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5776. {
  5777. DebugAssert(!FTerminal->CommandSessionOpened);
  5778. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5779. }
  5780. //---------------------------------------------------------------------------
  5781. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5782. {
  5783. bool Result = FTerminal->IsCapable[Capability] ||
  5784. FTerminal->CommandSessionOpened;
  5785. if (!Result)
  5786. {
  5787. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5788. if (!GUIConfiguration->ConfirmCommandSession)
  5789. {
  5790. Result = true;
  5791. }
  5792. else
  5793. {
  5794. TMessageParams Params(mpNeverAskAgainCheck);
  5795. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5796. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5797. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5798. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5799. if (Answer == qaNeverAskAgain)
  5800. {
  5801. GUIConfiguration->ConfirmCommandSession = false;
  5802. Result = true;
  5803. }
  5804. else if (Answer == qaOK)
  5805. {
  5806. Result = true;
  5807. }
  5808. }
  5809. if (Result)
  5810. {
  5811. Result = CommandSessionFallback();
  5812. }
  5813. }
  5814. return Result;
  5815. }
  5816. //---------------------------------------------------------------------------
  5817. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5818. {
  5819. if (EnsureCommandSessionFallback(fcAnyCommand))
  5820. {
  5821. DoConsoleDialog(Terminal, Command);
  5822. }
  5823. }
  5824. //---------------------------------------------------------------------------
  5825. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5826. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5827. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5828. {
  5829. if (!IsSideLocalBrowser(osRemote) &&
  5830. IsFileControl(DropSourceControl, osRemote) &&
  5831. (FDDExtMapFile != NULL))
  5832. {
  5833. Accept = true;
  5834. }
  5835. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5836. DebugAssert(FDDTargetControl != NULL);
  5837. }
  5838. //---------------------------------------------------------------------------
  5839. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5840. _di_IDataObject DataObj, int KeyState,
  5841. const TPoint & Point, int & Effect, bool & Accept)
  5842. {
  5843. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5844. }
  5845. //---------------------------------------------------------------------------
  5846. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5847. _di_IDataObject DataObj, int KeyState,
  5848. const TPoint & Point, int & Effect, bool & Accept)
  5849. {
  5850. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5851. }
  5852. //---------------------------------------------------------------------------
  5853. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5854. TObject * Sender)
  5855. {
  5856. DebugUsedParam(Sender);
  5857. DebugAssert(FDDTargetControl == Sender);
  5858. FDDTargetControl = NULL;
  5859. }
  5860. //---------------------------------------------------------------------------
  5861. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5862. {
  5863. FileControlDDDragLeave(SessionsPageControl);
  5864. }
  5865. //---------------------------------------------------------------------------
  5866. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5867. {
  5868. FileControlDDDragLeave(QueueView3);
  5869. }
  5870. //---------------------------------------------------------------------------
  5871. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5872. {
  5873. DebugAssert(!IsSideLocalBrowser(Side));
  5874. DebugUsedParam(Side);
  5875. bool Edit = false;
  5876. TRemoteFile * File = NULL;
  5877. UnicodeString FileName;
  5878. UnicodeString PointTo;
  5879. bool SymbolicLink = true;
  5880. if (RemoteDirView->ItemFocused)
  5881. {
  5882. DebugAssert(RemoteDirView->ItemFocused->Data);
  5883. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5884. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5885. if (Edit)
  5886. {
  5887. FileName = File->FileName;
  5888. PointTo = File->LinkTo;
  5889. }
  5890. else
  5891. {
  5892. PointTo = File->FileName;
  5893. }
  5894. }
  5895. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5896. Terminal->IsCapable[fcHardLink]))
  5897. {
  5898. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5899. if (Edit)
  5900. {
  5901. DebugAssert(File->FileName == FileName);
  5902. int Params = dfNoRecursive;
  5903. Terminal->ExceptionOnFail = true;
  5904. try
  5905. {
  5906. Terminal->DeleteFile(L"", File, &Params);
  5907. }
  5908. __finally
  5909. {
  5910. Terminal->ExceptionOnFail = false;
  5911. }
  5912. }
  5913. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  5914. }
  5915. }
  5916. //---------------------------------------------------------------------------
  5917. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  5918. {
  5919. return
  5920. (IsSideLocalBrowser(Side) ||
  5921. ((Terminal != NULL) &&
  5922. Terminal->ResolvingSymlinks &&
  5923. Terminal->IsCapable[fcSymbolicLink]));
  5924. }
  5925. //---------------------------------------------------------------------------
  5926. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  5927. {
  5928. return
  5929. !IsSideLocalBrowser(FCurrentSide) &&
  5930. (RemoteDirView->ItemFocused != NULL) &&
  5931. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  5932. Terminal->SessionData->ResolveSymlinks;
  5933. }
  5934. //---------------------------------------------------------------------------
  5935. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  5936. {
  5937. DebugAssert(!WinConfiguration->DisableOpenEdit);
  5938. FForceExecution = true;
  5939. try
  5940. {
  5941. DirView(osCurrent)->ExecuteCurrentFile();
  5942. }
  5943. __finally
  5944. {
  5945. FForceExecution = false;
  5946. }
  5947. }
  5948. //---------------------------------------------------------------------------
  5949. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  5950. {
  5951. TEditorData ExternalEditor;
  5952. ExternalEditor.Editor = edExternal;
  5953. bool Remember = false;
  5954. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  5955. {
  5956. if (Remember)
  5957. {
  5958. TEditorList * EditorList = new TEditorList;
  5959. try
  5960. {
  5961. *EditorList = *WinConfiguration->EditorList;
  5962. bool Found = false;
  5963. int i = 0;
  5964. while (!Found && (i < EditorList->Count))
  5965. {
  5966. const TEditorPreferences * Editor = EditorList->Editors[i];
  5967. if ((Editor->Data->Editor == edExternal) &&
  5968. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  5969. {
  5970. Found = true;
  5971. }
  5972. i++;
  5973. }
  5974. if (!Found)
  5975. {
  5976. EditorList->Add(new TEditorPreferences(ExternalEditor));
  5977. WinConfiguration->EditorList = EditorList;
  5978. }
  5979. }
  5980. __finally
  5981. {
  5982. delete EditorList;
  5983. }
  5984. }
  5985. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  5986. }
  5987. }
  5988. //---------------------------------------------------------------------------
  5989. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  5990. {
  5991. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  5992. if (FFileFindTerminal == Sender)
  5993. {
  5994. FFileFindTerminal = NULL;
  5995. HideFileFindDialog();
  5996. }
  5997. if (FClipboardTerminal == Sender)
  5998. {
  5999. ClipboardClear(); // implies ClipboardStop
  6000. }
  6001. }
  6002. //---------------------------------------------------------------------------
  6003. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6004. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6005. {
  6006. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6007. DebugAssert(Terminal != NULL);
  6008. if (Data->Terminal == Terminal)
  6009. {
  6010. Data->Terminal = NULL;
  6011. }
  6012. }
  6013. //---------------------------------------------------------------------------
  6014. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  6015. {
  6016. UpdateControls();
  6017. SessionColor = TColor(0);
  6018. UpdateRemotePathComboBox(false);
  6019. NeedSession(true);
  6020. }
  6021. //---------------------------------------------------------------------------
  6022. void __fastcall TCustomScpExplorerForm::NeedSession(bool ReloadSessions)
  6023. {
  6024. try
  6025. {
  6026. try
  6027. {
  6028. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  6029. }
  6030. __finally
  6031. {
  6032. if (!WinConfiguration->KeepOpenWhenNoSession &&
  6033. (!Terminal || !Terminal->Active))
  6034. {
  6035. TerminateApplication();
  6036. }
  6037. }
  6038. }
  6039. catch (EAbort &)
  6040. {
  6041. // swallow
  6042. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6043. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6044. }
  6045. }
  6046. //---------------------------------------------------------------------------
  6047. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  6048. {
  6049. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  6050. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6051. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  6052. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6053. try
  6054. {
  6055. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  6056. // Clear the design time unthemed tab
  6057. ((SessionsPageControl->PageCount > 0) &&
  6058. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6059. {
  6060. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6061. }
  6062. for (int Index = 0; Index <= TerminalList->Count; Index++)
  6063. {
  6064. TThemeTabSheet * TabSheet;
  6065. if (Index >= SessionsPageControl->PageCount)
  6066. {
  6067. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6068. TabSheet->PageControl = SessionsPageControl;
  6069. }
  6070. else
  6071. {
  6072. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6073. }
  6074. bool IsSessionTab = (Index < TerminalList->Count);
  6075. if (IsSessionTab)
  6076. {
  6077. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  6078. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6079. UpdateSessionTab(TabSheet);
  6080. }
  6081. else
  6082. {
  6083. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6084. TabSheet->Tag = 0; // not really needed
  6085. // We know that we are at the last page, sotherwise we could not call this (it assumes that new session tab is the last one)
  6086. UpdateNewSessionTab();
  6087. }
  6088. TabSheet->ShowCloseButton = IsSessionTab;
  6089. }
  6090. }
  6091. __finally
  6092. {
  6093. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6094. }
  6095. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6096. }
  6097. //---------------------------------------------------------------------------
  6098. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6099. {
  6100. DoTerminalListChanged();
  6101. }
  6102. //---------------------------------------------------------------------------
  6103. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6104. {
  6105. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6106. DebugAssert(TabSheet->ImageIndex == 0);
  6107. TabSheet->Caption =
  6108. WinConfiguration->SelectiveToolbarText ?
  6109. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6110. UnicodeString();
  6111. }
  6112. //---------------------------------------------------------------------------
  6113. TTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6114. {
  6115. return reinterpret_cast<TTerminal *>(TabSheet->Tag);
  6116. }
  6117. //---------------------------------------------------------------------------
  6118. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6119. {
  6120. if (DebugAlwaysTrue(TabSheet != NULL))
  6121. {
  6122. TManagedTerminal * ManagedTerminal =
  6123. dynamic_cast<TManagedTerminal *>(GetSessionTabTerminal(TabSheet));
  6124. if (DebugAlwaysTrue(ManagedTerminal != NULL))
  6125. {
  6126. TColor Color = (ManagedTerminal == FTerminal) ? FSessionColor : ManagedTerminal->StateData->Color;
  6127. TabSheet->ImageIndex = AddSessionColor(Color);
  6128. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ManagedTerminal, true);
  6129. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6130. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6131. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6132. {
  6133. ThemeTabSheet->Shadowed = !ManagedTerminal->Active;
  6134. }
  6135. }
  6136. }
  6137. }
  6138. //---------------------------------------------------------------------------
  6139. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6140. {
  6141. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6142. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6143. bool Result = (Terminal != NULL);
  6144. if (Result)
  6145. {
  6146. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6147. }
  6148. else
  6149. {
  6150. try
  6151. {
  6152. NewSession(false);
  6153. }
  6154. __finally
  6155. {
  6156. DoTerminalListChanged();
  6157. }
  6158. FSessionsPageControlNewSessionTime = Now();
  6159. }
  6160. return Result;
  6161. }
  6162. //---------------------------------------------------------------------------
  6163. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6164. {
  6165. SessionTabSwitched();
  6166. }
  6167. //---------------------------------------------------------------------------
  6168. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6169. {
  6170. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6171. DebugAssert(TransferList != NULL);
  6172. UnicodeString Name;
  6173. if (TransferList->ItemIndex <= 0)
  6174. {
  6175. Name = L"";
  6176. }
  6177. else
  6178. {
  6179. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6180. }
  6181. if (FCopyParamAutoSelected.IsEmpty())
  6182. {
  6183. // if previous preset was not autoselected, make new preset the "default"
  6184. FCopyParamDefault = Name;
  6185. }
  6186. GUIConfiguration->CopyParamCurrent = Name;
  6187. }
  6188. //---------------------------------------------------------------------------
  6189. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6190. {
  6191. // sanity check
  6192. bool ExistingPreset =
  6193. (FTransferListHoverIndex >= 0) &&
  6194. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6195. DebugAssert(ExistingPreset);
  6196. if (ExistingPreset)
  6197. {
  6198. HDC DC = GetDC(0);
  6199. TCanvas * Canvas = new TCanvas();
  6200. try
  6201. {
  6202. Canvas->Handle = DC;
  6203. Canvas->Font = GetToolbarFont(this);
  6204. UnicodeString Name;
  6205. if (FTransferListHoverIndex == 0)
  6206. {
  6207. Name = L"";
  6208. }
  6209. else
  6210. {
  6211. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6212. }
  6213. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6214. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6215. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6216. static_cast<TObject*>(GetComponent(fcTransferList)));
  6217. UnicodeString InfoStr =
  6218. GUIConfiguration->CopyParamPreset[Name].
  6219. GetInfoStr(L"; ",
  6220. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General));
  6221. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6222. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6223. {
  6224. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6225. {
  6226. InfoStr.SetLength(InfoStr.Length() - 1);
  6227. }
  6228. InfoStr += Ellipsis;
  6229. }
  6230. // UpdateCaption does not cause invalidation of whole submenu, while
  6231. // setting Caption property does.
  6232. // also it probably does not resize the label, even if necessary
  6233. // (we do not want that anyway)
  6234. TransferLabel->UpdateCaption(InfoStr);
  6235. }
  6236. __finally
  6237. {
  6238. Canvas->Handle = NULL;
  6239. ReleaseDC(0, DC);
  6240. delete Canvas;
  6241. }
  6242. }
  6243. }
  6244. //---------------------------------------------------------------------------
  6245. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6246. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6247. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6248. {
  6249. if (FTransferListHoverIndex != AHoverIndex)
  6250. {
  6251. FTransferListHoverIndex = AHoverIndex;
  6252. UpdateTransferLabel();
  6253. }
  6254. }
  6255. //---------------------------------------------------------------------------
  6256. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6257. {
  6258. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6259. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6260. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6261. {
  6262. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6263. {
  6264. OpenDirectory(GetSide(osCurrent));
  6265. Message.Result = 1;
  6266. }
  6267. else
  6268. {
  6269. TForm::Dispatch(&Message);
  6270. }
  6271. }
  6272. else
  6273. {
  6274. TForm::Dispatch(&Message);
  6275. }
  6276. }
  6277. //---------------------------------------------------------------------------
  6278. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6279. {
  6280. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6281. if ((FIgnoreNextDialogChar != 0) &&
  6282. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6283. {
  6284. Message.Result = 1;
  6285. }
  6286. else
  6287. {
  6288. TForm::Dispatch(&Message);
  6289. }
  6290. FIgnoreNextDialogChar = 0;
  6291. }
  6292. //---------------------------------------------------------------------------
  6293. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6294. {
  6295. // The four low-order bits are used internally by Windows
  6296. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6297. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6298. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6299. // 61730 - restore thru double click - undocumented
  6300. // 61490 - maximize thru double click - undocumented
  6301. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6302. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6303. {
  6304. SysResizing(Cmd);
  6305. }
  6306. TForm::Dispatch(&Message);
  6307. }
  6308. //---------------------------------------------------------------------------
  6309. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6310. {
  6311. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6312. // Also there no point returning TRUE as we are not able to
  6313. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6314. // Hence the process termination might be safer :)
  6315. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6316. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6317. {
  6318. Message.Result = FALSE;
  6319. }
  6320. else
  6321. {
  6322. Message.Result = TRUE;
  6323. }
  6324. // Do not call default handling as that triggers OnCloseQuery,
  6325. // where our implementation will popup confirmation dialogs, what we do not want,
  6326. // as per Vista guidelines:
  6327. // https://docs.microsoft.com/en-us/windows/desktop/shutdown/shutting-down
  6328. }
  6329. //---------------------------------------------------------------------------
  6330. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6331. {
  6332. if (Message.EndSession && IsApplicationMinimized())
  6333. {
  6334. // WORKAROUND
  6335. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6336. // when it receives WM_ENDSESSION,
  6337. // but that sometimes (particularly when application is minimized) cause crashes.
  6338. // Crash popups message that blocks log off.
  6339. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6340. // so we call ExitProcess() immediately, not even trying to cleanup.
  6341. // It still causes beep, so there's likely some popup, but it does not block
  6342. // log off.
  6343. ExitProcess(0);
  6344. }
  6345. TForm::Dispatch(&Message);
  6346. }
  6347. #ifdef _DEBUG
  6348. //---------------------------------------------------------------------------
  6349. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6350. {
  6351. TForm::Dispatch(&Message);
  6352. }
  6353. #endif
  6354. //---------------------------------------------------------------------------
  6355. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6356. {
  6357. }
  6358. //---------------------------------------------------------------------------
  6359. void __fastcall TCustomScpExplorerForm::DoShow()
  6360. {
  6361. // only now are the controls resized finally, so the size constraints
  6362. // will not conflict with possibly very small window size
  6363. RestoreFormParams();
  6364. FixControlsPlacement();
  6365. if (Position == poDefaultPosOnly)
  6366. {
  6367. CutFormToDesktop(this);
  6368. }
  6369. TForm::DoShow();
  6370. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6371. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6372. if (Terminal == NULL)
  6373. {
  6374. StartingDisconnected();
  6375. }
  6376. UpdatePixelsPerInchMainWindowCounter();
  6377. FShowing = true;
  6378. }
  6379. //---------------------------------------------------------------------------
  6380. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6381. {
  6382. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6383. }
  6384. //---------------------------------------------------------------------------
  6385. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6386. {
  6387. DoTerminalListChanged();
  6388. }
  6389. //---------------------------------------------------------------------------
  6390. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6391. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6392. TNotifyEvent OnBalloonClick, TObject * UserData)
  6393. {
  6394. bool Do;
  6395. UnicodeString Message;
  6396. if (E == NULL)
  6397. {
  6398. Message = Str;
  6399. Do = true;
  6400. }
  6401. else
  6402. {
  6403. Do = ExceptionMessage(E, Message);
  6404. }
  6405. if (Do && WinConfiguration->BalloonNotifications)
  6406. {
  6407. UnicodeString Title;
  6408. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6409. {
  6410. Message = TrimLeft(Message);
  6411. }
  6412. else
  6413. {
  6414. Message = UnformatMessage(Message);
  6415. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6416. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6417. Title = LoadResourceString(Captions[Type]);
  6418. if (Terminal != NULL)
  6419. {
  6420. Title = FORMAT(L"%s - %s",
  6421. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6422. }
  6423. }
  6424. if (Seconds == 0)
  6425. {
  6426. Seconds = WinConfiguration->NotificationsTimeout;
  6427. }
  6428. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6429. }
  6430. }
  6431. //---------------------------------------------------------------------------
  6432. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6433. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6434. UnicodeString HelpKeyword, const TMessageParams * Params,
  6435. TTerminal * Terminal)
  6436. {
  6437. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6438. (FOnFeedSynchronizeError != NULL)) &&
  6439. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6440. {
  6441. if (FOnFeedSynchronizeError != NULL)
  6442. {
  6443. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6444. }
  6445. else
  6446. {
  6447. DebugAssert(FErrorList != NULL);
  6448. TStringList * MoreMessagesCopy = NULL;
  6449. if (MoreMessages)
  6450. {
  6451. MoreMessagesCopy = new TStringList();
  6452. MoreMessagesCopy->Assign(MoreMessages);
  6453. }
  6454. FErrorList->AddObject(Message, MoreMessagesCopy);
  6455. }
  6456. PopupTrayBalloon(Terminal, Message, Type);
  6457. return ContinueAnswer(Answers);
  6458. }
  6459. else
  6460. {
  6461. bool UseBalloon = FTrayIcon->Visible;
  6462. if (UseBalloon)
  6463. {
  6464. PopupTrayBalloon(Terminal, Message, Type);
  6465. }
  6466. unsigned int Result;
  6467. try
  6468. {
  6469. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6470. }
  6471. __finally
  6472. {
  6473. // cancel only balloon we popped up, otherwise we may cancel notification
  6474. // balloon that was there before the message dialog
  6475. // (such as "dd confirmation opt in balloon)
  6476. if (UseBalloon)
  6477. {
  6478. FTrayIcon->CancelBalloon();
  6479. }
  6480. }
  6481. return Result;
  6482. }
  6483. }
  6484. //---------------------------------------------------------------------------
  6485. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6486. TTerminal * Terminal, Exception * E)
  6487. {
  6488. if (FTrayIcon->Visible)
  6489. {
  6490. PopupTrayBalloon(Terminal, L"", qtError, E);
  6491. }
  6492. // particularly prevent opening new session from jump list,
  6493. // while exception is shown
  6494. NonVisualDataModule->StartBusy();
  6495. try
  6496. {
  6497. ShowExtendedExceptionEx(Terminal, E);
  6498. }
  6499. __finally
  6500. {
  6501. NonVisualDataModule->EndBusy();
  6502. FTrayIcon->CancelBalloon();
  6503. }
  6504. }
  6505. //---------------------------------------------------------------------------
  6506. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6507. {
  6508. // cannot rely on active page being page for active terminal,
  6509. // as it can happen that active page is the "new session" page
  6510. // (e.g. when reconnecting active terminal, while login dialog
  6511. // invoked from "new session" page is modal)
  6512. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6513. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6514. }
  6515. //---------------------------------------------------------------------------
  6516. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6517. TTerminal * Terminal, Exception * E)
  6518. {
  6519. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6520. if (!Terminal->Active)
  6521. {
  6522. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6523. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6524. {
  6525. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6526. }
  6527. }
  6528. }
  6529. //---------------------------------------------------------------------------
  6530. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6531. UnicodeString Message, TQueryType Type,
  6532. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6533. {
  6534. if ((E == NULL) ||
  6535. ExceptionMessage(E, Message))
  6536. {
  6537. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6538. if (Important)
  6539. {
  6540. Seconds *= 5;
  6541. }
  6542. UnicodeString NoteMessage(UnformatMessage(Message));
  6543. if (Terminal != NULL)
  6544. {
  6545. NoteMessage = FORMAT(L"%s: %s",
  6546. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6547. }
  6548. if (WinConfiguration->BalloonNotifications)
  6549. {
  6550. AddNote(NoteMessage);
  6551. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6552. }
  6553. else
  6554. {
  6555. FlashOnBackground();
  6556. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6557. }
  6558. }
  6559. }
  6560. //---------------------------------------------------------------------------
  6561. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6562. {
  6563. RestoreApp();
  6564. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6565. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6566. !NonVisualDataModule->Busy)
  6567. {
  6568. TTerminal * Terminal = TerminalNoteData->Terminal;
  6569. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6570. if (!ComponentVisible[fcQueueView])
  6571. {
  6572. ToggleQueueVisibility();
  6573. GoToQueue();
  6574. }
  6575. }
  6576. }
  6577. //---------------------------------------------------------------------------
  6578. void __fastcall TCustomScpExplorerForm::QueueEvent(TTerminal * ATerminal,
  6579. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6580. {
  6581. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  6582. UnicodeString Message;
  6583. TNotifyEvent OnClick = NULL;
  6584. TObject * UserData = NULL;
  6585. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6586. switch (Event)
  6587. {
  6588. case qeEmptyButMonitored:
  6589. if ((ATerminal != Terminal) || QueueInvisible)
  6590. {
  6591. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6592. OnClick = QueueEmptyNoteClicked;
  6593. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6594. TerminalNoteData->Terminal = ATerminal;
  6595. UserData = TerminalNoteData;
  6596. }
  6597. break;
  6598. case qeEmpty:
  6599. OperationComplete(ManagedTerminal->QueueOperationStart);
  6600. break;
  6601. case qePendingUserAction:
  6602. if ((ATerminal != Terminal) ||
  6603. (QueueInvisible && !IsQueueAutoPopup()))
  6604. {
  6605. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6606. }
  6607. break;
  6608. default:
  6609. DebugFail();
  6610. }
  6611. if (!Message.IsEmpty())
  6612. {
  6613. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6614. }
  6615. }
  6616. //---------------------------------------------------------------------------
  6617. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6618. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6619. {
  6620. if (FDDExtMapFile != NULL)
  6621. {
  6622. CloseHandle(FDDExtMapFile);
  6623. FDDExtMapFile = NULL;
  6624. }
  6625. if (WinConfiguration->DDFakeFile)
  6626. {
  6627. DDFakeFileInitDrag(FileList, Created);
  6628. }
  6629. }
  6630. //---------------------------------------------------------------------------
  6631. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6632. {
  6633. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6634. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6635. {
  6636. FFakeFileDropTarget = FileName;
  6637. }
  6638. }
  6639. //---------------------------------------------------------------------------
  6640. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6641. {
  6642. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6643. if (!ForceDirectories(ApiPath(Result)))
  6644. {
  6645. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6646. }
  6647. FileSetAttr(ApiPath(Result), faHidden);
  6648. return Result;
  6649. }
  6650. //---------------------------------------------------------------------------
  6651. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6652. bool & Created)
  6653. {
  6654. FFakeFileDropTarget = UnicodeString();
  6655. FDragFakeDirectory = CreateFakeTransferDirectory();
  6656. FileList->AddItem(NULL, FDragFakeDirectory);
  6657. Created = true;
  6658. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6659. {
  6660. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6661. }
  6662. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6663. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6664. {
  6665. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6666. TDragExtCommStruct* CommStruct;
  6667. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6668. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6669. DebugAssert(CommStruct != NULL);
  6670. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6671. CommStruct->Dragging = true;
  6672. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6673. LENOF(CommStruct->DropDest));
  6674. NULL_TERMINATE(CommStruct->DropDest);
  6675. UnmapViewOfFile(CommStruct);
  6676. }
  6677. }
  6678. //---------------------------------------------------------------------------
  6679. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6680. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6681. {
  6682. bool Result;
  6683. DebugAssert(!IsLocalBrowserMode());
  6684. if (Sender == RemoteDirView)
  6685. {
  6686. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6687. }
  6688. else
  6689. {
  6690. DebugAssert(Sender == RemoteDriveView);
  6691. TStrings * FileList = RemoteDriveView->DragFileList();
  6692. try
  6693. {
  6694. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6695. }
  6696. __finally
  6697. {
  6698. delete FileList;
  6699. }
  6700. }
  6701. if (FDDTargetControl == RemoteDriveView)
  6702. {
  6703. RemoteDriveView->UpdateDropTarget();
  6704. }
  6705. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6706. // or from tree view to dir view.
  6707. // foMove happens when file/dir is dragged from remote tree view outside of
  6708. // application via dragex
  6709. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6710. (DropSourceControl == RemoteDriveView))
  6711. {
  6712. RemoteDriveView->UpdateDropSource();
  6713. }
  6714. return Result;
  6715. }
  6716. //---------------------------------------------------------------------------
  6717. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6718. {
  6719. DebugAssert(!IsLocalBrowserMode());
  6720. // This also handles drops of remote files to queue.
  6721. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6722. SAFE_DESTROY(FDDFileList);
  6723. if (!FFakeFileDropTarget.IsEmpty())
  6724. {
  6725. RemoveDir(ApiPath(FFakeFileDropTarget));
  6726. }
  6727. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6728. {
  6729. try
  6730. {
  6731. TDragResult DDResult = (Sender == RemoteDirView) ?
  6732. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6733. // Focus is moved to the target application,
  6734. // but as we are going to present the UI, we need to steal the focus back.
  6735. // This most likely won't work though (windows does not allow application
  6736. // to steal focus most of the time)
  6737. Application->BringToFront();
  6738. // On older version of Windows we never got drMove here, see also comment below.
  6739. // On Windows 10, we get the "move".
  6740. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6741. {
  6742. UnicodeString TargetDirectory;
  6743. TFileOperation Operation;
  6744. // drInvalid may mean drMove, see comment below
  6745. switch (DDResult)
  6746. {
  6747. case drCopy:
  6748. Operation = foCopy;
  6749. break;
  6750. case drMove:
  6751. Operation = foMove;
  6752. break;
  6753. default:
  6754. DebugFail();
  6755. case drInvalid:
  6756. // prefer "copy" for safety
  6757. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6758. break;
  6759. }
  6760. TTransferOperationParam Param;
  6761. UnicodeString CounterName;
  6762. bool ForceQueue;
  6763. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6764. {
  6765. // we get drInvalid both if d&d was intercepted by ddext,
  6766. // and when users drops on no-drop location.
  6767. // we tell the difference by existence of response from ddext,
  6768. // so we ignore absence of response in this case
  6769. if (DDResult != drInvalid)
  6770. {
  6771. // here we know that the extension is installed,
  6772. // as it is checked as soon as drag&drop starts from
  6773. // RemoteFileControlDDCreateDragFileList
  6774. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6775. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  6776. {
  6777. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  6778. }
  6779. else
  6780. {
  6781. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6782. }
  6783. }
  6784. }
  6785. else
  6786. {
  6787. // download using fake file
  6788. Param.Temp = false;
  6789. Param.DragDrop = true;
  6790. if (ForceQueue)
  6791. {
  6792. Param.Queue = asOn;
  6793. }
  6794. if (Sender == RemoteDirView)
  6795. {
  6796. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6797. }
  6798. if (RemoteFileControlFileOperation(Sender, Operation,
  6799. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6800. {
  6801. Configuration->Usage->Inc(CounterName);
  6802. }
  6803. }
  6804. }
  6805. }
  6806. __finally
  6807. {
  6808. CloseHandle(FDDExtMapFile);
  6809. FDDExtMapFile = NULL;
  6810. delete FDragFakeMonitors;
  6811. FDragFakeMonitors = NULL;
  6812. RemoveDir(ApiPath(FDragFakeDirectory));
  6813. FDragFakeDirectory = L"";
  6814. FFakeFileDropTarget = UnicodeString();
  6815. }
  6816. }
  6817. if (FDragDropSshTerminate.get() != NULL)
  6818. {
  6819. FDragDropSshTerminate->Rethrow();
  6820. }
  6821. }
  6822. //---------------------------------------------------------------------------
  6823. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6824. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  6825. {
  6826. // Remember drop effect so we know (when user drops files), if we copy or move
  6827. FLastDropEffect = dwEffect;
  6828. }
  6829. //---------------------------------------------------------------------------
  6830. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6831. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6832. {
  6833. bool Result;
  6834. if (FDDTargetControl == QueueView3)
  6835. {
  6836. Directory = DefaultDownloadTargetDirectory();
  6837. Result = true;
  6838. CounterName = L"DownloadsDragDropQueue";
  6839. ForceQueue = true;
  6840. }
  6841. else if (!FFakeFileDropTarget.IsEmpty())
  6842. {
  6843. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6844. Result = true;
  6845. ForceQueue = false;
  6846. CounterName = L"DownloadsDragDropFakeFile";
  6847. }
  6848. else
  6849. {
  6850. ForceQueue = false;
  6851. Enabled = false;
  6852. try
  6853. {
  6854. int Timer = 0;
  6855. Result = false;
  6856. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6857. {
  6858. {
  6859. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6860. TDragExtCommStruct* CommStruct;
  6861. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6862. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6863. DebugAssert(CommStruct != NULL);
  6864. Result = !CommStruct->Dragging;
  6865. if (Result)
  6866. {
  6867. Directory = ExtractFilePath(CommStruct->DropDest);
  6868. CounterName = L"DownloadsDragDropExternalExt";
  6869. }
  6870. UnmapViewOfFile(CommStruct);
  6871. }
  6872. if (!Result)
  6873. {
  6874. Sleep(50);
  6875. Timer += 50;
  6876. Application->ProcessMessages();
  6877. }
  6878. }
  6879. }
  6880. __finally
  6881. {
  6882. Enabled = true;
  6883. }
  6884. }
  6885. return Result;
  6886. }
  6887. //---------------------------------------------------------------------------
  6888. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  6889. const UnicodeString TempDir, int SecDelay)
  6890. {
  6891. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  6892. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  6893. if (FDelayedDeletionTimer == NULL)
  6894. {
  6895. DebugAssert(HandleAllocated());
  6896. FDelayedDeletionTimer = new TTimer(this);
  6897. FDelayedDeletionTimer->Interval = 10000;
  6898. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  6899. }
  6900. else
  6901. {
  6902. FDelayedDeletionTimer->Enabled = true;
  6903. }
  6904. }
  6905. //---------------------------------------------------------------------------
  6906. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  6907. {
  6908. DebugAssert(FDelayedDeletionList != NULL);
  6909. TDateTime N = Now();
  6910. TDateTime Alert;
  6911. UnicodeString Directory;
  6912. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  6913. {
  6914. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  6915. if ((N >= Alert) || (Sender == NULL))
  6916. {
  6917. Directory = FDelayedDeletionList->Strings[Index];
  6918. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  6919. {
  6920. FDelayedDeletionList->Delete(Index);
  6921. }
  6922. }
  6923. }
  6924. if (FDelayedDeletionList->Count == 0)
  6925. {
  6926. FDelayedDeletionTimer->Enabled = false;
  6927. }
  6928. }
  6929. //---------------------------------------------------------------------------
  6930. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  6931. {
  6932. DebugAssert(!IsLocalBrowserMode());
  6933. if (IsFileControl(FDDTargetControl, osRemote) ||
  6934. (FDDTargetControl == SessionsPageControl))
  6935. {
  6936. TTerminal * TargetTerminal = NULL;
  6937. TFileOperation Operation = foNone;
  6938. if (FDDTargetControl == SessionsPageControl)
  6939. {
  6940. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  6941. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  6942. // do not allow dropping on the "+" tab
  6943. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  6944. if (TargetTerminal != NULL)
  6945. {
  6946. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  6947. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  6948. {
  6949. Operation = foRemoteMove;
  6950. }
  6951. else
  6952. {
  6953. Operation = foRemoteCopy;
  6954. }
  6955. }
  6956. }
  6957. else
  6958. {
  6959. if (FLastDropEffect == DROPEFFECT_MOVE)
  6960. {
  6961. Operation = foRemoteMove;
  6962. }
  6963. else if (FLastDropEffect == DROPEFFECT_COPY)
  6964. {
  6965. Operation = foRemoteCopy;
  6966. }
  6967. }
  6968. if (Operation != foNone)
  6969. {
  6970. RemoteFileControlFileOperation(DropSourceControl,
  6971. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  6972. }
  6973. // abort drag&drop
  6974. Abort();
  6975. }
  6976. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  6977. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  6978. (FDDTargetControl != QueueView3))
  6979. {
  6980. DebugAssert(!FDragTempDir.IsEmpty());
  6981. TTransferType Type;
  6982. UnicodeString TempDir = FDragTempDir;
  6983. // We clear FDragTempDir before calling
  6984. // just in case it fail (raises exception)
  6985. FDragTempDir = L"";
  6986. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  6987. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  6988. // empty directory parameter means temp directory -> don't display it!
  6989. UnicodeString TargetDir = L"";
  6990. int Options = 0;
  6991. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  6992. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  6993. {
  6994. Abort();
  6995. }
  6996. // TargetDir is set when dropped on local file control
  6997. // (this was workaround for legacy dirview event handling, now it should be
  6998. // made prettier)
  6999. if (TargetDir.IsEmpty())
  7000. {
  7001. TargetDir = TempDir;
  7002. if (ForceDirectories(ApiPath(TargetDir)))
  7003. {
  7004. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7005. FPendingTempSpaceWarn = true;
  7006. try
  7007. {
  7008. FDragDropOperation = true;
  7009. int Params = cpTemporary |
  7010. (Type == ttMove ? cpDelete : 0);
  7011. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7012. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7013. }
  7014. __finally
  7015. {
  7016. FDragDropOperation = false;
  7017. FPendingTempSpaceWarn = false;
  7018. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7019. }
  7020. }
  7021. else
  7022. {
  7023. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7024. }
  7025. }
  7026. }
  7027. }
  7028. //---------------------------------------------------------------------------
  7029. void __fastcall TCustomScpExplorerForm::DDDownload(
  7030. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7031. {
  7032. DebugAssert(!IsLocalBrowserMode());
  7033. TAutoBatch AutoBatch(this);
  7034. UpdateCopyParamCounters(*CopyParam);
  7035. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7036. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7037. {
  7038. RemoteDriveView->UpdateDropSource();
  7039. }
  7040. }
  7041. //---------------------------------------------------------------------------
  7042. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7043. {
  7044. public:
  7045. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7046. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7047. {
  7048. }
  7049. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7050. {
  7051. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7052. TDataObjectFilesEx::AllowData(FormatEtc);
  7053. }
  7054. };
  7055. //---------------------------------------------------------------------------
  7056. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7057. TObject * Sender, TDataObject *& DataObject)
  7058. {
  7059. if (FDDExtMapFile != NULL)
  7060. {
  7061. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7062. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7063. {
  7064. Abort();
  7065. }
  7066. if (FileList->Count > 0)
  7067. {
  7068. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7069. if (!FilesObject->IsValid(true, true))
  7070. {
  7071. FilesObject->_Release();
  7072. }
  7073. else
  7074. {
  7075. DataObject = FilesObject;
  7076. }
  7077. }
  7078. }
  7079. }
  7080. //---------------------------------------------------------------------------
  7081. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7082. {
  7083. DebugFail();
  7084. }
  7085. //---------------------------------------------------------------------------
  7086. void __fastcall TCustomScpExplorerForm::GoToTree()
  7087. {
  7088. ComponentVisible[fcRemoteTree] = true;
  7089. DriveView(osRemote)->SetFocus();
  7090. }
  7091. //---------------------------------------------------------------------------
  7092. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7093. TPanelExport Export)
  7094. {
  7095. TCustomDirView * DirView = this->DirView(Side);
  7096. std::unique_ptr<TStrings> ExportData(new TStringList());
  7097. switch (Export)
  7098. {
  7099. case pePath:
  7100. ExportData->Add(DirView->PathName);
  7101. break;
  7102. case peFileList:
  7103. case peFullFileList:
  7104. {
  7105. bool FullPath = (Export == peFullFileList);
  7106. DirView->CreateFileList(false, FullPath, ExportData.get());
  7107. for (int Index = 0; Index < ExportData->Count; Index++)
  7108. {
  7109. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7110. {
  7111. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7112. }
  7113. }
  7114. }
  7115. break;
  7116. default:
  7117. DebugFail();
  7118. }
  7119. return ExportData.release();
  7120. }
  7121. //---------------------------------------------------------------------------
  7122. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7123. TPanelExport Export, TPanelExportDestination Destination)
  7124. {
  7125. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7126. PanelExportStore(Side, Export, Destination, ExportData.get());
  7127. }
  7128. //---------------------------------------------------------------------------
  7129. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7130. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7131. TStrings * ExportData)
  7132. {
  7133. if (Destination == pedClipboard)
  7134. {
  7135. TInstantOperationVisualizer Visualizer;
  7136. CopyToClipboard(ExportData);
  7137. }
  7138. else
  7139. {
  7140. DebugFail();
  7141. }
  7142. }
  7143. //---------------------------------------------------------------------------
  7144. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7145. {
  7146. TCustomDirView * DirView = this->DirView(Side);
  7147. UnicodeString Mask = DirView->Mask;
  7148. if (DoFilterMaskDialog(DirView, Mask))
  7149. {
  7150. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7151. Configuration->Usage->Inc(L"Filters");
  7152. }
  7153. }
  7154. //---------------------------------------------------------------------------
  7155. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7156. {
  7157. return FQueueController->DefaultOperation();
  7158. }
  7159. //---------------------------------------------------------------------------
  7160. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7161. TQueueOperation Operation, void ** Param)
  7162. {
  7163. switch (Operation)
  7164. {
  7165. case qoPreferences:
  7166. return true;
  7167. case qoGoTo:
  7168. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7169. case qoOnceEmpty:
  7170. return !FQueueController->Empty;
  7171. default:
  7172. return FQueueController->AllowOperation(Operation, Param);
  7173. }
  7174. }
  7175. //---------------------------------------------------------------------------
  7176. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7177. {
  7178. if (DebugAlwaysTrue(QueueView3->Visible))
  7179. {
  7180. QueueView3->SetFocus();
  7181. }
  7182. }
  7183. //---------------------------------------------------------------------------
  7184. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7185. TQueueOperation Operation, void * Param)
  7186. {
  7187. if (Operation == qoGoTo)
  7188. {
  7189. GoToQueue();
  7190. }
  7191. else if (Operation == qoPreferences)
  7192. {
  7193. PreferencesDialog(pmQueue);
  7194. }
  7195. else
  7196. {
  7197. FQueueController->ExecuteOperation(Operation, Param);
  7198. }
  7199. }
  7200. //---------------------------------------------------------------------------
  7201. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7202. {
  7203. return (Queue != NULL) && Queue->Enabled;
  7204. }
  7205. //---------------------------------------------------------------------------
  7206. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7207. {
  7208. DebugAssert(Queue != NULL);
  7209. if (Queue != NULL)
  7210. {
  7211. Queue->Enabled = !Queue->Enabled;
  7212. }
  7213. }
  7214. //---------------------------------------------------------------------------
  7215. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7216. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7217. {
  7218. FQueueActedItem = QueueView3->ItemFocused;
  7219. }
  7220. //---------------------------------------------------------------------------
  7221. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7222. {
  7223. return TopDock->Height + QueueSplitter->Height;
  7224. }
  7225. //---------------------------------------------------------------------------
  7226. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7227. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7228. {
  7229. // when queue is hidden by double-clicking splitter, stray attempt to
  7230. // resize the panel with strange value arrives, make sure it is ignored
  7231. if (ComponentVisible[fcQueueView])
  7232. {
  7233. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7234. RemotePanel->Constraints->MinHeight;
  7235. if (NewSize > HeightLimit)
  7236. {
  7237. NewSize = HeightLimit;
  7238. }
  7239. }
  7240. else
  7241. {
  7242. Accept = false;
  7243. }
  7244. }
  7245. //---------------------------------------------------------------------------
  7246. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7247. TDragObject *& /*DragObject*/)
  7248. {
  7249. FQueueActedItem = QueueView3->ItemFocused;
  7250. }
  7251. //---------------------------------------------------------------------------
  7252. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7253. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7254. {
  7255. Accept = true;
  7256. if (Source == QueueView3)
  7257. {
  7258. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7259. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7260. if (Accept)
  7261. {
  7262. TQueueItemProxy * QueueItem;
  7263. TQueueItemProxy * DestQueueItem;
  7264. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7265. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7266. Accept = (QueueItem != DestQueueItem) &&
  7267. (QueueItem->Status == TQueueItem::qsPending) &&
  7268. (DestQueueItem->Status == TQueueItem::qsPending);
  7269. }
  7270. }
  7271. }
  7272. //---------------------------------------------------------------------------
  7273. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7274. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7275. {
  7276. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7277. {
  7278. TQueueItemProxy * QueueItem;
  7279. TQueueItemProxy * DestQueueItem;
  7280. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7281. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7282. QueueItem->Move(DestQueueItem);
  7283. }
  7284. }
  7285. //---------------------------------------------------------------------------
  7286. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7287. {
  7288. if ((QueueView3->ItemFocused == NULL) &&
  7289. (QueueView3->Items->Count > 0))
  7290. {
  7291. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7292. }
  7293. QueueLabelUpdateStatus();
  7294. }
  7295. //---------------------------------------------------------------------------
  7296. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7297. {
  7298. QueueLabelUpdateStatus();
  7299. }
  7300. //---------------------------------------------------------------------------
  7301. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7302. {
  7303. QueueLabel->UpdateStatus();
  7304. }
  7305. //---------------------------------------------------------------------------
  7306. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7307. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7308. {
  7309. if (Selected)
  7310. {
  7311. NonVisualDataModule->UpdateNonVisibleActions();
  7312. }
  7313. }
  7314. //---------------------------------------------------------------------------
  7315. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7316. {
  7317. TDragDropFilesEx * Result = NULL;
  7318. if (Sender == SessionsPageControl)
  7319. {
  7320. Result = FSessionsDragDropFilesEx;
  7321. }
  7322. else if (Sender == QueueView3)
  7323. {
  7324. Result = FQueueDragDropFilesEx;
  7325. }
  7326. else
  7327. {
  7328. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7329. if (DirView != NULL)
  7330. {
  7331. Result = DirView->DragDropFilesEx;
  7332. }
  7333. else
  7334. {
  7335. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7336. if (DriveView != NULL)
  7337. {
  7338. Result = DriveView->DragDropFilesEx;
  7339. }
  7340. }
  7341. }
  7342. DebugAssert(Result != NULL);
  7343. return Result;
  7344. }
  7345. //---------------------------------------------------------------------------
  7346. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7347. {
  7348. return (DView->SelCount == DView->FilesCount);
  7349. }
  7350. //---------------------------------------------------------------------------
  7351. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7352. {
  7353. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7354. }
  7355. //---------------------------------------------------------------------------
  7356. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7357. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7358. {
  7359. DebugAssert(!IsLocalBrowserMode());
  7360. TFileOperation Operation;
  7361. switch (Effect)
  7362. {
  7363. case DROPEFFECT_MOVE:
  7364. Operation = foMove;
  7365. break;
  7366. case DROPEFFECT_COPY:
  7367. // occures on WinXP (reported by user)
  7368. default:
  7369. Operation = foCopy;
  7370. break;
  7371. };
  7372. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7373. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7374. if (DragDropFilesEx->FileList->Count > 0)
  7375. {
  7376. TStrings * FileList = new TStringList();
  7377. try
  7378. {
  7379. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7380. {
  7381. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7382. }
  7383. FDragDropOperation = true;
  7384. TTransferOperationParam Param;
  7385. Param.TargetDirectory = TargetPath;
  7386. // upload, no temp dirs
  7387. Param.Temp = false;
  7388. Param.DragDrop = DragDrop;
  7389. Param.Options =
  7390. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7391. if (ForceQueue)
  7392. {
  7393. Param.Queue = asOn;
  7394. }
  7395. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7396. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7397. {
  7398. if (IsFileControl(DropSourceControl, osLocal))
  7399. {
  7400. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7401. }
  7402. else
  7403. {
  7404. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7405. }
  7406. }
  7407. }
  7408. __finally
  7409. {
  7410. FDragDropOperation = false;
  7411. delete FileList;
  7412. }
  7413. }
  7414. }
  7415. //---------------------------------------------------------------------------
  7416. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7417. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7418. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7419. {
  7420. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7421. }
  7422. //---------------------------------------------------------------------------
  7423. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7424. TObject * Sender, int grfKeyState, int & dwEffect)
  7425. {
  7426. DebugAssert(!IsLocalBrowserMode());
  7427. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7428. if ((dwEffect != DROPEFFECT_NONE) &&
  7429. IsFileControl(DropSourceControl, osRemote))
  7430. {
  7431. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7432. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7433. (RemoteDirView->DropTarget == NULL))
  7434. {
  7435. dwEffect = DROPEFFECT_NONE;
  7436. }
  7437. else
  7438. {
  7439. if (dwEffect == DROPEFFECT_COPY)
  7440. {
  7441. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7442. // currently we support copying always (at least via temporary directory);
  7443. // remove associated checks once this all proves stable and working
  7444. bool CopyCapable = true;
  7445. // if we do not support neither of operations, there's no discussion
  7446. if (!MoveCapable && !CopyCapable)
  7447. {
  7448. dwEffect = DROPEFFECT_NONE;
  7449. }
  7450. else
  7451. {
  7452. // The default effect inside remote panel is move,
  7453. // unless we do not support it, but support copy
  7454. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7455. {
  7456. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7457. }
  7458. else
  7459. {
  7460. // with ctrl-down, we want copy unless it is not supported
  7461. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7462. }
  7463. }
  7464. }
  7465. }
  7466. }
  7467. }
  7468. //---------------------------------------------------------------------------
  7469. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7470. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7471. {
  7472. DebugAssert(!IsLocalBrowserMode());
  7473. if (FDDTotalSize >= 0)
  7474. {
  7475. if (File->IsDirectory)
  7476. {
  7477. FDDTotalSize = -1;
  7478. }
  7479. else
  7480. {
  7481. FDDTotalSize += File->Size;
  7482. }
  7483. }
  7484. DebugAssert(!FDragTempDir.IsEmpty());
  7485. // TODO: this is quite ineffective
  7486. // TODO: what if invalid character replacement is disabled?
  7487. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7488. UnicodeString TransferFileName;
  7489. if (Sender == RemoteDriveView)
  7490. {
  7491. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7492. }
  7493. else
  7494. {
  7495. TransferFileName = File->FileName;
  7496. }
  7497. FDDFileList->AddObject(TransferFileName, File);
  7498. }
  7499. //---------------------------------------------------------------------------
  7500. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7501. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7502. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7503. {
  7504. // sometimes we do not get DDEnd so the list is not released
  7505. SAFE_DESTROY(FDDFileList);
  7506. FDDFileList = new TStringList();
  7507. FDragTempDir = WinConfiguration->TemporaryDir();
  7508. FDDTotalSize = 0;
  7509. FDragDropSshTerminate.reset(NULL);
  7510. }
  7511. //---------------------------------------------------------------------------
  7512. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7513. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7514. HRESULT & Result)
  7515. {
  7516. if (Result == DRAGDROP_S_DROP)
  7517. {
  7518. try
  7519. {
  7520. GlobalDragImageList->HideDragImage();
  7521. try
  7522. {
  7523. RemoteFileControlDDTargetDrop();
  7524. }
  7525. catch(ESshTerminate & E)
  7526. {
  7527. FDragDropSshTerminate.reset(E.Clone());
  7528. }
  7529. }
  7530. catch (Exception &E)
  7531. {
  7532. // If downloading fails we need to cancel drag&drop, otherwise
  7533. // Explorer shows error
  7534. // But by the way exception probably never reach this point as
  7535. // it's catched on way.
  7536. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7537. Result = DRAGDROP_S_CANCEL;
  7538. ShowExtendedException(Terminal, &E);
  7539. }
  7540. }
  7541. }
  7542. //---------------------------------------------------------------------------
  7543. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7544. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7545. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7546. {
  7547. TFileMasks::TParams MaskParams;
  7548. MaskParams.Size = Size;
  7549. MaskParams.Modification = Modification;
  7550. // this does not re-parse the mask if it is the same as the last time
  7551. FDirViewMatchMask = Masks;
  7552. bool ImplicitMatch;
  7553. Matches =
  7554. // RecurseInclude parameter has no effect as the Path is empty
  7555. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7556. (AllowImplicitMatches || !ImplicitMatch);
  7557. }
  7558. //---------------------------------------------------------------------------
  7559. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7560. TObject * Sender, TListItem * Item, WORD & Indexes)
  7561. {
  7562. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7563. UnicodeString Ext;
  7564. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7565. {
  7566. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7567. }
  7568. else
  7569. {
  7570. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7571. }
  7572. if (SameText(Ext, Configuration->PartialExt))
  7573. {
  7574. Indexes |= oiPartial;
  7575. }
  7576. }
  7577. //---------------------------------------------------------------------------
  7578. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7579. {
  7580. return
  7581. DirViewEnabled(osCurrent) &&
  7582. DirView(osCurrent)->CanPasteFromClipBoard();
  7583. }
  7584. //---------------------------------------------------------------------------
  7585. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7586. {
  7587. bool Result = false;
  7588. if (CanPasteToDirViewFromClipBoard())
  7589. {
  7590. Result = true;
  7591. }
  7592. else
  7593. {
  7594. UnicodeString ClipboardText;
  7595. if (NonEmptyTextFromClipboard(ClipboardText))
  7596. {
  7597. if (StoredSessions->IsUrl(ClipboardText))
  7598. {
  7599. Result = true;
  7600. }
  7601. else
  7602. {
  7603. Result = DirViewEnabled(osCurrent);
  7604. }
  7605. }
  7606. }
  7607. return Result;
  7608. }
  7609. //---------------------------------------------------------------------------
  7610. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7611. {
  7612. if (DoesClipboardContainOurFiles())
  7613. {
  7614. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7615. {
  7616. DebugAssert(!IsLocalBrowserMode());
  7617. TTerminalManager * Manager = TTerminalManager::Instance();
  7618. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7619. Manager->ActiveTerminal = FClipboardTerminal;
  7620. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7621. }
  7622. }
  7623. else if (CanPasteToDirViewFromClipBoard())
  7624. {
  7625. DirView(osCurrent)->PasteFromClipBoard();
  7626. }
  7627. else
  7628. {
  7629. UnicodeString ClipboardText;
  7630. if (NonEmptyTextFromClipboard(ClipboardText))
  7631. {
  7632. if (StoredSessions->IsUrl(ClipboardText))
  7633. {
  7634. NewSession(false, ClipboardText);
  7635. }
  7636. else
  7637. {
  7638. DirView(osCurrent)->Path = ClipboardText;
  7639. }
  7640. }
  7641. }
  7642. }
  7643. //---------------------------------------------------------------------------
  7644. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7645. {
  7646. // TBD
  7647. }
  7648. //---------------------------------------------------------------------------
  7649. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7650. {
  7651. TCustomDirView * ADirView = DirView(Side);
  7652. ADirView->SelectAll(Mode);
  7653. ADirView->SetFocus();
  7654. }
  7655. //---------------------------------------------------------------------------
  7656. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7657. {
  7658. TCustomDirView * ADirView = DirView(Side);
  7659. TFileFilter Filter;
  7660. DefaultFileFilter(Filter);
  7661. if (DoSelectMaskDialog(ADirView, Select, Filter))
  7662. {
  7663. Configuration->Usage->Inc(L"MaskSelections");
  7664. ADirView->SelectFiles(Filter, Select);
  7665. ADirView->SetFocus();
  7666. }
  7667. }
  7668. //---------------------------------------------------------------------------
  7669. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7670. {
  7671. TCustomDirView * ADirView = DirView(Side);
  7672. ADirView->RestoreSelectedNames();
  7673. ADirView->SetFocus();
  7674. }
  7675. //---------------------------------------------------------------------------
  7676. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7677. {
  7678. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7679. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7680. {
  7681. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7682. UnicodeString Ext;
  7683. if (!IsSideLocalBrowser(osCurrent))
  7684. {
  7685. Ext = UnixExtractFileExt(FileName);
  7686. }
  7687. else
  7688. {
  7689. Ext = ExtractFileExt(FileName);
  7690. }
  7691. if (Ext.IsEmpty())
  7692. {
  7693. Ext = L".";
  7694. }
  7695. TFileFilter Filter;
  7696. Filter.Masks = FORMAT(L"*%s", (Ext));
  7697. Filter.Directories = false;
  7698. CurrentDirView->SelectFiles(Filter, Select);
  7699. }
  7700. }
  7701. //---------------------------------------------------------------------------
  7702. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7703. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7704. {
  7705. UnicodeString Result;
  7706. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  7707. {
  7708. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  7709. }
  7710. else if (!IsSideLocalBrowser(Side) && (Terminal == NULL))
  7711. {
  7712. // noop
  7713. }
  7714. else
  7715. {
  7716. Result =
  7717. FMTLOAD(FILE_INFO_FORMAT,
  7718. (FormatBytes(FileInfo.SelectedSize),
  7719. FormatBytes(FileInfo.FilesSize),
  7720. FormatNumber(FileInfo.SelectedCount),
  7721. FormatNumber(FileInfo.FilesCount)));
  7722. }
  7723. return Result;
  7724. }
  7725. //---------------------------------------------------------------------------
  7726. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7727. TTBXStatusPanel * Panel, TOperationSide Side)
  7728. {
  7729. if (Panel->Index == 1)
  7730. {
  7731. ToggleShowHiddenFiles();
  7732. }
  7733. else if (Panel->Index == 2)
  7734. {
  7735. Filter(Side);
  7736. }
  7737. }
  7738. //---------------------------------------------------------------------------
  7739. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7740. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7741. {
  7742. DebugAssert(!StatusBar->SimplePanel);
  7743. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7744. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7745. }
  7746. //---------------------------------------------------------------------------
  7747. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7748. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7749. {
  7750. DebugAssert(StatusBar->Panels->Count >= 3);
  7751. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7752. if (FileInfo.HiddenCount > 0)
  7753. {
  7754. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7755. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7756. }
  7757. else
  7758. {
  7759. HiddenFilesPanel->ViewPriority = 0;
  7760. // not really necessary, just to cleanup no-longer-valid data
  7761. HiddenFilesPanel->Caption = L"";
  7762. }
  7763. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7764. if (FileInfo.FilteredCount > 0)
  7765. {
  7766. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7767. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7768. }
  7769. else
  7770. {
  7771. FilteredFilesPanel->ViewPriority = 0;
  7772. // not really necessary, just to cleanup no-longer-valid data
  7773. FilteredFilesPanel->Caption = L"";
  7774. }
  7775. }
  7776. //---------------------------------------------------------------------------
  7777. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7778. TObject * /*Sender*/)
  7779. {
  7780. if (DirView(osRemote)->Enabled)
  7781. {
  7782. DirView(osRemote)->SetFocus();
  7783. }
  7784. }
  7785. //---------------------------------------------------------------------------
  7786. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7787. {
  7788. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7789. switch (Config.Show)
  7790. {
  7791. case qvShow:
  7792. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7793. {
  7794. Config.Show = qvHide;
  7795. }
  7796. else
  7797. {
  7798. Config.Show = Config.LastHideShow;
  7799. }
  7800. break;
  7801. case qvHideWhenEmpty:
  7802. if (ComponentVisible[fcQueueView])
  7803. {
  7804. Config.Show = qvHide;
  7805. }
  7806. else
  7807. {
  7808. Config.LastHideShow = Config.Show;
  7809. Config.Show = qvShow;
  7810. }
  7811. break;
  7812. case qvHide:
  7813. Config.LastHideShow = Config.Show;
  7814. Config.Show = qvShow;
  7815. break;
  7816. }
  7817. WinConfiguration->QueueView = Config;
  7818. }
  7819. //---------------------------------------------------------------------------
  7820. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7821. {
  7822. UnicodeString Result;
  7823. if (FTerminal != NULL)
  7824. {
  7825. switch (WinConfiguration->PathInCaption)
  7826. {
  7827. case picShort:
  7828. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7829. break;
  7830. case picFull:
  7831. Result = FTerminal->CurrentDirectory;
  7832. break;
  7833. }
  7834. }
  7835. return Result;
  7836. }
  7837. //---------------------------------------------------------------------------
  7838. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  7839. {
  7840. TColor Result = GetWindowColor(FSessionColor);
  7841. return Result;
  7842. }
  7843. //---------------------------------------------------------------------------
  7844. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  7845. {
  7846. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  7847. return Result;
  7848. }
  7849. //---------------------------------------------------------------------------
  7850. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7851. {
  7852. TTerminalManager::Instance()->UpdateAppTitle();
  7853. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7854. // background to gray on Windows 7 (works on Windows 10).
  7855. // See also EnableControl
  7856. if (Showing)
  7857. {
  7858. bool HasTerminal = (Terminal != NULL) && Terminal->Active;
  7859. if (HasTerminal)
  7860. {
  7861. // Update path when it changes
  7862. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7863. {
  7864. UpdateSessionTab(SessionsPageControl->ActivePage);
  7865. }
  7866. // TODO needed yet with second local browser?
  7867. if (!RemoteDirView->Enabled)
  7868. {
  7869. RemoteDirView->Enabled = true;
  7870. if (FRemoteDirViewWasFocused)
  7871. {
  7872. ActiveControl = RemoteDirView;
  7873. }
  7874. }
  7875. RemoteDirView->Color = PanelColor();
  7876. }
  7877. else
  7878. {
  7879. if (RemoteDirView->Enabled)
  7880. {
  7881. // This is first called when the form is being constructed
  7882. // (not anymore due to Showing test above)
  7883. // but the false is overriden in the constructor later.
  7884. // An even later in TScpCommanderForm::DoShow()
  7885. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  7886. RemoteDirView->Enabled = false;
  7887. RemoteDirView->Color = DisabledPanelColor();
  7888. }
  7889. }
  7890. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  7891. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  7892. RemoteDriveView->Color = RemoteDirView->Color;
  7893. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  7894. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fsBackgroundTransfers];
  7895. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  7896. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  7897. QueueLabelUpdateStatus();
  7898. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  7899. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  7900. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  7901. }
  7902. }
  7903. //---------------------------------------------------------------------------
  7904. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  7905. {
  7906. UpdateControls();
  7907. }
  7908. //---------------------------------------------------------------------------
  7909. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  7910. TObject * Sender)
  7911. {
  7912. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7913. DebugAssert(DirView != NULL);
  7914. DoDirViewLoaded(DirView);
  7915. TransferPresetAutoSelect();
  7916. }
  7917. //---------------------------------------------------------------------------
  7918. void __fastcall TCustomScpExplorerForm::StartUpdates()
  7919. {
  7920. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  7921. // first run after installation
  7922. if (double(Updates.LastCheck) == 0)
  7923. {
  7924. // make sure next time there will be an update (if enabled)
  7925. Updates.LastCheck = TDateTime(1);
  7926. WinConfiguration->Updates = Updates;
  7927. }
  7928. else if ((double(Updates.Period) > 0) &&
  7929. (Now() - Updates.LastCheck >= Updates.Period))
  7930. {
  7931. TThreadMethod OnUpdatesChecked = NULL;
  7932. if (!IsUWP())
  7933. {
  7934. OnUpdatesChecked = UpdatesChecked;
  7935. }
  7936. StartUpdateThread(OnUpdatesChecked);
  7937. }
  7938. }
  7939. //---------------------------------------------------------------------------
  7940. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  7941. {
  7942. UnicodeString Message;
  7943. bool New;
  7944. TQueryType Type;
  7945. GetUpdatesMessage(Message, New, Type, false);
  7946. if (!Message.IsEmpty())
  7947. {
  7948. if (New)
  7949. {
  7950. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  7951. }
  7952. if (!New && (Type != qtWarning))
  7953. {
  7954. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  7955. }
  7956. else
  7957. {
  7958. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  7959. }
  7960. if (New)
  7961. {
  7962. Configuration->Usage->Inc(L"UpdateNotifications");
  7963. }
  7964. }
  7965. }
  7966. //---------------------------------------------------------------------------
  7967. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  7968. {
  7969. RestoreApp();
  7970. bool CanDisplay = !NonVisualDataModule->Busy;
  7971. if (!CanDisplay && (Screen->ActiveForm != NULL))
  7972. {
  7973. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  7974. }
  7975. if (CanDisplay)
  7976. {
  7977. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  7978. CheckForUpdates(true);
  7979. }
  7980. else
  7981. {
  7982. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  7983. }
  7984. }
  7985. //---------------------------------------------------------------------------
  7986. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  7987. TCopyParamRuleData & Data)
  7988. {
  7989. DebugAssert(Terminal != NULL);
  7990. Data.HostName = Terminal->SessionData->HostNameExpanded;
  7991. Data.UserName = Terminal->SessionData->UserNameExpanded;
  7992. Data.RemoteDirectory = RemoteDirView->PathName;
  7993. }
  7994. //---------------------------------------------------------------------------
  7995. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  7996. {
  7997. // Terminal can be null when we are changing local directory implicitly,
  7998. // because it has been deleted, while no session is connected
  7999. // (Login dialog is open)
  8000. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8001. {
  8002. DebugAssert(!IsLocalBrowserMode());
  8003. TCopyParamRuleData Data;
  8004. GetTransferPresetAutoSelectData(Data);
  8005. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8006. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8007. if (CopyParamIndex < 0)
  8008. {
  8009. // there is no preset that matches autoselection
  8010. // set preset that we consider "default"
  8011. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8012. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8013. }
  8014. else
  8015. {
  8016. // there is preset matching autoselection
  8017. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8018. if (CopyParamName == FCopyParamAutoSelected)
  8019. {
  8020. // autoselected the same preset as the last time
  8021. // make no change (i.e. preserve custom user preset, if any)
  8022. }
  8023. else
  8024. {
  8025. // autoselected the different preset then the last time (or there
  8026. // was default preset set)
  8027. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8028. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8029. }
  8030. }
  8031. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8032. {
  8033. if (CopyParamIndex >= 0)
  8034. {
  8035. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8036. }
  8037. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8038. try
  8039. {
  8040. int Fmt =
  8041. (CopyParamIndex < 0) ?
  8042. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8043. COPY_PARAM_AUTOSELECTED;
  8044. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8045. Data->Message = MainInstructions(Message);
  8046. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8047. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8048. CopyParamAttrs);
  8049. if (CopyParamIndex >= 0)
  8050. {
  8051. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8052. Info = FORMAT(L"%s\n \n%s", (Info,
  8053. FMTLOAD(COPY_PARAM_RULE,
  8054. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8055. }
  8056. Data->Message += L"\n\n" + Info;
  8057. if (WinConfiguration->CopyParamAutoSelectNotice)
  8058. {
  8059. TransferPresetNoteMessage(Data, true);
  8060. }
  8061. else
  8062. {
  8063. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8064. Data = NULL; // ownership passed
  8065. }
  8066. }
  8067. __finally
  8068. {
  8069. delete Data;
  8070. }
  8071. }
  8072. }
  8073. }
  8074. //---------------------------------------------------------------------------
  8075. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8076. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8077. {
  8078. DebugAssert(NoteData != NULL);
  8079. DebugAssert(!IsLocalBrowserMode());
  8080. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8081. TQueryButtonAlias Aliases[1];
  8082. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8083. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8084. Params.Aliases = Aliases;
  8085. Params.AliasesCount = LENOF(Aliases);
  8086. unsigned int Result =
  8087. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8088. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8089. switch (Result)
  8090. {
  8091. case qaNeverAskAgain:
  8092. DebugAssert(AllowNeverAskAgain);
  8093. WinConfiguration->CopyParamAutoSelectNotice = false;
  8094. break;
  8095. case qaIgnore:
  8096. PreferencesDialog(pmPresets);
  8097. break;
  8098. }
  8099. }
  8100. //---------------------------------------------------------------------------
  8101. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8102. {
  8103. // as of now useless, as this is used for notes only, never for balloons, ...
  8104. RestoreApp();
  8105. // .. and we should never be busy here
  8106. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8107. {
  8108. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8109. }
  8110. }
  8111. //---------------------------------------------------------------------------
  8112. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8113. TPreferencesMode APreferencesMode)
  8114. {
  8115. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8116. TCopyParamRuleData Data;
  8117. if (Terminal != NULL)
  8118. {
  8119. PreferencesData.reset(new TPreferencesDialogData());
  8120. GetTransferPresetAutoSelectData(Data);
  8121. PreferencesData->CopyParamRuleData = &Data;
  8122. }
  8123. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8124. }
  8125. //---------------------------------------------------------------------------
  8126. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8127. const TCustomCommandType & Command)
  8128. {
  8129. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8130. {
  8131. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  8132. }
  8133. }
  8134. //---------------------------------------------------------------------------
  8135. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8136. {
  8137. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8138. TCustomCommandType Command;
  8139. // make sure we use local custom command when remote are not supported
  8140. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8141. {
  8142. Command = FLastCustomCommand;
  8143. }
  8144. else
  8145. {
  8146. Command.Params = Command.Params | ccLocal;
  8147. }
  8148. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8149. FEditingFocusedAdHocCommand = OnFocused;
  8150. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8151. int Options =
  8152. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8153. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8154. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8155. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8156. {
  8157. FLastCustomCommand = Command;
  8158. UpdateCustomCommandsToolbar();
  8159. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8160. }
  8161. }
  8162. //---------------------------------------------------------------------------
  8163. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8164. {
  8165. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8166. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8167. DebugAssert(State > 0);
  8168. if (State <= 0)
  8169. {
  8170. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8171. }
  8172. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8173. }
  8174. //---------------------------------------------------------------------------
  8175. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8176. TCustomCommandType & Command, int & State)
  8177. {
  8178. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8179. if (Result)
  8180. {
  8181. Command = FLastCustomCommand;
  8182. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8183. }
  8184. return Result;
  8185. }
  8186. //---------------------------------------------------------------------------
  8187. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8188. {
  8189. ResetIncrementalSearch();
  8190. }
  8191. //---------------------------------------------------------------------------
  8192. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8193. {
  8194. DebugAssert(ComponentVisible[Component]);
  8195. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8196. }
  8197. //---------------------------------------------------------------------------
  8198. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8199. {
  8200. // when queue panel is resized here directly, the status bar is stretched
  8201. // over whole space the panel occupied
  8202. PostComponentHide(fcQueueView);
  8203. }
  8204. //---------------------------------------------------------------------------
  8205. void __fastcall TCustomScpExplorerForm::WMWinIniChange(TMessage & Message)
  8206. {
  8207. WinConfiguration->ResetSysDarkTheme();
  8208. ConfigurationChanged();
  8209. ConfigureInterface();
  8210. TForm::Dispatch(&Message);
  8211. }
  8212. //---------------------------------------------------------------------------
  8213. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8214. {
  8215. TMessage * M = static_cast<TMessage*>(Message);
  8216. switch (M->Msg)
  8217. {
  8218. case CM_DIALOGCHAR:
  8219. CMDialogChar(*M);
  8220. break;
  8221. case WM_APPCOMMAND:
  8222. WMAppCommand(*M);
  8223. break;
  8224. case WM_SYSCOMMAND:
  8225. WMSysCommand(*M);
  8226. break;
  8227. case WM_QUERYENDSESSION:
  8228. WMQueryEndSession(*M);
  8229. break;
  8230. case WM_ENDSESSION:
  8231. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8232. break;
  8233. #ifdef _DEBUG
  8234. case WM_WINDOWPOSCHANGED:
  8235. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8236. break;
  8237. case CM_CUSTOMSTYLECHANGED:
  8238. TForm::Dispatch(Message);
  8239. break;
  8240. #endif
  8241. case WM_COMPONENT_HIDE:
  8242. {
  8243. Byte Component = static_cast<Byte>(M->WParam);
  8244. // sanity check
  8245. if (ComponentVisible[Component])
  8246. {
  8247. // special treatment
  8248. if (Component == fcQueueView)
  8249. {
  8250. ToggleQueueVisibility();
  8251. DebugAssert(!ComponentVisible[fcQueueView]);
  8252. }
  8253. else
  8254. {
  8255. ComponentVisible[Component] = false;
  8256. }
  8257. }
  8258. }
  8259. break;
  8260. case WM_COPYDATA:
  8261. WMCopyData(*M);
  8262. break;
  8263. case WM_MANAGES_CAPTION:
  8264. // caption managed in TTerminalManager::UpdateAppTitle()
  8265. M->Result = 1;
  8266. break;
  8267. case WM_WANTS_MOUSEWHEEL:
  8268. M->Result = 1;
  8269. break;
  8270. case CM_SHOWINGCHANGED:
  8271. CMShowingChanged(*M);
  8272. break;
  8273. case WM_CLOSE:
  8274. WMClose(*M);
  8275. break;
  8276. case WM_DPICHANGED:
  8277. WMDpiChanged(*M);
  8278. break;
  8279. case CM_DPICHANGED:
  8280. CMDpiChanged(*M);
  8281. break;
  8282. case WM_WININICHANGE:
  8283. WMWinIniChange(*M);
  8284. break;
  8285. case CM_DIALOGKEY:
  8286. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8287. break;
  8288. default:
  8289. TForm::Dispatch(Message);
  8290. break;
  8291. }
  8292. }
  8293. //---------------------------------------------------------------------------
  8294. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8295. {
  8296. // noop
  8297. }
  8298. //---------------------------------------------------------------------------
  8299. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8300. {
  8301. TForm::Dispatch(&Message);
  8302. FSessionColors->Clear();
  8303. AddFixedSessionImages();
  8304. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8305. UpdateImages();
  8306. }
  8307. //---------------------------------------------------------------------------
  8308. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8309. {
  8310. TForm::Dispatch(&Message);
  8311. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8312. UpdatePixelsPerInchMainWindowCounter();
  8313. }
  8314. //---------------------------------------------------------------------------
  8315. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8316. {
  8317. // Cannot close window while we are busy.
  8318. // We cannot test this in FormCloseQuery as that is called also from
  8319. // Close(), which is called by CloseApplicationAction. So we can be busy
  8320. // there already even, when it is legitimate to close the application.
  8321. // Possibly a better place to handle this would be WMSysCommand.
  8322. if (NonVisualDataModule->Busy)
  8323. {
  8324. Message.Result = 1;
  8325. }
  8326. else
  8327. {
  8328. TForm::Dispatch(&Message);
  8329. }
  8330. }
  8331. //---------------------------------------------------------------------------
  8332. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8333. {
  8334. TForm::Dispatch(&Message);
  8335. if (Showing && (Terminal == NULL))
  8336. {
  8337. // When we are starting minimized (i.e. from an installer),
  8338. // postpone showing Login dialog until we get restored.
  8339. // Otherwise the Login dialog (and Authentication window) show restored
  8340. // over invidible (minimized) main window.
  8341. if (WindowState == wsMinimized)
  8342. {
  8343. FNeedSession = true;
  8344. }
  8345. else
  8346. {
  8347. // This happens before application ever goes idle, so the toolbars would
  8348. // stay enabled (initial state) until the Login dialog is dismissed.
  8349. UpdateActions();
  8350. NonVisualDataModule->StartBusy();
  8351. try
  8352. {
  8353. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8354. // otherwise the dialog does not receive focus.
  8355. // With Commander interface the ProcessMessages is called already
  8356. // by TDriveView, but with Explorer interface, we need to call it explicily
  8357. Application->ProcessMessages();
  8358. // do not reload sessions, they have been loaded just now (optimization)
  8359. NeedSession(false);
  8360. }
  8361. __finally
  8362. {
  8363. NonVisualDataModule->EndBusy();
  8364. }
  8365. }
  8366. }
  8367. }
  8368. //---------------------------------------------------------------------------
  8369. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8370. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8371. int & MaxHeight)
  8372. {
  8373. // workaround for bug in TWinControl.CalcConstraints
  8374. // Check for empty rect (restore from iconic state) is done there only after
  8375. // call to AdjustClientRect, which enlarges the rect (for forms).
  8376. TRect R = GetClientRect();
  8377. // when restoring from iconic state, place no restrictions
  8378. if (IsRectEmpty(R))
  8379. {
  8380. MinWidth = 0;
  8381. MinHeight = 0;
  8382. MaxWidth = 32000;
  8383. MaxHeight = 32000;
  8384. }
  8385. }
  8386. //---------------------------------------------------------------------------
  8387. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8388. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8389. {
  8390. // terminal can be already closed (e.g. dropped connection)
  8391. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8392. {
  8393. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8394. try
  8395. {
  8396. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8397. }
  8398. catch(...)
  8399. {
  8400. if (!Terminal->Active)
  8401. {
  8402. Close = true;
  8403. }
  8404. throw;
  8405. }
  8406. }
  8407. }
  8408. //---------------------------------------------------------------------------
  8409. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8410. {
  8411. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8412. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8413. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8414. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8415. {
  8416. OnGetSpaceAvailable = GetSpaceAvailable;
  8417. }
  8418. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8419. OnGetSpaceAvailable);
  8420. }
  8421. //---------------------------------------------------------------------------
  8422. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8423. {
  8424. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8425. Terminal->FillSessionDataForCode(Data.get());
  8426. return Data.release();
  8427. }
  8428. //---------------------------------------------------------------------------
  8429. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8430. {
  8431. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8432. DoGenerateUrlDialog(Data.get(), Paths);
  8433. }
  8434. //---------------------------------------------------------------------------
  8435. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8436. {
  8437. GenerateUrl(NULL);
  8438. }
  8439. //---------------------------------------------------------------------------
  8440. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8441. {
  8442. std::unique_ptr<TStrings> Paths(new TStringList());
  8443. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8444. GenerateUrl(Paths.get());
  8445. }
  8446. //---------------------------------------------------------------------------
  8447. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8448. {
  8449. FSessionColor = value;
  8450. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8451. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8452. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8453. DebugAssert(ColorItem != NULL);
  8454. ColorItem->Color = C;
  8455. NonVisualDataModule->ColorMenuItem->Color = C;
  8456. UpdateControls();
  8457. }
  8458. //---------------------------------------------------------------------------
  8459. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8460. {
  8461. if (value != FSessionColor)
  8462. {
  8463. UpdateSessionColor(value);
  8464. // Is null when called from LastTerminalClosed
  8465. if (Terminal != NULL)
  8466. {
  8467. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8468. }
  8469. }
  8470. }
  8471. //---------------------------------------------------------------------------
  8472. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8473. {
  8474. bool Result = FNoteTimer->Enabled;
  8475. if (Result)
  8476. {
  8477. // cannot cancel note too early
  8478. bool NotEarly =
  8479. (Now() - FNoteShown >
  8480. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8481. if (Force || NotEarly)
  8482. {
  8483. FNoteTimer->Enabled = false;
  8484. FNote = L"";
  8485. SAFE_DESTROY(FNoteData);
  8486. FOnNoteClick = NULL;
  8487. FNoteHints = FNotes->Text;
  8488. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8489. UpdateStatusBar();
  8490. }
  8491. }
  8492. return Result;
  8493. }
  8494. //---------------------------------------------------------------------------
  8495. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8496. {
  8497. DebugAssert(FNoteTimer->Enabled);
  8498. CancelNote(true);
  8499. }
  8500. //---------------------------------------------------------------------------
  8501. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8502. {
  8503. int P = Note.Pos(L"\n");
  8504. if (P > 0)
  8505. {
  8506. Note.SetLength(P - 1);
  8507. }
  8508. FNotes->Add(FORMAT(L"[%s] %s",
  8509. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8510. while (FNotes->Count > 10)
  8511. {
  8512. FNotes->Delete(0);
  8513. }
  8514. if (UpdateNow)
  8515. {
  8516. FNoteHints = FNotes->Text;
  8517. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8518. UpdateStatusBar();
  8519. }
  8520. }
  8521. //---------------------------------------------------------------------------
  8522. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8523. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8524. {
  8525. int P = Note.Pos(L"\n");
  8526. if (P > 0)
  8527. {
  8528. Note.SetLength(P - 1);
  8529. }
  8530. FNoteHints = FNotes->Text;
  8531. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8532. FNote = Note;
  8533. SAFE_DESTROY(FNoteData);
  8534. FNoteData = NoteData;
  8535. FOnNoteClick = OnNoteClick;
  8536. AddNote(Note, false);
  8537. UpdateStatusBar();
  8538. FNoteShown = Now();
  8539. FNoteTimer->Enabled = false;
  8540. if (Seconds == 0)
  8541. {
  8542. Seconds = WinConfiguration->NotificationsTimeout;
  8543. }
  8544. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8545. FNoteTimer->Enabled = true;
  8546. }
  8547. //---------------------------------------------------------------------------
  8548. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8549. {
  8550. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8551. }
  8552. //---------------------------------------------------------------------------
  8553. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8554. {
  8555. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8556. {
  8557. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8558. }
  8559. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8560. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8561. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8562. 0, NULL, NULL);
  8563. }
  8564. //---------------------------------------------------------------------------
  8565. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8566. {
  8567. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8568. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8569. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8570. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8571. }
  8572. //---------------------------------------------------------------------------
  8573. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8574. {
  8575. WinConfiguration->FormatSizeBytes = Style;
  8576. }
  8577. //---------------------------------------------------------------------------
  8578. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8579. {
  8580. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8581. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8582. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8583. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8584. }
  8585. //---------------------------------------------------------------------------
  8586. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8587. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8588. {
  8589. if (Panel->Index == 0)
  8590. {
  8591. if (FOnNoteClick != NULL)
  8592. {
  8593. // prevent the user data from being freed by possible call
  8594. // to CancelNote or PostNote during call to OnNoteClick
  8595. std::unique_ptr<TObject> NoteData(FNoteData);
  8596. TNotifyEvent OnNoteClick = FOnNoteClick;
  8597. FNoteData = NULL;
  8598. // need to cancel the note as we are going to delete its user data
  8599. CancelNote(true);
  8600. OnNoteClick(NoteData.get());
  8601. }
  8602. }
  8603. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8604. {
  8605. FileSystemInfo();
  8606. }
  8607. }
  8608. //---------------------------------------------------------------------------
  8609. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8610. {
  8611. // workaround:
  8612. // 1) for unknown reason, disabling window, while minimized,
  8613. // prevents it from restoring, even if it was enabled again meanwhile
  8614. // 2) when disabling the main window, while another has focus
  8615. // minimize is no longer possible
  8616. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8617. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8618. // Shouldn't we use IsApplicationMinimized() here?
  8619. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8620. {
  8621. Enabled = false;
  8622. }
  8623. FLockLevel++;
  8624. }
  8625. //---------------------------------------------------------------------------
  8626. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8627. {
  8628. DebugAssert(FLockLevel > 0);
  8629. FLockLevel--;
  8630. if (FLockLevel == 0)
  8631. {
  8632. DebugAssert(FLockSuspendLevel == 0);
  8633. Enabled = true;
  8634. }
  8635. }
  8636. //---------------------------------------------------------------------------
  8637. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8638. {
  8639. // We need to make sure that window is enabled when the last modal window closes
  8640. // otherwise focus is not restored correctly.
  8641. // So we re-enable the window when modal window opens and
  8642. // disable it back after is closes
  8643. if (FLockLevel > 0)
  8644. {
  8645. // while we have nesting counter, we know that we never be called
  8646. // recursivelly as Application->OnModalBegin is called only
  8647. // for the top-level modal window
  8648. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8649. {
  8650. // won't be disabled when conditions in LockWindow() were not satisfied
  8651. FDisabledOnLockSuspend = !Enabled;
  8652. // When minimized to tray (or actually when set to SW_HIDE),
  8653. // setting Enabled makes the window focusable even when there's
  8654. // modal window over it
  8655. if (!FTrayIcon->Visible)
  8656. {
  8657. Enabled = true;
  8658. }
  8659. }
  8660. FLockSuspendLevel++;
  8661. }
  8662. }
  8663. //---------------------------------------------------------------------------
  8664. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8665. {
  8666. if (FLockSuspendLevel > 0)
  8667. {
  8668. DebugAssert(FLockLevel > 0);
  8669. FLockSuspendLevel--;
  8670. // see comment in SuspendWindowLock
  8671. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8672. {
  8673. // Note that window can be enabled here, when we were minized to tray when
  8674. // was SuspendWindowLock() called.
  8675. // We should possibly do the same check as in LockWindow(),
  8676. // if it is ever possible that the consitions change between
  8677. // SuspendWindowLock() and ResumeWindowLock()
  8678. Enabled = !FDisabledOnLockSuspend;
  8679. }
  8680. }
  8681. }
  8682. //---------------------------------------------------------------------------
  8683. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8684. {
  8685. if (!TextOnly)
  8686. {
  8687. TTBXComboBoxItem * RemotePathComboBox =
  8688. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8689. TStrings * Items = RemotePathComboBox->Strings;
  8690. Items->BeginUpdate();
  8691. try
  8692. {
  8693. Items->Clear();
  8694. if (Terminal != NULL)
  8695. {
  8696. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8697. while (!IsUnixRootPath(APath))
  8698. {
  8699. int P = APath.LastDelimiter(L'/');
  8700. DebugAssert(P >= 0);
  8701. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8702. APath.SetLength(P - 1);
  8703. }
  8704. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8705. }
  8706. }
  8707. __finally
  8708. {
  8709. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8710. // Setting ItemIndex to -1 does not reset its text
  8711. if (Items->Count == 0)
  8712. {
  8713. RemotePathComboBox->Text = L"";
  8714. }
  8715. Items->EndUpdate();
  8716. }
  8717. }
  8718. }
  8719. //---------------------------------------------------------------------------
  8720. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8721. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8722. int & ImageIndex)
  8723. {
  8724. if (AIndex < 0)
  8725. {
  8726. AIndex = Sender->ItemIndex;
  8727. }
  8728. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8729. }
  8730. //---------------------------------------------------------------------------
  8731. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8732. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8733. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8734. {
  8735. ARect.Left += (10 * AIndex);
  8736. }
  8737. //---------------------------------------------------------------------------
  8738. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8739. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8740. {
  8741. AWidth += (10 * AIndex);
  8742. }
  8743. //---------------------------------------------------------------------------
  8744. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8745. TObject * Sender)
  8746. {
  8747. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8748. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8749. int Index = RemotePathComboBox->ItemIndex;
  8750. while (Index < RemotePathComboBox->Strings->Count - 1)
  8751. {
  8752. APath = UnixExtractFileDir(APath);
  8753. Index++;
  8754. }
  8755. // VanDyke style paths
  8756. if (APath.IsEmpty())
  8757. {
  8758. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8759. APath = ROOTDIRECTORY;
  8760. }
  8761. if (RemoteDirView->Path != APath)
  8762. {
  8763. RemoteDirView->Path = APath;
  8764. }
  8765. }
  8766. //---------------------------------------------------------------------------
  8767. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8768. {
  8769. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8770. DebugUsedParam(Sender);
  8771. UpdateRemotePathComboBox(true);
  8772. }
  8773. //---------------------------------------------------------------------------
  8774. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8775. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8776. {
  8777. ResetIncrementalSearch();
  8778. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8779. DebugAssert(DirView != NULL);
  8780. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8781. {
  8782. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8783. // OnEditing is called also from TCustomListView::CanEdit
  8784. if (Edit != NULL)
  8785. {
  8786. EditSelectBaseName(Edit);
  8787. }
  8788. }
  8789. }
  8790. //---------------------------------------------------------------------------
  8791. //---------------------------------------------------------------------------
  8792. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8793. {
  8794. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8795. Result->AutoDetectDnD = false;
  8796. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8797. Result->RenderDataOn = rdoEnterAndDropSync;
  8798. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8799. return Result;
  8800. }
  8801. //---------------------------------------------------------------------------
  8802. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8803. {
  8804. TForm::CreateWnd();
  8805. if (FSessionsDragDropFilesEx == NULL)
  8806. {
  8807. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8808. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8809. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8810. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8811. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8812. }
  8813. if (FQueueDragDropFilesEx == NULL)
  8814. {
  8815. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8816. // No need to set OnDragOver as we do not have any restrictions
  8817. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8818. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8819. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8820. }
  8821. }
  8822. //---------------------------------------------------------------------------
  8823. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8824. {
  8825. TForm::DestroyWnd();
  8826. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8827. FQueueDragDropFilesEx->DragDropControl = NULL;
  8828. }
  8829. //---------------------------------------------------------------------------
  8830. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8831. {
  8832. SideEnter(FCurrentSide);
  8833. FEverShown = true;
  8834. }
  8835. //---------------------------------------------------------------------------
  8836. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8837. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8838. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8839. {
  8840. if (!NonVisualDataModule->Busy)
  8841. {
  8842. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8843. Configuration->Usage->Inc(L"FileFinds");
  8844. LockWindow(true);
  8845. NonVisualDataModule->StartBusy();
  8846. WinConfiguration->LockedInterface = true;
  8847. try
  8848. {
  8849. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8850. }
  8851. __finally
  8852. {
  8853. WinConfiguration->LockedInterface = false;
  8854. NonVisualDataModule->EndBusy();
  8855. UnlockWindow();
  8856. }
  8857. }
  8858. }
  8859. //---------------------------------------------------------------------------
  8860. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8861. {
  8862. if (!NonVisualDataModule->Busy)
  8863. {
  8864. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8865. SetFocus();
  8866. RemoteDirView->Path = UnixExtractFilePath(Path);
  8867. UnicodeString FileName = UnixExtractFileName(Path);
  8868. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  8869. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  8870. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  8871. if (Item == NULL)
  8872. {
  8873. ReloadDirectory(osRemote);
  8874. Item = RemoteDirView->FindFileItem(FileName);
  8875. }
  8876. if (Item != NULL)
  8877. {
  8878. RemoteDirView->ItemFocused = Item;
  8879. RemoteDirView->ItemFocused->MakeVisible(false);
  8880. RemoteDirView->SetFocus();
  8881. }
  8882. }
  8883. }
  8884. //---------------------------------------------------------------------------
  8885. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  8886. {
  8887. bool Result = !NonVisualDataModule->Busy;
  8888. if (Result)
  8889. {
  8890. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8891. }
  8892. return Result;
  8893. }
  8894. //---------------------------------------------------------------------------
  8895. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  8896. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8897. {
  8898. if (CanOperateOnFoundFiles(ATerminal))
  8899. {
  8900. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  8901. FOnFileOperationFinished = OnFileOperationFinished;
  8902. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  8903. }
  8904. }
  8905. //---------------------------------------------------------------------------
  8906. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  8907. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8908. {
  8909. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  8910. }
  8911. //---------------------------------------------------------------------------
  8912. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  8913. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8914. {
  8915. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  8916. }
  8917. //---------------------------------------------------------------------------
  8918. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  8919. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8920. {
  8921. if (CanOperateOnFoundFiles(ATerminal))
  8922. {
  8923. for (int Index = 0; Index < FileList->Count; Index++)
  8924. {
  8925. UnicodeString FileName = FileList->Strings[Index];
  8926. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  8927. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  8928. OnFileOperationFinished(FileName, true);
  8929. }
  8930. }
  8931. }
  8932. //---------------------------------------------------------------------------
  8933. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  8934. {
  8935. FFileFindTerminal = Terminal;
  8936. ShowFileFindDialog(
  8937. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  8938. DoDownloadFoundFiles, DoEditFoundFiles);
  8939. }
  8940. //---------------------------------------------------------------------------
  8941. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  8942. {
  8943. FTaskbarList = TaskbarList;
  8944. }
  8945. //---------------------------------------------------------------------------
  8946. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  8947. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  8948. {
  8949. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8950. if (Index >= 0)
  8951. {
  8952. if (Button == mbLeft)
  8953. {
  8954. // "Mouse down" is raised only after tab is switched.
  8955. // If switching tab (switching session) takes long enough for user
  8956. // to actually release the button, "mouse down" is still raised,
  8957. // but we do not get "mouse up" event, so dragging is not cancelled,
  8958. // prevent that by not beginning dragging in the first place.
  8959. if (IsKeyPressed(VK_LBUTTON))
  8960. {
  8961. // when user clicks the "+", we get mouse down only after the session
  8962. // is closed, when new session tab is already on X:Y, so dragging
  8963. // starts, prevent that
  8964. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  8965. {
  8966. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8967. if (Terminal != NULL)
  8968. {
  8969. SessionsPageControl->BeginDrag(false);
  8970. }
  8971. }
  8972. }
  8973. }
  8974. else if (Button == mbMiddle)
  8975. {
  8976. // ignore middle-click for "New session tab"
  8977. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8978. if (Terminal != NULL)
  8979. {
  8980. CloseSessionTab(Index);
  8981. }
  8982. }
  8983. }
  8984. }
  8985. //---------------------------------------------------------------------------
  8986. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  8987. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  8988. {
  8989. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8990. // do not allow dropping on the "+" tab
  8991. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8992. if ((TargetTerminal != NULL) &&
  8993. (SessionsPageControl->ActivePage->PageIndex != Index))
  8994. {
  8995. Configuration->Usage->Inc(L"SessionTabMoves");
  8996. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  8997. // but we want to actually prevent that to avoid flicker
  8998. SessionsPageControl->ActivePage->PageIndex = Index;
  8999. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  9000. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9001. }
  9002. }
  9003. //---------------------------------------------------------------------------
  9004. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9005. TObject * Sender, TObject * Source, int X, int Y,
  9006. TDragState /*State*/, bool & Accept)
  9007. {
  9008. Accept = (Sender == Source);
  9009. if (Accept)
  9010. {
  9011. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9012. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9013. // do not allow dragging to the "+" tab
  9014. Accept = (Terminal != NULL);
  9015. }
  9016. }
  9017. //---------------------------------------------------------------------------
  9018. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  9019. const TPoint & Point, int & Effect)
  9020. {
  9021. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9022. if (Index < 0)
  9023. {
  9024. Effect = DROPEFFECT_NONE;
  9025. }
  9026. else
  9027. {
  9028. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9029. // do not allow dropping on the "+" tab
  9030. if (TargetTerminal == NULL)
  9031. {
  9032. Effect = DROPEFFECT_NONE;
  9033. }
  9034. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_MOVE))
  9035. {
  9036. Effect = DROPEFFECT_COPY;
  9037. }
  9038. }
  9039. }
  9040. //---------------------------------------------------------------------------
  9041. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9042. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9043. {
  9044. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9045. // do not allow dropping on the "+" tab
  9046. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9047. if (TargetTerminal != NULL)
  9048. {
  9049. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9050. if (!IsFileControl(DropSourceControl, osRemote))
  9051. {
  9052. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  9053. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9054. // Why don't we use Terminal->CurrentDirectory directly?
  9055. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9056. // do not force queue, drag drop
  9057. false, true);
  9058. }
  9059. }
  9060. }
  9061. //---------------------------------------------------------------------------
  9062. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9063. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9064. {
  9065. // Downloads are handled in RemoteFileControlDDEnd
  9066. if (!IsFileControl(DropSourceControl, osRemote))
  9067. {
  9068. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9069. Terminal->CurrentDirectory,
  9070. // force queue, drag drop
  9071. true, true);
  9072. }
  9073. }
  9074. //---------------------------------------------------------------------------
  9075. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9076. {
  9077. FShowing = false;
  9078. // Do not save empty workspace
  9079. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  9080. {
  9081. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9082. UnicodeString Name = WorkspaceName();
  9083. DoSaveWorkspace(
  9084. Name, DataList.get(),
  9085. !Configuration->DisablePasswordStoring &&
  9086. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9087. WinConfiguration->LastStoredSession = Name;
  9088. }
  9089. }
  9090. //---------------------------------------------------------------------------
  9091. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9092. {
  9093. TManagedTerminal * ManagedTerminal =
  9094. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9095. if (ManagedTerminal != NULL)
  9096. {
  9097. ManagedTerminal->DirectoryLoaded = Now();
  9098. }
  9099. }
  9100. //---------------------------------------------------------------------------
  9101. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9102. TListItem * /*Item*/, bool /*Selected*/)
  9103. {
  9104. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9105. switch (DirView->LastSelectMethod)
  9106. {
  9107. case smKeyboard:
  9108. Configuration->Usage->Inc(L"KeyboardSelections");
  9109. break;
  9110. case smMouse:
  9111. Configuration->Usage->Inc(L"MouseSelections");
  9112. break;
  9113. }
  9114. }
  9115. //---------------------------------------------------------------------------
  9116. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9117. {
  9118. if (Color != 0)
  9119. {
  9120. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9121. }
  9122. else
  9123. {
  9124. return FSessionTabImageIndex;
  9125. }
  9126. }
  9127. //---------------------------------------------------------------------------
  9128. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9129. {
  9130. TPngImageCollectionItem * Item =
  9131. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9132. FSessionColors->AddPng(Item->PngImage);
  9133. return FSessionColors->Count - 1;
  9134. }
  9135. //---------------------------------------------------------------------------
  9136. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9137. {
  9138. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9139. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9140. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9141. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9142. }
  9143. //---------------------------------------------------------------------------
  9144. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9145. {
  9146. for (int Index = 0; Index < Control->ControlCount; Index++)
  9147. {
  9148. TControl * AControl = Control->Controls[Index];
  9149. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9150. if (WinControl != NULL)
  9151. {
  9152. CollectItemsWithTextDisplayMode(WinControl);
  9153. }
  9154. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9155. if (Toolbar != NULL)
  9156. {
  9157. // we care for top-level items only
  9158. TTBCustomItem * Items = Toolbar->Items;
  9159. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9160. {
  9161. TTBCustomItem * Item = Items->Items[ItemIndex];
  9162. if (((Item->DisplayMode == nbdmImageAndText) ||
  9163. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9164. EligibleForImageDisplayMode(Item))
  9165. {
  9166. FItemsWithTextDisplayMode.insert(Item);
  9167. }
  9168. }
  9169. }
  9170. }
  9171. }
  9172. //---------------------------------------------------------------------------
  9173. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9174. {
  9175. return true;
  9176. }
  9177. //---------------------------------------------------------------------------
  9178. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9179. {
  9180. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9181. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9182. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9183. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9184. bool Result = true;
  9185. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9186. {
  9187. TTBCustomItem * Item = *i;
  9188. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9189. if (Label != NULL)
  9190. {
  9191. // optimization
  9192. if (Label->Visible == SelectiveToolbarText)
  9193. {
  9194. Result = false;
  9195. }
  9196. else
  9197. {
  9198. Label->Visible = SelectiveToolbarText;
  9199. }
  9200. }
  9201. else
  9202. {
  9203. // optimization
  9204. if (Item->DisplayMode == DisplayMode)
  9205. {
  9206. Result = false;
  9207. }
  9208. else
  9209. {
  9210. Item->DisplayMode = DisplayMode;
  9211. }
  9212. }
  9213. ++i;
  9214. }
  9215. if (Result)
  9216. {
  9217. UpdateNewSessionTab();
  9218. }
  9219. return Result;
  9220. }
  9221. //---------------------------------------------------------------------------
  9222. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9223. {
  9224. DebugFail();
  9225. }
  9226. //---------------------------------------------------------------------------
  9227. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9228. {
  9229. // Among other, a lock level is non zero, while directory is being loaded,
  9230. // even when it happens as a result of dirview navigation,
  9231. // i.e. when TNonVisualDataModule is NOT FBusy.
  9232. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9233. // Among other this prevents a panel auto update to occur while
  9234. // directory is changing.
  9235. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9236. }
  9237. //---------------------------------------------------------------------------
  9238. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9239. {
  9240. // While the window is disabled, we still seem to process menu shortcuts at least,
  9241. // so stop it at least here.
  9242. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9243. return
  9244. (Allowed == aaUpdate) ||
  9245. !NonVisualDataModule->Busy;
  9246. }
  9247. //---------------------------------------------------------------------------
  9248. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9249. {
  9250. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9251. }
  9252. //---------------------------------------------------------------------------
  9253. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9254. {
  9255. // This is somewhat redundant to LockWindow() call from
  9256. // TTerminalManager::TerminalReadDirectoryProgress.
  9257. // But disabling window is known not to block keyboard shorcuts
  9258. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9259. if (Busy > 0)
  9260. {
  9261. if (NonVisualDataModule->Busy)
  9262. {
  9263. State = false;
  9264. }
  9265. else
  9266. {
  9267. NonVisualDataModule->StartBusy();
  9268. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9269. LockWindow();
  9270. }
  9271. }
  9272. else if (Busy < 0)
  9273. {
  9274. UnlockWindow();
  9275. NonVisualDataModule->EndBusy();
  9276. }
  9277. else
  9278. {
  9279. State = NonVisualDataModule->Busy;
  9280. }
  9281. }
  9282. //---------------------------------------------------------------------------
  9283. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9284. {
  9285. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9286. // no context menu for "New session tab"
  9287. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9288. {
  9289. SessionsPageControl->ActivePageIndex = Index;
  9290. if (DebugAlwaysTrue(SessionTabSwitched()))
  9291. {
  9292. // copied from TControl.WMContextMenu
  9293. SendCancelMode(SessionsPageControl);
  9294. // explicit popup instead of using PopupMenu property
  9295. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9296. // while connecting yet not-connected session and hence
  9297. // allowing an access to commands over not-completelly connected session
  9298. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9299. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9300. PopupMenu->PopupComponent = SessionsPageControl;
  9301. PopupMenu->Popup(Point.x, Point.y);
  9302. }
  9303. }
  9304. Handled = true;
  9305. }
  9306. //---------------------------------------------------------------------------
  9307. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9308. {
  9309. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9310. }
  9311. //---------------------------------------------------------------------------
  9312. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9313. {
  9314. Configuration->Usage->Inc(L"PasswordChanges");
  9315. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9316. ChangePasswordData->DisableAuthentationsExceptPassword();
  9317. ChangePasswordData->ChangePassword = true;
  9318. std::unique_ptr<TTerminal> ChangePasswordSession(
  9319. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9320. ChangePasswordSession->AutoReadDirectory = false;
  9321. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9322. {
  9323. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9324. }
  9325. else
  9326. {
  9327. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9328. }
  9329. }
  9330. //---------------------------------------------------------------------------
  9331. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9332. {
  9333. // No nice way to assert SSH2
  9334. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9335. }
  9336. //---------------------------------------------------------------------------
  9337. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9338. {
  9339. TOperationVisualizer OperationVisualizer;
  9340. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9341. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9342. }
  9343. //---------------------------------------------------------------------------
  9344. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9345. {
  9346. TForm::ChangeScale(M, D);
  9347. int APixelsPerInch = GetControlPixelsPerInch(this);
  9348. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9349. try
  9350. {
  9351. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9352. }
  9353. __finally
  9354. {
  9355. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9356. }
  9357. }
  9358. //---------------------------------------------------------------------------
  9359. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9360. {
  9361. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9362. }
  9363. //---------------------------------------------------------------------
  9364. class TPublicControl : public TControl
  9365. {
  9366. friend class TCustomScpExplorerForm;
  9367. };
  9368. //---------------------------------------------------------------------------
  9369. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9370. {
  9371. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9372. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9373. }
  9374. //---------------------------------------------------------------------------
  9375. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9376. {
  9377. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9378. OpenBookmark(Side, Bookmark);
  9379. }
  9380. //---------------------------------------------------------------------------
  9381. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9382. {
  9383. DoBookmarkClick(osLocal, Sender);
  9384. }
  9385. //---------------------------------------------------------------------------
  9386. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9387. {
  9388. DoBookmarkClick(osRemote, Sender);
  9389. }
  9390. //---------------------------------------------------------------------------
  9391. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9392. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9393. {
  9394. // TODO
  9395. if (BookmarkList != NULL)
  9396. {
  9397. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9398. if (!WinConfiguration->UseLocationProfiles)
  9399. {
  9400. std::unique_ptr<TStringList> Directories(new TStringList());
  9401. Directories->CaseSensitive = (Side == osRemote);
  9402. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9403. {
  9404. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9405. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9406. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9407. {
  9408. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9409. Item->Caption = EscapeHotkey(Directory);
  9410. Item->ShortCut = Bookmark->ShortCut;
  9411. Item->OnClick = OnBookmarkClick;
  9412. Item->Tag = reinterpret_cast<int>(Bookmark);
  9413. Directories->Add(Directory);
  9414. Menu->Add(Item.release());
  9415. }
  9416. }
  9417. }
  9418. else
  9419. {
  9420. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9421. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9422. {
  9423. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9424. if (!Bookmark->Node.IsEmpty())
  9425. {
  9426. Folders->Add(Bookmark->Node);
  9427. }
  9428. }
  9429. for (int Index = 0; Index < Folders->Count; Index++)
  9430. {
  9431. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9432. Item->Caption = Folders->Strings[Index];
  9433. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9434. Folders->Objects[Index] = Item.get();
  9435. Menu->Add(Item.release());
  9436. }
  9437. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9438. {
  9439. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9440. TTBCustomItem * Parent;
  9441. if (!Bookmark->Node.IsEmpty())
  9442. {
  9443. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9444. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9445. }
  9446. else
  9447. {
  9448. Parent = Menu;
  9449. }
  9450. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9451. Item->Caption = Bookmark->Name;
  9452. Item->ShortCut = Bookmark->ShortCut;
  9453. Item->OnClick = OnBookmarkClick;
  9454. Item->Tag = reinterpret_cast<int>(Bookmark);
  9455. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9456. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9457. {
  9458. // No hint for location profiles that are actually mere "bookmarks"
  9459. }
  9460. else
  9461. {
  9462. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9463. UnicodeString LongHint;
  9464. if (!Bookmark->Local.IsEmpty())
  9465. {
  9466. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9467. }
  9468. if (!Bookmark->Remote.IsEmpty())
  9469. {
  9470. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9471. }
  9472. AddToList(Hint, LongHint, L"|");
  9473. Item->Hint = Hint;
  9474. }
  9475. Parent->Add(Item.release());
  9476. }
  9477. }
  9478. AddMenuSeparator(Menu);
  9479. }
  9480. }
  9481. //---------------------------------------------------------------------------
  9482. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9483. {
  9484. Menu->Clear();
  9485. std::unique_ptr<TTBCustomItem> Item;
  9486. Item.reset(new TTBXItem(Owner));
  9487. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9488. Item->Options = Item->Options << tboDefault;
  9489. Menu->Add(Item.release());
  9490. AddMenuSeparator(Menu);
  9491. if (Terminal != NULL)
  9492. {
  9493. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9494. }
  9495. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9496. Item.reset(new TTBXItem(Owner));
  9497. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9498. Menu->Add(Item.release());
  9499. }
  9500. //---------------------------------------------------------------------------
  9501. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9502. {
  9503. bool Result = true;
  9504. try
  9505. {
  9506. if (Side == osRemote)
  9507. {
  9508. Terminal->ExceptionOnFail = true;
  9509. }
  9510. try
  9511. {
  9512. DirView(Side)->Path = Path;
  9513. }
  9514. __finally
  9515. {
  9516. if (Side == osRemote)
  9517. {
  9518. Terminal->ExceptionOnFail = false;
  9519. }
  9520. }
  9521. }
  9522. catch (Exception & E)
  9523. {
  9524. if ((Side != osRemote) || Terminal->Active)
  9525. {
  9526. ShowExtendedExceptionEx(Terminal, &E);
  9527. Result = false;
  9528. }
  9529. else
  9530. {
  9531. throw;
  9532. }
  9533. }
  9534. return Result;
  9535. }
  9536. //---------------------------------------------------------------------------
  9537. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9538. {
  9539. // Make sure there some feedback even when loading tiny local folders
  9540. TInstantOperationVisualizer Visualizer;
  9541. DirView(Side)->ReloadDirectory();
  9542. }
  9543. //---------------------------------------------------------------------------
  9544. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9545. {
  9546. if (Index == SessionsPageControl->TabIndex)
  9547. {
  9548. CloseSession();
  9549. }
  9550. else
  9551. {
  9552. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9553. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9554. TTerminalManager * Manager = TTerminalManager::Instance();
  9555. if (!Terminal->Active)
  9556. {
  9557. Manager->FreeTerminal(Terminal);
  9558. }
  9559. else
  9560. {
  9561. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9562. if (CanCloseQueue(Queue))
  9563. {
  9564. Manager->FreeTerminal(Terminal);
  9565. }
  9566. }
  9567. }
  9568. }
  9569. //---------------------------------------------------------------------------
  9570. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9571. {
  9572. CloseSessionTab(Index);
  9573. }
  9574. //---------------------------------------------------------------------------
  9575. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side)
  9576. {
  9577. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  9578. {
  9579. TInstantOperationVisualizer Visualizer;
  9580. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9581. // before we create a new data object
  9582. ClipboardClear();
  9583. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9584. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9585. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9586. FClipboardFakeMonitors.reset(StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated));
  9587. if (FClipboardDragDropFilesEx.get() == NULL)
  9588. {
  9589. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9590. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9591. }
  9592. FClipboardDragDropFilesEx->FileList->Clear();
  9593. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9594. FClipboardDragDropFilesEx->CopyToClipboard();
  9595. FClipboardTerminal = FTerminal;
  9596. // Need full paths, as cwd can be different once files are pasted
  9597. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(false, true)));
  9598. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9599. {
  9600. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9601. }
  9602. }
  9603. }
  9604. //---------------------------------------------------------------------------
  9605. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9606. {
  9607. if (OpenClipboard(0))
  9608. {
  9609. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9610. CloseClipboard();
  9611. }
  9612. }
  9613. //---------------------------------------------------------------------------
  9614. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9615. {
  9616. return !FClipboardFakeDirectory.IsEmpty();
  9617. }
  9618. //---------------------------------------------------------------------------
  9619. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9620. {
  9621. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9622. FClipboardFakeDirectory = UnicodeString();
  9623. FClipboardTerminal = NULL;
  9624. // Also called in Idle()
  9625. FClipboardFakeMonitors.reset(NULL);
  9626. }
  9627. //---------------------------------------------------------------------------
  9628. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9629. {
  9630. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9631. {
  9632. ClipboardStop();
  9633. }
  9634. }
  9635. //---------------------------------------------------------------------------
  9636. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9637. {
  9638. TTerminalManager * Manager = TTerminalManager::Instance();
  9639. Manager->ActiveTerminal = FClipboardTerminal;
  9640. TTransferOperationParam Params;
  9641. Params.TargetDirectory = TargetDirectory;
  9642. Params.DragDrop = DragDrop;
  9643. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9644. }
  9645. //---------------------------------------------------------------------------
  9646. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9647. {
  9648. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9649. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9650. {
  9651. RemoveDir(ApiPath(FileName));
  9652. if (!NonVisualDataModule->Busy)
  9653. {
  9654. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9655. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9656. }
  9657. }
  9658. }
  9659. //---------------------------------------------------------------------------
  9660. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  9661. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  9662. {
  9663. TFileMasks::TParams MaskParams;
  9664. MaskParams.Size = Size;
  9665. MaskParams.Modification = Modification;
  9666. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  9667. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  9668. {
  9669. bool ImplicitMatch;
  9670. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  9671. !ImplicitMatch)
  9672. {
  9673. Color = Iter->Color;
  9674. break;
  9675. }
  9676. }
  9677. }
  9678. //---------------------------------------------------------------------------
  9679. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  9680. {
  9681. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9682. if (!DirView->IsEditing())
  9683. {
  9684. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  9685. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  9686. // while searching
  9687. if (Key == VK_BACK)
  9688. {
  9689. if (!FIncrementalSearch.IsEmpty())
  9690. {
  9691. if (FIncrementalSearch.Length() == 1)
  9692. {
  9693. ResetIncrementalSearch();
  9694. }
  9695. else
  9696. {
  9697. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  9698. IncrementalSearch(NewText, false, false);
  9699. }
  9700. Key = 0;
  9701. }
  9702. }
  9703. }
  9704. }
  9705. //---------------------------------------------------------------------------
  9706. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  9707. {
  9708. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9709. if (!DirView->IsEditing())
  9710. {
  9711. // Filter control sequences.
  9712. // When not searching yet, prefer use of the space for toggling file selection
  9713. // (so we cannot incrementally search for a string starting with a space).
  9714. if ((Key > VK_SPACE) ||
  9715. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  9716. {
  9717. IncrementalSearch(FIncrementalSearch + Key, false, false);
  9718. Key = 0;
  9719. }
  9720. }
  9721. }
  9722. //---------------------------------------------------------------------------
  9723. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  9724. {
  9725. if (!FIncrementalSearch.IsEmpty())
  9726. {
  9727. FIncrementalSearch = L"";
  9728. DirView(osCurrent)->UpdateStatusBar();
  9729. }
  9730. }
  9731. //---------------------------------------------------------------------------
  9732. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9733. {
  9734. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  9735. if (Item == NULL)
  9736. {
  9737. MessageBeep(MB_ICONHAND);
  9738. }
  9739. else
  9740. {
  9741. TCustomDirView * ADirView = DirView(osCurrent);
  9742. {
  9743. TAutoNestingCounter Guard(FIncrementalSearching);
  9744. ADirView->FocusItem(Item);
  9745. if (ADirView->NortonLike == nlOff)
  9746. {
  9747. if ((ADirView->Selected != Item) ||
  9748. (ADirView->SelCount > 1))
  9749. {
  9750. ADirView->ClearSelection();
  9751. ADirView->Selected = Item;
  9752. }
  9753. }
  9754. }
  9755. FIncrementalSearch = Text;
  9756. Item->MakeVisible(false);
  9757. TListItem * NextItem = SearchFile(Text, true, Reverse);
  9758. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  9759. ADirView->UpdateStatusBar();
  9760. }
  9761. }
  9762. //---------------------------------------------------------------------------
  9763. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  9764. {
  9765. int Index = Item->Index;
  9766. if (!Reverse)
  9767. {
  9768. Index++;
  9769. if (Index >= Item->Owner->Count)
  9770. {
  9771. Index = 0;
  9772. }
  9773. }
  9774. else
  9775. {
  9776. Index--;
  9777. if (Index < 0)
  9778. {
  9779. Index = Item->Owner->Count - 1;
  9780. }
  9781. }
  9782. return Item->Owner->Item[Index];
  9783. }
  9784. //---------------------------------------------------------------------------
  9785. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9786. {
  9787. TCustomDirView * ADirView = DirView(osCurrent);
  9788. if (ADirView->Items->Count == 0)
  9789. {
  9790. return NULL;
  9791. }
  9792. else
  9793. {
  9794. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  9795. TListItem * Item = CurrentItem;
  9796. if (SkipCurrent)
  9797. {
  9798. Item = DebugNotNull(GetNextFile(Item, Reverse));
  9799. }
  9800. while (true)
  9801. {
  9802. bool Matches = false;
  9803. switch (WinConfiguration->PanelSearch)
  9804. {
  9805. case isNameStartOnly:
  9806. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  9807. break;
  9808. case isName:
  9809. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  9810. break;
  9811. case isAll:
  9812. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  9813. int Index = 0;
  9814. while ((Index < ColCount) && !Matches)
  9815. {
  9816. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  9817. if (Visible)
  9818. {
  9819. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  9820. }
  9821. Index++;
  9822. }
  9823. break;
  9824. }
  9825. if (Matches)
  9826. {
  9827. return Item;
  9828. }
  9829. Item = GetNextFile(Item, Reverse);
  9830. if (Item == CurrentItem)
  9831. {
  9832. return NULL;
  9833. }
  9834. }
  9835. }
  9836. }
  9837. //---------------------------------------------------------------------------
  9838. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  9839. {
  9840. ResetIncrementalSearch();
  9841. }
  9842. //---------------------------------------------------------------------------
  9843. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  9844. {
  9845. if (Message.CharCode == VK_TAB)
  9846. {
  9847. if (!FIncrementalSearch.IsEmpty())
  9848. {
  9849. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  9850. bool Reverse = Shift.Contains(ssShift);
  9851. IncrementalSearch(FIncrementalSearch, true, Reverse);
  9852. Message.Result = 1;
  9853. return;
  9854. }
  9855. }
  9856. else if (Message.CharCode == VK_ESCAPE)
  9857. {
  9858. if (!FIncrementalSearch.IsEmpty())
  9859. {
  9860. ResetIncrementalSearch();
  9861. Message.Result = 1;
  9862. return;
  9863. }
  9864. }
  9865. TForm::Dispatch(&Message);
  9866. }
  9867. //---------------------------------------------------------------------------
  9868. void __fastcall TCustomScpExplorerForm::Deactivate()
  9869. {
  9870. TForm::Deactivate();
  9871. // E.g. when switching to an internal editor window
  9872. ResetIncrementalSearch();
  9873. }
  9874. //---------------------------------------------------------------------------
  9875. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  9876. {
  9877. // When switching to another application
  9878. ResetIncrementalSearch();
  9879. }
  9880. //---------------------------------------------------------------------------
  9881. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  9882. {
  9883. ResetIncrementalSearch();
  9884. }
  9885. //---------------------------------------------------------------------------
  9886. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  9887. {
  9888. if (FIncrementalSearching <= 0)
  9889. {
  9890. ResetIncrementalSearch();
  9891. }
  9892. }
  9893. //---------------------------------------------------------------------------
  9894. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  9895. {
  9896. CountClicksForWindowPrint(this);
  9897. }
  9898. //---------------------------------------------------------------------------