CustomScpExplorer.cpp 378 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433
  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. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2561. if (FMoveToQueue)
  2562. {
  2563. Abort();
  2564. }
  2565. }
  2566. catch (EAbort &)
  2567. {
  2568. if (FMoveToQueue)
  2569. {
  2570. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2571. DebugAssert(CopyParam.TransferSkipList == NULL);
  2572. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2573. if (TransferResumeList->Count > 0)
  2574. {
  2575. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2576. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2577. }
  2578. CopyParam.TransferSkipList = TransferResumeList.release();
  2579. // not really needed, just to keep it consistent with TransferResumeList
  2580. FTransferResumeList = NULL;
  2581. FMoveToQueue = false;
  2582. Configuration->Usage->Inc("MovesToBackground");
  2583. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2584. ClearTransferSourceSelection(Direction);
  2585. }
  2586. throw;
  2587. }
  2588. }
  2589. __finally
  2590. {
  2591. if (!SelectionRestored && (DView != NULL))
  2592. {
  2593. DView->DiscardSavedSelection();
  2594. }
  2595. FTransferResumeList = NULL;
  2596. }
  2597. }
  2598. return Result;
  2599. }
  2600. //---------------------------------------------------------------------------
  2601. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2602. TOperationSide Side, TStrings * FileList, void * Param)
  2603. {
  2604. DebugAssert(FileList->Count);
  2605. // We deliberately do not toggle alternative flag (Param), but use OR,
  2606. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2607. // shortcut of CurrentDeleteAlternativeAction
  2608. bool Alternative =
  2609. bool(Param) || UseAlternativeFunction();
  2610. bool Recycle;
  2611. if (IsSideLocalBrowser(Side))
  2612. {
  2613. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2614. }
  2615. else
  2616. {
  2617. Recycle =
  2618. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2619. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2620. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2621. }
  2622. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2623. if (!Result)
  2624. {
  2625. UnicodeString Query;
  2626. if (FileList->Count == 1)
  2627. {
  2628. if (IsSideLocalBrowser(Side))
  2629. {
  2630. Query = ExtractFileName(FileList->Strings[0]);
  2631. }
  2632. else
  2633. {
  2634. Query = UnixExtractFileName(FileList->Strings[0]);
  2635. }
  2636. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2637. }
  2638. else
  2639. {
  2640. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2641. }
  2642. TMessageParams Params(mpNeverAskAgainCheck);
  2643. Params.ImageName = L"Delete file";
  2644. unsigned int Answer =
  2645. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2646. if (Answer == qaNeverAskAgain)
  2647. {
  2648. Result = true;
  2649. if (Recycle)
  2650. {
  2651. WinConfiguration->ConfirmRecycling = false;
  2652. }
  2653. else
  2654. {
  2655. WinConfiguration->ConfirmDeleting = false;
  2656. }
  2657. }
  2658. else
  2659. {
  2660. Result = (Answer == qaOK);
  2661. }
  2662. }
  2663. if (Result)
  2664. {
  2665. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2666. }
  2667. return Result;
  2668. }
  2669. //---------------------------------------------------------------------------
  2670. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2671. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2672. {
  2673. TAutoBatch AutoBatch(this);
  2674. bool Result;
  2675. if ((Operation == foCopy) || (Operation == foMove))
  2676. {
  2677. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2678. }
  2679. else if (Operation == foRename)
  2680. {
  2681. DebugAssert(DirView(Side)->ItemFocused);
  2682. DirView(Side)->ItemFocused->EditCaption();
  2683. Result = true;
  2684. }
  2685. else if (Operation == foDelete)
  2686. {
  2687. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2688. }
  2689. else if (Operation == foSetProperties)
  2690. {
  2691. DirView(osRemote)->SaveSelectedNames();
  2692. Result = SetProperties(Side, FileList);
  2693. }
  2694. else if (Operation == foCustomCommand)
  2695. {
  2696. DebugAssert(Param);
  2697. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2698. DirView(osRemote)->SaveSelectedNames();
  2699. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2700. CustomCommand(FileList, *Command, NULL);
  2701. Result = true;
  2702. }
  2703. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2704. {
  2705. DebugAssert(!IsSideLocalBrowser(Side));
  2706. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2707. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2708. }
  2709. else if (Operation == foLock)
  2710. {
  2711. DebugAssert(!IsSideLocalBrowser(Side));
  2712. LockFiles(FileList, true);
  2713. Result = true;
  2714. }
  2715. else if (Operation == foUnlock)
  2716. {
  2717. DebugAssert(!IsSideLocalBrowser(Side));
  2718. LockFiles(FileList, false);
  2719. Result = true;
  2720. }
  2721. else
  2722. {
  2723. DebugFail();
  2724. }
  2725. return Result;
  2726. }
  2727. //---------------------------------------------------------------------------
  2728. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2729. {
  2730. if (Side == osCurrent)
  2731. {
  2732. Side = FCurrentSide;
  2733. }
  2734. return Side;
  2735. }
  2736. //---------------------------------------------------------------------------
  2737. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2738. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2739. {
  2740. Side = GetSide(Side);
  2741. bool Result;
  2742. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, IsSideLocalBrowser(Side), NULL);
  2743. try
  2744. {
  2745. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2746. }
  2747. __finally
  2748. {
  2749. delete FileList;
  2750. }
  2751. return Result;
  2752. }
  2753. //---------------------------------------------------------------------------
  2754. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2755. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2756. bool NoConfirmation, void * Param)
  2757. {
  2758. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2759. {
  2760. if ((Operation == foCopy) || (Operation == foMove))
  2761. {
  2762. DebugAssert(!IsLocalBrowserMode());
  2763. if (GetSide(Side) == osLocal)
  2764. {
  2765. Configuration->Usage->Inc(L"UploadsCommand");
  2766. }
  2767. else
  2768. {
  2769. Configuration->Usage->Inc(L"DownloadsCommand");
  2770. }
  2771. }
  2772. }
  2773. }
  2774. //---------------------------------------------------------------------------
  2775. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2776. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2777. {
  2778. TTransferOperationParam Param;
  2779. if ((WinConfiguration->Interface != ifCommander) ||
  2780. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2781. {
  2782. Flags &= ~cocShortCutHint;
  2783. }
  2784. TCustomDirView * DView = DirView(Side);
  2785. Param.Options =
  2786. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2787. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2788. if (FLAGSET(Flags, cocQueue))
  2789. {
  2790. Param.Queue = asOn;
  2791. }
  2792. else if (FLAGSET(Flags, cocNonQueue))
  2793. {
  2794. Param.Queue = asOff;
  2795. }
  2796. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2797. }
  2798. //---------------------------------------------------------------------------
  2799. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2800. {
  2801. int Count = ErrorList->Count;
  2802. try
  2803. {
  2804. if (ErrorList->Count)
  2805. {
  2806. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2807. if (MessageDialog(Message, qtError,
  2808. qaOK | qaCancel, HELP_NONE) == qaOK)
  2809. {
  2810. unsigned int Answer;
  2811. int Index = 0;
  2812. do
  2813. {
  2814. DebugAssert(Index >= 0 && Index < Count);
  2815. TQueryButtonAlias Aliases[2];
  2816. Aliases[0].Button = qaYes;
  2817. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2818. Aliases[1].Button = qaNo;
  2819. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2820. TMessageParams Params;
  2821. Params.Aliases = Aliases;
  2822. Params.AliasesCount = LENOF(Aliases);
  2823. int No = Index + 1;
  2824. UnicodeString Message = ErrorList->Strings[Index];
  2825. UnicodeString AMainInstructions;
  2826. UnicodeString Details;
  2827. if (ExtractMainInstructions(Message, AMainInstructions))
  2828. {
  2829. Details = Message;
  2830. Message = AMainInstructions;
  2831. }
  2832. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2833. Answer = MoreMessageDialog(
  2834. Message,
  2835. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2836. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2837. qaOK, HELP_NONE, &Params);
  2838. if (Answer == qaNo)
  2839. {
  2840. Index++;
  2841. }
  2842. if (Answer == qaYes)
  2843. {
  2844. Index--;
  2845. }
  2846. }
  2847. while (Answer != qaOK);
  2848. }
  2849. }
  2850. }
  2851. __finally
  2852. {
  2853. TStrings * List = ErrorList;
  2854. ErrorList = NULL;
  2855. for (int i = 0; i < Count; i++)
  2856. {
  2857. delete List->Objects[i];
  2858. }
  2859. delete List;
  2860. }
  2861. }
  2862. //---------------------------------------------------------------------------
  2863. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2864. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2865. {
  2866. // needed for checking filemasks, as there's no directory object
  2867. // associated with the file object
  2868. File->FullFileName = FullFileName;
  2869. TFileMasks::TParams MaskParams;
  2870. MaskParams.Size = File->Size;
  2871. MaskParams.Modification = File->Modification;
  2872. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2873. }
  2874. //---------------------------------------------------------------------------
  2875. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2876. {
  2877. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2878. Side = GetSide(Side);
  2879. TCustomDirView * DView = DirView(Side);
  2880. TListItem * FocusedItem = DView->ItemFocused;
  2881. UnicodeString Name;
  2882. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2883. {
  2884. Name = DView->ItemFileName(FocusedItem);
  2885. }
  2886. else
  2887. {
  2888. Name = LoadStr(NEW_FILE);
  2889. }
  2890. UnicodeString Names = Name;
  2891. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2892. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2893. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  2894. {
  2895. while (!Names.IsEmpty())
  2896. {
  2897. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2898. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2899. UnicodeString TargetFileName;
  2900. UnicodeString LocalFileName;
  2901. UnicodeString RootTempDir;
  2902. UnicodeString TempDir;
  2903. UnicodeString RemoteDirectory;
  2904. bool ExistingFile = false;
  2905. if (!IsSideLocalBrowser(Side))
  2906. {
  2907. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  2908. TRemoteFile * File = NULL;
  2909. if (Terminal->FileExists(Name, &File) &&
  2910. !File->IsDirectory)
  2911. {
  2912. try
  2913. {
  2914. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2915. ExistingFile = true;
  2916. }
  2917. __finally
  2918. {
  2919. delete File;
  2920. }
  2921. }
  2922. if (!ExistingFile)
  2923. {
  2924. RemoteDirectory = UnixExtractFilePath(Name);
  2925. TemporaryDirectoryForRemoteFiles(
  2926. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2927. TargetFileName = UnixExtractFileName(Name);
  2928. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2929. LocalFileName = TempDir +
  2930. // We probably do not want to trim the VMS version here
  2931. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2932. }
  2933. }
  2934. else
  2935. {
  2936. if (ExtractFilePath(Name).IsEmpty())
  2937. {
  2938. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2939. }
  2940. else
  2941. {
  2942. LocalFileName = ExpandFileName(Name);
  2943. }
  2944. TargetFileName = ExtractFileName(Name);
  2945. }
  2946. if (!ExistingFile)
  2947. {
  2948. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2949. if (NewFile)
  2950. {
  2951. int File = FileCreate(ApiPath(LocalFileName));
  2952. if (File < 0)
  2953. {
  2954. if (!RootTempDir.IsEmpty())
  2955. {
  2956. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2957. }
  2958. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2959. }
  2960. else
  2961. {
  2962. FileClose(File);
  2963. }
  2964. }
  2965. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2966. const TEditorData * ExternalEditor = NULL;
  2967. TFileMasks::TParams MaskParams; // size not known
  2968. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2969. false, MaskParams);
  2970. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2971. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  2972. }
  2973. }
  2974. }
  2975. }
  2976. //---------------------------------------------------------------------------
  2977. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2978. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2979. {
  2980. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2981. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2982. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2983. return
  2984. ((ExecuteFileBy == efInternalEditor)) ||
  2985. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2986. }
  2987. //---------------------------------------------------------------------------
  2988. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2989. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2990. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2991. UnicodeString RemoteDirectory, bool NewFile)
  2992. {
  2993. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2994. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2995. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2996. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2997. Side = GetSide(Side);
  2998. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2999. if (!IsSideLocalBrowser(Side))
  3000. {
  3001. Data->Terminal = Terminal;
  3002. Data->Queue = Queue;
  3003. Data->SessionData = CloneCurrentSessionData();
  3004. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3005. Data->RemoteDirectory = RemoteDirectory;
  3006. Data->SessionName = Terminal->SessionData->SessionName;
  3007. Data->LocalRootDirectory = LocalRootDirectory;
  3008. Data->OriginalFileName = OriginalFileName;
  3009. Data->Command = L""; // will be changed later for external editor
  3010. }
  3011. if (ExecuteFileBy == efInternalEditor)
  3012. {
  3013. if (!IsSideLocalBrowser(Side))
  3014. {
  3015. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3016. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3017. TForm * Editor;
  3018. try
  3019. {
  3020. TNotifyEvent OnFileChanged = NULL;
  3021. TNotifyEvent OnSaveAll = NULL;
  3022. TAnyModifiedEvent OnAnyModified = NULL;
  3023. // Edited files are uploaded in background queue, what we do not support with encrypted files
  3024. if (Terminal->IsCapable[fcBackgroundTransfers])
  3025. {
  3026. OnFileChanged = FEditorManager->FileChanged;
  3027. OnSaveAll = SaveAllInternalEditors;
  3028. OnAnyModified = AnyInternalEditorModified;
  3029. }
  3030. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  3031. FEditorManager->FileReload, FEditorManager->FileClosed,
  3032. OnSaveAll, OnAnyModified,
  3033. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3034. }
  3035. catch(...)
  3036. {
  3037. if (!LocalRootDirectory.IsEmpty())
  3038. {
  3039. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  3040. }
  3041. throw;
  3042. }
  3043. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3044. }
  3045. else
  3046. {
  3047. DebugAssert(!FStandaloneEditing);
  3048. TForm * Editor =
  3049. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3050. SaveAllInternalEditors, AnyInternalEditorModified,
  3051. L"", false, SessionColor, -1, NewFile);
  3052. FLocalEditors->Add(Editor);
  3053. }
  3054. }
  3055. else
  3056. {
  3057. HANDLE Process;
  3058. if (ExecuteFileBy == efExternalEditor)
  3059. {
  3060. UnicodeString Program, Params, Dir;
  3061. Data->Command = ExternalEditor->ExternalEditor;
  3062. ReformatFileNameCommand(Data->Command);
  3063. SplitCommand(Data->Command, Program, Params, Dir);
  3064. Params = ExpandFileNameCommand(Params, FileName);
  3065. Program = ExpandEnvironmentVariables(Program);
  3066. if (!ExecuteShell(Program, Params, Process))
  3067. {
  3068. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3069. }
  3070. }
  3071. else
  3072. {
  3073. DebugAssert(!IsSideLocalBrowser(Side));
  3074. if (!ExecuteShell(FileName, L"", Process))
  3075. {
  3076. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3077. }
  3078. }
  3079. if (IsSideLocalBrowser(Side) ||
  3080. ((ExecuteFileBy == efShell) &&
  3081. !WinConfiguration->Editor.SDIShellEditor) ||
  3082. ((ExecuteFileBy == efExternalEditor) &&
  3083. !ExternalEditor->SDIExternalEditor))
  3084. {
  3085. // no need for handle
  3086. if (Process != NULL)
  3087. {
  3088. DebugCheck(CloseHandle(Process));
  3089. }
  3090. Process = INVALID_HANDLE_VALUE;
  3091. }
  3092. else
  3093. {
  3094. if (Process == NULL)
  3095. {
  3096. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3097. }
  3098. }
  3099. if (!IsSideLocalBrowser(Side))
  3100. {
  3101. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3102. }
  3103. }
  3104. }
  3105. //---------------------------------------------------------------------------
  3106. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3107. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3108. void * /*Arg*/)
  3109. {
  3110. if (Token != NULL)
  3111. {
  3112. EditorFormFileSave(static_cast<TForm *>(Token));
  3113. }
  3114. }
  3115. //---------------------------------------------------------------------------
  3116. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3117. {
  3118. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3119. {
  3120. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3121. }
  3122. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3123. }
  3124. //---------------------------------------------------------------------------
  3125. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3126. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3127. void * Arg)
  3128. {
  3129. if ((Token != NULL) &&
  3130. IsEditorFormModified(static_cast<TForm *>(Token)))
  3131. {
  3132. *static_cast<bool *>(Arg) = true;
  3133. }
  3134. }
  3135. //---------------------------------------------------------------------------
  3136. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3137. TObject * /*Sender*/, bool & Modified)
  3138. {
  3139. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3140. {
  3141. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3142. {
  3143. Modified = true;
  3144. }
  3145. }
  3146. if (!Modified)
  3147. {
  3148. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3149. }
  3150. }
  3151. //---------------------------------------------------------------------------
  3152. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3153. {
  3154. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3155. }
  3156. //---------------------------------------------------------------------------
  3157. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3158. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3159. UnicodeString & Result, UnicodeString & RootDirectory)
  3160. {
  3161. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3162. {
  3163. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3164. Result = RootDirectory;
  3165. }
  3166. else
  3167. {
  3168. RootDirectory = L"";
  3169. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3170. Result = IncludeTrailingBackslash(Result);
  3171. }
  3172. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3173. {
  3174. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3175. }
  3176. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3177. {
  3178. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3179. {
  3180. RemoteDirectory.Delete(1, 1);
  3181. }
  3182. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3183. }
  3184. if (!ForceDirectories(ApiPath(Result)))
  3185. {
  3186. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3187. }
  3188. }
  3189. //---------------------------------------------------------------------------
  3190. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3191. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3192. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3193. {
  3194. DebugAssert(!IsLocalBrowserMode());
  3195. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3196. if (ForceText)
  3197. {
  3198. CopyParam.TransferMode = tmAscii;
  3199. }
  3200. TemporaryFileCopyParam(CopyParam);
  3201. if (AllFiles)
  3202. {
  3203. CopyParam.IncludeFileMask = TFileMasks();
  3204. }
  3205. if (TempDir.IsEmpty())
  3206. {
  3207. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3208. }
  3209. DebugAssert(!FAutoOperation);
  3210. FAutoOperation = AutoOperation;
  3211. Terminal->ExceptionOnFail = true;
  3212. try
  3213. {
  3214. try
  3215. {
  3216. // turn off confirmations, as for MDI editors we may possibly download
  3217. // the same file over
  3218. Terminal->CopyToLocal(
  3219. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3220. if (GetTargetNames)
  3221. {
  3222. for (int i = 0; i < FileList->Count; i++)
  3223. {
  3224. FileList->Strings[i] =
  3225. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3226. }
  3227. }
  3228. }
  3229. catch(...)
  3230. {
  3231. if (!RootTempDir.IsEmpty())
  3232. {
  3233. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3234. }
  3235. throw;
  3236. }
  3237. }
  3238. __finally
  3239. {
  3240. FAutoOperation = false;
  3241. Terminal->ExceptionOnFail = false;
  3242. }
  3243. }
  3244. //---------------------------------------------------------------------------
  3245. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3246. {
  3247. // Do not waste time checking for default editor list the next time,
  3248. // or testing if default editor is not notepad.
  3249. bool TryNextTime = false;
  3250. UnicodeString UsageState;
  3251. if (!WinConfiguration->EditorList->IsDefaultList())
  3252. {
  3253. UsageState = "H";
  3254. }
  3255. else
  3256. {
  3257. UnicodeString Executable;
  3258. UnicodeString ExecutableDescription;
  3259. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3260. {
  3261. UnicodeString Message =
  3262. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3263. unsigned int Answer =
  3264. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3265. if (Answer != qaOK)
  3266. {
  3267. UsageState = "R";
  3268. }
  3269. else
  3270. {
  3271. UsageState = "A";
  3272. TEditorData EditorData;
  3273. EditorData.Editor = edExternal;
  3274. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3275. EditorData.ExternalEditorOptionsAutodetect();
  3276. TEditorList EditorList;
  3277. EditorList = *WinConfiguration->EditorList;
  3278. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3279. WinConfiguration->EditorList = &EditorList;
  3280. }
  3281. }
  3282. }
  3283. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3284. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3285. }
  3286. //---------------------------------------------------------------------------
  3287. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3288. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3289. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3290. {
  3291. if (ExecuteFileBy == efDefaultEditor)
  3292. {
  3293. if (!WinConfiguration->OfferedEditorAutoConfig)
  3294. {
  3295. EditorAutoConfig();
  3296. }
  3297. const TEditorPreferences * Editor =
  3298. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3299. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3300. {
  3301. ExecuteFileBy = efInternalEditor;
  3302. ExternalEditor = NULL;
  3303. }
  3304. else if (Editor->Data->Editor == edOpen)
  3305. {
  3306. ExecuteFileBy = efShell;
  3307. ExternalEditor = NULL;
  3308. }
  3309. else
  3310. {
  3311. ExecuteFileBy = efExternalEditor;
  3312. ExternalEditor = Editor->Data;
  3313. }
  3314. }
  3315. }
  3316. //---------------------------------------------------------------------------
  3317. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3318. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3319. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3320. {
  3321. UnicodeString OriginalFileName;
  3322. UnicodeString LocalRootDirectory;
  3323. UnicodeString RemoteDirectory;
  3324. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3325. IsSideLocalBrowser(Side), MaskParams);
  3326. UnicodeString Counter;
  3327. UnicodeString LocalFileName;
  3328. bool Handled = false;
  3329. if (!IsSideLocalBrowser(Side))
  3330. {
  3331. // We need to trim VMS version here, so that we use name without version
  3332. // when uploading back to create a new version of the file
  3333. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3334. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3335. TObject * Token = NULL;
  3336. UnicodeString LocalDirectory;
  3337. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3338. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3339. LocalDirectory))
  3340. {
  3341. if (Token != NULL)
  3342. {
  3343. TForm * Form = dynamic_cast<TForm *>(Token);
  3344. if (Form->WindowState == wsMinimized)
  3345. {
  3346. ShowWindow(Form->Handle, SW_RESTORE);
  3347. }
  3348. else
  3349. {
  3350. Form->SetFocus();
  3351. }
  3352. Handled = true;
  3353. }
  3354. else
  3355. {
  3356. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3357. }
  3358. }
  3359. if (!Handled)
  3360. {
  3361. TStringList * FileList1 = new TStringList();
  3362. try
  3363. {
  3364. FileList1->AddObject(FullFileName, Object);
  3365. TemporarilyDownloadFiles(FileList1,
  3366. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3367. LocalRootDirectory, LocalDirectory, true, true, true);
  3368. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3369. }
  3370. __finally
  3371. {
  3372. delete FileList1;
  3373. }
  3374. switch (ExecuteFileBy)
  3375. {
  3376. case efShell:
  3377. Counter = "RemoteFilesExecuted";
  3378. break;
  3379. case efInternalEditor:
  3380. Counter = "RemoteFilesOpenedInInternalEditor";
  3381. break;
  3382. case efExternalEditor:
  3383. Counter = "RemoteFilesOpenedInExternalEditor";
  3384. break;
  3385. default:
  3386. DebugFail();
  3387. }
  3388. }
  3389. }
  3390. else
  3391. {
  3392. LocalFileName = FullFileName;
  3393. OriginalFileName = ExtractFileName(FullFileName);
  3394. switch (ExecuteFileBy)
  3395. {
  3396. case efShell:
  3397. Counter = "LocalFilesExecuted";
  3398. break;
  3399. case efInternalEditor:
  3400. Counter = "LocalFilesOpenedInInternalEditor";
  3401. break;
  3402. case efExternalEditor:
  3403. Counter = "LocalFilesOpenedInExternalEditor";
  3404. break;
  3405. default:
  3406. DebugFail();
  3407. }
  3408. }
  3409. if (!Handled)
  3410. {
  3411. Configuration->Usage->Inc(Counter);
  3412. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3413. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3414. }
  3415. }
  3416. //---------------------------------------------------------------------------
  3417. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3418. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3419. bool AllSelected, bool OnFocused)
  3420. {
  3421. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3422. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3423. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3424. Side = GetSide(Side);
  3425. TCustomDirView * DView = DirView(Side);
  3426. TStrings * FileList =
  3427. AllSelected ?
  3428. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3429. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3430. try
  3431. {
  3432. DebugAssert(AllSelected || (FileList->Count == 1));
  3433. for (int i = 0; i < FileList->Count; i++)
  3434. {
  3435. UnicodeString ListFileName = FileList->Strings[i];
  3436. UnicodeString FileNameOnly =
  3437. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3438. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3439. if (!DView->ItemIsDirectory(Item))
  3440. {
  3441. UnicodeString FullFileName;
  3442. if (!IsSideLocalBrowser(Side))
  3443. {
  3444. FullFileName = DirView(Side)->Path + ListFileName;
  3445. }
  3446. else
  3447. {
  3448. FullFileName = ListFileName;
  3449. }
  3450. TFileMasks::TParams MaskParams;
  3451. MaskParams.Size = DView->ItemFileSize(Item);
  3452. TDateTimePrecision Precision;
  3453. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3454. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3455. FileList->Objects[i], MaskParams);
  3456. }
  3457. }
  3458. }
  3459. __finally
  3460. {
  3461. delete FileList;
  3462. }
  3463. }
  3464. //---------------------------------------------------------------------------
  3465. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3466. {
  3467. CopyParam.FileNameCase = ncNoChange;
  3468. CopyParam.PreserveRights = false;
  3469. CopyParam.PreserveReadOnly = false;
  3470. CopyParam.ReplaceInvalidChars = true;
  3471. CopyParam.IncludeFileMask = TFileMasks();
  3472. CopyParam.NewerOnly = false;
  3473. CopyParam.FileMask = L"";
  3474. }
  3475. //---------------------------------------------------------------------------
  3476. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3477. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3478. {
  3479. TTerminalManager * Manager = TTerminalManager::Instance();
  3480. if (!IsActiveTerminal(Data->Terminal))
  3481. {
  3482. if (!NonVisualDataModule->Busy)
  3483. {
  3484. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3485. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3486. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3487. if (Data->Terminal == NULL)
  3488. {
  3489. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3490. if (SameSiteTerminal != NULL)
  3491. {
  3492. UnicodeString Message =
  3493. FMTLOAD(EDIT_SESSION_REATTACH,
  3494. (FileNameOnly, Data->SessionName, FileNameOnly));
  3495. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3496. {
  3497. Data->Terminal = Terminal;
  3498. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3499. Data->SessionName = Terminal->SessionData->SessionName;
  3500. // We might also overwrite session data
  3501. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3502. }
  3503. else
  3504. {
  3505. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3506. Abort();
  3507. }
  3508. }
  3509. }
  3510. // foreground session should reconnect itself
  3511. else if (Terminal != Data->Terminal)
  3512. {
  3513. UnicodeString Message =
  3514. MainInstructions(
  3515. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3516. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3517. {
  3518. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3519. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3520. }
  3521. else
  3522. {
  3523. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3524. Abort();
  3525. }
  3526. }
  3527. }
  3528. if (!IsActiveTerminal(Data->Terminal))
  3529. {
  3530. Configuration->Usage->Inc(L"EditInactiveSession");
  3531. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3532. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3533. (ExtractFileName(FileName), Data->SessionName)));
  3534. }
  3535. }
  3536. TStrings * FileList = new TStringList();
  3537. try
  3538. {
  3539. FileList->Add(FileName);
  3540. // consider using the same settings (preset) as when the file was downloaded
  3541. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3542. TemporaryFileCopyParam(CopyParam);
  3543. if (Data->ForceText)
  3544. {
  3545. CopyParam.TransferMode = tmAscii;
  3546. }
  3547. // so i do not need to worry if masking algorithm works in all cases
  3548. // ("" means "copy file name", no masking is actually done)
  3549. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3550. {
  3551. CopyParam.FileMask = L"";
  3552. }
  3553. else
  3554. {
  3555. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3556. }
  3557. DebugAssert(Data->Queue != NULL);
  3558. int Params = cpNoConfirmation | cpTemporary;
  3559. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3560. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3561. QueueItem->CompleteEvent = UploadCompleteEvent;
  3562. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3563. }
  3564. __finally
  3565. {
  3566. delete FileList;
  3567. }
  3568. }
  3569. //---------------------------------------------------------------------------
  3570. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3571. const UnicodeString FileName, const TEditedFileData * Data)
  3572. {
  3573. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3574. DebugAssert(!NonVisualDataModule->Busy);
  3575. if (!IsActiveTerminal(Data->Terminal))
  3576. {
  3577. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3578. (ExtractFileName(FileName), Data->SessionName)));
  3579. }
  3580. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3581. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3582. NonVisualDataModule->StartBusy();
  3583. try
  3584. {
  3585. std::unique_ptr<TRemoteFile> File;
  3586. UnicodeString RemoteFileName =
  3587. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3588. Terminal->ExceptionOnFail = true;
  3589. try
  3590. {
  3591. TRemoteFile * AFile = NULL;
  3592. Terminal->ReadFile(RemoteFileName, AFile);
  3593. File.reset(AFile);
  3594. if (!File->HaveFullFileName)
  3595. {
  3596. File->FullFileName = RemoteFileName;
  3597. }
  3598. }
  3599. __finally
  3600. {
  3601. Terminal->ExceptionOnFail = false;
  3602. }
  3603. std::unique_ptr<TStrings> FileList(new TStringList());
  3604. FileList->AddObject(RemoteFileName, File.get());
  3605. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3606. UnicodeString TempDir = ExtractFilePath(FileName);
  3607. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3608. TempDir, true, true, true);
  3609. // sanity check, the target file name should be still the same
  3610. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3611. }
  3612. __finally
  3613. {
  3614. NonVisualDataModule->EndBusy();
  3615. // it actually may not exist anymore...
  3616. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3617. }
  3618. }
  3619. //---------------------------------------------------------------------------
  3620. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3621. const TEditedFileData * Data, bool & KeepOpen)
  3622. {
  3623. // Command is set for external editors only (not for "shell" open).
  3624. if (!Data->Command.IsEmpty())
  3625. {
  3626. bool AnyFound = false;
  3627. bool AnyMDI = false;
  3628. bool AnyNonMDI = false;
  3629. bool AnyDetect = false;
  3630. TEditorList * EditorList = new TEditorList();
  3631. try
  3632. {
  3633. *EditorList = *WinConfiguration->EditorList;
  3634. for (int i = 0; i < EditorList->Count; i++)
  3635. {
  3636. const TEditorPreferences * Editor = EditorList->Editors[i];
  3637. if ((Editor->Data->Editor == edExternal) &&
  3638. (Editor->Data->ExternalEditor == Data->Command))
  3639. {
  3640. AnyFound = true;
  3641. if (Editor->Data->SDIExternalEditor)
  3642. {
  3643. AnyNonMDI = true;
  3644. if (Editor->Data->DetectMDIExternalEditor)
  3645. {
  3646. AnyDetect = true;
  3647. }
  3648. }
  3649. else
  3650. {
  3651. AnyMDI = true;
  3652. }
  3653. }
  3654. }
  3655. bool EnableMDI = false;
  3656. bool DisableDetect = false;
  3657. if (AnyMDI)
  3658. {
  3659. KeepOpen = true;
  3660. if (AnyNonMDI)
  3661. {
  3662. // there is at least one instance of the editor with MDI support enabled,
  3663. // and one with disabled, enable it for all instances
  3664. EnableMDI = true;
  3665. }
  3666. }
  3667. else if (AnyFound && !AnyDetect)
  3668. {
  3669. // at least once instance found but all have MDI autodetection disabled
  3670. // => close the file (default action)
  3671. }
  3672. else
  3673. {
  3674. // no instance of the editor has MDI support enabled
  3675. TMessageParams Params;
  3676. if (AnyFound)
  3677. {
  3678. // there is at least one instance of the editor
  3679. Params.Params |= mpNeverAskAgainCheck;
  3680. }
  3681. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3682. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3683. switch (Answer)
  3684. {
  3685. case qaNeverAskAgain:
  3686. DisableDetect = true;
  3687. break;
  3688. case qaNo:
  3689. EnableMDI = true;
  3690. KeepOpen = true;
  3691. break;
  3692. }
  3693. }
  3694. if (AnyFound && (EnableMDI || DisableDetect))
  3695. {
  3696. bool Changed = false;
  3697. for (int i = 0; i < EditorList->Count; i++)
  3698. {
  3699. const TEditorPreferences * Editor = EditorList->Editors[i];
  3700. if ((Editor->Data->Editor == edExternal) &&
  3701. (Editor->Data->ExternalEditor == Data->Command) &&
  3702. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3703. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3704. {
  3705. Changed = true;
  3706. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3707. if (EnableMDI)
  3708. {
  3709. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3710. }
  3711. if (DisableDetect)
  3712. {
  3713. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3714. }
  3715. EditorList->Change(i, UpdatedEditor);
  3716. }
  3717. }
  3718. if (Changed)
  3719. {
  3720. WinConfiguration->EditorList = EditorList;
  3721. }
  3722. }
  3723. }
  3724. __finally
  3725. {
  3726. delete EditorList;
  3727. }
  3728. }
  3729. else
  3730. {
  3731. // "open" case
  3732. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3733. qaOK, HELP_APP_EARLY_CLOSED);
  3734. }
  3735. }
  3736. //---------------------------------------------------------------------------
  3737. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3738. {
  3739. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3740. }
  3741. //---------------------------------------------------------------------------
  3742. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3743. {
  3744. SideEnter(osRemote);
  3745. }
  3746. //---------------------------------------------------------------------------
  3747. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3748. {
  3749. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3750. SideEnter(osRemote);
  3751. }
  3752. //---------------------------------------------------------------------------
  3753. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3754. {
  3755. FCurrentSide = Side;
  3756. if (Visible)
  3757. {
  3758. UpdateControls();
  3759. }
  3760. }
  3761. //---------------------------------------------------------------------------
  3762. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3763. TStrings * FileList, bool Alternative)
  3764. {
  3765. TCustomDirView * DView = DirView(Side);
  3766. DView->SaveSelection();
  3767. DView->SaveSelectedNames();
  3768. DebugAssert(!FAlternativeDelete);
  3769. FAlternativeDelete = Alternative;
  3770. try
  3771. {
  3772. if (!IsSideLocalBrowser(Side))
  3773. {
  3774. DebugAssert(Terminal != NULL);
  3775. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3776. }
  3777. else
  3778. {
  3779. if (IsLocalBrowserMode())
  3780. {
  3781. Configuration->Usage->Inc(L"LocalLocalDeletes");
  3782. }
  3783. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  3784. FProgressSide = FCurrentSide;
  3785. try
  3786. {
  3787. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3788. }
  3789. __finally
  3790. {
  3791. ReloadLocalDirectory();
  3792. }
  3793. }
  3794. FAlternativeDelete = false;
  3795. }
  3796. catch(...)
  3797. {
  3798. FAlternativeDelete = false;
  3799. DView->DiscardSavedSelection();
  3800. throw;
  3801. }
  3802. DView->RestoreSelection();
  3803. }
  3804. //---------------------------------------------------------------------------
  3805. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3806. {
  3807. DebugAssert(Terminal);
  3808. DebugAssert(!IsLocalBrowserMode());
  3809. RemoteDirView->SaveSelection();
  3810. RemoteDirView->SaveSelectedNames();
  3811. try
  3812. {
  3813. if (Lock)
  3814. {
  3815. Terminal->LockFiles(FileList);
  3816. }
  3817. else
  3818. {
  3819. Terminal->UnlockFiles(FileList);
  3820. }
  3821. }
  3822. catch(...)
  3823. {
  3824. RemoteDirView->DiscardSavedSelection();
  3825. throw;
  3826. }
  3827. RemoteDirView->RestoreSelection();
  3828. }
  3829. //---------------------------------------------------------------------------
  3830. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3831. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3832. bool NoConfirmation, bool Move)
  3833. {
  3834. DebugAssert(Terminal != NULL);
  3835. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  3836. DebugAssert(!IsLocalBrowserMode());
  3837. if (Session == NULL)
  3838. {
  3839. Session = Terminal;
  3840. }
  3841. if (Session == Terminal)
  3842. {
  3843. if (RemoteDriveView->DropTarget != NULL)
  3844. {
  3845. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3846. }
  3847. else if (RemoteDirView->DropTarget != NULL)
  3848. {
  3849. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3850. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3851. }
  3852. else
  3853. {
  3854. Target = RemoteDirView->Path;
  3855. }
  3856. }
  3857. else
  3858. {
  3859. Target = GetSessionPath(Session, osRemote);
  3860. }
  3861. Target = UnixIncludeTrailingBackslash(Target);
  3862. if (FileList->Count == 1)
  3863. {
  3864. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3865. }
  3866. else
  3867. {
  3868. FileMask = AnyMask;
  3869. }
  3870. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  3871. bool Result = true;
  3872. if (!NoConfirmation)
  3873. {
  3874. bool Multi = (FileList->Count > 1);
  3875. if (Move)
  3876. {
  3877. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3878. }
  3879. else
  3880. {
  3881. std::unique_ptr<TStrings> Sessions(new TStringList);
  3882. std::unique_ptr<TStrings> Directories(new TStringList);
  3883. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  3884. for (int Index = 0; Index < SessionList->Count; Index++)
  3885. {
  3886. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  3887. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  3888. {
  3889. Sessions->AddObject(SessionList->Strings[Index], ASession);
  3890. Directories->Add(GetSessionPath(ASession, osRemote));
  3891. }
  3892. }
  3893. TDirectRemoteCopy AllowDirectCopy;
  3894. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  3895. {
  3896. DebugAssert(DirectCopy);
  3897. AllowDirectCopy = drcAllow;
  3898. }
  3899. else if (Terminal->IsCapable[fcSecondaryShell])
  3900. {
  3901. DebugAssert(DirectCopy);
  3902. AllowDirectCopy = drcConfirmCommandSession;
  3903. }
  3904. else
  3905. {
  3906. DebugAssert(!DirectCopy);
  3907. AllowDirectCopy = drcDisallow;
  3908. }
  3909. void * ASession = Session;
  3910. Result = DoRemoteCopyDialog(
  3911. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal);
  3912. Session = static_cast<TManagedTerminal *>(ASession);
  3913. }
  3914. }
  3915. return Result;
  3916. }
  3917. //---------------------------------------------------------------------------
  3918. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3919. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3920. {
  3921. DebugAssert(!IsLocalBrowserMode());
  3922. bool DirectCopy;
  3923. UnicodeString Target, FileMask;
  3924. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3925. if (Result)
  3926. {
  3927. if (!Move && !DirectCopy)
  3928. {
  3929. Configuration->Usage->Inc("RemoteCopyTemp");
  3930. UnicodeString RootTempDir;
  3931. UnicodeString TempDir;
  3932. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3933. TStrings * TemporaryFilesList = new TStringList();
  3934. try
  3935. {
  3936. TMakeLocalFileListParams MakeFileListParam;
  3937. MakeFileListParam.FileList = TemporaryFilesList;
  3938. MakeFileListParam.FileTimes = NULL;
  3939. MakeFileListParam.IncludeDirs = true;
  3940. MakeFileListParam.Recursive = false;
  3941. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  3942. TTerminalManager::Instance()->ActiveSession = Session;
  3943. if (TemporaryFilesList->Count > 0)
  3944. {
  3945. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3946. CopyParam.FileMask = FileMask;
  3947. DebugAssert(!FAutoOperation);
  3948. FAutoOperation = true;
  3949. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3950. }
  3951. }
  3952. __finally
  3953. {
  3954. delete TemporaryFilesList;
  3955. FAutoOperation = false;
  3956. if (!RootTempDir.IsEmpty())
  3957. {
  3958. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3959. }
  3960. }
  3961. }
  3962. else
  3963. {
  3964. RemoteDirView->SaveSelection();
  3965. RemoteDirView->SaveSelectedNames();
  3966. try
  3967. {
  3968. if (Move)
  3969. {
  3970. Configuration->Usage->Inc("RemoteMove");
  3971. Terminal->MoveFiles(FileList, Target, FileMask);
  3972. }
  3973. else
  3974. {
  3975. Configuration->Usage->Inc("RemoteCopyDirect");
  3976. DebugAssert(DirectCopy);
  3977. DebugAssert(Session == Terminal);
  3978. if (Terminal->IsCapable[fcRemoteCopy] ||
  3979. Terminal->CommandSessionOpened ||
  3980. CommandSessionFallback())
  3981. {
  3982. Terminal->CopyFiles(FileList, Target, FileMask);
  3983. }
  3984. }
  3985. }
  3986. catch(...)
  3987. {
  3988. RemoteDirView->DiscardSavedSelection();
  3989. throw;
  3990. }
  3991. RemoteDirView->RestoreSelection();
  3992. }
  3993. }
  3994. return Result;
  3995. }
  3996. //---------------------------------------------------------------------------
  3997. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3998. const UnicodeString & Path, TRemoteProperties & Properties)
  3999. {
  4000. DebugAssert(!IsLocalBrowserMode());
  4001. Properties.Valid = Properties.Valid << vpEncrypt;
  4002. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4003. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4004. }
  4005. //---------------------------------------------------------------------------
  4006. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4007. {
  4008. Side = GetSide(Side);
  4009. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4010. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4011. UnicodeString Name = LoadStr(NEW_FOLDER);
  4012. int AllowedChanges =
  4013. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4014. bool SaveSettings = false;
  4015. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4016. {
  4017. TWindowLock Lock(this);
  4018. if (!IsSideLocalBrowser(Side))
  4019. {
  4020. if (SaveSettings)
  4021. {
  4022. GUIConfiguration->NewDirectoryProperties = Properties;
  4023. }
  4024. CreateRemoteDirectory(Name, Properties);
  4025. }
  4026. else
  4027. {
  4028. DirView(Side)->CreateDirectory(Name);
  4029. }
  4030. }
  4031. }
  4032. //---------------------------------------------------------------------------
  4033. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4034. {
  4035. TWindowLock Lock(this);
  4036. DirView(Side)->ExecuteHomeDirectory();
  4037. }
  4038. //---------------------------------------------------------------------------
  4039. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4040. {
  4041. DoOpenDirectoryDialog(odBrowse, Side);
  4042. }
  4043. //---------------------------------------------------------------------------
  4044. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4045. {
  4046. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4047. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4048. bool Result = !Path.IsEmpty();
  4049. if (Result)
  4050. {
  4051. // While we might get here when the session is closed (from location profiles),
  4052. // it's not a problem as the Path setter is noop then
  4053. DirView(Side)->Path = Path;
  4054. }
  4055. return Result;
  4056. }
  4057. //---------------------------------------------------------------------------
  4058. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4059. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4060. bool & Close)
  4061. {
  4062. // terminal can be already closed (e.g. dropped connection)
  4063. if (Terminal != NULL)
  4064. {
  4065. try
  4066. {
  4067. TCalculateSizeParams Params;
  4068. Params.Stats = &Stats;
  4069. Terminal->CalculateFilesSize(FileList, Size, Params);
  4070. }
  4071. catch(...)
  4072. {
  4073. if (!Terminal->Active)
  4074. {
  4075. Close = true;
  4076. }
  4077. throw;
  4078. }
  4079. }
  4080. }
  4081. //---------------------------------------------------------------------------
  4082. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4083. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4084. bool & Close)
  4085. {
  4086. // terminal can be already closed (e.g. dropped connection)
  4087. if (Terminal != NULL)
  4088. {
  4089. Configuration->Usage->Inc(L"ChecksumCalculated");
  4090. try
  4091. {
  4092. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  4093. }
  4094. catch(...)
  4095. {
  4096. if (!Terminal->Active)
  4097. {
  4098. Close = true;
  4099. }
  4100. throw;
  4101. }
  4102. }
  4103. }
  4104. //---------------------------------------------------------------------------
  4105. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4106. {
  4107. bool Result;
  4108. if (!IsSideLocalBrowser(Side))
  4109. {
  4110. TRemoteTokenList * GroupList = NULL;
  4111. TRemoteTokenList * UserList = NULL;
  4112. try
  4113. {
  4114. TRemoteProperties CurrentProperties;
  4115. if (Terminal->LoadFilesProperties(FileList))
  4116. {
  4117. RemoteDirView->Invalidate();
  4118. }
  4119. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4120. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4121. if (CapableGroupChanging || CapableOwnerChanging)
  4122. {
  4123. if (CapableGroupChanging)
  4124. {
  4125. GroupList = Terminal->Groups->Duplicate();
  4126. }
  4127. if (CapableOwnerChanging)
  4128. {
  4129. UserList = Terminal->Users->Duplicate();
  4130. }
  4131. TRemoteDirectory * Files = Terminal->Files;
  4132. int Count = Files->Count;
  4133. if (Count > 100)
  4134. {
  4135. Count = 100;
  4136. }
  4137. for (int Index = 0; Index < Count; Index++)
  4138. {
  4139. TRemoteFile * File = Files->Files[Index];
  4140. if (CapableGroupChanging)
  4141. {
  4142. GroupList->AddUnique(File->Group);
  4143. }
  4144. if (CapableOwnerChanging)
  4145. {
  4146. UserList->AddUnique(File->Owner);
  4147. }
  4148. }
  4149. // if we haven't collected tokens for all files in current directory,
  4150. // make sure we collect them at least for all selected files.
  4151. // (note that so far the files in FileList has to be from current direcotry)
  4152. if (Count < Files->Count)
  4153. {
  4154. for (int Index = 0; Index < FileList->Count; Index++)
  4155. {
  4156. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4157. if (CapableGroupChanging)
  4158. {
  4159. GroupList->AddUnique(File->Group);
  4160. }
  4161. if (CapableOwnerChanging)
  4162. {
  4163. UserList->AddUnique(File->Owner);
  4164. }
  4165. }
  4166. }
  4167. }
  4168. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4169. int Flags = 0;
  4170. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4171. if (CapableOwnerChanging) Flags |= cpOwner;
  4172. if (CapableGroupChanging) Flags |= cpGroup;
  4173. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4174. if (Terminal->IsCapable[fcCalculatingChecksum])
  4175. {
  4176. CalculateChecksumEvent = CalculateChecksum;
  4177. }
  4178. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4179. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4180. TRemoteProperties NewProperties = CurrentProperties;
  4181. Result =
  4182. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4183. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4184. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4185. CalculateSize, CalculateChecksumEvent);
  4186. if (Result)
  4187. {
  4188. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4189. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4190. }
  4191. }
  4192. __finally
  4193. {
  4194. delete GroupList;
  4195. delete UserList;
  4196. }
  4197. }
  4198. else
  4199. {
  4200. DirView(Side)->DisplayPropertiesMenu();
  4201. Result = true;
  4202. }
  4203. return Result;
  4204. }
  4205. //---------------------------------------------------------------------------
  4206. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4207. {
  4208. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4209. {
  4210. FIgnoreNextDialogChar = Key;
  4211. }
  4212. Key = 0;
  4213. }
  4214. //---------------------------------------------------------------------------
  4215. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4216. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4217. {
  4218. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4219. if (Command != NULL)
  4220. {
  4221. KeyProcessed(Key, Shift);
  4222. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4223. {
  4224. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4225. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4226. false, false, const_cast<TCustomCommandType *>(Command));
  4227. }
  4228. }
  4229. }
  4230. //---------------------------------------------------------------------------
  4231. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4232. {
  4233. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4234. {
  4235. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4236. }
  4237. if (!DirView(osCurrent)->IsEditing())
  4238. {
  4239. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4240. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4241. {
  4242. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4243. // But so far it's not a problem, as it does what we want.
  4244. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4245. if (((Action->ShortCut == KeyShortCut) ||
  4246. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4247. AllowedAction(Action, aaShortCut))
  4248. {
  4249. // Has to be called before the action as the dialog char is already in queue.
  4250. // So when the action consumes message queue, we already need to have the
  4251. // FIgnoreNextDialogChar set
  4252. KeyProcessed(Key, Shift);
  4253. // Reset reference to previous component (when menu/toolbar was clicked).
  4254. // Needed to detect that action was invoked by keyboard shortcut
  4255. // in TNonVisualDataModule::ExplorerActionsExecute
  4256. Action->ActionComponent = NULL;
  4257. Action->Execute();
  4258. return;
  4259. }
  4260. }
  4261. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4262. {
  4263. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4264. {
  4265. KeyProcessed(Key, Shift);
  4266. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4267. return;
  4268. }
  4269. }
  4270. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4271. {
  4272. KeyProcessed(Key, Shift);
  4273. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4274. }
  4275. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4276. if (IsCustomShortCut(CustomShortCut))
  4277. {
  4278. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4279. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4280. if (WinConfiguration->SharedBookmarks != NULL)
  4281. {
  4282. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4283. if ((Bookmark != NULL) &&
  4284. OpenBookmark(FCurrentSide, Bookmark))
  4285. {
  4286. KeyProcessed(Key, Shift);
  4287. }
  4288. }
  4289. }
  4290. }
  4291. TForm::KeyDown(Key, Shift);
  4292. }
  4293. //---------------------------------------------------------------------------
  4294. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4295. {
  4296. bool OpenedTerminal = false;
  4297. UnicodeString ProtocolName;
  4298. UnicodeString ProtocolNameHint;
  4299. UnicodeString SecurityProtocolName;
  4300. UnicodeString DurationHint;
  4301. // See the comment in TScpCommanderForm::UpdateControls()
  4302. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4303. if (ActiveTerminal != NULL)
  4304. {
  4305. // Active is not enough here
  4306. if (ActiveTerminal->Status == ssOpened)
  4307. {
  4308. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4309. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4310. if (FileSystemInfo.ProtocolName.IsEmpty())
  4311. {
  4312. ProtocolName = SessionInfo.ProtocolName;
  4313. }
  4314. else
  4315. {
  4316. ProtocolName = FileSystemInfo.ProtocolName;
  4317. }
  4318. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4319. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4320. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4321. OpenedTerminal = true;
  4322. }
  4323. else if (ActiveTerminal->Status > ssClosed)
  4324. {
  4325. ProtocolName = LoadStr(STATUS_CONNECTING);
  4326. }
  4327. else
  4328. {
  4329. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4330. }
  4331. }
  4332. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4333. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4334. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4335. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4336. if (ProtocolStatusPanel->Caption != ProtocolName)
  4337. {
  4338. ProtocolStatusPanel->Caption = ProtocolName;
  4339. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4340. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4341. }
  4342. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4343. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4344. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4345. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4346. SecurityStatusPanel->Enabled = SecurityEnabled;
  4347. UnicodeString SecurityProtocolHint;
  4348. if (SecurityEnabled)
  4349. {
  4350. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4351. }
  4352. else
  4353. {
  4354. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4355. }
  4356. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4357. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4358. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4359. DurationStatusPanel->Hint = DurationHint;
  4360. UpdateStatusBar();
  4361. }
  4362. //---------------------------------------------------------------------------
  4363. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4364. {
  4365. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4366. DebugAssert(SessionStatusBar != NULL);
  4367. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4368. if (!FNote.IsEmpty())
  4369. {
  4370. NoteStatusPanel->Caption = FNote;
  4371. }
  4372. else
  4373. {
  4374. UpdateStatusPanelText(NoteStatusPanel);
  4375. }
  4376. NoteStatusPanel->Hint = FNoteHints;
  4377. UnicodeString LoginTime;
  4378. // See the comment in TScpCommanderForm::UpdateControls()
  4379. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4380. // Active is not enough here
  4381. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4382. {
  4383. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4384. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4385. }
  4386. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4387. DurationStatusPanel->Caption = LoginTime;
  4388. }
  4389. //---------------------------------------------------------------------------
  4390. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4391. {
  4392. Panel->Caption = L"";
  4393. }
  4394. //---------------------------------------------------------------------------
  4395. void __fastcall TCustomScpExplorerForm::Idle()
  4396. {
  4397. if (FShowing || FStandaloneEditing)
  4398. {
  4399. FEditorManager->Check();
  4400. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4401. // that needs to know if queue view is visible (and it may be closed after queue update)
  4402. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4403. }
  4404. if (FShowing)
  4405. {
  4406. if (!NonVisualDataModule->Busy &&
  4407. // Menu is opened or drag&drop is going on
  4408. (Mouse->Capture == NULL))
  4409. {
  4410. if (FRefreshRemoteDirectory)
  4411. {
  4412. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4413. {
  4414. Terminal->RefreshDirectory();
  4415. }
  4416. FRefreshRemoteDirectory = false;
  4417. }
  4418. if (FRefreshLocalDirectory)
  4419. {
  4420. ReloadLocalDirectory();
  4421. FRefreshLocalDirectory = false;
  4422. }
  4423. if (WinConfiguration->RefreshRemotePanel)
  4424. {
  4425. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4426. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4427. {
  4428. RemoteDirView->ReloadDirectory();
  4429. }
  4430. }
  4431. }
  4432. }
  4433. if (FShowing || FStandaloneEditing)
  4434. {
  4435. if (FQueueStatusInvalidated)
  4436. {
  4437. UpdateQueueStatus(false);
  4438. }
  4439. RefreshQueueItems();
  4440. }
  4441. if (FShowing)
  4442. {
  4443. UpdateStatusBar();
  4444. }
  4445. FIgnoreNextDialogChar = 0;
  4446. }
  4447. //---------------------------------------------------------------------------
  4448. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4449. {
  4450. try
  4451. {
  4452. FUserActionTimer->Enabled = false;
  4453. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4454. {
  4455. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4456. {
  4457. FPendingQueueActionItem->ProcessUserAction();
  4458. }
  4459. }
  4460. }
  4461. __finally
  4462. {
  4463. FPendingQueueActionItem = NULL;
  4464. }
  4465. }
  4466. //---------------------------------------------------------------------------
  4467. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4468. {
  4469. if (WinConfiguration->MinimizeToTray)
  4470. {
  4471. UpdateTrayIcon();
  4472. FTrayIcon->Visible = true;
  4473. if (Visible)
  4474. {
  4475. ShowWindow(Handle, SW_HIDE);
  4476. }
  4477. }
  4478. }
  4479. //---------------------------------------------------------------------------
  4480. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4481. {
  4482. // WORKAROUND
  4483. // When restoring maximized window from minimization,
  4484. // rarely some controls do not align properly.
  4485. // Two instances seen (both for Commander):
  4486. // - When restoring, window is temporarily narrower (not maximized),
  4487. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4488. // Once maximized already, top dock shinks back, but the session PageControl,
  4489. // do not align up, leaving space between TopDock and PageControl.
  4490. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4491. for (int Index = 0; Index < ControlCount; Index++)
  4492. {
  4493. RealignControl(Controls[Index]);
  4494. }
  4495. if (FTrayIcon->Visible)
  4496. {
  4497. FTrayIcon->Visible = false;
  4498. if (Visible)
  4499. {
  4500. ShowWindow(Handle, SW_SHOW);
  4501. }
  4502. }
  4503. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4504. {
  4505. FNeedSession = false;
  4506. NonVisualDataModule->StartBusy();
  4507. try
  4508. {
  4509. NeedSession(true);
  4510. }
  4511. __finally
  4512. {
  4513. NonVisualDataModule->EndBusy();
  4514. }
  4515. }
  4516. }
  4517. //---------------------------------------------------------------------------
  4518. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4519. {
  4520. FTrayIcon->Hint = GetMainForm()->Caption;
  4521. }
  4522. //---------------------------------------------------------------------------
  4523. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4524. {
  4525. UpdateTrayIcon();
  4526. }
  4527. //---------------------------------------------------------------------------
  4528. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4529. {
  4530. // workaround
  4531. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4532. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4533. // But WM_ACTIVATEAPP message can be received even when
  4534. // the main window is minimized to the tray and internal editor window is focused
  4535. // (after another application was previously active)
  4536. if (::IsIconic(Handle))
  4537. {
  4538. if (!IsAppIconic())
  4539. {
  4540. SetAppIconic(true);
  4541. }
  4542. }
  4543. ::ApplicationRestore();
  4544. Application->BringToFront();
  4545. }
  4546. //---------------------------------------------------------------------------
  4547. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4548. {
  4549. RestoreApp();
  4550. }
  4551. //---------------------------------------------------------------------------
  4552. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4553. {
  4554. if (OpenInNewWindow())
  4555. {
  4556. ExecuteNewInstance(SessionUrl);
  4557. }
  4558. else
  4559. {
  4560. TTerminalManager::Instance()->NewSession(SessionUrl);
  4561. }
  4562. }
  4563. //---------------------------------------------------------------------------
  4564. void TCustomScpExplorerForm::NewTab(TOperationSide)
  4565. {
  4566. NewSession();
  4567. }
  4568. //---------------------------------------------------------------------------
  4569. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4570. {
  4571. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4572. StoredSessions->NewSession(SessionName, SessionData);
  4573. // modified only, explicit
  4574. StoredSessions->Save(false, true);
  4575. // encode session name because of slashes in hierarchical sessions
  4576. return EncodeUrlString(SessionName);
  4577. }
  4578. //---------------------------------------------------------------------------
  4579. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4580. {
  4581. // current working directories become defaults here, what is not right
  4582. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4583. return SaveHiddenDuplicateSession(SessionData.get());
  4584. }
  4585. //---------------------------------------------------------------------------
  4586. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4587. {
  4588. if (OpenInNewWindow())
  4589. {
  4590. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4591. }
  4592. else
  4593. {
  4594. // current working directories become defaults here, what is not right
  4595. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4596. TTerminalManager * Manager = TTerminalManager::Instance();
  4597. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4598. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4599. // We definitelly want these
  4600. ASession->Disconnected = ManagedSession->Disconnected;
  4601. // Not sure about these two
  4602. ASession->Permanent = ManagedSession->Permanent;
  4603. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4604. Manager->ActiveSession = ASession;
  4605. }
  4606. }
  4607. //---------------------------------------------------------------------------
  4608. void __fastcall TCustomScpExplorerForm::RenameSession()
  4609. {
  4610. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4611. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME) &&
  4612. // When submitting the default name, do not fix it
  4613. (Name != ManagedSession->SessionData->SessionName))
  4614. {
  4615. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4616. TSessionData::ValidateName(Name);
  4617. // This is inconsistent with how color (for example) is handled.
  4618. ManagedSession->SessionData->Name = Name;
  4619. UpdateControls();
  4620. // Add/Remove distinguishing paths from sessions of the same name.
  4621. SessionListChanged();
  4622. }
  4623. }
  4624. //---------------------------------------------------------------------------
  4625. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4626. {
  4627. DebugAssert(Queue != NULL);
  4628. bool Result = Queue->IsEmpty;
  4629. if (!Result)
  4630. {
  4631. SetFocus();
  4632. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4633. }
  4634. return Result;
  4635. }
  4636. //---------------------------------------------------------------------------
  4637. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4638. {
  4639. return CanCloseQueue(FQueue);
  4640. }
  4641. //---------------------------------------------------------------------------
  4642. void __fastcall TCustomScpExplorerForm::CloseSession()
  4643. {
  4644. if (CanCloseQueue())
  4645. {
  4646. TTerminalManager::Instance()->FreeActiveTerminal();
  4647. }
  4648. }
  4649. //---------------------------------------------------------------------------
  4650. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4651. {
  4652. if (CanCloseQueue())
  4653. {
  4654. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4655. }
  4656. }
  4657. //---------------------------------------------------------------------------
  4658. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4659. {
  4660. DetachTerminal(Terminal);
  4661. RemoteDirView->Terminal = Terminal;
  4662. UpdateRemotePathComboBox(false);
  4663. InitStatusBar();
  4664. UpdateControls();
  4665. }
  4666. //---------------------------------------------------------------------------
  4667. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4668. {
  4669. InitStatusBar();
  4670. }
  4671. //---------------------------------------------------------------------------
  4672. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4673. {
  4674. if (DebugAlwaysTrue(!Terminal->Active))
  4675. {
  4676. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4677. }
  4678. }
  4679. //---------------------------------------------------------------------------
  4680. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4681. {
  4682. if (OpenInNewWindow())
  4683. {
  4684. // encode session name because of slashes in hierarchical sessions
  4685. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4686. }
  4687. else
  4688. {
  4689. TTerminalManager * Manager = TTerminalManager::Instance();
  4690. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4691. Manager->ActiveSession = Terminal;
  4692. }
  4693. }
  4694. //---------------------------------------------------------------------------
  4695. void __fastcall TCustomScpExplorerForm::DoOpenFolderOrWorkspace(const UnicodeString & Name, bool ConnectFirstTerminal)
  4696. {
  4697. TTerminalManager * Manager = TTerminalManager::Instance();
  4698. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4699. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4700. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  4701. // FirstSession can be null, if some of the
  4702. if (!ConnectFirstTerminal && (FirstSession != NULL))
  4703. {
  4704. FirstSession->Disconnected = true;
  4705. FirstSession->DisconnectedTemporarily = true;
  4706. }
  4707. Manager->ActiveSession = FirstSession;
  4708. }
  4709. //---------------------------------------------------------------------------
  4710. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4711. {
  4712. if (OpenInNewWindow())
  4713. {
  4714. ExecuteNewInstance(Name);
  4715. }
  4716. else
  4717. {
  4718. DoOpenFolderOrWorkspace(Name, true);
  4719. }
  4720. }
  4721. //---------------------------------------------------------------------------
  4722. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4723. bool &CanClose)
  4724. {
  4725. // See the comment in CloseApp()
  4726. if (NonVisualDataModule->Busy)
  4727. {
  4728. CanClose = false;
  4729. }
  4730. else
  4731. {
  4732. TTerminalManager * Manager = TTerminalManager::Instance();
  4733. int ActiveSessions = 0;
  4734. for (int Index = 0; Index < Manager->Count; Index++)
  4735. {
  4736. if (Manager->Sessions[Index]->Active)
  4737. {
  4738. ActiveSessions++;
  4739. }
  4740. }
  4741. // Confirm, when there's at least one active remote session or
  4742. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4743. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4744. WinConfiguration->ConfirmClosingSession)
  4745. {
  4746. unsigned int Result;
  4747. TMessageParams Params(mpNeverAskAgainCheck);
  4748. UnicodeString Message;
  4749. int Answers = qaOK | qaCancel;
  4750. // The current session is the only active session
  4751. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4752. {
  4753. if (!WinConfiguration->AutoSaveWorkspace)
  4754. {
  4755. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4756. }
  4757. else
  4758. {
  4759. Message = LoadStr(CLOSE_SESSION);
  4760. }
  4761. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4762. }
  4763. // Multiple active sessions or one active session, but it's not the current one
  4764. else if (ActiveSessions > 0)
  4765. {
  4766. if (!WinConfiguration->AutoSaveWorkspace)
  4767. {
  4768. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4769. }
  4770. else
  4771. {
  4772. Message = LoadStr(CLOSE_SESSIONS);
  4773. }
  4774. }
  4775. // Multiple tabs, but all are intactive or local, and saving of worspace is not enabled
  4776. else
  4777. {
  4778. DebugAlwaysTrue(Manager->Count > 1);
  4779. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  4780. Message = LoadStr(CLOSE_WORKSPACE);
  4781. }
  4782. UnicodeString Note;
  4783. if (WinConfiguration->AutoSaveWorkspace)
  4784. {
  4785. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4786. }
  4787. else
  4788. {
  4789. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4790. Answers = qaYes | qaNo | qaCancel;
  4791. }
  4792. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4793. SetFocus();
  4794. Result = MessageDialog(Message, qtConfirmation,
  4795. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4796. if (Result == qaNeverAskAgain)
  4797. {
  4798. WinConfiguration->ConfirmClosingSession = false;
  4799. }
  4800. if (Result == qaNo)
  4801. {
  4802. CanClose = SaveWorkspace(true);
  4803. // note that the workspace will be saved redundatly again from FormClose
  4804. }
  4805. else
  4806. {
  4807. CanClose =
  4808. (Result == qaOK) ||
  4809. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4810. (Result == qaNeverAskAgain);
  4811. }
  4812. }
  4813. if (CanClose && HasActiveTerminal())
  4814. {
  4815. CanClose = CanCloseQueue();
  4816. }
  4817. }
  4818. if (CanClose)
  4819. {
  4820. CanClose =
  4821. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4822. FEditorManager->CloseExternalFilesWithoutProcess();
  4823. if (CanClose)
  4824. {
  4825. while (CanClose && (FLocalEditors->Count > 0))
  4826. {
  4827. int PrevCount = FLocalEditors->Count;
  4828. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4829. CanClose = (FLocalEditors->Count < PrevCount);
  4830. }
  4831. if (CanClose)
  4832. {
  4833. CanClose = FEditorManager->Empty(true);
  4834. if (!CanClose)
  4835. {
  4836. SetFocus();
  4837. CanClose =
  4838. (MessageDialog(
  4839. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4840. }
  4841. }
  4842. }
  4843. }
  4844. }
  4845. //---------------------------------------------------------------------------
  4846. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4847. {
  4848. TForm * Form = dynamic_cast<TForm *>(Sender);
  4849. DebugAssert(Form != NULL);
  4850. Form->Close();
  4851. }
  4852. //---------------------------------------------------------------------------
  4853. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4854. {
  4855. TForm * Form = dynamic_cast<TForm *>(Sender);
  4856. delete Form;
  4857. }
  4858. //---------------------------------------------------------------------------
  4859. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4860. {
  4861. while (FLocalEditors->Count > 0)
  4862. {
  4863. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4864. }
  4865. }
  4866. //---------------------------------------------------------------------------
  4867. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4868. TObject *Sender)
  4869. {
  4870. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4871. try
  4872. {
  4873. SetProperties(osRemote, FileList);
  4874. }
  4875. __finally
  4876. {
  4877. delete FileList;
  4878. }
  4879. }
  4880. //---------------------------------------------------------------------------
  4881. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4882. {
  4883. if (value)
  4884. {
  4885. if (Component == fcCustomCommandsBand)
  4886. {
  4887. UpdateCustomCommandsToolbar();
  4888. }
  4889. if (Component == fcQueueFileList)
  4890. {
  4891. DebugAssert(!QueueFileListSplitter->Visible);
  4892. AdjustQueueLayout();
  4893. UpdateQueueFileList();
  4894. }
  4895. }
  4896. }
  4897. //---------------------------------------------------------------------------
  4898. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  4899. {
  4900. int MinHeightLimit =
  4901. GetStaticQueuePanelComponentsHeight() +
  4902. QueueFileListSplitter->Height +
  4903. QueueFileList->Height +
  4904. GetMinQueueViewHeight();
  4905. if (QueuePanel->ClientHeight < MinHeightLimit)
  4906. {
  4907. QueuePanel->ClientHeight = MinHeightLimit;
  4908. }
  4909. }
  4910. //---------------------------------------------------------------------------
  4911. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4912. {
  4913. TControl * Control = GetComponent(Component);
  4914. DebugAssert(Control);
  4915. bool Changed = (Control->Visible != value);
  4916. if (Changed)
  4917. {
  4918. ComponentShowing(Component, value);
  4919. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4920. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4921. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4922. if (value)
  4923. {
  4924. int RemainingHeight = Control->Parent->ClientHeight;
  4925. int RemainingWidth = Control->Parent->ClientWidth;
  4926. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4927. {
  4928. TControl * ChildControl = Control->Parent->Controls[i];
  4929. if (ChildControl->Visible)
  4930. {
  4931. switch (ChildControl->Align)
  4932. {
  4933. case alTop:
  4934. case alBottom:
  4935. RemainingHeight -= ChildControl->Height;
  4936. break;
  4937. case alLeft:
  4938. case alRight:
  4939. RemainingWidth -= ChildControl->Width;
  4940. break;
  4941. }
  4942. }
  4943. }
  4944. int Reserve = ScaleByTextHeight(this, 32);
  4945. // queue in explorer, trees in commander
  4946. if (Control->Height > RemainingHeight - Reserve)
  4947. {
  4948. Control->Height = RemainingHeight / 2;
  4949. }
  4950. if (Control->Width > RemainingWidth - Reserve)
  4951. {
  4952. Control->Width = RemainingWidth / 2;
  4953. }
  4954. }
  4955. Control->Visible = value;
  4956. if (WasFocused && Visible)
  4957. {
  4958. DirView(osCurrent)->SetFocus();
  4959. }
  4960. FixControlsPlacement();
  4961. }
  4962. }
  4963. //---------------------------------------------------------------------------
  4964. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4965. {
  4966. TControl * Control = GetComponent(Component);
  4967. if (Control == NULL)
  4968. {
  4969. return false;
  4970. }
  4971. else
  4972. {
  4973. return Control->Visible;
  4974. }
  4975. }
  4976. //---------------------------------------------------------------------------
  4977. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4978. {
  4979. bool Result;
  4980. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4981. {
  4982. Result = !IsUWP();
  4983. }
  4984. else
  4985. {
  4986. Result = true;
  4987. }
  4988. return Result;
  4989. }
  4990. //---------------------------------------------------------------------------
  4991. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  4992. {
  4993. if (DirView->ItemFocused != NULL)
  4994. {
  4995. DirView->ItemFocused->MakeVisible(false);
  4996. }
  4997. }
  4998. //---------------------------------------------------------------------------
  4999. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5000. {
  5001. MakeFocusedItemVisible(RemoteDirView);
  5002. QueueSplitter->Visible = QueuePanel->Visible;
  5003. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5004. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5005. TControl * QueueControlsOrder[] =
  5006. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5007. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5008. }
  5009. //---------------------------------------------------------------------------
  5010. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5011. {
  5012. switch (Component) {
  5013. case fcStatusBar: return RemoteStatusBar;
  5014. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5015. case fcQueueView: return QueuePanel;
  5016. case fcQueueToolbar: return QueueDock;
  5017. case fcRemoteTree: return RemoteDrivePanel;
  5018. case fcSessionsTabs: return SessionsPageControl;
  5019. case fcQueueFileList: return QueueFileList;
  5020. default: return NULL;
  5021. }
  5022. }
  5023. //---------------------------------------------------------------------------
  5024. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5025. TObject *Sender, TListColumn *Column, TPoint &Point)
  5026. {
  5027. DebugAssert(NonVisualDataModule && Column && Sender);
  5028. NonVisualDataModule->ListColumn = Column;
  5029. TPopupMenu * DirViewColumnMenu;
  5030. if (Sender == DirView(osOther))
  5031. {
  5032. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5033. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  5034. (Column->Index == uvName);
  5035. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  5036. (Column->Index == uvSize);
  5037. }
  5038. else
  5039. {
  5040. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5041. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  5042. (Column->Index == dvName);
  5043. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  5044. (Column->Index == dvSize);
  5045. }
  5046. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5047. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5048. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5049. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5050. }
  5051. //---------------------------------------------------------------------------
  5052. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5053. TObject *Sender, TListItem *Item, bool &AllowExec)
  5054. {
  5055. DoDirViewExecFile(Sender, Item, AllowExec);
  5056. }
  5057. //---------------------------------------------------------------------------
  5058. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5059. TListItem * Item, bool & AllowExec)
  5060. {
  5061. DebugAssert(Sender && Item && Configuration);
  5062. DebugAssert(AllowExec);
  5063. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5064. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5065. bool Remote = !IsSideLocalBrowser(Side);
  5066. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles();
  5067. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  5068. // on files only (not directories)
  5069. // and only when symlinks are resolved (apply to remote panel only)
  5070. if (!ADirView->ItemIsDirectory(Item) &&
  5071. (ResolvedSymlinks || FForceExecution))
  5072. {
  5073. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  5074. !FForceExecution && ResolvedSymlinks)
  5075. {
  5076. if (WinConfiguration->DoubleClickAction == dcaCopy)
  5077. {
  5078. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5079. UnlockWindow();
  5080. try
  5081. {
  5082. if (IsLocalBrowserMode())
  5083. {
  5084. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5085. }
  5086. else
  5087. {
  5088. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5089. }
  5090. }
  5091. __finally
  5092. {
  5093. LockWindow();
  5094. }
  5095. AllowExec = false;
  5096. }
  5097. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  5098. {
  5099. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5100. {
  5101. ExecuteFile(Side, efDefaultEditor);
  5102. AllowExec = false;
  5103. }
  5104. }
  5105. else
  5106. {
  5107. DebugFail();
  5108. }
  5109. }
  5110. // if we have not done anything special, fall back to default behavior
  5111. if (AllowExec)
  5112. {
  5113. if (Remote && !WinConfiguration->DisableOpenEdit)
  5114. {
  5115. ExecuteFile(osRemote, efShell);
  5116. AllowExec = false;
  5117. }
  5118. }
  5119. }
  5120. }
  5121. //---------------------------------------------------------------------------
  5122. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5123. {
  5124. return ((Side == osRemote) || (Side == osCurrent));
  5125. }
  5126. //---------------------------------------------------------------------------
  5127. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5128. {
  5129. DebugFail();
  5130. }
  5131. //---------------------------------------------------------------------------
  5132. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5133. {
  5134. DebugFail();
  5135. }
  5136. //---------------------------------------------------------------------------
  5137. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5138. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5139. {
  5140. TSynchronizeParamType Params;
  5141. Params.LocalDirectory = LocalDirectory;
  5142. Params.RemoteDirectory = RemoteDirectory;
  5143. int UnusedParams =
  5144. (GUIConfiguration->SynchronizeParams &
  5145. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5146. TTerminal::spCaseSensitive));
  5147. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5148. Params.Options = GUIConfiguration->SynchronizeOptions;
  5149. bool SaveSettings = false;
  5150. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5151. &DoSynchronizeTooManyDirectories);
  5152. DebugAssert(FSynchronizeController == NULL);
  5153. FSynchronizeController = &Controller;
  5154. bool Result;
  5155. try
  5156. {
  5157. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5158. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5159. int Options =
  5160. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5161. DebugAssert(FOnFeedSynchronizeError == NULL);
  5162. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5163. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5164. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  5165. if (Result)
  5166. {
  5167. if (SaveSettings)
  5168. {
  5169. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5170. GUIConfiguration->SynchronizeOptions = Params.Options;
  5171. }
  5172. else
  5173. {
  5174. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5175. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5176. FLAGSET(Params.Options, soSynchronize))
  5177. {
  5178. GUIConfiguration->SynchronizeOptions =
  5179. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5180. soSynchronize;
  5181. }
  5182. }
  5183. LocalDirectory = Params.LocalDirectory;
  5184. RemoteDirectory = Params.RemoteDirectory;
  5185. }
  5186. }
  5187. __finally
  5188. {
  5189. FSynchronizeController = NULL;
  5190. DebugAssert(FOnFeedSynchronizeError == NULL);
  5191. FOnFeedSynchronizeError = NULL;
  5192. }
  5193. return Result;
  5194. }
  5195. //---------------------------------------------------------------------------
  5196. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5197. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5198. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5199. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5200. TSynchronizeOptions * Options, bool Full)
  5201. {
  5202. // Keep-up-to-date mode
  5203. if (Terminal->Status == ssOpened)
  5204. {
  5205. try
  5206. {
  5207. int PParams = Params.Params;
  5208. if (!Full)
  5209. {
  5210. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5211. if (FLAGSET(Params.Options, soRecurse))
  5212. {
  5213. PParams |= TTerminal::spSubDirs;
  5214. }
  5215. }
  5216. else
  5217. {
  5218. // if keepuptodate is non-recursive,
  5219. // full sync before has to be non-recursive as well
  5220. if (FLAGCLEAR(Params.Options, soRecurse))
  5221. {
  5222. PParams |= TTerminal::spNoRecurse;
  5223. }
  5224. }
  5225. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5226. PParams, Checklist, Options);
  5227. }
  5228. catch (Exception & E)
  5229. {
  5230. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  5231. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5232. {
  5233. // noop, already logged in MoreMessageDialog
  5234. }
  5235. else
  5236. {
  5237. ShowExtendedExceptionEx(Terminal, &E);
  5238. throw;
  5239. }
  5240. }
  5241. }
  5242. }
  5243. //---------------------------------------------------------------------------
  5244. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5245. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5246. const UnicodeString ErrorStr)
  5247. {
  5248. if (!Directory.IsEmpty())
  5249. {
  5250. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5251. }
  5252. else
  5253. {
  5254. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5255. }
  5256. }
  5257. //---------------------------------------------------------------------------
  5258. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5259. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5260. {
  5261. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5262. {
  5263. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5264. }
  5265. else
  5266. {
  5267. TMessageParams Params(mpNeverAskAgainCheck);
  5268. unsigned int Result = MessageDialog(
  5269. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5270. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5271. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5272. {
  5273. MaxDirectories *= 2;
  5274. if (Result == qaNeverAskAgain)
  5275. {
  5276. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5277. }
  5278. }
  5279. else
  5280. {
  5281. Abort();
  5282. }
  5283. }
  5284. }
  5285. //---------------------------------------------------------------------------
  5286. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5287. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5288. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5289. TSynchronizeOptions * Options)
  5290. {
  5291. // Keep-up-to-date mode
  5292. DebugAssert(!FAutoOperation);
  5293. void * BatchStorage;
  5294. BatchStart(BatchStorage);
  5295. FAutoOperation = true;
  5296. bool AnyOperation = false;
  5297. TDateTime StartTime = Now();
  5298. TSynchronizeChecklist * AChecklist = NULL;
  5299. try
  5300. {
  5301. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5302. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5303. {
  5304. FSynchronizeProgressForm->Start();
  5305. }
  5306. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5307. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5308. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5309. Options);
  5310. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5311. SAFE_DESTROY(FSynchronizeProgressForm);
  5312. AnyOperation = (AChecklist->CheckedCount > 0);
  5313. if (AnyOperation)
  5314. {
  5315. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5316. CreateProgressForm(&SynchronizeProgress);
  5317. try
  5318. {
  5319. Terminal->SynchronizeApply(
  5320. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5321. SynchronizeProcessedItem, NULL, NULL, NULL);
  5322. }
  5323. __finally
  5324. {
  5325. DestroyProgressForm();
  5326. }
  5327. }
  5328. }
  5329. __finally
  5330. {
  5331. if (Checklist == NULL)
  5332. {
  5333. delete AChecklist;
  5334. }
  5335. else
  5336. {
  5337. *Checklist = AChecklist;
  5338. }
  5339. FAutoOperation = false;
  5340. SAFE_DESTROY(FSynchronizeProgressForm);
  5341. BatchEnd(BatchStorage);
  5342. ReloadLocalDirectory();
  5343. if (AnyOperation)
  5344. {
  5345. OperationComplete(StartTime);
  5346. }
  5347. }
  5348. }
  5349. //---------------------------------------------------------------------------
  5350. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5351. {
  5352. // can be called from command line
  5353. return Visible &&
  5354. ((DirView(osRemote)->SelCount > 0) ||
  5355. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5356. }
  5357. //---------------------------------------------------------------------------
  5358. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5359. {
  5360. LogSynchronizeEvent(Terminal, Message);
  5361. }
  5362. //---------------------------------------------------------------------------
  5363. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5364. int Params, TSynchronizeOptions & Options)
  5365. {
  5366. DebugAssert(!IsLocalBrowserMode());
  5367. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5368. {
  5369. Options.Filter = new TStringList();
  5370. Options.Filter->CaseSensitive = false;
  5371. Options.Filter->Duplicates = Types::dupAccept;
  5372. if (DirView(osRemote)->SelCount > 0)
  5373. {
  5374. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5375. }
  5376. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5377. {
  5378. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5379. }
  5380. Options.Filter->Sort();
  5381. }
  5382. }
  5383. //---------------------------------------------------------------------------
  5384. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5385. {
  5386. TCopyParamType Defaults;
  5387. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5388. ConfigStorage->AccessMode = smRead;
  5389. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5390. {
  5391. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5392. }
  5393. ConfigStorage.reset(NULL);
  5394. std::unique_ptr<TStringList> Options(new TStringList());
  5395. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5396. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5397. UnicodeString Result;
  5398. if (Options->Count > 0)
  5399. {
  5400. Result =
  5401. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5402. }
  5403. return Result;
  5404. }
  5405. //---------------------------------------------------------------------------
  5406. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5407. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5408. {
  5409. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5410. UnicodeString AdditionalParams =
  5411. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5412. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5413. SerializeCopyParamForCommandLine(CopyParams),
  5414. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5415. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5416. ExecuteNewInstance(SessionName, AdditionalParams);
  5417. }
  5418. //---------------------------------------------------------------------------
  5419. struct TSynchronizeParams
  5420. {
  5421. UnicodeString LocalDirectory;
  5422. UnicodeString RemoteDirectory;
  5423. TSynchronizeMode Mode;
  5424. TSynchronizeChecklist * Checklist;
  5425. int Params;
  5426. TCopyParamType * CopyParam;
  5427. TDateTime CollectElapsed;
  5428. TDateTime * StartTime;
  5429. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5430. };
  5431. //---------------------------------------------------------------------------
  5432. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5433. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5434. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5435. {
  5436. DebugAssert(!FAutoOperation);
  5437. void * BatchStorage;
  5438. BatchStart(BatchStorage);
  5439. FAutoOperation = true;
  5440. TFileOperationStatistics Statistics;
  5441. TDateTime Start = Now();
  5442. try
  5443. {
  5444. Params.OnProcessedItem = OnProcessedItem;
  5445. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5446. CreateProgressForm(&SynchronizeProgress);
  5447. Terminal->SynchronizeApply(
  5448. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5449. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5450. &Statistics);
  5451. }
  5452. __finally
  5453. {
  5454. Params.OnProcessedItem = NULL;
  5455. FAutoOperation = false;
  5456. DestroyProgressForm();
  5457. BatchEnd(BatchStorage);
  5458. ReloadLocalDirectory();
  5459. }
  5460. if (WinConfiguration->SynchronizeSummary)
  5461. {
  5462. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5463. // The statistics should be 0 anyway in this case
  5464. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5465. {
  5466. Message += L"\n";
  5467. if (Statistics.FilesUploaded > 0)
  5468. {
  5469. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5470. }
  5471. if (Statistics.FilesDownloaded > 0)
  5472. {
  5473. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5474. }
  5475. if (Statistics.FilesDeletedLocal > 0)
  5476. {
  5477. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5478. }
  5479. if (Statistics.FilesDeletedRemote > 0)
  5480. {
  5481. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5482. }
  5483. }
  5484. TDateTime Elapsed = (Now() - Start);
  5485. Message +=
  5486. L"\n" +
  5487. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5488. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5489. TMessageParams Params(mpNeverAskAgainCheck);
  5490. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5491. if (Result == qaNeverAskAgain)
  5492. {
  5493. WinConfiguration->SynchronizeSummary = false;
  5494. }
  5495. }
  5496. }
  5497. //---------------------------------------------------------------------------
  5498. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5499. {
  5500. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5501. {
  5502. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5503. }
  5504. // Not set in keep-up-to-date mode - Synchronize() method
  5505. if (Token != NULL)
  5506. {
  5507. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5508. if (Params.OnProcessedItem != NULL)
  5509. {
  5510. Params.OnProcessedItem(NULL, ChecklistItem);
  5511. }
  5512. }
  5513. }
  5514. //---------------------------------------------------------------------------
  5515. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5516. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5517. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5518. {
  5519. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5520. *Params.StartTime = Now();
  5521. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5522. }
  5523. //---------------------------------------------------------------------------
  5524. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5525. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5526. {
  5527. // terminal can be already closed (e.g. dropped connection)
  5528. if (Terminal != NULL)
  5529. {
  5530. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5531. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5532. }
  5533. }
  5534. //---------------------------------------------------------------------------
  5535. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5536. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5537. {
  5538. DebugAssert(!IsLocalBrowserMode());
  5539. TAutoBatch AutoBatch(this);
  5540. TAutoFlag AutoOperationFlag(FAutoOperation);
  5541. if (Side == osRemote)
  5542. {
  5543. std::unique_ptr<TStrings> FileList(new TStringList());
  5544. FileList->AddObject(FileName, RemoteFile);
  5545. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5546. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5547. RemoteDirView->SaveSelection();
  5548. RemoteDirView->SaveSelectedNames();
  5549. try
  5550. {
  5551. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5552. }
  5553. catch(...)
  5554. {
  5555. RemoteDirView->DiscardSavedSelection();
  5556. throw;
  5557. }
  5558. RemoteDirView->RestoreSelection();
  5559. }
  5560. else if (DebugAlwaysTrue(Side == osLocal))
  5561. {
  5562. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5563. {
  5564. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5565. }
  5566. UnicodeString Directory = ExtractFileDir(FileName);
  5567. ReloadLocalDirectory(Directory);
  5568. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5569. if (!SamePaths(Directory, NewDirectory))
  5570. {
  5571. ReloadLocalDirectory(NewDirectory);
  5572. }
  5573. }
  5574. }
  5575. //---------------------------------------------------------------------------
  5576. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5577. {
  5578. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5579. if (Side == osLocal)
  5580. {
  5581. if (Action == TSynchronizeChecklist::saDownloadNew)
  5582. {
  5583. OpenFolderInExplorer(LocalPath);
  5584. }
  5585. else
  5586. {
  5587. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5588. }
  5589. }
  5590. else if (DebugAlwaysTrue(Side == osRemote))
  5591. {
  5592. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5593. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5594. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5595. if (!LocalPath.IsEmpty())
  5596. {
  5597. SessionData->LocalDirectory = LocalPath;
  5598. }
  5599. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5600. ExecuteNewInstance(SessionName, FORMAT(L"%s=%s", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5601. }
  5602. }
  5603. //---------------------------------------------------------------------------
  5604. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5605. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5606. const TCopyParamType * CopyParams)
  5607. {
  5608. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5609. UnicodeString AdditionalParams =
  5610. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5611. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5612. SerializeCopyParamForCommandLine(CopyParams),
  5613. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5614. LocalDirectory, RemoteDirectory, Mode, Params));
  5615. ExecuteNewInstance(SessionName, AdditionalParams);
  5616. }
  5617. //---------------------------------------------------------------------------
  5618. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5619. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5620. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5621. {
  5622. int Result;
  5623. bool SaveSettings = false;
  5624. int Options =
  5625. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5626. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5627. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5628. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5629. bool Continue =
  5630. (UseDefaults == 0) ||
  5631. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5632. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5633. if (Continue)
  5634. {
  5635. Configuration->Usage->Inc(L"Synchronizations");
  5636. UpdateCopyParamCounters(CopyParam);
  5637. TSynchronizeOptions SynchronizeOptions;
  5638. GetSynchronizeOptions(Params, SynchronizeOptions);
  5639. if (SaveSettings)
  5640. {
  5641. GUIConfiguration->SynchronizeParams = Params;
  5642. }
  5643. else
  5644. {
  5645. SaveMode = false;
  5646. }
  5647. TDateTime StartTime = Now();
  5648. TSynchronizeChecklist * Checklist = NULL;
  5649. try
  5650. {
  5651. DebugAssert(!FAutoOperation);
  5652. FAutoOperation = true;
  5653. try
  5654. {
  5655. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5656. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5657. int Files = -1;
  5658. if (DataList->Count >= 1)
  5659. {
  5660. Files = StrToIntDef(DataList->Strings[0], Files);
  5661. }
  5662. else
  5663. {
  5664. DataList->Add(UnicodeString());
  5665. }
  5666. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5667. FSynchronizeProgressForm->Start();
  5668. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5669. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5670. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5671. &SynchronizeOptions);
  5672. if (Terminal->SessionData->CacheDirectories)
  5673. {
  5674. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5675. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5676. }
  5677. }
  5678. __finally
  5679. {
  5680. FAutoOperation = false;
  5681. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5682. SAFE_DESTROY(FSynchronizeProgressForm);
  5683. }
  5684. TSynchronizeParams SynchronizeParams;
  5685. SynchronizeParams.LocalDirectory = LocalDirectory;
  5686. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5687. SynchronizeParams.Mode = Mode;
  5688. SynchronizeParams.CopyParam = &CopyParam;
  5689. SynchronizeParams.Params = Params;
  5690. SynchronizeParams.Checklist = Checklist;
  5691. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5692. SynchronizeParams.StartTime = &StartTime;
  5693. SynchronizeParams.OnProcessedItem = NULL;
  5694. Result = Checklist->Count;
  5695. if (Checklist->Count > 0)
  5696. {
  5697. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5698. {
  5699. if (!DoSynchronizeChecklistDialog(
  5700. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5701. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5702. {
  5703. Result = -1;
  5704. }
  5705. }
  5706. else
  5707. {
  5708. FullSynchronize(SynchronizeParams, NULL, NULL);
  5709. }
  5710. }
  5711. else
  5712. {
  5713. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5714. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5715. }
  5716. }
  5717. __finally
  5718. {
  5719. delete Checklist;
  5720. }
  5721. OperationComplete(StartTime);
  5722. }
  5723. else
  5724. {
  5725. Result = -1;
  5726. }
  5727. return Result;
  5728. }
  5729. //---------------------------------------------------------------------------
  5730. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5731. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5732. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5733. {
  5734. if (Collect)
  5735. {
  5736. DebugAssert(FSynchronizeProgressForm != NULL);
  5737. if (!FSynchronizeProgressForm->Started)
  5738. {
  5739. FSynchronizeProgressForm->Start();
  5740. }
  5741. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5742. SetTaskbarListProgressValue(CompareProgress);
  5743. }
  5744. else
  5745. {
  5746. DebugAssert(FSynchronizeProgressForm == NULL);
  5747. DebugAssert(FProgressForm != NULL);
  5748. }
  5749. }
  5750. //---------------------------------------------------------------------------
  5751. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5752. {
  5753. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  5754. TRemoteFile * File = NULL;
  5755. Terminal->ReadFile(FullFileName, File);
  5756. if (File != NULL)
  5757. {
  5758. std::unique_ptr<TRemoteFile> FileOwner(File);
  5759. TAutoFlag Flag(FStandaloneEditing);
  5760. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5761. Application->ShowMainForm = false;
  5762. Application->Run();
  5763. }
  5764. }
  5765. //---------------------------------------------------------------------------
  5766. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  5767. {
  5768. DebugFail();
  5769. }
  5770. //---------------------------------------------------------------------------
  5771. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5772. {
  5773. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5774. SessionData->Assign(ManagedSession->SessionData);
  5775. UpdateSessionData(SessionData.get());
  5776. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  5777. if (IsActiveTerminal(ManagedSession))
  5778. {
  5779. ManagedSession->UpdateSessionCredentials(SessionData.get());
  5780. }
  5781. if (!ManagedSession->SessionData->HasSessionName())
  5782. {
  5783. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5784. // once IsWorkspace is cleared
  5785. SessionData->Name = UnicodeString();
  5786. }
  5787. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  5788. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  5789. SessionData->IsWorkspace = false;
  5790. return SessionData.release();
  5791. }
  5792. //---------------------------------------------------------------------------
  5793. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5794. {
  5795. TSessionData * SessionData = CloneCurrentSessionData();
  5796. try
  5797. {
  5798. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5799. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5800. }
  5801. __finally
  5802. {
  5803. delete SessionData;
  5804. }
  5805. }
  5806. //---------------------------------------------------------------------------
  5807. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5808. {
  5809. TTerminalManager * Manager = TTerminalManager::Instance();
  5810. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5811. if (DebugAlwaysTrue(ManagedSession != NULL))
  5812. {
  5813. // Update ManagedSession->StateData
  5814. UpdateSession(ManagedSession);
  5815. }
  5816. Manager->SaveWorkspace(DataList.get());
  5817. return DataList.release();
  5818. }
  5819. //---------------------------------------------------------------------------
  5820. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5821. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5822. {
  5823. WinConfiguration->LastWorkspace = Name;
  5824. if (!SavePasswords)
  5825. {
  5826. for (int Index = 0; Index < DataList->Count; Index++)
  5827. {
  5828. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5829. if (SessionData->Link.IsEmpty())
  5830. {
  5831. SessionData->ClearSessionPasswords();
  5832. }
  5833. }
  5834. }
  5835. StoredSessions->NewWorkspace(Name, DataList);
  5836. // modified only
  5837. StoredSessions->Save(false, Explicit);
  5838. }
  5839. //---------------------------------------------------------------------------
  5840. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5841. {
  5842. return
  5843. DefaultStr(WinConfiguration->LastWorkspace,
  5844. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5845. }
  5846. //---------------------------------------------------------------------------
  5847. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5848. {
  5849. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5850. bool AnyNonStoredSessionWithPassword = false;
  5851. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5852. bool AllNonStoredSessionsAnonymous = true;
  5853. TTerminalManager * Manager = TTerminalManager::Instance();
  5854. for (int Index = 0; Index < DataList->Count; Index++)
  5855. {
  5856. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5857. if (SessionData->Link.IsEmpty())
  5858. {
  5859. if (SessionData->HasAnySessionPassword())
  5860. {
  5861. AnyNonStoredSessionWithPassword = true;
  5862. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  5863. {
  5864. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5865. }
  5866. }
  5867. if (!SameText(SessionData->UserName, AnonymousUserName))
  5868. {
  5869. AllNonStoredSessionsAnonymous = false;
  5870. }
  5871. }
  5872. }
  5873. bool SavePasswords;
  5874. bool * PSavePasswords;
  5875. bool NotRecommendedSavingPasswords =
  5876. !CustomWinConfiguration->UseMasterPassword &&
  5877. !AllNonStoredSessionsAnonymous;
  5878. if (Configuration->DisablePasswordStoring ||
  5879. !AnyNonStoredSessionWithPassword)
  5880. {
  5881. PSavePasswords = NULL;
  5882. SavePasswords = false;
  5883. }
  5884. else
  5885. {
  5886. PSavePasswords = &SavePasswords;
  5887. SavePasswords =
  5888. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5889. !NotRecommendedSavingPasswords;
  5890. }
  5891. UnicodeString Name = WorkspaceName();
  5892. bool CreateShortcut = false;
  5893. bool Result =
  5894. DoSaveWorkspaceDialog(
  5895. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5896. EnableAutoSave);
  5897. if (Result)
  5898. {
  5899. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5900. if (CreateShortcut)
  5901. {
  5902. TOperationVisualizer Visualizer;
  5903. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5904. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5905. }
  5906. if (EnableAutoSave)
  5907. {
  5908. WinConfiguration->AutoSaveWorkspace = true;
  5909. WinConfiguration->AutoWorkspace = Name;
  5910. if (PSavePasswords != NULL)
  5911. {
  5912. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5913. }
  5914. }
  5915. }
  5916. return Result;
  5917. }
  5918. //---------------------------------------------------------------------------
  5919. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  5920. {
  5921. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  5922. DebugAssert(Session == ManagedSession);
  5923. if (!IsLocalBrowserMode())
  5924. {
  5925. SAFE_DESTROY(Session->RemoteExplorerState);
  5926. if (WinConfiguration->PreservePanelState)
  5927. {
  5928. Session->RemoteExplorerState = RemoteDirView->SaveState();
  5929. }
  5930. }
  5931. UpdateSessionData(Session->StateData);
  5932. }
  5933. //---------------------------------------------------------------------------
  5934. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5935. {
  5936. // Keep in sync with TSessionData::CopyStateData
  5937. DebugAssert(Data != NULL);
  5938. // This is inconsistent with how color (for example) is handled.
  5939. Data->Name = ManagedSession->SessionData->Name;
  5940. if (!IsLocalBrowserMode())
  5941. {
  5942. // cannot use RemoteDirView->Path, because it is empty if connection
  5943. // was already closed
  5944. // also only peek, we may not be connected at all atm
  5945. // so make sure we do not try retrieving current directory from the server
  5946. // (particularly with FTP)
  5947. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5948. if (!ACurrentDirectory.IsEmpty())
  5949. {
  5950. Data->RemoteDirectory = ACurrentDirectory;
  5951. }
  5952. }
  5953. Data->Color = SessionColor;
  5954. }
  5955. //---------------------------------------------------------------------------
  5956. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5957. {
  5958. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5959. DebugAssert(Toolbar != NULL);
  5960. for (int i = 0; i < Toolbar->Items->Count; i++)
  5961. {
  5962. TTBXCustomDropDownItem * DropDownItem;
  5963. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5964. if (DropDownItem != NULL)
  5965. {
  5966. ToolbarItemResize(DropDownItem,
  5967. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5968. Toolbar->NonClientWidth);
  5969. break;
  5970. }
  5971. }
  5972. }
  5973. //---------------------------------------------------------------------------
  5974. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5975. {
  5976. Item->EditWidth = Width;
  5977. }
  5978. //---------------------------------------------------------------------------
  5979. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5980. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5981. {
  5982. for (int i = 0; i < Toolbar->Items->Count; i++)
  5983. {
  5984. TTBXCustomDropDownItem * DropDownItem;
  5985. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5986. if (DropDownItem != NULL)
  5987. {
  5988. ASize.x -= DropDownItem->EditWidth;
  5989. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5990. }
  5991. }
  5992. }
  5993. //---------------------------------------------------------------------------
  5994. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5995. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5996. {
  5997. if (WinConfiguration->DDWarnLackOfTempSpace)
  5998. {
  5999. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6000. if (!ADrive.IsEmpty())
  6001. {
  6002. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6003. DebugAssert(RequiredSpace >= 0);
  6004. __int64 RequiredWithReserve;
  6005. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6006. if (FreeSpace < RequiredWithReserve)
  6007. {
  6008. unsigned int Result;
  6009. TMessageParams Params(mpNeverAskAgainCheck);
  6010. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6011. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6012. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6013. if (Result == qaNeverAskAgain)
  6014. {
  6015. WinConfiguration->DDWarnLackOfTempSpace = false;
  6016. }
  6017. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6018. }
  6019. }
  6020. }
  6021. }
  6022. //---------------------------------------------------------------------------
  6023. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6024. {
  6025. DoOpenDirectoryDialog(odAddBookmark, Side);
  6026. }
  6027. //---------------------------------------------------------------------------
  6028. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6029. {
  6030. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6031. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6032. try
  6033. {
  6034. TCustomDirView * DView = DirView(Side);
  6035. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6036. {
  6037. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6038. }
  6039. }
  6040. catch (...)
  6041. {
  6042. delete VisitedDirectories;
  6043. throw;
  6044. }
  6045. return VisitedDirectories;
  6046. }
  6047. //---------------------------------------------------------------------------
  6048. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6049. TOpenDirectoryMode Mode, TOperationSide Side)
  6050. {
  6051. bool Continue = true;
  6052. if (Mode == odAddBookmark)
  6053. {
  6054. TMessageParams Params(mpNeverAskAgainCheck);
  6055. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6056. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6057. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6058. unsigned int Answer =
  6059. MessageDialog(Message,
  6060. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6061. if (Answer == qaNeverAskAgain)
  6062. {
  6063. Continue = true;
  6064. WinConfiguration->UseSharedBookmarks = true;
  6065. }
  6066. else if (Answer == qaOK)
  6067. {
  6068. Continue = true;
  6069. WinConfiguration->UseSharedBookmarks = false;
  6070. }
  6071. else
  6072. {
  6073. Continue = false;
  6074. }
  6075. }
  6076. if (Continue)
  6077. {
  6078. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6079. try
  6080. {
  6081. UnicodeString Name = DirView(Side)->PathName;
  6082. bool Repeat;
  6083. do
  6084. {
  6085. Repeat = false;
  6086. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6087. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6088. // do not allow switching to location profiles,
  6089. // if we are not connected
  6090. HasDirView[osLocal] && (Terminal != NULL)))
  6091. {
  6092. TWindowLock Lock(this);
  6093. if (!TryOpenDirectory(Side, Name))
  6094. {
  6095. Repeat = true;
  6096. Mode = odBrowse;
  6097. }
  6098. }
  6099. }
  6100. while (Repeat);
  6101. }
  6102. __finally
  6103. {
  6104. delete VisitedDirectories;
  6105. }
  6106. }
  6107. }
  6108. //---------------------------------------------------------------------------
  6109. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6110. {
  6111. DebugAssert(!Terminal->CommandSessionOpened);
  6112. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6113. }
  6114. //---------------------------------------------------------------------------
  6115. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6116. {
  6117. bool Result = Terminal->IsCapable[Capability] ||
  6118. Terminal->CommandSessionOpened;
  6119. if (!Result)
  6120. {
  6121. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6122. if (!GUIConfiguration->ConfirmCommandSession)
  6123. {
  6124. Result = true;
  6125. }
  6126. else
  6127. {
  6128. TMessageParams Params(mpNeverAskAgainCheck);
  6129. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6130. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6131. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6132. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6133. if (Answer == qaNeverAskAgain)
  6134. {
  6135. GUIConfiguration->ConfirmCommandSession = false;
  6136. Result = true;
  6137. }
  6138. else if (Answer == qaOK)
  6139. {
  6140. Result = true;
  6141. }
  6142. }
  6143. if (Result)
  6144. {
  6145. Result = CommandSessionFallback();
  6146. }
  6147. }
  6148. return Result;
  6149. }
  6150. //---------------------------------------------------------------------------
  6151. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6152. {
  6153. if (EnsureCommandSessionFallback(fcAnyCommand))
  6154. {
  6155. DoConsoleDialog(Terminal, Command);
  6156. }
  6157. }
  6158. //---------------------------------------------------------------------------
  6159. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6160. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6161. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6162. {
  6163. if (!IsSideLocalBrowser(osRemote) &&
  6164. IsFileControl(DropSourceControl, osRemote) &&
  6165. (FDDExtMapFile != NULL))
  6166. {
  6167. Accept = true;
  6168. }
  6169. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6170. DebugAssert(FDDTargetControl != NULL);
  6171. }
  6172. //---------------------------------------------------------------------------
  6173. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6174. _di_IDataObject DataObj, int KeyState,
  6175. const TPoint & Point, int & Effect, bool & Accept)
  6176. {
  6177. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6178. }
  6179. //---------------------------------------------------------------------------
  6180. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6181. _di_IDataObject DataObj, int KeyState,
  6182. const TPoint & Point, int & Effect, bool & Accept)
  6183. {
  6184. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6185. }
  6186. //---------------------------------------------------------------------------
  6187. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6188. TObject * Sender)
  6189. {
  6190. DebugUsedParam(Sender);
  6191. DebugAssert(FDDTargetControl == Sender);
  6192. FDDTargetControl = NULL;
  6193. }
  6194. //---------------------------------------------------------------------------
  6195. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6196. {
  6197. FileControlDDDragLeave(SessionsPageControl);
  6198. }
  6199. //---------------------------------------------------------------------------
  6200. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6201. {
  6202. FileControlDDDragLeave(QueueView3);
  6203. }
  6204. //---------------------------------------------------------------------------
  6205. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6206. {
  6207. DebugAssert(!IsSideLocalBrowser(Side));
  6208. DebugUsedParam(Side);
  6209. bool Edit = false;
  6210. TRemoteFile * File = NULL;
  6211. UnicodeString FileName;
  6212. UnicodeString PointTo;
  6213. bool SymbolicLink = true;
  6214. if (RemoteDirView->ItemFocused)
  6215. {
  6216. DebugAssert(RemoteDirView->ItemFocused->Data);
  6217. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6218. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6219. if (Edit)
  6220. {
  6221. FileName = File->FileName;
  6222. PointTo = File->LinkTo;
  6223. }
  6224. else
  6225. {
  6226. PointTo = File->FileName;
  6227. }
  6228. }
  6229. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6230. Terminal->IsCapable[fcHardLink]))
  6231. {
  6232. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6233. if (Edit)
  6234. {
  6235. DebugAssert(File->FileName == FileName);
  6236. int Params = dfNoRecursive;
  6237. Terminal->ExceptionOnFail = true;
  6238. try
  6239. {
  6240. Terminal->DeleteFile(L"", File, &Params);
  6241. }
  6242. __finally
  6243. {
  6244. Terminal->ExceptionOnFail = false;
  6245. }
  6246. }
  6247. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6248. }
  6249. }
  6250. //---------------------------------------------------------------------------
  6251. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6252. {
  6253. return
  6254. (IsSideLocalBrowser(Side) ||
  6255. (HasActiveTerminal() &&
  6256. Terminal->ResolvingSymlinks &&
  6257. Terminal->IsCapable[fcSymbolicLink]));
  6258. }
  6259. //---------------------------------------------------------------------------
  6260. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6261. {
  6262. return
  6263. !IsSideLocalBrowser(FCurrentSide) &&
  6264. (RemoteDirView->ItemFocused != NULL) &&
  6265. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6266. Terminal->SessionData->ResolveSymlinks;
  6267. }
  6268. //---------------------------------------------------------------------------
  6269. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6270. {
  6271. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6272. FForceExecution = true;
  6273. try
  6274. {
  6275. DirView(osCurrent)->ExecuteCurrentFile();
  6276. }
  6277. __finally
  6278. {
  6279. FForceExecution = false;
  6280. }
  6281. }
  6282. //---------------------------------------------------------------------------
  6283. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6284. {
  6285. TEditorData ExternalEditor;
  6286. ExternalEditor.Editor = edExternal;
  6287. bool Remember = false;
  6288. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6289. {
  6290. if (Remember)
  6291. {
  6292. TEditorList * EditorList = new TEditorList;
  6293. try
  6294. {
  6295. *EditorList = *WinConfiguration->EditorList;
  6296. bool Found = false;
  6297. int i = 0;
  6298. while (!Found && (i < EditorList->Count))
  6299. {
  6300. const TEditorPreferences * Editor = EditorList->Editors[i];
  6301. if ((Editor->Data->Editor == edExternal) &&
  6302. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6303. {
  6304. Found = true;
  6305. }
  6306. i++;
  6307. }
  6308. if (!Found)
  6309. {
  6310. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6311. WinConfiguration->EditorList = EditorList;
  6312. }
  6313. }
  6314. __finally
  6315. {
  6316. delete EditorList;
  6317. }
  6318. }
  6319. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6320. }
  6321. }
  6322. //---------------------------------------------------------------------------
  6323. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6324. {
  6325. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6326. if (FFileFindTerminal == ATerminal)
  6327. {
  6328. FFileFindTerminal = NULL;
  6329. HideFileFindDialog();
  6330. }
  6331. if (FClipboardTerminal == ATerminal)
  6332. {
  6333. ClipboardClear(); // implies ClipboardStop
  6334. }
  6335. }
  6336. //---------------------------------------------------------------------------
  6337. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6338. {
  6339. DetachTerminal(Sender);
  6340. }
  6341. //---------------------------------------------------------------------------
  6342. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6343. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6344. {
  6345. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6346. DebugAssert(Terminal != NULL);
  6347. if (Data->Terminal == Terminal)
  6348. {
  6349. Data->Terminal = NULL;
  6350. }
  6351. }
  6352. //---------------------------------------------------------------------------
  6353. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6354. {
  6355. return NULL;
  6356. }
  6357. //---------------------------------------------------------------------------
  6358. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6359. {
  6360. UpdateControls();
  6361. SessionColor = TColor(0);
  6362. UpdateRemotePathComboBox(false);
  6363. NeedSession(false);
  6364. }
  6365. //---------------------------------------------------------------------------
  6366. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6367. {
  6368. try
  6369. {
  6370. // Cache, as the login dialog can change its value.
  6371. // See also DoShow. Also see TTerminalManager::NewSession.
  6372. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6373. try
  6374. {
  6375. if (ShowLogin)
  6376. {
  6377. bool ReloadSessions = !Startup;
  6378. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6379. }
  6380. }
  6381. __finally
  6382. {
  6383. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6384. // (so there was no chance for the user to open any session yet)
  6385. if ((ShowLogin || !Startup) &&
  6386. !WinConfiguration->KeepOpenWhenNoSession &&
  6387. // this rules out the implicit Commander's local-local browser
  6388. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6389. {
  6390. TerminateApplication();
  6391. }
  6392. }
  6393. }
  6394. catch (EAbort &)
  6395. {
  6396. // swallow
  6397. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6398. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6399. }
  6400. }
  6401. //---------------------------------------------------------------------------
  6402. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6403. {
  6404. TTerminalManager * Manager = TTerminalManager::Instance();
  6405. DebugAssert(!Manager->Updating);
  6406. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6407. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6408. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6409. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6410. try
  6411. {
  6412. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6413. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6414. {
  6415. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6416. }
  6417. for (int Index = 0; Index <= Manager->Count; Index++)
  6418. {
  6419. TThemeTabSheet * TabSheet;
  6420. if (Index >= SessionsPageControl->PageCount)
  6421. {
  6422. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6423. TabSheet->PageControl = SessionsPageControl;
  6424. }
  6425. else
  6426. {
  6427. TabSheet = SessionsPageControl->Pages[Index];
  6428. }
  6429. bool IsSessionTab = (Index < Manager->Count);
  6430. if (IsSessionTab)
  6431. {
  6432. TTerminal * Terminal = Manager->Sessions[Index];
  6433. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6434. TabSheet->CaptionTruncation = tttEllipsis;
  6435. UpdateSessionTab(TabSheet);
  6436. }
  6437. else
  6438. {
  6439. TabSheet->Tag = 0; // not really needed
  6440. TabSheet->Shadowed = false;
  6441. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6442. TabSheet->CaptionTruncation = tttNoText;
  6443. // 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)
  6444. UpdateNewTabTab();
  6445. }
  6446. }
  6447. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6448. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6449. {
  6450. SessionsPageControl->UpdateTabsCaptionTruncation();
  6451. }
  6452. }
  6453. __finally
  6454. {
  6455. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6456. }
  6457. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6458. }
  6459. //---------------------------------------------------------------------------
  6460. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6461. {
  6462. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6463. }
  6464. //---------------------------------------------------------------------------
  6465. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6466. {
  6467. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6468. UnicodeString TabCaption;
  6469. if (WinConfiguration->SelectiveToolbarText)
  6470. {
  6471. TabCaption = GetNewTabTabCaption();
  6472. }
  6473. TabSheet->BaseCaption = TabCaption;
  6474. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6475. }
  6476. //---------------------------------------------------------------------------
  6477. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6478. {
  6479. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6480. }
  6481. //---------------------------------------------------------------------------
  6482. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6483. {
  6484. DebugFail();
  6485. return UnicodeString();
  6486. }
  6487. //---------------------------------------------------------------------------
  6488. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6489. {
  6490. if (DebugAlwaysTrue(TabSheet != NULL))
  6491. {
  6492. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6493. if (DebugAlwaysTrue(ASession != NULL))
  6494. {
  6495. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6496. if (ASession->LocalBrowser)
  6497. {
  6498. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6499. }
  6500. else
  6501. {
  6502. TabSheet->ImageIndex = AddSessionColor(Color);
  6503. }
  6504. TTerminalManager * Manager = TTerminalManager::Instance();
  6505. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6506. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6507. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6508. }
  6509. }
  6510. }
  6511. //---------------------------------------------------------------------------
  6512. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6513. {
  6514. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6515. }
  6516. //---------------------------------------------------------------------------
  6517. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6518. {
  6519. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6520. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6521. bool Result = (Session != NULL);
  6522. if (Result)
  6523. {
  6524. TTerminalManager::Instance()->ActiveSession = Session;
  6525. }
  6526. else
  6527. {
  6528. try
  6529. {
  6530. NewTab();
  6531. }
  6532. __finally
  6533. {
  6534. SessionListChanged();
  6535. }
  6536. FSessionsPageControlNewTabTime = Now();
  6537. }
  6538. return Result;
  6539. }
  6540. //---------------------------------------------------------------------------
  6541. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6542. {
  6543. SessionTabSwitched();
  6544. }
  6545. //---------------------------------------------------------------------------
  6546. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6547. {
  6548. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6549. DebugAssert(TransferList != NULL);
  6550. UnicodeString Name;
  6551. if (TransferList->ItemIndex <= 0)
  6552. {
  6553. Name = L"";
  6554. }
  6555. else
  6556. {
  6557. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6558. }
  6559. if (FCopyParamAutoSelected.IsEmpty())
  6560. {
  6561. // if previous preset was not autoselected, make new preset the "default"
  6562. FCopyParamDefault = Name;
  6563. }
  6564. GUIConfiguration->CopyParamCurrent = Name;
  6565. }
  6566. //---------------------------------------------------------------------------
  6567. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6568. {
  6569. // sanity check
  6570. bool ExistingPreset =
  6571. (FTransferListHoverIndex >= 0) &&
  6572. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6573. DebugAssert(ExistingPreset);
  6574. if (ExistingPreset)
  6575. {
  6576. HDC DC = GetDC(0);
  6577. TCanvas * Canvas = new TCanvas();
  6578. try
  6579. {
  6580. Canvas->Handle = DC;
  6581. Canvas->Font = GetToolbarFont(this);
  6582. UnicodeString Name;
  6583. if (FTransferListHoverIndex == 0)
  6584. {
  6585. Name = L"";
  6586. }
  6587. else
  6588. {
  6589. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6590. }
  6591. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6592. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6593. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6594. static_cast<TObject*>(GetComponent(fcTransferList)));
  6595. UnicodeString InfoStr =
  6596. GUIConfiguration->CopyParamPreset[Name].
  6597. GetInfoStr(L"; ",
  6598. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6599. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6600. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6601. {
  6602. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6603. {
  6604. InfoStr.SetLength(InfoStr.Length() - 1);
  6605. }
  6606. InfoStr += Ellipsis;
  6607. }
  6608. // UpdateCaption does not cause invalidation of whole submenu, while
  6609. // setting Caption property does.
  6610. // also it probably does not resize the label, even if necessary
  6611. // (we do not want that anyway)
  6612. TransferLabel->UpdateCaption(InfoStr);
  6613. }
  6614. __finally
  6615. {
  6616. Canvas->Handle = NULL;
  6617. ReleaseDC(0, DC);
  6618. delete Canvas;
  6619. }
  6620. }
  6621. }
  6622. //---------------------------------------------------------------------------
  6623. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6624. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6625. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6626. {
  6627. if (FTransferListHoverIndex != AHoverIndex)
  6628. {
  6629. FTransferListHoverIndex = AHoverIndex;
  6630. UpdateTransferLabel();
  6631. }
  6632. }
  6633. //---------------------------------------------------------------------------
  6634. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6635. {
  6636. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6637. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6638. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6639. {
  6640. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6641. {
  6642. if (!NonVisualDataModule->Busy)
  6643. {
  6644. OpenDirectory(GetSide(osCurrent));
  6645. }
  6646. Message.Result = 1;
  6647. }
  6648. else
  6649. {
  6650. TForm::Dispatch(&Message);
  6651. }
  6652. }
  6653. else
  6654. {
  6655. TForm::Dispatch(&Message);
  6656. }
  6657. }
  6658. //---------------------------------------------------------------------------
  6659. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6660. {
  6661. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6662. if ((FIgnoreNextDialogChar != 0) &&
  6663. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6664. {
  6665. Message.Result = 1;
  6666. }
  6667. else
  6668. {
  6669. TForm::Dispatch(&Message);
  6670. }
  6671. FIgnoreNextDialogChar = 0;
  6672. }
  6673. //---------------------------------------------------------------------------
  6674. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6675. {
  6676. // The four low-order bits are used internally by Windows
  6677. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6678. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6679. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6680. // 61730 - restore thru double click - undocumented
  6681. // 61490 - maximize thru double click - undocumented
  6682. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6683. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6684. {
  6685. SysResizing(Cmd);
  6686. }
  6687. TForm::Dispatch(&Message);
  6688. }
  6689. //---------------------------------------------------------------------------
  6690. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6691. {
  6692. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6693. // Also there no point returning TRUE as we are not able to
  6694. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6695. // Hence the process termination might be safer :)
  6696. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6697. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6698. {
  6699. Message.Result = FALSE;
  6700. }
  6701. else
  6702. {
  6703. Message.Result = TRUE;
  6704. }
  6705. // Do not call default handling as that triggers OnCloseQuery,
  6706. // where our implementation will popup confirmation dialogs, what we do not want,
  6707. // as per Vista guidelines:
  6708. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6709. }
  6710. //---------------------------------------------------------------------------
  6711. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6712. {
  6713. if (Message.EndSession && IsApplicationMinimized())
  6714. {
  6715. // WORKAROUND
  6716. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6717. // when it receives WM_ENDSESSION,
  6718. // but that sometimes (particularly when application is minimized) cause crashes.
  6719. // Crash popups message that blocks log off.
  6720. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6721. // so we call ExitProcess() immediately, not even trying to cleanup.
  6722. // It still causes beep, so there's likely some popup, but it does not block
  6723. // log off.
  6724. ExitProcess(0);
  6725. }
  6726. TForm::Dispatch(&Message);
  6727. }
  6728. #ifdef _DEBUG
  6729. //---------------------------------------------------------------------------
  6730. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6731. {
  6732. TForm::Dispatch(&Message);
  6733. }
  6734. #endif
  6735. //---------------------------------------------------------------------------
  6736. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6737. {
  6738. }
  6739. //---------------------------------------------------------------------------
  6740. void __fastcall TCustomScpExplorerForm::DoShow()
  6741. {
  6742. // Called from TCustomScpExplorerForm::CMShowingChanged
  6743. // only now are the controls resized finally, so the size constraints
  6744. // will not conflict with possibly very small window size
  6745. RestoreFormParams();
  6746. // This indicates that the form was rescaled after parameters were loaded and
  6747. // as most probably the parameters were actually saved with the current form scaling
  6748. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6749. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6750. // and no rounding problems will occur.
  6751. // See also RestoreForm.
  6752. // (we should be safe to call RestoreParams unconditionally)
  6753. if (PixelsPerInch != Screen->PixelsPerInch)
  6754. {
  6755. RestoreParams();
  6756. }
  6757. FixControlsPlacement();
  6758. if (Position == poDefaultPosOnly)
  6759. {
  6760. CutFormToDesktop(this);
  6761. }
  6762. TForm::DoShow();
  6763. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6764. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6765. // This was previously in NeedSession, where other related code is.
  6766. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  6767. // unpleasanly shows briefly.
  6768. if ((ManagedSession == NULL) &&
  6769. !WinConfiguration->ShowLoginWhenNoSession &&
  6770. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6771. // This detects if workspace was saved the last time the main widow was closed
  6772. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6773. {
  6774. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false);
  6775. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6776. }
  6777. // Before main window displays
  6778. if (ManagedSession == NULL)
  6779. {
  6780. StartingWithoutSession();
  6781. }
  6782. UpdatePixelsPerInchMainWindowCounter();
  6783. FShowing = true;
  6784. }
  6785. //---------------------------------------------------------------------------
  6786. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6787. {
  6788. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6789. }
  6790. //---------------------------------------------------------------------------
  6791. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  6792. {
  6793. SessionListChanged();
  6794. InitStatusBar();
  6795. UpdateControls();
  6796. }
  6797. //---------------------------------------------------------------------------
  6798. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6799. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6800. TNotifyEvent OnBalloonClick, TObject * UserData)
  6801. {
  6802. bool Do;
  6803. UnicodeString Message;
  6804. if (E == NULL)
  6805. {
  6806. Message = Str;
  6807. Do = true;
  6808. }
  6809. else
  6810. {
  6811. Do = ExceptionMessage(E, Message);
  6812. }
  6813. if (Do && WinConfiguration->BalloonNotifications)
  6814. {
  6815. UnicodeString Title;
  6816. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6817. {
  6818. Message = TrimLeft(Message);
  6819. }
  6820. else
  6821. {
  6822. Message = UnformatMessage(Message);
  6823. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6824. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6825. Title = LoadResourceString(Captions[Type]);
  6826. if (Terminal != NULL)
  6827. {
  6828. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  6829. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  6830. }
  6831. }
  6832. if (Seconds == 0)
  6833. {
  6834. Seconds = WinConfiguration->NotificationsTimeout;
  6835. }
  6836. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6837. }
  6838. }
  6839. //---------------------------------------------------------------------------
  6840. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6841. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6842. UnicodeString HelpKeyword, const TMessageParams * Params,
  6843. TTerminal * Terminal)
  6844. {
  6845. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6846. (FOnFeedSynchronizeError != NULL)) &&
  6847. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6848. {
  6849. if (FOnFeedSynchronizeError != NULL)
  6850. {
  6851. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6852. }
  6853. else
  6854. {
  6855. DebugAssert(FErrorList != NULL);
  6856. TStringList * MoreMessagesCopy = NULL;
  6857. if (MoreMessages)
  6858. {
  6859. MoreMessagesCopy = new TStringList();
  6860. MoreMessagesCopy->Assign(MoreMessages);
  6861. }
  6862. FErrorList->AddObject(Message, MoreMessagesCopy);
  6863. }
  6864. PopupTrayBalloon(Terminal, Message, Type);
  6865. return ContinueAnswer(Answers);
  6866. }
  6867. else
  6868. {
  6869. bool UseBalloon = FTrayIcon->Visible;
  6870. if (UseBalloon)
  6871. {
  6872. PopupTrayBalloon(Terminal, Message, Type);
  6873. }
  6874. unsigned int Result;
  6875. try
  6876. {
  6877. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6878. }
  6879. __finally
  6880. {
  6881. // cancel only balloon we popped up, otherwise we may cancel notification
  6882. // balloon that was there before the message dialog
  6883. // (such as "dd confirmation opt in balloon)
  6884. if (UseBalloon)
  6885. {
  6886. FTrayIcon->CancelBalloon();
  6887. }
  6888. }
  6889. return Result;
  6890. }
  6891. }
  6892. //---------------------------------------------------------------------------
  6893. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6894. TTerminal * Terminal, Exception * E)
  6895. {
  6896. if (FTrayIcon->Visible)
  6897. {
  6898. PopupTrayBalloon(Terminal, L"", qtError, E);
  6899. }
  6900. // particularly prevent opening new session from jump list,
  6901. // while exception is shown
  6902. NonVisualDataModule->StartBusy();
  6903. try
  6904. {
  6905. ShowExtendedExceptionEx(Terminal, E);
  6906. }
  6907. __finally
  6908. {
  6909. NonVisualDataModule->EndBusy();
  6910. FTrayIcon->CancelBalloon();
  6911. }
  6912. }
  6913. //---------------------------------------------------------------------------
  6914. void __fastcall TCustomScpExplorerForm::SessionReady()
  6915. {
  6916. InitStatusBar();
  6917. // cannot rely on active page being page for active terminal,
  6918. // as it can happen that active page is the "new session" page
  6919. // (e.g. when reconnecting active terminal, while login dialog
  6920. // invoked from "new session" page is modal)
  6921. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  6922. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  6923. }
  6924. //---------------------------------------------------------------------------
  6925. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6926. TTerminal * Terminal, Exception * E)
  6927. {
  6928. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6929. if (!Terminal->Active)
  6930. {
  6931. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6932. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6933. {
  6934. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6935. }
  6936. }
  6937. }
  6938. //---------------------------------------------------------------------------
  6939. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6940. UnicodeString Message, TQueryType Type,
  6941. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6942. {
  6943. if ((E == NULL) ||
  6944. ExceptionMessage(E, Message))
  6945. {
  6946. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6947. if (Important)
  6948. {
  6949. Seconds *= 5;
  6950. }
  6951. UnicodeString NoteMessage(UnformatMessage(Message));
  6952. if (Terminal != NULL)
  6953. {
  6954. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  6955. NoteMessage = FORMAT(L"%s: %s",
  6956. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  6957. }
  6958. if (WinConfiguration->BalloonNotifications)
  6959. {
  6960. AddNote(NoteMessage);
  6961. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6962. }
  6963. else
  6964. {
  6965. FlashOnBackground();
  6966. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6967. }
  6968. }
  6969. }
  6970. //---------------------------------------------------------------------------
  6971. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6972. {
  6973. RestoreApp();
  6974. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6975. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6976. !NonVisualDataModule->Busy)
  6977. {
  6978. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6979. TTerminalManager::Instance()->ActiveSession = Terminal;
  6980. if (!ComponentVisible[fcQueueView])
  6981. {
  6982. ToggleQueueVisibility();
  6983. GoToQueue();
  6984. }
  6985. }
  6986. }
  6987. //---------------------------------------------------------------------------
  6988. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6989. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6990. {
  6991. UnicodeString Message;
  6992. TNotifyEvent OnClick = NULL;
  6993. TObject * UserData = NULL;
  6994. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6995. switch (Event)
  6996. {
  6997. case qeEmptyButMonitored:
  6998. if ((ATerminal != Terminal) || QueueInvisible)
  6999. {
  7000. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7001. OnClick = QueueEmptyNoteClicked;
  7002. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7003. TerminalNoteData->Terminal = ATerminal;
  7004. UserData = TerminalNoteData;
  7005. }
  7006. break;
  7007. case qeEmpty:
  7008. OperationComplete(ATerminal->QueueOperationStart);
  7009. break;
  7010. case qePendingUserAction:
  7011. if ((ATerminal != Terminal) ||
  7012. (QueueInvisible && !IsQueueAutoPopup()))
  7013. {
  7014. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7015. }
  7016. break;
  7017. default:
  7018. DebugFail();
  7019. }
  7020. if (!Message.IsEmpty())
  7021. {
  7022. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7023. }
  7024. }
  7025. //---------------------------------------------------------------------------
  7026. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7027. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7028. {
  7029. if (FDDExtMapFile != NULL)
  7030. {
  7031. CloseHandle(FDDExtMapFile);
  7032. FDDExtMapFile = NULL;
  7033. }
  7034. if (WinConfiguration->DDFakeFile)
  7035. {
  7036. DDFakeFileInitDrag(FileList, Created);
  7037. }
  7038. }
  7039. //---------------------------------------------------------------------------
  7040. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7041. {
  7042. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7043. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7044. {
  7045. FFakeFileDropTarget = FileName;
  7046. }
  7047. }
  7048. //---------------------------------------------------------------------------
  7049. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7050. {
  7051. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7052. if (!ForceDirectories(ApiPath(Result)))
  7053. {
  7054. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7055. }
  7056. FileSetAttr(ApiPath(Result), faHidden);
  7057. return Result;
  7058. }
  7059. //---------------------------------------------------------------------------
  7060. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7061. bool & Created)
  7062. {
  7063. FFakeFileDropTarget = UnicodeString();
  7064. FDragFakeDirectory = CreateFakeTransferDirectory();
  7065. FileList->AddItem(NULL, FDragFakeDirectory);
  7066. Created = true;
  7067. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7068. {
  7069. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7070. }
  7071. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7072. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7073. {
  7074. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7075. TDragExtCommStruct* CommStruct;
  7076. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7077. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7078. DebugAssert(CommStruct != NULL);
  7079. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7080. CommStruct->Dragging = true;
  7081. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7082. LENOF(CommStruct->DropDest));
  7083. NULL_TERMINATE(CommStruct->DropDest);
  7084. UnmapViewOfFile(CommStruct);
  7085. }
  7086. }
  7087. //---------------------------------------------------------------------------
  7088. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7089. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7090. {
  7091. bool Result;
  7092. DebugAssert(!IsLocalBrowserMode());
  7093. if (Sender == RemoteDirView)
  7094. {
  7095. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7096. }
  7097. else
  7098. {
  7099. DebugAssert(Sender == RemoteDriveView);
  7100. TStrings * FileList = RemoteDriveView->DragFileList();
  7101. try
  7102. {
  7103. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7104. }
  7105. __finally
  7106. {
  7107. delete FileList;
  7108. }
  7109. }
  7110. if (FDDTargetControl == RemoteDriveView)
  7111. {
  7112. RemoteDriveView->UpdateDropTarget();
  7113. }
  7114. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7115. // or from tree view to dir view.
  7116. // foMove happens when file/dir is dragged from remote tree view outside of
  7117. // application via dragex
  7118. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7119. (DropSourceControl == RemoteDriveView))
  7120. {
  7121. RemoteDriveView->UpdateDropSource();
  7122. }
  7123. return Result;
  7124. }
  7125. //---------------------------------------------------------------------------
  7126. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7127. {
  7128. DebugAssert(!IsLocalBrowserMode());
  7129. // This also handles drops of remote files to queue.
  7130. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7131. SAFE_DESTROY(FDDFileList);
  7132. if (!FFakeFileDropTarget.IsEmpty())
  7133. {
  7134. RemoveDir(ApiPath(FFakeFileDropTarget));
  7135. }
  7136. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7137. {
  7138. try
  7139. {
  7140. TDragResult DDResult = (Sender == RemoteDirView) ?
  7141. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7142. // Focus is moved to the target application,
  7143. // but as we are going to present the UI, we need to steal the focus back.
  7144. // This most likely won't work though (windows does not allow application
  7145. // to steal focus most of the time)
  7146. Application->BringToFront();
  7147. // On older version of Windows we never got drMove here, see also comment below.
  7148. // On Windows 10, we get the "move".
  7149. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7150. {
  7151. UnicodeString TargetDirectory;
  7152. TFileOperation Operation;
  7153. // drInvalid may mean drMove, see comment below
  7154. switch (DDResult)
  7155. {
  7156. case drCopy:
  7157. Operation = foCopy;
  7158. break;
  7159. case drMove:
  7160. Operation = foMove;
  7161. break;
  7162. default:
  7163. DebugFail();
  7164. case drInvalid:
  7165. // prefer "copy" for safety
  7166. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7167. break;
  7168. }
  7169. TTransferOperationParam Param;
  7170. UnicodeString CounterName;
  7171. bool ForceQueue;
  7172. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7173. {
  7174. // we get drInvalid both if d&d was intercepted by ddext,
  7175. // and when users drops on no-drop location.
  7176. // we tell the difference by existence of response from ddext,
  7177. // so we ignore absence of response in this case
  7178. if (DDResult != drInvalid)
  7179. {
  7180. // here we know that the extension is installed,
  7181. // as it is checked as soon as drag&drop starts from
  7182. // RemoteFileControlDDCreateDragFileList
  7183. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7184. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7185. {
  7186. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7187. }
  7188. else
  7189. {
  7190. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7191. }
  7192. }
  7193. }
  7194. else
  7195. {
  7196. // download using fake file
  7197. Param.Temp = false;
  7198. Param.DragDrop = true;
  7199. if (ForceQueue)
  7200. {
  7201. Param.Queue = asOn;
  7202. }
  7203. if (Sender == RemoteDirView)
  7204. {
  7205. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7206. }
  7207. if (RemoteFileControlFileOperation(Sender, Operation,
  7208. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7209. {
  7210. Configuration->Usage->Inc(CounterName);
  7211. }
  7212. }
  7213. }
  7214. }
  7215. __finally
  7216. {
  7217. CloseHandle(FDDExtMapFile);
  7218. FDDExtMapFile = NULL;
  7219. delete FDragFakeMonitors;
  7220. FDragFakeMonitors = NULL;
  7221. RemoveDir(ApiPath(FDragFakeDirectory));
  7222. FDragFakeDirectory = L"";
  7223. FFakeFileDropTarget = UnicodeString();
  7224. }
  7225. }
  7226. if (FDragDropSshTerminate.get() != NULL)
  7227. {
  7228. FDragDropSshTerminate->Rethrow();
  7229. }
  7230. }
  7231. //---------------------------------------------------------------------------
  7232. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7233. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7234. {
  7235. // Remember drop effect so we know (when user drops files), if we copy or move
  7236. FLastDropEffect = dwEffect;
  7237. }
  7238. //---------------------------------------------------------------------------
  7239. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7240. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7241. {
  7242. bool Result;
  7243. if (FDDTargetControl == QueueView3)
  7244. {
  7245. Directory = DefaultDownloadTargetDirectory();
  7246. Result = true;
  7247. CounterName = L"DownloadsDragDropQueue";
  7248. ForceQueue = true;
  7249. }
  7250. else if (!FFakeFileDropTarget.IsEmpty())
  7251. {
  7252. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7253. Result = true;
  7254. ForceQueue = false;
  7255. CounterName = L"DownloadsDragDropFakeFile";
  7256. }
  7257. else
  7258. {
  7259. ForceQueue = false;
  7260. Enabled = false;
  7261. try
  7262. {
  7263. int Timer = 0;
  7264. Result = false;
  7265. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7266. {
  7267. {
  7268. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7269. TDragExtCommStruct* CommStruct;
  7270. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7271. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7272. DebugAssert(CommStruct != NULL);
  7273. Result = !CommStruct->Dragging;
  7274. if (Result)
  7275. {
  7276. Directory = ExtractFilePath(CommStruct->DropDest);
  7277. CounterName = L"DownloadsDragDropExternalExt";
  7278. }
  7279. UnmapViewOfFile(CommStruct);
  7280. }
  7281. if (!Result)
  7282. {
  7283. Sleep(50);
  7284. Timer += 50;
  7285. Application->ProcessMessages();
  7286. }
  7287. }
  7288. }
  7289. __finally
  7290. {
  7291. Enabled = true;
  7292. }
  7293. }
  7294. return Result;
  7295. }
  7296. //---------------------------------------------------------------------------
  7297. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7298. const UnicodeString TempDir, int SecDelay)
  7299. {
  7300. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7301. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7302. if (FDelayedDeletionTimer == NULL)
  7303. {
  7304. DebugAssert(HandleAllocated());
  7305. FDelayedDeletionTimer = new TTimer(this);
  7306. FDelayedDeletionTimer->Interval = 10000;
  7307. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7308. }
  7309. else
  7310. {
  7311. FDelayedDeletionTimer->Enabled = true;
  7312. }
  7313. }
  7314. //---------------------------------------------------------------------------
  7315. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7316. {
  7317. DebugAssert(FDelayedDeletionList != NULL);
  7318. TDateTime N = Now();
  7319. TDateTime Alert;
  7320. UnicodeString Directory;
  7321. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7322. {
  7323. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7324. if ((N >= Alert) || (Sender == NULL))
  7325. {
  7326. Directory = FDelayedDeletionList->Strings[Index];
  7327. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7328. {
  7329. FDelayedDeletionList->Delete(Index);
  7330. }
  7331. }
  7332. }
  7333. if (FDelayedDeletionList->Count == 0)
  7334. {
  7335. FDelayedDeletionTimer->Enabled = false;
  7336. }
  7337. }
  7338. //---------------------------------------------------------------------------
  7339. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7340. {
  7341. DebugAssert(!IsLocalBrowserMode());
  7342. if (IsFileControl(FDDTargetControl, osRemote) ||
  7343. (FDDTargetControl == SessionsPageControl))
  7344. {
  7345. TManagedTerminal * TargetTerminal = NULL;
  7346. TFileOperation Operation = foNone;
  7347. if (FDDTargetControl == SessionsPageControl)
  7348. {
  7349. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7350. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7351. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7352. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7353. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7354. {
  7355. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7356. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7357. {
  7358. Operation = foRemoteMove;
  7359. }
  7360. else
  7361. {
  7362. Operation = foRemoteCopy;
  7363. }
  7364. }
  7365. }
  7366. else
  7367. {
  7368. if (FLastDropEffect == DROPEFFECT_MOVE)
  7369. {
  7370. Operation = foRemoteMove;
  7371. }
  7372. else if (FLastDropEffect == DROPEFFECT_COPY)
  7373. {
  7374. Operation = foRemoteCopy;
  7375. }
  7376. }
  7377. if (Operation != foNone)
  7378. {
  7379. RemoteFileControlFileOperation(DropSourceControl,
  7380. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7381. }
  7382. // abort drag&drop
  7383. Abort();
  7384. }
  7385. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7386. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7387. (FDDTargetControl != QueueView3))
  7388. {
  7389. DebugAssert(!FDragTempDir.IsEmpty());
  7390. TTransferType Type;
  7391. UnicodeString TempDir = FDragTempDir;
  7392. // We clear FDragTempDir before calling
  7393. // just in case it fail (raises exception)
  7394. FDragTempDir = L"";
  7395. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7396. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7397. // empty directory parameter means temp directory -> don't display it!
  7398. UnicodeString TargetDir = L"";
  7399. int Options = 0;
  7400. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7401. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7402. {
  7403. Abort();
  7404. }
  7405. // TargetDir is set when dropped on local file control
  7406. // (this was workaround for legacy dirview event handling, now it should be
  7407. // made prettier)
  7408. if (TargetDir.IsEmpty())
  7409. {
  7410. TargetDir = TempDir;
  7411. if (ForceDirectories(ApiPath(TargetDir)))
  7412. {
  7413. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7414. FPendingTempSpaceWarn = true;
  7415. try
  7416. {
  7417. FDragDropOperation = true;
  7418. int Params = cpTemporary |
  7419. (Type == ttMove ? cpDelete : 0);
  7420. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7421. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7422. }
  7423. __finally
  7424. {
  7425. FDragDropOperation = false;
  7426. FPendingTempSpaceWarn = false;
  7427. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7428. }
  7429. }
  7430. else
  7431. {
  7432. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7433. }
  7434. }
  7435. }
  7436. }
  7437. //---------------------------------------------------------------------------
  7438. void __fastcall TCustomScpExplorerForm::DDDownload(
  7439. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7440. {
  7441. DebugAssert(!IsLocalBrowserMode());
  7442. TAutoBatch AutoBatch(this);
  7443. UpdateCopyParamCounters(*CopyParam);
  7444. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7445. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7446. {
  7447. RemoteDriveView->UpdateDropSource();
  7448. }
  7449. }
  7450. //---------------------------------------------------------------------------
  7451. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7452. {
  7453. public:
  7454. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7455. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7456. {
  7457. }
  7458. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7459. {
  7460. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7461. TDataObjectFilesEx::AllowData(FormatEtc);
  7462. }
  7463. };
  7464. //---------------------------------------------------------------------------
  7465. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7466. TObject * Sender, TDataObject *& DataObject)
  7467. {
  7468. if (FDDExtMapFile != NULL)
  7469. {
  7470. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7471. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7472. {
  7473. Abort();
  7474. }
  7475. if (FileList->Count > 0)
  7476. {
  7477. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7478. if (!FilesObject->IsValid(true, true))
  7479. {
  7480. FilesObject->_Release();
  7481. }
  7482. else
  7483. {
  7484. DataObject = FilesObject;
  7485. }
  7486. }
  7487. }
  7488. }
  7489. //---------------------------------------------------------------------------
  7490. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7491. {
  7492. DebugFail();
  7493. }
  7494. //---------------------------------------------------------------------------
  7495. void __fastcall TCustomScpExplorerForm::GoToTree()
  7496. {
  7497. ComponentVisible[fcRemoteTree] = true;
  7498. DriveView(osRemote)->SetFocus();
  7499. }
  7500. //---------------------------------------------------------------------------
  7501. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7502. TPanelExport Export)
  7503. {
  7504. TCustomDirView * DirView = this->DirView(Side);
  7505. std::unique_ptr<TStrings> ExportData(new TStringList());
  7506. switch (Export)
  7507. {
  7508. case pePath:
  7509. ExportData->Add(DirView->PathName);
  7510. break;
  7511. case peFileList:
  7512. case peFullFileList:
  7513. {
  7514. bool FullPath = (Export == peFullFileList);
  7515. DirView->CreateFileList(false, FullPath, ExportData.get());
  7516. for (int Index = 0; Index < ExportData->Count; Index++)
  7517. {
  7518. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7519. {
  7520. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7521. }
  7522. }
  7523. }
  7524. break;
  7525. default:
  7526. DebugFail();
  7527. }
  7528. return ExportData.release();
  7529. }
  7530. //---------------------------------------------------------------------------
  7531. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7532. TPanelExport Export, TPanelExportDestination Destination)
  7533. {
  7534. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7535. PanelExportStore(Side, Export, Destination, ExportData.get());
  7536. }
  7537. //---------------------------------------------------------------------------
  7538. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7539. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7540. TStrings * ExportData)
  7541. {
  7542. if (Destination == pedClipboard)
  7543. {
  7544. TInstantOperationVisualizer Visualizer;
  7545. CopyToClipboard(ExportData);
  7546. }
  7547. else
  7548. {
  7549. DebugFail();
  7550. }
  7551. }
  7552. //---------------------------------------------------------------------------
  7553. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7554. {
  7555. TCustomDirView * DirView = this->DirView(Side);
  7556. UnicodeString Mask = DirView->Mask;
  7557. if (DoFilterMaskDialog(DirView, Mask))
  7558. {
  7559. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7560. Configuration->Usage->Inc(L"Filters");
  7561. }
  7562. }
  7563. //---------------------------------------------------------------------------
  7564. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7565. {
  7566. return FQueueController->DefaultOperation();
  7567. }
  7568. //---------------------------------------------------------------------------
  7569. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7570. TQueueOperation Operation, void ** Param)
  7571. {
  7572. switch (Operation)
  7573. {
  7574. case qoPreferences:
  7575. return true;
  7576. case qoGoTo:
  7577. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7578. case qoOnceEmpty:
  7579. return IsAnythingQueued();
  7580. default:
  7581. return FQueueController->AllowOperation(Operation, Param);
  7582. }
  7583. }
  7584. //---------------------------------------------------------------------------
  7585. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7586. {
  7587. if (DebugAlwaysTrue(QueueView3->Visible))
  7588. {
  7589. QueueView3->SetFocus();
  7590. }
  7591. }
  7592. //---------------------------------------------------------------------------
  7593. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7594. TQueueOperation Operation, void * Param)
  7595. {
  7596. if (Operation == qoGoTo)
  7597. {
  7598. GoToQueue();
  7599. }
  7600. else if (Operation == qoPreferences)
  7601. {
  7602. PreferencesDialog(pmQueue);
  7603. }
  7604. else
  7605. {
  7606. FQueueController->ExecuteOperation(Operation, Param);
  7607. }
  7608. }
  7609. //---------------------------------------------------------------------------
  7610. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7611. {
  7612. return (Queue != NULL) && Queue->Enabled;
  7613. }
  7614. //---------------------------------------------------------------------------
  7615. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7616. {
  7617. DebugAssert(Queue != NULL);
  7618. if (Queue != NULL)
  7619. {
  7620. Queue->Enabled = !Queue->Enabled;
  7621. }
  7622. }
  7623. //---------------------------------------------------------------------------
  7624. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7625. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7626. {
  7627. FQueueActedItem = QueueView3->ItemFocused;
  7628. }
  7629. //---------------------------------------------------------------------------
  7630. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7631. {
  7632. return TopDock->Height + QueueSplitter->Height;
  7633. }
  7634. //---------------------------------------------------------------------------
  7635. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7636. {
  7637. return
  7638. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7639. (QueueDock->Visible ? QueueDock->Height : 0) +
  7640. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7641. }
  7642. //---------------------------------------------------------------------------
  7643. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7644. {
  7645. return ScaleByTextHeight(this, 48);
  7646. }
  7647. //---------------------------------------------------------------------------
  7648. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7649. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7650. {
  7651. // when queue is hidden by double-clicking splitter, stray attempt to
  7652. // resize the panel with strange value arrives, make sure it is ignored
  7653. if (ComponentVisible[fcQueueView])
  7654. {
  7655. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7656. if (NewSize > MaxHeightLimit)
  7657. {
  7658. NewSize = MaxHeightLimit;
  7659. }
  7660. int MinHeightLimit =
  7661. GetStaticQueuePanelComponentsHeight() +
  7662. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7663. GetMinQueueViewHeight();
  7664. if (NewSize < MinHeightLimit)
  7665. {
  7666. NewSize = MinHeightLimit;
  7667. }
  7668. }
  7669. else
  7670. {
  7671. Accept = false;
  7672. }
  7673. }
  7674. //---------------------------------------------------------------------------
  7675. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7676. {
  7677. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7678. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7679. if (QueueViewHeight < GetMinQueueViewHeight())
  7680. {
  7681. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7682. }
  7683. }
  7684. //---------------------------------------------------------------------------
  7685. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7686. TDragObject *& /*DragObject*/)
  7687. {
  7688. FQueueActedItem = QueueView3->ItemFocused;
  7689. }
  7690. //---------------------------------------------------------------------------
  7691. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7692. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7693. {
  7694. Accept = true;
  7695. if (Source == QueueView3)
  7696. {
  7697. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7698. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7699. if (Accept)
  7700. {
  7701. TQueueItemProxy * QueueItem;
  7702. TQueueItemProxy * DestQueueItem;
  7703. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7704. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7705. Accept = (QueueItem != DestQueueItem) &&
  7706. (QueueItem->Status == TQueueItem::qsPending) &&
  7707. (DestQueueItem->Status == TQueueItem::qsPending);
  7708. }
  7709. }
  7710. }
  7711. //---------------------------------------------------------------------------
  7712. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7713. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7714. {
  7715. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7716. {
  7717. TQueueItemProxy * QueueItem;
  7718. TQueueItemProxy * DestQueueItem;
  7719. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7720. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7721. QueueItem->Move(DestQueueItem);
  7722. }
  7723. }
  7724. //---------------------------------------------------------------------------
  7725. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7726. {
  7727. if ((QueueView3->ItemFocused == NULL) &&
  7728. (QueueView3->Items->Count > 0))
  7729. {
  7730. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7731. }
  7732. QueueLabelUpdateStatus();
  7733. }
  7734. //---------------------------------------------------------------------------
  7735. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7736. {
  7737. QueueLabelUpdateStatus();
  7738. }
  7739. //---------------------------------------------------------------------------
  7740. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7741. {
  7742. QueueLabel->UpdateStatus();
  7743. }
  7744. //---------------------------------------------------------------------------
  7745. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7746. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7747. {
  7748. if (Selected)
  7749. {
  7750. NonVisualDataModule->UpdateNonVisibleActions();
  7751. }
  7752. }
  7753. //---------------------------------------------------------------------------
  7754. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7755. {
  7756. TDragDropFilesEx * Result = NULL;
  7757. if (Sender == SessionsPageControl)
  7758. {
  7759. Result = FSessionsDragDropFilesEx;
  7760. }
  7761. else if (Sender == QueueView3)
  7762. {
  7763. Result = FQueueDragDropFilesEx;
  7764. }
  7765. else
  7766. {
  7767. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7768. if (DirView != NULL)
  7769. {
  7770. Result = DirView->DragDropFilesEx;
  7771. }
  7772. else
  7773. {
  7774. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7775. if (DriveView != NULL)
  7776. {
  7777. Result = DriveView->DragDropFilesEx;
  7778. }
  7779. }
  7780. }
  7781. DebugAssert(Result != NULL);
  7782. return Result;
  7783. }
  7784. //---------------------------------------------------------------------------
  7785. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7786. {
  7787. return (DView->SelCount == DView->FilesCount);
  7788. }
  7789. //---------------------------------------------------------------------------
  7790. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7791. {
  7792. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7793. }
  7794. //---------------------------------------------------------------------------
  7795. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7796. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7797. {
  7798. DebugAssert(!IsLocalBrowserMode());
  7799. TFileOperation Operation;
  7800. switch (Effect)
  7801. {
  7802. case DROPEFFECT_MOVE:
  7803. Operation = foMove;
  7804. break;
  7805. case DROPEFFECT_COPY:
  7806. // occures on WinXP (reported by user)
  7807. default:
  7808. Operation = foCopy;
  7809. break;
  7810. };
  7811. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7812. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7813. if (DragDropFilesEx->FileList->Count > 0)
  7814. {
  7815. TStrings * FileList = new TStringList();
  7816. try
  7817. {
  7818. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7819. {
  7820. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7821. }
  7822. FDragDropOperation = true;
  7823. TTransferOperationParam Param;
  7824. Param.TargetDirectory = TargetPath;
  7825. // upload, no temp dirs
  7826. Param.Temp = false;
  7827. Param.DragDrop = DragDrop;
  7828. Param.Options =
  7829. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7830. if (ForceQueue)
  7831. {
  7832. Param.Queue = asOn;
  7833. }
  7834. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7835. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7836. {
  7837. if (IsFileControl(DropSourceControl, osLocal))
  7838. {
  7839. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7840. }
  7841. else
  7842. {
  7843. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7844. }
  7845. }
  7846. }
  7847. __finally
  7848. {
  7849. FDragDropOperation = false;
  7850. delete FileList;
  7851. }
  7852. }
  7853. }
  7854. //---------------------------------------------------------------------------
  7855. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7856. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7857. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7858. {
  7859. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7860. }
  7861. //---------------------------------------------------------------------------
  7862. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7863. TObject * Sender, int grfKeyState, int & dwEffect)
  7864. {
  7865. DebugAssert(!IsLocalBrowserMode());
  7866. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7867. if ((dwEffect != DROPEFFECT_NONE) &&
  7868. IsFileControl(DropSourceControl, osRemote))
  7869. {
  7870. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7871. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7872. (RemoteDirView->DropTarget == NULL))
  7873. {
  7874. dwEffect = DROPEFFECT_NONE;
  7875. }
  7876. else
  7877. {
  7878. if (dwEffect == DROPEFFECT_COPY)
  7879. {
  7880. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  7881. // currently we support copying always (at least via temporary directory);
  7882. // remove associated checks once this all proves stable and working
  7883. bool CopyCapable = true;
  7884. // if we do not support neither of operations, there's no discussion
  7885. if (!MoveCapable && !CopyCapable)
  7886. {
  7887. dwEffect = DROPEFFECT_NONE;
  7888. }
  7889. else
  7890. {
  7891. // The default effect inside remote panel is move,
  7892. // unless we do not support it, but support copy
  7893. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7894. {
  7895. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7896. }
  7897. else
  7898. {
  7899. // with ctrl-down, we want copy unless it is not supported
  7900. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7901. }
  7902. }
  7903. }
  7904. }
  7905. }
  7906. }
  7907. //---------------------------------------------------------------------------
  7908. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7909. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7910. {
  7911. DebugAssert(!IsLocalBrowserMode());
  7912. if (FDDTotalSize >= 0)
  7913. {
  7914. if (File->IsDirectory)
  7915. {
  7916. FDDTotalSize = -1;
  7917. }
  7918. else
  7919. {
  7920. FDDTotalSize += File->Size;
  7921. }
  7922. }
  7923. DebugAssert(!FDragTempDir.IsEmpty());
  7924. // TODO: this is quite ineffective
  7925. // TODO: what if invalid character replacement is disabled?
  7926. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7927. UnicodeString TransferFileName;
  7928. if (Sender == RemoteDriveView)
  7929. {
  7930. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7931. }
  7932. else
  7933. {
  7934. TransferFileName = File->FileName;
  7935. }
  7936. FDDFileList->AddObject(TransferFileName, File);
  7937. }
  7938. //---------------------------------------------------------------------------
  7939. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7940. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7941. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7942. {
  7943. // sometimes we do not get DDEnd so the list is not released
  7944. SAFE_DESTROY(FDDFileList);
  7945. FDDFileList = new TStringList();
  7946. FDragTempDir = WinConfiguration->TemporaryDir();
  7947. FDDTotalSize = 0;
  7948. FDragDropSshTerminate.reset(NULL);
  7949. }
  7950. //---------------------------------------------------------------------------
  7951. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7952. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7953. HRESULT & Result)
  7954. {
  7955. if (Result == DRAGDROP_S_DROP)
  7956. {
  7957. try
  7958. {
  7959. GlobalDragImageList->HideDragImage();
  7960. try
  7961. {
  7962. RemoteFileControlDDTargetDrop();
  7963. }
  7964. catch(ESshTerminate & E)
  7965. {
  7966. FDragDropSshTerminate.reset(E.Clone());
  7967. }
  7968. }
  7969. catch (Exception &E)
  7970. {
  7971. // If downloading fails we need to cancel drag&drop, otherwise
  7972. // Explorer shows error
  7973. // But by the way exception probably never reach this point as
  7974. // it's catched on way.
  7975. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7976. Result = DRAGDROP_S_CANCEL;
  7977. ShowExtendedException(Terminal, &E);
  7978. }
  7979. }
  7980. }
  7981. //---------------------------------------------------------------------------
  7982. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7983. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7984. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7985. {
  7986. TFileMasks::TParams MaskParams;
  7987. MaskParams.Size = Size;
  7988. MaskParams.Modification = Modification;
  7989. // this does not re-parse the mask if it is the same as the last time
  7990. FDirViewMatchMask = Masks;
  7991. bool ImplicitMatch;
  7992. Matches =
  7993. // RecurseInclude parameter has no effect as the Path is empty
  7994. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7995. (AllowImplicitMatches || !ImplicitMatch);
  7996. }
  7997. //---------------------------------------------------------------------------
  7998. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7999. TObject * Sender, TListItem * Item, WORD & Indexes)
  8000. {
  8001. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8002. UnicodeString Ext;
  8003. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  8004. {
  8005. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  8006. }
  8007. else
  8008. {
  8009. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  8010. }
  8011. if (SameText(Ext, Configuration->PartialExt))
  8012. {
  8013. Indexes |= oiPartial;
  8014. }
  8015. }
  8016. //---------------------------------------------------------------------------
  8017. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8018. {
  8019. return
  8020. DirViewEnabled(osCurrent) &&
  8021. DirView(osCurrent)->CanPasteFromClipBoard();
  8022. }
  8023. //---------------------------------------------------------------------------
  8024. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8025. {
  8026. bool Result = false;
  8027. if (CanPasteToDirViewFromClipBoard())
  8028. {
  8029. Result = true;
  8030. }
  8031. else
  8032. {
  8033. UnicodeString ClipboardText;
  8034. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8035. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8036. {
  8037. if (StoredSessions->IsUrl(ClipboardText))
  8038. {
  8039. Result = true;
  8040. }
  8041. else
  8042. {
  8043. Result = DirViewEnabled(osCurrent);
  8044. }
  8045. }
  8046. }
  8047. return Result;
  8048. }
  8049. //---------------------------------------------------------------------------
  8050. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8051. {
  8052. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8053. if (DoesClipboardContainOurFiles())
  8054. {
  8055. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8056. {
  8057. DebugAssert(!IsLocalBrowserMode());
  8058. TTerminalManager * Manager = TTerminalManager::Instance();
  8059. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8060. Manager->ActiveSession = FClipboardTerminal;
  8061. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8062. }
  8063. }
  8064. else if (CanPasteToDirViewFromClipBoard())
  8065. {
  8066. DirView(osCurrent)->PasteFromClipBoard();
  8067. }
  8068. else
  8069. {
  8070. UnicodeString ClipboardText;
  8071. if (NonEmptyTextFromClipboard(ClipboardText))
  8072. {
  8073. if (StoredSessions->IsUrl(ClipboardText))
  8074. {
  8075. NewSession(ClipboardText);
  8076. }
  8077. else
  8078. {
  8079. DirView(osCurrent)->Path = ClipboardText;
  8080. }
  8081. }
  8082. }
  8083. }
  8084. //---------------------------------------------------------------------------
  8085. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8086. {
  8087. // TBD
  8088. }
  8089. //---------------------------------------------------------------------------
  8090. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8091. {
  8092. TCustomDirView * ADirView = DirView(Side);
  8093. ADirView->SelectAll(Mode);
  8094. ADirView->SetFocus();
  8095. }
  8096. //---------------------------------------------------------------------------
  8097. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8098. {
  8099. TCustomDirView * ADirView = DirView(Side);
  8100. TFileFilter Filter;
  8101. DefaultFileFilter(Filter);
  8102. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8103. {
  8104. Configuration->Usage->Inc(L"MaskSelections");
  8105. ADirView->SelectFiles(Filter, Select);
  8106. ADirView->SetFocus();
  8107. }
  8108. }
  8109. //---------------------------------------------------------------------------
  8110. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8111. {
  8112. TCustomDirView * ADirView = DirView(Side);
  8113. ADirView->RestoreSelectedNames();
  8114. ADirView->SetFocus();
  8115. }
  8116. //---------------------------------------------------------------------------
  8117. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8118. {
  8119. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8120. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8121. {
  8122. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8123. UnicodeString Ext;
  8124. if (!IsSideLocalBrowser(osCurrent))
  8125. {
  8126. Ext = UnixExtractFileExt(FileName);
  8127. }
  8128. else
  8129. {
  8130. Ext = ExtractFileExt(FileName);
  8131. }
  8132. if (Ext.IsEmpty())
  8133. {
  8134. Ext = L".";
  8135. }
  8136. TFileFilter Filter;
  8137. Filter.Masks = FORMAT(L"*%s", (Ext));
  8138. Filter.Directories = false;
  8139. CurrentDirView->SelectFiles(Filter, Select);
  8140. }
  8141. }
  8142. //---------------------------------------------------------------------------
  8143. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8144. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8145. {
  8146. UnicodeString Result;
  8147. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8148. {
  8149. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8150. }
  8151. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8152. {
  8153. // noop
  8154. }
  8155. else
  8156. {
  8157. Result =
  8158. FMTLOAD(FILE_INFO_FORMAT,
  8159. (FormatBytes(FileInfo.SelectedSize),
  8160. FormatBytes(FileInfo.FilesSize),
  8161. FormatNumber(FileInfo.SelectedCount),
  8162. FormatNumber(FileInfo.FilesCount)));
  8163. }
  8164. return Result;
  8165. }
  8166. //---------------------------------------------------------------------------
  8167. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8168. TTBXStatusPanel * Panel, TOperationSide Side)
  8169. {
  8170. if (Panel->Index == 1)
  8171. {
  8172. ToggleShowHiddenFiles();
  8173. }
  8174. else if (Panel->Index == 2)
  8175. {
  8176. Filter(Side);
  8177. }
  8178. }
  8179. //---------------------------------------------------------------------------
  8180. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8181. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8182. {
  8183. DebugAssert(!StatusBar->SimplePanel);
  8184. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8185. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8186. }
  8187. //---------------------------------------------------------------------------
  8188. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8189. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8190. {
  8191. DebugAssert(StatusBar->Panels->Count >= 3);
  8192. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8193. if (FileInfo.HiddenCount > 0)
  8194. {
  8195. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8196. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8197. }
  8198. else
  8199. {
  8200. HiddenFilesPanel->ViewPriority = 0;
  8201. // not really necessary, just to cleanup no-longer-valid data
  8202. HiddenFilesPanel->Caption = L"";
  8203. }
  8204. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8205. if (FileInfo.FilteredCount > 0)
  8206. {
  8207. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8208. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8209. }
  8210. else
  8211. {
  8212. FilteredFilesPanel->ViewPriority = 0;
  8213. // not really necessary, just to cleanup no-longer-valid data
  8214. FilteredFilesPanel->Caption = L"";
  8215. }
  8216. }
  8217. //---------------------------------------------------------------------------
  8218. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8219. TObject * /*Sender*/)
  8220. {
  8221. if (DirView(osOther)->Enabled)
  8222. {
  8223. DirView(osOther)->SetFocus();
  8224. }
  8225. }
  8226. //---------------------------------------------------------------------------
  8227. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8228. {
  8229. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8230. switch (Config.Show)
  8231. {
  8232. case qvShow:
  8233. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8234. {
  8235. Config.Show = qvHide;
  8236. }
  8237. else
  8238. {
  8239. Config.Show = Config.LastHideShow;
  8240. }
  8241. break;
  8242. case qvHideWhenEmpty:
  8243. if (ComponentVisible[fcQueueView])
  8244. {
  8245. Config.Show = qvHide;
  8246. }
  8247. else
  8248. {
  8249. Config.LastHideShow = Config.Show;
  8250. Config.Show = qvShow;
  8251. }
  8252. break;
  8253. case qvHide:
  8254. Config.LastHideShow = Config.Show;
  8255. Config.Show = qvShow;
  8256. break;
  8257. }
  8258. WinConfiguration->QueueView = Config;
  8259. }
  8260. //---------------------------------------------------------------------------
  8261. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8262. {
  8263. UnicodeString Result;
  8264. if (Terminal != NULL)
  8265. {
  8266. switch (WinConfiguration->PathInCaption)
  8267. {
  8268. case picShort:
  8269. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8270. break;
  8271. case picFull:
  8272. Result = Terminal->CurrentDirectory;
  8273. break;
  8274. }
  8275. }
  8276. return Result;
  8277. }
  8278. //---------------------------------------------------------------------------
  8279. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8280. {
  8281. TColor Result = GetWindowColor(FSessionColor);
  8282. return Result;
  8283. }
  8284. //---------------------------------------------------------------------------
  8285. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8286. {
  8287. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8288. return Result;
  8289. }
  8290. //---------------------------------------------------------------------------
  8291. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8292. {
  8293. if (!FSessionChanging)
  8294. {
  8295. TTerminalManager::Instance()->UpdateAppTitle();
  8296. }
  8297. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8298. // background to gray on Windows 7 (works on Windows 10).
  8299. // See also EnableControl
  8300. if (Showing)
  8301. {
  8302. if (ManagedSession != NULL)
  8303. {
  8304. // Update path when it changes
  8305. if ((SessionsPageControl->ActivePage != NULL) &&
  8306. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8307. {
  8308. UpdateSessionTab(SessionsPageControl->ActivePage);
  8309. }
  8310. }
  8311. NonVisualDataModule->ReconnectSessionAction->Update();
  8312. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8313. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8314. CenterReconnectToolbar();
  8315. bool HasTerminal = HasActiveTerminal();
  8316. RemoteDirView->Enabled = HasTerminal;
  8317. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8318. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8319. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8320. RemoteDriveView->Color = RemoteDirView->Color;
  8321. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8322. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8323. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8324. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8325. QueueFileList->Enabled = QueueView3->Enabled;
  8326. QueueFileList->Color = QueueView3->Color;
  8327. QueueFileList->Font->Color = QueueView3->Font->Color;
  8328. QueueLabelUpdateStatus();
  8329. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8330. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8331. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8332. {
  8333. UpdateDarkMode();
  8334. }
  8335. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8336. }
  8337. }
  8338. //---------------------------------------------------------------------------
  8339. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8340. {
  8341. UpdateControls();
  8342. }
  8343. //---------------------------------------------------------------------------
  8344. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8345. TObject * Sender)
  8346. {
  8347. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8348. DebugAssert(DirView != NULL);
  8349. DoDirViewLoaded(DirView);
  8350. TransferPresetAutoSelect();
  8351. }
  8352. //---------------------------------------------------------------------------
  8353. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8354. {
  8355. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8356. // first run after installation
  8357. if (double(Updates.LastCheck) == 0)
  8358. {
  8359. // make sure next time there will be an update (if enabled)
  8360. Updates.LastCheck = TDateTime(1);
  8361. WinConfiguration->Updates = Updates;
  8362. }
  8363. else if ((double(Updates.Period) > 0) &&
  8364. (Now() - Updates.LastCheck >= Updates.Period))
  8365. {
  8366. TThreadMethod OnUpdatesChecked = NULL;
  8367. if (!IsUWP())
  8368. {
  8369. OnUpdatesChecked = UpdatesChecked;
  8370. }
  8371. StartUpdateThread(OnUpdatesChecked);
  8372. }
  8373. }
  8374. //---------------------------------------------------------------------------
  8375. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8376. {
  8377. UnicodeString Message;
  8378. bool New;
  8379. TQueryType Type;
  8380. GetUpdatesMessage(Message, New, Type, false);
  8381. if (!Message.IsEmpty())
  8382. {
  8383. if (New)
  8384. {
  8385. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8386. }
  8387. if (!New && (Type != qtWarning))
  8388. {
  8389. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8390. }
  8391. else
  8392. {
  8393. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8394. }
  8395. if (New)
  8396. {
  8397. Configuration->Usage->Inc(L"UpdateNotifications");
  8398. }
  8399. }
  8400. }
  8401. //---------------------------------------------------------------------------
  8402. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8403. {
  8404. RestoreApp();
  8405. bool CanDisplay = !NonVisualDataModule->Busy;
  8406. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8407. {
  8408. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8409. }
  8410. if (CanDisplay)
  8411. {
  8412. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8413. CheckForUpdates(true);
  8414. }
  8415. else
  8416. {
  8417. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8418. }
  8419. }
  8420. //---------------------------------------------------------------------------
  8421. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8422. TCopyParamRuleData & Data)
  8423. {
  8424. DebugAssert(Terminal != NULL);
  8425. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8426. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8427. Data.RemoteDirectory = RemoteDirView->PathName;
  8428. }
  8429. //---------------------------------------------------------------------------
  8430. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8431. {
  8432. // Terminal can be null when we are changing local directory implicitly,
  8433. // because it has been deleted, while no session is connected
  8434. // (Login dialog is open)
  8435. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8436. {
  8437. DebugAssert(!IsLocalBrowserMode());
  8438. TCopyParamRuleData Data;
  8439. GetTransferPresetAutoSelectData(Data);
  8440. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8441. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8442. if (CopyParamIndex < 0)
  8443. {
  8444. // there is no preset that matches autoselection
  8445. // set preset that we consider "default"
  8446. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8447. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8448. }
  8449. else
  8450. {
  8451. // there is preset matching autoselection
  8452. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8453. if (CopyParamName == FCopyParamAutoSelected)
  8454. {
  8455. // autoselected the same preset as the last time
  8456. // make no change (i.e. preserve custom user preset, if any)
  8457. }
  8458. else
  8459. {
  8460. // autoselected the different preset then the last time (or there
  8461. // was default preset set)
  8462. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8463. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8464. }
  8465. }
  8466. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8467. {
  8468. if (CopyParamIndex >= 0)
  8469. {
  8470. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8471. }
  8472. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8473. try
  8474. {
  8475. int Fmt =
  8476. (CopyParamIndex < 0) ?
  8477. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8478. COPY_PARAM_AUTOSELECTED;
  8479. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8480. Data->Message = MainInstructions(Message);
  8481. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8482. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8483. CopyParamAttrs);
  8484. if (CopyParamIndex >= 0)
  8485. {
  8486. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8487. Info = FORMAT(L"%s\n \n%s", (Info,
  8488. FMTLOAD(COPY_PARAM_RULE,
  8489. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8490. }
  8491. Data->Message += L"\n\n" + Info;
  8492. if (WinConfiguration->CopyParamAutoSelectNotice)
  8493. {
  8494. TransferPresetNoteMessage(Data, true);
  8495. }
  8496. else
  8497. {
  8498. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8499. Data = NULL; // ownership passed
  8500. }
  8501. }
  8502. __finally
  8503. {
  8504. delete Data;
  8505. }
  8506. }
  8507. }
  8508. }
  8509. //---------------------------------------------------------------------------
  8510. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8511. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8512. {
  8513. DebugAssert(NoteData != NULL);
  8514. DebugAssert(!IsLocalBrowserMode());
  8515. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8516. TQueryButtonAlias Aliases[1];
  8517. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8518. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8519. Params.Aliases = Aliases;
  8520. Params.AliasesCount = LENOF(Aliases);
  8521. unsigned int Result =
  8522. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8523. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8524. switch (Result)
  8525. {
  8526. case qaNeverAskAgain:
  8527. DebugAssert(AllowNeverAskAgain);
  8528. WinConfiguration->CopyParamAutoSelectNotice = false;
  8529. break;
  8530. case qaIgnore:
  8531. PreferencesDialog(pmPresets);
  8532. break;
  8533. }
  8534. }
  8535. //---------------------------------------------------------------------------
  8536. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8537. {
  8538. // as of now useless, as this is used for notes only, never for balloons, ...
  8539. RestoreApp();
  8540. // .. and we should never be busy here
  8541. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8542. {
  8543. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8544. }
  8545. }
  8546. //---------------------------------------------------------------------------
  8547. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8548. TPreferencesMode APreferencesMode)
  8549. {
  8550. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8551. TCopyParamRuleData Data;
  8552. if (Terminal != NULL)
  8553. {
  8554. PreferencesData.reset(new TPreferencesDialogData());
  8555. GetTransferPresetAutoSelectData(Data);
  8556. PreferencesData->CopyParamRuleData = &Data;
  8557. }
  8558. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8559. }
  8560. //---------------------------------------------------------------------------
  8561. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8562. const TCustomCommandType & Command)
  8563. {
  8564. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8565. {
  8566. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8567. }
  8568. }
  8569. //---------------------------------------------------------------------------
  8570. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8571. {
  8572. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8573. TCustomCommandType Command;
  8574. // make sure we use local custom command when remote are not supported
  8575. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8576. {
  8577. Command = FLastCustomCommand;
  8578. }
  8579. else
  8580. {
  8581. Command.Params = Command.Params | ccLocal;
  8582. }
  8583. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8584. FEditingFocusedAdHocCommand = OnFocused;
  8585. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8586. int Options =
  8587. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8588. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8589. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8590. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8591. {
  8592. FLastCustomCommand = Command;
  8593. UpdateCustomCommandsToolbar();
  8594. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8595. }
  8596. }
  8597. //---------------------------------------------------------------------------
  8598. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8599. {
  8600. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8601. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8602. DebugAssert(State > 0);
  8603. if (State <= 0)
  8604. {
  8605. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8606. }
  8607. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8608. }
  8609. //---------------------------------------------------------------------------
  8610. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8611. TCustomCommandType & Command, int & State)
  8612. {
  8613. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8614. if (Result)
  8615. {
  8616. Command = FLastCustomCommand;
  8617. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8618. }
  8619. return Result;
  8620. }
  8621. //---------------------------------------------------------------------------
  8622. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8623. {
  8624. ResetIncrementalSearch();
  8625. }
  8626. //---------------------------------------------------------------------------
  8627. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8628. {
  8629. DebugAssert(ComponentVisible[Component]);
  8630. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8631. }
  8632. //---------------------------------------------------------------------------
  8633. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8634. {
  8635. // when queue panel is resized here directly, the status bar is stretched
  8636. // over whole space the panel occupied
  8637. PostComponentHide(fcQueueView);
  8638. }
  8639. //---------------------------------------------------------------------------
  8640. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8641. {
  8642. ComponentVisible[fcQueueFileList] = false;
  8643. }
  8644. //---------------------------------------------------------------------------
  8645. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8646. {
  8647. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8648. RefreshColorMode();
  8649. ResetSysDarkTheme();
  8650. ConfigurationChanged();
  8651. ConfigureInterface();
  8652. // Should be called for all controls
  8653. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8654. }
  8655. //---------------------------------------------------------------------------
  8656. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8657. {
  8658. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8659. if (!FInvalid &&
  8660. (Message.LParam != 0) &&
  8661. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8662. {
  8663. ThemeChanged();
  8664. }
  8665. TForm::Dispatch(&Message);
  8666. }
  8667. //---------------------------------------------------------------------------
  8668. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8669. {
  8670. TMessage * M = static_cast<TMessage*>(Message);
  8671. switch (M->Msg)
  8672. {
  8673. case CM_DIALOGCHAR:
  8674. CMDialogChar(*M);
  8675. break;
  8676. case WM_APPCOMMAND:
  8677. WMAppCommand(*M);
  8678. break;
  8679. case WM_SYSCOMMAND:
  8680. WMSysCommand(*M);
  8681. break;
  8682. case WM_QUERYENDSESSION:
  8683. WMQueryEndSession(*M);
  8684. break;
  8685. case WM_ENDSESSION:
  8686. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8687. break;
  8688. #ifdef _DEBUG
  8689. case WM_WINDOWPOSCHANGED:
  8690. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8691. break;
  8692. case CM_CUSTOMSTYLECHANGED:
  8693. TForm::Dispatch(Message);
  8694. break;
  8695. #endif
  8696. case WM_COMPONENT_HIDE:
  8697. {
  8698. Byte Component = static_cast<Byte>(M->WParam);
  8699. // sanity check
  8700. if (ComponentVisible[Component])
  8701. {
  8702. // special treatment
  8703. if (Component == fcQueueView)
  8704. {
  8705. ToggleQueueVisibility();
  8706. DebugAssert(!ComponentVisible[fcQueueView]);
  8707. }
  8708. else
  8709. {
  8710. ComponentVisible[Component] = false;
  8711. }
  8712. }
  8713. }
  8714. break;
  8715. case WM_COPYDATA:
  8716. WMCopyData(*M);
  8717. break;
  8718. case WM_MANAGES_CAPTION:
  8719. // caption managed in TTerminalManager::UpdateAppTitle()
  8720. M->Result = 1;
  8721. break;
  8722. case WM_WANTS_MOUSEWHEEL:
  8723. M->Result = 1;
  8724. break;
  8725. case CM_SHOWINGCHANGED:
  8726. CMShowingChanged(*M);
  8727. break;
  8728. case WM_CLOSE:
  8729. WMClose(*M);
  8730. break;
  8731. case WM_DPICHANGED:
  8732. WMDpiChanged(*M);
  8733. break;
  8734. case CM_DPICHANGED:
  8735. CMDpiChanged(*M);
  8736. break;
  8737. case WM_SETTINGCHANGE:
  8738. WMSettingChange(*M);
  8739. break;
  8740. case CM_DIALOGKEY:
  8741. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8742. break;
  8743. default:
  8744. TForm::Dispatch(Message);
  8745. break;
  8746. }
  8747. }
  8748. //---------------------------------------------------------------------------
  8749. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8750. {
  8751. // noop
  8752. }
  8753. //---------------------------------------------------------------------------
  8754. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8755. {
  8756. TForm::Dispatch(&Message);
  8757. FSessionColors->Clear();
  8758. AddFixedSessionImages();
  8759. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8760. UpdateImages();
  8761. }
  8762. //---------------------------------------------------------------------------
  8763. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8764. {
  8765. TForm::Dispatch(&Message);
  8766. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8767. UpdatePixelsPerInchMainWindowCounter();
  8768. }
  8769. //---------------------------------------------------------------------------
  8770. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8771. {
  8772. // Cannot close window while we are busy.
  8773. // We cannot test this in FormCloseQuery as that is called also from
  8774. // Close(), which is called by CloseApplicationAction. So we can be busy
  8775. // there already even, when it is legitimate to close the application.
  8776. // Possibly a better place to handle this would be WMSysCommand.
  8777. if (NonVisualDataModule->Busy)
  8778. {
  8779. Message.Result = 1;
  8780. }
  8781. else
  8782. {
  8783. TForm::Dispatch(&Message);
  8784. }
  8785. }
  8786. //---------------------------------------------------------------------------
  8787. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8788. {
  8789. // Ignoring implicit local-local browser of Commander, opened from DoShow
  8790. bool NoSession = (ManagedSession == NULL);
  8791. // This calls TCustomScpExplorerForm::DoShow
  8792. TForm::Dispatch(&Message);
  8793. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  8794. if (Showing && !FStarted)
  8795. {
  8796. FStarted = true;
  8797. InterfaceStarted();
  8798. }
  8799. if (Showing && NoSession)
  8800. {
  8801. // When we are starting minimized (i.e. from an installer),
  8802. // postpone showing Login dialog until we get restored.
  8803. // Otherwise the Login dialog (and Authentication window) show restored
  8804. // over invisible (minimized) main window.
  8805. if (WindowState == wsMinimized)
  8806. {
  8807. FNeedSession = true;
  8808. }
  8809. else
  8810. {
  8811. // This happens before application ever goes idle, so the toolbars would
  8812. // stay enabled (initial state) until the Login dialog is dismissed.
  8813. UpdateActions();
  8814. NonVisualDataModule->StartBusy();
  8815. try
  8816. {
  8817. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8818. // otherwise the dialog does not receive focus.
  8819. // With Commander interface the ProcessMessages is called already
  8820. // by TDriveView, but with Explorer interface, we need to call it explicily
  8821. Application->ProcessMessages();
  8822. // do not reload sessions, they have been loaded just now (optimization)
  8823. NeedSession(true);
  8824. }
  8825. __finally
  8826. {
  8827. NonVisualDataModule->EndBusy();
  8828. }
  8829. }
  8830. }
  8831. }
  8832. //---------------------------------------------------------------------------
  8833. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8834. {
  8835. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8836. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8837. }
  8838. //---------------------------------------------------------------------------
  8839. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8840. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8841. int & MaxHeight)
  8842. {
  8843. // workaround for bug in TWinControl.CalcConstraints
  8844. // Check for empty rect (restore from iconic state) is done there only after
  8845. // call to AdjustClientRect, which enlarges the rect (for forms).
  8846. TRect R = GetClientRect();
  8847. // when restoring from iconic state, place no restrictions
  8848. if (IsRectEmpty(R))
  8849. {
  8850. MinWidth = 0;
  8851. MinHeight = 0;
  8852. MaxWidth = 32000;
  8853. MaxHeight = 32000;
  8854. }
  8855. }
  8856. //---------------------------------------------------------------------------
  8857. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8858. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8859. {
  8860. // terminal can be already closed (e.g. dropped connection)
  8861. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8862. {
  8863. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8864. try
  8865. {
  8866. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8867. }
  8868. catch(...)
  8869. {
  8870. if (!Terminal->Active)
  8871. {
  8872. Close = true;
  8873. }
  8874. throw;
  8875. }
  8876. }
  8877. }
  8878. //---------------------------------------------------------------------------
  8879. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8880. {
  8881. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8882. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8883. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8884. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8885. {
  8886. OnGetSpaceAvailable = GetSpaceAvailable;
  8887. }
  8888. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8889. OnGetSpaceAvailable);
  8890. }
  8891. //---------------------------------------------------------------------------
  8892. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8893. {
  8894. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8895. Terminal->FillSessionDataForCode(Data.get());
  8896. return Data.release();
  8897. }
  8898. //---------------------------------------------------------------------------
  8899. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8900. {
  8901. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8902. DoGenerateUrlDialog(Data.get(), Paths);
  8903. }
  8904. //---------------------------------------------------------------------------
  8905. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8906. {
  8907. GenerateUrl(NULL);
  8908. }
  8909. //---------------------------------------------------------------------------
  8910. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8911. {
  8912. std::unique_ptr<TStrings> Paths(new TStringList());
  8913. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8914. GenerateUrl(Paths.get());
  8915. }
  8916. //---------------------------------------------------------------------------
  8917. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8918. {
  8919. FSessionColor = value;
  8920. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8921. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8922. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8923. DebugAssert(ColorItem != NULL);
  8924. ColorItem->Color = C;
  8925. NonVisualDataModule->ColorMenuItem->Color = C;
  8926. UpdateControls();
  8927. }
  8928. //---------------------------------------------------------------------------
  8929. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8930. {
  8931. if (value != FSessionColor)
  8932. {
  8933. UpdateSessionColor(value);
  8934. // Is null when called from LastTerminalClosed
  8935. if (Terminal != NULL)
  8936. {
  8937. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8938. }
  8939. }
  8940. }
  8941. //---------------------------------------------------------------------------
  8942. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8943. {
  8944. bool Result = FNoteTimer->Enabled;
  8945. if (Result)
  8946. {
  8947. // cannot cancel note too early
  8948. bool NotEarly =
  8949. (Now() - FNoteShown >
  8950. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8951. if (Force || NotEarly)
  8952. {
  8953. FNoteTimer->Enabled = false;
  8954. FNote = L"";
  8955. SAFE_DESTROY(FNoteData);
  8956. FOnNoteClick = NULL;
  8957. FNoteHints = FNotes->Text;
  8958. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8959. UpdateStatusBar();
  8960. }
  8961. }
  8962. return Result;
  8963. }
  8964. //---------------------------------------------------------------------------
  8965. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8966. {
  8967. DebugAssert(FNoteTimer->Enabled);
  8968. CancelNote(true);
  8969. }
  8970. //---------------------------------------------------------------------------
  8971. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  8972. {
  8973. FNoteHints = FNotes->Text.TrimRight();
  8974. }
  8975. //---------------------------------------------------------------------------
  8976. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8977. {
  8978. int P = Note.Pos(L"\n");
  8979. if (P > 0)
  8980. {
  8981. Note.SetLength(P - 1);
  8982. }
  8983. FNotes->Add(FORMAT(L"[%s] %s",
  8984. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8985. while (FNotes->Count > 10)
  8986. {
  8987. FNotes->Delete(0);
  8988. }
  8989. if (UpdateNow)
  8990. {
  8991. UpdateNoteHints();
  8992. UpdateStatusBar();
  8993. }
  8994. }
  8995. //---------------------------------------------------------------------------
  8996. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8997. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8998. {
  8999. int P = Note.Pos(L"\n");
  9000. if (P > 0)
  9001. {
  9002. Note.SetLength(P - 1);
  9003. }
  9004. UpdateNoteHints();
  9005. FNote = Note;
  9006. SAFE_DESTROY(FNoteData);
  9007. FNoteData = NoteData;
  9008. FOnNoteClick = OnNoteClick;
  9009. AddNote(Note, false);
  9010. UpdateStatusBar();
  9011. FNoteShown = Now();
  9012. FNoteTimer->Enabled = false;
  9013. if (Seconds == 0)
  9014. {
  9015. Seconds = WinConfiguration->NotificationsTimeout;
  9016. }
  9017. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9018. FNoteTimer->Enabled = true;
  9019. }
  9020. //---------------------------------------------------------------------------
  9021. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9022. {
  9023. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9024. }
  9025. //---------------------------------------------------------------------------
  9026. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9027. {
  9028. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9029. {
  9030. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9031. }
  9032. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9033. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9034. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9035. 0, NULL, NULL);
  9036. }
  9037. //---------------------------------------------------------------------------
  9038. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9039. {
  9040. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  9041. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  9042. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  9043. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9044. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9045. }
  9046. //---------------------------------------------------------------------------
  9047. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9048. {
  9049. WinConfiguration->FormatSizeBytes = Style;
  9050. }
  9051. //---------------------------------------------------------------------------
  9052. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9053. {
  9054. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9055. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9056. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9057. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9058. }
  9059. //---------------------------------------------------------------------------
  9060. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9061. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9062. {
  9063. if (Panel->Index == 0)
  9064. {
  9065. if (FOnNoteClick != NULL)
  9066. {
  9067. // prevent the user data from being freed by possible call
  9068. // to CancelNote or PostNote during call to OnNoteClick
  9069. std::unique_ptr<TObject> NoteData(FNoteData);
  9070. TNotifyEvent OnNoteClick = FOnNoteClick;
  9071. FNoteData = NULL;
  9072. // need to cancel the note as we are going to delete its user data
  9073. CancelNote(true);
  9074. OnNoteClick(NoteData.get());
  9075. }
  9076. }
  9077. if (HasActiveTerminal() &&
  9078. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9079. {
  9080. FileSystemInfo();
  9081. }
  9082. }
  9083. //---------------------------------------------------------------------------
  9084. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9085. {
  9086. // workaround:
  9087. // 1) for unknown reason, disabling window, while minimized,
  9088. // prevents it from restoring, even if it was enabled again meanwhile
  9089. // 2) when disabling the main window, while another has focus
  9090. // minimize is no longer possible
  9091. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9092. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9093. // Shouldn't we use IsApplicationMinimized() here?
  9094. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9095. {
  9096. Enabled = false;
  9097. }
  9098. FLockLevel++;
  9099. }
  9100. //---------------------------------------------------------------------------
  9101. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9102. {
  9103. DebugAssert(FLockLevel > 0);
  9104. FLockLevel--;
  9105. if (FLockLevel == 0)
  9106. {
  9107. DebugAssert(FLockSuspendLevel == 0);
  9108. Enabled = true;
  9109. }
  9110. }
  9111. //---------------------------------------------------------------------------
  9112. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9113. {
  9114. // We need to make sure that window is enabled when the last modal window closes
  9115. // otherwise focus is not restored correctly.
  9116. // So we re-enable the window when modal window opens and
  9117. // disable it back after is closes
  9118. if (FLockLevel > 0)
  9119. {
  9120. // while we have nesting counter, we know that we never be called
  9121. // recursivelly as Application->OnModalBegin is called only
  9122. // for the top-level modal window
  9123. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9124. {
  9125. // won't be disabled when conditions in LockWindow() were not satisfied
  9126. FDisabledOnLockSuspend = !Enabled;
  9127. // When minimized to tray (or actually when set to SW_HIDE),
  9128. // setting Enabled makes the window focusable even when there's
  9129. // modal window over it
  9130. if (!FTrayIcon->Visible)
  9131. {
  9132. Enabled = true;
  9133. }
  9134. }
  9135. FLockSuspendLevel++;
  9136. }
  9137. }
  9138. //---------------------------------------------------------------------------
  9139. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9140. {
  9141. if (FLockSuspendLevel > 0)
  9142. {
  9143. DebugAssert(FLockLevel > 0);
  9144. FLockSuspendLevel--;
  9145. // see comment in SuspendWindowLock
  9146. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9147. {
  9148. // Note that window can be enabled here, when we were minized to tray when
  9149. // was SuspendWindowLock() called.
  9150. // We should possibly do the same check as in LockWindow(),
  9151. // if it is ever possible that the consitions change between
  9152. // SuspendWindowLock() and ResumeWindowLock()
  9153. Enabled = !FDisabledOnLockSuspend;
  9154. }
  9155. }
  9156. }
  9157. //---------------------------------------------------------------------------
  9158. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9159. {
  9160. TTBXComboBoxItem * RemotePathComboBox =
  9161. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9162. if (!TextOnly)
  9163. {
  9164. TStrings * Items = RemotePathComboBox->Strings;
  9165. Items->BeginUpdate();
  9166. try
  9167. {
  9168. Items->Clear();
  9169. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9170. {
  9171. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9172. while (!IsUnixRootPath(APath))
  9173. {
  9174. int P = APath.LastDelimiter(L'/');
  9175. DebugAssert(P >= 0);
  9176. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9177. APath.SetLength(P - 1);
  9178. }
  9179. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9180. }
  9181. }
  9182. __finally
  9183. {
  9184. Items->EndUpdate();
  9185. }
  9186. }
  9187. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9188. // Setting ItemIndex to -1 does not reset its text
  9189. if (RemotePathComboBox->Strings->Count == 0)
  9190. {
  9191. RemotePathComboBox->Text = L"";
  9192. }
  9193. }
  9194. //---------------------------------------------------------------------------
  9195. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9196. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9197. {
  9198. if (AIndex < 0)
  9199. {
  9200. AIndex = Sender->ItemIndex;
  9201. }
  9202. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9203. }
  9204. //---------------------------------------------------------------------------
  9205. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9206. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9207. int & ImageIndex)
  9208. {
  9209. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9210. }
  9211. //---------------------------------------------------------------------------
  9212. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9213. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9214. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9215. {
  9216. if (!IsLocalBrowserMode())
  9217. {
  9218. ARect.Left += (10 * AIndex);
  9219. }
  9220. }
  9221. //---------------------------------------------------------------------------
  9222. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9223. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9224. {
  9225. AWidth += (10 * AIndex);
  9226. }
  9227. //---------------------------------------------------------------------------
  9228. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9229. {
  9230. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9231. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9232. int Index = RemotePathComboBox->ItemIndex;
  9233. while (Index < RemotePathComboBox->Strings->Count - 1)
  9234. {
  9235. APath = UnixExtractFileDir(APath);
  9236. Index++;
  9237. }
  9238. // VanDyke style paths
  9239. if (APath.IsEmpty())
  9240. {
  9241. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9242. APath = ROOTDIRECTORY;
  9243. }
  9244. if (RemoteDirView->Path != APath)
  9245. {
  9246. RemoteDirView->Path = APath;
  9247. }
  9248. }
  9249. //---------------------------------------------------------------------------
  9250. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9251. {
  9252. DoRemotePathComboBoxItemClick(Sender);
  9253. }
  9254. //---------------------------------------------------------------------------
  9255. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9256. {
  9257. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9258. DebugUsedParam(Sender);
  9259. UpdateRemotePathComboBox(true);
  9260. }
  9261. //---------------------------------------------------------------------------
  9262. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9263. {
  9264. DoRemotePathComboBoxCancel(Sender);
  9265. }
  9266. //---------------------------------------------------------------------------
  9267. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9268. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9269. {
  9270. ResetIncrementalSearch();
  9271. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9272. DebugAssert(DirView != NULL);
  9273. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9274. {
  9275. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9276. // OnEditing is called also from TCustomListView::CanEdit
  9277. if (Edit != NULL)
  9278. {
  9279. EditSelectBaseName(Edit);
  9280. }
  9281. }
  9282. }
  9283. //---------------------------------------------------------------------------
  9284. //---------------------------------------------------------------------------
  9285. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9286. {
  9287. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9288. Result->AutoDetectDnD = false;
  9289. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9290. Result->RenderDataOn = rdoEnterAndDropSync;
  9291. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9292. return Result;
  9293. }
  9294. //---------------------------------------------------------------------------
  9295. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9296. {
  9297. if (IsWin10Build(2004))
  9298. {
  9299. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9300. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9301. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9302. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9303. }
  9304. }
  9305. //---------------------------------------------------------------------------
  9306. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9307. {
  9308. TForm::CreateWnd();
  9309. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9310. UpdateDarkMode();
  9311. if (FSessionsDragDropFilesEx == NULL)
  9312. {
  9313. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9314. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9315. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9316. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9317. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9318. }
  9319. if (FQueueDragDropFilesEx == NULL)
  9320. {
  9321. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9322. // No need to set OnDragOver as we do not have any restrictions
  9323. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9324. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9325. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9326. }
  9327. }
  9328. //---------------------------------------------------------------------------
  9329. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9330. {
  9331. TForm::DestroyWnd();
  9332. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9333. FQueueDragDropFilesEx->DragDropControl = NULL;
  9334. }
  9335. //---------------------------------------------------------------------------
  9336. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9337. {
  9338. SideEnter(FCurrentSide);
  9339. FEverShown = true;
  9340. }
  9341. //---------------------------------------------------------------------------
  9342. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9343. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9344. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9345. {
  9346. if (!NonVisualDataModule->Busy)
  9347. {
  9348. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9349. Configuration->Usage->Inc(L"FileFinds");
  9350. LockWindow(true);
  9351. NonVisualDataModule->StartBusy();
  9352. WinConfiguration->LockedInterface = true;
  9353. try
  9354. {
  9355. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9356. }
  9357. __finally
  9358. {
  9359. WinConfiguration->LockedInterface = false;
  9360. NonVisualDataModule->EndBusy();
  9361. UnlockWindow();
  9362. }
  9363. }
  9364. }
  9365. //---------------------------------------------------------------------------
  9366. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9367. {
  9368. if (!NonVisualDataModule->Busy)
  9369. {
  9370. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9371. SetFocus();
  9372. RemoteDirView->Path = UnixExtractFilePath(Path);
  9373. UnicodeString FileName = UnixExtractFileName(Path);
  9374. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9375. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9376. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9377. if (Item == NULL)
  9378. {
  9379. ReloadDirectory(osRemote);
  9380. Item = RemoteDirView->FindFileItem(FileName);
  9381. }
  9382. if (Item != NULL)
  9383. {
  9384. RemoteDirView->ItemFocused = Item;
  9385. RemoteDirView->ItemFocused->MakeVisible(false);
  9386. RemoteDirView->SetFocus();
  9387. }
  9388. }
  9389. }
  9390. //---------------------------------------------------------------------------
  9391. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9392. {
  9393. bool Result = !NonVisualDataModule->Busy;
  9394. if (Result)
  9395. {
  9396. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9397. }
  9398. return Result;
  9399. }
  9400. //---------------------------------------------------------------------------
  9401. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9402. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9403. {
  9404. if (CanOperateOnFoundFiles(ATerminal))
  9405. {
  9406. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9407. FOnFileOperationFinished = OnFileOperationFinished;
  9408. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9409. }
  9410. }
  9411. //---------------------------------------------------------------------------
  9412. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9413. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9414. {
  9415. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9416. }
  9417. //---------------------------------------------------------------------------
  9418. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9419. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9420. {
  9421. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9422. }
  9423. //---------------------------------------------------------------------------
  9424. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9425. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9426. {
  9427. if (CanOperateOnFoundFiles(ATerminal))
  9428. {
  9429. for (int Index = 0; Index < FileList->Count; Index++)
  9430. {
  9431. UnicodeString FileName = FileList->Strings[Index];
  9432. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9433. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9434. OnFileOperationFinished(osRemote, FileName, true);
  9435. }
  9436. }
  9437. }
  9438. //---------------------------------------------------------------------------
  9439. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9440. {
  9441. FFileFindTerminal = Terminal;
  9442. ShowFileFindDialog(
  9443. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9444. DoDownloadFoundFiles, DoEditFoundFiles);
  9445. }
  9446. //---------------------------------------------------------------------------
  9447. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9448. {
  9449. FTaskbarList = TaskbarList;
  9450. }
  9451. //---------------------------------------------------------------------------
  9452. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9453. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9454. {
  9455. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9456. if (Index >= 0)
  9457. {
  9458. if (Button == mbLeft)
  9459. {
  9460. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9461. // If switching tab (switching session) takes long enough for user
  9462. // to actually release the button, "mouse down" is still raised,
  9463. // but we do not get "mouse up" event, so dragging is not cancelled,
  9464. // prevent that by not beginning dragging in the first place.
  9465. if (IsKeyPressed(VK_LBUTTON))
  9466. {
  9467. // when user clicks the "+", we get mouse down only after the session
  9468. // is closed, when new session tab is already on X:Y, so dragging
  9469. // starts, prevent that
  9470. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9471. {
  9472. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9473. if (Terminal != NULL)
  9474. {
  9475. SessionsPageControl->BeginDrag(false);
  9476. }
  9477. }
  9478. }
  9479. }
  9480. else if (Button == mbMiddle)
  9481. {
  9482. // ignore middle-click for "New session tab" and the last local-local tab
  9483. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9484. if ((Session != NULL) && CanCloseSession(Session))
  9485. {
  9486. CloseSessionTab(Index);
  9487. }
  9488. }
  9489. }
  9490. }
  9491. //---------------------------------------------------------------------------
  9492. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9493. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9494. {
  9495. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9496. // do not allow dropping on the "+" tab
  9497. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9498. if ((TargetTerminal != NULL) &&
  9499. (SessionsPageControl->ActivePage->PageIndex != Index))
  9500. {
  9501. Configuration->Usage->Inc(L"SessionTabMoves");
  9502. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9503. // but we want to actually prevent that to avoid flicker
  9504. SessionsPageControl->ActivePage->PageIndex = Index;
  9505. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9506. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9507. }
  9508. }
  9509. //---------------------------------------------------------------------------
  9510. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9511. TObject * Sender, TObject * Source, int X, int Y,
  9512. TDragState /*State*/, bool & Accept)
  9513. {
  9514. Accept = (Sender == Source);
  9515. if (Accept)
  9516. {
  9517. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9518. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9519. // do not allow dragging to the "+" tab
  9520. Accept = (Terminal != NULL);
  9521. }
  9522. }
  9523. //---------------------------------------------------------------------------
  9524. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9525. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9526. {
  9527. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9528. if (Index < 0)
  9529. {
  9530. Effect = DROPEFFECT_NONE;
  9531. }
  9532. else
  9533. {
  9534. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9535. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9536. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9537. {
  9538. Effect = DROPEFFECT_NONE;
  9539. }
  9540. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9541. {
  9542. Effect = DROPEFFECT_COPY;
  9543. }
  9544. }
  9545. }
  9546. //---------------------------------------------------------------------------
  9547. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9548. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9549. {
  9550. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9551. // do not allow dropping on local-local, disconnected and the "+" tab
  9552. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9553. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9554. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9555. {
  9556. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9557. if (!IsFileControl(DropSourceControl, osRemote))
  9558. {
  9559. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9560. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9561. // Why don't we use Terminal->CurrentDirectory directly?
  9562. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9563. // do not force queue, drag drop
  9564. false, true);
  9565. }
  9566. }
  9567. }
  9568. //---------------------------------------------------------------------------
  9569. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9570. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9571. {
  9572. // Downloads are handled in RemoteFileControlDDEnd
  9573. if (!IsFileControl(DropSourceControl, osRemote))
  9574. {
  9575. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9576. Terminal->CurrentDirectory,
  9577. // force queue, drag drop
  9578. true, true);
  9579. }
  9580. }
  9581. //---------------------------------------------------------------------------
  9582. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9583. {
  9584. FShowing = false;
  9585. // Do not save empty workspace
  9586. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9587. {
  9588. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9589. UnicodeString Name = WorkspaceName();
  9590. DoSaveWorkspace(
  9591. Name, DataList.get(),
  9592. !Configuration->DisablePasswordStoring &&
  9593. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9594. WinConfiguration->LastStoredSession = Name;
  9595. }
  9596. }
  9597. //---------------------------------------------------------------------------
  9598. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9599. {
  9600. TManagedTerminal * ManagedTerminal =
  9601. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9602. if (ManagedTerminal != NULL)
  9603. {
  9604. ManagedTerminal->DirectoryLoaded = Now();
  9605. }
  9606. }
  9607. //---------------------------------------------------------------------------
  9608. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9609. TListItem * /*Item*/, bool /*Selected*/)
  9610. {
  9611. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9612. switch (DirView->LastSelectMethod)
  9613. {
  9614. case smKeyboard:
  9615. Configuration->Usage->Inc(L"KeyboardSelections");
  9616. break;
  9617. case smMouse:
  9618. Configuration->Usage->Inc(L"MouseSelections");
  9619. break;
  9620. }
  9621. }
  9622. //---------------------------------------------------------------------------
  9623. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9624. {
  9625. if (Color != 0)
  9626. {
  9627. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9628. }
  9629. else
  9630. {
  9631. return FSessionTabImageIndex;
  9632. }
  9633. }
  9634. //---------------------------------------------------------------------------
  9635. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9636. {
  9637. TPngImageCollectionItem * Item =
  9638. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9639. FSessionColors->AddPng(Item->PngImage);
  9640. return FSessionColors->Count - 1;
  9641. }
  9642. //---------------------------------------------------------------------------
  9643. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9644. {
  9645. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9646. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  9647. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  9648. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9649. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9650. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  9651. }
  9652. //---------------------------------------------------------------------------
  9653. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  9654. {
  9655. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  9656. }
  9657. //---------------------------------------------------------------------------
  9658. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  9659. {
  9660. switch (Side)
  9661. {
  9662. case osLocal:
  9663. return FNewLocalTabTabImageIndex;
  9664. case osRemote:
  9665. case osCurrent:
  9666. return FNewRemoteTabTabImageIndex;
  9667. default:
  9668. DebugFail();
  9669. return -1;
  9670. }
  9671. }
  9672. //---------------------------------------------------------------------------
  9673. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9674. {
  9675. for (int Index = 0; Index < Control->ControlCount; Index++)
  9676. {
  9677. TControl * AControl = Control->Controls[Index];
  9678. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9679. if (WinControl != NULL)
  9680. {
  9681. CollectItemsWithTextDisplayMode(WinControl);
  9682. }
  9683. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9684. if (Toolbar != NULL)
  9685. {
  9686. // we care for top-level items only
  9687. TTBCustomItem * Items = Toolbar->Items;
  9688. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9689. {
  9690. TTBCustomItem * Item = Items->Items[ItemIndex];
  9691. if (((Item->DisplayMode == nbdmImageAndText) ||
  9692. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9693. EligibleForImageDisplayMode(Item))
  9694. {
  9695. FItemsWithTextDisplayMode.insert(Item);
  9696. }
  9697. }
  9698. }
  9699. }
  9700. }
  9701. //---------------------------------------------------------------------------
  9702. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9703. {
  9704. return true;
  9705. }
  9706. //---------------------------------------------------------------------------
  9707. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9708. {
  9709. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9710. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9711. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9712. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9713. bool Result = true;
  9714. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9715. {
  9716. TTBCustomItem * Item = *i;
  9717. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9718. if (Label != NULL)
  9719. {
  9720. // optimization
  9721. if (Label->Visible == SelectiveToolbarText)
  9722. {
  9723. Result = false;
  9724. }
  9725. else
  9726. {
  9727. Label->Visible = SelectiveToolbarText;
  9728. }
  9729. }
  9730. else
  9731. {
  9732. // optimization
  9733. if (Item->DisplayMode == DisplayMode)
  9734. {
  9735. Result = false;
  9736. }
  9737. else
  9738. {
  9739. Item->DisplayMode = DisplayMode;
  9740. }
  9741. }
  9742. ++i;
  9743. }
  9744. UpdateNewTabTab();
  9745. return Result;
  9746. }
  9747. //---------------------------------------------------------------------------
  9748. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9749. {
  9750. DebugFail();
  9751. }
  9752. //---------------------------------------------------------------------------
  9753. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9754. {
  9755. // Among other, a lock level is non zero, while directory is being loaded,
  9756. // even when it happens as a result of dirview navigation,
  9757. // i.e. when TNonVisualDataModule is NOT FBusy.
  9758. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9759. // Among other this prevents a panel auto update to occur while
  9760. // directory is changing.
  9761. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9762. }
  9763. //---------------------------------------------------------------------------
  9764. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  9765. {
  9766. // While the window is disabled, we still seem to process menu shortcuts at least,
  9767. // so stop it at least here.
  9768. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9769. bool Result;
  9770. if (Allowed == aaUpdate)
  9771. {
  9772. Result = true;
  9773. }
  9774. else
  9775. {
  9776. Result = !NonVisualDataModule->Busy;
  9777. if (Result && (Allowed == aaShortCut))
  9778. {
  9779. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  9780. Action->Update();
  9781. Result = Action->Enabled;
  9782. }
  9783. }
  9784. return Result;
  9785. }
  9786. //---------------------------------------------------------------------------
  9787. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9788. {
  9789. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9790. }
  9791. //---------------------------------------------------------------------------
  9792. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9793. {
  9794. // This is somewhat redundant to LockWindow() call from
  9795. // TTerminalManager::TerminalReadDirectoryProgress.
  9796. // But disabling window is known not to block keyboard shorcuts
  9797. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9798. if (Busy > 0)
  9799. {
  9800. if (NonVisualDataModule->Busy)
  9801. {
  9802. State = false;
  9803. }
  9804. else
  9805. {
  9806. NonVisualDataModule->StartBusy();
  9807. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9808. LockWindow();
  9809. }
  9810. }
  9811. else if (Busy < 0)
  9812. {
  9813. UnlockWindow();
  9814. NonVisualDataModule->EndBusy();
  9815. }
  9816. else
  9817. {
  9818. State = NonVisualDataModule->Busy;
  9819. }
  9820. }
  9821. //---------------------------------------------------------------------------
  9822. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9823. {
  9824. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9825. if (Index >= 0)
  9826. {
  9827. TPopupMenu * PopupMenu = NULL;
  9828. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9829. if (Session != NULL)
  9830. {
  9831. SessionsPageControl->ActivePageIndex = Index;
  9832. if (DebugAlwaysTrue(SessionTabSwitched()))
  9833. {
  9834. // copied from TControl.WMContextMenu
  9835. SendCancelMode(SessionsPageControl);
  9836. // explicit popup instead of using PopupMenu property
  9837. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9838. // while connecting yet not-connected session and hence
  9839. // allowing an access to commands over not-completelly connected session
  9840. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  9841. }
  9842. }
  9843. else
  9844. {
  9845. if (SupportsLocalBrowser())
  9846. {
  9847. PopupMenu = NonVisualDataModule->NewTabPopup;
  9848. }
  9849. }
  9850. if (PopupMenu != NULL)
  9851. {
  9852. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9853. PopupMenu->PopupComponent = SessionsPageControl;
  9854. PopupMenu->Popup(Point.x, Point.y);
  9855. }
  9856. }
  9857. Handled = true;
  9858. }
  9859. //---------------------------------------------------------------------------
  9860. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9861. {
  9862. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  9863. }
  9864. //---------------------------------------------------------------------------
  9865. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9866. {
  9867. Configuration->Usage->Inc(L"PasswordChanges");
  9868. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9869. ChangePasswordData->DisableAuthentationsExceptPassword();
  9870. ChangePasswordData->ChangePassword = true;
  9871. std::unique_ptr<TTerminal> ChangePasswordSession(
  9872. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9873. ChangePasswordSession->AutoReadDirectory = false;
  9874. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9875. {
  9876. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9877. }
  9878. else
  9879. {
  9880. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9881. }
  9882. }
  9883. //---------------------------------------------------------------------------
  9884. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9885. {
  9886. // No nice way to assert SSH2
  9887. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  9888. }
  9889. //---------------------------------------------------------------------------
  9890. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9891. {
  9892. TOperationVisualizer OperationVisualizer;
  9893. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9894. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9895. }
  9896. //---------------------------------------------------------------------------
  9897. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9898. {
  9899. TForm::ChangeScale(M, D);
  9900. int APixelsPerInch = GetControlPixelsPerInch(this);
  9901. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9902. try
  9903. {
  9904. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9905. }
  9906. __finally
  9907. {
  9908. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9909. }
  9910. }
  9911. //---------------------------------------------------------------------------
  9912. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9913. {
  9914. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9915. }
  9916. //---------------------------------------------------------------------
  9917. class TPublicControl : public TControl
  9918. {
  9919. friend class TCustomScpExplorerForm;
  9920. };
  9921. //---------------------------------------------------------------------------
  9922. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9923. {
  9924. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9925. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9926. }
  9927. //---------------------------------------------------------------------------
  9928. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9929. {
  9930. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9931. OpenBookmark(Side, Bookmark);
  9932. }
  9933. //---------------------------------------------------------------------------
  9934. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9935. {
  9936. DoBookmarkClick(osLocal, Sender);
  9937. }
  9938. //---------------------------------------------------------------------------
  9939. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9940. {
  9941. DoBookmarkClick(osRemote, Sender);
  9942. }
  9943. //---------------------------------------------------------------------------
  9944. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9945. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9946. {
  9947. if (BookmarkList != NULL)
  9948. {
  9949. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9950. if (IsLocalBrowserMode())
  9951. {
  9952. Side = osLocal;
  9953. }
  9954. if (!WinConfiguration->UseLocationProfiles)
  9955. {
  9956. std::unique_ptr<TStringList> Directories(new TStringList());
  9957. Directories->CaseSensitive = (Side == osRemote);
  9958. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9959. {
  9960. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9961. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9962. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9963. {
  9964. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9965. Item->Caption = EscapeHotkey(Directory);
  9966. Item->ShortCut = Bookmark->ShortCut;
  9967. Item->OnClick = OnBookmarkClick;
  9968. Item->Tag = reinterpret_cast<int>(Bookmark);
  9969. Directories->Add(Directory);
  9970. Menu->Add(Item.release());
  9971. }
  9972. }
  9973. }
  9974. else
  9975. {
  9976. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9977. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9978. {
  9979. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9980. if (!Bookmark->Node.IsEmpty())
  9981. {
  9982. Folders->Add(Bookmark->Node);
  9983. }
  9984. }
  9985. for (int Index = 0; Index < Folders->Count; Index++)
  9986. {
  9987. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9988. Item->Caption = Folders->Strings[Index];
  9989. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  9990. Folders->Objects[Index] = Item.get();
  9991. Menu->Add(Item.release());
  9992. }
  9993. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9994. {
  9995. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9996. TTBCustomItem * Parent;
  9997. if (!Bookmark->Node.IsEmpty())
  9998. {
  9999. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10000. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10001. }
  10002. else
  10003. {
  10004. Parent = Menu;
  10005. }
  10006. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10007. Item->Caption = Bookmark->Name;
  10008. Item->ShortCut = Bookmark->ShortCut;
  10009. Item->OnClick = OnBookmarkClick;
  10010. Item->Tag = reinterpret_cast<int>(Bookmark);
  10011. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10012. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10013. {
  10014. // No hint for location profiles that are actually mere "bookmarks"
  10015. }
  10016. else
  10017. {
  10018. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10019. UnicodeString LongHint;
  10020. if (!Bookmark->Local.IsEmpty())
  10021. {
  10022. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10023. }
  10024. if (!Bookmark->Remote.IsEmpty())
  10025. {
  10026. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10027. }
  10028. AddToList(Hint, LongHint, L"|");
  10029. Item->Hint = Hint;
  10030. }
  10031. Parent->Add(Item.release());
  10032. }
  10033. }
  10034. AddMenuSeparator(Menu);
  10035. }
  10036. }
  10037. //---------------------------------------------------------------------------
  10038. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10039. {
  10040. Menu->Clear();
  10041. std::unique_ptr<TTBCustomItem> Item;
  10042. Item.reset(new TTBXItem(Owner));
  10043. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10044. Item->Options = Item->Options << tboDefault;
  10045. Menu->Add(Item.release());
  10046. AddMenuSeparator(Menu);
  10047. if (Terminal != NULL)
  10048. {
  10049. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10050. }
  10051. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10052. Item.reset(new TTBXItem(Owner));
  10053. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10054. Menu->Add(Item.release());
  10055. }
  10056. //---------------------------------------------------------------------------
  10057. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10058. {
  10059. bool Result = true;
  10060. bool Remote = !IsSideLocalBrowser(Side);
  10061. try
  10062. {
  10063. if (Remote)
  10064. {
  10065. Terminal->ExceptionOnFail = true;
  10066. }
  10067. try
  10068. {
  10069. DirView(Side)->Path = Path;
  10070. }
  10071. __finally
  10072. {
  10073. if (Remote)
  10074. {
  10075. Terminal->ExceptionOnFail = false;
  10076. }
  10077. }
  10078. }
  10079. catch (Exception & E)
  10080. {
  10081. if (!Remote || Terminal->Active)
  10082. {
  10083. ShowExtendedExceptionEx(Terminal, &E);
  10084. Result = false;
  10085. }
  10086. else
  10087. {
  10088. throw;
  10089. }
  10090. }
  10091. return Result;
  10092. }
  10093. //---------------------------------------------------------------------------
  10094. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10095. {
  10096. // Make sure there some feedback even when loading tiny local folders
  10097. TInstantOperationVisualizer Visualizer;
  10098. DirView(Side)->ReloadDirectory();
  10099. }
  10100. //---------------------------------------------------------------------------
  10101. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10102. {
  10103. if (Index == SessionsPageControl->TabIndex)
  10104. {
  10105. CloseSession();
  10106. }
  10107. else
  10108. {
  10109. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10110. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10111. TTerminalManager * Manager = TTerminalManager::Instance();
  10112. if (!Terminal->Active)
  10113. {
  10114. Manager->FreeTerminal(Terminal);
  10115. }
  10116. else
  10117. {
  10118. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10119. if (CanCloseQueue(Queue))
  10120. {
  10121. Manager->FreeTerminal(Terminal);
  10122. }
  10123. }
  10124. }
  10125. }
  10126. //---------------------------------------------------------------------------
  10127. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10128. {
  10129. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10130. if (Session == NULL)
  10131. {
  10132. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10133. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10134. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10135. //P = SessionsPageControl->ClientToScreen(P);
  10136. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10137. }
  10138. else
  10139. {
  10140. CloseSessionTab(Index);
  10141. }
  10142. }
  10143. //---------------------------------------------------------------------------
  10144. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10145. {
  10146. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10147. {
  10148. TInstantOperationVisualizer Visualizer;
  10149. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10150. // before we create a new data object
  10151. ClipboardClear();
  10152. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10153. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10154. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10155. // Intermediate variable to WORKAROUND a crash in compiler
  10156. TObjectList * ClipboardFakeMonitors =
  10157. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10158. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10159. if (FClipboardDragDropFilesEx.get() == NULL)
  10160. {
  10161. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10162. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10163. }
  10164. FClipboardDragDropFilesEx->FileList->Clear();
  10165. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10166. FClipboardDragDropFilesEx->CopyToClipboard();
  10167. FClipboardTerminal = Terminal;
  10168. // Need full paths, as cwd can be different once files are pasted
  10169. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10170. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10171. {
  10172. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10173. }
  10174. }
  10175. }
  10176. //---------------------------------------------------------------------------
  10177. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10178. {
  10179. if (OpenClipboard(0))
  10180. {
  10181. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10182. CloseClipboard();
  10183. }
  10184. }
  10185. //---------------------------------------------------------------------------
  10186. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10187. {
  10188. return !FClipboardFakeDirectory.IsEmpty();
  10189. }
  10190. //---------------------------------------------------------------------------
  10191. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10192. {
  10193. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10194. FClipboardFakeDirectory = UnicodeString();
  10195. FClipboardTerminal = NULL;
  10196. // Also called in Idle()
  10197. FClipboardFakeMonitors.reset(NULL);
  10198. }
  10199. //---------------------------------------------------------------------------
  10200. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10201. {
  10202. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10203. {
  10204. ClipboardStop();
  10205. }
  10206. }
  10207. //---------------------------------------------------------------------------
  10208. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10209. {
  10210. TTerminalManager * Manager = TTerminalManager::Instance();
  10211. Manager->ActiveSession = FClipboardTerminal;
  10212. TTransferOperationParam Params;
  10213. Params.TargetDirectory = TargetDirectory;
  10214. Params.DragDrop = DragDrop;
  10215. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10216. }
  10217. //---------------------------------------------------------------------------
  10218. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10219. {
  10220. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  10221. // (as it pumps a Windows message queue while being shutted down)
  10222. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10223. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10224. {
  10225. // Can fail as it can be e.g. locked by AV, so we retry that later
  10226. bool Removed = RemoveDir(ApiPath(FileName));
  10227. try
  10228. {
  10229. if (!NonVisualDataModule->Busy)
  10230. {
  10231. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10232. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10233. }
  10234. }
  10235. __finally
  10236. {
  10237. if (!Removed)
  10238. {
  10239. RemoveDir(ApiPath(FileName));
  10240. }
  10241. }
  10242. }
  10243. }
  10244. //---------------------------------------------------------------------------
  10245. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10246. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10247. {
  10248. TFileMasks::TParams MaskParams;
  10249. MaskParams.Size = Size;
  10250. MaskParams.Modification = Modification;
  10251. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10252. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10253. {
  10254. bool ImplicitMatch;
  10255. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  10256. !ImplicitMatch)
  10257. {
  10258. Color = Iter->Color;
  10259. break;
  10260. }
  10261. }
  10262. }
  10263. //---------------------------------------------------------------------------
  10264. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10265. {
  10266. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10267. if (!DirView->IsEditing())
  10268. {
  10269. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10270. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10271. // while searching
  10272. if (Key == VK_BACK)
  10273. {
  10274. if (!FIncrementalSearch.IsEmpty())
  10275. {
  10276. if (FIncrementalSearch.Length() == 1)
  10277. {
  10278. ResetIncrementalSearch();
  10279. }
  10280. else
  10281. {
  10282. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10283. IncrementalSearch(NewText, false, false);
  10284. }
  10285. Key = 0;
  10286. }
  10287. }
  10288. }
  10289. }
  10290. //---------------------------------------------------------------------------
  10291. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10292. {
  10293. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10294. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10295. {
  10296. // Filter control sequences.
  10297. // When not searching yet, prefer use of the space for toggling file selection
  10298. // (so we cannot incrementally search for a string starting with a space).
  10299. if ((Key > VK_SPACE) ||
  10300. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10301. {
  10302. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10303. Key = 0;
  10304. }
  10305. }
  10306. }
  10307. //---------------------------------------------------------------------------
  10308. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10309. {
  10310. if (!FIncrementalSearch.IsEmpty())
  10311. {
  10312. FIncrementalSearch = L"";
  10313. DirView(osCurrent)->UpdateStatusBar();
  10314. }
  10315. }
  10316. //---------------------------------------------------------------------------
  10317. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10318. {
  10319. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10320. if (Item == NULL)
  10321. {
  10322. MessageBeep(MB_ICONHAND);
  10323. }
  10324. else
  10325. {
  10326. TCustomDirView * ADirView = DirView(osCurrent);
  10327. {
  10328. TAutoNestingCounter Guard(FIncrementalSearching);
  10329. ADirView->FocusItem(Item);
  10330. if (ADirView->NortonLike == nlOff)
  10331. {
  10332. if ((ADirView->Selected != Item) ||
  10333. (ADirView->SelCount > 1))
  10334. {
  10335. ADirView->ClearSelection();
  10336. ADirView->Selected = Item;
  10337. }
  10338. }
  10339. }
  10340. FIncrementalSearch = Text;
  10341. Item->MakeVisible(false);
  10342. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10343. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10344. ADirView->UpdateStatusBar();
  10345. }
  10346. }
  10347. //---------------------------------------------------------------------------
  10348. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10349. {
  10350. int Index = Item->Index;
  10351. if (!Reverse)
  10352. {
  10353. Index++;
  10354. if (Index >= Item->Owner->Count)
  10355. {
  10356. Index = 0;
  10357. }
  10358. }
  10359. else
  10360. {
  10361. Index--;
  10362. if (Index < 0)
  10363. {
  10364. Index = Item->Owner->Count - 1;
  10365. }
  10366. }
  10367. return Item->Owner->Item[Index];
  10368. }
  10369. //---------------------------------------------------------------------------
  10370. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10371. {
  10372. TCustomDirView * ADirView = DirView(osCurrent);
  10373. if (ADirView->Items->Count == 0)
  10374. {
  10375. return NULL;
  10376. }
  10377. else
  10378. {
  10379. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10380. TListItem * Item = CurrentItem;
  10381. if (SkipCurrent)
  10382. {
  10383. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10384. }
  10385. while (true)
  10386. {
  10387. bool Matches = false;
  10388. switch (WinConfiguration->PanelSearch)
  10389. {
  10390. case isNameStartOnly:
  10391. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  10392. break;
  10393. case isName:
  10394. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  10395. break;
  10396. case isAll:
  10397. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10398. int Index = 0;
  10399. while ((Index < ColCount) && !Matches)
  10400. {
  10401. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10402. if (Visible)
  10403. {
  10404. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10405. }
  10406. Index++;
  10407. }
  10408. break;
  10409. }
  10410. if (Matches)
  10411. {
  10412. return Item;
  10413. }
  10414. Item = GetNextFile(Item, Reverse);
  10415. if (Item == CurrentItem)
  10416. {
  10417. return NULL;
  10418. }
  10419. }
  10420. }
  10421. }
  10422. //---------------------------------------------------------------------------
  10423. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10424. {
  10425. ResetIncrementalSearch();
  10426. }
  10427. //---------------------------------------------------------------------------
  10428. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10429. {
  10430. if (Message.CharCode == VK_TAB)
  10431. {
  10432. if (!FIncrementalSearch.IsEmpty())
  10433. {
  10434. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10435. bool Reverse = Shift.Contains(ssShift);
  10436. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10437. Message.Result = 1;
  10438. return;
  10439. }
  10440. }
  10441. else if (Message.CharCode == VK_ESCAPE)
  10442. {
  10443. if (!FIncrementalSearch.IsEmpty())
  10444. {
  10445. ResetIncrementalSearch();
  10446. Message.Result = 1;
  10447. return;
  10448. }
  10449. }
  10450. TForm::Dispatch(&Message);
  10451. }
  10452. //---------------------------------------------------------------------------
  10453. void __fastcall TCustomScpExplorerForm::Deactivate()
  10454. {
  10455. TForm::Deactivate();
  10456. // E.g. when switching to an internal editor window
  10457. ResetIncrementalSearch();
  10458. }
  10459. //---------------------------------------------------------------------------
  10460. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10461. {
  10462. // When switching to another application
  10463. ResetIncrementalSearch();
  10464. }
  10465. //---------------------------------------------------------------------------
  10466. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10467. {
  10468. ResetIncrementalSearch();
  10469. }
  10470. //---------------------------------------------------------------------------
  10471. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10472. {
  10473. if (FIncrementalSearching <= 0)
  10474. {
  10475. ResetIncrementalSearch();
  10476. }
  10477. }
  10478. //---------------------------------------------------------------------------
  10479. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10480. {
  10481. CountClicksForWindowPrint(this);
  10482. }
  10483. //---------------------------------------------------------------------------
  10484. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10485. {
  10486. CenterReconnectToolbar();
  10487. }
  10488. //---------------------------------------------------------------------------
  10489. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10490. {
  10491. if (RemoteDirView->ItemFocused != NULL)
  10492. {
  10493. RemoteDirView->ItemFocused->Selected = true;
  10494. }
  10495. }
  10496. //---------------------------------------------------------------------------
  10497. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10498. {
  10499. if (!FQueueStatusUpdating)
  10500. {
  10501. bool Refresh;
  10502. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10503. {
  10504. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10505. }
  10506. else
  10507. {
  10508. if (FQueueFileList->GetCount() > 0)
  10509. {
  10510. FQueueFileList->Clear();
  10511. Refresh = true;
  10512. }
  10513. }
  10514. int Count = FQueueFileList->GetCount();
  10515. if (QueueFileList->Items->Count != Count)
  10516. {
  10517. DebugAssert(Refresh);
  10518. QueueFileList->Items->Count = Count;
  10519. }
  10520. if (Refresh)
  10521. {
  10522. QueueFileList->Invalidate();
  10523. }
  10524. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10525. }
  10526. }
  10527. //---------------------------------------------------------------------------
  10528. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10529. {
  10530. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10531. }
  10532. //---------------------------------------------------------------------------
  10533. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10534. {
  10535. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10536. // See TSynchronizeChecklistDialog::UpdateTimer
  10537. if (QueueFileList->Items->Count > 0)
  10538. {
  10539. QueueFileList->Items->Item[0]->MakeVisible(false);
  10540. }
  10541. UpdateQueueFileList();
  10542. }
  10543. //---------------------------------------------------------------------------
  10544. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10545. {
  10546. Active = QueueView3->Focused() || QueueFileList->Focused();
  10547. }
  10548. //---------------------------------------------------------------------------
  10549. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10550. {
  10551. QueueLabelUpdateStatus();
  10552. }
  10553. //---------------------------------------------------------------------------
  10554. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10555. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10556. {
  10557. int State = FQueueFileList->GetState(Item->Index);
  10558. if (State == qfsProcessed)
  10559. {
  10560. Sender->Canvas->Font->Color = clGrayText;
  10561. }
  10562. }
  10563. //---------------------------------------------------------------------------
  10564. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10565. {
  10566. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10567. QueueFileList->Column[0]->Width =
  10568. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10569. }
  10570. //---------------------------------------------------------------------------
  10571. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10572. {
  10573. QueueFileListColumnAutoSize();
  10574. }
  10575. //---------------------------------------------------------------------------
  10576. void __fastcall TCustomScpExplorerForm::CloseApp()
  10577. {
  10578. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10579. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10580. // So we clear the flag here.
  10581. DebugAssert(NonVisualDataModule->Busy);
  10582. NonVisualDataModule->EndBusy();
  10583. try
  10584. {
  10585. Close();
  10586. }
  10587. __finally
  10588. {
  10589. NonVisualDataModule->StartBusy();
  10590. }
  10591. }
  10592. //---------------------------------------------------------------------------
  10593. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10594. {
  10595. return (Terminal != NULL) && Terminal->Active;
  10596. }
  10597. //---------------------------------------------------------------------------
  10598. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  10599. {
  10600. return (ManagedSession != NULL);
  10601. }
  10602. //---------------------------------------------------------------------------
  10603. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10604. {
  10605. return IsActiveTerminal(Terminal);
  10606. }
  10607. //---------------------------------------------------------------------------
  10608. void TCustomScpExplorerForm::LocalLocalCopy(
  10609. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  10610. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  10611. {
  10612. DebugFail();
  10613. }
  10614. //---------------------------------------------------------------------------
  10615. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  10616. {
  10617. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  10618. }
  10619. //---------------------------------------------------------------------------
  10620. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  10621. {
  10622. // Changing tab list triggers the OnResize.
  10623. if (!FUpdatingSessionTabs)
  10624. {
  10625. // UpdateTabsCaptionTruncation should be enough
  10626. SessionListChanged(true);
  10627. }
  10628. }
  10629. //---------------------------------------------------------------------------
  10630. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  10631. {
  10632. if (ASession == ManagedSession)
  10633. {
  10634. UpdateSession(ASession);
  10635. }
  10636. TSessionData * Data = ASession->StateData;
  10637. UnicodeString Result;
  10638. if (Side == osLocal)
  10639. {
  10640. Result = Data->LocalDirectory;
  10641. }
  10642. else if (DebugAlwaysTrue(Side == osOther))
  10643. {
  10644. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  10645. }
  10646. return Result;
  10647. }
  10648. //---------------------------------------------------------------------------
  10649. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  10650. {
  10651. UnicodeString Result;
  10652. if (!ASession->Active)
  10653. {
  10654. Result = LoadStr(STATUS_NOT_CONNECTED2);
  10655. }
  10656. else
  10657. {
  10658. Result = GetSessionPath(ASession, osRemote);
  10659. }
  10660. return Result;
  10661. }
  10662. //---------------------------------------------------------------------------
  10663. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  10664. {
  10665. return LoadStr(NEW_REMOTE_TAB_HINT);
  10666. }
  10667. //---------------------------------------------------------------------------
  10668. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  10669. {
  10670. if (Index >= 0)
  10671. {
  10672. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10673. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  10674. if (TabSession == NULL)
  10675. {
  10676. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  10677. if (NewTabTabCaption != TabSheet->Caption)
  10678. {
  10679. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  10680. }
  10681. }
  10682. else
  10683. {
  10684. TTerminalManager * Manager = TTerminalManager::Instance();
  10685. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  10686. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  10687. }
  10688. }
  10689. }
  10690. //---------------------------------------------------------------------------
  10691. typedef std::vector<TListItem *> TListItemsVector;
  10692. struct TCalculateSizeOperation
  10693. {
  10694. TCalculateSizeStats Stats;
  10695. TListItemsVector ListItems;
  10696. size_t Index;
  10697. };
  10698. //---------------------------------------------------------------------------
  10699. void TCustomScpExplorerForm::CalculateDirectorySizes()
  10700. {
  10701. TCalculateSizeOperation CalculateSizeOperation;
  10702. TOperationSide Side = GetSide(osCurrent);
  10703. TCustomDirView * ADirView = DirView(Side);
  10704. bool FullPath = IsSideLocalBrowser(Side);
  10705. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, FullPath, NULL, true));
  10706. std::unique_ptr<TStrings> FileList(new TStringList());
  10707. for (int Index = 0; Index < AllFileList->Count; Index++)
  10708. {
  10709. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  10710. if (ADirView->ItemIsDirectory(Item))
  10711. {
  10712. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  10713. CalculateSizeOperation.ListItems.push_back(Item);
  10714. }
  10715. }
  10716. CalculateSizeOperation.Index = 0;
  10717. __int64 Size = 0;
  10718. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation);
  10719. FCalculateSizeOperation = &CalculateSizeOperation;
  10720. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  10721. FOnFileOperationFinished = DirectorySizeCalculated;
  10722. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation);
  10723. FPrimaryOperation = foCalculateSize;
  10724. TCalculatedSizes CalculatedSizes;
  10725. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  10726. if (Side == osLocal)
  10727. {
  10728. Terminal->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  10729. }
  10730. else
  10731. {
  10732. TCalculateSizeParams Params;
  10733. Params.Stats = &CalculateSizeOperation.Stats;
  10734. Params.UseCache = true;
  10735. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  10736. }
  10737. }
  10738. //---------------------------------------------------------------------------
  10739. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(TOperationSide Side, const UnicodeString & FileName, bool)
  10740. {
  10741. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  10742. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  10743. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  10744. // optimization
  10745. bool GotExpectedItem =
  10746. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  10747. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  10748. int Index = -1;
  10749. if (DebugAlwaysTrue(GotExpectedItem))
  10750. {
  10751. Index = FCalculateSizeOperation->Index;
  10752. FCalculateSizeOperation->Index++;
  10753. }
  10754. else
  10755. {
  10756. TListItemsVector::iterator I =
  10757. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  10758. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  10759. {
  10760. Index = I - FCalculateSizeOperation->ListItems.begin();
  10761. }
  10762. }
  10763. if (DebugAlwaysTrue(Index >= 0))
  10764. {
  10765. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  10766. ADirView->SetItemCalculatedSize(Item, Size);
  10767. }
  10768. }