CustomScpExplorer.cpp 387 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700
  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 <algorithm>
  8. #include <Bookmarks.h>
  9. #include <Interface.h>
  10. #include <Exceptions.h>
  11. #include <CoreMain.h>
  12. #include <FileSystems.h>
  13. #include <TextsCore.h>
  14. #include <TextsWin.h>
  15. #include <HelpWin.h>
  16. #include <VCLCommon.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. #include <DirectoryMonitor.hpp>
  34. #include <System.IOUtils.hpp>
  35. //---------------------------------------------------------------------------
  36. #pragma package(smart_init)
  37. #pragma link "CustomDirView"
  38. #pragma link "CustomUnixDirView"
  39. #pragma link "IEListView"
  40. #pragma link "NortonLikeListView"
  41. #pragma link "UnixDirView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "UnixDriveView"
  44. #pragma link "CustomDriveView"
  45. #pragma link "TB2Dock"
  46. #pragma link "TBXStatusBars"
  47. #pragma link "TB2Item"
  48. #pragma link "TB2Toolbar"
  49. #pragma link "ThemePageControl"
  50. #pragma link "PathLabel"
  51. #pragma resource "*.dfm"
  52. //---------------------------------------------------------------------------
  53. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  54. static const int SessionPanelCount = 3;
  55. //---------------------------------------------------------------------------
  56. class TMutexGuard
  57. {
  58. public:
  59. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  60. int Timeout = 5000)
  61. {
  62. FMutex = NULL;
  63. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  64. if (WaitResult == WAIT_TIMEOUT)
  65. {
  66. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  67. }
  68. else
  69. {
  70. FMutex = AMutex;
  71. }
  72. }
  73. ~TMutexGuard()
  74. {
  75. if (FMutex != NULL)
  76. {
  77. ReleaseMutex(FMutex);
  78. }
  79. }
  80. private:
  81. HANDLE FMutex;
  82. };
  83. //---------------------------------------------------------------------------
  84. class TWindowLock
  85. {
  86. public:
  87. TWindowLock(TCustomScpExplorerForm * Form) :
  88. FForm(Form)
  89. {
  90. FForm->LockWindow();
  91. }
  92. ~TWindowLock()
  93. {
  94. FForm->UnlockWindow();
  95. }
  96. private:
  97. TCustomScpExplorerForm * FForm;
  98. };
  99. //---------------------------------------------------------------------------
  100. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  101. FForm(Form)
  102. {
  103. FForm->BatchStart(FBatchStorage);
  104. }
  105. //---------------------------------------------------------------------------
  106. TAutoBatch::~TAutoBatch()
  107. {
  108. FForm->BatchEnd(FBatchStorage);
  109. }
  110. //---------------------------------------------------------------------------
  111. struct TTransferOperationParam
  112. {
  113. TTransferOperationParam();
  114. UnicodeString TargetDirectory;
  115. bool Temp;
  116. bool DragDrop;
  117. int Options;
  118. TAutoSwitch Queue;
  119. };
  120. //---------------------------------------------------------------------------
  121. TTransferOperationParam::TTransferOperationParam()
  122. {
  123. Temp = false;
  124. DragDrop = false;
  125. Options = 0;
  126. Queue = asAuto;
  127. }
  128. //---------------------------------------------------------------------------
  129. class TTransferPresetNoteData : public TObject
  130. {
  131. public:
  132. UnicodeString Message;
  133. };
  134. //---------------------------------------------------------------------------
  135. class TTerminalNoteData : public TObject
  136. {
  137. public:
  138. TManagedTerminal * Terminal;
  139. };
  140. //---------------------------------------------------------------------------
  141. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  142. FFormRestored(false),
  143. TForm(Owner)
  144. {
  145. AddStartupSequence(L"F");
  146. FInvalid = false;
  147. FCurrentSide = osRemote;
  148. FEverShown = false;
  149. FDocks = new TList();
  150. InitControls();
  151. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  152. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  153. NonVisualDataModule->ScpExplorer = this;
  154. RestoreParams();
  155. ConfigurationChanged();
  156. RemoteDirView->Invalidate();
  157. FAutoOperation = false;
  158. FOnFileOperationFinished = NULL;
  159. FPrimaryOperation = foNone;
  160. FForceExecution = false;
  161. FIgnoreNextDialogChar = 0;
  162. FErrorList = NULL;
  163. FSynchronizeProgressForm = NULL;
  164. FProgressForm = NULL;
  165. FRefreshLocalDirectory = false;
  166. FRefreshRemoteDirectory = false;
  167. FDDExtMapFile = NULL;
  168. // CreateMutexW keeps failing with ERROR_NOACCESS
  169. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  170. DebugAssert(FDDExtMutex != NULL);
  171. FDDTargetControl = NULL;
  172. FDelayedDeletionTimer = NULL;
  173. FDelayedDeletionList = new TStringList();
  174. FDDFileList = NULL;
  175. FPendingTempSpaceWarn = false;
  176. FCapturedLog = NULL;
  177. FDragDropOperation = false;
  178. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  179. FAllowTransferPresetAutoSelect = true;
  180. FSessionChanging = false;
  181. FCopyParamDefault = L"";
  182. FSynchronizeController = NULL;
  183. FPendingQueueActionItem = NULL;
  184. FLockLevel = 0;
  185. FLockSuspendLevel = 0;
  186. FDisabledOnLockSuspend = false;
  187. FAlternativeDelete = false;
  188. FTrayIcon = new ::TTrayIcon(0);
  189. FTrayIcon->OnClick = TrayIconClick;
  190. FMaxQueueLength = 0;
  191. FLastContextPopupScreenPoint = TPoint(-1, -1);
  192. FTransferResumeList = NULL;
  193. FDeletedFiles = NULL;
  194. FMoveToQueue = false;
  195. StandaloneOperation = false;
  196. FOnFeedSynchronizeError = NULL;
  197. FOnSynchronizeAbort = NULL;
  198. FSynchronizeTerminal = NULL;
  199. FNeedSession = false;
  200. FDoNotIdleCurrentTerminal = 0;
  201. FIncrementalSearching = 0;
  202. FIncrementalSearchHaveNext = false;
  203. FQueueFileList.reset(new TQueueFileList());
  204. FProgressSide = osCurrent;
  205. FCalculateSizeOperation = NULL;
  206. FDownloadingFromClipboard = false;
  207. FClipboardFakeMonitorsPendingReset = false;
  208. FEditorManager = new TEditorManager();
  209. FEditorManager->OnFileChange = ExecutedFileChanged;
  210. FEditorManager->OnFileReload = ExecutedFileReload;
  211. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  212. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  213. FLocalEditors = new TList();
  214. FQueueStatus = NULL;
  215. FQueueStatusSection = new TCriticalSection();
  216. FQueueStatusInvalidated = false;
  217. FQueueItemInvalidated = false;
  218. FQueueStatusUpdating = false;
  219. FQueueActedItem = NULL;
  220. FQueueController = new TQueueController(QueueView3);
  221. FUserActionTimer = new TTimer(this);
  222. FUserActionTimer->Enabled = false;
  223. FUserActionTimer->Interval = 10;
  224. FUserActionTimer->OnTimer = UserActionTimer;
  225. FNotes = new TStringList();
  226. FNoteTimer = new TTimer(this);
  227. FNoteTimer->Enabled = false;
  228. FNoteTimer->OnTimer = NoteTimer;
  229. FOnNoteClick = NULL;
  230. FOle32Library = LoadLibrary(L"Ole32.dll");
  231. FDragMoveCursor = FOle32Library != NULL ?
  232. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  233. UseSystemSettings(this);
  234. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  235. static_cast<TObject*>(GetComponent(fcTransferList)));
  236. DebugAssert(TransferList != NULL);
  237. FTransferListHoverIndex = -1;
  238. TransferList->OnChange = TransferListChange;
  239. TransferList->OnDrawItem = TransferListDrawItem;
  240. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  241. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  242. UseDesktopFont(SessionsPageControl);
  243. UpdateSessionsPageControlHeight();
  244. UseDesktopFont(RemoteDirView);
  245. UseDesktopFont(RemoteDriveView);
  246. UseDesktopFont(QueueView3);
  247. UseDesktopFont(QueueFileList);
  248. UseDesktopFont(QueueLabel);
  249. UseDesktopFont(RemoteStatusBar);
  250. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  251. QueueSplitter->ShowHint = true;
  252. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  253. QueueFileListSplitter->ShowHint = true;
  254. RemotePanelSplitter->ShowHint = true;
  255. UpdateImages();
  256. FCustomCommandMenu = CreateTBXPopupMenu(this);
  257. FCustomCommandLocalFileList = NULL;
  258. FCustomCommandRemoteFileList = NULL;
  259. FSessionColors = new TPngImageList(this);
  260. FSessionColors->ColorDepth = cd32Bit;
  261. AddFixedSessionImages();
  262. SessionsPageControl->Images = FSessionColors;
  263. UpdateQueueLabel();
  264. CheckStoreTransition();
  265. CreateHiddenWindow();
  266. StartUpdates();
  267. }
  268. //---------------------------------------------------------------------------
  269. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  270. {
  271. FInvalid = true;
  272. // this has to be one of the very first things to do
  273. StopUpdateThread();
  274. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  275. TTerminalManager::Instance()->ScpExplorer = NULL;
  276. delete FCustomCommandLocalFileList;
  277. delete FCustomCommandRemoteFileList;
  278. delete FCustomCommandMenu;
  279. delete FTrayIcon;
  280. FTrayIcon = NULL;
  281. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  282. delete FEditorManager;
  283. ForceCloseLocalEditors();
  284. delete FLocalEditors;
  285. HideFileFindDialog();
  286. if (FDelayedDeletionTimer)
  287. {
  288. DoDelayedDeletion(NULL);
  289. SAFE_DESTROY(FDelayedDeletionTimer);
  290. }
  291. SAFE_DESTROY(FDelayedDeletionList);
  292. // sometimes we do not get DDEnd so the list is not released
  293. SAFE_DESTROY(FDDFileList);
  294. DebugAssert(FSynchronizeController == NULL);
  295. CloseHandle(FDDExtMutex);
  296. FDDExtMutex = NULL;
  297. FreeLibrary(FOle32Library);
  298. FOle32Library = NULL;
  299. FDragMoveCursor = NULL;
  300. DebugAssert(!FErrorList);
  301. if (FEverShown)
  302. {
  303. // when window is never shown (like when running command-line operation),
  304. // particularly window site is not restored correctly (BoundsRect value set
  305. // in RestoreForm gets lost during handle allocation), so we do not want
  306. // it to be stored
  307. StoreParams();
  308. }
  309. ManagedSession = NULL;
  310. Queue = NULL;
  311. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  312. NonVisualDataModule->ScpExplorer = NULL;
  313. delete FQueueController;
  314. FQueueController = NULL;
  315. delete FQueueStatusSection;
  316. FQueueStatusSection = NULL;
  317. delete FQueueStatus;
  318. FQueueStatus = NULL;
  319. delete FUserActionTimer;
  320. FUserActionTimer = NULL;
  321. delete FNoteTimer;
  322. delete FNotes;
  323. delete FNoteData;
  324. SAFE_DESTROY(FDocks);
  325. SessionsPageControl->Images = NULL;
  326. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  327. SAFE_DESTROY(FSessionColors);
  328. SAFE_DESTROY(FSessionsDragDropFilesEx);
  329. SAFE_DESTROY(FHistoryMenu[0][0]);
  330. SAFE_DESTROY(FHistoryMenu[0][1]);
  331. SAFE_DESTROY(FHistoryMenu[1][0]);
  332. SAFE_DESTROY(FHistoryMenu[1][1]);
  333. if (FHiddenWindow != NULL)
  334. {
  335. DestroyWindow(FHiddenWindow);
  336. FHiddenWindow = NULL;
  337. }
  338. }
  339. //---------------------------------------------------------------------------
  340. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  341. {
  342. std::unique_ptr<TSessionData> Data;
  343. if (!Session.IsEmpty())
  344. {
  345. bool DefaultsOnly;
  346. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  347. }
  348. TTerminalManager * Manager = TTerminalManager::Instance();
  349. for (int Index = 0; Index < Manager->Count; Index++)
  350. {
  351. TTerminal * Terminal = Manager->Sessions[Index];
  352. if (Session.IsEmpty() ||
  353. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  354. {
  355. if (Path.IsEmpty())
  356. {
  357. Terminal->ClearCaches();
  358. }
  359. else
  360. {
  361. Terminal->DirectoryModified(Path, true);
  362. }
  363. }
  364. }
  365. // We should flag a pending refresh for the background terminals or busy foreground terminals
  366. if (HasActiveTerminal() &&
  367. CanCommandLineFromAnotherInstance() &&
  368. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  369. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  370. {
  371. Terminal->ReloadDirectory();
  372. }
  373. }
  374. //---------------------------------------------------------------------------
  375. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  376. {
  377. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  378. size_t MessageSize = sizeof(TCopyDataMessage);
  379. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  380. if (Result)
  381. {
  382. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  383. Result = (Message.Version == TCopyDataMessage::Version1);
  384. if (Result)
  385. {
  386. switch (Message.Command)
  387. {
  388. case TCopyDataMessage::CommandCanCommandLine:
  389. Result = CanCommandLineFromAnotherInstance();
  390. break;
  391. case TCopyDataMessage::CommandCommandLine:
  392. {
  393. UnicodeString CommandLine(Message.CommandLine);
  394. Result = CommandLineFromAnotherInstance(CommandLine);
  395. }
  396. break;
  397. case TCopyDataMessage::RefreshPanel:
  398. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  399. break;
  400. case TCopyDataMessage::MainWindowCheck:
  401. Result = true;
  402. break;
  403. default:
  404. Result = false;
  405. break;
  406. }
  407. }
  408. }
  409. Message.Result = Result ? 1 : 0;
  410. }
  411. //---------------------------------------------------------------------------
  412. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  413. {
  414. WNDCLASS WindowClass = {0};
  415. WindowClass.lpfnWndProc = DefWindowProc;
  416. WindowClass.hInstance = HInstance;
  417. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  418. FHiddenWindow = NULL;
  419. if (RegisterClass(&WindowClass))
  420. {
  421. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  422. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  423. }
  424. }
  425. //---------------------------------------------------------------------------
  426. bool __fastcall TCustomScpExplorerForm::CanConsole()
  427. {
  428. return
  429. HasActiveTerminal() &&
  430. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  431. }
  432. //---------------------------------------------------------------------------
  433. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  434. {
  435. bool Result = Visible && !NonVisualDataModule->Busy;
  436. return Result;
  437. }
  438. //---------------------------------------------------------------------------
  439. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  440. const UnicodeString & CommandLine)
  441. {
  442. TProgramParams Params(CommandLine);
  443. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  444. if (Result)
  445. {
  446. NonVisualDataModule->StartBusy();
  447. try
  448. {
  449. // this action is initiated from another process,
  450. // so it's likely that our window is not visible,
  451. // and user won't see what is going on
  452. Application->BringToFront();
  453. // reload sessions as we may be asked to open a session
  454. // just stored by another instance
  455. StoredSessions->Reload();
  456. UnicodeString SessionName = Params.Param[1];
  457. std::unique_ptr<TObjectList> DataList(new TObjectList());
  458. try
  459. {
  460. UnicodeString DownloadFile; // unused
  461. int Flags = GetCommandLineParseUrlFlags(&Params);
  462. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  463. }
  464. catch (EAbort &)
  465. {
  466. // Happens when opening session in PuTTY and in other situations,
  467. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  468. // to process the command-line on its own (or to pass it to yet another instance)
  469. }
  470. if (DataList->Count > 0)
  471. {
  472. TTerminalManager * Manager = TTerminalManager::Instance();
  473. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  474. }
  475. }
  476. __finally
  477. {
  478. NonVisualDataModule->EndBusy();
  479. }
  480. }
  481. return Result;
  482. }
  483. //---------------------------------------------------------------------------
  484. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  485. {
  486. TManagedTerminal * Result;
  487. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  488. {
  489. Result = FManagedSession;
  490. }
  491. else
  492. {
  493. Result = NULL;
  494. }
  495. return Result;
  496. }
  497. //---------------------------------------------------------------------------
  498. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  499. {
  500. if (FManagedSession != value)
  501. {
  502. SessionChanging();
  503. DoSetManagedSession(value, false);
  504. }
  505. }
  506. //---------------------------------------------------------------------------
  507. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  508. {
  509. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  510. FManagedSession = value;
  511. {
  512. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  513. FAllowTransferPresetAutoSelect = false;
  514. TAutoFlag SessionChangingFlag(FSessionChanging);
  515. SessionChanged(Replace);
  516. }
  517. if (Terminal != NULL)
  518. {
  519. TransferPresetAutoSelect();
  520. }
  521. // Update app and tab titles, prevented above by FSessionChanging.
  522. UpdateControls();
  523. }
  524. //---------------------------------------------------------------------------
  525. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  526. {
  527. DoSetManagedSession(value, true);
  528. }
  529. //---------------------------------------------------------------------------
  530. void __fastcall TCustomScpExplorerForm::SessionChanging()
  531. {
  532. if (ManagedSession != NULL)
  533. {
  534. UpdateSession(ManagedSession);
  535. }
  536. }
  537. //---------------------------------------------------------------------------
  538. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  539. {
  540. if (Terminal != NULL)
  541. {
  542. UpdateSessionColor((TColor)Terminal->StateData->Color);
  543. }
  544. if (!TTerminalManager::Instance()->Updating)
  545. {
  546. SessionListChanged();
  547. }
  548. if (Terminal != NULL)
  549. {
  550. TObject * State = NULL;
  551. if (WinConfiguration->PreservePanelState)
  552. {
  553. State = Terminal->RemoteExplorerState;
  554. DirView(osRemote)->AnnounceState(State);
  555. }
  556. try
  557. {
  558. if (Replaced)
  559. {
  560. RemoteDirView->ReplaceTerminal(Terminal);
  561. }
  562. else
  563. {
  564. RemoteDirView->Terminal = Terminal;
  565. }
  566. if (Terminal->Active)
  567. {
  568. Terminal->RefreshDirectory();
  569. }
  570. if (State != NULL)
  571. {
  572. DirView(osRemote)->RestoreState(State);
  573. }
  574. }
  575. __finally
  576. {
  577. DirView(osRemote)->AnnounceState(NULL);
  578. }
  579. }
  580. else
  581. {
  582. RemoteDirView->Terminal = NULL;
  583. }
  584. InitStatusBar();
  585. UpdateTransferList();
  586. // Update panels Enable state before refreshing the labels
  587. UpdateControls();
  588. }
  589. //---------------------------------------------------------------------------
  590. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  591. {
  592. if (Queue != value)
  593. {
  594. if (FQueue != NULL)
  595. {
  596. FQueue->OnListUpdate = NULL;
  597. FQueue->OnQueueItemUpdate = NULL;
  598. }
  599. FQueue = value;
  600. if (FQueue != NULL)
  601. {
  602. DebugAssert(FQueue->OnListUpdate == NULL);
  603. FQueue->OnListUpdate = QueueListUpdate;
  604. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  605. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  606. }
  607. QueueChanged();
  608. }
  609. }
  610. //---------------------------------------------------------------------------
  611. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  612. TListItem * Item)
  613. {
  614. if (FQueueActedItem == Item)
  615. {
  616. FQueueActedItem = NULL;
  617. if ((QueueView3->PopupMenu != NULL) &&
  618. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  619. {
  620. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  621. ReleaseCapture();
  622. }
  623. }
  624. if (Item->Data == FPendingQueueActionItem)
  625. {
  626. FPendingQueueActionItem = NULL;
  627. }
  628. }
  629. //---------------------------------------------------------------------------
  630. bool TCustomScpExplorerForm::IsAnythingQueued()
  631. {
  632. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  633. }
  634. //---------------------------------------------------------------------------
  635. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  636. {
  637. {
  638. TGuard Guard(FQueueStatusSection);
  639. FQueueStatusInvalidated = false;
  640. if (FQueue != NULL)
  641. {
  642. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  643. }
  644. }
  645. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  646. {
  647. FMaxQueueLength = FQueueStatus->Count;
  648. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  649. }
  650. {
  651. TAutoFlag Flag(FQueueStatusUpdating);
  652. FQueueController->UpdateQueueStatus(FQueueStatus);
  653. }
  654. UpdateQueueFileList();
  655. SetQueueProgress();
  656. UpdateQueueView();
  657. bool IsEmpty = !IsAnythingQueued();
  658. if (IsEmpty && (Terminal != NULL))
  659. {
  660. TOnceDoneOperation OnceDoneOperation =
  661. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  662. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  663. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  664. {
  665. FQueue->Enabled = false;
  666. }
  667. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  668. {
  669. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  670. }
  671. }
  672. }
  673. //---------------------------------------------------------------------------
  674. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  675. {
  676. UnicodeString Result;
  677. if (FQueueStatus != NULL)
  678. {
  679. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  680. {
  681. TFileOperationProgressType * ProgressData =
  682. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  683. if ((ProgressData != NULL) && ProgressData->InProgress)
  684. {
  685. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  686. }
  687. }
  688. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  689. {
  690. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  691. }
  692. }
  693. return Result;
  694. }
  695. //---------------------------------------------------------------------------
  696. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  697. {
  698. ComponentVisible[fcQueueView] =
  699. !IsLocalBrowserMode() &&
  700. ((WinConfiguration->QueueView.Show == qvShow) ||
  701. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  702. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  703. }
  704. //---------------------------------------------------------------------------
  705. void __fastcall TCustomScpExplorerForm::QueueChanged()
  706. {
  707. if (FQueueStatus != NULL)
  708. {
  709. // UpdateFileList implementation relies on the status being recreated when session changes
  710. delete FQueueStatus;
  711. FQueueStatus = NULL;
  712. }
  713. UpdateQueueStatus(true);
  714. }
  715. //---------------------------------------------------------------------------
  716. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  717. {
  718. if (FQueue == Queue)
  719. {
  720. FQueueStatusInvalidated = true;
  721. }
  722. }
  723. //---------------------------------------------------------------------------
  724. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  725. TQueueItem * Item)
  726. {
  727. if (FQueue == Queue)
  728. {
  729. TGuard Guard(FQueueStatusSection);
  730. // this may be running in parallel with QueueChanged
  731. if (FQueueStatus != NULL)
  732. {
  733. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  734. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  735. {
  736. FRefreshLocalDirectory = (QueueItem == NULL) ||
  737. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  738. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  739. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  740. }
  741. if (QueueItem != NULL)
  742. {
  743. QueueItem->UserData = (void*)true;
  744. FQueueItemInvalidated = true;
  745. }
  746. }
  747. }
  748. }
  749. //---------------------------------------------------------------------------
  750. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  751. {
  752. // during standalone editing, we have no way to see/control queue,
  753. // so we have to always popup prompts automatically
  754. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  755. }
  756. //---------------------------------------------------------------------------
  757. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  758. {
  759. if (FQueueStatus != NULL)
  760. {
  761. bool QueueAutoPopup = IsQueueAutoPopup();
  762. bool NeedRefresh = FQueueController->NeedRefresh();
  763. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  764. FQueueItemInvalidated = false;
  765. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  766. bool Updated = false;
  767. TQueueItemProxy * QueueItem;
  768. bool WasUserAction;
  769. for (int Index = 0; Index < Limit; Index++)
  770. {
  771. bool Update = false;
  772. QueueItem = FQueueStatus->Items[Index];
  773. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  774. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  775. (FPendingQueueActionItem == NULL))
  776. {
  777. FPendingQueueActionItem = QueueItem;
  778. FUserActionTimer->Enabled = true;
  779. }
  780. if ((bool)QueueItem->UserData)
  781. {
  782. QueueItem->UserData = (void*)false;
  783. QueueItem->Update();
  784. Updated = true;
  785. Update = true;
  786. }
  787. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  788. {
  789. Update = true;
  790. }
  791. if (Update)
  792. {
  793. FQueueController->RefreshQueueItem(QueueItem);
  794. }
  795. }
  796. if (Updated)
  797. {
  798. NonVisualDataModule->UpdateNonVisibleActions();
  799. SetQueueProgress();
  800. UpdateQueueFileList();
  801. }
  802. }
  803. }
  804. //---------------------------------------------------------------------------
  805. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  806. {
  807. if (FTaskbarList != NULL)
  808. {
  809. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  810. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  811. }
  812. }
  813. //---------------------------------------------------------------------------
  814. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  815. {
  816. if (Progress >= 0)
  817. {
  818. if (FTaskbarList != NULL)
  819. {
  820. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  821. }
  822. }
  823. else
  824. {
  825. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  826. }
  827. }
  828. //---------------------------------------------------------------------------
  829. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  830. {
  831. int OverallProgress;
  832. // FProgressForm is null when this is called from SetQueueProgress
  833. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  834. {
  835. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  836. }
  837. else if (!ProgressData->IsIndeterminate())
  838. {
  839. OverallProgress = ProgressData->OverallProgress();
  840. }
  841. else
  842. {
  843. OverallProgress = -1;
  844. }
  845. SetTaskbarListProgressValue(OverallProgress);
  846. }
  847. //---------------------------------------------------------------------------
  848. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  849. {
  850. TTerminalManager::Instance()->QueueStatusUpdated();
  851. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  852. {
  853. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  854. {
  855. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  856. {
  857. TFileOperationProgressType * ProgressData = NULL;
  858. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  859. {
  860. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  861. }
  862. if ((ProgressData != NULL) &&
  863. ProgressData->InProgress)
  864. {
  865. SetTaskbarListProgressValue(ProgressData);
  866. }
  867. else
  868. {
  869. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  870. }
  871. }
  872. else
  873. {
  874. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  875. }
  876. }
  877. else
  878. {
  879. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  880. }
  881. }
  882. UpdateQueueLabel();
  883. }
  884. //---------------------------------------------------------------------------
  885. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  886. {
  887. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  888. if (FQueueStatus != NULL)
  889. {
  890. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  891. if (ActiveAndPendingPrimaryCount > 0)
  892. {
  893. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  894. }
  895. }
  896. QueueLabel->Caption = Caption;
  897. }
  898. //---------------------------------------------------------------------------
  899. void TCustomScpExplorerForm::CheckStoreTransition()
  900. {
  901. if (WinConfiguration->StoreTransition == stInit)
  902. {
  903. if (IsUWP())
  904. {
  905. WinConfiguration->StoreTransition = stStoreFresh;
  906. AppLog(L"Fresh UWP/store installation");
  907. }
  908. else
  909. {
  910. WinConfiguration->StoreTransition = stStandard;
  911. AppLog(L"Fresh standard installation");
  912. }
  913. }
  914. else if ((WinConfiguration->StoreTransition == stStandard) && IsUWP())
  915. {
  916. WinConfiguration->StoreTransition = stStoreMigrated;
  917. AppLog(L"Standard installation migrated to store installation");
  918. }
  919. if (WinConfiguration->StoreTransition == stStoreMigrated)
  920. {
  921. TTBXToolbar * Toolbar = new TTBXToolbar(MessageDock);
  922. Toolbar->Parent = MessageDock;
  923. Toolbar->FullSize = true;
  924. Toolbar->Color = Application->HintColor;
  925. TTBXLabelItem * LabelItem;
  926. LabelItem = new TTBXLabelItem(Toolbar);
  927. LabelItem->Margin = ScaleByTextHeight(MessageDock, 2); // Is this automatically rescaled?
  928. LabelItem->Caption = LoadStr(STORE_MIGRATION_THANKS);
  929. Toolbar->Items->Add(LabelItem);
  930. TTBXCustomItem * LinkItem = new TTBXItem(Toolbar);
  931. LinkItem->FontSettings->Color = LinkColor;
  932. LinkItem->Caption = LoadStr(STORE_MIGRATION_LEARN);
  933. LinkItem->OnClick = StoreTransitionLinkClick;
  934. Toolbar->Items->Add(LinkItem);
  935. AddMenuSeparator(Toolbar->Items);
  936. TTBCustomItem * CloseItem = new TTBXItem(Toolbar);
  937. CloseItem->Caption = LoadStr(MESSAGE_DISMISS);
  938. CloseItem->OnClick = StoreTransitionCloseClick;
  939. Toolbar->Items->Add(CloseItem);
  940. GiveTBItemPriority(CloseItem);
  941. }
  942. }
  943. //---------------------------------------------------------------------------
  944. void __fastcall TCustomScpExplorerForm::StoreTransitionLinkClick(TObject *)
  945. {
  946. ShowHelp(HELP_STORE_TRANSITION);
  947. }
  948. //---------------------------------------------------------------------------
  949. void __fastcall TCustomScpExplorerForm::StoreTransitionCloseClick(TObject * Sender)
  950. {
  951. TTBCustomItem * Item = DebugNotNull(dynamic_cast<TTBCustomItem *>(Sender));
  952. TTBXToolbar * Toolbar = DebugNotNull(dynamic_cast<TTBXToolbar *>(DebugNotNull(DebugNotNull(Item->Parent)->ParentComponent)));
  953. delete Toolbar;
  954. WinConfiguration->StoreTransition = stStoreAcknowledged;
  955. UpdateControls();
  956. }
  957. //---------------------------------------------------------------------------
  958. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  959. {
  960. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  961. static_cast<TComponent*>(GetComponent(fcTransferList)));
  962. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  963. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  964. TransferList->Strings->BeginUpdate();
  965. try
  966. {
  967. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  968. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  969. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  970. if (FTransferDropDownHint.IsEmpty())
  971. {
  972. FTransferDropDownHint = TransferDropDown->Hint;
  973. }
  974. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  975. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  976. TransferDropDown->Text = StripHotkey(Name);
  977. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  978. (FTransferDropDownHint, StripHotkey(Name),
  979. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  980. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  981. // update the label, otherwise when it is updated only on the first draw
  982. // of the list, it is drawn "bold" for some reason
  983. FTransferListHoverIndex = TransferList->ItemIndex;
  984. UpdateTransferLabel();
  985. }
  986. __finally
  987. {
  988. TransferList->Strings->EndUpdate();
  989. }
  990. }
  991. //---------------------------------------------------------------------------
  992. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  993. {
  994. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  995. DebugAssert(Toolbar != NULL);
  996. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  997. }
  998. //---------------------------------------------------------------------------
  999. void __fastcall TCustomScpExplorerForm::UpdateActions()
  1000. {
  1001. TForm::UpdateActions();
  1002. if (ComponentVisible[fcCustomCommandsBand])
  1003. {
  1004. UpdateCustomCommandsToolbar();
  1005. }
  1006. }
  1007. //---------------------------------------------------------------------------
  1008. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  1009. {
  1010. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  1011. }
  1012. //---------------------------------------------------------------------------
  1013. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  1014. {
  1015. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  1016. {
  1017. DirView->RowSelect = WinConfiguration->FullRowSelect;
  1018. // Without this, the panel turns gray and unused part of header turns black
  1019. DirView->Perform(CM_COLORCHANGED, 0, 0);
  1020. }
  1021. }
  1022. //---------------------------------------------------------------------------
  1023. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  1024. {
  1025. Color = GetBtnFaceColor();
  1026. DebugAssert(Configuration && RemoteDirView);
  1027. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1028. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  1029. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  1030. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  1031. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1032. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1033. UpdateRowSelect(RemoteDirView);
  1034. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1035. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  1036. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  1037. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1038. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1039. UpdateSessionsPageControlHeight();
  1040. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1041. UpdateToolbarDisplayMode();
  1042. UpdateTransferList();
  1043. if (Terminal != NULL)
  1044. {
  1045. TransferPresetAutoSelect();
  1046. }
  1047. UpdateQueueView();
  1048. UpdateControls();
  1049. // this can be called even before constuctor finishes.
  1050. if (FEditorManager != NULL)
  1051. {
  1052. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  1053. }
  1054. // this can be called even before constuctor finishes.
  1055. if (FLocalEditors != NULL)
  1056. {
  1057. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  1058. {
  1059. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  1060. }
  1061. }
  1062. if (ComponentVisible[fcCustomCommandsBand])
  1063. {
  1064. UpdateCustomCommandsToolbar();
  1065. }
  1066. // show only when keeping queue items forever,
  1067. // otherwise, it's enough to have it in the context menu
  1068. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1069. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1070. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1071. {
  1072. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1073. FFileColorsCurrent = WinConfiguration->FileColors;
  1074. FileColorsChanged();
  1075. }
  1076. }
  1077. //---------------------------------------------------------------------------
  1078. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1079. {
  1080. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1081. DirView->Invalidate();
  1082. }
  1083. //---------------------------------------------------------------------------
  1084. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1085. {
  1086. DoFileColorsChanged(RemoteDirView);
  1087. }
  1088. //---------------------------------------------------------------------------
  1089. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1090. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1091. void * /*Arg*/)
  1092. {
  1093. if (Token != NULL)
  1094. {
  1095. TForm * Editor = dynamic_cast<TForm*>(Token);
  1096. DebugAssert(Editor != NULL);
  1097. ReconfigureEditorForm(Editor);
  1098. }
  1099. }
  1100. //---------------------------------------------------------------------------
  1101. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1102. {
  1103. WinConfiguration->DDTransferConfirmation = asOn;
  1104. }
  1105. //---------------------------------------------------------------------------
  1106. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1107. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1108. {
  1109. // noop
  1110. }
  1111. //---------------------------------------------------------------------------
  1112. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1113. {
  1114. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1115. }
  1116. //---------------------------------------------------------------------------
  1117. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1118. {
  1119. if (DoNotShowAgain)
  1120. {
  1121. if (DragDrop)
  1122. {
  1123. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1124. {
  1125. PopupTrayBalloon(
  1126. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1127. }
  1128. WinConfiguration->DDTransferConfirmation = asOff;
  1129. }
  1130. else
  1131. {
  1132. WinConfiguration->ConfirmTransferring = false;
  1133. }
  1134. }
  1135. else
  1136. {
  1137. // User explicitly unchecked "do not show again",
  1138. // so show him the dialog the next time
  1139. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1140. {
  1141. WinConfiguration->DDTransferConfirmation = asOn;
  1142. }
  1143. }
  1144. }
  1145. //---------------------------------------------------------------------------
  1146. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1147. TTransferDirection Direction, TTransferType Type, bool Temp,
  1148. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1149. bool Confirm, bool DragDrop, int Options)
  1150. {
  1151. bool Result = true;
  1152. DebugAssert(HasActiveTerminal());
  1153. // these parameters are known in advance
  1154. int Params =
  1155. FLAGMASK(Type == ttMove, cpDelete);
  1156. bool ToTemp = (Temp && (Direction == tdToLocal));
  1157. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1158. {
  1159. bool DisableNewerOnly =
  1160. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1161. ToTemp;
  1162. Options |=
  1163. FLAGMASK(ToTemp, coTemp) |
  1164. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1165. coDoNotShowAgain;
  1166. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1167. int CopyParamAttrs = (Direction == tdToRemote ?
  1168. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1169. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1170. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1171. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1172. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1173. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1174. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1175. if (Result)
  1176. {
  1177. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1178. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1179. }
  1180. }
  1181. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1182. {
  1183. Configuration->Usage->Inc(L"TransfersOnBackground");
  1184. // these parameter are known only after transfer dialog
  1185. Params |=
  1186. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1187. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1188. Result = false;
  1189. ClearTransferSourceSelection(Direction);
  1190. }
  1191. return Result;
  1192. }
  1193. //---------------------------------------------------------------------------
  1194. void TCustomScpExplorerForm::ClearOperationSelection(TOperationSide Side)
  1195. {
  1196. if (FOnFileOperationFinished != NULL)
  1197. {
  1198. FOnFileOperationFinished(Side, UnicodeString(), true);
  1199. }
  1200. else
  1201. {
  1202. if (HasDirView[Side])
  1203. {
  1204. DirView(Side)->SelectAll(smNone);
  1205. }
  1206. }
  1207. }
  1208. //---------------------------------------------------------------------------
  1209. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1210. {
  1211. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1212. ClearOperationSelection(Side);
  1213. }
  1214. //---------------------------------------------------------------------------
  1215. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1216. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1217. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1218. int Params)
  1219. {
  1220. DebugAssert(Queue != NULL);
  1221. bool SingleFile = false;
  1222. if (FileList->Count == 1)
  1223. {
  1224. if (Direction == tdToRemote)
  1225. {
  1226. UnicodeString FileName = FileList->Strings[0];
  1227. SingleFile = FileExists(ApiPath(FileName));
  1228. }
  1229. else
  1230. {
  1231. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1232. SingleFile = !File->IsDirectory;
  1233. }
  1234. }
  1235. TQueueItem * QueueItem;
  1236. if (Direction == tdToRemote)
  1237. {
  1238. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1239. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1240. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1241. }
  1242. else
  1243. {
  1244. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1245. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1246. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1247. }
  1248. AddQueueItem(Queue, QueueItem, Terminal);
  1249. }
  1250. //---------------------------------------------------------------------------
  1251. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1252. {
  1253. if (Queue->IsEmpty)
  1254. {
  1255. Terminal->QueueOperationStart = Now();
  1256. }
  1257. Queue->AddItem(QueueItem);
  1258. }
  1259. //---------------------------------------------------------------------------
  1260. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1261. {
  1262. }
  1263. //---------------------------------------------------------------------------
  1264. void __fastcall TCustomScpExplorerForm::InitControls()
  1265. {
  1266. DebugAssert(FDocks != NULL);
  1267. for (int Index = 0; Index < ComponentCount; Index++)
  1268. {
  1269. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1270. if ((Dock != NULL) && (Dock->Tag == 0))
  1271. {
  1272. FDocks->Add(Dock);
  1273. }
  1274. }
  1275. CollectItemsWithTextDisplayMode(this);
  1276. }
  1277. //---------------------------------------------------------------------------
  1278. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1279. {
  1280. ConfigurationChanged();
  1281. QueuePanel->Height =
  1282. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1283. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1284. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1285. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1286. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1287. QueueFileList->Height =
  1288. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1289. if (QueueFileList->Visible)
  1290. {
  1291. AdjustQueueLayout();
  1292. }
  1293. UpdateCustomCommandsToolbar();
  1294. }
  1295. //---------------------------------------------------------------------------
  1296. void __fastcall TCustomScpExplorerForm::StoreParams()
  1297. {
  1298. int APixelsPerInch = GetControlPixelsPerInch(this);
  1299. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1300. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1301. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1302. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1303. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1304. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1305. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1306. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1307. }
  1308. //---------------------------------------------------------------------------
  1309. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1310. {
  1311. if (!FFormRestored)
  1312. {
  1313. FFormRestored = true;
  1314. RestoreFormParams();
  1315. }
  1316. TForm::CreateParams(Params);
  1317. }
  1318. //---------------------------------------------------------------------------
  1319. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1320. {
  1321. DebugAssert(FDocks != NULL);
  1322. for (int Index = 0; Index < FDocks->Count; Index++)
  1323. {
  1324. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1325. }
  1326. }
  1327. //---------------------------------------------------------------------------
  1328. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1329. {
  1330. SetDockAllowDrag(true);
  1331. while (!ButtonsStr.IsEmpty())
  1332. {
  1333. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1334. UnicodeString Name = CutToChar(S, L'=', true);
  1335. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1336. {
  1337. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1338. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1339. {
  1340. while (!S.IsEmpty())
  1341. {
  1342. UnicodeString S2 = CutToChar(S, L',', true);
  1343. UnicodeString Name = CutToChar(S2, L':', true);
  1344. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1345. {
  1346. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1347. if (SameText(GetToolbarItemName(Item), Name))
  1348. {
  1349. // forward compatibility
  1350. UnicodeString S3 = CutToChar(S2, L':', true);
  1351. bool Visible = StrToIntDef(S3, 0);
  1352. Item->Visible = Visible;
  1353. }
  1354. }
  1355. }
  1356. }
  1357. }
  1358. }
  1359. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1360. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1361. }
  1362. //---------------------------------------------------------------------------
  1363. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1364. {
  1365. UnicodeString Result;
  1366. SetDockAllowDrag(true);
  1367. Result = ::GetToolbarsLayoutStr(this);
  1368. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1369. return Result;
  1370. }
  1371. //---------------------------------------------------------------------------
  1372. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1373. {
  1374. UnicodeString Result;
  1375. if (Item->Action != NULL)
  1376. {
  1377. Result = Item->Action->Name;
  1378. Result = RemoveSuffix(Result, L"Action", true);
  1379. }
  1380. else
  1381. {
  1382. Result = Item->Name;
  1383. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1384. Result = RemoveSuffix(Result, L"Item", true);
  1385. }
  1386. return Result;
  1387. }
  1388. //---------------------------------------------------------------------------
  1389. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1390. {
  1391. UnicodeString Result;
  1392. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1393. {
  1394. UnicodeString ToolbarStr;
  1395. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1396. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1397. {
  1398. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1399. {
  1400. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1401. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1402. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1403. // and save the changes here only.
  1404. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1405. {
  1406. UnicodeString Name = GetToolbarItemName(Item);
  1407. DebugAssert(Name.Pos(L"TBX") == 0);
  1408. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1409. }
  1410. }
  1411. }
  1412. if (!ToolbarStr.IsEmpty())
  1413. {
  1414. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1415. }
  1416. }
  1417. return Result;
  1418. }
  1419. //---------------------------------------------------------------------------
  1420. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1421. {
  1422. DebugAssert(FProgressForm == NULL);
  1423. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1424. bool AllowMoveToQueue = (FTransferResumeList != NULL) || (FDeletedFiles != NULL);
  1425. FProgressForm = new TProgressForm(Application, AllowMoveToQueue, AllowSkip, SynchronizeProgress);
  1426. FProgressForm->DeleteLocalToRecycleBin =
  1427. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1428. FProgressForm->DeleteRemoteToRecycleBin =
  1429. HasActiveTerminal() &&
  1430. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1431. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1432. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1433. LockWindow();
  1434. // Actually, do not know what hides the progress once the operation finishes
  1435. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1436. SetTaskbarListProgressState(TBPF_NORMAL);
  1437. }
  1438. //---------------------------------------------------------------------------
  1439. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1440. {
  1441. SAFE_DESTROY(FProgressForm);
  1442. UnlockWindow();
  1443. SetQueueProgress();
  1444. }
  1445. //---------------------------------------------------------------------------
  1446. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1447. TFileOperationProgressType & ProgressData)
  1448. {
  1449. // Download to temporary local directory
  1450. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1451. {
  1452. bool Continue = true;
  1453. FPendingTempSpaceWarn = false;
  1454. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1455. if (!Continue)
  1456. {
  1457. Abort();
  1458. }
  1459. }
  1460. // operation is being executed and we still didn't show up progress form
  1461. if (ProgressData.InProgress && !FProgressForm)
  1462. {
  1463. CreateProgressForm(NULL);
  1464. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1465. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1466. }
  1467. // operation is finished (or terminated), so we hide progress form
  1468. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1469. {
  1470. DestroyProgressForm();
  1471. if (ProgressData.Operation == foCalculateSize)
  1472. {
  1473. // When calculating size before transfer, the abort caused by
  1474. // cancel flag set due to "MoveToQueue" below,
  1475. // is not propagated back to us in ExecuteFileOperation,
  1476. // as it is silently swallowed.
  1477. // So we explicitly (re)throw it here.
  1478. if (FMoveToQueue)
  1479. {
  1480. Abort();
  1481. }
  1482. }
  1483. else
  1484. {
  1485. if ((ProgressData.Cancel == csContinue) &&
  1486. !FAutoOperation)
  1487. {
  1488. OperationComplete(ProgressData.StartTime);
  1489. }
  1490. }
  1491. }
  1492. if (FProgressForm &&
  1493. // Can happen only during synchronization, when ending one of the operations
  1494. ProgressData.InProgress)
  1495. {
  1496. FProgressForm->SetProgressData(ProgressData);
  1497. DebugAssert(ProgressData.InProgress);
  1498. SetTaskbarListProgressValue(&ProgressData);
  1499. if (FProgressForm->Cancel > csContinue)
  1500. {
  1501. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1502. if (FProgressForm->Cancel == csCancelFile)
  1503. {
  1504. FProgressForm->ClearCancel();
  1505. }
  1506. // cancel cancels even the move
  1507. FMoveToQueue = false;
  1508. }
  1509. else if (FProgressForm->MoveToQueue)
  1510. {
  1511. FMoveToQueue = true;
  1512. ProgressData.SetCancelAtLeast(csCancel);
  1513. }
  1514. if ((FTransferResumeList != NULL) &&
  1515. ProgressData.InProgress &&
  1516. ProgressData.IsTransfer() &&
  1517. !ProgressData.FullFileName.IsEmpty())
  1518. {
  1519. if ((FTransferResumeList->Count == 0) ||
  1520. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1521. {
  1522. // note that we do not recognize directories from files here
  1523. FTransferResumeList->Add(ProgressData.FullFileName);
  1524. }
  1525. }
  1526. }
  1527. }
  1528. //---------------------------------------------------------------------------
  1529. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1530. const TDateTime & StartTime)
  1531. {
  1532. if (GUIConfiguration->BeepOnFinish &&
  1533. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1534. {
  1535. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1536. DWORD Sound;
  1537. if (!ExtractFileExt(BeepSound).IsEmpty())
  1538. {
  1539. Sound = SND_FILENAME;
  1540. }
  1541. else
  1542. {
  1543. Sound = SND_ALIAS;
  1544. }
  1545. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1546. }
  1547. }
  1548. //---------------------------------------------------------------------------
  1549. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1550. TFileOperationProgressType & ProgressData)
  1551. {
  1552. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1553. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1554. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1555. FileOperationProgress(ProgressData);
  1556. }
  1557. //---------------------------------------------------------------------------
  1558. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1559. {
  1560. UnicodeString Result;
  1561. if (FProgressForm != NULL)
  1562. {
  1563. Result = FProgressForm->ProgressStr();
  1564. }
  1565. return Result;
  1566. }
  1567. //---------------------------------------------------------------------------
  1568. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1569. bool DragDrop)
  1570. {
  1571. return (!DragDrop && (DropSourceControl == NULL)) ||
  1572. (DropSourceControl == DirView(osOther));
  1573. }
  1574. //---------------------------------------------------------------------------
  1575. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1576. {
  1577. TCustomDirView * ADirView = DirView(Side);
  1578. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1579. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1580. // this can happen when local drive is unplugged in the middle of the operation
  1581. if (Item != NULL)
  1582. {
  1583. if (Unselect)
  1584. {
  1585. Item->Selected = false;
  1586. }
  1587. ADirView->MakeProgressVisible(Item);
  1588. }
  1589. return Item;
  1590. }
  1591. //---------------------------------------------------------------------------
  1592. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1593. TFileOperation Operation, TOperationSide Side,
  1594. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1595. TOnceDoneOperation & OnceDoneOperation)
  1596. {
  1597. if (!FAutoOperation)
  1598. {
  1599. // no selection on "/upload", form serves only as event handler
  1600. // (it is not displayed)
  1601. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1602. ((Operation == FPrimaryOperation) ||
  1603. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1604. {
  1605. TOperationSide DViewSide;
  1606. if ((FProgressSide != osCurrent) &&
  1607. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1608. {
  1609. // assume the operation is over the focused panel
  1610. DViewSide = FProgressSide;
  1611. }
  1612. else
  1613. {
  1614. DViewSide = Side;
  1615. }
  1616. if (FOnFileOperationFinished != NULL)
  1617. {
  1618. FOnFileOperationFinished(DViewSide, FileName, Success);
  1619. }
  1620. else
  1621. {
  1622. VisualiseOperationFinished(DViewSide, FileName, Success);
  1623. }
  1624. }
  1625. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1626. {
  1627. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1628. {
  1629. if (Side == osLocal)
  1630. {
  1631. Configuration->Usage->Inc(L"UploadedFiles");
  1632. }
  1633. else
  1634. {
  1635. Configuration->Usage->Inc(L"DownloadedFiles");
  1636. }
  1637. }
  1638. }
  1639. }
  1640. if (Success && (FSynchronizeController != NULL))
  1641. {
  1642. DebugAssert(!IsSideLocalBrowser(osRemote));
  1643. if (Operation == foCopy)
  1644. {
  1645. DebugAssert(Side == osLocal);
  1646. FSynchronizeController->LogOperation(soUpload, FileName);
  1647. }
  1648. else if (Operation == foDelete)
  1649. {
  1650. DebugAssert(Side == osRemote);
  1651. FSynchronizeController->LogOperation(soDelete, FileName);
  1652. }
  1653. }
  1654. if (FProgressForm)
  1655. {
  1656. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1657. }
  1658. }
  1659. //---------------------------------------------------------------------------
  1660. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1661. TFileOperation Operation, TOperationSide Side,
  1662. bool Temp, const UnicodeString & FileName, Boolean Success,
  1663. TOnceDoneOperation & OnceDoneOperation)
  1664. {
  1665. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1666. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1667. OnceDoneOperation);
  1668. }
  1669. //---------------------------------------------------------------------------
  1670. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1671. {
  1672. return false;
  1673. }
  1674. //---------------------------------------------------------------------------
  1675. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1676. {
  1677. return false;
  1678. }
  1679. //---------------------------------------------------------------------------
  1680. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1681. {
  1682. return false;
  1683. }
  1684. //---------------------------------------------------------------------------
  1685. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1686. {
  1687. return NULL;
  1688. }
  1689. //---------------------------------------------------------------------------
  1690. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1691. {
  1692. DebugAssert(GetSide(Side) == osRemote);
  1693. DebugUsedParam(Side);
  1694. return RemoteDirView;
  1695. }
  1696. //---------------------------------------------------------------------------
  1697. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1698. {
  1699. DebugAssert(GetSide(Side) == osRemote);
  1700. DebugUsedParam(Side);
  1701. return RemoteDriveView;
  1702. }
  1703. //---------------------------------------------------------------------------
  1704. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1705. {
  1706. DebugAssert(GetSide(Side) == osRemote);
  1707. DebugUsedParam(Side);
  1708. return HasActiveTerminal();
  1709. }
  1710. //---------------------------------------------------------------------------
  1711. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1712. TOperationSide Side, int FilesOnly)
  1713. {
  1714. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1715. }
  1716. //---------------------------------------------------------------------------
  1717. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1718. TOperationSide Side, int FilesOnly)
  1719. {
  1720. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1721. }
  1722. //---------------------------------------------------------------------------
  1723. struct THistoryItemData
  1724. {
  1725. short int Side;
  1726. short int Index;
  1727. };
  1728. //---------------------------------------------------------------------------
  1729. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1730. {
  1731. DirView(Side)->HistoryGo(Index);
  1732. }
  1733. //---------------------------------------------------------------------------
  1734. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1735. {
  1736. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1737. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1738. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1739. }
  1740. //---------------------------------------------------------------------------
  1741. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1742. bool Back)
  1743. {
  1744. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1745. {
  1746. TCustomDirView * DView = DirView(Side);
  1747. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1748. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1749. if (ICount > 10)
  1750. {
  1751. ICount = 10;
  1752. }
  1753. Menu->Items->Clear();
  1754. THistoryItemData Data;
  1755. Data.Side = Side;
  1756. for (short int i = 1; i <= ICount; i++)
  1757. {
  1758. TTBCustomItem * Item = new TTBXItem(Menu);
  1759. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1760. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1761. Item->Hint = DView->HistoryPath[Data.Index];
  1762. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1763. Item->Tag = *reinterpret_cast<int*>(&Data);
  1764. Item->OnClick = HistoryItemClick;
  1765. Menu->Items->Add(Item);
  1766. }
  1767. }
  1768. }
  1769. //---------------------------------------------------------------------------
  1770. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1771. TOperationSide Side, bool Back)
  1772. {
  1773. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1774. {
  1775. // workaround
  1776. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1777. // operator new allocates memory in Pascal code, but calls inline
  1778. // contructor in C++, leading in problems, the function does
  1779. // both in Pascal code
  1780. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1781. UpdateHistoryMenu(Side, Back);
  1782. }
  1783. return FHistoryMenu[Side == osLocal][Back];
  1784. }
  1785. //---------------------------------------------------------------------------
  1786. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1787. TCustomDirView *Sender)
  1788. {
  1789. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1790. UpdateHistoryMenu(Side, true);
  1791. UpdateHistoryMenu(Side, false);
  1792. }
  1793. //---------------------------------------------------------------------------
  1794. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1795. {
  1796. // remote custom commands can be executed only if the server supports shell commands
  1797. // or have secondary shell
  1798. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1799. }
  1800. //---------------------------------------------------------------------------
  1801. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1802. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1803. {
  1804. // -1 = hidden, 0 = disabled, 1 = enabled
  1805. int Result;
  1806. TFileCustomCommand RemoteCustomCommand;
  1807. TLocalCustomCommand LocalCustomCommand;
  1808. TFileCustomCommand * NonInteractiveCustomCommand =
  1809. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1810. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1811. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1812. if (FLAGCLEAR(Command.Params, ccLocal))
  1813. {
  1814. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1815. // custom command that does not operate with files can be executed anytime ...
  1816. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1817. {
  1818. // ... but do not show such command in remote file menu
  1819. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1820. {
  1821. Result = AllowedState;
  1822. }
  1823. else
  1824. {
  1825. Result = -1;
  1826. }
  1827. }
  1828. else
  1829. {
  1830. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1831. {
  1832. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1833. }
  1834. else
  1835. {
  1836. Result = -1;
  1837. }
  1838. }
  1839. }
  1840. else
  1841. {
  1842. // custom command that does not operate with files can be executed anytime
  1843. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1844. {
  1845. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1846. }
  1847. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1848. {
  1849. if ((ListType == ccltAll) || (ListType == ccltFile))
  1850. {
  1851. // special case is "diff"-style command that can be executed over any side,
  1852. // if we have both sides
  1853. Result =
  1854. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1855. // directly (instead of EnableSelectedOperation) to pass
  1856. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1857. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1858. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1859. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1860. }
  1861. else if (ListType == ccltBoth)
  1862. {
  1863. DebugAssert(!IsLocalBrowserMode());
  1864. Result = 1;
  1865. }
  1866. else
  1867. {
  1868. Result = -1;
  1869. }
  1870. }
  1871. else
  1872. {
  1873. if ((ListType == ccltAll) ||
  1874. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1875. {
  1876. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1877. }
  1878. else
  1879. {
  1880. Result = -1;
  1881. }
  1882. }
  1883. if ((Result > 0) &&
  1884. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1885. !HasActiveTerminal())
  1886. {
  1887. Result = 0;
  1888. }
  1889. }
  1890. return Result;
  1891. }
  1892. //---------------------------------------------------------------------------
  1893. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1894. TStrings * FileList, const TCustomCommandType & ACommand,
  1895. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1896. {
  1897. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1898. {
  1899. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1900. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1901. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1902. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1903. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1904. bool Capture =
  1905. FLAGSET(ACommand.Params, ccShowResults) ||
  1906. FLAGSET(ACommand.Params, ccCopyResults) ||
  1907. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1908. TCaptureOutputEvent OutputEvent = NULL;
  1909. DebugAssert(FCapturedLog == NULL);
  1910. if (Capture)
  1911. {
  1912. FCapturedLog = new TStringList();
  1913. OutputEvent = TerminalCaptureLog;
  1914. }
  1915. try
  1916. {
  1917. if (!RemoteCustomCommand.IsFileCommand(Command))
  1918. {
  1919. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1920. OutputEvent);
  1921. }
  1922. else
  1923. {
  1924. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1925. }
  1926. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1927. {
  1928. if (FLAGSET(ACommand.Params, ccCopyResults))
  1929. {
  1930. CopyToClipboard(FCapturedLog);
  1931. }
  1932. if (FLAGSET(ACommand.Params, ccShowResults))
  1933. {
  1934. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1935. }
  1936. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1937. {
  1938. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1939. }
  1940. }
  1941. }
  1942. __finally
  1943. {
  1944. SAFE_DESTROY(FCapturedLog);
  1945. }
  1946. }
  1947. }
  1948. //---------------------------------------------------------------------------
  1949. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1950. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1951. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1952. {
  1953. DebugAssert(!IsLocalBrowserMode());
  1954. TStrings * LocalFileList = NULL;
  1955. TStrings * RemoteFileList = NULL;
  1956. TStrings * RemoteFileListFull = NULL;
  1957. try
  1958. {
  1959. if (LocalFileCommand)
  1960. {
  1961. if (ALocalFileList == NULL)
  1962. {
  1963. DebugAssert(HasDirView[osLocal]);
  1964. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1965. // directly (instead of EnableSelectedOperation) to pass
  1966. // false to FocusedFileOnlyWhenFocused
  1967. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1968. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1969. }
  1970. else
  1971. {
  1972. LocalFileList = ALocalFileList;
  1973. }
  1974. if (FileListCommand)
  1975. {
  1976. if (LocalFileList->Count != 1)
  1977. {
  1978. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1979. }
  1980. }
  1981. else
  1982. {
  1983. if ((LocalFileList->Count != 1) &&
  1984. (FileList->Count != 1) &&
  1985. (LocalFileList->Count != FileList->Count))
  1986. {
  1987. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1988. }
  1989. }
  1990. }
  1991. UnicodeString RootTempDir;
  1992. UnicodeString TempDir;
  1993. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1994. if (!RemoteFiles)
  1995. {
  1996. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true);
  1997. }
  1998. try
  1999. {
  2000. TDateTimes RemoteFileTimes;
  2001. if (RemoteFiles)
  2002. {
  2003. RemoteFileList = FileList;
  2004. }
  2005. else
  2006. {
  2007. RemoteFileList = new TStringList();
  2008. TMakeLocalFileListParams MakeFileListParam;
  2009. MakeFileListParam.FileList = RemoteFileList;
  2010. MakeFileListParam.FileTimes = &RemoteFileTimes;
  2011. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2012. MakeFileListParam.Recursive =
  2013. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  2014. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2015. if (!MakeFileListParam.Recursive)
  2016. {
  2017. RemoteFileTimes.clear();
  2018. RemoteFileListFull = new TStringList();
  2019. MakeFileListParam.FileList = RemoteFileListFull;
  2020. MakeFileListParam.Recursive = true;
  2021. MakeFileListParam.IncludeDirs = false;
  2022. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2023. }
  2024. else
  2025. {
  2026. RemoteFileListFull = RemoteFileList;
  2027. }
  2028. }
  2029. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  2030. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2031. if (!NonBlocking)
  2032. {
  2033. int Count;
  2034. if (FileListCommand)
  2035. {
  2036. Count = 1;
  2037. }
  2038. else if (LocalFileCommand)
  2039. {
  2040. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  2041. }
  2042. else
  2043. {
  2044. Count = RemoteFileList->Count;
  2045. }
  2046. Progress.Start(foCustomCommand, osRemote, Count);
  2047. DebugAssert(FProgressForm != NULL);
  2048. FProgressForm->ReadOnly = true;
  2049. }
  2050. try
  2051. {
  2052. if (FileListCommand)
  2053. {
  2054. UnicodeString LocalFile;
  2055. // MakeFileList does not delimit filenames
  2056. UnicodeString FileList = MakeFileList(RemoteFileList);
  2057. if (LocalFileCommand)
  2058. {
  2059. DebugAssert(LocalFileList->Count == 1);
  2060. LocalFile = LocalFileList->Strings[0];
  2061. }
  2062. TLocalCustomCommand CustomCommand(Data,
  2063. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  2064. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  2065. if (NonBlocking)
  2066. {
  2067. DebugAssert(!POutput);
  2068. ExecuteShellChecked(ShellCommand);
  2069. }
  2070. else
  2071. {
  2072. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2073. }
  2074. }
  2075. else if (LocalFileCommand)
  2076. {
  2077. if (LocalFileList->Count == 1)
  2078. {
  2079. UnicodeString LocalFile = LocalFileList->Strings[0];
  2080. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2081. {
  2082. UnicodeString FileName = RemoteFileList->Strings[Index];
  2083. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2084. Progress.SetFile(FileNameForProgress);
  2085. TLocalCustomCommand CustomCommand(Data,
  2086. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2087. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2088. TOnceDoneOperation OnceDoneOperation;
  2089. // Do not unselect anything, as it won't work with two panels anyway
  2090. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2091. }
  2092. }
  2093. else if (RemoteFileList->Count == 1)
  2094. {
  2095. UnicodeString FileName = RemoteFileList->Strings[0];
  2096. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2097. {
  2098. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2099. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2100. Progress.SetFile(LocalFileNameOnly);
  2101. TLocalCustomCommand CustomCommand(
  2102. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2103. FileName, LocalFileName, L"");
  2104. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2105. TOnceDoneOperation OnceDoneOperation;
  2106. // Do not unselect anything, as it won't work with two panels anyway
  2107. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2108. }
  2109. }
  2110. else
  2111. {
  2112. if (LocalFileList->Count != RemoteFileList->Count)
  2113. {
  2114. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2115. }
  2116. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2117. {
  2118. UnicodeString FileName = RemoteFileList->Strings[Index];
  2119. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2120. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2121. Progress.SetFile(LocalFileNameOnly); // sic
  2122. TLocalCustomCommand CustomCommand(
  2123. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2124. FileName, LocalFileName, L"");
  2125. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2126. TOnceDoneOperation OnceDoneOperation;
  2127. // Do not unselect anything, as it won't work with two panels anyway
  2128. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2129. }
  2130. }
  2131. }
  2132. else
  2133. {
  2134. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2135. {
  2136. UnicodeString FileName = RemoteFileList->Strings[Index];
  2137. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2138. Progress.SetFile(FileNameForProgress);
  2139. TLocalCustomCommand CustomCommand(Data,
  2140. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2141. FileName, L"", L"");
  2142. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2143. TOnceDoneOperation OnceDoneOperation;
  2144. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2145. }
  2146. }
  2147. }
  2148. __finally
  2149. {
  2150. if (!NonBlocking)
  2151. {
  2152. Progress.Stop();
  2153. }
  2154. }
  2155. DebugAssert(!FAutoOperation);
  2156. if (!RemoteFiles)
  2157. {
  2158. TempDir = IncludeTrailingBackslash(TempDir);
  2159. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2160. {
  2161. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2162. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2163. // Skip directories, process only nested files.
  2164. // The check is redundant as FileAge fails for directories anyway.
  2165. !DirectoryExists(FileName))
  2166. {
  2167. UnicodeString RemoteDir =
  2168. UnixExtractFileDir(
  2169. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2170. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2171. TDateTime NewTime;
  2172. if (FileAge(FileName, NewTime) &&
  2173. (NewTime != RemoteFileTimes[Index]))
  2174. {
  2175. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2176. TemporaryFileCopyParam(CopyParam);
  2177. CopyParam.FileMask = L"";
  2178. FAutoOperation = true;
  2179. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2180. TemporaryFilesList->Add(FileName);
  2181. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2182. }
  2183. }
  2184. }
  2185. }
  2186. }
  2187. __finally
  2188. {
  2189. FAutoOperation = false;
  2190. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2191. {
  2192. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2193. }
  2194. }
  2195. }
  2196. __finally
  2197. {
  2198. if (RemoteFileList != FileList)
  2199. {
  2200. delete RemoteFileList;
  2201. }
  2202. if (LocalFileList != ALocalFileList)
  2203. {
  2204. delete LocalFileList;
  2205. }
  2206. if (RemoteFileListFull != RemoteFileList)
  2207. {
  2208. delete RemoteFileListFull;
  2209. }
  2210. }
  2211. }
  2212. //---------------------------------------------------------------------------
  2213. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2214. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2215. bool FileListCommand, UnicodeString * POutput)
  2216. {
  2217. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2218. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2219. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2220. {
  2221. UnicodeString FileName = SelectedFileList->Strings[Index];
  2222. if (DirectoryExists(FileName))
  2223. {
  2224. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2225. {
  2226. LocalFileList->Add(FileName);
  2227. }
  2228. if (FLAGSET(ACommand.Params, ccRecursive))
  2229. {
  2230. TMakeLocalFileListParams MakeFileListParam;
  2231. MakeFileListParam.FileList = LocalFileList.get();
  2232. MakeFileListParam.FileTimes = NULL;
  2233. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2234. MakeFileListParam.Recursive = true;
  2235. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2236. }
  2237. }
  2238. else
  2239. {
  2240. LocalFileList->Add(FileName);
  2241. }
  2242. }
  2243. UnicodeString RemotePath;
  2244. if (Terminal != NULL)
  2245. {
  2246. RemotePath = Terminal->CurrentDirectory;
  2247. }
  2248. if (FileListCommand)
  2249. {
  2250. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2251. TLocalCustomCommand CustomCommand(
  2252. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2253. L"", L"", FileList);
  2254. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2255. }
  2256. else
  2257. {
  2258. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2259. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2260. DebugAssert(FProgressForm != NULL);
  2261. FProgressForm->ReadOnly = true;
  2262. try
  2263. {
  2264. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2265. {
  2266. UnicodeString FileName = LocalFileList->Strings[Index];
  2267. Progress.SetFile(FileName);
  2268. TLocalCustomCommand CustomCommand(
  2269. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2270. FileName, L"", L"");
  2271. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2272. TOnceDoneOperation OnceDoneOperation;
  2273. Progress.Finish(FileName, true, OnceDoneOperation);
  2274. }
  2275. }
  2276. __finally
  2277. {
  2278. Progress.Stop();
  2279. }
  2280. }
  2281. }
  2282. //---------------------------------------------------------------------------
  2283. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2284. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2285. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2286. {
  2287. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2288. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2289. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2290. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2291. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2292. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2293. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2294. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2295. std::unique_ptr<UnicodeString> POutput;
  2296. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2297. FLAGSET(ACommand.Params, ccCopyResults))
  2298. {
  2299. POutput.reset(new UnicodeString());
  2300. }
  2301. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2302. FProgressSide = FCurrentSide;
  2303. if (!LocalCustomCommand.IsFileCommand(Command))
  2304. {
  2305. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2306. }
  2307. // remote files?
  2308. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2309. {
  2310. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2311. }
  2312. // local files
  2313. else
  2314. {
  2315. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2316. }
  2317. if (POutput.get() != NULL)
  2318. {
  2319. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2320. int P = POutput->Pos(sLineBreak);
  2321. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2322. {
  2323. POutput->SetLength(P - 1);
  2324. }
  2325. // Copy even empty output to clipboard
  2326. if (FLAGSET(ACommand.Params, ccCopyResults))
  2327. {
  2328. CopyToClipboard(*POutput);
  2329. }
  2330. // But do not show an empty message box.
  2331. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2332. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2333. !POutput->IsEmpty())
  2334. {
  2335. TClipboardHandler ClipboardHandler;
  2336. ClipboardHandler.Text = *POutput;
  2337. TMessageParams Params;
  2338. TQueryButtonAlias Aliases[1];
  2339. Aliases[0].Button = qaRetry;
  2340. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2341. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2342. Params.Aliases = Aliases;
  2343. Params.AliasesCount = LENOF(Aliases);
  2344. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2345. }
  2346. }
  2347. }
  2348. //---------------------------------------------------------------------------
  2349. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2350. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2351. {
  2352. TCustomCommandData Data;
  2353. UnicodeString Site;
  2354. UnicodeString RemotePath;
  2355. if (HasActiveTerminal())
  2356. {
  2357. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2358. Data = TCustomCommandData(SessionData.get());
  2359. Site = Terminal->SessionData->SessionKey;
  2360. RemotePath = Terminal->CurrentDirectory;
  2361. }
  2362. UnicodeString HelpKeyword = ACommand.HomePage;
  2363. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2364. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2365. {
  2366. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2367. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2368. {
  2369. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2370. }
  2371. else
  2372. {
  2373. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2374. }
  2375. if (!DoCustomCommandOptionsDialog(
  2376. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2377. {
  2378. Abort();
  2379. }
  2380. }
  2381. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2382. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2383. {
  2384. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2385. }
  2386. else
  2387. {
  2388. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2389. }
  2390. }
  2391. //---------------------------------------------------------------------------
  2392. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2393. const TCustomCommandType & Command)
  2394. {
  2395. DebugAssert(FCustomCommandLocalFileList != NULL);
  2396. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2397. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2398. TStrings * LocalFileList = new TStringList();
  2399. TStrings * RemoteFileList = new TStringList();
  2400. try
  2401. {
  2402. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2403. {
  2404. LocalFileList->Clear();
  2405. LocalFileList->AddObject(
  2406. FCustomCommandLocalFileList->Strings[Index],
  2407. FCustomCommandLocalFileList->Objects[Index]);
  2408. RemoteFileList->Clear();
  2409. RemoteFileList->AddObject(
  2410. FCustomCommandRemoteFileList->Strings[Index],
  2411. FCustomCommandRemoteFileList->Objects[Index]);
  2412. CustomCommand(RemoteFileList, Command, LocalFileList);
  2413. }
  2414. }
  2415. __finally
  2416. {
  2417. delete LocalFileList;
  2418. delete RemoteFileList;
  2419. }
  2420. }
  2421. //---------------------------------------------------------------------------
  2422. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2423. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2424. {
  2425. delete FCustomCommandLocalFileList;
  2426. delete FCustomCommandRemoteFileList;
  2427. // takeover ownership,
  2428. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2429. FCustomCommandLocalFileList = LocalFileList;
  2430. FCustomCommandRemoteFileList = RemoteFileList;
  2431. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2432. if (Button != NULL)
  2433. {
  2434. FCustomCommandMenu->Items->Clear();
  2435. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2436. MenuPopup(FCustomCommandMenu, Button);
  2437. }
  2438. else
  2439. {
  2440. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2441. }
  2442. }
  2443. //---------------------------------------------------------------------------
  2444. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2445. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2446. {
  2447. DebugAssert(FCapturedLog != NULL);
  2448. if ((OutputType == cotOutput) || (OutputType == cotError))
  2449. {
  2450. FCapturedLog->Add(AddedLine);
  2451. }
  2452. }
  2453. //---------------------------------------------------------------------------
  2454. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2455. TOperationSide Side)
  2456. {
  2457. return
  2458. (Side == osOther) &&
  2459. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2460. }
  2461. //---------------------------------------------------------------------------
  2462. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2463. TOperationSide Side, TTBXCustomItem * Item,
  2464. TResolvedDoubleClickAction DoubleClickAction1, TResolvedDoubleClickAction DoubleClickAction2)
  2465. {
  2466. TCustomDirView * DView = DirView(Side);
  2467. bool IsDefault = false;
  2468. if (DView->ItemFocused != NULL)
  2469. {
  2470. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  2471. TResolvedDoubleClickAction DefaultAction =
  2472. WinConfiguration->ResolveDoubleClickAction(DView->ItemIsDirectory(DView->ItemFocused), ATerminal);
  2473. IsDefault = (DefaultAction == DoubleClickAction1) || (DefaultAction == DoubleClickAction2);
  2474. }
  2475. TTBItemOptions O = Item->Options;
  2476. if (IsDefault)
  2477. {
  2478. Item->Options = O << tboDefault;
  2479. }
  2480. else
  2481. {
  2482. Item->Options = O >> tboDefault;
  2483. }
  2484. }
  2485. //---------------------------------------------------------------------------
  2486. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2487. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2488. {
  2489. TCustomDirView * DView = DirView(Side);
  2490. TListItem * Item = DView->ItemFocused;
  2491. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2492. EnableFocusedOperation[Side])
  2493. {
  2494. TPoint ClientPoint;
  2495. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2496. TPoint(0, 0) : MousePos;
  2497. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2498. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2499. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2500. }
  2501. }
  2502. //---------------------------------------------------------------------------
  2503. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2504. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2505. {
  2506. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, rdcaChangeDir, rdcaOpen);
  2507. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, rdcaEdit, rdcaNone);
  2508. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, rdcaCopy, rdcaNone);
  2509. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2510. Handled = true;
  2511. }
  2512. //---------------------------------------------------------------------------
  2513. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2514. {
  2515. }
  2516. //---------------------------------------------------------------------------
  2517. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2518. {
  2519. DebugAssert(FErrorList == NULL);
  2520. if (WinConfiguration->ContinueOnError)
  2521. {
  2522. FErrorList = new TStringList();
  2523. Configuration->Usage->Inc(L"ContinuationsOnError");
  2524. }
  2525. NonVisualDataModule->StartBusy();
  2526. }
  2527. //---------------------------------------------------------------------------
  2528. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2529. {
  2530. NonVisualDataModule->EndBusy();
  2531. if (FErrorList)
  2532. {
  2533. HandleErrorList(FErrorList);
  2534. }
  2535. }
  2536. //---------------------------------------------------------------------------
  2537. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2538. const TCopyParamType & CopyParam)
  2539. {
  2540. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2541. {
  2542. Configuration->Usage->Inc(L"FileMaskUses");
  2543. }
  2544. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2545. {
  2546. Configuration->Usage->Inc(L"OperationMaskUses");
  2547. }
  2548. }
  2549. //---------------------------------------------------------------------------
  2550. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2551. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2552. {
  2553. DebugAssert(!IsLocalBrowserMode());
  2554. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2555. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2556. TTransferOperationParam DefaultParam;
  2557. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2558. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2559. switch (Param.Queue)
  2560. {
  2561. case asOn:
  2562. CopyParam.Queue = true;
  2563. break;
  2564. case asOff:
  2565. CopyParam.Queue = false;
  2566. break;
  2567. case asAuto:
  2568. default:
  2569. // keep default
  2570. break;
  2571. }
  2572. bool Result =
  2573. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2574. if (Result)
  2575. {
  2576. DebugAssert(Terminal);
  2577. bool SelectionRestored = false;
  2578. TCustomDirView * DView = NULL;
  2579. if (HasDirView[Side])
  2580. {
  2581. DView = DirView(Side);
  2582. DView->SaveSelection();
  2583. DView->SaveSelectedNames();
  2584. }
  2585. UpdateCopyParamCounters(CopyParam);
  2586. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2587. DebugAssert(FTransferResumeList == NULL);
  2588. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2589. FMoveToQueue = false;
  2590. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2591. try
  2592. {
  2593. TStrings * PermanentFileList;
  2594. std::unique_ptr<TStrings> PermanentFileListOwner;
  2595. try
  2596. {
  2597. if (Side == osLocal)
  2598. {
  2599. PermanentFileList = FileList;
  2600. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2601. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2602. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2603. if (Operation == foMove)
  2604. {
  2605. ReloadLocalDirectory();
  2606. if (DView != NULL)
  2607. {
  2608. DView->RestoreSelection();
  2609. }
  2610. SelectionRestored = true;
  2611. }
  2612. }
  2613. else
  2614. {
  2615. // Clone the file list as it may refer to current directory files,
  2616. // which get destroyed, when the source directory is reloaded after foMove operation.
  2617. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2618. // But for a hotfix, we are not going to do such a big change.
  2619. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2620. PermanentFileList = PermanentFileListOwner.get();
  2621. try
  2622. {
  2623. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2624. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2625. }
  2626. __finally
  2627. {
  2628. if (Operation == foMove)
  2629. {
  2630. if (DView != NULL)
  2631. {
  2632. DView->RestoreSelection();
  2633. }
  2634. SelectionRestored = true;
  2635. }
  2636. ReloadLocalDirectory(Param.TargetDirectory);
  2637. }
  2638. }
  2639. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2640. if (FMoveToQueue)
  2641. {
  2642. Abort();
  2643. }
  2644. }
  2645. catch (EAbort &)
  2646. {
  2647. if (FMoveToQueue)
  2648. {
  2649. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2650. DebugAssert(CopyParam.TransferSkipList == NULL);
  2651. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2652. if (TransferResumeList->Count > 0)
  2653. {
  2654. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2655. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2656. }
  2657. CopyParam.TransferSkipList = TransferResumeList.release();
  2658. // not really needed, just to keep it consistent with TransferResumeList
  2659. FTransferResumeList = NULL;
  2660. FMoveToQueue = false;
  2661. Configuration->Usage->Inc("MovesToBackground");
  2662. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2663. ClearTransferSourceSelection(Direction);
  2664. }
  2665. throw;
  2666. }
  2667. }
  2668. __finally
  2669. {
  2670. if (!SelectionRestored && (DView != NULL))
  2671. {
  2672. DView->DiscardSavedSelection();
  2673. }
  2674. FTransferResumeList = NULL;
  2675. }
  2676. }
  2677. return Result;
  2678. }
  2679. //---------------------------------------------------------------------------
  2680. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2681. TOperationSide Side, TStrings * FileList, void * Param)
  2682. {
  2683. DebugAssert(FileList->Count);
  2684. // We deliberately do not toggle alternative flag (Param), but use OR,
  2685. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2686. // shortcut of CurrentDeleteAlternativeAction
  2687. bool Alternative =
  2688. bool(Param) || UseAlternativeFunction();
  2689. bool Recycle;
  2690. if (IsSideLocalBrowser(Side))
  2691. {
  2692. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2693. }
  2694. else
  2695. {
  2696. Recycle =
  2697. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2698. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2699. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2700. }
  2701. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2702. if (!Result)
  2703. {
  2704. UnicodeString Query;
  2705. if (FileList->Count == 1)
  2706. {
  2707. if (IsSideLocalBrowser(Side))
  2708. {
  2709. Query = ExtractFileName(FileList->Strings[0]);
  2710. }
  2711. else
  2712. {
  2713. Query = UnixExtractFileName(FileList->Strings[0]);
  2714. }
  2715. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2716. }
  2717. else
  2718. {
  2719. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2720. }
  2721. TMessageParams Params(mpNeverAskAgainCheck);
  2722. Params.ImageName = L"Delete file";
  2723. unsigned int Answer =
  2724. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2725. if (Answer == qaNeverAskAgain)
  2726. {
  2727. Result = true;
  2728. if (Recycle)
  2729. {
  2730. WinConfiguration->ConfirmRecycling = false;
  2731. }
  2732. else
  2733. {
  2734. WinConfiguration->ConfirmDeleting = false;
  2735. }
  2736. }
  2737. else
  2738. {
  2739. Result = (Answer == qaOK);
  2740. }
  2741. }
  2742. if (Result)
  2743. {
  2744. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2745. }
  2746. return Result;
  2747. }
  2748. //---------------------------------------------------------------------------
  2749. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2750. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2751. {
  2752. TAutoBatch AutoBatch(this);
  2753. bool Result;
  2754. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2755. {
  2756. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2757. }
  2758. else if (Operation == foRename)
  2759. {
  2760. DebugAssert(DirView(Side)->ItemFocused);
  2761. DirView(Side)->ItemFocused->EditCaption();
  2762. Result = true;
  2763. }
  2764. else if (Operation == foDelete)
  2765. {
  2766. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2767. }
  2768. else if (Operation == foSetProperties)
  2769. {
  2770. DirView(osRemote)->SaveSelectedNames();
  2771. Result = SetProperties(Side, FileList);
  2772. }
  2773. else if (Operation == foCustomCommand)
  2774. {
  2775. DebugAssert(Param);
  2776. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2777. DirView(osRemote)->SaveSelectedNames();
  2778. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2779. CustomCommand(FileList, *Command, NULL);
  2780. Result = true;
  2781. }
  2782. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2783. {
  2784. DebugAssert(!IsSideLocalBrowser(Side));
  2785. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2786. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2787. }
  2788. else if (Operation == foLock)
  2789. {
  2790. DebugAssert(!IsSideLocalBrowser(Side));
  2791. LockFiles(FileList, true);
  2792. Result = true;
  2793. }
  2794. else if (Operation == foUnlock)
  2795. {
  2796. DebugAssert(!IsSideLocalBrowser(Side));
  2797. LockFiles(FileList, false);
  2798. Result = true;
  2799. }
  2800. else
  2801. {
  2802. DebugFail();
  2803. }
  2804. return Result;
  2805. }
  2806. //---------------------------------------------------------------------------
  2807. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2808. {
  2809. if (Side == osCurrent)
  2810. {
  2811. Side = FCurrentSide;
  2812. }
  2813. return Side;
  2814. }
  2815. //---------------------------------------------------------------------------
  2816. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2817. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2818. {
  2819. Side = GetSide(Side);
  2820. bool FullPath = IsSideLocalBrowser(Side);
  2821. TCustomDirView * ADirView = DirView(Side);
  2822. std::unique_ptr<TStringList> FileList(new TStringList());
  2823. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2824. bool ReloadProperties =
  2825. !IsSideLocalBrowser(Side) && (Operation == foSetProperties) &&
  2826. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2827. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2828. if (Result && ReloadProperties)
  2829. {
  2830. std::unique_ptr<TStringList> FileList2(new TStringList());
  2831. FileList->CaseSensitive = true;
  2832. FileList->Sort();
  2833. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2834. {
  2835. TListItem * Item = ADirView->Items->Item[Index];
  2836. UnicodeString FileName = ADirView->ItemFileName(Item);
  2837. int Index2;
  2838. if (FileList->Find(FileName, Index2))
  2839. {
  2840. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2841. }
  2842. }
  2843. LoadFilesProperties(FileList2.get());
  2844. }
  2845. return Result;
  2846. }
  2847. //---------------------------------------------------------------------------
  2848. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2849. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2850. bool NoConfirmation, void * Param)
  2851. {
  2852. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2853. {
  2854. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2855. {
  2856. DebugAssert(!IsLocalBrowserMode());
  2857. if (GetSide(Side) == osLocal)
  2858. {
  2859. Configuration->Usage->Inc(L"UploadsCommand");
  2860. }
  2861. else
  2862. {
  2863. Configuration->Usage->Inc(L"DownloadsCommand");
  2864. }
  2865. }
  2866. }
  2867. }
  2868. //---------------------------------------------------------------------------
  2869. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2870. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2871. {
  2872. TTransferOperationParam Param;
  2873. if ((WinConfiguration->Interface != ifCommander) ||
  2874. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2875. {
  2876. Flags &= ~cocShortCutHint;
  2877. }
  2878. TCustomDirView * DView = DirView(Side);
  2879. Param.Options =
  2880. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2881. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2882. if (FLAGSET(Flags, cocQueue))
  2883. {
  2884. Param.Queue = asOn;
  2885. }
  2886. else if (FLAGSET(Flags, cocNonQueue))
  2887. {
  2888. Param.Queue = asOff;
  2889. }
  2890. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2891. }
  2892. //---------------------------------------------------------------------------
  2893. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2894. {
  2895. int Count = ErrorList->Count;
  2896. try
  2897. {
  2898. if (ErrorList->Count)
  2899. {
  2900. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2901. if (MessageDialog(Message, qtError,
  2902. qaOK | qaCancel, HELP_NONE) == qaOK)
  2903. {
  2904. unsigned int Answer;
  2905. int Index = 0;
  2906. do
  2907. {
  2908. DebugAssert(Index >= 0 && Index < Count);
  2909. TQueryButtonAlias Aliases[2];
  2910. Aliases[0].Button = qaYes;
  2911. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2912. Aliases[1].Button = qaNo;
  2913. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2914. TMessageParams Params;
  2915. Params.Aliases = Aliases;
  2916. Params.AliasesCount = LENOF(Aliases);
  2917. int No = Index + 1;
  2918. UnicodeString Message = ErrorList->Strings[Index];
  2919. UnicodeString AMainInstructions;
  2920. UnicodeString Details;
  2921. if (ExtractMainInstructions(Message, AMainInstructions))
  2922. {
  2923. Details = Message;
  2924. Message = AMainInstructions;
  2925. }
  2926. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2927. Answer = MoreMessageDialog(
  2928. Message,
  2929. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2930. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2931. qaOK, HELP_NONE, &Params);
  2932. if (Answer == qaNo)
  2933. {
  2934. Index++;
  2935. }
  2936. if (Answer == qaYes)
  2937. {
  2938. Index--;
  2939. }
  2940. }
  2941. while (Answer != qaOK);
  2942. }
  2943. }
  2944. }
  2945. __finally
  2946. {
  2947. TStrings * List = ErrorList;
  2948. ErrorList = NULL;
  2949. for (int i = 0; i < Count; i++)
  2950. {
  2951. delete List->Objects[i];
  2952. }
  2953. delete List;
  2954. }
  2955. }
  2956. //---------------------------------------------------------------------------
  2957. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2958. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2959. {
  2960. // needed for checking filemasks, as there's no directory object
  2961. // associated with the file object
  2962. File->FullFileName = FullFileName;
  2963. TFileMasks::TParams MaskParams;
  2964. MaskParams.Size = File->Size;
  2965. MaskParams.Modification = File->Modification;
  2966. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2967. }
  2968. //---------------------------------------------------------------------------
  2969. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2970. {
  2971. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2972. Side = GetSide(Side);
  2973. TCustomDirView * DView = DirView(Side);
  2974. TListItem * FocusedItem = DView->ItemFocused;
  2975. UnicodeString Name;
  2976. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2977. {
  2978. Name = DView->ItemFileName(FocusedItem);
  2979. }
  2980. else
  2981. {
  2982. Name = LoadStr(NEW_FILE);
  2983. }
  2984. UnicodeString Names = Name;
  2985. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2986. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2987. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  2988. {
  2989. while (!Names.IsEmpty())
  2990. {
  2991. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2992. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2993. UnicodeString TargetFileName;
  2994. UnicodeString LocalFileName;
  2995. UnicodeString RootTempDir;
  2996. UnicodeString TempDir;
  2997. UnicodeString RemoteDirectory;
  2998. bool ExistingFile = false;
  2999. if (!IsSideLocalBrowser(Side))
  3000. {
  3001. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  3002. TRemoteFile * File = NULL;
  3003. if (Terminal->FileExists(Name, &File) &&
  3004. !File->IsDirectory)
  3005. {
  3006. try
  3007. {
  3008. ExecuteRemoteFile(Name, File, efDefaultEditor);
  3009. ExistingFile = true;
  3010. }
  3011. __finally
  3012. {
  3013. delete File;
  3014. }
  3015. }
  3016. if (!ExistingFile)
  3017. {
  3018. RemoteDirectory = UnixExtractFilePath(Name);
  3019. TemporaryDirectoryForRemoteFiles(
  3020. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  3021. TargetFileName = UnixExtractFileName(Name);
  3022. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3023. LocalFileName = TempDir +
  3024. // We probably do not want to trim the VMS version here
  3025. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  3026. }
  3027. }
  3028. else
  3029. {
  3030. if (ExtractFilePath(Name).IsEmpty())
  3031. {
  3032. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  3033. }
  3034. else
  3035. {
  3036. LocalFileName = ExpandFileName(Name);
  3037. }
  3038. TargetFileName = ExtractFileName(Name);
  3039. }
  3040. if (!ExistingFile)
  3041. {
  3042. bool NewFile = !FileExists(ApiPath(LocalFileName));
  3043. if (NewFile)
  3044. {
  3045. int File = FileCreate(ApiPath(LocalFileName));
  3046. if (File < 0)
  3047. {
  3048. if (!RootTempDir.IsEmpty())
  3049. {
  3050. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3051. }
  3052. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  3053. }
  3054. else
  3055. {
  3056. FileClose(File);
  3057. }
  3058. }
  3059. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  3060. const TEditorData * ExternalEditor = NULL;
  3061. TFileMasks::TParams MaskParams; // size not known
  3062. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  3063. false, MaskParams);
  3064. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  3065. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  3066. }
  3067. }
  3068. }
  3069. }
  3070. //---------------------------------------------------------------------------
  3071. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  3072. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  3073. {
  3074. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3075. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3076. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3077. return
  3078. ((ExecuteFileBy == efInternalEditor)) ||
  3079. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  3080. }
  3081. //---------------------------------------------------------------------------
  3082. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  3083. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  3084. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  3085. UnicodeString RemoteDirectory, bool NewFile)
  3086. {
  3087. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3088. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3089. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3090. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3091. Side = GetSide(Side);
  3092. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3093. if (!IsSideLocalBrowser(Side))
  3094. {
  3095. Data->Terminal = Terminal;
  3096. Data->Queue = Queue;
  3097. Data->SessionData = CloneCurrentSessionData();
  3098. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3099. Data->RemoteDirectory = RemoteDirectory;
  3100. Data->SessionName = Terminal->SessionData->SessionName;
  3101. Data->LocalRootDirectory = LocalRootDirectory;
  3102. Data->OriginalFileName = OriginalFileName;
  3103. Data->Command = L""; // will be changed later for external editor
  3104. }
  3105. if (ExecuteFileBy == efInternalEditor)
  3106. {
  3107. if (!IsSideLocalBrowser(Side))
  3108. {
  3109. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3110. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3111. TForm * Editor;
  3112. try
  3113. {
  3114. Editor =
  3115. ShowEditorForm(
  3116. FileName, this,
  3117. FEditorManager->FileChanged, FEditorManager->FileReload, FEditorManager->FileClosed,
  3118. SaveAllInternalEditors, AnyInternalEditorModified,
  3119. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3120. }
  3121. catch(...)
  3122. {
  3123. if (!LocalRootDirectory.IsEmpty())
  3124. {
  3125. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  3126. }
  3127. throw;
  3128. }
  3129. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3130. }
  3131. else
  3132. {
  3133. DebugAssert(!StandaloneOperation);
  3134. TForm * Editor =
  3135. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3136. SaveAllInternalEditors, AnyInternalEditorModified,
  3137. L"", false, SessionColor, -1, NewFile);
  3138. FLocalEditors->Add(Editor);
  3139. }
  3140. }
  3141. else
  3142. {
  3143. HANDLE Process;
  3144. if (ExecuteFileBy == efExternalEditor)
  3145. {
  3146. UnicodeString Program, Params, Dir;
  3147. Data->Command = ExternalEditor->ExternalEditor;
  3148. ReformatFileNameCommand(Data->Command);
  3149. SplitCommand(Data->Command, Program, Params, Dir);
  3150. Params = ExpandFileNameCommand(Params, FileName);
  3151. Program = ExpandEnvironmentVariables(Program);
  3152. if (!ExecuteShell(Program, Params, Process))
  3153. {
  3154. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3155. }
  3156. }
  3157. else
  3158. {
  3159. DebugAssert(!IsSideLocalBrowser(Side));
  3160. if (!ExecuteShell(FileName, L"", Process))
  3161. {
  3162. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3163. }
  3164. }
  3165. if (IsSideLocalBrowser(Side) ||
  3166. ((ExecuteFileBy == efShell) &&
  3167. !WinConfiguration->Editor.SDIShellEditor) ||
  3168. ((ExecuteFileBy == efExternalEditor) &&
  3169. !ExternalEditor->SDIExternalEditor))
  3170. {
  3171. // no need for handle
  3172. if (Process != NULL)
  3173. {
  3174. DebugCheck(CloseHandle(Process));
  3175. }
  3176. Process = INVALID_HANDLE_VALUE;
  3177. }
  3178. else
  3179. {
  3180. if (Process == NULL)
  3181. {
  3182. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3183. }
  3184. }
  3185. if (!IsSideLocalBrowser(Side))
  3186. {
  3187. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3188. }
  3189. }
  3190. }
  3191. //---------------------------------------------------------------------------
  3192. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3193. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3194. void * /*Arg*/)
  3195. {
  3196. if (Token != NULL)
  3197. {
  3198. EditorFormFileSave(static_cast<TForm *>(Token));
  3199. }
  3200. }
  3201. //---------------------------------------------------------------------------
  3202. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3203. {
  3204. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3205. {
  3206. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3207. }
  3208. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3209. }
  3210. //---------------------------------------------------------------------------
  3211. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3212. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3213. void * Arg)
  3214. {
  3215. if ((Token != NULL) &&
  3216. IsEditorFormModified(static_cast<TForm *>(Token)))
  3217. {
  3218. *static_cast<bool *>(Arg) = true;
  3219. }
  3220. }
  3221. //---------------------------------------------------------------------------
  3222. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3223. TObject * /*Sender*/, bool & Modified)
  3224. {
  3225. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3226. {
  3227. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3228. {
  3229. Modified = true;
  3230. }
  3231. }
  3232. if (!Modified)
  3233. {
  3234. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3235. }
  3236. }
  3237. //---------------------------------------------------------------------------
  3238. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3239. {
  3240. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3241. }
  3242. //---------------------------------------------------------------------------
  3243. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3244. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3245. UnicodeString & Result, UnicodeString & RootDirectory)
  3246. {
  3247. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3248. {
  3249. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3250. Result = RootDirectory;
  3251. }
  3252. else
  3253. {
  3254. RootDirectory = L"";
  3255. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3256. Result = IncludeTrailingBackslash(Result);
  3257. }
  3258. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3259. {
  3260. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3261. }
  3262. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3263. {
  3264. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3265. {
  3266. RemoteDirectory.Delete(1, 1);
  3267. }
  3268. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3269. }
  3270. if (!ForceDirectories(ApiPath(Result)))
  3271. {
  3272. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3273. }
  3274. }
  3275. //---------------------------------------------------------------------------
  3276. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3277. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3278. bool GetTargetNames, bool AutoOperation)
  3279. {
  3280. DebugAssert(!IsLocalBrowserMode());
  3281. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3282. if (ForceText)
  3283. {
  3284. CopyParam.TransferMode = tmAscii;
  3285. }
  3286. TemporaryFileCopyParam(CopyParam);
  3287. if (TempDir.IsEmpty())
  3288. {
  3289. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3290. }
  3291. DebugAssert(!FAutoOperation);
  3292. FAutoOperation = AutoOperation;
  3293. Terminal->ExceptionOnFail = true;
  3294. try
  3295. {
  3296. try
  3297. {
  3298. // turn off confirmations, as for MDI editors we may possibly download
  3299. // the same file over
  3300. Terminal->CopyToLocal(
  3301. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3302. if (GetTargetNames)
  3303. {
  3304. for (int i = 0; i < FileList->Count; i++)
  3305. {
  3306. FileList->Strings[i] =
  3307. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3308. }
  3309. }
  3310. }
  3311. catch(...)
  3312. {
  3313. if (!RootTempDir.IsEmpty())
  3314. {
  3315. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3316. }
  3317. throw;
  3318. }
  3319. }
  3320. __finally
  3321. {
  3322. FAutoOperation = false;
  3323. Terminal->ExceptionOnFail = false;
  3324. }
  3325. }
  3326. //---------------------------------------------------------------------------
  3327. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3328. {
  3329. // Do not waste time checking for default editor list the next time,
  3330. // or testing if default editor is not notepad.
  3331. bool TryNextTime = false;
  3332. UnicodeString UsageState;
  3333. if (!WinConfiguration->EditorList->IsDefaultList())
  3334. {
  3335. UsageState = "H";
  3336. }
  3337. else
  3338. {
  3339. UnicodeString Executable;
  3340. UnicodeString ExecutableDescription;
  3341. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3342. {
  3343. UnicodeString Message =
  3344. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3345. unsigned int Answer =
  3346. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3347. if (Answer != qaOK)
  3348. {
  3349. UsageState = "R";
  3350. }
  3351. else
  3352. {
  3353. UsageState = "A";
  3354. TEditorData EditorData;
  3355. EditorData.Editor = edExternal;
  3356. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3357. EditorData.ExternalEditorOptionsAutodetect();
  3358. TEditorList EditorList;
  3359. EditorList = *WinConfiguration->EditorList;
  3360. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3361. WinConfiguration->EditorList = &EditorList;
  3362. }
  3363. }
  3364. }
  3365. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3366. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3367. }
  3368. //---------------------------------------------------------------------------
  3369. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3370. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3371. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3372. {
  3373. if (ExecuteFileBy == efDefaultEditor)
  3374. {
  3375. if (!WinConfiguration->OfferedEditorAutoConfig)
  3376. {
  3377. EditorAutoConfig();
  3378. }
  3379. const TEditorPreferences * Editor =
  3380. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3381. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3382. {
  3383. ExecuteFileBy = efInternalEditor;
  3384. ExternalEditor = NULL;
  3385. }
  3386. else if (Editor->Data->Editor == edOpen)
  3387. {
  3388. ExecuteFileBy = efShell;
  3389. ExternalEditor = NULL;
  3390. }
  3391. else
  3392. {
  3393. ExecuteFileBy = efExternalEditor;
  3394. ExternalEditor = Editor->Data;
  3395. }
  3396. }
  3397. }
  3398. //---------------------------------------------------------------------------
  3399. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3400. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3401. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3402. {
  3403. UnicodeString OriginalFileName;
  3404. UnicodeString LocalRootDirectory;
  3405. UnicodeString RemoteDirectory;
  3406. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3407. IsSideLocalBrowser(Side), MaskParams);
  3408. UnicodeString Counter;
  3409. UnicodeString LocalFileName;
  3410. bool Handled = false;
  3411. if (!IsSideLocalBrowser(Side))
  3412. {
  3413. // We need to trim VMS version here, so that we use name without version
  3414. // when uploading back to create a new version of the file
  3415. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3416. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3417. TObject * Token = NULL;
  3418. UnicodeString LocalDirectory;
  3419. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3420. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3421. LocalDirectory))
  3422. {
  3423. if (Token != NULL)
  3424. {
  3425. TForm * Form = dynamic_cast<TForm *>(Token);
  3426. if (Form->WindowState == wsMinimized)
  3427. {
  3428. ShowWindow(Form->Handle, SW_RESTORE);
  3429. }
  3430. else
  3431. {
  3432. Form->SetFocus();
  3433. }
  3434. Handled = true;
  3435. }
  3436. else
  3437. {
  3438. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3439. }
  3440. }
  3441. if (!Handled)
  3442. {
  3443. TStringList * FileList1 = new TStringList();
  3444. try
  3445. {
  3446. FileList1->AddObject(FullFileName, Object);
  3447. TemporarilyDownloadFiles(FileList1,
  3448. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3449. LocalRootDirectory, LocalDirectory, true, true);
  3450. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3451. }
  3452. __finally
  3453. {
  3454. delete FileList1;
  3455. }
  3456. switch (ExecuteFileBy)
  3457. {
  3458. case efShell:
  3459. Counter = "RemoteFilesExecuted";
  3460. break;
  3461. case efInternalEditor:
  3462. Counter = "RemoteFilesOpenedInInternalEditor";
  3463. break;
  3464. case efExternalEditor:
  3465. Counter = "RemoteFilesOpenedInExternalEditor";
  3466. break;
  3467. default:
  3468. DebugFail();
  3469. }
  3470. }
  3471. }
  3472. else
  3473. {
  3474. LocalFileName = FullFileName;
  3475. OriginalFileName = ExtractFileName(FullFileName);
  3476. switch (ExecuteFileBy)
  3477. {
  3478. case efShell:
  3479. Counter = "LocalFilesExecuted";
  3480. break;
  3481. case efInternalEditor:
  3482. Counter = "LocalFilesOpenedInInternalEditor";
  3483. break;
  3484. case efExternalEditor:
  3485. Counter = "LocalFilesOpenedInExternalEditor";
  3486. break;
  3487. default:
  3488. DebugFail();
  3489. }
  3490. }
  3491. if (!Handled)
  3492. {
  3493. Configuration->Usage->Inc(Counter);
  3494. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3495. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3496. }
  3497. }
  3498. //---------------------------------------------------------------------------
  3499. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3500. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3501. bool AllSelected, bool OnFocused)
  3502. {
  3503. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3504. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3505. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3506. Side = GetSide(Side);
  3507. TCustomDirView * DView = DirView(Side);
  3508. TStrings * FileList =
  3509. AllSelected ?
  3510. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3511. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3512. try
  3513. {
  3514. DebugAssert(AllSelected || (FileList->Count == 1));
  3515. for (int i = 0; i < FileList->Count; i++)
  3516. {
  3517. UnicodeString ListFileName = FileList->Strings[i];
  3518. UnicodeString FileNameOnly =
  3519. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3520. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3521. if (!DView->ItemIsDirectory(Item))
  3522. {
  3523. UnicodeString FullFileName;
  3524. if (!IsSideLocalBrowser(Side))
  3525. {
  3526. FullFileName = DirView(Side)->Path + ListFileName;
  3527. }
  3528. else
  3529. {
  3530. FullFileName = ListFileName;
  3531. }
  3532. TFileMasks::TParams MaskParams;
  3533. MaskParams.Size = DView->ItemFileSize(Item);
  3534. TDateTimePrecision Precision;
  3535. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3536. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3537. FileList->Objects[i], MaskParams);
  3538. }
  3539. }
  3540. }
  3541. __finally
  3542. {
  3543. delete FileList;
  3544. }
  3545. }
  3546. //---------------------------------------------------------------------------
  3547. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3548. {
  3549. CopyParam.FileNameCase = ncNoChange;
  3550. CopyParam.PreserveRights = false;
  3551. CopyParam.PreserveReadOnly = false;
  3552. CopyParam.ReplaceInvalidChars = true;
  3553. CopyParam.IncludeFileMask = TFileMasks();
  3554. CopyParam.NewerOnly = false;
  3555. CopyParam.FileMask = L"";
  3556. }
  3557. //---------------------------------------------------------------------------
  3558. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
  3559. const UnicodeString & FileName, TEditedFileData * Data, HANDLE UploadCompleteEvent, bool & Retry)
  3560. {
  3561. TTerminalManager * Manager = TTerminalManager::Instance();
  3562. if (!IsActiveTerminal(Data->Terminal))
  3563. {
  3564. if (!NonVisualDataModule->Busy)
  3565. {
  3566. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3567. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3568. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3569. if (Data->Terminal == NULL)
  3570. {
  3571. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3572. if (SameSiteTerminal != NULL)
  3573. {
  3574. UnicodeString Message =
  3575. FMTLOAD(EDIT_SESSION_REATTACH,
  3576. (FileNameOnly, Data->SessionName, FileNameOnly));
  3577. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3578. {
  3579. Data->Terminal = Terminal;
  3580. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3581. Data->SessionName = Terminal->SessionData->SessionName;
  3582. // We might also overwrite session data
  3583. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3584. }
  3585. else
  3586. {
  3587. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3588. Abort();
  3589. }
  3590. }
  3591. }
  3592. // foreground session should reconnect itself
  3593. else if (Terminal != Data->Terminal)
  3594. {
  3595. UnicodeString Message =
  3596. MainInstructions(
  3597. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3598. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3599. {
  3600. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3601. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3602. }
  3603. else
  3604. {
  3605. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3606. Abort();
  3607. }
  3608. }
  3609. }
  3610. if (!IsActiveTerminal(Data->Terminal))
  3611. {
  3612. Configuration->Usage->Inc(L"EditInactiveSession");
  3613. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3614. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3615. (ExtractFileName(FileName), Data->SessionName)));
  3616. }
  3617. }
  3618. TStrings * FileList = new TStringList();
  3619. try
  3620. {
  3621. FileList->Add(FileName);
  3622. // consider using the same settings (preset) as when the file was downloaded
  3623. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3624. TemporaryFileCopyParam(CopyParam);
  3625. if (Data->ForceText)
  3626. {
  3627. CopyParam.TransferMode = tmAscii;
  3628. }
  3629. // so i do not need to worry if masking algorithm works in all cases
  3630. // ("" means "copy file name", no masking is actually done)
  3631. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3632. {
  3633. CopyParam.FileMask = L"";
  3634. }
  3635. else
  3636. {
  3637. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3638. }
  3639. int Params = cpNoConfirmation | cpTemporary;
  3640. if (Terminal->IsCapable[fcBackgroundTransfers])
  3641. {
  3642. DebugAssert(Data->Queue != NULL);
  3643. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3644. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3645. QueueItem->CompleteEvent = UploadCompleteEvent;
  3646. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3647. }
  3648. else
  3649. {
  3650. if (NonVisualDataModule->Busy)
  3651. {
  3652. Retry = true;
  3653. }
  3654. else
  3655. {
  3656. Terminal->CopyToRemote(FileList, Data->RemoteDirectory, &CopyParam, Params, NULL);
  3657. SetEvent(UploadCompleteEvent);
  3658. }
  3659. }
  3660. }
  3661. __finally
  3662. {
  3663. delete FileList;
  3664. }
  3665. }
  3666. //---------------------------------------------------------------------------
  3667. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3668. const UnicodeString FileName, const TEditedFileData * Data)
  3669. {
  3670. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3671. DebugAssert(!NonVisualDataModule->Busy);
  3672. if (!IsActiveTerminal(Data->Terminal))
  3673. {
  3674. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3675. (ExtractFileName(FileName), Data->SessionName)));
  3676. }
  3677. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3678. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3679. NonVisualDataModule->StartBusy();
  3680. try
  3681. {
  3682. std::unique_ptr<TRemoteFile> File;
  3683. UnicodeString RemoteFileName =
  3684. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3685. Terminal->ExceptionOnFail = true;
  3686. try
  3687. {
  3688. TRemoteFile * AFile = NULL;
  3689. Terminal->ReadFile(RemoteFileName, AFile);
  3690. File.reset(AFile);
  3691. if (!File->HaveFullFileName)
  3692. {
  3693. File->FullFileName = RemoteFileName;
  3694. }
  3695. }
  3696. __finally
  3697. {
  3698. Terminal->ExceptionOnFail = false;
  3699. }
  3700. std::unique_ptr<TStrings> FileList(new TStringList());
  3701. FileList->AddObject(RemoteFileName, File.get());
  3702. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3703. UnicodeString TempDir = ExtractFilePath(FileName);
  3704. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, true);
  3705. // sanity check, the target file name should be still the same
  3706. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3707. }
  3708. __finally
  3709. {
  3710. NonVisualDataModule->EndBusy();
  3711. // it actually may not exist anymore...
  3712. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3713. }
  3714. }
  3715. //---------------------------------------------------------------------------
  3716. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3717. const TEditedFileData * Data, bool & KeepOpen)
  3718. {
  3719. // Command is set for external editors only (not for "shell" open).
  3720. if (!Data->Command.IsEmpty())
  3721. {
  3722. bool AnyFound = false;
  3723. bool AnyMDI = false;
  3724. bool AnyNonMDI = false;
  3725. bool AnyDetect = false;
  3726. TEditorList * EditorList = new TEditorList();
  3727. try
  3728. {
  3729. *EditorList = *WinConfiguration->EditorList;
  3730. for (int i = 0; i < EditorList->Count; i++)
  3731. {
  3732. const TEditorPreferences * Editor = EditorList->Editors[i];
  3733. if ((Editor->Data->Editor == edExternal) &&
  3734. (Editor->Data->ExternalEditor == Data->Command))
  3735. {
  3736. AnyFound = true;
  3737. if (Editor->Data->SDIExternalEditor)
  3738. {
  3739. AnyNonMDI = true;
  3740. if (Editor->Data->DetectMDIExternalEditor)
  3741. {
  3742. AnyDetect = true;
  3743. }
  3744. }
  3745. else
  3746. {
  3747. AnyMDI = true;
  3748. }
  3749. }
  3750. }
  3751. bool EnableMDI = false;
  3752. bool DisableDetect = false;
  3753. if (AnyMDI)
  3754. {
  3755. KeepOpen = true;
  3756. if (AnyNonMDI)
  3757. {
  3758. // there is at least one instance of the editor with MDI support enabled,
  3759. // and one with disabled, enable it for all instances
  3760. EnableMDI = true;
  3761. }
  3762. }
  3763. else if (AnyFound && !AnyDetect)
  3764. {
  3765. // at least once instance found but all have MDI autodetection disabled
  3766. // => close the file (default action)
  3767. }
  3768. else
  3769. {
  3770. // no instance of the editor has MDI support enabled
  3771. TMessageParams Params;
  3772. if (AnyFound)
  3773. {
  3774. // there is at least one instance of the editor
  3775. Params.Params |= mpNeverAskAgainCheck;
  3776. }
  3777. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3778. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3779. switch (Answer)
  3780. {
  3781. case qaNeverAskAgain:
  3782. DisableDetect = true;
  3783. break;
  3784. case qaNo:
  3785. EnableMDI = true;
  3786. KeepOpen = true;
  3787. break;
  3788. }
  3789. }
  3790. if (AnyFound && (EnableMDI || DisableDetect))
  3791. {
  3792. bool Changed = false;
  3793. for (int i = 0; i < EditorList->Count; i++)
  3794. {
  3795. const TEditorPreferences * Editor = EditorList->Editors[i];
  3796. if ((Editor->Data->Editor == edExternal) &&
  3797. (Editor->Data->ExternalEditor == Data->Command) &&
  3798. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3799. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3800. {
  3801. Changed = true;
  3802. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3803. if (EnableMDI)
  3804. {
  3805. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3806. }
  3807. if (DisableDetect)
  3808. {
  3809. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3810. }
  3811. EditorList->Change(i, UpdatedEditor);
  3812. }
  3813. }
  3814. if (Changed)
  3815. {
  3816. WinConfiguration->EditorList = EditorList;
  3817. }
  3818. }
  3819. }
  3820. __finally
  3821. {
  3822. delete EditorList;
  3823. }
  3824. }
  3825. else
  3826. {
  3827. // "open" case
  3828. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3829. qaOK, HELP_APP_EARLY_CLOSED);
  3830. }
  3831. }
  3832. //---------------------------------------------------------------------------
  3833. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3834. {
  3835. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3836. }
  3837. //---------------------------------------------------------------------------
  3838. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3839. {
  3840. SideEnter(osRemote);
  3841. }
  3842. //---------------------------------------------------------------------------
  3843. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3844. {
  3845. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3846. SideEnter(osRemote);
  3847. }
  3848. //---------------------------------------------------------------------------
  3849. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3850. {
  3851. FCurrentSide = Side;
  3852. if (Visible)
  3853. {
  3854. UpdateControls();
  3855. }
  3856. }
  3857. //---------------------------------------------------------------------------
  3858. void __fastcall TCustomScpExplorerForm::FileDeleted(TOperationSide Side, const UnicodeString & FileName, bool Success)
  3859. {
  3860. VisualiseOperationFinished(Side, FileName, Success);
  3861. if (DebugAlwaysTrue(FDeletedFiles != NULL) && Success)
  3862. {
  3863. FDeletedFiles->Add(FileName);
  3864. }
  3865. }
  3866. //---------------------------------------------------------------------------
  3867. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3868. TStrings * FileList, bool Alternative)
  3869. {
  3870. TCustomDirView * DView = DirView(Side);
  3871. DView->SaveSelection();
  3872. DView->SaveSelectedNames();
  3873. DebugAssert(!FAlternativeDelete);
  3874. FAlternativeDelete = Alternative;
  3875. try
  3876. {
  3877. if (!IsSideLocalBrowser(Side))
  3878. {
  3879. DebugAssert(Terminal != NULL);
  3880. // Clone the file list as it may refer to current directory files, which get destroyed, when the directory is reloaded after the operation
  3881. std::unique_ptr<TStrings> PermanentFileList(TRemoteFileList::CloneStrings(FileList));
  3882. std::unique_ptr<TStringList> DeletedFiles(new TStringList());
  3883. DeletedFiles->CaseSensitive = DebugAlwaysTrue(!IsSideLocalBrowser(Side));
  3884. int Params = FLAGMASK(Alternative, dfAlternative);
  3885. try
  3886. {
  3887. TValueRestorer<TStrings *> DeletedFilesRestorer(FDeletedFiles);
  3888. DebugAssert(FDeletedFiles == NULL);
  3889. FDeletedFiles = DeletedFiles.get();
  3890. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  3891. FOnFileOperationFinished = FileDeleted;
  3892. FMoveToQueue = false;
  3893. Terminal->DeleteFiles(FileList, Params);
  3894. // Probably not needed for deleting, just for consistency with transfer code
  3895. if (FMoveToQueue)
  3896. {
  3897. Abort();
  3898. }
  3899. }
  3900. catch (EAbort &)
  3901. {
  3902. if (FMoveToQueue)
  3903. {
  3904. DeletedFiles->Sorted = true;
  3905. for (int Index = 0; Index < PermanentFileList->Count; Index++)
  3906. {
  3907. if (DeletedFiles->IndexOf(PermanentFileList->Strings[Index]) >= 0)
  3908. {
  3909. // We should always be deleting the first item => what can be used to optimize this code, if needed
  3910. DebugAssert(Index == 0);
  3911. PermanentFileList->Delete(Index);
  3912. Index--;
  3913. }
  3914. }
  3915. Configuration->Usage->Inc("MovesToBackgroundDelete");
  3916. TQueueItem * QueueItem = new TDeleteQueueItem(Terminal, PermanentFileList.get(), Params);
  3917. AddQueueItem(Queue, QueueItem, Terminal);
  3918. ClearOperationSelection(osRemote);
  3919. }
  3920. }
  3921. }
  3922. else
  3923. {
  3924. if (IsLocalBrowserMode())
  3925. {
  3926. Configuration->Usage->Inc(L"LocalLocalDeletes");
  3927. }
  3928. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  3929. FProgressSide = FCurrentSide;
  3930. try
  3931. {
  3932. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3933. }
  3934. __finally
  3935. {
  3936. ReloadLocalDirectory();
  3937. }
  3938. }
  3939. FAlternativeDelete = false;
  3940. }
  3941. catch(...)
  3942. {
  3943. FAlternativeDelete = false;
  3944. DView->DiscardSavedSelection();
  3945. throw;
  3946. }
  3947. DView->RestoreSelection();
  3948. }
  3949. //---------------------------------------------------------------------------
  3950. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3951. {
  3952. DebugAssert(Terminal);
  3953. DebugAssert(!IsLocalBrowserMode());
  3954. RemoteDirView->SaveSelection();
  3955. RemoteDirView->SaveSelectedNames();
  3956. try
  3957. {
  3958. if (Lock)
  3959. {
  3960. Terminal->LockFiles(FileList);
  3961. }
  3962. else
  3963. {
  3964. Terminal->UnlockFiles(FileList);
  3965. }
  3966. }
  3967. catch(...)
  3968. {
  3969. RemoteDirView->DiscardSavedSelection();
  3970. throw;
  3971. }
  3972. RemoteDirView->RestoreSelection();
  3973. }
  3974. //---------------------------------------------------------------------------
  3975. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3976. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3977. bool NoConfirmation, bool Move)
  3978. {
  3979. DebugAssert(Terminal != NULL);
  3980. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  3981. DebugAssert(!IsLocalBrowserMode());
  3982. if (Session == NULL)
  3983. {
  3984. Session = Terminal;
  3985. }
  3986. if (Session == Terminal)
  3987. {
  3988. if (RemoteDriveView->DropTarget != NULL)
  3989. {
  3990. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3991. }
  3992. else if (RemoteDirView->DropTarget != NULL)
  3993. {
  3994. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3995. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3996. }
  3997. else
  3998. {
  3999. Target = RemoteDirView->Path;
  4000. }
  4001. }
  4002. else
  4003. {
  4004. Target = GetSessionPath(Session, osRemote);
  4005. }
  4006. Target = UnixIncludeTrailingBackslash(Target);
  4007. if (FileList->Count == 1)
  4008. {
  4009. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  4010. }
  4011. else
  4012. {
  4013. FileMask = AnyMask;
  4014. }
  4015. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  4016. bool Result = true;
  4017. if (!NoConfirmation)
  4018. {
  4019. bool Multi = (FileList->Count > 1);
  4020. if (Move)
  4021. {
  4022. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  4023. }
  4024. else
  4025. {
  4026. std::unique_ptr<TStrings> Sessions(new TStringList);
  4027. std::unique_ptr<TStrings> Directories(new TStringList);
  4028. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  4029. for (int Index = 0; Index < SessionList->Count; Index++)
  4030. {
  4031. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  4032. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  4033. {
  4034. Sessions->AddObject(SessionList->Strings[Index], ASession);
  4035. Directories->Add(GetSessionPath(ASession, osRemote));
  4036. }
  4037. }
  4038. TDirectRemoteCopy AllowDirectCopy;
  4039. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  4040. {
  4041. DebugAssert(DirectCopy);
  4042. AllowDirectCopy = drcAllow;
  4043. }
  4044. else if (Terminal->IsCapable[fcSecondaryShell])
  4045. {
  4046. DebugAssert(DirectCopy);
  4047. AllowDirectCopy = drcConfirmCommandSession;
  4048. }
  4049. else
  4050. {
  4051. DebugAssert(!DirectCopy);
  4052. AllowDirectCopy = drcDisallow;
  4053. }
  4054. void * ASession = Session;
  4055. Result = DoRemoteCopyDialog(
  4056. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal);
  4057. Session = static_cast<TManagedTerminal *>(ASession);
  4058. }
  4059. }
  4060. return Result;
  4061. }
  4062. //---------------------------------------------------------------------------
  4063. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  4064. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  4065. {
  4066. DebugAssert(!IsLocalBrowserMode());
  4067. bool DirectCopy;
  4068. UnicodeString Target, FileMask;
  4069. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  4070. if (Result)
  4071. {
  4072. if (!Move && !DirectCopy)
  4073. {
  4074. Configuration->Usage->Inc("RemoteCopyTemp");
  4075. UnicodeString RootTempDir;
  4076. UnicodeString TempDir;
  4077. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false);
  4078. TStrings * TemporaryFilesList = new TStringList();
  4079. try
  4080. {
  4081. TMakeLocalFileListParams MakeFileListParam;
  4082. MakeFileListParam.FileList = TemporaryFilesList;
  4083. MakeFileListParam.FileTimes = NULL;
  4084. MakeFileListParam.IncludeDirs = true;
  4085. MakeFileListParam.Recursive = false;
  4086. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  4087. TTerminalManager::Instance()->ActiveSession = Session;
  4088. if (TemporaryFilesList->Count > 0)
  4089. {
  4090. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4091. CopyParam.FileMask = FileMask;
  4092. DebugAssert(!FAutoOperation);
  4093. FAutoOperation = true;
  4094. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  4095. }
  4096. }
  4097. __finally
  4098. {
  4099. delete TemporaryFilesList;
  4100. FAutoOperation = false;
  4101. if (!RootTempDir.IsEmpty())
  4102. {
  4103. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  4104. }
  4105. }
  4106. }
  4107. else
  4108. {
  4109. RemoteDirView->SaveSelection();
  4110. RemoteDirView->SaveSelectedNames();
  4111. try
  4112. {
  4113. if (Move)
  4114. {
  4115. Configuration->Usage->Inc("RemoteMove");
  4116. Terminal->MoveFiles(FileList, Target, FileMask);
  4117. }
  4118. else
  4119. {
  4120. Configuration->Usage->Inc("RemoteCopyDirect");
  4121. DebugAssert(DirectCopy);
  4122. DebugAssert(Session == Terminal);
  4123. if (Terminal->IsCapable[fcRemoteCopy] ||
  4124. Terminal->CommandSessionOpened ||
  4125. CommandSessionFallback())
  4126. {
  4127. Terminal->CopyFiles(FileList, Target, FileMask);
  4128. }
  4129. }
  4130. }
  4131. catch(...)
  4132. {
  4133. RemoteDirView->DiscardSavedSelection();
  4134. throw;
  4135. }
  4136. RemoteDirView->RestoreSelection();
  4137. }
  4138. }
  4139. return Result;
  4140. }
  4141. //---------------------------------------------------------------------------
  4142. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4143. const UnicodeString & Path, TRemoteProperties & Properties)
  4144. {
  4145. DebugAssert(!IsLocalBrowserMode());
  4146. Properties.Valid = Properties.Valid << vpEncrypt;
  4147. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4148. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4149. }
  4150. //---------------------------------------------------------------------------
  4151. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4152. {
  4153. Side = GetSide(Side);
  4154. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4155. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4156. UnicodeString Name = LoadStr(NEW_FOLDER);
  4157. int AllowedChanges =
  4158. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4159. bool SaveSettings = false;
  4160. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4161. {
  4162. TWindowLock Lock(this);
  4163. if (!IsSideLocalBrowser(Side))
  4164. {
  4165. if (SaveSettings)
  4166. {
  4167. GUIConfiguration->NewDirectoryProperties = Properties;
  4168. }
  4169. CreateRemoteDirectory(Name, Properties);
  4170. }
  4171. else
  4172. {
  4173. DirView(Side)->CreateDirectory(Name);
  4174. }
  4175. }
  4176. }
  4177. //---------------------------------------------------------------------------
  4178. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4179. {
  4180. TWindowLock Lock(this);
  4181. DirView(Side)->ExecuteHomeDirectory();
  4182. }
  4183. //---------------------------------------------------------------------------
  4184. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4185. {
  4186. DoOpenDirectoryDialog(odBrowse, Side);
  4187. }
  4188. //---------------------------------------------------------------------------
  4189. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4190. {
  4191. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4192. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4193. bool Result = !Path.IsEmpty();
  4194. if (Result)
  4195. {
  4196. TryOpenDirectory(Side, Path);
  4197. }
  4198. return Result;
  4199. }
  4200. //---------------------------------------------------------------------------
  4201. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4202. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4203. bool & Close)
  4204. {
  4205. // terminal can be already closed (e.g. dropped connection)
  4206. if (Terminal != NULL)
  4207. {
  4208. try
  4209. {
  4210. TCalculateSizeParams Params;
  4211. Params.Stats = &Stats;
  4212. Terminal->CalculateFilesSize(FileList, Size, Params);
  4213. }
  4214. catch(...)
  4215. {
  4216. if (!Terminal->Active)
  4217. {
  4218. Close = true;
  4219. }
  4220. throw;
  4221. }
  4222. }
  4223. }
  4224. //---------------------------------------------------------------------------
  4225. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4226. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4227. bool & Close)
  4228. {
  4229. if ((Terminal != NULL) && // terminal can be already closed (e.g. dropped connection)
  4230. EnsureCommandSessionFallback(fcCalculatingChecksum))
  4231. {
  4232. Configuration->Usage->Inc(L"ChecksumCalculated");
  4233. try
  4234. {
  4235. Terminal->CalculateFilesChecksum(Alg, FileList, OnCalculatedChecksum);
  4236. }
  4237. catch(...)
  4238. {
  4239. if (!Terminal->Active)
  4240. {
  4241. Close = true;
  4242. }
  4243. throw;
  4244. }
  4245. }
  4246. }
  4247. //---------------------------------------------------------------------------
  4248. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4249. {
  4250. if (Terminal->LoadFilesProperties(FileList))
  4251. {
  4252. RemoteDirView->Invalidate();
  4253. }
  4254. }
  4255. //---------------------------------------------------------------------------
  4256. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4257. {
  4258. bool Result;
  4259. if (!IsSideLocalBrowser(Side))
  4260. {
  4261. TRemoteTokenList * GroupList = NULL;
  4262. TRemoteTokenList * UserList = NULL;
  4263. try
  4264. {
  4265. TRemoteProperties CurrentProperties;
  4266. LoadFilesProperties(FileList);
  4267. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4268. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4269. if (CapableGroupChanging || CapableOwnerChanging)
  4270. {
  4271. if (CapableGroupChanging)
  4272. {
  4273. GroupList = Terminal->Groups->Duplicate();
  4274. }
  4275. if (CapableOwnerChanging)
  4276. {
  4277. UserList = Terminal->Users->Duplicate();
  4278. }
  4279. TRemoteDirectory * Files = Terminal->Files;
  4280. int Count = Files->Count;
  4281. if (Count > 100)
  4282. {
  4283. Count = 100;
  4284. }
  4285. for (int Index = 0; Index < Count; Index++)
  4286. {
  4287. TRemoteFile * File = Files->Files[Index];
  4288. if (CapableGroupChanging)
  4289. {
  4290. GroupList->AddUnique(File->Group);
  4291. }
  4292. if (CapableOwnerChanging)
  4293. {
  4294. UserList->AddUnique(File->Owner);
  4295. }
  4296. }
  4297. // if we haven't collected tokens for all files in current directory,
  4298. // make sure we collect them at least for all selected files.
  4299. // (note that so far the files in FileList has to be from current direcotry)
  4300. if (Count < Files->Count)
  4301. {
  4302. for (int Index = 0; Index < FileList->Count; Index++)
  4303. {
  4304. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4305. if (CapableGroupChanging)
  4306. {
  4307. GroupList->AddUnique(File->Group);
  4308. }
  4309. if (CapableOwnerChanging)
  4310. {
  4311. UserList->AddUnique(File->Owner);
  4312. }
  4313. }
  4314. }
  4315. }
  4316. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4317. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4318. for (int Index = 0; (Index < FileList->Count) && CapableAclChanging; Index++)
  4319. {
  4320. if (dynamic_cast<TRemoteFile *>(FileList->Objects[Index])->IsDirectory)
  4321. {
  4322. CapableAclChanging = false;
  4323. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4324. }
  4325. }
  4326. int Flags = 0;
  4327. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4328. if (CapableAclChanging) Flags |= cpAcl;
  4329. if (CapableOwnerChanging) Flags |= cpOwner;
  4330. if (CapableGroupChanging) Flags |= cpGroup;
  4331. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4332. if (Terminal->IsCapable[fcCalculatingChecksum] ||
  4333. (Terminal->IsCapable[fcSecondaryShell] && !Terminal->IsEncryptingFiles()))
  4334. {
  4335. CalculateChecksumEvent = CalculateChecksum;
  4336. }
  4337. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4338. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4339. TRemoteProperties NewProperties = CurrentProperties;
  4340. Result =
  4341. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4342. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4343. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4344. CalculateSize, CalculateChecksumEvent);
  4345. if (Result)
  4346. {
  4347. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4348. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4349. }
  4350. }
  4351. __finally
  4352. {
  4353. delete GroupList;
  4354. delete UserList;
  4355. }
  4356. }
  4357. else
  4358. {
  4359. DirView(Side)->DisplayPropertiesMenu();
  4360. Result = true;
  4361. }
  4362. return Result;
  4363. }
  4364. //---------------------------------------------------------------------------
  4365. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4366. {
  4367. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4368. {
  4369. FIgnoreNextDialogChar = Key;
  4370. }
  4371. Key = 0;
  4372. }
  4373. //---------------------------------------------------------------------------
  4374. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4375. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4376. {
  4377. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4378. if (Command != NULL)
  4379. {
  4380. KeyProcessed(Key, Shift);
  4381. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4382. {
  4383. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4384. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4385. false, false, const_cast<TCustomCommandType *>(Command));
  4386. }
  4387. }
  4388. }
  4389. //---------------------------------------------------------------------------
  4390. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4391. {
  4392. if (!NonVisualDataModule->Busy)
  4393. {
  4394. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4395. {
  4396. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4397. }
  4398. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4399. {
  4400. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4401. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4402. {
  4403. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4404. // But so far it's not a problem, as it does what we want.
  4405. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4406. if (((Action->ShortCut == KeyShortCut) ||
  4407. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4408. AllowedAction(Action, aaShortCut))
  4409. {
  4410. // Has to be called before the action as the dialog char is already in queue.
  4411. // So when the action consumes message queue, we already need to have the
  4412. // FIgnoreNextDialogChar set
  4413. KeyProcessed(Key, Shift);
  4414. // Reset reference to previous component (when menu/toolbar was clicked).
  4415. // Needed to detect that action was invoked by keyboard shortcut
  4416. // in TNonVisualDataModule::ExplorerActionsExecute
  4417. Action->ActionComponent = NULL;
  4418. Action->Execute();
  4419. return;
  4420. }
  4421. }
  4422. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4423. {
  4424. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4425. {
  4426. KeyProcessed(Key, Shift);
  4427. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4428. return;
  4429. }
  4430. }
  4431. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4432. {
  4433. KeyProcessed(Key, Shift);
  4434. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4435. }
  4436. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4437. if (IsCustomShortCut(CustomShortCut))
  4438. {
  4439. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4440. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4441. if (WinConfiguration->SharedBookmarks != NULL)
  4442. {
  4443. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4444. if ((Bookmark != NULL) &&
  4445. OpenBookmark(FCurrentSide, Bookmark))
  4446. {
  4447. KeyProcessed(Key, Shift);
  4448. }
  4449. }
  4450. }
  4451. }
  4452. }
  4453. TForm::KeyDown(Key, Shift);
  4454. }
  4455. //---------------------------------------------------------------------------
  4456. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4457. {
  4458. bool OpenedTerminal = false;
  4459. UnicodeString ProtocolName;
  4460. UnicodeString ProtocolNameHint;
  4461. UnicodeString SecurityProtocolName;
  4462. UnicodeString DurationHint;
  4463. // See the comment in TScpCommanderForm::UpdateControls()
  4464. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4465. if (ActiveTerminal != NULL)
  4466. {
  4467. // Active is not enough here
  4468. if (ActiveTerminal->Status == ssOpened)
  4469. {
  4470. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4471. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4472. if (FileSystemInfo.ProtocolName.IsEmpty())
  4473. {
  4474. ProtocolName = SessionInfo.ProtocolName;
  4475. }
  4476. else
  4477. {
  4478. ProtocolName = FileSystemInfo.ProtocolName;
  4479. }
  4480. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4481. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4482. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4483. OpenedTerminal = true;
  4484. }
  4485. else if (ActiveTerminal->Status > ssClosed)
  4486. {
  4487. ProtocolName = LoadStr(STATUS_CONNECTING);
  4488. }
  4489. else
  4490. {
  4491. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4492. }
  4493. }
  4494. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4495. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4496. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4497. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4498. if (ProtocolStatusPanel->Caption != ProtocolName)
  4499. {
  4500. ProtocolStatusPanel->Caption = ProtocolName;
  4501. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4502. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4503. }
  4504. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4505. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4506. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4507. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4508. SecurityStatusPanel->Enabled = SecurityEnabled;
  4509. UnicodeString SecurityProtocolHint;
  4510. if (SecurityEnabled)
  4511. {
  4512. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4513. }
  4514. else
  4515. {
  4516. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4517. }
  4518. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4519. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4520. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4521. DurationStatusPanel->Hint = DurationHint;
  4522. UpdateStatusBar();
  4523. }
  4524. //---------------------------------------------------------------------------
  4525. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4526. {
  4527. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4528. DebugAssert(SessionStatusBar != NULL);
  4529. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4530. if (!FNote.IsEmpty())
  4531. {
  4532. NoteStatusPanel->Caption = FNote;
  4533. }
  4534. else
  4535. {
  4536. UpdateStatusPanelText(NoteStatusPanel);
  4537. }
  4538. NoteStatusPanel->Hint = FNoteHints;
  4539. UnicodeString LoginTime;
  4540. // See the comment in TScpCommanderForm::UpdateControls()
  4541. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4542. // Active is not enough here
  4543. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4544. {
  4545. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4546. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4547. }
  4548. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4549. DurationStatusPanel->Caption = LoginTime;
  4550. }
  4551. //---------------------------------------------------------------------------
  4552. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4553. {
  4554. Panel->Caption = L"";
  4555. }
  4556. //---------------------------------------------------------------------------
  4557. void __fastcall TCustomScpExplorerForm::Idle()
  4558. {
  4559. if (FShowing ||
  4560. // Particularly for
  4561. // - Standalone editing
  4562. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4563. // while "keeping remote directory up to date".
  4564. StandaloneOperation)
  4565. {
  4566. FEditorManager->Check();
  4567. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4568. // that needs to know if queue view is visible (and it may be closed after queue update)
  4569. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4570. }
  4571. if (FShowing)
  4572. {
  4573. if (!NonVisualDataModule->Busy &&
  4574. // Menu is opened or drag&drop is going on
  4575. (Mouse->Capture == NULL))
  4576. {
  4577. if (FRefreshRemoteDirectory)
  4578. {
  4579. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4580. {
  4581. Terminal->RefreshDirectory();
  4582. }
  4583. FRefreshRemoteDirectory = false;
  4584. }
  4585. if (FRefreshLocalDirectory)
  4586. {
  4587. ReloadLocalDirectory();
  4588. FRefreshLocalDirectory = false;
  4589. }
  4590. if (WinConfiguration->RefreshRemotePanel)
  4591. {
  4592. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4593. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4594. {
  4595. RemoteDirView->ReloadDirectory();
  4596. }
  4597. }
  4598. }
  4599. }
  4600. if (FShowing || StandaloneOperation)
  4601. {
  4602. if (FQueueStatusInvalidated)
  4603. {
  4604. UpdateQueueStatus(false);
  4605. }
  4606. RefreshQueueItems();
  4607. }
  4608. if (FShowing)
  4609. {
  4610. UpdateStatusBar();
  4611. }
  4612. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4613. {
  4614. FClipboardFakeMonitorsPendingReset = false;
  4615. FClipboardFakeMonitors.reset(NULL);
  4616. }
  4617. FIgnoreNextDialogChar = 0;
  4618. }
  4619. //---------------------------------------------------------------------------
  4620. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4621. {
  4622. try
  4623. {
  4624. FUserActionTimer->Enabled = false;
  4625. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4626. {
  4627. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4628. {
  4629. FPendingQueueActionItem->ProcessUserAction();
  4630. }
  4631. }
  4632. }
  4633. __finally
  4634. {
  4635. FPendingQueueActionItem = NULL;
  4636. }
  4637. }
  4638. //---------------------------------------------------------------------------
  4639. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4640. {
  4641. if (WinConfiguration->MinimizeToTray)
  4642. {
  4643. UpdateTrayIcon();
  4644. FTrayIcon->Visible = true;
  4645. if (Visible)
  4646. {
  4647. ShowWindow(Handle, SW_HIDE);
  4648. }
  4649. }
  4650. }
  4651. //---------------------------------------------------------------------------
  4652. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4653. {
  4654. // WORKAROUND
  4655. // When restoring maximized window from minimization,
  4656. // rarely some controls do not align properly.
  4657. // Two instances seen (both for Commander):
  4658. // - When restoring, window is temporarily narrower (not maximized),
  4659. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4660. // Once maximized already, top dock shinks back, but the session PageControl,
  4661. // do not align up, leaving space between TopDock and PageControl.
  4662. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4663. for (int Index = 0; Index < ControlCount; Index++)
  4664. {
  4665. RealignControl(Controls[Index]);
  4666. }
  4667. if (FTrayIcon->Visible)
  4668. {
  4669. FTrayIcon->Visible = false;
  4670. if (Visible)
  4671. {
  4672. ShowWindow(Handle, SW_SHOW);
  4673. }
  4674. }
  4675. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4676. {
  4677. FNeedSession = false;
  4678. NonVisualDataModule->StartBusy();
  4679. try
  4680. {
  4681. NeedSession(true);
  4682. }
  4683. __finally
  4684. {
  4685. NonVisualDataModule->EndBusy();
  4686. }
  4687. }
  4688. }
  4689. //---------------------------------------------------------------------------
  4690. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4691. {
  4692. FTrayIcon->Hint = GetMainForm()->Caption;
  4693. }
  4694. //---------------------------------------------------------------------------
  4695. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4696. {
  4697. UpdateTrayIcon();
  4698. }
  4699. //---------------------------------------------------------------------------
  4700. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4701. {
  4702. // workaround
  4703. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4704. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4705. // But WM_ACTIVATEAPP message can be received even when
  4706. // the main window is minimized to the tray and internal editor window is focused
  4707. // (after another application was previously active)
  4708. if (::IsIconic(Handle))
  4709. {
  4710. if (!IsAppIconic())
  4711. {
  4712. SetAppIconic(true);
  4713. }
  4714. }
  4715. ::ApplicationRestore();
  4716. Application->BringToFront();
  4717. }
  4718. //---------------------------------------------------------------------------
  4719. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4720. {
  4721. RestoreApp();
  4722. }
  4723. //---------------------------------------------------------------------------
  4724. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4725. {
  4726. if (OpenInNewWindow())
  4727. {
  4728. ExecuteNewInstance(SessionUrl);
  4729. }
  4730. else
  4731. {
  4732. TTerminalManager::Instance()->NewSession(SessionUrl);
  4733. }
  4734. }
  4735. //---------------------------------------------------------------------------
  4736. void TCustomScpExplorerForm::NewTab(TOperationSide)
  4737. {
  4738. NewSession();
  4739. }
  4740. //---------------------------------------------------------------------------
  4741. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4742. {
  4743. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4744. StoredSessions->NewSession(SessionName, SessionData);
  4745. // modified only, explicit
  4746. StoredSessions->Save(false, true);
  4747. // encode session name because of slashes in hierarchical sessions
  4748. return EncodeUrlString(SessionName);
  4749. }
  4750. //---------------------------------------------------------------------------
  4751. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4752. {
  4753. // current working directories become defaults here, what is not right
  4754. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4755. return SaveHiddenDuplicateSession(SessionData.get());
  4756. }
  4757. //---------------------------------------------------------------------------
  4758. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4759. {
  4760. if (OpenInNewWindow())
  4761. {
  4762. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4763. }
  4764. else
  4765. {
  4766. // current working directories become defaults here, what is not right
  4767. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4768. TTerminalManager * Manager = TTerminalManager::Instance();
  4769. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4770. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4771. // We definitelly want these
  4772. ASession->Disconnected = ManagedSession->Disconnected;
  4773. // Not sure about these two
  4774. ASession->Permanent = ManagedSession->Permanent;
  4775. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4776. Manager->ActiveSession = ASession;
  4777. }
  4778. }
  4779. //---------------------------------------------------------------------------
  4780. void __fastcall TCustomScpExplorerForm::RenameSession()
  4781. {
  4782. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4783. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME) &&
  4784. // When submitting the default name, do not fix it
  4785. (Name != ManagedSession->SessionData->SessionName))
  4786. {
  4787. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4788. TSessionData::ValidateName(Name);
  4789. // This is inconsistent with how color (for example) is handled.
  4790. ManagedSession->SessionData->Name = Name;
  4791. UpdateControls();
  4792. // Add/Remove distinguishing paths from sessions of the same name.
  4793. SessionListChanged();
  4794. }
  4795. }
  4796. //---------------------------------------------------------------------------
  4797. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4798. {
  4799. DebugAssert(Queue != NULL);
  4800. bool Result = Queue->IsEmpty;
  4801. if (!Result)
  4802. {
  4803. SetFocus();
  4804. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4805. }
  4806. return Result;
  4807. }
  4808. //---------------------------------------------------------------------------
  4809. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4810. {
  4811. return CanCloseQueue(FQueue);
  4812. }
  4813. //---------------------------------------------------------------------------
  4814. void __fastcall TCustomScpExplorerForm::CloseSession()
  4815. {
  4816. if (CanCloseQueue())
  4817. {
  4818. TTerminalManager::Instance()->FreeActiveTerminal();
  4819. }
  4820. }
  4821. //---------------------------------------------------------------------------
  4822. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4823. {
  4824. if (CanCloseQueue())
  4825. {
  4826. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4827. }
  4828. }
  4829. //---------------------------------------------------------------------------
  4830. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4831. {
  4832. DetachTerminal(Terminal);
  4833. RemoteDirView->Terminal = Terminal;
  4834. UpdateRemotePathComboBox(false);
  4835. InitStatusBar();
  4836. UpdateControls();
  4837. }
  4838. //---------------------------------------------------------------------------
  4839. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4840. {
  4841. InitStatusBar();
  4842. }
  4843. //---------------------------------------------------------------------------
  4844. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4845. {
  4846. if (DebugAlwaysTrue(!Terminal->Active))
  4847. {
  4848. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4849. }
  4850. }
  4851. //---------------------------------------------------------------------------
  4852. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4853. {
  4854. if (OpenInNewWindow())
  4855. {
  4856. // encode session name because of slashes in hierarchical sessions
  4857. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4858. }
  4859. else
  4860. {
  4861. TTerminalManager * Manager = TTerminalManager::Instance();
  4862. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4863. Manager->ActiveSession = Terminal;
  4864. }
  4865. }
  4866. //---------------------------------------------------------------------------
  4867. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  4868. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  4869. {
  4870. TTerminalManager * Manager = TTerminalManager::Instance();
  4871. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4872. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4873. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  4874. {
  4875. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  4876. // FirstSession can be null, if some of the
  4877. if (!ConnectFirstTerminal && (FirstSession != NULL))
  4878. {
  4879. FirstSession->Disconnected = true;
  4880. FirstSession->DisconnectedTemporarily = true;
  4881. }
  4882. Manager->ActiveSession = FirstSession;
  4883. }
  4884. }
  4885. //---------------------------------------------------------------------------
  4886. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4887. {
  4888. if (OpenInNewWindow())
  4889. {
  4890. ExecuteNewInstance(Name);
  4891. }
  4892. else
  4893. {
  4894. DoOpenFolderOrWorkspace(Name, true, false);
  4895. }
  4896. }
  4897. //---------------------------------------------------------------------------
  4898. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4899. bool &CanClose)
  4900. {
  4901. // See the comment in CloseApp()
  4902. if (NonVisualDataModule->Busy)
  4903. {
  4904. CanClose = false;
  4905. }
  4906. else
  4907. {
  4908. TTerminalManager * Manager = TTerminalManager::Instance();
  4909. int ActiveSessions = 0;
  4910. for (int Index = 0; Index < Manager->Count; Index++)
  4911. {
  4912. if (Manager->Sessions[Index]->Active)
  4913. {
  4914. ActiveSessions++;
  4915. }
  4916. }
  4917. // Confirm, when there's at least one active remote session or
  4918. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4919. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4920. WinConfiguration->ConfirmClosingSession)
  4921. {
  4922. unsigned int Result;
  4923. TMessageParams Params(mpNeverAskAgainCheck);
  4924. UnicodeString Message;
  4925. int Answers = qaOK | qaCancel;
  4926. // The current session is the only active session
  4927. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4928. {
  4929. if (!WinConfiguration->AutoSaveWorkspace)
  4930. {
  4931. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4932. }
  4933. else
  4934. {
  4935. Message = LoadStr(CLOSE_SESSION);
  4936. }
  4937. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4938. }
  4939. // Multiple active sessions or one active session, but it's not the current one
  4940. else if (ActiveSessions > 0)
  4941. {
  4942. if (!WinConfiguration->AutoSaveWorkspace)
  4943. {
  4944. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4945. }
  4946. else
  4947. {
  4948. Message = LoadStr(CLOSE_SESSIONS);
  4949. }
  4950. }
  4951. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  4952. else
  4953. {
  4954. DebugAlwaysTrue(Manager->Count > 1);
  4955. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  4956. Message = LoadStr(CLOSE_WORKSPACE);
  4957. }
  4958. UnicodeString Note;
  4959. if (WinConfiguration->AutoSaveWorkspace)
  4960. {
  4961. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4962. }
  4963. else
  4964. {
  4965. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4966. Answers = qaYes | qaNo | qaCancel;
  4967. }
  4968. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4969. SetFocus();
  4970. Result = MessageDialog(Message, qtConfirmation,
  4971. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4972. if (Result == qaNeverAskAgain)
  4973. {
  4974. WinConfiguration->ConfirmClosingSession = false;
  4975. }
  4976. if (Result == qaNo)
  4977. {
  4978. CanClose = SaveWorkspace(true);
  4979. // note that the workspace will be saved redundatly again from FormClose
  4980. }
  4981. else
  4982. {
  4983. CanClose =
  4984. (Result == qaOK) ||
  4985. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4986. (Result == qaNeverAskAgain);
  4987. }
  4988. }
  4989. if (CanClose && HasActiveTerminal())
  4990. {
  4991. CanClose = CanCloseQueue();
  4992. }
  4993. }
  4994. if (CanClose)
  4995. {
  4996. CanClose =
  4997. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4998. FEditorManager->CloseExternalFilesWithoutProcess();
  4999. if (CanClose)
  5000. {
  5001. while (CanClose && (FLocalEditors->Count > 0))
  5002. {
  5003. int PrevCount = FLocalEditors->Count;
  5004. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  5005. CanClose = (FLocalEditors->Count < PrevCount);
  5006. }
  5007. if (CanClose)
  5008. {
  5009. CanClose = FEditorManager->Empty(true);
  5010. if (!CanClose)
  5011. {
  5012. SetFocus();
  5013. CanClose =
  5014. (MessageDialog(
  5015. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  5016. }
  5017. }
  5018. }
  5019. }
  5020. }
  5021. //---------------------------------------------------------------------------
  5022. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  5023. {
  5024. TForm * Form = dynamic_cast<TForm *>(Sender);
  5025. DebugAssert(Form != NULL);
  5026. Form->Close();
  5027. }
  5028. //---------------------------------------------------------------------------
  5029. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  5030. {
  5031. TForm * Form = dynamic_cast<TForm *>(Sender);
  5032. delete Form;
  5033. }
  5034. //---------------------------------------------------------------------------
  5035. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  5036. {
  5037. while (FLocalEditors->Count > 0)
  5038. {
  5039. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  5040. }
  5041. }
  5042. //---------------------------------------------------------------------------
  5043. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  5044. TObject *Sender)
  5045. {
  5046. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  5047. try
  5048. {
  5049. SetProperties(osRemote, FileList);
  5050. }
  5051. __finally
  5052. {
  5053. delete FileList;
  5054. }
  5055. }
  5056. //---------------------------------------------------------------------------
  5057. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  5058. {
  5059. if (value)
  5060. {
  5061. if (Component == fcCustomCommandsBand)
  5062. {
  5063. UpdateCustomCommandsToolbar();
  5064. }
  5065. if (Component == fcQueueFileList)
  5066. {
  5067. DebugAssert(!QueueFileListSplitter->Visible);
  5068. AdjustQueueLayout();
  5069. UpdateQueueFileList();
  5070. }
  5071. }
  5072. }
  5073. //---------------------------------------------------------------------------
  5074. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  5075. {
  5076. int MinHeightLimit =
  5077. GetStaticQueuePanelComponentsHeight() +
  5078. QueueFileListSplitter->Height +
  5079. QueueFileList->Height +
  5080. GetMinQueueViewHeight();
  5081. if (QueuePanel->ClientHeight < MinHeightLimit)
  5082. {
  5083. QueuePanel->ClientHeight = MinHeightLimit;
  5084. }
  5085. }
  5086. //---------------------------------------------------------------------------
  5087. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  5088. {
  5089. TControl * Control = GetComponent(Component);
  5090. DebugAssert(Control);
  5091. bool Changed = (Control->Visible != value);
  5092. if (Changed)
  5093. {
  5094. ComponentShowing(Component, value);
  5095. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  5096. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  5097. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  5098. if (value)
  5099. {
  5100. int RemainingHeight = Control->Parent->ClientHeight;
  5101. int RemainingWidth = Control->Parent->ClientWidth;
  5102. for (int i = 0; i < Control->Parent->ControlCount; i++)
  5103. {
  5104. TControl * ChildControl = Control->Parent->Controls[i];
  5105. if (ChildControl->Visible)
  5106. {
  5107. switch (ChildControl->Align)
  5108. {
  5109. case alTop:
  5110. case alBottom:
  5111. RemainingHeight -= ChildControl->Height;
  5112. break;
  5113. case alLeft:
  5114. case alRight:
  5115. RemainingWidth -= ChildControl->Width;
  5116. break;
  5117. }
  5118. }
  5119. }
  5120. int Reserve = ScaleByTextHeight(this, 32);
  5121. // queue in explorer, trees in commander
  5122. if (Control->Height > RemainingHeight - Reserve)
  5123. {
  5124. Control->Height = RemainingHeight / 2;
  5125. }
  5126. if (Control->Width > RemainingWidth - Reserve)
  5127. {
  5128. Control->Width = RemainingWidth / 2;
  5129. }
  5130. }
  5131. Control->Visible = value;
  5132. if (WasFocused && Visible)
  5133. {
  5134. DirView(osCurrent)->SetFocus();
  5135. }
  5136. FixControlsPlacement();
  5137. }
  5138. }
  5139. //---------------------------------------------------------------------------
  5140. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  5141. {
  5142. TControl * Control = GetComponent(Component);
  5143. if (Control == NULL)
  5144. {
  5145. return false;
  5146. }
  5147. else
  5148. {
  5149. return Control->Visible;
  5150. }
  5151. }
  5152. //---------------------------------------------------------------------------
  5153. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  5154. {
  5155. bool Result;
  5156. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  5157. {
  5158. Result = !IsUWP();
  5159. }
  5160. else
  5161. {
  5162. Result = true;
  5163. }
  5164. return Result;
  5165. }
  5166. //---------------------------------------------------------------------------
  5167. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  5168. {
  5169. if (DirView->ItemFocused != NULL)
  5170. {
  5171. DirView->ItemFocused->MakeVisible(false);
  5172. }
  5173. }
  5174. //---------------------------------------------------------------------------
  5175. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5176. {
  5177. MakeFocusedItemVisible(RemoteDirView);
  5178. QueueSplitter->Visible = QueuePanel->Visible;
  5179. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5180. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5181. TControl * TopControlsOrder[] =
  5182. { TopDock, MessageDock, SessionsPageControl };
  5183. SetVerticalControlsOrder(TopControlsOrder, LENOF(TopControlsOrder));
  5184. TControl * QueueControlsOrder[] =
  5185. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5186. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5187. }
  5188. //---------------------------------------------------------------------------
  5189. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5190. {
  5191. switch (Component) {
  5192. case fcStatusBar: return RemoteStatusBar;
  5193. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5194. case fcQueueView: return QueuePanel;
  5195. case fcQueueToolbar: return QueueDock;
  5196. case fcRemoteTree: return RemoteDrivePanel;
  5197. case fcSessionsTabs: return SessionsPageControl;
  5198. case fcQueueFileList: return QueueFileList;
  5199. default: return NULL;
  5200. }
  5201. }
  5202. //---------------------------------------------------------------------------
  5203. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5204. TObject *Sender, TListColumn *Column, TPoint &Point)
  5205. {
  5206. DebugAssert(NonVisualDataModule && Column && Sender);
  5207. NonVisualDataModule->ListColumn = Column;
  5208. TPopupMenu * DirViewColumnMenu;
  5209. if (Sender == DirView(osOther))
  5210. {
  5211. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5212. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  5213. (Column->Index == uvName);
  5214. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  5215. (Column->Index == uvSize);
  5216. }
  5217. else
  5218. {
  5219. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5220. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  5221. (Column->Index == dvName);
  5222. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  5223. (Column->Index == dvSize);
  5224. }
  5225. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5226. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5227. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5228. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5229. }
  5230. //---------------------------------------------------------------------------
  5231. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5232. TObject *Sender, TListItem *Item, bool &AllowExec)
  5233. {
  5234. DoDirViewExecFile(Sender, Item, AllowExec);
  5235. }
  5236. //---------------------------------------------------------------------------
  5237. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5238. TListItem * Item, bool & AllowExec)
  5239. {
  5240. DebugAssert(Sender && Item && Configuration);
  5241. DebugAssert(AllowExec);
  5242. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5243. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5244. bool Remote = !IsSideLocalBrowser(Side);
  5245. DebugAssert(Item == ADirView->ItemFocused);
  5246. bool IsDirectory = ADirView->ItemIsDirectory(Item);
  5247. TResolvedDoubleClickAction Action;
  5248. if (FForceExecution)
  5249. {
  5250. Action = IsDirectory ? rdcaChangeDir : rdcaOpen;
  5251. }
  5252. else
  5253. {
  5254. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  5255. Action = WinConfiguration->ResolveDoubleClickAction(IsDirectory, ATerminal);
  5256. }
  5257. if (Action == rdcaCopy)
  5258. {
  5259. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5260. UnlockWindow();
  5261. try
  5262. {
  5263. if (IsLocalBrowserMode())
  5264. {
  5265. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5266. }
  5267. else
  5268. {
  5269. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5270. }
  5271. }
  5272. __finally
  5273. {
  5274. LockWindow();
  5275. }
  5276. AllowExec = false;
  5277. }
  5278. else if (Action == rdcaEdit)
  5279. {
  5280. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5281. {
  5282. ExecuteFile(Side, efDefaultEditor);
  5283. AllowExec = false;
  5284. }
  5285. }
  5286. else if (Action == rdcaOpen)
  5287. {
  5288. if (DebugAlwaysTrue(!IsDirectory) && Remote && !WinConfiguration->DisableOpenEdit)
  5289. {
  5290. ExecuteFile(osRemote, efShell);
  5291. AllowExec = false;
  5292. }
  5293. }
  5294. }
  5295. //---------------------------------------------------------------------------
  5296. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5297. {
  5298. return ((Side == osRemote) || (Side == osCurrent));
  5299. }
  5300. //---------------------------------------------------------------------------
  5301. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5302. {
  5303. DebugFail();
  5304. }
  5305. //---------------------------------------------------------------------------
  5306. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5307. {
  5308. DebugFail();
  5309. }
  5310. //---------------------------------------------------------------------------
  5311. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5312. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5313. {
  5314. TSynchronizeParamType Params;
  5315. Params.LocalDirectory = LocalDirectory;
  5316. Params.RemoteDirectory = RemoteDirectory;
  5317. int UnusedParams =
  5318. (GUIConfiguration->SynchronizeParams &
  5319. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5320. TTerminal::spCaseSensitive));
  5321. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5322. Params.Options = GUIConfiguration->SynchronizeOptions;
  5323. bool SaveSettings = false;
  5324. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5325. &DoSynchronizeTooManyDirectories);
  5326. DebugAssert(FSynchronizeController == NULL);
  5327. FSynchronizeController = &Controller;
  5328. bool Result;
  5329. try
  5330. {
  5331. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5332. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5333. int Options =
  5334. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5335. DebugAssert(FOnFeedSynchronizeError == NULL);
  5336. DebugAssert(FOnSynchronizeAbort == NULL);
  5337. DebugAssert(FSynchronizeTerminal == NULL);
  5338. FSynchronizeTerminal = Terminal;
  5339. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5340. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5341. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5342. if (Result)
  5343. {
  5344. if (SaveSettings)
  5345. {
  5346. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5347. GUIConfiguration->SynchronizeOptions = Params.Options;
  5348. }
  5349. else
  5350. {
  5351. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5352. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5353. FLAGSET(Params.Options, soSynchronize))
  5354. {
  5355. GUIConfiguration->SynchronizeOptions =
  5356. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5357. soSynchronize;
  5358. }
  5359. }
  5360. LocalDirectory = Params.LocalDirectory;
  5361. RemoteDirectory = Params.RemoteDirectory;
  5362. }
  5363. }
  5364. __finally
  5365. {
  5366. FSynchronizeController = NULL;
  5367. DebugAssert(FOnFeedSynchronizeError == NULL);
  5368. FOnFeedSynchronizeError = NULL;
  5369. DebugAssert(FOnSynchronizeAbort == NULL);
  5370. FOnSynchronizeAbort = NULL;
  5371. FSynchronizeTerminal = NULL;
  5372. }
  5373. return Result;
  5374. }
  5375. //---------------------------------------------------------------------------
  5376. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5377. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5378. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5379. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5380. TSynchronizeOptions * Options, bool Full)
  5381. {
  5382. // Keep-up-to-date mode
  5383. if (Terminal->Status == ssOpened)
  5384. {
  5385. try
  5386. {
  5387. int PParams = Params.Params;
  5388. if (!Full)
  5389. {
  5390. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5391. if (FLAGSET(Params.Options, soRecurse))
  5392. {
  5393. PParams |= TTerminal::spSubDirs;
  5394. }
  5395. }
  5396. else
  5397. {
  5398. // if keepuptodate is non-recursive,
  5399. // full sync before has to be non-recursive as well
  5400. if (FLAGCLEAR(Params.Options, soRecurse))
  5401. {
  5402. PParams |= TTerminal::spNoRecurse;
  5403. }
  5404. }
  5405. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5406. PParams, Checklist, Options);
  5407. }
  5408. catch (Exception & E)
  5409. {
  5410. if (!Terminal->Active)
  5411. {
  5412. ShowExtendedExceptionEx(Terminal, &E);
  5413. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5414. if (!Terminal->Active)
  5415. {
  5416. throw;
  5417. }
  5418. }
  5419. else
  5420. {
  5421. if (FLAGSET(Params.Options, soContinueOnError) &&
  5422. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5423. {
  5424. // noop, already logged in MoreMessageDialog
  5425. }
  5426. else
  5427. {
  5428. // We get mostly EAbort here, so this is noop.
  5429. // Exception is an error when listing directories while looking for differences.
  5430. ShowExtendedExceptionEx(Terminal, &E);
  5431. throw;
  5432. }
  5433. }
  5434. }
  5435. }
  5436. }
  5437. //---------------------------------------------------------------------------
  5438. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5439. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5440. const UnicodeString ErrorStr)
  5441. {
  5442. if (!Directory.IsEmpty())
  5443. {
  5444. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5445. }
  5446. else
  5447. {
  5448. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5449. }
  5450. }
  5451. //---------------------------------------------------------------------------
  5452. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5453. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5454. {
  5455. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5456. {
  5457. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5458. }
  5459. else
  5460. {
  5461. TMessageParams Params(mpNeverAskAgainCheck);
  5462. unsigned int Result = MessageDialog(
  5463. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5464. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5465. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5466. {
  5467. MaxDirectories *= 2;
  5468. if (Result == qaNeverAskAgain)
  5469. {
  5470. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5471. }
  5472. }
  5473. else
  5474. {
  5475. Abort();
  5476. }
  5477. }
  5478. }
  5479. //---------------------------------------------------------------------------
  5480. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5481. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5482. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5483. TSynchronizeOptions * Options)
  5484. {
  5485. // Keep-up-to-date mode
  5486. DebugAssert(!FAutoOperation);
  5487. void * BatchStorage;
  5488. BatchStart(BatchStorage);
  5489. FAutoOperation = true;
  5490. bool AnyOperation = false;
  5491. TDateTime StartTime = Now();
  5492. TSynchronizeChecklist * AChecklist = NULL;
  5493. try
  5494. {
  5495. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5496. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5497. {
  5498. FSynchronizeProgressForm->Start();
  5499. }
  5500. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5501. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5502. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5503. Options);
  5504. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5505. SAFE_DESTROY(FSynchronizeProgressForm);
  5506. AnyOperation = (AChecklist->CheckedCount > 0);
  5507. if (AnyOperation)
  5508. {
  5509. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5510. CreateProgressForm(&SynchronizeProgress);
  5511. try
  5512. {
  5513. Terminal->SynchronizeApply(
  5514. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5515. SynchronizeProcessedItem, NULL, NULL, NULL);
  5516. }
  5517. __finally
  5518. {
  5519. DestroyProgressForm();
  5520. }
  5521. }
  5522. }
  5523. __finally
  5524. {
  5525. if (Checklist == NULL)
  5526. {
  5527. delete AChecklist;
  5528. }
  5529. else
  5530. {
  5531. *Checklist = AChecklist;
  5532. }
  5533. FAutoOperation = false;
  5534. SAFE_DESTROY(FSynchronizeProgressForm);
  5535. BatchEnd(BatchStorage);
  5536. ReloadLocalDirectory();
  5537. if (AnyOperation)
  5538. {
  5539. OperationComplete(StartTime);
  5540. }
  5541. }
  5542. }
  5543. //---------------------------------------------------------------------------
  5544. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5545. {
  5546. // can be called from command line
  5547. return Visible &&
  5548. ((DirView(osRemote)->SelCount > 0) ||
  5549. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5550. }
  5551. //---------------------------------------------------------------------------
  5552. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5553. {
  5554. LogSynchronizeEvent(Terminal, Message);
  5555. }
  5556. //---------------------------------------------------------------------------
  5557. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5558. int Params, TSynchronizeOptions & Options)
  5559. {
  5560. DebugAssert(!IsLocalBrowserMode());
  5561. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5562. {
  5563. Options.Filter = new TStringList();
  5564. Options.Filter->CaseSensitive = false;
  5565. Options.Filter->Duplicates = Types::dupAccept;
  5566. if (DirView(osRemote)->SelCount > 0)
  5567. {
  5568. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5569. }
  5570. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5571. {
  5572. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5573. }
  5574. Options.Filter->Sort();
  5575. }
  5576. }
  5577. //---------------------------------------------------------------------------
  5578. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5579. {
  5580. TCopyParamType Defaults;
  5581. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5582. ConfigStorage->AccessMode = smRead;
  5583. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5584. {
  5585. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5586. }
  5587. ConfigStorage.reset(NULL);
  5588. std::unique_ptr<TStringList> Options(new TStringList());
  5589. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5590. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5591. UnicodeString Result;
  5592. if (Options->Count > 0)
  5593. {
  5594. Result =
  5595. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5596. }
  5597. return Result;
  5598. }
  5599. //---------------------------------------------------------------------------
  5600. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5601. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5602. {
  5603. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5604. UnicodeString AdditionalParams =
  5605. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5606. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5607. SerializeCopyParamForCommandLine(CopyParams),
  5608. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5609. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5610. ExecuteNewInstance(SessionName, AdditionalParams);
  5611. }
  5612. //---------------------------------------------------------------------------
  5613. struct TSynchronizeParams
  5614. {
  5615. UnicodeString LocalDirectory;
  5616. UnicodeString RemoteDirectory;
  5617. TSynchronizeMode Mode;
  5618. TSynchronizeChecklist * Checklist;
  5619. int Params;
  5620. TCopyParamType * CopyParam;
  5621. TDateTime CollectElapsed;
  5622. TDateTime * StartTime;
  5623. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5624. };
  5625. //---------------------------------------------------------------------------
  5626. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5627. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5628. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5629. {
  5630. DebugAssert(!FAutoOperation);
  5631. void * BatchStorage;
  5632. BatchStart(BatchStorage);
  5633. FAutoOperation = true;
  5634. TFileOperationStatistics Statistics;
  5635. TDateTime Start = Now();
  5636. try
  5637. {
  5638. Params.OnProcessedItem = OnProcessedItem;
  5639. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5640. CreateProgressForm(&SynchronizeProgress);
  5641. Terminal->SynchronizeApply(
  5642. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5643. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5644. &Statistics);
  5645. }
  5646. __finally
  5647. {
  5648. Params.OnProcessedItem = NULL;
  5649. FAutoOperation = false;
  5650. DestroyProgressForm();
  5651. BatchEnd(BatchStorage);
  5652. ReloadLocalDirectory();
  5653. }
  5654. if (WinConfiguration->SynchronizeSummary)
  5655. {
  5656. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5657. // The statistics should be 0 anyway in this case
  5658. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5659. {
  5660. Message += L"\n";
  5661. if (Statistics.FilesUploaded > 0)
  5662. {
  5663. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5664. }
  5665. if (Statistics.FilesDownloaded > 0)
  5666. {
  5667. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5668. }
  5669. if (Statistics.FilesDeletedLocal > 0)
  5670. {
  5671. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5672. }
  5673. if (Statistics.FilesDeletedRemote > 0)
  5674. {
  5675. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5676. }
  5677. }
  5678. TDateTime Elapsed = (Now() - Start);
  5679. Message +=
  5680. L"\n" +
  5681. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5682. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5683. TMessageParams Params(mpNeverAskAgainCheck);
  5684. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5685. if (Result == qaNeverAskAgain)
  5686. {
  5687. WinConfiguration->SynchronizeSummary = false;
  5688. }
  5689. }
  5690. }
  5691. //---------------------------------------------------------------------------
  5692. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5693. {
  5694. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5695. {
  5696. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5697. }
  5698. // Not set in keep-up-to-date mode - Synchronize() method
  5699. if (Token != NULL)
  5700. {
  5701. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5702. if (Params.OnProcessedItem != NULL)
  5703. {
  5704. Params.OnProcessedItem(NULL, ChecklistItem);
  5705. }
  5706. }
  5707. }
  5708. //---------------------------------------------------------------------------
  5709. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5710. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5711. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5712. {
  5713. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5714. *Params.StartTime = Now();
  5715. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5716. }
  5717. //---------------------------------------------------------------------------
  5718. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5719. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5720. {
  5721. // terminal can be already closed (e.g. dropped connection)
  5722. if (Terminal != NULL)
  5723. {
  5724. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5725. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5726. }
  5727. }
  5728. //---------------------------------------------------------------------------
  5729. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5730. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5731. {
  5732. DebugAssert(!IsLocalBrowserMode());
  5733. TAutoBatch AutoBatch(this);
  5734. TAutoFlag AutoOperationFlag(FAutoOperation);
  5735. if (Side == osRemote)
  5736. {
  5737. std::unique_ptr<TStrings> FileList(new TStringList());
  5738. FileList->AddObject(FileName, RemoteFile);
  5739. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5740. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5741. RemoteDirView->SaveSelection();
  5742. RemoteDirView->SaveSelectedNames();
  5743. try
  5744. {
  5745. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5746. }
  5747. catch(...)
  5748. {
  5749. RemoteDirView->DiscardSavedSelection();
  5750. throw;
  5751. }
  5752. RemoteDirView->RestoreSelection();
  5753. }
  5754. else if (DebugAlwaysTrue(Side == osLocal))
  5755. {
  5756. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5757. {
  5758. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5759. }
  5760. UnicodeString Directory = ExtractFileDir(FileName);
  5761. ReloadLocalDirectory(Directory);
  5762. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5763. if (!SamePaths(Directory, NewDirectory))
  5764. {
  5765. ReloadLocalDirectory(NewDirectory);
  5766. }
  5767. }
  5768. }
  5769. //---------------------------------------------------------------------------
  5770. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5771. {
  5772. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5773. if (Side == osLocal)
  5774. {
  5775. if (Action == TSynchronizeChecklist::saDownloadNew)
  5776. {
  5777. OpenFolderInExplorer(LocalPath);
  5778. }
  5779. else
  5780. {
  5781. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5782. }
  5783. }
  5784. else if (DebugAlwaysTrue(Side == osRemote))
  5785. {
  5786. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5787. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5788. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5789. if (!LocalPath.IsEmpty())
  5790. {
  5791. SessionData->LocalDirectory = LocalPath;
  5792. }
  5793. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5794. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5795. }
  5796. }
  5797. //---------------------------------------------------------------------------
  5798. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5799. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5800. const TCopyParamType * CopyParams)
  5801. {
  5802. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5803. UnicodeString AdditionalParams =
  5804. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5805. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5806. SerializeCopyParamForCommandLine(CopyParams),
  5807. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5808. LocalDirectory, RemoteDirectory, Mode, Params));
  5809. ExecuteNewInstance(SessionName, AdditionalParams);
  5810. }
  5811. //---------------------------------------------------------------------------
  5812. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5813. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5814. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5815. {
  5816. int Result;
  5817. bool SaveSettings = false;
  5818. int Options =
  5819. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5820. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5821. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5822. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5823. bool Continue =
  5824. (UseDefaults == 0) ||
  5825. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5826. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5827. if (Continue)
  5828. {
  5829. Configuration->Usage->Inc(L"Synchronizations");
  5830. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  5831. UpdateCopyParamCounters(CopyParam);
  5832. TSynchronizeOptions SynchronizeOptions;
  5833. GetSynchronizeOptions(Params, SynchronizeOptions);
  5834. if (SaveSettings)
  5835. {
  5836. GUIConfiguration->SynchronizeParams = Params;
  5837. }
  5838. else
  5839. {
  5840. SaveMode = false;
  5841. }
  5842. TDateTime StartTime = Now();
  5843. TSynchronizeChecklist * Checklist = NULL;
  5844. try
  5845. {
  5846. DebugAssert(!FAutoOperation);
  5847. FAutoOperation = true;
  5848. try
  5849. {
  5850. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5851. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5852. int Files = -1;
  5853. if (DataList->Count >= 1)
  5854. {
  5855. Files = StrToIntDef(DataList->Strings[0], Files);
  5856. }
  5857. else
  5858. {
  5859. DataList->Add(UnicodeString());
  5860. }
  5861. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5862. FSynchronizeProgressForm->Start();
  5863. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5864. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5865. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5866. &SynchronizeOptions);
  5867. if (Terminal->SessionData->CacheDirectories)
  5868. {
  5869. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5870. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5871. }
  5872. }
  5873. __finally
  5874. {
  5875. FAutoOperation = false;
  5876. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5877. SAFE_DESTROY(FSynchronizeProgressForm);
  5878. }
  5879. TSynchronizeParams SynchronizeParams;
  5880. SynchronizeParams.LocalDirectory = LocalDirectory;
  5881. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5882. SynchronizeParams.Mode = Mode;
  5883. SynchronizeParams.CopyParam = &CopyParam;
  5884. SynchronizeParams.Params = Params;
  5885. SynchronizeParams.Checklist = Checklist;
  5886. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5887. SynchronizeParams.StartTime = &StartTime;
  5888. SynchronizeParams.OnProcessedItem = NULL;
  5889. Result = Checklist->Count;
  5890. if (Checklist->Count > 0)
  5891. {
  5892. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5893. {
  5894. if (!DoSynchronizeChecklistDialog(
  5895. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5896. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5897. {
  5898. Result = -1;
  5899. }
  5900. }
  5901. else
  5902. {
  5903. FullSynchronize(SynchronizeParams, NULL, NULL);
  5904. }
  5905. }
  5906. else
  5907. {
  5908. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5909. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5910. }
  5911. }
  5912. __finally
  5913. {
  5914. delete Checklist;
  5915. }
  5916. OperationComplete(StartTime);
  5917. }
  5918. else
  5919. {
  5920. Result = -1;
  5921. }
  5922. return Result;
  5923. }
  5924. //---------------------------------------------------------------------------
  5925. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5926. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5927. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5928. {
  5929. if (Collect)
  5930. {
  5931. DebugAssert(FSynchronizeProgressForm != NULL);
  5932. if (!FSynchronizeProgressForm->Started)
  5933. {
  5934. FSynchronizeProgressForm->Start();
  5935. }
  5936. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5937. SetTaskbarListProgressValue(CompareProgress);
  5938. }
  5939. else
  5940. {
  5941. DebugAssert(FSynchronizeProgressForm == NULL);
  5942. DebugAssert(FProgressForm != NULL);
  5943. }
  5944. }
  5945. //---------------------------------------------------------------------------
  5946. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5947. {
  5948. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  5949. TRemoteFile * File = NULL;
  5950. Terminal->ReadFile(FullFileName, File);
  5951. if (File != NULL)
  5952. {
  5953. std::unique_ptr<TRemoteFile> FileOwner(File);
  5954. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5955. Application->ShowMainForm = false;
  5956. Application->Run();
  5957. }
  5958. }
  5959. //---------------------------------------------------------------------------
  5960. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  5961. {
  5962. DebugFail();
  5963. }
  5964. //---------------------------------------------------------------------------
  5965. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5966. {
  5967. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5968. SessionData->Assign(ManagedSession->SessionData);
  5969. UpdateSessionData(SessionData.get());
  5970. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  5971. if (IsActiveTerminal(ManagedSession))
  5972. {
  5973. ManagedSession->UpdateSessionCredentials(SessionData.get());
  5974. }
  5975. if (!ManagedSession->SessionData->HasSessionName())
  5976. {
  5977. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5978. // once IsWorkspace is cleared
  5979. SessionData->Name = UnicodeString();
  5980. }
  5981. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  5982. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  5983. SessionData->IsWorkspace = false;
  5984. return SessionData.release();
  5985. }
  5986. //---------------------------------------------------------------------------
  5987. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5988. {
  5989. TSessionData * SessionData = CloneCurrentSessionData();
  5990. try
  5991. {
  5992. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5993. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5994. }
  5995. __finally
  5996. {
  5997. delete SessionData;
  5998. }
  5999. }
  6000. //---------------------------------------------------------------------------
  6001. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  6002. {
  6003. TTerminalManager * Manager = TTerminalManager::Instance();
  6004. std::unique_ptr<TObjectList> DataList(new TObjectList());
  6005. if (DebugAlwaysTrue(ManagedSession != NULL))
  6006. {
  6007. // Update ManagedSession->StateData
  6008. UpdateSession(ManagedSession);
  6009. }
  6010. Manager->SaveWorkspace(DataList.get());
  6011. return DataList.release();
  6012. }
  6013. //---------------------------------------------------------------------------
  6014. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  6015. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  6016. {
  6017. WinConfiguration->LastWorkspace = Name;
  6018. if (!SavePasswords)
  6019. {
  6020. for (int Index = 0; Index < DataList->Count; Index++)
  6021. {
  6022. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6023. if (SessionData->Link.IsEmpty())
  6024. {
  6025. SessionData->ClearSessionPasswords();
  6026. }
  6027. }
  6028. }
  6029. StoredSessions->NewWorkspace(Name, DataList);
  6030. // modified only
  6031. StoredSessions->Save(false, Explicit);
  6032. }
  6033. //---------------------------------------------------------------------------
  6034. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  6035. {
  6036. return
  6037. DefaultStr(WinConfiguration->LastWorkspace,
  6038. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  6039. }
  6040. //---------------------------------------------------------------------------
  6041. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  6042. {
  6043. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  6044. bool AnyNonStoredSessionWithPassword = false;
  6045. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  6046. bool AllNonStoredSessionsAnonymous = true;
  6047. TTerminalManager * Manager = TTerminalManager::Instance();
  6048. for (int Index = 0; Index < DataList->Count; Index++)
  6049. {
  6050. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6051. if (SessionData->Link.IsEmpty())
  6052. {
  6053. if (SessionData->HasAnySessionPassword())
  6054. {
  6055. AnyNonStoredSessionWithPassword = true;
  6056. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  6057. {
  6058. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  6059. }
  6060. }
  6061. if (!SameText(SessionData->UserName, AnonymousUserName))
  6062. {
  6063. AllNonStoredSessionsAnonymous = false;
  6064. }
  6065. }
  6066. }
  6067. bool SavePasswords;
  6068. bool * PSavePasswords;
  6069. bool NotRecommendedSavingPasswords =
  6070. !CustomWinConfiguration->UseMasterPassword &&
  6071. !AllNonStoredSessionsAnonymous;
  6072. if (Configuration->DisablePasswordStoring ||
  6073. !AnyNonStoredSessionWithPassword)
  6074. {
  6075. PSavePasswords = NULL;
  6076. SavePasswords = false;
  6077. }
  6078. else
  6079. {
  6080. PSavePasswords = &SavePasswords;
  6081. SavePasswords =
  6082. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  6083. !NotRecommendedSavingPasswords;
  6084. }
  6085. UnicodeString Name = WorkspaceName();
  6086. bool CreateShortcut = false;
  6087. bool Result =
  6088. DoSaveWorkspaceDialog(
  6089. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  6090. EnableAutoSave);
  6091. if (Result)
  6092. {
  6093. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  6094. if (CreateShortcut)
  6095. {
  6096. TOperationVisualizer Visualizer;
  6097. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  6098. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  6099. }
  6100. if (EnableAutoSave)
  6101. {
  6102. WinConfiguration->AutoSaveWorkspace = true;
  6103. WinConfiguration->AutoWorkspace = Name;
  6104. if (PSavePasswords != NULL)
  6105. {
  6106. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  6107. }
  6108. }
  6109. }
  6110. return Result;
  6111. }
  6112. //---------------------------------------------------------------------------
  6113. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  6114. {
  6115. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  6116. DebugAssert(Session == ManagedSession);
  6117. if (!IsLocalBrowserMode())
  6118. {
  6119. SAFE_DESTROY(Session->RemoteExplorerState);
  6120. if (WinConfiguration->PreservePanelState)
  6121. {
  6122. Session->RemoteExplorerState = RemoteDirView->SaveState();
  6123. }
  6124. }
  6125. UpdateSessionData(Session->StateData);
  6126. }
  6127. //---------------------------------------------------------------------------
  6128. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  6129. {
  6130. // Keep in sync with TSessionData::CopyStateData
  6131. DebugAssert(Data != NULL);
  6132. // This is inconsistent with how color (for example) is handled.
  6133. Data->Name = ManagedSession->SessionData->Name;
  6134. if (!IsLocalBrowserMode())
  6135. {
  6136. // cannot use RemoteDirView->Path, because it is empty if connection
  6137. // was already closed
  6138. // also only peek, we may not be connected at all atm
  6139. // so make sure we do not try retrieving current directory from the server
  6140. // (particularly with FTP)
  6141. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  6142. if (!ACurrentDirectory.IsEmpty())
  6143. {
  6144. Data->RemoteDirectory = ACurrentDirectory;
  6145. }
  6146. }
  6147. Data->Color = SessionColor;
  6148. }
  6149. //---------------------------------------------------------------------------
  6150. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  6151. {
  6152. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  6153. DebugAssert(Toolbar != NULL);
  6154. for (int i = 0; i < Toolbar->Items->Count; i++)
  6155. {
  6156. TTBXCustomDropDownItem * DropDownItem;
  6157. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6158. if (DropDownItem != NULL)
  6159. {
  6160. ToolbarItemResize(DropDownItem,
  6161. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  6162. Toolbar->NonClientWidth);
  6163. break;
  6164. }
  6165. }
  6166. }
  6167. //---------------------------------------------------------------------------
  6168. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6169. {
  6170. Item->EditWidth = Width;
  6171. }
  6172. //---------------------------------------------------------------------------
  6173. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6174. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6175. {
  6176. for (int i = 0; i < Toolbar->Items->Count; i++)
  6177. {
  6178. TTBXCustomDropDownItem * DropDownItem;
  6179. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6180. if (DropDownItem != NULL)
  6181. {
  6182. ASize.x -= DropDownItem->EditWidth;
  6183. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  6184. }
  6185. }
  6186. }
  6187. //---------------------------------------------------------------------------
  6188. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6189. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6190. {
  6191. if (WinConfiguration->DDWarnLackOfTempSpace)
  6192. {
  6193. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6194. if (!ADrive.IsEmpty())
  6195. {
  6196. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6197. DebugAssert(RequiredSpace >= 0);
  6198. __int64 RequiredWithReserve;
  6199. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6200. if (FreeSpace < RequiredWithReserve)
  6201. {
  6202. unsigned int Result;
  6203. TMessageParams Params(mpNeverAskAgainCheck);
  6204. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6205. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6206. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6207. if (Result == qaNeverAskAgain)
  6208. {
  6209. WinConfiguration->DDWarnLackOfTempSpace = false;
  6210. }
  6211. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6212. }
  6213. }
  6214. }
  6215. }
  6216. //---------------------------------------------------------------------------
  6217. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6218. {
  6219. DoOpenDirectoryDialog(odAddBookmark, Side);
  6220. }
  6221. //---------------------------------------------------------------------------
  6222. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6223. {
  6224. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6225. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6226. try
  6227. {
  6228. TCustomDirView * DView = DirView(Side);
  6229. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6230. {
  6231. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6232. }
  6233. }
  6234. catch (...)
  6235. {
  6236. delete VisitedDirectories;
  6237. throw;
  6238. }
  6239. return VisitedDirectories;
  6240. }
  6241. //---------------------------------------------------------------------------
  6242. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6243. TOpenDirectoryMode Mode, TOperationSide Side)
  6244. {
  6245. bool Continue = true;
  6246. if (Mode == odAddBookmark)
  6247. {
  6248. TMessageParams Params(mpNeverAskAgainCheck);
  6249. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6250. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6251. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6252. unsigned int Answer =
  6253. MessageDialog(Message,
  6254. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6255. if (Answer == qaNeverAskAgain)
  6256. {
  6257. Continue = true;
  6258. WinConfiguration->UseSharedBookmarks = true;
  6259. }
  6260. else if (Answer == qaOK)
  6261. {
  6262. Continue = true;
  6263. WinConfiguration->UseSharedBookmarks = false;
  6264. }
  6265. else
  6266. {
  6267. Continue = false;
  6268. }
  6269. }
  6270. if (Continue)
  6271. {
  6272. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6273. try
  6274. {
  6275. UnicodeString Name = DirView(Side)->PathName;
  6276. bool Repeat;
  6277. do
  6278. {
  6279. Repeat = false;
  6280. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6281. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6282. // do not allow switching to location profiles,
  6283. // if we are not connected
  6284. HasDirView[osLocal] && (Terminal != NULL)))
  6285. {
  6286. TWindowLock Lock(this);
  6287. if (!TryOpenDirectory(Side, Name))
  6288. {
  6289. Repeat = true;
  6290. Mode = odBrowse;
  6291. }
  6292. }
  6293. }
  6294. while (Repeat);
  6295. }
  6296. __finally
  6297. {
  6298. delete VisitedDirectories;
  6299. }
  6300. }
  6301. }
  6302. //---------------------------------------------------------------------------
  6303. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6304. {
  6305. DebugAssert(!Terminal->CommandSessionOpened);
  6306. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6307. }
  6308. //---------------------------------------------------------------------------
  6309. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6310. {
  6311. bool Result = Terminal->IsCapable[Capability] ||
  6312. Terminal->CommandSessionOpened;
  6313. if (!Result)
  6314. {
  6315. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6316. if (!GUIConfiguration->ConfirmCommandSession)
  6317. {
  6318. Result = true;
  6319. }
  6320. else
  6321. {
  6322. TMessageParams Params(mpNeverAskAgainCheck);
  6323. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6324. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6325. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6326. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6327. if (Answer == qaNeverAskAgain)
  6328. {
  6329. GUIConfiguration->ConfirmCommandSession = false;
  6330. Result = true;
  6331. }
  6332. else if (Answer == qaOK)
  6333. {
  6334. Result = true;
  6335. }
  6336. }
  6337. if (Result)
  6338. {
  6339. Result = CommandSessionFallback();
  6340. }
  6341. }
  6342. return Result;
  6343. }
  6344. //---------------------------------------------------------------------------
  6345. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6346. {
  6347. if (EnsureCommandSessionFallback(fcAnyCommand))
  6348. {
  6349. DoConsoleDialog(Terminal, Command);
  6350. }
  6351. }
  6352. //---------------------------------------------------------------------------
  6353. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6354. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6355. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6356. {
  6357. if (!IsSideLocalBrowser(osRemote) &&
  6358. IsFileControl(DropSourceControl, osRemote) &&
  6359. (FDDExtMapFile != NULL))
  6360. {
  6361. Accept = true;
  6362. }
  6363. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6364. DebugAssert(FDDTargetControl != NULL);
  6365. }
  6366. //---------------------------------------------------------------------------
  6367. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6368. _di_IDataObject DataObj, int KeyState,
  6369. const TPoint & Point, int & Effect, bool & Accept)
  6370. {
  6371. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6372. }
  6373. //---------------------------------------------------------------------------
  6374. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6375. _di_IDataObject DataObj, int KeyState,
  6376. const TPoint & Point, int & Effect, bool & Accept)
  6377. {
  6378. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6379. }
  6380. //---------------------------------------------------------------------------
  6381. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6382. TObject * Sender)
  6383. {
  6384. DebugUsedParam(Sender);
  6385. DebugAssert(FDDTargetControl == Sender);
  6386. FDDTargetControl = NULL;
  6387. }
  6388. //---------------------------------------------------------------------------
  6389. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6390. {
  6391. FileControlDDDragLeave(SessionsPageControl);
  6392. }
  6393. //---------------------------------------------------------------------------
  6394. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6395. {
  6396. FileControlDDDragLeave(QueueView3);
  6397. }
  6398. //---------------------------------------------------------------------------
  6399. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6400. {
  6401. DebugAssert(!IsSideLocalBrowser(Side));
  6402. DebugUsedParam(Side);
  6403. bool Edit = false;
  6404. TRemoteFile * File = NULL;
  6405. UnicodeString FileName;
  6406. UnicodeString PointTo;
  6407. bool SymbolicLink = true;
  6408. if (RemoteDirView->ItemFocused)
  6409. {
  6410. DebugAssert(RemoteDirView->ItemFocused->Data);
  6411. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6412. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6413. if (Edit)
  6414. {
  6415. FileName = File->FileName;
  6416. PointTo = File->LinkTo;
  6417. }
  6418. else if (!File->IsParentDirectory)
  6419. {
  6420. PointTo = File->FileName;
  6421. }
  6422. }
  6423. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6424. Terminal->IsCapable[fcHardLink]))
  6425. {
  6426. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6427. if (Edit)
  6428. {
  6429. DebugAssert(File->FileName == FileName);
  6430. int Params = dfNoRecursive;
  6431. Terminal->ExceptionOnFail = true;
  6432. try
  6433. {
  6434. Terminal->DeleteFile(L"", File, &Params);
  6435. }
  6436. __finally
  6437. {
  6438. Terminal->ExceptionOnFail = false;
  6439. }
  6440. }
  6441. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6442. }
  6443. }
  6444. //---------------------------------------------------------------------------
  6445. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6446. {
  6447. return
  6448. (IsSideLocalBrowser(Side) ||
  6449. (HasActiveTerminal() &&
  6450. Terminal->ResolvingSymlinks &&
  6451. Terminal->IsCapable[fcSymbolicLink]));
  6452. }
  6453. //---------------------------------------------------------------------------
  6454. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6455. {
  6456. return
  6457. !IsSideLocalBrowser(FCurrentSide) &&
  6458. (RemoteDirView->ItemFocused != NULL) &&
  6459. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6460. Terminal->SessionData->ResolveSymlinks;
  6461. }
  6462. //---------------------------------------------------------------------------
  6463. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6464. {
  6465. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6466. FForceExecution = true;
  6467. try
  6468. {
  6469. DirView(osCurrent)->ExecuteCurrentFile();
  6470. }
  6471. __finally
  6472. {
  6473. FForceExecution = false;
  6474. }
  6475. }
  6476. //---------------------------------------------------------------------------
  6477. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6478. {
  6479. TEditorData ExternalEditor;
  6480. ExternalEditor.Editor = edExternal;
  6481. bool Remember = false;
  6482. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6483. {
  6484. if (Remember)
  6485. {
  6486. TEditorList * EditorList = new TEditorList;
  6487. try
  6488. {
  6489. *EditorList = *WinConfiguration->EditorList;
  6490. bool Found = false;
  6491. int i = 0;
  6492. while (!Found && (i < EditorList->Count))
  6493. {
  6494. const TEditorPreferences * Editor = EditorList->Editors[i];
  6495. if ((Editor->Data->Editor == edExternal) &&
  6496. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6497. {
  6498. Found = true;
  6499. }
  6500. i++;
  6501. }
  6502. if (!Found)
  6503. {
  6504. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6505. WinConfiguration->EditorList = EditorList;
  6506. }
  6507. }
  6508. __finally
  6509. {
  6510. delete EditorList;
  6511. }
  6512. }
  6513. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6514. }
  6515. }
  6516. //---------------------------------------------------------------------------
  6517. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6518. {
  6519. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6520. if (FFileFindTerminal == ATerminal)
  6521. {
  6522. FFileFindTerminal = NULL;
  6523. HideFileFindDialog();
  6524. }
  6525. if (FClipboardTerminal == ATerminal)
  6526. {
  6527. ClipboardClear(); // implies ClipboardStop
  6528. }
  6529. if ((FOnSynchronizeAbort != NULL) &&
  6530. (ATerminal == FManagedSession))
  6531. {
  6532. FOnSynchronizeAbort(NULL);
  6533. }
  6534. }
  6535. //---------------------------------------------------------------------------
  6536. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6537. {
  6538. DetachTerminal(Sender);
  6539. }
  6540. //---------------------------------------------------------------------------
  6541. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6542. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6543. {
  6544. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6545. DebugAssert(Terminal != NULL);
  6546. if (Data->Terminal == Terminal)
  6547. {
  6548. Data->Terminal = NULL;
  6549. }
  6550. }
  6551. //---------------------------------------------------------------------------
  6552. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6553. {
  6554. return NULL;
  6555. }
  6556. //---------------------------------------------------------------------------
  6557. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6558. {
  6559. UpdateControls();
  6560. SessionColor = TColor(0);
  6561. UpdateRemotePathComboBox(false);
  6562. NeedSession(false);
  6563. }
  6564. //---------------------------------------------------------------------------
  6565. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6566. {
  6567. try
  6568. {
  6569. // Cache, as the login dialog can change its value.
  6570. // See also DoShow. Also see TTerminalManager::NewSession.
  6571. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6572. try
  6573. {
  6574. if (ShowLogin)
  6575. {
  6576. bool ReloadSessions = !Startup;
  6577. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6578. }
  6579. }
  6580. __finally
  6581. {
  6582. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6583. // (so there was no chance for the user to open any session yet)
  6584. if ((ShowLogin || !Startup) &&
  6585. !WinConfiguration->KeepOpenWhenNoSession &&
  6586. // this rules out the implicit Commander's local-local browser
  6587. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6588. {
  6589. TerminateApplication();
  6590. }
  6591. }
  6592. }
  6593. catch (EAbort &)
  6594. {
  6595. // swallow
  6596. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6597. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6598. }
  6599. }
  6600. //---------------------------------------------------------------------------
  6601. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6602. {
  6603. TTerminalManager * Manager = TTerminalManager::Instance();
  6604. DebugAssert(!Manager->Updating);
  6605. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6606. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6607. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6608. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6609. try
  6610. {
  6611. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6612. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6613. {
  6614. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6615. }
  6616. for (int Index = 0; Index <= Manager->Count; Index++)
  6617. {
  6618. TThemeTabSheet * TabSheet;
  6619. if (Index >= SessionsPageControl->PageCount)
  6620. {
  6621. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6622. TabSheet->PageControl = SessionsPageControl;
  6623. }
  6624. else
  6625. {
  6626. TabSheet = SessionsPageControl->Pages[Index];
  6627. }
  6628. bool IsSessionTab = (Index < Manager->Count);
  6629. if (IsSessionTab)
  6630. {
  6631. TTerminal * Terminal = Manager->Sessions[Index];
  6632. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6633. TabSheet->CaptionTruncation = tttEllipsis;
  6634. UpdateSessionTab(TabSheet);
  6635. }
  6636. else
  6637. {
  6638. TabSheet->Tag = 0; // not really needed
  6639. TabSheet->Shadowed = false;
  6640. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6641. TabSheet->CaptionTruncation = tttNoText;
  6642. // We know that we are at the last page, otherwise we could not call this (it assumes that new session tab is the last one)
  6643. UpdateNewTabTab();
  6644. }
  6645. }
  6646. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6647. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6648. {
  6649. SessionsPageControl->UpdateTabsCaptionTruncation();
  6650. }
  6651. }
  6652. __finally
  6653. {
  6654. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6655. }
  6656. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6657. }
  6658. //---------------------------------------------------------------------------
  6659. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6660. {
  6661. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6662. }
  6663. //---------------------------------------------------------------------------
  6664. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6665. {
  6666. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6667. UnicodeString TabCaption;
  6668. if (WinConfiguration->SelectiveToolbarText)
  6669. {
  6670. TabCaption = GetNewTabTabCaption();
  6671. }
  6672. TabSheet->BaseCaption = TabCaption;
  6673. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6674. }
  6675. //---------------------------------------------------------------------------
  6676. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6677. {
  6678. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6679. }
  6680. //---------------------------------------------------------------------------
  6681. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6682. {
  6683. DebugFail();
  6684. return UnicodeString();
  6685. }
  6686. //---------------------------------------------------------------------------
  6687. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6688. {
  6689. if (DebugAlwaysTrue(TabSheet != NULL))
  6690. {
  6691. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6692. if (DebugAlwaysTrue(ASession != NULL))
  6693. {
  6694. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6695. if (ASession->LocalBrowser)
  6696. {
  6697. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6698. }
  6699. else
  6700. {
  6701. TabSheet->ImageIndex = AddSessionColor(Color);
  6702. }
  6703. TTerminalManager * Manager = TTerminalManager::Instance();
  6704. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6705. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6706. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6707. }
  6708. }
  6709. }
  6710. //---------------------------------------------------------------------------
  6711. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6712. {
  6713. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6714. }
  6715. //---------------------------------------------------------------------------
  6716. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6717. {
  6718. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6719. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6720. bool Result = (Session != NULL);
  6721. if (Result)
  6722. {
  6723. TTerminalManager::Instance()->ActiveSession = Session;
  6724. }
  6725. else
  6726. {
  6727. try
  6728. {
  6729. NewTab();
  6730. }
  6731. __finally
  6732. {
  6733. SessionListChanged();
  6734. }
  6735. FSessionsPageControlNewTabTime = Now();
  6736. }
  6737. return Result;
  6738. }
  6739. //---------------------------------------------------------------------------
  6740. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6741. {
  6742. TWindowLock Lock(this);
  6743. SessionTabSwitched();
  6744. }
  6745. //---------------------------------------------------------------------------
  6746. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6747. {
  6748. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6749. DebugAssert(TransferList != NULL);
  6750. UnicodeString Name;
  6751. if (TransferList->ItemIndex <= 0)
  6752. {
  6753. Name = L"";
  6754. }
  6755. else
  6756. {
  6757. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6758. }
  6759. if (FCopyParamAutoSelected.IsEmpty())
  6760. {
  6761. // if previous preset was not autoselected, make new preset the "default"
  6762. FCopyParamDefault = Name;
  6763. }
  6764. GUIConfiguration->CopyParamCurrent = Name;
  6765. }
  6766. //---------------------------------------------------------------------------
  6767. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6768. {
  6769. // sanity check
  6770. bool ExistingPreset =
  6771. (FTransferListHoverIndex >= 0) &&
  6772. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6773. DebugAssert(ExistingPreset);
  6774. if (ExistingPreset)
  6775. {
  6776. HDC DC = GetDC(0);
  6777. TCanvas * Canvas = new TCanvas();
  6778. try
  6779. {
  6780. Canvas->Handle = DC;
  6781. Canvas->Font = GetToolbarFont(this);
  6782. UnicodeString Name;
  6783. if (FTransferListHoverIndex == 0)
  6784. {
  6785. Name = L"";
  6786. }
  6787. else
  6788. {
  6789. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6790. }
  6791. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6792. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6793. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6794. static_cast<TObject*>(GetComponent(fcTransferList)));
  6795. UnicodeString InfoStr =
  6796. GUIConfiguration->CopyParamPreset[Name].
  6797. GetInfoStr(L"; ",
  6798. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6799. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6800. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6801. {
  6802. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6803. {
  6804. InfoStr.SetLength(InfoStr.Length() - 1);
  6805. }
  6806. InfoStr += Ellipsis;
  6807. }
  6808. // UpdateCaption does not cause invalidation of whole submenu, while
  6809. // setting Caption property does.
  6810. // also it probably does not resize the label, even if necessary
  6811. // (we do not want that anyway)
  6812. TransferLabel->UpdateCaption(InfoStr);
  6813. }
  6814. __finally
  6815. {
  6816. Canvas->Handle = NULL;
  6817. ReleaseDC(0, DC);
  6818. delete Canvas;
  6819. }
  6820. }
  6821. }
  6822. //---------------------------------------------------------------------------
  6823. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6824. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6825. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6826. {
  6827. if (FTransferListHoverIndex != AHoverIndex)
  6828. {
  6829. FTransferListHoverIndex = AHoverIndex;
  6830. UpdateTransferLabel();
  6831. }
  6832. }
  6833. //---------------------------------------------------------------------------
  6834. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6835. {
  6836. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6837. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6838. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6839. {
  6840. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6841. {
  6842. if (!NonVisualDataModule->Busy)
  6843. {
  6844. OpenDirectory(GetSide(osCurrent));
  6845. }
  6846. Message.Result = 1;
  6847. }
  6848. else
  6849. {
  6850. TForm::Dispatch(&Message);
  6851. }
  6852. }
  6853. else
  6854. {
  6855. TForm::Dispatch(&Message);
  6856. }
  6857. }
  6858. //---------------------------------------------------------------------------
  6859. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6860. {
  6861. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6862. if ((FIgnoreNextDialogChar != 0) &&
  6863. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6864. {
  6865. Message.Result = 1;
  6866. }
  6867. else
  6868. {
  6869. TForm::Dispatch(&Message);
  6870. }
  6871. FIgnoreNextDialogChar = 0;
  6872. }
  6873. //---------------------------------------------------------------------------
  6874. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6875. {
  6876. // The four low-order bits are used internally by Windows
  6877. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6878. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6879. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6880. // 61730 - restore thru double click - undocumented
  6881. // 61490 - maximize thru double click - undocumented
  6882. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6883. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6884. {
  6885. SysResizing(Cmd);
  6886. }
  6887. TForm::Dispatch(&Message);
  6888. }
  6889. //---------------------------------------------------------------------------
  6890. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6891. {
  6892. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6893. // Also there no point returning TRUE as we are not able to
  6894. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6895. // Hence the process termination might be safer :)
  6896. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6897. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6898. {
  6899. Message.Result = FALSE;
  6900. }
  6901. else
  6902. {
  6903. Message.Result = TRUE;
  6904. }
  6905. // Do not call default handling as that triggers OnCloseQuery,
  6906. // where our implementation will popup confirmation dialogs, what we do not want,
  6907. // as per Vista guidelines:
  6908. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6909. }
  6910. //---------------------------------------------------------------------------
  6911. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6912. {
  6913. if (Message.EndSession && IsApplicationMinimized())
  6914. {
  6915. // WORKAROUND
  6916. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6917. // when it receives WM_ENDSESSION,
  6918. // but that sometimes (particularly when application is minimized) cause crashes.
  6919. // Crash popups message that blocks log off.
  6920. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6921. // so we call ExitProcess() immediately, not even trying to cleanup.
  6922. // It still causes beep, so there's likely some popup, but it does not block
  6923. // log off.
  6924. ExitProcess(0);
  6925. }
  6926. TForm::Dispatch(&Message);
  6927. }
  6928. #ifdef _DEBUG
  6929. //---------------------------------------------------------------------------
  6930. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6931. {
  6932. TForm::Dispatch(&Message);
  6933. }
  6934. #endif
  6935. //---------------------------------------------------------------------------
  6936. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6937. {
  6938. }
  6939. //---------------------------------------------------------------------------
  6940. void __fastcall TCustomScpExplorerForm::DoShow()
  6941. {
  6942. // Called from TCustomScpExplorerForm::CMShowingChanged
  6943. // only now are the controls resized finally, so the size constraints
  6944. // will not conflict with possibly very small window size
  6945. RestoreFormParams();
  6946. // This indicates that the form was rescaled after parameters were loaded and
  6947. // as most probably the parameters were actually saved with the current form scaling
  6948. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6949. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6950. // and no rounding problems will occur.
  6951. // See also RestoreForm.
  6952. // (we should be safe to call RestoreParams unconditionally)
  6953. if (PixelsPerInch != Screen->PixelsPerInch)
  6954. {
  6955. RestoreParams();
  6956. }
  6957. FixControlsPlacement();
  6958. if (Position == poDefaultPosOnly)
  6959. {
  6960. CutFormToDesktop(this);
  6961. }
  6962. TForm::DoShow();
  6963. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6964. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6965. // This was previously in NeedSession, where other related code is.
  6966. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  6967. // unpleasantly shows briefly.
  6968. if ((ManagedSession == NULL) &&
  6969. !WinConfiguration->ShowLoginWhenNoSession &&
  6970. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6971. // This detects if workspace was saved the last time the main widow was closed
  6972. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6973. {
  6974. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  6975. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6976. }
  6977. // Before main window displays
  6978. if (ManagedSession == NULL)
  6979. {
  6980. StartingWithoutSession();
  6981. }
  6982. UpdatePixelsPerInchMainWindowCounter();
  6983. FShowing = true;
  6984. }
  6985. //---------------------------------------------------------------------------
  6986. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6987. {
  6988. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6989. }
  6990. //---------------------------------------------------------------------------
  6991. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  6992. {
  6993. SessionListChanged();
  6994. InitStatusBar();
  6995. UpdateControls();
  6996. }
  6997. //---------------------------------------------------------------------------
  6998. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6999. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  7000. TNotifyEvent OnBalloonClick, TObject * UserData)
  7001. {
  7002. bool Do;
  7003. UnicodeString Message;
  7004. if (E == NULL)
  7005. {
  7006. Message = Str;
  7007. Do = true;
  7008. }
  7009. else
  7010. {
  7011. Do = ExceptionMessage(E, Message);
  7012. }
  7013. if (Do && WinConfiguration->BalloonNotifications)
  7014. {
  7015. UnicodeString Title;
  7016. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  7017. {
  7018. Message = TrimLeft(Message);
  7019. }
  7020. else
  7021. {
  7022. Message = UnformatMessage(Message);
  7023. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  7024. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  7025. Title = LoadResourceString(Captions[Type]);
  7026. if (Terminal != NULL)
  7027. {
  7028. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7029. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  7030. }
  7031. }
  7032. if (Seconds == 0)
  7033. {
  7034. Seconds = WinConfiguration->NotificationsTimeout;
  7035. }
  7036. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  7037. }
  7038. }
  7039. //---------------------------------------------------------------------------
  7040. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  7041. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  7042. UnicodeString HelpKeyword, const TMessageParams * Params,
  7043. TTerminal * Terminal)
  7044. {
  7045. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  7046. (FOnFeedSynchronizeError != NULL)) &&
  7047. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  7048. {
  7049. if (FOnFeedSynchronizeError != NULL)
  7050. {
  7051. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  7052. }
  7053. else
  7054. {
  7055. DebugAssert(FErrorList != NULL);
  7056. TStringList * MoreMessagesCopy = NULL;
  7057. if (MoreMessages)
  7058. {
  7059. MoreMessagesCopy = new TStringList();
  7060. MoreMessagesCopy->Assign(MoreMessages);
  7061. }
  7062. FErrorList->AddObject(Message, MoreMessagesCopy);
  7063. }
  7064. PopupTrayBalloon(Terminal, Message, Type);
  7065. return ContinueAnswer(Answers);
  7066. }
  7067. else
  7068. {
  7069. bool UseBalloon = FTrayIcon->Visible;
  7070. if (UseBalloon)
  7071. {
  7072. PopupTrayBalloon(Terminal, Message, Type);
  7073. }
  7074. unsigned int Result;
  7075. try
  7076. {
  7077. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  7078. }
  7079. __finally
  7080. {
  7081. // cancel only balloon we popped up, otherwise we may cancel notification
  7082. // balloon that was there before the message dialog
  7083. // (such as "dd confirmation opt in balloon)
  7084. if (UseBalloon)
  7085. {
  7086. FTrayIcon->CancelBalloon();
  7087. }
  7088. }
  7089. return Result;
  7090. }
  7091. }
  7092. //---------------------------------------------------------------------------
  7093. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  7094. TTerminal * Terminal, Exception * E)
  7095. {
  7096. if (FTrayIcon->Visible)
  7097. {
  7098. PopupTrayBalloon(Terminal, L"", qtError, E);
  7099. }
  7100. // particularly prevent opening new session from jump list,
  7101. // while exception is shown
  7102. NonVisualDataModule->StartBusy();
  7103. try
  7104. {
  7105. ShowExtendedExceptionEx(Terminal, E);
  7106. }
  7107. __finally
  7108. {
  7109. NonVisualDataModule->EndBusy();
  7110. FTrayIcon->CancelBalloon();
  7111. }
  7112. }
  7113. //---------------------------------------------------------------------------
  7114. void __fastcall TCustomScpExplorerForm::SessionReady()
  7115. {
  7116. InitStatusBar();
  7117. // cannot rely on active page being page for active terminal,
  7118. // as it can happen that active page is the "new session" page
  7119. // (e.g. when reconnecting active terminal, while login dialog
  7120. // invoked from "new session" page is modal)
  7121. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  7122. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  7123. }
  7124. //---------------------------------------------------------------------------
  7125. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  7126. TTerminal * Terminal, Exception * E)
  7127. {
  7128. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  7129. if (!Terminal->Active)
  7130. {
  7131. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  7132. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  7133. {
  7134. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  7135. }
  7136. }
  7137. }
  7138. //---------------------------------------------------------------------------
  7139. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  7140. UnicodeString Message, TQueryType Type,
  7141. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  7142. {
  7143. if ((E == NULL) ||
  7144. ExceptionMessage(E, Message))
  7145. {
  7146. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  7147. if (Important)
  7148. {
  7149. Seconds *= 5;
  7150. }
  7151. UnicodeString NoteMessage(UnformatMessage(Message));
  7152. if (Terminal != NULL)
  7153. {
  7154. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7155. NoteMessage = FORMAT(L"%s: %s",
  7156. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  7157. }
  7158. if (WinConfiguration->BalloonNotifications)
  7159. {
  7160. AddNote(NoteMessage);
  7161. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  7162. }
  7163. else
  7164. {
  7165. FlashOnBackground();
  7166. PostNote(NoteMessage, Seconds, OnClick, UserData);
  7167. }
  7168. }
  7169. }
  7170. //---------------------------------------------------------------------------
  7171. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7172. {
  7173. RestoreApp();
  7174. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7175. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7176. !NonVisualDataModule->Busy)
  7177. {
  7178. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7179. TTerminalManager::Instance()->ActiveSession = Terminal;
  7180. if (!ComponentVisible[fcQueueView])
  7181. {
  7182. ToggleQueueVisibility();
  7183. GoToQueue();
  7184. }
  7185. }
  7186. }
  7187. //---------------------------------------------------------------------------
  7188. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7189. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7190. {
  7191. UnicodeString Message;
  7192. TNotifyEvent OnClick = NULL;
  7193. TObject * UserData = NULL;
  7194. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7195. switch (Event)
  7196. {
  7197. case qeEmptyButMonitored:
  7198. if ((ATerminal != Terminal) || QueueInvisible)
  7199. {
  7200. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7201. OnClick = QueueEmptyNoteClicked;
  7202. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7203. TerminalNoteData->Terminal = ATerminal;
  7204. UserData = TerminalNoteData;
  7205. }
  7206. break;
  7207. case qeEmpty:
  7208. OperationComplete(ATerminal->QueueOperationStart);
  7209. break;
  7210. case qePendingUserAction:
  7211. if ((ATerminal != Terminal) ||
  7212. (QueueInvisible && !IsQueueAutoPopup()))
  7213. {
  7214. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7215. }
  7216. break;
  7217. default:
  7218. DebugFail();
  7219. }
  7220. if (!Message.IsEmpty())
  7221. {
  7222. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7223. }
  7224. }
  7225. //---------------------------------------------------------------------------
  7226. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7227. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7228. {
  7229. if (FDDExtMapFile != NULL)
  7230. {
  7231. CloseHandle(FDDExtMapFile);
  7232. FDDExtMapFile = NULL;
  7233. }
  7234. if (WinConfiguration->DDFakeFile)
  7235. {
  7236. DDFakeFileInitDrag(FileList, Created);
  7237. }
  7238. }
  7239. //---------------------------------------------------------------------------
  7240. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7241. {
  7242. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7243. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7244. {
  7245. FFakeFileDropTarget = FileName;
  7246. }
  7247. }
  7248. //---------------------------------------------------------------------------
  7249. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7250. {
  7251. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7252. if (!ForceDirectories(ApiPath(Result)))
  7253. {
  7254. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7255. }
  7256. FileSetAttr(ApiPath(Result), faHidden);
  7257. return Result;
  7258. }
  7259. //---------------------------------------------------------------------------
  7260. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7261. bool & Created)
  7262. {
  7263. FFakeFileDropTarget = UnicodeString();
  7264. FDragFakeDirectory = CreateFakeTransferDirectory();
  7265. FileList->AddItem(NULL, FDragFakeDirectory);
  7266. Created = true;
  7267. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7268. {
  7269. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7270. }
  7271. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7272. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7273. {
  7274. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7275. TDragExtCommStruct* CommStruct;
  7276. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7277. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7278. DebugAssert(CommStruct != NULL);
  7279. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7280. CommStruct->Dragging = true;
  7281. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7282. LENOF(CommStruct->DropDest));
  7283. NULL_TERMINATE(CommStruct->DropDest);
  7284. UnmapViewOfFile(CommStruct);
  7285. }
  7286. }
  7287. //---------------------------------------------------------------------------
  7288. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7289. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7290. {
  7291. bool Result;
  7292. DebugAssert(!IsLocalBrowserMode());
  7293. if (Sender == RemoteDirView)
  7294. {
  7295. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7296. }
  7297. else
  7298. {
  7299. DebugAssert(Sender == RemoteDriveView);
  7300. TStrings * FileList = RemoteDriveView->DragFileList();
  7301. try
  7302. {
  7303. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7304. }
  7305. __finally
  7306. {
  7307. delete FileList;
  7308. }
  7309. }
  7310. if (FDDTargetControl == RemoteDriveView)
  7311. {
  7312. RemoteDriveView->UpdateDropTarget();
  7313. }
  7314. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7315. // or from tree view to dir view.
  7316. // foMove happens when file/dir is dragged from remote tree view outside of
  7317. // application via dragex
  7318. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7319. (DropSourceControl == RemoteDriveView))
  7320. {
  7321. RemoteDriveView->UpdateDropSource();
  7322. }
  7323. return Result;
  7324. }
  7325. //---------------------------------------------------------------------------
  7326. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7327. {
  7328. DebugAssert(!IsLocalBrowserMode());
  7329. // This also handles drops of remote files to queue.
  7330. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7331. SAFE_DESTROY(FDDFileList);
  7332. if (!FFakeFileDropTarget.IsEmpty())
  7333. {
  7334. RemoveDir(ApiPath(FFakeFileDropTarget));
  7335. }
  7336. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7337. {
  7338. try
  7339. {
  7340. TDragResult DDResult = (Sender == RemoteDirView) ?
  7341. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7342. // Focus is moved to the target application,
  7343. // but as we are going to present the UI, we need to steal the focus back.
  7344. // This most likely won't work though (windows does not allow application
  7345. // to steal focus most of the time)
  7346. Application->BringToFront();
  7347. // On older version of Windows we never got drMove here, see also comment below.
  7348. // On Windows 10, we get the "move".
  7349. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7350. {
  7351. UnicodeString TargetDirectory;
  7352. TFileOperation Operation;
  7353. // drInvalid may mean drMove, see comment below
  7354. switch (DDResult)
  7355. {
  7356. case drCopy:
  7357. Operation = foCopy;
  7358. break;
  7359. case drMove:
  7360. Operation = foMove;
  7361. break;
  7362. default:
  7363. DebugFail();
  7364. case drInvalid:
  7365. // prefer "copy" for safety
  7366. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7367. break;
  7368. }
  7369. TTransferOperationParam Param;
  7370. UnicodeString CounterName;
  7371. bool ForceQueue;
  7372. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7373. {
  7374. // we get drInvalid both if d&d was intercepted by ddext,
  7375. // and when users drops on no-drop location.
  7376. // we tell the difference by existence of response from ddext,
  7377. // so we ignore absence of response in this case
  7378. if (DDResult != drInvalid)
  7379. {
  7380. // here we know that the extension is installed,
  7381. // as it is checked as soon as drag&drop starts from
  7382. // RemoteFileControlDDCreateDragFileList
  7383. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7384. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7385. {
  7386. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7387. }
  7388. else
  7389. {
  7390. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7391. }
  7392. }
  7393. }
  7394. else
  7395. {
  7396. // download using fake file
  7397. Param.Temp = false;
  7398. Param.DragDrop = true;
  7399. if (ForceQueue)
  7400. {
  7401. Param.Queue = asOn;
  7402. }
  7403. if (Sender == RemoteDirView)
  7404. {
  7405. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7406. }
  7407. if (RemoteFileControlFileOperation(Sender, Operation,
  7408. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7409. {
  7410. Configuration->Usage->Inc(CounterName);
  7411. }
  7412. }
  7413. }
  7414. }
  7415. __finally
  7416. {
  7417. CloseHandle(FDDExtMapFile);
  7418. FDDExtMapFile = NULL;
  7419. delete FDragFakeMonitors;
  7420. FDragFakeMonitors = NULL;
  7421. RemoveDir(ApiPath(FDragFakeDirectory));
  7422. FDragFakeDirectory = L"";
  7423. FFakeFileDropTarget = UnicodeString();
  7424. }
  7425. }
  7426. if (FDragDropSshTerminate.get() != NULL)
  7427. {
  7428. FDragDropSshTerminate->Rethrow();
  7429. }
  7430. }
  7431. //---------------------------------------------------------------------------
  7432. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7433. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7434. {
  7435. // Remember drop effect so we know (when user drops files), if we copy or move
  7436. FLastDropEffect = dwEffect;
  7437. }
  7438. //---------------------------------------------------------------------------
  7439. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7440. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7441. {
  7442. bool Result;
  7443. if (FDDTargetControl == QueueView3)
  7444. {
  7445. Directory = DefaultDownloadTargetDirectory();
  7446. Result = true;
  7447. CounterName = L"DownloadsDragDropQueue";
  7448. ForceQueue = true;
  7449. }
  7450. else if (!FFakeFileDropTarget.IsEmpty())
  7451. {
  7452. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7453. Result = true;
  7454. ForceQueue = false;
  7455. CounterName = L"DownloadsDragDropFakeFile";
  7456. }
  7457. else
  7458. {
  7459. ForceQueue = false;
  7460. Enabled = false;
  7461. try
  7462. {
  7463. int Timer = 0;
  7464. Result = false;
  7465. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7466. {
  7467. {
  7468. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7469. TDragExtCommStruct* CommStruct;
  7470. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7471. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7472. DebugAssert(CommStruct != NULL);
  7473. Result = !CommStruct->Dragging;
  7474. if (Result)
  7475. {
  7476. Directory = ExtractFilePath(CommStruct->DropDest);
  7477. CounterName = L"DownloadsDragDropExternalExt";
  7478. }
  7479. UnmapViewOfFile(CommStruct);
  7480. }
  7481. if (!Result)
  7482. {
  7483. Sleep(50);
  7484. Timer += 50;
  7485. Application->ProcessMessages();
  7486. }
  7487. }
  7488. }
  7489. __finally
  7490. {
  7491. Enabled = true;
  7492. }
  7493. }
  7494. return Result;
  7495. }
  7496. //---------------------------------------------------------------------------
  7497. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7498. const UnicodeString TempDir, int SecDelay)
  7499. {
  7500. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7501. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7502. if (FDelayedDeletionTimer == NULL)
  7503. {
  7504. DebugAssert(HandleAllocated());
  7505. FDelayedDeletionTimer = new TTimer(this);
  7506. FDelayedDeletionTimer->Interval = 10000;
  7507. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7508. }
  7509. else
  7510. {
  7511. FDelayedDeletionTimer->Enabled = true;
  7512. }
  7513. }
  7514. //---------------------------------------------------------------------------
  7515. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7516. {
  7517. DebugAssert(FDelayedDeletionList != NULL);
  7518. TDateTime N = Now();
  7519. TDateTime Alert;
  7520. UnicodeString Directory;
  7521. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7522. {
  7523. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7524. if ((N >= Alert) || (Sender == NULL))
  7525. {
  7526. Directory = FDelayedDeletionList->Strings[Index];
  7527. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7528. {
  7529. FDelayedDeletionList->Delete(Index);
  7530. }
  7531. }
  7532. }
  7533. if (FDelayedDeletionList->Count == 0)
  7534. {
  7535. FDelayedDeletionTimer->Enabled = false;
  7536. }
  7537. }
  7538. //---------------------------------------------------------------------------
  7539. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7540. {
  7541. DebugAssert(!IsLocalBrowserMode());
  7542. if (IsFileControl(FDDTargetControl, osRemote) ||
  7543. (FDDTargetControl == SessionsPageControl))
  7544. {
  7545. TManagedTerminal * TargetTerminal = NULL;
  7546. TFileOperation Operation = foNone;
  7547. if (FDDTargetControl == SessionsPageControl)
  7548. {
  7549. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7550. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7551. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7552. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7553. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7554. {
  7555. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7556. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7557. {
  7558. Operation = foRemoteMove;
  7559. }
  7560. else
  7561. {
  7562. Operation = foRemoteCopy;
  7563. }
  7564. }
  7565. }
  7566. else
  7567. {
  7568. if (FLastDropEffect == DROPEFFECT_MOVE)
  7569. {
  7570. Operation = foRemoteMove;
  7571. }
  7572. else if (FLastDropEffect == DROPEFFECT_COPY)
  7573. {
  7574. Operation = foRemoteCopy;
  7575. }
  7576. }
  7577. if (Operation != foNone)
  7578. {
  7579. RemoteFileControlFileOperation(DropSourceControl,
  7580. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7581. }
  7582. // abort drag&drop
  7583. Abort();
  7584. }
  7585. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7586. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7587. (FDDTargetControl != QueueView3))
  7588. {
  7589. DebugAssert(!FDragTempDir.IsEmpty());
  7590. TTransferType Type;
  7591. UnicodeString TempDir = FDragTempDir;
  7592. // We clear FDragTempDir before calling
  7593. // just in case it fail (raises exception)
  7594. FDragTempDir = L"";
  7595. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7596. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7597. // empty directory parameter means temp directory -> don't display it!
  7598. UnicodeString TargetDir = L"";
  7599. int Options = 0;
  7600. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7601. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7602. {
  7603. Abort();
  7604. }
  7605. // TargetDir is set when dropped on local file control
  7606. // (this was workaround for legacy dirview event handling, now it should be
  7607. // made prettier)
  7608. if (TargetDir.IsEmpty())
  7609. {
  7610. TargetDir = TempDir;
  7611. if (ForceDirectories(ApiPath(TargetDir)))
  7612. {
  7613. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7614. FPendingTempSpaceWarn = true;
  7615. try
  7616. {
  7617. FDragDropOperation = true;
  7618. int Params = cpTemporary |
  7619. (Type == ttMove ? cpDelete : 0);
  7620. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7621. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7622. }
  7623. __finally
  7624. {
  7625. FDragDropOperation = false;
  7626. FPendingTempSpaceWarn = false;
  7627. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7628. }
  7629. }
  7630. else
  7631. {
  7632. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7633. }
  7634. }
  7635. }
  7636. }
  7637. //---------------------------------------------------------------------------
  7638. void __fastcall TCustomScpExplorerForm::DDDownload(
  7639. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  7640. {
  7641. DebugAssert(!IsLocalBrowserMode());
  7642. TAutoBatch AutoBatch(this);
  7643. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  7644. UpdateCopyParamCounters(*CopyParam);
  7645. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7646. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7647. {
  7648. RemoteDriveView->UpdateDropSource();
  7649. }
  7650. }
  7651. //---------------------------------------------------------------------------
  7652. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7653. {
  7654. public:
  7655. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7656. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7657. {
  7658. }
  7659. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7660. {
  7661. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7662. TDataObjectFilesEx::AllowData(FormatEtc);
  7663. }
  7664. };
  7665. //---------------------------------------------------------------------------
  7666. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7667. TObject * Sender, TDataObject *& DataObject)
  7668. {
  7669. if (FDDExtMapFile != NULL)
  7670. {
  7671. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7672. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7673. {
  7674. Abort();
  7675. }
  7676. if (FileList->Count > 0)
  7677. {
  7678. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7679. if (!FilesObject->IsValid(true, true))
  7680. {
  7681. FilesObject->_Release();
  7682. }
  7683. else
  7684. {
  7685. DataObject = FilesObject;
  7686. }
  7687. }
  7688. }
  7689. }
  7690. //---------------------------------------------------------------------------
  7691. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7692. {
  7693. DebugFail();
  7694. }
  7695. //---------------------------------------------------------------------------
  7696. void __fastcall TCustomScpExplorerForm::GoToTree()
  7697. {
  7698. ComponentVisible[fcRemoteTree] = true;
  7699. DriveView(osRemote)->SetFocus();
  7700. }
  7701. //---------------------------------------------------------------------------
  7702. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7703. TPanelExport Export)
  7704. {
  7705. TCustomDirView * DirView = this->DirView(Side);
  7706. std::unique_ptr<TStrings> ExportData(new TStringList());
  7707. switch (Export)
  7708. {
  7709. case pePath:
  7710. ExportData->Add(DirView->PathName);
  7711. break;
  7712. case peFileList:
  7713. case peFullFileList:
  7714. {
  7715. bool FullPath = (Export == peFullFileList);
  7716. DirView->CreateFileList(false, FullPath, ExportData.get());
  7717. for (int Index = 0; Index < ExportData->Count; Index++)
  7718. {
  7719. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7720. {
  7721. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7722. }
  7723. }
  7724. }
  7725. break;
  7726. default:
  7727. DebugFail();
  7728. }
  7729. return ExportData.release();
  7730. }
  7731. //---------------------------------------------------------------------------
  7732. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7733. TPanelExport Export, TPanelExportDestination Destination)
  7734. {
  7735. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7736. PanelExportStore(Side, Export, Destination, ExportData.get());
  7737. }
  7738. //---------------------------------------------------------------------------
  7739. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7740. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7741. TStrings * ExportData)
  7742. {
  7743. if (Destination == pedClipboard)
  7744. {
  7745. TInstantOperationVisualizer Visualizer;
  7746. CopyToClipboard(ExportData);
  7747. }
  7748. else
  7749. {
  7750. DebugFail();
  7751. }
  7752. }
  7753. //---------------------------------------------------------------------------
  7754. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7755. {
  7756. TCustomDirView * DirView = this->DirView(Side);
  7757. UnicodeString Mask = DirView->Mask;
  7758. if (DoFilterMaskDialog(DirView, Mask))
  7759. {
  7760. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7761. Configuration->Usage->Inc(L"Filters");
  7762. }
  7763. }
  7764. //---------------------------------------------------------------------------
  7765. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7766. {
  7767. return FQueueController->DefaultOperation();
  7768. }
  7769. //---------------------------------------------------------------------------
  7770. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7771. TQueueOperation Operation, void ** Param)
  7772. {
  7773. switch (Operation)
  7774. {
  7775. case qoPreferences:
  7776. return true;
  7777. case qoGoTo:
  7778. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7779. case qoOnceEmpty:
  7780. return IsAnythingQueued();
  7781. default:
  7782. return FQueueController->AllowOperation(Operation, Param);
  7783. }
  7784. }
  7785. //---------------------------------------------------------------------------
  7786. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7787. {
  7788. if (DebugAlwaysTrue(QueueView3->Visible))
  7789. {
  7790. QueueView3->SetFocus();
  7791. }
  7792. }
  7793. //---------------------------------------------------------------------------
  7794. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7795. TQueueOperation Operation, void * Param)
  7796. {
  7797. if (Operation == qoGoTo)
  7798. {
  7799. GoToQueue();
  7800. }
  7801. else if (Operation == qoPreferences)
  7802. {
  7803. PreferencesDialog(pmQueue);
  7804. }
  7805. else
  7806. {
  7807. FQueueController->ExecuteOperation(Operation, Param);
  7808. }
  7809. }
  7810. //---------------------------------------------------------------------------
  7811. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7812. {
  7813. return (Queue != NULL) && Queue->Enabled;
  7814. }
  7815. //---------------------------------------------------------------------------
  7816. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7817. {
  7818. DebugAssert(Queue != NULL);
  7819. if (Queue != NULL)
  7820. {
  7821. Queue->Enabled = !Queue->Enabled;
  7822. }
  7823. }
  7824. //---------------------------------------------------------------------------
  7825. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7826. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7827. {
  7828. FQueueActedItem = QueueView3->ItemFocused;
  7829. }
  7830. //---------------------------------------------------------------------------
  7831. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7832. {
  7833. return TopDock->Height + (MessageDock->Visible ? MessageDock->Height : 0) + QueueSplitter->Height;
  7834. }
  7835. //---------------------------------------------------------------------------
  7836. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7837. {
  7838. return
  7839. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7840. (QueueDock->Visible ? QueueDock->Height : 0) +
  7841. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7842. }
  7843. //---------------------------------------------------------------------------
  7844. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7845. {
  7846. return ScaleByTextHeight(this, 48);
  7847. }
  7848. //---------------------------------------------------------------------------
  7849. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7850. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7851. {
  7852. // when queue is hidden by double-clicking splitter, stray attempt to
  7853. // resize the panel with strange value arrives, make sure it is ignored
  7854. if (ComponentVisible[fcQueueView])
  7855. {
  7856. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7857. if (NewSize > MaxHeightLimit)
  7858. {
  7859. NewSize = MaxHeightLimit;
  7860. }
  7861. int MinHeightLimit =
  7862. GetStaticQueuePanelComponentsHeight() +
  7863. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7864. GetMinQueueViewHeight();
  7865. if (NewSize < MinHeightLimit)
  7866. {
  7867. NewSize = MinHeightLimit;
  7868. }
  7869. }
  7870. else
  7871. {
  7872. Accept = false;
  7873. }
  7874. }
  7875. //---------------------------------------------------------------------------
  7876. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7877. {
  7878. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7879. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7880. if (QueueViewHeight < GetMinQueueViewHeight())
  7881. {
  7882. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7883. }
  7884. }
  7885. //---------------------------------------------------------------------------
  7886. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7887. TDragObject *& /*DragObject*/)
  7888. {
  7889. FQueueActedItem = QueueView3->ItemFocused;
  7890. }
  7891. //---------------------------------------------------------------------------
  7892. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7893. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7894. {
  7895. Accept = true;
  7896. if (Source == QueueView3)
  7897. {
  7898. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7899. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7900. if (Accept)
  7901. {
  7902. TQueueItemProxy * QueueItem;
  7903. TQueueItemProxy * DestQueueItem;
  7904. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7905. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7906. Accept = (QueueItem != DestQueueItem) &&
  7907. (QueueItem->Status == TQueueItem::qsPending) &&
  7908. (DestQueueItem->Status == TQueueItem::qsPending);
  7909. }
  7910. }
  7911. }
  7912. //---------------------------------------------------------------------------
  7913. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7914. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7915. {
  7916. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7917. {
  7918. TQueueItemProxy * QueueItem;
  7919. TQueueItemProxy * DestQueueItem;
  7920. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7921. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7922. QueueItem->Move(DestQueueItem);
  7923. }
  7924. }
  7925. //---------------------------------------------------------------------------
  7926. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7927. {
  7928. if ((QueueView3->ItemFocused == NULL) &&
  7929. (QueueView3->Items->Count > 0))
  7930. {
  7931. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7932. }
  7933. QueueLabelUpdateStatus();
  7934. }
  7935. //---------------------------------------------------------------------------
  7936. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7937. {
  7938. QueueLabelUpdateStatus();
  7939. }
  7940. //---------------------------------------------------------------------------
  7941. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7942. {
  7943. QueueLabel->UpdateStatus();
  7944. }
  7945. //---------------------------------------------------------------------------
  7946. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7947. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7948. {
  7949. if (Selected)
  7950. {
  7951. NonVisualDataModule->UpdateNonVisibleActions();
  7952. }
  7953. }
  7954. //---------------------------------------------------------------------------
  7955. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7956. {
  7957. TDragDropFilesEx * Result = NULL;
  7958. if (Sender == SessionsPageControl)
  7959. {
  7960. Result = FSessionsDragDropFilesEx;
  7961. }
  7962. else if (Sender == QueueView3)
  7963. {
  7964. Result = FQueueDragDropFilesEx;
  7965. }
  7966. else
  7967. {
  7968. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7969. if (DirView != NULL)
  7970. {
  7971. Result = DirView->DragDropFilesEx;
  7972. }
  7973. else
  7974. {
  7975. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7976. if (DriveView != NULL)
  7977. {
  7978. Result = DriveView->DragDropFilesEx;
  7979. }
  7980. }
  7981. }
  7982. DebugAssert(Result != NULL);
  7983. return Result;
  7984. }
  7985. //---------------------------------------------------------------------------
  7986. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7987. {
  7988. return (DView->SelCount == DView->FilesCount);
  7989. }
  7990. //---------------------------------------------------------------------------
  7991. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7992. {
  7993. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7994. }
  7995. //---------------------------------------------------------------------------
  7996. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7997. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7998. {
  7999. DebugAssert(!IsLocalBrowserMode());
  8000. TFileOperation Operation;
  8001. switch (Effect)
  8002. {
  8003. case DROPEFFECT_MOVE:
  8004. Operation = foMove;
  8005. break;
  8006. case DROPEFFECT_COPY:
  8007. // occures on WinXP (reported by user)
  8008. default:
  8009. Operation = foCopy;
  8010. break;
  8011. };
  8012. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  8013. // see a comment in TUnixDirView::PerformItemDragDropOperation
  8014. if (DragDropFilesEx->FileList->Count > 0)
  8015. {
  8016. TStrings * FileList = new TStringList();
  8017. try
  8018. {
  8019. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  8020. {
  8021. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  8022. }
  8023. FDragDropOperation = true;
  8024. TTransferOperationParam Param;
  8025. Param.TargetDirectory = TargetPath;
  8026. // upload, no temp dirs
  8027. Param.Temp = false;
  8028. Param.DragDrop = DragDrop;
  8029. Param.Options =
  8030. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  8031. if (ForceQueue)
  8032. {
  8033. Param.Queue = asOn;
  8034. }
  8035. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  8036. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  8037. {
  8038. if (IsFileControl(DropSourceControl, osLocal))
  8039. {
  8040. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  8041. }
  8042. else
  8043. {
  8044. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  8045. }
  8046. }
  8047. }
  8048. __finally
  8049. {
  8050. FDragDropOperation = false;
  8051. delete FileList;
  8052. }
  8053. }
  8054. }
  8055. //---------------------------------------------------------------------------
  8056. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  8057. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  8058. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  8059. {
  8060. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  8061. }
  8062. //---------------------------------------------------------------------------
  8063. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  8064. TObject * Sender, int grfKeyState, int & dwEffect)
  8065. {
  8066. DebugAssert(!IsLocalBrowserMode());
  8067. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  8068. if ((dwEffect != DROPEFFECT_NONE) &&
  8069. IsFileControl(DropSourceControl, osRemote))
  8070. {
  8071. // do not allow drop on remote panel (on free space, still allow drop on directories)
  8072. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  8073. (RemoteDirView->DropTarget == NULL))
  8074. {
  8075. dwEffect = DROPEFFECT_NONE;
  8076. }
  8077. else
  8078. {
  8079. if (dwEffect == DROPEFFECT_COPY)
  8080. {
  8081. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  8082. // currently we support copying always (at least via temporary directory);
  8083. // remove associated checks once this all proves stable and working
  8084. bool CopyCapable = true;
  8085. // if we do not support neither of operations, there's no discussion
  8086. if (!MoveCapable && !CopyCapable)
  8087. {
  8088. dwEffect = DROPEFFECT_NONE;
  8089. }
  8090. else
  8091. {
  8092. // The default effect inside remote panel is move,
  8093. // unless we do not support it, but support copy
  8094. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  8095. {
  8096. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  8097. }
  8098. else
  8099. {
  8100. // with ctrl-down, we want copy unless it is not supported
  8101. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  8102. }
  8103. }
  8104. }
  8105. }
  8106. }
  8107. }
  8108. //---------------------------------------------------------------------------
  8109. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  8110. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  8111. {
  8112. DebugAssert(!IsLocalBrowserMode());
  8113. if (FDDTotalSize >= 0)
  8114. {
  8115. if (File->IsDirectory)
  8116. {
  8117. FDDTotalSize = -1;
  8118. }
  8119. else
  8120. {
  8121. FDDTotalSize += File->Size;
  8122. }
  8123. }
  8124. DebugAssert(!FDragTempDir.IsEmpty());
  8125. // TODO: this is quite ineffective
  8126. // TODO: what if invalid character replacement is disabled?
  8127. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  8128. UnicodeString TransferFileName;
  8129. if (Sender == RemoteDriveView)
  8130. {
  8131. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  8132. }
  8133. else
  8134. {
  8135. TransferFileName = File->FileName;
  8136. }
  8137. FDDFileList->AddObject(TransferFileName, File);
  8138. }
  8139. //---------------------------------------------------------------------------
  8140. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  8141. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  8142. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  8143. {
  8144. // sometimes we do not get DDEnd so the list is not released
  8145. SAFE_DESTROY(FDDFileList);
  8146. FDDFileList = new TStringList();
  8147. FDragTempDir = WinConfiguration->TemporaryDir();
  8148. FDDTotalSize = 0;
  8149. FDragDropSshTerminate.reset(NULL);
  8150. }
  8151. //---------------------------------------------------------------------------
  8152. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  8153. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  8154. HRESULT & Result)
  8155. {
  8156. if (Result == DRAGDROP_S_DROP)
  8157. {
  8158. try
  8159. {
  8160. GlobalDragImageList->HideDragImage();
  8161. try
  8162. {
  8163. RemoteFileControlDDTargetDrop();
  8164. }
  8165. catch(ESshTerminate & E)
  8166. {
  8167. FDragDropSshTerminate.reset(E.Clone());
  8168. }
  8169. }
  8170. catch (Exception &E)
  8171. {
  8172. // If downloading fails we need to cancel drag&drop, otherwise
  8173. // Explorer shows error
  8174. // But by the way exception probably never reach this point as
  8175. // it's catched on way.
  8176. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8177. Result = DRAGDROP_S_CANCEL;
  8178. ShowExtendedException(Terminal, &E);
  8179. }
  8180. }
  8181. }
  8182. //---------------------------------------------------------------------------
  8183. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8184. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8185. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8186. {
  8187. TFileMasks::TParams MaskParams;
  8188. MaskParams.Size = Size;
  8189. MaskParams.Modification = Modification;
  8190. // this does not re-parse the mask if it is the same as the last time
  8191. FDirViewMatchMask = Masks;
  8192. bool ImplicitMatch;
  8193. Matches =
  8194. // Local and RecurseInclude parameters have no effect as there's no path.
  8195. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  8196. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  8197. (AllowImplicitMatches || !ImplicitMatch);
  8198. }
  8199. //---------------------------------------------------------------------------
  8200. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8201. TObject * Sender, TListItem * Item, WORD & Indexes)
  8202. {
  8203. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8204. UnicodeString Ext;
  8205. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  8206. {
  8207. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  8208. }
  8209. else
  8210. {
  8211. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  8212. }
  8213. if (SameText(Ext, Configuration->PartialExt))
  8214. {
  8215. Indexes |= oiPartial;
  8216. }
  8217. }
  8218. //---------------------------------------------------------------------------
  8219. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8220. {
  8221. return
  8222. DirViewEnabled(osCurrent) &&
  8223. DirView(osCurrent)->CanPasteFromClipBoard();
  8224. }
  8225. //---------------------------------------------------------------------------
  8226. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8227. {
  8228. bool Result = false;
  8229. if (CanPasteToDirViewFromClipBoard())
  8230. {
  8231. Result = true;
  8232. }
  8233. else
  8234. {
  8235. UnicodeString ClipboardText;
  8236. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8237. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8238. {
  8239. if (StoredSessions->IsUrl(ClipboardText))
  8240. {
  8241. Result = true;
  8242. }
  8243. else
  8244. {
  8245. Result = DirViewEnabled(osCurrent);
  8246. }
  8247. }
  8248. }
  8249. return Result;
  8250. }
  8251. //---------------------------------------------------------------------------
  8252. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8253. {
  8254. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8255. if (DoesClipboardContainOurFiles())
  8256. {
  8257. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8258. {
  8259. DebugAssert(!IsLocalBrowserMode());
  8260. TTerminalManager * Manager = TTerminalManager::Instance();
  8261. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8262. Manager->ActiveSession = FClipboardTerminal;
  8263. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8264. }
  8265. }
  8266. else if (CanPasteToDirViewFromClipBoard())
  8267. {
  8268. DirView(osCurrent)->PasteFromClipBoard();
  8269. }
  8270. else
  8271. {
  8272. UnicodeString ClipboardText;
  8273. if (NonEmptyTextFromClipboard(ClipboardText))
  8274. {
  8275. if (StoredSessions->IsUrl(ClipboardText))
  8276. {
  8277. NewSession(ClipboardText);
  8278. }
  8279. else
  8280. {
  8281. DirView(osCurrent)->Path = ClipboardText;
  8282. }
  8283. }
  8284. }
  8285. }
  8286. //---------------------------------------------------------------------------
  8287. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8288. {
  8289. // TBD
  8290. }
  8291. //---------------------------------------------------------------------------
  8292. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8293. {
  8294. TCustomDirView * ADirView = DirView(Side);
  8295. ADirView->SelectAll(Mode);
  8296. ADirView->SetFocus();
  8297. }
  8298. //---------------------------------------------------------------------------
  8299. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8300. {
  8301. TCustomDirView * ADirView = DirView(Side);
  8302. TFileFilter Filter;
  8303. DefaultFileFilter(Filter);
  8304. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8305. {
  8306. Configuration->Usage->Inc(L"MaskSelections");
  8307. ADirView->SelectFiles(Filter, Select);
  8308. ADirView->SetFocus();
  8309. }
  8310. }
  8311. //---------------------------------------------------------------------------
  8312. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8313. {
  8314. TCustomDirView * ADirView = DirView(Side);
  8315. ADirView->RestoreSelectedNames();
  8316. ADirView->SetFocus();
  8317. }
  8318. //---------------------------------------------------------------------------
  8319. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8320. {
  8321. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8322. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8323. {
  8324. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8325. UnicodeString Ext;
  8326. if (!IsSideLocalBrowser(osCurrent))
  8327. {
  8328. Ext = UnixExtractFileExt(FileName);
  8329. }
  8330. else
  8331. {
  8332. Ext = ExtractFileExt(FileName);
  8333. }
  8334. if (Ext.IsEmpty())
  8335. {
  8336. Ext = L".";
  8337. }
  8338. TFileFilter Filter;
  8339. Filter.Masks = FORMAT(L"*%s", (Ext));
  8340. Filter.Directories = false;
  8341. CurrentDirView->SelectFiles(Filter, Select);
  8342. }
  8343. }
  8344. //---------------------------------------------------------------------------
  8345. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8346. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8347. {
  8348. UnicodeString Result;
  8349. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8350. {
  8351. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8352. }
  8353. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8354. {
  8355. // noop
  8356. }
  8357. else
  8358. {
  8359. Result =
  8360. FMTLOAD(FILE_INFO_FORMAT,
  8361. (FormatBytes(FileInfo.SelectedSize),
  8362. FormatBytes(FileInfo.FilesSize),
  8363. FormatNumber(FileInfo.SelectedCount),
  8364. FormatNumber(FileInfo.FilesCount)));
  8365. }
  8366. return Result;
  8367. }
  8368. //---------------------------------------------------------------------------
  8369. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8370. TTBXStatusPanel * Panel, TOperationSide Side)
  8371. {
  8372. if (Panel->Index == 1)
  8373. {
  8374. ToggleShowHiddenFiles();
  8375. }
  8376. else if (Panel->Index == 2)
  8377. {
  8378. Filter(Side);
  8379. }
  8380. }
  8381. //---------------------------------------------------------------------------
  8382. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8383. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8384. {
  8385. DebugAssert(!StatusBar->SimplePanel);
  8386. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8387. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8388. }
  8389. //---------------------------------------------------------------------------
  8390. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8391. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8392. {
  8393. DebugAssert(StatusBar->Panels->Count >= 3);
  8394. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8395. if (FileInfo.HiddenCount > 0)
  8396. {
  8397. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8398. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8399. }
  8400. else
  8401. {
  8402. HiddenFilesPanel->ViewPriority = 0;
  8403. // not really necessary, just to cleanup no-longer-valid data
  8404. HiddenFilesPanel->Caption = L"";
  8405. }
  8406. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8407. if (FileInfo.FilteredCount > 0)
  8408. {
  8409. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8410. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8411. }
  8412. else
  8413. {
  8414. FilteredFilesPanel->ViewPriority = 0;
  8415. // not really necessary, just to cleanup no-longer-valid data
  8416. FilteredFilesPanel->Caption = L"";
  8417. }
  8418. }
  8419. //---------------------------------------------------------------------------
  8420. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8421. TObject * /*Sender*/)
  8422. {
  8423. if (DirView(osOther)->Enabled)
  8424. {
  8425. DirView(osOther)->SetFocus();
  8426. }
  8427. }
  8428. //---------------------------------------------------------------------------
  8429. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8430. {
  8431. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8432. switch (Config.Show)
  8433. {
  8434. case qvShow:
  8435. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8436. {
  8437. Config.Show = qvHide;
  8438. }
  8439. else
  8440. {
  8441. Config.Show = Config.LastHideShow;
  8442. }
  8443. break;
  8444. case qvHideWhenEmpty:
  8445. if (ComponentVisible[fcQueueView])
  8446. {
  8447. Config.Show = qvHide;
  8448. }
  8449. else
  8450. {
  8451. Config.LastHideShow = Config.Show;
  8452. Config.Show = qvShow;
  8453. }
  8454. break;
  8455. case qvHide:
  8456. Config.LastHideShow = Config.Show;
  8457. Config.Show = qvShow;
  8458. break;
  8459. }
  8460. WinConfiguration->QueueView = Config;
  8461. }
  8462. //---------------------------------------------------------------------------
  8463. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8464. {
  8465. UnicodeString Result;
  8466. if (Terminal != NULL)
  8467. {
  8468. switch (WinConfiguration->PathInCaption)
  8469. {
  8470. case picShort:
  8471. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8472. break;
  8473. case picFull:
  8474. Result = Terminal->CurrentDirectory;
  8475. break;
  8476. }
  8477. }
  8478. return Result;
  8479. }
  8480. //---------------------------------------------------------------------------
  8481. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8482. {
  8483. TColor Result = GetWindowColor(FSessionColor);
  8484. return Result;
  8485. }
  8486. //---------------------------------------------------------------------------
  8487. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8488. {
  8489. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8490. return Result;
  8491. }
  8492. //---------------------------------------------------------------------------
  8493. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8494. {
  8495. if (!FSessionChanging)
  8496. {
  8497. TTerminalManager::Instance()->UpdateAppTitle();
  8498. }
  8499. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8500. // background to gray on Windows 7 (works on Windows 10).
  8501. // See also EnableControl
  8502. if (Showing)
  8503. {
  8504. if (ManagedSession != NULL)
  8505. {
  8506. // Update path when it changes
  8507. if ((SessionsPageControl->ActivePage != NULL) &&
  8508. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8509. {
  8510. UpdateSessionTab(SessionsPageControl->ActivePage);
  8511. }
  8512. }
  8513. NonVisualDataModule->ReconnectSessionAction->Update();
  8514. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8515. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8516. CenterReconnectToolbar();
  8517. bool HasTerminal = HasActiveTerminal();
  8518. RemoteDirView->Enabled = HasTerminal;
  8519. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8520. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8521. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8522. RemoteDriveView->Color = RemoteDirView->Color;
  8523. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8524. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8525. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8526. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8527. QueueFileList->Enabled = QueueView3->Enabled;
  8528. QueueFileList->Color = QueueView3->Color;
  8529. QueueFileList->Font->Color = QueueView3->Font->Color;
  8530. QueueLabelUpdateStatus();
  8531. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8532. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8533. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8534. {
  8535. UpdateDarkMode();
  8536. }
  8537. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8538. MessageDock->Visible = (MessageDock->ToolbarCount > 0);
  8539. }
  8540. }
  8541. //---------------------------------------------------------------------------
  8542. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8543. {
  8544. UpdateControls();
  8545. }
  8546. //---------------------------------------------------------------------------
  8547. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8548. TObject * Sender)
  8549. {
  8550. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8551. DebugAssert(DirView != NULL);
  8552. DoDirViewLoaded(DirView);
  8553. TransferPresetAutoSelect();
  8554. }
  8555. //---------------------------------------------------------------------------
  8556. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8557. {
  8558. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8559. // first run after installation
  8560. if (double(Updates.LastCheck) == 0)
  8561. {
  8562. // make sure next time there will be an update (if enabled)
  8563. Updates.LastCheck = TDateTime(1);
  8564. WinConfiguration->Updates = Updates;
  8565. }
  8566. else if ((double(Updates.Period) > 0) &&
  8567. (Now() - Updates.LastCheck >= Updates.Period))
  8568. {
  8569. TThreadMethod OnUpdatesChecked = NULL;
  8570. if (!IsUWP())
  8571. {
  8572. OnUpdatesChecked = UpdatesChecked;
  8573. }
  8574. StartUpdateThread(OnUpdatesChecked);
  8575. }
  8576. }
  8577. //---------------------------------------------------------------------------
  8578. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8579. {
  8580. UnicodeString Message;
  8581. bool New;
  8582. TQueryType Type;
  8583. GetUpdatesMessage(Message, New, Type, false);
  8584. if (!Message.IsEmpty())
  8585. {
  8586. if (New)
  8587. {
  8588. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8589. }
  8590. if (!New && (Type != qtWarning))
  8591. {
  8592. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8593. }
  8594. else
  8595. {
  8596. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8597. }
  8598. if (New)
  8599. {
  8600. Configuration->Usage->Inc(L"UpdateNotifications");
  8601. }
  8602. }
  8603. }
  8604. //---------------------------------------------------------------------------
  8605. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8606. {
  8607. RestoreApp();
  8608. bool CanDisplay = !NonVisualDataModule->Busy;
  8609. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8610. {
  8611. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8612. }
  8613. if (CanDisplay)
  8614. {
  8615. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8616. CheckForUpdates(true);
  8617. }
  8618. else
  8619. {
  8620. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8621. }
  8622. }
  8623. //---------------------------------------------------------------------------
  8624. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8625. TCopyParamRuleData & Data)
  8626. {
  8627. DebugAssert(Terminal != NULL);
  8628. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8629. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8630. Data.RemoteDirectory = RemoteDirView->PathName;
  8631. }
  8632. //---------------------------------------------------------------------------
  8633. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8634. {
  8635. // Terminal can be null when we are changing local directory implicitly,
  8636. // because it has been deleted, while no session is connected
  8637. // (Login dialog is open)
  8638. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8639. {
  8640. DebugAssert(!IsLocalBrowserMode());
  8641. TCopyParamRuleData Data;
  8642. GetTransferPresetAutoSelectData(Data);
  8643. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8644. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8645. if (CopyParamIndex < 0)
  8646. {
  8647. // there is no preset that matches autoselection
  8648. // set preset that we consider "default"
  8649. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8650. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8651. }
  8652. else
  8653. {
  8654. // there is preset matching autoselection
  8655. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8656. if (CopyParamName == FCopyParamAutoSelected)
  8657. {
  8658. // autoselected the same preset as the last time
  8659. // make no change (i.e. preserve custom user preset, if any)
  8660. }
  8661. else
  8662. {
  8663. // autoselected the different preset then the last time (or there
  8664. // was default preset set)
  8665. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8666. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8667. }
  8668. }
  8669. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8670. {
  8671. if (CopyParamIndex >= 0)
  8672. {
  8673. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8674. }
  8675. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8676. try
  8677. {
  8678. int Fmt =
  8679. (CopyParamIndex < 0) ?
  8680. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8681. COPY_PARAM_AUTOSELECTED;
  8682. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8683. Data->Message = MainInstructions(Message);
  8684. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8685. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8686. CopyParamAttrs);
  8687. if (CopyParamIndex >= 0)
  8688. {
  8689. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8690. Info = FORMAT(L"%s\n \n%s", (Info,
  8691. FMTLOAD(COPY_PARAM_RULE,
  8692. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8693. }
  8694. Data->Message += L"\n\n" + Info;
  8695. if (WinConfiguration->CopyParamAutoSelectNotice)
  8696. {
  8697. TransferPresetNoteMessage(Data, true);
  8698. }
  8699. else
  8700. {
  8701. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8702. Data = NULL; // ownership passed
  8703. }
  8704. }
  8705. __finally
  8706. {
  8707. delete Data;
  8708. }
  8709. }
  8710. }
  8711. }
  8712. //---------------------------------------------------------------------------
  8713. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8714. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8715. {
  8716. DebugAssert(NoteData != NULL);
  8717. DebugAssert(!IsLocalBrowserMode());
  8718. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8719. TQueryButtonAlias Aliases[1];
  8720. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8721. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8722. Params.Aliases = Aliases;
  8723. Params.AliasesCount = LENOF(Aliases);
  8724. unsigned int Result =
  8725. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8726. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8727. switch (Result)
  8728. {
  8729. case qaNeverAskAgain:
  8730. DebugAssert(AllowNeverAskAgain);
  8731. WinConfiguration->CopyParamAutoSelectNotice = false;
  8732. break;
  8733. case qaIgnore:
  8734. PreferencesDialog(pmPresets);
  8735. break;
  8736. }
  8737. }
  8738. //---------------------------------------------------------------------------
  8739. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8740. {
  8741. // as of now useless, as this is used for notes only, never for balloons, ...
  8742. RestoreApp();
  8743. // .. and we should never be busy here
  8744. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8745. {
  8746. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8747. }
  8748. }
  8749. //---------------------------------------------------------------------------
  8750. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8751. TPreferencesMode APreferencesMode)
  8752. {
  8753. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8754. TCopyParamRuleData Data;
  8755. if (Terminal != NULL)
  8756. {
  8757. PreferencesData.reset(new TPreferencesDialogData());
  8758. GetTransferPresetAutoSelectData(Data);
  8759. PreferencesData->CopyParamRuleData = &Data;
  8760. }
  8761. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8762. }
  8763. //---------------------------------------------------------------------------
  8764. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8765. const TCustomCommandType & Command)
  8766. {
  8767. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8768. {
  8769. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8770. }
  8771. }
  8772. //---------------------------------------------------------------------------
  8773. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8774. {
  8775. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8776. TCustomCommandType Command;
  8777. // make sure we use local custom command when remote are not supported
  8778. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8779. {
  8780. Command = FLastCustomCommand;
  8781. }
  8782. else
  8783. {
  8784. Command.Params = Command.Params | ccLocal;
  8785. }
  8786. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8787. FEditingFocusedAdHocCommand = OnFocused;
  8788. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8789. int Options =
  8790. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8791. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8792. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8793. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8794. {
  8795. FLastCustomCommand = Command;
  8796. UpdateCustomCommandsToolbar();
  8797. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8798. }
  8799. }
  8800. //---------------------------------------------------------------------------
  8801. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8802. {
  8803. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8804. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8805. DebugAssert(State > 0);
  8806. if (State <= 0)
  8807. {
  8808. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8809. }
  8810. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8811. }
  8812. //---------------------------------------------------------------------------
  8813. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8814. TCustomCommandType & Command, int & State)
  8815. {
  8816. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8817. if (Result)
  8818. {
  8819. Command = FLastCustomCommand;
  8820. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8821. }
  8822. return Result;
  8823. }
  8824. //---------------------------------------------------------------------------
  8825. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8826. {
  8827. ResetIncrementalSearch();
  8828. }
  8829. //---------------------------------------------------------------------------
  8830. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8831. {
  8832. DebugAssert(ComponentVisible[Component]);
  8833. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8834. }
  8835. //---------------------------------------------------------------------------
  8836. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8837. {
  8838. // when queue panel is resized here directly, the status bar is stretched
  8839. // over whole space the panel occupied
  8840. PostComponentHide(fcQueueView);
  8841. }
  8842. //---------------------------------------------------------------------------
  8843. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8844. {
  8845. ComponentVisible[fcQueueFileList] = false;
  8846. }
  8847. //---------------------------------------------------------------------------
  8848. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8849. {
  8850. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8851. RefreshColorMode();
  8852. ResetSysDarkTheme();
  8853. ConfigurationChanged();
  8854. ConfigureInterface();
  8855. // Should be called for all controls
  8856. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8857. }
  8858. //---------------------------------------------------------------------------
  8859. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8860. {
  8861. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8862. if (!FInvalid &&
  8863. (Message.LParam != 0) &&
  8864. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8865. {
  8866. ThemeChanged();
  8867. }
  8868. TForm::Dispatch(&Message);
  8869. }
  8870. //---------------------------------------------------------------------------
  8871. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8872. {
  8873. TMessage * M = static_cast<TMessage*>(Message);
  8874. switch (M->Msg)
  8875. {
  8876. case CM_DIALOGCHAR:
  8877. CMDialogChar(*M);
  8878. break;
  8879. case WM_APPCOMMAND:
  8880. WMAppCommand(*M);
  8881. break;
  8882. case WM_SYSCOMMAND:
  8883. WMSysCommand(*M);
  8884. break;
  8885. case WM_QUERYENDSESSION:
  8886. WMQueryEndSession(*M);
  8887. break;
  8888. case WM_ENDSESSION:
  8889. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8890. break;
  8891. #ifdef _DEBUG
  8892. case WM_WINDOWPOSCHANGED:
  8893. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8894. break;
  8895. case CM_CUSTOMSTYLECHANGED:
  8896. TForm::Dispatch(Message);
  8897. break;
  8898. #endif
  8899. case WM_COMPONENT_HIDE:
  8900. {
  8901. Byte Component = static_cast<Byte>(M->WParam);
  8902. // sanity check
  8903. if (ComponentVisible[Component])
  8904. {
  8905. // special treatment
  8906. if (Component == fcQueueView)
  8907. {
  8908. ToggleQueueVisibility();
  8909. DebugAssert(!ComponentVisible[fcQueueView]);
  8910. }
  8911. else
  8912. {
  8913. ComponentVisible[Component] = false;
  8914. }
  8915. }
  8916. }
  8917. break;
  8918. case WM_COPYDATA:
  8919. WMCopyData(*M);
  8920. break;
  8921. case WM_MANAGES_CAPTION:
  8922. // caption managed in TTerminalManager::UpdateAppTitle()
  8923. M->Result = 1;
  8924. break;
  8925. case WM_WANTS_MOUSEWHEEL:
  8926. M->Result = 1;
  8927. break;
  8928. case CM_SHOWINGCHANGED:
  8929. CMShowingChanged(*M);
  8930. break;
  8931. case WM_CLOSE:
  8932. WMClose(*M);
  8933. break;
  8934. case WM_DPICHANGED:
  8935. WMDpiChanged(*M);
  8936. break;
  8937. case CM_DPICHANGED:
  8938. CMDpiChanged(*M);
  8939. break;
  8940. case WM_SETTINGCHANGE:
  8941. WMSettingChange(*M);
  8942. break;
  8943. case CM_DIALOGKEY:
  8944. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8945. break;
  8946. default:
  8947. TForm::Dispatch(Message);
  8948. break;
  8949. }
  8950. }
  8951. //---------------------------------------------------------------------------
  8952. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8953. {
  8954. // noop
  8955. }
  8956. //---------------------------------------------------------------------------
  8957. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8958. {
  8959. TForm::Dispatch(&Message);
  8960. FSessionColors->Clear();
  8961. AddFixedSessionImages();
  8962. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8963. UpdateImages();
  8964. }
  8965. //---------------------------------------------------------------------------
  8966. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8967. {
  8968. TForm::Dispatch(&Message);
  8969. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8970. UpdatePixelsPerInchMainWindowCounter();
  8971. }
  8972. //---------------------------------------------------------------------------
  8973. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8974. {
  8975. // Cannot close window while we are busy.
  8976. // We cannot test this in FormCloseQuery as that is called also from
  8977. // Close(), which is called by CloseApplicationAction. So we can be busy
  8978. // there already even, when it is legitimate to close the application.
  8979. // Possibly a better place to handle this would be WMSysCommand.
  8980. if (NonVisualDataModule->Busy)
  8981. {
  8982. Message.Result = 1;
  8983. }
  8984. else
  8985. {
  8986. TForm::Dispatch(&Message);
  8987. }
  8988. }
  8989. //---------------------------------------------------------------------------
  8990. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8991. {
  8992. if (!FShowingChanged)
  8993. {
  8994. AddStartupSequence(L"D");
  8995. FShowingChanged = true;
  8996. }
  8997. // Ignoring implicit local-local browser of Commander, opened from DoShow
  8998. bool NoSession = (ManagedSession == NULL);
  8999. // This calls TCustomScpExplorerForm::DoShow
  9000. TForm::Dispatch(&Message);
  9001. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  9002. if (Showing && !FStarted)
  9003. {
  9004. FStarted = true;
  9005. InterfaceStarted();
  9006. }
  9007. if (Showing && NoSession)
  9008. {
  9009. // When we are starting minimized (i.e. from an installer),
  9010. // postpone showing Login dialog until we get restored.
  9011. // Otherwise the Login dialog (and Authentication window) show restored
  9012. // over invisible (minimized) main window.
  9013. if (WindowState == wsMinimized)
  9014. {
  9015. FNeedSession = true;
  9016. }
  9017. else
  9018. {
  9019. // This happens before application ever goes idle, so the toolbars would
  9020. // stay enabled (initial state) until the Login dialog is dismissed.
  9021. UpdateActions();
  9022. NonVisualDataModule->StartBusy();
  9023. try
  9024. {
  9025. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  9026. // otherwise the dialog does not receive focus.
  9027. // With Commander interface the ProcessMessages is called already
  9028. // by TDriveView, but with Explorer interface, we need to call it explicily
  9029. Application->ProcessMessages();
  9030. // do not reload sessions, they have been loaded just now (optimization)
  9031. NeedSession(true);
  9032. }
  9033. __finally
  9034. {
  9035. NonVisualDataModule->EndBusy();
  9036. }
  9037. }
  9038. }
  9039. }
  9040. //---------------------------------------------------------------------------
  9041. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  9042. {
  9043. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  9044. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  9045. }
  9046. //---------------------------------------------------------------------------
  9047. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  9048. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  9049. int & MaxHeight)
  9050. {
  9051. // workaround for bug in TWinControl.CalcConstraints
  9052. // Check for empty rect (restore from iconic state) is done there only after
  9053. // call to AdjustClientRect, which enlarges the rect (for forms).
  9054. TRect R = GetClientRect();
  9055. // when restoring from iconic state, place no restrictions
  9056. if (IsRectEmpty(R))
  9057. {
  9058. MinWidth = 0;
  9059. MinHeight = 0;
  9060. MaxWidth = 32000;
  9061. MaxHeight = 32000;
  9062. }
  9063. }
  9064. //---------------------------------------------------------------------------
  9065. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  9066. TSpaceAvailable & ASpaceAvailable, bool & Close)
  9067. {
  9068. // terminal can be already closed (e.g. dropped connection)
  9069. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  9070. {
  9071. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  9072. try
  9073. {
  9074. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  9075. }
  9076. catch(...)
  9077. {
  9078. if (!Terminal->Active)
  9079. {
  9080. Close = true;
  9081. }
  9082. throw;
  9083. }
  9084. }
  9085. }
  9086. //---------------------------------------------------------------------------
  9087. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  9088. {
  9089. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  9090. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  9091. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  9092. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  9093. {
  9094. OnGetSpaceAvailable = GetSpaceAvailable;
  9095. }
  9096. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  9097. OnGetSpaceAvailable);
  9098. }
  9099. //---------------------------------------------------------------------------
  9100. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  9101. {
  9102. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  9103. Terminal->FillSessionDataForCode(Data.get());
  9104. return Data.release();
  9105. }
  9106. //---------------------------------------------------------------------------
  9107. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  9108. {
  9109. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  9110. DoGenerateUrlDialog(Data.get(), Paths);
  9111. }
  9112. //---------------------------------------------------------------------------
  9113. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  9114. {
  9115. GenerateUrl(NULL);
  9116. }
  9117. //---------------------------------------------------------------------------
  9118. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  9119. {
  9120. std::unique_ptr<TStrings> Paths(new TStringList());
  9121. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  9122. GenerateUrl(Paths.get());
  9123. }
  9124. //---------------------------------------------------------------------------
  9125. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  9126. {
  9127. FSessionColor = value;
  9128. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  9129. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  9130. static_cast<TObject *>(GetComponent(fcColorMenu)));
  9131. DebugAssert(ColorItem != NULL);
  9132. ColorItem->Color = C;
  9133. NonVisualDataModule->ColorMenuItem->Color = C;
  9134. UpdateControls();
  9135. }
  9136. //---------------------------------------------------------------------------
  9137. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  9138. {
  9139. if (value != FSessionColor)
  9140. {
  9141. UpdateSessionColor(value);
  9142. // Is null when called from LastTerminalClosed
  9143. if (Terminal != NULL)
  9144. {
  9145. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  9146. }
  9147. }
  9148. }
  9149. //---------------------------------------------------------------------------
  9150. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  9151. {
  9152. bool Result = FNoteTimer->Enabled;
  9153. if (Result)
  9154. {
  9155. // cannot cancel note too early
  9156. bool NotEarly =
  9157. (Now() - FNoteShown >
  9158. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  9159. if (Force || NotEarly)
  9160. {
  9161. FNoteTimer->Enabled = false;
  9162. FNote = L"";
  9163. SAFE_DESTROY(FNoteData);
  9164. FOnNoteClick = NULL;
  9165. FNoteHints = FNotes->Text;
  9166. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  9167. UpdateStatusBar();
  9168. }
  9169. }
  9170. return Result;
  9171. }
  9172. //---------------------------------------------------------------------------
  9173. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  9174. {
  9175. DebugAssert(FNoteTimer->Enabled);
  9176. CancelNote(true);
  9177. }
  9178. //---------------------------------------------------------------------------
  9179. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9180. {
  9181. FNoteHints = FNotes->Text.TrimRight();
  9182. }
  9183. //---------------------------------------------------------------------------
  9184. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9185. {
  9186. int P = Note.Pos(L"\n");
  9187. if (P > 0)
  9188. {
  9189. Note.SetLength(P - 1);
  9190. }
  9191. FNotes->Add(FORMAT(L"[%s] %s",
  9192. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9193. while (FNotes->Count > 10)
  9194. {
  9195. FNotes->Delete(0);
  9196. }
  9197. if (UpdateNow)
  9198. {
  9199. UpdateNoteHints();
  9200. UpdateStatusBar();
  9201. }
  9202. }
  9203. //---------------------------------------------------------------------------
  9204. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  9205. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9206. {
  9207. int P = Note.Pos(L"\n");
  9208. if (P > 0)
  9209. {
  9210. Note.SetLength(P - 1);
  9211. }
  9212. UpdateNoteHints();
  9213. FNote = Note;
  9214. SAFE_DESTROY(FNoteData);
  9215. FNoteData = NoteData;
  9216. FOnNoteClick = OnNoteClick;
  9217. AddNote(Note, false);
  9218. UpdateStatusBar();
  9219. FNoteShown = Now();
  9220. FNoteTimer->Enabled = false;
  9221. if (Seconds == 0)
  9222. {
  9223. Seconds = WinConfiguration->NotificationsTimeout;
  9224. }
  9225. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9226. FNoteTimer->Enabled = true;
  9227. }
  9228. //---------------------------------------------------------------------------
  9229. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9230. {
  9231. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9232. }
  9233. //---------------------------------------------------------------------------
  9234. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9235. {
  9236. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9237. {
  9238. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9239. }
  9240. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9241. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9242. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9243. 0, NULL, NULL);
  9244. }
  9245. //---------------------------------------------------------------------------
  9246. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9247. {
  9248. bool NewShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9249. UnicodeString Note =
  9250. FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1), (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, (NewShowHiddenFiles ? 2 : 3))));
  9251. PostNote(Note, 0, NULL, NULL);
  9252. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9253. WinConfiguration->ShowHiddenFiles = NewShowHiddenFiles;
  9254. }
  9255. //---------------------------------------------------------------------------
  9256. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9257. {
  9258. WinConfiguration->FormatSizeBytes = Style;
  9259. }
  9260. //---------------------------------------------------------------------------
  9261. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9262. {
  9263. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9264. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9265. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9266. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9267. }
  9268. //---------------------------------------------------------------------------
  9269. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9270. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9271. {
  9272. if (Panel->Index == 0)
  9273. {
  9274. if (FOnNoteClick != NULL)
  9275. {
  9276. // prevent the user data from being freed by possible call
  9277. // to CancelNote or PostNote during call to OnNoteClick
  9278. std::unique_ptr<TObject> NoteData(FNoteData);
  9279. TNotifyEvent OnNoteClick = FOnNoteClick;
  9280. FNoteData = NULL;
  9281. // need to cancel the note as we are going to delete its user data
  9282. CancelNote(true);
  9283. OnNoteClick(NoteData.get());
  9284. }
  9285. }
  9286. if (HasActiveTerminal() &&
  9287. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9288. {
  9289. FileSystemInfo();
  9290. }
  9291. }
  9292. //---------------------------------------------------------------------------
  9293. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9294. {
  9295. // workaround:
  9296. // 1) for unknown reason, disabling window, while minimized,
  9297. // prevents it from restoring, even if it was enabled again meanwhile
  9298. // 2) when disabling the main window, while another has focus
  9299. // minimize is no longer possible
  9300. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9301. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9302. // Shouldn't we use IsApplicationMinimized() here?
  9303. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9304. {
  9305. Enabled = false;
  9306. }
  9307. FLockLevel++;
  9308. }
  9309. //---------------------------------------------------------------------------
  9310. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9311. {
  9312. DebugAssert(FLockLevel > 0);
  9313. FLockLevel--;
  9314. if (FLockLevel == 0)
  9315. {
  9316. DebugAssert(FLockSuspendLevel == 0);
  9317. Enabled = true;
  9318. }
  9319. }
  9320. //---------------------------------------------------------------------------
  9321. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9322. {
  9323. // We need to make sure that window is enabled when the last modal window closes
  9324. // otherwise focus is not restored correctly.
  9325. // So we re-enable the window when modal window opens and
  9326. // disable it back after is closes
  9327. if (FLockLevel > 0)
  9328. {
  9329. // while we have nesting counter, we know that we never be called
  9330. // recursivelly as Application->OnModalBegin is called only
  9331. // for the top-level modal window
  9332. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9333. {
  9334. // won't be disabled when conditions in LockWindow() were not satisfied
  9335. FDisabledOnLockSuspend = !Enabled;
  9336. // When minimized to tray (or actually when set to SW_HIDE),
  9337. // setting Enabled makes the window focusable even when there's
  9338. // modal window over it
  9339. if (!FTrayIcon->Visible)
  9340. {
  9341. Enabled = true;
  9342. }
  9343. }
  9344. FLockSuspendLevel++;
  9345. }
  9346. }
  9347. //---------------------------------------------------------------------------
  9348. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9349. {
  9350. if (FLockSuspendLevel > 0)
  9351. {
  9352. DebugAssert(FLockLevel > 0);
  9353. FLockSuspendLevel--;
  9354. // see comment in SuspendWindowLock
  9355. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9356. {
  9357. // Note that window can be enabled here, when we were minized to tray when
  9358. // was SuspendWindowLock() called.
  9359. // We should possibly do the same check as in LockWindow(),
  9360. // if it is ever possible that the consitions change between
  9361. // SuspendWindowLock() and ResumeWindowLock()
  9362. Enabled = !FDisabledOnLockSuspend;
  9363. }
  9364. }
  9365. }
  9366. //---------------------------------------------------------------------------
  9367. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9368. {
  9369. TTBXComboBoxItem * RemotePathComboBox =
  9370. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9371. if (!TextOnly)
  9372. {
  9373. TStrings * Items = RemotePathComboBox->Strings;
  9374. Items->BeginUpdate();
  9375. try
  9376. {
  9377. Items->Clear();
  9378. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9379. {
  9380. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9381. while (!IsUnixRootPath(APath))
  9382. {
  9383. int P = APath.LastDelimiter(L'/');
  9384. DebugAssert(P >= 0);
  9385. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9386. APath.SetLength(P - 1);
  9387. }
  9388. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9389. }
  9390. }
  9391. __finally
  9392. {
  9393. Items->EndUpdate();
  9394. }
  9395. }
  9396. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9397. // Setting ItemIndex to -1 does not reset its text
  9398. if (RemotePathComboBox->Strings->Count == 0)
  9399. {
  9400. RemotePathComboBox->Text = L"";
  9401. }
  9402. }
  9403. //---------------------------------------------------------------------------
  9404. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9405. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9406. {
  9407. if (AIndex < 0)
  9408. {
  9409. AIndex = Sender->ItemIndex;
  9410. }
  9411. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9412. }
  9413. //---------------------------------------------------------------------------
  9414. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9415. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9416. int & ImageIndex)
  9417. {
  9418. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9419. }
  9420. //---------------------------------------------------------------------------
  9421. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9422. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9423. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9424. {
  9425. if (!IsLocalBrowserMode())
  9426. {
  9427. ARect.Left += (10 * AIndex);
  9428. }
  9429. }
  9430. //---------------------------------------------------------------------------
  9431. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9432. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9433. {
  9434. AWidth += (10 * AIndex);
  9435. }
  9436. //---------------------------------------------------------------------------
  9437. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9438. {
  9439. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9440. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9441. int Index = RemotePathComboBox->ItemIndex;
  9442. while (Index < RemotePathComboBox->Strings->Count - 1)
  9443. {
  9444. APath = UnixExtractFileDir(APath);
  9445. Index++;
  9446. }
  9447. // VanDyke style paths
  9448. if (APath.IsEmpty())
  9449. {
  9450. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9451. APath = ROOTDIRECTORY;
  9452. }
  9453. if (RemoteDirView->Path != APath)
  9454. {
  9455. RemoteDirView->Path = APath;
  9456. }
  9457. }
  9458. //---------------------------------------------------------------------------
  9459. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9460. {
  9461. DoRemotePathComboBoxItemClick(Sender);
  9462. }
  9463. //---------------------------------------------------------------------------
  9464. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9465. {
  9466. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9467. DebugUsedParam(Sender);
  9468. UpdateRemotePathComboBox(true);
  9469. }
  9470. //---------------------------------------------------------------------------
  9471. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9472. {
  9473. DoRemotePathComboBoxCancel(Sender);
  9474. }
  9475. //---------------------------------------------------------------------------
  9476. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9477. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9478. {
  9479. ResetIncrementalSearch();
  9480. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9481. DebugAssert(DirView != NULL);
  9482. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9483. {
  9484. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9485. // OnEditing is called also from TCustomListView::CanEdit
  9486. if (Edit != NULL)
  9487. {
  9488. EditSelectBaseName(Edit);
  9489. }
  9490. }
  9491. }
  9492. //---------------------------------------------------------------------------
  9493. //---------------------------------------------------------------------------
  9494. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9495. {
  9496. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9497. Result->AutoDetectDnD = false;
  9498. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9499. Result->RenderDataOn = rdoEnterAndDropSync;
  9500. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9501. return Result;
  9502. }
  9503. //---------------------------------------------------------------------------
  9504. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9505. {
  9506. if (IsWin10Build(2004))
  9507. {
  9508. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9509. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9510. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9511. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9512. }
  9513. }
  9514. //---------------------------------------------------------------------------
  9515. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9516. {
  9517. TForm::CreateWnd();
  9518. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9519. UpdateDarkMode();
  9520. if (FSessionsDragDropFilesEx == NULL)
  9521. {
  9522. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9523. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9524. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9525. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9526. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9527. }
  9528. if (FQueueDragDropFilesEx == NULL)
  9529. {
  9530. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9531. // No need to set OnDragOver as we do not have any restrictions
  9532. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9533. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9534. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9535. }
  9536. }
  9537. //---------------------------------------------------------------------------
  9538. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9539. {
  9540. TForm::DestroyWnd();
  9541. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9542. FQueueDragDropFilesEx->DragDropControl = NULL;
  9543. }
  9544. //---------------------------------------------------------------------------
  9545. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9546. {
  9547. SideEnter(FCurrentSide);
  9548. FEverShown = true;
  9549. }
  9550. //---------------------------------------------------------------------------
  9551. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9552. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9553. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9554. {
  9555. if (!NonVisualDataModule->Busy)
  9556. {
  9557. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9558. Configuration->Usage->Inc(L"FileFinds");
  9559. LockWindow(true);
  9560. NonVisualDataModule->StartBusy();
  9561. WinConfiguration->LockedInterface = true;
  9562. try
  9563. {
  9564. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9565. }
  9566. __finally
  9567. {
  9568. WinConfiguration->LockedInterface = false;
  9569. NonVisualDataModule->EndBusy();
  9570. UnlockWindow();
  9571. }
  9572. }
  9573. }
  9574. //---------------------------------------------------------------------------
  9575. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9576. {
  9577. if (!NonVisualDataModule->Busy)
  9578. {
  9579. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9580. SetFocus();
  9581. RemoteDirView->Path = UnixExtractFilePath(Path);
  9582. UnicodeString FileName = UnixExtractFileName(Path);
  9583. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9584. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9585. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9586. if (Item == NULL)
  9587. {
  9588. ReloadDirectory(osRemote);
  9589. Item = RemoteDirView->FindFileItem(FileName);
  9590. }
  9591. if (Item != NULL)
  9592. {
  9593. RemoteDirView->ItemFocused = Item;
  9594. RemoteDirView->ItemFocused->MakeVisible(false);
  9595. RemoteDirView->SetFocus();
  9596. }
  9597. }
  9598. }
  9599. //---------------------------------------------------------------------------
  9600. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9601. {
  9602. bool Result = !NonVisualDataModule->Busy;
  9603. if (Result)
  9604. {
  9605. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9606. }
  9607. return Result;
  9608. }
  9609. //---------------------------------------------------------------------------
  9610. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9611. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9612. {
  9613. if (CanOperateOnFoundFiles(ATerminal))
  9614. {
  9615. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9616. FOnFileOperationFinished = OnFileOperationFinished;
  9617. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9618. }
  9619. }
  9620. //---------------------------------------------------------------------------
  9621. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9622. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9623. {
  9624. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9625. }
  9626. //---------------------------------------------------------------------------
  9627. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9628. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9629. {
  9630. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9631. }
  9632. //---------------------------------------------------------------------------
  9633. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9634. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9635. {
  9636. if (CanOperateOnFoundFiles(ATerminal))
  9637. {
  9638. for (int Index = 0; Index < FileList->Count; Index++)
  9639. {
  9640. UnicodeString FileName = FileList->Strings[Index];
  9641. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9642. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9643. OnFileOperationFinished(osRemote, FileName, true);
  9644. }
  9645. }
  9646. }
  9647. //---------------------------------------------------------------------------
  9648. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9649. {
  9650. FFileFindTerminal = Terminal;
  9651. ShowFileFindDialog(
  9652. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9653. DoDownloadFoundFiles, DoEditFoundFiles);
  9654. }
  9655. //---------------------------------------------------------------------------
  9656. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9657. {
  9658. FTaskbarList = TaskbarList;
  9659. }
  9660. //---------------------------------------------------------------------------
  9661. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9662. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9663. {
  9664. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9665. if (Index >= 0)
  9666. {
  9667. if (Button == mbLeft)
  9668. {
  9669. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9670. // If switching tab (switching session) takes long enough for user
  9671. // to actually release the button, "mouse down" is still raised,
  9672. // but we do not get "mouse up" event, so dragging is not cancelled,
  9673. // prevent that by not beginning dragging in the first place.
  9674. if (IsKeyPressed(VK_LBUTTON))
  9675. {
  9676. // when user clicks the "+", we get mouse down only after the session
  9677. // is closed, when new session tab is already on X:Y, so dragging
  9678. // starts, prevent that
  9679. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9680. {
  9681. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9682. if (Terminal != NULL)
  9683. {
  9684. SessionsPageControl->BeginDrag(false);
  9685. }
  9686. }
  9687. }
  9688. }
  9689. else if (Button == mbMiddle)
  9690. {
  9691. // ignore middle-click for "New session tab" and the last local-local tab
  9692. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9693. if ((Session != NULL) && CanCloseSession(Session))
  9694. {
  9695. CloseSessionTab(Index);
  9696. }
  9697. }
  9698. }
  9699. }
  9700. //---------------------------------------------------------------------------
  9701. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9702. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9703. {
  9704. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9705. // do not allow dropping on the "+" tab
  9706. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9707. if ((TargetTerminal != NULL) &&
  9708. (SessionsPageControl->ActivePage->PageIndex != Index))
  9709. {
  9710. Configuration->Usage->Inc(L"SessionTabMoves");
  9711. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9712. // but we want to actually prevent that to avoid flicker
  9713. SessionsPageControl->ActivePage->PageIndex = Index;
  9714. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9715. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9716. }
  9717. }
  9718. //---------------------------------------------------------------------------
  9719. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9720. TObject * Sender, TObject * Source, int X, int Y,
  9721. TDragState /*State*/, bool & Accept)
  9722. {
  9723. Accept = (Sender == Source);
  9724. if (Accept)
  9725. {
  9726. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9727. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9728. // do not allow dragging to the "+" tab
  9729. Accept = (Terminal != NULL);
  9730. }
  9731. }
  9732. //---------------------------------------------------------------------------
  9733. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9734. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9735. {
  9736. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9737. if (Index < 0)
  9738. {
  9739. Effect = DROPEFFECT_NONE;
  9740. }
  9741. else
  9742. {
  9743. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9744. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9745. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9746. {
  9747. Effect = DROPEFFECT_NONE;
  9748. }
  9749. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9750. {
  9751. Effect = DROPEFFECT_COPY;
  9752. }
  9753. }
  9754. }
  9755. //---------------------------------------------------------------------------
  9756. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9757. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9758. {
  9759. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9760. // do not allow dropping on local-local, disconnected and the "+" tab
  9761. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9762. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9763. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9764. {
  9765. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9766. if (!IsFileControl(DropSourceControl, osRemote))
  9767. {
  9768. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9769. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9770. // Why don't we use Terminal->CurrentDirectory directly?
  9771. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9772. // do not force queue, drag drop
  9773. false, true);
  9774. }
  9775. }
  9776. }
  9777. //---------------------------------------------------------------------------
  9778. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9779. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9780. {
  9781. // Downloads are handled in RemoteFileControlDDEnd
  9782. if (!IsFileControl(DropSourceControl, osRemote))
  9783. {
  9784. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9785. Terminal->CurrentDirectory,
  9786. // force queue, drag drop
  9787. true, true);
  9788. }
  9789. }
  9790. //---------------------------------------------------------------------------
  9791. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9792. {
  9793. FShowing = false;
  9794. // Do not save empty workspace
  9795. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9796. {
  9797. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9798. UnicodeString Name = WorkspaceName();
  9799. DoSaveWorkspace(
  9800. Name, DataList.get(),
  9801. !Configuration->DisablePasswordStoring &&
  9802. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9803. WinConfiguration->LastStoredSession = Name;
  9804. }
  9805. }
  9806. //---------------------------------------------------------------------------
  9807. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9808. {
  9809. TManagedTerminal * ManagedTerminal =
  9810. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9811. if (ManagedTerminal != NULL)
  9812. {
  9813. ManagedTerminal->DirectoryLoaded = Now();
  9814. }
  9815. }
  9816. //---------------------------------------------------------------------------
  9817. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9818. TListItem * /*Item*/, bool /*Selected*/)
  9819. {
  9820. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9821. switch (DirView->LastSelectMethod)
  9822. {
  9823. case smKeyboard:
  9824. Configuration->Usage->Inc(L"KeyboardSelections");
  9825. break;
  9826. case smMouse:
  9827. Configuration->Usage->Inc(L"MouseSelections");
  9828. break;
  9829. }
  9830. }
  9831. //---------------------------------------------------------------------------
  9832. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9833. {
  9834. if (Color != 0)
  9835. {
  9836. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9837. }
  9838. else
  9839. {
  9840. return FSessionTabImageIndex;
  9841. }
  9842. }
  9843. //---------------------------------------------------------------------------
  9844. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9845. {
  9846. TPngImageCollectionItem * Item =
  9847. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9848. FSessionColors->AddPng(Item->PngImage);
  9849. return FSessionColors->Count - 1;
  9850. }
  9851. //---------------------------------------------------------------------------
  9852. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9853. {
  9854. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9855. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  9856. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  9857. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9858. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9859. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  9860. }
  9861. //---------------------------------------------------------------------------
  9862. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  9863. {
  9864. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  9865. }
  9866. //---------------------------------------------------------------------------
  9867. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  9868. {
  9869. switch (Side)
  9870. {
  9871. case osLocal:
  9872. return FNewLocalTabTabImageIndex;
  9873. case osRemote:
  9874. case osCurrent:
  9875. return FNewRemoteTabTabImageIndex;
  9876. default:
  9877. DebugFail();
  9878. return -1;
  9879. }
  9880. }
  9881. //---------------------------------------------------------------------------
  9882. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9883. {
  9884. for (int Index = 0; Index < Control->ControlCount; Index++)
  9885. {
  9886. TControl * AControl = Control->Controls[Index];
  9887. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9888. if (WinControl != NULL)
  9889. {
  9890. CollectItemsWithTextDisplayMode(WinControl);
  9891. }
  9892. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9893. if (Toolbar != NULL)
  9894. {
  9895. // we care for top-level items only
  9896. TTBCustomItem * Items = Toolbar->Items;
  9897. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9898. {
  9899. TTBCustomItem * Item = Items->Items[ItemIndex];
  9900. if (((Item->DisplayMode == nbdmImageAndText) ||
  9901. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9902. EligibleForImageDisplayMode(Item))
  9903. {
  9904. FItemsWithTextDisplayMode.insert(Item);
  9905. }
  9906. }
  9907. }
  9908. }
  9909. }
  9910. //---------------------------------------------------------------------------
  9911. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9912. {
  9913. return true;
  9914. }
  9915. //---------------------------------------------------------------------------
  9916. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9917. {
  9918. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9919. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9920. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9921. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9922. bool Result = true;
  9923. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9924. {
  9925. TTBCustomItem * Item = *i;
  9926. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9927. if (Label != NULL)
  9928. {
  9929. // optimization
  9930. if (Label->Visible == SelectiveToolbarText)
  9931. {
  9932. Result = false;
  9933. }
  9934. else
  9935. {
  9936. Label->Visible = SelectiveToolbarText;
  9937. }
  9938. }
  9939. else
  9940. {
  9941. // optimization
  9942. if (Item->DisplayMode == DisplayMode)
  9943. {
  9944. Result = false;
  9945. }
  9946. else
  9947. {
  9948. Item->DisplayMode = DisplayMode;
  9949. }
  9950. }
  9951. ++i;
  9952. }
  9953. UpdateNewTabTab();
  9954. return Result;
  9955. }
  9956. //---------------------------------------------------------------------------
  9957. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9958. {
  9959. DebugFail();
  9960. }
  9961. //---------------------------------------------------------------------------
  9962. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9963. {
  9964. // Among other, a lock level is non zero, while directory is being loaded,
  9965. // even when it happens as a result of dirview navigation,
  9966. // i.e. when TNonVisualDataModule is NOT FBusy.
  9967. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9968. // Among other this prevents a panel auto update to occur while
  9969. // directory is changing.
  9970. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9971. }
  9972. //---------------------------------------------------------------------------
  9973. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  9974. {
  9975. // While the window is disabled, we still seem to process menu shortcuts at least,
  9976. // so stop it at least here.
  9977. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9978. bool Result;
  9979. if (Allowed == aaUpdate)
  9980. {
  9981. Result = true;
  9982. }
  9983. else
  9984. {
  9985. Result = !NonVisualDataModule->Busy;
  9986. if (Result && (Allowed == aaShortCut))
  9987. {
  9988. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  9989. Action->Update();
  9990. Result = Action->Enabled;
  9991. }
  9992. }
  9993. return Result;
  9994. }
  9995. //---------------------------------------------------------------------------
  9996. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9997. {
  9998. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9999. }
  10000. //---------------------------------------------------------------------------
  10001. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  10002. {
  10003. // This is somewhat redundant to LockWindow() call from
  10004. // TTerminalManager::TerminalReadDirectoryProgress.
  10005. // But disabling window is known not to block keyboard shorcuts
  10006. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  10007. if (Busy > 0)
  10008. {
  10009. if (NonVisualDataModule->Busy)
  10010. {
  10011. State = false;
  10012. }
  10013. else
  10014. {
  10015. NonVisualDataModule->StartBusy();
  10016. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  10017. LockWindow();
  10018. }
  10019. }
  10020. else if (Busy < 0)
  10021. {
  10022. UnlockWindow();
  10023. NonVisualDataModule->EndBusy();
  10024. }
  10025. else
  10026. {
  10027. State = NonVisualDataModule->Busy;
  10028. }
  10029. }
  10030. //---------------------------------------------------------------------------
  10031. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  10032. {
  10033. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  10034. if (Index >= 0)
  10035. {
  10036. TPopupMenu * PopupMenu = NULL;
  10037. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10038. if (Session != NULL)
  10039. {
  10040. SessionsPageControl->ActivePageIndex = Index;
  10041. if (DebugAlwaysTrue(SessionTabSwitched()))
  10042. {
  10043. // copied from TControl.WMContextMenu
  10044. SendCancelMode(SessionsPageControl);
  10045. // explicit popup instead of using PopupMenu property
  10046. // to avoid menu to popup somewhere within SessionTabSwitched above,
  10047. // while connecting yet not-connected session and hence
  10048. // allowing an access to commands over not-completelly connected session
  10049. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  10050. }
  10051. }
  10052. else
  10053. {
  10054. if (SupportsLocalBrowser())
  10055. {
  10056. PopupMenu = NonVisualDataModule->NewTabPopup;
  10057. }
  10058. }
  10059. if (PopupMenu != NULL)
  10060. {
  10061. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  10062. PopupMenu->PopupComponent = SessionsPageControl;
  10063. PopupMenu->Popup(Point.x, Point.y);
  10064. }
  10065. }
  10066. Handled = true;
  10067. }
  10068. //---------------------------------------------------------------------------
  10069. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  10070. {
  10071. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  10072. }
  10073. //---------------------------------------------------------------------------
  10074. void __fastcall TCustomScpExplorerForm::ChangePassword()
  10075. {
  10076. Configuration->Usage->Inc(L"PasswordChanges");
  10077. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  10078. ChangePasswordData->DisableAuthentationsExceptPassword();
  10079. ChangePasswordData->ChangePassword = true;
  10080. std::unique_ptr<TTerminal> ChangePasswordSession(
  10081. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  10082. ChangePasswordSession->AutoReadDirectory = false;
  10083. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  10084. {
  10085. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  10086. }
  10087. else
  10088. {
  10089. Configuration->Usage->Inc(L"PasswordChangesFailed");
  10090. }
  10091. }
  10092. //---------------------------------------------------------------------------
  10093. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  10094. {
  10095. // No nice way to assert SSH2
  10096. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  10097. }
  10098. //---------------------------------------------------------------------------
  10099. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  10100. {
  10101. TOperationVisualizer OperationVisualizer;
  10102. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  10103. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  10104. }
  10105. //---------------------------------------------------------------------------
  10106. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  10107. {
  10108. TForm::ChangeScale(M, D);
  10109. int APixelsPerInch = GetControlPixelsPerInch(this);
  10110. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  10111. try
  10112. {
  10113. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  10114. }
  10115. __finally
  10116. {
  10117. GUIConfiguration->ChangeResourceModule(ResourceModule);
  10118. }
  10119. }
  10120. //---------------------------------------------------------------------------
  10121. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  10122. {
  10123. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  10124. }
  10125. //---------------------------------------------------------------------
  10126. class TPublicControl : public TControl
  10127. {
  10128. friend class TCustomScpExplorerForm;
  10129. };
  10130. //---------------------------------------------------------------------------
  10131. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  10132. {
  10133. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  10134. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  10135. }
  10136. //---------------------------------------------------------------------------
  10137. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  10138. {
  10139. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  10140. OpenBookmark(Side, Bookmark);
  10141. }
  10142. //---------------------------------------------------------------------------
  10143. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  10144. {
  10145. DoBookmarkClick(osLocal, Sender);
  10146. }
  10147. //---------------------------------------------------------------------------
  10148. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  10149. {
  10150. DoBookmarkClick(osRemote, Sender);
  10151. }
  10152. //---------------------------------------------------------------------------
  10153. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  10154. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  10155. {
  10156. if (BookmarkList != NULL)
  10157. {
  10158. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  10159. if (IsLocalBrowserMode())
  10160. {
  10161. Side = osLocal;
  10162. }
  10163. if (!WinConfiguration->UseLocationProfiles)
  10164. {
  10165. std::unique_ptr<TStringList> Directories(new TStringList());
  10166. Directories->CaseSensitive = (Side == osRemote);
  10167. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10168. {
  10169. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10170. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  10171. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  10172. {
  10173. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10174. Item->Caption = EscapeHotkey(Directory);
  10175. Item->ShortCut = Bookmark->ShortCut;
  10176. Item->OnClick = OnBookmarkClick;
  10177. Item->Tag = reinterpret_cast<int>(Bookmark);
  10178. Directories->Add(Directory);
  10179. Menu->Add(Item.release());
  10180. }
  10181. }
  10182. }
  10183. else
  10184. {
  10185. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10186. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10187. {
  10188. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10189. if (!Bookmark->Node.IsEmpty())
  10190. {
  10191. Folders->Add(Bookmark->Node);
  10192. }
  10193. }
  10194. for (int Index = 0; Index < Folders->Count; Index++)
  10195. {
  10196. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10197. Item->Caption = Folders->Strings[Index];
  10198. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10199. Folders->Objects[Index] = Item.get();
  10200. Menu->Add(Item.release());
  10201. }
  10202. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10203. {
  10204. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10205. TTBCustomItem * Parent;
  10206. if (!Bookmark->Node.IsEmpty())
  10207. {
  10208. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10209. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10210. }
  10211. else
  10212. {
  10213. Parent = Menu;
  10214. }
  10215. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10216. Item->Caption = Bookmark->Name;
  10217. Item->ShortCut = Bookmark->ShortCut;
  10218. Item->OnClick = OnBookmarkClick;
  10219. Item->Tag = reinterpret_cast<int>(Bookmark);
  10220. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10221. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10222. {
  10223. // No hint for location profiles that are actually mere "bookmarks"
  10224. }
  10225. else
  10226. {
  10227. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10228. UnicodeString LongHint;
  10229. if (!Bookmark->Local.IsEmpty())
  10230. {
  10231. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10232. }
  10233. if (!Bookmark->Remote.IsEmpty())
  10234. {
  10235. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10236. }
  10237. AddToList(Hint, LongHint, L"|");
  10238. Item->Hint = Hint;
  10239. }
  10240. Parent->Add(Item.release());
  10241. }
  10242. }
  10243. AddMenuSeparator(Menu);
  10244. }
  10245. }
  10246. //---------------------------------------------------------------------------
  10247. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10248. {
  10249. Menu->Clear();
  10250. std::unique_ptr<TTBCustomItem> Item;
  10251. Item.reset(new TTBXItem(Owner));
  10252. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10253. Item->Options = Item->Options << tboDefault;
  10254. Menu->Add(Item.release());
  10255. AddMenuSeparator(Menu);
  10256. if (Terminal != NULL)
  10257. {
  10258. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10259. }
  10260. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10261. Item.reset(new TTBXItem(Owner));
  10262. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10263. Menu->Add(Item.release());
  10264. }
  10265. //---------------------------------------------------------------------------
  10266. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10267. {
  10268. bool Result = true;
  10269. bool Remote = !IsSideLocalBrowser(Side);
  10270. try
  10271. {
  10272. UnicodeString APath = Path;
  10273. if (Remote)
  10274. {
  10275. Terminal->ExceptionOnFail = true;
  10276. }
  10277. else
  10278. {
  10279. APath = ExpandEnvironmentVariables(Path);
  10280. }
  10281. try
  10282. {
  10283. DirView(Side)->Path = APath;
  10284. }
  10285. __finally
  10286. {
  10287. if (Remote)
  10288. {
  10289. Terminal->ExceptionOnFail = false;
  10290. }
  10291. }
  10292. }
  10293. catch (Exception & E)
  10294. {
  10295. if (!Remote || Terminal->Active)
  10296. {
  10297. ShowExtendedExceptionEx(Terminal, &E);
  10298. Result = false;
  10299. }
  10300. else
  10301. {
  10302. throw;
  10303. }
  10304. }
  10305. return Result;
  10306. }
  10307. //---------------------------------------------------------------------------
  10308. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10309. {
  10310. // Make sure there some feedback even when loading tiny local folders
  10311. TInstantOperationVisualizer Visualizer;
  10312. DirView(Side)->ReloadDirectory();
  10313. }
  10314. //---------------------------------------------------------------------------
  10315. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10316. {
  10317. if (Index == SessionsPageControl->TabIndex)
  10318. {
  10319. CloseSession();
  10320. }
  10321. else
  10322. {
  10323. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10324. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10325. TTerminalManager * Manager = TTerminalManager::Instance();
  10326. if (!Terminal->Active)
  10327. {
  10328. Manager->FreeTerminal(Terminal);
  10329. }
  10330. else
  10331. {
  10332. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10333. if (CanCloseQueue(Queue))
  10334. {
  10335. Manager->FreeTerminal(Terminal);
  10336. }
  10337. }
  10338. }
  10339. }
  10340. //---------------------------------------------------------------------------
  10341. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10342. {
  10343. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10344. if (Session == NULL)
  10345. {
  10346. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10347. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10348. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10349. //P = SessionsPageControl->ClientToScreen(P);
  10350. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10351. }
  10352. else
  10353. {
  10354. CloseSessionTab(Index);
  10355. }
  10356. }
  10357. //---------------------------------------------------------------------------
  10358. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10359. {
  10360. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10361. {
  10362. TInstantOperationVisualizer Visualizer;
  10363. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10364. // before we create a new data object
  10365. ClipboardClear();
  10366. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10367. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10368. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10369. // Intermediate variable to WORKAROUND a crash in compiler
  10370. TObjectList * ClipboardFakeMonitors =
  10371. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10372. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10373. if (FClipboardDragDropFilesEx.get() == NULL)
  10374. {
  10375. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10376. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10377. }
  10378. FClipboardDragDropFilesEx->FileList->Clear();
  10379. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10380. FClipboardDragDropFilesEx->CopyToClipboard();
  10381. FClipboardTerminal = Terminal;
  10382. // Need full paths, as cwd can be different once files are pasted
  10383. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10384. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10385. {
  10386. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10387. }
  10388. }
  10389. }
  10390. //---------------------------------------------------------------------------
  10391. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10392. {
  10393. if (OpenClipboard(0))
  10394. {
  10395. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10396. CloseClipboard();
  10397. }
  10398. }
  10399. //---------------------------------------------------------------------------
  10400. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10401. {
  10402. return !FClipboardFakeDirectory.IsEmpty();
  10403. }
  10404. //---------------------------------------------------------------------------
  10405. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10406. {
  10407. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10408. FClipboardFakeDirectory = UnicodeString();
  10409. FClipboardTerminal = NULL;
  10410. if (FDownloadingFromClipboard)
  10411. {
  10412. // We are called by the monitor, so attempt to release it would deadlock
  10413. FClipboardFakeMonitorsPendingReset = true;
  10414. }
  10415. else
  10416. {
  10417. FClipboardFakeMonitors.reset(NULL);
  10418. }
  10419. }
  10420. //---------------------------------------------------------------------------
  10421. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10422. {
  10423. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10424. {
  10425. ClipboardStop();
  10426. }
  10427. }
  10428. //---------------------------------------------------------------------------
  10429. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10430. {
  10431. TTerminalManager * Manager = TTerminalManager::Instance();
  10432. Manager->ActiveSession = FClipboardTerminal;
  10433. TTransferOperationParam Params;
  10434. Params.TargetDirectory = TargetDirectory;
  10435. Params.DragDrop = DragDrop;
  10436. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10437. }
  10438. //---------------------------------------------------------------------------
  10439. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10440. {
  10441. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10442. // (as it pumps a Windows message queue while being shutted down)
  10443. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10444. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10445. {
  10446. // Can fail as it can be e.g. locked by AV, so we retry that later
  10447. bool Removed = RemoveDir(ApiPath(FileName));
  10448. try
  10449. {
  10450. if (!NonVisualDataModule->Busy)
  10451. {
  10452. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10453. FClipboardFakeMonitorsPendingReset = false;
  10454. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10455. TAutoFlag Flag(FDownloadingFromClipboard);
  10456. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10457. }
  10458. }
  10459. __finally
  10460. {
  10461. if (!Removed)
  10462. {
  10463. RemoveDir(ApiPath(FileName));
  10464. }
  10465. }
  10466. }
  10467. }
  10468. //---------------------------------------------------------------------------
  10469. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10470. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10471. {
  10472. TFileMasks::TParams MaskParams;
  10473. MaskParams.Size = Size;
  10474. MaskParams.Modification = Modification;
  10475. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10476. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10477. // TUnixDirView::ItemFullFileName returns trailing backslash for directories, what filemask cannot deal with
  10478. if (!Local && Directory)
  10479. {
  10480. FileName = UnixExcludeTrailingBackslash(FileName);
  10481. }
  10482. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10483. {
  10484. bool ImplicitMatch;
  10485. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10486. !ImplicitMatch)
  10487. {
  10488. Color = Iter->Color;
  10489. break;
  10490. }
  10491. }
  10492. }
  10493. //---------------------------------------------------------------------------
  10494. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10495. {
  10496. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10497. if (!DirView->IsEditing())
  10498. {
  10499. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10500. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10501. // while searching
  10502. if (Key == VK_BACK)
  10503. {
  10504. if (!FIncrementalSearch.IsEmpty())
  10505. {
  10506. if (FIncrementalSearch.Length() == 1)
  10507. {
  10508. ResetIncrementalSearch();
  10509. }
  10510. else
  10511. {
  10512. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10513. IncrementalSearch(NewText, false, false);
  10514. }
  10515. Key = 0;
  10516. }
  10517. }
  10518. }
  10519. }
  10520. //---------------------------------------------------------------------------
  10521. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10522. {
  10523. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10524. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10525. {
  10526. // Filter control sequences.
  10527. // When not searching yet, prefer use of the space for toggling file selection
  10528. // (so we cannot incrementally search for a string starting with a space).
  10529. if ((Key > VK_SPACE) ||
  10530. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10531. {
  10532. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10533. Key = 0;
  10534. }
  10535. }
  10536. }
  10537. //---------------------------------------------------------------------------
  10538. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10539. {
  10540. if (!FIncrementalSearch.IsEmpty())
  10541. {
  10542. FIncrementalSearch = L"";
  10543. DirView(osCurrent)->UpdateStatusBar();
  10544. }
  10545. }
  10546. //---------------------------------------------------------------------------
  10547. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10548. {
  10549. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10550. if (Item == NULL)
  10551. {
  10552. MessageBeep(MB_ICONHAND);
  10553. }
  10554. else
  10555. {
  10556. TCustomDirView * ADirView = DirView(osCurrent);
  10557. {
  10558. TAutoNestingCounter Guard(FIncrementalSearching);
  10559. ADirView->FocusItem(Item);
  10560. if (ADirView->NortonLike == nlOff)
  10561. {
  10562. if ((ADirView->Selected != Item) ||
  10563. (ADirView->SelCount > 1))
  10564. {
  10565. ADirView->ClearSelection();
  10566. ADirView->Selected = Item;
  10567. }
  10568. }
  10569. }
  10570. FIncrementalSearch = Text;
  10571. Item->MakeVisible(false);
  10572. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10573. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10574. ADirView->UpdateStatusBar();
  10575. }
  10576. }
  10577. //---------------------------------------------------------------------------
  10578. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10579. {
  10580. int Index = Item->Index;
  10581. if (!Reverse)
  10582. {
  10583. Index++;
  10584. if (Index >= Item->Owner->Count)
  10585. {
  10586. Index = 0;
  10587. }
  10588. }
  10589. else
  10590. {
  10591. Index--;
  10592. if (Index < 0)
  10593. {
  10594. Index = Item->Owner->Count - 1;
  10595. }
  10596. }
  10597. return Item->Owner->Item[Index];
  10598. }
  10599. //---------------------------------------------------------------------------
  10600. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10601. {
  10602. TCustomDirView * ADirView = DirView(osCurrent);
  10603. if (ADirView->Items->Count == 0)
  10604. {
  10605. return NULL;
  10606. }
  10607. else
  10608. {
  10609. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10610. TListItem * Item = CurrentItem;
  10611. if (SkipCurrent)
  10612. {
  10613. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10614. }
  10615. while (true)
  10616. {
  10617. bool Matches = false;
  10618. switch (WinConfiguration->PanelSearch)
  10619. {
  10620. case isNameStartOnly:
  10621. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item).SubString(1, Text.Length()), Text);
  10622. break;
  10623. case isName:
  10624. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item), Text);
  10625. break;
  10626. case isAll:
  10627. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10628. int Index = 0;
  10629. while ((Index < ColCount) && !Matches)
  10630. {
  10631. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10632. if (Visible)
  10633. {
  10634. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10635. }
  10636. Index++;
  10637. }
  10638. break;
  10639. }
  10640. if (Matches)
  10641. {
  10642. return Item;
  10643. }
  10644. Item = GetNextFile(Item, Reverse);
  10645. if (Item == CurrentItem)
  10646. {
  10647. return NULL;
  10648. }
  10649. }
  10650. }
  10651. }
  10652. //---------------------------------------------------------------------------
  10653. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10654. {
  10655. ResetIncrementalSearch();
  10656. }
  10657. //---------------------------------------------------------------------------
  10658. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10659. {
  10660. if (Message.CharCode == VK_TAB)
  10661. {
  10662. if (!FIncrementalSearch.IsEmpty())
  10663. {
  10664. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10665. bool Reverse = Shift.Contains(ssShift);
  10666. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10667. Message.Result = 1;
  10668. return;
  10669. }
  10670. }
  10671. else if (Message.CharCode == VK_ESCAPE)
  10672. {
  10673. if (!FIncrementalSearch.IsEmpty())
  10674. {
  10675. ResetIncrementalSearch();
  10676. Message.Result = 1;
  10677. return;
  10678. }
  10679. }
  10680. TForm::Dispatch(&Message);
  10681. }
  10682. //---------------------------------------------------------------------------
  10683. void __fastcall TCustomScpExplorerForm::Deactivate()
  10684. {
  10685. TForm::Deactivate();
  10686. // E.g. when switching to an internal editor window
  10687. ResetIncrementalSearch();
  10688. }
  10689. //---------------------------------------------------------------------------
  10690. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10691. {
  10692. // When switching to another application
  10693. ResetIncrementalSearch();
  10694. }
  10695. //---------------------------------------------------------------------------
  10696. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10697. {
  10698. ResetIncrementalSearch();
  10699. }
  10700. //---------------------------------------------------------------------------
  10701. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10702. {
  10703. if (FIncrementalSearching <= 0)
  10704. {
  10705. ResetIncrementalSearch();
  10706. }
  10707. }
  10708. //---------------------------------------------------------------------------
  10709. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10710. {
  10711. CountClicksForWindowPrint(this);
  10712. }
  10713. //---------------------------------------------------------------------------
  10714. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10715. {
  10716. CenterReconnectToolbar();
  10717. }
  10718. //---------------------------------------------------------------------------
  10719. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10720. {
  10721. if (RemoteDirView->ItemFocused != NULL)
  10722. {
  10723. RemoteDirView->ItemFocused->Selected = true;
  10724. }
  10725. }
  10726. //---------------------------------------------------------------------------
  10727. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10728. {
  10729. if (!FQueueStatusUpdating)
  10730. {
  10731. bool Refresh;
  10732. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10733. {
  10734. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10735. }
  10736. else
  10737. {
  10738. if (FQueueFileList->GetCount() > 0)
  10739. {
  10740. FQueueFileList->Clear();
  10741. Refresh = true;
  10742. }
  10743. }
  10744. int Count = FQueueFileList->GetCount();
  10745. if (QueueFileList->Items->Count != Count)
  10746. {
  10747. DebugAssert(Refresh);
  10748. QueueFileList->Items->Count = Count;
  10749. }
  10750. if (Refresh)
  10751. {
  10752. QueueFileList->Invalidate();
  10753. }
  10754. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10755. }
  10756. }
  10757. //---------------------------------------------------------------------------
  10758. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10759. {
  10760. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10761. }
  10762. //---------------------------------------------------------------------------
  10763. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10764. {
  10765. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10766. // See TSynchronizeChecklistDialog::UpdateTimer
  10767. if (QueueFileList->Items->Count > 0)
  10768. {
  10769. QueueFileList->Items->Item[0]->MakeVisible(false);
  10770. }
  10771. UpdateQueueFileList();
  10772. }
  10773. //---------------------------------------------------------------------------
  10774. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10775. {
  10776. Active = QueueView3->Focused() || QueueFileList->Focused();
  10777. }
  10778. //---------------------------------------------------------------------------
  10779. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10780. {
  10781. QueueLabelUpdateStatus();
  10782. }
  10783. //---------------------------------------------------------------------------
  10784. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10785. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10786. {
  10787. int State = FQueueFileList->GetState(Item->Index);
  10788. if (State == qfsProcessed)
  10789. {
  10790. Sender->Canvas->Font->Color = clGrayText;
  10791. }
  10792. }
  10793. //---------------------------------------------------------------------------
  10794. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10795. {
  10796. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10797. QueueFileList->Column[0]->Width =
  10798. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10799. }
  10800. //---------------------------------------------------------------------------
  10801. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10802. {
  10803. QueueFileListColumnAutoSize();
  10804. }
  10805. //---------------------------------------------------------------------------
  10806. void __fastcall TCustomScpExplorerForm::CloseApp()
  10807. {
  10808. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10809. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10810. // So we clear the flag here.
  10811. DebugAssert(NonVisualDataModule->Busy);
  10812. NonVisualDataModule->EndBusy();
  10813. try
  10814. {
  10815. Close();
  10816. }
  10817. __finally
  10818. {
  10819. NonVisualDataModule->StartBusy();
  10820. }
  10821. }
  10822. //---------------------------------------------------------------------------
  10823. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10824. {
  10825. return (Terminal != NULL) && Terminal->Active;
  10826. }
  10827. //---------------------------------------------------------------------------
  10828. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  10829. {
  10830. return (ManagedSession != NULL);
  10831. }
  10832. //---------------------------------------------------------------------------
  10833. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10834. {
  10835. return IsActiveTerminal(Terminal);
  10836. }
  10837. //---------------------------------------------------------------------------
  10838. void TCustomScpExplorerForm::LocalLocalCopy(
  10839. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  10840. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  10841. {
  10842. DebugFail();
  10843. }
  10844. //---------------------------------------------------------------------------
  10845. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  10846. {
  10847. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  10848. }
  10849. //---------------------------------------------------------------------------
  10850. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  10851. {
  10852. // Changing tab list triggers the OnResize.
  10853. if (!FUpdatingSessionTabs)
  10854. {
  10855. // UpdateTabsCaptionTruncation should be enough
  10856. SessionListChanged(true);
  10857. }
  10858. }
  10859. //---------------------------------------------------------------------------
  10860. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  10861. {
  10862. if (ASession == ManagedSession)
  10863. {
  10864. UpdateSession(ASession);
  10865. }
  10866. TSessionData * Data = ASession->StateData;
  10867. UnicodeString Result;
  10868. if (Side == osLocal)
  10869. {
  10870. Result = Data->LocalDirectory;
  10871. }
  10872. else if (DebugAlwaysTrue(Side == osOther))
  10873. {
  10874. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  10875. }
  10876. return Result;
  10877. }
  10878. //---------------------------------------------------------------------------
  10879. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  10880. {
  10881. UnicodeString Result;
  10882. if (!ASession->Active)
  10883. {
  10884. Result = LoadStr(STATUS_NOT_CONNECTED2);
  10885. }
  10886. else
  10887. {
  10888. Result = GetSessionPath(ASession, osRemote);
  10889. }
  10890. return Result;
  10891. }
  10892. //---------------------------------------------------------------------------
  10893. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  10894. {
  10895. return LoadStr(NEW_REMOTE_TAB_HINT);
  10896. }
  10897. //---------------------------------------------------------------------------
  10898. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  10899. {
  10900. if (Index >= 0)
  10901. {
  10902. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10903. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  10904. if (TabSession == NULL)
  10905. {
  10906. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  10907. if (NewTabTabCaption != TabSheet->Caption)
  10908. {
  10909. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  10910. }
  10911. }
  10912. else
  10913. {
  10914. TTerminalManager * Manager = TTerminalManager::Instance();
  10915. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  10916. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  10917. }
  10918. }
  10919. }
  10920. //---------------------------------------------------------------------------
  10921. typedef std::vector<TListItem *> TListItemsVector;
  10922. struct TCalculateSizeOperation
  10923. {
  10924. TCalculateSizeStats Stats;
  10925. TListItemsVector ListItems;
  10926. size_t Index;
  10927. };
  10928. //---------------------------------------------------------------------------
  10929. void TCustomScpExplorerForm::CalculateDirectorySizes()
  10930. {
  10931. TCalculateSizeOperation CalculateSizeOperation;
  10932. TOperationSide Side = GetSide(osCurrent);
  10933. TCustomDirView * ADirView = DirView(Side);
  10934. bool FullPath = IsSideLocalBrowser(Side);
  10935. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, FullPath, NULL, true));
  10936. std::unique_ptr<TStrings> FileList(new TStringList());
  10937. for (int Index = 0; Index < AllFileList->Count; Index++)
  10938. {
  10939. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  10940. if (ADirView->ItemIsDirectory(Item))
  10941. {
  10942. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  10943. CalculateSizeOperation.ListItems.push_back(Item);
  10944. }
  10945. }
  10946. CalculateSizeOperation.Index = 0;
  10947. __int64 Size = 0;
  10948. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation);
  10949. FCalculateSizeOperation = &CalculateSizeOperation;
  10950. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  10951. FOnFileOperationFinished = DirectorySizeCalculated;
  10952. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation);
  10953. FPrimaryOperation = foCalculateSize;
  10954. TCalculatedSizes CalculatedSizes;
  10955. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  10956. if (Side == osLocal)
  10957. {
  10958. Terminal->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  10959. }
  10960. else
  10961. {
  10962. TCalculateSizeParams Params;
  10963. Params.Stats = &CalculateSizeOperation.Stats;
  10964. Params.UseCache = true;
  10965. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  10966. }
  10967. }
  10968. //---------------------------------------------------------------------------
  10969. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(TOperationSide Side, const UnicodeString & FileName, bool)
  10970. {
  10971. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  10972. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  10973. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  10974. // optimization
  10975. bool GotExpectedItem =
  10976. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  10977. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  10978. int Index = -1;
  10979. if (DebugAlwaysTrue(GotExpectedItem))
  10980. {
  10981. Index = FCalculateSizeOperation->Index;
  10982. FCalculateSizeOperation->Index++;
  10983. }
  10984. else
  10985. {
  10986. TListItemsVector::iterator I =
  10987. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  10988. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  10989. {
  10990. Index = I - FCalculateSizeOperation->ListItems.begin();
  10991. }
  10992. }
  10993. if (DebugAlwaysTrue(Index >= 0))
  10994. {
  10995. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  10996. ADirView->SetItemCalculatedSize(Item, Size);
  10997. }
  10998. }
  10999. //---------------------------------------------------------------------------
  11000. void __fastcall TCustomScpExplorerForm::MessageDockRequestDock(TObject *, TTBCustomDockableWindow *, bool & Accept)
  11001. {
  11002. Accept = false;
  11003. }
  11004. //---------------------------------------------------------------------------