CustomScpExplorer.cpp 330 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <Bookmarks.h>
  8. #include <Interface.h>
  9. #include <Exceptions.h>
  10. #include <CoreMain.h>
  11. #include <FileSystems.h>
  12. #include <TextsCore.h>
  13. #include <TextsWin.h>
  14. #include <HelpWin.h>
  15. #include <VCLCommon.h>
  16. #include <Progress.h>
  17. #include <SynchronizeProgress.h>
  18. #include <DragExt.h>
  19. #include <WinApi.h>
  20. #include "GUITools.h"
  21. #include "NonVisual.h"
  22. #include "Glyphs.h"
  23. #include "Tools.h"
  24. #include "WinConfiguration.h"
  25. #include "TerminalManager.h"
  26. #include "EditorManager.h"
  27. #include "ProgParams.h"
  28. #include "Setup.h"
  29. #include <Consts.hpp>
  30. #include <DateUtils.hpp>
  31. #include <TB2Common.hpp>
  32. #include <DirectoryMonitor.hpp>
  33. //---------------------------------------------------------------------------
  34. #pragma package(smart_init)
  35. #pragma link "CustomDirView"
  36. #pragma link "CustomUnixDirView"
  37. #pragma link "IEListView"
  38. #pragma link "NortonLikeListView"
  39. #pragma link "UnixDirView"
  40. #pragma link "CustomDriveView"
  41. #pragma link "UnixDriveView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "TB2Dock"
  44. #pragma link "TBXStatusBars"
  45. #pragma link "TB2Item"
  46. #pragma link "TB2Toolbar"
  47. #pragma link "ThemePageControl"
  48. #pragma link "PathLabel"
  49. #ifndef NO_RESOURCES
  50. #pragma resource "*.dfm"
  51. #endif
  52. //---------------------------------------------------------------------------
  53. #define 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. class TAutoBatch
  119. {
  120. public:
  121. TAutoBatch(TCustomScpExplorerForm * Form) :
  122. FForm(Form)
  123. {
  124. FForm->BatchStart(FBatchStorage);
  125. }
  126. ~TAutoBatch()
  127. {
  128. FForm->BatchEnd(FBatchStorage);
  129. }
  130. private:
  131. TCustomScpExplorerForm * FForm;
  132. void * FBatchStorage;
  133. };
  134. //---------------------------------------------------------------------------
  135. struct TTransferOperationParam
  136. {
  137. TTransferOperationParam();
  138. UnicodeString TargetDirectory;
  139. bool Temp;
  140. bool DragDrop;
  141. int Options;
  142. TAutoSwitch Queue;
  143. };
  144. //---------------------------------------------------------------------------
  145. TTransferOperationParam::TTransferOperationParam()
  146. {
  147. Temp = false;
  148. DragDrop = false;
  149. Options = 0;
  150. Queue = asAuto;
  151. }
  152. //---------------------------------------------------------------------------
  153. class TTransferPresetNoteData : public TObject
  154. {
  155. public:
  156. UnicodeString Message;
  157. };
  158. //---------------------------------------------------------------------------
  159. class TTerminalNoteData : public TObject
  160. {
  161. public:
  162. TTerminal * Terminal;
  163. };
  164. //---------------------------------------------------------------------------
  165. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  166. FFormRestored(false),
  167. TForm(Owner)
  168. {
  169. FCurrentSide = osRemote;
  170. FEverShown = false;
  171. FDocks = new TList();
  172. RestoreParams();
  173. ConfigurationChanged();
  174. RemoteDirView->Invalidate();
  175. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  176. NonVisualDataModule->ScpExplorer = this;
  177. FAutoOperation = false;
  178. FOnFileOperationFinished = NULL;
  179. FForceExecution = false;
  180. FIgnoreNextDialogChar = 0;
  181. FErrorList = NULL;
  182. FSynchronizeProgressForm = NULL;
  183. FProgressForm = NULL;
  184. FRefreshLocalDirectory = false;
  185. FRefreshRemoteDirectory = false;
  186. FDDMoveSlipped = false;
  187. FDDExtMapFile = NULL;
  188. // CreateMutexW keeps failing with ERROR_NOACCESS
  189. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  190. DebugAssert(FDDExtMutex != NULL);
  191. FDDTargetControl = NULL;
  192. FDelayedDeletionTimer = NULL;
  193. FDelayedDeletionList = new TStringList();
  194. FDDFileList = NULL;
  195. FPendingTempSpaceWarn = false;
  196. FCapturedLog = NULL;
  197. FDragDropOperation = false;
  198. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  199. FAllowTransferPresetAutoSelect = true;
  200. FCopyParamDefault = L"";
  201. FSynchronizeController = NULL;
  202. FPendingQueueActionItem = NULL;
  203. FLockLevel = 0;
  204. FLockSuspendLevel = 0;
  205. FDisabledOnLockSuspend = false;
  206. FAlternativeDelete = false;
  207. FTrayIcon = new ::TTrayIcon(0);
  208. FTrayIcon->OnClick = TrayIconClick;
  209. FMaxQueueLength = 0;
  210. FLastContextPopupScreenPoint = TPoint(-1, -1);
  211. FTransferResumeList = NULL;
  212. FMoveToQueue = false;
  213. FStandaloneEditing = false;
  214. FOnFeedSynchronizeError = NULL;
  215. FNeedSession = false;
  216. FDoNotIdleCurrentTerminal = 0;
  217. FEditorManager = new TEditorManager();
  218. FEditorManager->OnFileChange = ExecutedFileChanged;
  219. FEditorManager->OnFileReload = ExecutedFileReload;
  220. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  221. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  222. FLocalEditors = new TList();
  223. FQueueStatus = NULL;
  224. FQueueStatusSection = new TCriticalSection();
  225. FQueueStatusInvalidated = false;
  226. FQueueItemInvalidated = false;
  227. FQueueActedItem = NULL;
  228. FQueueController = new TQueueController(QueueView3);
  229. FUserActionTimer = new TTimer(this);
  230. FUserActionTimer->Enabled = false;
  231. FUserActionTimer->Interval = 10;
  232. FUserActionTimer->OnTimer = UserActionTimer;
  233. FNotes = new TStringList();
  234. FNoteTimer = new TTimer(this);
  235. FNoteTimer->Enabled = false;
  236. FNoteTimer->OnTimer = NoteTimer;
  237. FOnNoteClick = NULL;
  238. FOle32Library = LoadLibrary(L"Ole32.dll");
  239. FDragMoveCursor = FOle32Library != NULL ?
  240. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  241. UseSystemSettings(this);
  242. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  243. static_cast<TObject*>(GetComponent(fcTransferList)));
  244. DebugAssert(TransferList != NULL);
  245. FTransferListHoverIndex = -1;
  246. TransferList->OnChange = TransferListChange;
  247. TransferList->OnDrawItem = TransferListDrawItem;
  248. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  249. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  250. UseDesktopFont(SessionsPageControl);
  251. UpdateSessionsPageControlHeight();
  252. UseDesktopFont(RemoteDirView);
  253. UseDesktopFont(RemoteDriveView);
  254. UseDesktopFont(QueueView3);
  255. UseDesktopFont(QueueLabel);
  256. UseDesktopFont(RemoteStatusBar);
  257. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  258. QueueSplitter->ShowHint = true;
  259. RemotePanelSplitter->ShowHint = true;
  260. UpdateImages();
  261. FCustomCommandMenu = CreateTBXPopupMenu(this);
  262. FCustomCommandLocalFileList = NULL;
  263. FCustomCommandRemoteFileList = NULL;
  264. FSessionColors = new TPngImageList(this);
  265. FSessionColors->ColorDepth = cd32Bit;
  266. AddFixedSessionImages();
  267. SessionsPageControl->Images = FSessionColors;
  268. QueueLabel->FocusControl = QueueView3;
  269. UpdateQueueLabel();
  270. FRemoteDirViewWasFocused = true;
  271. CreateHiddenWindow();
  272. StartUpdates();
  273. }
  274. //---------------------------------------------------------------------------
  275. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  276. {
  277. // this has to be one of the very first things to do
  278. StopUpdateThread();
  279. SessionsPageControl->Images = NULL;
  280. SAFE_DESTROY(FSessionColors);
  281. SAFE_DESTROY(FSessionsDragDropFilesEx);
  282. delete FCustomCommandLocalFileList;
  283. delete FCustomCommandRemoteFileList;
  284. delete FCustomCommandMenu;
  285. delete FTrayIcon;
  286. FTrayIcon = NULL;
  287. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  288. delete FEditorManager;
  289. ForceCloseLocalEditors();
  290. delete FLocalEditors;
  291. HideFileFindDialog();
  292. if (FDelayedDeletionTimer)
  293. {
  294. DoDelayedDeletion(NULL);
  295. SAFE_DESTROY(FDelayedDeletionTimer);
  296. }
  297. SAFE_DESTROY(FDelayedDeletionList);
  298. // sometimes we do not get DDEnd so the list is not released
  299. SAFE_DESTROY(FDDFileList);
  300. DebugAssert(FSynchronizeController == NULL);
  301. CloseHandle(FDDExtMutex);
  302. FDDExtMutex = NULL;
  303. FreeLibrary(FOle32Library);
  304. FOle32Library = NULL;
  305. FDragMoveCursor = NULL;
  306. DebugAssert(!FErrorList);
  307. if (FEverShown)
  308. {
  309. // when window is never shown (like when running command-line operation),
  310. // particularly window site is not restored correctly (BoundsRect value set
  311. // in RestoreForm gets lost during handle allocation), so we do not want
  312. // it to be stored
  313. StoreParams();
  314. }
  315. Terminal = NULL;
  316. Queue = NULL;
  317. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  318. NonVisualDataModule->ScpExplorer = NULL;
  319. delete FQueueController;
  320. FQueueController = NULL;
  321. delete FQueueStatusSection;
  322. FQueueStatusSection = NULL;
  323. delete FQueueStatus;
  324. FQueueStatus = NULL;
  325. delete FUserActionTimer;
  326. FUserActionTimer = NULL;
  327. delete FNoteTimer;
  328. delete FNotes;
  329. delete FNoteData;
  330. SAFE_DESTROY(FDocks);
  331. SAFE_DESTROY(FHistoryMenu[0][0]);
  332. SAFE_DESTROY(FHistoryMenu[0][1]);
  333. SAFE_DESTROY(FHistoryMenu[1][0]);
  334. SAFE_DESTROY(FHistoryMenu[1][1]);
  335. if (FHiddenWindow != NULL)
  336. {
  337. DestroyWindow(FHiddenWindow);
  338. FHiddenWindow = NULL;
  339. }
  340. }
  341. //---------------------------------------------------------------------------
  342. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  343. {
  344. std::unique_ptr<TSessionData> Data;
  345. if (!Session.IsEmpty())
  346. {
  347. bool DefaultsOnly;
  348. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  349. }
  350. TTerminalManager * Manager = TTerminalManager::Instance();
  351. for (int Index = 0; Index < Manager->Count; Index++)
  352. {
  353. TTerminal * Terminal = Manager->Terminals[Index];
  354. if (Session.IsEmpty() ||
  355. Terminal->SessionData->IsSameSite(Data.get()))
  356. {
  357. if (Path.IsEmpty())
  358. {
  359. Terminal->ClearCaches();
  360. }
  361. else
  362. {
  363. Terminal->DirectoryModified(Path, true);
  364. }
  365. }
  366. }
  367. // We should flag a pending refresh for the background terminals or busy foreground terminals
  368. if ((Terminal != NULL) && Terminal->Active &&
  369. CanCommandLineFromAnotherInstance() &&
  370. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  371. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  372. {
  373. Terminal->ReloadDirectory();
  374. }
  375. }
  376. //---------------------------------------------------------------------------
  377. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  378. {
  379. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  380. size_t MessageSize = sizeof(TCopyDataMessage);
  381. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  382. if (Result)
  383. {
  384. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  385. Result = (Message.Version == TCopyDataMessage::Version1);
  386. if (Result)
  387. {
  388. switch (Message.Command)
  389. {
  390. case TCopyDataMessage::CommandCanCommandLine:
  391. Result = CanCommandLineFromAnotherInstance();
  392. break;
  393. case TCopyDataMessage::CommandCommandLine:
  394. {
  395. UnicodeString CommandLine(Message.CommandLine);
  396. Result = CommandLineFromAnotherInstance(CommandLine);
  397. }
  398. break;
  399. case TCopyDataMessage::RefreshPanel:
  400. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  401. break;
  402. case TCopyDataMessage::MainWindowCheck:
  403. Result = true;
  404. break;
  405. default:
  406. Result = false;
  407. break;
  408. }
  409. }
  410. }
  411. Message.Result = Result ? 1 : 0;
  412. }
  413. //---------------------------------------------------------------------------
  414. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  415. {
  416. WNDCLASS WindowClass = {0};
  417. WindowClass.lpfnWndProc = DefWindowProc;
  418. WindowClass.hInstance = HInstance;
  419. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  420. FHiddenWindow = NULL;
  421. if (RegisterClass(&WindowClass))
  422. {
  423. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  424. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  425. }
  426. }
  427. //---------------------------------------------------------------------------
  428. bool __fastcall TCustomScpExplorerForm::CanConsole()
  429. {
  430. return (Terminal != NULL) && (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  431. }
  432. //---------------------------------------------------------------------------
  433. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  434. {
  435. bool Result = Visible && !NonVisualDataModule->Busy;
  436. return Result;
  437. }
  438. //---------------------------------------------------------------------------
  439. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  440. const UnicodeString & CommandLine)
  441. {
  442. TProgramParams Params(CommandLine);
  443. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  444. if (Result)
  445. {
  446. NonVisualDataModule->StartBusy();
  447. try
  448. {
  449. // this action is initiated from another process,
  450. // so it's likely that our window is not visible,
  451. // and user won't see what is going on
  452. Application->BringToFront();
  453. // reload sessions as we may be asked to open a session
  454. // just stored by another instance
  455. StoredSessions->Reload();
  456. UnicodeString SessionName = Params.Param[1];
  457. std::unique_ptr<TObjectList> DataList(new TObjectList());
  458. UnicodeString DownloadFile; // unused
  459. try
  460. {
  461. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  462. }
  463. catch (EAbort &)
  464. {
  465. // Happens when opening session in PuTTY and in other situations,
  466. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  467. // to process the command-line on its own (or to pass it to yet another instance)
  468. }
  469. if (DataList->Count > 0)
  470. {
  471. TTerminalManager * Manager = TTerminalManager::Instance();
  472. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  473. }
  474. }
  475. __finally
  476. {
  477. NonVisualDataModule->EndBusy();
  478. }
  479. }
  480. return Result;
  481. }
  482. //---------------------------------------------------------------------------
  483. void __fastcall TCustomScpExplorerForm::SetTerminal(TTerminal * value)
  484. {
  485. if (FTerminal != value)
  486. {
  487. TerminalChanging();
  488. FTerminal = value;
  489. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  490. FAllowTransferPresetAutoSelect = false;
  491. try
  492. {
  493. TerminalChanged();
  494. }
  495. __finally
  496. {
  497. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  498. }
  499. if (Terminal != NULL)
  500. {
  501. TransferPresetAutoSelect();
  502. }
  503. }
  504. }
  505. //---------------------------------------------------------------------------
  506. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  507. {
  508. if (FTerminal != NULL)
  509. {
  510. UpdateTerminal(Terminal);
  511. }
  512. }
  513. //---------------------------------------------------------------------------
  514. void __fastcall TCustomScpExplorerForm::TerminalChanged()
  515. {
  516. RemoteDirView->Terminal = Terminal;
  517. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  518. TManagedTerminal * ManagedTerminal = NULL;
  519. if (Terminal != NULL)
  520. {
  521. if (Terminal->Active)
  522. {
  523. Terminal->RefreshDirectory();
  524. }
  525. ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  526. DebugAssert(ManagedTerminal != NULL);
  527. if (WinConfiguration->PreservePanelState)
  528. {
  529. if (ManagedTerminal->RemoteExplorerState != NULL)
  530. {
  531. RemoteDirView->RestoreState(ManagedTerminal->RemoteExplorerState);
  532. }
  533. else
  534. {
  535. RemoteDirView->ClearState();
  536. }
  537. }
  538. InitStatusBar();
  539. }
  540. DoTerminalListChanged();
  541. if (ManagedTerminal != NULL)
  542. {
  543. // this has to be set only after the tab is switched from DoTerminalListChanged,
  544. // otherwise we are changing color of wrong tab
  545. SessionColor = (TColor)ManagedTerminal->StateData->Color;
  546. }
  547. UpdateTransferList();
  548. // Update panels Enable state before refreshing the labels
  549. UpdateControls();
  550. }
  551. //---------------------------------------------------------------------------
  552. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  553. {
  554. if (Queue != value)
  555. {
  556. if (FQueue != NULL)
  557. {
  558. FQueue->OnListUpdate = NULL;
  559. FQueue->OnQueueItemUpdate = NULL;
  560. }
  561. FQueue = value;
  562. if (FQueue != NULL)
  563. {
  564. DebugAssert(FQueue->OnListUpdate == NULL);
  565. FQueue->OnListUpdate = QueueListUpdate;
  566. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  567. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  568. }
  569. QueueChanged();
  570. }
  571. }
  572. //---------------------------------------------------------------------------
  573. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  574. TListItem * Item)
  575. {
  576. if (FQueueActedItem == Item)
  577. {
  578. FQueueActedItem = NULL;
  579. if ((QueueView3->PopupMenu != NULL) &&
  580. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  581. {
  582. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  583. ReleaseCapture();
  584. }
  585. }
  586. if (Item->Data == FPendingQueueActionItem)
  587. {
  588. FPendingQueueActionItem = NULL;
  589. }
  590. }
  591. //---------------------------------------------------------------------------
  592. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  593. {
  594. {
  595. TGuard Guard(FQueueStatusSection);
  596. FQueueStatusInvalidated = false;
  597. if (FQueue != NULL)
  598. {
  599. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  600. }
  601. }
  602. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  603. {
  604. FMaxQueueLength = FQueueStatus->Count;
  605. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  606. }
  607. FQueueController->UpdateQueueStatus(FQueueStatus);
  608. SetQueueProgress();
  609. UpdateQueueView();
  610. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  611. if (IsEmpty && (Terminal != NULL))
  612. {
  613. TOnceDoneOperation OnceDoneOperation =
  614. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  615. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  616. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  617. {
  618. FQueue->Enabled = false;
  619. }
  620. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  621. {
  622. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  623. }
  624. }
  625. }
  626. //---------------------------------------------------------------------------
  627. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  628. {
  629. UnicodeString Result;
  630. if (FQueueStatus != NULL)
  631. {
  632. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  633. {
  634. TFileOperationProgressType * ProgressData =
  635. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  636. if ((ProgressData != NULL) && ProgressData->InProgress)
  637. {
  638. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  639. }
  640. }
  641. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  642. {
  643. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  644. }
  645. }
  646. return Result;
  647. }
  648. //---------------------------------------------------------------------------
  649. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  650. {
  651. ComponentVisible[fcQueueView] =
  652. (WinConfiguration->QueueView.Show == qvShow) ||
  653. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  654. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  655. }
  656. //---------------------------------------------------------------------------
  657. void __fastcall TCustomScpExplorerForm::QueueChanged()
  658. {
  659. if (FQueueStatus != NULL)
  660. {
  661. delete FQueueStatus;
  662. FQueueStatus = NULL;
  663. }
  664. UpdateQueueStatus(true);
  665. }
  666. //---------------------------------------------------------------------------
  667. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  668. {
  669. if (FQueue == Queue)
  670. {
  671. FQueueStatusInvalidated = true;
  672. }
  673. }
  674. //---------------------------------------------------------------------------
  675. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  676. TQueueItem * Item)
  677. {
  678. if (FQueue == Queue)
  679. {
  680. TGuard Guard(FQueueStatusSection);
  681. // this may be running in parallel with QueueChanged
  682. if (FQueueStatus != NULL)
  683. {
  684. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  685. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  686. {
  687. FRefreshLocalDirectory = (QueueItem == NULL) ||
  688. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  689. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  690. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  691. }
  692. if (QueueItem != NULL)
  693. {
  694. QueueItem->UserData = (void*)true;
  695. FQueueItemInvalidated = true;
  696. }
  697. }
  698. }
  699. }
  700. //---------------------------------------------------------------------------
  701. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  702. {
  703. // during standalone editing, we have no way to see/control queue,
  704. // so we have to always popup prompts automatically
  705. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  706. }
  707. //---------------------------------------------------------------------------
  708. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  709. {
  710. if (FQueueStatus != NULL)
  711. {
  712. bool QueueAutoPopup = IsQueueAutoPopup();
  713. bool NeedRefresh = FQueueController->NeedRefresh();
  714. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  715. FQueueItemInvalidated = false;
  716. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  717. bool Updated = false;
  718. TQueueItemProxy * QueueItem;
  719. bool WasUserAction;
  720. for (int Index = 0; Index < Limit; Index++)
  721. {
  722. bool Update = false;
  723. QueueItem = FQueueStatus->Items[Index];
  724. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  725. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  726. (FPendingQueueActionItem == NULL))
  727. {
  728. FPendingQueueActionItem = QueueItem;
  729. FUserActionTimer->Enabled = true;
  730. }
  731. if ((bool)QueueItem->UserData)
  732. {
  733. QueueItem->UserData = (void*)false;
  734. QueueItem->Update();
  735. Updated = true;
  736. Update = true;
  737. }
  738. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  739. {
  740. Update = true;
  741. }
  742. if (Update)
  743. {
  744. FQueueController->RefreshQueueItem(QueueItem);
  745. }
  746. }
  747. if (Updated)
  748. {
  749. NonVisualDataModule->UpdateNonVisibleActions();
  750. SetQueueProgress();
  751. }
  752. }
  753. }
  754. //---------------------------------------------------------------------------
  755. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  756. {
  757. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  758. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  759. }
  760. //---------------------------------------------------------------------------
  761. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  762. {
  763. if (ProgressData->Operation != foCalculateSize)
  764. {
  765. int OverallProgress;
  766. // FProgressForm is null when this is called from SetQueueProgress
  767. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  768. {
  769. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  770. }
  771. else
  772. {
  773. OverallProgress = ProgressData->OverallProgress();
  774. }
  775. // implies TBPF_NORMAL
  776. FTaskbarList->SetProgressValue(GetMainForm()->Handle, OverallProgress, 100);
  777. }
  778. else
  779. {
  780. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  781. }
  782. }
  783. //---------------------------------------------------------------------------
  784. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  785. {
  786. TTerminalManager::Instance()->QueueStatusUpdated();
  787. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  788. {
  789. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  790. {
  791. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  792. {
  793. TFileOperationProgressType * ProgressData = NULL;
  794. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  795. {
  796. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  797. }
  798. if ((ProgressData != NULL) &&
  799. ProgressData->InProgress)
  800. {
  801. SetTaskbarListProgressValue(ProgressData);
  802. }
  803. else
  804. {
  805. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  806. }
  807. }
  808. else
  809. {
  810. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  811. }
  812. }
  813. else
  814. {
  815. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  816. }
  817. }
  818. UpdateQueueLabel();
  819. }
  820. //---------------------------------------------------------------------------
  821. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  822. {
  823. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  824. if (FQueueStatus != NULL)
  825. {
  826. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  827. if (ActiveAndPendingPrimaryCount > 0)
  828. {
  829. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  830. }
  831. }
  832. QueueLabel->Caption = Caption;
  833. }
  834. //---------------------------------------------------------------------------
  835. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  836. {
  837. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  838. static_cast<TComponent*>(GetComponent(fcTransferList)));
  839. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  840. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  841. TransferList->Strings->BeginUpdate();
  842. try
  843. {
  844. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  845. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  846. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  847. if (FTransferDropDownHint.IsEmpty())
  848. {
  849. FTransferDropDownHint = TransferDropDown->Hint;
  850. }
  851. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  852. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  853. TransferDropDown->Text = StripHotkey(Name);
  854. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  855. (FTransferDropDownHint, StripHotkey(Name),
  856. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  857. FLAGMASK(Terminal != NULL, Terminal->UsableCopyParamAttrs(0).General))));
  858. // update the label, otherwise when it is updated only on the first draw
  859. // of the list, it is drawn "bold" for some reason
  860. FTransferListHoverIndex = TransferList->ItemIndex;
  861. UpdateTransferLabel();
  862. }
  863. __finally
  864. {
  865. TransferList->Strings->EndUpdate();
  866. }
  867. }
  868. //---------------------------------------------------------------------------
  869. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  870. {
  871. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  872. DebugAssert(Toolbar != NULL);
  873. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  874. }
  875. //---------------------------------------------------------------------------
  876. void __fastcall TCustomScpExplorerForm::UpdateActions()
  877. {
  878. TForm::UpdateActions();
  879. if (ComponentVisible[fcCustomCommandsBand])
  880. {
  881. UpdateCustomCommandsToolbar();
  882. }
  883. }
  884. //---------------------------------------------------------------------------
  885. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  886. {
  887. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  888. }
  889. //---------------------------------------------------------------------------
  890. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  891. {
  892. DebugAssert(Configuration && RemoteDirView);
  893. RemoteDirView->DDAllowMove = WinConfiguration->DDAllowMoveInit;
  894. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  895. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  896. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  897. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  898. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  899. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  900. {
  901. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  902. // selection is not redrawn automatically when RowSelect changes
  903. RemoteDirView->Invalidate();
  904. }
  905. RemoteDriveView->DDAllowMove = WinConfiguration->DDAllowMoveInit;
  906. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  907. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  908. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  909. UpdateSessionsPageControlHeight();
  910. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  911. UpdateToolbarDisplayMode();
  912. UpdateTransferList();
  913. if (Terminal != NULL)
  914. {
  915. TransferPresetAutoSelect();
  916. }
  917. UpdateQueueView();
  918. UpdateControls();
  919. // this can be called even before constuctor finishes.
  920. if (FEditorManager != NULL)
  921. {
  922. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  923. }
  924. // this can be called even before constuctor finishes.
  925. if (FLocalEditors != NULL)
  926. {
  927. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  928. {
  929. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  930. }
  931. }
  932. if (ComponentVisible[fcCustomCommandsBand])
  933. {
  934. UpdateCustomCommandsToolbar();
  935. }
  936. // show only when keeping queue items forever,
  937. // otherwise, its enough to have in in the context menu
  938. QueueDeleteAllDoneQueueToolbarItem->Visible =
  939. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  940. }
  941. //---------------------------------------------------------------------------
  942. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  943. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  944. void * /*Arg*/)
  945. {
  946. if (Token != NULL)
  947. {
  948. TForm * Editor = dynamic_cast<TForm*>(Token);
  949. DebugAssert(Editor != NULL);
  950. ReconfigureEditorForm(Editor);
  951. }
  952. }
  953. //---------------------------------------------------------------------------
  954. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  955. {
  956. WinConfiguration->DDTransferConfirmation = asOn;
  957. }
  958. //---------------------------------------------------------------------------
  959. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  960. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  961. {
  962. // noop
  963. }
  964. //---------------------------------------------------------------------------
  965. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  966. TTransferDirection Direction, TTransferType Type, bool Temp,
  967. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  968. bool Confirm, bool DragDrop, int Options)
  969. {
  970. bool Result = true;
  971. DebugAssert(Terminal && Terminal->Active);
  972. // Temp means d&d here so far, may change in future!
  973. if (Temp && (Direction == tdToLocal) && (Type == ttMove) &&
  974. !WinConfiguration->DDAllowMove)
  975. {
  976. TMessageParams Params(mpNeverAskAgainCheck);
  977. unsigned int Answer = MessageDialog(LoadStr(DND_DOWNLOAD_MOVE_WARNING), qtWarning,
  978. qaOK | qaCancel, HELP_DND_DOWNLOAD_MOVE_WARNING, &Params);
  979. if (Answer == qaNeverAskAgain)
  980. {
  981. WinConfiguration->DDAllowMove = true;
  982. }
  983. else if (Answer == qaCancel)
  984. {
  985. Result = false;
  986. }
  987. }
  988. // these parameters are known in advance
  989. int Params =
  990. FLAGMASK(Type == ttMove, cpDelete);
  991. bool ToTemp = (Temp && (Direction == tdToLocal));
  992. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  993. {
  994. bool DisableNewerOnly =
  995. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  996. ToTemp;
  997. Options |=
  998. FLAGMASK(ToTemp, coTemp) |
  999. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  1000. coDoNotShowAgain;
  1001. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1002. int CopyParamAttrs = (Direction == tdToRemote ?
  1003. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1004. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1005. int OutputOptions =
  1006. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1007. cooDoNotShowAgain);
  1008. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1009. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1010. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1011. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions);
  1012. if (Result)
  1013. {
  1014. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1015. {
  1016. if (DragDrop)
  1017. {
  1018. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1019. {
  1020. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1021. NULL, 0, EnableDDTransferConfirmation, NULL);
  1022. }
  1023. WinConfiguration->DDTransferConfirmation = asOff;
  1024. }
  1025. else
  1026. {
  1027. WinConfiguration->ConfirmTransferring = false;
  1028. }
  1029. }
  1030. else
  1031. {
  1032. // User exclicitly unchecked "do not show again",
  1033. // so show him the dialog the next time
  1034. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1035. {
  1036. WinConfiguration->DDTransferConfirmation = asOn;
  1037. }
  1038. }
  1039. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1040. }
  1041. }
  1042. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1043. {
  1044. Configuration->Usage->Inc(L"TransfersOnBackground");
  1045. // these parameter are known only after transfer dialog
  1046. Params |=
  1047. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1048. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1049. Result = false;
  1050. ClearTransferSourceSelection(Direction);
  1051. }
  1052. return Result;
  1053. }
  1054. //---------------------------------------------------------------------------
  1055. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1056. {
  1057. if (FOnFileOperationFinished != NULL)
  1058. {
  1059. FOnFileOperationFinished(UnicodeString(), true);
  1060. }
  1061. else
  1062. {
  1063. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1064. if (HasDirView[Side])
  1065. {
  1066. DirView(Side)->SelectAll(smNone);
  1067. }
  1068. }
  1069. }
  1070. //---------------------------------------------------------------------------
  1071. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1072. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1073. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1074. int Params)
  1075. {
  1076. DebugAssert(Queue != NULL);
  1077. bool SingleFile = false;
  1078. if (FileList->Count == 1)
  1079. {
  1080. if (Direction == tdToRemote)
  1081. {
  1082. UnicodeString FileName = FileList->Strings[0];
  1083. SingleFile = FileExists(ApiPath(FileName));
  1084. }
  1085. else
  1086. {
  1087. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1088. SingleFile = !File->IsDirectory;
  1089. }
  1090. }
  1091. TQueueItem * QueueItem;
  1092. if (Direction == tdToRemote)
  1093. {
  1094. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1095. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1096. }
  1097. else
  1098. {
  1099. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1100. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1101. }
  1102. AddQueueItem(Queue, QueueItem, Terminal);
  1103. }
  1104. //---------------------------------------------------------------------------
  1105. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TTerminal * Terminal)
  1106. {
  1107. if (Queue->IsEmpty)
  1108. {
  1109. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  1110. ManagedTerminal->QueueOperationStart = Now();
  1111. }
  1112. Queue->AddItem(QueueItem);
  1113. }
  1114. //---------------------------------------------------------------------------
  1115. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1116. {
  1117. }
  1118. //---------------------------------------------------------------------------
  1119. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1120. {
  1121. DebugAssert(FDocks != NULL);
  1122. for (int Index = 0; Index < ComponentCount; Index++)
  1123. {
  1124. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1125. if ((Dock != NULL) && (Dock->Tag == 0))
  1126. {
  1127. FDocks->Add(Dock);
  1128. }
  1129. }
  1130. CollectItemsWithTextDisplayMode(this);
  1131. ConfigurationChanged();
  1132. QueuePanel->Height =
  1133. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1134. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1135. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1136. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1137. }
  1138. //---------------------------------------------------------------------------
  1139. void __fastcall TCustomScpExplorerForm::StoreParams()
  1140. {
  1141. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1142. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1143. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1144. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1145. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1146. }
  1147. //---------------------------------------------------------------------------
  1148. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1149. {
  1150. if (!FFormRestored)
  1151. {
  1152. FFormRestored = true;
  1153. RestoreFormParams();
  1154. }
  1155. TForm::CreateParams(Params);
  1156. }
  1157. //---------------------------------------------------------------------------
  1158. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1159. {
  1160. DebugAssert(FDocks != NULL);
  1161. for (int Index = 0; Index < FDocks->Count; Index++)
  1162. {
  1163. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1164. }
  1165. }
  1166. //---------------------------------------------------------------------------
  1167. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1168. {
  1169. SetDockAllowDrag(true);
  1170. while (!ButtonsStr.IsEmpty())
  1171. {
  1172. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1173. UnicodeString Name = CutToChar(S, L'=', true);
  1174. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1175. {
  1176. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1177. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1178. {
  1179. while (!S.IsEmpty())
  1180. {
  1181. UnicodeString S2 = CutToChar(S, L',', true);
  1182. UnicodeString Name = CutToChar(S2, L':', true);
  1183. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1184. {
  1185. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1186. if (SameText(GetToolbarItemName(Item), Name))
  1187. {
  1188. // forward compatibility
  1189. UnicodeString S3 = CutToChar(S2, L':', true);
  1190. bool Visible = StrToIntDef(S3, 0);
  1191. Item->Visible = Visible;
  1192. }
  1193. }
  1194. }
  1195. }
  1196. }
  1197. }
  1198. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1199. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1200. }
  1201. //---------------------------------------------------------------------------
  1202. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1203. {
  1204. UnicodeString Result;
  1205. SetDockAllowDrag(true);
  1206. Result = ::GetToolbarsLayoutStr(this);
  1207. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1208. return Result;
  1209. }
  1210. //---------------------------------------------------------------------------
  1211. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1212. {
  1213. UnicodeString Result;
  1214. if (Item->Action != NULL)
  1215. {
  1216. Result = Item->Action->Name;
  1217. Result = RemoveSuffix(Result, L"Action");
  1218. }
  1219. else
  1220. {
  1221. Result = Item->Name;
  1222. Result = RemoveSuffix(Result, L"SubmenuItem");
  1223. Result = RemoveSuffix(Result, L"Item");
  1224. }
  1225. return Result;
  1226. }
  1227. //---------------------------------------------------------------------------
  1228. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1229. {
  1230. UnicodeString Result;
  1231. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1232. {
  1233. UnicodeString ToolbarStr;
  1234. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1235. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1236. {
  1237. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1238. {
  1239. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1240. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1241. // Once we introduce any buttons that aer hidden by default, we would have to remember their initial state
  1242. // and save the changes here only.
  1243. if (!Item->Visible)
  1244. {
  1245. UnicodeString Name = GetToolbarItemName(Item);
  1246. DebugAssert(Name.Pos(L"TBX") == 0);
  1247. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1248. }
  1249. }
  1250. }
  1251. if (!ToolbarStr.IsEmpty())
  1252. {
  1253. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1254. }
  1255. }
  1256. return Result;
  1257. }
  1258. //---------------------------------------------------------------------------
  1259. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1260. {
  1261. DebugAssert(FProgressForm == NULL);
  1262. FProgressForm =
  1263. new TProgressForm(Application, (FTransferResumeList != NULL), Terminal->IsCapable[fsSkipTransfer], SynchronizeProgress);
  1264. FProgressForm->DeleteLocalToRecycleBin =
  1265. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1266. FProgressForm->DeleteRemoteToRecycleBin =
  1267. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1268. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1269. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1270. LockWindow();
  1271. if (FTaskbarList != NULL)
  1272. {
  1273. // Actually, do not know what hides the progress once the operation finishes
  1274. SetTaskbarListProgressState(TBPF_NORMAL);
  1275. }
  1276. }
  1277. //---------------------------------------------------------------------------
  1278. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1279. {
  1280. UnlockWindow();
  1281. SAFE_DESTROY(FProgressForm);
  1282. SetQueueProgress();
  1283. }
  1284. //---------------------------------------------------------------------------
  1285. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1286. TFileOperationProgressType & ProgressData)
  1287. {
  1288. // Download to temporary local directory
  1289. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1290. {
  1291. bool Continue = true;
  1292. FPendingTempSpaceWarn = false;
  1293. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1294. if (!Continue)
  1295. {
  1296. Abort();
  1297. }
  1298. }
  1299. // operation is being executed and we still didn't show up progress form
  1300. if (ProgressData.InProgress && !FProgressForm)
  1301. {
  1302. CreateProgressForm(NULL);
  1303. // When main window is hidden, synchronisation form does not exist (we do not get here during synchronization - only comparing),
  1304. // we suppose "/upload" or URL download mode
  1305. if (!Visible && (ProgressData.Operation != foCalculateSize) &&
  1306. (ProgressData.Operation != foCalculateChecksum) &&
  1307. (FSynchronizeProgressForm == NULL) &&
  1308. !FStandaloneEditing)
  1309. {
  1310. FProgressForm->OnceDoneOperation = odoDisconnect;
  1311. }
  1312. }
  1313. // operation is finished (or terminated), so we hide progress form
  1314. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1315. {
  1316. DestroyProgressForm();
  1317. if (ProgressData.Operation == foCalculateSize)
  1318. {
  1319. // When calculating size before transfer, the abort caused by
  1320. // cancel flag set due to "MoveToQueue" below,
  1321. // is not propagated back to us in ExecuteFileOperation,
  1322. // as it is silently swallowed.
  1323. // So we explicitly (re)throw it here.
  1324. if (FMoveToQueue)
  1325. {
  1326. Abort();
  1327. }
  1328. }
  1329. else
  1330. {
  1331. if ((ProgressData.Cancel == csContinue) &&
  1332. !FAutoOperation)
  1333. {
  1334. OperationComplete(ProgressData.StartTime);
  1335. }
  1336. }
  1337. }
  1338. if (FProgressForm &&
  1339. // Can happen only during synchronization, when ending one of the operations
  1340. ProgressData.InProgress)
  1341. {
  1342. FProgressForm->SetProgressData(ProgressData);
  1343. if (FTaskbarList != NULL)
  1344. {
  1345. DebugAssert(ProgressData.InProgress);
  1346. SetTaskbarListProgressValue(&ProgressData);
  1347. }
  1348. if (FProgressForm->Cancel > csContinue)
  1349. {
  1350. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1351. if (FProgressForm->Cancel == csCancelFile)
  1352. {
  1353. FProgressForm->ClearCancel();
  1354. }
  1355. // cancel cancels even the move
  1356. FMoveToQueue = false;
  1357. }
  1358. else if (FProgressForm->MoveToQueue)
  1359. {
  1360. FMoveToQueue = true;
  1361. ProgressData.SetCancelAtLeast(csCancel);
  1362. }
  1363. if ((FTransferResumeList != NULL) &&
  1364. ProgressData.InProgress &&
  1365. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1366. !ProgressData.FullFileName.IsEmpty())
  1367. {
  1368. if ((FTransferResumeList->Count == 0) ||
  1369. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1370. {
  1371. // note that we do not recognize directories from files here
  1372. FTransferResumeList->Add(ProgressData.FullFileName);
  1373. }
  1374. }
  1375. }
  1376. }
  1377. //---------------------------------------------------------------------------
  1378. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1379. const TDateTime & StartTime)
  1380. {
  1381. if (GUIConfiguration->BeepOnFinish &&
  1382. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1383. {
  1384. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1385. DWORD Sound;
  1386. if (!ExtractFileExt(BeepSound).IsEmpty())
  1387. {
  1388. Sound = SND_FILENAME;
  1389. }
  1390. else
  1391. {
  1392. Sound = SND_ALIAS;
  1393. }
  1394. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1395. }
  1396. }
  1397. //---------------------------------------------------------------------------
  1398. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1399. TFileOperationProgressType & ProgressData)
  1400. {
  1401. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1402. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1403. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1404. FileOperationProgress(ProgressData);
  1405. }
  1406. //---------------------------------------------------------------------------
  1407. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1408. {
  1409. UnicodeString Result;
  1410. if (FProgressForm != NULL)
  1411. {
  1412. Result = FProgressForm->ProgressStr();
  1413. }
  1414. return Result;
  1415. }
  1416. //---------------------------------------------------------------------------
  1417. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1418. bool DragDrop)
  1419. {
  1420. return (!DragDrop && (DropSourceControl == NULL)) ||
  1421. (DropSourceControl == RemoteDirView);
  1422. }
  1423. //---------------------------------------------------------------------------
  1424. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1425. TFileOperation Operation, TOperationSide Side,
  1426. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1427. TOnceDoneOperation & OnceDoneOperation)
  1428. {
  1429. if (!FAutoOperation)
  1430. {
  1431. // no selection on "/upload", form servers only as event handler
  1432. // (it is not displayed)
  1433. if (PanelOperation(Side, FDragDropOperation) &&
  1434. Visible && (Operation != foCalculateSize) &&
  1435. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1436. {
  1437. if (FOnFileOperationFinished != NULL)
  1438. {
  1439. FOnFileOperationFinished(FileName, Success);
  1440. }
  1441. else
  1442. {
  1443. TCustomDirView * DView = DirView(Side);
  1444. UnicodeString FileNameOnly = ExtractFileName(FileName, (Side == osRemote));
  1445. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1446. // this can happen when local drive is unplugged in the middle of the operation
  1447. if (Item != NULL)
  1448. {
  1449. if (Success) Item->Selected = false;
  1450. DView->MakeProgressVisible(Item);
  1451. }
  1452. }
  1453. }
  1454. if ((Operation == foCopy) || (Operation == foMove))
  1455. {
  1456. if (Side == osLocal)
  1457. {
  1458. Configuration->Usage->Inc(L"UploadedFiles");
  1459. }
  1460. else
  1461. {
  1462. Configuration->Usage->Inc(L"DownloadedFiles");
  1463. }
  1464. }
  1465. }
  1466. if (Success && (FSynchronizeController != NULL))
  1467. {
  1468. if (Operation == foCopy)
  1469. {
  1470. DebugAssert(Side == osLocal);
  1471. FSynchronizeController->LogOperation(soUpload, FileName);
  1472. }
  1473. else if (Operation == foDelete)
  1474. {
  1475. DebugAssert(Side == osRemote);
  1476. FSynchronizeController->LogOperation(soDelete, FileName);
  1477. }
  1478. }
  1479. if (FProgressForm)
  1480. {
  1481. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1482. }
  1483. }
  1484. //---------------------------------------------------------------------------
  1485. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1486. TFileOperation Operation, TOperationSide Side,
  1487. bool Temp, const UnicodeString & FileName, Boolean Success,
  1488. TOnceDoneOperation & OnceDoneOperation)
  1489. {
  1490. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1491. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1492. OnceDoneOperation);
  1493. }
  1494. //---------------------------------------------------------------------------
  1495. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1496. {
  1497. DebugAssert(GetSide(Side) == osRemote);
  1498. DebugUsedParam(Side);
  1499. return RemoteDirView;
  1500. }
  1501. //---------------------------------------------------------------------------
  1502. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1503. {
  1504. DebugAssert(GetSide(Side) == osRemote);
  1505. DebugUsedParam(Side);
  1506. return (Terminal != NULL);
  1507. }
  1508. //---------------------------------------------------------------------------
  1509. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1510. TOperationSide Side, int FilesOnly)
  1511. {
  1512. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1513. }
  1514. //---------------------------------------------------------------------------
  1515. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1516. TOperationSide Side, int FilesOnly)
  1517. {
  1518. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1519. }
  1520. //---------------------------------------------------------------------------
  1521. struct THistoryItemData
  1522. {
  1523. short int Side;
  1524. short int Index;
  1525. };
  1526. //---------------------------------------------------------------------------
  1527. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1528. {
  1529. DirView(Side)->HistoryGo(Index);
  1530. }
  1531. //---------------------------------------------------------------------------
  1532. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1533. {
  1534. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1535. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1536. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1537. }
  1538. //---------------------------------------------------------------------------
  1539. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1540. bool Back)
  1541. {
  1542. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1543. {
  1544. TCustomDirView * DView = DirView(Side);
  1545. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1546. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1547. if (ICount > 10)
  1548. {
  1549. ICount = 10;
  1550. }
  1551. Menu->Items->Clear();
  1552. THistoryItemData Data;
  1553. Data.Side = Side;
  1554. for (short int i = 1; i <= ICount; i++)
  1555. {
  1556. TTBCustomItem * Item = new TTBXItem(Menu);
  1557. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1558. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, (Side == osRemote));
  1559. Item->Hint = DView->HistoryPath[Data.Index];
  1560. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1561. Item->Tag = *reinterpret_cast<int*>(&Data);
  1562. Item->OnClick = HistoryItemClick;
  1563. Menu->Items->Add(Item);
  1564. }
  1565. }
  1566. }
  1567. //---------------------------------------------------------------------------
  1568. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1569. TOperationSide Side, bool Back)
  1570. {
  1571. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1572. {
  1573. // workaround
  1574. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1575. // operator new allocates memory in Pascal code, but calls inline
  1576. // contructor in C++, leading in problems, the function does
  1577. // both in Pascal code
  1578. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1579. UpdateHistoryMenu(Side, Back);
  1580. }
  1581. return FHistoryMenu[Side == osLocal][Back];
  1582. }
  1583. //---------------------------------------------------------------------------
  1584. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1585. TCustomDirView *Sender)
  1586. {
  1587. TOperationSide Side = (Sender == DirView(osRemote) ? osRemote : osLocal);
  1588. UpdateHistoryMenu(Side, true);
  1589. UpdateHistoryMenu(Side, false);
  1590. }
  1591. //---------------------------------------------------------------------------
  1592. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1593. {
  1594. // remote custom commands can be executed only if the server supports shell commands
  1595. // or have secondary shell
  1596. return (FTerminal != NULL) && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1597. }
  1598. //---------------------------------------------------------------------------
  1599. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1600. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1601. {
  1602. int Result;
  1603. TFileCustomCommand RemoteCustomCommand;
  1604. TLocalCustomCommand LocalCustomCommand;
  1605. TFileCustomCommand * NonInteractiveCustomCommand =
  1606. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1607. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1608. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1609. if (FLAGCLEAR(Command.Params, ccLocal))
  1610. {
  1611. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1612. // custom command that does not operate with files can be executed anytime ...
  1613. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1614. {
  1615. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1616. {
  1617. // ... but do not show such command in remote file menu (TODO)
  1618. Result = AllowedState;
  1619. }
  1620. else
  1621. {
  1622. Result = -1;
  1623. }
  1624. }
  1625. else
  1626. {
  1627. if ((ListType == ccltAll) || (ListType == ccltFile))
  1628. {
  1629. Result = ((FCurrentSide == osRemote) && DirView(osRemote)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1630. }
  1631. else
  1632. {
  1633. Result = -1;
  1634. }
  1635. }
  1636. }
  1637. else
  1638. {
  1639. // custom command that does not operate with files can be executed anytime
  1640. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1641. {
  1642. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1643. }
  1644. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1645. {
  1646. if ((ListType == ccltAll) || (ListType == ccltFile))
  1647. {
  1648. // special case is "diff"-style command that can be executed over any side,
  1649. // if we have both sides
  1650. Result =
  1651. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1652. // directly (instead of EnableSelectedOperation) to pass
  1653. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1654. ((HasDirView[osLocal] && DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1655. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1656. }
  1657. else if (ListType == ccltBoth)
  1658. {
  1659. Result = 1;
  1660. }
  1661. else
  1662. {
  1663. Result = -1;
  1664. }
  1665. }
  1666. else
  1667. {
  1668. if ((ListType == ccltAll) || (ListType == ccltFile))
  1669. {
  1670. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1671. }
  1672. else
  1673. {
  1674. Result = -1;
  1675. }
  1676. }
  1677. }
  1678. return Result;
  1679. }
  1680. //---------------------------------------------------------------------------
  1681. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1682. TStrings * FileList, const TCustomCommandType & ACommand,
  1683. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1684. {
  1685. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1686. {
  1687. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1688. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1689. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1690. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1691. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1692. bool Capture =
  1693. FLAGSET(ACommand.Params, ccShowResults) ||
  1694. FLAGSET(ACommand.Params, ccCopyResults) ||
  1695. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1696. TCaptureOutputEvent OutputEvent = NULL;
  1697. DebugAssert(FCapturedLog == NULL);
  1698. if (Capture)
  1699. {
  1700. FCapturedLog = new TStringList();
  1701. OutputEvent = TerminalCaptureLog;
  1702. }
  1703. try
  1704. {
  1705. if (!RemoteCustomCommand.IsFileCommand(Command))
  1706. {
  1707. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1708. OutputEvent);
  1709. }
  1710. else
  1711. {
  1712. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1713. }
  1714. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1715. {
  1716. if (FLAGSET(ACommand.Params, ccCopyResults))
  1717. {
  1718. CopyToClipboard(FCapturedLog);
  1719. }
  1720. if (FLAGSET(ACommand.Params, ccShowResults))
  1721. {
  1722. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1723. }
  1724. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1725. {
  1726. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1727. }
  1728. }
  1729. }
  1730. __finally
  1731. {
  1732. SAFE_DESTROY(FCapturedLog);
  1733. }
  1734. }
  1735. }
  1736. //---------------------------------------------------------------------------
  1737. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1738. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1739. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1740. {
  1741. TStrings * LocalFileList = NULL;
  1742. TStrings * RemoteFileList = NULL;
  1743. TStrings * RemoteFileListFull = NULL;
  1744. try
  1745. {
  1746. if (LocalFileCommand)
  1747. {
  1748. if (ALocalFileList == NULL)
  1749. {
  1750. DebugAssert(HasDirView[osLocal]);
  1751. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1752. // directly (instead of EnableSelectedOperation) to pass
  1753. // false to FocusedFileOnlyWhenFocused
  1754. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1755. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1756. }
  1757. else
  1758. {
  1759. LocalFileList = ALocalFileList;
  1760. }
  1761. if (FileListCommand)
  1762. {
  1763. if (LocalFileList->Count != 1)
  1764. {
  1765. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1766. }
  1767. }
  1768. else
  1769. {
  1770. if ((LocalFileList->Count != 1) &&
  1771. (FileList->Count != 1) &&
  1772. (LocalFileList->Count != FileList->Count))
  1773. {
  1774. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1775. }
  1776. }
  1777. }
  1778. UnicodeString RootTempDir;
  1779. UnicodeString TempDir;
  1780. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1781. if (!RemoteFiles)
  1782. {
  1783. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1784. }
  1785. try
  1786. {
  1787. TDateTimes RemoteFileTimes;
  1788. if (RemoteFiles)
  1789. {
  1790. RemoteFileList = FileList;
  1791. }
  1792. else
  1793. {
  1794. RemoteFileList = new TStringList();
  1795. TMakeLocalFileListParams MakeFileListParam;
  1796. MakeFileListParam.FileList = RemoteFileList;
  1797. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1798. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1799. MakeFileListParam.Recursive =
  1800. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1801. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1802. if (!MakeFileListParam.Recursive)
  1803. {
  1804. RemoteFileTimes.clear();
  1805. RemoteFileListFull = new TStringList();
  1806. MakeFileListParam.FileList = RemoteFileListFull;
  1807. MakeFileListParam.Recursive = true;
  1808. MakeFileListParam.IncludeDirs = false;
  1809. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1810. }
  1811. else
  1812. {
  1813. RemoteFileListFull = RemoteFileList;
  1814. }
  1815. }
  1816. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1817. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1818. if (!NonBlocking)
  1819. {
  1820. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : FileList->Count);
  1821. DebugAssert(FProgressForm != NULL);
  1822. FProgressForm->ReadOnly = true;
  1823. }
  1824. try
  1825. {
  1826. if (FileListCommand)
  1827. {
  1828. UnicodeString LocalFile;
  1829. // MakeFileList does not delimit filenames
  1830. UnicodeString FileList = MakeFileList(RemoteFileList);
  1831. if (LocalFileCommand)
  1832. {
  1833. DebugAssert(LocalFileList->Count == 1);
  1834. LocalFile = LocalFileList->Strings[0];
  1835. }
  1836. TLocalCustomCommand CustomCommand(Data,
  1837. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1838. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1839. if (NonBlocking)
  1840. {
  1841. DebugAssert(!POutput);
  1842. ExecuteShellChecked(ShellCommand);
  1843. }
  1844. else
  1845. {
  1846. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1847. }
  1848. }
  1849. else if (LocalFileCommand)
  1850. {
  1851. if (LocalFileList->Count == 1)
  1852. {
  1853. UnicodeString LocalFile = LocalFileList->Strings[0];
  1854. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1855. {
  1856. UnicodeString FileName = RemoteFileList->Strings[Index];
  1857. TLocalCustomCommand CustomCommand(Data,
  1858. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1859. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1860. }
  1861. }
  1862. else if (RemoteFileList->Count == 1)
  1863. {
  1864. UnicodeString FileName = RemoteFileList->Strings[0];
  1865. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1866. {
  1867. TLocalCustomCommand CustomCommand(
  1868. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1869. FileName, LocalFileList->Strings[Index], L"");
  1870. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1871. }
  1872. }
  1873. else
  1874. {
  1875. if (LocalFileList->Count != RemoteFileList->Count)
  1876. {
  1877. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1878. }
  1879. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1880. {
  1881. UnicodeString FileName = RemoteFileList->Strings[Index];
  1882. TLocalCustomCommand CustomCommand(
  1883. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1884. FileName, LocalFileList->Strings[Index], L"");
  1885. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1886. }
  1887. }
  1888. }
  1889. else
  1890. {
  1891. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1892. {
  1893. TLocalCustomCommand CustomCommand(Data,
  1894. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1895. RemoteFileList->Strings[Index], L"", L"");
  1896. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1897. }
  1898. }
  1899. }
  1900. __finally
  1901. {
  1902. if (!NonBlocking)
  1903. {
  1904. Progress.Stop();
  1905. }
  1906. }
  1907. DebugAssert(!FAutoOperation);
  1908. if (!RemoteFiles)
  1909. {
  1910. TempDir = IncludeTrailingBackslash(TempDir);
  1911. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1912. {
  1913. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1914. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1915. // Skip directories, process only nested files.
  1916. // The check is redundant as FileAge fails for directories anyway.
  1917. !DirectoryExists(FileName))
  1918. {
  1919. UnicodeString RemoteDir =
  1920. UnixExtractFileDir(
  1921. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  1922. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  1923. TDateTime NewTime;
  1924. if (FileAge(FileName, NewTime) &&
  1925. (NewTime != RemoteFileTimes[Index]))
  1926. {
  1927. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  1928. TemporaryFileCopyParam(CopyParam);
  1929. CopyParam.FileMask = L"";
  1930. FAutoOperation = true;
  1931. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  1932. TemporaryFilesList->Add(FileName);
  1933. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  1934. }
  1935. }
  1936. }
  1937. }
  1938. }
  1939. __finally
  1940. {
  1941. FAutoOperation = false;
  1942. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  1943. {
  1944. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  1945. }
  1946. }
  1947. }
  1948. __finally
  1949. {
  1950. if (RemoteFileList != FileList)
  1951. {
  1952. delete RemoteFileList;
  1953. }
  1954. if (LocalFileList != ALocalFileList)
  1955. {
  1956. delete LocalFileList;
  1957. }
  1958. if (RemoteFileListFull != RemoteFileList)
  1959. {
  1960. delete RemoteFileListFull;
  1961. }
  1962. }
  1963. }
  1964. //---------------------------------------------------------------------------
  1965. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithRemoteFiles(
  1966. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  1967. bool FileListCommand, UnicodeString * POutput)
  1968. {
  1969. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  1970. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  1971. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  1972. {
  1973. UnicodeString FileName = SelectedFileList->Strings[Index];
  1974. if (DirectoryExists(FileName))
  1975. {
  1976. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  1977. {
  1978. LocalFileList->Add(FileName);
  1979. }
  1980. if (FLAGSET(ACommand.Params, ccRecursive))
  1981. {
  1982. TMakeLocalFileListParams MakeFileListParam;
  1983. MakeFileListParam.FileList = LocalFileList.get();
  1984. MakeFileListParam.FileTimes = NULL;
  1985. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1986. MakeFileListParam.Recursive = true;
  1987. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  1988. }
  1989. }
  1990. else
  1991. {
  1992. LocalFileList->Add(FileName);
  1993. }
  1994. }
  1995. if (FileListCommand)
  1996. {
  1997. UnicodeString FileList = MakeFileList(LocalFileList.get());
  1998. TLocalCustomCommand CustomCommand(
  1999. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2000. L"", L"", FileList);
  2001. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2002. }
  2003. else
  2004. {
  2005. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2006. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : LocalFileList->Count);
  2007. DebugAssert(FProgressForm != NULL);
  2008. FProgressForm->ReadOnly = true;
  2009. try
  2010. {
  2011. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2012. {
  2013. UnicodeString FileName = LocalFileList->Strings[Index];
  2014. TLocalCustomCommand CustomCommand(
  2015. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2016. FileName, L"", L"");
  2017. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2018. }
  2019. }
  2020. __finally
  2021. {
  2022. Progress.Stop();
  2023. }
  2024. }
  2025. }
  2026. //---------------------------------------------------------------------------
  2027. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2028. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2029. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2030. {
  2031. TLocalCustomCommand LocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory());
  2032. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2033. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2034. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2035. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2036. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2037. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2038. std::unique_ptr<UnicodeString> POutput;
  2039. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2040. FLAGSET(ACommand.Params, ccCopyResults))
  2041. {
  2042. POutput.reset(new UnicodeString());
  2043. }
  2044. if (!LocalCustomCommand.IsFileCommand(Command))
  2045. {
  2046. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2047. }
  2048. // remote files?
  2049. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2050. {
  2051. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2052. }
  2053. // local files
  2054. else
  2055. {
  2056. LocalCustomCommandWithRemoteFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2057. }
  2058. if (POutput.get() != NULL)
  2059. {
  2060. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2061. int P = POutput->Pos(sLineBreak);
  2062. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2063. {
  2064. POutput->SetLength(P - 1);
  2065. }
  2066. // Copy even empty output to clipboard
  2067. if (FLAGSET(ACommand.Params, ccCopyResults))
  2068. {
  2069. CopyToClipboard(*POutput);
  2070. }
  2071. // But do not show an empty message box.
  2072. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2073. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2074. !POutput->IsEmpty())
  2075. {
  2076. TClipboardHandler ClipboardHandler;
  2077. ClipboardHandler.Text = *POutput;
  2078. TMessageParams Params;
  2079. TQueryButtonAlias Aliases[1];
  2080. Aliases[0].Button = qaRetry;
  2081. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2082. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2083. Params.Aliases = Aliases;
  2084. Params.AliasesCount = LENOF(Aliases);
  2085. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2086. }
  2087. }
  2088. }
  2089. //---------------------------------------------------------------------------
  2090. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2091. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2092. {
  2093. TCustomCommandData Data(Terminal);
  2094. UnicodeString Site = Terminal->SessionData->SessionKey;
  2095. UnicodeString HelpKeyword = ACommand.HomePage;
  2096. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2097. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2098. {
  2099. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2100. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2101. {
  2102. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, Terminal->CurrentDirectory));
  2103. }
  2104. else
  2105. {
  2106. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory()));
  2107. }
  2108. if (!DoCustomCommandOptionsDialog(
  2109. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2110. {
  2111. Abort();
  2112. }
  2113. }
  2114. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2115. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2116. {
  2117. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2118. }
  2119. else
  2120. {
  2121. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2122. }
  2123. }
  2124. //---------------------------------------------------------------------------
  2125. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2126. const TCustomCommandType & Command)
  2127. {
  2128. DebugAssert(FCustomCommandLocalFileList != NULL);
  2129. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2130. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2131. TStrings * LocalFileList = new TStringList();
  2132. TStrings * RemoteFileList = new TStringList();
  2133. try
  2134. {
  2135. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2136. {
  2137. LocalFileList->Clear();
  2138. LocalFileList->AddObject(
  2139. FCustomCommandLocalFileList->Strings[Index],
  2140. FCustomCommandLocalFileList->Objects[Index]);
  2141. RemoteFileList->Clear();
  2142. RemoteFileList->AddObject(
  2143. FCustomCommandRemoteFileList->Strings[Index],
  2144. FCustomCommandRemoteFileList->Objects[Index]);
  2145. CustomCommand(RemoteFileList, Command, LocalFileList);
  2146. }
  2147. }
  2148. __finally
  2149. {
  2150. delete LocalFileList;
  2151. delete RemoteFileList;
  2152. }
  2153. }
  2154. //---------------------------------------------------------------------------
  2155. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2156. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2157. {
  2158. delete FCustomCommandLocalFileList;
  2159. delete FCustomCommandRemoteFileList;
  2160. // takeover ownership,
  2161. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2162. FCustomCommandLocalFileList = LocalFileList;
  2163. FCustomCommandRemoteFileList = RemoteFileList;
  2164. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2165. if (Button != NULL)
  2166. {
  2167. FCustomCommandMenu->Items->Clear();
  2168. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
  2169. MenuPopup(FCustomCommandMenu, Button);
  2170. }
  2171. else
  2172. {
  2173. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2174. }
  2175. }
  2176. //---------------------------------------------------------------------------
  2177. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2178. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2179. {
  2180. DebugAssert(FCapturedLog != NULL);
  2181. if ((OutputType == cotOutput) || (OutputType == cotError))
  2182. {
  2183. FCapturedLog->Add(AddedLine);
  2184. }
  2185. }
  2186. //---------------------------------------------------------------------------
  2187. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2188. TOperationSide Side)
  2189. {
  2190. return (Side == osRemote) &&
  2191. ((Control == RemoteDirView) || (Control == RemoteDriveView));
  2192. }
  2193. //---------------------------------------------------------------------------
  2194. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2195. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2196. {
  2197. TTBItemOptions O;
  2198. O = Item->Options;
  2199. TCustomDirView * DView = DirView(Side);
  2200. if ((DView->ItemFocused != NULL) &&
  2201. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2202. // when resolving links is disabled, default action is to enter the directory,
  2203. // no matter what DoubleClickAction is configured to
  2204. ((Side != osRemote) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2205. // Can only Edit files, but can Open/Copy even directories
  2206. ((DoubleClickAction != dcaEdit) ||
  2207. !DView->ItemIsDirectory(DView->ItemFocused)))
  2208. {
  2209. Item->Options = O << tboDefault;
  2210. }
  2211. else
  2212. {
  2213. Item->Options = O >> tboDefault;
  2214. }
  2215. }
  2216. //---------------------------------------------------------------------------
  2217. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2218. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2219. {
  2220. TCustomDirView * DView = DirView(Side);
  2221. TListItem * Item = DView->ItemFocused;
  2222. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2223. EnableFocusedOperation[Side])
  2224. {
  2225. TPoint ClientPoint;
  2226. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2227. TPoint(0, 0) : MousePos;
  2228. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2229. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2230. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2231. }
  2232. }
  2233. //---------------------------------------------------------------------------
  2234. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2235. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2236. {
  2237. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2238. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2239. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2240. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2241. Handled = true;
  2242. }
  2243. //---------------------------------------------------------------------------
  2244. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2245. {
  2246. }
  2247. //---------------------------------------------------------------------------
  2248. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2249. {
  2250. DebugAssert(FErrorList == NULL);
  2251. if (WinConfiguration->ContinueOnError)
  2252. {
  2253. FErrorList = new TStringList();
  2254. Configuration->Usage->Inc(L"ContinuationsOnError");
  2255. }
  2256. NonVisualDataModule->StartBusy();
  2257. }
  2258. //---------------------------------------------------------------------------
  2259. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2260. {
  2261. NonVisualDataModule->EndBusy();
  2262. if (FErrorList)
  2263. {
  2264. HandleErrorList(FErrorList);
  2265. }
  2266. }
  2267. //---------------------------------------------------------------------------
  2268. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2269. const TCopyParamType & CopyParam)
  2270. {
  2271. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2272. {
  2273. Configuration->Usage->Inc(L"FileMaskUses");
  2274. }
  2275. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2276. {
  2277. Configuration->Usage->Inc(L"OperationMaskUses");
  2278. }
  2279. }
  2280. //---------------------------------------------------------------------------
  2281. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2282. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2283. {
  2284. void * BatchStorage;
  2285. BatchStart(BatchStorage);
  2286. bool Result;
  2287. try
  2288. {
  2289. if ((Operation == foCopy) || (Operation == foMove))
  2290. {
  2291. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2292. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2293. UnicodeString TargetDirectory;
  2294. bool Temp = false;
  2295. bool DragDrop = false;
  2296. int Options = 0;
  2297. TAutoSwitch UseQueue = asAuto;
  2298. if (Param != NULL)
  2299. {
  2300. TTransferOperationParam& TParam =
  2301. *static_cast<TTransferOperationParam*>(Param);
  2302. TargetDirectory = TParam.TargetDirectory;
  2303. Temp = TParam.Temp;
  2304. DragDrop = TParam.DragDrop;
  2305. Options = TParam.Options;
  2306. UseQueue = TParam.Queue;
  2307. }
  2308. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2309. switch (UseQueue)
  2310. {
  2311. case asOn:
  2312. CopyParam.Queue = true;
  2313. break;
  2314. case asOff:
  2315. CopyParam.Queue = false;
  2316. break;
  2317. case asAuto:
  2318. default:
  2319. // keep default
  2320. break;
  2321. }
  2322. Result =
  2323. CopyParamDialog(Direction, Type, Temp, FileList, TargetDirectory,
  2324. CopyParam, !NoConfirmation, DragDrop, Options);
  2325. if (Result)
  2326. {
  2327. DebugAssert(Terminal);
  2328. bool SelectionRestored = false;
  2329. TCustomDirView * DView = NULL;
  2330. if (HasDirView[Side])
  2331. {
  2332. DView = DirView(Side);
  2333. DView->SaveSelection();
  2334. DView->SaveSelectedNames();
  2335. }
  2336. UpdateCopyParamCounters(CopyParam);
  2337. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2338. DebugAssert(FTransferResumeList == NULL);
  2339. FTransferResumeList =
  2340. Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2341. FMoveToQueue = false;
  2342. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2343. try
  2344. {
  2345. TStrings * PermanentFileList;
  2346. std::unique_ptr<TStrings> PermanentFileListOwner;
  2347. try
  2348. {
  2349. if (Side == osLocal)
  2350. {
  2351. PermanentFileList = FileList;
  2352. Params |= FLAGMASK(Temp, cpTemporary);
  2353. Terminal->CopyToRemote(FileList, TargetDirectory, &CopyParam, Params, NULL);
  2354. if (Operation == foMove)
  2355. {
  2356. ReloadLocalDirectory();
  2357. if (DView != NULL)
  2358. {
  2359. DView->RestoreSelection();
  2360. }
  2361. SelectionRestored = true;
  2362. }
  2363. }
  2364. else
  2365. {
  2366. // Clone the file list as it may refer to current directory files,
  2367. // which get destroyed, when the source directory is reloaded after foMove operation.
  2368. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2369. // But for a hotfix, we are not going to do such a big change.
  2370. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2371. PermanentFileList = PermanentFileListOwner.get();
  2372. try
  2373. {
  2374. Terminal->CopyToLocal(
  2375. FileList, TargetDirectory, &CopyParam, Params, NULL);
  2376. }
  2377. __finally
  2378. {
  2379. if (Operation == foMove)
  2380. {
  2381. if (DView != NULL)
  2382. {
  2383. DView->RestoreSelection();
  2384. }
  2385. SelectionRestored = true;
  2386. }
  2387. ReloadLocalDirectory(TargetDirectory);
  2388. }
  2389. }
  2390. }
  2391. catch (EAbort &)
  2392. {
  2393. if (FMoveToQueue)
  2394. {
  2395. Params |=
  2396. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2397. DebugAssert(CopyParam.TransferSkipList == NULL);
  2398. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2399. if (TransferResumeList->Count > 0)
  2400. {
  2401. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2402. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2403. }
  2404. CopyParam.TransferSkipList = TransferResumeList.release();
  2405. // not really needed, just to keep it consistent with TransferResumeList
  2406. FTransferResumeList = NULL;
  2407. FMoveToQueue = false;
  2408. Configuration->Usage->Inc("MovesToBackground");
  2409. AddQueueItem(Queue, Direction, PermanentFileList, TargetDirectory, CopyParam, Params);
  2410. ClearTransferSourceSelection(Direction);
  2411. }
  2412. throw;
  2413. }
  2414. }
  2415. __finally
  2416. {
  2417. if (!SelectionRestored && (DView != NULL))
  2418. {
  2419. DView->DiscardSavedSelection();
  2420. }
  2421. FTransferResumeList = NULL;
  2422. }
  2423. }
  2424. }
  2425. else if (Operation == foRename)
  2426. {
  2427. DebugAssert(DirView(Side)->ItemFocused);
  2428. DirView(Side)->ItemFocused->EditCaption();
  2429. Result = true;
  2430. }
  2431. else if (Operation == foDelete)
  2432. {
  2433. DebugAssert(FileList->Count);
  2434. // We deliberately do not toggle alternative flag (Param), but use OR,
  2435. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2436. // shortcut of CurrentDeleteAlternativeAction
  2437. bool Alternative =
  2438. bool(Param) || UseAlternativeFunction();
  2439. bool Recycle;
  2440. if (Side == osLocal)
  2441. {
  2442. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2443. }
  2444. else
  2445. {
  2446. Recycle = (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2447. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2448. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2449. }
  2450. Result =
  2451. !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2452. if (!Result)
  2453. {
  2454. UnicodeString Query;
  2455. if (FileList->Count == 1)
  2456. {
  2457. if (Side == osLocal)
  2458. {
  2459. Query = ExtractFileName(FileList->Strings[0]);
  2460. }
  2461. else
  2462. {
  2463. Query = UnixExtractFileName(FileList->Strings[0]);
  2464. }
  2465. Query = FMTLOAD(
  2466. (Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2467. }
  2468. else
  2469. {
  2470. Query = FMTLOAD(
  2471. (Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2472. }
  2473. TMessageParams Params(mpNeverAskAgainCheck);
  2474. Params.ImageName = L"Delete file";
  2475. unsigned int Answer = MessageDialog(MainInstructions(Query), qtConfirmation,
  2476. qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2477. if (Answer == qaNeverAskAgain)
  2478. {
  2479. Result = true;
  2480. if (Recycle)
  2481. {
  2482. WinConfiguration->ConfirmRecycling = false;
  2483. }
  2484. else
  2485. {
  2486. WinConfiguration->ConfirmDeleting = false;
  2487. }
  2488. }
  2489. else
  2490. {
  2491. Result = (Answer == qaOK);
  2492. }
  2493. }
  2494. if (Result)
  2495. {
  2496. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2497. }
  2498. }
  2499. else if (Operation == foSetProperties)
  2500. {
  2501. RemoteDirView->SaveSelectedNames();
  2502. Result = SetProperties(Side, FileList);
  2503. }
  2504. else if (Operation == foCustomCommand)
  2505. {
  2506. DebugAssert(Param);
  2507. DebugAssert(Side == osRemote);
  2508. RemoteDirView->SaveSelectedNames();
  2509. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2510. CustomCommand(FileList, *Command, NULL);
  2511. Result = true;
  2512. }
  2513. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2514. {
  2515. DebugAssert(Side == osRemote);
  2516. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2517. (Operation == foRemoteMove), reinterpret_cast<TTerminal *>(Param));
  2518. }
  2519. else if (Operation == foLock)
  2520. {
  2521. DebugAssert(Side == osRemote);
  2522. LockFiles(FileList, true);
  2523. Result = true;
  2524. }
  2525. else if (Operation == foUnlock)
  2526. {
  2527. DebugAssert(Side == osRemote);
  2528. LockFiles(FileList, false);
  2529. Result = true;
  2530. }
  2531. else
  2532. {
  2533. DebugFail();
  2534. }
  2535. }
  2536. __finally
  2537. {
  2538. BatchEnd(BatchStorage);
  2539. }
  2540. return Result;
  2541. }
  2542. //---------------------------------------------------------------------------
  2543. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2544. {
  2545. if (Side == osCurrent)
  2546. {
  2547. Side = FCurrentSide;
  2548. }
  2549. return Side;
  2550. }
  2551. //---------------------------------------------------------------------------
  2552. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2553. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2554. {
  2555. Side = GetSide(Side);
  2556. bool Result;
  2557. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, (Side == osLocal), NULL);
  2558. try
  2559. {
  2560. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2561. }
  2562. __finally
  2563. {
  2564. delete FileList;
  2565. }
  2566. return Result;
  2567. }
  2568. //---------------------------------------------------------------------------
  2569. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2570. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2571. bool NoConfirmation, void * Param)
  2572. {
  2573. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2574. {
  2575. if ((Operation == foCopy) || (Operation == foMove))
  2576. {
  2577. if (GetSide(Side) == osLocal)
  2578. {
  2579. Configuration->Usage->Inc(L"UploadsCommand");
  2580. }
  2581. else
  2582. {
  2583. Configuration->Usage->Inc(L"DownloadsCommand");
  2584. }
  2585. }
  2586. }
  2587. }
  2588. //---------------------------------------------------------------------------
  2589. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2590. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2591. {
  2592. TTransferOperationParam Param;
  2593. if ((WinConfiguration->Interface != ifCommander) ||
  2594. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2595. {
  2596. Flags &= ~cocShortCutHint;
  2597. }
  2598. TCustomDirView * DView = DirView(Side);
  2599. Param.Options =
  2600. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2601. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2602. if (FLAGSET(Flags, cocQueue))
  2603. {
  2604. Param.Queue = asOn;
  2605. }
  2606. else if (FLAGSET(Flags, cocNonQueue))
  2607. {
  2608. Param.Queue = asOff;
  2609. }
  2610. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2611. }
  2612. //---------------------------------------------------------------------------
  2613. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2614. {
  2615. try
  2616. {
  2617. if (ErrorList->Count)
  2618. {
  2619. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2620. if (MessageDialog(Message, qtError,
  2621. qaOK | qaCancel, HELP_NONE) == qaOK)
  2622. {
  2623. unsigned int Answer;
  2624. int Index = 0;
  2625. do
  2626. {
  2627. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2628. TQueryButtonAlias Aliases[2];
  2629. Aliases[0].Button = qaYes;
  2630. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2631. Aliases[1].Button = qaNo;
  2632. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2633. TMessageParams Params;
  2634. Params.Aliases = Aliases;
  2635. Params.AliasesCount = LENOF(Aliases);
  2636. Answer = MoreMessageDialog(
  2637. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2638. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2639. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2640. qaOK, HELP_NONE, &Params);
  2641. if (Answer == qaNo)
  2642. {
  2643. Index++;
  2644. }
  2645. if (Answer == qaYes)
  2646. {
  2647. Index--;
  2648. }
  2649. }
  2650. while (Answer != qaOK);
  2651. }
  2652. }
  2653. }
  2654. __finally
  2655. {
  2656. TStrings * List = ErrorList;
  2657. ErrorList = NULL;
  2658. for (int i = 0; i < List->Count; i++)
  2659. {
  2660. delete List->Objects[i];
  2661. }
  2662. delete List;
  2663. }
  2664. }
  2665. //---------------------------------------------------------------------------
  2666. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2667. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2668. {
  2669. // needed for checking filemasks, as there's no directory object
  2670. // associated with the file object
  2671. File->FullFileName = FullFileName;
  2672. TFileMasks::TParams MaskParams;
  2673. MaskParams.Size = File->Size;
  2674. MaskParams.Modification = File->Modification;
  2675. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2676. }
  2677. //---------------------------------------------------------------------------
  2678. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2679. {
  2680. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2681. Side = GetSide(Side);
  2682. TCustomDirView * DView = DirView(Side);
  2683. TListItem * FocusedItem = DView->ItemFocused;
  2684. UnicodeString Name;
  2685. if ((FocusedItem != NULL) && !DView->ItemIsParentDirectory(FocusedItem))
  2686. {
  2687. Name = DView->ItemFileName(FocusedItem);
  2688. }
  2689. else
  2690. {
  2691. Name = LoadStr(NEW_FILE);
  2692. }
  2693. UnicodeString Names = Name;
  2694. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2695. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2696. HELP_EDIT_NEW, History.get(), true))
  2697. {
  2698. while (!Names.IsEmpty())
  2699. {
  2700. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2701. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2702. UnicodeString TargetFileName;
  2703. UnicodeString LocalFileName;
  2704. UnicodeString RootTempDir;
  2705. UnicodeString TempDir;
  2706. UnicodeString RemoteDirectory;
  2707. bool ExistingFile = false;
  2708. if (Side == osRemote)
  2709. {
  2710. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2711. TRemoteFile * File = NULL;
  2712. if (FTerminal->FileExists(Name, &File))
  2713. {
  2714. try
  2715. {
  2716. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2717. ExistingFile = true;
  2718. }
  2719. __finally
  2720. {
  2721. delete File;
  2722. }
  2723. }
  2724. if (!ExistingFile)
  2725. {
  2726. RemoteDirectory = UnixExtractFilePath(Name);
  2727. TemporaryDirectoryForRemoteFiles(
  2728. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2729. TargetFileName = UnixExtractFileName(Name);
  2730. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2731. LocalFileName = TempDir +
  2732. // We probably do not want to trim the VMS version here
  2733. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2734. }
  2735. }
  2736. else
  2737. {
  2738. if (ExtractFilePath(Name).IsEmpty())
  2739. {
  2740. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2741. }
  2742. else
  2743. {
  2744. LocalFileName = ExpandFileName(Name);
  2745. }
  2746. TargetFileName = ExtractFileName(Name);
  2747. }
  2748. if (!ExistingFile)
  2749. {
  2750. if (!FileExists(ApiPath(LocalFileName)))
  2751. {
  2752. int File = FileCreate(ApiPath(LocalFileName));
  2753. if (File < 0)
  2754. {
  2755. if (!RootTempDir.IsEmpty())
  2756. {
  2757. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2758. }
  2759. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2760. }
  2761. else
  2762. {
  2763. FileClose(File);
  2764. }
  2765. }
  2766. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2767. const TEditorData * ExternalEditor = NULL;
  2768. TFileMasks::TParams MaskParams; // size not known
  2769. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2770. false, MaskParams);
  2771. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2772. ExternalEditor, RootTempDir, RemoteDirectory);
  2773. }
  2774. }
  2775. }
  2776. }
  2777. //---------------------------------------------------------------------------
  2778. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2779. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2780. {
  2781. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2782. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2783. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2784. return
  2785. ((ExecuteFileBy == efInternalEditor)) ||
  2786. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2787. }
  2788. //---------------------------------------------------------------------------
  2789. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2790. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2791. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2792. UnicodeString RemoteDirectory)
  2793. {
  2794. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2795. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2796. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2797. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2798. Side = GetSide(Side);
  2799. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2800. if (Side == osRemote)
  2801. {
  2802. Data->Terminal = Terminal;
  2803. Data->Queue = Queue;
  2804. Data->SessionData = CloneCurrentSessionData();
  2805. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2806. Data->RemoteDirectory = RemoteDirectory;
  2807. Data->SessionName = Terminal->SessionData->SessionName;
  2808. Data->LocalRootDirectory = LocalRootDirectory;
  2809. Data->OriginalFileName = OriginalFileName;
  2810. Data->Command = L""; // will be changed later for external editor
  2811. }
  2812. if (ExecuteFileBy == efInternalEditor)
  2813. {
  2814. if (Side == osRemote)
  2815. {
  2816. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2817. L" - " + Terminal->SessionData->SessionName;
  2818. TForm * Editor;
  2819. try
  2820. {
  2821. TNotifyEvent OnFileChanged = NULL;
  2822. TNotifyEvent OnSaveAll = NULL;
  2823. TAnyModifiedEvent OnAnyModified = NULL;
  2824. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2825. if (Terminal->IsCapable[fsBackgroundTransfers])
  2826. {
  2827. OnFileChanged = FEditorManager->FileChanged;
  2828. OnSaveAll = SaveAllInternalEditors;
  2829. OnAnyModified = AnyInternalEditorModified;
  2830. }
  2831. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2832. FEditorManager->FileReload, FEditorManager->FileClosed,
  2833. OnSaveAll, OnAnyModified,
  2834. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding);
  2835. }
  2836. catch(...)
  2837. {
  2838. if (!LocalRootDirectory.IsEmpty())
  2839. {
  2840. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2841. }
  2842. throw;
  2843. }
  2844. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2845. }
  2846. else
  2847. {
  2848. DebugAssert(!FStandaloneEditing);
  2849. TForm * Editor =
  2850. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2851. SaveAllInternalEditors, AnyInternalEditorModified,
  2852. L"", false, SessionColor, -1);
  2853. FLocalEditors->Add(Editor);
  2854. }
  2855. }
  2856. else
  2857. {
  2858. HANDLE Process;
  2859. if (ExecuteFileBy == efExternalEditor)
  2860. {
  2861. UnicodeString Program, Params, Dir;
  2862. Data->Command = ExternalEditor->ExternalEditor;
  2863. ReformatFileNameCommand(Data->Command);
  2864. SplitCommand(Data->Command, Program, Params, Dir);
  2865. Params = ExpandFileNameCommand(Params, FileName);
  2866. Program = ExpandEnvironmentVariables(Program);
  2867. if (!ExecuteShell(Program, Params, Process))
  2868. {
  2869. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2870. }
  2871. }
  2872. else
  2873. {
  2874. DebugAssert(Side == osRemote);
  2875. if (!ExecuteShell(FileName, L"", Process))
  2876. {
  2877. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2878. }
  2879. }
  2880. if ((Side == osLocal) ||
  2881. ((ExecuteFileBy == efShell) &&
  2882. !WinConfiguration->Editor.SDIShellEditor) ||
  2883. ((ExecuteFileBy == efExternalEditor) &&
  2884. !ExternalEditor->SDIExternalEditor))
  2885. {
  2886. // no need for handle
  2887. if (Process != NULL)
  2888. {
  2889. DebugCheck(CloseHandle(Process));
  2890. }
  2891. Process = INVALID_HANDLE_VALUE;
  2892. }
  2893. else
  2894. {
  2895. if (Process == NULL)
  2896. {
  2897. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2898. }
  2899. }
  2900. if (Side == osRemote)
  2901. {
  2902. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2903. }
  2904. }
  2905. }
  2906. //---------------------------------------------------------------------------
  2907. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2908. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2909. void * /*Arg*/)
  2910. {
  2911. if (Token != NULL)
  2912. {
  2913. EditorFormFileSave(static_cast<TForm *>(Token));
  2914. }
  2915. }
  2916. //---------------------------------------------------------------------------
  2917. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  2918. {
  2919. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  2920. {
  2921. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  2922. }
  2923. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  2924. }
  2925. //---------------------------------------------------------------------------
  2926. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  2927. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2928. void * Arg)
  2929. {
  2930. if ((Token != NULL) &&
  2931. IsEditorFormModified(static_cast<TForm *>(Token)))
  2932. {
  2933. *static_cast<bool *>(Arg) = true;
  2934. }
  2935. }
  2936. //---------------------------------------------------------------------------
  2937. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  2938. TObject * /*Sender*/, bool & Modified)
  2939. {
  2940. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  2941. {
  2942. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  2943. {
  2944. Modified = true;
  2945. }
  2946. }
  2947. if (!Modified)
  2948. {
  2949. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  2950. }
  2951. }
  2952. //---------------------------------------------------------------------------
  2953. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  2954. {
  2955. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  2956. }
  2957. //---------------------------------------------------------------------------
  2958. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  2959. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  2960. UnicodeString & Result, UnicodeString & RootDirectory)
  2961. {
  2962. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  2963. {
  2964. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  2965. Result = RootDirectory;
  2966. }
  2967. else
  2968. {
  2969. RootDirectory = L"";
  2970. Result = WinConfiguration->ExpandedTemporaryDirectory();
  2971. Result = IncludeTrailingBackslash(Result);
  2972. }
  2973. if (WinConfiguration->TemporaryDirectoryAppendSession)
  2974. {
  2975. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  2976. }
  2977. if (WinConfiguration->TemporaryDirectoryAppendPath)
  2978. {
  2979. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  2980. {
  2981. RemoteDirectory.Delete(1, 1);
  2982. }
  2983. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  2984. }
  2985. if (!ForceDirectories(ApiPath(Result)))
  2986. {
  2987. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  2988. }
  2989. }
  2990. //---------------------------------------------------------------------------
  2991. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  2992. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  2993. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  2994. {
  2995. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2996. if (ForceText)
  2997. {
  2998. CopyParam.TransferMode = tmAscii;
  2999. }
  3000. TemporaryFileCopyParam(CopyParam);
  3001. if (AllFiles)
  3002. {
  3003. CopyParam.IncludeFileMask = TFileMasks();
  3004. }
  3005. if (TempDir.IsEmpty())
  3006. {
  3007. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3008. }
  3009. DebugAssert(!FAutoOperation);
  3010. FAutoOperation = AutoOperation;
  3011. Terminal->ExceptionOnFail = true;
  3012. try
  3013. {
  3014. try
  3015. {
  3016. // turn off confirmations, as for MDI editors we may possibly download
  3017. // the same file over
  3018. Terminal->CopyToLocal(
  3019. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3020. if (GetTargetNames)
  3021. {
  3022. for (int i = 0; i < FileList->Count; i++)
  3023. {
  3024. FileList->Strings[i] =
  3025. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3026. }
  3027. }
  3028. }
  3029. catch(...)
  3030. {
  3031. if (!RootTempDir.IsEmpty())
  3032. {
  3033. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3034. }
  3035. throw;
  3036. }
  3037. }
  3038. __finally
  3039. {
  3040. FAutoOperation = false;
  3041. Terminal->ExceptionOnFail = false;
  3042. }
  3043. }
  3044. //---------------------------------------------------------------------------
  3045. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3046. {
  3047. // Do not waste time checking for default editor list the next time,
  3048. // or testing if default editor is not notepad.
  3049. bool TryNextTime = false;
  3050. UnicodeString UsageState;
  3051. if (!WinConfiguration->EditorList->IsDefaultList())
  3052. {
  3053. UsageState = "H";
  3054. }
  3055. else
  3056. {
  3057. UnicodeString Executable;
  3058. UnicodeString ExecutableDescription;
  3059. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3060. {
  3061. UnicodeString Message =
  3062. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3063. unsigned int Answer =
  3064. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3065. if (Answer != qaOK)
  3066. {
  3067. UsageState = "R";
  3068. }
  3069. else
  3070. {
  3071. UsageState = "A";
  3072. TEditorData EditorData;
  3073. EditorData.Editor = edExternal;
  3074. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3075. EditorData.DecideExternalEditorText();
  3076. TEditorList EditorList;
  3077. EditorList = *WinConfiguration->EditorList;
  3078. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3079. WinConfiguration->EditorList = &EditorList;
  3080. }
  3081. }
  3082. }
  3083. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3084. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3085. }
  3086. //---------------------------------------------------------------------------
  3087. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3088. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3089. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3090. {
  3091. if (ExecuteFileBy == efDefaultEditor)
  3092. {
  3093. if (!WinConfiguration->OfferedEditorAutoConfig)
  3094. {
  3095. EditorAutoConfig();
  3096. }
  3097. const TEditorPreferences * Editor =
  3098. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3099. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3100. {
  3101. ExecuteFileBy = efInternalEditor;
  3102. ExternalEditor = NULL;
  3103. }
  3104. else if (Editor->Data->Editor == edOpen)
  3105. {
  3106. ExecuteFileBy = efShell;
  3107. ExternalEditor = NULL;
  3108. }
  3109. else
  3110. {
  3111. ExecuteFileBy = efExternalEditor;
  3112. ExternalEditor = Editor->Data;
  3113. }
  3114. }
  3115. }
  3116. //---------------------------------------------------------------------------
  3117. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3118. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3119. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3120. {
  3121. UnicodeString OriginalFileName;
  3122. UnicodeString LocalRootDirectory;
  3123. UnicodeString RemoteDirectory;
  3124. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3125. (Side == osLocal), MaskParams);
  3126. UnicodeString Counter;
  3127. UnicodeString LocalFileName;
  3128. bool Handled = false;
  3129. if (Side == osRemote)
  3130. {
  3131. // We need to trim VMS version here, so that we use name without version
  3132. // when uploading back to create a new version of the file
  3133. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3134. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3135. TObject * Token = NULL;
  3136. UnicodeString LocalDirectory;
  3137. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3138. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3139. LocalDirectory))
  3140. {
  3141. if (Token != NULL)
  3142. {
  3143. TForm * Form = dynamic_cast<TForm *>(Token);
  3144. if (Form->WindowState == wsMinimized)
  3145. {
  3146. ShowWindow(Form->Handle, SW_RESTORE);
  3147. }
  3148. else
  3149. {
  3150. Form->SetFocus();
  3151. }
  3152. Handled = true;
  3153. }
  3154. else
  3155. {
  3156. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3157. }
  3158. }
  3159. if (!Handled)
  3160. {
  3161. TStringList * FileList1 = new TStringList();
  3162. try
  3163. {
  3164. FileList1->AddObject(FullFileName, Object);
  3165. TemporarilyDownloadFiles(FileList1,
  3166. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3167. LocalRootDirectory, LocalDirectory, true, true, true);
  3168. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3169. }
  3170. __finally
  3171. {
  3172. delete FileList1;
  3173. }
  3174. switch (ExecuteFileBy)
  3175. {
  3176. case efShell:
  3177. Counter = "RemoteFilesExecuted";
  3178. break;
  3179. case efInternalEditor:
  3180. Counter = "RemoteFilesOpenedInInternalEditor";
  3181. break;
  3182. case efExternalEditor:
  3183. Counter = "RemoteFilesOpenedInExternalEditor";
  3184. break;
  3185. default:
  3186. DebugFail();
  3187. }
  3188. }
  3189. }
  3190. else
  3191. {
  3192. LocalFileName = FullFileName;
  3193. OriginalFileName = ExtractFileName(FullFileName);
  3194. switch (ExecuteFileBy)
  3195. {
  3196. case efShell:
  3197. Counter = "LocalFilesExecuted";
  3198. break;
  3199. case efInternalEditor:
  3200. Counter = "LocalFilesOpenedInInternalEditor";
  3201. break;
  3202. case efExternalEditor:
  3203. Counter = "LocalFilesOpenedInExternalEditor";
  3204. break;
  3205. default:
  3206. DebugFail();
  3207. }
  3208. }
  3209. if (!Handled)
  3210. {
  3211. Configuration->Usage->Inc(Counter);
  3212. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3213. ExternalEditor, LocalRootDirectory, RemoteDirectory);
  3214. }
  3215. }
  3216. //---------------------------------------------------------------------------
  3217. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3218. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3219. bool AllSelected, bool OnFocused)
  3220. {
  3221. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3222. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3223. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3224. Side = GetSide(Side);
  3225. TCustomDirView * DView = DirView(Side);
  3226. TStrings * FileList = AllSelected ?
  3227. DView->CreateFileList(OnFocused, Side == osLocal) :
  3228. DView->CreateFocusedFileList(Side == osLocal);
  3229. try
  3230. {
  3231. DebugAssert(AllSelected || (FileList->Count == 1));
  3232. for (int i = 0; i < FileList->Count; i++)
  3233. {
  3234. UnicodeString ListFileName = FileList->Strings[i];
  3235. UnicodeString FileNameOnly = (Side == osRemote) ?
  3236. UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3237. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3238. if (!DView->ItemIsDirectory(Item))
  3239. {
  3240. UnicodeString FullFileName;
  3241. if (Side == osRemote)
  3242. {
  3243. FullFileName = RemoteDirView->Path + ListFileName;
  3244. }
  3245. else
  3246. {
  3247. FullFileName = ListFileName;
  3248. }
  3249. TFileMasks::TParams MaskParams;
  3250. MaskParams.Size = DView->ItemFileSize(Item);
  3251. TDateTimePrecision Precision;
  3252. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3253. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3254. FileList->Objects[i], MaskParams);
  3255. }
  3256. }
  3257. }
  3258. __finally
  3259. {
  3260. delete FileList;
  3261. }
  3262. }
  3263. //---------------------------------------------------------------------------
  3264. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3265. {
  3266. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3267. CopyParam.FileNameCase = ncNoChange;
  3268. CopyParam.PreserveRights = false;
  3269. CopyParam.PreserveReadOnly = false;
  3270. CopyParam.ReplaceInvalidChars = true;
  3271. CopyParam.IncludeFileMask = TFileMasks();
  3272. CopyParam.NewerOnly = false;
  3273. CopyParam.FileMask = L"";
  3274. }
  3275. //---------------------------------------------------------------------------
  3276. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3277. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3278. {
  3279. TTerminalManager * Manager = TTerminalManager::Instance();
  3280. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3281. {
  3282. if (!NonVisualDataModule->Busy)
  3283. {
  3284. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3285. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3286. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3287. if (Data->Terminal == NULL)
  3288. {
  3289. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3290. if (SameSiteTerminal != NULL)
  3291. {
  3292. UnicodeString Message =
  3293. FMTLOAD(EDIT_SESSION_REATTACH,
  3294. (FileNameOnly, Data->SessionName, FileNameOnly));
  3295. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3296. {
  3297. Data->Terminal = Terminal;
  3298. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3299. Data->SessionName = Terminal->SessionData->SessionName;
  3300. // We might also overwrite session data
  3301. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3302. }
  3303. else
  3304. {
  3305. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3306. Abort();
  3307. }
  3308. }
  3309. }
  3310. // foreground session should reconnect itself
  3311. else if (Terminal != Data->Terminal)
  3312. {
  3313. UnicodeString Message =
  3314. MainInstructions(
  3315. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3316. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3317. {
  3318. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3319. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3320. }
  3321. else
  3322. {
  3323. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3324. Abort();
  3325. }
  3326. }
  3327. }
  3328. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3329. {
  3330. Configuration->Usage->Inc(L"EditInactiveSession");
  3331. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3332. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3333. (ExtractFileName(FileName), Data->SessionName)));
  3334. }
  3335. }
  3336. TStrings * FileList = new TStringList();
  3337. try
  3338. {
  3339. FileList->Add(FileName);
  3340. // consider using the same settings (preset) as when the file was downloaded
  3341. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3342. TemporaryFileCopyParam(CopyParam);
  3343. if (Data->ForceText)
  3344. {
  3345. CopyParam.TransferMode = tmAscii;
  3346. }
  3347. // so i do not need to worry if masking algorithm works in all cases
  3348. // ("" means "copy file name", no masking is actually done)
  3349. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3350. {
  3351. CopyParam.FileMask = L"";
  3352. }
  3353. else
  3354. {
  3355. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3356. }
  3357. DebugAssert(Data->Queue != NULL);
  3358. int Params = cpNoConfirmation | cpTemporary;
  3359. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3360. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3361. QueueItem->CompleteEvent = UploadCompleteEvent;
  3362. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3363. }
  3364. __finally
  3365. {
  3366. delete FileList;
  3367. }
  3368. }
  3369. //---------------------------------------------------------------------------
  3370. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3371. const UnicodeString FileName, const TEditedFileData * Data)
  3372. {
  3373. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3374. DebugAssert(!NonVisualDataModule->Busy);
  3375. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3376. {
  3377. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3378. (ExtractFileName(FileName), Data->SessionName)));
  3379. }
  3380. TTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3381. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3382. NonVisualDataModule->StartBusy();
  3383. try
  3384. {
  3385. std::unique_ptr<TRemoteFile> File;
  3386. UnicodeString RemoteFileName =
  3387. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3388. FTerminal->ExceptionOnFail = true;
  3389. try
  3390. {
  3391. TRemoteFile * AFile = NULL;
  3392. FTerminal->ReadFile(RemoteFileName, AFile);
  3393. File.reset(AFile);
  3394. if (!File->HaveFullFileName)
  3395. {
  3396. File->FullFileName = RemoteFileName;
  3397. }
  3398. }
  3399. __finally
  3400. {
  3401. FTerminal->ExceptionOnFail = false;
  3402. }
  3403. std::unique_ptr<TStrings> FileList(new TStringList());
  3404. FileList->AddObject(RemoteFileName, File.get());
  3405. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3406. UnicodeString TempDir = ExtractFilePath(FileName);
  3407. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3408. TempDir, true, true, true);
  3409. // sanity check, the target file name should be still the same
  3410. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3411. }
  3412. __finally
  3413. {
  3414. NonVisualDataModule->EndBusy();
  3415. // it actually may not exist anymore...
  3416. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3417. }
  3418. }
  3419. //---------------------------------------------------------------------------
  3420. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3421. const TEditedFileData * Data, bool & KeepOpen)
  3422. {
  3423. // Command is set for external editors only (not for "shell" open).
  3424. if (!Data->Command.IsEmpty())
  3425. {
  3426. bool AnyFound = false;
  3427. bool AnyMDI = false;
  3428. bool AnyNonMDI = false;
  3429. bool AnyDetect = false;
  3430. TEditorList * EditorList = new TEditorList();
  3431. try
  3432. {
  3433. *EditorList = *WinConfiguration->EditorList;
  3434. for (int i = 0; i < EditorList->Count; i++)
  3435. {
  3436. const TEditorPreferences * Editor = EditorList->Editors[i];
  3437. if ((Editor->Data->Editor == edExternal) &&
  3438. (Editor->Data->ExternalEditor == Data->Command))
  3439. {
  3440. AnyFound = true;
  3441. if (Editor->Data->SDIExternalEditor)
  3442. {
  3443. AnyNonMDI = true;
  3444. if (Editor->Data->DetectMDIExternalEditor)
  3445. {
  3446. AnyDetect = true;
  3447. }
  3448. }
  3449. else
  3450. {
  3451. AnyMDI = true;
  3452. }
  3453. }
  3454. }
  3455. bool EnableMDI = false;
  3456. bool DisableDetect = false;
  3457. if (AnyMDI)
  3458. {
  3459. KeepOpen = true;
  3460. if (AnyNonMDI)
  3461. {
  3462. // there is at least one instance of the editor with MDI support enabled,
  3463. // and one with disabled, enable it for all instances
  3464. EnableMDI = true;
  3465. }
  3466. }
  3467. else if (AnyFound && !AnyDetect)
  3468. {
  3469. // at least once instance found but all have MDI autodetection disabled
  3470. // => close the file (default action)
  3471. }
  3472. else
  3473. {
  3474. // no instance of the editor has MDI support enabled
  3475. TMessageParams Params;
  3476. if (AnyFound)
  3477. {
  3478. // there is at least one instance of the editor
  3479. Params.Params |= mpNeverAskAgainCheck;
  3480. }
  3481. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3482. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3483. switch (Answer)
  3484. {
  3485. case qaNeverAskAgain:
  3486. DisableDetect = true;
  3487. break;
  3488. case qaNo:
  3489. EnableMDI = true;
  3490. KeepOpen = true;
  3491. break;
  3492. }
  3493. }
  3494. if (AnyFound && (EnableMDI || DisableDetect))
  3495. {
  3496. bool Changed = false;
  3497. for (int i = 0; i < EditorList->Count; i++)
  3498. {
  3499. const TEditorPreferences * Editor = EditorList->Editors[i];
  3500. if ((Editor->Data->Editor == edExternal) &&
  3501. (Editor->Data->ExternalEditor == Data->Command) &&
  3502. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3503. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3504. {
  3505. Changed = true;
  3506. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3507. if (EnableMDI)
  3508. {
  3509. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3510. }
  3511. if (DisableDetect)
  3512. {
  3513. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3514. }
  3515. EditorList->Change(i, UpdatedEditor);
  3516. }
  3517. }
  3518. if (Changed)
  3519. {
  3520. WinConfiguration->EditorList = EditorList;
  3521. }
  3522. }
  3523. }
  3524. __finally
  3525. {
  3526. delete EditorList;
  3527. }
  3528. }
  3529. else
  3530. {
  3531. // "open" case
  3532. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3533. qaOK, HELP_APP_EARLY_CLOSED);
  3534. }
  3535. }
  3536. //---------------------------------------------------------------------------
  3537. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3538. {
  3539. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3540. }
  3541. //---------------------------------------------------------------------------
  3542. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3543. {
  3544. SideEnter(osRemote);
  3545. }
  3546. //---------------------------------------------------------------------------
  3547. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3548. {
  3549. MakeNextInTabOrder(RemoteDirView, RemoteDriveView);
  3550. SideEnter(osRemote);
  3551. }
  3552. //---------------------------------------------------------------------------
  3553. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3554. {
  3555. FCurrentSide = Side;
  3556. if (Visible)
  3557. {
  3558. UpdateControls();
  3559. }
  3560. }
  3561. //---------------------------------------------------------------------------
  3562. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3563. TStrings * FileList, bool Alternative)
  3564. {
  3565. TCustomDirView * DView = DirView(Side);
  3566. DView->SaveSelection();
  3567. DView->SaveSelectedNames();
  3568. DebugAssert(!FAlternativeDelete);
  3569. FAlternativeDelete = Alternative;
  3570. try
  3571. {
  3572. if (Side == osRemote)
  3573. {
  3574. DebugAssert(Terminal != NULL);
  3575. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3576. }
  3577. else
  3578. {
  3579. try
  3580. {
  3581. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3582. }
  3583. __finally
  3584. {
  3585. ReloadLocalDirectory();
  3586. }
  3587. }
  3588. FAlternativeDelete = false;
  3589. }
  3590. catch(...)
  3591. {
  3592. FAlternativeDelete = false;
  3593. DView->DiscardSavedSelection();
  3594. throw;
  3595. }
  3596. DView->RestoreSelection();
  3597. }
  3598. //---------------------------------------------------------------------------
  3599. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3600. {
  3601. DebugAssert(Terminal);
  3602. RemoteDirView->SaveSelection();
  3603. RemoteDirView->SaveSelectedNames();
  3604. try
  3605. {
  3606. if (Lock)
  3607. {
  3608. Terminal->LockFiles(FileList);
  3609. }
  3610. else
  3611. {
  3612. Terminal->UnlockFiles(FileList);
  3613. }
  3614. }
  3615. catch(...)
  3616. {
  3617. RemoteDirView->DiscardSavedSelection();
  3618. throw;
  3619. }
  3620. RemoteDirView->RestoreSelection();
  3621. }
  3622. //---------------------------------------------------------------------------
  3623. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TTerminal *& Session,
  3624. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3625. bool NoConfirmation, bool Move)
  3626. {
  3627. DebugAssert(Terminal != NULL);
  3628. // update Terminal->StateData->RemoteDirectory
  3629. UpdateTerminal(Terminal);
  3630. if (Session == NULL)
  3631. {
  3632. Session = TTerminalManager::Instance()->ActiveTerminal;
  3633. }
  3634. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3635. {
  3636. if (RemoteDriveView->DropTarget != NULL)
  3637. {
  3638. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3639. }
  3640. else if (RemoteDirView->DropTarget != NULL)
  3641. {
  3642. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3643. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3644. }
  3645. else
  3646. {
  3647. Target = RemoteDirView->Path;
  3648. }
  3649. }
  3650. else
  3651. {
  3652. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Session));
  3653. Target = ManagedTerminal->StateData->RemoteDirectory;
  3654. }
  3655. Target = UnixIncludeTrailingBackslash(Target);
  3656. if (FileList->Count == 1)
  3657. {
  3658. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3659. }
  3660. else
  3661. {
  3662. FileMask = AnyMask;
  3663. }
  3664. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3665. bool Result = true;
  3666. if (!NoConfirmation)
  3667. {
  3668. bool Multi = (FileList->Count > 1);
  3669. if (Move)
  3670. {
  3671. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3672. }
  3673. else
  3674. {
  3675. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3676. TStrings * Directories = new TStringList;
  3677. try
  3678. {
  3679. for (int Index = 0; Index < Sessions->Count; Index++)
  3680. {
  3681. TManagedTerminal * Terminal =
  3682. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3683. Directories->Add(Terminal->StateData->RemoteDirectory);
  3684. }
  3685. TDirectRemoteCopy AllowDirectCopy;
  3686. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3687. {
  3688. DebugAssert(DirectCopy);
  3689. AllowDirectCopy = drcAllow;
  3690. }
  3691. else if (FTerminal->IsCapable[fcSecondaryShell])
  3692. {
  3693. DebugAssert(DirectCopy);
  3694. AllowDirectCopy = drcConfirmCommandSession;
  3695. }
  3696. else
  3697. {
  3698. DebugAssert(!DirectCopy);
  3699. AllowDirectCopy = drcDisallow;
  3700. }
  3701. void * ASession = Session;
  3702. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3703. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3704. Session = static_cast<TTerminal *>(ASession);
  3705. }
  3706. __finally
  3707. {
  3708. delete Directories;
  3709. }
  3710. }
  3711. }
  3712. return Result;
  3713. }
  3714. //---------------------------------------------------------------------------
  3715. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3716. TStrings * FileList, bool NoConfirmation, bool Move, TTerminal * Session)
  3717. {
  3718. bool DirectCopy;
  3719. UnicodeString Target, FileMask;
  3720. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3721. if (Result)
  3722. {
  3723. if (!Move && !DirectCopy)
  3724. {
  3725. Configuration->Usage->Inc("RemoteCopyTemp");
  3726. UnicodeString RootTempDir;
  3727. UnicodeString TempDir;
  3728. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3729. TStrings * TemporaryFilesList = new TStringList();
  3730. try
  3731. {
  3732. TMakeLocalFileListParams MakeFileListParam;
  3733. MakeFileListParam.FileList = TemporaryFilesList;
  3734. MakeFileListParam.FileTimes = NULL;
  3735. MakeFileListParam.IncludeDirs = true;
  3736. MakeFileListParam.Recursive = false;
  3737. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3738. TTerminalManager::Instance()->ActiveTerminal = Session;
  3739. if (TemporaryFilesList->Count > 0)
  3740. {
  3741. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3742. CopyParam.FileMask = FileMask;
  3743. DebugAssert(!FAutoOperation);
  3744. FAutoOperation = true;
  3745. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3746. }
  3747. }
  3748. __finally
  3749. {
  3750. delete TemporaryFilesList;
  3751. FAutoOperation = false;
  3752. if (!RootTempDir.IsEmpty())
  3753. {
  3754. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3755. }
  3756. }
  3757. }
  3758. else
  3759. {
  3760. RemoteDirView->SaveSelection();
  3761. RemoteDirView->SaveSelectedNames();
  3762. try
  3763. {
  3764. if (Move)
  3765. {
  3766. Configuration->Usage->Inc("RemoteMove");
  3767. Terminal->MoveFiles(FileList, Target, FileMask);
  3768. }
  3769. else
  3770. {
  3771. Configuration->Usage->Inc("RemoteCopyDirect");
  3772. DebugAssert(DirectCopy);
  3773. DebugAssert(Session == FTerminal);
  3774. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3775. FTerminal->CommandSessionOpened ||
  3776. CommandSessionFallback())
  3777. {
  3778. Terminal->CopyFiles(FileList, Target, FileMask);
  3779. }
  3780. }
  3781. }
  3782. catch(...)
  3783. {
  3784. RemoteDirView->DiscardSavedSelection();
  3785. throw;
  3786. }
  3787. RemoteDirView->RestoreSelection();
  3788. }
  3789. }
  3790. return Result;
  3791. }
  3792. //---------------------------------------------------------------------------
  3793. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3794. const UnicodeString & Path, TRemoteProperties & Properties)
  3795. {
  3796. Properties.Valid = Properties.Valid << vpEncrypt;
  3797. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3798. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3799. }
  3800. //---------------------------------------------------------------------------
  3801. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3802. {
  3803. Side = GetSide(Side);
  3804. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3805. TRemoteProperties * AProperties = (Side == osRemote ? &Properties : NULL);
  3806. UnicodeString Name = LoadStr(NEW_FOLDER);
  3807. int AllowedChanges =
  3808. FLAGMASK((Side == osRemote) && Terminal->IsCapable[fcModeChanging], cpMode);
  3809. bool SaveSettings = false;
  3810. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3811. {
  3812. TWindowLock Lock(this);
  3813. if (Side == osRemote)
  3814. {
  3815. if (SaveSettings)
  3816. {
  3817. GUIConfiguration->NewDirectoryProperties = Properties;
  3818. }
  3819. CreateRemoteDirectory(Name, Properties);
  3820. }
  3821. else
  3822. {
  3823. DirView(Side)->CreateDirectory(Name);
  3824. }
  3825. }
  3826. }
  3827. //---------------------------------------------------------------------------
  3828. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3829. {
  3830. TWindowLock Lock(this);
  3831. DirView(Side)->ExecuteHomeDirectory();
  3832. }
  3833. //---------------------------------------------------------------------------
  3834. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3835. {
  3836. DoOpenDirectoryDialog(odBrowse, Side);
  3837. }
  3838. //---------------------------------------------------------------------------
  3839. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3840. {
  3841. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3842. bool Result = !Path.IsEmpty();
  3843. if (Result)
  3844. {
  3845. // While we might get here when the session is closed (from location profiles),
  3846. // it's not a problem as the Path setter is noop then
  3847. DirView(Side)->Path = Path;
  3848. }
  3849. return Result;
  3850. }
  3851. //---------------------------------------------------------------------------
  3852. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3853. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3854. bool & Close)
  3855. {
  3856. // terminal can be already closed (e.g. dropped connection)
  3857. if (Terminal != NULL)
  3858. {
  3859. try
  3860. {
  3861. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3862. }
  3863. catch(...)
  3864. {
  3865. if (!Terminal->Active)
  3866. {
  3867. Close = true;
  3868. }
  3869. throw;
  3870. }
  3871. }
  3872. }
  3873. //---------------------------------------------------------------------------
  3874. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3875. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3876. bool & Close)
  3877. {
  3878. // terminal can be already closed (e.g. dropped connection)
  3879. if (Terminal != NULL)
  3880. {
  3881. Configuration->Usage->Inc(L"ChecksumCalculated");
  3882. try
  3883. {
  3884. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3885. }
  3886. catch(...)
  3887. {
  3888. if (!Terminal->Active)
  3889. {
  3890. Close = true;
  3891. }
  3892. throw;
  3893. }
  3894. }
  3895. }
  3896. //---------------------------------------------------------------------------
  3897. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3898. {
  3899. bool Result;
  3900. if (Side == osRemote)
  3901. {
  3902. TRemoteTokenList * GroupList = NULL;
  3903. TRemoteTokenList * UserList = NULL;
  3904. try
  3905. {
  3906. TRemoteProperties CurrentProperties;
  3907. if (Terminal->LoadFilesProperties(FileList))
  3908. {
  3909. RemoteDirView->Invalidate();
  3910. }
  3911. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  3912. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  3913. if (CapableGroupChanging || CapableOwnerChanging)
  3914. {
  3915. if (CapableGroupChanging)
  3916. {
  3917. GroupList = Terminal->Groups->Duplicate();
  3918. }
  3919. if (CapableOwnerChanging)
  3920. {
  3921. UserList = Terminal->Users->Duplicate();
  3922. }
  3923. TRemoteDirectory * Files = Terminal->Files;
  3924. int Count = Files->Count;
  3925. if (Count > 100)
  3926. {
  3927. Count = 100;
  3928. }
  3929. for (int Index = 0; Index < Count; Index++)
  3930. {
  3931. TRemoteFile * File = Files->Files[Index];
  3932. if (CapableGroupChanging)
  3933. {
  3934. GroupList->AddUnique(File->Group);
  3935. }
  3936. if (CapableOwnerChanging)
  3937. {
  3938. UserList->AddUnique(File->Owner);
  3939. }
  3940. }
  3941. // if we haven't collected tokens for all files in current directory,
  3942. // make sure we collect them at least for all selected files.
  3943. // (note that so far the files in FileList has to be from current direcotry)
  3944. if (Count < Files->Count)
  3945. {
  3946. for (int Index = 0; Index < FileList->Count; Index++)
  3947. {
  3948. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  3949. if (CapableGroupChanging)
  3950. {
  3951. GroupList->AddUnique(File->Group);
  3952. }
  3953. if (CapableOwnerChanging)
  3954. {
  3955. UserList->AddUnique(File->Owner);
  3956. }
  3957. }
  3958. }
  3959. }
  3960. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  3961. int Flags = 0;
  3962. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  3963. if (CapableOwnerChanging) Flags |= cpOwner;
  3964. if (CapableGroupChanging) Flags |= cpGroup;
  3965. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  3966. if (Terminal->IsCapable[fcCalculatingChecksum])
  3967. {
  3968. CalculateChecksumEvent = CalculateChecksum;
  3969. }
  3970. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  3971. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  3972. TRemoteProperties NewProperties = CurrentProperties;
  3973. Result =
  3974. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  3975. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  3976. Terminal->IsCapable[fcGroupOwnerChangingByID],
  3977. CalculateSize, CalculateChecksumEvent);
  3978. if (Result)
  3979. {
  3980. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  3981. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  3982. }
  3983. }
  3984. __finally
  3985. {
  3986. delete GroupList;
  3987. delete UserList;
  3988. }
  3989. }
  3990. else
  3991. {
  3992. DirView(Side)->DisplayPropertiesMenu();
  3993. Result = true;
  3994. }
  3995. return Result;
  3996. }
  3997. //---------------------------------------------------------------------------
  3998. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  3999. {
  4000. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4001. {
  4002. FIgnoreNextDialogChar = Key;
  4003. }
  4004. Key = 0;
  4005. }
  4006. //---------------------------------------------------------------------------
  4007. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4008. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4009. {
  4010. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4011. if (Command != NULL)
  4012. {
  4013. KeyProcessed(Key, Shift);
  4014. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4015. {
  4016. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4017. false, false, const_cast<TCustomCommandType *>(Command));
  4018. }
  4019. }
  4020. }
  4021. //---------------------------------------------------------------------------
  4022. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4023. {
  4024. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4025. {
  4026. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4027. }
  4028. if (!DirView(osCurrent)->IsEditing())
  4029. {
  4030. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4031. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4032. {
  4033. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4034. if (((Action->ShortCut == KeyShortCut) ||
  4035. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4036. AllowedAction(Action, aaShortCut))
  4037. {
  4038. // Has to be called before the action as the dialog char is already in queue.
  4039. // So when the action consumes message queue, we already need to have the
  4040. // FIgnoreNextDialogChar set
  4041. KeyProcessed(Key, Shift);
  4042. // Reset reference to previous component (when menu/toolbar was clicked).
  4043. // Needed to detect that action was invoked by keyboard shortcut
  4044. // in TNonVisualDataModule::ExplorerActionsExecute
  4045. Action->ActionComponent = NULL;
  4046. Action->Execute();
  4047. return;
  4048. }
  4049. }
  4050. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4051. {
  4052. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4053. {
  4054. KeyProcessed(Key, Shift);
  4055. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4056. return;
  4057. }
  4058. }
  4059. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4060. {
  4061. KeyProcessed(Key, Shift);
  4062. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4063. }
  4064. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4065. if (IsCustomShortCut(CustomShortCut))
  4066. {
  4067. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4068. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4069. if (WinConfiguration->SharedBookmarks != NULL)
  4070. {
  4071. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4072. if ((Bookmark != NULL) &&
  4073. OpenBookmark(FCurrentSide, Bookmark))
  4074. {
  4075. KeyProcessed(Key, Shift);
  4076. }
  4077. }
  4078. }
  4079. }
  4080. TForm::KeyDown(Key, Shift);
  4081. }
  4082. //---------------------------------------------------------------------------
  4083. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4084. {
  4085. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4086. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4087. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4088. DebugAssert(Terminal);
  4089. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4090. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4091. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4092. // expanded from ?: to avoid memory leaks
  4093. if (SecurityEnabled)
  4094. {
  4095. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4096. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4097. }
  4098. else
  4099. {
  4100. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4101. }
  4102. if (FileSystemInfo.ProtocolName.IsEmpty())
  4103. {
  4104. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4105. }
  4106. else
  4107. {
  4108. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4109. }
  4110. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4111. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4112. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4113. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4114. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4115. UnicodeString CompressionStr = CSCompressionStr;
  4116. if (CSCompressionStr != SCCompressionStr)
  4117. {
  4118. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4119. }
  4120. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4121. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4122. UpdateStatusBar();
  4123. }
  4124. //---------------------------------------------------------------------------
  4125. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4126. {
  4127. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4128. DebugAssert(SessionStatusBar != NULL);
  4129. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4130. {
  4131. // note: (Terminal->Status < sshReady) currently never happens here,
  4132. // so STATUS_CONNECTING is never used
  4133. SessionStatusBar->SimplePanel = true;
  4134. SessionStatusBar->SimpleText = LoadStr(
  4135. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4136. }
  4137. else
  4138. {
  4139. DebugAssert(Terminal);
  4140. SessionStatusBar->SimplePanel = false;
  4141. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4142. if (!FNote.IsEmpty())
  4143. {
  4144. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4145. }
  4146. else
  4147. {
  4148. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4149. }
  4150. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4151. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4152. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4153. }
  4154. }
  4155. //---------------------------------------------------------------------------
  4156. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4157. {
  4158. Panel->Caption = L"";
  4159. }
  4160. //---------------------------------------------------------------------------
  4161. void __fastcall TCustomScpExplorerForm::Idle()
  4162. {
  4163. if (FShowing || FStandaloneEditing)
  4164. {
  4165. FEditorManager->Check();
  4166. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4167. // that needs to know if queue view is visible (and it may be closed after queue update)
  4168. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4169. }
  4170. if (FShowing)
  4171. {
  4172. if (!NonVisualDataModule->Busy &&
  4173. // Menu is opened or drag&drop is going on
  4174. (Mouse->Capture == NULL))
  4175. {
  4176. if (FRefreshRemoteDirectory)
  4177. {
  4178. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4179. {
  4180. Terminal->RefreshDirectory();
  4181. }
  4182. FRefreshRemoteDirectory = false;
  4183. }
  4184. if (FRefreshLocalDirectory)
  4185. {
  4186. ReloadLocalDirectory();
  4187. FRefreshLocalDirectory = false;
  4188. }
  4189. if (WinConfiguration->RefreshRemotePanel)
  4190. {
  4191. TManagedTerminal * ManagedTerminal =
  4192. dynamic_cast<TManagedTerminal *>(Terminal);
  4193. if ((ManagedTerminal != NULL) && (Terminal->Status == ssOpened) &&
  4194. (Now() - ManagedTerminal->DirectoryLoaded >
  4195. WinConfiguration->RefreshRemotePanelInterval))
  4196. {
  4197. RemoteDirView->ReloadDirectory();
  4198. }
  4199. }
  4200. }
  4201. }
  4202. if (FShowing || FStandaloneEditing)
  4203. {
  4204. if (FQueueStatusInvalidated)
  4205. {
  4206. UpdateQueueStatus(false);
  4207. }
  4208. RefreshQueueItems();
  4209. }
  4210. if (FShowing)
  4211. {
  4212. UpdateStatusBar();
  4213. }
  4214. FIgnoreNextDialogChar = 0;
  4215. }
  4216. //---------------------------------------------------------------------------
  4217. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4218. {
  4219. try
  4220. {
  4221. FUserActionTimer->Enabled = false;
  4222. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4223. {
  4224. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4225. {
  4226. FPendingQueueActionItem->ProcessUserAction();
  4227. }
  4228. }
  4229. }
  4230. __finally
  4231. {
  4232. FPendingQueueActionItem = NULL;
  4233. }
  4234. }
  4235. //---------------------------------------------------------------------------
  4236. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4237. {
  4238. if (WinConfiguration->MinimizeToTray)
  4239. {
  4240. UpdateTrayIcon();
  4241. FTrayIcon->Visible = true;
  4242. if (Visible)
  4243. {
  4244. ShowWindow(Handle, SW_HIDE);
  4245. }
  4246. }
  4247. }
  4248. //---------------------------------------------------------------------------
  4249. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4250. {
  4251. // WORKAROUND
  4252. // When restoring maximized window from minimization,
  4253. // rarely some controls do not align properly.
  4254. // Two instances seen (both for Commander):
  4255. // - When restoring, window is temporarily narrower (not maximized),
  4256. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4257. // Once maximized already, top dock shinks back, but the session PageControl,
  4258. // do not align up, leaving space between TopDock and PageControl.
  4259. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4260. for (int Index = 0; Index < ControlCount; Index++)
  4261. {
  4262. RealignControl(Controls[Index]);
  4263. }
  4264. if (FTrayIcon->Visible)
  4265. {
  4266. FTrayIcon->Visible = false;
  4267. if (Visible)
  4268. {
  4269. ShowWindow(Handle, SW_SHOW);
  4270. }
  4271. }
  4272. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4273. {
  4274. FNeedSession = false;
  4275. NonVisualDataModule->StartBusy();
  4276. try
  4277. {
  4278. NeedSession(false);
  4279. }
  4280. __finally
  4281. {
  4282. NonVisualDataModule->EndBusy();
  4283. }
  4284. }
  4285. }
  4286. //---------------------------------------------------------------------------
  4287. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4288. {
  4289. FTrayIcon->Hint = GetMainForm()->Caption;
  4290. }
  4291. //---------------------------------------------------------------------------
  4292. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4293. {
  4294. UpdateTrayIcon();
  4295. }
  4296. //---------------------------------------------------------------------------
  4297. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4298. {
  4299. // workaround
  4300. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4301. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4302. // But WM_ACTIVATEAPP message can be received even when
  4303. // the main window is minimized to the tray and internal editor window is focused
  4304. // (after another application was previously active)
  4305. if (::IsIconic(Handle))
  4306. {
  4307. if (!IsAppIconic())
  4308. {
  4309. SetAppIconic(true);
  4310. }
  4311. }
  4312. ::ApplicationRestore();
  4313. Application->BringToFront();
  4314. }
  4315. //---------------------------------------------------------------------------
  4316. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4317. {
  4318. RestoreApp();
  4319. }
  4320. //---------------------------------------------------------------------------
  4321. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4322. {
  4323. if (OpenInNewWindow())
  4324. {
  4325. // todo: Pass FromSite
  4326. ExecuteNewInstance(SessionUrl);
  4327. }
  4328. else
  4329. {
  4330. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4331. }
  4332. }
  4333. //---------------------------------------------------------------------------
  4334. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4335. {
  4336. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4337. // current working directories become defaults here, what is not right
  4338. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4339. StoredSessions->NewSession(SessionName, SessionData.get());
  4340. // modified only, explicit
  4341. StoredSessions->Save(false, true);
  4342. // encode session name because of slashes in hierarchical sessions
  4343. return EncodeUrlString(SessionName);
  4344. }
  4345. //---------------------------------------------------------------------------
  4346. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4347. {
  4348. if (OpenInNewWindow())
  4349. {
  4350. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4351. }
  4352. else
  4353. {
  4354. // current working directories become defaults here, what is not right
  4355. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4356. TTerminalManager * Manager = TTerminalManager::Instance();
  4357. TTerminal * Terminal = Manager->NewTerminal(SessionData.get());
  4358. Manager->ActiveTerminal = Terminal;
  4359. }
  4360. }
  4361. //---------------------------------------------------------------------------
  4362. void __fastcall TCustomScpExplorerForm::RenameSession()
  4363. {
  4364. UnicodeString Name = Terminal->SessionData->Name;
  4365. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4366. {
  4367. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4368. TSessionData::ValidateName(Name);
  4369. // This is inconsistent with how color (for example) is handled.
  4370. Terminal->SessionData->Name = Name;
  4371. UpdateControls();
  4372. // Add/Remove distinguishing paths from sessions of the same name.
  4373. DoTerminalListChanged();
  4374. }
  4375. }
  4376. //---------------------------------------------------------------------------
  4377. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4378. {
  4379. DebugAssert(Queue != NULL);
  4380. bool Result = Queue->IsEmpty;
  4381. if (!Result)
  4382. {
  4383. SetFocus();
  4384. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4385. }
  4386. return Result;
  4387. }
  4388. //---------------------------------------------------------------------------
  4389. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4390. {
  4391. return CanCloseQueue(FQueue);
  4392. }
  4393. //---------------------------------------------------------------------------
  4394. void __fastcall TCustomScpExplorerForm::CloseSession()
  4395. {
  4396. if (CanCloseQueue())
  4397. {
  4398. TTerminalManager::Instance()->FreeActiveTerminal();
  4399. }
  4400. }
  4401. //---------------------------------------------------------------------------
  4402. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4403. {
  4404. if (OpenInNewWindow())
  4405. {
  4406. // encode session name because of slashes in hierarchical sessions
  4407. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4408. }
  4409. else
  4410. {
  4411. TTerminalManager * Manager = TTerminalManager::Instance();
  4412. TTerminal * Terminal = Manager->NewTerminal(Data);
  4413. Manager->ActiveTerminal = Terminal;
  4414. }
  4415. }
  4416. //---------------------------------------------------------------------------
  4417. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4418. {
  4419. if (OpenInNewWindow())
  4420. {
  4421. ExecuteNewInstance(Name);
  4422. }
  4423. else
  4424. {
  4425. TTerminalManager * Manager = TTerminalManager::Instance();
  4426. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4427. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4428. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  4429. }
  4430. }
  4431. //---------------------------------------------------------------------------
  4432. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4433. bool &CanClose)
  4434. {
  4435. if (Terminal != NULL)
  4436. {
  4437. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4438. {
  4439. unsigned int Result;
  4440. TMessageParams Params(mpNeverAskAgainCheck);
  4441. UnicodeString Message;
  4442. int Answers = qaOK | qaCancel;
  4443. if (TTerminalManager::Instance()->Count > 1)
  4444. {
  4445. if (!WinConfiguration->AutoSaveWorkspace)
  4446. {
  4447. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE2);
  4448. Answers = qaYes | qaNo | qaCancel;
  4449. }
  4450. else
  4451. {
  4452. Message = MainInstructions(LoadStr(CLOSE_SESSIONS));
  4453. }
  4454. }
  4455. else
  4456. {
  4457. Message = MainInstructions(FMTLOAD(CLOSE_SESSION, (Terminal->SessionData->SessionName)));
  4458. }
  4459. if (WinConfiguration->AutoSaveWorkspace)
  4460. {
  4461. Message =
  4462. FORMAT("%s\n\n%s", (Message,
  4463. FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()))));
  4464. }
  4465. SetFocus();
  4466. Result = MessageDialog(Message, qtConfirmation,
  4467. Answers, HELP_NONE, &Params);
  4468. if (Result == qaNeverAskAgain)
  4469. {
  4470. WinConfiguration->ConfirmClosingSession = false;
  4471. }
  4472. if (Result == qaNo)
  4473. {
  4474. CanClose = SaveWorkspace(true);
  4475. // note that the workspace will be saved redundatly again from FormClose
  4476. }
  4477. else
  4478. {
  4479. CanClose =
  4480. (Result == qaOK) ||
  4481. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4482. (Result == qaNeverAskAgain);
  4483. }
  4484. }
  4485. if (CanClose)
  4486. {
  4487. CanClose = CanCloseQueue();
  4488. }
  4489. }
  4490. if (CanClose)
  4491. {
  4492. CanClose =
  4493. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4494. FEditorManager->CloseExternalFilesWithoutProcess();
  4495. if (CanClose)
  4496. {
  4497. while (CanClose && (FLocalEditors->Count > 0))
  4498. {
  4499. int PrevCount = FLocalEditors->Count;
  4500. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4501. CanClose = (FLocalEditors->Count < PrevCount);
  4502. }
  4503. if (CanClose)
  4504. {
  4505. CanClose = FEditorManager->Empty(true);
  4506. if (!CanClose)
  4507. {
  4508. SetFocus();
  4509. CanClose =
  4510. (MessageDialog(
  4511. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4512. }
  4513. }
  4514. }
  4515. }
  4516. }
  4517. //---------------------------------------------------------------------------
  4518. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4519. {
  4520. TForm * Form = dynamic_cast<TForm *>(Sender);
  4521. DebugAssert(Form != NULL);
  4522. Form->Close();
  4523. }
  4524. //---------------------------------------------------------------------------
  4525. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4526. {
  4527. TForm * Form = dynamic_cast<TForm *>(Sender);
  4528. delete Form;
  4529. }
  4530. //---------------------------------------------------------------------------
  4531. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4532. {
  4533. while (FLocalEditors->Count > 0)
  4534. {
  4535. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4536. }
  4537. }
  4538. //---------------------------------------------------------------------------
  4539. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4540. TObject *Sender)
  4541. {
  4542. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4543. try
  4544. {
  4545. SetProperties(osRemote, FileList);
  4546. }
  4547. __finally
  4548. {
  4549. delete FileList;
  4550. }
  4551. }
  4552. //---------------------------------------------------------------------------
  4553. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4554. {
  4555. if (value)
  4556. {
  4557. if (Component == fcCustomCommandsBand)
  4558. {
  4559. UpdateCustomCommandsToolbar();
  4560. }
  4561. }
  4562. }
  4563. //---------------------------------------------------------------------------
  4564. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4565. {
  4566. TControl * Control = GetComponent(Component);
  4567. DebugAssert(Control);
  4568. bool Changed = (Control->Visible != value);
  4569. if (Changed)
  4570. {
  4571. ComponentShowing(Component, value);
  4572. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4573. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4574. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4575. if (value)
  4576. {
  4577. int RemainingHeight = Control->Parent->ClientHeight;
  4578. int RemainingWidth = Control->Parent->ClientWidth;
  4579. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4580. {
  4581. TControl * ChildControl = Control->Parent->Controls[i];
  4582. if (ChildControl->Visible)
  4583. {
  4584. switch (ChildControl->Align)
  4585. {
  4586. case alTop:
  4587. case alBottom:
  4588. RemainingHeight -= ChildControl->Height;
  4589. break;
  4590. case alLeft:
  4591. case alRight:
  4592. RemainingWidth -= ChildControl->Width;
  4593. break;
  4594. }
  4595. }
  4596. }
  4597. int Reserve = ScaleByTextHeight(this, 32);
  4598. // queue in explorer, trees in commander
  4599. if (Control->Height > RemainingHeight - Reserve)
  4600. {
  4601. Control->Height = RemainingHeight / 2;
  4602. }
  4603. if (Control->Width > RemainingWidth - Reserve)
  4604. {
  4605. Control->Width = RemainingWidth / 2;
  4606. }
  4607. }
  4608. Control->Visible = value;
  4609. if (WasFocused && Visible)
  4610. {
  4611. DirView(osCurrent)->SetFocus();
  4612. }
  4613. FixControlsPlacement();
  4614. }
  4615. }
  4616. //---------------------------------------------------------------------------
  4617. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4618. {
  4619. TControl * Control = GetComponent(Component);
  4620. if (Control == NULL)
  4621. {
  4622. return false;
  4623. }
  4624. else
  4625. {
  4626. return Control->Visible;
  4627. }
  4628. }
  4629. //---------------------------------------------------------------------------
  4630. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4631. {
  4632. bool Result;
  4633. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4634. {
  4635. Result = !IsUWP();
  4636. }
  4637. else
  4638. {
  4639. Result = true;
  4640. }
  4641. return Result;
  4642. }
  4643. //---------------------------------------------------------------------------
  4644. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4645. {
  4646. if (RemoteDirView->ItemFocused != NULL)
  4647. {
  4648. RemoteDirView->ItemFocused->MakeVisible(false);
  4649. }
  4650. QueueSplitter->Visible = QueuePanel->Visible;
  4651. RemotePanelSplitter->Visible = RemoteDriveView->Visible;
  4652. }
  4653. //---------------------------------------------------------------------------
  4654. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4655. {
  4656. switch (Component) {
  4657. case fcStatusBar: return RemoteStatusBar;
  4658. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4659. case fcQueueView: return QueuePanel;
  4660. case fcQueueToolbar: return QueueDock;
  4661. case fcRemoteTree: return RemoteDriveView;
  4662. case fcSessionsTabs: return SessionsPageControl;
  4663. default: return NULL;
  4664. }
  4665. }
  4666. //---------------------------------------------------------------------------
  4667. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4668. TObject *Sender, TListColumn *Column, TPoint &Point)
  4669. {
  4670. DebugAssert(NonVisualDataModule && Column && Sender);
  4671. NonVisualDataModule->ListColumn = Column;
  4672. TPopupMenu * DirViewColumnMenu;
  4673. if (Sender == RemoteDirView)
  4674. {
  4675. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4676. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4677. (Column->Index == uvName);
  4678. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4679. (Column->Index == uvSize);
  4680. }
  4681. else
  4682. {
  4683. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4684. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4685. (Column->Index == dvName);
  4686. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4687. (Column->Index == dvSize);
  4688. }
  4689. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4690. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4691. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4692. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4693. }
  4694. //---------------------------------------------------------------------------
  4695. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4696. TObject *Sender, TListItem *Item, bool &AllowExec)
  4697. {
  4698. DoDirViewExecFile(Sender, Item, AllowExec);
  4699. }
  4700. //---------------------------------------------------------------------------
  4701. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4702. TListItem * Item, bool & AllowExec)
  4703. {
  4704. DebugAssert(Sender && Item && Configuration);
  4705. DebugAssert(AllowExec);
  4706. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4707. bool Remote = (ADirView == DirView(osRemote));
  4708. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4709. TOperationSide Side = (Remote ? osRemote : osLocal);
  4710. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4711. // on files only (not directories)
  4712. // and only when symlinks are resolved (apply to remote panel only)
  4713. if (!ADirView->ItemIsDirectory(Item) &&
  4714. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4715. {
  4716. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4717. !FForceExecution &&
  4718. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4719. {
  4720. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4721. {
  4722. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4723. UnlockWindow();
  4724. try
  4725. {
  4726. ExecuteFileOperation(
  4727. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4728. }
  4729. __finally
  4730. {
  4731. LockWindow();
  4732. }
  4733. AllowExec = false;
  4734. }
  4735. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4736. {
  4737. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4738. {
  4739. ExecuteFile(Side, efDefaultEditor);
  4740. AllowExec = false;
  4741. }
  4742. }
  4743. else
  4744. {
  4745. DebugFail();
  4746. }
  4747. }
  4748. // if we have not done anything special, fall back to default behavior
  4749. if (AllowExec)
  4750. {
  4751. if (Remote && !WinConfiguration->DisableOpenEdit)
  4752. {
  4753. ExecuteFile(osRemote, efShell);
  4754. AllowExec = false;
  4755. }
  4756. }
  4757. }
  4758. }
  4759. //---------------------------------------------------------------------------
  4760. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4761. {
  4762. return ((Side == osRemote) || (Side == osCurrent));
  4763. }
  4764. //---------------------------------------------------------------------------
  4765. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4766. {
  4767. DebugFail();
  4768. }
  4769. //---------------------------------------------------------------------------
  4770. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4771. {
  4772. DebugFail();
  4773. }
  4774. //---------------------------------------------------------------------------
  4775. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4776. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, bool UseDefaults)
  4777. {
  4778. TSynchronizeParamType Params;
  4779. Params.LocalDirectory = LocalDirectory;
  4780. Params.RemoteDirectory = RemoteDirectory;
  4781. int UnusedParams =
  4782. (GUIConfiguration->SynchronizeParams &
  4783. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4784. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4785. Params.Options = GUIConfiguration->SynchronizeOptions;
  4786. bool SaveSettings = false;
  4787. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4788. &DoSynchronizeTooManyDirectories);
  4789. DebugAssert(FSynchronizeController == NULL);
  4790. FSynchronizeController = &Controller;
  4791. bool Result;
  4792. try
  4793. {
  4794. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4795. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4796. int Options =
  4797. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4798. DebugAssert(FOnFeedSynchronizeError == NULL);
  4799. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4800. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4801. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4802. if (Result)
  4803. {
  4804. if (SaveSettings)
  4805. {
  4806. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4807. GUIConfiguration->SynchronizeOptions = Params.Options;
  4808. }
  4809. else
  4810. {
  4811. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4812. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4813. FLAGSET(Params.Options, soSynchronize))
  4814. {
  4815. GUIConfiguration->SynchronizeOptions =
  4816. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4817. soSynchronize;
  4818. }
  4819. }
  4820. LocalDirectory = Params.LocalDirectory;
  4821. RemoteDirectory = Params.RemoteDirectory;
  4822. }
  4823. }
  4824. __finally
  4825. {
  4826. FSynchronizeController = NULL;
  4827. DebugAssert(FOnFeedSynchronizeError == NULL);
  4828. FOnFeedSynchronizeError = NULL;
  4829. }
  4830. return Result;
  4831. }
  4832. //---------------------------------------------------------------------------
  4833. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4834. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4835. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4836. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4837. TSynchronizeOptions * Options, bool Full)
  4838. {
  4839. // Keep-up-to-date mode
  4840. if (Terminal->Status == ssOpened)
  4841. {
  4842. try
  4843. {
  4844. int PParams = Params.Params;
  4845. if (!Full)
  4846. {
  4847. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4848. if (FLAGSET(Params.Options, soRecurse))
  4849. {
  4850. PParams |= TTerminal::spSubDirs;
  4851. }
  4852. }
  4853. else
  4854. {
  4855. // if keepuptodate is non-recursive,
  4856. // full sync before has to be non-recursive as well
  4857. if (FLAGCLEAR(Params.Options, soRecurse))
  4858. {
  4859. PParams |= TTerminal::spNoRecurse;
  4860. }
  4861. }
  4862. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  4863. PParams, Checklist, Options);
  4864. }
  4865. catch (Exception & E)
  4866. {
  4867. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  4868. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  4869. {
  4870. // noop, already logged in MoreMessageDialog
  4871. }
  4872. else
  4873. {
  4874. ShowExtendedExceptionEx(Terminal, &E);
  4875. throw;
  4876. }
  4877. }
  4878. }
  4879. }
  4880. //---------------------------------------------------------------------------
  4881. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  4882. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  4883. const UnicodeString ErrorStr)
  4884. {
  4885. if (!Directory.IsEmpty())
  4886. {
  4887. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  4888. }
  4889. else
  4890. {
  4891. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  4892. }
  4893. }
  4894. //---------------------------------------------------------------------------
  4895. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  4896. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  4897. {
  4898. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  4899. {
  4900. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  4901. }
  4902. else
  4903. {
  4904. TMessageParams Params(mpNeverAskAgainCheck);
  4905. unsigned int Result = MessageDialog(
  4906. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  4907. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  4908. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  4909. {
  4910. MaxDirectories *= 2;
  4911. if (Result == qaNeverAskAgain)
  4912. {
  4913. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  4914. }
  4915. }
  4916. else
  4917. {
  4918. Abort();
  4919. }
  4920. }
  4921. }
  4922. //---------------------------------------------------------------------------
  4923. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  4924. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  4925. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  4926. TSynchronizeOptions * Options)
  4927. {
  4928. // Keep-up-to-date mode
  4929. DebugAssert(!FAutoOperation);
  4930. void * BatchStorage;
  4931. BatchStart(BatchStorage);
  4932. FAutoOperation = true;
  4933. bool AnyOperation = false;
  4934. TDateTime StartTime = Now();
  4935. TSynchronizeChecklist * AChecklist = NULL;
  4936. try
  4937. {
  4938. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  4939. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  4940. {
  4941. FSynchronizeProgressForm->Start();
  4942. }
  4943. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  4944. static_cast<TTerminal::TSynchronizeMode>(Mode),
  4945. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4946. Options);
  4947. SAFE_DESTROY(FSynchronizeProgressForm);
  4948. AnyOperation = (AChecklist->CheckedCount > 0);
  4949. if (AnyOperation)
  4950. {
  4951. TSynchronizeProgress SynchronizeProgress(AChecklist);
  4952. CreateProgressForm(&SynchronizeProgress);
  4953. try
  4954. {
  4955. Terminal->SynchronizeApply(
  4956. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4957. SynchronizeProcessedItem, NULL, NULL, NULL);
  4958. }
  4959. __finally
  4960. {
  4961. DestroyProgressForm();
  4962. }
  4963. }
  4964. }
  4965. __finally
  4966. {
  4967. if (Checklist == NULL)
  4968. {
  4969. delete AChecklist;
  4970. }
  4971. else
  4972. {
  4973. *Checklist = AChecklist;
  4974. }
  4975. FAutoOperation = false;
  4976. SAFE_DESTROY(FSynchronizeProgressForm);
  4977. BatchEnd(BatchStorage);
  4978. ReloadLocalDirectory();
  4979. if (AnyOperation)
  4980. {
  4981. OperationComplete(StartTime);
  4982. }
  4983. }
  4984. }
  4985. //---------------------------------------------------------------------------
  4986. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  4987. {
  4988. // can be called from command line
  4989. return Visible &&
  4990. ((DirView(osRemote)->SelCount > 0) ||
  4991. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  4992. }
  4993. //---------------------------------------------------------------------------
  4994. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  4995. {
  4996. LogSynchronizeEvent(Terminal, Message);
  4997. }
  4998. //---------------------------------------------------------------------------
  4999. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5000. int Params, TSynchronizeOptions & Options)
  5001. {
  5002. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5003. {
  5004. Options.Filter = new TStringList();
  5005. Options.Filter->CaseSensitive = false;
  5006. Options.Filter->Duplicates = Types::dupAccept;
  5007. if (DirView(osRemote)->SelCount > 0)
  5008. {
  5009. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5010. }
  5011. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5012. {
  5013. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5014. }
  5015. Options.Filter->Sort();
  5016. }
  5017. }
  5018. //---------------------------------------------------------------------------
  5019. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5020. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5021. {
  5022. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5023. UnicodeString AdditionalParams =
  5024. FORMAT(L"%s \"%s\" \"%s\" %d %d %s", (
  5025. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5026. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  5027. TProgramParams::FormatSwitch(DEFAULTS_SWITCH)));
  5028. TCopyParamType Defaults;
  5029. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5030. ConfigStorage->AccessMode = smRead;
  5031. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false, false))
  5032. {
  5033. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5034. }
  5035. ConfigStorage.reset(NULL);
  5036. std::unique_ptr<TStringList> Options(new TStringList());
  5037. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5038. GUIConfiguration->SaveCopyParam(OptionsStorage.get(), CopyParams, &Defaults);
  5039. if (Options->Count > 0)
  5040. {
  5041. AdditionalParams +=
  5042. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAW_CONFIG_SWITCH), StringsToParams(Options.get())));
  5043. }
  5044. ExecuteNewInstance(SessionName, AdditionalParams);
  5045. }
  5046. //---------------------------------------------------------------------------
  5047. struct TSynchronizeParams
  5048. {
  5049. UnicodeString LocalDirectory;
  5050. UnicodeString RemoteDirectory;
  5051. TSynchronizeMode Mode;
  5052. TSynchronizeChecklist * Checklist;
  5053. int Params;
  5054. TCopyParamType * CopyParam;
  5055. TDateTime CollectElapsed;
  5056. TDateTime * StartTime;
  5057. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5058. };
  5059. //---------------------------------------------------------------------------
  5060. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5061. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5062. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5063. {
  5064. DebugAssert(!FAutoOperation);
  5065. void * BatchStorage;
  5066. BatchStart(BatchStorage);
  5067. FAutoOperation = true;
  5068. TFileOperationStatistics Statistics;
  5069. TDateTime Start = Now();
  5070. try
  5071. {
  5072. Params.OnProcessedItem = OnProcessedItem;
  5073. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5074. CreateProgressForm(&SynchronizeProgress);
  5075. Terminal->SynchronizeApply(
  5076. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5077. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5078. &Statistics);
  5079. }
  5080. __finally
  5081. {
  5082. Params.OnProcessedItem = NULL;
  5083. FAutoOperation = false;
  5084. DestroyProgressForm();
  5085. BatchEnd(BatchStorage);
  5086. ReloadLocalDirectory();
  5087. }
  5088. if (WinConfiguration->SynchronizeSummary)
  5089. {
  5090. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5091. // The statistics should be 0 anyway in this case
  5092. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5093. {
  5094. Message += L"\n";
  5095. if (Statistics.FilesUploaded > 0)
  5096. {
  5097. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5098. }
  5099. if (Statistics.FilesDownloaded > 0)
  5100. {
  5101. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5102. }
  5103. if (Statistics.FilesDeletedLocal > 0)
  5104. {
  5105. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5106. }
  5107. if (Statistics.FilesDeletedRemote > 0)
  5108. {
  5109. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5110. }
  5111. }
  5112. TDateTime Elapsed = (Now() - Start);
  5113. Message +=
  5114. L"\n" +
  5115. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5116. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5117. TMessageParams Params(mpNeverAskAgainCheck);
  5118. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5119. if (Result == qaNeverAskAgain)
  5120. {
  5121. WinConfiguration->SynchronizeSummary = false;
  5122. }
  5123. }
  5124. }
  5125. //---------------------------------------------------------------------------
  5126. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5127. {
  5128. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5129. {
  5130. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5131. }
  5132. // Not set in keep-up-to-date mode - Synchronize() method
  5133. if (Token != NULL)
  5134. {
  5135. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5136. if (Params.OnProcessedItem != NULL)
  5137. {
  5138. Params.OnProcessedItem(NULL, ChecklistItem);
  5139. }
  5140. }
  5141. }
  5142. //---------------------------------------------------------------------------
  5143. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5144. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5145. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5146. {
  5147. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5148. *Params.StartTime = Now();
  5149. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5150. }
  5151. //---------------------------------------------------------------------------
  5152. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5153. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5154. {
  5155. // terminal can be already closed (e.g. dropped connection)
  5156. if (Terminal != NULL)
  5157. {
  5158. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5159. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5160. }
  5161. }
  5162. //---------------------------------------------------------------------------
  5163. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5164. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5165. {
  5166. TAutoBatch AutoBatch(this);
  5167. TAutoFlag AutoOperationFlag(FAutoOperation);
  5168. if (Side == osRemote)
  5169. {
  5170. std::unique_ptr<TStrings> FileList(new TStringList());
  5171. FileList->AddObject(FileName, RemoteFile);
  5172. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5173. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5174. RemoteDirView->SaveSelection();
  5175. RemoteDirView->SaveSelectedNames();
  5176. try
  5177. {
  5178. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5179. }
  5180. catch(...)
  5181. {
  5182. RemoteDirView->DiscardSavedSelection();
  5183. throw;
  5184. }
  5185. RemoteDirView->RestoreSelection();
  5186. }
  5187. else if (DebugAlwaysTrue(Side == osLocal))
  5188. {
  5189. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5190. {
  5191. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5192. }
  5193. UnicodeString Directory = ExtractFileDir(FileName);
  5194. ReloadLocalDirectory(Directory);
  5195. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5196. if (!SamePaths(Directory, NewDirectory))
  5197. {
  5198. ReloadLocalDirectory(NewDirectory);
  5199. }
  5200. }
  5201. }
  5202. //---------------------------------------------------------------------------
  5203. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5204. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5205. TSynchronizeMode & Mode, bool & SaveMode, bool UseDefaults)
  5206. {
  5207. int Result;
  5208. int Params = GUIConfiguration->SynchronizeParams;
  5209. bool SaveSettings = false;
  5210. int Options =
  5211. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5212. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5213. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5214. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5215. bool Continue =
  5216. UseDefaults ||
  5217. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5218. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs);
  5219. if (Continue)
  5220. {
  5221. Configuration->Usage->Inc(L"Synchronizations");
  5222. UpdateCopyParamCounters(CopyParam);
  5223. TSynchronizeOptions SynchronizeOptions;
  5224. GetSynchronizeOptions(Params, SynchronizeOptions);
  5225. if (SaveSettings)
  5226. {
  5227. GUIConfiguration->SynchronizeParams = Params;
  5228. }
  5229. else
  5230. {
  5231. SaveMode = false;
  5232. }
  5233. TDateTime StartTime = Now();
  5234. TSynchronizeChecklist * Checklist = NULL;
  5235. try
  5236. {
  5237. DebugAssert(!FAutoOperation);
  5238. FAutoOperation = true;
  5239. try
  5240. {
  5241. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  5242. FSynchronizeProgressForm->Start();
  5243. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5244. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5245. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5246. &SynchronizeOptions);
  5247. }
  5248. __finally
  5249. {
  5250. FAutoOperation = false;
  5251. SAFE_DESTROY(FSynchronizeProgressForm);
  5252. }
  5253. TSynchronizeParams SynchronizeParams;
  5254. SynchronizeParams.LocalDirectory = LocalDirectory;
  5255. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5256. SynchronizeParams.Mode = Mode;
  5257. SynchronizeParams.CopyParam = &CopyParam;
  5258. SynchronizeParams.Params = Params;
  5259. SynchronizeParams.Checklist = Checklist;
  5260. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5261. SynchronizeParams.StartTime = &StartTime;
  5262. SynchronizeParams.OnProcessedItem = NULL;
  5263. Result = Checklist->Count;
  5264. if (Checklist->Count > 0)
  5265. {
  5266. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5267. {
  5268. if (!DoSynchronizeChecklistDialog(
  5269. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5270. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, &SynchronizeParams))
  5271. {
  5272. Result = -1;
  5273. }
  5274. }
  5275. else
  5276. {
  5277. FullSynchronize(SynchronizeParams, NULL, NULL);
  5278. }
  5279. }
  5280. else
  5281. {
  5282. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5283. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5284. }
  5285. }
  5286. __finally
  5287. {
  5288. delete Checklist;
  5289. }
  5290. OperationComplete(StartTime);
  5291. }
  5292. else
  5293. {
  5294. Result = -1;
  5295. }
  5296. return Result;
  5297. }
  5298. //---------------------------------------------------------------------------
  5299. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5300. const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
  5301. bool & Continue, bool Collect)
  5302. {
  5303. if (Collect)
  5304. {
  5305. DebugAssert(FSynchronizeProgressForm != NULL);
  5306. if (!FSynchronizeProgressForm->Started)
  5307. {
  5308. FSynchronizeProgressForm->Start();
  5309. }
  5310. FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Continue);
  5311. }
  5312. else
  5313. {
  5314. DebugAssert(FSynchronizeProgressForm == NULL);
  5315. DebugAssert(FProgressForm != NULL);
  5316. }
  5317. }
  5318. //---------------------------------------------------------------------------
  5319. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5320. {
  5321. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5322. TRemoteFile * File = NULL;
  5323. Terminal->ReadFile(FullFileName, File);
  5324. if (File != NULL)
  5325. {
  5326. std::unique_ptr<TRemoteFile> FileOwner(File);
  5327. TAutoFlag Flag(FStandaloneEditing);
  5328. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5329. Application->ShowMainForm = false;
  5330. Application->Run();
  5331. }
  5332. }
  5333. //---------------------------------------------------------------------------
  5334. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5335. {
  5336. DebugFail();
  5337. }
  5338. //---------------------------------------------------------------------------
  5339. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5340. {
  5341. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5342. SessionData->Assign(Terminal->SessionData);
  5343. UpdateSessionData(SessionData.get());
  5344. TTerminalManager::Instance()->UpdateSessionCredentials(SessionData.get());
  5345. if (!Terminal->SessionData->HasSessionName())
  5346. {
  5347. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5348. // once IsWorkspace is cleared
  5349. SessionData->Name = UnicodeString();
  5350. SessionData->IsWorkspace = false;
  5351. }
  5352. return SessionData.release();
  5353. }
  5354. //---------------------------------------------------------------------------
  5355. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5356. {
  5357. TSessionData * SessionData = CloneCurrentSessionData();
  5358. try
  5359. {
  5360. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5361. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5362. }
  5363. __finally
  5364. {
  5365. delete SessionData;
  5366. }
  5367. }
  5368. //---------------------------------------------------------------------------
  5369. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5370. {
  5371. TTerminalManager * Manager = TTerminalManager::Instance();
  5372. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5373. if (DebugAlwaysTrue(Terminal != NULL))
  5374. {
  5375. // Update (Managed)Terminal->StateData
  5376. UpdateTerminal(Terminal);
  5377. }
  5378. Manager->SaveWorkspace(DataList.get());
  5379. return DataList.release();
  5380. }
  5381. //---------------------------------------------------------------------------
  5382. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5383. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5384. {
  5385. WinConfiguration->LastWorkspace = Name;
  5386. if (!SavePasswords)
  5387. {
  5388. for (int Index = 0; Index < DataList->Count; Index++)
  5389. {
  5390. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5391. if (SessionData->Link.IsEmpty())
  5392. {
  5393. SessionData->ClearSessionPasswords();
  5394. }
  5395. }
  5396. }
  5397. StoredSessions->NewWorkspace(Name, DataList);
  5398. // modified only
  5399. StoredSessions->Save(false, Explicit);
  5400. }
  5401. //---------------------------------------------------------------------------
  5402. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5403. {
  5404. return
  5405. DefaultStr(WinConfiguration->LastWorkspace,
  5406. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5407. }
  5408. //---------------------------------------------------------------------------
  5409. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5410. {
  5411. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5412. bool AnyNonStoredSessionWithPassword = false;
  5413. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5414. bool AllNonStoredSessionsAnonymous = true;
  5415. TTerminalManager * Manager = TTerminalManager::Instance();
  5416. for (int Index = 0; Index < DataList->Count; Index++)
  5417. {
  5418. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5419. if (SessionData->Link.IsEmpty())
  5420. {
  5421. if (SessionData->HasAnySessionPassword())
  5422. {
  5423. AnyNonStoredSessionWithPassword = true;
  5424. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5425. {
  5426. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5427. }
  5428. }
  5429. if (!SameText(SessionData->UserName, AnonymousUserName))
  5430. {
  5431. AllNonStoredSessionsAnonymous = false;
  5432. }
  5433. }
  5434. }
  5435. bool SavePasswords;
  5436. bool * PSavePasswords;
  5437. bool NotRecommendedSavingPasswords =
  5438. !CustomWinConfiguration->UseMasterPassword &&
  5439. !AllNonStoredSessionsAnonymous;
  5440. if (Configuration->DisablePasswordStoring ||
  5441. !AnyNonStoredSessionWithPassword)
  5442. {
  5443. PSavePasswords = NULL;
  5444. SavePasswords = false;
  5445. }
  5446. else
  5447. {
  5448. PSavePasswords = &SavePasswords;
  5449. SavePasswords =
  5450. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5451. !NotRecommendedSavingPasswords;
  5452. }
  5453. UnicodeString Name = WorkspaceName();
  5454. bool CreateShortcut = false;
  5455. bool Result =
  5456. DoSaveWorkspaceDialog(
  5457. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5458. EnableAutoSave);
  5459. if (Result)
  5460. {
  5461. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5462. if (CreateShortcut)
  5463. {
  5464. TOperationVisualizer Visualizer;
  5465. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5466. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5467. }
  5468. if (EnableAutoSave)
  5469. {
  5470. WinConfiguration->AutoSaveWorkspace = true;
  5471. WinConfiguration->AutoWorkspace = Name;
  5472. if (PSavePasswords != NULL)
  5473. {
  5474. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5475. }
  5476. }
  5477. }
  5478. return Result;
  5479. }
  5480. //---------------------------------------------------------------------------
  5481. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TTerminal * Terminal)
  5482. {
  5483. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  5484. DebugAssert(ManagedTerminal != NULL);
  5485. SAFE_DESTROY(ManagedTerminal->RemoteExplorerState);
  5486. if (WinConfiguration->PreservePanelState)
  5487. {
  5488. ManagedTerminal->RemoteExplorerState = RemoteDirView->SaveState();
  5489. }
  5490. UpdateSessionData(ManagedTerminal->StateData);
  5491. }
  5492. //---------------------------------------------------------------------------
  5493. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5494. {
  5495. // Keep in sync with TSessionData::CopyStateData
  5496. DebugAssert(Data != NULL);
  5497. // This is inconsistent with how color (for example) is handled.
  5498. Data->Name = Terminal->SessionData->Name;
  5499. // cannot use RemoteDirView->Path, because it is empty if connection
  5500. // was already closed
  5501. // also only peek, we may not be connected at all atm
  5502. // so make sure we do not try retrieving current directory from the server
  5503. // (particularly with FTP)
  5504. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5505. if (!ACurrentDirectory.IsEmpty())
  5506. {
  5507. Data->RemoteDirectory = ACurrentDirectory;
  5508. }
  5509. Data->Color = SessionColor;
  5510. }
  5511. //---------------------------------------------------------------------------
  5512. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5513. {
  5514. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5515. DebugAssert(Toolbar != NULL);
  5516. for (int i = 0; i < Toolbar->Items->Count; i++)
  5517. {
  5518. TTBXCustomDropDownItem * DropDownItem;
  5519. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5520. if (DropDownItem != NULL)
  5521. {
  5522. ToolbarItemResize(DropDownItem,
  5523. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5524. Toolbar->NonClientWidth);
  5525. break;
  5526. }
  5527. }
  5528. }
  5529. //---------------------------------------------------------------------------
  5530. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5531. {
  5532. Item->EditWidth = Width;
  5533. }
  5534. //---------------------------------------------------------------------------
  5535. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5536. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5537. {
  5538. for (int i = 0; i < Toolbar->Items->Count; i++)
  5539. {
  5540. TTBXCustomDropDownItem * DropDownItem;
  5541. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5542. if (DropDownItem != NULL)
  5543. {
  5544. ASize.x -= DropDownItem->EditWidth;
  5545. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5546. }
  5547. }
  5548. }
  5549. //---------------------------------------------------------------------------
  5550. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5551. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5552. {
  5553. if (WinConfiguration->DDWarnLackOfTempSpace)
  5554. {
  5555. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5556. if (!ADrive.IsEmpty())
  5557. {
  5558. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5559. DebugAssert(RequiredSpace >= 0);
  5560. __int64 RequiredWithReserve;
  5561. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5562. if (FreeSpace < RequiredWithReserve)
  5563. {
  5564. unsigned int Result;
  5565. TMessageParams Params(mpNeverAskAgainCheck);
  5566. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5567. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5568. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5569. if (Result == qaNeverAskAgain)
  5570. {
  5571. WinConfiguration->DDWarnLackOfTempSpace = false;
  5572. }
  5573. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5574. }
  5575. }
  5576. }
  5577. }
  5578. //---------------------------------------------------------------------------
  5579. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5580. {
  5581. DoOpenDirectoryDialog(odAddBookmark, Side);
  5582. }
  5583. //---------------------------------------------------------------------------
  5584. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5585. {
  5586. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5587. TStringList * VisitedDirectories = CreateSortedStringList((Side == osRemote));
  5588. try
  5589. {
  5590. TCustomDirView * DView = DirView(Side);
  5591. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5592. {
  5593. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5594. }
  5595. }
  5596. catch (...)
  5597. {
  5598. delete VisitedDirectories;
  5599. throw;
  5600. }
  5601. return VisitedDirectories;
  5602. }
  5603. //---------------------------------------------------------------------------
  5604. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5605. TOpenDirectoryMode Mode, TOperationSide Side)
  5606. {
  5607. bool Continue = true;
  5608. if (Mode == odAddBookmark)
  5609. {
  5610. TMessageParams Params(mpNeverAskAgainCheck);
  5611. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5612. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5613. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5614. unsigned int Answer =
  5615. MessageDialog(Message,
  5616. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5617. if (Answer == qaNeverAskAgain)
  5618. {
  5619. Continue = true;
  5620. WinConfiguration->UseSharedBookmarks = true;
  5621. }
  5622. else if (Answer == qaOK)
  5623. {
  5624. Continue = true;
  5625. WinConfiguration->UseSharedBookmarks = false;
  5626. }
  5627. else
  5628. {
  5629. Continue = false;
  5630. }
  5631. }
  5632. if (Continue)
  5633. {
  5634. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5635. try
  5636. {
  5637. UnicodeString Name = DirView(Side)->PathName;
  5638. bool Repeat;
  5639. do
  5640. {
  5641. Repeat = false;
  5642. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5643. // do not allow switching to location profiles,
  5644. // if we are not connected
  5645. HasDirView[osLocal] && (Terminal != NULL)))
  5646. {
  5647. TWindowLock Lock(this);
  5648. if (!TryOpenDirectory(Side, Name))
  5649. {
  5650. Repeat = true;
  5651. Mode = odBrowse;
  5652. }
  5653. }
  5654. }
  5655. while (Repeat);
  5656. }
  5657. __finally
  5658. {
  5659. delete VisitedDirectories;
  5660. }
  5661. }
  5662. }
  5663. //---------------------------------------------------------------------------
  5664. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5665. {
  5666. DebugAssert(!FTerminal->CommandSessionOpened);
  5667. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5668. }
  5669. //---------------------------------------------------------------------------
  5670. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5671. {
  5672. bool Result = FTerminal->IsCapable[Capability] ||
  5673. FTerminal->CommandSessionOpened;
  5674. if (!Result)
  5675. {
  5676. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5677. if (!GUIConfiguration->ConfirmCommandSession)
  5678. {
  5679. Result = true;
  5680. }
  5681. else
  5682. {
  5683. TMessageParams Params(mpNeverAskAgainCheck);
  5684. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5685. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5686. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5687. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5688. if (Answer == qaNeverAskAgain)
  5689. {
  5690. GUIConfiguration->ConfirmCommandSession = false;
  5691. Result = true;
  5692. }
  5693. else if (Answer == qaOK)
  5694. {
  5695. Result = true;
  5696. }
  5697. }
  5698. if (Result)
  5699. {
  5700. Result = CommandSessionFallback();
  5701. }
  5702. }
  5703. return Result;
  5704. }
  5705. //---------------------------------------------------------------------------
  5706. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5707. {
  5708. if (EnsureCommandSessionFallback(fcAnyCommand))
  5709. {
  5710. DoConsoleDialog(Terminal, Command);
  5711. }
  5712. }
  5713. //---------------------------------------------------------------------------
  5714. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5715. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5716. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5717. {
  5718. if (IsFileControl(DropSourceControl, osRemote) &&
  5719. (FDDExtMapFile != NULL))
  5720. {
  5721. Accept = true;
  5722. }
  5723. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5724. DebugAssert(FDDTargetControl != NULL);
  5725. }
  5726. //---------------------------------------------------------------------------
  5727. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5728. _di_IDataObject DataObj, int KeyState,
  5729. const TPoint & Point, int & Effect, bool & Accept)
  5730. {
  5731. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5732. }
  5733. //---------------------------------------------------------------------------
  5734. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5735. _di_IDataObject DataObj, int KeyState,
  5736. const TPoint & Point, int & Effect, bool & Accept)
  5737. {
  5738. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5739. }
  5740. //---------------------------------------------------------------------------
  5741. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5742. TObject * Sender)
  5743. {
  5744. DebugUsedParam(Sender);
  5745. DebugAssert(FDDTargetControl == Sender);
  5746. FDDTargetControl = NULL;
  5747. }
  5748. //---------------------------------------------------------------------------
  5749. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5750. {
  5751. FileControlDDDragLeave(SessionsPageControl);
  5752. }
  5753. //---------------------------------------------------------------------------
  5754. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5755. {
  5756. FileControlDDDragLeave(QueueView3);
  5757. }
  5758. //---------------------------------------------------------------------------
  5759. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5760. {
  5761. DebugAssert(GetSide(Side) == osRemote);
  5762. DebugUsedParam(Side);
  5763. bool Edit = false;
  5764. TRemoteFile * File = NULL;
  5765. UnicodeString FileName;
  5766. UnicodeString PointTo;
  5767. bool SymbolicLink = true;
  5768. if (RemoteDirView->ItemFocused)
  5769. {
  5770. DebugAssert(RemoteDirView->ItemFocused->Data);
  5771. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5772. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5773. if (Edit)
  5774. {
  5775. FileName = File->FileName;
  5776. PointTo = File->LinkTo;
  5777. }
  5778. else
  5779. {
  5780. PointTo = File->FileName;
  5781. }
  5782. }
  5783. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5784. Terminal->IsCapable[fcHardLink]))
  5785. {
  5786. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5787. if (Edit)
  5788. {
  5789. DebugAssert(File->FileName == FileName);
  5790. int Params = dfNoRecursive;
  5791. Terminal->ExceptionOnFail = true;
  5792. try
  5793. {
  5794. Terminal->DeleteFile(L"", File, &Params);
  5795. }
  5796. __finally
  5797. {
  5798. Terminal->ExceptionOnFail = false;
  5799. }
  5800. }
  5801. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  5802. }
  5803. }
  5804. //---------------------------------------------------------------------------
  5805. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  5806. {
  5807. return
  5808. (Terminal != NULL) &&
  5809. ((GetSide(Side) != osRemote) ||
  5810. (Terminal->ResolvingSymlinks &&
  5811. Terminal->IsCapable[fcSymbolicLink]));
  5812. }
  5813. //---------------------------------------------------------------------------
  5814. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  5815. {
  5816. return
  5817. (FCurrentSide == osRemote) &&
  5818. (RemoteDirView->ItemFocused != NULL) &&
  5819. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  5820. Terminal->SessionData->ResolveSymlinks;
  5821. }
  5822. //---------------------------------------------------------------------------
  5823. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  5824. {
  5825. DebugAssert(!WinConfiguration->DisableOpenEdit);
  5826. FForceExecution = true;
  5827. try
  5828. {
  5829. DirView(osCurrent)->ExecuteCurrentFile();
  5830. }
  5831. __finally
  5832. {
  5833. FForceExecution = false;
  5834. }
  5835. }
  5836. //---------------------------------------------------------------------------
  5837. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  5838. {
  5839. TEditorData ExternalEditor;
  5840. ExternalEditor.Editor = edExternal;
  5841. bool Remember = false;
  5842. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc,
  5843. (GetSide(osCurrent) == osRemote)))
  5844. {
  5845. if (Remember)
  5846. {
  5847. TEditorList * EditorList = new TEditorList;
  5848. try
  5849. {
  5850. *EditorList = *WinConfiguration->EditorList;
  5851. bool Found = false;
  5852. int i = 0;
  5853. while (!Found && (i < EditorList->Count))
  5854. {
  5855. const TEditorPreferences * Editor = EditorList->Editors[i];
  5856. if ((Editor->Data->Editor == edExternal) &&
  5857. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  5858. {
  5859. Found = true;
  5860. }
  5861. i++;
  5862. }
  5863. if (!Found)
  5864. {
  5865. EditorList->Add(new TEditorPreferences(ExternalEditor));
  5866. WinConfiguration->EditorList = EditorList;
  5867. }
  5868. }
  5869. __finally
  5870. {
  5871. delete EditorList;
  5872. }
  5873. }
  5874. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  5875. }
  5876. }
  5877. //---------------------------------------------------------------------------
  5878. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  5879. {
  5880. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  5881. if (FFileFindTerminal == Sender)
  5882. {
  5883. FFileFindTerminal = NULL;
  5884. HideFileFindDialog();
  5885. }
  5886. if (FClipboardTerminal == Sender)
  5887. {
  5888. ClipboardClear(); // implies ClipboardStop
  5889. }
  5890. }
  5891. //---------------------------------------------------------------------------
  5892. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  5893. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  5894. {
  5895. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  5896. DebugAssert(Terminal != NULL);
  5897. if (Data->Terminal == Terminal)
  5898. {
  5899. Data->Terminal = NULL;
  5900. }
  5901. }
  5902. //---------------------------------------------------------------------------
  5903. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  5904. {
  5905. UpdateControls();
  5906. SessionColor = TColor(0);
  5907. UpdateRemotePathComboBox(false);
  5908. NeedSession(true);
  5909. }
  5910. //---------------------------------------------------------------------------
  5911. void __fastcall TCustomScpExplorerForm::NeedSession(bool ReloadSessions)
  5912. {
  5913. try
  5914. {
  5915. try
  5916. {
  5917. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  5918. }
  5919. __finally
  5920. {
  5921. if (!WinConfiguration->KeepOpenWhenNoSession &&
  5922. (!Terminal || !Terminal->Active))
  5923. {
  5924. TerminateApplication();
  5925. }
  5926. }
  5927. }
  5928. catch (EAbort &)
  5929. {
  5930. // swallow
  5931. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  5932. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  5933. }
  5934. }
  5935. //---------------------------------------------------------------------------
  5936. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  5937. {
  5938. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  5939. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  5940. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  5941. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  5942. try
  5943. {
  5944. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  5945. // Clear the design time unthemed tab
  5946. ((SessionsPageControl->PageCount > 0) &&
  5947. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  5948. {
  5949. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  5950. }
  5951. for (int Index = 0; Index <= TerminalList->Count; Index++)
  5952. {
  5953. TThemeTabSheet * TabSheet;
  5954. if (Index >= SessionsPageControl->PageCount)
  5955. {
  5956. TabSheet = new TThemeTabSheet(SessionsPageControl);
  5957. TabSheet->PageControl = SessionsPageControl;
  5958. }
  5959. else
  5960. {
  5961. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  5962. }
  5963. bool IsSessionTab = (Index < TerminalList->Count);
  5964. if (IsSessionTab)
  5965. {
  5966. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  5967. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  5968. UpdateSessionTab(TabSheet);
  5969. }
  5970. else
  5971. {
  5972. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  5973. TabSheet->Tag = 0; // not really needed
  5974. // We know that we are at the last page, sotherwise we could not call this (it assumes that new session tab is the last one)
  5975. UpdateNewSessionTab();
  5976. }
  5977. TabSheet->ShowCloseButton = IsSessionTab;
  5978. }
  5979. }
  5980. __finally
  5981. {
  5982. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  5983. }
  5984. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  5985. }
  5986. //---------------------------------------------------------------------------
  5987. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  5988. {
  5989. DoTerminalListChanged();
  5990. }
  5991. //---------------------------------------------------------------------------
  5992. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  5993. {
  5994. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  5995. DebugAssert(TabSheet->ImageIndex == 0);
  5996. TabSheet->Caption =
  5997. WinConfiguration->SelectiveToolbarText ?
  5998. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  5999. UnicodeString();
  6000. }
  6001. //---------------------------------------------------------------------------
  6002. TTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6003. {
  6004. return reinterpret_cast<TTerminal *>(TabSheet->Tag);
  6005. }
  6006. //---------------------------------------------------------------------------
  6007. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6008. {
  6009. if (DebugAlwaysTrue(TabSheet != NULL))
  6010. {
  6011. TManagedTerminal * ManagedTerminal =
  6012. dynamic_cast<TManagedTerminal *>(GetSessionTabTerminal(TabSheet));
  6013. if (DebugAlwaysTrue(ManagedTerminal != NULL))
  6014. {
  6015. TColor Color = (ManagedTerminal == FTerminal) ? FSessionColor : ManagedTerminal->StateData->Color;
  6016. TabSheet->ImageIndex = AddSessionColor(Color);
  6017. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ManagedTerminal, true);
  6018. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6019. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6020. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6021. {
  6022. ThemeTabSheet->Shadowed = !ManagedTerminal->Active;
  6023. }
  6024. }
  6025. }
  6026. }
  6027. //---------------------------------------------------------------------------
  6028. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6029. {
  6030. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6031. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6032. bool Result = (Terminal != NULL);
  6033. if (Result)
  6034. {
  6035. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6036. }
  6037. else
  6038. {
  6039. try
  6040. {
  6041. NewSession(false);
  6042. }
  6043. __finally
  6044. {
  6045. DoTerminalListChanged();
  6046. }
  6047. FSessionsPageControlNewSessionTime = Now();
  6048. }
  6049. return Result;
  6050. }
  6051. //---------------------------------------------------------------------------
  6052. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6053. {
  6054. SessionTabSwitched();
  6055. }
  6056. //---------------------------------------------------------------------------
  6057. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6058. {
  6059. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6060. DebugAssert(TransferList != NULL);
  6061. UnicodeString Name;
  6062. if (TransferList->ItemIndex <= 0)
  6063. {
  6064. Name = L"";
  6065. }
  6066. else
  6067. {
  6068. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6069. }
  6070. if (FCopyParamAutoSelected.IsEmpty())
  6071. {
  6072. // if previous preset was not autoselected, make new preset the "default"
  6073. FCopyParamDefault = Name;
  6074. }
  6075. GUIConfiguration->CopyParamCurrent = Name;
  6076. }
  6077. //---------------------------------------------------------------------------
  6078. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6079. {
  6080. // sanity check
  6081. bool ExistingPreset =
  6082. (FTransferListHoverIndex >= 0) &&
  6083. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6084. DebugAssert(ExistingPreset);
  6085. if (ExistingPreset)
  6086. {
  6087. HDC DC = GetDC(0);
  6088. TCanvas * Canvas = new TCanvas();
  6089. try
  6090. {
  6091. Canvas->Handle = DC;
  6092. Canvas->Font = GetToolbarFont(this);
  6093. UnicodeString Name;
  6094. if (FTransferListHoverIndex == 0)
  6095. {
  6096. Name = L"";
  6097. }
  6098. else
  6099. {
  6100. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6101. }
  6102. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6103. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6104. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6105. static_cast<TObject*>(GetComponent(fcTransferList)));
  6106. UnicodeString InfoStr =
  6107. GUIConfiguration->CopyParamPreset[Name].
  6108. GetInfoStr(L"; ",
  6109. FLAGMASK(Terminal != NULL, Terminal->UsableCopyParamAttrs(0).General));
  6110. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6111. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6112. {
  6113. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6114. {
  6115. InfoStr.SetLength(InfoStr.Length() - 1);
  6116. }
  6117. InfoStr += Ellipsis;
  6118. }
  6119. // UpdateCaption does not cause invalidation of whole submenu, while
  6120. // setting Caption property does.
  6121. // also it probably does not resize the label, even if necessary
  6122. // (we do not want that anyway)
  6123. TransferLabel->UpdateCaption(InfoStr);
  6124. }
  6125. __finally
  6126. {
  6127. Canvas->Handle = NULL;
  6128. ReleaseDC(0, DC);
  6129. delete Canvas;
  6130. }
  6131. }
  6132. }
  6133. //---------------------------------------------------------------------------
  6134. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6135. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6136. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6137. {
  6138. if (FTransferListHoverIndex != AHoverIndex)
  6139. {
  6140. FTransferListHoverIndex = AHoverIndex;
  6141. UpdateTransferLabel();
  6142. }
  6143. }
  6144. //---------------------------------------------------------------------------
  6145. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6146. {
  6147. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6148. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6149. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6150. {
  6151. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6152. {
  6153. OpenDirectory(GetSide(osCurrent));
  6154. Message.Result = 1;
  6155. }
  6156. else
  6157. {
  6158. TForm::Dispatch(&Message);
  6159. }
  6160. }
  6161. else
  6162. {
  6163. TForm::Dispatch(&Message);
  6164. }
  6165. }
  6166. //---------------------------------------------------------------------------
  6167. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6168. {
  6169. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6170. if ((FIgnoreNextDialogChar != 0) &&
  6171. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6172. {
  6173. Message.Result = 1;
  6174. }
  6175. else
  6176. {
  6177. TForm::Dispatch(&Message);
  6178. }
  6179. FIgnoreNextDialogChar = 0;
  6180. }
  6181. //---------------------------------------------------------------------------
  6182. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6183. {
  6184. // The four low-order bits are used internally by Windows
  6185. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6186. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6187. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6188. // 61730 - restore thru double click - undocumented
  6189. // 61490 - maximize thru double click - undocumented
  6190. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6191. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6192. {
  6193. SysResizing(Cmd);
  6194. }
  6195. TForm::Dispatch(&Message);
  6196. }
  6197. //---------------------------------------------------------------------------
  6198. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6199. {
  6200. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6201. // Also there no point returning TRUE as we are not able to
  6202. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6203. // Hence the process termination might be safer :)
  6204. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6205. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6206. {
  6207. Message.Result = FALSE;
  6208. }
  6209. else
  6210. {
  6211. Message.Result = TRUE;
  6212. }
  6213. // Do not call default handling as that triggers OnCloseQuery,
  6214. // where our implementation will popup confirmation dialogs, what we do not want,
  6215. // as per Vista guidelines:
  6216. // https://docs.microsoft.com/en-us/windows/desktop/shutdown/shutting-down
  6217. }
  6218. //---------------------------------------------------------------------------
  6219. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6220. {
  6221. if (Message.EndSession && IsApplicationMinimized())
  6222. {
  6223. // WORKAROUND
  6224. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6225. // when it receives WM_ENDSESSION,
  6226. // but that sometimes (particularly when application is minimized) cause crashes.
  6227. // Crash popups message that blocks log off.
  6228. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6229. // so we call ExitProcess() immediately, not even trying to cleanup.
  6230. // It still causes beep, so there's likely some popup, but it does not block
  6231. // log off.
  6232. ExitProcess(0);
  6233. }
  6234. TForm::Dispatch(&Message);
  6235. }
  6236. #ifdef _DEBUG
  6237. //---------------------------------------------------------------------------
  6238. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6239. {
  6240. TForm::Dispatch(&Message);
  6241. }
  6242. #endif
  6243. //---------------------------------------------------------------------------
  6244. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6245. {
  6246. }
  6247. //---------------------------------------------------------------------------
  6248. void __fastcall TCustomScpExplorerForm::DoShow()
  6249. {
  6250. // only now are the controls resized finally, so the size constraints
  6251. // will not conflict with possibly very small window size
  6252. RestoreFormParams();
  6253. FixControlsPlacement();
  6254. if (Position == poDefaultPosOnly)
  6255. {
  6256. CutFormToDesktop(this);
  6257. }
  6258. TForm::DoShow();
  6259. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6260. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6261. if (Terminal == NULL)
  6262. {
  6263. StartingDisconnected();
  6264. }
  6265. UpdatePixelsPerInchMainWindowCounter();
  6266. FShowing = true;
  6267. }
  6268. //---------------------------------------------------------------------------
  6269. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6270. {
  6271. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6272. }
  6273. //---------------------------------------------------------------------------
  6274. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6275. {
  6276. DoTerminalListChanged();
  6277. }
  6278. //---------------------------------------------------------------------------
  6279. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6280. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6281. TNotifyEvent OnBalloonClick, TObject * UserData)
  6282. {
  6283. bool Do;
  6284. UnicodeString Message;
  6285. if (E == NULL)
  6286. {
  6287. Message = Str;
  6288. Do = true;
  6289. }
  6290. else
  6291. {
  6292. Do = ExceptionMessage(E, Message);
  6293. }
  6294. if (Do && WinConfiguration->BalloonNotifications)
  6295. {
  6296. UnicodeString Title;
  6297. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6298. {
  6299. Message = TrimLeft(Message);
  6300. }
  6301. else
  6302. {
  6303. Message = UnformatMessage(Message);
  6304. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6305. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6306. Title = LoadResourceString(Captions[Type]);
  6307. if (Terminal != NULL)
  6308. {
  6309. Title = FORMAT(L"%s - %s",
  6310. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6311. }
  6312. }
  6313. if (Seconds == 0)
  6314. {
  6315. Seconds = WinConfiguration->NotificationsTimeout;
  6316. }
  6317. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6318. }
  6319. }
  6320. //---------------------------------------------------------------------------
  6321. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6322. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6323. UnicodeString HelpKeyword, const TMessageParams * Params,
  6324. TTerminal * Terminal)
  6325. {
  6326. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6327. (FOnFeedSynchronizeError != NULL)) &&
  6328. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6329. {
  6330. if (FOnFeedSynchronizeError != NULL)
  6331. {
  6332. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6333. }
  6334. else
  6335. {
  6336. DebugAssert(FErrorList != NULL);
  6337. TStringList * MoreMessagesCopy = NULL;
  6338. if (MoreMessages)
  6339. {
  6340. MoreMessagesCopy = new TStringList();
  6341. MoreMessagesCopy->Assign(MoreMessages);
  6342. }
  6343. FErrorList->AddObject(Message, MoreMessagesCopy);
  6344. }
  6345. PopupTrayBalloon(Terminal, Message, Type);
  6346. return ContinueAnswer(Answers);
  6347. }
  6348. else
  6349. {
  6350. bool UseBalloon = FTrayIcon->Visible;
  6351. if (UseBalloon)
  6352. {
  6353. PopupTrayBalloon(Terminal, Message, Type);
  6354. }
  6355. unsigned int Result;
  6356. try
  6357. {
  6358. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6359. }
  6360. __finally
  6361. {
  6362. // cancel only balloon we popped up, otherwise we may cancel notification
  6363. // balloon that was there before the message dialog
  6364. // (such as "dd confirmation opt in balloon)
  6365. if (UseBalloon)
  6366. {
  6367. FTrayIcon->CancelBalloon();
  6368. }
  6369. }
  6370. return Result;
  6371. }
  6372. }
  6373. //---------------------------------------------------------------------------
  6374. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6375. TTerminal * Terminal, Exception * E)
  6376. {
  6377. if (FTrayIcon->Visible)
  6378. {
  6379. PopupTrayBalloon(Terminal, L"", qtError, E);
  6380. }
  6381. // particularly prevent opening new session from jump list,
  6382. // while exception is shown
  6383. NonVisualDataModule->StartBusy();
  6384. try
  6385. {
  6386. ShowExtendedExceptionEx(Terminal, E);
  6387. }
  6388. __finally
  6389. {
  6390. NonVisualDataModule->EndBusy();
  6391. FTrayIcon->CancelBalloon();
  6392. }
  6393. }
  6394. //---------------------------------------------------------------------------
  6395. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6396. {
  6397. // cannot rely on active page being page for active terminal,
  6398. // as it can happen that active page is the "new session" page
  6399. // (e.g. when reconnecting active terminal, while login dialog
  6400. // invoked from "new session" page is modal)
  6401. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6402. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6403. }
  6404. //---------------------------------------------------------------------------
  6405. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6406. TTerminal * Terminal, Exception * E)
  6407. {
  6408. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6409. if (!Terminal->Active)
  6410. {
  6411. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6412. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6413. {
  6414. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6415. }
  6416. }
  6417. }
  6418. //---------------------------------------------------------------------------
  6419. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6420. UnicodeString Message, TQueryType Type,
  6421. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6422. {
  6423. if ((E == NULL) ||
  6424. ExceptionMessage(E, Message))
  6425. {
  6426. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6427. if (Important)
  6428. {
  6429. Seconds *= 5;
  6430. }
  6431. UnicodeString NoteMessage(UnformatMessage(Message));
  6432. if (Terminal != NULL)
  6433. {
  6434. NoteMessage = FORMAT(L"%s: %s",
  6435. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6436. }
  6437. if (WinConfiguration->BalloonNotifications)
  6438. {
  6439. AddNote(NoteMessage);
  6440. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6441. }
  6442. else
  6443. {
  6444. FlashOnBackground();
  6445. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6446. }
  6447. }
  6448. }
  6449. //---------------------------------------------------------------------------
  6450. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6451. {
  6452. RestoreApp();
  6453. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6454. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6455. !NonVisualDataModule->Busy)
  6456. {
  6457. TTerminal * Terminal = TerminalNoteData->Terminal;
  6458. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6459. if (!ComponentVisible[fcQueueView])
  6460. {
  6461. ToggleQueueVisibility();
  6462. GoToQueue();
  6463. }
  6464. }
  6465. }
  6466. //---------------------------------------------------------------------------
  6467. void __fastcall TCustomScpExplorerForm::QueueEvent(TTerminal * ATerminal,
  6468. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6469. {
  6470. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  6471. UnicodeString Message;
  6472. TNotifyEvent OnClick = NULL;
  6473. TObject * UserData = NULL;
  6474. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6475. switch (Event)
  6476. {
  6477. case qeEmptyButMonitored:
  6478. if ((ATerminal != Terminal) || QueueInvisible)
  6479. {
  6480. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6481. OnClick = QueueEmptyNoteClicked;
  6482. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6483. TerminalNoteData->Terminal = ATerminal;
  6484. UserData = TerminalNoteData;
  6485. }
  6486. break;
  6487. case qeEmpty:
  6488. OperationComplete(ManagedTerminal->QueueOperationStart);
  6489. break;
  6490. case qePendingUserAction:
  6491. if ((ATerminal != Terminal) ||
  6492. (QueueInvisible && !IsQueueAutoPopup()))
  6493. {
  6494. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6495. }
  6496. break;
  6497. default:
  6498. DebugFail();
  6499. }
  6500. if (!Message.IsEmpty())
  6501. {
  6502. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6503. }
  6504. }
  6505. //---------------------------------------------------------------------------
  6506. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6507. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6508. {
  6509. if (FDDExtMapFile != NULL)
  6510. {
  6511. CloseHandle(FDDExtMapFile);
  6512. FDDExtMapFile = NULL;
  6513. }
  6514. if (WinConfiguration->DDFakeFile)
  6515. {
  6516. DDFakeFileInitDrag(FileList, Created);
  6517. }
  6518. }
  6519. //---------------------------------------------------------------------------
  6520. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6521. {
  6522. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6523. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6524. {
  6525. FFakeFileDropTarget = FileName;
  6526. }
  6527. }
  6528. //---------------------------------------------------------------------------
  6529. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6530. {
  6531. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6532. if (!ForceDirectories(ApiPath(Result)))
  6533. {
  6534. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6535. }
  6536. FileSetAttr(ApiPath(Result), faHidden);
  6537. return Result;
  6538. }
  6539. //---------------------------------------------------------------------------
  6540. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6541. bool & Created)
  6542. {
  6543. FFakeFileDropTarget = UnicodeString();
  6544. FDragFakeDirectory = CreateFakeTransferDirectory();
  6545. FileList->AddItem(NULL, FDragFakeDirectory);
  6546. Created = true;
  6547. if (!WinConfiguration->IsDDExtRunning() || (GetWindowsBuild() >= 17134))
  6548. {
  6549. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6550. }
  6551. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6552. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6553. {
  6554. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6555. TDragExtCommStruct* CommStruct;
  6556. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6557. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6558. DebugAssert(CommStruct != NULL);
  6559. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6560. CommStruct->Dragging = true;
  6561. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6562. LENOF(CommStruct->DropDest));
  6563. NULL_TERMINATE(CommStruct->DropDest);
  6564. UnmapViewOfFile(CommStruct);
  6565. }
  6566. FDDMoveSlipped = false;
  6567. }
  6568. //---------------------------------------------------------------------------
  6569. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6570. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6571. {
  6572. bool Result;
  6573. if (Sender == RemoteDirView)
  6574. {
  6575. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6576. }
  6577. else
  6578. {
  6579. DebugAssert(Sender == RemoteDriveView);
  6580. TStrings * FileList = RemoteDriveView->DragFileList();
  6581. try
  6582. {
  6583. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6584. }
  6585. __finally
  6586. {
  6587. delete FileList;
  6588. }
  6589. }
  6590. if (FDDTargetControl == RemoteDriveView)
  6591. {
  6592. RemoteDriveView->UpdateDropTarget();
  6593. }
  6594. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6595. // or from tree view to dir view.
  6596. // foMove happens when file/dir is dragged from remote tree view outside of
  6597. // application via dragex
  6598. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6599. (DropSourceControl == RemoteDriveView))
  6600. {
  6601. RemoteDriveView->UpdateDropSource();
  6602. }
  6603. return Result;
  6604. }
  6605. //---------------------------------------------------------------------------
  6606. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6607. {
  6608. // This also handles drops of remote files to queue.
  6609. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6610. SAFE_DESTROY(FDDFileList);
  6611. if (!FFakeFileDropTarget.IsEmpty())
  6612. {
  6613. RemoveDir(ApiPath(FFakeFileDropTarget));
  6614. }
  6615. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6616. {
  6617. try
  6618. {
  6619. TDragResult DDResult = (Sender == RemoteDirView) ?
  6620. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6621. // Focus is moved to the target application,
  6622. // but as we are going to present the UI, we need to steal the focus back.
  6623. // This most likely won't work though (windows does not allow application
  6624. // to steal focus most of the time)
  6625. Application->BringToFront();
  6626. // On older version of Windows we never got drMove here, see also comment below.
  6627. // On Windows 10, we get the "move".
  6628. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6629. {
  6630. UnicodeString TargetDirectory;
  6631. TFileOperation Operation;
  6632. // drInvalid may mean drMove, see comment below
  6633. switch (DDResult)
  6634. {
  6635. case drCopy:
  6636. Operation = foCopy;
  6637. break;
  6638. case drMove:
  6639. Operation = foMove;
  6640. break;
  6641. default:
  6642. DebugFail();
  6643. case drInvalid:
  6644. // prefer "copy" for safety
  6645. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6646. break;
  6647. }
  6648. if (FDDMoveSlipped)
  6649. {
  6650. Operation = foMove;
  6651. }
  6652. TTransferOperationParam Param;
  6653. UnicodeString CounterName;
  6654. bool ForceQueue;
  6655. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6656. {
  6657. // we get drInvalid both if d&d was intercepted by ddext,
  6658. // and when users drops on no-drop location.
  6659. // we tell the difference by existence of response from ddext,
  6660. // so we ignore absence of response in this case
  6661. if (DDResult != drInvalid)
  6662. {
  6663. // here we know that the extension is installed,
  6664. // as it is checked as soon as drag&drop starts from
  6665. // RemoteFileControlDDCreateDragFileList
  6666. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6667. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6668. }
  6669. }
  6670. else
  6671. {
  6672. // download using fake file
  6673. Param.Temp = false;
  6674. Param.DragDrop = true;
  6675. if (ForceQueue)
  6676. {
  6677. Param.Queue = asOn;
  6678. }
  6679. if (Sender == RemoteDirView)
  6680. {
  6681. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6682. }
  6683. if (RemoteFileControlFileOperation(Sender, Operation,
  6684. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6685. {
  6686. Configuration->Usage->Inc(CounterName);
  6687. }
  6688. }
  6689. }
  6690. }
  6691. __finally
  6692. {
  6693. CloseHandle(FDDExtMapFile);
  6694. FDDExtMapFile = NULL;
  6695. delete FDragFakeMonitors;
  6696. FDragFakeMonitors = NULL;
  6697. RemoveDir(ApiPath(FDragFakeDirectory));
  6698. FDragFakeDirectory = L"";
  6699. FFakeFileDropTarget = UnicodeString();
  6700. }
  6701. }
  6702. if (FDragDropSshTerminate.get() != NULL)
  6703. {
  6704. FDragDropSshTerminate->Rethrow();
  6705. }
  6706. }
  6707. //---------------------------------------------------------------------------
  6708. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6709. TObject * Sender, int dwEffect, HRESULT & /*Result*/)
  6710. {
  6711. HCURSOR SlippedCopyCursor;
  6712. FDDMoveSlipped =
  6713. (FDragMoveCursor != NULL) &&
  6714. (!WinConfiguration->DDAllowMoveInit) && (dwEffect == DROPEFFECT_Copy) &&
  6715. ((IsFileControl(FDDTargetControl, osRemote) && (GetKeyState(VK_CONTROL) >= 0) &&
  6716. FTerminal->IsCapable[fcRemoteMove]) ||
  6717. (IsFileControl(FDDTargetControl, osLocal) && (GetKeyState(VK_SHIFT) < 0)));
  6718. SlippedCopyCursor = FDDMoveSlipped ? FDragMoveCursor : Dragdrop::DefaultCursor;
  6719. DragDropFiles(Sender)->CHCopy = SlippedCopyCursor;
  6720. DragDropFiles(Sender)->CHScrollCopy = SlippedCopyCursor;
  6721. // Remember drop effect so we know (when user drops files), if we copy or move
  6722. FLastDropEffect = dwEffect;
  6723. }
  6724. //---------------------------------------------------------------------------
  6725. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6726. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6727. {
  6728. bool Result;
  6729. if (FDDTargetControl == QueueView3)
  6730. {
  6731. Directory = DefaultDownloadTargetDirectory();
  6732. Result = true;
  6733. CounterName = L"DownloadsDragDropQueue";
  6734. ForceQueue = true;
  6735. }
  6736. else if (!FFakeFileDropTarget.IsEmpty())
  6737. {
  6738. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6739. Result = true;
  6740. ForceQueue = false;
  6741. CounterName = L"DownloadsDragDropFakeFile";
  6742. }
  6743. else
  6744. {
  6745. ForceQueue = false;
  6746. Enabled = false;
  6747. try
  6748. {
  6749. int Timer = 0;
  6750. Result = false;
  6751. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6752. {
  6753. {
  6754. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6755. TDragExtCommStruct* CommStruct;
  6756. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6757. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6758. DebugAssert(CommStruct != NULL);
  6759. Result = !CommStruct->Dragging;
  6760. if (Result)
  6761. {
  6762. Directory = ExtractFilePath(CommStruct->DropDest);
  6763. CounterName = L"DownloadsDragDropExternalExt";
  6764. }
  6765. UnmapViewOfFile(CommStruct);
  6766. }
  6767. if (!Result)
  6768. {
  6769. Sleep(50);
  6770. Timer += 50;
  6771. Application->ProcessMessages();
  6772. }
  6773. }
  6774. }
  6775. __finally
  6776. {
  6777. Enabled = true;
  6778. }
  6779. }
  6780. return Result;
  6781. }
  6782. //---------------------------------------------------------------------------
  6783. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  6784. const UnicodeString TempDir, int SecDelay)
  6785. {
  6786. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  6787. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  6788. if (FDelayedDeletionTimer == NULL)
  6789. {
  6790. DebugAssert(HandleAllocated());
  6791. FDelayedDeletionTimer = new TTimer(this);
  6792. FDelayedDeletionTimer->Interval = 10000;
  6793. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  6794. }
  6795. else
  6796. {
  6797. FDelayedDeletionTimer->Enabled = true;
  6798. }
  6799. }
  6800. //---------------------------------------------------------------------------
  6801. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  6802. {
  6803. DebugAssert(FDelayedDeletionList != NULL);
  6804. TDateTime N = Now();
  6805. TDateTime Alert;
  6806. UnicodeString Directory;
  6807. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  6808. {
  6809. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  6810. if ((N >= Alert) || (Sender == NULL))
  6811. {
  6812. Directory = FDelayedDeletionList->Strings[Index];
  6813. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  6814. {
  6815. FDelayedDeletionList->Delete(Index);
  6816. }
  6817. }
  6818. }
  6819. if (FDelayedDeletionList->Count == 0)
  6820. {
  6821. FDelayedDeletionTimer->Enabled = false;
  6822. }
  6823. }
  6824. //---------------------------------------------------------------------------
  6825. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  6826. {
  6827. if (IsFileControl(FDDTargetControl, osRemote) ||
  6828. (FDDTargetControl == SessionsPageControl))
  6829. {
  6830. TTerminal * TargetTerminal = NULL;
  6831. TFileOperation Operation = foNone;
  6832. if (FDDTargetControl == SessionsPageControl)
  6833. {
  6834. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  6835. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  6836. // do not allow dropping on the "+" tab
  6837. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  6838. if (TargetTerminal != NULL)
  6839. {
  6840. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  6841. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  6842. {
  6843. Operation = foRemoteMove;
  6844. }
  6845. else
  6846. {
  6847. Operation = foRemoteCopy;
  6848. }
  6849. }
  6850. }
  6851. else
  6852. {
  6853. // when move from remote side is disabled, we allow copying inside the remote
  6854. // panel, but we interpret is as moving (we also slip in the move cursor)
  6855. if ((FLastDropEffect == DROPEFFECT_MOVE) ||
  6856. (!WinConfiguration->DDAllowMoveInit && (FLastDropEffect == DROPEFFECT_COPY) &&
  6857. FDDMoveSlipped))
  6858. {
  6859. Operation = foRemoteMove;
  6860. }
  6861. else if (FLastDropEffect == DROPEFFECT_COPY)
  6862. {
  6863. Operation = foRemoteCopy;
  6864. }
  6865. }
  6866. if (Operation != foNone)
  6867. {
  6868. RemoteFileControlFileOperation(DropSourceControl,
  6869. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  6870. }
  6871. // abort drag&drop
  6872. Abort();
  6873. }
  6874. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  6875. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  6876. (FDDTargetControl != QueueView3))
  6877. {
  6878. DebugAssert(!FDragTempDir.IsEmpty());
  6879. TTransferType Type;
  6880. UnicodeString TempDir = FDragTempDir;
  6881. // We clear FDragTempDir before calling
  6882. // just in case it fail (raises exception)
  6883. FDragTempDir = L"";
  6884. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  6885. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  6886. // empty directory parameter means temp directory -> don't display it!
  6887. UnicodeString TargetDir = L"";
  6888. int Options = 0;
  6889. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  6890. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  6891. {
  6892. Abort();
  6893. }
  6894. // TargetDir is set when dropped on local file control
  6895. // (this was workaround for legacy dirview event handling, now it should be
  6896. // made prettier)
  6897. if (TargetDir.IsEmpty())
  6898. {
  6899. TargetDir = TempDir;
  6900. if (ForceDirectories(ApiPath(TargetDir)))
  6901. {
  6902. DebugAssert(Terminal && !TargetDir.IsEmpty());
  6903. FPendingTempSpaceWarn = true;
  6904. try
  6905. {
  6906. FDragDropOperation = true;
  6907. int Params = cpTemporary |
  6908. (Type == ttMove ? cpDelete : 0);
  6909. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  6910. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  6911. }
  6912. __finally
  6913. {
  6914. FDragDropOperation = false;
  6915. FPendingTempSpaceWarn = false;
  6916. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  6917. }
  6918. }
  6919. else
  6920. {
  6921. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  6922. }
  6923. }
  6924. }
  6925. }
  6926. //---------------------------------------------------------------------------
  6927. void __fastcall TCustomScpExplorerForm::DDDownload(
  6928. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  6929. {
  6930. void * BatchStorage;
  6931. BatchStart(BatchStorage);
  6932. try
  6933. {
  6934. UpdateCopyParamCounters(*CopyParam);
  6935. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  6936. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  6937. {
  6938. RemoteDriveView->UpdateDropSource();
  6939. }
  6940. }
  6941. __finally
  6942. {
  6943. BatchEnd(BatchStorage);
  6944. }
  6945. }
  6946. //---------------------------------------------------------------------------
  6947. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  6948. {
  6949. public:
  6950. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  6951. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename)
  6952. {
  6953. }
  6954. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  6955. {
  6956. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  6957. TDataObjectFilesEx::AllowData(FormatEtc);
  6958. }
  6959. };
  6960. //---------------------------------------------------------------------------
  6961. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  6962. TObject * Sender, TDataObject *& DataObject)
  6963. {
  6964. if (FDDExtMapFile != NULL)
  6965. {
  6966. TFileList * FileList = DragDropFiles(Sender)->FileList;
  6967. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  6968. {
  6969. Abort();
  6970. }
  6971. if (FileList->Count > 0)
  6972. {
  6973. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  6974. if (!FilesObject->IsValid(true, true))
  6975. {
  6976. FilesObject->_Release();
  6977. }
  6978. else
  6979. {
  6980. DataObject = FilesObject;
  6981. }
  6982. }
  6983. }
  6984. }
  6985. //---------------------------------------------------------------------------
  6986. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  6987. {
  6988. DebugFail();
  6989. }
  6990. //---------------------------------------------------------------------------
  6991. void __fastcall TCustomScpExplorerForm::GoToTree()
  6992. {
  6993. ComponentVisible[fcRemoteTree] = true;
  6994. RemoteDriveView->SetFocus();
  6995. }
  6996. //---------------------------------------------------------------------------
  6997. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  6998. TPanelExport Export)
  6999. {
  7000. TCustomDirView * DirView = this->DirView(Side);
  7001. std::unique_ptr<TStrings> ExportData(new TStringList());
  7002. switch (Export)
  7003. {
  7004. case pePath:
  7005. ExportData->Add(DirView->PathName);
  7006. break;
  7007. case peFileList:
  7008. case peFullFileList:
  7009. {
  7010. bool FullPath = (Export == peFullFileList);
  7011. DirView->CreateFileList(false, FullPath, ExportData.get());
  7012. for (int Index = 0; Index < ExportData->Count; Index++)
  7013. {
  7014. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7015. {
  7016. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7017. }
  7018. }
  7019. }
  7020. break;
  7021. default:
  7022. DebugFail();
  7023. }
  7024. return ExportData.release();
  7025. }
  7026. //---------------------------------------------------------------------------
  7027. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7028. TPanelExport Export, TPanelExportDestination Destination)
  7029. {
  7030. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7031. PanelExportStore(Side, Export, Destination, ExportData.get());
  7032. }
  7033. //---------------------------------------------------------------------------
  7034. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7035. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7036. TStrings * ExportData)
  7037. {
  7038. if (Destination == pedClipboard)
  7039. {
  7040. TInstantOperationVisualizer Visualizer;
  7041. CopyToClipboard(ExportData);
  7042. }
  7043. else
  7044. {
  7045. DebugFail();
  7046. }
  7047. }
  7048. //---------------------------------------------------------------------------
  7049. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7050. {
  7051. TCustomDirView * DirView = this->DirView(Side);
  7052. TFileFilter Filter;
  7053. DefaultFileFilter(Filter);
  7054. Filter.Masks = DirView->Mask;
  7055. if (DoFilterMaskDialog(DirView, &Filter))
  7056. {
  7057. DirView->Mask = TFileMasks::NormalizeMask(Filter.Masks);
  7058. Configuration->Usage->Inc(L"Filters");
  7059. }
  7060. }
  7061. //---------------------------------------------------------------------------
  7062. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7063. {
  7064. return FQueueController->DefaultOperation();
  7065. }
  7066. //---------------------------------------------------------------------------
  7067. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7068. TQueueOperation Operation, void ** Param)
  7069. {
  7070. switch (Operation)
  7071. {
  7072. case qoPreferences:
  7073. return true;
  7074. case qoGoTo:
  7075. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7076. case qoOnceEmpty:
  7077. return !FQueueController->Empty;
  7078. default:
  7079. return FQueueController->AllowOperation(Operation, Param);
  7080. }
  7081. }
  7082. //---------------------------------------------------------------------------
  7083. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7084. {
  7085. if (DebugAlwaysTrue(QueueView3->Visible))
  7086. {
  7087. QueueView3->SetFocus();
  7088. }
  7089. }
  7090. //---------------------------------------------------------------------------
  7091. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7092. TQueueOperation Operation, void * Param)
  7093. {
  7094. if (Operation == qoGoTo)
  7095. {
  7096. GoToQueue();
  7097. }
  7098. else if (Operation == qoPreferences)
  7099. {
  7100. PreferencesDialog(pmQueue);
  7101. }
  7102. else
  7103. {
  7104. FQueueController->ExecuteOperation(Operation, Param);
  7105. }
  7106. }
  7107. //---------------------------------------------------------------------------
  7108. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7109. {
  7110. return (Queue != NULL) && Queue->Enabled;
  7111. }
  7112. //---------------------------------------------------------------------------
  7113. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7114. {
  7115. DebugAssert(Queue != NULL);
  7116. if (Queue != NULL)
  7117. {
  7118. Queue->Enabled = !Queue->Enabled;
  7119. }
  7120. }
  7121. //---------------------------------------------------------------------------
  7122. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7123. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7124. {
  7125. FQueueActedItem = QueueView3->ItemFocused;
  7126. }
  7127. //---------------------------------------------------------------------------
  7128. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7129. {
  7130. return TopDock->Height + QueueSplitter->Height;
  7131. }
  7132. //---------------------------------------------------------------------------
  7133. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7134. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7135. {
  7136. // when queue is hidden by double-clicking splitter, stray attempt to
  7137. // resize the panel with strange value arrives, make sure it is ignored
  7138. if (ComponentVisible[fcQueueView])
  7139. {
  7140. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7141. RemotePanel->Constraints->MinHeight;
  7142. if (NewSize > HeightLimit)
  7143. {
  7144. NewSize = HeightLimit;
  7145. }
  7146. }
  7147. else
  7148. {
  7149. Accept = false;
  7150. }
  7151. }
  7152. //---------------------------------------------------------------------------
  7153. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7154. TDragObject *& /*DragObject*/)
  7155. {
  7156. FQueueActedItem = QueueView3->ItemFocused;
  7157. }
  7158. //---------------------------------------------------------------------------
  7159. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7160. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7161. {
  7162. Accept = true;
  7163. if (Source == QueueView3)
  7164. {
  7165. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7166. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7167. if (Accept)
  7168. {
  7169. TQueueItemProxy * QueueItem;
  7170. TQueueItemProxy * DestQueueItem;
  7171. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7172. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7173. Accept = (QueueItem != DestQueueItem) &&
  7174. (QueueItem->Status == TQueueItem::qsPending) &&
  7175. (DestQueueItem->Status == TQueueItem::qsPending);
  7176. }
  7177. }
  7178. }
  7179. //---------------------------------------------------------------------------
  7180. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7181. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7182. {
  7183. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7184. {
  7185. TQueueItemProxy * QueueItem;
  7186. TQueueItemProxy * DestQueueItem;
  7187. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7188. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7189. QueueItem->Move(DestQueueItem);
  7190. }
  7191. }
  7192. //---------------------------------------------------------------------------
  7193. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7194. {
  7195. if ((QueueView3->ItemFocused == NULL) &&
  7196. (QueueView3->Items->Count > 0))
  7197. {
  7198. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7199. }
  7200. QueueLabelUpdateStatus();
  7201. }
  7202. //---------------------------------------------------------------------------
  7203. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7204. {
  7205. QueueLabelUpdateStatus();
  7206. }
  7207. //---------------------------------------------------------------------------
  7208. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7209. {
  7210. QueueLabel->UpdateStatus();
  7211. }
  7212. //---------------------------------------------------------------------------
  7213. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7214. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7215. {
  7216. if (Selected)
  7217. {
  7218. NonVisualDataModule->UpdateNonVisibleActions();
  7219. }
  7220. }
  7221. //---------------------------------------------------------------------------
  7222. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7223. {
  7224. TDragDropFilesEx * Result = NULL;
  7225. if (Sender == SessionsPageControl)
  7226. {
  7227. Result = FSessionsDragDropFilesEx;
  7228. }
  7229. else if (Sender == QueueView3)
  7230. {
  7231. Result = FQueueDragDropFilesEx;
  7232. }
  7233. else
  7234. {
  7235. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7236. if (DirView != NULL)
  7237. {
  7238. Result = DirView->DragDropFilesEx;
  7239. }
  7240. else
  7241. {
  7242. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7243. if (DriveView != NULL)
  7244. {
  7245. Result = DriveView->DragDropFilesEx;
  7246. }
  7247. }
  7248. }
  7249. DebugAssert(Result != NULL);
  7250. return Result;
  7251. }
  7252. //---------------------------------------------------------------------------
  7253. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7254. {
  7255. return (DView->SelCount == DView->FilesCount);
  7256. }
  7257. //---------------------------------------------------------------------------
  7258. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7259. {
  7260. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7261. }
  7262. //---------------------------------------------------------------------------
  7263. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7264. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue)
  7265. {
  7266. TFileOperation Operation;
  7267. switch (Effect)
  7268. {
  7269. case DROPEFFECT_MOVE:
  7270. Operation = foMove;
  7271. break;
  7272. case DROPEFFECT_COPY:
  7273. // occures on WinXP (reported by user)
  7274. default:
  7275. Operation = foCopy;
  7276. break;
  7277. };
  7278. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7279. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7280. if (DragDropFilesEx->FileList->Count > 0)
  7281. {
  7282. TStrings * FileList = new TStringList();
  7283. try
  7284. {
  7285. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7286. {
  7287. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7288. }
  7289. FDragDropOperation = true;
  7290. TTransferOperationParam Param;
  7291. Param.TargetDirectory = TargetPath;
  7292. // upload, no temp dirs
  7293. Param.Temp = false;
  7294. Param.DragDrop = true;
  7295. Param.Options =
  7296. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7297. if (ForceQueue)
  7298. {
  7299. Param.Queue = asOn;
  7300. }
  7301. if (ExecuteFileOperation(Operation, osLocal, FileList,
  7302. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7303. {
  7304. if (IsFileControl(DropSourceControl, osLocal))
  7305. {
  7306. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7307. }
  7308. else
  7309. {
  7310. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7311. }
  7312. }
  7313. }
  7314. __finally
  7315. {
  7316. FDragDropOperation = false;
  7317. delete FileList;
  7318. }
  7319. }
  7320. }
  7321. //---------------------------------------------------------------------------
  7322. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7323. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7324. UnicodeString TargetPath, bool & /*DoOperation*/)
  7325. {
  7326. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false);
  7327. }
  7328. //---------------------------------------------------------------------------
  7329. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7330. TObject * Sender, int grfKeyState, int & dwEffect)
  7331. {
  7332. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7333. if ((dwEffect != DROPEFFECT_None) &&
  7334. IsFileControl(DropSourceControl, osRemote))
  7335. {
  7336. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7337. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7338. (RemoteDirView->DropTarget == NULL))
  7339. {
  7340. dwEffect = DROPEFFECT_None;
  7341. }
  7342. else
  7343. {
  7344. if (dwEffect == DROPEFFECT_Copy)
  7345. {
  7346. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7347. // currently we support copying always (at least via temporary directory);
  7348. // remove associated checks once this all proves stable and working
  7349. bool CopyCapable = true;
  7350. // if we do not support neither of operations, there's no discussion
  7351. if (!MoveCapable && !CopyCapable)
  7352. {
  7353. dwEffect = DROPEFFECT_None;
  7354. }
  7355. // when moving is disabled, we need to keep effect to "copy",
  7356. // which will be later interpretted as move (with slipped-in cursor)
  7357. else if (!WinConfiguration->DDAllowMoveInit && FLAGCLEAR(grfKeyState, MK_CONTROL))
  7358. {
  7359. // no-op, keep copy
  7360. }
  7361. else
  7362. {
  7363. // The default effect inside remote panel is move,
  7364. // unless we do not support it, but support copy
  7365. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7366. {
  7367. dwEffect = MoveCapable ? DROPEFFECT_Move : DROPEFFECT_Copy;
  7368. }
  7369. else
  7370. {
  7371. // with ctrl-down, we want copy unless it is not supported
  7372. dwEffect = CopyCapable ? DROPEFFECT_Copy : DROPEFFECT_None;
  7373. }
  7374. }
  7375. }
  7376. }
  7377. }
  7378. }
  7379. //---------------------------------------------------------------------------
  7380. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7381. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7382. {
  7383. if (FDDTotalSize >= 0)
  7384. {
  7385. if (File->IsDirectory)
  7386. {
  7387. FDDTotalSize = -1;
  7388. }
  7389. else
  7390. {
  7391. FDDTotalSize += File->Size;
  7392. }
  7393. }
  7394. DebugAssert(!FDragTempDir.IsEmpty());
  7395. // TODO: this is quite ineffective
  7396. // TODO: what if invalid character replacement is disabled?
  7397. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7398. UnicodeString TransferFileName;
  7399. if (Sender == RemoteDriveView)
  7400. {
  7401. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7402. }
  7403. else
  7404. {
  7405. TransferFileName = File->FileName;
  7406. }
  7407. FDDFileList->AddObject(TransferFileName, File);
  7408. }
  7409. //---------------------------------------------------------------------------
  7410. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7411. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7412. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7413. {
  7414. // sometimes we do not get DDEnd so the list is not released
  7415. SAFE_DESTROY(FDDFileList);
  7416. FDDFileList = new TStringList();
  7417. FDragTempDir = WinConfiguration->TemporaryDir();
  7418. FDDTotalSize = 0;
  7419. FDragDropSshTerminate.reset(NULL);
  7420. }
  7421. //---------------------------------------------------------------------------
  7422. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7423. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7424. HRESULT & Result)
  7425. {
  7426. if (Result == DRAGDROP_S_DROP)
  7427. {
  7428. try
  7429. {
  7430. GlobalDragImageList->HideDragImage();
  7431. try
  7432. {
  7433. RemoteFileControlDDTargetDrop();
  7434. }
  7435. catch(ESshTerminate & E)
  7436. {
  7437. FDragDropSshTerminate.reset(E.Clone());
  7438. }
  7439. }
  7440. catch (Exception &E)
  7441. {
  7442. // If downloading fails we need to cancel drag&drop, otherwise
  7443. // Explorer shows error
  7444. // But by the way exception probably never reach this point as
  7445. // it's catched on way.
  7446. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7447. Result = DRAGDROP_S_CANCEL;
  7448. ShowExtendedException(Terminal, &E);
  7449. }
  7450. }
  7451. }
  7452. //---------------------------------------------------------------------------
  7453. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7454. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7455. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7456. {
  7457. TFileMasks::TParams MaskParams;
  7458. MaskParams.Size = Size;
  7459. MaskParams.Modification = Modification;
  7460. // this does not re-parse the mask if it is the same as the last time
  7461. FDirViewMatchMask = Masks;
  7462. bool ImplicitMatch;
  7463. Matches =
  7464. // RecurseInclude parameter has no effect as the Path is empty
  7465. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7466. (AllowImplicitMatches || !ImplicitMatch);
  7467. }
  7468. //---------------------------------------------------------------------------
  7469. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7470. TObject * Sender, TListItem * Item, WORD & Indexes)
  7471. {
  7472. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7473. UnicodeString Ext;
  7474. if (DirView == RemoteDirView)
  7475. {
  7476. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7477. }
  7478. else
  7479. {
  7480. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7481. }
  7482. if (SameText(Ext, Configuration->PartialExt))
  7483. {
  7484. Indexes |= oiPartial;
  7485. }
  7486. }
  7487. //---------------------------------------------------------------------------
  7488. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7489. {
  7490. return
  7491. DirViewEnabled(osCurrent) &&
  7492. DirView(osCurrent)->CanPasteFromClipBoard();
  7493. }
  7494. //---------------------------------------------------------------------------
  7495. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7496. {
  7497. bool Result = false;
  7498. if (CanPasteToDirViewFromClipBoard())
  7499. {
  7500. Result = true;
  7501. }
  7502. else
  7503. {
  7504. UnicodeString ClipboardText;
  7505. if (NonEmptyTextFromClipboard(ClipboardText))
  7506. {
  7507. if (StoredSessions->IsUrl(ClipboardText))
  7508. {
  7509. Result = true;
  7510. }
  7511. else
  7512. {
  7513. Result = DirViewEnabled(osCurrent);
  7514. }
  7515. }
  7516. }
  7517. return Result;
  7518. }
  7519. //---------------------------------------------------------------------------
  7520. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7521. {
  7522. if (DoesClipboardContainOurFiles())
  7523. {
  7524. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7525. {
  7526. TTerminalManager * Manager = TTerminalManager::Instance();
  7527. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7528. Manager->ActiveTerminal = FClipboardTerminal;
  7529. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7530. }
  7531. }
  7532. else if (CanPasteToDirViewFromClipBoard())
  7533. {
  7534. DirView(osCurrent)->PasteFromClipBoard();
  7535. }
  7536. else
  7537. {
  7538. UnicodeString ClipboardText;
  7539. if (NonEmptyTextFromClipboard(ClipboardText))
  7540. {
  7541. if (StoredSessions->IsUrl(ClipboardText))
  7542. {
  7543. NewSession(false, ClipboardText);
  7544. }
  7545. else
  7546. {
  7547. DirView(osCurrent)->Path = ClipboardText;
  7548. }
  7549. }
  7550. }
  7551. }
  7552. //---------------------------------------------------------------------------
  7553. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7554. {
  7555. // TBD
  7556. }
  7557. //---------------------------------------------------------------------------
  7558. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7559. {
  7560. TCustomDirView * ADirView = DirView(Side);
  7561. ADirView->SelectAll(Mode);
  7562. ADirView->SetFocus();
  7563. }
  7564. //---------------------------------------------------------------------------
  7565. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7566. {
  7567. TCustomDirView * ADirView = DirView(Side);
  7568. TFileFilter Filter;
  7569. DefaultFileFilter(Filter);
  7570. if (DoSelectMaskDialog(ADirView, Select, &Filter, Configuration))
  7571. {
  7572. Configuration->Usage->Inc(L"MaskSelections");
  7573. ADirView->SelectFiles(Filter, Select);
  7574. ADirView->SetFocus();
  7575. }
  7576. }
  7577. //---------------------------------------------------------------------------
  7578. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7579. {
  7580. TCustomDirView * ADirView = DirView(Side);
  7581. ADirView->RestoreSelectedNames();
  7582. ADirView->SetFocus();
  7583. }
  7584. //---------------------------------------------------------------------------
  7585. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7586. {
  7587. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7588. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7589. {
  7590. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7591. UnicodeString Ext;
  7592. if (GetSide(osCurrent) == osRemote)
  7593. {
  7594. Ext = UnixExtractFileExt(FileName);
  7595. }
  7596. else
  7597. {
  7598. Ext = ExtractFileExt(FileName);
  7599. }
  7600. if (Ext.IsEmpty())
  7601. {
  7602. Ext = L".";
  7603. }
  7604. TFileFilter Filter;
  7605. Filter.Masks = FORMAT(L"*%s", (Ext));
  7606. Filter.Directories = false;
  7607. CurrentDirView->SelectFiles(Filter, Select);
  7608. }
  7609. }
  7610. //---------------------------------------------------------------------------
  7611. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7612. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7613. {
  7614. UnicodeString Result;
  7615. if ((Side == osRemote) && (Terminal == NULL))
  7616. {
  7617. // noop
  7618. }
  7619. else
  7620. {
  7621. Result =
  7622. FMTLOAD(FILE_INFO_FORMAT,
  7623. (FormatBytes(FileInfo.SelectedSize),
  7624. FormatBytes(FileInfo.FilesSize),
  7625. FormatNumber(FileInfo.SelectedCount),
  7626. FormatNumber(FileInfo.FilesCount)));
  7627. }
  7628. return Result;
  7629. }
  7630. //---------------------------------------------------------------------------
  7631. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7632. TTBXStatusPanel * Panel, TOperationSide Side)
  7633. {
  7634. if (Panel->Index == 1)
  7635. {
  7636. ToggleShowHiddenFiles();
  7637. }
  7638. else if (Panel->Index == 2)
  7639. {
  7640. Filter(Side);
  7641. }
  7642. }
  7643. //---------------------------------------------------------------------------
  7644. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7645. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7646. {
  7647. DebugAssert(!StatusBar->SimplePanel);
  7648. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7649. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7650. }
  7651. //---------------------------------------------------------------------------
  7652. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7653. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7654. {
  7655. DebugAssert(StatusBar->Panels->Count >= 3);
  7656. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7657. if (FileInfo.HiddenCount > 0)
  7658. {
  7659. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7660. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7661. }
  7662. else
  7663. {
  7664. HiddenFilesPanel->ViewPriority = 0;
  7665. // not really necessary, just to cleanup no-longer-valid data
  7666. HiddenFilesPanel->Caption = L"";
  7667. }
  7668. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7669. if (FileInfo.FilteredCount > 0)
  7670. {
  7671. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7672. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7673. }
  7674. else
  7675. {
  7676. FilteredFilesPanel->ViewPriority = 0;
  7677. // not really necessary, just to cleanup no-longer-valid data
  7678. FilteredFilesPanel->Caption = L"";
  7679. }
  7680. }
  7681. //---------------------------------------------------------------------------
  7682. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7683. TObject * /*Sender*/)
  7684. {
  7685. if (RemoteDirView->Enabled)
  7686. {
  7687. RemoteDirView->SetFocus();
  7688. }
  7689. }
  7690. //---------------------------------------------------------------------------
  7691. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7692. {
  7693. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7694. switch (Config.Show)
  7695. {
  7696. case qvShow:
  7697. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7698. {
  7699. Config.Show = qvHide;
  7700. }
  7701. else
  7702. {
  7703. Config.Show = Config.LastHideShow;
  7704. }
  7705. break;
  7706. case qvHideWhenEmpty:
  7707. if (ComponentVisible[fcQueueView])
  7708. {
  7709. Config.Show = qvHide;
  7710. }
  7711. else
  7712. {
  7713. Config.LastHideShow = Config.Show;
  7714. Config.Show = qvShow;
  7715. }
  7716. break;
  7717. case qvHide:
  7718. Config.LastHideShow = Config.Show;
  7719. Config.Show = qvShow;
  7720. break;
  7721. }
  7722. WinConfiguration->QueueView = Config;
  7723. }
  7724. //---------------------------------------------------------------------------
  7725. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7726. {
  7727. UnicodeString Result;
  7728. if (FTerminal != NULL)
  7729. {
  7730. switch (WinConfiguration->PathInCaption)
  7731. {
  7732. case picShort:
  7733. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7734. break;
  7735. case picFull:
  7736. Result = FTerminal->CurrentDirectory;
  7737. break;
  7738. }
  7739. }
  7740. return Result;
  7741. }
  7742. //---------------------------------------------------------------------------
  7743. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7744. {
  7745. TTerminalManager::Instance()->UpdateAppTitle();
  7746. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7747. // background to gray on Windows 7 (works on Windows 10).
  7748. // See also EnableControl
  7749. if (Showing)
  7750. {
  7751. bool HasTerminal = (Terminal != NULL);
  7752. if (HasTerminal)
  7753. {
  7754. // Update path when it changes
  7755. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7756. {
  7757. UpdateSessionTab(SessionsPageControl->ActivePage);
  7758. }
  7759. if (!RemoteDirView->Enabled)
  7760. {
  7761. RemoteDirView->Enabled = true;
  7762. if (FRemoteDirViewWasFocused)
  7763. {
  7764. ActiveControl = RemoteDirView;
  7765. }
  7766. }
  7767. RemoteDriveView->Enabled = true;
  7768. RemoteDirView->Color = (FSessionColor != 0 ? FSessionColor : clWindow);
  7769. RemoteDriveView->Color = RemoteDirView->Color;
  7770. }
  7771. else
  7772. {
  7773. if (RemoteDirView->Enabled)
  7774. {
  7775. // This is first called when the form is being constructed
  7776. // (not anymore due to Showing test above)
  7777. // but the false is overriden in the constructor later.
  7778. // An even later in TScpCommanderForm::DoShow()
  7779. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  7780. EnableControl(RemoteDirView, false);
  7781. }
  7782. EnableControl(RemoteDriveView, false);
  7783. }
  7784. EnableControl(QueueView3, HasTerminal && Terminal->IsCapable[fsBackgroundTransfers]);
  7785. QueueLabelUpdateStatus();
  7786. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  7787. }
  7788. }
  7789. //---------------------------------------------------------------------------
  7790. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  7791. {
  7792. UpdateControls();
  7793. }
  7794. //---------------------------------------------------------------------------
  7795. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  7796. TObject * Sender)
  7797. {
  7798. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7799. DebugAssert(DirView != NULL);
  7800. DoDirViewLoaded(DirView);
  7801. TransferPresetAutoSelect();
  7802. }
  7803. //---------------------------------------------------------------------------
  7804. void __fastcall TCustomScpExplorerForm::StartUpdates()
  7805. {
  7806. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  7807. // first run after installation
  7808. if (double(Updates.LastCheck) == 0)
  7809. {
  7810. // make sure next time there will be an update (if enabled)
  7811. Updates.LastCheck = TDateTime(1);
  7812. WinConfiguration->Updates = Updates;
  7813. }
  7814. else if ((double(Updates.Period) > 0) &&
  7815. (Now() - Updates.LastCheck >= Updates.Period))
  7816. {
  7817. TThreadMethod OnUpdatesChecked = NULL;
  7818. if (!IsUWP())
  7819. {
  7820. OnUpdatesChecked = UpdatesChecked;
  7821. }
  7822. StartUpdateThread(OnUpdatesChecked);
  7823. }
  7824. }
  7825. //---------------------------------------------------------------------------
  7826. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  7827. {
  7828. UnicodeString Message;
  7829. bool New;
  7830. TQueryType Type;
  7831. GetUpdatesMessage(Message, New, Type, false);
  7832. if (!Message.IsEmpty())
  7833. {
  7834. if (New)
  7835. {
  7836. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  7837. }
  7838. if (!New && (Type != qtWarning))
  7839. {
  7840. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  7841. }
  7842. else
  7843. {
  7844. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  7845. }
  7846. if (New)
  7847. {
  7848. Configuration->Usage->Inc(L"UpdateNotifications");
  7849. }
  7850. }
  7851. }
  7852. //---------------------------------------------------------------------------
  7853. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  7854. {
  7855. RestoreApp();
  7856. bool CanDisplay = !NonVisualDataModule->Busy;
  7857. if (!CanDisplay && (Screen->ActiveForm != NULL))
  7858. {
  7859. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  7860. }
  7861. if (CanDisplay)
  7862. {
  7863. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  7864. CheckForUpdates(true);
  7865. }
  7866. else
  7867. {
  7868. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  7869. }
  7870. }
  7871. //---------------------------------------------------------------------------
  7872. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  7873. TCopyParamRuleData & Data)
  7874. {
  7875. DebugAssert(Terminal != NULL);
  7876. Data.HostName = Terminal->SessionData->HostNameExpanded;
  7877. Data.UserName = Terminal->SessionData->UserNameExpanded;
  7878. Data.RemoteDirectory = RemoteDirView->PathName;
  7879. }
  7880. //---------------------------------------------------------------------------
  7881. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  7882. {
  7883. // Terminal can be null when we are changing local directory implicitly,
  7884. // because it has been deleted, while no session is connected
  7885. // (Login dialog is open)
  7886. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  7887. {
  7888. TCopyParamRuleData Data;
  7889. GetTransferPresetAutoSelectData(Data);
  7890. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  7891. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  7892. if (CopyParamIndex < 0)
  7893. {
  7894. // there is no preset that matches autoselection
  7895. // set preset that we consider "default"
  7896. FCopyParamAutoSelected = L""; // forget last autoselected preset
  7897. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  7898. }
  7899. else
  7900. {
  7901. // there is preset matching autoselection
  7902. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  7903. if (CopyParamName == FCopyParamAutoSelected)
  7904. {
  7905. // autoselected the same preset as the last time
  7906. // make no change (i.e. preserve custom user preset, if any)
  7907. }
  7908. else
  7909. {
  7910. // autoselected the different preset then the last time (or there
  7911. // was default preset set)
  7912. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  7913. GUIConfiguration->CopyParamCurrent = CopyParamName;
  7914. }
  7915. }
  7916. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  7917. {
  7918. if (CopyParamIndex >= 0)
  7919. {
  7920. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  7921. }
  7922. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  7923. try
  7924. {
  7925. int Fmt =
  7926. (CopyParamIndex < 0) ?
  7927. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  7928. COPY_PARAM_AUTOSELECTED;
  7929. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  7930. Data->Message = MainInstructions(Message);
  7931. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  7932. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  7933. CopyParamAttrs);
  7934. if (CopyParamIndex >= 0)
  7935. {
  7936. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  7937. Info = FORMAT(L"%s\n \n%s", (Info,
  7938. FMTLOAD(COPY_PARAM_RULE,
  7939. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  7940. }
  7941. Data->Message += L"\n\n" + Info;
  7942. if (WinConfiguration->CopyParamAutoSelectNotice)
  7943. {
  7944. TransferPresetNoteMessage(Data, true);
  7945. }
  7946. else
  7947. {
  7948. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  7949. Data = NULL; // ownership passed
  7950. }
  7951. }
  7952. __finally
  7953. {
  7954. delete Data;
  7955. }
  7956. }
  7957. }
  7958. }
  7959. //---------------------------------------------------------------------------
  7960. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  7961. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  7962. {
  7963. DebugAssert(NoteData != NULL);
  7964. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  7965. TQueryButtonAlias Aliases[1];
  7966. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  7967. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  7968. Params.Aliases = Aliases;
  7969. Params.AliasesCount = LENOF(Aliases);
  7970. unsigned int Result =
  7971. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  7972. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  7973. switch (Result)
  7974. {
  7975. case qaNeverAskAgain:
  7976. DebugAssert(AllowNeverAskAgain);
  7977. WinConfiguration->CopyParamAutoSelectNotice = false;
  7978. break;
  7979. case qaIgnore:
  7980. PreferencesDialog(pmPresets);
  7981. break;
  7982. }
  7983. }
  7984. //---------------------------------------------------------------------------
  7985. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  7986. {
  7987. // as of now useless, as this is used for notes only, never for balloons, ...
  7988. RestoreApp();
  7989. // .. and we should never be busy here
  7990. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  7991. {
  7992. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  7993. }
  7994. }
  7995. //---------------------------------------------------------------------------
  7996. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  7997. TPreferencesMode APreferencesMode)
  7998. {
  7999. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8000. TCopyParamRuleData Data;
  8001. if (Terminal != NULL)
  8002. {
  8003. PreferencesData.reset(new TPreferencesDialogData());
  8004. GetTransferPresetAutoSelectData(Data);
  8005. PreferencesData->CopyParamRuleData = &Data;
  8006. }
  8007. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8008. }
  8009. //---------------------------------------------------------------------------
  8010. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8011. const TCustomCommandType & Command)
  8012. {
  8013. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8014. {
  8015. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  8016. }
  8017. }
  8018. //---------------------------------------------------------------------------
  8019. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8020. {
  8021. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8022. TCustomCommandType Command;
  8023. // make sure we use local custom command when remote are not supported
  8024. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8025. {
  8026. Command = FLastCustomCommand;
  8027. }
  8028. else
  8029. {
  8030. Command.Params = Command.Params | ccLocal;
  8031. }
  8032. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8033. FEditingFocusedAdHocCommand = OnFocused;
  8034. bool LocalSide = (FCurrentSide == osLocal);
  8035. int Options =
  8036. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8037. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8038. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8039. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8040. {
  8041. FLastCustomCommand = Command;
  8042. UpdateCustomCommandsToolbar();
  8043. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8044. }
  8045. }
  8046. //---------------------------------------------------------------------------
  8047. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8048. {
  8049. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8050. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8051. DebugAssert(State > 0);
  8052. if (State <= 0)
  8053. {
  8054. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8055. }
  8056. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8057. }
  8058. //---------------------------------------------------------------------------
  8059. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8060. TCustomCommandType & Command, int & State)
  8061. {
  8062. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8063. if (Result)
  8064. {
  8065. Command = FLastCustomCommand;
  8066. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8067. }
  8068. return Result;
  8069. }
  8070. //---------------------------------------------------------------------------
  8071. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8072. {
  8073. if (RemoteDirView->ItemFocused != NULL)
  8074. {
  8075. RemoteDirView->ItemFocused->CancelEdit();
  8076. }
  8077. }
  8078. //---------------------------------------------------------------------------
  8079. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8080. {
  8081. DebugAssert(ComponentVisible[Component]);
  8082. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8083. }
  8084. //---------------------------------------------------------------------------
  8085. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8086. {
  8087. // when queue panel is resized here directly, the status bar is stretched
  8088. // over whole space the panel occupied
  8089. PostComponentHide(fcQueueView);
  8090. }
  8091. //---------------------------------------------------------------------------
  8092. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8093. {
  8094. TMessage * M = static_cast<TMessage*>(Message);
  8095. switch (M->Msg)
  8096. {
  8097. case CM_DIALOGCHAR:
  8098. CMDialogChar(*M);
  8099. break;
  8100. case WM_APPCOMMAND:
  8101. WMAppCommand(*M);
  8102. break;
  8103. case WM_SYSCOMMAND:
  8104. WMSysCommand(*M);
  8105. break;
  8106. case WM_QUERYENDSESSION:
  8107. WMQueryEndSession(*M);
  8108. break;
  8109. case WM_ENDSESSION:
  8110. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8111. break;
  8112. #ifdef _DEBUG
  8113. case WM_WINDOWPOSCHANGED:
  8114. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8115. break;
  8116. case CM_CUSTOMSTYLECHANGED:
  8117. TForm::Dispatch(Message);
  8118. break;
  8119. #endif
  8120. case WM_COMPONENT_HIDE:
  8121. {
  8122. Byte Component = static_cast<Byte>(M->WParam);
  8123. // sanity check
  8124. if (ComponentVisible[Component])
  8125. {
  8126. // special treatment
  8127. if (Component == fcQueueView)
  8128. {
  8129. ToggleQueueVisibility();
  8130. DebugAssert(!ComponentVisible[fcQueueView]);
  8131. }
  8132. else
  8133. {
  8134. ComponentVisible[Component] = false;
  8135. }
  8136. }
  8137. }
  8138. break;
  8139. case WM_COPYDATA:
  8140. WMCopyData(*M);
  8141. break;
  8142. case WM_MANAGES_CAPTION:
  8143. // caption managed in TTerminalManager::UpdateAppTitle()
  8144. M->Result = 1;
  8145. break;
  8146. case WM_WANTS_MOUSEWHEEL:
  8147. M->Result = 1;
  8148. break;
  8149. case CM_SHOWINGCHANGED:
  8150. CMShowingChanged(*M);
  8151. break;
  8152. case WM_CLOSE:
  8153. WMClose(*M);
  8154. break;
  8155. case WM_DPICHANGED:
  8156. WMDpiChanged(*M);
  8157. break;
  8158. case CM_DPICHANGED:
  8159. CMDpiChanged(*M);
  8160. break;
  8161. default:
  8162. TForm::Dispatch(Message);
  8163. break;
  8164. }
  8165. }
  8166. //---------------------------------------------------------------------------
  8167. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8168. {
  8169. // noop
  8170. }
  8171. //---------------------------------------------------------------------------
  8172. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8173. {
  8174. TForm::Dispatch(&Message);
  8175. FSessionColors->Clear();
  8176. AddFixedSessionImages();
  8177. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8178. UpdateImages();
  8179. }
  8180. //---------------------------------------------------------------------------
  8181. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8182. {
  8183. TForm::Dispatch(&Message);
  8184. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8185. UpdatePixelsPerInchMainWindowCounter();
  8186. }
  8187. //---------------------------------------------------------------------------
  8188. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8189. {
  8190. // Cannot close window while we are busy.
  8191. // We cannot test this in FormCloseQuery as that is called also from
  8192. // Close(), which is called by CloseApplicationAction. So we can be busy
  8193. // there already even, when it is legitimate to close the application.
  8194. // Possibly a better place to handle this would be WMSysCommand.
  8195. if (NonVisualDataModule->Busy)
  8196. {
  8197. Message.Result = 1;
  8198. }
  8199. else
  8200. {
  8201. TForm::Dispatch(&Message);
  8202. }
  8203. }
  8204. //---------------------------------------------------------------------------
  8205. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8206. {
  8207. TForm::Dispatch(&Message);
  8208. if (Showing && (Terminal == NULL))
  8209. {
  8210. // When we are starting minimized (i.e. from an installer),
  8211. // postpone showing Login dialog until we get restored.
  8212. // Otherwise the Login dialog (and Authentication window) show restored
  8213. // over invidible (minimized) main window.
  8214. if (WindowState == wsMinimized)
  8215. {
  8216. FNeedSession = true;
  8217. }
  8218. else
  8219. {
  8220. // This happens before application ever goes idle, so the toolbars would
  8221. // stay enabled (initial state) until the Login dialog is dismissed.
  8222. UpdateActions();
  8223. NonVisualDataModule->StartBusy();
  8224. try
  8225. {
  8226. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8227. // otherwise the dialog does not receive focus.
  8228. // With Commander interface the ProcessMessages is called already
  8229. // by TDriveView, but with Explorer interface, we need to call it explicily
  8230. Application->ProcessMessages();
  8231. // do not reload sessions, they have been loaded just now (optimization)
  8232. NeedSession(false);
  8233. }
  8234. __finally
  8235. {
  8236. NonVisualDataModule->EndBusy();
  8237. }
  8238. }
  8239. }
  8240. }
  8241. //---------------------------------------------------------------------------
  8242. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8243. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8244. int & MaxHeight)
  8245. {
  8246. // workaround for bug in TWinControl.CalcConstraints
  8247. // Check for empty rect (restore from iconic state) is done there only after
  8248. // call to AdjustClientRect, which enlarges the rect (for forms).
  8249. TRect R = GetClientRect();
  8250. // when restoring from iconic state, place no restrictions
  8251. if (IsRectEmpty(R))
  8252. {
  8253. MinWidth = 0;
  8254. MinHeight = 0;
  8255. MaxWidth = 32000;
  8256. MaxHeight = 32000;
  8257. }
  8258. }
  8259. //---------------------------------------------------------------------------
  8260. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8261. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8262. {
  8263. // terminal can be already closed (e.g. dropped connection)
  8264. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8265. {
  8266. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8267. try
  8268. {
  8269. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8270. }
  8271. catch(...)
  8272. {
  8273. if (!Terminal->Active)
  8274. {
  8275. Close = true;
  8276. }
  8277. throw;
  8278. }
  8279. }
  8280. }
  8281. //---------------------------------------------------------------------------
  8282. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8283. {
  8284. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8285. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8286. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8287. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8288. {
  8289. OnGetSpaceAvailable = GetSpaceAvailable;
  8290. }
  8291. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8292. OnGetSpaceAvailable);
  8293. }
  8294. //---------------------------------------------------------------------------
  8295. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8296. {
  8297. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8298. Terminal->FillSessionDataForCode(Data.get());
  8299. return Data.release();
  8300. }
  8301. //---------------------------------------------------------------------------
  8302. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8303. {
  8304. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8305. DoGenerateUrlDialog(Data.get(), Paths);
  8306. }
  8307. //---------------------------------------------------------------------------
  8308. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8309. {
  8310. GenerateUrl(NULL);
  8311. }
  8312. //---------------------------------------------------------------------------
  8313. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8314. {
  8315. std::unique_ptr<TStrings> Paths(new TStringList());
  8316. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8317. GenerateUrl(Paths.get());
  8318. }
  8319. //---------------------------------------------------------------------------
  8320. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8321. {
  8322. if (value != FSessionColor)
  8323. {
  8324. FSessionColor = value;
  8325. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8326. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8327. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8328. DebugAssert(ColorItem != NULL);
  8329. ColorItem->Color = C;
  8330. NonVisualDataModule->ColorMenuItem->Color = C;
  8331. // Is null when called from LastTerminalClosed
  8332. if (Terminal != NULL)
  8333. {
  8334. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8335. }
  8336. UpdateControls();
  8337. }
  8338. }
  8339. //---------------------------------------------------------------------------
  8340. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8341. {
  8342. bool Result = FNoteTimer->Enabled;
  8343. if (Result)
  8344. {
  8345. // cannot cancel note too early
  8346. bool NotEarly =
  8347. (Now() - FNoteShown >
  8348. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8349. if (Force || NotEarly)
  8350. {
  8351. FNoteTimer->Enabled = false;
  8352. FNote = L"";
  8353. SAFE_DESTROY(FNoteData);
  8354. FOnNoteClick = NULL;
  8355. FNoteHints = FNotes->Text;
  8356. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8357. UpdateStatusBar();
  8358. }
  8359. }
  8360. return Result;
  8361. }
  8362. //---------------------------------------------------------------------------
  8363. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8364. {
  8365. DebugAssert(FNoteTimer->Enabled);
  8366. CancelNote(true);
  8367. }
  8368. //---------------------------------------------------------------------------
  8369. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8370. {
  8371. int P = Note.Pos(L"\n");
  8372. if (P > 0)
  8373. {
  8374. Note.SetLength(P - 1);
  8375. }
  8376. FNotes->Add(FORMAT(L"[%s] %s",
  8377. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8378. while (FNotes->Count > 10)
  8379. {
  8380. FNotes->Delete(0);
  8381. }
  8382. if (UpdateNow)
  8383. {
  8384. FNoteHints = FNotes->Text;
  8385. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8386. UpdateStatusBar();
  8387. }
  8388. }
  8389. //---------------------------------------------------------------------------
  8390. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8391. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8392. {
  8393. int P = Note.Pos(L"\n");
  8394. if (P > 0)
  8395. {
  8396. Note.SetLength(P - 1);
  8397. }
  8398. FNoteHints = FNotes->Text;
  8399. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8400. FNote = Note;
  8401. SAFE_DESTROY(FNoteData);
  8402. FNoteData = NoteData;
  8403. FOnNoteClick = OnNoteClick;
  8404. AddNote(Note, false);
  8405. UpdateStatusBar();
  8406. FNoteShown = Now();
  8407. FNoteTimer->Enabled = false;
  8408. if (Seconds == 0)
  8409. {
  8410. Seconds = WinConfiguration->NotificationsTimeout;
  8411. }
  8412. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8413. FNoteTimer->Enabled = true;
  8414. }
  8415. //---------------------------------------------------------------------------
  8416. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8417. {
  8418. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8419. }
  8420. //---------------------------------------------------------------------------
  8421. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8422. {
  8423. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8424. {
  8425. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8426. }
  8427. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8428. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8429. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8430. 0, NULL, NULL);
  8431. }
  8432. //---------------------------------------------------------------------------
  8433. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8434. {
  8435. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8436. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8437. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8438. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8439. }
  8440. //---------------------------------------------------------------------------
  8441. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8442. {
  8443. WinConfiguration->FormatSizeBytes = Style;
  8444. }
  8445. //---------------------------------------------------------------------------
  8446. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8447. {
  8448. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8449. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8450. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8451. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8452. }
  8453. //---------------------------------------------------------------------------
  8454. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8455. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8456. {
  8457. if (Panel->Index == 0)
  8458. {
  8459. if (FOnNoteClick != NULL)
  8460. {
  8461. // prevent the user data from being freed by possible call
  8462. // to CancelNote or PostNote during call to OnNoteClick
  8463. std::unique_ptr<TObject> NoteData(FNoteData);
  8464. TNotifyEvent OnNoteClick = FOnNoteClick;
  8465. FNoteData = NULL;
  8466. // need to cancel the note as we are going to delete its user data
  8467. CancelNote(true);
  8468. OnNoteClick(NoteData.get());
  8469. }
  8470. }
  8471. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8472. {
  8473. FileSystemInfo();
  8474. }
  8475. }
  8476. //---------------------------------------------------------------------------
  8477. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8478. {
  8479. // workaround:
  8480. // 1) for unknown reason, disabling window, while minimized,
  8481. // prevents it from restoring, even if it was enabled again meanwhile
  8482. // 2) when disabling the main window, while another has focus
  8483. // minimize is no longer possible
  8484. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8485. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8486. // Shouldn't we use IsApplicationMinimized() here?
  8487. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8488. {
  8489. Enabled = false;
  8490. }
  8491. FLockLevel++;
  8492. }
  8493. //---------------------------------------------------------------------------
  8494. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8495. {
  8496. DebugAssert(FLockLevel > 0);
  8497. FLockLevel--;
  8498. if (FLockLevel == 0)
  8499. {
  8500. DebugAssert(FLockSuspendLevel == 0);
  8501. Enabled = true;
  8502. }
  8503. }
  8504. //---------------------------------------------------------------------------
  8505. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8506. {
  8507. // We need to make sure that window is enabled when the last modal window closes
  8508. // otherwise focus is not restored correctly.
  8509. // So we re-enable the window when modal window opens and
  8510. // disable it back after is closes
  8511. if (FLockLevel > 0)
  8512. {
  8513. // while we have nesting counter, we know that we never be called
  8514. // recursivelly as Application->OnModalBegin is called only
  8515. // for the top-level modal window
  8516. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8517. {
  8518. // won't be disabled when conditions in LockWindow() were not satisfied
  8519. FDisabledOnLockSuspend = !Enabled;
  8520. // When minimized to tray (or actually when set to SW_HIDE),
  8521. // setting Enabled makes the window focusable even when there's
  8522. // modal window over it
  8523. if (!FTrayIcon->Visible)
  8524. {
  8525. Enabled = true;
  8526. }
  8527. }
  8528. FLockSuspendLevel++;
  8529. }
  8530. }
  8531. //---------------------------------------------------------------------------
  8532. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8533. {
  8534. if (FLockSuspendLevel > 0)
  8535. {
  8536. DebugAssert(FLockLevel > 0);
  8537. FLockSuspendLevel--;
  8538. // see comment in SuspendWindowLock
  8539. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8540. {
  8541. // Note that window can be enabled here, when we were minized to tray when
  8542. // was SuspendWindowLock() called.
  8543. // We should possibly do the same check as in LockWindow(),
  8544. // if it is ever possible that the consitions change between
  8545. // SuspendWindowLock() and ResumeWindowLock()
  8546. Enabled = !FDisabledOnLockSuspend;
  8547. }
  8548. }
  8549. }
  8550. //---------------------------------------------------------------------------
  8551. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8552. {
  8553. if (!TextOnly)
  8554. {
  8555. TTBXComboBoxItem * RemotePathComboBox =
  8556. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8557. TStrings * Items = RemotePathComboBox->Strings;
  8558. Items->BeginUpdate();
  8559. try
  8560. {
  8561. Items->Clear();
  8562. if (Terminal != NULL)
  8563. {
  8564. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8565. while (!IsUnixRootPath(APath))
  8566. {
  8567. int P = APath.LastDelimiter(L'/');
  8568. DebugAssert(P >= 0);
  8569. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8570. APath.SetLength(P - 1);
  8571. }
  8572. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8573. }
  8574. }
  8575. __finally
  8576. {
  8577. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8578. // Setting ItemIndex to -1 does not reset its text
  8579. if (Items->Count == 0)
  8580. {
  8581. RemotePathComboBox->Text = L"";
  8582. }
  8583. Items->EndUpdate();
  8584. }
  8585. }
  8586. }
  8587. //---------------------------------------------------------------------------
  8588. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8589. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8590. int & ImageIndex)
  8591. {
  8592. if (AIndex < 0)
  8593. {
  8594. AIndex = Sender->ItemIndex;
  8595. }
  8596. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8597. }
  8598. //---------------------------------------------------------------------------
  8599. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8600. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8601. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8602. {
  8603. ARect.Left += (10 * AIndex);
  8604. }
  8605. //---------------------------------------------------------------------------
  8606. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8607. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8608. {
  8609. AWidth += (10 * AIndex);
  8610. }
  8611. //---------------------------------------------------------------------------
  8612. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8613. TObject * Sender)
  8614. {
  8615. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8616. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8617. int Index = RemotePathComboBox->ItemIndex;
  8618. while (Index < RemotePathComboBox->Strings->Count - 1)
  8619. {
  8620. APath = UnixExtractFileDir(APath);
  8621. Index++;
  8622. }
  8623. // VanDyke style paths
  8624. if (APath.IsEmpty())
  8625. {
  8626. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8627. APath = ROOTDIRECTORY;
  8628. }
  8629. if (RemoteDirView->Path != APath)
  8630. {
  8631. RemoteDirView->Path = APath;
  8632. }
  8633. }
  8634. //---------------------------------------------------------------------------
  8635. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8636. {
  8637. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8638. DebugUsedParam(Sender);
  8639. UpdateRemotePathComboBox(true);
  8640. }
  8641. //---------------------------------------------------------------------------
  8642. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8643. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8644. {
  8645. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8646. DebugAssert(DirView != NULL);
  8647. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8648. {
  8649. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8650. // OnEditing is called also from TCustomListView::CanEdit
  8651. if (Edit != NULL)
  8652. {
  8653. EditSelectBaseName(Edit);
  8654. }
  8655. }
  8656. }
  8657. //---------------------------------------------------------------------------
  8658. //---------------------------------------------------------------------------
  8659. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8660. {
  8661. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8662. Result->AutoDetectDnD = false;
  8663. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8664. Result->RenderDataOn = rdoEnterAndDropSync;
  8665. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8666. return Result;
  8667. }
  8668. //---------------------------------------------------------------------------
  8669. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8670. {
  8671. TForm::CreateWnd();
  8672. if (FSessionsDragDropFilesEx == NULL)
  8673. {
  8674. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8675. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8676. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8677. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8678. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8679. }
  8680. if (FQueueDragDropFilesEx == NULL)
  8681. {
  8682. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8683. // No need to set OnDragOver as we do not have any restrictions
  8684. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8685. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8686. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8687. }
  8688. }
  8689. //---------------------------------------------------------------------------
  8690. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8691. {
  8692. TForm::DestroyWnd();
  8693. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8694. FQueueDragDropFilesEx->DragDropControl = NULL;
  8695. }
  8696. //---------------------------------------------------------------------------
  8697. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8698. {
  8699. SideEnter(FCurrentSide);
  8700. FEverShown = true;
  8701. }
  8702. //---------------------------------------------------------------------------
  8703. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8704. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8705. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8706. {
  8707. if (!NonVisualDataModule->Busy)
  8708. {
  8709. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8710. Configuration->Usage->Inc(L"FileFinds");
  8711. LockWindow(true);
  8712. NonVisualDataModule->StartBusy();
  8713. WinConfiguration->LockedInterface = true;
  8714. try
  8715. {
  8716. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8717. }
  8718. __finally
  8719. {
  8720. WinConfiguration->LockedInterface = false;
  8721. NonVisualDataModule->EndBusy();
  8722. UnlockWindow();
  8723. }
  8724. }
  8725. }
  8726. //---------------------------------------------------------------------------
  8727. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8728. {
  8729. if (!NonVisualDataModule->Busy)
  8730. {
  8731. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8732. SetFocus();
  8733. RemoteDirView->Path = UnixExtractFilePath(Path);
  8734. TListItem * Item = RemoteDirView->FindFileItem(UnixExtractFileName(Path));
  8735. if (Item != NULL)
  8736. {
  8737. RemoteDirView->ItemFocused = Item;
  8738. RemoteDirView->ItemFocused->MakeVisible(false);
  8739. RemoteDirView->SetFocus();
  8740. }
  8741. }
  8742. }
  8743. //---------------------------------------------------------------------------
  8744. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  8745. {
  8746. bool Result = !NonVisualDataModule->Busy;
  8747. if (Result)
  8748. {
  8749. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8750. }
  8751. return Result;
  8752. }
  8753. //---------------------------------------------------------------------------
  8754. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  8755. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8756. {
  8757. if (CanOperateOnFoundFiles(ATerminal))
  8758. {
  8759. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  8760. FOnFileOperationFinished = OnFileOperationFinished;
  8761. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  8762. }
  8763. }
  8764. //---------------------------------------------------------------------------
  8765. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  8766. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8767. {
  8768. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  8769. }
  8770. //---------------------------------------------------------------------------
  8771. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  8772. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8773. {
  8774. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  8775. }
  8776. //---------------------------------------------------------------------------
  8777. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  8778. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8779. {
  8780. if (CanOperateOnFoundFiles(ATerminal))
  8781. {
  8782. for (int Index = 0; Index < FileList->Count; Index++)
  8783. {
  8784. UnicodeString FileName = FileList->Strings[Index];
  8785. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  8786. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  8787. OnFileOperationFinished(FileName, true);
  8788. }
  8789. }
  8790. }
  8791. //---------------------------------------------------------------------------
  8792. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  8793. {
  8794. FFileFindTerminal = Terminal;
  8795. ShowFileFindDialog(
  8796. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  8797. DoDownloadFoundFiles, DoEditFoundFiles);
  8798. }
  8799. //---------------------------------------------------------------------------
  8800. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  8801. {
  8802. FTaskbarList = TaskbarList;
  8803. }
  8804. //---------------------------------------------------------------------------
  8805. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  8806. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  8807. {
  8808. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8809. if (Index >= 0)
  8810. {
  8811. if (Button == mbLeft)
  8812. {
  8813. // "Mouse down" is raised only after tab is switched.
  8814. // If switching tab (switching session) takes long enough for user
  8815. // to actually release the button, "mouse down" is still raised,
  8816. // but we do not get "mouse up" event, so dragging is not cancelled,
  8817. // prevent that by not beginning dragging in the first place.
  8818. if (IsKeyPressed(VK_LBUTTON))
  8819. {
  8820. // when user clicks the "+", we get mouse down only after the session
  8821. // is closed, when new session tab is already on X:Y, so dragging
  8822. // starts, prevent that
  8823. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  8824. {
  8825. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8826. if (Terminal != NULL)
  8827. {
  8828. SessionsPageControl->BeginDrag(false);
  8829. }
  8830. }
  8831. }
  8832. }
  8833. else if (Button == mbMiddle)
  8834. {
  8835. // ignore middle-click for "New session tab"
  8836. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8837. if (Terminal != NULL)
  8838. {
  8839. SessionsPageControl->ActivePageIndex = Index;
  8840. // Switch to session tab (has to be session tab, due to previous check)
  8841. if (DebugAlwaysTrue(SessionTabSwitched()))
  8842. {
  8843. CloseSession();
  8844. }
  8845. }
  8846. }
  8847. }
  8848. }
  8849. //---------------------------------------------------------------------------
  8850. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  8851. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  8852. {
  8853. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8854. // do not allow dropping on the "+" tab
  8855. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8856. if ((TargetTerminal != NULL) &&
  8857. (SessionsPageControl->ActivePage->PageIndex != Index))
  8858. {
  8859. Configuration->Usage->Inc(L"SessionTabMoves");
  8860. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  8861. // but we want to actually prevent that to avoid flicker
  8862. SessionsPageControl->ActivePage->PageIndex = Index;
  8863. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  8864. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  8865. }
  8866. }
  8867. //---------------------------------------------------------------------------
  8868. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  8869. TObject * Sender, TObject * Source, int X, int Y,
  8870. TDragState /*State*/, bool & Accept)
  8871. {
  8872. Accept = (Sender == Source);
  8873. if (Accept)
  8874. {
  8875. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8876. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8877. // do not allow dragging to the "+" tab
  8878. Accept = (Terminal != NULL);
  8879. }
  8880. }
  8881. //---------------------------------------------------------------------------
  8882. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  8883. const TPoint & Point, int & Effect)
  8884. {
  8885. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8886. if (Index < 0)
  8887. {
  8888. Effect = DROPEFFECT_None;
  8889. }
  8890. else
  8891. {
  8892. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8893. // do not allow dropping on the "+" tab
  8894. if (TargetTerminal == NULL)
  8895. {
  8896. Effect = DROPEFFECT_None;
  8897. }
  8898. }
  8899. }
  8900. //---------------------------------------------------------------------------
  8901. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  8902. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  8903. {
  8904. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8905. // do not allow dropping on the "+" tab
  8906. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8907. if (TargetTerminal != NULL)
  8908. {
  8909. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  8910. if (!IsFileControl(DropSourceControl, osRemote))
  8911. {
  8912. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  8913. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  8914. // Why don't we use Terminal->CurrentDirectory directly?
  8915. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory, false);
  8916. }
  8917. }
  8918. }
  8919. //---------------------------------------------------------------------------
  8920. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  8921. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  8922. {
  8923. // Downloads are handled in RemoteFileControlDDEnd
  8924. if (!IsFileControl(DropSourceControl, osRemote))
  8925. {
  8926. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  8927. Terminal->CurrentDirectory,
  8928. // force queue
  8929. true);
  8930. }
  8931. }
  8932. //---------------------------------------------------------------------------
  8933. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  8934. {
  8935. FShowing = false;
  8936. // Do not save empty workspace
  8937. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  8938. {
  8939. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  8940. UnicodeString Name = WorkspaceName();
  8941. DoSaveWorkspace(
  8942. Name, DataList.get(),
  8943. !Configuration->DisablePasswordStoring &&
  8944. WinConfiguration->AutoSaveWorkspacePasswords, false);
  8945. WinConfiguration->LastStoredSession = Name;
  8946. }
  8947. }
  8948. //---------------------------------------------------------------------------
  8949. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  8950. {
  8951. TManagedTerminal * ManagedTerminal =
  8952. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  8953. if (ManagedTerminal != NULL)
  8954. {
  8955. ManagedTerminal->DirectoryLoaded = Now();
  8956. }
  8957. }
  8958. //---------------------------------------------------------------------------
  8959. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  8960. TListItem * /*Item*/, bool /*Selected*/)
  8961. {
  8962. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8963. switch (DirView->LastSelectMethod)
  8964. {
  8965. case smKeyboard:
  8966. Configuration->Usage->Inc(L"KeyboardSelections");
  8967. break;
  8968. case smMouse:
  8969. Configuration->Usage->Inc(L"MouseSelections");
  8970. break;
  8971. }
  8972. }
  8973. //---------------------------------------------------------------------------
  8974. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  8975. {
  8976. if (Color != 0)
  8977. {
  8978. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  8979. }
  8980. else
  8981. {
  8982. return FSessionTabImageIndex;
  8983. }
  8984. }
  8985. //---------------------------------------------------------------------------
  8986. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  8987. {
  8988. TPngImageCollectionItem * Item =
  8989. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  8990. FSessionColors->AddPng(Item->PngImage);
  8991. return FSessionColors->Count - 1;
  8992. }
  8993. //---------------------------------------------------------------------------
  8994. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  8995. {
  8996. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  8997. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  8998. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  8999. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9000. }
  9001. //---------------------------------------------------------------------------
  9002. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9003. {
  9004. for (int Index = 0; Index < Control->ControlCount; Index++)
  9005. {
  9006. TControl * AControl = Control->Controls[Index];
  9007. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9008. if (WinControl != NULL)
  9009. {
  9010. CollectItemsWithTextDisplayMode(WinControl);
  9011. }
  9012. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9013. if (Toolbar != NULL)
  9014. {
  9015. // we care for top-level items only
  9016. TTBCustomItem * Items = Toolbar->Items;
  9017. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9018. {
  9019. TTBCustomItem * Item = Items->Items[ItemIndex];
  9020. if (((Item->DisplayMode == nbdmImageAndText) ||
  9021. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9022. EligibleForImageDisplayMode(Item))
  9023. {
  9024. FItemsWithTextDisplayMode.insert(Item);
  9025. }
  9026. }
  9027. }
  9028. }
  9029. }
  9030. //---------------------------------------------------------------------------
  9031. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9032. {
  9033. return true;
  9034. }
  9035. //---------------------------------------------------------------------------
  9036. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9037. {
  9038. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9039. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9040. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9041. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9042. bool Result = true;
  9043. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9044. {
  9045. TTBCustomItem * Item = *i;
  9046. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9047. if (Label != NULL)
  9048. {
  9049. // optimization
  9050. if (Label->Visible == SelectiveToolbarText)
  9051. {
  9052. Result = false;
  9053. }
  9054. else
  9055. {
  9056. Label->Visible = SelectiveToolbarText;
  9057. }
  9058. }
  9059. else
  9060. {
  9061. // optimization
  9062. if (Item->DisplayMode == DisplayMode)
  9063. {
  9064. Result = false;
  9065. }
  9066. else
  9067. {
  9068. Item->DisplayMode = DisplayMode;
  9069. }
  9070. }
  9071. ++i;
  9072. }
  9073. if (Result)
  9074. {
  9075. UpdateNewSessionTab();
  9076. }
  9077. return Result;
  9078. }
  9079. //---------------------------------------------------------------------------
  9080. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9081. {
  9082. DebugFail();
  9083. }
  9084. //---------------------------------------------------------------------------
  9085. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9086. {
  9087. // Among other, a lock level is non zero, while directory is being loaded,
  9088. // even when it happens as a result of dirview navigation,
  9089. // i.e. when TNonVisualDataModule is NOT FBusy.
  9090. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9091. // Among other this prevents a panel auto update to occur while
  9092. // directory is changing.
  9093. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9094. }
  9095. //---------------------------------------------------------------------------
  9096. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9097. {
  9098. // While the window is disabled, we still seem to process menu shortcuts at least,
  9099. // so stop it at least here.
  9100. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9101. return
  9102. (Allowed == aaUpdate) ||
  9103. !NonVisualDataModule->Busy;
  9104. }
  9105. //---------------------------------------------------------------------------
  9106. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9107. {
  9108. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9109. }
  9110. //---------------------------------------------------------------------------
  9111. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9112. {
  9113. // This is somewhat redundant to LockWindow() call from
  9114. // TTerminalManager::TerminalReadDirectoryProgress.
  9115. // But disabling window is known not to block keyboard shorcuts
  9116. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9117. if (Busy > 0)
  9118. {
  9119. if (NonVisualDataModule->Busy)
  9120. {
  9121. State = false;
  9122. }
  9123. else
  9124. {
  9125. NonVisualDataModule->StartBusy();
  9126. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9127. LockWindow();
  9128. }
  9129. }
  9130. else if (Busy < 0)
  9131. {
  9132. UnlockWindow();
  9133. NonVisualDataModule->EndBusy();
  9134. }
  9135. else
  9136. {
  9137. State = NonVisualDataModule->Busy;
  9138. }
  9139. }
  9140. //---------------------------------------------------------------------------
  9141. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9142. {
  9143. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9144. // no context menu for "New session tab"
  9145. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9146. {
  9147. SessionsPageControl->ActivePageIndex = Index;
  9148. if (DebugAlwaysTrue(SessionTabSwitched()))
  9149. {
  9150. // copied from TControl.WMContextMenu
  9151. SendCancelMode(SessionsPageControl);
  9152. // explicit popup instead of using PopupMenu property
  9153. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9154. // while connecting yet not-connected session and hence
  9155. // allowing an access to commands over not-completelly connected session
  9156. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9157. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9158. PopupMenu->PopupComponent = SessionsPageControl;
  9159. PopupMenu->Popup(Point.x, Point.y);
  9160. }
  9161. }
  9162. Handled = true;
  9163. }
  9164. //---------------------------------------------------------------------------
  9165. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9166. {
  9167. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9168. }
  9169. //---------------------------------------------------------------------------
  9170. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9171. {
  9172. Configuration->Usage->Inc(L"PasswordChanges");
  9173. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9174. ChangePasswordData->DisableAuthentationsExceptPassword();
  9175. ChangePasswordData->ChangePassword = true;
  9176. std::unique_ptr<TTerminal> ChangePasswordSession(
  9177. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9178. ChangePasswordSession->AutoReadDirectory = false;
  9179. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9180. {
  9181. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9182. }
  9183. else
  9184. {
  9185. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9186. }
  9187. }
  9188. //---------------------------------------------------------------------------
  9189. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9190. {
  9191. // No nice way to assert SSH2
  9192. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9193. }
  9194. //---------------------------------------------------------------------------
  9195. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9196. {
  9197. TOperationVisualizer OperationVisualizer;
  9198. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9199. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9200. }
  9201. //---------------------------------------------------------------------------
  9202. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9203. {
  9204. TForm::ChangeScale(M, D);
  9205. int APixelsPerInch = GetControlPixelsPerInch(this);
  9206. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9207. try
  9208. {
  9209. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9210. }
  9211. __finally
  9212. {
  9213. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9214. }
  9215. }
  9216. //---------------------------------------------------------------------------
  9217. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9218. {
  9219. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9220. }
  9221. //---------------------------------------------------------------------
  9222. class TPublicControl : public TControl
  9223. {
  9224. friend class TCustomScpExplorerForm;
  9225. };
  9226. //---------------------------------------------------------------------------
  9227. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9228. {
  9229. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9230. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9231. }
  9232. //---------------------------------------------------------------------------
  9233. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9234. {
  9235. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9236. OpenBookmark(Side, Bookmark);
  9237. }
  9238. //---------------------------------------------------------------------------
  9239. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9240. {
  9241. DoBookmarkClick(osLocal, Sender);
  9242. }
  9243. //---------------------------------------------------------------------------
  9244. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9245. {
  9246. DoBookmarkClick(osRemote, Sender);
  9247. }
  9248. //---------------------------------------------------------------------------
  9249. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9250. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9251. {
  9252. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9253. if (!WinConfiguration->UseLocationProfiles)
  9254. {
  9255. std::unique_ptr<TStringList> Directories(new TStringList());
  9256. Directories->CaseSensitive = (Side == osRemote);
  9257. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9258. {
  9259. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9260. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9261. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9262. {
  9263. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9264. Item->Caption = EscapeHotkey(Directory);
  9265. Item->ShortCut = Bookmark->ShortCut;
  9266. Item->OnClick = OnBookmarkClick;
  9267. Item->Tag = reinterpret_cast<int>(Bookmark);
  9268. Directories->Add(Directory);
  9269. Menu->Add(Item.release());
  9270. }
  9271. }
  9272. }
  9273. else
  9274. {
  9275. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9276. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9277. {
  9278. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9279. if (!Bookmark->Node.IsEmpty())
  9280. {
  9281. Folders->Add(Bookmark->Node);
  9282. }
  9283. }
  9284. for (int Index = 0; Index < Folders->Count; Index++)
  9285. {
  9286. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9287. Item->Caption = Folders->Strings[Index];
  9288. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9289. Folders->Objects[Index] = Item.get();
  9290. Menu->Add(Item.release());
  9291. }
  9292. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9293. {
  9294. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9295. TTBCustomItem * Parent;
  9296. if (!Bookmark->Node.IsEmpty())
  9297. {
  9298. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9299. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9300. }
  9301. else
  9302. {
  9303. Parent = Menu;
  9304. }
  9305. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9306. Item->Caption = Bookmark->Name;
  9307. Item->ShortCut = Bookmark->ShortCut;
  9308. Item->OnClick = OnBookmarkClick;
  9309. Item->Tag = reinterpret_cast<int>(Bookmark);
  9310. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9311. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9312. {
  9313. // No hint for location profiles that are actually mere "bookmarks"
  9314. }
  9315. else
  9316. {
  9317. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9318. UnicodeString LongHint;
  9319. if (!Bookmark->Local.IsEmpty())
  9320. {
  9321. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9322. }
  9323. if (!Bookmark->Remote.IsEmpty())
  9324. {
  9325. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9326. }
  9327. AddToList(Hint, LongHint, L"|");
  9328. Item->Hint = Hint;
  9329. }
  9330. Parent->Add(Item.release());
  9331. }
  9332. }
  9333. }
  9334. //---------------------------------------------------------------------------
  9335. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9336. {
  9337. Menu->Clear();
  9338. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9339. AddMenuSeparator(Menu);
  9340. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9341. AddMenuSeparator(Menu);
  9342. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9343. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9344. Menu->Add(Item.release());
  9345. }
  9346. //---------------------------------------------------------------------------
  9347. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9348. {
  9349. bool Result = true;
  9350. try
  9351. {
  9352. if (Side == osRemote)
  9353. {
  9354. Terminal->ExceptionOnFail = true;
  9355. }
  9356. try
  9357. {
  9358. DirView(Side)->Path = Path;
  9359. }
  9360. __finally
  9361. {
  9362. if (Side == osRemote)
  9363. {
  9364. Terminal->ExceptionOnFail = false;
  9365. }
  9366. }
  9367. }
  9368. catch (Exception & E)
  9369. {
  9370. if ((Side != osRemote) || Terminal->Active)
  9371. {
  9372. ShowExtendedExceptionEx(Terminal, &E);
  9373. Result = false;
  9374. }
  9375. else
  9376. {
  9377. throw;
  9378. }
  9379. }
  9380. return Result;
  9381. }
  9382. //---------------------------------------------------------------------------
  9383. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9384. {
  9385. // Make sure there some feedback even when loading tine local folders
  9386. TInstantOperationVisualizer Visualizer;
  9387. DirView(Side)->ReloadDirectory();
  9388. }
  9389. //---------------------------------------------------------------------------
  9390. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9391. {
  9392. if (Index == SessionsPageControl->TabIndex)
  9393. {
  9394. CloseSession();
  9395. }
  9396. else
  9397. {
  9398. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9399. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9400. TTerminalManager * Manager = TTerminalManager::Instance();
  9401. if (!Terminal->Active)
  9402. {
  9403. Manager->FreeTerminal(Terminal);
  9404. }
  9405. else
  9406. {
  9407. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9408. if (CanCloseQueue(Queue))
  9409. {
  9410. Manager->FreeTerminal(Terminal);
  9411. }
  9412. }
  9413. }
  9414. }
  9415. //---------------------------------------------------------------------------
  9416. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side)
  9417. {
  9418. if (DebugAlwaysTrue(GetSide(Side) == osRemote))
  9419. {
  9420. TInstantOperationVisualizer Visualizer;
  9421. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9422. // before we create a new data object
  9423. ClipboardClear();
  9424. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9425. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9426. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9427. FClipboardFakeMonitors.reset(StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated));
  9428. if (FClipboardDragDropFilesEx.get() == NULL)
  9429. {
  9430. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9431. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9432. }
  9433. FClipboardDragDropFilesEx->FileList->Clear();
  9434. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9435. FClipboardDragDropFilesEx->CopyToClipboard();
  9436. FClipboardTerminal = FTerminal;
  9437. // Need full paths, as cwd can be different once files are pasted
  9438. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(false, true)));
  9439. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9440. {
  9441. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9442. }
  9443. }
  9444. }
  9445. //---------------------------------------------------------------------------
  9446. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9447. {
  9448. if (OpenClipboard(0))
  9449. {
  9450. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9451. CloseClipboard();
  9452. }
  9453. }
  9454. //---------------------------------------------------------------------------
  9455. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9456. {
  9457. return !FClipboardFakeDirectory.IsEmpty();
  9458. }
  9459. //---------------------------------------------------------------------------
  9460. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9461. {
  9462. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9463. FClipboardFakeDirectory = UnicodeString();
  9464. FClipboardTerminal = NULL;
  9465. // Also called in Idle()
  9466. FClipboardFakeMonitors.reset(NULL);
  9467. }
  9468. //---------------------------------------------------------------------------
  9469. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9470. {
  9471. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9472. {
  9473. ClipboardStop();
  9474. }
  9475. }
  9476. //---------------------------------------------------------------------------
  9477. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9478. {
  9479. TTerminalManager * Manager = TTerminalManager::Instance();
  9480. Manager->ActiveTerminal = FClipboardTerminal;
  9481. TTransferOperationParam Params;
  9482. Params.TargetDirectory = TargetDirectory;
  9483. Params.DragDrop = DragDrop;
  9484. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9485. }
  9486. //---------------------------------------------------------------------------
  9487. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9488. {
  9489. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9490. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9491. {
  9492. RemoveDir(ApiPath(FileName));
  9493. if (!NonVisualDataModule->Busy)
  9494. {
  9495. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9496. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9497. }
  9498. }
  9499. }
  9500. //---------------------------------------------------------------------------