CustomScpExplorer.cpp 285 KB

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