CustomScpExplorer.cpp 286 KB

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