CustomScpExplorer.cpp 378 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <Bookmarks.h>
  8. #include <Interface.h>
  9. #include <Exceptions.h>
  10. #include <CoreMain.h>
  11. #include <FileSystems.h>
  12. #include <TextsCore.h>
  13. #include <TextsWin.h>
  14. #include <HelpWin.h>
  15. #include <VCLCommon.h>
  16. #include <Progress.h>
  17. #include <SynchronizeProgress.h>
  18. #include <DragExt.h>
  19. #include <WinApi.h>
  20. #include "GUITools.h"
  21. #include "NonVisual.h"
  22. #include "Glyphs.h"
  23. #include "Tools.h"
  24. #include "WinConfiguration.h"
  25. #include "TerminalManager.h"
  26. #include "EditorManager.h"
  27. #include "ProgParams.h"
  28. #include "Setup.h"
  29. #include <Consts.hpp>
  30. #include <DateUtils.hpp>
  31. #include <TB2Common.hpp>
  32. #include <DirectoryMonitor.hpp>
  33. #include <System.IOUtils.hpp>
  34. //---------------------------------------------------------------------------
  35. #pragma package(smart_init)
  36. #pragma link "CustomDirView"
  37. #pragma link "CustomUnixDirView"
  38. #pragma link "IEListView"
  39. #pragma link "NortonLikeListView"
  40. #pragma link "UnixDirView"
  41. #pragma link "CustomDriveView"
  42. #pragma link "UnixDriveView"
  43. #pragma link "CustomDriveView"
  44. #pragma link "TB2Dock"
  45. #pragma link "TBXStatusBars"
  46. #pragma link "TB2Item"
  47. #pragma link "TB2Toolbar"
  48. #pragma link "ThemePageControl"
  49. #pragma link "PathLabel"
  50. #pragma resource "*.dfm"
  51. //---------------------------------------------------------------------------
  52. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  53. static const int SessionPanelCount = 3;
  54. //---------------------------------------------------------------------------
  55. class TMutexGuard
  56. {
  57. public:
  58. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  59. int Timeout = 5000)
  60. {
  61. FMutex = NULL;
  62. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  63. if (WaitResult == WAIT_TIMEOUT)
  64. {
  65. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  66. }
  67. else
  68. {
  69. FMutex = AMutex;
  70. }
  71. }
  72. ~TMutexGuard()
  73. {
  74. if (FMutex != NULL)
  75. {
  76. ReleaseMutex(FMutex);
  77. }
  78. }
  79. private:
  80. HANDLE FMutex;
  81. };
  82. //---------------------------------------------------------------------------
  83. class TWindowLock
  84. {
  85. public:
  86. TWindowLock(TCustomScpExplorerForm * Form) :
  87. FForm(Form)
  88. {
  89. FForm->LockWindow();
  90. }
  91. ~TWindowLock()
  92. {
  93. FForm->UnlockWindow();
  94. }
  95. private:
  96. TCustomScpExplorerForm * FForm;
  97. };
  98. //---------------------------------------------------------------------------
  99. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  100. FForm(Form)
  101. {
  102. FForm->BatchStart(FBatchStorage);
  103. }
  104. //---------------------------------------------------------------------------
  105. TAutoBatch::~TAutoBatch()
  106. {
  107. FForm->BatchEnd(FBatchStorage);
  108. }
  109. //---------------------------------------------------------------------------
  110. struct TTransferOperationParam
  111. {
  112. TTransferOperationParam();
  113. UnicodeString TargetDirectory;
  114. bool Temp;
  115. bool DragDrop;
  116. int Options;
  117. TAutoSwitch Queue;
  118. };
  119. //---------------------------------------------------------------------------
  120. TTransferOperationParam::TTransferOperationParam()
  121. {
  122. Temp = false;
  123. DragDrop = false;
  124. Options = 0;
  125. Queue = asAuto;
  126. }
  127. //---------------------------------------------------------------------------
  128. class TTransferPresetNoteData : public TObject
  129. {
  130. public:
  131. UnicodeString Message;
  132. };
  133. //---------------------------------------------------------------------------
  134. class TTerminalNoteData : public TObject
  135. {
  136. public:
  137. TManagedTerminal * Terminal;
  138. };
  139. //---------------------------------------------------------------------------
  140. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  141. FFormRestored(false),
  142. TForm(Owner)
  143. {
  144. FInvalid = false;
  145. FCurrentSide = osRemote;
  146. FEverShown = false;
  147. FDocks = new TList();
  148. InitControls();
  149. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  150. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  151. NonVisualDataModule->ScpExplorer = this;
  152. RestoreParams();
  153. ConfigurationChanged();
  154. RemoteDirView->Invalidate();
  155. FAutoOperation = false;
  156. FOnFileOperationFinished = NULL;
  157. FPrimaryOperation = foNone;
  158. FForceExecution = false;
  159. FIgnoreNextDialogChar = 0;
  160. FErrorList = NULL;
  161. FSynchronizeProgressForm = NULL;
  162. FProgressForm = NULL;
  163. FRefreshLocalDirectory = false;
  164. FRefreshRemoteDirectory = false;
  165. FDDExtMapFile = NULL;
  166. // CreateMutexW keeps failing with ERROR_NOACCESS
  167. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  168. DebugAssert(FDDExtMutex != NULL);
  169. FDDTargetControl = NULL;
  170. FDelayedDeletionTimer = NULL;
  171. FDelayedDeletionList = new TStringList();
  172. FDDFileList = NULL;
  173. FPendingTempSpaceWarn = false;
  174. FCapturedLog = NULL;
  175. FDragDropOperation = false;
  176. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  177. FAllowTransferPresetAutoSelect = true;
  178. FSessionChanging = false;
  179. FCopyParamDefault = L"";
  180. FSynchronizeController = NULL;
  181. FPendingQueueActionItem = NULL;
  182. FLockLevel = 0;
  183. FLockSuspendLevel = 0;
  184. FDisabledOnLockSuspend = false;
  185. FAlternativeDelete = false;
  186. FTrayIcon = new ::TTrayIcon(0);
  187. FTrayIcon->OnClick = TrayIconClick;
  188. FMaxQueueLength = 0;
  189. FLastContextPopupScreenPoint = TPoint(-1, -1);
  190. FTransferResumeList = NULL;
  191. FMoveToQueue = false;
  192. FStandaloneEditing = false;
  193. FOnFeedSynchronizeError = NULL;
  194. FNeedSession = false;
  195. FDoNotIdleCurrentTerminal = 0;
  196. FIncrementalSearching = 0;
  197. FIncrementalSearchHaveNext = false;
  198. FQueueFileList.reset(new TQueueFileList());
  199. FProgressSide = osCurrent;
  200. FCalculateSizeOperation = NULL;
  201. FEditorManager = new TEditorManager();
  202. FEditorManager->OnFileChange = ExecutedFileChanged;
  203. FEditorManager->OnFileReload = ExecutedFileReload;
  204. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  205. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  206. FLocalEditors = new TList();
  207. FQueueStatus = NULL;
  208. FQueueStatusSection = new TCriticalSection();
  209. FQueueStatusInvalidated = false;
  210. FQueueItemInvalidated = false;
  211. FQueueStatusUpdating = false;
  212. FQueueActedItem = NULL;
  213. FQueueController = new TQueueController(QueueView3);
  214. FUserActionTimer = new TTimer(this);
  215. FUserActionTimer->Enabled = false;
  216. FUserActionTimer->Interval = 10;
  217. FUserActionTimer->OnTimer = UserActionTimer;
  218. FNotes = new TStringList();
  219. FNoteTimer = new TTimer(this);
  220. FNoteTimer->Enabled = false;
  221. FNoteTimer->OnTimer = NoteTimer;
  222. FOnNoteClick = NULL;
  223. FOle32Library = LoadLibrary(L"Ole32.dll");
  224. FDragMoveCursor = FOle32Library != NULL ?
  225. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  226. UseSystemSettings(this);
  227. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  228. static_cast<TObject*>(GetComponent(fcTransferList)));
  229. DebugAssert(TransferList != NULL);
  230. FTransferListHoverIndex = -1;
  231. TransferList->OnChange = TransferListChange;
  232. TransferList->OnDrawItem = TransferListDrawItem;
  233. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  234. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  235. UseDesktopFont(SessionsPageControl);
  236. UpdateSessionsPageControlHeight();
  237. UseDesktopFont(RemoteDirView);
  238. UseDesktopFont(RemoteDriveView);
  239. UseDesktopFont(QueueView3);
  240. UseDesktopFont(QueueFileList);
  241. UseDesktopFont(QueueLabel);
  242. UseDesktopFont(RemoteStatusBar);
  243. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  244. QueueSplitter->ShowHint = true;
  245. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  246. QueueFileListSplitter->ShowHint = true;
  247. RemotePanelSplitter->ShowHint = true;
  248. UpdateImages();
  249. FCustomCommandMenu = CreateTBXPopupMenu(this);
  250. FCustomCommandLocalFileList = NULL;
  251. FCustomCommandRemoteFileList = NULL;
  252. FSessionColors = new TPngImageList(this);
  253. FSessionColors->ColorDepth = cd32Bit;
  254. AddFixedSessionImages();
  255. SessionsPageControl->Images = FSessionColors;
  256. UpdateQueueLabel();
  257. CreateHiddenWindow();
  258. StartUpdates();
  259. }
  260. //---------------------------------------------------------------------------
  261. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  262. {
  263. FInvalid = true;
  264. // this has to be one of the very first things to do
  265. StopUpdateThread();
  266. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  267. TTerminalManager::Instance()->ScpExplorer = NULL;
  268. delete FCustomCommandLocalFileList;
  269. delete FCustomCommandRemoteFileList;
  270. delete FCustomCommandMenu;
  271. delete FTrayIcon;
  272. FTrayIcon = NULL;
  273. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  274. delete FEditorManager;
  275. ForceCloseLocalEditors();
  276. delete FLocalEditors;
  277. HideFileFindDialog();
  278. if (FDelayedDeletionTimer)
  279. {
  280. DoDelayedDeletion(NULL);
  281. SAFE_DESTROY(FDelayedDeletionTimer);
  282. }
  283. SAFE_DESTROY(FDelayedDeletionList);
  284. // sometimes we do not get DDEnd so the list is not released
  285. SAFE_DESTROY(FDDFileList);
  286. DebugAssert(FSynchronizeController == NULL);
  287. CloseHandle(FDDExtMutex);
  288. FDDExtMutex = NULL;
  289. FreeLibrary(FOle32Library);
  290. FOle32Library = NULL;
  291. FDragMoveCursor = NULL;
  292. DebugAssert(!FErrorList);
  293. if (FEverShown)
  294. {
  295. // when window is never shown (like when running command-line operation),
  296. // particularly window site is not restored correctly (BoundsRect value set
  297. // in RestoreForm gets lost during handle allocation), so we do not want
  298. // it to be stored
  299. StoreParams();
  300. }
  301. ManagedSession = NULL;
  302. Queue = NULL;
  303. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  304. NonVisualDataModule->ScpExplorer = NULL;
  305. delete FQueueController;
  306. FQueueController = NULL;
  307. delete FQueueStatusSection;
  308. FQueueStatusSection = NULL;
  309. delete FQueueStatus;
  310. FQueueStatus = NULL;
  311. delete FUserActionTimer;
  312. FUserActionTimer = NULL;
  313. delete FNoteTimer;
  314. delete FNotes;
  315. delete FNoteData;
  316. SAFE_DESTROY(FDocks);
  317. SessionsPageControl->Images = NULL;
  318. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  319. SAFE_DESTROY(FSessionColors);
  320. SAFE_DESTROY(FSessionsDragDropFilesEx);
  321. SAFE_DESTROY(FHistoryMenu[0][0]);
  322. SAFE_DESTROY(FHistoryMenu[0][1]);
  323. SAFE_DESTROY(FHistoryMenu[1][0]);
  324. SAFE_DESTROY(FHistoryMenu[1][1]);
  325. if (FHiddenWindow != NULL)
  326. {
  327. DestroyWindow(FHiddenWindow);
  328. FHiddenWindow = NULL;
  329. }
  330. }
  331. //---------------------------------------------------------------------------
  332. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  333. {
  334. std::unique_ptr<TSessionData> Data;
  335. if (!Session.IsEmpty())
  336. {
  337. bool DefaultsOnly;
  338. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  339. }
  340. TTerminalManager * Manager = TTerminalManager::Instance();
  341. for (int Index = 0; Index < Manager->Count; Index++)
  342. {
  343. TTerminal * Terminal = Manager->Sessions[Index];
  344. if (Session.IsEmpty() ||
  345. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  346. {
  347. if (Path.IsEmpty())
  348. {
  349. Terminal->ClearCaches();
  350. }
  351. else
  352. {
  353. Terminal->DirectoryModified(Path, true);
  354. }
  355. }
  356. }
  357. // We should flag a pending refresh for the background terminals or busy foreground terminals
  358. if (HasActiveTerminal() &&
  359. CanCommandLineFromAnotherInstance() &&
  360. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  361. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  362. {
  363. Terminal->ReloadDirectory();
  364. }
  365. }
  366. //---------------------------------------------------------------------------
  367. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  368. {
  369. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  370. size_t MessageSize = sizeof(TCopyDataMessage);
  371. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  372. if (Result)
  373. {
  374. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  375. Result = (Message.Version == TCopyDataMessage::Version1);
  376. if (Result)
  377. {
  378. switch (Message.Command)
  379. {
  380. case TCopyDataMessage::CommandCanCommandLine:
  381. Result = CanCommandLineFromAnotherInstance();
  382. break;
  383. case TCopyDataMessage::CommandCommandLine:
  384. {
  385. UnicodeString CommandLine(Message.CommandLine);
  386. Result = CommandLineFromAnotherInstance(CommandLine);
  387. }
  388. break;
  389. case TCopyDataMessage::RefreshPanel:
  390. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  391. break;
  392. case TCopyDataMessage::MainWindowCheck:
  393. Result = true;
  394. break;
  395. default:
  396. Result = false;
  397. break;
  398. }
  399. }
  400. }
  401. Message.Result = Result ? 1 : 0;
  402. }
  403. //---------------------------------------------------------------------------
  404. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  405. {
  406. WNDCLASS WindowClass = {0};
  407. WindowClass.lpfnWndProc = DefWindowProc;
  408. WindowClass.hInstance = HInstance;
  409. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  410. FHiddenWindow = NULL;
  411. if (RegisterClass(&WindowClass))
  412. {
  413. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  414. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  415. }
  416. }
  417. //---------------------------------------------------------------------------
  418. bool __fastcall TCustomScpExplorerForm::CanConsole()
  419. {
  420. return
  421. HasActiveTerminal() &&
  422. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  423. }
  424. //---------------------------------------------------------------------------
  425. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  426. {
  427. bool Result = Visible && !NonVisualDataModule->Busy;
  428. return Result;
  429. }
  430. //---------------------------------------------------------------------------
  431. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  432. const UnicodeString & CommandLine)
  433. {
  434. TProgramParams Params(CommandLine);
  435. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  436. if (Result)
  437. {
  438. NonVisualDataModule->StartBusy();
  439. try
  440. {
  441. // this action is initiated from another process,
  442. // so it's likely that our window is not visible,
  443. // and user won't see what is going on
  444. Application->BringToFront();
  445. // reload sessions as we may be asked to open a session
  446. // just stored by another instance
  447. StoredSessions->Reload();
  448. UnicodeString SessionName = Params.Param[1];
  449. std::unique_ptr<TObjectList> DataList(new TObjectList());
  450. UnicodeString DownloadFile; // unused
  451. try
  452. {
  453. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  454. }
  455. catch (EAbort &)
  456. {
  457. // Happens when opening session in PuTTY and in other situations,
  458. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  459. // to process the command-line on its own (or to pass it to yet another instance)
  460. }
  461. if (DataList->Count > 0)
  462. {
  463. TTerminalManager * Manager = TTerminalManager::Instance();
  464. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  465. }
  466. }
  467. __finally
  468. {
  469. NonVisualDataModule->EndBusy();
  470. }
  471. }
  472. return Result;
  473. }
  474. //---------------------------------------------------------------------------
  475. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  476. {
  477. TManagedTerminal * Result;
  478. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  479. {
  480. Result = FManagedSession;
  481. }
  482. else
  483. {
  484. Result = NULL;
  485. }
  486. return Result;
  487. }
  488. //---------------------------------------------------------------------------
  489. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  490. {
  491. if (FManagedSession != value)
  492. {
  493. SessionChanging();
  494. DoSetManagedSession(value, false);
  495. }
  496. }
  497. //---------------------------------------------------------------------------
  498. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  499. {
  500. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  501. FManagedSession = value;
  502. {
  503. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  504. FAllowTransferPresetAutoSelect = false;
  505. TAutoFlag SessionChangingFlag(FSessionChanging);
  506. SessionChanged(Replace);
  507. }
  508. if (Terminal != NULL)
  509. {
  510. TransferPresetAutoSelect();
  511. }
  512. // Update app and tab titles, prevented above by FSessionChanging.
  513. UpdateControls();
  514. }
  515. //---------------------------------------------------------------------------
  516. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  517. {
  518. DoSetManagedSession(value, true);
  519. }
  520. //---------------------------------------------------------------------------
  521. void __fastcall TCustomScpExplorerForm::SessionChanging()
  522. {
  523. if (ManagedSession != NULL)
  524. {
  525. UpdateSession(ManagedSession);
  526. }
  527. }
  528. //---------------------------------------------------------------------------
  529. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  530. {
  531. if (Terminal != NULL)
  532. {
  533. UpdateSessionColor((TColor)Terminal->StateData->Color);
  534. }
  535. if (!TTerminalManager::Instance()->Updating)
  536. {
  537. SessionListChanged();
  538. }
  539. if (Terminal != NULL)
  540. {
  541. TObject * State = NULL;
  542. if (WinConfiguration->PreservePanelState)
  543. {
  544. State = Terminal->RemoteExplorerState;
  545. DirView(osRemote)->AnnounceState(State);
  546. }
  547. try
  548. {
  549. if (Replaced)
  550. {
  551. RemoteDirView->ReplaceTerminal(Terminal);
  552. }
  553. else
  554. {
  555. RemoteDirView->Terminal = Terminal;
  556. }
  557. if (Terminal->Active)
  558. {
  559. Terminal->RefreshDirectory();
  560. }
  561. if (State != NULL)
  562. {
  563. DirView(osRemote)->RestoreState(State);
  564. }
  565. }
  566. __finally
  567. {
  568. DirView(osRemote)->AnnounceState(NULL);
  569. }
  570. }
  571. else
  572. {
  573. RemoteDirView->Terminal = NULL;
  574. }
  575. InitStatusBar();
  576. UpdateTransferList();
  577. // Update panels Enable state before refreshing the labels
  578. UpdateControls();
  579. }
  580. //---------------------------------------------------------------------------
  581. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  582. {
  583. if (Queue != value)
  584. {
  585. if (FQueue != NULL)
  586. {
  587. FQueue->OnListUpdate = NULL;
  588. FQueue->OnQueueItemUpdate = NULL;
  589. }
  590. FQueue = value;
  591. if (FQueue != NULL)
  592. {
  593. DebugAssert(FQueue->OnListUpdate == NULL);
  594. FQueue->OnListUpdate = QueueListUpdate;
  595. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  596. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  597. }
  598. QueueChanged();
  599. }
  600. }
  601. //---------------------------------------------------------------------------
  602. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  603. TListItem * Item)
  604. {
  605. if (FQueueActedItem == Item)
  606. {
  607. FQueueActedItem = NULL;
  608. if ((QueueView3->PopupMenu != NULL) &&
  609. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  610. {
  611. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  612. ReleaseCapture();
  613. }
  614. }
  615. if (Item->Data == FPendingQueueActionItem)
  616. {
  617. FPendingQueueActionItem = NULL;
  618. }
  619. }
  620. //---------------------------------------------------------------------------
  621. bool TCustomScpExplorerForm::IsAnythingQueued()
  622. {
  623. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  624. }
  625. //---------------------------------------------------------------------------
  626. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  627. {
  628. {
  629. TGuard Guard(FQueueStatusSection);
  630. FQueueStatusInvalidated = false;
  631. if (FQueue != NULL)
  632. {
  633. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  634. }
  635. }
  636. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  637. {
  638. FMaxQueueLength = FQueueStatus->Count;
  639. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  640. }
  641. {
  642. TAutoFlag Flag(FQueueStatusUpdating);
  643. FQueueController->UpdateQueueStatus(FQueueStatus);
  644. }
  645. UpdateQueueFileList();
  646. SetQueueProgress();
  647. UpdateQueueView();
  648. bool IsEmpty = !IsAnythingQueued();
  649. if (IsEmpty && (Terminal != NULL))
  650. {
  651. TOnceDoneOperation OnceDoneOperation =
  652. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  653. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  654. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  655. {
  656. FQueue->Enabled = false;
  657. }
  658. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  659. {
  660. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  661. }
  662. }
  663. }
  664. //---------------------------------------------------------------------------
  665. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  666. {
  667. UnicodeString Result;
  668. if (FQueueStatus != NULL)
  669. {
  670. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  671. {
  672. TFileOperationProgressType * ProgressData =
  673. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  674. if ((ProgressData != NULL) && ProgressData->InProgress)
  675. {
  676. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  677. }
  678. }
  679. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  680. {
  681. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  682. }
  683. }
  684. return Result;
  685. }
  686. //---------------------------------------------------------------------------
  687. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  688. {
  689. ComponentVisible[fcQueueView] =
  690. !IsLocalBrowserMode() &&
  691. ((WinConfiguration->QueueView.Show == qvShow) ||
  692. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  693. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  694. }
  695. //---------------------------------------------------------------------------
  696. void __fastcall TCustomScpExplorerForm::QueueChanged()
  697. {
  698. if (FQueueStatus != NULL)
  699. {
  700. // UpdateFileList implementation relies on the status being recreated when session changes
  701. delete FQueueStatus;
  702. FQueueStatus = NULL;
  703. }
  704. UpdateQueueStatus(true);
  705. }
  706. //---------------------------------------------------------------------------
  707. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  708. {
  709. if (FQueue == Queue)
  710. {
  711. FQueueStatusInvalidated = true;
  712. }
  713. }
  714. //---------------------------------------------------------------------------
  715. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  716. TQueueItem * Item)
  717. {
  718. if (FQueue == Queue)
  719. {
  720. TGuard Guard(FQueueStatusSection);
  721. // this may be running in parallel with QueueChanged
  722. if (FQueueStatus != NULL)
  723. {
  724. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  725. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  726. {
  727. FRefreshLocalDirectory = (QueueItem == NULL) ||
  728. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  729. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  730. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  731. }
  732. if (QueueItem != NULL)
  733. {
  734. QueueItem->UserData = (void*)true;
  735. FQueueItemInvalidated = true;
  736. }
  737. }
  738. }
  739. }
  740. //---------------------------------------------------------------------------
  741. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  742. {
  743. // during standalone editing, we have no way to see/control queue,
  744. // so we have to always popup prompts automatically
  745. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  746. }
  747. //---------------------------------------------------------------------------
  748. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  749. {
  750. if (FQueueStatus != NULL)
  751. {
  752. bool QueueAutoPopup = IsQueueAutoPopup();
  753. bool NeedRefresh = FQueueController->NeedRefresh();
  754. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  755. FQueueItemInvalidated = false;
  756. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  757. bool Updated = false;
  758. TQueueItemProxy * QueueItem;
  759. bool WasUserAction;
  760. for (int Index = 0; Index < Limit; Index++)
  761. {
  762. bool Update = false;
  763. QueueItem = FQueueStatus->Items[Index];
  764. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  765. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  766. (FPendingQueueActionItem == NULL))
  767. {
  768. FPendingQueueActionItem = QueueItem;
  769. FUserActionTimer->Enabled = true;
  770. }
  771. if ((bool)QueueItem->UserData)
  772. {
  773. QueueItem->UserData = (void*)false;
  774. QueueItem->Update();
  775. Updated = true;
  776. Update = true;
  777. }
  778. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  779. {
  780. Update = true;
  781. }
  782. if (Update)
  783. {
  784. FQueueController->RefreshQueueItem(QueueItem);
  785. }
  786. }
  787. if (Updated)
  788. {
  789. NonVisualDataModule->UpdateNonVisibleActions();
  790. SetQueueProgress();
  791. UpdateQueueFileList();
  792. }
  793. }
  794. }
  795. //---------------------------------------------------------------------------
  796. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  797. {
  798. if (FTaskbarList != NULL)
  799. {
  800. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  801. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  802. }
  803. }
  804. //---------------------------------------------------------------------------
  805. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  806. {
  807. if (Progress >= 0)
  808. {
  809. if (FTaskbarList != NULL)
  810. {
  811. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  812. }
  813. }
  814. else
  815. {
  816. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  817. }
  818. }
  819. //---------------------------------------------------------------------------
  820. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  821. {
  822. int OverallProgress;
  823. // FProgressForm is null when this is called from SetQueueProgress
  824. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  825. {
  826. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  827. }
  828. else if (!TFileOperationProgressType::IsIndeterminateOperation(ProgressData->Operation))
  829. {
  830. OverallProgress = ProgressData->OverallProgress();
  831. }
  832. else
  833. {
  834. OverallProgress = -1;
  835. }
  836. SetTaskbarListProgressValue(OverallProgress);
  837. }
  838. //---------------------------------------------------------------------------
  839. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  840. {
  841. TTerminalManager::Instance()->QueueStatusUpdated();
  842. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  843. {
  844. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  845. {
  846. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  847. {
  848. TFileOperationProgressType * ProgressData = NULL;
  849. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  850. {
  851. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  852. }
  853. if ((ProgressData != NULL) &&
  854. ProgressData->InProgress)
  855. {
  856. SetTaskbarListProgressValue(ProgressData);
  857. }
  858. else
  859. {
  860. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  861. }
  862. }
  863. else
  864. {
  865. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  866. }
  867. }
  868. else
  869. {
  870. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  871. }
  872. }
  873. UpdateQueueLabel();
  874. }
  875. //---------------------------------------------------------------------------
  876. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  877. {
  878. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  879. if (FQueueStatus != NULL)
  880. {
  881. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  882. if (ActiveAndPendingPrimaryCount > 0)
  883. {
  884. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  885. }
  886. }
  887. QueueLabel->Caption = Caption;
  888. }
  889. //---------------------------------------------------------------------------
  890. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  891. {
  892. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  893. static_cast<TComponent*>(GetComponent(fcTransferList)));
  894. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  895. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  896. TransferList->Strings->BeginUpdate();
  897. try
  898. {
  899. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  900. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  901. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  902. if (FTransferDropDownHint.IsEmpty())
  903. {
  904. FTransferDropDownHint = TransferDropDown->Hint;
  905. }
  906. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  907. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  908. TransferDropDown->Text = StripHotkey(Name);
  909. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  910. (FTransferDropDownHint, StripHotkey(Name),
  911. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  912. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  913. // update the label, otherwise when it is updated only on the first draw
  914. // of the list, it is drawn "bold" for some reason
  915. FTransferListHoverIndex = TransferList->ItemIndex;
  916. UpdateTransferLabel();
  917. }
  918. __finally
  919. {
  920. TransferList->Strings->EndUpdate();
  921. }
  922. }
  923. //---------------------------------------------------------------------------
  924. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  925. {
  926. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  927. DebugAssert(Toolbar != NULL);
  928. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  929. }
  930. //---------------------------------------------------------------------------
  931. void __fastcall TCustomScpExplorerForm::UpdateActions()
  932. {
  933. TForm::UpdateActions();
  934. if (ComponentVisible[fcCustomCommandsBand])
  935. {
  936. UpdateCustomCommandsToolbar();
  937. }
  938. }
  939. //---------------------------------------------------------------------------
  940. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  941. {
  942. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  943. }
  944. //---------------------------------------------------------------------------
  945. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  946. {
  947. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  948. {
  949. DirView->RowSelect = WinConfiguration->FullRowSelect;
  950. // selection is not redrawn automatically when RowSelect changes
  951. DirView->Invalidate();
  952. }
  953. }
  954. //---------------------------------------------------------------------------
  955. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  956. {
  957. Color = GetBtnFaceColor();
  958. DebugAssert(Configuration && RemoteDirView);
  959. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  960. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  961. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  962. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  963. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  964. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  965. UpdateRowSelect(RemoteDirView);
  966. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  967. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  968. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  969. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  970. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  971. UpdateSessionsPageControlHeight();
  972. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  973. UpdateToolbarDisplayMode();
  974. UpdateTransferList();
  975. if (Terminal != NULL)
  976. {
  977. TransferPresetAutoSelect();
  978. }
  979. UpdateQueueView();
  980. UpdateControls();
  981. // this can be called even before constuctor finishes.
  982. if (FEditorManager != NULL)
  983. {
  984. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  985. }
  986. // this can be called even before constuctor finishes.
  987. if (FLocalEditors != NULL)
  988. {
  989. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  990. {
  991. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  992. }
  993. }
  994. if (ComponentVisible[fcCustomCommandsBand])
  995. {
  996. UpdateCustomCommandsToolbar();
  997. }
  998. // show only when keeping queue items forever,
  999. // otherwise, its enough to have in in the context menu
  1000. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1001. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1002. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1003. {
  1004. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1005. FFileColorsCurrent = WinConfiguration->FileColors;
  1006. FileColorsChanged();
  1007. }
  1008. }
  1009. //---------------------------------------------------------------------------
  1010. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1011. {
  1012. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1013. DirView->Invalidate();
  1014. }
  1015. //---------------------------------------------------------------------------
  1016. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1017. {
  1018. DoFileColorsChanged(RemoteDirView);
  1019. }
  1020. //---------------------------------------------------------------------------
  1021. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1022. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1023. void * /*Arg*/)
  1024. {
  1025. if (Token != NULL)
  1026. {
  1027. TForm * Editor = dynamic_cast<TForm*>(Token);
  1028. DebugAssert(Editor != NULL);
  1029. ReconfigureEditorForm(Editor);
  1030. }
  1031. }
  1032. //---------------------------------------------------------------------------
  1033. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1034. {
  1035. WinConfiguration->DDTransferConfirmation = asOn;
  1036. }
  1037. //---------------------------------------------------------------------------
  1038. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1039. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1040. {
  1041. // noop
  1042. }
  1043. //---------------------------------------------------------------------------
  1044. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1045. {
  1046. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1047. }
  1048. //---------------------------------------------------------------------------
  1049. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1050. {
  1051. if (DoNotShowAgain)
  1052. {
  1053. if (DragDrop)
  1054. {
  1055. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1056. {
  1057. PopupTrayBalloon(
  1058. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1059. }
  1060. WinConfiguration->DDTransferConfirmation = asOff;
  1061. }
  1062. else
  1063. {
  1064. WinConfiguration->ConfirmTransferring = false;
  1065. }
  1066. }
  1067. else
  1068. {
  1069. // User explicitly unchecked "do not show again",
  1070. // so show him the dialog the next time
  1071. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1072. {
  1073. WinConfiguration->DDTransferConfirmation = asOn;
  1074. }
  1075. }
  1076. }
  1077. //---------------------------------------------------------------------------
  1078. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1079. TTransferDirection Direction, TTransferType Type, bool Temp,
  1080. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1081. bool Confirm, bool DragDrop, int Options)
  1082. {
  1083. bool Result = true;
  1084. DebugAssert(HasActiveTerminal());
  1085. // these parameters are known in advance
  1086. int Params =
  1087. FLAGMASK(Type == ttMove, cpDelete);
  1088. bool ToTemp = (Temp && (Direction == tdToLocal));
  1089. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1090. {
  1091. bool DisableNewerOnly =
  1092. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1093. ToTemp;
  1094. Options |=
  1095. FLAGMASK(ToTemp, coTemp) |
  1096. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1097. coDoNotShowAgain;
  1098. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1099. int CopyParamAttrs = (Direction == tdToRemote ?
  1100. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1101. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1102. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1103. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1104. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1105. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1106. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1107. if (Result)
  1108. {
  1109. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1110. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1111. }
  1112. }
  1113. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1114. {
  1115. Configuration->Usage->Inc(L"TransfersOnBackground");
  1116. // these parameter are known only after transfer dialog
  1117. Params |=
  1118. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1119. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1120. Result = false;
  1121. ClearTransferSourceSelection(Direction);
  1122. }
  1123. return Result;
  1124. }
  1125. //---------------------------------------------------------------------------
  1126. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1127. {
  1128. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1129. if (FOnFileOperationFinished != NULL)
  1130. {
  1131. FOnFileOperationFinished(Side, UnicodeString(), true);
  1132. }
  1133. else
  1134. {
  1135. if (HasDirView[Side])
  1136. {
  1137. DirView(Side)->SelectAll(smNone);
  1138. }
  1139. }
  1140. }
  1141. //---------------------------------------------------------------------------
  1142. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1143. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1144. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1145. int Params)
  1146. {
  1147. DebugAssert(Queue != NULL);
  1148. bool SingleFile = false;
  1149. if (FileList->Count == 1)
  1150. {
  1151. if (Direction == tdToRemote)
  1152. {
  1153. UnicodeString FileName = FileList->Strings[0];
  1154. SingleFile = FileExists(ApiPath(FileName));
  1155. }
  1156. else
  1157. {
  1158. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1159. SingleFile = !File->IsDirectory;
  1160. }
  1161. }
  1162. TQueueItem * QueueItem;
  1163. if (Direction == tdToRemote)
  1164. {
  1165. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1166. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1167. }
  1168. else
  1169. {
  1170. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1171. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1172. }
  1173. AddQueueItem(Queue, QueueItem, Terminal);
  1174. }
  1175. //---------------------------------------------------------------------------
  1176. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1177. {
  1178. if (Queue->IsEmpty)
  1179. {
  1180. Terminal->QueueOperationStart = Now();
  1181. }
  1182. Queue->AddItem(QueueItem);
  1183. }
  1184. //---------------------------------------------------------------------------
  1185. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1186. {
  1187. }
  1188. //---------------------------------------------------------------------------
  1189. void __fastcall TCustomScpExplorerForm::InitControls()
  1190. {
  1191. DebugAssert(FDocks != NULL);
  1192. for (int Index = 0; Index < ComponentCount; Index++)
  1193. {
  1194. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1195. if ((Dock != NULL) && (Dock->Tag == 0))
  1196. {
  1197. FDocks->Add(Dock);
  1198. }
  1199. }
  1200. CollectItemsWithTextDisplayMode(this);
  1201. }
  1202. //---------------------------------------------------------------------------
  1203. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1204. {
  1205. ConfigurationChanged();
  1206. QueuePanel->Height =
  1207. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1208. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1209. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1210. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1211. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1212. QueueFileList->Height =
  1213. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1214. if (QueueFileList->Visible)
  1215. {
  1216. AdjustQueueLayout();
  1217. }
  1218. UpdateCustomCommandsToolbar();
  1219. }
  1220. //---------------------------------------------------------------------------
  1221. void __fastcall TCustomScpExplorerForm::StoreParams()
  1222. {
  1223. int APixelsPerInch = GetControlPixelsPerInch(this);
  1224. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1225. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1226. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1227. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1228. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1229. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1230. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1231. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1232. }
  1233. //---------------------------------------------------------------------------
  1234. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1235. {
  1236. if (!FFormRestored)
  1237. {
  1238. FFormRestored = true;
  1239. RestoreFormParams();
  1240. }
  1241. TForm::CreateParams(Params);
  1242. }
  1243. //---------------------------------------------------------------------------
  1244. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1245. {
  1246. DebugAssert(FDocks != NULL);
  1247. for (int Index = 0; Index < FDocks->Count; Index++)
  1248. {
  1249. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1250. }
  1251. }
  1252. //---------------------------------------------------------------------------
  1253. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1254. {
  1255. SetDockAllowDrag(true);
  1256. while (!ButtonsStr.IsEmpty())
  1257. {
  1258. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1259. UnicodeString Name = CutToChar(S, L'=', true);
  1260. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1261. {
  1262. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1263. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1264. {
  1265. while (!S.IsEmpty())
  1266. {
  1267. UnicodeString S2 = CutToChar(S, L',', true);
  1268. UnicodeString Name = CutToChar(S2, L':', true);
  1269. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1270. {
  1271. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1272. if (SameText(GetToolbarItemName(Item), Name))
  1273. {
  1274. // forward compatibility
  1275. UnicodeString S3 = CutToChar(S2, L':', true);
  1276. bool Visible = StrToIntDef(S3, 0);
  1277. Item->Visible = Visible;
  1278. }
  1279. }
  1280. }
  1281. }
  1282. }
  1283. }
  1284. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1285. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1286. }
  1287. //---------------------------------------------------------------------------
  1288. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1289. {
  1290. UnicodeString Result;
  1291. SetDockAllowDrag(true);
  1292. Result = ::GetToolbarsLayoutStr(this);
  1293. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1294. return Result;
  1295. }
  1296. //---------------------------------------------------------------------------
  1297. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1298. {
  1299. UnicodeString Result;
  1300. if (Item->Action != NULL)
  1301. {
  1302. Result = Item->Action->Name;
  1303. Result = RemoveSuffix(Result, L"Action", true);
  1304. }
  1305. else
  1306. {
  1307. Result = Item->Name;
  1308. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1309. Result = RemoveSuffix(Result, L"Item", true);
  1310. }
  1311. return Result;
  1312. }
  1313. //---------------------------------------------------------------------------
  1314. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1315. {
  1316. UnicodeString Result;
  1317. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1318. {
  1319. UnicodeString ToolbarStr;
  1320. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1321. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1322. {
  1323. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1324. {
  1325. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1326. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1327. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1328. // and save the changes here only.
  1329. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1330. {
  1331. UnicodeString Name = GetToolbarItemName(Item);
  1332. DebugAssert(Name.Pos(L"TBX") == 0);
  1333. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1334. }
  1335. }
  1336. }
  1337. if (!ToolbarStr.IsEmpty())
  1338. {
  1339. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1340. }
  1341. }
  1342. return Result;
  1343. }
  1344. //---------------------------------------------------------------------------
  1345. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1346. {
  1347. DebugAssert(FProgressForm == NULL);
  1348. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1349. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1350. FProgressForm->DeleteLocalToRecycleBin =
  1351. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1352. FProgressForm->DeleteRemoteToRecycleBin =
  1353. HasActiveTerminal() &&
  1354. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1355. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1356. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1357. LockWindow();
  1358. // Actually, do not know what hides the progress once the operation finishes
  1359. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1360. SetTaskbarListProgressState(TBPF_NORMAL);
  1361. }
  1362. //---------------------------------------------------------------------------
  1363. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1364. {
  1365. UnlockWindow();
  1366. SAFE_DESTROY(FProgressForm);
  1367. SetQueueProgress();
  1368. }
  1369. //---------------------------------------------------------------------------
  1370. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1371. TFileOperationProgressType & ProgressData)
  1372. {
  1373. // Download to temporary local directory
  1374. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1375. {
  1376. bool Continue = true;
  1377. FPendingTempSpaceWarn = false;
  1378. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1379. if (!Continue)
  1380. {
  1381. Abort();
  1382. }
  1383. }
  1384. // operation is being executed and we still didn't show up progress form
  1385. if (ProgressData.InProgress && !FProgressForm)
  1386. {
  1387. CreateProgressForm(NULL);
  1388. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1389. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1390. }
  1391. // operation is finished (or terminated), so we hide progress form
  1392. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1393. {
  1394. DestroyProgressForm();
  1395. if (ProgressData.Operation == foCalculateSize)
  1396. {
  1397. // When calculating size before transfer, the abort caused by
  1398. // cancel flag set due to "MoveToQueue" below,
  1399. // is not propagated back to us in ExecuteFileOperation,
  1400. // as it is silently swallowed.
  1401. // So we explicitly (re)throw it here.
  1402. if (FMoveToQueue)
  1403. {
  1404. Abort();
  1405. }
  1406. }
  1407. else
  1408. {
  1409. if ((ProgressData.Cancel == csContinue) &&
  1410. !FAutoOperation)
  1411. {
  1412. OperationComplete(ProgressData.StartTime);
  1413. }
  1414. }
  1415. }
  1416. if (FProgressForm &&
  1417. // Can happen only during synchronization, when ending one of the operations
  1418. ProgressData.InProgress)
  1419. {
  1420. FProgressForm->SetProgressData(ProgressData);
  1421. DebugAssert(ProgressData.InProgress);
  1422. SetTaskbarListProgressValue(&ProgressData);
  1423. if (FProgressForm->Cancel > csContinue)
  1424. {
  1425. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1426. if (FProgressForm->Cancel == csCancelFile)
  1427. {
  1428. FProgressForm->ClearCancel();
  1429. }
  1430. // cancel cancels even the move
  1431. FMoveToQueue = false;
  1432. }
  1433. else if (FProgressForm->MoveToQueue)
  1434. {
  1435. FMoveToQueue = true;
  1436. ProgressData.SetCancelAtLeast(csCancel);
  1437. }
  1438. if ((FTransferResumeList != NULL) &&
  1439. ProgressData.InProgress &&
  1440. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1441. !ProgressData.FullFileName.IsEmpty())
  1442. {
  1443. if ((FTransferResumeList->Count == 0) ||
  1444. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1445. {
  1446. // note that we do not recognize directories from files here
  1447. FTransferResumeList->Add(ProgressData.FullFileName);
  1448. }
  1449. }
  1450. }
  1451. }
  1452. //---------------------------------------------------------------------------
  1453. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1454. const TDateTime & StartTime)
  1455. {
  1456. if (GUIConfiguration->BeepOnFinish &&
  1457. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1458. {
  1459. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1460. DWORD Sound;
  1461. if (!ExtractFileExt(BeepSound).IsEmpty())
  1462. {
  1463. Sound = SND_FILENAME;
  1464. }
  1465. else
  1466. {
  1467. Sound = SND_ALIAS;
  1468. }
  1469. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1470. }
  1471. }
  1472. //---------------------------------------------------------------------------
  1473. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1474. TFileOperationProgressType & ProgressData)
  1475. {
  1476. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1477. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1478. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1479. FileOperationProgress(ProgressData);
  1480. }
  1481. //---------------------------------------------------------------------------
  1482. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1483. {
  1484. UnicodeString Result;
  1485. if (FProgressForm != NULL)
  1486. {
  1487. Result = FProgressForm->ProgressStr();
  1488. }
  1489. return Result;
  1490. }
  1491. //---------------------------------------------------------------------------
  1492. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1493. bool DragDrop)
  1494. {
  1495. return (!DragDrop && (DropSourceControl == NULL)) ||
  1496. (DropSourceControl == DirView(osOther));
  1497. }
  1498. //---------------------------------------------------------------------------
  1499. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1500. {
  1501. TCustomDirView * ADirView = DirView(Side);
  1502. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1503. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1504. // this can happen when local drive is unplugged in the middle of the operation
  1505. if (Item != NULL)
  1506. {
  1507. if (Unselect)
  1508. {
  1509. Item->Selected = false;
  1510. }
  1511. ADirView->MakeProgressVisible(Item);
  1512. }
  1513. return Item;
  1514. }
  1515. //---------------------------------------------------------------------------
  1516. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1517. TFileOperation Operation, TOperationSide Side,
  1518. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1519. TOnceDoneOperation & OnceDoneOperation)
  1520. {
  1521. if (!FAutoOperation)
  1522. {
  1523. // no selection on "/upload", form serves only as event handler
  1524. // (it is not displayed)
  1525. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1526. ((Operation == FPrimaryOperation) ||
  1527. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1528. {
  1529. TOperationSide DViewSide;
  1530. if ((FProgressSide != osCurrent) &&
  1531. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1532. {
  1533. // assume the operation is over the focused panel
  1534. DViewSide = FProgressSide;
  1535. }
  1536. else
  1537. {
  1538. DViewSide = Side;
  1539. }
  1540. if (FOnFileOperationFinished != NULL)
  1541. {
  1542. FOnFileOperationFinished(DViewSide, FileName, Success);
  1543. }
  1544. else
  1545. {
  1546. VisualiseOperationFinished(DViewSide, FileName, Success);
  1547. }
  1548. }
  1549. if ((Operation == foCopy) || (Operation == foMove))
  1550. {
  1551. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1552. {
  1553. if (Side == osLocal)
  1554. {
  1555. Configuration->Usage->Inc(L"UploadedFiles");
  1556. }
  1557. else
  1558. {
  1559. Configuration->Usage->Inc(L"DownloadedFiles");
  1560. }
  1561. }
  1562. }
  1563. }
  1564. if (Success && (FSynchronizeController != NULL))
  1565. {
  1566. DebugAssert(!IsSideLocalBrowser(osRemote));
  1567. if (Operation == foCopy)
  1568. {
  1569. DebugAssert(Side == osLocal);
  1570. FSynchronizeController->LogOperation(soUpload, FileName);
  1571. }
  1572. else if (Operation == foDelete)
  1573. {
  1574. DebugAssert(Side == osRemote);
  1575. FSynchronizeController->LogOperation(soDelete, FileName);
  1576. }
  1577. }
  1578. if (FProgressForm)
  1579. {
  1580. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1581. }
  1582. }
  1583. //---------------------------------------------------------------------------
  1584. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1585. TFileOperation Operation, TOperationSide Side,
  1586. bool Temp, const UnicodeString & FileName, Boolean Success,
  1587. TOnceDoneOperation & OnceDoneOperation)
  1588. {
  1589. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1590. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1591. OnceDoneOperation);
  1592. }
  1593. //---------------------------------------------------------------------------
  1594. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1595. {
  1596. return false;
  1597. }
  1598. //---------------------------------------------------------------------------
  1599. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1600. {
  1601. return false;
  1602. }
  1603. //---------------------------------------------------------------------------
  1604. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1605. {
  1606. return false;
  1607. }
  1608. //---------------------------------------------------------------------------
  1609. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1610. {
  1611. return NULL;
  1612. }
  1613. //---------------------------------------------------------------------------
  1614. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1615. {
  1616. DebugAssert(GetSide(Side) == osRemote);
  1617. DebugUsedParam(Side);
  1618. return RemoteDirView;
  1619. }
  1620. //---------------------------------------------------------------------------
  1621. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1622. {
  1623. DebugAssert(GetSide(Side) == osRemote);
  1624. DebugUsedParam(Side);
  1625. return RemoteDriveView;
  1626. }
  1627. //---------------------------------------------------------------------------
  1628. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1629. {
  1630. DebugAssert(GetSide(Side) == osRemote);
  1631. DebugUsedParam(Side);
  1632. return HasActiveTerminal();
  1633. }
  1634. //---------------------------------------------------------------------------
  1635. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1636. TOperationSide Side, int FilesOnly)
  1637. {
  1638. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1639. }
  1640. //---------------------------------------------------------------------------
  1641. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1642. TOperationSide Side, int FilesOnly)
  1643. {
  1644. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1645. }
  1646. //---------------------------------------------------------------------------
  1647. struct THistoryItemData
  1648. {
  1649. short int Side;
  1650. short int Index;
  1651. };
  1652. //---------------------------------------------------------------------------
  1653. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1654. {
  1655. DirView(Side)->HistoryGo(Index);
  1656. }
  1657. //---------------------------------------------------------------------------
  1658. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1659. {
  1660. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1661. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1662. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1663. }
  1664. //---------------------------------------------------------------------------
  1665. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1666. bool Back)
  1667. {
  1668. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1669. {
  1670. TCustomDirView * DView = DirView(Side);
  1671. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1672. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1673. if (ICount > 10)
  1674. {
  1675. ICount = 10;
  1676. }
  1677. Menu->Items->Clear();
  1678. THistoryItemData Data;
  1679. Data.Side = Side;
  1680. for (short int i = 1; i <= ICount; i++)
  1681. {
  1682. TTBCustomItem * Item = new TTBXItem(Menu);
  1683. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1684. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1685. Item->Hint = DView->HistoryPath[Data.Index];
  1686. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1687. Item->Tag = *reinterpret_cast<int*>(&Data);
  1688. Item->OnClick = HistoryItemClick;
  1689. Menu->Items->Add(Item);
  1690. }
  1691. }
  1692. }
  1693. //---------------------------------------------------------------------------
  1694. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1695. TOperationSide Side, bool Back)
  1696. {
  1697. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1698. {
  1699. // workaround
  1700. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1701. // operator new allocates memory in Pascal code, but calls inline
  1702. // contructor in C++, leading in problems, the function does
  1703. // both in Pascal code
  1704. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1705. UpdateHistoryMenu(Side, Back);
  1706. }
  1707. return FHistoryMenu[Side == osLocal][Back];
  1708. }
  1709. //---------------------------------------------------------------------------
  1710. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1711. TCustomDirView *Sender)
  1712. {
  1713. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1714. UpdateHistoryMenu(Side, true);
  1715. UpdateHistoryMenu(Side, false);
  1716. }
  1717. //---------------------------------------------------------------------------
  1718. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1719. {
  1720. // remote custom commands can be executed only if the server supports shell commands
  1721. // or have secondary shell
  1722. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1723. }
  1724. //---------------------------------------------------------------------------
  1725. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1726. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1727. {
  1728. // -1 = hidden, 0 = disabled, 1 = enabled
  1729. int Result;
  1730. TFileCustomCommand RemoteCustomCommand;
  1731. TLocalCustomCommand LocalCustomCommand;
  1732. TFileCustomCommand * NonInteractiveCustomCommand =
  1733. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1734. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1735. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1736. if (FLAGCLEAR(Command.Params, ccLocal))
  1737. {
  1738. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1739. // custom command that does not operate with files can be executed anytime ...
  1740. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1741. {
  1742. // ... but do not show such command in remote file menu
  1743. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1744. {
  1745. Result = AllowedState;
  1746. }
  1747. else
  1748. {
  1749. Result = -1;
  1750. }
  1751. }
  1752. else
  1753. {
  1754. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1755. {
  1756. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1757. }
  1758. else
  1759. {
  1760. Result = -1;
  1761. }
  1762. }
  1763. }
  1764. else
  1765. {
  1766. // custom command that does not operate with files can be executed anytime
  1767. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1768. {
  1769. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1770. }
  1771. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1772. {
  1773. if ((ListType == ccltAll) || (ListType == ccltFile))
  1774. {
  1775. // special case is "diff"-style command that can be executed over any side,
  1776. // if we have both sides
  1777. Result =
  1778. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1779. // directly (instead of EnableSelectedOperation) to pass
  1780. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1781. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1782. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1783. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1784. }
  1785. else if (ListType == ccltBoth)
  1786. {
  1787. DebugAssert(!IsLocalBrowserMode());
  1788. Result = 1;
  1789. }
  1790. else
  1791. {
  1792. Result = -1;
  1793. }
  1794. }
  1795. else
  1796. {
  1797. if ((ListType == ccltAll) ||
  1798. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1799. {
  1800. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1801. }
  1802. else
  1803. {
  1804. Result = -1;
  1805. }
  1806. }
  1807. if ((Result > 0) &&
  1808. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1809. !HasActiveTerminal())
  1810. {
  1811. Result = 0;
  1812. }
  1813. }
  1814. return Result;
  1815. }
  1816. //---------------------------------------------------------------------------
  1817. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1818. TStrings * FileList, const TCustomCommandType & ACommand,
  1819. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1820. {
  1821. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1822. {
  1823. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1824. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1825. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1826. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1827. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1828. bool Capture =
  1829. FLAGSET(ACommand.Params, ccShowResults) ||
  1830. FLAGSET(ACommand.Params, ccCopyResults) ||
  1831. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1832. TCaptureOutputEvent OutputEvent = NULL;
  1833. DebugAssert(FCapturedLog == NULL);
  1834. if (Capture)
  1835. {
  1836. FCapturedLog = new TStringList();
  1837. OutputEvent = TerminalCaptureLog;
  1838. }
  1839. try
  1840. {
  1841. if (!RemoteCustomCommand.IsFileCommand(Command))
  1842. {
  1843. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1844. OutputEvent);
  1845. }
  1846. else
  1847. {
  1848. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1849. }
  1850. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1851. {
  1852. if (FLAGSET(ACommand.Params, ccCopyResults))
  1853. {
  1854. CopyToClipboard(FCapturedLog);
  1855. }
  1856. if (FLAGSET(ACommand.Params, ccShowResults))
  1857. {
  1858. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1859. }
  1860. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1861. {
  1862. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1863. }
  1864. }
  1865. }
  1866. __finally
  1867. {
  1868. SAFE_DESTROY(FCapturedLog);
  1869. }
  1870. }
  1871. }
  1872. //---------------------------------------------------------------------------
  1873. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1874. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1875. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1876. {
  1877. DebugAssert(!IsLocalBrowserMode());
  1878. TStrings * LocalFileList = NULL;
  1879. TStrings * RemoteFileList = NULL;
  1880. TStrings * RemoteFileListFull = NULL;
  1881. try
  1882. {
  1883. if (LocalFileCommand)
  1884. {
  1885. if (ALocalFileList == NULL)
  1886. {
  1887. DebugAssert(HasDirView[osLocal]);
  1888. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1889. // directly (instead of EnableSelectedOperation) to pass
  1890. // false to FocusedFileOnlyWhenFocused
  1891. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1892. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1893. }
  1894. else
  1895. {
  1896. LocalFileList = ALocalFileList;
  1897. }
  1898. if (FileListCommand)
  1899. {
  1900. if (LocalFileList->Count != 1)
  1901. {
  1902. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1903. }
  1904. }
  1905. else
  1906. {
  1907. if ((LocalFileList->Count != 1) &&
  1908. (FileList->Count != 1) &&
  1909. (LocalFileList->Count != FileList->Count))
  1910. {
  1911. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1912. }
  1913. }
  1914. }
  1915. UnicodeString RootTempDir;
  1916. UnicodeString TempDir;
  1917. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1918. if (!RemoteFiles)
  1919. {
  1920. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1921. }
  1922. try
  1923. {
  1924. TDateTimes RemoteFileTimes;
  1925. if (RemoteFiles)
  1926. {
  1927. RemoteFileList = FileList;
  1928. }
  1929. else
  1930. {
  1931. RemoteFileList = new TStringList();
  1932. TMakeLocalFileListParams MakeFileListParam;
  1933. MakeFileListParam.FileList = RemoteFileList;
  1934. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1935. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1936. MakeFileListParam.Recursive =
  1937. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1938. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1939. if (!MakeFileListParam.Recursive)
  1940. {
  1941. RemoteFileTimes.clear();
  1942. RemoteFileListFull = new TStringList();
  1943. MakeFileListParam.FileList = RemoteFileListFull;
  1944. MakeFileListParam.Recursive = true;
  1945. MakeFileListParam.IncludeDirs = false;
  1946. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1947. }
  1948. else
  1949. {
  1950. RemoteFileListFull = RemoteFileList;
  1951. }
  1952. }
  1953. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1954. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1955. if (!NonBlocking)
  1956. {
  1957. int Count;
  1958. if (FileListCommand)
  1959. {
  1960. Count = 1;
  1961. }
  1962. else if (LocalFileCommand)
  1963. {
  1964. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1965. }
  1966. else
  1967. {
  1968. Count = RemoteFileList->Count;
  1969. }
  1970. Progress.Start(foCustomCommand, osRemote, Count);
  1971. DebugAssert(FProgressForm != NULL);
  1972. FProgressForm->ReadOnly = true;
  1973. }
  1974. try
  1975. {
  1976. if (FileListCommand)
  1977. {
  1978. UnicodeString LocalFile;
  1979. // MakeFileList does not delimit filenames
  1980. UnicodeString FileList = MakeFileList(RemoteFileList);
  1981. if (LocalFileCommand)
  1982. {
  1983. DebugAssert(LocalFileList->Count == 1);
  1984. LocalFile = LocalFileList->Strings[0];
  1985. }
  1986. TLocalCustomCommand CustomCommand(Data,
  1987. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1988. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1989. if (NonBlocking)
  1990. {
  1991. DebugAssert(!POutput);
  1992. ExecuteShellChecked(ShellCommand);
  1993. }
  1994. else
  1995. {
  1996. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1997. }
  1998. }
  1999. else if (LocalFileCommand)
  2000. {
  2001. if (LocalFileList->Count == 1)
  2002. {
  2003. UnicodeString LocalFile = LocalFileList->Strings[0];
  2004. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2005. {
  2006. UnicodeString FileName = RemoteFileList->Strings[Index];
  2007. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2008. Progress.SetFile(FileNameForProgress);
  2009. TLocalCustomCommand CustomCommand(Data,
  2010. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2011. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2012. TOnceDoneOperation OnceDoneOperation;
  2013. // Do not unselect anything, as it won't work with two panels anyway
  2014. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2015. }
  2016. }
  2017. else if (RemoteFileList->Count == 1)
  2018. {
  2019. UnicodeString FileName = RemoteFileList->Strings[0];
  2020. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2021. {
  2022. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2023. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2024. Progress.SetFile(LocalFileNameOnly);
  2025. TLocalCustomCommand CustomCommand(
  2026. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2027. FileName, LocalFileName, L"");
  2028. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2029. TOnceDoneOperation OnceDoneOperation;
  2030. // Do not unselect anything, as it won't work with two panels anyway
  2031. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2032. }
  2033. }
  2034. else
  2035. {
  2036. if (LocalFileList->Count != RemoteFileList->Count)
  2037. {
  2038. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2039. }
  2040. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2041. {
  2042. UnicodeString FileName = RemoteFileList->Strings[Index];
  2043. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2044. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2045. Progress.SetFile(LocalFileNameOnly); // sic
  2046. TLocalCustomCommand CustomCommand(
  2047. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2048. FileName, LocalFileName, L"");
  2049. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2050. TOnceDoneOperation OnceDoneOperation;
  2051. // Do not unselect anything, as it won't work with two panels anyway
  2052. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2053. }
  2054. }
  2055. }
  2056. else
  2057. {
  2058. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2059. {
  2060. UnicodeString FileName = RemoteFileList->Strings[Index];
  2061. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2062. Progress.SetFile(FileNameForProgress);
  2063. TLocalCustomCommand CustomCommand(Data,
  2064. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2065. FileName, L"", L"");
  2066. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2067. TOnceDoneOperation OnceDoneOperation;
  2068. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2069. }
  2070. }
  2071. }
  2072. __finally
  2073. {
  2074. if (!NonBlocking)
  2075. {
  2076. Progress.Stop();
  2077. }
  2078. }
  2079. DebugAssert(!FAutoOperation);
  2080. if (!RemoteFiles)
  2081. {
  2082. TempDir = IncludeTrailingBackslash(TempDir);
  2083. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2084. {
  2085. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2086. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2087. // Skip directories, process only nested files.
  2088. // The check is redundant as FileAge fails for directories anyway.
  2089. !DirectoryExists(FileName))
  2090. {
  2091. UnicodeString RemoteDir =
  2092. UnixExtractFileDir(
  2093. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2094. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2095. TDateTime NewTime;
  2096. if (FileAge(FileName, NewTime) &&
  2097. (NewTime != RemoteFileTimes[Index]))
  2098. {
  2099. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2100. TemporaryFileCopyParam(CopyParam);
  2101. CopyParam.FileMask = L"";
  2102. FAutoOperation = true;
  2103. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2104. TemporaryFilesList->Add(FileName);
  2105. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2106. }
  2107. }
  2108. }
  2109. }
  2110. }
  2111. __finally
  2112. {
  2113. FAutoOperation = false;
  2114. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2115. {
  2116. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2117. }
  2118. }
  2119. }
  2120. __finally
  2121. {
  2122. if (RemoteFileList != FileList)
  2123. {
  2124. delete RemoteFileList;
  2125. }
  2126. if (LocalFileList != ALocalFileList)
  2127. {
  2128. delete LocalFileList;
  2129. }
  2130. if (RemoteFileListFull != RemoteFileList)
  2131. {
  2132. delete RemoteFileListFull;
  2133. }
  2134. }
  2135. }
  2136. //---------------------------------------------------------------------------
  2137. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2138. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2139. bool FileListCommand, UnicodeString * POutput)
  2140. {
  2141. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2142. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2143. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2144. {
  2145. UnicodeString FileName = SelectedFileList->Strings[Index];
  2146. if (DirectoryExists(FileName))
  2147. {
  2148. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2149. {
  2150. LocalFileList->Add(FileName);
  2151. }
  2152. if (FLAGSET(ACommand.Params, ccRecursive))
  2153. {
  2154. TMakeLocalFileListParams MakeFileListParam;
  2155. MakeFileListParam.FileList = LocalFileList.get();
  2156. MakeFileListParam.FileTimes = NULL;
  2157. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2158. MakeFileListParam.Recursive = true;
  2159. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2160. }
  2161. }
  2162. else
  2163. {
  2164. LocalFileList->Add(FileName);
  2165. }
  2166. }
  2167. UnicodeString RemotePath;
  2168. if (Terminal != NULL)
  2169. {
  2170. RemotePath = Terminal->CurrentDirectory;
  2171. }
  2172. if (FileListCommand)
  2173. {
  2174. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2175. TLocalCustomCommand CustomCommand(
  2176. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2177. L"", L"", FileList);
  2178. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2179. }
  2180. else
  2181. {
  2182. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2183. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2184. DebugAssert(FProgressForm != NULL);
  2185. FProgressForm->ReadOnly = true;
  2186. try
  2187. {
  2188. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2189. {
  2190. UnicodeString FileName = LocalFileList->Strings[Index];
  2191. Progress.SetFile(FileName);
  2192. TLocalCustomCommand CustomCommand(
  2193. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2194. FileName, L"", L"");
  2195. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2196. TOnceDoneOperation OnceDoneOperation;
  2197. Progress.Finish(FileName, true, OnceDoneOperation);
  2198. }
  2199. }
  2200. __finally
  2201. {
  2202. Progress.Stop();
  2203. }
  2204. }
  2205. }
  2206. //---------------------------------------------------------------------------
  2207. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2208. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2209. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2210. {
  2211. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2212. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2213. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2214. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2215. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2216. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2217. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2218. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2219. std::unique_ptr<UnicodeString> POutput;
  2220. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2221. FLAGSET(ACommand.Params, ccCopyResults))
  2222. {
  2223. POutput.reset(new UnicodeString());
  2224. }
  2225. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2226. FProgressSide = FCurrentSide;
  2227. if (!LocalCustomCommand.IsFileCommand(Command))
  2228. {
  2229. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2230. }
  2231. // remote files?
  2232. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2233. {
  2234. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2235. }
  2236. // local files
  2237. else
  2238. {
  2239. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2240. }
  2241. if (POutput.get() != NULL)
  2242. {
  2243. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2244. int P = POutput->Pos(sLineBreak);
  2245. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2246. {
  2247. POutput->SetLength(P - 1);
  2248. }
  2249. // Copy even empty output to clipboard
  2250. if (FLAGSET(ACommand.Params, ccCopyResults))
  2251. {
  2252. CopyToClipboard(*POutput);
  2253. }
  2254. // But do not show an empty message box.
  2255. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2256. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2257. !POutput->IsEmpty())
  2258. {
  2259. TClipboardHandler ClipboardHandler;
  2260. ClipboardHandler.Text = *POutput;
  2261. TMessageParams Params;
  2262. TQueryButtonAlias Aliases[1];
  2263. Aliases[0].Button = qaRetry;
  2264. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2265. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2266. Params.Aliases = Aliases;
  2267. Params.AliasesCount = LENOF(Aliases);
  2268. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2269. }
  2270. }
  2271. }
  2272. //---------------------------------------------------------------------------
  2273. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2274. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2275. {
  2276. TCustomCommandData Data;
  2277. UnicodeString Site;
  2278. UnicodeString RemotePath;
  2279. if (HasActiveTerminal())
  2280. {
  2281. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2282. Data = TCustomCommandData(SessionData.get());
  2283. Site = Terminal->SessionData->SessionKey;
  2284. RemotePath = Terminal->CurrentDirectory;
  2285. }
  2286. UnicodeString HelpKeyword = ACommand.HomePage;
  2287. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2288. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2289. {
  2290. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2291. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2292. {
  2293. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2294. }
  2295. else
  2296. {
  2297. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2298. }
  2299. if (!DoCustomCommandOptionsDialog(
  2300. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2301. {
  2302. Abort();
  2303. }
  2304. }
  2305. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2306. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2307. {
  2308. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2309. }
  2310. else
  2311. {
  2312. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2313. }
  2314. }
  2315. //---------------------------------------------------------------------------
  2316. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2317. const TCustomCommandType & Command)
  2318. {
  2319. DebugAssert(FCustomCommandLocalFileList != NULL);
  2320. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2321. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2322. TStrings * LocalFileList = new TStringList();
  2323. TStrings * RemoteFileList = new TStringList();
  2324. try
  2325. {
  2326. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2327. {
  2328. LocalFileList->Clear();
  2329. LocalFileList->AddObject(
  2330. FCustomCommandLocalFileList->Strings[Index],
  2331. FCustomCommandLocalFileList->Objects[Index]);
  2332. RemoteFileList->Clear();
  2333. RemoteFileList->AddObject(
  2334. FCustomCommandRemoteFileList->Strings[Index],
  2335. FCustomCommandRemoteFileList->Objects[Index]);
  2336. CustomCommand(RemoteFileList, Command, LocalFileList);
  2337. }
  2338. }
  2339. __finally
  2340. {
  2341. delete LocalFileList;
  2342. delete RemoteFileList;
  2343. }
  2344. }
  2345. //---------------------------------------------------------------------------
  2346. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2347. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2348. {
  2349. delete FCustomCommandLocalFileList;
  2350. delete FCustomCommandRemoteFileList;
  2351. // takeover ownership,
  2352. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2353. FCustomCommandLocalFileList = LocalFileList;
  2354. FCustomCommandRemoteFileList = RemoteFileList;
  2355. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2356. if (Button != NULL)
  2357. {
  2358. FCustomCommandMenu->Items->Clear();
  2359. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2360. MenuPopup(FCustomCommandMenu, Button);
  2361. }
  2362. else
  2363. {
  2364. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2365. }
  2366. }
  2367. //---------------------------------------------------------------------------
  2368. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2369. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2370. {
  2371. DebugAssert(FCapturedLog != NULL);
  2372. if ((OutputType == cotOutput) || (OutputType == cotError))
  2373. {
  2374. FCapturedLog->Add(AddedLine);
  2375. }
  2376. }
  2377. //---------------------------------------------------------------------------
  2378. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2379. TOperationSide Side)
  2380. {
  2381. return
  2382. (Side == osOther) &&
  2383. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2384. }
  2385. //---------------------------------------------------------------------------
  2386. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2387. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2388. {
  2389. TTBItemOptions O;
  2390. O = Item->Options;
  2391. TCustomDirView * DView = DirView(Side);
  2392. if ((DView->ItemFocused != NULL) &&
  2393. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2394. // when resolving links is disabled, default action is to enter the directory,
  2395. // no matter what DoubleClickAction is configured to
  2396. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2397. // Can only Edit files, but can Open/Copy even directories
  2398. ((DoubleClickAction != dcaEdit) ||
  2399. !DView->ItemIsDirectory(DView->ItemFocused)))
  2400. {
  2401. Item->Options = O << tboDefault;
  2402. }
  2403. else
  2404. {
  2405. Item->Options = O >> tboDefault;
  2406. }
  2407. }
  2408. //---------------------------------------------------------------------------
  2409. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2410. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2411. {
  2412. TCustomDirView * DView = DirView(Side);
  2413. TListItem * Item = DView->ItemFocused;
  2414. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2415. EnableFocusedOperation[Side])
  2416. {
  2417. TPoint ClientPoint;
  2418. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2419. TPoint(0, 0) : MousePos;
  2420. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2421. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2422. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2423. }
  2424. }
  2425. //---------------------------------------------------------------------------
  2426. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2427. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2428. {
  2429. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2430. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2431. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2432. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2433. Handled = true;
  2434. }
  2435. //---------------------------------------------------------------------------
  2436. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2437. {
  2438. }
  2439. //---------------------------------------------------------------------------
  2440. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2441. {
  2442. DebugAssert(FErrorList == NULL);
  2443. if (WinConfiguration->ContinueOnError)
  2444. {
  2445. FErrorList = new TStringList();
  2446. Configuration->Usage->Inc(L"ContinuationsOnError");
  2447. }
  2448. NonVisualDataModule->StartBusy();
  2449. }
  2450. //---------------------------------------------------------------------------
  2451. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2452. {
  2453. NonVisualDataModule->EndBusy();
  2454. if (FErrorList)
  2455. {
  2456. HandleErrorList(FErrorList);
  2457. }
  2458. }
  2459. //---------------------------------------------------------------------------
  2460. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2461. const TCopyParamType & CopyParam)
  2462. {
  2463. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2464. {
  2465. Configuration->Usage->Inc(L"FileMaskUses");
  2466. }
  2467. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2468. {
  2469. Configuration->Usage->Inc(L"OperationMaskUses");
  2470. }
  2471. }
  2472. //---------------------------------------------------------------------------
  2473. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2474. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2475. {
  2476. DebugAssert(!IsLocalBrowserMode());
  2477. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2478. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2479. TTransferOperationParam DefaultParam;
  2480. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2481. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2482. switch (Param.Queue)
  2483. {
  2484. case asOn:
  2485. CopyParam.Queue = true;
  2486. break;
  2487. case asOff:
  2488. CopyParam.Queue = false;
  2489. break;
  2490. case asAuto:
  2491. default:
  2492. // keep default
  2493. break;
  2494. }
  2495. bool Result =
  2496. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2497. if (Result)
  2498. {
  2499. DebugAssert(Terminal);
  2500. bool SelectionRestored = false;
  2501. TCustomDirView * DView = NULL;
  2502. if (HasDirView[Side])
  2503. {
  2504. DView = DirView(Side);
  2505. DView->SaveSelection();
  2506. DView->SaveSelectedNames();
  2507. }
  2508. UpdateCopyParamCounters(CopyParam);
  2509. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2510. DebugAssert(FTransferResumeList == NULL);
  2511. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2512. FMoveToQueue = false;
  2513. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2514. try
  2515. {
  2516. TStrings * PermanentFileList;
  2517. std::unique_ptr<TStrings> PermanentFileListOwner;
  2518. try
  2519. {
  2520. if (Side == osLocal)
  2521. {
  2522. PermanentFileList = FileList;
  2523. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2524. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2525. if (Operation == foMove)
  2526. {
  2527. ReloadLocalDirectory();
  2528. if (DView != NULL)
  2529. {
  2530. DView->RestoreSelection();
  2531. }
  2532. SelectionRestored = true;
  2533. }
  2534. }
  2535. else
  2536. {
  2537. // Clone the file list as it may refer to current directory files,
  2538. // which get destroyed, when the source directory is reloaded after foMove operation.
  2539. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2540. // But for a hotfix, we are not going to do such a big change.
  2541. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2542. PermanentFileList = PermanentFileListOwner.get();
  2543. try
  2544. {
  2545. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2546. }
  2547. __finally
  2548. {
  2549. if (Operation == foMove)
  2550. {
  2551. if (DView != NULL)
  2552. {
  2553. DView->RestoreSelection();
  2554. }
  2555. SelectionRestored = true;
  2556. }
  2557. ReloadLocalDirectory(Param.TargetDirectory);
  2558. }
  2559. }
  2560. }
  2561. catch (EAbort &)
  2562. {
  2563. if (FMoveToQueue)
  2564. {
  2565. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2566. DebugAssert(CopyParam.TransferSkipList == NULL);
  2567. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2568. if (TransferResumeList->Count > 0)
  2569. {
  2570. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2571. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2572. }
  2573. CopyParam.TransferSkipList = TransferResumeList.release();
  2574. // not really needed, just to keep it consistent with TransferResumeList
  2575. FTransferResumeList = NULL;
  2576. FMoveToQueue = false;
  2577. Configuration->Usage->Inc("MovesToBackground");
  2578. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2579. ClearTransferSourceSelection(Direction);
  2580. }
  2581. throw;
  2582. }
  2583. }
  2584. __finally
  2585. {
  2586. if (!SelectionRestored && (DView != NULL))
  2587. {
  2588. DView->DiscardSavedSelection();
  2589. }
  2590. FTransferResumeList = NULL;
  2591. }
  2592. }
  2593. return Result;
  2594. }
  2595. //---------------------------------------------------------------------------
  2596. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2597. TOperationSide Side, TStrings * FileList, void * Param)
  2598. {
  2599. DebugAssert(FileList->Count);
  2600. // We deliberately do not toggle alternative flag (Param), but use OR,
  2601. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2602. // shortcut of CurrentDeleteAlternativeAction
  2603. bool Alternative =
  2604. bool(Param) || UseAlternativeFunction();
  2605. bool Recycle;
  2606. if (IsSideLocalBrowser(Side))
  2607. {
  2608. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2609. }
  2610. else
  2611. {
  2612. Recycle =
  2613. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2614. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2615. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2616. }
  2617. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2618. if (!Result)
  2619. {
  2620. UnicodeString Query;
  2621. if (FileList->Count == 1)
  2622. {
  2623. if (IsSideLocalBrowser(Side))
  2624. {
  2625. Query = ExtractFileName(FileList->Strings[0]);
  2626. }
  2627. else
  2628. {
  2629. Query = UnixExtractFileName(FileList->Strings[0]);
  2630. }
  2631. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2632. }
  2633. else
  2634. {
  2635. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2636. }
  2637. TMessageParams Params(mpNeverAskAgainCheck);
  2638. Params.ImageName = L"Delete file";
  2639. unsigned int Answer =
  2640. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2641. if (Answer == qaNeverAskAgain)
  2642. {
  2643. Result = true;
  2644. if (Recycle)
  2645. {
  2646. WinConfiguration->ConfirmRecycling = false;
  2647. }
  2648. else
  2649. {
  2650. WinConfiguration->ConfirmDeleting = false;
  2651. }
  2652. }
  2653. else
  2654. {
  2655. Result = (Answer == qaOK);
  2656. }
  2657. }
  2658. if (Result)
  2659. {
  2660. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2661. }
  2662. return Result;
  2663. }
  2664. //---------------------------------------------------------------------------
  2665. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2666. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2667. {
  2668. TAutoBatch AutoBatch(this);
  2669. bool Result;
  2670. if ((Operation == foCopy) || (Operation == foMove))
  2671. {
  2672. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2673. }
  2674. else if (Operation == foRename)
  2675. {
  2676. DebugAssert(DirView(Side)->ItemFocused);
  2677. DirView(Side)->ItemFocused->EditCaption();
  2678. Result = true;
  2679. }
  2680. else if (Operation == foDelete)
  2681. {
  2682. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2683. }
  2684. else if (Operation == foSetProperties)
  2685. {
  2686. DirView(osRemote)->SaveSelectedNames();
  2687. Result = SetProperties(Side, FileList);
  2688. }
  2689. else if (Operation == foCustomCommand)
  2690. {
  2691. DebugAssert(Param);
  2692. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2693. DirView(osRemote)->SaveSelectedNames();
  2694. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2695. CustomCommand(FileList, *Command, NULL);
  2696. Result = true;
  2697. }
  2698. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2699. {
  2700. DebugAssert(!IsSideLocalBrowser(Side));
  2701. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2702. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2703. }
  2704. else if (Operation == foLock)
  2705. {
  2706. DebugAssert(!IsSideLocalBrowser(Side));
  2707. LockFiles(FileList, true);
  2708. Result = true;
  2709. }
  2710. else if (Operation == foUnlock)
  2711. {
  2712. DebugAssert(!IsSideLocalBrowser(Side));
  2713. LockFiles(FileList, false);
  2714. Result = true;
  2715. }
  2716. else
  2717. {
  2718. DebugFail();
  2719. }
  2720. return Result;
  2721. }
  2722. //---------------------------------------------------------------------------
  2723. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2724. {
  2725. if (Side == osCurrent)
  2726. {
  2727. Side = FCurrentSide;
  2728. }
  2729. return Side;
  2730. }
  2731. //---------------------------------------------------------------------------
  2732. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2733. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2734. {
  2735. Side = GetSide(Side);
  2736. bool Result;
  2737. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, IsSideLocalBrowser(Side), NULL);
  2738. try
  2739. {
  2740. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2741. }
  2742. __finally
  2743. {
  2744. delete FileList;
  2745. }
  2746. return Result;
  2747. }
  2748. //---------------------------------------------------------------------------
  2749. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2750. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2751. bool NoConfirmation, void * Param)
  2752. {
  2753. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2754. {
  2755. if ((Operation == foCopy) || (Operation == foMove))
  2756. {
  2757. DebugAssert(!IsLocalBrowserMode());
  2758. if (GetSide(Side) == osLocal)
  2759. {
  2760. Configuration->Usage->Inc(L"UploadsCommand");
  2761. }
  2762. else
  2763. {
  2764. Configuration->Usage->Inc(L"DownloadsCommand");
  2765. }
  2766. }
  2767. }
  2768. }
  2769. //---------------------------------------------------------------------------
  2770. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2771. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2772. {
  2773. TTransferOperationParam Param;
  2774. if ((WinConfiguration->Interface != ifCommander) ||
  2775. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2776. {
  2777. Flags &= ~cocShortCutHint;
  2778. }
  2779. TCustomDirView * DView = DirView(Side);
  2780. Param.Options =
  2781. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2782. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2783. if (FLAGSET(Flags, cocQueue))
  2784. {
  2785. Param.Queue = asOn;
  2786. }
  2787. else if (FLAGSET(Flags, cocNonQueue))
  2788. {
  2789. Param.Queue = asOff;
  2790. }
  2791. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2792. }
  2793. //---------------------------------------------------------------------------
  2794. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2795. {
  2796. int Count = ErrorList->Count;
  2797. try
  2798. {
  2799. if (ErrorList->Count)
  2800. {
  2801. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2802. if (MessageDialog(Message, qtError,
  2803. qaOK | qaCancel, HELP_NONE) == qaOK)
  2804. {
  2805. unsigned int Answer;
  2806. int Index = 0;
  2807. do
  2808. {
  2809. DebugAssert(Index >= 0 && Index < Count);
  2810. TQueryButtonAlias Aliases[2];
  2811. Aliases[0].Button = qaYes;
  2812. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2813. Aliases[1].Button = qaNo;
  2814. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2815. TMessageParams Params;
  2816. Params.Aliases = Aliases;
  2817. Params.AliasesCount = LENOF(Aliases);
  2818. int No = Index + 1;
  2819. UnicodeString Message = ErrorList->Strings[Index];
  2820. UnicodeString AMainInstructions;
  2821. UnicodeString Details;
  2822. if (ExtractMainInstructions(Message, AMainInstructions))
  2823. {
  2824. Details = Message;
  2825. Message = AMainInstructions;
  2826. }
  2827. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2828. Answer = MoreMessageDialog(
  2829. Message,
  2830. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2831. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2832. qaOK, HELP_NONE, &Params);
  2833. if (Answer == qaNo)
  2834. {
  2835. Index++;
  2836. }
  2837. if (Answer == qaYes)
  2838. {
  2839. Index--;
  2840. }
  2841. }
  2842. while (Answer != qaOK);
  2843. }
  2844. }
  2845. }
  2846. __finally
  2847. {
  2848. TStrings * List = ErrorList;
  2849. ErrorList = NULL;
  2850. for (int i = 0; i < Count; i++)
  2851. {
  2852. delete List->Objects[i];
  2853. }
  2854. delete List;
  2855. }
  2856. }
  2857. //---------------------------------------------------------------------------
  2858. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2859. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2860. {
  2861. // needed for checking filemasks, as there's no directory object
  2862. // associated with the file object
  2863. File->FullFileName = FullFileName;
  2864. TFileMasks::TParams MaskParams;
  2865. MaskParams.Size = File->Size;
  2866. MaskParams.Modification = File->Modification;
  2867. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2868. }
  2869. //---------------------------------------------------------------------------
  2870. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2871. {
  2872. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2873. Side = GetSide(Side);
  2874. TCustomDirView * DView = DirView(Side);
  2875. TListItem * FocusedItem = DView->ItemFocused;
  2876. UnicodeString Name;
  2877. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2878. {
  2879. Name = DView->ItemFileName(FocusedItem);
  2880. }
  2881. else
  2882. {
  2883. Name = LoadStr(NEW_FILE);
  2884. }
  2885. UnicodeString Names = Name;
  2886. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2887. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2888. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  2889. {
  2890. while (!Names.IsEmpty())
  2891. {
  2892. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2893. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2894. UnicodeString TargetFileName;
  2895. UnicodeString LocalFileName;
  2896. UnicodeString RootTempDir;
  2897. UnicodeString TempDir;
  2898. UnicodeString RemoteDirectory;
  2899. bool ExistingFile = false;
  2900. if (!IsSideLocalBrowser(Side))
  2901. {
  2902. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  2903. TRemoteFile * File = NULL;
  2904. if (Terminal->FileExists(Name, &File) &&
  2905. !File->IsDirectory)
  2906. {
  2907. try
  2908. {
  2909. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2910. ExistingFile = true;
  2911. }
  2912. __finally
  2913. {
  2914. delete File;
  2915. }
  2916. }
  2917. if (!ExistingFile)
  2918. {
  2919. RemoteDirectory = UnixExtractFilePath(Name);
  2920. TemporaryDirectoryForRemoteFiles(
  2921. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2922. TargetFileName = UnixExtractFileName(Name);
  2923. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2924. LocalFileName = TempDir +
  2925. // We probably do not want to trim the VMS version here
  2926. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2927. }
  2928. }
  2929. else
  2930. {
  2931. if (ExtractFilePath(Name).IsEmpty())
  2932. {
  2933. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2934. }
  2935. else
  2936. {
  2937. LocalFileName = ExpandFileName(Name);
  2938. }
  2939. TargetFileName = ExtractFileName(Name);
  2940. }
  2941. if (!ExistingFile)
  2942. {
  2943. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2944. if (NewFile)
  2945. {
  2946. int File = FileCreate(ApiPath(LocalFileName));
  2947. if (File < 0)
  2948. {
  2949. if (!RootTempDir.IsEmpty())
  2950. {
  2951. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2952. }
  2953. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2954. }
  2955. else
  2956. {
  2957. FileClose(File);
  2958. }
  2959. }
  2960. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2961. const TEditorData * ExternalEditor = NULL;
  2962. TFileMasks::TParams MaskParams; // size not known
  2963. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2964. false, MaskParams);
  2965. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2966. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  2967. }
  2968. }
  2969. }
  2970. }
  2971. //---------------------------------------------------------------------------
  2972. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2973. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2974. {
  2975. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2976. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2977. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2978. return
  2979. ((ExecuteFileBy == efInternalEditor)) ||
  2980. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2981. }
  2982. //---------------------------------------------------------------------------
  2983. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2984. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2985. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2986. UnicodeString RemoteDirectory, bool NewFile)
  2987. {
  2988. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2989. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2990. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2991. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2992. Side = GetSide(Side);
  2993. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2994. if (!IsSideLocalBrowser(Side))
  2995. {
  2996. Data->Terminal = Terminal;
  2997. Data->Queue = Queue;
  2998. Data->SessionData = CloneCurrentSessionData();
  2999. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3000. Data->RemoteDirectory = RemoteDirectory;
  3001. Data->SessionName = Terminal->SessionData->SessionName;
  3002. Data->LocalRootDirectory = LocalRootDirectory;
  3003. Data->OriginalFileName = OriginalFileName;
  3004. Data->Command = L""; // will be changed later for external editor
  3005. }
  3006. if (ExecuteFileBy == efInternalEditor)
  3007. {
  3008. if (!IsSideLocalBrowser(Side))
  3009. {
  3010. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3011. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3012. TForm * Editor;
  3013. try
  3014. {
  3015. TNotifyEvent OnFileChanged = NULL;
  3016. TNotifyEvent OnSaveAll = NULL;
  3017. TAnyModifiedEvent OnAnyModified = NULL;
  3018. // Edited files are uploaded in background queue, what we do not support with encrypted files
  3019. if (Terminal->IsCapable[fcBackgroundTransfers])
  3020. {
  3021. OnFileChanged = FEditorManager->FileChanged;
  3022. OnSaveAll = SaveAllInternalEditors;
  3023. OnAnyModified = AnyInternalEditorModified;
  3024. }
  3025. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  3026. FEditorManager->FileReload, FEditorManager->FileClosed,
  3027. OnSaveAll, OnAnyModified,
  3028. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3029. }
  3030. catch(...)
  3031. {
  3032. if (!LocalRootDirectory.IsEmpty())
  3033. {
  3034. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  3035. }
  3036. throw;
  3037. }
  3038. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3039. }
  3040. else
  3041. {
  3042. DebugAssert(!FStandaloneEditing);
  3043. TForm * Editor =
  3044. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3045. SaveAllInternalEditors, AnyInternalEditorModified,
  3046. L"", false, SessionColor, -1, NewFile);
  3047. FLocalEditors->Add(Editor);
  3048. }
  3049. }
  3050. else
  3051. {
  3052. HANDLE Process;
  3053. if (ExecuteFileBy == efExternalEditor)
  3054. {
  3055. UnicodeString Program, Params, Dir;
  3056. Data->Command = ExternalEditor->ExternalEditor;
  3057. ReformatFileNameCommand(Data->Command);
  3058. SplitCommand(Data->Command, Program, Params, Dir);
  3059. Params = ExpandFileNameCommand(Params, FileName);
  3060. Program = ExpandEnvironmentVariables(Program);
  3061. if (!ExecuteShell(Program, Params, Process))
  3062. {
  3063. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3064. }
  3065. }
  3066. else
  3067. {
  3068. DebugAssert(!IsSideLocalBrowser(Side));
  3069. if (!ExecuteShell(FileName, L"", Process))
  3070. {
  3071. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3072. }
  3073. }
  3074. if (IsSideLocalBrowser(Side) ||
  3075. ((ExecuteFileBy == efShell) &&
  3076. !WinConfiguration->Editor.SDIShellEditor) ||
  3077. ((ExecuteFileBy == efExternalEditor) &&
  3078. !ExternalEditor->SDIExternalEditor))
  3079. {
  3080. // no need for handle
  3081. if (Process != NULL)
  3082. {
  3083. DebugCheck(CloseHandle(Process));
  3084. }
  3085. Process = INVALID_HANDLE_VALUE;
  3086. }
  3087. else
  3088. {
  3089. if (Process == NULL)
  3090. {
  3091. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3092. }
  3093. }
  3094. if (!IsSideLocalBrowser(Side))
  3095. {
  3096. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3097. }
  3098. }
  3099. }
  3100. //---------------------------------------------------------------------------
  3101. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3102. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3103. void * /*Arg*/)
  3104. {
  3105. if (Token != NULL)
  3106. {
  3107. EditorFormFileSave(static_cast<TForm *>(Token));
  3108. }
  3109. }
  3110. //---------------------------------------------------------------------------
  3111. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3112. {
  3113. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3114. {
  3115. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3116. }
  3117. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3118. }
  3119. //---------------------------------------------------------------------------
  3120. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3121. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3122. void * Arg)
  3123. {
  3124. if ((Token != NULL) &&
  3125. IsEditorFormModified(static_cast<TForm *>(Token)))
  3126. {
  3127. *static_cast<bool *>(Arg) = true;
  3128. }
  3129. }
  3130. //---------------------------------------------------------------------------
  3131. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3132. TObject * /*Sender*/, bool & Modified)
  3133. {
  3134. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3135. {
  3136. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3137. {
  3138. Modified = true;
  3139. }
  3140. }
  3141. if (!Modified)
  3142. {
  3143. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3144. }
  3145. }
  3146. //---------------------------------------------------------------------------
  3147. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3148. {
  3149. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3150. }
  3151. //---------------------------------------------------------------------------
  3152. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3153. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3154. UnicodeString & Result, UnicodeString & RootDirectory)
  3155. {
  3156. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3157. {
  3158. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3159. Result = RootDirectory;
  3160. }
  3161. else
  3162. {
  3163. RootDirectory = L"";
  3164. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3165. Result = IncludeTrailingBackslash(Result);
  3166. }
  3167. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3168. {
  3169. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3170. }
  3171. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3172. {
  3173. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3174. {
  3175. RemoteDirectory.Delete(1, 1);
  3176. }
  3177. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3178. }
  3179. if (!ForceDirectories(ApiPath(Result)))
  3180. {
  3181. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3182. }
  3183. }
  3184. //---------------------------------------------------------------------------
  3185. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3186. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3187. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3188. {
  3189. DebugAssert(!IsLocalBrowserMode());
  3190. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3191. if (ForceText)
  3192. {
  3193. CopyParam.TransferMode = tmAscii;
  3194. }
  3195. TemporaryFileCopyParam(CopyParam);
  3196. if (AllFiles)
  3197. {
  3198. CopyParam.IncludeFileMask = TFileMasks();
  3199. }
  3200. if (TempDir.IsEmpty())
  3201. {
  3202. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3203. }
  3204. DebugAssert(!FAutoOperation);
  3205. FAutoOperation = AutoOperation;
  3206. Terminal->ExceptionOnFail = true;
  3207. try
  3208. {
  3209. try
  3210. {
  3211. // turn off confirmations, as for MDI editors we may possibly download
  3212. // the same file over
  3213. Terminal->CopyToLocal(
  3214. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3215. if (GetTargetNames)
  3216. {
  3217. for (int i = 0; i < FileList->Count; i++)
  3218. {
  3219. FileList->Strings[i] =
  3220. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3221. }
  3222. }
  3223. }
  3224. catch(...)
  3225. {
  3226. if (!RootTempDir.IsEmpty())
  3227. {
  3228. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3229. }
  3230. throw;
  3231. }
  3232. }
  3233. __finally
  3234. {
  3235. FAutoOperation = false;
  3236. Terminal->ExceptionOnFail = false;
  3237. }
  3238. }
  3239. //---------------------------------------------------------------------------
  3240. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3241. {
  3242. // Do not waste time checking for default editor list the next time,
  3243. // or testing if default editor is not notepad.
  3244. bool TryNextTime = false;
  3245. UnicodeString UsageState;
  3246. if (!WinConfiguration->EditorList->IsDefaultList())
  3247. {
  3248. UsageState = "H";
  3249. }
  3250. else
  3251. {
  3252. UnicodeString Executable;
  3253. UnicodeString ExecutableDescription;
  3254. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3255. {
  3256. UnicodeString Message =
  3257. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3258. unsigned int Answer =
  3259. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3260. if (Answer != qaOK)
  3261. {
  3262. UsageState = "R";
  3263. }
  3264. else
  3265. {
  3266. UsageState = "A";
  3267. TEditorData EditorData;
  3268. EditorData.Editor = edExternal;
  3269. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3270. EditorData.ExternalEditorOptionsAutodetect();
  3271. TEditorList EditorList;
  3272. EditorList = *WinConfiguration->EditorList;
  3273. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3274. WinConfiguration->EditorList = &EditorList;
  3275. }
  3276. }
  3277. }
  3278. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3279. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3280. }
  3281. //---------------------------------------------------------------------------
  3282. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3283. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3284. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3285. {
  3286. if (ExecuteFileBy == efDefaultEditor)
  3287. {
  3288. if (!WinConfiguration->OfferedEditorAutoConfig)
  3289. {
  3290. EditorAutoConfig();
  3291. }
  3292. const TEditorPreferences * Editor =
  3293. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3294. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3295. {
  3296. ExecuteFileBy = efInternalEditor;
  3297. ExternalEditor = NULL;
  3298. }
  3299. else if (Editor->Data->Editor == edOpen)
  3300. {
  3301. ExecuteFileBy = efShell;
  3302. ExternalEditor = NULL;
  3303. }
  3304. else
  3305. {
  3306. ExecuteFileBy = efExternalEditor;
  3307. ExternalEditor = Editor->Data;
  3308. }
  3309. }
  3310. }
  3311. //---------------------------------------------------------------------------
  3312. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3313. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3314. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3315. {
  3316. UnicodeString OriginalFileName;
  3317. UnicodeString LocalRootDirectory;
  3318. UnicodeString RemoteDirectory;
  3319. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3320. IsSideLocalBrowser(Side), MaskParams);
  3321. UnicodeString Counter;
  3322. UnicodeString LocalFileName;
  3323. bool Handled = false;
  3324. if (!IsSideLocalBrowser(Side))
  3325. {
  3326. // We need to trim VMS version here, so that we use name without version
  3327. // when uploading back to create a new version of the file
  3328. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3329. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3330. TObject * Token = NULL;
  3331. UnicodeString LocalDirectory;
  3332. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3333. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3334. LocalDirectory))
  3335. {
  3336. if (Token != NULL)
  3337. {
  3338. TForm * Form = dynamic_cast<TForm *>(Token);
  3339. if (Form->WindowState == wsMinimized)
  3340. {
  3341. ShowWindow(Form->Handle, SW_RESTORE);
  3342. }
  3343. else
  3344. {
  3345. Form->SetFocus();
  3346. }
  3347. Handled = true;
  3348. }
  3349. else
  3350. {
  3351. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3352. }
  3353. }
  3354. if (!Handled)
  3355. {
  3356. TStringList * FileList1 = new TStringList();
  3357. try
  3358. {
  3359. FileList1->AddObject(FullFileName, Object);
  3360. TemporarilyDownloadFiles(FileList1,
  3361. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3362. LocalRootDirectory, LocalDirectory, true, true, true);
  3363. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3364. }
  3365. __finally
  3366. {
  3367. delete FileList1;
  3368. }
  3369. switch (ExecuteFileBy)
  3370. {
  3371. case efShell:
  3372. Counter = "RemoteFilesExecuted";
  3373. break;
  3374. case efInternalEditor:
  3375. Counter = "RemoteFilesOpenedInInternalEditor";
  3376. break;
  3377. case efExternalEditor:
  3378. Counter = "RemoteFilesOpenedInExternalEditor";
  3379. break;
  3380. default:
  3381. DebugFail();
  3382. }
  3383. }
  3384. }
  3385. else
  3386. {
  3387. LocalFileName = FullFileName;
  3388. OriginalFileName = ExtractFileName(FullFileName);
  3389. switch (ExecuteFileBy)
  3390. {
  3391. case efShell:
  3392. Counter = "LocalFilesExecuted";
  3393. break;
  3394. case efInternalEditor:
  3395. Counter = "LocalFilesOpenedInInternalEditor";
  3396. break;
  3397. case efExternalEditor:
  3398. Counter = "LocalFilesOpenedInExternalEditor";
  3399. break;
  3400. default:
  3401. DebugFail();
  3402. }
  3403. }
  3404. if (!Handled)
  3405. {
  3406. Configuration->Usage->Inc(Counter);
  3407. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3408. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3409. }
  3410. }
  3411. //---------------------------------------------------------------------------
  3412. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3413. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3414. bool AllSelected, bool OnFocused)
  3415. {
  3416. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3417. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3418. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3419. Side = GetSide(Side);
  3420. TCustomDirView * DView = DirView(Side);
  3421. TStrings * FileList =
  3422. AllSelected ?
  3423. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3424. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3425. try
  3426. {
  3427. DebugAssert(AllSelected || (FileList->Count == 1));
  3428. for (int i = 0; i < FileList->Count; i++)
  3429. {
  3430. UnicodeString ListFileName = FileList->Strings[i];
  3431. UnicodeString FileNameOnly =
  3432. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3433. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3434. if (!DView->ItemIsDirectory(Item))
  3435. {
  3436. UnicodeString FullFileName;
  3437. if (!IsSideLocalBrowser(Side))
  3438. {
  3439. FullFileName = DirView(Side)->Path + ListFileName;
  3440. }
  3441. else
  3442. {
  3443. FullFileName = ListFileName;
  3444. }
  3445. TFileMasks::TParams MaskParams;
  3446. MaskParams.Size = DView->ItemFileSize(Item);
  3447. TDateTimePrecision Precision;
  3448. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3449. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3450. FileList->Objects[i], MaskParams);
  3451. }
  3452. }
  3453. }
  3454. __finally
  3455. {
  3456. delete FileList;
  3457. }
  3458. }
  3459. //---------------------------------------------------------------------------
  3460. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3461. {
  3462. CopyParam.FileNameCase = ncNoChange;
  3463. CopyParam.PreserveRights = false;
  3464. CopyParam.PreserveReadOnly = false;
  3465. CopyParam.ReplaceInvalidChars = true;
  3466. CopyParam.IncludeFileMask = TFileMasks();
  3467. CopyParam.NewerOnly = false;
  3468. CopyParam.FileMask = L"";
  3469. }
  3470. //---------------------------------------------------------------------------
  3471. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3472. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3473. {
  3474. TTerminalManager * Manager = TTerminalManager::Instance();
  3475. if (!IsActiveTerminal(Data->Terminal))
  3476. {
  3477. if (!NonVisualDataModule->Busy)
  3478. {
  3479. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3480. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3481. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3482. if (Data->Terminal == NULL)
  3483. {
  3484. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3485. if (SameSiteTerminal != NULL)
  3486. {
  3487. UnicodeString Message =
  3488. FMTLOAD(EDIT_SESSION_REATTACH,
  3489. (FileNameOnly, Data->SessionName, FileNameOnly));
  3490. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3491. {
  3492. Data->Terminal = Terminal;
  3493. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3494. Data->SessionName = Terminal->SessionData->SessionName;
  3495. // We might also overwrite session data
  3496. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3497. }
  3498. else
  3499. {
  3500. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3501. Abort();
  3502. }
  3503. }
  3504. }
  3505. // foreground session should reconnect itself
  3506. else if (Terminal != Data->Terminal)
  3507. {
  3508. UnicodeString Message =
  3509. MainInstructions(
  3510. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3511. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3512. {
  3513. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3514. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3515. }
  3516. else
  3517. {
  3518. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3519. Abort();
  3520. }
  3521. }
  3522. }
  3523. if (!IsActiveTerminal(Data->Terminal))
  3524. {
  3525. Configuration->Usage->Inc(L"EditInactiveSession");
  3526. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3527. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3528. (ExtractFileName(FileName), Data->SessionName)));
  3529. }
  3530. }
  3531. TStrings * FileList = new TStringList();
  3532. try
  3533. {
  3534. FileList->Add(FileName);
  3535. // consider using the same settings (preset) as when the file was downloaded
  3536. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3537. TemporaryFileCopyParam(CopyParam);
  3538. if (Data->ForceText)
  3539. {
  3540. CopyParam.TransferMode = tmAscii;
  3541. }
  3542. // so i do not need to worry if masking algorithm works in all cases
  3543. // ("" means "copy file name", no masking is actually done)
  3544. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3545. {
  3546. CopyParam.FileMask = L"";
  3547. }
  3548. else
  3549. {
  3550. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3551. }
  3552. DebugAssert(Data->Queue != NULL);
  3553. int Params = cpNoConfirmation | cpTemporary;
  3554. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3555. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3556. QueueItem->CompleteEvent = UploadCompleteEvent;
  3557. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3558. }
  3559. __finally
  3560. {
  3561. delete FileList;
  3562. }
  3563. }
  3564. //---------------------------------------------------------------------------
  3565. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3566. const UnicodeString FileName, const TEditedFileData * Data)
  3567. {
  3568. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3569. DebugAssert(!NonVisualDataModule->Busy);
  3570. if (!IsActiveTerminal(Data->Terminal))
  3571. {
  3572. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3573. (ExtractFileName(FileName), Data->SessionName)));
  3574. }
  3575. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3576. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3577. NonVisualDataModule->StartBusy();
  3578. try
  3579. {
  3580. std::unique_ptr<TRemoteFile> File;
  3581. UnicodeString RemoteFileName =
  3582. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3583. Terminal->ExceptionOnFail = true;
  3584. try
  3585. {
  3586. TRemoteFile * AFile = NULL;
  3587. Terminal->ReadFile(RemoteFileName, AFile);
  3588. File.reset(AFile);
  3589. if (!File->HaveFullFileName)
  3590. {
  3591. File->FullFileName = RemoteFileName;
  3592. }
  3593. }
  3594. __finally
  3595. {
  3596. Terminal->ExceptionOnFail = false;
  3597. }
  3598. std::unique_ptr<TStrings> FileList(new TStringList());
  3599. FileList->AddObject(RemoteFileName, File.get());
  3600. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3601. UnicodeString TempDir = ExtractFilePath(FileName);
  3602. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3603. TempDir, true, true, true);
  3604. // sanity check, the target file name should be still the same
  3605. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3606. }
  3607. __finally
  3608. {
  3609. NonVisualDataModule->EndBusy();
  3610. // it actually may not exist anymore...
  3611. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3612. }
  3613. }
  3614. //---------------------------------------------------------------------------
  3615. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3616. const TEditedFileData * Data, bool & KeepOpen)
  3617. {
  3618. // Command is set for external editors only (not for "shell" open).
  3619. if (!Data->Command.IsEmpty())
  3620. {
  3621. bool AnyFound = false;
  3622. bool AnyMDI = false;
  3623. bool AnyNonMDI = false;
  3624. bool AnyDetect = false;
  3625. TEditorList * EditorList = new TEditorList();
  3626. try
  3627. {
  3628. *EditorList = *WinConfiguration->EditorList;
  3629. for (int i = 0; i < EditorList->Count; i++)
  3630. {
  3631. const TEditorPreferences * Editor = EditorList->Editors[i];
  3632. if ((Editor->Data->Editor == edExternal) &&
  3633. (Editor->Data->ExternalEditor == Data->Command))
  3634. {
  3635. AnyFound = true;
  3636. if (Editor->Data->SDIExternalEditor)
  3637. {
  3638. AnyNonMDI = true;
  3639. if (Editor->Data->DetectMDIExternalEditor)
  3640. {
  3641. AnyDetect = true;
  3642. }
  3643. }
  3644. else
  3645. {
  3646. AnyMDI = true;
  3647. }
  3648. }
  3649. }
  3650. bool EnableMDI = false;
  3651. bool DisableDetect = false;
  3652. if (AnyMDI)
  3653. {
  3654. KeepOpen = true;
  3655. if (AnyNonMDI)
  3656. {
  3657. // there is at least one instance of the editor with MDI support enabled,
  3658. // and one with disabled, enable it for all instances
  3659. EnableMDI = true;
  3660. }
  3661. }
  3662. else if (AnyFound && !AnyDetect)
  3663. {
  3664. // at least once instance found but all have MDI autodetection disabled
  3665. // => close the file (default action)
  3666. }
  3667. else
  3668. {
  3669. // no instance of the editor has MDI support enabled
  3670. TMessageParams Params;
  3671. if (AnyFound)
  3672. {
  3673. // there is at least one instance of the editor
  3674. Params.Params |= mpNeverAskAgainCheck;
  3675. }
  3676. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3677. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3678. switch (Answer)
  3679. {
  3680. case qaNeverAskAgain:
  3681. DisableDetect = true;
  3682. break;
  3683. case qaNo:
  3684. EnableMDI = true;
  3685. KeepOpen = true;
  3686. break;
  3687. }
  3688. }
  3689. if (AnyFound && (EnableMDI || DisableDetect))
  3690. {
  3691. bool Changed = false;
  3692. for (int i = 0; i < EditorList->Count; i++)
  3693. {
  3694. const TEditorPreferences * Editor = EditorList->Editors[i];
  3695. if ((Editor->Data->Editor == edExternal) &&
  3696. (Editor->Data->ExternalEditor == Data->Command) &&
  3697. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3698. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3699. {
  3700. Changed = true;
  3701. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3702. if (EnableMDI)
  3703. {
  3704. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3705. }
  3706. if (DisableDetect)
  3707. {
  3708. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3709. }
  3710. EditorList->Change(i, UpdatedEditor);
  3711. }
  3712. }
  3713. if (Changed)
  3714. {
  3715. WinConfiguration->EditorList = EditorList;
  3716. }
  3717. }
  3718. }
  3719. __finally
  3720. {
  3721. delete EditorList;
  3722. }
  3723. }
  3724. else
  3725. {
  3726. // "open" case
  3727. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3728. qaOK, HELP_APP_EARLY_CLOSED);
  3729. }
  3730. }
  3731. //---------------------------------------------------------------------------
  3732. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3733. {
  3734. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3735. }
  3736. //---------------------------------------------------------------------------
  3737. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3738. {
  3739. SideEnter(osRemote);
  3740. }
  3741. //---------------------------------------------------------------------------
  3742. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3743. {
  3744. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3745. SideEnter(osRemote);
  3746. }
  3747. //---------------------------------------------------------------------------
  3748. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3749. {
  3750. FCurrentSide = Side;
  3751. if (Visible)
  3752. {
  3753. UpdateControls();
  3754. }
  3755. }
  3756. //---------------------------------------------------------------------------
  3757. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3758. TStrings * FileList, bool Alternative)
  3759. {
  3760. TCustomDirView * DView = DirView(Side);
  3761. DView->SaveSelection();
  3762. DView->SaveSelectedNames();
  3763. DebugAssert(!FAlternativeDelete);
  3764. FAlternativeDelete = Alternative;
  3765. try
  3766. {
  3767. if (!IsSideLocalBrowser(Side))
  3768. {
  3769. DebugAssert(Terminal != NULL);
  3770. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3771. }
  3772. else
  3773. {
  3774. if (IsLocalBrowserMode())
  3775. {
  3776. Configuration->Usage->Inc(L"LocalLocalDeletes");
  3777. }
  3778. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  3779. FProgressSide = FCurrentSide;
  3780. try
  3781. {
  3782. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3783. }
  3784. __finally
  3785. {
  3786. ReloadLocalDirectory();
  3787. }
  3788. }
  3789. FAlternativeDelete = false;
  3790. }
  3791. catch(...)
  3792. {
  3793. FAlternativeDelete = false;
  3794. DView->DiscardSavedSelection();
  3795. throw;
  3796. }
  3797. DView->RestoreSelection();
  3798. }
  3799. //---------------------------------------------------------------------------
  3800. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3801. {
  3802. DebugAssert(Terminal);
  3803. DebugAssert(!IsLocalBrowserMode());
  3804. RemoteDirView->SaveSelection();
  3805. RemoteDirView->SaveSelectedNames();
  3806. try
  3807. {
  3808. if (Lock)
  3809. {
  3810. Terminal->LockFiles(FileList);
  3811. }
  3812. else
  3813. {
  3814. Terminal->UnlockFiles(FileList);
  3815. }
  3816. }
  3817. catch(...)
  3818. {
  3819. RemoteDirView->DiscardSavedSelection();
  3820. throw;
  3821. }
  3822. RemoteDirView->RestoreSelection();
  3823. }
  3824. //---------------------------------------------------------------------------
  3825. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3826. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3827. bool NoConfirmation, bool Move)
  3828. {
  3829. DebugAssert(Terminal != NULL);
  3830. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  3831. DebugAssert(!IsLocalBrowserMode());
  3832. if (Session == NULL)
  3833. {
  3834. Session = Terminal;
  3835. }
  3836. if (Session == Terminal)
  3837. {
  3838. if (RemoteDriveView->DropTarget != NULL)
  3839. {
  3840. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3841. }
  3842. else if (RemoteDirView->DropTarget != NULL)
  3843. {
  3844. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3845. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3846. }
  3847. else
  3848. {
  3849. Target = RemoteDirView->Path;
  3850. }
  3851. }
  3852. else
  3853. {
  3854. Target = GetSessionPath(Session, osRemote);
  3855. }
  3856. Target = UnixIncludeTrailingBackslash(Target);
  3857. if (FileList->Count == 1)
  3858. {
  3859. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3860. }
  3861. else
  3862. {
  3863. FileMask = AnyMask;
  3864. }
  3865. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  3866. bool Result = true;
  3867. if (!NoConfirmation)
  3868. {
  3869. bool Multi = (FileList->Count > 1);
  3870. if (Move)
  3871. {
  3872. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3873. }
  3874. else
  3875. {
  3876. std::unique_ptr<TStrings> Sessions(new TStringList);
  3877. std::unique_ptr<TStrings> Directories(new TStringList);
  3878. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  3879. for (int Index = 0; Index < SessionList->Count; Index++)
  3880. {
  3881. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  3882. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  3883. {
  3884. Sessions->AddObject(SessionList->Strings[Index], ASession);
  3885. Directories->Add(GetSessionPath(ASession, osRemote));
  3886. }
  3887. }
  3888. TDirectRemoteCopy AllowDirectCopy;
  3889. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  3890. {
  3891. DebugAssert(DirectCopy);
  3892. AllowDirectCopy = drcAllow;
  3893. }
  3894. else if (Terminal->IsCapable[fcSecondaryShell])
  3895. {
  3896. DebugAssert(DirectCopy);
  3897. AllowDirectCopy = drcConfirmCommandSession;
  3898. }
  3899. else
  3900. {
  3901. DebugAssert(!DirectCopy);
  3902. AllowDirectCopy = drcDisallow;
  3903. }
  3904. void * ASession = Session;
  3905. Result = DoRemoteCopyDialog(
  3906. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal);
  3907. Session = static_cast<TManagedTerminal *>(ASession);
  3908. }
  3909. }
  3910. return Result;
  3911. }
  3912. //---------------------------------------------------------------------------
  3913. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3914. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3915. {
  3916. DebugAssert(!IsLocalBrowserMode());
  3917. bool DirectCopy;
  3918. UnicodeString Target, FileMask;
  3919. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3920. if (Result)
  3921. {
  3922. if (!Move && !DirectCopy)
  3923. {
  3924. Configuration->Usage->Inc("RemoteCopyTemp");
  3925. UnicodeString RootTempDir;
  3926. UnicodeString TempDir;
  3927. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3928. TStrings * TemporaryFilesList = new TStringList();
  3929. try
  3930. {
  3931. TMakeLocalFileListParams MakeFileListParam;
  3932. MakeFileListParam.FileList = TemporaryFilesList;
  3933. MakeFileListParam.FileTimes = NULL;
  3934. MakeFileListParam.IncludeDirs = true;
  3935. MakeFileListParam.Recursive = false;
  3936. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  3937. TTerminalManager::Instance()->ActiveSession = Session;
  3938. if (TemporaryFilesList->Count > 0)
  3939. {
  3940. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3941. CopyParam.FileMask = FileMask;
  3942. DebugAssert(!FAutoOperation);
  3943. FAutoOperation = true;
  3944. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3945. }
  3946. }
  3947. __finally
  3948. {
  3949. delete TemporaryFilesList;
  3950. FAutoOperation = false;
  3951. if (!RootTempDir.IsEmpty())
  3952. {
  3953. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3954. }
  3955. }
  3956. }
  3957. else
  3958. {
  3959. RemoteDirView->SaveSelection();
  3960. RemoteDirView->SaveSelectedNames();
  3961. try
  3962. {
  3963. if (Move)
  3964. {
  3965. Configuration->Usage->Inc("RemoteMove");
  3966. Terminal->MoveFiles(FileList, Target, FileMask);
  3967. }
  3968. else
  3969. {
  3970. Configuration->Usage->Inc("RemoteCopyDirect");
  3971. DebugAssert(DirectCopy);
  3972. DebugAssert(Session == Terminal);
  3973. if (Terminal->IsCapable[fcRemoteCopy] ||
  3974. Terminal->CommandSessionOpened ||
  3975. CommandSessionFallback())
  3976. {
  3977. Terminal->CopyFiles(FileList, Target, FileMask);
  3978. }
  3979. }
  3980. }
  3981. catch(...)
  3982. {
  3983. RemoteDirView->DiscardSavedSelection();
  3984. throw;
  3985. }
  3986. RemoteDirView->RestoreSelection();
  3987. }
  3988. }
  3989. return Result;
  3990. }
  3991. //---------------------------------------------------------------------------
  3992. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3993. const UnicodeString & Path, TRemoteProperties & Properties)
  3994. {
  3995. DebugAssert(!IsLocalBrowserMode());
  3996. Properties.Valid = Properties.Valid << vpEncrypt;
  3997. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3998. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3999. }
  4000. //---------------------------------------------------------------------------
  4001. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4002. {
  4003. Side = GetSide(Side);
  4004. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4005. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4006. UnicodeString Name = LoadStr(NEW_FOLDER);
  4007. int AllowedChanges =
  4008. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4009. bool SaveSettings = false;
  4010. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4011. {
  4012. TWindowLock Lock(this);
  4013. if (!IsSideLocalBrowser(Side))
  4014. {
  4015. if (SaveSettings)
  4016. {
  4017. GUIConfiguration->NewDirectoryProperties = Properties;
  4018. }
  4019. CreateRemoteDirectory(Name, Properties);
  4020. }
  4021. else
  4022. {
  4023. DirView(Side)->CreateDirectory(Name);
  4024. }
  4025. }
  4026. }
  4027. //---------------------------------------------------------------------------
  4028. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4029. {
  4030. TWindowLock Lock(this);
  4031. DirView(Side)->ExecuteHomeDirectory();
  4032. }
  4033. //---------------------------------------------------------------------------
  4034. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4035. {
  4036. DoOpenDirectoryDialog(odBrowse, Side);
  4037. }
  4038. //---------------------------------------------------------------------------
  4039. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4040. {
  4041. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4042. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4043. bool Result = !Path.IsEmpty();
  4044. if (Result)
  4045. {
  4046. // While we might get here when the session is closed (from location profiles),
  4047. // it's not a problem as the Path setter is noop then
  4048. DirView(Side)->Path = Path;
  4049. }
  4050. return Result;
  4051. }
  4052. //---------------------------------------------------------------------------
  4053. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4054. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4055. bool & Close)
  4056. {
  4057. // terminal can be already closed (e.g. dropped connection)
  4058. if (Terminal != NULL)
  4059. {
  4060. try
  4061. {
  4062. TCalculateSizeParams Params;
  4063. Params.Stats = &Stats;
  4064. Terminal->CalculateFilesSize(FileList, Size, Params);
  4065. }
  4066. catch(...)
  4067. {
  4068. if (!Terminal->Active)
  4069. {
  4070. Close = true;
  4071. }
  4072. throw;
  4073. }
  4074. }
  4075. }
  4076. //---------------------------------------------------------------------------
  4077. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4078. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4079. bool & Close)
  4080. {
  4081. // terminal can be already closed (e.g. dropped connection)
  4082. if (Terminal != NULL)
  4083. {
  4084. Configuration->Usage->Inc(L"ChecksumCalculated");
  4085. try
  4086. {
  4087. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  4088. }
  4089. catch(...)
  4090. {
  4091. if (!Terminal->Active)
  4092. {
  4093. Close = true;
  4094. }
  4095. throw;
  4096. }
  4097. }
  4098. }
  4099. //---------------------------------------------------------------------------
  4100. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4101. {
  4102. bool Result;
  4103. if (!IsSideLocalBrowser(Side))
  4104. {
  4105. TRemoteTokenList * GroupList = NULL;
  4106. TRemoteTokenList * UserList = NULL;
  4107. try
  4108. {
  4109. TRemoteProperties CurrentProperties;
  4110. if (Terminal->LoadFilesProperties(FileList))
  4111. {
  4112. RemoteDirView->Invalidate();
  4113. }
  4114. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4115. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4116. if (CapableGroupChanging || CapableOwnerChanging)
  4117. {
  4118. if (CapableGroupChanging)
  4119. {
  4120. GroupList = Terminal->Groups->Duplicate();
  4121. }
  4122. if (CapableOwnerChanging)
  4123. {
  4124. UserList = Terminal->Users->Duplicate();
  4125. }
  4126. TRemoteDirectory * Files = Terminal->Files;
  4127. int Count = Files->Count;
  4128. if (Count > 100)
  4129. {
  4130. Count = 100;
  4131. }
  4132. for (int Index = 0; Index < Count; Index++)
  4133. {
  4134. TRemoteFile * File = Files->Files[Index];
  4135. if (CapableGroupChanging)
  4136. {
  4137. GroupList->AddUnique(File->Group);
  4138. }
  4139. if (CapableOwnerChanging)
  4140. {
  4141. UserList->AddUnique(File->Owner);
  4142. }
  4143. }
  4144. // if we haven't collected tokens for all files in current directory,
  4145. // make sure we collect them at least for all selected files.
  4146. // (note that so far the files in FileList has to be from current direcotry)
  4147. if (Count < Files->Count)
  4148. {
  4149. for (int Index = 0; Index < FileList->Count; Index++)
  4150. {
  4151. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4152. if (CapableGroupChanging)
  4153. {
  4154. GroupList->AddUnique(File->Group);
  4155. }
  4156. if (CapableOwnerChanging)
  4157. {
  4158. UserList->AddUnique(File->Owner);
  4159. }
  4160. }
  4161. }
  4162. }
  4163. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4164. int Flags = 0;
  4165. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4166. if (CapableOwnerChanging) Flags |= cpOwner;
  4167. if (CapableGroupChanging) Flags |= cpGroup;
  4168. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4169. if (Terminal->IsCapable[fcCalculatingChecksum])
  4170. {
  4171. CalculateChecksumEvent = CalculateChecksum;
  4172. }
  4173. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4174. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4175. TRemoteProperties NewProperties = CurrentProperties;
  4176. Result =
  4177. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4178. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4179. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4180. CalculateSize, CalculateChecksumEvent);
  4181. if (Result)
  4182. {
  4183. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4184. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4185. }
  4186. }
  4187. __finally
  4188. {
  4189. delete GroupList;
  4190. delete UserList;
  4191. }
  4192. }
  4193. else
  4194. {
  4195. DirView(Side)->DisplayPropertiesMenu();
  4196. Result = true;
  4197. }
  4198. return Result;
  4199. }
  4200. //---------------------------------------------------------------------------
  4201. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4202. {
  4203. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4204. {
  4205. FIgnoreNextDialogChar = Key;
  4206. }
  4207. Key = 0;
  4208. }
  4209. //---------------------------------------------------------------------------
  4210. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4211. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4212. {
  4213. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4214. if (Command != NULL)
  4215. {
  4216. KeyProcessed(Key, Shift);
  4217. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4218. {
  4219. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4220. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4221. false, false, const_cast<TCustomCommandType *>(Command));
  4222. }
  4223. }
  4224. }
  4225. //---------------------------------------------------------------------------
  4226. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4227. {
  4228. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4229. {
  4230. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4231. }
  4232. if (!DirView(osCurrent)->IsEditing())
  4233. {
  4234. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4235. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4236. {
  4237. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4238. // But so far it's not a problem, as it does what we want.
  4239. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4240. if (((Action->ShortCut == KeyShortCut) ||
  4241. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4242. AllowedAction(Action, aaShortCut))
  4243. {
  4244. // Has to be called before the action as the dialog char is already in queue.
  4245. // So when the action consumes message queue, we already need to have the
  4246. // FIgnoreNextDialogChar set
  4247. KeyProcessed(Key, Shift);
  4248. // Reset reference to previous component (when menu/toolbar was clicked).
  4249. // Needed to detect that action was invoked by keyboard shortcut
  4250. // in TNonVisualDataModule::ExplorerActionsExecute
  4251. Action->ActionComponent = NULL;
  4252. Action->Execute();
  4253. return;
  4254. }
  4255. }
  4256. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4257. {
  4258. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4259. {
  4260. KeyProcessed(Key, Shift);
  4261. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4262. return;
  4263. }
  4264. }
  4265. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4266. {
  4267. KeyProcessed(Key, Shift);
  4268. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4269. }
  4270. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4271. if (IsCustomShortCut(CustomShortCut))
  4272. {
  4273. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4274. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4275. if (WinConfiguration->SharedBookmarks != NULL)
  4276. {
  4277. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4278. if ((Bookmark != NULL) &&
  4279. OpenBookmark(FCurrentSide, Bookmark))
  4280. {
  4281. KeyProcessed(Key, Shift);
  4282. }
  4283. }
  4284. }
  4285. }
  4286. TForm::KeyDown(Key, Shift);
  4287. }
  4288. //---------------------------------------------------------------------------
  4289. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4290. {
  4291. bool OpenedTerminal = false;
  4292. UnicodeString ProtocolName;
  4293. UnicodeString ProtocolNameHint;
  4294. UnicodeString SecurityProtocolName;
  4295. UnicodeString DurationHint;
  4296. // See the comment in TScpCommanderForm::UpdateControls()
  4297. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4298. if (ActiveTerminal != NULL)
  4299. {
  4300. // Active is not enough here
  4301. if (ActiveTerminal->Status == ssOpened)
  4302. {
  4303. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4304. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4305. if (FileSystemInfo.ProtocolName.IsEmpty())
  4306. {
  4307. ProtocolName = SessionInfo.ProtocolName;
  4308. }
  4309. else
  4310. {
  4311. ProtocolName = FileSystemInfo.ProtocolName;
  4312. }
  4313. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4314. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4315. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4316. OpenedTerminal = true;
  4317. }
  4318. else if (ActiveTerminal->Status > ssClosed)
  4319. {
  4320. ProtocolName = LoadStr(STATUS_CONNECTING);
  4321. }
  4322. else
  4323. {
  4324. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4325. }
  4326. }
  4327. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4328. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4329. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4330. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4331. if (ProtocolStatusPanel->Caption != ProtocolName)
  4332. {
  4333. ProtocolStatusPanel->Caption = ProtocolName;
  4334. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4335. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4336. }
  4337. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4338. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4339. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4340. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4341. SecurityStatusPanel->Enabled = SecurityEnabled;
  4342. UnicodeString SecurityProtocolHint;
  4343. if (SecurityEnabled)
  4344. {
  4345. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4346. }
  4347. else
  4348. {
  4349. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4350. }
  4351. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4352. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4353. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4354. DurationStatusPanel->Hint = DurationHint;
  4355. UpdateStatusBar();
  4356. }
  4357. //---------------------------------------------------------------------------
  4358. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4359. {
  4360. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4361. DebugAssert(SessionStatusBar != NULL);
  4362. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4363. if (!FNote.IsEmpty())
  4364. {
  4365. NoteStatusPanel->Caption = FNote;
  4366. }
  4367. else
  4368. {
  4369. UpdateStatusPanelText(NoteStatusPanel);
  4370. }
  4371. NoteStatusPanel->Hint = FNoteHints;
  4372. UnicodeString LoginTime;
  4373. // See the comment in TScpCommanderForm::UpdateControls()
  4374. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4375. // Active is not enough here
  4376. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4377. {
  4378. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4379. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4380. }
  4381. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4382. DurationStatusPanel->Caption = LoginTime;
  4383. }
  4384. //---------------------------------------------------------------------------
  4385. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4386. {
  4387. Panel->Caption = L"";
  4388. }
  4389. //---------------------------------------------------------------------------
  4390. void __fastcall TCustomScpExplorerForm::Idle()
  4391. {
  4392. if (FShowing || FStandaloneEditing)
  4393. {
  4394. FEditorManager->Check();
  4395. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4396. // that needs to know if queue view is visible (and it may be closed after queue update)
  4397. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4398. }
  4399. if (FShowing)
  4400. {
  4401. if (!NonVisualDataModule->Busy &&
  4402. // Menu is opened or drag&drop is going on
  4403. (Mouse->Capture == NULL))
  4404. {
  4405. if (FRefreshRemoteDirectory)
  4406. {
  4407. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4408. {
  4409. Terminal->RefreshDirectory();
  4410. }
  4411. FRefreshRemoteDirectory = false;
  4412. }
  4413. if (FRefreshLocalDirectory)
  4414. {
  4415. ReloadLocalDirectory();
  4416. FRefreshLocalDirectory = false;
  4417. }
  4418. if (WinConfiguration->RefreshRemotePanel)
  4419. {
  4420. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4421. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4422. {
  4423. RemoteDirView->ReloadDirectory();
  4424. }
  4425. }
  4426. }
  4427. }
  4428. if (FShowing || FStandaloneEditing)
  4429. {
  4430. if (FQueueStatusInvalidated)
  4431. {
  4432. UpdateQueueStatus(false);
  4433. }
  4434. RefreshQueueItems();
  4435. }
  4436. if (FShowing)
  4437. {
  4438. UpdateStatusBar();
  4439. }
  4440. FIgnoreNextDialogChar = 0;
  4441. }
  4442. //---------------------------------------------------------------------------
  4443. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4444. {
  4445. try
  4446. {
  4447. FUserActionTimer->Enabled = false;
  4448. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4449. {
  4450. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4451. {
  4452. FPendingQueueActionItem->ProcessUserAction();
  4453. }
  4454. }
  4455. }
  4456. __finally
  4457. {
  4458. FPendingQueueActionItem = NULL;
  4459. }
  4460. }
  4461. //---------------------------------------------------------------------------
  4462. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4463. {
  4464. if (WinConfiguration->MinimizeToTray)
  4465. {
  4466. UpdateTrayIcon();
  4467. FTrayIcon->Visible = true;
  4468. if (Visible)
  4469. {
  4470. ShowWindow(Handle, SW_HIDE);
  4471. }
  4472. }
  4473. }
  4474. //---------------------------------------------------------------------------
  4475. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4476. {
  4477. // WORKAROUND
  4478. // When restoring maximized window from minimization,
  4479. // rarely some controls do not align properly.
  4480. // Two instances seen (both for Commander):
  4481. // - When restoring, window is temporarily narrower (not maximized),
  4482. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4483. // Once maximized already, top dock shinks back, but the session PageControl,
  4484. // do not align up, leaving space between TopDock and PageControl.
  4485. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4486. for (int Index = 0; Index < ControlCount; Index++)
  4487. {
  4488. RealignControl(Controls[Index]);
  4489. }
  4490. if (FTrayIcon->Visible)
  4491. {
  4492. FTrayIcon->Visible = false;
  4493. if (Visible)
  4494. {
  4495. ShowWindow(Handle, SW_SHOW);
  4496. }
  4497. }
  4498. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4499. {
  4500. FNeedSession = false;
  4501. NonVisualDataModule->StartBusy();
  4502. try
  4503. {
  4504. NeedSession(true);
  4505. }
  4506. __finally
  4507. {
  4508. NonVisualDataModule->EndBusy();
  4509. }
  4510. }
  4511. }
  4512. //---------------------------------------------------------------------------
  4513. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4514. {
  4515. FTrayIcon->Hint = GetMainForm()->Caption;
  4516. }
  4517. //---------------------------------------------------------------------------
  4518. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4519. {
  4520. UpdateTrayIcon();
  4521. }
  4522. //---------------------------------------------------------------------------
  4523. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4524. {
  4525. // workaround
  4526. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4527. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4528. // But WM_ACTIVATEAPP message can be received even when
  4529. // the main window is minimized to the tray and internal editor window is focused
  4530. // (after another application was previously active)
  4531. if (::IsIconic(Handle))
  4532. {
  4533. if (!IsAppIconic())
  4534. {
  4535. SetAppIconic(true);
  4536. }
  4537. }
  4538. ::ApplicationRestore();
  4539. Application->BringToFront();
  4540. }
  4541. //---------------------------------------------------------------------------
  4542. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4543. {
  4544. RestoreApp();
  4545. }
  4546. //---------------------------------------------------------------------------
  4547. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4548. {
  4549. if (OpenInNewWindow())
  4550. {
  4551. ExecuteNewInstance(SessionUrl);
  4552. }
  4553. else
  4554. {
  4555. TTerminalManager::Instance()->NewSession(SessionUrl);
  4556. }
  4557. }
  4558. //---------------------------------------------------------------------------
  4559. void TCustomScpExplorerForm::NewTab(TOperationSide)
  4560. {
  4561. NewSession();
  4562. }
  4563. //---------------------------------------------------------------------------
  4564. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4565. {
  4566. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4567. StoredSessions->NewSession(SessionName, SessionData);
  4568. // modified only, explicit
  4569. StoredSessions->Save(false, true);
  4570. // encode session name because of slashes in hierarchical sessions
  4571. return EncodeUrlString(SessionName);
  4572. }
  4573. //---------------------------------------------------------------------------
  4574. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4575. {
  4576. // current working directories become defaults here, what is not right
  4577. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4578. return SaveHiddenDuplicateSession(SessionData.get());
  4579. }
  4580. //---------------------------------------------------------------------------
  4581. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4582. {
  4583. if (OpenInNewWindow())
  4584. {
  4585. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4586. }
  4587. else
  4588. {
  4589. // current working directories become defaults here, what is not right
  4590. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4591. TTerminalManager * Manager = TTerminalManager::Instance();
  4592. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4593. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4594. // We definitelly want these
  4595. ASession->Disconnected = ManagedSession->Disconnected;
  4596. // Not sure about these two
  4597. ASession->Permanent = ManagedSession->Permanent;
  4598. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4599. Manager->ActiveSession = ASession;
  4600. }
  4601. }
  4602. //---------------------------------------------------------------------------
  4603. void __fastcall TCustomScpExplorerForm::RenameSession()
  4604. {
  4605. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4606. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME) &&
  4607. // When submitting the default name, do not fix it
  4608. (Name != ManagedSession->SessionData->SessionName))
  4609. {
  4610. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4611. TSessionData::ValidateName(Name);
  4612. // This is inconsistent with how color (for example) is handled.
  4613. ManagedSession->SessionData->Name = Name;
  4614. UpdateControls();
  4615. // Add/Remove distinguishing paths from sessions of the same name.
  4616. SessionListChanged();
  4617. }
  4618. }
  4619. //---------------------------------------------------------------------------
  4620. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4621. {
  4622. DebugAssert(Queue != NULL);
  4623. bool Result = Queue->IsEmpty;
  4624. if (!Result)
  4625. {
  4626. SetFocus();
  4627. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4628. }
  4629. return Result;
  4630. }
  4631. //---------------------------------------------------------------------------
  4632. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4633. {
  4634. return CanCloseQueue(FQueue);
  4635. }
  4636. //---------------------------------------------------------------------------
  4637. void __fastcall TCustomScpExplorerForm::CloseSession()
  4638. {
  4639. if (CanCloseQueue())
  4640. {
  4641. TTerminalManager::Instance()->FreeActiveTerminal();
  4642. }
  4643. }
  4644. //---------------------------------------------------------------------------
  4645. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4646. {
  4647. if (CanCloseQueue())
  4648. {
  4649. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4650. }
  4651. }
  4652. //---------------------------------------------------------------------------
  4653. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4654. {
  4655. DetachTerminal(Terminal);
  4656. RemoteDirView->Terminal = Terminal;
  4657. UpdateRemotePathComboBox(false);
  4658. InitStatusBar();
  4659. UpdateControls();
  4660. }
  4661. //---------------------------------------------------------------------------
  4662. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4663. {
  4664. InitStatusBar();
  4665. }
  4666. //---------------------------------------------------------------------------
  4667. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4668. {
  4669. if (DebugAlwaysTrue(!Terminal->Active))
  4670. {
  4671. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4672. }
  4673. }
  4674. //---------------------------------------------------------------------------
  4675. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4676. {
  4677. if (OpenInNewWindow())
  4678. {
  4679. // encode session name because of slashes in hierarchical sessions
  4680. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4681. }
  4682. else
  4683. {
  4684. TTerminalManager * Manager = TTerminalManager::Instance();
  4685. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4686. Manager->ActiveSession = Terminal;
  4687. }
  4688. }
  4689. //---------------------------------------------------------------------------
  4690. void __fastcall TCustomScpExplorerForm::DoOpenFolderOrWorkspace(const UnicodeString & Name, bool ConnectFirstTerminal)
  4691. {
  4692. TTerminalManager * Manager = TTerminalManager::Instance();
  4693. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4694. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4695. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  4696. // FirstSession can be null, if some of the
  4697. if (!ConnectFirstTerminal && (FirstSession != NULL))
  4698. {
  4699. FirstSession->Disconnected = true;
  4700. FirstSession->DisconnectedTemporarily = true;
  4701. }
  4702. Manager->ActiveSession = FirstSession;
  4703. }
  4704. //---------------------------------------------------------------------------
  4705. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4706. {
  4707. if (OpenInNewWindow())
  4708. {
  4709. ExecuteNewInstance(Name);
  4710. }
  4711. else
  4712. {
  4713. DoOpenFolderOrWorkspace(Name, true);
  4714. }
  4715. }
  4716. //---------------------------------------------------------------------------
  4717. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4718. bool &CanClose)
  4719. {
  4720. // See the comment in CloseApp()
  4721. if (NonVisualDataModule->Busy)
  4722. {
  4723. CanClose = false;
  4724. }
  4725. else
  4726. {
  4727. TTerminalManager * Manager = TTerminalManager::Instance();
  4728. int ActiveSessions = 0;
  4729. for (int Index = 0; Index < Manager->Count; Index++)
  4730. {
  4731. if (Manager->Sessions[Index]->Active)
  4732. {
  4733. ActiveSessions++;
  4734. }
  4735. }
  4736. // Confirm, when there's at least one active remote session or
  4737. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4738. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4739. WinConfiguration->ConfirmClosingSession)
  4740. {
  4741. unsigned int Result;
  4742. TMessageParams Params(mpNeverAskAgainCheck);
  4743. UnicodeString Message;
  4744. int Answers = qaOK | qaCancel;
  4745. // The current session is the only active session
  4746. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4747. {
  4748. if (!WinConfiguration->AutoSaveWorkspace)
  4749. {
  4750. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4751. }
  4752. else
  4753. {
  4754. Message = LoadStr(CLOSE_SESSION);
  4755. }
  4756. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4757. }
  4758. // Multiple active sessions or one active session, but it's not the current one
  4759. else if (ActiveSessions > 0)
  4760. {
  4761. if (!WinConfiguration->AutoSaveWorkspace)
  4762. {
  4763. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4764. }
  4765. else
  4766. {
  4767. Message = LoadStr(CLOSE_SESSIONS);
  4768. }
  4769. }
  4770. // Multiple tabs, but all are intactive or local, and saving of worspace is not enabled
  4771. else
  4772. {
  4773. DebugAlwaysTrue(Manager->Count > 1);
  4774. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  4775. Message = LoadStr(CLOSE_WORKSPACE);
  4776. }
  4777. UnicodeString Note;
  4778. if (WinConfiguration->AutoSaveWorkspace)
  4779. {
  4780. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4781. }
  4782. else
  4783. {
  4784. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4785. Answers = qaYes | qaNo | qaCancel;
  4786. }
  4787. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4788. SetFocus();
  4789. Result = MessageDialog(Message, qtConfirmation,
  4790. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4791. if (Result == qaNeverAskAgain)
  4792. {
  4793. WinConfiguration->ConfirmClosingSession = false;
  4794. }
  4795. if (Result == qaNo)
  4796. {
  4797. CanClose = SaveWorkspace(true);
  4798. // note that the workspace will be saved redundatly again from FormClose
  4799. }
  4800. else
  4801. {
  4802. CanClose =
  4803. (Result == qaOK) ||
  4804. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4805. (Result == qaNeverAskAgain);
  4806. }
  4807. }
  4808. if (CanClose && HasActiveTerminal())
  4809. {
  4810. CanClose = CanCloseQueue();
  4811. }
  4812. }
  4813. if (CanClose)
  4814. {
  4815. CanClose =
  4816. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4817. FEditorManager->CloseExternalFilesWithoutProcess();
  4818. if (CanClose)
  4819. {
  4820. while (CanClose && (FLocalEditors->Count > 0))
  4821. {
  4822. int PrevCount = FLocalEditors->Count;
  4823. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4824. CanClose = (FLocalEditors->Count < PrevCount);
  4825. }
  4826. if (CanClose)
  4827. {
  4828. CanClose = FEditorManager->Empty(true);
  4829. if (!CanClose)
  4830. {
  4831. SetFocus();
  4832. CanClose =
  4833. (MessageDialog(
  4834. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4835. }
  4836. }
  4837. }
  4838. }
  4839. }
  4840. //---------------------------------------------------------------------------
  4841. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4842. {
  4843. TForm * Form = dynamic_cast<TForm *>(Sender);
  4844. DebugAssert(Form != NULL);
  4845. Form->Close();
  4846. }
  4847. //---------------------------------------------------------------------------
  4848. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4849. {
  4850. TForm * Form = dynamic_cast<TForm *>(Sender);
  4851. delete Form;
  4852. }
  4853. //---------------------------------------------------------------------------
  4854. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4855. {
  4856. while (FLocalEditors->Count > 0)
  4857. {
  4858. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4859. }
  4860. }
  4861. //---------------------------------------------------------------------------
  4862. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4863. TObject *Sender)
  4864. {
  4865. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4866. try
  4867. {
  4868. SetProperties(osRemote, FileList);
  4869. }
  4870. __finally
  4871. {
  4872. delete FileList;
  4873. }
  4874. }
  4875. //---------------------------------------------------------------------------
  4876. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4877. {
  4878. if (value)
  4879. {
  4880. if (Component == fcCustomCommandsBand)
  4881. {
  4882. UpdateCustomCommandsToolbar();
  4883. }
  4884. if (Component == fcQueueFileList)
  4885. {
  4886. DebugAssert(!QueueFileListSplitter->Visible);
  4887. AdjustQueueLayout();
  4888. UpdateQueueFileList();
  4889. }
  4890. }
  4891. }
  4892. //---------------------------------------------------------------------------
  4893. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  4894. {
  4895. int MinHeightLimit =
  4896. GetStaticQueuePanelComponentsHeight() +
  4897. QueueFileListSplitter->Height +
  4898. QueueFileList->Height +
  4899. GetMinQueueViewHeight();
  4900. if (QueuePanel->ClientHeight < MinHeightLimit)
  4901. {
  4902. QueuePanel->ClientHeight = MinHeightLimit;
  4903. }
  4904. }
  4905. //---------------------------------------------------------------------------
  4906. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4907. {
  4908. TControl * Control = GetComponent(Component);
  4909. DebugAssert(Control);
  4910. bool Changed = (Control->Visible != value);
  4911. if (Changed)
  4912. {
  4913. ComponentShowing(Component, value);
  4914. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4915. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4916. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4917. if (value)
  4918. {
  4919. int RemainingHeight = Control->Parent->ClientHeight;
  4920. int RemainingWidth = Control->Parent->ClientWidth;
  4921. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4922. {
  4923. TControl * ChildControl = Control->Parent->Controls[i];
  4924. if (ChildControl->Visible)
  4925. {
  4926. switch (ChildControl->Align)
  4927. {
  4928. case alTop:
  4929. case alBottom:
  4930. RemainingHeight -= ChildControl->Height;
  4931. break;
  4932. case alLeft:
  4933. case alRight:
  4934. RemainingWidth -= ChildControl->Width;
  4935. break;
  4936. }
  4937. }
  4938. }
  4939. int Reserve = ScaleByTextHeight(this, 32);
  4940. // queue in explorer, trees in commander
  4941. if (Control->Height > RemainingHeight - Reserve)
  4942. {
  4943. Control->Height = RemainingHeight / 2;
  4944. }
  4945. if (Control->Width > RemainingWidth - Reserve)
  4946. {
  4947. Control->Width = RemainingWidth / 2;
  4948. }
  4949. }
  4950. Control->Visible = value;
  4951. if (WasFocused && Visible)
  4952. {
  4953. DirView(osCurrent)->SetFocus();
  4954. }
  4955. FixControlsPlacement();
  4956. }
  4957. }
  4958. //---------------------------------------------------------------------------
  4959. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4960. {
  4961. TControl * Control = GetComponent(Component);
  4962. if (Control == NULL)
  4963. {
  4964. return false;
  4965. }
  4966. else
  4967. {
  4968. return Control->Visible;
  4969. }
  4970. }
  4971. //---------------------------------------------------------------------------
  4972. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4973. {
  4974. bool Result;
  4975. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4976. {
  4977. Result = !IsUWP();
  4978. }
  4979. else
  4980. {
  4981. Result = true;
  4982. }
  4983. return Result;
  4984. }
  4985. //---------------------------------------------------------------------------
  4986. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  4987. {
  4988. if (DirView->ItemFocused != NULL)
  4989. {
  4990. DirView->ItemFocused->MakeVisible(false);
  4991. }
  4992. }
  4993. //---------------------------------------------------------------------------
  4994. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4995. {
  4996. MakeFocusedItemVisible(RemoteDirView);
  4997. QueueSplitter->Visible = QueuePanel->Visible;
  4998. QueueFileListSplitter->Visible = QueueFileList->Visible;
  4999. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5000. TControl * QueueControlsOrder[] =
  5001. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5002. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5003. }
  5004. //---------------------------------------------------------------------------
  5005. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5006. {
  5007. switch (Component) {
  5008. case fcStatusBar: return RemoteStatusBar;
  5009. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5010. case fcQueueView: return QueuePanel;
  5011. case fcQueueToolbar: return QueueDock;
  5012. case fcRemoteTree: return RemoteDrivePanel;
  5013. case fcSessionsTabs: return SessionsPageControl;
  5014. case fcQueueFileList: return QueueFileList;
  5015. default: return NULL;
  5016. }
  5017. }
  5018. //---------------------------------------------------------------------------
  5019. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5020. TObject *Sender, TListColumn *Column, TPoint &Point)
  5021. {
  5022. DebugAssert(NonVisualDataModule && Column && Sender);
  5023. NonVisualDataModule->ListColumn = Column;
  5024. TPopupMenu * DirViewColumnMenu;
  5025. if (Sender == DirView(osOther))
  5026. {
  5027. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5028. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  5029. (Column->Index == uvName);
  5030. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  5031. (Column->Index == uvSize);
  5032. }
  5033. else
  5034. {
  5035. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5036. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  5037. (Column->Index == dvName);
  5038. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  5039. (Column->Index == dvSize);
  5040. }
  5041. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5042. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5043. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5044. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5045. }
  5046. //---------------------------------------------------------------------------
  5047. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5048. TObject *Sender, TListItem *Item, bool &AllowExec)
  5049. {
  5050. DoDirViewExecFile(Sender, Item, AllowExec);
  5051. }
  5052. //---------------------------------------------------------------------------
  5053. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5054. TListItem * Item, bool & AllowExec)
  5055. {
  5056. DebugAssert(Sender && Item && Configuration);
  5057. DebugAssert(AllowExec);
  5058. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5059. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5060. bool Remote = !IsSideLocalBrowser(Side);
  5061. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles();
  5062. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  5063. // on files only (not directories)
  5064. // and only when symlinks are resolved (apply to remote panel only)
  5065. if (!ADirView->ItemIsDirectory(Item) &&
  5066. (ResolvedSymlinks || FForceExecution))
  5067. {
  5068. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  5069. !FForceExecution && ResolvedSymlinks)
  5070. {
  5071. if (WinConfiguration->DoubleClickAction == dcaCopy)
  5072. {
  5073. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5074. UnlockWindow();
  5075. try
  5076. {
  5077. if (IsLocalBrowserMode())
  5078. {
  5079. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5080. }
  5081. else
  5082. {
  5083. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5084. }
  5085. }
  5086. __finally
  5087. {
  5088. LockWindow();
  5089. }
  5090. AllowExec = false;
  5091. }
  5092. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  5093. {
  5094. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5095. {
  5096. ExecuteFile(Side, efDefaultEditor);
  5097. AllowExec = false;
  5098. }
  5099. }
  5100. else
  5101. {
  5102. DebugFail();
  5103. }
  5104. }
  5105. // if we have not done anything special, fall back to default behavior
  5106. if (AllowExec)
  5107. {
  5108. if (Remote && !WinConfiguration->DisableOpenEdit)
  5109. {
  5110. ExecuteFile(osRemote, efShell);
  5111. AllowExec = false;
  5112. }
  5113. }
  5114. }
  5115. }
  5116. //---------------------------------------------------------------------------
  5117. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5118. {
  5119. return ((Side == osRemote) || (Side == osCurrent));
  5120. }
  5121. //---------------------------------------------------------------------------
  5122. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5123. {
  5124. DebugFail();
  5125. }
  5126. //---------------------------------------------------------------------------
  5127. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5128. {
  5129. DebugFail();
  5130. }
  5131. //---------------------------------------------------------------------------
  5132. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5133. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5134. {
  5135. TSynchronizeParamType Params;
  5136. Params.LocalDirectory = LocalDirectory;
  5137. Params.RemoteDirectory = RemoteDirectory;
  5138. int UnusedParams =
  5139. (GUIConfiguration->SynchronizeParams &
  5140. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5141. TTerminal::spCaseSensitive));
  5142. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5143. Params.Options = GUIConfiguration->SynchronizeOptions;
  5144. bool SaveSettings = false;
  5145. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5146. &DoSynchronizeTooManyDirectories);
  5147. DebugAssert(FSynchronizeController == NULL);
  5148. FSynchronizeController = &Controller;
  5149. bool Result;
  5150. try
  5151. {
  5152. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5153. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5154. int Options =
  5155. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5156. DebugAssert(FOnFeedSynchronizeError == NULL);
  5157. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5158. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5159. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  5160. if (Result)
  5161. {
  5162. if (SaveSettings)
  5163. {
  5164. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5165. GUIConfiguration->SynchronizeOptions = Params.Options;
  5166. }
  5167. else
  5168. {
  5169. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5170. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5171. FLAGSET(Params.Options, soSynchronize))
  5172. {
  5173. GUIConfiguration->SynchronizeOptions =
  5174. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5175. soSynchronize;
  5176. }
  5177. }
  5178. LocalDirectory = Params.LocalDirectory;
  5179. RemoteDirectory = Params.RemoteDirectory;
  5180. }
  5181. }
  5182. __finally
  5183. {
  5184. FSynchronizeController = NULL;
  5185. DebugAssert(FOnFeedSynchronizeError == NULL);
  5186. FOnFeedSynchronizeError = NULL;
  5187. }
  5188. return Result;
  5189. }
  5190. //---------------------------------------------------------------------------
  5191. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5192. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5193. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5194. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5195. TSynchronizeOptions * Options, bool Full)
  5196. {
  5197. // Keep-up-to-date mode
  5198. if (Terminal->Status == ssOpened)
  5199. {
  5200. try
  5201. {
  5202. int PParams = Params.Params;
  5203. if (!Full)
  5204. {
  5205. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5206. if (FLAGSET(Params.Options, soRecurse))
  5207. {
  5208. PParams |= TTerminal::spSubDirs;
  5209. }
  5210. }
  5211. else
  5212. {
  5213. // if keepuptodate is non-recursive,
  5214. // full sync before has to be non-recursive as well
  5215. if (FLAGCLEAR(Params.Options, soRecurse))
  5216. {
  5217. PParams |= TTerminal::spNoRecurse;
  5218. }
  5219. }
  5220. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5221. PParams, Checklist, Options);
  5222. }
  5223. catch (Exception & E)
  5224. {
  5225. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  5226. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5227. {
  5228. // noop, already logged in MoreMessageDialog
  5229. }
  5230. else
  5231. {
  5232. ShowExtendedExceptionEx(Terminal, &E);
  5233. throw;
  5234. }
  5235. }
  5236. }
  5237. }
  5238. //---------------------------------------------------------------------------
  5239. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5240. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5241. const UnicodeString ErrorStr)
  5242. {
  5243. if (!Directory.IsEmpty())
  5244. {
  5245. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5246. }
  5247. else
  5248. {
  5249. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5250. }
  5251. }
  5252. //---------------------------------------------------------------------------
  5253. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5254. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5255. {
  5256. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5257. {
  5258. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5259. }
  5260. else
  5261. {
  5262. TMessageParams Params(mpNeverAskAgainCheck);
  5263. unsigned int Result = MessageDialog(
  5264. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5265. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5266. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5267. {
  5268. MaxDirectories *= 2;
  5269. if (Result == qaNeverAskAgain)
  5270. {
  5271. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5272. }
  5273. }
  5274. else
  5275. {
  5276. Abort();
  5277. }
  5278. }
  5279. }
  5280. //---------------------------------------------------------------------------
  5281. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5282. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5283. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5284. TSynchronizeOptions * Options)
  5285. {
  5286. // Keep-up-to-date mode
  5287. DebugAssert(!FAutoOperation);
  5288. void * BatchStorage;
  5289. BatchStart(BatchStorage);
  5290. FAutoOperation = true;
  5291. bool AnyOperation = false;
  5292. TDateTime StartTime = Now();
  5293. TSynchronizeChecklist * AChecklist = NULL;
  5294. try
  5295. {
  5296. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5297. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5298. {
  5299. FSynchronizeProgressForm->Start();
  5300. }
  5301. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5302. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5303. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5304. Options);
  5305. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5306. SAFE_DESTROY(FSynchronizeProgressForm);
  5307. AnyOperation = (AChecklist->CheckedCount > 0);
  5308. if (AnyOperation)
  5309. {
  5310. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5311. CreateProgressForm(&SynchronizeProgress);
  5312. try
  5313. {
  5314. Terminal->SynchronizeApply(
  5315. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5316. SynchronizeProcessedItem, NULL, NULL, NULL);
  5317. }
  5318. __finally
  5319. {
  5320. DestroyProgressForm();
  5321. }
  5322. }
  5323. }
  5324. __finally
  5325. {
  5326. if (Checklist == NULL)
  5327. {
  5328. delete AChecklist;
  5329. }
  5330. else
  5331. {
  5332. *Checklist = AChecklist;
  5333. }
  5334. FAutoOperation = false;
  5335. SAFE_DESTROY(FSynchronizeProgressForm);
  5336. BatchEnd(BatchStorage);
  5337. ReloadLocalDirectory();
  5338. if (AnyOperation)
  5339. {
  5340. OperationComplete(StartTime);
  5341. }
  5342. }
  5343. }
  5344. //---------------------------------------------------------------------------
  5345. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5346. {
  5347. // can be called from command line
  5348. return Visible &&
  5349. ((DirView(osRemote)->SelCount > 0) ||
  5350. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5351. }
  5352. //---------------------------------------------------------------------------
  5353. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5354. {
  5355. LogSynchronizeEvent(Terminal, Message);
  5356. }
  5357. //---------------------------------------------------------------------------
  5358. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5359. int Params, TSynchronizeOptions & Options)
  5360. {
  5361. DebugAssert(!IsLocalBrowserMode());
  5362. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5363. {
  5364. Options.Filter = new TStringList();
  5365. Options.Filter->CaseSensitive = false;
  5366. Options.Filter->Duplicates = Types::dupAccept;
  5367. if (DirView(osRemote)->SelCount > 0)
  5368. {
  5369. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5370. }
  5371. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5372. {
  5373. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5374. }
  5375. Options.Filter->Sort();
  5376. }
  5377. }
  5378. //---------------------------------------------------------------------------
  5379. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5380. {
  5381. TCopyParamType Defaults;
  5382. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5383. ConfigStorage->AccessMode = smRead;
  5384. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5385. {
  5386. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5387. }
  5388. ConfigStorage.reset(NULL);
  5389. std::unique_ptr<TStringList> Options(new TStringList());
  5390. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5391. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5392. UnicodeString Result;
  5393. if (Options->Count > 0)
  5394. {
  5395. Result =
  5396. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5397. }
  5398. return Result;
  5399. }
  5400. //---------------------------------------------------------------------------
  5401. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5402. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5403. {
  5404. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5405. UnicodeString AdditionalParams =
  5406. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5407. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5408. SerializeCopyParamForCommandLine(CopyParams),
  5409. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5410. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5411. ExecuteNewInstance(SessionName, AdditionalParams);
  5412. }
  5413. //---------------------------------------------------------------------------
  5414. struct TSynchronizeParams
  5415. {
  5416. UnicodeString LocalDirectory;
  5417. UnicodeString RemoteDirectory;
  5418. TSynchronizeMode Mode;
  5419. TSynchronizeChecklist * Checklist;
  5420. int Params;
  5421. TCopyParamType * CopyParam;
  5422. TDateTime CollectElapsed;
  5423. TDateTime * StartTime;
  5424. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5425. };
  5426. //---------------------------------------------------------------------------
  5427. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5428. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5429. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5430. {
  5431. DebugAssert(!FAutoOperation);
  5432. void * BatchStorage;
  5433. BatchStart(BatchStorage);
  5434. FAutoOperation = true;
  5435. TFileOperationStatistics Statistics;
  5436. TDateTime Start = Now();
  5437. try
  5438. {
  5439. Params.OnProcessedItem = OnProcessedItem;
  5440. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5441. CreateProgressForm(&SynchronizeProgress);
  5442. Terminal->SynchronizeApply(
  5443. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5444. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5445. &Statistics);
  5446. }
  5447. __finally
  5448. {
  5449. Params.OnProcessedItem = NULL;
  5450. FAutoOperation = false;
  5451. DestroyProgressForm();
  5452. BatchEnd(BatchStorage);
  5453. ReloadLocalDirectory();
  5454. }
  5455. if (WinConfiguration->SynchronizeSummary)
  5456. {
  5457. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5458. // The statistics should be 0 anyway in this case
  5459. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5460. {
  5461. Message += L"\n";
  5462. if (Statistics.FilesUploaded > 0)
  5463. {
  5464. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5465. }
  5466. if (Statistics.FilesDownloaded > 0)
  5467. {
  5468. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5469. }
  5470. if (Statistics.FilesDeletedLocal > 0)
  5471. {
  5472. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5473. }
  5474. if (Statistics.FilesDeletedRemote > 0)
  5475. {
  5476. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5477. }
  5478. }
  5479. TDateTime Elapsed = (Now() - Start);
  5480. Message +=
  5481. L"\n" +
  5482. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5483. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5484. TMessageParams Params(mpNeverAskAgainCheck);
  5485. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5486. if (Result == qaNeverAskAgain)
  5487. {
  5488. WinConfiguration->SynchronizeSummary = false;
  5489. }
  5490. }
  5491. }
  5492. //---------------------------------------------------------------------------
  5493. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5494. {
  5495. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5496. {
  5497. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5498. }
  5499. // Not set in keep-up-to-date mode - Synchronize() method
  5500. if (Token != NULL)
  5501. {
  5502. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5503. if (Params.OnProcessedItem != NULL)
  5504. {
  5505. Params.OnProcessedItem(NULL, ChecklistItem);
  5506. }
  5507. }
  5508. }
  5509. //---------------------------------------------------------------------------
  5510. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5511. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5512. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5513. {
  5514. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5515. *Params.StartTime = Now();
  5516. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5517. }
  5518. //---------------------------------------------------------------------------
  5519. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5520. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5521. {
  5522. // terminal can be already closed (e.g. dropped connection)
  5523. if (Terminal != NULL)
  5524. {
  5525. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5526. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5527. }
  5528. }
  5529. //---------------------------------------------------------------------------
  5530. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5531. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5532. {
  5533. DebugAssert(!IsLocalBrowserMode());
  5534. TAutoBatch AutoBatch(this);
  5535. TAutoFlag AutoOperationFlag(FAutoOperation);
  5536. if (Side == osRemote)
  5537. {
  5538. std::unique_ptr<TStrings> FileList(new TStringList());
  5539. FileList->AddObject(FileName, RemoteFile);
  5540. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5541. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5542. RemoteDirView->SaveSelection();
  5543. RemoteDirView->SaveSelectedNames();
  5544. try
  5545. {
  5546. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5547. }
  5548. catch(...)
  5549. {
  5550. RemoteDirView->DiscardSavedSelection();
  5551. throw;
  5552. }
  5553. RemoteDirView->RestoreSelection();
  5554. }
  5555. else if (DebugAlwaysTrue(Side == osLocal))
  5556. {
  5557. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5558. {
  5559. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5560. }
  5561. UnicodeString Directory = ExtractFileDir(FileName);
  5562. ReloadLocalDirectory(Directory);
  5563. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5564. if (!SamePaths(Directory, NewDirectory))
  5565. {
  5566. ReloadLocalDirectory(NewDirectory);
  5567. }
  5568. }
  5569. }
  5570. //---------------------------------------------------------------------------
  5571. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5572. {
  5573. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5574. if (Side == osLocal)
  5575. {
  5576. if (Action == TSynchronizeChecklist::saDownloadNew)
  5577. {
  5578. OpenFolderInExplorer(LocalPath);
  5579. }
  5580. else
  5581. {
  5582. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5583. }
  5584. }
  5585. else if (DebugAlwaysTrue(Side == osRemote))
  5586. {
  5587. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5588. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5589. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5590. if (!LocalPath.IsEmpty())
  5591. {
  5592. SessionData->LocalDirectory = LocalPath;
  5593. }
  5594. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5595. ExecuteNewInstance(SessionName, FORMAT(L"%s=%s", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5596. }
  5597. }
  5598. //---------------------------------------------------------------------------
  5599. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5600. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5601. 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(SYNCHRONIZE_SWITCH),
  5609. LocalDirectory, RemoteDirectory, Mode, Params));
  5610. ExecuteNewInstance(SessionName, AdditionalParams);
  5611. }
  5612. //---------------------------------------------------------------------------
  5613. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5614. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5615. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5616. {
  5617. int Result;
  5618. bool SaveSettings = false;
  5619. int Options =
  5620. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5621. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5622. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5623. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5624. bool Continue =
  5625. (UseDefaults == 0) ||
  5626. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5627. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5628. if (Continue)
  5629. {
  5630. Configuration->Usage->Inc(L"Synchronizations");
  5631. UpdateCopyParamCounters(CopyParam);
  5632. TSynchronizeOptions SynchronizeOptions;
  5633. GetSynchronizeOptions(Params, SynchronizeOptions);
  5634. if (SaveSettings)
  5635. {
  5636. GUIConfiguration->SynchronizeParams = Params;
  5637. }
  5638. else
  5639. {
  5640. SaveMode = false;
  5641. }
  5642. TDateTime StartTime = Now();
  5643. TSynchronizeChecklist * Checklist = NULL;
  5644. try
  5645. {
  5646. DebugAssert(!FAutoOperation);
  5647. FAutoOperation = true;
  5648. try
  5649. {
  5650. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5651. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5652. int Files = -1;
  5653. if (DataList->Count >= 1)
  5654. {
  5655. Files = StrToIntDef(DataList->Strings[0], Files);
  5656. }
  5657. else
  5658. {
  5659. DataList->Add(UnicodeString());
  5660. }
  5661. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5662. FSynchronizeProgressForm->Start();
  5663. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5664. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5665. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5666. &SynchronizeOptions);
  5667. if (Terminal->SessionData->CacheDirectories)
  5668. {
  5669. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5670. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5671. }
  5672. }
  5673. __finally
  5674. {
  5675. FAutoOperation = false;
  5676. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5677. SAFE_DESTROY(FSynchronizeProgressForm);
  5678. }
  5679. TSynchronizeParams SynchronizeParams;
  5680. SynchronizeParams.LocalDirectory = LocalDirectory;
  5681. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5682. SynchronizeParams.Mode = Mode;
  5683. SynchronizeParams.CopyParam = &CopyParam;
  5684. SynchronizeParams.Params = Params;
  5685. SynchronizeParams.Checklist = Checklist;
  5686. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5687. SynchronizeParams.StartTime = &StartTime;
  5688. SynchronizeParams.OnProcessedItem = NULL;
  5689. Result = Checklist->Count;
  5690. if (Checklist->Count > 0)
  5691. {
  5692. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5693. {
  5694. if (!DoSynchronizeChecklistDialog(
  5695. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5696. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5697. {
  5698. Result = -1;
  5699. }
  5700. }
  5701. else
  5702. {
  5703. FullSynchronize(SynchronizeParams, NULL, NULL);
  5704. }
  5705. }
  5706. else
  5707. {
  5708. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5709. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5710. }
  5711. }
  5712. __finally
  5713. {
  5714. delete Checklist;
  5715. }
  5716. OperationComplete(StartTime);
  5717. }
  5718. else
  5719. {
  5720. Result = -1;
  5721. }
  5722. return Result;
  5723. }
  5724. //---------------------------------------------------------------------------
  5725. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5726. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5727. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5728. {
  5729. if (Collect)
  5730. {
  5731. DebugAssert(FSynchronizeProgressForm != NULL);
  5732. if (!FSynchronizeProgressForm->Started)
  5733. {
  5734. FSynchronizeProgressForm->Start();
  5735. }
  5736. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5737. SetTaskbarListProgressValue(CompareProgress);
  5738. }
  5739. else
  5740. {
  5741. DebugAssert(FSynchronizeProgressForm == NULL);
  5742. DebugAssert(FProgressForm != NULL);
  5743. }
  5744. }
  5745. //---------------------------------------------------------------------------
  5746. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5747. {
  5748. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  5749. TRemoteFile * File = NULL;
  5750. Terminal->ReadFile(FullFileName, File);
  5751. if (File != NULL)
  5752. {
  5753. std::unique_ptr<TRemoteFile> FileOwner(File);
  5754. TAutoFlag Flag(FStandaloneEditing);
  5755. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5756. Application->ShowMainForm = false;
  5757. Application->Run();
  5758. }
  5759. }
  5760. //---------------------------------------------------------------------------
  5761. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  5762. {
  5763. DebugFail();
  5764. }
  5765. //---------------------------------------------------------------------------
  5766. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5767. {
  5768. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5769. SessionData->Assign(ManagedSession->SessionData);
  5770. UpdateSessionData(SessionData.get());
  5771. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  5772. if (IsActiveTerminal(ManagedSession))
  5773. {
  5774. ManagedSession->UpdateSessionCredentials(SessionData.get());
  5775. }
  5776. if (!ManagedSession->SessionData->HasSessionName())
  5777. {
  5778. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5779. // once IsWorkspace is cleared
  5780. SessionData->Name = UnicodeString();
  5781. }
  5782. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  5783. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  5784. SessionData->IsWorkspace = false;
  5785. return SessionData.release();
  5786. }
  5787. //---------------------------------------------------------------------------
  5788. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5789. {
  5790. TSessionData * SessionData = CloneCurrentSessionData();
  5791. try
  5792. {
  5793. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5794. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5795. }
  5796. __finally
  5797. {
  5798. delete SessionData;
  5799. }
  5800. }
  5801. //---------------------------------------------------------------------------
  5802. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5803. {
  5804. TTerminalManager * Manager = TTerminalManager::Instance();
  5805. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5806. if (DebugAlwaysTrue(ManagedSession != NULL))
  5807. {
  5808. // Update ManagedSession->StateData
  5809. UpdateSession(ManagedSession);
  5810. }
  5811. Manager->SaveWorkspace(DataList.get());
  5812. return DataList.release();
  5813. }
  5814. //---------------------------------------------------------------------------
  5815. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5816. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5817. {
  5818. WinConfiguration->LastWorkspace = Name;
  5819. if (!SavePasswords)
  5820. {
  5821. for (int Index = 0; Index < DataList->Count; Index++)
  5822. {
  5823. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5824. if (SessionData->Link.IsEmpty())
  5825. {
  5826. SessionData->ClearSessionPasswords();
  5827. }
  5828. }
  5829. }
  5830. StoredSessions->NewWorkspace(Name, DataList);
  5831. // modified only
  5832. StoredSessions->Save(false, Explicit);
  5833. }
  5834. //---------------------------------------------------------------------------
  5835. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5836. {
  5837. return
  5838. DefaultStr(WinConfiguration->LastWorkspace,
  5839. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5840. }
  5841. //---------------------------------------------------------------------------
  5842. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5843. {
  5844. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5845. bool AnyNonStoredSessionWithPassword = false;
  5846. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5847. bool AllNonStoredSessionsAnonymous = true;
  5848. TTerminalManager * Manager = TTerminalManager::Instance();
  5849. for (int Index = 0; Index < DataList->Count; Index++)
  5850. {
  5851. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5852. if (SessionData->Link.IsEmpty())
  5853. {
  5854. if (SessionData->HasAnySessionPassword())
  5855. {
  5856. AnyNonStoredSessionWithPassword = true;
  5857. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  5858. {
  5859. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5860. }
  5861. }
  5862. if (!SameText(SessionData->UserName, AnonymousUserName))
  5863. {
  5864. AllNonStoredSessionsAnonymous = false;
  5865. }
  5866. }
  5867. }
  5868. bool SavePasswords;
  5869. bool * PSavePasswords;
  5870. bool NotRecommendedSavingPasswords =
  5871. !CustomWinConfiguration->UseMasterPassword &&
  5872. !AllNonStoredSessionsAnonymous;
  5873. if (Configuration->DisablePasswordStoring ||
  5874. !AnyNonStoredSessionWithPassword)
  5875. {
  5876. PSavePasswords = NULL;
  5877. SavePasswords = false;
  5878. }
  5879. else
  5880. {
  5881. PSavePasswords = &SavePasswords;
  5882. SavePasswords =
  5883. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5884. !NotRecommendedSavingPasswords;
  5885. }
  5886. UnicodeString Name = WorkspaceName();
  5887. bool CreateShortcut = false;
  5888. bool Result =
  5889. DoSaveWorkspaceDialog(
  5890. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5891. EnableAutoSave);
  5892. if (Result)
  5893. {
  5894. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5895. if (CreateShortcut)
  5896. {
  5897. TOperationVisualizer Visualizer;
  5898. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5899. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5900. }
  5901. if (EnableAutoSave)
  5902. {
  5903. WinConfiguration->AutoSaveWorkspace = true;
  5904. WinConfiguration->AutoWorkspace = Name;
  5905. if (PSavePasswords != NULL)
  5906. {
  5907. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5908. }
  5909. }
  5910. }
  5911. return Result;
  5912. }
  5913. //---------------------------------------------------------------------------
  5914. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  5915. {
  5916. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  5917. DebugAssert(Session == ManagedSession);
  5918. if (!IsLocalBrowserMode())
  5919. {
  5920. SAFE_DESTROY(Session->RemoteExplorerState);
  5921. if (WinConfiguration->PreservePanelState)
  5922. {
  5923. Session->RemoteExplorerState = RemoteDirView->SaveState();
  5924. }
  5925. }
  5926. UpdateSessionData(Session->StateData);
  5927. }
  5928. //---------------------------------------------------------------------------
  5929. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5930. {
  5931. // Keep in sync with TSessionData::CopyStateData
  5932. DebugAssert(Data != NULL);
  5933. // This is inconsistent with how color (for example) is handled.
  5934. Data->Name = ManagedSession->SessionData->Name;
  5935. if (!IsLocalBrowserMode())
  5936. {
  5937. // cannot use RemoteDirView->Path, because it is empty if connection
  5938. // was already closed
  5939. // also only peek, we may not be connected at all atm
  5940. // so make sure we do not try retrieving current directory from the server
  5941. // (particularly with FTP)
  5942. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5943. if (!ACurrentDirectory.IsEmpty())
  5944. {
  5945. Data->RemoteDirectory = ACurrentDirectory;
  5946. }
  5947. }
  5948. Data->Color = SessionColor;
  5949. }
  5950. //---------------------------------------------------------------------------
  5951. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5952. {
  5953. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5954. DebugAssert(Toolbar != NULL);
  5955. for (int i = 0; i < Toolbar->Items->Count; i++)
  5956. {
  5957. TTBXCustomDropDownItem * DropDownItem;
  5958. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5959. if (DropDownItem != NULL)
  5960. {
  5961. ToolbarItemResize(DropDownItem,
  5962. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5963. Toolbar->NonClientWidth);
  5964. break;
  5965. }
  5966. }
  5967. }
  5968. //---------------------------------------------------------------------------
  5969. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5970. {
  5971. Item->EditWidth = Width;
  5972. }
  5973. //---------------------------------------------------------------------------
  5974. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5975. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5976. {
  5977. for (int i = 0; i < Toolbar->Items->Count; i++)
  5978. {
  5979. TTBXCustomDropDownItem * DropDownItem;
  5980. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5981. if (DropDownItem != NULL)
  5982. {
  5983. ASize.x -= DropDownItem->EditWidth;
  5984. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5985. }
  5986. }
  5987. }
  5988. //---------------------------------------------------------------------------
  5989. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5990. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5991. {
  5992. if (WinConfiguration->DDWarnLackOfTempSpace)
  5993. {
  5994. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5995. if (!ADrive.IsEmpty())
  5996. {
  5997. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5998. DebugAssert(RequiredSpace >= 0);
  5999. __int64 RequiredWithReserve;
  6000. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6001. if (FreeSpace < RequiredWithReserve)
  6002. {
  6003. unsigned int Result;
  6004. TMessageParams Params(mpNeverAskAgainCheck);
  6005. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6006. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6007. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6008. if (Result == qaNeverAskAgain)
  6009. {
  6010. WinConfiguration->DDWarnLackOfTempSpace = false;
  6011. }
  6012. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6013. }
  6014. }
  6015. }
  6016. }
  6017. //---------------------------------------------------------------------------
  6018. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6019. {
  6020. DoOpenDirectoryDialog(odAddBookmark, Side);
  6021. }
  6022. //---------------------------------------------------------------------------
  6023. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6024. {
  6025. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6026. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6027. try
  6028. {
  6029. TCustomDirView * DView = DirView(Side);
  6030. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6031. {
  6032. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6033. }
  6034. }
  6035. catch (...)
  6036. {
  6037. delete VisitedDirectories;
  6038. throw;
  6039. }
  6040. return VisitedDirectories;
  6041. }
  6042. //---------------------------------------------------------------------------
  6043. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6044. TOpenDirectoryMode Mode, TOperationSide Side)
  6045. {
  6046. bool Continue = true;
  6047. if (Mode == odAddBookmark)
  6048. {
  6049. TMessageParams Params(mpNeverAskAgainCheck);
  6050. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6051. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6052. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6053. unsigned int Answer =
  6054. MessageDialog(Message,
  6055. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6056. if (Answer == qaNeverAskAgain)
  6057. {
  6058. Continue = true;
  6059. WinConfiguration->UseSharedBookmarks = true;
  6060. }
  6061. else if (Answer == qaOK)
  6062. {
  6063. Continue = true;
  6064. WinConfiguration->UseSharedBookmarks = false;
  6065. }
  6066. else
  6067. {
  6068. Continue = false;
  6069. }
  6070. }
  6071. if (Continue)
  6072. {
  6073. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6074. try
  6075. {
  6076. UnicodeString Name = DirView(Side)->PathName;
  6077. bool Repeat;
  6078. do
  6079. {
  6080. Repeat = false;
  6081. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6082. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6083. // do not allow switching to location profiles,
  6084. // if we are not connected
  6085. HasDirView[osLocal] && (Terminal != NULL)))
  6086. {
  6087. TWindowLock Lock(this);
  6088. if (!TryOpenDirectory(Side, Name))
  6089. {
  6090. Repeat = true;
  6091. Mode = odBrowse;
  6092. }
  6093. }
  6094. }
  6095. while (Repeat);
  6096. }
  6097. __finally
  6098. {
  6099. delete VisitedDirectories;
  6100. }
  6101. }
  6102. }
  6103. //---------------------------------------------------------------------------
  6104. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6105. {
  6106. DebugAssert(!Terminal->CommandSessionOpened);
  6107. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6108. }
  6109. //---------------------------------------------------------------------------
  6110. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6111. {
  6112. bool Result = Terminal->IsCapable[Capability] ||
  6113. Terminal->CommandSessionOpened;
  6114. if (!Result)
  6115. {
  6116. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6117. if (!GUIConfiguration->ConfirmCommandSession)
  6118. {
  6119. Result = true;
  6120. }
  6121. else
  6122. {
  6123. TMessageParams Params(mpNeverAskAgainCheck);
  6124. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6125. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6126. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6127. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6128. if (Answer == qaNeverAskAgain)
  6129. {
  6130. GUIConfiguration->ConfirmCommandSession = false;
  6131. Result = true;
  6132. }
  6133. else if (Answer == qaOK)
  6134. {
  6135. Result = true;
  6136. }
  6137. }
  6138. if (Result)
  6139. {
  6140. Result = CommandSessionFallback();
  6141. }
  6142. }
  6143. return Result;
  6144. }
  6145. //---------------------------------------------------------------------------
  6146. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6147. {
  6148. if (EnsureCommandSessionFallback(fcAnyCommand))
  6149. {
  6150. DoConsoleDialog(Terminal, Command);
  6151. }
  6152. }
  6153. //---------------------------------------------------------------------------
  6154. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6155. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6156. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6157. {
  6158. if (!IsSideLocalBrowser(osRemote) &&
  6159. IsFileControl(DropSourceControl, osRemote) &&
  6160. (FDDExtMapFile != NULL))
  6161. {
  6162. Accept = true;
  6163. }
  6164. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6165. DebugAssert(FDDTargetControl != NULL);
  6166. }
  6167. //---------------------------------------------------------------------------
  6168. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6169. _di_IDataObject DataObj, int KeyState,
  6170. const TPoint & Point, int & Effect, bool & Accept)
  6171. {
  6172. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6173. }
  6174. //---------------------------------------------------------------------------
  6175. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6176. _di_IDataObject DataObj, int KeyState,
  6177. const TPoint & Point, int & Effect, bool & Accept)
  6178. {
  6179. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6180. }
  6181. //---------------------------------------------------------------------------
  6182. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6183. TObject * Sender)
  6184. {
  6185. DebugUsedParam(Sender);
  6186. DebugAssert(FDDTargetControl == Sender);
  6187. FDDTargetControl = NULL;
  6188. }
  6189. //---------------------------------------------------------------------------
  6190. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6191. {
  6192. FileControlDDDragLeave(SessionsPageControl);
  6193. }
  6194. //---------------------------------------------------------------------------
  6195. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6196. {
  6197. FileControlDDDragLeave(QueueView3);
  6198. }
  6199. //---------------------------------------------------------------------------
  6200. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6201. {
  6202. DebugAssert(!IsSideLocalBrowser(Side));
  6203. DebugUsedParam(Side);
  6204. bool Edit = false;
  6205. TRemoteFile * File = NULL;
  6206. UnicodeString FileName;
  6207. UnicodeString PointTo;
  6208. bool SymbolicLink = true;
  6209. if (RemoteDirView->ItemFocused)
  6210. {
  6211. DebugAssert(RemoteDirView->ItemFocused->Data);
  6212. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6213. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6214. if (Edit)
  6215. {
  6216. FileName = File->FileName;
  6217. PointTo = File->LinkTo;
  6218. }
  6219. else
  6220. {
  6221. PointTo = File->FileName;
  6222. }
  6223. }
  6224. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6225. Terminal->IsCapable[fcHardLink]))
  6226. {
  6227. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6228. if (Edit)
  6229. {
  6230. DebugAssert(File->FileName == FileName);
  6231. int Params = dfNoRecursive;
  6232. Terminal->ExceptionOnFail = true;
  6233. try
  6234. {
  6235. Terminal->DeleteFile(L"", File, &Params);
  6236. }
  6237. __finally
  6238. {
  6239. Terminal->ExceptionOnFail = false;
  6240. }
  6241. }
  6242. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6243. }
  6244. }
  6245. //---------------------------------------------------------------------------
  6246. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6247. {
  6248. return
  6249. (IsSideLocalBrowser(Side) ||
  6250. (HasActiveTerminal() &&
  6251. Terminal->ResolvingSymlinks &&
  6252. Terminal->IsCapable[fcSymbolicLink]));
  6253. }
  6254. //---------------------------------------------------------------------------
  6255. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6256. {
  6257. return
  6258. !IsSideLocalBrowser(FCurrentSide) &&
  6259. (RemoteDirView->ItemFocused != NULL) &&
  6260. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6261. Terminal->SessionData->ResolveSymlinks;
  6262. }
  6263. //---------------------------------------------------------------------------
  6264. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6265. {
  6266. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6267. FForceExecution = true;
  6268. try
  6269. {
  6270. DirView(osCurrent)->ExecuteCurrentFile();
  6271. }
  6272. __finally
  6273. {
  6274. FForceExecution = false;
  6275. }
  6276. }
  6277. //---------------------------------------------------------------------------
  6278. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6279. {
  6280. TEditorData ExternalEditor;
  6281. ExternalEditor.Editor = edExternal;
  6282. bool Remember = false;
  6283. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6284. {
  6285. if (Remember)
  6286. {
  6287. TEditorList * EditorList = new TEditorList;
  6288. try
  6289. {
  6290. *EditorList = *WinConfiguration->EditorList;
  6291. bool Found = false;
  6292. int i = 0;
  6293. while (!Found && (i < EditorList->Count))
  6294. {
  6295. const TEditorPreferences * Editor = EditorList->Editors[i];
  6296. if ((Editor->Data->Editor == edExternal) &&
  6297. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6298. {
  6299. Found = true;
  6300. }
  6301. i++;
  6302. }
  6303. if (!Found)
  6304. {
  6305. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6306. WinConfiguration->EditorList = EditorList;
  6307. }
  6308. }
  6309. __finally
  6310. {
  6311. delete EditorList;
  6312. }
  6313. }
  6314. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6315. }
  6316. }
  6317. //---------------------------------------------------------------------------
  6318. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6319. {
  6320. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6321. if (FFileFindTerminal == ATerminal)
  6322. {
  6323. FFileFindTerminal = NULL;
  6324. HideFileFindDialog();
  6325. }
  6326. if (FClipboardTerminal == ATerminal)
  6327. {
  6328. ClipboardClear(); // implies ClipboardStop
  6329. }
  6330. }
  6331. //---------------------------------------------------------------------------
  6332. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6333. {
  6334. DetachTerminal(Sender);
  6335. }
  6336. //---------------------------------------------------------------------------
  6337. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6338. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6339. {
  6340. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6341. DebugAssert(Terminal != NULL);
  6342. if (Data->Terminal == Terminal)
  6343. {
  6344. Data->Terminal = NULL;
  6345. }
  6346. }
  6347. //---------------------------------------------------------------------------
  6348. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6349. {
  6350. return NULL;
  6351. }
  6352. //---------------------------------------------------------------------------
  6353. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6354. {
  6355. UpdateControls();
  6356. SessionColor = TColor(0);
  6357. UpdateRemotePathComboBox(false);
  6358. NeedSession(false);
  6359. }
  6360. //---------------------------------------------------------------------------
  6361. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6362. {
  6363. try
  6364. {
  6365. // Cache, as the login dialog can change its value.
  6366. // See also DoShow. Also see TTerminalManager::NewSession.
  6367. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6368. try
  6369. {
  6370. if (ShowLogin)
  6371. {
  6372. bool ReloadSessions = !Startup;
  6373. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6374. }
  6375. }
  6376. __finally
  6377. {
  6378. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6379. // (so there was no chance for the user to open any session yet)
  6380. if ((ShowLogin || !Startup) &&
  6381. !WinConfiguration->KeepOpenWhenNoSession &&
  6382. // this rules out the implicit Commander's local-local browser
  6383. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6384. {
  6385. TerminateApplication();
  6386. }
  6387. }
  6388. }
  6389. catch (EAbort &)
  6390. {
  6391. // swallow
  6392. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6393. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6394. }
  6395. }
  6396. //---------------------------------------------------------------------------
  6397. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6398. {
  6399. TTerminalManager * Manager = TTerminalManager::Instance();
  6400. DebugAssert(!Manager->Updating);
  6401. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6402. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6403. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6404. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6405. try
  6406. {
  6407. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6408. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6409. {
  6410. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6411. }
  6412. for (int Index = 0; Index <= Manager->Count; Index++)
  6413. {
  6414. TThemeTabSheet * TabSheet;
  6415. if (Index >= SessionsPageControl->PageCount)
  6416. {
  6417. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6418. TabSheet->PageControl = SessionsPageControl;
  6419. }
  6420. else
  6421. {
  6422. TabSheet = SessionsPageControl->Pages[Index];
  6423. }
  6424. bool IsSessionTab = (Index < Manager->Count);
  6425. if (IsSessionTab)
  6426. {
  6427. TTerminal * Terminal = Manager->Sessions[Index];
  6428. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6429. TabSheet->CaptionTruncation = tttEllipsis;
  6430. UpdateSessionTab(TabSheet);
  6431. }
  6432. else
  6433. {
  6434. TabSheet->Tag = 0; // not really needed
  6435. TabSheet->Shadowed = false;
  6436. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6437. TabSheet->CaptionTruncation = tttNoText;
  6438. // 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)
  6439. UpdateNewTabTab();
  6440. }
  6441. }
  6442. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6443. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6444. {
  6445. SessionsPageControl->UpdateTabsCaptionTruncation();
  6446. }
  6447. }
  6448. __finally
  6449. {
  6450. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6451. }
  6452. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6453. }
  6454. //---------------------------------------------------------------------------
  6455. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6456. {
  6457. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6458. }
  6459. //---------------------------------------------------------------------------
  6460. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6461. {
  6462. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6463. UnicodeString TabCaption;
  6464. if (WinConfiguration->SelectiveToolbarText)
  6465. {
  6466. TabCaption = GetNewTabTabCaption();
  6467. }
  6468. TabSheet->BaseCaption = TabCaption;
  6469. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6470. }
  6471. //---------------------------------------------------------------------------
  6472. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6473. {
  6474. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6475. }
  6476. //---------------------------------------------------------------------------
  6477. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6478. {
  6479. DebugFail();
  6480. return UnicodeString();
  6481. }
  6482. //---------------------------------------------------------------------------
  6483. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6484. {
  6485. if (DebugAlwaysTrue(TabSheet != NULL))
  6486. {
  6487. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6488. if (DebugAlwaysTrue(ASession != NULL))
  6489. {
  6490. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6491. if (ASession->LocalBrowser)
  6492. {
  6493. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6494. }
  6495. else
  6496. {
  6497. TabSheet->ImageIndex = AddSessionColor(Color);
  6498. }
  6499. TTerminalManager * Manager = TTerminalManager::Instance();
  6500. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6501. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6502. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6503. }
  6504. }
  6505. }
  6506. //---------------------------------------------------------------------------
  6507. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6508. {
  6509. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6510. }
  6511. //---------------------------------------------------------------------------
  6512. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6513. {
  6514. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6515. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6516. bool Result = (Session != NULL);
  6517. if (Result)
  6518. {
  6519. TTerminalManager::Instance()->ActiveSession = Session;
  6520. }
  6521. else
  6522. {
  6523. try
  6524. {
  6525. NewTab();
  6526. }
  6527. __finally
  6528. {
  6529. SessionListChanged();
  6530. }
  6531. FSessionsPageControlNewTabTime = Now();
  6532. }
  6533. return Result;
  6534. }
  6535. //---------------------------------------------------------------------------
  6536. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6537. {
  6538. SessionTabSwitched();
  6539. }
  6540. //---------------------------------------------------------------------------
  6541. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6542. {
  6543. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6544. DebugAssert(TransferList != NULL);
  6545. UnicodeString Name;
  6546. if (TransferList->ItemIndex <= 0)
  6547. {
  6548. Name = L"";
  6549. }
  6550. else
  6551. {
  6552. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6553. }
  6554. if (FCopyParamAutoSelected.IsEmpty())
  6555. {
  6556. // if previous preset was not autoselected, make new preset the "default"
  6557. FCopyParamDefault = Name;
  6558. }
  6559. GUIConfiguration->CopyParamCurrent = Name;
  6560. }
  6561. //---------------------------------------------------------------------------
  6562. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6563. {
  6564. // sanity check
  6565. bool ExistingPreset =
  6566. (FTransferListHoverIndex >= 0) &&
  6567. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6568. DebugAssert(ExistingPreset);
  6569. if (ExistingPreset)
  6570. {
  6571. HDC DC = GetDC(0);
  6572. TCanvas * Canvas = new TCanvas();
  6573. try
  6574. {
  6575. Canvas->Handle = DC;
  6576. Canvas->Font = GetToolbarFont(this);
  6577. UnicodeString Name;
  6578. if (FTransferListHoverIndex == 0)
  6579. {
  6580. Name = L"";
  6581. }
  6582. else
  6583. {
  6584. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6585. }
  6586. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6587. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6588. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6589. static_cast<TObject*>(GetComponent(fcTransferList)));
  6590. UnicodeString InfoStr =
  6591. GUIConfiguration->CopyParamPreset[Name].
  6592. GetInfoStr(L"; ",
  6593. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6594. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6595. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6596. {
  6597. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6598. {
  6599. InfoStr.SetLength(InfoStr.Length() - 1);
  6600. }
  6601. InfoStr += Ellipsis;
  6602. }
  6603. // UpdateCaption does not cause invalidation of whole submenu, while
  6604. // setting Caption property does.
  6605. // also it probably does not resize the label, even if necessary
  6606. // (we do not want that anyway)
  6607. TransferLabel->UpdateCaption(InfoStr);
  6608. }
  6609. __finally
  6610. {
  6611. Canvas->Handle = NULL;
  6612. ReleaseDC(0, DC);
  6613. delete Canvas;
  6614. }
  6615. }
  6616. }
  6617. //---------------------------------------------------------------------------
  6618. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6619. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6620. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6621. {
  6622. if (FTransferListHoverIndex != AHoverIndex)
  6623. {
  6624. FTransferListHoverIndex = AHoverIndex;
  6625. UpdateTransferLabel();
  6626. }
  6627. }
  6628. //---------------------------------------------------------------------------
  6629. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6630. {
  6631. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6632. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6633. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6634. {
  6635. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6636. {
  6637. if (!NonVisualDataModule->Busy)
  6638. {
  6639. OpenDirectory(GetSide(osCurrent));
  6640. }
  6641. Message.Result = 1;
  6642. }
  6643. else
  6644. {
  6645. TForm::Dispatch(&Message);
  6646. }
  6647. }
  6648. else
  6649. {
  6650. TForm::Dispatch(&Message);
  6651. }
  6652. }
  6653. //---------------------------------------------------------------------------
  6654. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6655. {
  6656. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6657. if ((FIgnoreNextDialogChar != 0) &&
  6658. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6659. {
  6660. Message.Result = 1;
  6661. }
  6662. else
  6663. {
  6664. TForm::Dispatch(&Message);
  6665. }
  6666. FIgnoreNextDialogChar = 0;
  6667. }
  6668. //---------------------------------------------------------------------------
  6669. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6670. {
  6671. // The four low-order bits are used internally by Windows
  6672. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6673. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6674. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6675. // 61730 - restore thru double click - undocumented
  6676. // 61490 - maximize thru double click - undocumented
  6677. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6678. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6679. {
  6680. SysResizing(Cmd);
  6681. }
  6682. TForm::Dispatch(&Message);
  6683. }
  6684. //---------------------------------------------------------------------------
  6685. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6686. {
  6687. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6688. // Also there no point returning TRUE as we are not able to
  6689. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6690. // Hence the process termination might be safer :)
  6691. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6692. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6693. {
  6694. Message.Result = FALSE;
  6695. }
  6696. else
  6697. {
  6698. Message.Result = TRUE;
  6699. }
  6700. // Do not call default handling as that triggers OnCloseQuery,
  6701. // where our implementation will popup confirmation dialogs, what we do not want,
  6702. // as per Vista guidelines:
  6703. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6704. }
  6705. //---------------------------------------------------------------------------
  6706. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6707. {
  6708. if (Message.EndSession && IsApplicationMinimized())
  6709. {
  6710. // WORKAROUND
  6711. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6712. // when it receives WM_ENDSESSION,
  6713. // but that sometimes (particularly when application is minimized) cause crashes.
  6714. // Crash popups message that blocks log off.
  6715. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6716. // so we call ExitProcess() immediately, not even trying to cleanup.
  6717. // It still causes beep, so there's likely some popup, but it does not block
  6718. // log off.
  6719. ExitProcess(0);
  6720. }
  6721. TForm::Dispatch(&Message);
  6722. }
  6723. #ifdef _DEBUG
  6724. //---------------------------------------------------------------------------
  6725. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6726. {
  6727. TForm::Dispatch(&Message);
  6728. }
  6729. #endif
  6730. //---------------------------------------------------------------------------
  6731. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6732. {
  6733. }
  6734. //---------------------------------------------------------------------------
  6735. void __fastcall TCustomScpExplorerForm::DoShow()
  6736. {
  6737. // Called from TCustomScpExplorerForm::CMShowingChanged
  6738. // only now are the controls resized finally, so the size constraints
  6739. // will not conflict with possibly very small window size
  6740. RestoreFormParams();
  6741. // This indicates that the form was rescaled after parameters were loaded and
  6742. // as most probably the parameters were actually saved with the current form scaling
  6743. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6744. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6745. // and no rounding problems will occur.
  6746. // See also RestoreForm.
  6747. // (we should be safe to call RestoreParams unconditionally)
  6748. if (PixelsPerInch != Screen->PixelsPerInch)
  6749. {
  6750. RestoreParams();
  6751. }
  6752. FixControlsPlacement();
  6753. if (Position == poDefaultPosOnly)
  6754. {
  6755. CutFormToDesktop(this);
  6756. }
  6757. TForm::DoShow();
  6758. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6759. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6760. // This was previously in NeedSession, where other related code is.
  6761. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  6762. // unpleasanly shows briefly.
  6763. if ((ManagedSession == NULL) &&
  6764. !WinConfiguration->ShowLoginWhenNoSession &&
  6765. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6766. // This detects if workspace was saved the last time the main widow was closed
  6767. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6768. {
  6769. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false);
  6770. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6771. }
  6772. // Before main window displays
  6773. if (ManagedSession == NULL)
  6774. {
  6775. StartingWithoutSession();
  6776. }
  6777. UpdatePixelsPerInchMainWindowCounter();
  6778. FShowing = true;
  6779. }
  6780. //---------------------------------------------------------------------------
  6781. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6782. {
  6783. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6784. }
  6785. //---------------------------------------------------------------------------
  6786. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  6787. {
  6788. SessionListChanged();
  6789. InitStatusBar();
  6790. UpdateControls();
  6791. }
  6792. //---------------------------------------------------------------------------
  6793. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6794. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6795. TNotifyEvent OnBalloonClick, TObject * UserData)
  6796. {
  6797. bool Do;
  6798. UnicodeString Message;
  6799. if (E == NULL)
  6800. {
  6801. Message = Str;
  6802. Do = true;
  6803. }
  6804. else
  6805. {
  6806. Do = ExceptionMessage(E, Message);
  6807. }
  6808. if (Do && WinConfiguration->BalloonNotifications)
  6809. {
  6810. UnicodeString Title;
  6811. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6812. {
  6813. Message = TrimLeft(Message);
  6814. }
  6815. else
  6816. {
  6817. Message = UnformatMessage(Message);
  6818. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6819. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6820. Title = LoadResourceString(Captions[Type]);
  6821. if (Terminal != NULL)
  6822. {
  6823. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  6824. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  6825. }
  6826. }
  6827. if (Seconds == 0)
  6828. {
  6829. Seconds = WinConfiguration->NotificationsTimeout;
  6830. }
  6831. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6832. }
  6833. }
  6834. //---------------------------------------------------------------------------
  6835. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6836. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6837. UnicodeString HelpKeyword, const TMessageParams * Params,
  6838. TTerminal * Terminal)
  6839. {
  6840. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6841. (FOnFeedSynchronizeError != NULL)) &&
  6842. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6843. {
  6844. if (FOnFeedSynchronizeError != NULL)
  6845. {
  6846. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6847. }
  6848. else
  6849. {
  6850. DebugAssert(FErrorList != NULL);
  6851. TStringList * MoreMessagesCopy = NULL;
  6852. if (MoreMessages)
  6853. {
  6854. MoreMessagesCopy = new TStringList();
  6855. MoreMessagesCopy->Assign(MoreMessages);
  6856. }
  6857. FErrorList->AddObject(Message, MoreMessagesCopy);
  6858. }
  6859. PopupTrayBalloon(Terminal, Message, Type);
  6860. return ContinueAnswer(Answers);
  6861. }
  6862. else
  6863. {
  6864. bool UseBalloon = FTrayIcon->Visible;
  6865. if (UseBalloon)
  6866. {
  6867. PopupTrayBalloon(Terminal, Message, Type);
  6868. }
  6869. unsigned int Result;
  6870. try
  6871. {
  6872. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6873. }
  6874. __finally
  6875. {
  6876. // cancel only balloon we popped up, otherwise we may cancel notification
  6877. // balloon that was there before the message dialog
  6878. // (such as "dd confirmation opt in balloon)
  6879. if (UseBalloon)
  6880. {
  6881. FTrayIcon->CancelBalloon();
  6882. }
  6883. }
  6884. return Result;
  6885. }
  6886. }
  6887. //---------------------------------------------------------------------------
  6888. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6889. TTerminal * Terminal, Exception * E)
  6890. {
  6891. if (FTrayIcon->Visible)
  6892. {
  6893. PopupTrayBalloon(Terminal, L"", qtError, E);
  6894. }
  6895. // particularly prevent opening new session from jump list,
  6896. // while exception is shown
  6897. NonVisualDataModule->StartBusy();
  6898. try
  6899. {
  6900. ShowExtendedExceptionEx(Terminal, E);
  6901. }
  6902. __finally
  6903. {
  6904. NonVisualDataModule->EndBusy();
  6905. FTrayIcon->CancelBalloon();
  6906. }
  6907. }
  6908. //---------------------------------------------------------------------------
  6909. void __fastcall TCustomScpExplorerForm::SessionReady()
  6910. {
  6911. InitStatusBar();
  6912. // cannot rely on active page being page for active terminal,
  6913. // as it can happen that active page is the "new session" page
  6914. // (e.g. when reconnecting active terminal, while login dialog
  6915. // invoked from "new session" page is modal)
  6916. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  6917. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  6918. }
  6919. //---------------------------------------------------------------------------
  6920. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6921. TTerminal * Terminal, Exception * E)
  6922. {
  6923. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6924. if (!Terminal->Active)
  6925. {
  6926. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6927. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6928. {
  6929. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6930. }
  6931. }
  6932. }
  6933. //---------------------------------------------------------------------------
  6934. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6935. UnicodeString Message, TQueryType Type,
  6936. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6937. {
  6938. if ((E == NULL) ||
  6939. ExceptionMessage(E, Message))
  6940. {
  6941. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6942. if (Important)
  6943. {
  6944. Seconds *= 5;
  6945. }
  6946. UnicodeString NoteMessage(UnformatMessage(Message));
  6947. if (Terminal != NULL)
  6948. {
  6949. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  6950. NoteMessage = FORMAT(L"%s: %s",
  6951. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  6952. }
  6953. if (WinConfiguration->BalloonNotifications)
  6954. {
  6955. AddNote(NoteMessage);
  6956. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6957. }
  6958. else
  6959. {
  6960. FlashOnBackground();
  6961. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6962. }
  6963. }
  6964. }
  6965. //---------------------------------------------------------------------------
  6966. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6967. {
  6968. RestoreApp();
  6969. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6970. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6971. !NonVisualDataModule->Busy)
  6972. {
  6973. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6974. TTerminalManager::Instance()->ActiveSession = Terminal;
  6975. if (!ComponentVisible[fcQueueView])
  6976. {
  6977. ToggleQueueVisibility();
  6978. GoToQueue();
  6979. }
  6980. }
  6981. }
  6982. //---------------------------------------------------------------------------
  6983. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6984. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6985. {
  6986. UnicodeString Message;
  6987. TNotifyEvent OnClick = NULL;
  6988. TObject * UserData = NULL;
  6989. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6990. switch (Event)
  6991. {
  6992. case qeEmptyButMonitored:
  6993. if ((ATerminal != Terminal) || QueueInvisible)
  6994. {
  6995. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6996. OnClick = QueueEmptyNoteClicked;
  6997. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6998. TerminalNoteData->Terminal = ATerminal;
  6999. UserData = TerminalNoteData;
  7000. }
  7001. break;
  7002. case qeEmpty:
  7003. OperationComplete(ATerminal->QueueOperationStart);
  7004. break;
  7005. case qePendingUserAction:
  7006. if ((ATerminal != Terminal) ||
  7007. (QueueInvisible && !IsQueueAutoPopup()))
  7008. {
  7009. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7010. }
  7011. break;
  7012. default:
  7013. DebugFail();
  7014. }
  7015. if (!Message.IsEmpty())
  7016. {
  7017. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7018. }
  7019. }
  7020. //---------------------------------------------------------------------------
  7021. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7022. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7023. {
  7024. if (FDDExtMapFile != NULL)
  7025. {
  7026. CloseHandle(FDDExtMapFile);
  7027. FDDExtMapFile = NULL;
  7028. }
  7029. if (WinConfiguration->DDFakeFile)
  7030. {
  7031. DDFakeFileInitDrag(FileList, Created);
  7032. }
  7033. }
  7034. //---------------------------------------------------------------------------
  7035. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7036. {
  7037. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7038. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7039. {
  7040. FFakeFileDropTarget = FileName;
  7041. }
  7042. }
  7043. //---------------------------------------------------------------------------
  7044. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7045. {
  7046. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7047. if (!ForceDirectories(ApiPath(Result)))
  7048. {
  7049. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7050. }
  7051. FileSetAttr(ApiPath(Result), faHidden);
  7052. return Result;
  7053. }
  7054. //---------------------------------------------------------------------------
  7055. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7056. bool & Created)
  7057. {
  7058. FFakeFileDropTarget = UnicodeString();
  7059. FDragFakeDirectory = CreateFakeTransferDirectory();
  7060. FileList->AddItem(NULL, FDragFakeDirectory);
  7061. Created = true;
  7062. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7063. {
  7064. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7065. }
  7066. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7067. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7068. {
  7069. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7070. TDragExtCommStruct* CommStruct;
  7071. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7072. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7073. DebugAssert(CommStruct != NULL);
  7074. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7075. CommStruct->Dragging = true;
  7076. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7077. LENOF(CommStruct->DropDest));
  7078. NULL_TERMINATE(CommStruct->DropDest);
  7079. UnmapViewOfFile(CommStruct);
  7080. }
  7081. }
  7082. //---------------------------------------------------------------------------
  7083. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7084. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7085. {
  7086. bool Result;
  7087. DebugAssert(!IsLocalBrowserMode());
  7088. if (Sender == RemoteDirView)
  7089. {
  7090. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7091. }
  7092. else
  7093. {
  7094. DebugAssert(Sender == RemoteDriveView);
  7095. TStrings * FileList = RemoteDriveView->DragFileList();
  7096. try
  7097. {
  7098. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7099. }
  7100. __finally
  7101. {
  7102. delete FileList;
  7103. }
  7104. }
  7105. if (FDDTargetControl == RemoteDriveView)
  7106. {
  7107. RemoteDriveView->UpdateDropTarget();
  7108. }
  7109. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7110. // or from tree view to dir view.
  7111. // foMove happens when file/dir is dragged from remote tree view outside of
  7112. // application via dragex
  7113. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7114. (DropSourceControl == RemoteDriveView))
  7115. {
  7116. RemoteDriveView->UpdateDropSource();
  7117. }
  7118. return Result;
  7119. }
  7120. //---------------------------------------------------------------------------
  7121. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7122. {
  7123. DebugAssert(!IsLocalBrowserMode());
  7124. // This also handles drops of remote files to queue.
  7125. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7126. SAFE_DESTROY(FDDFileList);
  7127. if (!FFakeFileDropTarget.IsEmpty())
  7128. {
  7129. RemoveDir(ApiPath(FFakeFileDropTarget));
  7130. }
  7131. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7132. {
  7133. try
  7134. {
  7135. TDragResult DDResult = (Sender == RemoteDirView) ?
  7136. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7137. // Focus is moved to the target application,
  7138. // but as we are going to present the UI, we need to steal the focus back.
  7139. // This most likely won't work though (windows does not allow application
  7140. // to steal focus most of the time)
  7141. Application->BringToFront();
  7142. // On older version of Windows we never got drMove here, see also comment below.
  7143. // On Windows 10, we get the "move".
  7144. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7145. {
  7146. UnicodeString TargetDirectory;
  7147. TFileOperation Operation;
  7148. // drInvalid may mean drMove, see comment below
  7149. switch (DDResult)
  7150. {
  7151. case drCopy:
  7152. Operation = foCopy;
  7153. break;
  7154. case drMove:
  7155. Operation = foMove;
  7156. break;
  7157. default:
  7158. DebugFail();
  7159. case drInvalid:
  7160. // prefer "copy" for safety
  7161. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7162. break;
  7163. }
  7164. TTransferOperationParam Param;
  7165. UnicodeString CounterName;
  7166. bool ForceQueue;
  7167. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7168. {
  7169. // we get drInvalid both if d&d was intercepted by ddext,
  7170. // and when users drops on no-drop location.
  7171. // we tell the difference by existence of response from ddext,
  7172. // so we ignore absence of response in this case
  7173. if (DDResult != drInvalid)
  7174. {
  7175. // here we know that the extension is installed,
  7176. // as it is checked as soon as drag&drop starts from
  7177. // RemoteFileControlDDCreateDragFileList
  7178. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7179. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7180. {
  7181. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7182. }
  7183. else
  7184. {
  7185. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7186. }
  7187. }
  7188. }
  7189. else
  7190. {
  7191. // download using fake file
  7192. Param.Temp = false;
  7193. Param.DragDrop = true;
  7194. if (ForceQueue)
  7195. {
  7196. Param.Queue = asOn;
  7197. }
  7198. if (Sender == RemoteDirView)
  7199. {
  7200. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7201. }
  7202. if (RemoteFileControlFileOperation(Sender, Operation,
  7203. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7204. {
  7205. Configuration->Usage->Inc(CounterName);
  7206. }
  7207. }
  7208. }
  7209. }
  7210. __finally
  7211. {
  7212. CloseHandle(FDDExtMapFile);
  7213. FDDExtMapFile = NULL;
  7214. delete FDragFakeMonitors;
  7215. FDragFakeMonitors = NULL;
  7216. RemoveDir(ApiPath(FDragFakeDirectory));
  7217. FDragFakeDirectory = L"";
  7218. FFakeFileDropTarget = UnicodeString();
  7219. }
  7220. }
  7221. if (FDragDropSshTerminate.get() != NULL)
  7222. {
  7223. FDragDropSshTerminate->Rethrow();
  7224. }
  7225. }
  7226. //---------------------------------------------------------------------------
  7227. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7228. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7229. {
  7230. // Remember drop effect so we know (when user drops files), if we copy or move
  7231. FLastDropEffect = dwEffect;
  7232. }
  7233. //---------------------------------------------------------------------------
  7234. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7235. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7236. {
  7237. bool Result;
  7238. if (FDDTargetControl == QueueView3)
  7239. {
  7240. Directory = DefaultDownloadTargetDirectory();
  7241. Result = true;
  7242. CounterName = L"DownloadsDragDropQueue";
  7243. ForceQueue = true;
  7244. }
  7245. else if (!FFakeFileDropTarget.IsEmpty())
  7246. {
  7247. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7248. Result = true;
  7249. ForceQueue = false;
  7250. CounterName = L"DownloadsDragDropFakeFile";
  7251. }
  7252. else
  7253. {
  7254. ForceQueue = false;
  7255. Enabled = false;
  7256. try
  7257. {
  7258. int Timer = 0;
  7259. Result = false;
  7260. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7261. {
  7262. {
  7263. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7264. TDragExtCommStruct* CommStruct;
  7265. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7266. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7267. DebugAssert(CommStruct != NULL);
  7268. Result = !CommStruct->Dragging;
  7269. if (Result)
  7270. {
  7271. Directory = ExtractFilePath(CommStruct->DropDest);
  7272. CounterName = L"DownloadsDragDropExternalExt";
  7273. }
  7274. UnmapViewOfFile(CommStruct);
  7275. }
  7276. if (!Result)
  7277. {
  7278. Sleep(50);
  7279. Timer += 50;
  7280. Application->ProcessMessages();
  7281. }
  7282. }
  7283. }
  7284. __finally
  7285. {
  7286. Enabled = true;
  7287. }
  7288. }
  7289. return Result;
  7290. }
  7291. //---------------------------------------------------------------------------
  7292. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7293. const UnicodeString TempDir, int SecDelay)
  7294. {
  7295. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7296. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7297. if (FDelayedDeletionTimer == NULL)
  7298. {
  7299. DebugAssert(HandleAllocated());
  7300. FDelayedDeletionTimer = new TTimer(this);
  7301. FDelayedDeletionTimer->Interval = 10000;
  7302. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7303. }
  7304. else
  7305. {
  7306. FDelayedDeletionTimer->Enabled = true;
  7307. }
  7308. }
  7309. //---------------------------------------------------------------------------
  7310. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7311. {
  7312. DebugAssert(FDelayedDeletionList != NULL);
  7313. TDateTime N = Now();
  7314. TDateTime Alert;
  7315. UnicodeString Directory;
  7316. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7317. {
  7318. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7319. if ((N >= Alert) || (Sender == NULL))
  7320. {
  7321. Directory = FDelayedDeletionList->Strings[Index];
  7322. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7323. {
  7324. FDelayedDeletionList->Delete(Index);
  7325. }
  7326. }
  7327. }
  7328. if (FDelayedDeletionList->Count == 0)
  7329. {
  7330. FDelayedDeletionTimer->Enabled = false;
  7331. }
  7332. }
  7333. //---------------------------------------------------------------------------
  7334. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7335. {
  7336. DebugAssert(!IsLocalBrowserMode());
  7337. if (IsFileControl(FDDTargetControl, osRemote) ||
  7338. (FDDTargetControl == SessionsPageControl))
  7339. {
  7340. TManagedTerminal * TargetTerminal = NULL;
  7341. TFileOperation Operation = foNone;
  7342. if (FDDTargetControl == SessionsPageControl)
  7343. {
  7344. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7345. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7346. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7347. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7348. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7349. {
  7350. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7351. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7352. {
  7353. Operation = foRemoteMove;
  7354. }
  7355. else
  7356. {
  7357. Operation = foRemoteCopy;
  7358. }
  7359. }
  7360. }
  7361. else
  7362. {
  7363. if (FLastDropEffect == DROPEFFECT_MOVE)
  7364. {
  7365. Operation = foRemoteMove;
  7366. }
  7367. else if (FLastDropEffect == DROPEFFECT_COPY)
  7368. {
  7369. Operation = foRemoteCopy;
  7370. }
  7371. }
  7372. if (Operation != foNone)
  7373. {
  7374. RemoteFileControlFileOperation(DropSourceControl,
  7375. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7376. }
  7377. // abort drag&drop
  7378. Abort();
  7379. }
  7380. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7381. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7382. (FDDTargetControl != QueueView3))
  7383. {
  7384. DebugAssert(!FDragTempDir.IsEmpty());
  7385. TTransferType Type;
  7386. UnicodeString TempDir = FDragTempDir;
  7387. // We clear FDragTempDir before calling
  7388. // just in case it fail (raises exception)
  7389. FDragTempDir = L"";
  7390. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7391. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7392. // empty directory parameter means temp directory -> don't display it!
  7393. UnicodeString TargetDir = L"";
  7394. int Options = 0;
  7395. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7396. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7397. {
  7398. Abort();
  7399. }
  7400. // TargetDir is set when dropped on local file control
  7401. // (this was workaround for legacy dirview event handling, now it should be
  7402. // made prettier)
  7403. if (TargetDir.IsEmpty())
  7404. {
  7405. TargetDir = TempDir;
  7406. if (ForceDirectories(ApiPath(TargetDir)))
  7407. {
  7408. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7409. FPendingTempSpaceWarn = true;
  7410. try
  7411. {
  7412. FDragDropOperation = true;
  7413. int Params = cpTemporary |
  7414. (Type == ttMove ? cpDelete : 0);
  7415. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7416. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7417. }
  7418. __finally
  7419. {
  7420. FDragDropOperation = false;
  7421. FPendingTempSpaceWarn = false;
  7422. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7423. }
  7424. }
  7425. else
  7426. {
  7427. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7428. }
  7429. }
  7430. }
  7431. }
  7432. //---------------------------------------------------------------------------
  7433. void __fastcall TCustomScpExplorerForm::DDDownload(
  7434. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7435. {
  7436. DebugAssert(!IsLocalBrowserMode());
  7437. TAutoBatch AutoBatch(this);
  7438. UpdateCopyParamCounters(*CopyParam);
  7439. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7440. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7441. {
  7442. RemoteDriveView->UpdateDropSource();
  7443. }
  7444. }
  7445. //---------------------------------------------------------------------------
  7446. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7447. {
  7448. public:
  7449. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7450. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7451. {
  7452. }
  7453. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7454. {
  7455. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7456. TDataObjectFilesEx::AllowData(FormatEtc);
  7457. }
  7458. };
  7459. //---------------------------------------------------------------------------
  7460. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7461. TObject * Sender, TDataObject *& DataObject)
  7462. {
  7463. if (FDDExtMapFile != NULL)
  7464. {
  7465. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7466. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7467. {
  7468. Abort();
  7469. }
  7470. if (FileList->Count > 0)
  7471. {
  7472. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7473. if (!FilesObject->IsValid(true, true))
  7474. {
  7475. FilesObject->_Release();
  7476. }
  7477. else
  7478. {
  7479. DataObject = FilesObject;
  7480. }
  7481. }
  7482. }
  7483. }
  7484. //---------------------------------------------------------------------------
  7485. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7486. {
  7487. DebugFail();
  7488. }
  7489. //---------------------------------------------------------------------------
  7490. void __fastcall TCustomScpExplorerForm::GoToTree()
  7491. {
  7492. ComponentVisible[fcRemoteTree] = true;
  7493. DriveView(osRemote)->SetFocus();
  7494. }
  7495. //---------------------------------------------------------------------------
  7496. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7497. TPanelExport Export)
  7498. {
  7499. TCustomDirView * DirView = this->DirView(Side);
  7500. std::unique_ptr<TStrings> ExportData(new TStringList());
  7501. switch (Export)
  7502. {
  7503. case pePath:
  7504. ExportData->Add(DirView->PathName);
  7505. break;
  7506. case peFileList:
  7507. case peFullFileList:
  7508. {
  7509. bool FullPath = (Export == peFullFileList);
  7510. DirView->CreateFileList(false, FullPath, ExportData.get());
  7511. for (int Index = 0; Index < ExportData->Count; Index++)
  7512. {
  7513. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7514. {
  7515. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7516. }
  7517. }
  7518. }
  7519. break;
  7520. default:
  7521. DebugFail();
  7522. }
  7523. return ExportData.release();
  7524. }
  7525. //---------------------------------------------------------------------------
  7526. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7527. TPanelExport Export, TPanelExportDestination Destination)
  7528. {
  7529. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7530. PanelExportStore(Side, Export, Destination, ExportData.get());
  7531. }
  7532. //---------------------------------------------------------------------------
  7533. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7534. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7535. TStrings * ExportData)
  7536. {
  7537. if (Destination == pedClipboard)
  7538. {
  7539. TInstantOperationVisualizer Visualizer;
  7540. CopyToClipboard(ExportData);
  7541. }
  7542. else
  7543. {
  7544. DebugFail();
  7545. }
  7546. }
  7547. //---------------------------------------------------------------------------
  7548. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7549. {
  7550. TCustomDirView * DirView = this->DirView(Side);
  7551. UnicodeString Mask = DirView->Mask;
  7552. if (DoFilterMaskDialog(DirView, Mask))
  7553. {
  7554. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7555. Configuration->Usage->Inc(L"Filters");
  7556. }
  7557. }
  7558. //---------------------------------------------------------------------------
  7559. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7560. {
  7561. return FQueueController->DefaultOperation();
  7562. }
  7563. //---------------------------------------------------------------------------
  7564. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7565. TQueueOperation Operation, void ** Param)
  7566. {
  7567. switch (Operation)
  7568. {
  7569. case qoPreferences:
  7570. return true;
  7571. case qoGoTo:
  7572. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7573. case qoOnceEmpty:
  7574. return IsAnythingQueued();
  7575. default:
  7576. return FQueueController->AllowOperation(Operation, Param);
  7577. }
  7578. }
  7579. //---------------------------------------------------------------------------
  7580. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7581. {
  7582. if (DebugAlwaysTrue(QueueView3->Visible))
  7583. {
  7584. QueueView3->SetFocus();
  7585. }
  7586. }
  7587. //---------------------------------------------------------------------------
  7588. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7589. TQueueOperation Operation, void * Param)
  7590. {
  7591. if (Operation == qoGoTo)
  7592. {
  7593. GoToQueue();
  7594. }
  7595. else if (Operation == qoPreferences)
  7596. {
  7597. PreferencesDialog(pmQueue);
  7598. }
  7599. else
  7600. {
  7601. FQueueController->ExecuteOperation(Operation, Param);
  7602. }
  7603. }
  7604. //---------------------------------------------------------------------------
  7605. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7606. {
  7607. return (Queue != NULL) && Queue->Enabled;
  7608. }
  7609. //---------------------------------------------------------------------------
  7610. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7611. {
  7612. DebugAssert(Queue != NULL);
  7613. if (Queue != NULL)
  7614. {
  7615. Queue->Enabled = !Queue->Enabled;
  7616. }
  7617. }
  7618. //---------------------------------------------------------------------------
  7619. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7620. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7621. {
  7622. FQueueActedItem = QueueView3->ItemFocused;
  7623. }
  7624. //---------------------------------------------------------------------------
  7625. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7626. {
  7627. return TopDock->Height + QueueSplitter->Height;
  7628. }
  7629. //---------------------------------------------------------------------------
  7630. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7631. {
  7632. return
  7633. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7634. (QueueDock->Visible ? QueueDock->Height : 0) +
  7635. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7636. }
  7637. //---------------------------------------------------------------------------
  7638. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7639. {
  7640. return ScaleByTextHeight(this, 48);
  7641. }
  7642. //---------------------------------------------------------------------------
  7643. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7644. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7645. {
  7646. // when queue is hidden by double-clicking splitter, stray attempt to
  7647. // resize the panel with strange value arrives, make sure it is ignored
  7648. if (ComponentVisible[fcQueueView])
  7649. {
  7650. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7651. if (NewSize > MaxHeightLimit)
  7652. {
  7653. NewSize = MaxHeightLimit;
  7654. }
  7655. int MinHeightLimit =
  7656. GetStaticQueuePanelComponentsHeight() +
  7657. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7658. GetMinQueueViewHeight();
  7659. if (NewSize < MinHeightLimit)
  7660. {
  7661. NewSize = MinHeightLimit;
  7662. }
  7663. }
  7664. else
  7665. {
  7666. Accept = false;
  7667. }
  7668. }
  7669. //---------------------------------------------------------------------------
  7670. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7671. {
  7672. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7673. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7674. if (QueueViewHeight < GetMinQueueViewHeight())
  7675. {
  7676. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7677. }
  7678. }
  7679. //---------------------------------------------------------------------------
  7680. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7681. TDragObject *& /*DragObject*/)
  7682. {
  7683. FQueueActedItem = QueueView3->ItemFocused;
  7684. }
  7685. //---------------------------------------------------------------------------
  7686. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7687. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7688. {
  7689. Accept = true;
  7690. if (Source == QueueView3)
  7691. {
  7692. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7693. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7694. if (Accept)
  7695. {
  7696. TQueueItemProxy * QueueItem;
  7697. TQueueItemProxy * DestQueueItem;
  7698. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7699. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7700. Accept = (QueueItem != DestQueueItem) &&
  7701. (QueueItem->Status == TQueueItem::qsPending) &&
  7702. (DestQueueItem->Status == TQueueItem::qsPending);
  7703. }
  7704. }
  7705. }
  7706. //---------------------------------------------------------------------------
  7707. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7708. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7709. {
  7710. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7711. {
  7712. TQueueItemProxy * QueueItem;
  7713. TQueueItemProxy * DestQueueItem;
  7714. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7715. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7716. QueueItem->Move(DestQueueItem);
  7717. }
  7718. }
  7719. //---------------------------------------------------------------------------
  7720. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7721. {
  7722. if ((QueueView3->ItemFocused == NULL) &&
  7723. (QueueView3->Items->Count > 0))
  7724. {
  7725. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7726. }
  7727. QueueLabelUpdateStatus();
  7728. }
  7729. //---------------------------------------------------------------------------
  7730. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7731. {
  7732. QueueLabelUpdateStatus();
  7733. }
  7734. //---------------------------------------------------------------------------
  7735. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7736. {
  7737. QueueLabel->UpdateStatus();
  7738. }
  7739. //---------------------------------------------------------------------------
  7740. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7741. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7742. {
  7743. if (Selected)
  7744. {
  7745. NonVisualDataModule->UpdateNonVisibleActions();
  7746. }
  7747. }
  7748. //---------------------------------------------------------------------------
  7749. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7750. {
  7751. TDragDropFilesEx * Result = NULL;
  7752. if (Sender == SessionsPageControl)
  7753. {
  7754. Result = FSessionsDragDropFilesEx;
  7755. }
  7756. else if (Sender == QueueView3)
  7757. {
  7758. Result = FQueueDragDropFilesEx;
  7759. }
  7760. else
  7761. {
  7762. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7763. if (DirView != NULL)
  7764. {
  7765. Result = DirView->DragDropFilesEx;
  7766. }
  7767. else
  7768. {
  7769. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7770. if (DriveView != NULL)
  7771. {
  7772. Result = DriveView->DragDropFilesEx;
  7773. }
  7774. }
  7775. }
  7776. DebugAssert(Result != NULL);
  7777. return Result;
  7778. }
  7779. //---------------------------------------------------------------------------
  7780. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7781. {
  7782. return (DView->SelCount == DView->FilesCount);
  7783. }
  7784. //---------------------------------------------------------------------------
  7785. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7786. {
  7787. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7788. }
  7789. //---------------------------------------------------------------------------
  7790. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7791. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7792. {
  7793. DebugAssert(!IsLocalBrowserMode());
  7794. TFileOperation Operation;
  7795. switch (Effect)
  7796. {
  7797. case DROPEFFECT_MOVE:
  7798. Operation = foMove;
  7799. break;
  7800. case DROPEFFECT_COPY:
  7801. // occures on WinXP (reported by user)
  7802. default:
  7803. Operation = foCopy;
  7804. break;
  7805. };
  7806. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7807. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7808. if (DragDropFilesEx->FileList->Count > 0)
  7809. {
  7810. TStrings * FileList = new TStringList();
  7811. try
  7812. {
  7813. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7814. {
  7815. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7816. }
  7817. FDragDropOperation = true;
  7818. TTransferOperationParam Param;
  7819. Param.TargetDirectory = TargetPath;
  7820. // upload, no temp dirs
  7821. Param.Temp = false;
  7822. Param.DragDrop = DragDrop;
  7823. Param.Options =
  7824. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7825. if (ForceQueue)
  7826. {
  7827. Param.Queue = asOn;
  7828. }
  7829. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7830. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7831. {
  7832. if (IsFileControl(DropSourceControl, osLocal))
  7833. {
  7834. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7835. }
  7836. else
  7837. {
  7838. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7839. }
  7840. }
  7841. }
  7842. __finally
  7843. {
  7844. FDragDropOperation = false;
  7845. delete FileList;
  7846. }
  7847. }
  7848. }
  7849. //---------------------------------------------------------------------------
  7850. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7851. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7852. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7853. {
  7854. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7855. }
  7856. //---------------------------------------------------------------------------
  7857. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7858. TObject * Sender, int grfKeyState, int & dwEffect)
  7859. {
  7860. DebugAssert(!IsLocalBrowserMode());
  7861. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7862. if ((dwEffect != DROPEFFECT_NONE) &&
  7863. IsFileControl(DropSourceControl, osRemote))
  7864. {
  7865. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7866. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7867. (RemoteDirView->DropTarget == NULL))
  7868. {
  7869. dwEffect = DROPEFFECT_NONE;
  7870. }
  7871. else
  7872. {
  7873. if (dwEffect == DROPEFFECT_COPY)
  7874. {
  7875. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  7876. // currently we support copying always (at least via temporary directory);
  7877. // remove associated checks once this all proves stable and working
  7878. bool CopyCapable = true;
  7879. // if we do not support neither of operations, there's no discussion
  7880. if (!MoveCapable && !CopyCapable)
  7881. {
  7882. dwEffect = DROPEFFECT_NONE;
  7883. }
  7884. else
  7885. {
  7886. // The default effect inside remote panel is move,
  7887. // unless we do not support it, but support copy
  7888. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7889. {
  7890. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7891. }
  7892. else
  7893. {
  7894. // with ctrl-down, we want copy unless it is not supported
  7895. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7896. }
  7897. }
  7898. }
  7899. }
  7900. }
  7901. }
  7902. //---------------------------------------------------------------------------
  7903. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7904. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7905. {
  7906. DebugAssert(!IsLocalBrowserMode());
  7907. if (FDDTotalSize >= 0)
  7908. {
  7909. if (File->IsDirectory)
  7910. {
  7911. FDDTotalSize = -1;
  7912. }
  7913. else
  7914. {
  7915. FDDTotalSize += File->Size;
  7916. }
  7917. }
  7918. DebugAssert(!FDragTempDir.IsEmpty());
  7919. // TODO: this is quite ineffective
  7920. // TODO: what if invalid character replacement is disabled?
  7921. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7922. UnicodeString TransferFileName;
  7923. if (Sender == RemoteDriveView)
  7924. {
  7925. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7926. }
  7927. else
  7928. {
  7929. TransferFileName = File->FileName;
  7930. }
  7931. FDDFileList->AddObject(TransferFileName, File);
  7932. }
  7933. //---------------------------------------------------------------------------
  7934. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7935. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7936. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7937. {
  7938. // sometimes we do not get DDEnd so the list is not released
  7939. SAFE_DESTROY(FDDFileList);
  7940. FDDFileList = new TStringList();
  7941. FDragTempDir = WinConfiguration->TemporaryDir();
  7942. FDDTotalSize = 0;
  7943. FDragDropSshTerminate.reset(NULL);
  7944. }
  7945. //---------------------------------------------------------------------------
  7946. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7947. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7948. HRESULT & Result)
  7949. {
  7950. if (Result == DRAGDROP_S_DROP)
  7951. {
  7952. try
  7953. {
  7954. GlobalDragImageList->HideDragImage();
  7955. try
  7956. {
  7957. RemoteFileControlDDTargetDrop();
  7958. }
  7959. catch(ESshTerminate & E)
  7960. {
  7961. FDragDropSshTerminate.reset(E.Clone());
  7962. }
  7963. }
  7964. catch (Exception &E)
  7965. {
  7966. // If downloading fails we need to cancel drag&drop, otherwise
  7967. // Explorer shows error
  7968. // But by the way exception probably never reach this point as
  7969. // it's catched on way.
  7970. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7971. Result = DRAGDROP_S_CANCEL;
  7972. ShowExtendedException(Terminal, &E);
  7973. }
  7974. }
  7975. }
  7976. //---------------------------------------------------------------------------
  7977. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7978. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7979. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7980. {
  7981. TFileMasks::TParams MaskParams;
  7982. MaskParams.Size = Size;
  7983. MaskParams.Modification = Modification;
  7984. // this does not re-parse the mask if it is the same as the last time
  7985. FDirViewMatchMask = Masks;
  7986. bool ImplicitMatch;
  7987. Matches =
  7988. // RecurseInclude parameter has no effect as the Path is empty
  7989. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7990. (AllowImplicitMatches || !ImplicitMatch);
  7991. }
  7992. //---------------------------------------------------------------------------
  7993. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7994. TObject * Sender, TListItem * Item, WORD & Indexes)
  7995. {
  7996. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7997. UnicodeString Ext;
  7998. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7999. {
  8000. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  8001. }
  8002. else
  8003. {
  8004. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  8005. }
  8006. if (SameText(Ext, Configuration->PartialExt))
  8007. {
  8008. Indexes |= oiPartial;
  8009. }
  8010. }
  8011. //---------------------------------------------------------------------------
  8012. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8013. {
  8014. return
  8015. DirViewEnabled(osCurrent) &&
  8016. DirView(osCurrent)->CanPasteFromClipBoard();
  8017. }
  8018. //---------------------------------------------------------------------------
  8019. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8020. {
  8021. bool Result = false;
  8022. if (CanPasteToDirViewFromClipBoard())
  8023. {
  8024. Result = true;
  8025. }
  8026. else
  8027. {
  8028. UnicodeString ClipboardText;
  8029. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8030. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8031. {
  8032. if (StoredSessions->IsUrl(ClipboardText))
  8033. {
  8034. Result = true;
  8035. }
  8036. else
  8037. {
  8038. Result = DirViewEnabled(osCurrent);
  8039. }
  8040. }
  8041. }
  8042. return Result;
  8043. }
  8044. //---------------------------------------------------------------------------
  8045. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8046. {
  8047. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8048. if (DoesClipboardContainOurFiles())
  8049. {
  8050. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8051. {
  8052. DebugAssert(!IsLocalBrowserMode());
  8053. TTerminalManager * Manager = TTerminalManager::Instance();
  8054. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8055. Manager->ActiveSession = FClipboardTerminal;
  8056. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8057. }
  8058. }
  8059. else if (CanPasteToDirViewFromClipBoard())
  8060. {
  8061. DirView(osCurrent)->PasteFromClipBoard();
  8062. }
  8063. else
  8064. {
  8065. UnicodeString ClipboardText;
  8066. if (NonEmptyTextFromClipboard(ClipboardText))
  8067. {
  8068. if (StoredSessions->IsUrl(ClipboardText))
  8069. {
  8070. NewSession(ClipboardText);
  8071. }
  8072. else
  8073. {
  8074. DirView(osCurrent)->Path = ClipboardText;
  8075. }
  8076. }
  8077. }
  8078. }
  8079. //---------------------------------------------------------------------------
  8080. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8081. {
  8082. // TBD
  8083. }
  8084. //---------------------------------------------------------------------------
  8085. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8086. {
  8087. TCustomDirView * ADirView = DirView(Side);
  8088. ADirView->SelectAll(Mode);
  8089. ADirView->SetFocus();
  8090. }
  8091. //---------------------------------------------------------------------------
  8092. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8093. {
  8094. TCustomDirView * ADirView = DirView(Side);
  8095. TFileFilter Filter;
  8096. DefaultFileFilter(Filter);
  8097. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8098. {
  8099. Configuration->Usage->Inc(L"MaskSelections");
  8100. ADirView->SelectFiles(Filter, Select);
  8101. ADirView->SetFocus();
  8102. }
  8103. }
  8104. //---------------------------------------------------------------------------
  8105. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8106. {
  8107. TCustomDirView * ADirView = DirView(Side);
  8108. ADirView->RestoreSelectedNames();
  8109. ADirView->SetFocus();
  8110. }
  8111. //---------------------------------------------------------------------------
  8112. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8113. {
  8114. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8115. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8116. {
  8117. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8118. UnicodeString Ext;
  8119. if (!IsSideLocalBrowser(osCurrent))
  8120. {
  8121. Ext = UnixExtractFileExt(FileName);
  8122. }
  8123. else
  8124. {
  8125. Ext = ExtractFileExt(FileName);
  8126. }
  8127. if (Ext.IsEmpty())
  8128. {
  8129. Ext = L".";
  8130. }
  8131. TFileFilter Filter;
  8132. Filter.Masks = FORMAT(L"*%s", (Ext));
  8133. Filter.Directories = false;
  8134. CurrentDirView->SelectFiles(Filter, Select);
  8135. }
  8136. }
  8137. //---------------------------------------------------------------------------
  8138. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8139. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8140. {
  8141. UnicodeString Result;
  8142. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8143. {
  8144. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8145. }
  8146. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8147. {
  8148. // noop
  8149. }
  8150. else
  8151. {
  8152. Result =
  8153. FMTLOAD(FILE_INFO_FORMAT,
  8154. (FormatBytes(FileInfo.SelectedSize),
  8155. FormatBytes(FileInfo.FilesSize),
  8156. FormatNumber(FileInfo.SelectedCount),
  8157. FormatNumber(FileInfo.FilesCount)));
  8158. }
  8159. return Result;
  8160. }
  8161. //---------------------------------------------------------------------------
  8162. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8163. TTBXStatusPanel * Panel, TOperationSide Side)
  8164. {
  8165. if (Panel->Index == 1)
  8166. {
  8167. ToggleShowHiddenFiles();
  8168. }
  8169. else if (Panel->Index == 2)
  8170. {
  8171. Filter(Side);
  8172. }
  8173. }
  8174. //---------------------------------------------------------------------------
  8175. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8176. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8177. {
  8178. DebugAssert(!StatusBar->SimplePanel);
  8179. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8180. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8181. }
  8182. //---------------------------------------------------------------------------
  8183. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8184. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8185. {
  8186. DebugAssert(StatusBar->Panels->Count >= 3);
  8187. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8188. if (FileInfo.HiddenCount > 0)
  8189. {
  8190. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8191. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8192. }
  8193. else
  8194. {
  8195. HiddenFilesPanel->ViewPriority = 0;
  8196. // not really necessary, just to cleanup no-longer-valid data
  8197. HiddenFilesPanel->Caption = L"";
  8198. }
  8199. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8200. if (FileInfo.FilteredCount > 0)
  8201. {
  8202. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8203. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8204. }
  8205. else
  8206. {
  8207. FilteredFilesPanel->ViewPriority = 0;
  8208. // not really necessary, just to cleanup no-longer-valid data
  8209. FilteredFilesPanel->Caption = L"";
  8210. }
  8211. }
  8212. //---------------------------------------------------------------------------
  8213. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8214. TObject * /*Sender*/)
  8215. {
  8216. if (DirView(osOther)->Enabled)
  8217. {
  8218. DirView(osOther)->SetFocus();
  8219. }
  8220. }
  8221. //---------------------------------------------------------------------------
  8222. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8223. {
  8224. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8225. switch (Config.Show)
  8226. {
  8227. case qvShow:
  8228. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8229. {
  8230. Config.Show = qvHide;
  8231. }
  8232. else
  8233. {
  8234. Config.Show = Config.LastHideShow;
  8235. }
  8236. break;
  8237. case qvHideWhenEmpty:
  8238. if (ComponentVisible[fcQueueView])
  8239. {
  8240. Config.Show = qvHide;
  8241. }
  8242. else
  8243. {
  8244. Config.LastHideShow = Config.Show;
  8245. Config.Show = qvShow;
  8246. }
  8247. break;
  8248. case qvHide:
  8249. Config.LastHideShow = Config.Show;
  8250. Config.Show = qvShow;
  8251. break;
  8252. }
  8253. WinConfiguration->QueueView = Config;
  8254. }
  8255. //---------------------------------------------------------------------------
  8256. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8257. {
  8258. UnicodeString Result;
  8259. if (Terminal != NULL)
  8260. {
  8261. switch (WinConfiguration->PathInCaption)
  8262. {
  8263. case picShort:
  8264. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8265. break;
  8266. case picFull:
  8267. Result = Terminal->CurrentDirectory;
  8268. break;
  8269. }
  8270. }
  8271. return Result;
  8272. }
  8273. //---------------------------------------------------------------------------
  8274. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8275. {
  8276. TColor Result = GetWindowColor(FSessionColor);
  8277. return Result;
  8278. }
  8279. //---------------------------------------------------------------------------
  8280. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8281. {
  8282. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8283. return Result;
  8284. }
  8285. //---------------------------------------------------------------------------
  8286. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8287. {
  8288. if (!FSessionChanging)
  8289. {
  8290. TTerminalManager::Instance()->UpdateAppTitle();
  8291. }
  8292. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8293. // background to gray on Windows 7 (works on Windows 10).
  8294. // See also EnableControl
  8295. if (Showing)
  8296. {
  8297. if (ManagedSession != NULL)
  8298. {
  8299. // Update path when it changes
  8300. if ((SessionsPageControl->ActivePage != NULL) &&
  8301. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8302. {
  8303. UpdateSessionTab(SessionsPageControl->ActivePage);
  8304. }
  8305. }
  8306. NonVisualDataModule->ReconnectSessionAction->Update();
  8307. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8308. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8309. CenterReconnectToolbar();
  8310. bool HasTerminal = HasActiveTerminal();
  8311. RemoteDirView->Enabled = HasTerminal;
  8312. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8313. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8314. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8315. RemoteDriveView->Color = RemoteDirView->Color;
  8316. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8317. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8318. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8319. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8320. QueueFileList->Enabled = QueueView3->Enabled;
  8321. QueueFileList->Color = QueueView3->Color;
  8322. QueueFileList->Font->Color = QueueView3->Font->Color;
  8323. QueueLabelUpdateStatus();
  8324. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8325. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8326. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8327. {
  8328. UpdateDarkMode();
  8329. }
  8330. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8331. }
  8332. }
  8333. //---------------------------------------------------------------------------
  8334. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8335. {
  8336. UpdateControls();
  8337. }
  8338. //---------------------------------------------------------------------------
  8339. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8340. TObject * Sender)
  8341. {
  8342. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8343. DebugAssert(DirView != NULL);
  8344. DoDirViewLoaded(DirView);
  8345. TransferPresetAutoSelect();
  8346. }
  8347. //---------------------------------------------------------------------------
  8348. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8349. {
  8350. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8351. // first run after installation
  8352. if (double(Updates.LastCheck) == 0)
  8353. {
  8354. // make sure next time there will be an update (if enabled)
  8355. Updates.LastCheck = TDateTime(1);
  8356. WinConfiguration->Updates = Updates;
  8357. }
  8358. else if ((double(Updates.Period) > 0) &&
  8359. (Now() - Updates.LastCheck >= Updates.Period))
  8360. {
  8361. TThreadMethod OnUpdatesChecked = NULL;
  8362. if (!IsUWP())
  8363. {
  8364. OnUpdatesChecked = UpdatesChecked;
  8365. }
  8366. StartUpdateThread(OnUpdatesChecked);
  8367. }
  8368. }
  8369. //---------------------------------------------------------------------------
  8370. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8371. {
  8372. UnicodeString Message;
  8373. bool New;
  8374. TQueryType Type;
  8375. GetUpdatesMessage(Message, New, Type, false);
  8376. if (!Message.IsEmpty())
  8377. {
  8378. if (New)
  8379. {
  8380. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8381. }
  8382. if (!New && (Type != qtWarning))
  8383. {
  8384. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8385. }
  8386. else
  8387. {
  8388. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8389. }
  8390. if (New)
  8391. {
  8392. Configuration->Usage->Inc(L"UpdateNotifications");
  8393. }
  8394. }
  8395. }
  8396. //---------------------------------------------------------------------------
  8397. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8398. {
  8399. RestoreApp();
  8400. bool CanDisplay = !NonVisualDataModule->Busy;
  8401. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8402. {
  8403. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8404. }
  8405. if (CanDisplay)
  8406. {
  8407. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8408. CheckForUpdates(true);
  8409. }
  8410. else
  8411. {
  8412. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8413. }
  8414. }
  8415. //---------------------------------------------------------------------------
  8416. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8417. TCopyParamRuleData & Data)
  8418. {
  8419. DebugAssert(Terminal != NULL);
  8420. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8421. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8422. Data.RemoteDirectory = RemoteDirView->PathName;
  8423. }
  8424. //---------------------------------------------------------------------------
  8425. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8426. {
  8427. // Terminal can be null when we are changing local directory implicitly,
  8428. // because it has been deleted, while no session is connected
  8429. // (Login dialog is open)
  8430. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8431. {
  8432. DebugAssert(!IsLocalBrowserMode());
  8433. TCopyParamRuleData Data;
  8434. GetTransferPresetAutoSelectData(Data);
  8435. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8436. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8437. if (CopyParamIndex < 0)
  8438. {
  8439. // there is no preset that matches autoselection
  8440. // set preset that we consider "default"
  8441. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8442. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8443. }
  8444. else
  8445. {
  8446. // there is preset matching autoselection
  8447. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8448. if (CopyParamName == FCopyParamAutoSelected)
  8449. {
  8450. // autoselected the same preset as the last time
  8451. // make no change (i.e. preserve custom user preset, if any)
  8452. }
  8453. else
  8454. {
  8455. // autoselected the different preset then the last time (or there
  8456. // was default preset set)
  8457. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8458. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8459. }
  8460. }
  8461. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8462. {
  8463. if (CopyParamIndex >= 0)
  8464. {
  8465. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8466. }
  8467. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8468. try
  8469. {
  8470. int Fmt =
  8471. (CopyParamIndex < 0) ?
  8472. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8473. COPY_PARAM_AUTOSELECTED;
  8474. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8475. Data->Message = MainInstructions(Message);
  8476. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8477. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8478. CopyParamAttrs);
  8479. if (CopyParamIndex >= 0)
  8480. {
  8481. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8482. Info = FORMAT(L"%s\n \n%s", (Info,
  8483. FMTLOAD(COPY_PARAM_RULE,
  8484. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8485. }
  8486. Data->Message += L"\n\n" + Info;
  8487. if (WinConfiguration->CopyParamAutoSelectNotice)
  8488. {
  8489. TransferPresetNoteMessage(Data, true);
  8490. }
  8491. else
  8492. {
  8493. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8494. Data = NULL; // ownership passed
  8495. }
  8496. }
  8497. __finally
  8498. {
  8499. delete Data;
  8500. }
  8501. }
  8502. }
  8503. }
  8504. //---------------------------------------------------------------------------
  8505. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8506. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8507. {
  8508. DebugAssert(NoteData != NULL);
  8509. DebugAssert(!IsLocalBrowserMode());
  8510. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8511. TQueryButtonAlias Aliases[1];
  8512. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8513. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8514. Params.Aliases = Aliases;
  8515. Params.AliasesCount = LENOF(Aliases);
  8516. unsigned int Result =
  8517. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8518. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8519. switch (Result)
  8520. {
  8521. case qaNeverAskAgain:
  8522. DebugAssert(AllowNeverAskAgain);
  8523. WinConfiguration->CopyParamAutoSelectNotice = false;
  8524. break;
  8525. case qaIgnore:
  8526. PreferencesDialog(pmPresets);
  8527. break;
  8528. }
  8529. }
  8530. //---------------------------------------------------------------------------
  8531. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8532. {
  8533. // as of now useless, as this is used for notes only, never for balloons, ...
  8534. RestoreApp();
  8535. // .. and we should never be busy here
  8536. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8537. {
  8538. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8539. }
  8540. }
  8541. //---------------------------------------------------------------------------
  8542. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8543. TPreferencesMode APreferencesMode)
  8544. {
  8545. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8546. TCopyParamRuleData Data;
  8547. if (Terminal != NULL)
  8548. {
  8549. PreferencesData.reset(new TPreferencesDialogData());
  8550. GetTransferPresetAutoSelectData(Data);
  8551. PreferencesData->CopyParamRuleData = &Data;
  8552. }
  8553. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8554. }
  8555. //---------------------------------------------------------------------------
  8556. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8557. const TCustomCommandType & Command)
  8558. {
  8559. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8560. {
  8561. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8562. }
  8563. }
  8564. //---------------------------------------------------------------------------
  8565. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8566. {
  8567. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8568. TCustomCommandType Command;
  8569. // make sure we use local custom command when remote are not supported
  8570. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8571. {
  8572. Command = FLastCustomCommand;
  8573. }
  8574. else
  8575. {
  8576. Command.Params = Command.Params | ccLocal;
  8577. }
  8578. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8579. FEditingFocusedAdHocCommand = OnFocused;
  8580. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8581. int Options =
  8582. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8583. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8584. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8585. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8586. {
  8587. FLastCustomCommand = Command;
  8588. UpdateCustomCommandsToolbar();
  8589. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8590. }
  8591. }
  8592. //---------------------------------------------------------------------------
  8593. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8594. {
  8595. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8596. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8597. DebugAssert(State > 0);
  8598. if (State <= 0)
  8599. {
  8600. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8601. }
  8602. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8603. }
  8604. //---------------------------------------------------------------------------
  8605. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8606. TCustomCommandType & Command, int & State)
  8607. {
  8608. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8609. if (Result)
  8610. {
  8611. Command = FLastCustomCommand;
  8612. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8613. }
  8614. return Result;
  8615. }
  8616. //---------------------------------------------------------------------------
  8617. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8618. {
  8619. ResetIncrementalSearch();
  8620. }
  8621. //---------------------------------------------------------------------------
  8622. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8623. {
  8624. DebugAssert(ComponentVisible[Component]);
  8625. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8626. }
  8627. //---------------------------------------------------------------------------
  8628. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8629. {
  8630. // when queue panel is resized here directly, the status bar is stretched
  8631. // over whole space the panel occupied
  8632. PostComponentHide(fcQueueView);
  8633. }
  8634. //---------------------------------------------------------------------------
  8635. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8636. {
  8637. ComponentVisible[fcQueueFileList] = false;
  8638. }
  8639. //---------------------------------------------------------------------------
  8640. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8641. {
  8642. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8643. RefreshColorMode();
  8644. ResetSysDarkTheme();
  8645. ConfigurationChanged();
  8646. ConfigureInterface();
  8647. // Should be called for all controls
  8648. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8649. }
  8650. //---------------------------------------------------------------------------
  8651. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8652. {
  8653. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8654. if (!FInvalid &&
  8655. (Message.LParam != 0) &&
  8656. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8657. {
  8658. ThemeChanged();
  8659. }
  8660. TForm::Dispatch(&Message);
  8661. }
  8662. //---------------------------------------------------------------------------
  8663. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8664. {
  8665. TMessage * M = static_cast<TMessage*>(Message);
  8666. switch (M->Msg)
  8667. {
  8668. case CM_DIALOGCHAR:
  8669. CMDialogChar(*M);
  8670. break;
  8671. case WM_APPCOMMAND:
  8672. WMAppCommand(*M);
  8673. break;
  8674. case WM_SYSCOMMAND:
  8675. WMSysCommand(*M);
  8676. break;
  8677. case WM_QUERYENDSESSION:
  8678. WMQueryEndSession(*M);
  8679. break;
  8680. case WM_ENDSESSION:
  8681. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8682. break;
  8683. #ifdef _DEBUG
  8684. case WM_WINDOWPOSCHANGED:
  8685. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8686. break;
  8687. case CM_CUSTOMSTYLECHANGED:
  8688. TForm::Dispatch(Message);
  8689. break;
  8690. #endif
  8691. case WM_COMPONENT_HIDE:
  8692. {
  8693. Byte Component = static_cast<Byte>(M->WParam);
  8694. // sanity check
  8695. if (ComponentVisible[Component])
  8696. {
  8697. // special treatment
  8698. if (Component == fcQueueView)
  8699. {
  8700. ToggleQueueVisibility();
  8701. DebugAssert(!ComponentVisible[fcQueueView]);
  8702. }
  8703. else
  8704. {
  8705. ComponentVisible[Component] = false;
  8706. }
  8707. }
  8708. }
  8709. break;
  8710. case WM_COPYDATA:
  8711. WMCopyData(*M);
  8712. break;
  8713. case WM_MANAGES_CAPTION:
  8714. // caption managed in TTerminalManager::UpdateAppTitle()
  8715. M->Result = 1;
  8716. break;
  8717. case WM_WANTS_MOUSEWHEEL:
  8718. M->Result = 1;
  8719. break;
  8720. case CM_SHOWINGCHANGED:
  8721. CMShowingChanged(*M);
  8722. break;
  8723. case WM_CLOSE:
  8724. WMClose(*M);
  8725. break;
  8726. case WM_DPICHANGED:
  8727. WMDpiChanged(*M);
  8728. break;
  8729. case CM_DPICHANGED:
  8730. CMDpiChanged(*M);
  8731. break;
  8732. case WM_SETTINGCHANGE:
  8733. WMSettingChange(*M);
  8734. break;
  8735. case CM_DIALOGKEY:
  8736. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8737. break;
  8738. default:
  8739. TForm::Dispatch(Message);
  8740. break;
  8741. }
  8742. }
  8743. //---------------------------------------------------------------------------
  8744. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8745. {
  8746. // noop
  8747. }
  8748. //---------------------------------------------------------------------------
  8749. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8750. {
  8751. TForm::Dispatch(&Message);
  8752. FSessionColors->Clear();
  8753. AddFixedSessionImages();
  8754. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8755. UpdateImages();
  8756. }
  8757. //---------------------------------------------------------------------------
  8758. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8759. {
  8760. TForm::Dispatch(&Message);
  8761. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8762. UpdatePixelsPerInchMainWindowCounter();
  8763. }
  8764. //---------------------------------------------------------------------------
  8765. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8766. {
  8767. // Cannot close window while we are busy.
  8768. // We cannot test this in FormCloseQuery as that is called also from
  8769. // Close(), which is called by CloseApplicationAction. So we can be busy
  8770. // there already even, when it is legitimate to close the application.
  8771. // Possibly a better place to handle this would be WMSysCommand.
  8772. if (NonVisualDataModule->Busy)
  8773. {
  8774. Message.Result = 1;
  8775. }
  8776. else
  8777. {
  8778. TForm::Dispatch(&Message);
  8779. }
  8780. }
  8781. //---------------------------------------------------------------------------
  8782. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8783. {
  8784. // Ignoring implicit local-local browser of Commander, opened from DoShow
  8785. bool NoSession = (ManagedSession == NULL);
  8786. // This calls TCustomScpExplorerForm::DoShow
  8787. TForm::Dispatch(&Message);
  8788. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  8789. if (Showing && !FStarted)
  8790. {
  8791. FStarted = true;
  8792. InterfaceStarted();
  8793. }
  8794. if (Showing && NoSession)
  8795. {
  8796. // When we are starting minimized (i.e. from an installer),
  8797. // postpone showing Login dialog until we get restored.
  8798. // Otherwise the Login dialog (and Authentication window) show restored
  8799. // over invisible (minimized) main window.
  8800. if (WindowState == wsMinimized)
  8801. {
  8802. FNeedSession = true;
  8803. }
  8804. else
  8805. {
  8806. // This happens before application ever goes idle, so the toolbars would
  8807. // stay enabled (initial state) until the Login dialog is dismissed.
  8808. UpdateActions();
  8809. NonVisualDataModule->StartBusy();
  8810. try
  8811. {
  8812. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8813. // otherwise the dialog does not receive focus.
  8814. // With Commander interface the ProcessMessages is called already
  8815. // by TDriveView, but with Explorer interface, we need to call it explicily
  8816. Application->ProcessMessages();
  8817. // do not reload sessions, they have been loaded just now (optimization)
  8818. NeedSession(true);
  8819. }
  8820. __finally
  8821. {
  8822. NonVisualDataModule->EndBusy();
  8823. }
  8824. }
  8825. }
  8826. }
  8827. //---------------------------------------------------------------------------
  8828. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8829. {
  8830. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8831. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8832. }
  8833. //---------------------------------------------------------------------------
  8834. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8835. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8836. int & MaxHeight)
  8837. {
  8838. // workaround for bug in TWinControl.CalcConstraints
  8839. // Check for empty rect (restore from iconic state) is done there only after
  8840. // call to AdjustClientRect, which enlarges the rect (for forms).
  8841. TRect R = GetClientRect();
  8842. // when restoring from iconic state, place no restrictions
  8843. if (IsRectEmpty(R))
  8844. {
  8845. MinWidth = 0;
  8846. MinHeight = 0;
  8847. MaxWidth = 32000;
  8848. MaxHeight = 32000;
  8849. }
  8850. }
  8851. //---------------------------------------------------------------------------
  8852. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8853. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8854. {
  8855. // terminal can be already closed (e.g. dropped connection)
  8856. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8857. {
  8858. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8859. try
  8860. {
  8861. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8862. }
  8863. catch(...)
  8864. {
  8865. if (!Terminal->Active)
  8866. {
  8867. Close = true;
  8868. }
  8869. throw;
  8870. }
  8871. }
  8872. }
  8873. //---------------------------------------------------------------------------
  8874. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8875. {
  8876. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8877. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8878. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8879. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8880. {
  8881. OnGetSpaceAvailable = GetSpaceAvailable;
  8882. }
  8883. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8884. OnGetSpaceAvailable);
  8885. }
  8886. //---------------------------------------------------------------------------
  8887. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8888. {
  8889. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8890. Terminal->FillSessionDataForCode(Data.get());
  8891. return Data.release();
  8892. }
  8893. //---------------------------------------------------------------------------
  8894. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8895. {
  8896. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8897. DoGenerateUrlDialog(Data.get(), Paths);
  8898. }
  8899. //---------------------------------------------------------------------------
  8900. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8901. {
  8902. GenerateUrl(NULL);
  8903. }
  8904. //---------------------------------------------------------------------------
  8905. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8906. {
  8907. std::unique_ptr<TStrings> Paths(new TStringList());
  8908. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8909. GenerateUrl(Paths.get());
  8910. }
  8911. //---------------------------------------------------------------------------
  8912. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8913. {
  8914. FSessionColor = value;
  8915. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8916. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8917. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8918. DebugAssert(ColorItem != NULL);
  8919. ColorItem->Color = C;
  8920. NonVisualDataModule->ColorMenuItem->Color = C;
  8921. UpdateControls();
  8922. }
  8923. //---------------------------------------------------------------------------
  8924. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8925. {
  8926. if (value != FSessionColor)
  8927. {
  8928. UpdateSessionColor(value);
  8929. // Is null when called from LastTerminalClosed
  8930. if (Terminal != NULL)
  8931. {
  8932. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8933. }
  8934. }
  8935. }
  8936. //---------------------------------------------------------------------------
  8937. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8938. {
  8939. bool Result = FNoteTimer->Enabled;
  8940. if (Result)
  8941. {
  8942. // cannot cancel note too early
  8943. bool NotEarly =
  8944. (Now() - FNoteShown >
  8945. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8946. if (Force || NotEarly)
  8947. {
  8948. FNoteTimer->Enabled = false;
  8949. FNote = L"";
  8950. SAFE_DESTROY(FNoteData);
  8951. FOnNoteClick = NULL;
  8952. FNoteHints = FNotes->Text;
  8953. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8954. UpdateStatusBar();
  8955. }
  8956. }
  8957. return Result;
  8958. }
  8959. //---------------------------------------------------------------------------
  8960. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8961. {
  8962. DebugAssert(FNoteTimer->Enabled);
  8963. CancelNote(true);
  8964. }
  8965. //---------------------------------------------------------------------------
  8966. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  8967. {
  8968. FNoteHints = FNotes->Text.TrimRight();
  8969. }
  8970. //---------------------------------------------------------------------------
  8971. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8972. {
  8973. int P = Note.Pos(L"\n");
  8974. if (P > 0)
  8975. {
  8976. Note.SetLength(P - 1);
  8977. }
  8978. FNotes->Add(FORMAT(L"[%s] %s",
  8979. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8980. while (FNotes->Count > 10)
  8981. {
  8982. FNotes->Delete(0);
  8983. }
  8984. if (UpdateNow)
  8985. {
  8986. UpdateNoteHints();
  8987. UpdateStatusBar();
  8988. }
  8989. }
  8990. //---------------------------------------------------------------------------
  8991. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8992. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8993. {
  8994. int P = Note.Pos(L"\n");
  8995. if (P > 0)
  8996. {
  8997. Note.SetLength(P - 1);
  8998. }
  8999. UpdateNoteHints();
  9000. FNote = Note;
  9001. SAFE_DESTROY(FNoteData);
  9002. FNoteData = NoteData;
  9003. FOnNoteClick = OnNoteClick;
  9004. AddNote(Note, false);
  9005. UpdateStatusBar();
  9006. FNoteShown = Now();
  9007. FNoteTimer->Enabled = false;
  9008. if (Seconds == 0)
  9009. {
  9010. Seconds = WinConfiguration->NotificationsTimeout;
  9011. }
  9012. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9013. FNoteTimer->Enabled = true;
  9014. }
  9015. //---------------------------------------------------------------------------
  9016. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9017. {
  9018. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9019. }
  9020. //---------------------------------------------------------------------------
  9021. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9022. {
  9023. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9024. {
  9025. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9026. }
  9027. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9028. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9029. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9030. 0, NULL, NULL);
  9031. }
  9032. //---------------------------------------------------------------------------
  9033. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9034. {
  9035. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  9036. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  9037. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  9038. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9039. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9040. }
  9041. //---------------------------------------------------------------------------
  9042. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9043. {
  9044. WinConfiguration->FormatSizeBytes = Style;
  9045. }
  9046. //---------------------------------------------------------------------------
  9047. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9048. {
  9049. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9050. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9051. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9052. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9053. }
  9054. //---------------------------------------------------------------------------
  9055. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9056. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9057. {
  9058. if (Panel->Index == 0)
  9059. {
  9060. if (FOnNoteClick != NULL)
  9061. {
  9062. // prevent the user data from being freed by possible call
  9063. // to CancelNote or PostNote during call to OnNoteClick
  9064. std::unique_ptr<TObject> NoteData(FNoteData);
  9065. TNotifyEvent OnNoteClick = FOnNoteClick;
  9066. FNoteData = NULL;
  9067. // need to cancel the note as we are going to delete its user data
  9068. CancelNote(true);
  9069. OnNoteClick(NoteData.get());
  9070. }
  9071. }
  9072. if (HasActiveTerminal() &&
  9073. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9074. {
  9075. FileSystemInfo();
  9076. }
  9077. }
  9078. //---------------------------------------------------------------------------
  9079. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9080. {
  9081. // workaround:
  9082. // 1) for unknown reason, disabling window, while minimized,
  9083. // prevents it from restoring, even if it was enabled again meanwhile
  9084. // 2) when disabling the main window, while another has focus
  9085. // minimize is no longer possible
  9086. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9087. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9088. // Shouldn't we use IsApplicationMinimized() here?
  9089. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9090. {
  9091. Enabled = false;
  9092. }
  9093. FLockLevel++;
  9094. }
  9095. //---------------------------------------------------------------------------
  9096. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9097. {
  9098. DebugAssert(FLockLevel > 0);
  9099. FLockLevel--;
  9100. if (FLockLevel == 0)
  9101. {
  9102. DebugAssert(FLockSuspendLevel == 0);
  9103. Enabled = true;
  9104. }
  9105. }
  9106. //---------------------------------------------------------------------------
  9107. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9108. {
  9109. // We need to make sure that window is enabled when the last modal window closes
  9110. // otherwise focus is not restored correctly.
  9111. // So we re-enable the window when modal window opens and
  9112. // disable it back after is closes
  9113. if (FLockLevel > 0)
  9114. {
  9115. // while we have nesting counter, we know that we never be called
  9116. // recursivelly as Application->OnModalBegin is called only
  9117. // for the top-level modal window
  9118. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9119. {
  9120. // won't be disabled when conditions in LockWindow() were not satisfied
  9121. FDisabledOnLockSuspend = !Enabled;
  9122. // When minimized to tray (or actually when set to SW_HIDE),
  9123. // setting Enabled makes the window focusable even when there's
  9124. // modal window over it
  9125. if (!FTrayIcon->Visible)
  9126. {
  9127. Enabled = true;
  9128. }
  9129. }
  9130. FLockSuspendLevel++;
  9131. }
  9132. }
  9133. //---------------------------------------------------------------------------
  9134. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9135. {
  9136. if (FLockSuspendLevel > 0)
  9137. {
  9138. DebugAssert(FLockLevel > 0);
  9139. FLockSuspendLevel--;
  9140. // see comment in SuspendWindowLock
  9141. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9142. {
  9143. // Note that window can be enabled here, when we were minized to tray when
  9144. // was SuspendWindowLock() called.
  9145. // We should possibly do the same check as in LockWindow(),
  9146. // if it is ever possible that the consitions change between
  9147. // SuspendWindowLock() and ResumeWindowLock()
  9148. Enabled = !FDisabledOnLockSuspend;
  9149. }
  9150. }
  9151. }
  9152. //---------------------------------------------------------------------------
  9153. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9154. {
  9155. TTBXComboBoxItem * RemotePathComboBox =
  9156. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9157. if (!TextOnly)
  9158. {
  9159. TStrings * Items = RemotePathComboBox->Strings;
  9160. Items->BeginUpdate();
  9161. try
  9162. {
  9163. Items->Clear();
  9164. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9165. {
  9166. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9167. while (!IsUnixRootPath(APath))
  9168. {
  9169. int P = APath.LastDelimiter(L'/');
  9170. DebugAssert(P >= 0);
  9171. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9172. APath.SetLength(P - 1);
  9173. }
  9174. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9175. }
  9176. }
  9177. __finally
  9178. {
  9179. Items->EndUpdate();
  9180. }
  9181. }
  9182. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9183. // Setting ItemIndex to -1 does not reset its text
  9184. if (RemotePathComboBox->Strings->Count == 0)
  9185. {
  9186. RemotePathComboBox->Text = L"";
  9187. }
  9188. }
  9189. //---------------------------------------------------------------------------
  9190. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9191. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9192. {
  9193. if (AIndex < 0)
  9194. {
  9195. AIndex = Sender->ItemIndex;
  9196. }
  9197. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9198. }
  9199. //---------------------------------------------------------------------------
  9200. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9201. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9202. int & ImageIndex)
  9203. {
  9204. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9205. }
  9206. //---------------------------------------------------------------------------
  9207. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9208. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9209. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9210. {
  9211. if (!IsLocalBrowserMode())
  9212. {
  9213. ARect.Left += (10 * AIndex);
  9214. }
  9215. }
  9216. //---------------------------------------------------------------------------
  9217. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9218. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9219. {
  9220. AWidth += (10 * AIndex);
  9221. }
  9222. //---------------------------------------------------------------------------
  9223. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9224. {
  9225. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9226. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9227. int Index = RemotePathComboBox->ItemIndex;
  9228. while (Index < RemotePathComboBox->Strings->Count - 1)
  9229. {
  9230. APath = UnixExtractFileDir(APath);
  9231. Index++;
  9232. }
  9233. // VanDyke style paths
  9234. if (APath.IsEmpty())
  9235. {
  9236. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9237. APath = ROOTDIRECTORY;
  9238. }
  9239. if (RemoteDirView->Path != APath)
  9240. {
  9241. RemoteDirView->Path = APath;
  9242. }
  9243. }
  9244. //---------------------------------------------------------------------------
  9245. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9246. {
  9247. DoRemotePathComboBoxItemClick(Sender);
  9248. }
  9249. //---------------------------------------------------------------------------
  9250. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9251. {
  9252. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9253. DebugUsedParam(Sender);
  9254. UpdateRemotePathComboBox(true);
  9255. }
  9256. //---------------------------------------------------------------------------
  9257. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9258. {
  9259. DoRemotePathComboBoxCancel(Sender);
  9260. }
  9261. //---------------------------------------------------------------------------
  9262. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9263. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9264. {
  9265. ResetIncrementalSearch();
  9266. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9267. DebugAssert(DirView != NULL);
  9268. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9269. {
  9270. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9271. // OnEditing is called also from TCustomListView::CanEdit
  9272. if (Edit != NULL)
  9273. {
  9274. EditSelectBaseName(Edit);
  9275. }
  9276. }
  9277. }
  9278. //---------------------------------------------------------------------------
  9279. //---------------------------------------------------------------------------
  9280. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9281. {
  9282. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9283. Result->AutoDetectDnD = false;
  9284. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9285. Result->RenderDataOn = rdoEnterAndDropSync;
  9286. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9287. return Result;
  9288. }
  9289. //---------------------------------------------------------------------------
  9290. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9291. {
  9292. if (IsWin10Build(2004))
  9293. {
  9294. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9295. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9296. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9297. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9298. }
  9299. }
  9300. //---------------------------------------------------------------------------
  9301. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9302. {
  9303. TForm::CreateWnd();
  9304. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9305. UpdateDarkMode();
  9306. if (FSessionsDragDropFilesEx == NULL)
  9307. {
  9308. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9309. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9310. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9311. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9312. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9313. }
  9314. if (FQueueDragDropFilesEx == NULL)
  9315. {
  9316. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9317. // No need to set OnDragOver as we do not have any restrictions
  9318. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9319. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9320. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9321. }
  9322. }
  9323. //---------------------------------------------------------------------------
  9324. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9325. {
  9326. TForm::DestroyWnd();
  9327. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9328. FQueueDragDropFilesEx->DragDropControl = NULL;
  9329. }
  9330. //---------------------------------------------------------------------------
  9331. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9332. {
  9333. SideEnter(FCurrentSide);
  9334. FEverShown = true;
  9335. }
  9336. //---------------------------------------------------------------------------
  9337. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9338. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9339. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9340. {
  9341. if (!NonVisualDataModule->Busy)
  9342. {
  9343. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9344. Configuration->Usage->Inc(L"FileFinds");
  9345. LockWindow(true);
  9346. NonVisualDataModule->StartBusy();
  9347. WinConfiguration->LockedInterface = true;
  9348. try
  9349. {
  9350. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9351. }
  9352. __finally
  9353. {
  9354. WinConfiguration->LockedInterface = false;
  9355. NonVisualDataModule->EndBusy();
  9356. UnlockWindow();
  9357. }
  9358. }
  9359. }
  9360. //---------------------------------------------------------------------------
  9361. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9362. {
  9363. if (!NonVisualDataModule->Busy)
  9364. {
  9365. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9366. SetFocus();
  9367. RemoteDirView->Path = UnixExtractFilePath(Path);
  9368. UnicodeString FileName = UnixExtractFileName(Path);
  9369. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9370. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9371. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9372. if (Item == NULL)
  9373. {
  9374. ReloadDirectory(osRemote);
  9375. Item = RemoteDirView->FindFileItem(FileName);
  9376. }
  9377. if (Item != NULL)
  9378. {
  9379. RemoteDirView->ItemFocused = Item;
  9380. RemoteDirView->ItemFocused->MakeVisible(false);
  9381. RemoteDirView->SetFocus();
  9382. }
  9383. }
  9384. }
  9385. //---------------------------------------------------------------------------
  9386. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9387. {
  9388. bool Result = !NonVisualDataModule->Busy;
  9389. if (Result)
  9390. {
  9391. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9392. }
  9393. return Result;
  9394. }
  9395. //---------------------------------------------------------------------------
  9396. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9397. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9398. {
  9399. if (CanOperateOnFoundFiles(ATerminal))
  9400. {
  9401. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9402. FOnFileOperationFinished = OnFileOperationFinished;
  9403. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9404. }
  9405. }
  9406. //---------------------------------------------------------------------------
  9407. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9408. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9409. {
  9410. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9411. }
  9412. //---------------------------------------------------------------------------
  9413. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9414. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9415. {
  9416. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9417. }
  9418. //---------------------------------------------------------------------------
  9419. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9420. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9421. {
  9422. if (CanOperateOnFoundFiles(ATerminal))
  9423. {
  9424. for (int Index = 0; Index < FileList->Count; Index++)
  9425. {
  9426. UnicodeString FileName = FileList->Strings[Index];
  9427. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9428. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9429. OnFileOperationFinished(osRemote, FileName, true);
  9430. }
  9431. }
  9432. }
  9433. //---------------------------------------------------------------------------
  9434. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9435. {
  9436. FFileFindTerminal = Terminal;
  9437. ShowFileFindDialog(
  9438. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9439. DoDownloadFoundFiles, DoEditFoundFiles);
  9440. }
  9441. //---------------------------------------------------------------------------
  9442. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9443. {
  9444. FTaskbarList = TaskbarList;
  9445. }
  9446. //---------------------------------------------------------------------------
  9447. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9448. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9449. {
  9450. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9451. if (Index >= 0)
  9452. {
  9453. if (Button == mbLeft)
  9454. {
  9455. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9456. // If switching tab (switching session) takes long enough for user
  9457. // to actually release the button, "mouse down" is still raised,
  9458. // but we do not get "mouse up" event, so dragging is not cancelled,
  9459. // prevent that by not beginning dragging in the first place.
  9460. if (IsKeyPressed(VK_LBUTTON))
  9461. {
  9462. // when user clicks the "+", we get mouse down only after the session
  9463. // is closed, when new session tab is already on X:Y, so dragging
  9464. // starts, prevent that
  9465. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9466. {
  9467. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9468. if (Terminal != NULL)
  9469. {
  9470. SessionsPageControl->BeginDrag(false);
  9471. }
  9472. }
  9473. }
  9474. }
  9475. else if (Button == mbMiddle)
  9476. {
  9477. // ignore middle-click for "New session tab" and the last local-local tab
  9478. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9479. if ((Session != NULL) && CanCloseSession(Session))
  9480. {
  9481. CloseSessionTab(Index);
  9482. }
  9483. }
  9484. }
  9485. }
  9486. //---------------------------------------------------------------------------
  9487. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9488. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9489. {
  9490. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9491. // do not allow dropping on the "+" tab
  9492. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9493. if ((TargetTerminal != NULL) &&
  9494. (SessionsPageControl->ActivePage->PageIndex != Index))
  9495. {
  9496. Configuration->Usage->Inc(L"SessionTabMoves");
  9497. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9498. // but we want to actually prevent that to avoid flicker
  9499. SessionsPageControl->ActivePage->PageIndex = Index;
  9500. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9501. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9502. }
  9503. }
  9504. //---------------------------------------------------------------------------
  9505. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9506. TObject * Sender, TObject * Source, int X, int Y,
  9507. TDragState /*State*/, bool & Accept)
  9508. {
  9509. Accept = (Sender == Source);
  9510. if (Accept)
  9511. {
  9512. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9513. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9514. // do not allow dragging to the "+" tab
  9515. Accept = (Terminal != NULL);
  9516. }
  9517. }
  9518. //---------------------------------------------------------------------------
  9519. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9520. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9521. {
  9522. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9523. if (Index < 0)
  9524. {
  9525. Effect = DROPEFFECT_NONE;
  9526. }
  9527. else
  9528. {
  9529. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9530. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9531. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9532. {
  9533. Effect = DROPEFFECT_NONE;
  9534. }
  9535. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9536. {
  9537. Effect = DROPEFFECT_COPY;
  9538. }
  9539. }
  9540. }
  9541. //---------------------------------------------------------------------------
  9542. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9543. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9544. {
  9545. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9546. // do not allow dropping on local-local, disconnected and the "+" tab
  9547. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9548. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9549. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9550. {
  9551. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9552. if (!IsFileControl(DropSourceControl, osRemote))
  9553. {
  9554. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9555. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9556. // Why don't we use Terminal->CurrentDirectory directly?
  9557. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9558. // do not force queue, drag drop
  9559. false, true);
  9560. }
  9561. }
  9562. }
  9563. //---------------------------------------------------------------------------
  9564. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9565. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9566. {
  9567. // Downloads are handled in RemoteFileControlDDEnd
  9568. if (!IsFileControl(DropSourceControl, osRemote))
  9569. {
  9570. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9571. Terminal->CurrentDirectory,
  9572. // force queue, drag drop
  9573. true, true);
  9574. }
  9575. }
  9576. //---------------------------------------------------------------------------
  9577. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9578. {
  9579. FShowing = false;
  9580. // Do not save empty workspace
  9581. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9582. {
  9583. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9584. UnicodeString Name = WorkspaceName();
  9585. DoSaveWorkspace(
  9586. Name, DataList.get(),
  9587. !Configuration->DisablePasswordStoring &&
  9588. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9589. WinConfiguration->LastStoredSession = Name;
  9590. }
  9591. }
  9592. //---------------------------------------------------------------------------
  9593. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9594. {
  9595. TManagedTerminal * ManagedTerminal =
  9596. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9597. if (ManagedTerminal != NULL)
  9598. {
  9599. ManagedTerminal->DirectoryLoaded = Now();
  9600. }
  9601. }
  9602. //---------------------------------------------------------------------------
  9603. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9604. TListItem * /*Item*/, bool /*Selected*/)
  9605. {
  9606. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9607. switch (DirView->LastSelectMethod)
  9608. {
  9609. case smKeyboard:
  9610. Configuration->Usage->Inc(L"KeyboardSelections");
  9611. break;
  9612. case smMouse:
  9613. Configuration->Usage->Inc(L"MouseSelections");
  9614. break;
  9615. }
  9616. }
  9617. //---------------------------------------------------------------------------
  9618. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9619. {
  9620. if (Color != 0)
  9621. {
  9622. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9623. }
  9624. else
  9625. {
  9626. return FSessionTabImageIndex;
  9627. }
  9628. }
  9629. //---------------------------------------------------------------------------
  9630. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9631. {
  9632. TPngImageCollectionItem * Item =
  9633. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9634. FSessionColors->AddPng(Item->PngImage);
  9635. return FSessionColors->Count - 1;
  9636. }
  9637. //---------------------------------------------------------------------------
  9638. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9639. {
  9640. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9641. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  9642. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  9643. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9644. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9645. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  9646. }
  9647. //---------------------------------------------------------------------------
  9648. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  9649. {
  9650. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  9651. }
  9652. //---------------------------------------------------------------------------
  9653. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  9654. {
  9655. switch (Side)
  9656. {
  9657. case osLocal:
  9658. return FNewLocalTabTabImageIndex;
  9659. case osRemote:
  9660. case osCurrent:
  9661. return FNewRemoteTabTabImageIndex;
  9662. default:
  9663. DebugFail();
  9664. return -1;
  9665. }
  9666. }
  9667. //---------------------------------------------------------------------------
  9668. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9669. {
  9670. for (int Index = 0; Index < Control->ControlCount; Index++)
  9671. {
  9672. TControl * AControl = Control->Controls[Index];
  9673. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9674. if (WinControl != NULL)
  9675. {
  9676. CollectItemsWithTextDisplayMode(WinControl);
  9677. }
  9678. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9679. if (Toolbar != NULL)
  9680. {
  9681. // we care for top-level items only
  9682. TTBCustomItem * Items = Toolbar->Items;
  9683. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9684. {
  9685. TTBCustomItem * Item = Items->Items[ItemIndex];
  9686. if (((Item->DisplayMode == nbdmImageAndText) ||
  9687. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9688. EligibleForImageDisplayMode(Item))
  9689. {
  9690. FItemsWithTextDisplayMode.insert(Item);
  9691. }
  9692. }
  9693. }
  9694. }
  9695. }
  9696. //---------------------------------------------------------------------------
  9697. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9698. {
  9699. return true;
  9700. }
  9701. //---------------------------------------------------------------------------
  9702. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9703. {
  9704. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9705. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9706. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9707. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9708. bool Result = true;
  9709. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9710. {
  9711. TTBCustomItem * Item = *i;
  9712. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9713. if (Label != NULL)
  9714. {
  9715. // optimization
  9716. if (Label->Visible == SelectiveToolbarText)
  9717. {
  9718. Result = false;
  9719. }
  9720. else
  9721. {
  9722. Label->Visible = SelectiveToolbarText;
  9723. }
  9724. }
  9725. else
  9726. {
  9727. // optimization
  9728. if (Item->DisplayMode == DisplayMode)
  9729. {
  9730. Result = false;
  9731. }
  9732. else
  9733. {
  9734. Item->DisplayMode = DisplayMode;
  9735. }
  9736. }
  9737. ++i;
  9738. }
  9739. UpdateNewTabTab();
  9740. return Result;
  9741. }
  9742. //---------------------------------------------------------------------------
  9743. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9744. {
  9745. DebugFail();
  9746. }
  9747. //---------------------------------------------------------------------------
  9748. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9749. {
  9750. // Among other, a lock level is non zero, while directory is being loaded,
  9751. // even when it happens as a result of dirview navigation,
  9752. // i.e. when TNonVisualDataModule is NOT FBusy.
  9753. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9754. // Among other this prevents a panel auto update to occur while
  9755. // directory is changing.
  9756. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9757. }
  9758. //---------------------------------------------------------------------------
  9759. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  9760. {
  9761. // While the window is disabled, we still seem to process menu shortcuts at least,
  9762. // so stop it at least here.
  9763. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9764. bool Result;
  9765. if (Allowed == aaUpdate)
  9766. {
  9767. Result = true;
  9768. }
  9769. else
  9770. {
  9771. Result = !NonVisualDataModule->Busy;
  9772. if (Result && (Allowed == aaShortCut))
  9773. {
  9774. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  9775. Action->Update();
  9776. Result = Action->Enabled;
  9777. }
  9778. }
  9779. return Result;
  9780. }
  9781. //---------------------------------------------------------------------------
  9782. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9783. {
  9784. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9785. }
  9786. //---------------------------------------------------------------------------
  9787. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9788. {
  9789. // This is somewhat redundant to LockWindow() call from
  9790. // TTerminalManager::TerminalReadDirectoryProgress.
  9791. // But disabling window is known not to block keyboard shorcuts
  9792. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9793. if (Busy > 0)
  9794. {
  9795. if (NonVisualDataModule->Busy)
  9796. {
  9797. State = false;
  9798. }
  9799. else
  9800. {
  9801. NonVisualDataModule->StartBusy();
  9802. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9803. LockWindow();
  9804. }
  9805. }
  9806. else if (Busy < 0)
  9807. {
  9808. UnlockWindow();
  9809. NonVisualDataModule->EndBusy();
  9810. }
  9811. else
  9812. {
  9813. State = NonVisualDataModule->Busy;
  9814. }
  9815. }
  9816. //---------------------------------------------------------------------------
  9817. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9818. {
  9819. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9820. if (Index >= 0)
  9821. {
  9822. TPopupMenu * PopupMenu = NULL;
  9823. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9824. if (Session != NULL)
  9825. {
  9826. SessionsPageControl->ActivePageIndex = Index;
  9827. if (DebugAlwaysTrue(SessionTabSwitched()))
  9828. {
  9829. // copied from TControl.WMContextMenu
  9830. SendCancelMode(SessionsPageControl);
  9831. // explicit popup instead of using PopupMenu property
  9832. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9833. // while connecting yet not-connected session and hence
  9834. // allowing an access to commands over not-completelly connected session
  9835. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  9836. }
  9837. }
  9838. else
  9839. {
  9840. if (SupportsLocalBrowser())
  9841. {
  9842. PopupMenu = NonVisualDataModule->NewTabPopup;
  9843. }
  9844. }
  9845. if (PopupMenu != NULL)
  9846. {
  9847. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9848. PopupMenu->PopupComponent = SessionsPageControl;
  9849. PopupMenu->Popup(Point.x, Point.y);
  9850. }
  9851. }
  9852. Handled = true;
  9853. }
  9854. //---------------------------------------------------------------------------
  9855. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9856. {
  9857. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  9858. }
  9859. //---------------------------------------------------------------------------
  9860. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9861. {
  9862. Configuration->Usage->Inc(L"PasswordChanges");
  9863. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9864. ChangePasswordData->DisableAuthentationsExceptPassword();
  9865. ChangePasswordData->ChangePassword = true;
  9866. std::unique_ptr<TTerminal> ChangePasswordSession(
  9867. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9868. ChangePasswordSession->AutoReadDirectory = false;
  9869. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9870. {
  9871. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9872. }
  9873. else
  9874. {
  9875. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9876. }
  9877. }
  9878. //---------------------------------------------------------------------------
  9879. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9880. {
  9881. // No nice way to assert SSH2
  9882. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  9883. }
  9884. //---------------------------------------------------------------------------
  9885. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9886. {
  9887. TOperationVisualizer OperationVisualizer;
  9888. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9889. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9890. }
  9891. //---------------------------------------------------------------------------
  9892. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9893. {
  9894. TForm::ChangeScale(M, D);
  9895. int APixelsPerInch = GetControlPixelsPerInch(this);
  9896. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9897. try
  9898. {
  9899. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9900. }
  9901. __finally
  9902. {
  9903. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9904. }
  9905. }
  9906. //---------------------------------------------------------------------------
  9907. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9908. {
  9909. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9910. }
  9911. //---------------------------------------------------------------------
  9912. class TPublicControl : public TControl
  9913. {
  9914. friend class TCustomScpExplorerForm;
  9915. };
  9916. //---------------------------------------------------------------------------
  9917. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9918. {
  9919. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9920. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9921. }
  9922. //---------------------------------------------------------------------------
  9923. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9924. {
  9925. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9926. OpenBookmark(Side, Bookmark);
  9927. }
  9928. //---------------------------------------------------------------------------
  9929. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9930. {
  9931. DoBookmarkClick(osLocal, Sender);
  9932. }
  9933. //---------------------------------------------------------------------------
  9934. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9935. {
  9936. DoBookmarkClick(osRemote, Sender);
  9937. }
  9938. //---------------------------------------------------------------------------
  9939. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9940. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9941. {
  9942. if (BookmarkList != NULL)
  9943. {
  9944. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9945. if (IsLocalBrowserMode())
  9946. {
  9947. Side = osLocal;
  9948. }
  9949. if (!WinConfiguration->UseLocationProfiles)
  9950. {
  9951. std::unique_ptr<TStringList> Directories(new TStringList());
  9952. Directories->CaseSensitive = (Side == osRemote);
  9953. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9954. {
  9955. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9956. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9957. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9958. {
  9959. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9960. Item->Caption = EscapeHotkey(Directory);
  9961. Item->ShortCut = Bookmark->ShortCut;
  9962. Item->OnClick = OnBookmarkClick;
  9963. Item->Tag = reinterpret_cast<int>(Bookmark);
  9964. Directories->Add(Directory);
  9965. Menu->Add(Item.release());
  9966. }
  9967. }
  9968. }
  9969. else
  9970. {
  9971. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9972. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9973. {
  9974. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9975. if (!Bookmark->Node.IsEmpty())
  9976. {
  9977. Folders->Add(Bookmark->Node);
  9978. }
  9979. }
  9980. for (int Index = 0; Index < Folders->Count; Index++)
  9981. {
  9982. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9983. Item->Caption = Folders->Strings[Index];
  9984. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  9985. Folders->Objects[Index] = Item.get();
  9986. Menu->Add(Item.release());
  9987. }
  9988. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9989. {
  9990. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9991. TTBCustomItem * Parent;
  9992. if (!Bookmark->Node.IsEmpty())
  9993. {
  9994. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9995. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9996. }
  9997. else
  9998. {
  9999. Parent = Menu;
  10000. }
  10001. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10002. Item->Caption = Bookmark->Name;
  10003. Item->ShortCut = Bookmark->ShortCut;
  10004. Item->OnClick = OnBookmarkClick;
  10005. Item->Tag = reinterpret_cast<int>(Bookmark);
  10006. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10007. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10008. {
  10009. // No hint for location profiles that are actually mere "bookmarks"
  10010. }
  10011. else
  10012. {
  10013. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10014. UnicodeString LongHint;
  10015. if (!Bookmark->Local.IsEmpty())
  10016. {
  10017. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10018. }
  10019. if (!Bookmark->Remote.IsEmpty())
  10020. {
  10021. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10022. }
  10023. AddToList(Hint, LongHint, L"|");
  10024. Item->Hint = Hint;
  10025. }
  10026. Parent->Add(Item.release());
  10027. }
  10028. }
  10029. AddMenuSeparator(Menu);
  10030. }
  10031. }
  10032. //---------------------------------------------------------------------------
  10033. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10034. {
  10035. Menu->Clear();
  10036. std::unique_ptr<TTBCustomItem> Item;
  10037. Item.reset(new TTBXItem(Owner));
  10038. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10039. Item->Options = Item->Options << tboDefault;
  10040. Menu->Add(Item.release());
  10041. AddMenuSeparator(Menu);
  10042. if (Terminal != NULL)
  10043. {
  10044. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10045. }
  10046. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10047. Item.reset(new TTBXItem(Owner));
  10048. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10049. Menu->Add(Item.release());
  10050. }
  10051. //---------------------------------------------------------------------------
  10052. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10053. {
  10054. bool Result = true;
  10055. bool Remote = !IsSideLocalBrowser(Side);
  10056. try
  10057. {
  10058. if (Remote)
  10059. {
  10060. Terminal->ExceptionOnFail = true;
  10061. }
  10062. try
  10063. {
  10064. DirView(Side)->Path = Path;
  10065. }
  10066. __finally
  10067. {
  10068. if (Remote)
  10069. {
  10070. Terminal->ExceptionOnFail = false;
  10071. }
  10072. }
  10073. }
  10074. catch (Exception & E)
  10075. {
  10076. if (!Remote || Terminal->Active)
  10077. {
  10078. ShowExtendedExceptionEx(Terminal, &E);
  10079. Result = false;
  10080. }
  10081. else
  10082. {
  10083. throw;
  10084. }
  10085. }
  10086. return Result;
  10087. }
  10088. //---------------------------------------------------------------------------
  10089. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10090. {
  10091. // Make sure there some feedback even when loading tiny local folders
  10092. TInstantOperationVisualizer Visualizer;
  10093. DirView(Side)->ReloadDirectory();
  10094. }
  10095. //---------------------------------------------------------------------------
  10096. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10097. {
  10098. if (Index == SessionsPageControl->TabIndex)
  10099. {
  10100. CloseSession();
  10101. }
  10102. else
  10103. {
  10104. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10105. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10106. TTerminalManager * Manager = TTerminalManager::Instance();
  10107. if (!Terminal->Active)
  10108. {
  10109. Manager->FreeTerminal(Terminal);
  10110. }
  10111. else
  10112. {
  10113. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10114. if (CanCloseQueue(Queue))
  10115. {
  10116. Manager->FreeTerminal(Terminal);
  10117. }
  10118. }
  10119. }
  10120. }
  10121. //---------------------------------------------------------------------------
  10122. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10123. {
  10124. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10125. if (Session == NULL)
  10126. {
  10127. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10128. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10129. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10130. //P = SessionsPageControl->ClientToScreen(P);
  10131. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10132. }
  10133. else
  10134. {
  10135. CloseSessionTab(Index);
  10136. }
  10137. }
  10138. //---------------------------------------------------------------------------
  10139. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10140. {
  10141. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10142. {
  10143. TInstantOperationVisualizer Visualizer;
  10144. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10145. // before we create a new data object
  10146. ClipboardClear();
  10147. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10148. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10149. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10150. // Intermediate variable to WORKAROUND a crash in compiler
  10151. TObjectList * ClipboardFakeMonitors =
  10152. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10153. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10154. if (FClipboardDragDropFilesEx.get() == NULL)
  10155. {
  10156. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10157. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10158. }
  10159. FClipboardDragDropFilesEx->FileList->Clear();
  10160. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10161. FClipboardDragDropFilesEx->CopyToClipboard();
  10162. FClipboardTerminal = Terminal;
  10163. // Need full paths, as cwd can be different once files are pasted
  10164. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10165. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10166. {
  10167. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10168. }
  10169. }
  10170. }
  10171. //---------------------------------------------------------------------------
  10172. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10173. {
  10174. if (OpenClipboard(0))
  10175. {
  10176. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10177. CloseClipboard();
  10178. }
  10179. }
  10180. //---------------------------------------------------------------------------
  10181. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10182. {
  10183. return !FClipboardFakeDirectory.IsEmpty();
  10184. }
  10185. //---------------------------------------------------------------------------
  10186. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10187. {
  10188. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10189. FClipboardFakeDirectory = UnicodeString();
  10190. FClipboardTerminal = NULL;
  10191. // Also called in Idle()
  10192. FClipboardFakeMonitors.reset(NULL);
  10193. }
  10194. //---------------------------------------------------------------------------
  10195. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10196. {
  10197. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10198. {
  10199. ClipboardStop();
  10200. }
  10201. }
  10202. //---------------------------------------------------------------------------
  10203. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10204. {
  10205. TTerminalManager * Manager = TTerminalManager::Instance();
  10206. Manager->ActiveSession = FClipboardTerminal;
  10207. TTransferOperationParam Params;
  10208. Params.TargetDirectory = TargetDirectory;
  10209. Params.DragDrop = DragDrop;
  10210. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10211. }
  10212. //---------------------------------------------------------------------------
  10213. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10214. {
  10215. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  10216. // (as it pumps a Windows message queue while being shutted down)
  10217. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10218. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10219. {
  10220. // Can fail as it can be e.g. locked by AV, so we retry that later
  10221. bool Removed = RemoveDir(ApiPath(FileName));
  10222. try
  10223. {
  10224. if (!NonVisualDataModule->Busy)
  10225. {
  10226. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10227. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10228. }
  10229. }
  10230. __finally
  10231. {
  10232. if (!Removed)
  10233. {
  10234. RemoveDir(ApiPath(FileName));
  10235. }
  10236. }
  10237. }
  10238. }
  10239. //---------------------------------------------------------------------------
  10240. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10241. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10242. {
  10243. TFileMasks::TParams MaskParams;
  10244. MaskParams.Size = Size;
  10245. MaskParams.Modification = Modification;
  10246. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10247. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10248. {
  10249. bool ImplicitMatch;
  10250. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  10251. !ImplicitMatch)
  10252. {
  10253. Color = Iter->Color;
  10254. break;
  10255. }
  10256. }
  10257. }
  10258. //---------------------------------------------------------------------------
  10259. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10260. {
  10261. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10262. if (!DirView->IsEditing())
  10263. {
  10264. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10265. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10266. // while searching
  10267. if (Key == VK_BACK)
  10268. {
  10269. if (!FIncrementalSearch.IsEmpty())
  10270. {
  10271. if (FIncrementalSearch.Length() == 1)
  10272. {
  10273. ResetIncrementalSearch();
  10274. }
  10275. else
  10276. {
  10277. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10278. IncrementalSearch(NewText, false, false);
  10279. }
  10280. Key = 0;
  10281. }
  10282. }
  10283. }
  10284. }
  10285. //---------------------------------------------------------------------------
  10286. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10287. {
  10288. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10289. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10290. {
  10291. // Filter control sequences.
  10292. // When not searching yet, prefer use of the space for toggling file selection
  10293. // (so we cannot incrementally search for a string starting with a space).
  10294. if ((Key > VK_SPACE) ||
  10295. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10296. {
  10297. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10298. Key = 0;
  10299. }
  10300. }
  10301. }
  10302. //---------------------------------------------------------------------------
  10303. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10304. {
  10305. if (!FIncrementalSearch.IsEmpty())
  10306. {
  10307. FIncrementalSearch = L"";
  10308. DirView(osCurrent)->UpdateStatusBar();
  10309. }
  10310. }
  10311. //---------------------------------------------------------------------------
  10312. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10313. {
  10314. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10315. if (Item == NULL)
  10316. {
  10317. MessageBeep(MB_ICONHAND);
  10318. }
  10319. else
  10320. {
  10321. TCustomDirView * ADirView = DirView(osCurrent);
  10322. {
  10323. TAutoNestingCounter Guard(FIncrementalSearching);
  10324. ADirView->FocusItem(Item);
  10325. if (ADirView->NortonLike == nlOff)
  10326. {
  10327. if ((ADirView->Selected != Item) ||
  10328. (ADirView->SelCount > 1))
  10329. {
  10330. ADirView->ClearSelection();
  10331. ADirView->Selected = Item;
  10332. }
  10333. }
  10334. }
  10335. FIncrementalSearch = Text;
  10336. Item->MakeVisible(false);
  10337. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10338. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10339. ADirView->UpdateStatusBar();
  10340. }
  10341. }
  10342. //---------------------------------------------------------------------------
  10343. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10344. {
  10345. int Index = Item->Index;
  10346. if (!Reverse)
  10347. {
  10348. Index++;
  10349. if (Index >= Item->Owner->Count)
  10350. {
  10351. Index = 0;
  10352. }
  10353. }
  10354. else
  10355. {
  10356. Index--;
  10357. if (Index < 0)
  10358. {
  10359. Index = Item->Owner->Count - 1;
  10360. }
  10361. }
  10362. return Item->Owner->Item[Index];
  10363. }
  10364. //---------------------------------------------------------------------------
  10365. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10366. {
  10367. TCustomDirView * ADirView = DirView(osCurrent);
  10368. if (ADirView->Items->Count == 0)
  10369. {
  10370. return NULL;
  10371. }
  10372. else
  10373. {
  10374. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10375. TListItem * Item = CurrentItem;
  10376. if (SkipCurrent)
  10377. {
  10378. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10379. }
  10380. while (true)
  10381. {
  10382. bool Matches = false;
  10383. switch (WinConfiguration->PanelSearch)
  10384. {
  10385. case isNameStartOnly:
  10386. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  10387. break;
  10388. case isName:
  10389. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  10390. break;
  10391. case isAll:
  10392. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10393. int Index = 0;
  10394. while ((Index < ColCount) && !Matches)
  10395. {
  10396. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10397. if (Visible)
  10398. {
  10399. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10400. }
  10401. Index++;
  10402. }
  10403. break;
  10404. }
  10405. if (Matches)
  10406. {
  10407. return Item;
  10408. }
  10409. Item = GetNextFile(Item, Reverse);
  10410. if (Item == CurrentItem)
  10411. {
  10412. return NULL;
  10413. }
  10414. }
  10415. }
  10416. }
  10417. //---------------------------------------------------------------------------
  10418. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10419. {
  10420. ResetIncrementalSearch();
  10421. }
  10422. //---------------------------------------------------------------------------
  10423. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10424. {
  10425. if (Message.CharCode == VK_TAB)
  10426. {
  10427. if (!FIncrementalSearch.IsEmpty())
  10428. {
  10429. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10430. bool Reverse = Shift.Contains(ssShift);
  10431. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10432. Message.Result = 1;
  10433. return;
  10434. }
  10435. }
  10436. else if (Message.CharCode == VK_ESCAPE)
  10437. {
  10438. if (!FIncrementalSearch.IsEmpty())
  10439. {
  10440. ResetIncrementalSearch();
  10441. Message.Result = 1;
  10442. return;
  10443. }
  10444. }
  10445. TForm::Dispatch(&Message);
  10446. }
  10447. //---------------------------------------------------------------------------
  10448. void __fastcall TCustomScpExplorerForm::Deactivate()
  10449. {
  10450. TForm::Deactivate();
  10451. // E.g. when switching to an internal editor window
  10452. ResetIncrementalSearch();
  10453. }
  10454. //---------------------------------------------------------------------------
  10455. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10456. {
  10457. // When switching to another application
  10458. ResetIncrementalSearch();
  10459. }
  10460. //---------------------------------------------------------------------------
  10461. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10462. {
  10463. ResetIncrementalSearch();
  10464. }
  10465. //---------------------------------------------------------------------------
  10466. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10467. {
  10468. if (FIncrementalSearching <= 0)
  10469. {
  10470. ResetIncrementalSearch();
  10471. }
  10472. }
  10473. //---------------------------------------------------------------------------
  10474. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10475. {
  10476. CountClicksForWindowPrint(this);
  10477. }
  10478. //---------------------------------------------------------------------------
  10479. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10480. {
  10481. CenterReconnectToolbar();
  10482. }
  10483. //---------------------------------------------------------------------------
  10484. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10485. {
  10486. if (RemoteDirView->ItemFocused != NULL)
  10487. {
  10488. RemoteDirView->ItemFocused->Selected = true;
  10489. }
  10490. }
  10491. //---------------------------------------------------------------------------
  10492. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10493. {
  10494. if (!FQueueStatusUpdating)
  10495. {
  10496. bool Refresh;
  10497. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10498. {
  10499. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10500. }
  10501. else
  10502. {
  10503. if (FQueueFileList->GetCount() > 0)
  10504. {
  10505. FQueueFileList->Clear();
  10506. Refresh = true;
  10507. }
  10508. }
  10509. int Count = FQueueFileList->GetCount();
  10510. if (QueueFileList->Items->Count != Count)
  10511. {
  10512. DebugAssert(Refresh);
  10513. QueueFileList->Items->Count = Count;
  10514. }
  10515. if (Refresh)
  10516. {
  10517. QueueFileList->Invalidate();
  10518. }
  10519. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10520. }
  10521. }
  10522. //---------------------------------------------------------------------------
  10523. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10524. {
  10525. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10526. }
  10527. //---------------------------------------------------------------------------
  10528. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10529. {
  10530. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10531. // See TSynchronizeChecklistDialog::UpdateTimer
  10532. if (QueueFileList->Items->Count > 0)
  10533. {
  10534. QueueFileList->Items->Item[0]->MakeVisible(false);
  10535. }
  10536. UpdateQueueFileList();
  10537. }
  10538. //---------------------------------------------------------------------------
  10539. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10540. {
  10541. Active = QueueView3->Focused() || QueueFileList->Focused();
  10542. }
  10543. //---------------------------------------------------------------------------
  10544. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10545. {
  10546. QueueLabelUpdateStatus();
  10547. }
  10548. //---------------------------------------------------------------------------
  10549. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10550. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10551. {
  10552. int State = FQueueFileList->GetState(Item->Index);
  10553. if (State == qfsProcessed)
  10554. {
  10555. Sender->Canvas->Font->Color = clGrayText;
  10556. }
  10557. }
  10558. //---------------------------------------------------------------------------
  10559. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10560. {
  10561. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10562. QueueFileList->Column[0]->Width =
  10563. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10564. }
  10565. //---------------------------------------------------------------------------
  10566. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10567. {
  10568. QueueFileListColumnAutoSize();
  10569. }
  10570. //---------------------------------------------------------------------------
  10571. void __fastcall TCustomScpExplorerForm::CloseApp()
  10572. {
  10573. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10574. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10575. // So we clear the flag here.
  10576. DebugAssert(NonVisualDataModule->Busy);
  10577. NonVisualDataModule->EndBusy();
  10578. try
  10579. {
  10580. Close();
  10581. }
  10582. __finally
  10583. {
  10584. NonVisualDataModule->StartBusy();
  10585. }
  10586. }
  10587. //---------------------------------------------------------------------------
  10588. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10589. {
  10590. return (Terminal != NULL) && Terminal->Active;
  10591. }
  10592. //---------------------------------------------------------------------------
  10593. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  10594. {
  10595. return (ManagedSession != NULL);
  10596. }
  10597. //---------------------------------------------------------------------------
  10598. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10599. {
  10600. return IsActiveTerminal(Terminal);
  10601. }
  10602. //---------------------------------------------------------------------------
  10603. void TCustomScpExplorerForm::LocalLocalCopy(
  10604. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  10605. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  10606. {
  10607. DebugFail();
  10608. }
  10609. //---------------------------------------------------------------------------
  10610. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  10611. {
  10612. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  10613. }
  10614. //---------------------------------------------------------------------------
  10615. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  10616. {
  10617. // Changing tab list triggers the OnResize.
  10618. if (!FUpdatingSessionTabs)
  10619. {
  10620. // UpdateTabsCaptionTruncation should be enough
  10621. SessionListChanged(true);
  10622. }
  10623. }
  10624. //---------------------------------------------------------------------------
  10625. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  10626. {
  10627. if (ASession == ManagedSession)
  10628. {
  10629. UpdateSession(ASession);
  10630. }
  10631. TSessionData * Data = ASession->StateData;
  10632. UnicodeString Result;
  10633. if (Side == osLocal)
  10634. {
  10635. Result = Data->LocalDirectory;
  10636. }
  10637. else if (DebugAlwaysTrue(Side == osOther))
  10638. {
  10639. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  10640. }
  10641. return Result;
  10642. }
  10643. //---------------------------------------------------------------------------
  10644. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  10645. {
  10646. UnicodeString Result;
  10647. if (!ASession->Active)
  10648. {
  10649. Result = LoadStr(STATUS_NOT_CONNECTED2);
  10650. }
  10651. else
  10652. {
  10653. Result = GetSessionPath(ASession, osRemote);
  10654. }
  10655. return Result;
  10656. }
  10657. //---------------------------------------------------------------------------
  10658. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  10659. {
  10660. return LoadStr(NEW_REMOTE_TAB_HINT);
  10661. }
  10662. //---------------------------------------------------------------------------
  10663. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  10664. {
  10665. if (Index >= 0)
  10666. {
  10667. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10668. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  10669. if (TabSession == NULL)
  10670. {
  10671. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  10672. if (NewTabTabCaption != TabSheet->Caption)
  10673. {
  10674. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  10675. }
  10676. }
  10677. else
  10678. {
  10679. TTerminalManager * Manager = TTerminalManager::Instance();
  10680. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  10681. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  10682. }
  10683. }
  10684. }
  10685. //---------------------------------------------------------------------------
  10686. typedef std::vector<TListItem *> TListItemsVector;
  10687. struct TCalculateSizeOperation
  10688. {
  10689. TCalculateSizeStats Stats;
  10690. TListItemsVector ListItems;
  10691. size_t Index;
  10692. };
  10693. //---------------------------------------------------------------------------
  10694. void TCustomScpExplorerForm::CalculateDirectorySizes()
  10695. {
  10696. TCalculateSizeOperation CalculateSizeOperation;
  10697. TOperationSide Side = GetSide(osCurrent);
  10698. TCustomDirView * ADirView = DirView(Side);
  10699. bool FullPath = IsSideLocalBrowser(Side);
  10700. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, FullPath, NULL, true));
  10701. std::unique_ptr<TStrings> FileList(new TStringList());
  10702. for (int Index = 0; Index < AllFileList->Count; Index++)
  10703. {
  10704. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  10705. if (ADirView->ItemIsDirectory(Item))
  10706. {
  10707. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  10708. CalculateSizeOperation.ListItems.push_back(Item);
  10709. }
  10710. }
  10711. CalculateSizeOperation.Index = 0;
  10712. __int64 Size = 0;
  10713. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation);
  10714. FCalculateSizeOperation = &CalculateSizeOperation;
  10715. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  10716. FOnFileOperationFinished = DirectorySizeCalculated;
  10717. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation);
  10718. FPrimaryOperation = foCalculateSize;
  10719. TCalculatedSizes CalculatedSizes;
  10720. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  10721. if (Side == osLocal)
  10722. {
  10723. Terminal->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  10724. }
  10725. else
  10726. {
  10727. TCalculateSizeParams Params;
  10728. Params.Stats = &CalculateSizeOperation.Stats;
  10729. Params.UseCache = true;
  10730. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  10731. }
  10732. }
  10733. //---------------------------------------------------------------------------
  10734. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(TOperationSide Side, const UnicodeString & FileName, bool)
  10735. {
  10736. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  10737. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  10738. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  10739. // optimization
  10740. bool GotExpectedItem =
  10741. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  10742. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  10743. int Index = -1;
  10744. if (DebugAlwaysTrue(GotExpectedItem))
  10745. {
  10746. Index = FCalculateSizeOperation->Index;
  10747. FCalculateSizeOperation->Index++;
  10748. }
  10749. else
  10750. {
  10751. TListItemsVector::iterator I =
  10752. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  10753. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  10754. {
  10755. Index = I - FCalculateSizeOperation->ListItems.begin();
  10756. }
  10757. }
  10758. if (DebugAlwaysTrue(Index >= 0))
  10759. {
  10760. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  10761. ADirView->SetItemCalculatedSize(Item, Size);
  10762. }
  10763. }